The "next_submsg" argument passed to "get_bitmap()" is an offset within
[obnox/wireshark/wip.git] / packet-ber.c
1 /* packet-ber.c
2  * Helpers for ASN.1/BER dissection
3  * Ronnie Sahlberg (C) 2004
4  *
5  * $Id: packet-ber.c,v 1.6 2004/03/26 00:21:53 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 /* 
27  * ITU-T Recommendation X.690 (07/2002),
28  *   Information technology ASN.1 encoding rules:
29  *     Specification of Basic Encoding Rules (BER), Canonical Encoding Rules (CER) and Distinguished Encoding Rules (DER)
30  * 
31  */ 
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <ctype.h>
40
41 #include <glib.h>
42
43 #include <epan/packet.h>
44
45 #include <epan/strutil.h>
46 #include "prefs.h"
47 #include "packet-ber.h"
48
49
50 static gint proto_ber = -1;
51 static gint hf_ber_id_class = -1;
52 static gint hf_ber_id_pc = -1;
53 static gint hf_ber_id_uni_tag = -1;
54 static gint hf_ber_id_tag = -1;
55 static gint hf_ber_length = -1;
56 static gint hf_ber_bitstring_padding = -1;
57
58 static gint ett_ber_octet_string = -1;
59
60 static gboolean show_internal_ber_fields = FALSE;
61
62 proto_item *ber_last_created_item=NULL;
63
64
65 static const value_string ber_class_codes[] = {
66         { BER_CLASS_UNI,        "Universal" },
67         { BER_CLASS_APP,        "Application" },
68         { BER_CLASS_CON,        "Context Specific" },
69         { BER_CLASS_PRI,        "Private" },
70         { 0, NULL }
71 };
72
73 static const true_false_string ber_pc_codes = {
74         "Constructed Encoding",
75         "Primitive Encoding"
76 };
77
78 static const value_string ber_uni_tag_codes[] = {
79         { BER_UNI_TAG_EOC                               , "'end-of-content'" },
80         { BER_UNI_TAG_BOOLEAN                   , "BOOLEAN" },
81         { BER_UNI_TAG_INTEGER                   , "INTEGER" },
82         { BER_UNI_TAG_BITSTRING         , "BIT STRING" },
83         { BER_UNI_TAG_OCTETSTRING               , "OCTET STRING" },
84         { BER_UNI_TAG_NULL                      , "NULL" },
85         { BER_UNI_TAG_OID                               , "OBJECT IDENTIFIER" },
86         { BER_UNI_TAG_ObjectDescriptor, "ObjectDescriptor" },
87         { BER_UNI_TAG_REAL                      , "REAL" },
88         { BER_UNI_TAG_ENUMERATED                , "ENUMERATED" },
89         { BER_UNI_TAG_EMBEDDED_PDV      , "EMBEDDED PDV" },
90         { BER_UNI_TAG_UTF8String                , "UTF8String" },
91         { BER_UNI_TAG_RELATIVE_OID      , "RELATIVE-OID" },
92         { BER_UNI_TAG_SEQUENCE          , "SEQUENCE, SEQUENCE OF" },
93         { BER_UNI_TAG_SET                               , "SET, SET OF" },
94         { BER_UNI_TAG_NumericString     , "NumericString" },
95         { BER_UNI_TAG_PrintableString   , "PrintableString" },
96         { BER_UNI_TAG_TeletextString    , "TeletextString, T61String" },
97         { BER_UNI_TAG_VideotexString    , "VideotexString" },
98         { BER_UNI_TAG_IA5String         , "IA5String" },
99         { BER_UNI_TAG_UCTTime                   , "UCTTime" },
100         { BER_UNI_TAG_GeneralizedTime   , "GeneralizedTime" },
101         { BER_UNI_TAG_GraphicString     , "GraphicString" },
102         { BER_UNI_TAG_VisibleString     , "VisibleString, ISO64String" },
103         { BER_UNI_TAG_GeneralString     , "GeneralString" },
104         { BER_UNI_TAG_UniversalString   , "UniversalString" },
105         { BER_UNI_TAG_CHARACTERSTRING   , "CHARACTER STRING" },
106         { BER_UNI_TAG_BMPString         , "BMPString" },
107         { 0, NULL }
108 };
109
110
111 proto_item *get_ber_last_created_item(void) {
112   return ber_last_created_item;
113 }
114
115 static int dissect_ber_sq_of(gboolean implicit_tag, guint32 type, packet_info *pinfo, proto_tree *parent_tree, tvbuff_t *tvb, int offset, ber_sequence *seq, gint hf_id, gint ett_id);
116
117 /* 8.1 General rules for encoding */
118
119 /*  8.1.2 Identifier octets */
120 int get_ber_identifier(tvbuff_t *tvb, int offset, guint8 *class, gboolean *pc, guint32 *tag) {
121         guint8 id, t;
122         guint8 tmp_class;
123         gboolean tmp_pc;
124         guint32 tmp_tag;
125
126         id = tvb_get_guint8(tvb, offset);
127         offset += 1;
128         
129         /* 8.1.2.2 */
130         tmp_class = (id>>6) & 0x03;
131         tmp_pc = (id>>5) & 0x01;
132         tmp_tag = id&0x1F;
133         /* 8.1.2.4 */
134         if (tmp_tag == 0x1F) {
135                 tmp_tag = 0;
136                 while (tvb_length_remaining(tvb, offset) > 0) {
137                         t = tvb_get_guint8(tvb, offset);
138                         offset += 1;
139                         tmp_tag <<= 7;       
140                         tmp_tag |= t & 0x7F;
141                         if (t & 0x80) break;
142                 }
143         }
144
145         if (class)
146                 *class = tmp_class;
147         if (pc)
148                 *pc = tmp_pc;
149         if (tag)
150                 *tag = tmp_tag;
151
152         return offset;
153 }
154
155 int dissect_ber_identifier(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, guint8 *class, gboolean *pc, guint32 *tag) 
156 {
157         int old_offset = offset;
158         guint8 tmp_class;
159         gboolean tmp_pc;
160         guint32 tmp_tag;
161
162         offset = get_ber_identifier(tvb, offset, &tmp_class, &tmp_pc, &tmp_tag);
163         
164         if(show_internal_ber_fields){
165                 proto_tree_add_uint(tree, hf_ber_id_class, tvb, old_offset, 1, tmp_class<<6);
166                 proto_tree_add_boolean(tree, hf_ber_id_pc, tvb, old_offset, 1, (tmp_pc)?0x20:0x00);
167                 if(tmp_class==BER_CLASS_UNI){
168                         proto_tree_add_uint(tree, hf_ber_id_uni_tag, tvb, old_offset, offset - old_offset, tmp_tag);
169                 } else {
170                         proto_tree_add_uint(tree, hf_ber_id_tag, tvb, old_offset, offset - old_offset, tmp_tag);
171                 }
172         }
173
174         if (class)
175                 *class = tmp_class;
176         if (pc)
177                 *pc = tmp_pc;
178         if (tag)
179                 *tag = tmp_tag;
180
181         return offset;
182 }
183
184 /* this function gets the length octets of the BER TLV.
185  * We only handle (TAGs and) LENGTHs that fit inside 32 bit integers.
186  */
187 /* 8.1.3 Length octets */
188 int
189 get_ber_length(tvbuff_t *tvb, int offset, guint32 *length, gboolean *ind) {
190         guint8 oct, len;
191         guint32 tmp_length;
192         gboolean tmp_ind;
193
194         tmp_length = 0;
195         tmp_ind = FALSE;
196
197         oct = tvb_get_guint8(tvb, offset);
198         offset += 1;
199         
200         if (!(oct&0x80)) {
201                 /* 8.1.3.4 */
202                 tmp_length = oct;
203         } else {
204                 len = oct & 0x7F;
205                 if (len) {
206                         /* 8.1.3.5 */
207                         while (len--) {
208                                 oct = tvb_get_guint8(tvb, offset);
209                                 offset++;
210                                 tmp_length = (tmp_length<<8) + oct;
211                         }
212                 } else {
213                         /* 8.1.3.6 */
214                         tmp_ind = TRUE;
215                         /* TO DO */
216                 }
217         }
218
219         if (length)
220                 *length = tmp_length;
221         if (ind)
222                 *ind = tmp_ind;
223
224         return offset;
225 }
226
227 /* this function dissects the length octets of the BER TLV.
228  * We only handle (TAGs and) LENGTHs that fit inside 32 bit integers.
229  */
230 int
231 dissect_ber_length(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, guint32 *length, gboolean *ind)
232 {
233         int old_offset = offset;
234         guint32 tmp_length;
235         gboolean tmp_ind;
236
237         offset = get_ber_length(tvb, offset, &tmp_length, &tmp_ind);
238         
239         if(show_internal_ber_fields){
240                 proto_tree_add_uint(tree, hf_ber_length, tvb, old_offset, offset - old_offset, tmp_length);
241         }
242         if (length)
243                 *length = tmp_length;
244         if (ind)
245                 *ind = tmp_ind;
246         return offset;
247 }
248
249 /* 8.7 Encoding of an octetstring value */
250 int 
251 dissect_ber_octet_string(gboolean implicit_tag, packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, gint hf_id, tvbuff_t **out_tvb) {
252         guint8 class;
253         gboolean pc, ind;
254         guint32 tag;
255         guint32 len;
256         int end_offset;
257         proto_item *it;
258
259         /* read header and len for the octet string */
260         offset=dissect_ber_identifier(pinfo, tree, tvb, offset, &class, &pc, &tag);
261         offset=dissect_ber_length(pinfo, tree, tvb, offset, &len, &ind);
262         end_offset=offset+len;
263
264         /* sanity check: we only handle Constructed Universal Sequences */
265         if (!implicit_tag) {
266                 if( (class!=BER_CLASS_UNI)
267                   ||(tag!=BER_UNI_TAG_OCTETSTRING) ){
268                     proto_tree_add_text(tree, tvb, offset-2, 2, "BER Error: OctetString expected but Class:%d PC:%d Tag:%d was unexpected", class, pc, tag);
269                         return end_offset;
270                 }
271         }
272
273         ber_last_created_item = NULL;
274         if (pc) {
275                 /* constructed */
276                 /* TO DO */
277         } else {
278                 /* primitive */
279                 if (hf_id != -1) {
280                         it = proto_tree_add_item(tree, hf_id, tvb, offset, len, FALSE);
281                         ber_last_created_item = it;
282                 }
283                 if (out_tvb) {
284                         *out_tvb = tvb_new_subset(tvb, offset, len, len);
285                 }
286         }
287         return end_offset;
288 }
289
290 int dissect_ber_octet_string_wcb(gboolean implicit_tag, packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, gint hf_id, ber_callback func)
291 {
292         tvbuff_t *out_tvb;
293
294         offset = dissect_ber_octet_string(implicit_tag, pinfo, tree, tvb, offset, hf_id, (func)?&out_tvb:NULL);
295         if (func && (tvb_length(out_tvb)>0)) {
296                 if (hf_id != -1)
297                         tree = proto_item_add_subtree(ber_last_created_item, ett_ber_octet_string);
298                 func(pinfo, tree, out_tvb, 0);
299         }
300         return offset;
301 }
302
303
304 int
305 dissect_ber_integer(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, gint hf_id, guint32 *value)
306 {
307         guint8 class;
308         gboolean pc;
309         guint32 tag;
310         guint32 len;
311         gint32 val;
312         guint32 i;
313
314         offset=dissect_ber_identifier(pinfo, tree, tvb, offset, &class, &pc, &tag);
315         offset=dissect_ber_length(pinfo, tree, tvb, offset, &len, NULL);
316
317 /*      if(class!=BER_CLASS_UNI)*/
318         
319         val=0;
320         if (len > 0) {
321                 /* extend sign bit */
322                 val = (gint8)tvb_get_guint8(tvb, offset);
323                 offset++;
324         }
325         for(i=1;i<len;i++){
326                 val=(val<<8)|tvb_get_guint8(tvb, offset);
327                 offset++;
328         }
329
330         ber_last_created_item=NULL;
331
332         if(hf_id!=-1){  
333                 ber_last_created_item=proto_tree_add_item(tree, hf_id, tvb, offset-len, len, FALSE);
334         }
335         if(value){
336                 *value=val;
337         }
338
339         return offset;
340 }
341
342
343
344
345
346 int
347 dissect_ber_boolean(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, gint hf_id)
348 {
349         guint8 class;
350         gboolean pc;
351         guint32 tag;
352         guint32 len;
353         guint8 val;
354         header_field_info *hfi;
355
356         offset=dissect_ber_identifier(pinfo, tree, tvb, offset, &class, &pc, &tag);
357         offset=dissect_ber_length(pinfo, tree, tvb, offset, &len, NULL);
358
359 /*      if(class!=BER_CLASS_UNI)*/
360         
361         val=tvb_get_guint8(tvb, offset);
362         offset+=1;
363
364         ber_last_created_item=NULL;
365
366         if(hf_id!=-1){
367                 hfi = proto_registrar_get_nth(hf_id);
368                 if (hfi->type == FT_BOOLEAN)
369                         ber_last_created_item=proto_tree_add_boolean(tree, hf_id, tvb, offset-1, 1, val);
370                 else
371                         ber_last_created_item=proto_tree_add_uint(tree, hf_id, tvb, offset-1, 1, val?1:0);
372         }
373
374         return offset;
375 }
376
377
378
379
380
381 /* this function dissects a BER sequence 
382  */
383 int dissect_ber_sequence(gboolean implicit_tag, packet_info *pinfo, proto_tree *parent_tree, tvbuff_t *tvb, int offset, ber_sequence *seq, gint hf_id, gint ett_id) {
384         guint8 class;
385         gboolean pc, ind;
386         guint32 tag;
387         guint32 len;
388         proto_tree *tree = parent_tree;
389         proto_item *item = NULL;
390         int end_offset;
391
392         /* first we must read the sequence header */
393         offset = dissect_ber_identifier(pinfo, tree, tvb, offset, &class, &pc, &tag);
394         offset = dissect_ber_length(pinfo, tree, tvb, offset, &len, &ind);
395         end_offset = offset + len;
396
397         /* sanity check: we only handle Constructed Universal Sequences */
398         if ((!pc)
399                 ||(!implicit_tag&&((class!=BER_CLASS_UNI)
400                                                         ||(tag!=BER_UNI_TAG_SEQUENCE)))) {
401                 proto_tree_add_text(tree, tvb, offset-2, 2, "BER Error: Sequence expected but Class:%d PC:%d Tag:%d was unexpected", class, pc, tag);
402                 return end_offset;
403         }
404
405         /* create subtree */
406         if (hf_id != -1) {
407                 if(parent_tree){
408                         item = proto_tree_add_item(parent_tree, hf_id, tvb, offset, len, FALSE);
409                         tree = proto_item_add_subtree(item, ett_id);
410                 }
411         }
412
413         /* loop over all entries until we reach the end of the sequence */
414         while (offset < end_offset){
415                 guint8 class;
416                 gboolean pc;
417                 guint32 tag;
418                 guint32 len;
419                 int hoffset, eoffset;
420
421                 hoffset = offset;
422                 /* read header and len for next field */
423                 offset = get_ber_identifier(tvb, offset, &class, &pc, &tag);
424                 offset = get_ber_length(tvb, offset, &len, NULL);
425                 eoffset = offset + len;
426
427 ber_sequence_try_again:
428                 /* have we run out of known entries in the sequence ?*/
429                 if (!seq->func) {
430                         /* it was not,  move to the enxt one and try again */
431                         proto_tree_add_text(tree, tvb, offset, len, "BER Error: This field lies beyond the end of the known sequence definition.");
432                         offset = eoffset;
433                         continue;
434                 }
435
436                 /* verify that this one is the one we want */
437                 if( (seq->class!=class)
438                   ||(seq->tag!=tag) ){
439                         /* it was not,  move to the enxt one and try again */
440                         if(seq->flags&BER_FLAGS_OPTIONAL){
441                                 /* well this one was optional so just skip to the next one and try again. */
442                                 seq++;
443                                 goto ber_sequence_try_again;
444                         }
445                         if (!(seq->flags & BER_FLAGS_NOTCHKTAG)) {
446                                 proto_tree_add_text(tree, tvb, offset, len, "BER Error: Wrong field");
447                                 seq++;
448                                 offset=eoffset;
449                                 continue;
450                         }
451                 }
452
453                 if (!(seq->flags & BER_FLAGS_NOOWNTAG) && !(seq->flags & BER_FLAGS_IMPLTAG)) {
454                         /* dissect header and len for field */
455                         hoffset = dissect_ber_identifier(pinfo, tree, tvb, hoffset, NULL, NULL, NULL);
456                         hoffset = dissect_ber_length(pinfo, tree, tvb, hoffset, NULL, NULL);
457                 }
458                 
459                 /* call the dissector for this field */
460                 seq->func(pinfo, tree, tvb, hoffset);
461
462                 seq++;
463                 offset = eoffset;
464         }
465
466         /* if we didnt end up at exactly offset, then we ate too many bytes */
467         if (offset != end_offset) {
468                 proto_tree_add_text(tree, tvb, offset-2, 2, "BER Error: Sequence ate %d too many bytes", offset-end_offset);
469         }
470
471         return end_offset;
472 }
473
474
475
476 /* this function dissects a BER choice 
477  */
478 int
479 dissect_ber_choice(packet_info *pinfo, proto_tree *parent_tree, tvbuff_t *tvb, int offset, const ber_choice *choice, gint hf_id, gint ett_id)
480 {
481         guint8 class;
482         gboolean pc;
483         guint32 tag;
484         guint32 len;
485         const ber_choice *ch;
486         proto_tree *tree=parent_tree;
487         proto_item *item=NULL;
488         int end_offset;
489         int hoffset = offset;
490
491         /* read header and len for choice field */
492         offset=get_ber_identifier(tvb, offset, &class, &pc, &tag);
493         offset=get_ber_length(tvb, offset, &len, NULL);
494         end_offset=offset+len;
495
496         /* loop over all entries until we find the right choice or 
497            run out of entries */
498         ch = choice;
499         while(ch->func){
500                 if( (ch->class==class)
501                   &&(ch->tag==tag) ){
502                         if (!(ch->flags & BER_FLAGS_NOOWNTAG) && !(ch->flags & BER_FLAGS_IMPLTAG)) {
503                                 /* dissect header and len for field */
504                                 hoffset = dissect_ber_identifier(pinfo, tree, tvb, hoffset, NULL, NULL, NULL);
505                                 hoffset = dissect_ber_length(pinfo, tree, tvb, hoffset, NULL, NULL);
506                         }
507                         /* create subtree */
508                         if(hf_id!=-1){
509                                 if(parent_tree){
510                                         item = proto_tree_add_uint(parent_tree, hf_id, tvb, hoffset, end_offset - hoffset, ch->value);
511                                         tree = proto_item_add_subtree(item, ett_id);
512                                 }
513                         }
514                         offset=ch->func(pinfo, tree, tvb, hoffset);
515                         return end_offset;
516                         break;
517                 }
518                 ch++;
519         }
520         /* oops no more entries and we still havent found
521          * our guy :-(
522          */
523         proto_tree_add_text(tree, tvb, offset, len, "BER Error: This choice field was not found.");
524
525         return end_offset;
526 }
527
528 #if 0
529 /* this function dissects a BER GeneralString
530  */
531 int
532 dissect_ber_GeneralString(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, gint hf_id, char *name_string, int name_len)
533 {
534         guint8 class;
535         gboolean pc;
536         guint32 tag;
537         guint32 len;
538         int end_offset;
539         char str_arr[256];
540         guint32 max_len;
541         char *str;
542
543         str=str_arr;
544         max_len=255;
545         if(name_string){
546                 str=name_string;
547                 max_len=name_len;
548         }
549
550         /* first we must read the GeneralString header */
551         offset=dissect_ber_identifier(pinfo, tree, tvb, offset, &class, &pc, &tag);
552         offset=dissect_ber_length(pinfo, tree, tvb, offset, &len, NULL);
553         end_offset=offset+len;
554
555         /* sanity check: we only handle Universal GeneralString*/
556         if( (class!=BER_CLASS_UNI)
557           ||(tag!=BER_UNI_TAG_GENSTR) ){
558                 proto_tree_add_text(tree, tvb, offset-2, 2, "BER Error: GeneralString expected but Class:%d PC:%d Tag:%d was unexpected", class, pc, tag);
559                 return end_offset;
560         }
561
562         if(len>=(max_len-1)){
563                 len=max_len-1;
564         }
565         
566         tvb_memcpy(tvb, str, offset, len);
567         str[len]=0;
568
569         if(hf_id!=-1){
570                 proto_tree_add_string(tree, hf_id, tvb, offset, len, str);
571         }
572
573         return end_offset;
574 }
575 #endif
576 int
577 dissect_ber_restricted_string(gboolean implicit_tag, guint32 type, packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, gint hf_id, tvbuff_t **out_tvb) {
578         guint8 class;
579         gboolean pc;
580         guint32 tag;
581         guint32 len;
582         int eoffset;
583         int hoffset = offset;
584
585         offset = get_ber_identifier(tvb, offset, &class, &pc, &tag);
586         offset = get_ber_length(tvb, offset, &len, NULL);
587         eoffset = offset + len;
588
589         /* sanity check */
590         if (!implicit_tag) {
591                 if( (class!=BER_CLASS_UNI)
592                   ||(tag != type) ){
593                     proto_tree_add_text(tree, tvb, offset-2, 2, "BER Error: String with tag=%d expected but Class:%d PC:%d Tag:%d was unexpected", type, class, pc, tag);
594                         return eoffset;
595                 }
596         }
597
598         /* 8.21.3 */
599         return dissect_ber_octet_string(TRUE, pinfo, tree, tvb, hoffset, hf_id, out_tvb);
600 }
601
602 int
603 dissect_ber_GeneralString(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, gint hf_id, char *name_string, guint name_len)
604 {
605         tvbuff_t *out_tvb;
606
607         offset = dissect_ber_restricted_string(FALSE, BER_UNI_TAG_GeneralString, pinfo, tree, tvb, offset, hf_id, (name_string)?&out_tvb:NULL);
608
609         if (name_string) {
610                 if (tvb_length(out_tvb) >= name_len) {
611                         tvb_memcpy(out_tvb, name_string, 0, name_len-1);
612                         name_string[name_len-1] = '\0';
613                 } else {
614                         tvb_memcpy(out_tvb, name_string, 0, -1);
615                         name_string[tvb_length(out_tvb)] = '\0';
616                 }
617         }
618
619         return offset;
620 }
621
622 /* 8.19 Encoding of an object identifier value */
623 int dissect_ber_object_identifier(gboolean implicit_tag, packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, gint hf_id, char *value_string) {
624         guint8 class;
625         gboolean pc;
626         guint32 tag;
627         guint32 i, len;
628         int eoffset;
629         guint8 byte;
630         guint32 value;
631         char str[256],*strp;
632
633         offset = get_ber_identifier(tvb, offset, &class, &pc, &tag);
634         offset = dissect_ber_length(pinfo, tree, tvb, offset, &len, NULL);
635         eoffset = offset + len;
636
637         if (value_string) {
638                 value_string[0] = '\0';
639         }
640
641         /* sanity check */
642         if (!implicit_tag) {
643                 if( (class!=BER_CLASS_UNI)
644                   ||(tag != BER_UNI_TAG_OID) ){
645                     proto_tree_add_text(tree, tvb, offset-2, 2, "BER Error: Object Identifier expected but Class:%d PC:%d Tag:%d was unexpected", class, pc, tag);
646                         return eoffset;
647                 }
648         }
649
650         value=0;
651         for (i=0,strp=str; i<len; i++){
652                 byte = tvb_get_guint8(tvb, offset);
653                 offset++;
654
655                 if((strp-str)>200){
656             proto_tree_add_text(tree, tvb, offset, eoffset - offset, "BER Error: too long Object Identifier");
657                         return offset;
658                 }
659
660                 /* 8.19.4 */
661                 if (i == 0) {
662                         strp += sprintf(strp, "%d.%d", byte/40, byte%40);
663                         continue;
664                 }
665
666                 value = (value << 7) | (byte & 0x7F);
667                 if (byte & 0x80) {
668                         continue;
669                 }
670
671                 strp += sprintf(strp, ".%d", value);
672                 value = 0;
673         }
674         *strp = '\0';
675
676         if (hf_id != -1) {
677                 proto_tree_add_string(tree, hf_id, tvb, offset - len, len, str);
678         }
679
680         if (value_string) {
681                 strcpy(value_string, str);
682         }
683
684         return eoffset;
685 }
686
687 static int dissect_ber_sq_of(gboolean implicit_tag, guint32 type, packet_info *pinfo, proto_tree *parent_tree, tvbuff_t *tvb, int offset, ber_sequence *seq, gint hf_id, gint ett_id) {
688         guint8 class;
689         gboolean pc, ind;
690         guint32 tag;
691         guint32 len;
692         proto_tree *tree = parent_tree;
693         proto_item *item = NULL;
694         int cnt, hoffset, end_offset;
695         header_field_info *hfi;
696
697         /* first we must read the sequence header */
698         offset = dissect_ber_identifier(pinfo, tree, tvb, offset, &class, &pc, &tag);
699         offset = dissect_ber_length(pinfo, tree, tvb, offset, &len, &ind);
700         end_offset = offset + len;
701
702         /* sanity check: we only handle Constructed Universal Sequences */
703         if (!pc
704                 ||(!implicit_tag&&((class!=BER_CLASS_UNI)
705                                                         ||(tag!=type)))) {
706                 proto_tree_add_text(tree, tvb, offset-2, 2, "BER Error: %s Of expected but Class:%d PC:%d Tag:%d was unexpected", 
707                                                         (type==BER_UNI_TAG_SEQUENCE)?"Set":"Sequence", class, pc, tag);
708                 return end_offset;
709         }
710
711         /* count number of items */
712         cnt = 0;
713         hoffset = offset;
714         while (offset < end_offset){
715                 guint32 len;
716                 /* read header and len for next field */
717                 offset = get_ber_identifier(tvb, offset, NULL, NULL, NULL);
718                 offset = get_ber_length(tvb, offset, &len, NULL);
719                 offset += len;
720                 cnt++;
721         }
722         offset = hoffset;
723
724         /* create subtree */
725         if (hf_id != -1) {
726                 hfi = proto_registrar_get_nth(hf_id);
727                 if(parent_tree){
728                         if (hfi->type == FT_NONE)
729                                 item = proto_tree_add_item(parent_tree, hf_id, tvb, offset, len, FALSE);
730                         else {
731                                 item = proto_tree_add_uint(parent_tree, hf_id, tvb, offset, len, cnt);
732                                 proto_item_append_text(item, (cnt==1)?" item":" items");
733                         }
734                         tree = proto_item_add_subtree(item, ett_id);
735                 }
736         }
737
738         /* loop over all entries until we reach the end of the sequence */
739         while (offset < end_offset){
740                 guint8 class;
741                 gboolean pc;
742                 guint32 tag;
743                 guint32 len;
744                 int eoffset;
745                 int hoffset;
746
747                 hoffset = offset;
748                 /* read header and len for next field */
749                 offset = get_ber_identifier(tvb, offset, &class, &pc, &tag);
750                 offset = get_ber_length(tvb, offset, &len, NULL);
751                 eoffset = offset + len;
752
753                 /* verify that this one is the one we want */
754                 if ((seq->class!=class)
755                         ||(seq->tag!=tag) ){
756                         if (!(seq->flags & BER_FLAGS_NOTCHKTAG)) {
757                                 proto_tree_add_text(tree, tvb, offset, len, "BER Error: Wrong field");
758                                 offset = eoffset;
759                                 continue;
760                         }
761                 }
762
763                 if (!(seq->flags & BER_FLAGS_NOOWNTAG) && !(seq->flags & BER_FLAGS_IMPLTAG)) {
764                         /* dissect header and len for field */
765                         hoffset = dissect_ber_identifier(pinfo, tree, tvb, hoffset, NULL, NULL, NULL);
766                         hoffset = dissect_ber_length(pinfo, tree, tvb, hoffset, NULL, NULL);
767                 }
768                 
769                 /* call the dissector for this field */
770                 seq->func(pinfo, tree, tvb, hoffset);
771                 cnt++;
772                 offset = eoffset;
773         }
774
775         /* if we didnt end up at exactly offset, then we ate too many bytes */
776         if (offset != end_offset) {
777                 proto_tree_add_text(tree, tvb, offset-2, 2, "BER Error: %s Of ate %d too many bytes", 
778                                                         (type==BER_UNI_TAG_SEQUENCE)?"Set":"Sequence", offset-end_offset);
779         }
780
781         return end_offset;
782 }
783
784 int dissect_ber_sequence_of(gboolean implicit_tag, packet_info *pinfo, proto_tree *parent_tree, tvbuff_t *tvb, int offset, ber_sequence *seq, gint hf_id, gint ett_id) {
785         return dissect_ber_sq_of(implicit_tag, BER_UNI_TAG_SEQUENCE, pinfo, parent_tree, tvb, offset, seq, hf_id, ett_id);
786 }
787
788 int dissect_ber_set_of(gboolean implicit_tag, packet_info *pinfo, proto_tree *parent_tree, tvbuff_t *tvb, int offset, ber_sequence *seq, gint hf_id, gint ett_id) {
789         return dissect_ber_sq_of(implicit_tag, BER_UNI_TAG_SET, pinfo, parent_tree, tvb, offset, seq, hf_id, ett_id);
790 }
791
792 int 
793 dissect_ber_generalized_time(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, gint hf_id)
794 {
795         char str[32];
796         const guint8 *tmpstr;
797         guint8 class;
798         gboolean pc;
799         guint32 tag;
800         guint32 len;
801         int end_offset;
802
803         offset=dissect_ber_identifier(pinfo, tree, tvb, offset, &class, &pc, &tag);
804         offset=dissect_ber_length(pinfo, tree, tvb, offset, &len, NULL);
805         end_offset=offset+len;
806
807         /* sanity check. we only handle universal/generalized time */
808         if( (class!=BER_CLASS_UNI)
809           ||(tag!=BER_UNI_TAG_GeneralizedTime)){
810                 proto_tree_add_text(tree, tvb, offset-2, 2, "BER Error: GeneralizedTime expected but Class:%d PC:%d Tag:%d was unexpected", class, pc, tag);
811                 return end_offset;
812                 end_offset=offset+len;
813         }
814
815         tmpstr=tvb_get_ptr(tvb, offset, len);
816         snprintf(str, 31, "%.4s-%.2s-%.2s %.2s:%.2s:%.2s (%.1s)",
817                 tmpstr, tmpstr+4, tmpstr+6, tmpstr+8,
818                 tmpstr+10, tmpstr+12, tmpstr+14);
819         str[31]=0; /* just in case ... */
820                 
821         if(hf_id!=-1){
822                 proto_tree_add_string(tree, hf_id, tvb, offset, len, str);
823         }
824
825         offset+=len;
826         return offset;
827 }
828
829 /* 8.6 Encoding of a bitstring value */
830 int dissect_ber_bitstring(gboolean implicit_tag, packet_info *pinfo, proto_tree *parent_tree, tvbuff_t *tvb, int offset, asn_namedbit *named_bits, gint hf_id, gint ett_id, tvbuff_t **out_tvb) 
831 {
832         guint8 class;
833         gboolean pc, ind;
834         guint32 tag;
835         guint32 len;
836         guint8 pad, b0, b1, val;
837         int end_offset;
838         proto_item *item = NULL;
839         proto_tree *tree = NULL;
840         asn_namedbit *nb;
841
842         /* read header and len for the octet string */
843         offset = dissect_ber_identifier(pinfo, parent_tree, tvb, offset, &class, &pc, &tag);
844         offset = dissect_ber_length(pinfo, parent_tree, tvb, offset, &len, &ind);
845         end_offset = offset + len;
846
847         /* sanity check: we only handle Universal BitSrings */
848         if (!implicit_tag) {
849                 if( (class!=BER_CLASS_UNI)
850                   ||(tag!=BER_UNI_TAG_BITSTRING) ){
851                     proto_tree_add_text(parent_tree, tvb, offset-2, 2, "BER Error: BitString expected but Class:%d PC:%d Tag:%d was unexpected", class, pc, tag);
852                         return end_offset;
853                 }
854         }
855
856         ber_last_created_item = NULL;
857
858         if (pc) {
859                 /* constructed */
860                 /* TO DO */
861         } else {
862                 /* primitive */
863                 /* padding */
864                 pad = tvb_get_guint8(tvb, offset);
865                 proto_tree_add_item(parent_tree, hf_ber_bitstring_padding, tvb, offset, 1, FALSE);
866                 offset++;
867                 len--;
868                 if ( hf_id != -1) {
869                         item = proto_tree_add_item(parent_tree, hf_id, tvb, offset, len, FALSE);
870                         ber_last_created_item = item;
871                         if (ett_id != -1) {
872                                 tree = proto_item_add_subtree(item, ett_id);
873                         }
874                 }
875                 if (out_tvb) {
876                         *out_tvb = tvb_new_subset(tvb, offset, len, 8*len-pad);
877                 }
878         }
879
880         if (named_bits) {
881                 nb = named_bits;
882                 while (nb->p_id) {
883                         if (nb->bit < (8*len-pad)) {
884                                 val = tvb_get_guint8(tvb, offset + nb->bit/8);
885                                 val &= 0x80 >> (nb->bit%8);
886                                 b0 = (nb->gb0 == -1) ? nb->bit/8 : nb->gb0/8;
887                                 b1 = (nb->gb1 == -1) ? nb->bit/8 : nb->gb1/8;
888                                 proto_tree_add_item(tree, *(nb->p_id), tvb, offset + b0, b1 - b0 + 1, FALSE);
889                         } else {  /* 8.6.2.4 */
890                                 val = 0;
891                                 proto_tree_add_boolean(tree, *(nb->p_id), tvb, offset + len, 0, 0x00);
892                         }
893                         if (val) {
894                                 if (item && nb->tstr)
895                                         proto_item_append_text(item, " %s", nb->tstr);
896                         } else {
897                                 if (item && nb->fstr)
898                                         proto_item_append_text(item, " %s", nb->fstr);
899                         }
900                         nb++;
901                 }
902         }
903
904         return end_offset;
905 }
906
907 int dissect_ber_bitstring32(gboolean implicit_tag, packet_info *pinfo, proto_tree *parent_tree, tvbuff_t *tvb, int offset, int **bit_fields, gint hf_id, gint ett_id, tvbuff_t **out_tvb) 
908 {
909         tvbuff_t *tmp_tvb;
910         proto_tree *tree;
911         guint32 val;
912         int **bf;
913         header_field_info *hfi;
914
915         offset = dissect_ber_bitstring(implicit_tag, pinfo, parent_tree, tvb, offset, NULL, hf_id, ett_id, &tmp_tvb);
916         
917         tree = proto_item_get_subtree(ber_last_created_item);
918         if (bit_fields && tree) {
919                 val = tvb_get_ntohl(tmp_tvb, 0);
920                 bf = bit_fields;
921                 while (*bf) {
922                         proto_tree_add_item(tree, **bf, tmp_tvb, 0, 4, FALSE);
923                         hfi = proto_registrar_get_nth(**bf);
924                         if (val & hfi->bitmask)
925                                 proto_item_append_text(ber_last_created_item, " %s", hfi->name);
926                         bf++;
927                 }
928         }
929
930         if (out_tvb)
931                 *out_tvb = tmp_tvb;
932
933         return offset;
934 }
935
936 void
937 proto_register_ber(void)
938 {
939     static hf_register_info hf[] = {
940         { &hf_ber_id_class, {
941             "Class", "ber.id.class", FT_UINT8, BASE_DEC,
942             VALS(ber_class_codes), 0xc0, "Class of BER TLV Identifier", HFILL }},
943         { &hf_ber_bitstring_padding, {
944             "Padding", "ber.bitstring.padding", FT_UINT8, BASE_DEC,
945             NULL, 0x0, "Number of unsused bits in the last octet of the bitstring", HFILL }},
946         { &hf_ber_id_pc, {
947             "P/C", "ber.id.pc", FT_BOOLEAN, 8,
948             TFS(&ber_pc_codes), 0x20, "Primitive or Constructed BER encoding", HFILL }},
949         { &hf_ber_id_uni_tag, {
950             "Tag", "ber.id.uni_tag", FT_UINT8, BASE_DEC,
951             VALS(ber_uni_tag_codes), 0x1f, "Universal tag type", HFILL }},
952         { &hf_ber_id_tag, {
953             "Tag", "ber.id.tag", FT_UINT32, BASE_DEC,
954             NULL, 0, "Tag value for non-Universal classes", HFILL }},
955         { &hf_ber_length, {
956             "Length", "ber.length", FT_UINT32, BASE_DEC,
957             NULL, 0, "Length of contents", HFILL }},
958
959     };
960
961     static gint *ett[] = {
962         &ett_ber_octet_string,
963     };
964     module_t *ber_module;
965
966     proto_ber = proto_register_protocol("Basic Encoding Rules (ASN.1 X.690)", "BER", "ber");
967     proto_register_field_array(proto_ber, hf, array_length(hf));
968     proto_register_subtree_array(ett, array_length(ett));
969
970         proto_set_cant_toggle(proto_ber);
971
972     /* Register preferences */
973     ber_module = prefs_register_protocol(proto_ber, NULL);
974     prefs_register_bool_preference(ber_module, "show_internals",
975         "Show internal BER encapsulation tokens",
976         "Whether the dissector should also display internal"
977         " ASN.1 BER details such as Identifier and Length fields", &show_internal_ber_fields);
978 }
979
980 void
981 proto_reg_handoff_ber(void)
982 {
983 }