Coverage Report

Created: 2025-08-04 07:15

/src/wireshark/epan/dissectors/packet-per.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
XXX all this offset>>3 and calculations of bytes in the tvb every time
3
we put something in the tree is just silly.  should be replaced with some
4
proper helper routines
5
*/
6
/* packet-per.c
7
 * Routines for dissection of ASN.1 Aligned PER
8
 * 2003  Ronnie Sahlberg
9
 *
10
 * Wireshark - Network traffic analyzer
11
 * By Gerald Combs <gerald@wireshark.org>
12
 * Copyright 1998 Gerald Combs
13
 *
14
 * SPDX-License-Identifier: GPL-2.0-or-later
15
 */
16
17
#include "config.h"
18
19
#include <epan/packet.h>
20
#include <epan/exceptions.h>
21
#include <epan/oids.h>
22
#include <epan/to_str.h>
23
#include <epan/asn1.h>
24
#include <epan/expert.h>
25
#include <wsutil/str_util.h>
26
#include <epan/tfs.h>
27
28
#include <wsutil/array.h>
29
#include <wsutil/ws_padding_to.h>
30
31
#include "packet-per.h"
32
33
void proto_register_per(void);
34
35
static int proto_per;
36
static int hf_per_GeneralString_length;
37
static int hf_per_extension_bit;
38
static int hf_per_extension_present_bit;
39
static int hf_per_choice_index;
40
static int hf_per_choice_extension_index;
41
static int hf_per_enum_index;
42
static int hf_per_enum_extension_index;
43
static int hf_per_num_sequence_extensions;
44
static int hf_per_small_number_bit;
45
static int hf_per_optional_field_bit;
46
static int hf_per_sequence_of_length;
47
static int hf_per_object_identifier_length;
48
static int hf_per_open_type_length;
49
static int hf_per_real_length;
50
static int hf_per_octet_string_length;
51
static int hf_per_bit_string_length;
52
static int hf_per_normally_small_nonnegative_whole_number_length;
53
static int hf_per_const_int_len;
54
static int hf_per_direct_reference; /* T_direct_reference */
55
static int hf_per_indirect_reference; /* T_indirect_reference */
56
static int hf_per_data_value_descriptor; /* T_data_value_descriptor */
57
static int hf_per_encoding; /* External_encoding */
58
static int hf_per_single_ASN1_type; /* T_single_ASN1_type */
59
static int hf_per_octet_aligned; /* T_octet_aligned */
60
static int hf_per_arbitrary; /* T_arbitrary */
61
static int hf_per_integer_length; /* Show integer length if "show internal per fields" */
62
/* static int hf_per_debug_pos; */
63
static int hf_per_internal_range;
64
static int hf_per_internal_num_bits;
65
static int hf_per_internal_min;
66
static int hf_per_internal_value;
67
static int hf_per_internal_min_int;
68
static int hf_per_internal_value_int;
69
70
static int hf_per_encoding_boiler_plate;
71
72
static int ett_per_open_type;
73
static int ett_per_containing;
74
static int ett_per_sequence_of_item;
75
static int ett_per_External;
76
static int ett_per_External_encoding;
77
static int ett_per_named_bits;
78
79
static expert_field ei_per_size_constraint_value;
80
static expert_field ei_per_size_constraint_too_few;
81
static expert_field ei_per_size_constraint_too_many;
82
static expert_field ei_per_choice_extension_unknown;
83
static expert_field ei_per_sequence_extension_unknown;
84
static expert_field ei_per_encoding_error;
85
static expert_field ei_per_oid_not_implemented;
86
static expert_field ei_per_undecoded;
87
static expert_field ei_per_field_not_integer;
88
static expert_field ei_per_external_type;
89
static expert_field ei_per_open_type;
90
static expert_field ei_per_open_type_len;
91
92
static dissector_table_t per_oid_dissector_table;
93
94
/*
95
#define DEBUG_ENTRY(x) \
96
printf("#%u  %s   tvb:0x%08x\n",actx->pinfo->num,x,(int)tvb);
97
*/
98
#define DEBUG_ENTRY(x) \
99
18.2M
  ;
100
101
2.22M
#define BLEN(old_offset, offset) (((offset)>>3)!=((old_offset)>>3)?((offset)>>3)-((old_offset)>>3):1)
102
103
/* whether the PER helpers should put the internal PER fields into the tree
104
   or not.
105
*/
106
static bool display_internal_per_fields;
107
108
109
110
static const true_false_string tfs_extension_bit = {
111
  "Extension bit is set",
112
  "Extension bit is clear"
113
};
114
static const true_false_string tfs_small_number_bit = {
115
  "The number is small, 0-63",
116
  "The number is large, >63"
117
};
118
119
void
120
add_per_encoded_label(tvbuff_t* tvb, packet_info* pinfo _U_, proto_tree* tree)
121
12
{
122
12
  proto_item* ti;
123
124
12
  ti = proto_tree_add_item(tree, hf_per_encoding_boiler_plate, tvb, 0, -1, ENC_NA);
125
12
  proto_item_set_generated(ti);
126
127
12
}
128
129
773k
#define BYTE_ALIGN_OFFSET(offset) if(offset&0x07){offset=(offset&0xfffffff8)+8;}
130
131
2.95M
#define SEQ_MAX_COMPONENTS 128
132
133
static void per_check_value(uint32_t value, uint32_t min_len, uint32_t max_len, asn1_ctx_t *actx, proto_item *item, bool is_signed)
134
4.56M
{
135
4.56M
  if ((is_signed == false) && (value > max_len)) {
136
209k
    expert_add_info_format(actx->pinfo, item, &ei_per_size_constraint_value, "Size constraint: value too big: %u (%u .. %u)", value, min_len, max_len);
137
4.35M
  } else if ((is_signed == true) && ((int32_t)value > (int32_t)max_len)) {
138
9.67k
    expert_add_info_format(actx->pinfo, item, &ei_per_size_constraint_value, "Size constraint: value too big: %d (%d .. %d)", (int32_t)value, (int32_t)min_len, (int32_t)max_len);
139
9.67k
  }
140
4.56M
}
141
142
static void per_check_value64(uint64_t value, uint64_t min_len, uint64_t max_len, asn1_ctx_t *actx, proto_item *item, bool is_signed)
143
2.87k
{
144
2.87k
  if ((is_signed == false) && (value > max_len)) {
145
113
    expert_add_info_format(actx->pinfo, item, &ei_per_size_constraint_value, "Size constraint: value too big: %" PRIu64 " (%" PRIu64 " .. %" PRIu64 ")", value, min_len, max_len);
146
2.75k
  } else if ((is_signed == true) && ((int64_t)value > (int64_t)max_len)) {
147
0
    expert_add_info_format(actx->pinfo, item, &ei_per_size_constraint_value, "Size constraint: value too big: %" PRId64 " (%" PRId64 " .. %" PRId64 ")", (int64_t)value, (int64_t)min_len, (int64_t)max_len);
148
0
  }
149
2.87k
}
150
151
static void per_check_items(uint32_t cnt, int min_len, int max_len, asn1_ctx_t *actx, proto_item *item)
152
380k
{
153
380k
  if (min_len != NO_BOUND && cnt < (uint32_t)min_len) {
154
291
    expert_add_info_format(actx->pinfo, item, &ei_per_size_constraint_too_few, "Size constraint: too few items: %d (%d .. %d)", cnt, min_len, max_len);
155
380k
  } else if (max_len != NO_BOUND && cnt > (uint32_t)max_len) {
156
13.9k
    expert_add_info_format(actx->pinfo, item, &ei_per_size_constraint_too_many, "Size constraint: too many items: %d (%d .. %d)", cnt, min_len, max_len);
157
13.9k
  }
158
380k
}
159
160
161
void dissect_per_not_decoded_yet(proto_tree* tree, packet_info* pinfo, tvbuff_t *tvb, const char* reason)
162
19.6k
{
163
19.6k
  proto_tree_add_expert_format(tree, pinfo, &ei_per_undecoded, tvb, 0, 0, "something unknown here [%s]",reason);
164
19.6k
  col_append_fstr(pinfo->cinfo, COL_INFO, "[UNKNOWN PER: %s]", reason);
165
19.6k
  THROW(ReportedBoundsError);
166
19.6k
}
167
168
/* 10 Encoding procedures -------------------------------------------------- */
169
170
/* 10.2 Open type fields --------------------------------------------------- */
171
static uint32_t
172
dissect_per_open_type_internal(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, void* type_cb, asn1_cb_variant variant)
173
158k
{
174
158k
  int type_length, start_offset, end_offset, fragmented_length = 0, pdu_length, pdu_offset;
175
158k
  tvbuff_t *val_tvb = NULL, *pdu_tvb = NULL, *fragment_tvb = NULL;
176
158k
  header_field_info *hfi;
177
158k
  proto_tree *subtree = tree;
178
158k
  bool is_fragmented;
179
158k
  int captured_pdu_length;
180
181
158k
  hfi = (hf_index <= 0) ? NULL : proto_registrar_get_nth(hf_index);
182
183
158k
  start_offset = offset;
184
158k
  do {
185
158k
    offset = dissect_per_length_determinant(tvb, offset, actx, tree, hf_per_open_type_length, &type_length, &is_fragmented);
186
158k
    if (actx->aligned) BYTE_ALIGN_OFFSET(offset);
187
158k
    if (is_fragmented) {
188
104
      fragment_tvb = tvb_new_octet_aligned(tvb, offset, 8*type_length);
189
104
      if (fragmented_length == 0) {
190
0
        pdu_tvb = tvb_new_composite();
191
0
      }
192
104
      tvb_composite_append(pdu_tvb, fragment_tvb);
193
104
      offset += 8*type_length;
194
104
      fragmented_length += type_length;
195
104
    }
196
158k
  } while (is_fragmented);
197
158k
  if (fragmented_length) {
198
0
    if (type_length) {
199
0
      tvb_composite_append(pdu_tvb, tvb_new_octet_aligned(tvb, offset, 8*type_length));
200
0
      fragmented_length += type_length;
201
0
    }
202
0
    tvb_composite_finalize(pdu_tvb);
203
0
    add_new_data_source(actx->pinfo, pdu_tvb, "Fragmented OCTET STRING");
204
0
    pdu_offset = 0;
205
0
    pdu_length = fragmented_length;
206
158k
  } else {
207
158k
    pdu_tvb = tvb;
208
158k
    pdu_offset = offset;
209
158k
    pdu_length = type_length;
210
158k
  }
211
158k
  end_offset = offset + type_length * 8;
212
213
158k
  if (variant==CB_NEW_DISSECTOR) {
214
153k
    if (fragmented_length) {
215
0
      val_tvb = pdu_tvb;
216
153k
    } else {
217
153k
      if (!pdu_length) {
218
66.3k
        return end_offset;
219
66.3k
      }
220
      /* Check if we have a tvb that holds the whole PDU */
221
87.3k
      captured_pdu_length = tvb_captured_length(pdu_tvb) - (pdu_offset>>3);
222
87.3k
      if(captured_pdu_length < pdu_length){
223
26.0k
        val_tvb = tvb_new_octet_aligned(pdu_tvb, pdu_offset, captured_pdu_length * 8);
224
26.0k
        actx->created_item = proto_tree_add_expert_format(tree, actx->pinfo, &ei_per_open_type_len, tvb, pdu_offset >> 3,
225
26.0k
          captured_pdu_length,"Open type length(%i) > available data(%i)", pdu_length, captured_pdu_length);
226
26.0k
        pdu_length = captured_pdu_length;
227
61.2k
      } else {
228
61.2k
        val_tvb = tvb_new_octet_aligned(pdu_tvb, pdu_offset, pdu_length * 8);
229
61.2k
      }
230
      /* Add new data source if the offset was unaligned */
231
87.3k
      if ((pdu_offset & 7) != 0) {
232
277
        add_new_data_source(actx->pinfo, val_tvb, "Unaligned OCTET STRING");
233
277
      }
234
87.3k
    }
235
87.3k
    if (hfi) {
236
87.3k
      if (FT_IS_UINT(hfi->type)||FT_IS_INT(hfi->type)) {
237
0
        if (FT_IS_UINT(hfi->type))
238
0
          actx->created_item = proto_tree_add_uint(tree, hf_index, val_tvb, 0, pdu_length, pdu_length);
239
0
        else
240
0
          actx->created_item = proto_tree_add_int(tree, hf_index, val_tvb, 0, pdu_length, pdu_length);
241
0
        proto_item_append_text(actx->created_item, plurality(pdu_length, " octet", " octets"));
242
87.3k
      } else {
243
87.3k
        actx->created_item = proto_tree_add_item(tree, hf_index, val_tvb, 0, pdu_length, ENC_BIG_ENDIAN);
244
87.3k
      }
245
87.3k
      subtree = proto_item_add_subtree(actx->created_item, ett_per_open_type);
246
87.3k
    }
247
87.3k
  }
248
249
92.4k
  if (type_cb) {
250
87.5k
    switch (variant) {
251
222
      case CB_ASN1_ENC:
252
222
        ((per_type_fn)type_cb)(pdu_tvb, pdu_offset, actx, tree, hf_index);
253
222
        break;
254
87.3k
      case CB_NEW_DISSECTOR:
255
        /* Pas actx->private_data as "data" to the called function */
256
87.3k
        ((dissector_t)type_cb)(val_tvb, actx->pinfo, subtree, actx->private_data);
257
87.3k
        break;
258
0
      case CB_DISSECTOR_HANDLE:
259
0
        break;
260
87.5k
    }
261
87.5k
  } else {
262
4.88k
    actx->created_item = proto_tree_add_expert(tree, actx->pinfo, &ei_per_open_type, tvb, start_offset>>3, BLEN(start_offset, end_offset));
263
4.88k
  }
264
265
89.0k
  return end_offset;
266
92.4k
}
267
268
uint32_t
269
dissect_per_open_type(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, per_type_fn type_cb)
270
1.30k
{
271
1.30k
  return dissect_per_open_type_internal(tvb, offset, actx, tree, hf_index, (void*)type_cb, CB_ASN1_ENC);
272
1.30k
}
273
274
uint32_t
275
dissect_per_open_type_pdu_new(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, dissector_t type_cb)
276
157k
{
277
157k
  return dissect_per_open_type_internal(tvb, offset, actx, tree, hf_index, (void*)type_cb, CB_NEW_DISSECTOR);
278
157k
}
279
280
/* 10.9 General rules for encoding a length determinant --------------------
281
282
    NOTE 1 - (Tutorial) The procedures of this subclause are invoked when an explicit length field is needed
283
        for some part of the encoding regardless of whether the length count is bounded above
284
        (by PER-visible constraints) or not. The part of the encoding to which the length applies may
285
        be a bit string (with the length count in bits), an octet string (with the length count in octets),
286
        a known-multiplier character string (with the length count in characters), or a list of fields
287
        (with the length count in components of a sequence-of or set-of).
288
289
    NOTE 2 - (Tutorial) In the case of the ALIGNED variant if the length count is bounded above by an upper bound
290
        that is less than 64K, then the constrained whole number encoding is used for the length.
291
        For sufficiently small ranges the result is a bit-field, otherwise the unconstrained length ("n" say)
292
        is encoded into an octet-aligned bit-field in one of three ways (in order of increasing size):
293
    a)  ("n" less than 128) a single octet containing "n" with bit 8 set to zero;
294
    b)  ("n" less than 16K) two octets containing "n" with bit 8 of the first octet set to 1 and bit 7 set to zero;
295
    c)  (large "n") a single octet containing a count "m" with bit 8 set to 1 and bit 7 set to 1.
296
      The count "m" is one to four, and the length indicates that a fragment of the material follows
297
      (a multiple "m" of 16K items). For all values of "m", the fragment is then followed by another length encoding
298
      for the remainder of the material.
299
300
    NOTE 3 - (Tutorial) In the UNALIGNED variant, if the length count is bounded above by an upper bound that is less
301
      than 64K, then the constrained whole number encoding is used to encode the length in the minimum number of
302
      bits necessary to represent the range. Otherwise, the unconstrained length ("n" say) is encoded into a bit
303
      field in the manner described above in Note 2.
304
305
 */
306
uint32_t
307
dissect_per_length_determinant(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, uint32_t *length, bool *is_fragmented)
308
292k
{
309
292k
  uint8_t byte;
310
292k
  uint32_t len;
311
292k
  proto_item *pi;
312
292k
  int num_bits;
313
292k
  int i, bit, str_length, str_index;
314
292k
  bool tmp;
315
316
292k
  if(!length){
317
0
    length=&len;
318
0
  }
319
292k
  if (is_fragmented) {
320
195k
    *is_fragmented = false;
321
195k
  }
322
323
  /* byte aligned */
324
292k
  if (actx->aligned){
325
247k
    BYTE_ALIGN_OFFSET(offset);
326
247k
    byte=tvb_get_uint8(tvb, offset>>3);
327
247k
    offset+=8;
328
247k
  }else{
329
45.0k
    char *str;
330
45.0k
    uint32_t val;
331
332
45.0k
    val = 0;
333
334
    /* prepare the string (max number of bits + quartet separators + prepended space) */
335
45.0k
    str_length = 256+64+1;
336
45.0k
    str=(char *)wmem_alloc(actx->pinfo->pool, str_length+1);
337
45.0k
    str_index = 0;
338
339
45.0k
    str_length = snprintf(str, str_length+1, " ");
340
195k
    for(bit=0;bit<((int)(offset&0x07));bit++){
341
150k
      if(bit&&(!(bit%4))){
342
14.1k
        if (str_index < str_length) str[str_index++] = ' ';
343
14.1k
      }
344
150k
      if (str_index < str_length) str[str_index++] = '.';
345
150k
    }
346
    /* read the bits for the int */
347
45.0k
    num_bits = 8;
348
458k
    for(i=0;i<num_bits;i++){
349
414k
      if(bit&&(!(bit%4))){
350
95.7k
        if (str_index < str_length) str[str_index++] = ' ';
351
95.7k
      }
352
414k
      if(bit&&(!(bit%8))){
353
43.9k
        if (str_index < str_length) str[str_index++] = ' ';
354
43.9k
      }
355
414k
      bit++;
356
414k
      offset=dissect_per_boolean(tvb, offset, actx, tree, -1, &tmp);
357
414k
      val<<=1;
358
414k
      if(tmp){
359
126k
        val|=1;
360
126k
        if (str_index < str_length) str[str_index++] = '1';
361
126k
        if (i==0) { /* bit 8 is 1, so not a single byte length */
362
10.2k
          num_bits = 16;
363
10.2k
        }
364
116k
        else if (i==1 && val==3) { /* bits 8 and 7 both 1, so unconstrained */
365
1.88k
          if (!is_fragmented) {
366
1.46k
            *length = 0;
367
1.46k
            dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "10.9 Unconstrained");
368
1.46k
            return offset;
369
1.46k
          } else {
370
424
            num_bits = 8;
371
424
            *is_fragmented = true;
372
424
          }
373
1.88k
        }
374
288k
      } else {
375
288k
        if (str_index < str_length) str[str_index++] = '0';
376
288k
      }
377
414k
    }
378
43.5k
    str[str_index] = '\0'; /* Terminate string */
379
43.5k
    if(is_fragmented && *is_fragmented==true){
380
413
      *length = val&0x3f;
381
413
      if (*length>4 || *length==0) {
382
356
        *length = 0;
383
356
        *is_fragmented = false;
384
356
        dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "10.9 Unconstrained unexpected fragment count");
385
356
        return offset;
386
356
      }
387
57
      *length *= 0x4000;
388
57
      if(hf_index > 0){
389
57
        pi = proto_tree_add_uint(tree, hf_index, tvb, (offset>>3)-1, 1, *length);
390
57
        if (display_internal_per_fields)
391
0
          proto_item_append_text(pi," %s", str);
392
57
        else
393
57
          proto_item_set_hidden(pi);
394
57
      }
395
396
57
      return offset;
397
413
    }
398
43.1k
    else if((val&0x80)==0 && num_bits==8){
399
34.1k
      *length = val;
400
34.1k
      if(hf_index > 0){
401
34.1k
        pi = proto_tree_add_uint(tree, hf_index, tvb, (offset>>3)-1, 1, *length);
402
34.1k
        if (display_internal_per_fields)
403
0
          proto_item_append_text(pi," %s", str);
404
34.1k
        else
405
34.1k
          proto_item_set_hidden(pi);
406
34.1k
      }
407
408
34.1k
      return offset;
409
34.1k
    }
410
8.99k
    else if (num_bits==16) {
411
8.32k
      *length = val&0x3fff;
412
8.32k
      if(hf_index > 0){
413
8.32k
        pi = proto_tree_add_uint(tree, hf_index, tvb, (offset>>3)-2, 2, *length);
414
8.32k
        if (display_internal_per_fields)
415
0
          proto_item_append_text(pi," %s", str);
416
8.32k
        else
417
8.32k
          proto_item_set_hidden(pi);
418
8.32k
      }
419
420
8.32k
      return offset;
421
8.32k
    }
422
674
    *length = 0;
423
674
    dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "10.9 Unaligned");
424
674
    return offset;
425
426
43.5k
  }
427
428
  /* 10.9.3.6 */
429
247k
  if((byte&0x80)==0){
430
218k
    *length=byte;
431
218k
    if(hf_index > 0){
432
218k
      pi = proto_tree_add_uint(tree, hf_index, tvb, (offset>>3)-1, 1, *length);
433
218k
      if (!display_internal_per_fields) proto_item_set_hidden(pi);
434
218k
    }
435
218k
    return offset;
436
218k
  }
437
438
  /* 10.9.3.7 */
439
29.2k
  if((byte&0xc0)==0x80){
440
20.5k
    *length=(byte&0x3f);
441
20.5k
    *length=((*length)<<8)+tvb_get_uint8(tvb, offset>>3);
442
20.5k
    offset+=8;
443
20.5k
    if(hf_index > 0){
444
20.4k
      pi = proto_tree_add_uint(tree, hf_index, tvb, (offset>>3)-2, 2, *length);
445
20.4k
      if (!display_internal_per_fields) proto_item_set_hidden(pi);
446
20.4k
    }
447
20.5k
    return offset;
448
20.5k
  }
449
  /* 10.9.3.8.1 */
450
8.67k
  else if (is_fragmented){
451
3.79k
    *length = byte&0x3f;
452
3.79k
    if (*length>4 || *length==0) {
453
3.66k
      *length = 0;
454
3.66k
      dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "10.9 Unconstrained unexpected fragment count");
455
3.66k
      return offset;
456
3.66k
    }
457
129
    *length *= 0x4000;
458
129
    *is_fragmented = true;
459
129
    if(hf_index > 0){
460
129
      pi = proto_tree_add_uint(tree, hf_index, tvb, (offset>>3)-1, 1, *length);
461
129
      if (!display_internal_per_fields) proto_item_set_hidden(pi);
462
129
    }
463
129
    return offset;
464
3.79k
  }
465
4.87k
  *length = 0;
466
4.87k
  dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "10.9.3.8.1");
467
4.87k
  return offset;
468
29.2k
}
469
470
/* 10.6   normally small non-negative whole number */
471
static uint32_t
472
dissect_per_normally_small_nonnegative_whole_number(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, uint32_t *length)
473
55.4k
{
474
55.4k
  bool small_number, length_bit;
475
55.4k
  uint32_t len, length_determinant;
476
55.4k
  proto_item *pi;
477
478
55.4k
DEBUG_ENTRY("dissect_per_normally_small_nonnegative_whole_number");
479
55.4k
  if(!length){
480
0
    length=&len;
481
0
  }
482
483
55.4k
  offset=dissect_per_boolean(tvb, offset, actx, tree, hf_per_small_number_bit, &small_number);
484
55.4k
  if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
485
55.4k
  if(!small_number){
486
43.1k
    int i;
487
    /* 10.6.1 */
488
43.1k
    *length=0;
489
301k
    for(i=0;i<6;i++){
490
258k
      offset=dissect_per_boolean(tvb, offset, actx, tree, -1, &length_bit);
491
258k
      *length<<=1;
492
258k
      if (length_bit) {
493
69.1k
        *length|=1;
494
69.1k
      }
495
258k
    }
496
43.1k
    if(hf_index > 0){
497
43.0k
      pi = proto_tree_add_uint(tree, hf_index, tvb, (offset-6)>>3, (offset%8<6)?2:1, *length);
498
43.0k
      if (!display_internal_per_fields) proto_item_set_hidden(pi);
499
43.0k
    }
500
43.1k
    return offset;
501
43.1k
  }
502
503
  /* 10.6.2 */
504
12.2k
  offset=dissect_per_length_determinant(tvb, offset, actx, tree, hf_per_normally_small_nonnegative_whole_number_length, &length_determinant, NULL);
505
12.2k
  switch (length_determinant) {
506
6.15k
    case 0:
507
6.15k
      *length = 0;
508
6.15k
      break;
509
356
    case 1:
510
356
      *length = tvb_get_bits8(tvb, offset, 8);
511
356
      offset += 8;
512
356
      break;
513
210
    case 2:
514
210
      *length = tvb_get_bits16(tvb, offset, 16, ENC_BIG_ENDIAN);
515
210
      offset += 16;
516
210
      break;
517
406
    case 3:
518
406
      *length = tvb_get_bits32(tvb, offset, 24, ENC_BIG_ENDIAN);
519
406
      offset += 24;
520
406
      break;
521
113
    case 4:
522
113
      *length = tvb_get_bits32(tvb, offset, 32, ENC_BIG_ENDIAN);
523
113
      offset += 32;
524
113
      break;
525
2.89k
    default:
526
2.89k
      dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "too long integer(per_normally_small_nonnegative_whole_number)");
527
2.89k
      offset += 8*length_determinant;
528
2.89k
      *length = 0;
529
2.89k
      return offset;
530
12.2k
  }
531
7.16k
  if(hf_index > 0){
532
7.16k
    pi = proto_tree_add_uint(tree, hf_index, tvb, (offset-(8*length_determinant))>>3, length_determinant, *length);
533
7.16k
    if (!display_internal_per_fields) proto_item_set_hidden(pi);
534
7.16k
  }
535
536
7.16k
  return offset;
537
12.2k
}
538
539
540
541
/* this function reads a GeneralString */
542
/* currently based on pure guesswork since RFC2833 didn't tell me much
543
   i guess that the PER encoding for this is a normally-small-whole-number
544
   followed by a ascii string.
545
546
   based on pure guesswork.  it looks ok in the only capture i have where
547
   there is a 1 byte general string encoded
548
*/
549
uint32_t
550
dissect_per_GeneralString(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index)
551
0
{
552
0
  uint32_t length;
553
554
0
  offset=dissect_per_length_determinant(tvb, offset, actx, tree, hf_per_GeneralString_length, &length, NULL);
555
556
0
  proto_tree_add_item(tree, hf_index, tvb, offset>>3, length, ENC_BIG_ENDIAN);
557
558
0
  offset+=length*8;
559
560
0
  return offset;
561
0
}
562
563
/* 17 Encoding the null type */
564
uint32_t
565
226k
dissect_per_null(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx _U_, proto_tree *tree, int hf_index) {
566
226k
  proto_item *ti_tmp;
567
568
226k
  ti_tmp = proto_tree_add_item(tree, hf_index, tvb, offset>>3, 0, ENC_BIG_ENDIAN);
569
226k
  proto_item_append_text(ti_tmp, ": NULL");
570
571
226k
  return offset;
572
226k
}
573
574
/* 19 this function dissects a sequence of */
575
// Arbitrary. Allow a sequence of NULLs, but not too many since we might add
576
// a hierarchy of tree items per NULL
577
2.42M
#define PER_SEQUENCE_OF_MAX_NULLS 10
578
static uint32_t
579
dissect_per_sequence_of_helper(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, per_type_fn func, int hf_index, uint32_t length)
580
399k
{
581
399k
  uint32_t i;
582
583
399k
DEBUG_ENTRY("dissect_per_sequence_of_helper");
584
399k
  uint32_t old_offset = offset;
585
2.82M
  for(i=0;i<length;i++){
586
2.42M
    uint32_t lold_offset=offset;
587
2.42M
    proto_item *litem;
588
2.42M
    proto_tree *ltree;
589
590
2.42M
    ltree=proto_tree_add_subtree_format(tree, tvb, offset>>3, 0, ett_per_sequence_of_item, &litem, "Item %d", i);
591
592
2.42M
    offset=(*func)(tvb, offset, actx, ltree, hf_index);
593
2.42M
    proto_item_set_len(litem, (offset>>3)!=(lold_offset>>3)?(offset>>3)-(lold_offset>>3):1);
594
2.42M
    if (i >= PER_SEQUENCE_OF_MAX_NULLS-1 && offset <= old_offset) {
595
1
      dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "too many nulls in sequence");
596
1
    }
597
2.42M
  }
598
599
399k
  return offset;
600
399k
}
601
uint32_t
602
dissect_per_sequence_of(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *parent_tree, int hf_index, int ett_index, const per_sequence_t *seq)
603
19.5k
{
604
19.5k
  proto_item *item;
605
19.5k
  proto_tree *tree;
606
19.5k
  uint32_t old_offset=offset;
607
19.5k
  uint32_t length;
608
19.5k
  header_field_info *hfi;
609
610
19.5k
DEBUG_ENTRY("dissect_per_sequence_of");
611
612
  /* semi-constrained whole number for number of elements */
613
  /* each element encoded as 10.9 */
614
615
19.5k
  offset=dissect_per_length_determinant(tvb, offset, actx, parent_tree, hf_per_sequence_of_length, &length, NULL);
616
617
19.5k
  hfi = proto_registrar_get_nth(hf_index);
618
19.5k
  if (FT_IS_UINT(hfi->type)) {
619
18.5k
    item = proto_tree_add_uint(parent_tree, hf_index, tvb, old_offset>>3, 0, length);
620
18.5k
    proto_item_append_text(item, (length==1)?" item":" items");
621
18.5k
  } else {
622
977
    item=proto_tree_add_item(parent_tree, hf_index, tvb, old_offset>>3, 0, ENC_BIG_ENDIAN);
623
977
  }
624
19.5k
  tree=proto_item_add_subtree(item, ett_index);
625
626
19.5k
  offset=dissect_per_sequence_of_helper(tvb, offset, actx, tree, seq->func, *seq->p_id, length);
627
628
629
19.5k
  proto_item_set_len(item, (offset>>3)!=(old_offset>>3)?(offset>>3)-(old_offset>>3):1);
630
19.5k
  return offset;
631
19.5k
}
632
633
/* XXX we don't do >64k length strings   yet */
634
static uint32_t
635
dissect_per_restricted_character_string_sorted(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, int min_len, int max_len, bool has_extension, uint16_t lb, uint16_t ub, const char *alphabet, int alphabet_length, tvbuff_t **value_tvb)
636
17.3k
{
637
17.3k
  uint32_t length;
638
17.3k
  bool byte_aligned, use_canonical_order;
639
17.3k
  wmem_strbuf_t *buf;
640
17.3k
  int str_len;
641
17.3k
  char *str;
642
17.3k
  unsigned char_pos;
643
17.3k
  int bits_per_char;
644
17.3k
  uint32_t old_offset;
645
646
17.3k
DEBUG_ENTRY("dissect_per_restricted_character_string");
647
648
  /* xx.x if the length is 0 bytes there will be no encoding */
649
17.3k
  if(max_len==0){
650
0
    if (value_tvb) {
651
0
      *value_tvb = tvb_new_child_real_data(tvb, NULL, 0, 0);
652
0
    }
653
0
    return offset;
654
0
  }
655
656
657
17.3k
  if (min_len == NO_BOUND) {
658
3.03k
    min_len=0;
659
3.03k
  }
660
661
662
  /* 27.5.2 depending of the alphabet length, find how many bits
663
     are used to encode each character */
664
/* unaligned PER */
665
17.3k
  if (actx->aligned){
666
667
15.9k
    if(alphabet_length<=2){
668
0
      bits_per_char=1;
669
15.9k
    } else if(alphabet_length<=4){
670
0
      bits_per_char=2;
671
15.9k
    } else if(alphabet_length<=16){
672
10.4k
      bits_per_char=4;
673
10.4k
    } else {
674
5.51k
      bits_per_char=8;
675
5.51k
    }
676
15.9k
  }else{
677
1.47k
    if(alphabet_length<=2){
678
0
      bits_per_char=1;
679
1.47k
    } else if(alphabet_length<=4){
680
0
      bits_per_char=2;
681
1.47k
    } else if(alphabet_length<=8){
682
0
      bits_per_char=3;
683
1.47k
    } else if(alphabet_length<=16){
684
2
      bits_per_char=4;
685
1.46k
    } else if(alphabet_length<=32){
686
0
      bits_per_char=5;
687
1.46k
    } else if(alphabet_length<=64){
688
113
      bits_per_char=6;
689
1.35k
    } else if(alphabet_length<=128){
690
1.35k
      bits_per_char=7;
691
1.35k
    } else {
692
0
      bits_per_char=8;
693
0
    }
694
1.47k
  }
695
696
  /* 27.4 If the type is extensible for PER encodings (see 9.3.16),
697
   * then a bit-field consisting of a single bit shall be added to the field-list.
698
   * The single bit shall be set to zero if the value is within the range of the extension root,
699
   * and to one otherwise. If the value is outside the range of the extension root,
700
   * then the following encoding shall be as if there was no effective size constraint,
701
   * and shall have an effective permitted-alphabet constraint that consists of the set of characters
702
   * of the unconstrained type.
703
   *  NOTE - Only the known-multiplier character string types can be extensible for PER encodings.
704
   * Extensibility markers on other character string types do not affect the PER encoding.
705
   */
706
707
17.3k
  if (has_extension) {
708
280
    bool extension_present;
709
280
    offset = dissect_per_boolean(tvb, offset, actx, tree, hf_per_extension_present_bit, &extension_present);
710
280
    if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
711
280
    if(extension_present){
712
31
      min_len = NO_BOUND;
713
31
      max_len = NO_BOUND;
714
31
    }
715
280
  }
716
717
17.3k
  byte_aligned=true;
718
17.3k
  if((min_len==max_len)&&(max_len<=2)){
719
31
    byte_aligned=false;
720
31
  }
721
17.3k
  if ((max_len != NO_BOUND) && (max_len < 2)) {
722
0
    byte_aligned=false;
723
0
  }
724
725
  /* xx.x */
726
17.3k
  length=max_len;
727
17.3k
  old_offset = offset;
728
17.3k
  if (max_len == NO_BOUND) {
729
3.06k
    offset = dissect_per_length_determinant(tvb, offset, actx, tree, hf_per_octet_string_length, &length, NULL);
730
    /* the unconstrained strings are always byte aligned (27.6.3)*/
731
3.06k
    byte_aligned=true;
732
14.3k
  } else if(min_len!=max_len){
733
12.4k
    offset=dissect_per_constrained_integer(tvb, offset, actx,
734
12.4k
      tree, hf_per_octet_string_length, min_len, max_len,
735
12.4k
      &length, false);
736
12.4k
      if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
737
12.4k
  }
738
739
17.3k
  if(!length){
740
    /* there is no string at all, so don't do any byte alignment */
741
    /* byte_aligned=false; */
742
    /* Advance offset to next 'element' */
743
677
    if (offset == old_offset)
744
0
      offset = offset + 1;
745
677
  }
746
747
17.3k
  if((byte_aligned)&&(actx->aligned)){
748
15.5k
    BYTE_ALIGN_OFFSET(offset);
749
15.5k
  }
750
751
  /* 30.5: if "ub" is less than or equal to 2^b-1, then "v" is the value specified in above , else
752
     the characters are placed in the canonical order defined in ITU-T Rec. X.680 | ISO/IEC 8824-1,
753
     clause 43. The first is assigned the value zero and the next in canonical order is assigned a value
754
     that is one greater than the value assigned to the previous character in the canonical order. These are the values "v" */
755
17.3k
  use_canonical_order = (ub <= ((uint16_t)(1<<bits_per_char)-1)) ? false : true;
756
757
17.3k
  buf = wmem_strbuf_new_len(actx->pinfo->pool, NULL, length);
758
17.3k
  old_offset=offset;
759
346k
  for(char_pos=0;char_pos<length;char_pos++){
760
329k
    unsigned char val;
761
329k
    int i;
762
329k
    bool bit;
763
764
329k
    val=0;
765
2.31M
    for(i=0;i<bits_per_char;i++){
766
1.98M
      offset=dissect_per_boolean(tvb, offset, actx, tree, -1, &bit);
767
1.98M
      val=(val<<1)|bit;
768
1.98M
    }
769
329k
    if(use_canonical_order == false){
770
172k
      if (val > ub || val < lb) {
771
31.1k
        wmem_strbuf_append_unichar_repl(buf);
772
141k
      } else {
773
141k
        wmem_strbuf_append_c(buf, val);
774
141k
      }
775
172k
    } else {
776
157k
      if (val < alphabet_length){
777
143k
        wmem_strbuf_append_c(buf, alphabet[val]);
778
143k
      } else {
779
13.3k
        wmem_strbuf_append_unichar_repl(buf);
780
13.3k
      }
781
157k
    }
782
329k
  }
783
17.3k
  str_len = (int)wmem_strbuf_get_len(buf);
784
17.3k
  str = wmem_strbuf_finalize(buf);
785
  /* Note that str can contain embedded nulls. Length claims any bytes partially used.  */
786
17.3k
  proto_tree_add_string(tree, hf_index, tvb, (old_offset>>3), ((offset+7)>>3)-(old_offset>>3), str);
787
17.3k
  if (value_tvb) {
788
9.41k
    *value_tvb = tvb_new_child_real_data(tvb, str, str_len, str_len);
789
9.41k
  }
790
17.3k
  return offset;
791
17.3k
}
792
793
static const char*
794
sort_alphabet(char *sorted_alphabet, const char *alphabet, int alphabet_length, uint16_t *lb, uint16_t *ub)
795
10.6k
{
796
10.6k
  int i, j;
797
10.6k
  unsigned char c, c_max, c_min;
798
10.6k
  char tmp_buf[256];
799
800
  /*
801
   * XXX - presumably all members of alphabet will be in the
802
   * range 0 to 127. asn2wrs.py doesn't properly handle the
803
   * Quadruple or CharacterStringList types needed for other
804
   * characters, nor representing characters outside ASCII
805
   * in the "cstring" notation (possibly in UTF-8?)
806
   */
807
10.6k
  if (!alphabet_length) return sorted_alphabet;
808
10.6k
  memset(tmp_buf, 0, 256);
809
10.6k
  c_min = c_max = (unsigned char)alphabet[0];
810
162k
  for (i=0; i<alphabet_length; i++) {
811
151k
    c = (unsigned char)alphabet[i];
812
151k
    tmp_buf[c] = 1;
813
151k
    if (c > c_max) c_max = c;
814
51.1k
    else if (c < c_min) c_min = c;
815
151k
  }
816
278k
  for (i=c_min,j=0; i<=c_max; i++) {
817
267k
    if (tmp_buf[i]) sorted_alphabet[j++] = i;
818
267k
  }
819
10.6k
  *lb = (uint16_t)c_min;
820
10.6k
  *ub = (uint16_t)c_max;
821
10.6k
  return sorted_alphabet;
822
10.6k
}
823
824
uint32_t
825
dissect_per_restricted_character_string(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, int min_len, int max_len, bool has_extension, const char *alphabet, int alphabet_length, tvbuff_t **value_tvb)
826
10.6k
{
827
10.6k
  const char *alphabet_ptr;
828
10.6k
  char sorted_alphabet[128];
829
10.6k
  uint16_t lb = 0;
830
10.6k
  uint16_t ub = 65535;
831
832
  /* XXX: We don't handle permitted-alphabet characters outside the
833
   * ASCII range if used in BMPString (UCS2) or UniversalString (UCS4)
834
   */
835
10.6k
  if (alphabet_length > 127) {
836
0
    alphabet_ptr = alphabet;
837
10.6k
  } else {
838
10.6k
    alphabet_ptr = sort_alphabet(sorted_alphabet, alphabet, alphabet_length, &lb, &ub);
839
10.6k
  }
840
841
  /* This is for a restricted character string type with a permitted-
842
   * alphabet constraint type. Such constraints are only PER-visible for
843
   * the known-multiplier character string types.
844
   */
845
846
10.6k
  return dissect_per_restricted_character_string_sorted(tvb, offset, actx, tree, hf_index, min_len, max_len, has_extension, lb, ub, alphabet_ptr, alphabet_length, value_tvb);
847
10.6k
}
848
849
uint32_t
850
dissect_per_IA5String(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, int min_len, int max_len, bool has_extension, tvbuff_t **value_tvb)
851
3.43k
{
852
3.43k
  offset=dissect_per_restricted_character_string_sorted(tvb, offset, actx, tree, hf_index, min_len, max_len, has_extension,
853
3.43k
    0, 127, NULL, 128, value_tvb);
854
855
3.43k
  return offset;
856
3.43k
}
857
858
uint32_t
859
dissect_per_NumericString(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, int min_len, int max_len, bool has_extension, tvbuff_t **value_tvb)
860
2
{
861
2
  offset=dissect_per_restricted_character_string_sorted(tvb, offset, actx, tree, hf_index, min_len, max_len, has_extension,
862
2
    32, 57, " 0123456789", 11, value_tvb);
863
864
2
  return offset;
865
2
}
866
uint32_t
867
dissect_per_PrintableString(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, int min_len, int max_len, bool has_extension, tvbuff_t **value_tvb)
868
363
{
869
363
  offset=dissect_per_restricted_character_string_sorted(tvb, offset, actx, tree, hf_index, min_len, max_len, has_extension,
870
363
    32, 122, " '()+,-.*0123456789:=?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 74, value_tvb);
871
363
  return offset;
872
363
}
873
uint32_t
874
dissect_per_VisibleString(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, int min_len, int max_len, bool has_extension, tvbuff_t **value_tvb)
875
2.94k
{
876
2.94k
  offset=dissect_per_restricted_character_string_sorted(tvb, offset, actx, tree, hf_index, min_len, max_len, has_extension,
877
2.94k
    32, 126, " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", 95, value_tvb);
878
2.94k
  return offset;
879
2.94k
}
880
uint32_t
881
dissect_per_BMPString(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, int min_len, int max_len, bool has_extension _U_)
882
2.51k
{
883
2.51k
  uint32_t length;
884
885
  /* xx.x if the length is 0 bytes there will be no encoding */
886
2.51k
  if(max_len==0){
887
0
    return offset;
888
0
  }
889
890
891
2.51k
  if (min_len == NO_BOUND) {
892
13
    min_len = 0;
893
13
  }
894
895
896
  /* xx.x */
897
2.51k
  length=max_len;
898
2.51k
  if(min_len!=max_len){
899
2.51k
    offset=dissect_per_constrained_integer(tvb, offset, actx,
900
2.51k
      tree, hf_per_octet_string_length, min_len, max_len,
901
2.51k
      &length, false);
902
2.51k
    if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
903
2.51k
  }
904
905
906
  /* align to byte boundary */
907
2.51k
  BYTE_ALIGN_OFFSET(offset);
908
909
2.51k
  if(length>=1024){
910
8
    dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "BMPString too long");
911
8
    length=1024;
912
8
  }
913
914
2.51k
  proto_tree_add_item(tree, hf_index, tvb, offset>>3, length*2, ENC_UCS_2|ENC_BIG_ENDIAN);
915
916
2.51k
  offset+=(length<<3)*2;
917
918
2.51k
  return offset;
919
2.51k
}
920
uint32_t
921
dissect_per_UTF8String(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, int min_len _U_, int max_len _U_, bool has_extension _U_)
922
246
{
923
246
  tvbuff_t *val_tvb;
924
246
  uint32_t  length;
925
926
  /* UTF8String is not a known-multiplier character string (UTF8
927
   * characters are variable width.) Hence subclause 27.6 applies,
928
   * and "constraints are never PER-visible, and the type can never
929
   * be extensible for PER encoding."
930
   */
931
932
  /* 27.6.3 unconstrained length determinant with "n" in octets */
933
246
  offset = dissect_per_length_determinant(tvb, offset, actx, tree, hf_per_octet_string_length, &length, NULL);
934
935
246
  if(length){
936
937
    /* Unnecessary because the length determinant is aligned. */
938
79
    if(actx->aligned) {
939
8
      BYTE_ALIGN_OFFSET(offset);
940
8
    }
941
942
79
    val_tvb = tvb_new_octet_aligned(tvb, offset, length * 8);
943
    /* Add new data source if the offset was unaligned */
944
79
    if ((offset & 7) != 0) {
945
57
      add_new_data_source(actx->pinfo, val_tvb, "Unaligned UTF8String");
946
57
    }
947
948
79
    proto_tree_add_item(tree, hf_index, val_tvb, 0, length, ENC_UTF_8);
949
167
  } else {
950
    /* tvb_new_octet_aligned doesn't like zero length.
951
     * length zero indicates a present but empty string, so add it
952
     */
953
167
    proto_tree_add_item(tree, hf_index, tvb, (offset-1)>>3, length, ENC_UTF_8);
954
167
  }
955
956
246
  offset+=(length<<3);
957
958
246
  return offset;
959
246
}
960
961
uint32_t
962
dissect_per_object_descriptor(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, tvbuff_t **value_tvb)
963
1
{
964
1
  offset=dissect_per_octet_string(tvb, offset, actx, tree, hf_index, -1, -1, false, value_tvb);
965
966
1
  return offset;
967
1
}
968
969
970
/* this function dissects a constrained sequence of */
971
uint32_t
972
dissect_per_constrained_sequence_of(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *parent_tree, int hf_index, int ett_index, const per_sequence_t *seq, int min_len, int max_len, bool has_extension)
973
382k
{
974
382k
  proto_item *item;
975
382k
  proto_tree *tree;
976
382k
  uint32_t old_offset=offset;
977
382k
  uint32_t length;
978
382k
  header_field_info *hfi;
979
980
382k
DEBUG_ENTRY("dissect_per_constrained_sequence_of");
981
982
  /* 19.4 If there is a PER-visible constraint and an extension marker is present in it,
983
   * a single bit shall be added to the field-list in a bit-field of length one
984
   */
985
382k
  if (has_extension) {
986
2.74k
    bool extension_present;
987
2.74k
    offset=dissect_per_boolean(tvb, offset, actx, parent_tree, hf_per_extension_present_bit, &extension_present);
988
2.74k
    if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
989
2.74k
    if (extension_present){
990
      /* 10.9 shall be invoked to add the length determinant as a semi-constrained whole number to the field-list,
991
       * followed by the component values
992
       */
993
879
      offset = dissect_per_length_determinant(tvb, offset, actx, parent_tree, hf_per_sequence_of_length, &length, NULL);
994
879
      goto call_sohelper;
995
879
    }
996
2.74k
  }
997
998
  /* 19.5 if min==max and min,max<64k ==> no length determinant */
999
381k
  if((min_len==max_len) && (min_len<65536)){
1000
27.1k
    length=min_len;
1001
27.1k
    goto call_sohelper;
1002
27.1k
  }
1003
1004
  /* 19.6 ub>=64k or unset */
1005
354k
  if ((max_len >= 65536) || (max_len == NO_BOUND)) {
1006
    /* no constraint, see 10.9.4.2 */
1007
1.95k
    offset=dissect_per_length_determinant(tvb, offset, actx, parent_tree, hf_per_sequence_of_length, &length, NULL);
1008
1.95k
    goto call_sohelper;
1009
1.95k
  }
1010
1011
  /* constrained whole number for number of elements */
1012
352k
  offset=dissect_per_constrained_integer(tvb, offset, actx,
1013
352k
    parent_tree, hf_per_sequence_of_length, min_len, max_len,
1014
352k
    &length, false);
1015
352k
  if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
1016
1017
380k
call_sohelper:
1018
380k
  hfi = proto_registrar_get_nth(hf_index);
1019
380k
  if (FT_IS_UINT(hfi->type)) {
1020
380k
    item = proto_tree_add_uint(parent_tree, hf_index, tvb, offset>>3, 0, length);
1021
380k
    proto_item_append_text(item, (length==1)?" item":" items");
1022
380k
  } else {
1023
0
    item=proto_tree_add_item(parent_tree, hf_index, tvb, offset>>3, 0, ENC_BIG_ENDIAN);
1024
0
  }
1025
380k
  tree=proto_item_add_subtree(item, ett_index);
1026
380k
  per_check_items(length, min_len, max_len, actx, item);
1027
1028
380k
  old_offset = offset;
1029
380k
  offset=dissect_per_sequence_of_helper(tvb, offset, actx, tree, seq->func, *seq->p_id, length);
1030
1031
380k
  if (offset == old_offset)
1032
1.41k
    length = 0;
1033
379k
  else if (offset >> 3 == old_offset >> 3)
1034
33.4k
      length = 1;
1035
345k
    else
1036
345k
      length = (offset >> 3) - (old_offset >> 3);
1037
1038
380k
  proto_item_set_len(item, length);
1039
380k
  return offset;
1040
352k
}
1041
1042
/* this function dissects a constrained set of */
1043
uint32_t
1044
dissect_per_constrained_set_of(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *parent_tree, int hf_index, int ett_index, const per_sequence_t *seq, int min_len, int max_len, bool has_extension)
1045
8
{
1046
  /* for basic-per  a set-of is encoded in the same way as a sequence-of */
1047
8
DEBUG_ENTRY("dissect_per_constrained_set_of");
1048
8
  offset=dissect_per_constrained_sequence_of(tvb, offset, actx, parent_tree, hf_index, ett_index, seq, min_len, max_len, has_extension);
1049
8
  return offset;
1050
8
}
1051
1052
1053
1054
1055
1056
1057
/* this function dissects a set of */
1058
uint32_t
1059
dissect_per_set_of(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *parent_tree, int hf_index, int ett_index, const per_sequence_t *seq)
1060
0
{
1061
  /* for basic-per  a set-of is encoded in the same way as a sequence-of */
1062
0
DEBUG_ENTRY("dissect_per_set_of");
1063
0
  offset=dissect_per_sequence_of(tvb, offset, actx, parent_tree, hf_index, ett_index, seq);
1064
0
  return offset;
1065
0
}
1066
1067
1068
1069
1070
/* 23 Encoding the object identifier type */
1071
static uint32_t
1072
dissect_per_any_oid(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, tvbuff_t **value_tvb,
1073
        bool is_absolute)
1074
17.5k
{
1075
17.5k
  unsigned length;
1076
17.5k
  const char *str;
1077
17.5k
  tvbuff_t *val_tvb = NULL;
1078
17.5k
  header_field_info *hfi;
1079
1080
17.5k
  DEBUG_ENTRY("dissect_per_any_oid");
1081
1082
17.5k
  offset = dissect_per_length_determinant(tvb, offset, actx, tree, hf_per_object_identifier_length, &length, NULL);
1083
17.5k
  if(length == 0){
1084
2.08k
    dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "unexpected length");
1085
2.08k
  }
1086
17.5k
  if (actx->aligned) BYTE_ALIGN_OFFSET(offset);
1087
17.5k
  val_tvb = tvb_new_octet_aligned(tvb, offset, length * 8);
1088
  /* Add new data source if the offet was unaligned */
1089
17.5k
  if ((offset & 7) != 0) {
1090
77
    add_new_data_source(actx->pinfo, val_tvb, "Unaligned OCTET STRING");
1091
77
  }
1092
1093
17.5k
  hfi = proto_registrar_get_nth(hf_index);
1094
17.5k
  if ((is_absolute && hfi->type == FT_OID) || (is_absolute && hfi->type == FT_REL_OID)) {
1095
13.1k
    actx->created_item = proto_tree_add_item(tree, hf_index, val_tvb, 0, length, ENC_BIG_ENDIAN);
1096
13.1k
  } else if (FT_IS_STRING(hfi->type)) {
1097
0
    str = oid_encoded2string(actx->pinfo->pool, tvb_get_ptr(val_tvb, 0, length), length);
1098
0
    actx->created_item = proto_tree_add_string(tree, hf_index, val_tvb, 0, length, str);
1099
4.44k
  } else {
1100
4.44k
    DISSECTOR_ASSERT_NOT_REACHED();
1101
4.44k
  }
1102
1103
17.5k
  if (value_tvb) *value_tvb = val_tvb;
1104
1105
17.5k
  offset += 8 * length;
1106
1107
17.5k
  return offset;
1108
17.5k
}
1109
1110
uint32_t
1111
dissect_per_object_identifier(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, tvbuff_t **value_tvb)
1112
15.4k
{
1113
15.4k
  return dissect_per_any_oid(tvb, offset, actx, tree, hf_index, value_tvb, true);
1114
15.4k
}
1115
1116
uint32_t
1117
dissect_per_relative_oid(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, tvbuff_t **value_tvb)
1118
0
{
1119
0
  return dissect_per_any_oid(tvb, offset, actx, tree, hf_index, value_tvb, false);
1120
0
}
1121
1122
static uint32_t
1123
dissect_per_any_oid_str(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, const char **value_stringx,
1124
      bool is_absolute)
1125
2.10k
{
1126
2.10k
  tvbuff_t *value_tvb = NULL;
1127
2.10k
  unsigned length;
1128
1129
2.10k
  offset = dissect_per_any_oid(tvb, offset, actx, tree, hf_index, (value_stringx) ? &value_tvb : NULL, is_absolute);
1130
1131
2.10k
  if (value_stringx) {
1132
1.52k
    if (value_tvb && (length = tvb_captured_length(value_tvb))) {
1133
1.52k
      *value_stringx = oid_encoded2string(actx->pinfo->pool, tvb_get_ptr(value_tvb, 0, length), length);
1134
1.52k
    } else {
1135
0
      *value_stringx = "";
1136
0
    }
1137
1.52k
  }
1138
1139
2.10k
  return offset;
1140
2.10k
}
1141
1142
uint32_t
1143
dissect_per_object_identifier_str(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, const char **value_stringx)
1144
2.10k
{
1145
2.10k
  return dissect_per_any_oid_str(tvb, offset, actx, tree, hf_index, value_stringx, true);
1146
2.10k
}
1147
1148
uint32_t
1149
dissect_per_relative_oid_str(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, const char **value_stringx)
1150
0
{
1151
0
  return dissect_per_any_oid_str(tvb, offset, actx, tree, hf_index, value_stringx, false);
1152
0
}
1153
1154
1155
/* this function reads a single bit */
1156
uint32_t
1157
dissect_per_boolean(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, bool *bool_val)
1158
8.48M
{
1159
8.48M
  uint8_t ch, mask;
1160
8.48M
  bool value;
1161
8.48M
  header_field_info *hfi;
1162
1163
8.48M
DEBUG_ENTRY("dissect_per_boolean");
1164
1165
8.48M
  ch=tvb_get_uint8(tvb, offset>>3);
1166
8.48M
  mask=1<<(7-(offset&0x07));
1167
8.48M
  if(ch&mask){
1168
2.80M
    value=1;
1169
5.67M
  } else {
1170
5.67M
    value=0;
1171
5.67M
  }
1172
8.48M
  if(hf_index > 0){
1173
5.76M
    char bits[10];
1174
5.76M
    bits[0] = mask&0x80?'0'+value:'.';
1175
5.76M
    bits[1] = mask&0x40?'0'+value:'.';
1176
5.76M
    bits[2] = mask&0x20?'0'+value:'.';
1177
5.76M
    bits[3] = mask&0x10?'0'+value:'.';
1178
5.76M
    bits[4] = ' ';
1179
5.76M
    bits[5] = mask&0x08?'0'+value:'.';
1180
5.76M
    bits[6] = mask&0x04?'0'+value:'.';
1181
5.76M
    bits[7] = mask&0x02?'0'+value:'.';
1182
5.76M
    bits[8] = mask&0x01?'0'+value:'.';
1183
5.76M
    bits[9] = '\0';
1184
1185
5.76M
    hfi = proto_registrar_get_nth(hf_index);
1186
5.76M
    actx->created_item = proto_tree_add_boolean_format(tree, hf_index, tvb, offset>>3, 1, value,
1187
5.76M
                   "%s %s: %s", bits, hfi->name,
1188
5.76M
                   value?"True":"False");
1189
5.76M
  } else {
1190
2.71M
    actx->created_item = NULL;
1191
2.71M
  }
1192
1193
8.48M
  if(bool_val){
1194
8.00M
    *bool_val=value;
1195
8.00M
  }
1196
8.48M
  return offset+1;
1197
8.48M
}
1198
1199
/* we currently only handle integers up to 32 bits in length. */
1200
uint32_t
1201
dissect_per_integer(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, int32_t *value)
1202
2.19k
{
1203
2.19k
  uint32_t i, length;
1204
2.19k
  uint32_t val;
1205
2.19k
  tvbuff_t *val_tvb=NULL;
1206
2.19k
  proto_item *it=NULL;
1207
2.19k
  header_field_info *hfi;
1208
1209
  /* 12.2.6 b */
1210
2.19k
  offset=dissect_per_length_determinant(tvb, offset, actx, tree,hf_per_integer_length, &length, NULL);
1211
  /* gassert here? */
1212
2.19k
  if(length>4){
1213
819
    dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "too long integer(per_integer)");
1214
819
    length=4;
1215
819
  }
1216
1217
2.19k
  if(length == 0){
1218
262
    dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "unexpected length");
1219
262
  }
1220
1221
2.19k
  if (actx->aligned) BYTE_ALIGN_OFFSET(offset);
1222
2.19k
  val_tvb = tvb_new_octet_aligned(tvb, offset, length * 8);
1223
2.19k
  val=0;
1224
4.07k
  for(i=0;i<length;i++){
1225
1.88k
    if(i==0){
1226
719
      if(tvb_get_uint8(val_tvb, i)&0x80){
1227
        /* negative number */
1228
79
        val=0xffffffff;
1229
640
      } else {
1230
        /* positive number */
1231
640
        val=0;
1232
640
      }
1233
719
    }
1234
1.88k
    val=(val<<8)|tvb_get_uint8(val_tvb,i);
1235
1.88k
  }
1236
2.19k
  offset += length * 8;
1237
1238
2.19k
  hfi = proto_registrar_get_nth(hf_index);
1239
2.19k
  if (! hfi)
1240
0
    THROW(ReportedBoundsError);
1241
2.19k
  if (FT_IS_INT(hfi->type)) {
1242
186
    it=proto_tree_add_int(tree, hf_index, tvb, (offset>>3)-(length+1), length+1, val);
1243
2.00k
  } else if (FT_IS_UINT(hfi->type)) {
1244
533
    it=proto_tree_add_uint(tree, hf_index, tvb, (offset>>3)-(length+1), length+1, val);
1245
1.47k
  } else {
1246
1.47k
    proto_tree_add_expert_format(tree, actx->pinfo, &ei_per_field_not_integer, tvb, (offset>>3)-(length+1), length+1,
1247
1.47k
                    "Field is not an integer: %s", hfi->abbrev);
1248
1.47k
    REPORT_DISSECTOR_BUG("PER integer field that's not an FT_INT* or FT_UINT*");
1249
1.47k
  }
1250
1251
2.19k
  actx->created_item = it;
1252
1253
2.19k
  if(value){
1254
204
    *value=val;
1255
204
  }
1256
1257
2.19k
  return offset;
1258
2.19k
}
1259
1260
uint32_t
1261
dissect_per_integer64b(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, int64_t *value)
1262
88
{
1263
88
  uint32_t i, length;
1264
88
  uint64_t val;
1265
88
  proto_item *it=NULL;
1266
88
  header_field_info *hfi;
1267
1268
  /* 12.2.6 b */
1269
88
  offset=dissect_per_length_determinant(tvb, offset, actx, tree, -1, &length, NULL);
1270
  /* gassert here? */
1271
88
  if(length>8){
1272
23
    dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "too long integer (64b)");
1273
23
    length=8;
1274
23
  }
1275
1276
88
  val=0;
1277
167
  for(i=0;i<length;i++){
1278
79
    if(i==0){
1279
31
      if(tvb_get_uint8(tvb, offset>>3)&0x80){
1280
        /* negative number */
1281
2
        val=UINT64_C(0xffffffffffffffff);
1282
29
      } else {
1283
        /* positive number */
1284
29
        val=0;
1285
29
      }
1286
31
    }
1287
79
    val=(val<<8)|tvb_get_uint8(tvb,offset>>3);
1288
79
    offset+=8;
1289
79
  }
1290
1291
88
  hfi = proto_registrar_get_nth(hf_index);
1292
88
  if (! hfi)
1293
0
    THROW(ReportedBoundsError);
1294
88
  if (FT_IS_INT(hfi->type)) {
1295
0
    it=proto_tree_add_int64(tree, hf_index, tvb, (offset>>3)-(length+1), length+1, (int64_t)val);
1296
88
  } else if (FT_IS_UINT(hfi->type)) {
1297
47
    it=proto_tree_add_uint64(tree, hf_index, tvb, (offset>>3)-(length+1), length+1, val);
1298
47
  } else {
1299
41
    proto_tree_add_expert_format(tree, actx->pinfo, &ei_per_field_not_integer, tvb, (offset>>3)-(length+1), length+1,
1300
41
                    "Field is not an integer: %s", hfi->abbrev);
1301
41
    REPORT_DISSECTOR_BUG("PER integer field that's not an FT_INT* or FT_UINT*");
1302
41
  }
1303
1304
1305
88
  actx->created_item = it;
1306
1307
88
  if(value){
1308
0
    *value=(int64_t)val;
1309
0
  }
1310
1311
88
  return offset;
1312
88
}
1313
/* this function reads a constrained integer  with or without a
1314
   PER visible extension marker present
1315
1316
   has_extension==true  would map to asn constructs such as:
1317
    rfc-number  INTEGER (1..32768, ...)
1318
   while has_extension==false would map to:
1319
    t35CountryCode  INTEGER (0..255)
1320
1321
   it only handles integers that fit inside a 32 bit integer
1322
10.5.1 info only
1323
10.5.2 info only
1324
10.5.3 range=ub-lb+1
1325
10.5.4 empty range
1326
10.5.5 info only
1327
  10.5.6 unaligned version
1328
10.5.7 aligned version
1329
10.5.7.1 decoding of 0-255 1-8 bits
1330
10.5.7.2 decoding og 0-256 8 bits
1331
10.5.7.3 decoding of 0-65535 16 bits
1332
  10.5.7.4
1333
*/
1334
uint32_t
1335
dissect_per_constrained_integer(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, uint32_t min, uint32_t max, uint32_t *value, bool has_extension)
1336
4.58M
{
1337
4.58M
  proto_item *it=NULL;
1338
4.58M
  uint32_t range, val;
1339
4.58M
  int val_start, val_length;
1340
4.58M
  nstime_t timeval;
1341
4.58M
  header_field_info *hfi;
1342
4.58M
  int num_bits;
1343
1344
4.58M
DEBUG_ENTRY("dissect_per_constrained_integer");
1345
4.58M
  if(has_extension){
1346
19.2k
    bool extension_present;
1347
19.2k
    offset=dissect_per_boolean(tvb, offset, actx, tree, hf_per_extension_present_bit, &extension_present);
1348
19.2k
    if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
1349
19.2k
    if(extension_present){
1350
1.68k
      offset = dissect_per_integer(tvb, offset, actx, tree, hf_index, (int32_t*)value);
1351
1.68k
      return offset;
1352
1.68k
    }
1353
19.2k
  }
1354
1355
4.58M
  hfi = proto_registrar_get_nth(hf_index);
1356
1357
  /* 10.5.3 Let "range" be defined as the integer value ("ub" - "lb"   1), and let the value to be encoded be "n".
1358
   * 10.5.7 In the case of the ALIGNED variant the encoding depends on whether
1359
   *      d)  "range" is greater than 64K (the indefinite length case).
1360
   */
1361
4.58M
  if(((max-min)>65536)&&(actx->aligned)){
1362
    /* just set range really big so it will fall through
1363
       to the bottom of the encoding */
1364
8.66k
    range=1000000;
1365
4.57M
  } else {
1366
    /* Really ugly hack.
1367
     * We should really use uint64_t as parameters for min/max.
1368
     * This is to prevent range from being 0 if
1369
     * the range for a signed integer spans the entire 32 bit range.
1370
     * Special case the 2 common cases when this can happen until
1371
     * a real fix is implemented.
1372
     */
1373
4.57M
    if( (max==0x7fffffff && min==0x80000000)
1374
4.57M
    ||  (max==0xffffffff && min==0x00000000) ){
1375
3.27k
      range=0xffffffff;
1376
4.57M
    } else {
1377
4.57M
      range=max-min+1;
1378
4.57M
    }
1379
4.57M
  }
1380
1381
4.58M
  val=0;
1382
4.58M
  timeval.secs=val; timeval.nsecs=0;
1383
  /* 10.5.4 If "range" has the value 1, then the result of the encoding shall be an empty bit-field (no bits).*/
1384
1385
  /* something is really wrong if range is 0 */
1386
4.58M
  DISSECTOR_ASSERT(range!=0);
1387
1388
4.58M
  if(range==1){
1389
419k
    val_start = offset>>3; val_length = 0;
1390
419k
    val = min;
1391
4.16M
  } else if((range<=255)||(!actx->aligned)) {
1392
    /* 10.5.7.1
1393
     * 10.5.6 In the case of the UNALIGNED variant the value ("n" - "lb") shall be encoded
1394
     * as a non-negative  binary integer in a bit field as specified in 10.3 with the minimum
1395
     * number of bits necessary to represent the range.
1396
     */
1397
3.90M
    char *str;
1398
3.90M
    int i, length;
1399
3.90M
    uint32_t mask,mask2;
1400
    /* We only handle 32 bit integers */
1401
3.90M
    mask  = 0x80000000;
1402
3.90M
    mask2 = 0x7fffffff;
1403
3.90M
    i = 32;
1404
110M
    while ((range & mask)== 0){
1405
106M
      i = i - 1;
1406
106M
      mask = mask>>1;
1407
106M
      mask2 = mask2>>1;
1408
106M
    }
1409
3.90M
    if ((range & mask2) == 0)
1410
2.70M
      i = i-1;
1411
1412
3.90M
    num_bits = i;
1413
3.90M
    length=(num_bits+7)>>3;
1414
3.90M
    if(range<=2){
1415
782k
      num_bits=1;
1416
782k
    }
1417
1418
3.90M
    val_start = (offset)>>3;
1419
3.90M
    val_length = length;
1420
3.90M
    val = (uint32_t)tvb_get_bits64(tvb,offset,num_bits,ENC_BIG_ENDIAN);
1421
1422
3.90M
    if (display_internal_per_fields){
1423
0
      str = decode_bits_in_field(actx->pinfo->pool, (offset&0x07),num_bits,val,ENC_BIG_ENDIAN);
1424
0
      if (FT_IS_INT(hfi->type)) {
1425
0
        proto_tree_add_int(tree, hf_per_internal_min_int, tvb, val_start, val_length, min);
1426
0
      } else {
1427
0
        proto_tree_add_uint(tree, hf_per_internal_min, tvb, val_start, val_length, min);
1428
0
      }
1429
0
      proto_tree_add_uint64(tree, hf_per_internal_range, tvb, val_start, val_length, range);
1430
0
      proto_tree_add_uint(tree, hf_per_internal_num_bits, tvb, val_start, val_length, num_bits);
1431
0
      if (FT_IS_INT(hfi->type)) {
1432
0
        proto_tree_add_int64_format_value(tree, hf_per_internal_value_int, tvb, val_start, val_length, val + min, "%s decimal value: %i", str, val + min);
1433
0
      } else {
1434
0
        proto_tree_add_uint64_format_value(tree, hf_per_internal_value, tvb, val_start, val_length, val + min, "%s decimal value: %u", str, val + min);
1435
0
      }
1436
0
    }
1437
    /* The actual value */
1438
3.90M
    val+=min;
1439
3.90M
    offset = offset+num_bits;
1440
3.90M
  } else if(range==256){
1441
    /* 10.5.7.2 */
1442
1443
    /* in the aligned case, align to byte boundary */
1444
37.7k
    BYTE_ALIGN_OFFSET(offset);
1445
37.7k
    val=tvb_get_uint8(tvb, offset>>3);
1446
37.7k
    offset+=8;
1447
1448
37.7k
    val_start = (offset>>3)-1; val_length = 1;
1449
37.7k
    val+=min;
1450
223k
  } else if(range<=65536){
1451
    /* 10.5.7.3 */
1452
1453
    /* in the aligned case, align to byte boundary */
1454
214k
    BYTE_ALIGN_OFFSET(offset);
1455
214k
    val=tvb_get_uint8(tvb, offset>>3);
1456
214k
    val<<=8;
1457
214k
    offset+=8;
1458
214k
    val|=tvb_get_uint8(tvb, offset>>3);
1459
214k
    offset+=8;
1460
1461
214k
    val_start = (offset>>3)-2; val_length = 2;
1462
214k
    val+=min;
1463
214k
  } else {
1464
8.72k
    int i,num_bytes;
1465
8.72k
    bool bit;
1466
1467
    /* 10.5.7.4 */
1468
    /* 12.2.6 */
1469
8.72k
    offset=dissect_per_boolean(tvb, offset, actx, tree, -1, &bit);
1470
8.72k
    num_bytes=bit;
1471
8.72k
    offset=dissect_per_boolean(tvb, offset, actx, tree, -1, &bit);
1472
8.72k
    num_bytes=(num_bytes<<1)|bit;
1473
1474
8.72k
    num_bytes++;  /* lower bound for length determinant is 1 */
1475
8.72k
    if (display_internal_per_fields)
1476
0
      proto_tree_add_uint(tree, hf_per_const_int_len, tvb, (offset>>3), 1, num_bytes);
1477
1478
    /* byte aligned */
1479
8.72k
    BYTE_ALIGN_OFFSET(offset);
1480
8.72k
    val=0;
1481
21.5k
    for(i=0;i<num_bytes;i++){
1482
12.8k
      val=(val<<8)|tvb_get_uint8(tvb,offset>>3);
1483
12.8k
      offset+=8;
1484
12.8k
    }
1485
8.72k
    val_start = (offset>>3)-(num_bytes+1); val_length = num_bytes+1;
1486
8.72k
    val+=min;
1487
8.72k
  }
1488
1489
4.58M
  timeval.secs = val;
1490
4.58M
  if (FT_IS_UINT(hfi->type)) {
1491
4.47M
    it = proto_tree_add_uint(tree, hf_index, tvb, val_start, val_length, val);
1492
4.47M
    per_check_value(val, min, max, actx, it, false);
1493
4.47M
  } else if (FT_IS_INT(hfi->type)) {
1494
90.2k
    it = proto_tree_add_int(tree, hf_index, tvb, val_start, val_length, val);
1495
90.2k
    per_check_value(val, min, max, actx, it, true);
1496
90.2k
  } else if (FT_IS_TIME(hfi->type)) {
1497
1.00k
    it = proto_tree_add_time(tree, hf_index, tvb, val_start, val_length, &timeval);
1498
17.4k
  } else {
1499
17.4k
    THROW(ReportedBoundsError);
1500
17.4k
  }
1501
4.58M
  actx->created_item = it;
1502
4.58M
  if (value) *value = val;
1503
4.58M
  return offset;
1504
4.58M
}
1505
1506
uint32_t
1507
dissect_per_constrained_integer_64b(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, uint64_t min, uint64_t max, uint64_t *value, bool has_extension)
1508
3.37k
{
1509
3.37k
  proto_item *it=NULL, *int_item=NULL;
1510
3.37k
  uint64_t range, val;
1511
3.37k
  int val_start, val_length;
1512
3.37k
  nstime_t timeval;
1513
3.37k
  header_field_info *hfi;
1514
3.37k
  int num_bits;
1515
3.37k
  bool tmp;
1516
1517
3.37k
DEBUG_ENTRY("dissect_per_constrained_integer_64b");
1518
3.37k
  if(has_extension){
1519
959
    bool extension_present;
1520
959
    offset=dissect_per_boolean(tvb, offset, actx, tree, hf_per_extension_present_bit, &extension_present);
1521
959
    if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
1522
959
    if(extension_present){
1523
88
      offset = dissect_per_integer64b(tvb, offset, actx, tree, hf_index, (int64_t*)value);
1524
88
      return offset;
1525
88
    }
1526
959
  }
1527
1528
3.28k
  hfi = proto_registrar_get_nth(hf_index);
1529
1530
  /* 10.5.3 Let "range" be defined as the integer value ("ub" - "lb"   1), and let the value to be encoded be "n".
1531
   * 10.5.7 In the case of the ALIGNED variant the encoding depends on whether
1532
   *      d)  "range" is greater than 64K (the indefinite length case).
1533
   */
1534
3.28k
  if(((max-min)>65536)&&(actx->aligned)){
1535
    /* just set range really big so it will fall through
1536
       to the bottom of the encoding */
1537
    /* range=1000000; */
1538
2.33k
    range = max-min;
1539
2.33k
    if (range==65536)
1540
0
      range++; /* make it fall trough? */
1541
2.33k
  } else {
1542
    /* Copied from the 32 bit version, assuming the same problem occurs
1543
     * at 64 bit boundary.
1544
     * Really ugly hack.
1545
     * We should really use uint64_t as parameters for min/max.
1546
     * This is to prevent range from being 0 if
1547
     * the range for a signed integer spans the entire 32 bit range.
1548
     * Special case the 2 common cases when this can happen until
1549
     * a real fix is implemented.
1550
     */
1551
956
    if( (max==INT64_C(0x7fffffffffffffff) && min==INT64_C(0x8000000000000000))
1552
956
    ||  (max==INT64_C(0xffffffffffffffff) && min==0) ){
1553
0
      range=INT64_C(0xffffffffffffffff);
1554
956
    } else {
1555
956
      range=max-min+1;
1556
956
    }
1557
956
  }
1558
1559
3.28k
  val=0;
1560
3.28k
  timeval.secs=0; timeval.nsecs=0;
1561
  /* 10.5.4 If "range" has the value 1, then the result of the encoding shall be an empty bit-field (no bits).*/
1562
1563
  /* something is really wrong if range is 0 */
1564
3.28k
  DISSECTOR_ASSERT(range!=0);
1565
1566
3.28k
  if(range==1){
1567
0
    val_start = offset>>3; val_length = 0;
1568
0
    val = min;
1569
3.28k
  } else if((range<=255)||(!actx->aligned)) {
1570
    /* 10.5.7.1
1571
     * 10.5.6 In the case of the UNALIGNED variant the value ("n" - "lb") shall be encoded
1572
     * as a non-negative  binary integer in a bit field as specified in 10.3 with the minimum
1573
     * number of bits necessary to represent the range.
1574
     */
1575
953
    char *str;
1576
953
    int i, bit, length, str_length, str_index = 0;
1577
953
    uint64_t mask,mask2;
1578
    /* We only handle 64 bit integers */
1579
953
    mask  = UINT64_C(0x8000000000000000);
1580
953
    mask2 = UINT64_C(0x7fffffffffffffff);
1581
953
    i = 64;
1582
22.7k
    while ((range & mask)== 0){
1583
21.8k
      i = i - 1;
1584
21.8k
      mask = mask>>1;
1585
21.8k
      mask2 = mask2>>1;
1586
21.8k
    }
1587
953
    if ((range & mask2) == 0)
1588
953
      i = i-1;
1589
1590
953
    num_bits = i;
1591
953
    length=1;
1592
953
    if(range<=2){
1593
0
      num_bits=1;
1594
0
    }
1595
1596
    /* prepare the string (max number of bits + quartet separators) */
1597
953
    str_length = 512+128;
1598
953
    str = (char *)wmem_alloc(actx->pinfo->pool, str_length+1);
1599
4.63k
    for(bit=0;bit<((int)(offset&0x07));bit++){
1600
3.67k
      if(bit&&(!(bit%4))){
1601
381
        if (str_index < str_length) str[str_index++] = ' ';
1602
381
      }
1603
3.67k
      if (str_index < str_length) str[str_index++] = '.';
1604
3.67k
    }
1605
    /* read the bits for the int */
1606
38.7k
    for(i=0;i<num_bits;i++){
1607
37.7k
      if(bit&&(!(bit%4))){
1608
9.35k
        if (str_index < str_length) str[str_index++] = ' ';
1609
9.35k
      }
1610
37.7k
      if(bit&&(!(bit%8))){
1611
4.56k
        length+=1;
1612
4.56k
        if (str_index < str_length) str[str_index++] = ' ';
1613
4.56k
      }
1614
37.7k
      bit++;
1615
37.7k
      offset=dissect_per_boolean(tvb, offset, actx, tree, -1, &tmp);
1616
37.7k
      val<<=1;
1617
37.7k
      if(tmp){
1618
11.9k
        val|=1;
1619
11.9k
        if (str_index < str_length) str[str_index++] = '1';
1620
25.8k
      } else {
1621
25.8k
        if (str_index < str_length) str[str_index++] = '0';
1622
25.8k
      }
1623
37.7k
    }
1624
3.48k
    for(;bit%8;bit++){
1625
2.53k
      if(bit&&(!(bit%4))){
1626
326
        if (str_index < str_length) str[str_index++] = ' ';
1627
326
      }
1628
2.53k
      if (str_index < str_length) str[str_index++] = '.';
1629
2.53k
    }
1630
953
    str[str_index] = '\0'; /* Terminate string */
1631
953
    val_start = (offset-num_bits)>>3; val_length = length;
1632
953
    val+=min;
1633
953
    if (display_internal_per_fields) {
1634
0
      proto_tree_add_uint64(tree, hf_per_internal_range, tvb, val_start, val_length, range);
1635
0
      proto_tree_add_uint(tree, hf_per_internal_num_bits, tvb, val_start,val_length, num_bits);
1636
0
      proto_tree_add_uint64_format_value(tree, hf_per_internal_value, tvb, val_start, val_length, val, "%s decimal value: %" PRIu64, str, val);
1637
0
    }
1638
2.33k
  } else if(range==256){
1639
    /* 10.5.7.2 */
1640
1641
    /* in the aligned case, align to byte boundary */
1642
0
    BYTE_ALIGN_OFFSET(offset);
1643
0
    val=tvb_get_uint8(tvb, offset>>3);
1644
0
    offset+=8;
1645
1646
0
    val_start = (offset>>3)-1; val_length = 1;
1647
0
    val+=min;
1648
2.33k
  } else if(range<=65536){
1649
    /* 10.5.7.3 */
1650
1651
    /* in the aligned case, align to byte boundary */
1652
0
    BYTE_ALIGN_OFFSET(offset);
1653
0
    val=tvb_get_uint8(tvb, offset>>3);
1654
0
    val<<=8;
1655
0
    offset+=8;
1656
0
    val|=tvb_get_uint8(tvb, offset>>3);
1657
0
    offset+=8;
1658
1659
0
    val_start = (offset>>3)-2; val_length = 2;
1660
0
    val+=min;
1661
2.33k
  } else {
1662
2.33k
    int i,num_bytes,n_bits;
1663
1664
    /* 10.5.7.4 */
1665
    /* 12.2.6 */
1666
    /* calculate the number of bits to hold the length */
1667
2.33k
    if ((range & INT64_C(0xffffffff00000000)) != 0){
1668
2.33k
      n_bits=3;
1669
2.33k
    }else{
1670
3
      n_bits=2;
1671
3
    }
1672
2.33k
    num_bytes =tvb_get_bits8(tvb, offset, n_bits);
1673
2.33k
    num_bytes++;  /* lower bound for length determinant is 1 */
1674
2.33k
    if (display_internal_per_fields){
1675
0
      int_item = proto_tree_add_bits_item(tree, hf_per_const_int_len, tvb, offset,n_bits, ENC_BIG_ENDIAN);
1676
0
      proto_item_append_text(int_item,"+1=%u bytes, Range = (%" PRIu64 ")",num_bytes, range);
1677
0
    }
1678
2.33k
    offset = offset+n_bits;
1679
    /* byte aligned */
1680
2.33k
    BYTE_ALIGN_OFFSET(offset);
1681
2.33k
    val=0;
1682
7.39k
    for(i=0;i<num_bytes;i++){
1683
5.06k
      val=(val<<8)|tvb_get_uint8(tvb,offset>>3);
1684
5.06k
      offset+=8;
1685
5.06k
    }
1686
2.33k
    val_start = (offset>>3)-(num_bytes+1); val_length = num_bytes+1;
1687
2.33k
    val+=min;
1688
2.33k
  }
1689
1690
1691
3.28k
  if (FT_IS_UINT(hfi->type)) {
1692
2.84k
    it = proto_tree_add_uint64(tree, hf_index, tvb, val_start, val_length, val);
1693
2.84k
    per_check_value64(val, min, max, actx, it, false);
1694
2.84k
  } else if (FT_IS_INT(hfi->type)) {
1695
26
    it = proto_tree_add_int64(tree, hf_index, tvb, val_start, val_length, val);
1696
26
    per_check_value64(val, min, max, actx, it, true);
1697
417
  } else if (FT_IS_TIME(hfi->type)) {
1698
0
    timeval.secs = (uint32_t)val;
1699
0
    it = proto_tree_add_time(tree, hf_index, tvb, val_start, val_length, &timeval);
1700
417
  } else {
1701
417
    THROW(ReportedBoundsError);
1702
417
  }
1703
3.28k
  actx->created_item = it;
1704
3.28k
  if (value) *value = val;
1705
3.28k
  return offset;
1706
3.37k
}
1707
1708
/* 13 Encoding the enumerated type */
1709
uint32_t
1710
dissect_per_enumerated(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, uint32_t root_num, uint32_t *value, bool has_extension, uint32_t ext_num, uint32_t *value_map)
1711
1.34M
{
1712
1713
1.34M
  proto_item *it=NULL;
1714
1.34M
  uint32_t enum_index, val;
1715
1.34M
  uint32_t start_offset = offset;
1716
1.34M
  bool extension_present = false;
1717
1.34M
  header_field_info *hfi;
1718
1719
1.34M
  if (has_extension) {
1720
    /* Extension bit */
1721
54.8k
    offset = dissect_per_boolean(tvb, offset, actx, tree, hf_per_extension_present_bit, &extension_present);
1722
54.8k
    if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
1723
54.8k
  }
1724
1725
1.34M
  if (!extension_present) {
1726
    /* 13.2  */
1727
1.33M
    offset = dissect_per_constrained_integer(tvb, offset, actx, tree, hf_per_enum_index, 0, root_num - 1, &enum_index, false);
1728
1.33M
    if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
1729
1.33M
  } else {
1730
    /* 13.3 ".. and the value shall be added to the field-list as a
1731
     * normally small non-negative whole number whose value is the
1732
     * enumeration index of the additional enumeration and with "lb" set to 0.."
1733
     */
1734
9.13k
    offset = dissect_per_normally_small_nonnegative_whole_number(tvb, offset, actx, tree, hf_per_enum_extension_index, &enum_index);
1735
9.13k
    enum_index += root_num;
1736
9.13k
  }
1737
1.34M
  val = (value_map && (enum_index<(root_num+ext_num))) ? value_map[enum_index] : enum_index;
1738
1.34M
  hfi = proto_registrar_get_nth(hf_index);
1739
1.34M
  if (FT_IS_UINT(hfi->type)) {
1740
1.34M
    it = proto_tree_add_uint(tree, hf_index, tvb, start_offset>>3, BLEN(start_offset, offset), val);
1741
1.34M
  } else {
1742
3.33k
    THROW(ReportedBoundsError);
1743
3.33k
  }
1744
1.34M
  actx->created_item = it;
1745
1.34M
  if (value) *value = val;
1746
1.34M
  return offset;
1747
1.34M
}
1748
1749
/* 14 Encoding the real type */
1750
uint32_t
1751
dissect_per_real(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, double *value)
1752
0
{
1753
0
  uint32_t val_length, end_offset;
1754
0
  tvbuff_t *val_tvb;
1755
0
  double val = 0;
1756
1757
0
  offset = dissect_per_length_determinant(tvb, offset, actx, tree, hf_per_real_length, &val_length, NULL);
1758
0
  if (actx->aligned) BYTE_ALIGN_OFFSET(offset);
1759
0
  val_tvb = tvb_new_octet_aligned(tvb, offset, val_length * 8);
1760
  /* Add new data source if the offet was unaligned */
1761
0
  if ((offset & 7) != 0) {
1762
0
    add_new_data_source(actx->pinfo, val_tvb, "Unaligned OCTET STRING");
1763
0
  }
1764
0
  end_offset = offset + val_length * 8;
1765
1766
0
  val = asn1_get_real(tvb_get_ptr(val_tvb, 0, val_length), val_length);
1767
0
  actx->created_item = proto_tree_add_double(tree, hf_index, val_tvb, 0, val_length, val);
1768
1769
0
  if (value) *value = val;
1770
1771
0
  return end_offset;
1772
0
}
1773
1774
/* 22 Encoding the choice type */
1775
uint32_t
1776
dissect_per_choice(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, int ett_index, const per_choice_t *choice, int *value)
1777
875k
{
1778
875k
  bool /*extension_present,*/ extension_flag;
1779
875k
  int extension_root_entries;
1780
875k
  uint32_t choice_index;
1781
875k
  int i, idx, cidx;
1782
875k
  uint32_t ext_length = 0;
1783
875k
  uint32_t old_offset = offset;
1784
875k
  proto_item *choice_item = NULL;
1785
875k
  proto_tree *choice_tree = NULL;
1786
1787
875k
DEBUG_ENTRY("dissect_per_choice");
1788
1789
875k
  if (value) *value = -1;
1790
1791
  /* 22.5 */
1792
875k
  if (choice[0].extension == ASN1_NO_EXTENSIONS){
1793
    /*extension_present = false; ?? */
1794
773k
    extension_flag = false;
1795
773k
  } else {
1796
    /*extension_present = true; ?? */
1797
101k
    offset = dissect_per_boolean(tvb, offset, actx, tree, hf_per_extension_bit, &extension_flag);
1798
101k
    if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
1799
101k
  }
1800
1801
  /* count the number of entries in the extension root and extension addition */
1802
875k
  extension_root_entries = 0;
1803
4.37M
  for (i=0; choice[i].p_id; i++) {
1804
3.49M
    switch(choice[i].extension){
1805
2.46M
      case ASN1_NO_EXTENSIONS:
1806
3.24M
      case ASN1_EXTENSION_ROOT:
1807
3.24M
        extension_root_entries++;
1808
3.24M
        break;
1809
247k
      case ASN1_NOT_EXTENSION_ROOT:
1810
247k
        break;
1811
3.49M
    }
1812
3.49M
  }
1813
1814
875k
  if (!extension_flag) {  /* 22.6, 22.7 */
1815
859k
    if (extension_root_entries == 1) {  /* 22.5 */
1816
4.23k
      choice_index = 0;
1817
855k
    } else {
1818
855k
      offset = dissect_per_constrained_integer(tvb, offset, actx,
1819
855k
        tree, hf_per_choice_index, 0, extension_root_entries - 1,
1820
855k
        &choice_index, false);
1821
855k
      if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
1822
855k
    }
1823
1824
859k
    idx = -1; cidx = choice_index;
1825
1.56M
    for (i=0; choice[i].p_id; i++) {
1826
1.55M
      if(choice[i].extension != ASN1_NOT_EXTENSION_ROOT){
1827
1.55M
        if (!cidx) { idx = i; break; }
1828
702k
        cidx--;
1829
702k
      }
1830
1.55M
    }
1831
859k
  } else {  /* 22.8 */
1832
15.8k
    offset = dissect_per_normally_small_nonnegative_whole_number(tvb, offset, actx, tree, hf_per_choice_extension_index, &choice_index);
1833
15.8k
    offset = dissect_per_length_determinant(tvb, offset, actx, tree, hf_per_open_type_length, &ext_length, NULL);
1834
1835
15.8k
    idx = -1; cidx = choice_index;
1836
254k
    for (i=0; choice[i].p_id; i++) {
1837
244k
      if(choice[i].extension == ASN1_NOT_EXTENSION_ROOT){
1838
30.7k
        if (!cidx) { idx = i; break; }
1839
24.7k
        cidx--;
1840
24.7k
      }
1841
244k
    }
1842
15.8k
  }
1843
1844
875k
  if (idx != -1) {
1845
861k
    choice_item = proto_tree_add_uint(tree, hf_index, tvb, old_offset>>3, 0, choice[idx].value);
1846
861k
    choice_tree = proto_item_add_subtree(choice_item, ett_index);
1847
861k
    if (!extension_flag) {
1848
855k
      offset = choice[idx].func(tvb, offset, actx, choice_tree, *choice[idx].p_id);
1849
855k
    } else {
1850
6.02k
      choice[idx].func(tvb, offset, actx, choice_tree, *choice[idx].p_id);
1851
6.02k
      offset += ext_length * 8;
1852
6.02k
    }
1853
861k
    proto_item_set_len(choice_item, BLEN(old_offset, offset));
1854
861k
  } else {
1855
13.6k
    if (!extension_flag) {
1856
3.07k
      dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "unknown extension root index in choice");
1857
10.5k
    } else {
1858
10.5k
      offset += ext_length * 8;
1859
10.5k
      proto_tree_add_expert_format(tree, actx->pinfo, &ei_per_choice_extension_unknown,
1860
10.5k
                    tvb, old_offset>>3, BLEN(old_offset, offset),
1861
10.5k
                    "Choice no. %d in extension", choice_index);
1862
10.5k
    }
1863
13.6k
  }
1864
1865
875k
  if (value && (idx != -1))
1866
12.9k
    *value = choice[idx].value;
1867
1868
875k
  return offset;
1869
875k
}
1870
1871
1872
static const char *
1873
index_get_optional_name(const per_sequence_t *sequence, int idx)
1874
4.55M
{
1875
4.55M
  int i;
1876
4.55M
  header_field_info *hfi;
1877
1878
22.3M
  for(i=0;sequence[i].p_id;i++){
1879
22.3M
    if((sequence[i].extension!=ASN1_NOT_EXTENSION_ROOT)&&(sequence[i].optional==ASN1_OPTIONAL)){
1880
19.1M
      if (idx == 0) {
1881
4.55M
        hfi = proto_registrar_get_nth(*sequence[i].p_id);
1882
4.55M
        return (hfi) ? hfi->name : "<unknown field>";
1883
4.55M
      }
1884
14.5M
      idx--;
1885
14.5M
    }
1886
22.3M
  }
1887
0
  return "<unknown type>";
1888
4.55M
}
1889
1890
static const char *
1891
index_get_extension_name(const per_sequence_t *sequence, int idx)
1892
192k
{
1893
192k
  int i;
1894
192k
  header_field_info *hfi;
1895
1896
2.07M
  for(i=0;sequence[i].p_id;i++){
1897
1.95M
    if(sequence[i].extension==ASN1_NOT_EXTENSION_ROOT){
1898
859k
      if (idx == 0) {
1899
76.1k
        if (*sequence[i].p_id == -1 || *sequence[i].p_id == 0) return "extension addition group";
1900
8.93k
        hfi = proto_registrar_get_nth(*sequence[i].p_id);
1901
8.93k
        return (hfi) ? hfi->name : "<unknown field>";
1902
76.1k
      }
1903
783k
      idx--;
1904
783k
    }
1905
1.95M
  }
1906
116k
  return "<unknown type>";
1907
192k
}
1908
1909
static const char *
1910
index_get_field_name(const per_sequence_t *sequence, int idx)
1911
0
{
1912
0
  if (sequence) {
1913
0
    header_field_info *hfi = proto_registrar_get_nth(*sequence[idx].p_id);
1914
1915
0
    if (hfi) {
1916
0
      return hfi->name;
1917
0
    }
1918
0
  }
1919
0
  return "<unknown field>";
1920
0
}
1921
1922
/* this functions decodes a SEQUENCE
1923
   it can only handle SEQUENCES with at most 32 DEFAULT or OPTIONAL fields
1924
18.1 extension bit
1925
18.2 optional/default items in root
1926
18.3 we ignore the case where n>64K
1927
18.4 the root sequence
1928
     18.5
1929
     18.6
1930
     18.7
1931
     18.8
1932
     18.9
1933
*/
1934
uint32_t
1935
dissect_per_sequence(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *parent_tree, int hf_index, int ett_index, const per_sequence_t *sequence)
1936
2.94M
{
1937
2.94M
  bool /*extension_present,*/ extension_flag, optional_field_flag;
1938
2.94M
  proto_item *item;
1939
2.94M
  proto_tree *tree;
1940
2.94M
  uint32_t old_offset=offset;
1941
2.94M
  uint32_t i, j, num_opts;
1942
2.94M
  uint32_t optional_mask[SEQ_MAX_COMPONENTS>>5];
1943
1944
2.94M
DEBUG_ENTRY("dissect_per_sequence");
1945
2.94M
  DISSECTOR_ASSERT(sequence);
1946
1947
2.94M
  item=proto_tree_add_item(parent_tree, hf_index, tvb, offset>>3, 0, ENC_BIG_ENDIAN);
1948
2.94M
  tree=proto_item_add_subtree(item, ett_index);
1949
1950
1951
  /* first check if there should be an extension bit for this CHOICE.
1952
     we do this by just checking the first choice arm
1953
   */
1954
  /* 18.1 */
1955
2.94M
  extension_flag=0;
1956
2.94M
  if(sequence[0].extension==ASN1_NO_EXTENSIONS){
1957
    /*extension_present=0;  ?? */
1958
2.62M
  } else {
1959
    /*extension_present=1; ?? */
1960
318k
    offset=dissect_per_boolean(tvb, offset, actx, tree, hf_per_extension_bit, &extension_flag);
1961
318k
    if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
1962
318k
  }
1963
  /* 18.2 */
1964
2.94M
  num_opts=0;
1965
12.0M
  for(i=0;sequence[i].p_id;i++){
1966
9.07M
    if((sequence[i].extension!=ASN1_NOT_EXTENSION_ROOT)&&(sequence[i].optional==ASN1_OPTIONAL)){
1967
4.50M
      num_opts++;
1968
4.50M
    }
1969
9.07M
  }
1970
2.94M
  if (num_opts > SEQ_MAX_COMPONENTS) {
1971
0
    dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "too many optional/default components");
1972
0
  }
1973
1974
2.94M
  memset(optional_mask, 0, sizeof(optional_mask));
1975
7.44M
  for(i=0;i<num_opts;i++){
1976
4.50M
    offset=dissect_per_boolean(tvb, offset, actx, tree, hf_per_optional_field_bit, &optional_field_flag);
1977
4.50M
    if (tree) {
1978
4.49M
      proto_item_append_text(actx->created_item, " (%s %s present)",
1979
4.49M
        index_get_optional_name(sequence, i), optional_field_flag?"is":"is NOT");
1980
4.49M
    }
1981
4.50M
    if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
1982
4.50M
    if(optional_field_flag){
1983
1.64M
      optional_mask[i>>5]|=0x80000000>>(i&0x1f);
1984
1.64M
    }
1985
4.50M
  }
1986
1987
1988
  /* 18.4 */
1989
11.5M
  for(i=0,j=0;sequence[i].p_id;i++){
1990
8.58M
    if( (sequence[i].extension==ASN1_NO_EXTENSIONS)
1991
8.58M
    ||  (sequence[i].extension==ASN1_EXTENSION_ROOT) ){
1992
8.11M
      if(sequence[i].optional==ASN1_OPTIONAL){
1993
4.22M
        bool is_present;
1994
4.22M
        if (num_opts == 0){
1995
0
          continue;
1996
0
        }
1997
4.22M
        is_present=(0x80000000>>(j&0x1f))&optional_mask[j>>5];
1998
4.22M
        num_opts--;
1999
4.22M
        j++;
2000
4.22M
        if(!is_present){
2001
2.70M
          continue;
2002
2.70M
        }
2003
4.22M
      }
2004
5.40M
      if(sequence[i].func){
2005
5.40M
        offset=sequence[i].func(tvb, offset, actx, tree, *sequence[i].p_id);
2006
5.40M
      } else {
2007
0
        dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, index_get_field_name(sequence, i));
2008
0
      }
2009
5.40M
    }
2010
8.58M
  }
2011
2012
2013
2.94M
  if(extension_flag){
2014
30.8k
    bool extension_bit;
2015
30.8k
    uint32_t num_known_extensions;
2016
30.8k
    uint32_t num_extensions;
2017
30.8k
    uint32_t extension_mask;
2018
2019
30.8k
    offset=dissect_per_normally_small_nonnegative_whole_number(tvb, offset, actx, tree, hf_per_num_sequence_extensions, &num_extensions);
2020
    /* the X.691 standard is VERY unclear here.
2021
       there is no mention that the lower bound lb for this
2022
       (apparently) semiconstrained value is 1,
2023
       apart from the NOTE: comment in 18.8 that this value can
2024
       not be 0.
2025
       In my book, there is a semantic difference between having
2026
       a comment that says that the value can not be zero
2027
       and stating that the lb is 1.
2028
       I don't know if this is right or not but it makes
2029
       some of the very few captures I have decode properly.
2030
2031
       It could also be that the captures I have are generated by
2032
       a broken implementation.
2033
       If this is wrong and you don't report it as a bug
2034
       then it won't get fixed!
2035
    */
2036
30.8k
    num_extensions+=1;
2037
30.8k
    if (num_extensions > 32) {
2038
1.30k
      dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "too many extensions");
2039
1.30k
    }
2040
2041
30.8k
    extension_mask=0;
2042
223k
    for(i=0;i<num_extensions;i++){
2043
192k
      offset=dissect_per_boolean(tvb, offset, actx, tree, hf_per_extension_present_bit, &extension_bit);
2044
192k
      if (tree) {
2045
192k
        proto_item_append_text(actx->created_item, " (%s %s present)",
2046
192k
          index_get_extension_name(sequence, i), extension_bit?"is":"is NOT");
2047
192k
      }
2048
192k
      if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
2049
2050
192k
      extension_mask=(extension_mask<<1)|extension_bit;
2051
192k
    }
2052
2053
    /* find how many extensions we know about */
2054
30.8k
    num_known_extensions=0;
2055
287k
    for(i=0;sequence[i].p_id;i++){
2056
256k
      if(sequence[i].extension==ASN1_NOT_EXTENSION_ROOT){
2057
110k
        num_known_extensions++;
2058
110k
      }
2059
256k
    }
2060
2061
    /* decode the extensions one by one */
2062
131k
    for(i=0;i<num_extensions;i++){
2063
100k
      uint32_t length;
2064
100k
      uint32_t new_offset;
2065
100k
      int32_t difference;
2066
100k
      uint32_t extension_index;
2067
100k
      uint32_t k;
2068
2069
100k
      if(!((1U<<(num_extensions-1-i))&extension_mask)){
2070
        /* this extension is not encoded in this PDU */
2071
75.4k
        continue;
2072
75.4k
      }
2073
2074
25.1k
      offset=dissect_per_length_determinant(tvb, offset, actx, tree, hf_per_open_type_length, &length, NULL);
2075
2076
25.1k
      if(i>=num_known_extensions){
2077
        /* we don't know how to decode this extension */
2078
11.0k
        offset+=length*8;
2079
11.0k
        expert_add_info(actx->pinfo, item, &ei_per_sequence_extension_unknown);
2080
11.0k
        continue;
2081
11.0k
      }
2082
2083
14.0k
      extension_index=0;
2084
144k
      for(j=0,k=0;sequence[j].p_id;j++){
2085
143k
        if(sequence[j].extension==ASN1_NOT_EXTENSION_ROOT){
2086
56.7k
          if(k==i){
2087
12.6k
            extension_index=j;
2088
12.6k
            break;
2089
12.6k
          }
2090
44.1k
          k++;
2091
44.1k
        }
2092
143k
      }
2093
2094
14.0k
      if(sequence[extension_index].func){
2095
12.6k
        new_offset=sequence[extension_index].func(tvb, offset, actx, tree, *sequence[extension_index].p_id);
2096
12.6k
        offset+=length*8;
2097
12.6k
        difference = offset - new_offset;
2098
        /* A difference of 7 or less might be byte aligning */
2099
        /* Difference could be 8 if open type has no bits and the length is 1 */
2100
12.6k
        if ((length > 1) && (difference > 7)) {
2101
6.24k
          proto_tree_add_expert_format(tree, actx->pinfo, &ei_per_encoding_error, tvb, new_offset>>3, (offset-new_offset)>>3,
2102
6.24k
            "Possible encoding error full length not decoded. Open type length %u, decoded %u",length, length - (difference>>3));
2103
6.24k
        }
2104
6.38k
        else if (difference < 0) {
2105
960
          proto_tree_add_expert_format(tree, actx->pinfo, &ei_per_encoding_error, tvb, new_offset>>3, (offset-new_offset)>>3,
2106
960
            "Possible encoding error open type length less than dissected bits. Open type length %u, decoded %u", length, length - (difference>>3));
2107
960
        }
2108
12.6k
      } else {
2109
1.45k
        dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, index_get_field_name(sequence, extension_index));
2110
1.45k
        offset+=length*8;
2111
1.45k
      }
2112
14.0k
    }
2113
30.8k
  }
2114
2115
2.94M
  proto_item_set_len(item, (offset>>3)!=(old_offset>>3)?(offset>>3)-(old_offset>>3):1);
2116
2.94M
  actx->created_item = item;
2117
2.94M
  return offset;
2118
2.94M
}
2119
2120
uint32_t
2121
dissect_per_sequence_eag(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, const per_sequence_t *sequence)
2122
10.2k
{
2123
10.2k
  bool optional_field_flag;
2124
10.2k
  uint32_t i, j, num_opts;
2125
10.2k
  uint32_t optional_mask[SEQ_MAX_COMPONENTS>>5];
2126
2127
10.2k
DEBUG_ENTRY("dissect_per_sequence_eag");
2128
2129
10.2k
  num_opts=0;
2130
62.9k
  for(i=0;sequence[i].p_id;i++){
2131
52.6k
    if(sequence[i].optional==ASN1_OPTIONAL){
2132
52.6k
      num_opts++;
2133
52.6k
    }
2134
52.6k
  }
2135
10.2k
  if (num_opts > SEQ_MAX_COMPONENTS) {
2136
0
    dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "too many optional/default components");
2137
0
  }
2138
2139
10.2k
  memset(optional_mask, 0, sizeof(optional_mask));
2140
62.8k
  for(i=0;i<num_opts;i++){
2141
52.5k
    offset=dissect_per_boolean(tvb, offset, actx, tree, hf_per_optional_field_bit, &optional_field_flag);
2142
52.5k
    if (tree) {
2143
52.5k
      proto_item_append_text(actx->created_item, " (%s %s present)",
2144
52.5k
        index_get_optional_name(sequence, i), optional_field_flag?"is":"is NOT");
2145
52.5k
    }
2146
52.5k
    if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
2147
52.5k
    if(optional_field_flag){
2148
22.8k
      optional_mask[i>>5]|=0x80000000>>(i&0x1f);
2149
22.8k
    }
2150
52.5k
  }
2151
2152
54.0k
  for(i=0,j=0;sequence[i].p_id;i++){
2153
43.7k
    if(sequence[i].optional==ASN1_OPTIONAL){
2154
43.7k
      bool is_present;
2155
43.7k
      if (num_opts == 0){
2156
0
        continue;
2157
0
      }
2158
43.7k
      is_present=(0x80000000>>(j&0x1f))&optional_mask[j>>5];
2159
43.7k
      num_opts--;
2160
43.7k
      j++;
2161
43.7k
      if(!is_present){
2162
25.4k
        continue;
2163
25.4k
      }
2164
43.7k
    }
2165
18.3k
    if(sequence[i].func){
2166
18.3k
      offset=sequence[i].func(tvb, offset, actx, tree, *sequence[i].p_id);
2167
18.3k
    } else {
2168
0
      dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, index_get_field_name(sequence, i));
2169
0
    }
2170
18.3k
  }
2171
2172
10.2k
  return offset;
2173
10.2k
}
2174
2175
2176
/* 15 Encoding the bitstring type
2177
2178
   max_len or min_len == NO_BOUND means there is no lower/upper constraint
2179
2180
*/
2181
2182
static tvbuff_t *dissect_per_bit_string_display(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, header_field_info *hfi, uint32_t length, int * const *named_bits, int num_named_bits _U_)
2183
267k
{
2184
267k
  tvbuff_t *out_tvb = NULL;
2185
267k
  uint32_t pad_length;
2186
267k
  uint64_t value;
2187
2188
267k
  out_tvb = tvb_new_octet_aligned(tvb, offset, length);
2189
267k
  add_new_data_source(actx->pinfo, out_tvb, "Bitstring tvb");
2190
2191
267k
  if (hfi) {
2192
258k
    actx->created_item = proto_tree_add_item(tree, hf_index, out_tvb, 0, -1, ENC_BIG_ENDIAN);
2193
258k
    proto_item_append_text(actx->created_item, " [bit length %u", length);
2194
258k
    pad_length = WS_PADDING_TO_8(length);
2195
258k
    if (pad_length!=0) {
2196
186k
      proto_item_append_text(actx->created_item, ", %u LSB pad bits", pad_length);
2197
186k
    }
2198
2199
258k
    if (length<=64) { /* if read into 64 bits also handle length <= 24, 40, 48, 56 bits */
2200
251k
      if (length<=8) {
2201
88.8k
        value = tvb_get_bits8(out_tvb, 0, length);
2202
163k
      }else if (length<=16) {
2203
61.6k
        value = tvb_get_bits16(out_tvb, 0, length, ENC_BIG_ENDIAN);
2204
101k
      }else if (length<=24) { /* first read 16 and then the remaining bits */
2205
19.6k
        value = tvb_get_bits16(out_tvb, 0, 16, ENC_BIG_ENDIAN);
2206
19.6k
        value <<= 8 - pad_length;
2207
19.6k
        value |= tvb_get_bits8(out_tvb, 16, length - 16);
2208
81.8k
      }else if (length<=32) {
2209
74.1k
        value = tvb_get_bits32(out_tvb, 0, length, ENC_BIG_ENDIAN);
2210
74.1k
      }else if (length<=40) { /* first read 32 and then the remaining bits */
2211
4.43k
        value = tvb_get_bits32(out_tvb, 0, 32, ENC_BIG_ENDIAN);
2212
4.43k
        value <<= 8 - pad_length;
2213
4.43k
        value |= tvb_get_bits8(out_tvb, 32, length - 32);
2214
4.43k
      }else if (length<=48) { /* first read 32 and then the remaining bits */
2215
2.05k
        value = tvb_get_bits32(out_tvb, 0, 32, ENC_BIG_ENDIAN);
2216
2.05k
        value <<= 16 - pad_length;
2217
2.05k
        value |= tvb_get_bits16(out_tvb, 32, length - 32, ENC_BIG_ENDIAN);
2218
2.05k
      }else if (length<=56) { /* first read 32 and 16 then the remaining bits */
2219
309
        value = tvb_get_bits32(out_tvb, 0, 32, ENC_BIG_ENDIAN);
2220
309
        value <<= 16;
2221
309
        value |= tvb_get_bits16(out_tvb, 32, 16, ENC_BIG_ENDIAN);
2222
309
        value <<= 8 - pad_length;
2223
309
        value |= tvb_get_bits8(out_tvb, 48, length - 48);
2224
924
      }else {
2225
924
        value = tvb_get_bits64(out_tvb, 0, length, ENC_BIG_ENDIAN);
2226
924
      }
2227
251k
      proto_item_append_text(actx->created_item, ", %s decimal value %" PRIu64,
2228
251k
        decode_bits_in_field(actx->pinfo->pool, 0, length, value, ENC_BIG_ENDIAN), value);
2229
251k
      if (named_bits) {
2230
14.8k
        const uint32_t named_bits_bytelen = (num_named_bits + 7) / 8;
2231
14.8k
        proto_tree *subtree = proto_item_add_subtree(actx->created_item, ett_per_named_bits);
2232
41.4k
        for (uint32_t i = 0; i < named_bits_bytelen; i++) {
2233
          // If less data is available than the number of named bits, then
2234
          // the trailing (right) bits are assumed to be 0.
2235
26.5k
          value = 0;
2236
26.5k
          const uint32_t bit_offset = 8 * i;
2237
26.5k
          if (bit_offset < length) {
2238
26.5k
            value = tvb_get_uint8(out_tvb, i);
2239
26.5k
          }
2240
2241
          // Process 8 bits at a time instead of 64, each field masks a
2242
          // single byte.
2243
26.5k
          int* const * section_named_bits = named_bits + bit_offset;
2244
26.5k
          int* flags[9];
2245
26.5k
          if (num_named_bits - bit_offset > 8) {
2246
11.6k
            memcpy(&flags[0], named_bits + bit_offset, 8 * sizeof(int*));
2247
11.6k
            flags[8] = NULL;
2248
11.6k
            section_named_bits = flags;
2249
11.6k
          }
2250
2251
          // TODO should non-zero pad bits be masked from the value?
2252
          // When trailing zeroes are not present in the data, mark the
2253
          // last byte for the lack of a better alternative.
2254
26.5k
          proto_tree_add_bitmask_list_value(subtree, out_tvb, offset + MIN(i, length - 1), 1, section_named_bits, value);
2255
26.5k
        }
2256
14.8k
      }
2257
251k
    }
2258
258k
    proto_item_append_text(actx->created_item, "]");
2259
258k
  }
2260
2261
267k
  return out_tvb;
2262
267k
}
2263
uint32_t
2264
dissect_per_bit_string(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, int min_len, int max_len, bool has_extension, int * const *named_bits, int num_named_bits, tvbuff_t **value_tvb, int *len)
2265
268k
{
2266
  /*int val_start, val_length;*/
2267
268k
  uint32_t length, fragmented_length = 0;
2268
268k
  header_field_info *hfi;
2269
268k
  bool is_fragmented = false;
2270
268k
  tvbuff_t *fragmented_tvb = NULL, *out_tvb = NULL, *fragment_tvb = NULL;
2271
2272
268k
  hfi = (hf_index <= 0) ? NULL : proto_registrar_get_nth(hf_index);
2273
2274
268k
DEBUG_ENTRY("dissect_per_bit_string");
2275
  /* 15.8 if the length is 0 bytes there will be no encoding */
2276
268k
  if(max_len==0) {
2277
0
    if (value_tvb)
2278
0
      *value_tvb = out_tvb;
2279
0
    if (len)
2280
0
      *len = 0;
2281
0
    return offset;
2282
0
  }
2283
2284
268k
  if (min_len == NO_BOUND) {
2285
3.83k
    min_len = 0;
2286
3.83k
  }
2287
  /* 15.6 If an extension marker is present in the size constraint specification of the bitstring type,
2288
   * a single bit shall be added to the field-list in a bit-field of length one.
2289
   * The bit shall be set to 1 if the length of this encoding is not within the range of the extension root,
2290
   * and zero otherwise.
2291
   */
2292
268k
   if (has_extension) {
2293
5.79k
     bool extension_present;
2294
5.79k
     offset = dissect_per_boolean(tvb, offset, actx, tree, hf_per_extension_present_bit, &extension_present);
2295
5.79k
    if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
2296
5.79k
    if(extension_present){
2297
844
    next_fragment1:
2298
844
      offset=dissect_per_length_determinant(tvb, offset, actx, tree, hf_per_bit_string_length, &length, &is_fragmented);
2299
844
      if(length || fragmented_length){
2300
        /* align to byte */
2301
595
        if (actx->aligned){
2302
573
          BYTE_ALIGN_OFFSET(offset);
2303
573
        }
2304
595
        if(is_fragmented){
2305
1
          fragment_tvb = tvb_new_octet_aligned(tvb, offset, length);
2306
1
          if(fragmented_length==0)
2307
0
            fragmented_tvb = tvb_new_composite();
2308
1
          tvb_composite_append(fragmented_tvb, fragment_tvb);
2309
1
          offset += length;
2310
1
          fragmented_length += length;
2311
1
          goto next_fragment1;
2312
1
        }
2313
594
        if(fragmented_length){
2314
0
          if(length){
2315
0
            tvb_composite_append(fragmented_tvb, tvb_new_octet_aligned(tvb, offset, length));
2316
0
            fragmented_length += length;
2317
0
          }
2318
0
          tvb_composite_finalize(fragmented_tvb);
2319
0
          add_new_data_source(actx->pinfo, fragmented_tvb, "Fragmented bitstring tvb");
2320
0
          out_tvb = dissect_per_bit_string_display(fragmented_tvb, 0, actx, tree, hf_index, hfi,
2321
0
                     fragmented_length, named_bits, num_named_bits);
2322
0
        }
2323
594
        else
2324
594
          out_tvb = dissect_per_bit_string_display(tvb, offset, actx, tree, hf_index, hfi, length, named_bits, num_named_bits);
2325
594
      }
2326
      /* XXX: ?? */
2327
      /*val_start = offset>>3;*/
2328
      /*val_length = (length+7)/8;*/
2329
843
      offset+=length;
2330
2331
843
      if (value_tvb)
2332
607
        *value_tvb = out_tvb;
2333
843
      if (len)
2334
364
        *len = fragmented_length ? fragmented_length : length;
2335
2336
843
      return offset;
2337
844
     }
2338
5.79k
   }
2339
2340
  /* 15.9 if length is fixed and less than or equal to sixteen bits*/
2341
268k
  if ((min_len==max_len) && (max_len<=16)) {
2342
138k
    out_tvb = dissect_per_bit_string_display(tvb, offset, actx, tree, hf_index, hfi, min_len, named_bits, num_named_bits);
2343
138k
    offset+=min_len;
2344
138k
    if (value_tvb)
2345
15.8k
      *value_tvb = out_tvb;
2346
138k
    if (len)
2347
0
      *len = min_len;
2348
138k
    return offset;
2349
138k
  }
2350
2351
2352
  /* 15.10 if length is fixed and less than to 64kbits*/
2353
129k
  if((min_len==max_len)&&(min_len<65536)){
2354
    /* (octet-aligned in the ALIGNED variant)
2355
     * align to byte
2356
     */
2357
101k
    if (actx->aligned){
2358
8.27k
      BYTE_ALIGN_OFFSET(offset);
2359
8.27k
    }
2360
101k
    out_tvb = dissect_per_bit_string_display(tvb, offset, actx, tree, hf_index, hfi, min_len, named_bits, num_named_bits);
2361
101k
    offset+=min_len;
2362
101k
    if (value_tvb)
2363
25.4k
      *value_tvb = out_tvb;
2364
101k
    if (len)
2365
0
      *len = min_len;
2366
101k
    return offset;
2367
101k
  }
2368
2369
  /* 15.11 */
2370
27.7k
  if (max_len != NO_BOUND && max_len < 65536) {
2371
23.1k
    offset=dissect_per_constrained_integer(tvb, offset, actx,
2372
23.1k
      tree, hf_per_bit_string_length, min_len, max_len,
2373
23.1k
      &length, false);
2374
23.1k
      if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
2375
23.1k
  } else {
2376
4.60k
  next_fragment2:
2377
4.55k
    offset=dissect_per_length_determinant(tvb, offset, actx, tree, hf_per_bit_string_length, &length, &is_fragmented);
2378
4.55k
  }
2379
27.7k
  if(length || fragmented_length){
2380
    /* align to byte */
2381
26.5k
    if (actx->aligned){
2382
6.19k
      BYTE_ALIGN_OFFSET(offset);
2383
6.19k
    }
2384
26.5k
    if(is_fragmented){
2385
7
      fragment_tvb = tvb_new_octet_aligned(tvb, offset, length);
2386
7
      if(fragmented_length==0)
2387
0
        fragmented_tvb = tvb_new_composite();
2388
7
      tvb_composite_append(fragmented_tvb, fragment_tvb);
2389
7
      offset += length;
2390
7
      fragmented_length += length;
2391
7
      goto next_fragment2;
2392
7
    }
2393
26.5k
    if(fragmented_length){
2394
0
      if(length){
2395
0
        tvb_composite_append(fragmented_tvb, tvb_new_octet_aligned(tvb, offset, length));
2396
0
        fragmented_length += length;
2397
0
      }
2398
0
      tvb_composite_finalize(fragmented_tvb);
2399
0
      add_new_data_source(actx->pinfo, fragmented_tvb, "Fragmented bitstring tvb");
2400
0
      out_tvb = dissect_per_bit_string_display(fragmented_tvb, 0, actx, tree, hf_index, hfi,
2401
0
                 fragmented_length, named_bits, num_named_bits);
2402
0
    }
2403
26.5k
    else
2404
26.5k
      out_tvb = dissect_per_bit_string_display(tvb, offset, actx, tree, hf_index, hfi, length, named_bits, num_named_bits);
2405
26.5k
  }
2406
  /* XXX: ?? */
2407
  /*val_start = offset>>3;*/
2408
  /*val_length = (length+7)/8;*/
2409
27.6k
  offset+=length;
2410
2411
27.6k
  if (value_tvb)
2412
4.75k
    *value_tvb = out_tvb;
2413
27.6k
  if (len)
2414
1.49k
    *len = fragmented_length ? fragmented_length : length;
2415
2416
27.6k
  return offset;
2417
27.6k
}
2418
2419
uint32_t dissect_per_bit_string_containing_pdu_new(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, int min_len, int max_len, bool has_extension, dissector_t type_cb)
2420
471
{
2421
471
  tvbuff_t *val_tvb = NULL;
2422
471
  proto_tree *subtree = tree;
2423
2424
471
  offset = dissect_per_bit_string(tvb, offset, actx, tree, hf_index, min_len, max_len, has_extension, NULL, 0, &val_tvb, NULL);
2425
2426
471
  if (type_cb && val_tvb) {
2427
371
    subtree = proto_item_add_subtree(actx->created_item, ett_per_containing);
2428
371
    type_cb(val_tvb, actx->pinfo, subtree, NULL);
2429
371
  }
2430
2431
471
  return offset;
2432
471
}
2433
2434
/* this function dissects an OCTET STRING
2435
  16.1
2436
  16.2
2437
  16.3
2438
  16.4
2439
  16.5
2440
  16.6
2441
  16.7
2442
  16.8
2443
2444
   max_len or min_len == NO_BOUND means there is no lower/upper constraint
2445
2446
   hf_index can either be a FT_BYTES or an FT_STRING
2447
*/
2448
uint32_t
2449
dissect_per_octet_string(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, int min_len, int max_len, bool has_extension, tvbuff_t **value_tvb)
2450
161k
{
2451
161k
  int val_start = 0, val_length;
2452
161k
  uint32_t length = 0, fragmented_length = 0;
2453
161k
  header_field_info *hfi;
2454
161k
  bool is_fragmented = false;
2455
161k
  tvbuff_t *out_tvb = NULL, *fragment_tvb = NULL;
2456
2457
161k
  hfi = (hf_index <= 0) ? NULL : proto_registrar_get_nth(hf_index);
2458
2459
161k
DEBUG_ENTRY("dissect_per_octet_string");
2460
2461
161k
  if (has_extension) {  /* 16.3 an extension marker is present */
2462
90
    bool extension_present;
2463
90
    offset = dissect_per_boolean(tvb, offset, actx, tree, hf_per_extension_present_bit, &extension_present);
2464
90
    if (!display_internal_per_fields) proto_item_set_hidden(actx->created_item);
2465
90
    if (extension_present) max_len = NO_BOUND;  /* skip to 16.8 */
2466
90
  }
2467
2468
161k
  if (min_len == NO_BOUND) {
2469
31.4k
    min_len = 0;
2470
31.4k
  }
2471
161k
  if (max_len==0) {  /* 16.5 if the length is 0 bytes there will be no encoding */
2472
0
    val_start = offset>>3;
2473
0
    val_length = 0;
2474
2475
161k
  } else if((min_len==max_len)&&(max_len<=2)) {
2476
    /* 16.6 if length is fixed and less than or equal to two bytes*/
2477
11.9k
    val_start = offset>>3;
2478
11.9k
    val_length = min_len;
2479
11.9k
    out_tvb = tvb_new_octet_aligned(tvb, offset, val_length * 8);
2480
    /* Add new data source if the offet was unaligned */
2481
11.9k
    if ((offset & 7) != 0) {
2482
6.78k
      add_new_data_source(actx->pinfo, out_tvb, "Unaligned OCTET STRING");
2483
6.78k
    }
2484
11.9k
    offset+=min_len*8;
2485
2486
149k
  } else if ((min_len==max_len)&&(min_len<65536)) {
2487
    /* 16.7 if length is fixed and less than to 64k*/
2488
2489
    /* align to byte */
2490
54.7k
    if (actx->aligned){
2491
48.0k
      BYTE_ALIGN_OFFSET(offset);
2492
48.0k
    }
2493
54.7k
    val_start = offset>>3;
2494
54.7k
    val_length = min_len;
2495
54.7k
    out_tvb = tvb_new_octet_aligned(tvb, offset, val_length * 8);
2496
54.7k
    if ((offset & 7) != 0) {
2497
5.87k
      add_new_data_source(actx->pinfo, out_tvb, "Unaligned OCTET STRING");
2498
5.87k
    }
2499
54.7k
    offset+=min_len*8;
2500
2501
94.5k
  } else {  /* 16.8 */
2502
94.5k
    if(max_len>0) {
2503
63.1k
      offset = dissect_per_constrained_integer(tvb, offset, actx, tree,
2504
63.1k
        hf_per_octet_string_length, min_len, max_len, &length, false);
2505
2506
63.1k
        if (!display_internal_per_fields)
2507
62.6k
          proto_item_set_hidden(actx->created_item);
2508
63.1k
    } else {
2509
31.4k
    next_fragment:
2510
31.4k
      offset = dissect_per_length_determinant(tvb, offset, actx, tree,
2511
31.4k
        hf_per_octet_string_length, &length, &is_fragmented);
2512
31.4k
    }
2513
2514
94.5k
    if(length || fragmented_length){
2515
      /* align to byte */
2516
84.5k
      if (actx->aligned){
2517
12.9k
        BYTE_ALIGN_OFFSET(offset);
2518
12.9k
      }
2519
84.5k
      if (is_fragmented) {
2520
74
        fragment_tvb = tvb_new_octet_aligned(tvb, offset, length * 8);
2521
74
        if (fragmented_length == 0)
2522
0
          out_tvb = tvb_new_composite();
2523
74
        tvb_composite_append(out_tvb, fragment_tvb);
2524
74
        offset += length * 8;
2525
74
        fragmented_length += length;
2526
74
        goto next_fragment;
2527
74
      }
2528
84.4k
      if (fragmented_length) {
2529
0
        if (length) {
2530
0
          tvb_composite_append(out_tvb, tvb_new_octet_aligned(tvb, offset, length * 8));
2531
0
          fragmented_length += length;
2532
0
        }
2533
0
        tvb_composite_finalize(out_tvb);
2534
0
        add_new_data_source(actx->pinfo, out_tvb, "Fragmented OCTET STRING");
2535
84.4k
      } else {
2536
84.4k
        out_tvb = tvb_new_octet_aligned(tvb, offset, length * 8);
2537
84.4k
        if ((offset & 7) != 0) {
2538
59.2k
          add_new_data_source(actx->pinfo, out_tvb, "Unaligned OCTET STRING");
2539
59.2k
        }
2540
84.4k
      }
2541
84.4k
    } else {
2542
10.0k
      val_start = offset>>3;
2543
10.0k
    }
2544
94.4k
    val_length = fragmented_length ? fragmented_length : length;
2545
94.4k
    offset+=length*8;
2546
94.4k
  }
2547
2548
161k
  if (hfi) {
2549
149k
    if (FT_IS_UINT(hfi->type)||FT_IS_INT(hfi->type)) {
2550
      /* If the type has been converted to FT_UINT or FT_INT in the .cnf file
2551
       * display the length of this octet string instead of the octetstring itself
2552
       */
2553
3.95k
      if (FT_IS_UINT(hfi->type))
2554
3.95k
        actx->created_item = proto_tree_add_uint(tree, hf_index, out_tvb, 0, val_length, val_length);
2555
0
      else
2556
0
        actx->created_item = proto_tree_add_int(tree, hf_index, out_tvb, 0, val_length, val_length);
2557
3.95k
      proto_item_append_text(actx->created_item, plurality(val_length, " octet", " octets"));
2558
145k
    } else {
2559
145k
      if(out_tvb){
2560
139k
        actx->created_item = proto_tree_add_item(tree, hf_index, out_tvb, 0, val_length, ENC_BIG_ENDIAN);
2561
139k
      }else{
2562
        /* Length = 0 */
2563
6.71k
        actx->created_item = proto_tree_add_item(tree, hf_index, tvb, val_start, val_length, ENC_BIG_ENDIAN);
2564
6.71k
      }
2565
145k
    }
2566
149k
  }
2567
2568
161k
  if (value_tvb)
2569
100k
    *value_tvb = (out_tvb) ? out_tvb : tvb_new_subset_length(tvb, val_start, val_length);
2570
2571
161k
  return offset;
2572
161k
}
2573
2574
uint32_t dissect_per_octet_string_containing_pdu_new(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, int min_len, int max_len, bool has_extension, dissector_t type_cb)
2575
7.53k
{
2576
7.53k
  tvbuff_t *val_tvb = NULL;
2577
7.53k
  proto_tree *subtree = tree;
2578
2579
7.53k
  offset = dissect_per_octet_string(tvb, offset, actx, tree, hf_index, min_len, max_len, has_extension, &val_tvb);
2580
2581
7.53k
  if (type_cb && val_tvb && (tvb_reported_length(val_tvb) > 0)) {
2582
4.44k
    subtree = proto_item_add_subtree(actx->created_item, ett_per_containing);
2583
4.44k
    type_cb(val_tvb, actx->pinfo, subtree, NULL);
2584
4.44k
  }
2585
2586
7.53k
  return offset;
2587
7.53k
}
2588
2589
uint32_t dissect_per_size_constrained_type(tvbuff_t *tvb, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index, per_type_fn type_cb, const char *name, int min_len, int max_len, bool has_extension)
2590
209
{
2591
209
  asn1_stack_frame_push(actx, name);
2592
209
  asn1_param_push_integer(actx, min_len);
2593
209
  asn1_param_push_integer(actx, max_len);
2594
209
  asn1_param_push_boolean(actx, has_extension);
2595
2596
209
  offset = type_cb(tvb, offset, actx, tree, hf_index);
2597
2598
209
  asn1_stack_frame_pop(actx, name);
2599
2600
209
  return offset;
2601
209
}
2602
2603
bool get_size_constraint_from_stack(asn1_ctx_t *actx, const char *name, int *pmin_len, int *pmax_len, bool *phas_extension)
2604
194
{
2605
194
  asn1_par_t *par;
2606
2607
194
  if (pmin_len) *pmin_len = NO_BOUND;
2608
194
  if (pmax_len) *pmax_len = NO_BOUND;
2609
194
  if (phas_extension) *phas_extension = false;
2610
2611
194
  if (!actx->stack) return false;
2612
194
  if (strcmp(actx->stack->name, name)) return false;
2613
2614
194
  par = actx->stack->par;
2615
194
  if (!par || (par->ptype != ASN1_PAR_INTEGER)) return false;
2616
194
  if (pmin_len) *pmin_len = par->value.v_integer;
2617
194
  par = par->next;
2618
194
  if (!par || (par->ptype != ASN1_PAR_INTEGER)) return false;
2619
194
  if (pmax_len) *pmax_len = par->value.v_integer;
2620
194
  par = par->next;
2621
194
  if (!par || (par->ptype != ASN1_PAR_BOOLEAN)) return false;
2622
194
  if (phas_extension) *phas_extension = par->value.v_boolean;
2623
2624
194
  return true;
2625
194
}
2626
2627
2628
/* 26 Encoding of a value of the external type */
2629
2630
/* code generated from definition in 26.1 */
2631
/*
2632
[UNIVERSAL 8] IMPLICIT SEQUENCE {
2633
  direct-reference OBJECT IDENTIFIER OPTIONAL,
2634
  indirect-reference INTEGER OPTIONAL,
2635
  data-value-descriptor ObjectDescriptor OPTIONAL,
2636
    encoding CHOICE {
2637
    single-ASN1-type [0] ABSTRACT-SYNTAX.&Type,
2638
    octet-aligned [1] IMPLICIT OCTET STRING,
2639
    arbitrary [2] IMPLICIT BIT STRING
2640
  }
2641
}
2642
*/
2643
/* NOTE: This sequence type differs from that in ITU-T Rec. X.680 | ISO/IEC 8824-1 for historical reasons. */
2644
2645
static int
2646
3
dissect_per_T_direct_reference(tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index) {
2647
2648
3
  DISSECTOR_ASSERT(actx);
2649
3
  offset = dissect_per_object_identifier_str(tvb, offset, actx, tree, hf_index, &actx->external.direct_reference);
2650
2651
3
  actx->external.direct_ref_present = true;
2652
3
  return offset;
2653
3
}
2654
2655
2656
2657
static int
2658
2
dissect_per_T_indirect_reference(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2659
2
  offset = dissect_per_integer(tvb, offset, actx, tree, hf_index, &actx->external.indirect_reference);
2660
2661
2
  actx->external.indirect_ref_present = true;
2662
2
  return offset;
2663
2
}
2664
2665
2666
2667
static int
2668
1
dissect_per_T_data_value_descriptor(tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index) {
2669
1
  offset = dissect_per_object_descriptor(tvb, offset, actx, tree, hf_index, &actx->external.data_value_descriptor);
2670
2671
1
  actx->external.data_value_descr_present = true;
2672
1
  return offset;
2673
1
}
2674
2675
2676
2677
static int
2678
1
dissect_per_T_single_ASN1_type(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2679
1
  offset = dissect_per_open_type(tvb, offset, actx, tree, actx->external.hf_index, actx->external.u.per.type_cb);
2680
2681
1
  return offset;
2682
1
}
2683
2684
2685
2686
static int
2687
0
dissect_per_T_octet_aligned(tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index) {
2688
0
  offset = dissect_per_octet_string(tvb, offset, actx, tree, hf_index,
2689
0
            NO_BOUND, NO_BOUND, false, &actx->external.octet_aligned);
2690
2691
0
  if (actx->external.octet_aligned) {
2692
0
    if (actx->external.u.per.type_cb) {
2693
0
      actx->external.u.per.type_cb(actx->external.octet_aligned, 0, actx, tree, actx->external.hf_index);
2694
0
    } else {
2695
0
      actx->created_item = proto_tree_add_expert(tree, actx->pinfo, &ei_per_external_type, actx->external.octet_aligned, 0, -1);
2696
0
    }
2697
0
  }
2698
0
  return offset;
2699
0
}
2700
2701
2702
2703
static int
2704
0
dissect_per_T_arbitrary(tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index) {
2705
0
  offset = dissect_per_bit_string(tvb, offset, actx, tree, hf_index,
2706
0
          NO_BOUND, NO_BOUND, false, NULL, 0, &actx->external.arbitrary, NULL);
2707
2708
0
  if (actx->external.arbitrary) {
2709
0
    if (actx->external.u.per.type_cb) {
2710
0
      actx->external.u.per.type_cb(actx->external.arbitrary, 0, actx, tree, actx->external.hf_index);
2711
0
    } else {
2712
0
      actx->created_item = proto_tree_add_expert(tree, actx->pinfo, &ei_per_external_type, actx->external.arbitrary, 0, -1);
2713
0
    }
2714
0
  }
2715
0
  return offset;
2716
0
}
2717
2718
2719
static const value_string per_External_encoding_vals[] = {
2720
  {   0, "single-ASN1-type" },
2721
  {   1, "octet-aligned" },
2722
  {   2, "arbitrary" },
2723
  { 0, NULL }
2724
};
2725
2726
static const per_choice_t External_encoding_choice[] = {
2727
  {   0, &hf_per_single_ASN1_type, ASN1_NO_EXTENSIONS     , dissect_per_T_single_ASN1_type },
2728
  {   1, &hf_per_octet_aligned   , ASN1_NO_EXTENSIONS     , dissect_per_T_octet_aligned },
2729
  {   2, &hf_per_arbitrary       , ASN1_NO_EXTENSIONS     , dissect_per_T_arbitrary },
2730
  { 0, NULL, 0, NULL }
2731
};
2732
2733
static int
2734
1
dissect_per_External_encoding(tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index) {
2735
  // This assertion is used to remove clang's warning.
2736
1
  DISSECTOR_ASSERT(actx);
2737
1
  offset = dissect_per_choice(tvb, offset, actx, tree, hf_index,
2738
1
            ett_per_External_encoding, External_encoding_choice,
2739
1
            &actx->external.encoding);
2740
2741
1
  return offset;
2742
1
}
2743
2744
2745
static const per_sequence_t External_sequence[] = {
2746
  { &hf_per_direct_reference, ASN1_NO_EXTENSIONS      , ASN1_OPTIONAL    , dissect_per_T_direct_reference },
2747
  { &hf_per_indirect_reference, ASN1_NO_EXTENSIONS    , ASN1_OPTIONAL    , dissect_per_T_indirect_reference },
2748
  { &hf_per_data_value_descriptor, ASN1_NO_EXTENSIONS , ASN1_OPTIONAL    , dissect_per_T_data_value_descriptor },
2749
  { &hf_per_encoding        , ASN1_NO_EXTENSIONS      , ASN1_NOT_OPTIONAL, dissect_per_External_encoding },
2750
  { NULL, 0, 0, NULL }
2751
};
2752
2753
static int
2754
3
dissect_per_External(tvbuff_t *tvb _U_, int offset _U_, asn1_ctx_t *actx _U_, proto_tree *tree _U_, int hf_index _U_) {
2755
3
  offset = dissect_per_sequence(tvb, offset, actx, tree, hf_index,
2756
3
              ett_per_External, External_sequence);
2757
2758
3
  return offset;
2759
3
}
2760
2761
uint32_t
2762
dissect_per_external_type(tvbuff_t *tvb _U_, uint32_t offset, asn1_ctx_t *actx, proto_tree *tree _U_, int hf_index _U_, per_type_fn type_cb)
2763
3
{
2764
3
  asn1_ctx_clean_external(actx);
2765
3
  actx->external.u.per.type_cb = type_cb;
2766
3
  offset = dissect_per_External(tvb, offset, actx, tree, hf_index);
2767
2768
3
  asn1_ctx_clean_external(actx);
2769
3
  return offset;
2770
3
}
2771
2772
/*
2773
 * Calls the callback defined with register_per_oid_dissector() if found.
2774
 * Offset is in bits.
2775
 */
2776
2777
int
2778
call_per_oid_callback(const char *oid, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset, asn1_ctx_t *actx, int hf_index)
2779
80
{
2780
80
  uint32_t type_length, end_offset, start_offset;
2781
80
  tvbuff_t *val_tvb = NULL;
2782
2783
80
  start_offset = offset;
2784
80
  offset = dissect_per_length_determinant(tvb, offset, actx, tree, hf_per_open_type_length, &type_length, NULL);
2785
80
  if(type_length == 0){
2786
2
    dissect_per_not_decoded_yet(tree, actx->pinfo, tvb, "unexpected length");
2787
2
  }
2788
80
  if (actx->aligned) BYTE_ALIGN_OFFSET(offset);
2789
80
  end_offset = offset + type_length;
2790
2791
2792
  /* length in bits */
2793
80
  val_tvb = tvb_new_octet_aligned(tvb, offset, type_length * 8);
2794
80
  if ((offset & 7) != 0) {
2795
0
    add_new_data_source(actx->pinfo, val_tvb, "Unaligned OCTET STRING");
2796
0
  }
2797
2798
80
  if (oid == NULL ||
2799
80
    (dissector_try_string_with_data(per_oid_dissector_table, oid, val_tvb, pinfo, tree, true, actx)) == 0)
2800
69
  {
2801
69
    proto_tree_add_expert(tree, pinfo, &ei_per_oid_not_implemented, val_tvb, 0, -1);
2802
69
    dissect_per_open_type(tvb, start_offset, actx, tree, hf_index, NULL);
2803
69
  }
2804
2805
80
  return end_offset;
2806
80
}
2807
2808
void
2809
register_per_oid_dissector(const char *oid, dissector_t dissector, int proto, const char *name)
2810
0
{
2811
0
  dissector_handle_t dissector_handle;
2812
2813
  /* FIXME: This would be better as register_dissector()
2814
   * so the dissector could be referenced by name
2815
   * from the command line, Lua, etc.
2816
   * But can we blindly trust name to be a unique dissector name,
2817
   * or should we prefix "per." or something?
2818
   */
2819
0
  dissector_handle = create_dissector_handle(dissector, proto);
2820
0
  dissector_add_string("per.oid", oid, dissector_handle);
2821
0
  oid_add_from_string(name, oid);
2822
0
}
2823
2824
2825
void
2826
proto_register_per(void)
2827
14
{
2828
14
  static hf_register_info hf[] = {
2829
14
    { &hf_per_num_sequence_extensions,
2830
14
      { "Number of Sequence Extensions", "per.num_sequence_extensions", FT_UINT32, BASE_DEC,
2831
14
        NULL, 0, "Number of extensions encoded in this sequence", HFILL }},
2832
14
    { &hf_per_choice_index,
2833
14
      { "Choice Index", "per.choice_index", FT_UINT32, BASE_DEC,
2834
14
        NULL, 0, "Which index of the Choice within extension root is encoded", HFILL }},
2835
14
    { &hf_per_choice_extension_index,
2836
14
      { "Choice Extension Index", "per.choice_extension_index", FT_UINT32, BASE_DEC,
2837
14
        NULL, 0, "Which index of the Choice within extension addition is encoded", HFILL }},
2838
14
    { &hf_per_enum_index,
2839
14
      { "Enumerated Index", "per.enum_index", FT_UINT32, BASE_DEC,
2840
14
        NULL, 0, "Which index of the Enumerated within extension root is encoded", HFILL }},
2841
14
    { &hf_per_enum_extension_index,
2842
14
      { "Enumerated Extension Index", "per.enum_extension_index", FT_UINT32, BASE_DEC,
2843
14
        NULL, 0, "Which index of the Enumerated within extension addition is encoded", HFILL }},
2844
14
    { &hf_per_GeneralString_length,
2845
14
      { "GeneralString Length", "per.generalstring_length", FT_UINT32, BASE_DEC,
2846
14
        NULL, 0, "Length of the GeneralString", HFILL }},
2847
14
    { &hf_per_extension_bit,
2848
14
      { "Extension Bit", "per.extension_bit", FT_BOOLEAN, 8,
2849
14
        TFS(&tfs_extension_bit), 0x01, "The extension bit of an aggregate", HFILL }},
2850
14
    { &hf_per_extension_present_bit,
2851
14
      { "Extension Present Bit", "per.extension_present_bit", FT_BOOLEAN, 8,
2852
14
        NULL, 0x01, "Whether this optional extension is present or not", HFILL }},
2853
14
    { &hf_per_small_number_bit,
2854
14
      { "Small Number Bit", "per.small_number_bit", FT_BOOLEAN, 8,
2855
14
        TFS(&tfs_small_number_bit), 0x01, "The small number bit for a section 10.6 integer", HFILL }},
2856
14
    { &hf_per_optional_field_bit,
2857
14
      { "Optional Field Bit", "per.optional_field_bit", FT_BOOLEAN, 8,
2858
14
        NULL, 0x01, "This bit specifies the presence/absence of an optional field", HFILL }},
2859
14
    { &hf_per_sequence_of_length,
2860
14
      { "Sequence-Of Length", "per.sequence_of_length", FT_UINT32, BASE_DEC,
2861
14
        NULL, 0, "Number of items in the Sequence Of", HFILL }},
2862
14
    { &hf_per_object_identifier_length,
2863
14
      { "Object Identifier Length", "per.object_length", FT_UINT32, BASE_DEC,
2864
14
        NULL, 0, "Length of the object identifier", HFILL }},
2865
14
    { &hf_per_open_type_length,
2866
14
      { "Open Type Length", "per.open_type_length", FT_UINT32, BASE_DEC,
2867
14
        NULL, 0, "Length of an open type encoding", HFILL }},
2868
14
    { &hf_per_real_length,
2869
14
      { "Real Length", "per.real_length", FT_UINT32, BASE_DEC,
2870
14
        NULL, 0, "Length of an real encoding", HFILL }},
2871
14
    { &hf_per_octet_string_length,
2872
14
      { "Octet String Length", "per.octet_string_length", FT_UINT32, BASE_DEC,
2873
14
        NULL, 0, "Number of bytes in the Octet String", HFILL }},
2874
14
    { &hf_per_bit_string_length,
2875
14
      { "Bit String Length", "per.bit_string_length", FT_UINT32, BASE_DEC,
2876
14
        NULL, 0, "Number of bits in the Bit String", HFILL }},
2877
14
    { &hf_per_normally_small_nonnegative_whole_number_length,
2878
14
      { "Normally Small Non-negative Whole Number Length", "per.normally_small_nonnegative_whole_number_length", FT_UINT32, BASE_DEC,
2879
14
        NULL, 0, "Number of bytes in the Normally Small Non-negative Whole Number", HFILL }},
2880
14
    { &hf_per_const_int_len,
2881
14
      { "Constrained Integer Length", "per.const_int_len", FT_UINT32, BASE_DEC,
2882
14
        NULL, 0, "Number of bytes in the Constrained Integer", HFILL }},
2883
14
    { &hf_per_direct_reference,
2884
14
      { "direct-reference", "per.direct_reference",
2885
14
        FT_OID, BASE_NONE, NULL, 0,
2886
14
        "per.T_direct_reference", HFILL }},
2887
14
    { &hf_per_indirect_reference,
2888
14
      { "indirect-reference", "per.indirect_reference",
2889
14
        FT_INT32, BASE_DEC, NULL, 0,
2890
14
        "per.T_indirect_reference", HFILL }},
2891
14
    { &hf_per_data_value_descriptor,
2892
14
      { "data-value-descriptor", "per.data_value_descriptor",
2893
14
        FT_STRING, BASE_NONE, NULL, 0,
2894
14
        "per.T_data_value_descriptor", HFILL }},
2895
14
    { &hf_per_encoding,
2896
14
      { "encoding", "per.encoding",
2897
14
        FT_UINT32, BASE_DEC, VALS(per_External_encoding_vals), 0,
2898
14
        "per.External_encoding", HFILL }},
2899
14
    { &hf_per_single_ASN1_type,
2900
14
      { "single-ASN1-type", "per.single_ASN1_type",
2901
14
        FT_NONE, BASE_NONE, NULL, 0,
2902
14
        "per.T_single_ASN1_type", HFILL }},
2903
14
    { &hf_per_octet_aligned,
2904
14
      { "octet-aligned", "per.octet_aligned",
2905
14
        FT_BYTES, BASE_NONE, NULL, 0,
2906
14
        "per.T_octet_aligned", HFILL }},
2907
14
    { &hf_per_arbitrary,
2908
14
      { "arbitrary", "per.arbitrary",
2909
14
        FT_BYTES, BASE_NONE, NULL, 0,
2910
14
        "per.T_arbitrary", HFILL }},
2911
14
    { &hf_per_integer_length,
2912
14
      { "integer length", "per.integer_length",
2913
14
        FT_UINT32, BASE_DEC, NULL, 0,
2914
14
        NULL, HFILL }},
2915
#if 0
2916
    { &hf_per_debug_pos,
2917
      { "Current bit offset", "per.debug_pos",
2918
        FT_UINT32, BASE_DEC, NULL, 0,
2919
        NULL, HFILL }},
2920
#endif
2921
14
    { &hf_per_internal_range,
2922
14
      { "Range", "per.internal.range",
2923
14
        FT_UINT64, BASE_DEC, NULL, 0,
2924
14
        NULL, HFILL }},
2925
14
    { &hf_per_internal_num_bits,
2926
14
      { "Bitfield length", "per.internal.num_bits",
2927
14
        FT_UINT32, BASE_DEC, NULL, 0,
2928
14
        NULL, HFILL }},
2929
14
    { &hf_per_internal_min,
2930
14
      { "Min", "per.internal.min",
2931
14
        FT_UINT32, BASE_DEC, NULL, 0,
2932
14
        NULL, HFILL }},
2933
14
    { &hf_per_internal_value,
2934
14
      { "Bits", "per.internal.value",
2935
14
        FT_UINT64, BASE_DEC, NULL, 0,
2936
14
        NULL, HFILL }},
2937
14
    { &hf_per_internal_min_int,
2938
14
      { "Min", "per.internal.min_int",
2939
14
        FT_INT32, BASE_DEC, NULL, 0,
2940
14
        NULL, HFILL } },
2941
14
    { &hf_per_internal_value_int,
2942
14
      { "Bits", "per.internal.value_int",
2943
14
        FT_INT64, BASE_DEC, NULL, 0,
2944
14
        NULL, HFILL } },
2945
14
    { &hf_per_encoding_boiler_plate,
2946
14
      { "PER encoded protocol, to see PER internal fields set protocol PER preferences", "per.encoding_boiler_plate",
2947
14
        FT_NONE, BASE_NONE, NULL, 0x0,
2948
14
        NULL, HFILL } },
2949
2950
14
  };
2951
2952
14
  static int *ett[] = {
2953
14
    &ett_per_open_type,
2954
14
    &ett_per_containing,
2955
14
    &ett_per_sequence_of_item,
2956
14
    &ett_per_External,
2957
14
    &ett_per_External_encoding,
2958
14
    &ett_per_named_bits,
2959
14
  };
2960
14
  static ei_register_info ei[] = {
2961
14
    { &ei_per_size_constraint_value,
2962
14
      { "per.size_constraint.value", PI_PROTOCOL, PI_WARN, "Size constraint: value too big", EXPFILL }},
2963
14
    { &ei_per_size_constraint_too_few,
2964
14
      { "per.size_constraint.too_few", PI_PROTOCOL, PI_WARN, "Size constraint: too few items", EXPFILL }},
2965
14
    { &ei_per_size_constraint_too_many,
2966
14
      { "per.size_constraint.too_many", PI_PROTOCOL, PI_WARN, "Size constraint: too many items", EXPFILL }},
2967
14
    { &ei_per_choice_extension_unknown,
2968
14
      { "per.choice_extension_unknown", PI_UNDECODED, PI_NOTE, "unknown choice extension", EXPFILL }},
2969
14
    { &ei_per_sequence_extension_unknown,
2970
14
      { "per.sequence_extension_unknown", PI_UNDECODED, PI_NOTE, "unknown sequence extension", EXPFILL }},
2971
14
    { &ei_per_encoding_error,
2972
14
      { "per.encoding_error", PI_MALFORMED, PI_WARN, "Encoding error", EXPFILL }},
2973
14
    { &ei_per_oid_not_implemented,
2974
14
      { "per.error.oid_not_implemented", PI_UNDECODED, PI_WARN, "PER: Dissector for OID not implemented. Contact Wireshark developers if you want this supported", EXPFILL }},
2975
14
    { &ei_per_undecoded,
2976
14
      { "per.error.undecoded", PI_UNDECODED, PI_WARN, "PER: Something unknown here", EXPFILL }},
2977
14
    { &ei_per_field_not_integer,
2978
14
      { "per.field_not_integer", PI_PROTOCOL, PI_ERROR, "Field is not an integer", EXPFILL }},
2979
14
    { &ei_per_external_type,
2980
14
      { "per.external_type.unknown", PI_PROTOCOL, PI_WARN, "Unknown EXTERNAL Type", EXPFILL }},
2981
14
    { &ei_per_open_type,
2982
14
      { "per.open_type.unknown", PI_PROTOCOL, PI_WARN, "Unknown Open Type", EXPFILL }},
2983
14
    { &ei_per_open_type_len,
2984
14
      { "per.open_type.len", PI_PROTOCOL, PI_ERROR, "Open Type length > available data(tvb)", EXPFILL }}
2985
14
  };
2986
2987
14
  module_t *per_module;
2988
14
  expert_module_t* expert_per;
2989
2990
14
  proto_per = proto_register_protocol("Packed Encoding Rules (ASN.1 X.691)", "PER", "per");
2991
14
  proto_register_field_array(proto_per, hf, array_length(hf));
2992
14
  proto_register_subtree_array(ett, array_length(ett));
2993
14
  expert_per = expert_register_protocol(proto_per);
2994
14
  expert_register_field_array(expert_per, ei, array_length(ei));
2995
2996
14
  proto_set_cant_toggle(proto_per);
2997
2998
14
  per_module = prefs_register_protocol(proto_per, NULL);
2999
14
  prefs_register_bool_preference(per_module, "display_internal_per_fields",
3000
14
               "Display the internal PER fields in the tree",
3001
14
               "Whether the dissector should put the internal PER data in the tree or if it should hide it",
3002
14
               &display_internal_per_fields);
3003
3004
14
  per_oid_dissector_table = register_dissector_table("per.oid", "PER OID", proto_per, FT_STRING, STRING_CASE_SENSITIVE);
3005
3006
3007
14
}
3008
3009
/*
3010
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
3011
 *
3012
 * Local variables:
3013
 * c-basic-offset: 8
3014
 * tab-width: 8
3015
 * indent-tabs-mode: t
3016
 * End:
3017
 *
3018
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
3019
 * :indentSize=8:tabSize=8:noTabs=false:
3020
 */