2 * Common routines for ASN.1
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
20 #include <epan/packet.h>
24 void asn1_ctx_init(asn1_ctx_t
*actx
, asn1_enc_e encoding
, bool aligned
, packet_info
*pinfo
) {
25 memset(actx
, '\0', sizeof(*actx
));
26 actx
->signature
= ASN1_CTX_SIGNATURE
;
27 actx
->encoding
= encoding
;
28 actx
->aligned
= aligned
;
32 bool asn1_ctx_check_signature(asn1_ctx_t
*actx
) {
33 return actx
&& (actx
->signature
== ASN1_CTX_SIGNATURE
);
36 void asn1_ctx_clean_external(asn1_ctx_t
*actx
) {
37 memset(&actx
->external
, '\0', sizeof(actx
->external
));
38 actx
->external
.hf_index
= -1;
39 actx
->external
.encoding
= -1;
42 void asn1_ctx_clean_epdv(asn1_ctx_t
*actx
) {
43 memset(&actx
->embedded_pdv
, '\0', sizeof(actx
->embedded_pdv
));
44 actx
->embedded_pdv
.hf_index
= -1;
45 actx
->embedded_pdv
.identification
= -1;
49 /*--- stack/parameters ---*/
51 void asn1_stack_frame_push(asn1_ctx_t
*actx
, const char *name
) {
52 asn1_stack_frame_t
*frame
;
54 frame
= wmem_new0(actx
->pinfo
->pool
, asn1_stack_frame_t
);
56 frame
->next
= actx
->stack
;
60 void asn1_stack_frame_pop(asn1_ctx_t
*actx
, const char *name
) {
61 DISSECTOR_ASSERT(actx
->stack
);
62 DISSECTOR_ASSERT(!strcmp(actx
->stack
->name
, name
));
63 actx
->stack
= actx
->stack
->next
;
66 void asn1_stack_frame_check(asn1_ctx_t
*actx
, const char *name
, const asn1_par_def_t
*par_def
) {
67 const asn1_par_def_t
*pd
= par_def
;
70 DISSECTOR_ASSERT(actx
->stack
);
71 DISSECTOR_ASSERT(!strcmp(actx
->stack
->name
, name
));
73 par
= actx
->stack
->par
;
75 DISSECTOR_ASSERT(par
);
76 DISSECTOR_ASSERT((pd
->ptype
== ASN1_PAR_IRR
) || (par
->ptype
== pd
->ptype
));
81 DISSECTOR_ASSERT(!par
);
84 static asn1_par_t
*get_par_by_name(asn1_ctx_t
*actx
, const char *name
) {
85 asn1_par_t
*par
= NULL
;
87 DISSECTOR_ASSERT(actx
->stack
);
88 par
= actx
->stack
->par
;
90 if (!strcmp(par
->name
, name
))
97 static asn1_par_t
*push_new_par(asn1_ctx_t
*actx
) {
98 asn1_par_t
*par
, **pp
;
100 DISSECTOR_ASSERT(actx
->stack
);
102 par
= wmem_new0(actx
->pinfo
->pool
, asn1_par_t
);
104 pp
= &(actx
->stack
->par
);
112 void asn1_param_push_boolean(asn1_ctx_t
*actx
, bool value
) {
115 par
= push_new_par(actx
);
116 par
->ptype
= ASN1_PAR_BOOLEAN
;
117 par
->value
.v_boolean
= value
;
120 void asn1_param_push_integer(asn1_ctx_t
*actx
, int32_t value
) {
123 par
= push_new_par(actx
);
124 par
->ptype
= ASN1_PAR_INTEGER
;
125 par
->value
.v_integer
= value
;
128 bool asn1_param_get_boolean(asn1_ctx_t
*actx
, const char *name
) {
129 asn1_par_t
*par
= NULL
;
131 par
= get_par_by_name(actx
, name
);
132 DISSECTOR_ASSERT(par
);
133 return par
->value
.v_boolean
;
136 int32_t asn1_param_get_integer(asn1_ctx_t
*actx
, const char *name
) {
137 asn1_par_t
*par
= NULL
;
139 par
= get_par_by_name(actx
, name
);
140 DISSECTOR_ASSERT(par
);
141 return par
->value
.v_integer
;
147 void rose_ctx_init(rose_ctx_t
*rctx
) {
148 memset(rctx
, '\0', sizeof(*rctx
));
149 rctx
->signature
= ROSE_CTX_SIGNATURE
;
152 bool rose_ctx_check_signature(rose_ctx_t
*rctx
) {
153 return rctx
&& (rctx
->signature
== ROSE_CTX_SIGNATURE
);
156 void rose_ctx_clean_data(rose_ctx_t
*rctx
) {
157 memset(&rctx
->d
, '\0', sizeof(rctx
->d
));
161 asn1_ctx_t
*get_asn1_ctx(void *ptr
) {
162 asn1_ctx_t
*actx
= (asn1_ctx_t
*)ptr
;
164 if (!asn1_ctx_check_signature(actx
))
170 rose_ctx_t
*get_rose_ctx(void *ptr
) {
171 rose_ctx_t
*rctx
= (rose_ctx_t
*)ptr
;
172 asn1_ctx_t
*actx
= (asn1_ctx_t
*)ptr
;
174 if (!asn1_ctx_check_signature(actx
))
178 rctx
= actx
->rose_ctx
;
180 if (!rose_ctx_check_signature(rctx
))
186 /** Only tested for BER */
187 double asn1_get_real(const uint8_t *real_ptr
, int len
) {
193 /* 8.5.2 If the real value is the value zero,
194 * there shall be no contents octets in the encoding.
196 if (len
< 1) return val
;
201 if (octet
& 0x80) { /* binary encoding */
205 uint8_t B
; /* Base */
206 uint8_t F
; /* scaling Factor */
207 int32_t E
= 0; /* Exponent (supported max 3 octets/24 bit) */
208 uint64_t N
= 0; /* N (supported max 8 octets/64 bit) */
212 if(octet
& 0x40) S
= -1; else S
= 1;
213 switch(octet
& 0x30) {
214 case 0x00: B
= 2; break;
215 case 0x10: B
= 8; break;
216 case 0x20: B
= 16; break;
217 case 0x30: /* Reserved */
219 /* TODO Add some warning in tree about reserved value for Base */
222 F
= (octet
& 0x0c) >> 2;
224 /* 8.5.6.4 Exponent length */
225 lenE
= (octet
& 0x3) + 1;
227 /* we can't handle exponents > 24 bits */
228 /* TODO Next octet(s) define length of exponent */
229 DISSECTOR_ASSERT(lenE
!= 4);
231 /* Ensure the buffer len and its content are coherent */
232 DISSECTOR_ASSERT(lenE
< len
);
234 Eneg
= ((*p
) & 0x80) ? true : false;
235 for (i
= 0; i
< lenE
; i
++) {
237 /* 2's complement: inverse bits */
238 E
= (E
<<8) | ((uint8_t) ~(*p
));
245 /* 2's complement: ... and add 1 (and make negative of course) */
251 /* we can't handle integers > 64 bits */
252 DISSECTOR_ASSERT(lenN
<= 8);
254 for (i
=0; i
<lenN
; i
++) {
258 val
= (double) S
* N
* pow(2, F
) * pow(B
, E
);
259 } else if (octet
& 0x40) { /* SpecialRealValue */
260 switch (octet
& 0x3F) {
261 case 0x00: val
= HUGE_VAL
; break;
262 case 0x01: val
= -HUGE_VAL
; break;
263 case 0x02: val
= NAN
; break;
265 } else { /* decimal encoding */
266 buf
= g_strndup(p
, len
);
267 val
= g_ascii_strtod(buf
, NULL
);
275 * Editor modelines - https://www.wireshark.org/tools/modelines.html
280 * indent-tabs-mode: nil
283 * ex: set shiftwidth=2 tabstop=8 expandtab:
284 * :indentSize=2:tabSize=8:noTabs=true: