Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / asn1.c
blobc6ff819d6f0581634ebafabc0e29249ab1d42879
1 /* asn1.c
2 * Common routines for ASN.1
3 * 2007 Anders Broman
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
12 #include "config.h"
14 #include <glib.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <math.h>
20 #include <epan/packet.h>
22 #include "asn1.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;
29 actx->pinfo = pinfo;
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);
55 frame->name = name;
56 frame->next = actx->stack;
57 actx->stack = frame;
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;
68 asn1_par_t *par;
70 DISSECTOR_ASSERT(actx->stack);
71 DISSECTOR_ASSERT(!strcmp(actx->stack->name, name));
73 par = actx->stack->par;
74 while (pd->name) {
75 DISSECTOR_ASSERT(par);
76 DISSECTOR_ASSERT((pd->ptype == ASN1_PAR_IRR) || (par->ptype == pd->ptype));
77 par->name = pd->name;
78 pd++;
79 par = par->next;
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;
89 while (par) {
90 if (!strcmp(par->name, name))
91 return par;
92 par = par->next;
94 return par;
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);
105 while (*pp)
106 pp = &((*pp)->next);
107 *pp = par;
109 return par;
112 void asn1_param_push_boolean(asn1_ctx_t *actx, bool value) {
113 asn1_par_t *par;
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) {
121 asn1_par_t *par;
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;
145 /*--- ROSE ---*/
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));
158 rctx->d.code = -1;
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))
165 actx = NULL;
167 return 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))
175 actx = NULL;
177 if (actx)
178 rctx = actx->rose_ctx;
180 if (!rose_ctx_check_signature(rctx))
181 rctx = NULL;
183 return rctx;
186 /** Only tested for BER */
187 double asn1_get_real(const uint8_t *real_ptr, int len) {
188 uint8_t octet;
189 const uint8_t *p;
190 uint8_t *buf;
191 double val = 0;
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;
198 octet = real_ptr[0];
199 p = real_ptr + 1;
200 len -= 1;
201 if (octet & 0x80) { /* binary encoding */
202 int i;
203 bool Eneg;
204 int8_t S; /* Sign */
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) */
210 uint8_t lenE, lenN;
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 */
218 default:
219 /* TODO Add some warning in tree about reserved value for Base */
220 return 0;
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++) {
236 if(Eneg) {
237 /* 2's complement: inverse bits */
238 E = (E<<8) | ((uint8_t) ~(*p));
239 } else {
240 E = (E<<8) | *p;
242 p++;
244 if(Eneg) {
245 /* 2's complement: ... and add 1 (and make negative of course) */
246 E = -(E + 1);
249 lenN = len - lenE;
251 /* we can't handle integers > 64 bits */
252 DISSECTOR_ASSERT(lenN <= 8);
254 for (i=0; i<lenN; i++) {
255 N = (N<<8) | *p;
256 p++;
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);
268 g_free(buf);
271 return val;
275 * Editor modelines - https://www.wireshark.org/tools/modelines.html
277 * Local Variables:
278 * c-basic-offset: 2
279 * tab-width: 8
280 * indent-tabs-mode: nil
281 * End:
283 * ex: set shiftwidth=2 tabstop=8 expandtab:
284 * :indentSize=2:tabSize=8:noTabs=true: