Coverage Report

Created: 2025-12-31 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtasn1/lib/parser_aux.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2000-2025 Free Software Foundation, Inc.
3
 *
4
 * This file is part of LIBTASN1.
5
 *
6
 * The LIBTASN1 library is free software; you can redistribute it
7
 * and/or modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful, but
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see
18
 * <https://www.gnu.org/licenses/>.
19
 */
20
21
#include <config.h>
22
23
#include <limits.h>   /* WORD_BIT */
24
25
#include "int.h"
26
#include "parser_aux.h"
27
#include "gstr.h"
28
#include "structure.h"
29
#include "element.h"
30
#include "c-ctype.h"
31
32
char _asn1_identifierMissing[ASN1_MAX_NAME_SIZE + 1]; /* identifier name not found */
33
34
/* Return a hash of the N bytes of X using the method described by
35
   Bruno Haible in https://www.haible.de/bruno/hashfunc.html.
36
   Note that while many hash functions reduce their result via modulo
37
   to a 0..table_size-1 range, this function does not do that.
38
39
   This implementation has been changed from size_t -> unsigned int. */
40
41
#ifdef __clang__
42
__attribute__((no_sanitize ("integer")))
43
#endif
44
     _GL_ATTRIBUTE_PURE static unsigned int _asn1_hash_name (const char *x)
45
381k
{
46
381k
  const unsigned char *s = (unsigned char *) x;
47
381k
  unsigned h = 0;
48
49
2.61M
  while (*s)
50
2.23M
    h = (*s++) + ((h << 9) | (h >> (WORD_BIT - 9)));
51
52
381k
  return h;
53
381k
}
54
55
/******************************************************/
56
/* Function : _asn1_add_static_node                   */
57
/* Description: creates a new NODE_ASN element and    */
58
/* puts it in the list pointed by e_list.       */
59
/* Parameters:                                        */
60
/*   e_list: of type list_type; must be NULL initially */
61
/*   type: type of the new element (see ASN1_ETYPE_   */
62
/*         and CONST_ constants).                     */
63
/* Return: pointer to the new element.                */
64
/******************************************************/
65
asn1_node
66
_asn1_add_static_node (list_type **e_list, unsigned int type)
67
51.1k
{
68
51.1k
  list_type *p;
69
51.1k
  asn1_node punt;
70
71
51.1k
  punt = calloc (1, sizeof (struct asn1_node_st));
72
51.1k
  if (punt == NULL)
73
0
    return NULL;
74
75
51.1k
  p = malloc (sizeof (list_type));
76
51.1k
  if (p == NULL)
77
0
    {
78
0
      free (punt);
79
0
      return NULL;
80
0
    }
81
82
51.1k
  p->node = punt;
83
51.1k
  p->next = *e_list;
84
51.1k
  *e_list = p;
85
86
51.1k
  punt->type = type;
87
88
51.1k
  return punt;
89
51.1k
}
90
91
static int
92
_asn1_add_static_node2 (list_type **e_list, asn1_node node)
93
74.0k
{
94
74.0k
  list_type *p;
95
96
74.0k
  p = malloc (sizeof (list_type));
97
74.0k
  if (p == NULL)
98
0
    {
99
0
      return -1;
100
0
    }
101
102
74.0k
  p->node = node;
103
74.0k
  p->next = *e_list;
104
74.0k
  *e_list = p;
105
106
74.0k
  return 0;
107
74.0k
}
108
109
/**
110
 * asn1_find_node:
111
 * @pointer: NODE_ASN element pointer.
112
 * @name: null terminated string with the element's name to find.
113
 *
114
 * Searches for an element called @name starting from @pointer.  The
115
 * name is composed by different identifiers separated by dots.  When
116
 * *@pointer has a name, the first identifier must be the name of
117
 * *@pointer, otherwise it must be the name of one child of *@pointer.
118
 *
119
 * Returns: the search result, or %NULL if not found.
120
 **/
121
asn1_node
122
asn1_find_node (asn1_node_const pointer, const char *name)
123
108k
{
124
108k
  asn1_node_const p;
125
108k
  char *n_end, n[ASN1_MAX_NAME_SIZE + 1];
126
108k
  const char *n_start;
127
108k
  unsigned int nsize;
128
108k
  unsigned int nhash;
129
108k
  const struct asn1_node_array_st *numbered_children;
130
131
108k
  if (pointer == NULL)
132
1.25k
    return NULL;
133
134
107k
  if (name == NULL)
135
0
    return NULL;
136
137
107k
  p = pointer;
138
107k
  n_start = name;
139
140
107k
  if (name[0] == '?' && name[1] == 'C' && p->name[0] == '?')
141
1.44k
    {       /* ?CURRENT */
142
1.44k
      n_start = strchr (n_start, '.');
143
1.44k
      if (n_start)
144
1.32k
  n_start++;
145
1.44k
    }
146
106k
  else if (p->name[0] != 0)
147
103k
    {       /* has *pointer got a name ? */
148
103k
      n_end = strchr (n_start, '.');  /* search the first dot */
149
103k
      if (n_end)
150
102k
  {
151
102k
    nsize = n_end - n_start;
152
102k
    if (nsize >= sizeof (n))
153
0
      return NULL;
154
155
102k
    memcpy (n, n_start, nsize);
156
102k
    n[nsize] = 0;
157
102k
    n_start = n_end;
158
102k
    n_start++;
159
160
102k
    nhash = _asn1_hash_name (n);
161
102k
  }
162
209
      else
163
209
  {
164
209
    _asn1_str_cpy (n, sizeof (n), n_start);
165
209
    nhash = _asn1_hash_name (n);
166
167
209
    n_start = NULL;
168
209
  }
169
170
103k
      while (p)
171
103k
  {
172
103k
    if (nhash == p->name_hash && (!strcmp (p->name, n)))
173
102k
      break;
174
122
    else
175
122
      p = p->right;
176
103k
  }     /* while */
177
178
103k
      if (p == NULL)
179
122
  return NULL;
180
103k
    }
181
3.17k
  else
182
3.17k
    {       /* *pointer doesn't have a name */
183
3.17k
      if (n_start[0] == 0)
184
411
  return (asn1_node) p;
185
3.17k
    }
186
187
216k
  while (n_start)
188
110k
    {       /* Has the end of NAME been reached? */
189
110k
      n_end = strchr (n_start, '.');  /* search the next dot */
190
110k
      if (n_end)
191
3.39k
  {
192
3.39k
    nsize = n_end - n_start;
193
3.39k
    if (nsize >= sizeof (n))
194
0
      return NULL;
195
196
3.39k
    memcpy (n, n_start, nsize);
197
3.39k
    n[nsize] = 0;
198
3.39k
    n_start = n_end;
199
3.39k
    n_start++;
200
201
3.39k
    nhash = _asn1_hash_name (n);
202
3.39k
  }
203
106k
      else
204
106k
  {
205
106k
    _asn1_str_cpy (n, sizeof (n), n_start);
206
106k
    nhash = _asn1_hash_name (n);
207
106k
    n_start = NULL;
208
106k
  }
209
210
110k
      if (p->down == NULL)
211
80
  return NULL;
212
213
110k
      numbered_children = &p->numbered_children;
214
110k
      p = p->down;
215
110k
      if (p == NULL)
216
0
  return NULL;
217
218
      /* The identifier "?LAST" indicates the last element
219
         in the right chain. */
220
110k
      if (n[0] == '?' && n[1] == 'L')  /* ?LAST */
221
3.18k
  {
222
18.8k
    while (p->right)
223
15.6k
      p = p->right;
224
3.18k
  }
225
106k
      else
226
106k
  {     /* no "?LAST" */
227
106k
    if (n[0] == '?' && c_isdigit (n[1]))
228
1.05k
      {
229
1.05k
        long position = strtol (n + 1, NULL, 10);
230
1.05k
        if (position > 0 && position < LONG_MAX)
231
301
    p = _asn1_node_array_get (numbered_children, position - 1);
232
1.05k
      }
233
2.54M
    while (p)
234
2.54M
      {
235
2.54M
        if (p->name_hash == nhash && !strcmp (p->name, n))
236
106k
    break;
237
2.43M
        else
238
2.43M
    p = p->right;
239
2.54M
      }
240
106k
  }
241
110k
      if (p == NULL)
242
858
  return NULL;
243
110k
    }        /* while */
244
245
106k
  return (asn1_node) p;
246
107k
}
247
248
249
/******************************************************************/
250
/* Function : _asn1_set_value                                     */
251
/* Description: sets the field VALUE in a NODE_ASN element. The   */
252
/*              previous value (if exist) will be lost            */
253
/* Parameters:                                                    */
254
/*   node: element pointer.                                       */
255
/*   value: pointer to the value that you want to set.            */
256
/*   len: character number of value.                              */
257
/* Return: pointer to the NODE_ASN element.                       */
258
/******************************************************************/
259
asn1_node
260
_asn1_set_value (asn1_node node, const void *value, unsigned int len)
261
284k
{
262
284k
  if (node == NULL)
263
0
    return node;
264
284k
  if (node->value)
265
3.47k
    {
266
3.47k
      if (node->value != node->small_value)
267
1.02k
  free (node->value);
268
3.47k
      node->value = NULL;
269
3.47k
      node->value_len = 0;
270
3.47k
    }
271
272
284k
  if (!len)
273
6.55k
    return node;
274
275
278k
  if (len < sizeof (node->small_value))
276
207k
    {
277
207k
      node->value = node->small_value;
278
207k
    }
279
71.0k
  else
280
71.0k
    {
281
71.0k
      node->value = malloc (len);
282
71.0k
      if (node->value == NULL)
283
0
  return NULL;
284
71.0k
    }
285
278k
  node->value_len = len;
286
287
278k
  memcpy (node->value, value, len);
288
278k
  return node;
289
278k
}
290
291
/******************************************************************/
292
/* Function : _asn1_set_value_lv                                  */
293
/* Description: sets the field VALUE in a NODE_ASN element. The   */
294
/*              previous value (if exist) will be lost. The value */
295
/*    given is stored as an length-value format (LV     */
296
/* Parameters:                                                    */
297
/*   node: element pointer.                                       */
298
/*   value: pointer to the value that you want to set.            */
299
/*   len: character number of value.                              */
300
/* Return: pointer to the NODE_ASN element.                       */
301
/******************************************************************/
302
asn1_node
303
_asn1_set_value_lv (asn1_node node, const void *value, unsigned int len)
304
5.71k
{
305
5.71k
  int len2;
306
5.71k
  void *temp;
307
308
5.71k
  if (node == NULL)
309
0
    return node;
310
311
5.71k
  asn1_length_der (len, NULL, &len2);
312
5.71k
  temp = malloc (len + len2);
313
5.71k
  if (temp == NULL)
314
0
    return NULL;
315
316
5.71k
  asn1_octet_der (value, len, temp, &len2);
317
5.71k
  return _asn1_set_value_m (node, temp, len2);
318
5.71k
}
319
320
/* the same as _asn1_set_value except that it sets an already malloc'ed
321
 * value.
322
 */
323
asn1_node
324
_asn1_set_value_m (asn1_node node, void *value, unsigned int len)
325
5.71k
{
326
5.71k
  if (node == NULL)
327
0
    return node;
328
329
5.71k
  if (node->value)
330
0
    {
331
0
      if (node->value != node->small_value)
332
0
  free (node->value);
333
0
      node->value = NULL;
334
0
      node->value_len = 0;
335
0
    }
336
337
5.71k
  if (!len)
338
0
    return node;
339
340
5.71k
  node->value = value;
341
5.71k
  node->value_len = len;
342
343
5.71k
  return node;
344
5.71k
}
345
346
/******************************************************************/
347
/* Function : _asn1_append_value                                  */
348
/* Description: appends to the field VALUE in a NODE_ASN element. */
349
/*                        */
350
/* Parameters:                                                    */
351
/*   node: element pointer.                                       */
352
/*   value: pointer to the value that you want to be appended.    */
353
/*   len: character number of value.                              */
354
/* Return: pointer to the NODE_ASN element.                       */
355
/******************************************************************/
356
asn1_node
357
_asn1_append_value (asn1_node node, const void *value, unsigned int len)
358
0
{
359
0
  if (node == NULL)
360
0
    return node;
361
362
0
  if (node->value == NULL)
363
0
    return _asn1_set_value (node, value, len);
364
365
0
  if (len == 0)
366
0
    return node;
367
368
0
  if (node->value == node->small_value)
369
0
    {
370
      /* value is in node */
371
0
      int prev_len = node->value_len;
372
0
      node->value_len += len;
373
0
      node->value = malloc (node->value_len);
374
0
      if (node->value == NULL)
375
0
  {
376
0
    node->value_len = 0;
377
0
    return NULL;
378
0
  }
379
380
0
      if (prev_len > 0)
381
0
  memcpy (node->value, node->small_value, prev_len);
382
383
0
      memcpy (&node->value[prev_len], value, len);
384
385
0
      return node;
386
0
    }
387
0
  else        /* if (node->value != NULL && node->value != node->small_value) */
388
0
    {
389
      /* value is allocated */
390
0
      int prev_len = node->value_len;
391
0
      node->value_len += len;
392
393
0
      node->value = _asn1_realloc (node->value, node->value_len);
394
0
      if (node->value == NULL)
395
0
  {
396
0
    node->value_len = 0;
397
0
    return NULL;
398
0
  }
399
400
0
      memcpy (&node->value[prev_len], value, len);
401
402
0
      return node;
403
0
    }
404
0
}
405
406
/******************************************************************/
407
/* Function : _asn1_set_name                                      */
408
/* Description: sets the field NAME in a NODE_ASN element. The    */
409
/*              previous value (if exist) will be lost            */
410
/* Parameters:                                                    */
411
/*   node: element pointer.                                       */
412
/*   name: a null terminated string with the name that you want   */
413
/*         to set.                                                */
414
/* Return: pointer to the NODE_ASN element.                       */
415
/******************************************************************/
416
asn1_node
417
_asn1_set_name (asn1_node node, const char *name)
418
168k
{
419
168k
  if (node == NULL)
420
0
    return node;
421
422
168k
  _asn1_str_cpy (node->name, sizeof (node->name), name ? name : "");
423
168k
  node->name_hash = _asn1_hash_name (node->name);
424
425
168k
  return node;
426
168k
}
427
428
/******************************************************************/
429
/* Function : _asn1_cpy_name                                      */
430
/* Description: copies the field NAME in a NODE_ASN element.      */
431
/* Parameters:                                                    */
432
/*   dst: a dest element pointer.                                 */
433
/*   src: a source element pointer.                               */
434
/* Return: pointer to the NODE_ASN element.                       */
435
/******************************************************************/
436
asn1_node
437
_asn1_cpy_name (asn1_node dst, asn1_node_const src)
438
509k
{
439
509k
  if (dst == NULL)
440
0
    return dst;
441
442
509k
  if (src == NULL)
443
0
    {
444
0
      dst->name[0] = 0;
445
0
      dst->name_hash = _asn1_hash_name (dst->name);
446
0
      return dst;
447
0
    }
448
449
509k
  _asn1_str_cpy (dst->name, sizeof (dst->name), src->name);
450
509k
  dst->name_hash = src->name_hash;
451
452
509k
  return dst;
453
509k
}
454
455
/******************************************************************/
456
/* Function : _asn1_set_right                                     */
457
/* Description: sets the field RIGHT in a NODE_ASN element.       */
458
/* Parameters:                                                    */
459
/*   node: element pointer.                                       */
460
/*   right: pointer to a NODE_ASN element that you want be pointed*/
461
/*          by NODE.                                              */
462
/* Return: pointer to *NODE.                                      */
463
/******************************************************************/
464
asn1_node
465
_asn1_set_right (asn1_node node, asn1_node right)
466
514k
{
467
514k
  if (node == NULL)
468
266
    return node;
469
513k
  node->right = right;
470
513k
  if (right)
471
495k
    right->left = node;
472
513k
  return node;
473
514k
}
474
475
476
/******************************************************************/
477
/* Function : _asn1_get_last_right                                */
478
/* Description: return the last element along the right chain.    */
479
/* Parameters:                                                    */
480
/*   node: starting element pointer.                              */
481
/* Return: pointer to the last element along the right chain.     */
482
/******************************************************************/
483
asn1_node
484
_asn1_get_last_right (asn1_node_const node)
485
22.2k
{
486
22.2k
  asn1_node_const p;
487
488
22.2k
  if (node == NULL)
489
0
    return NULL;
490
22.2k
  p = node;
491
1.47M
  while (p->right)
492
1.45M
    p = p->right;
493
22.2k
  return (asn1_node) p;
494
22.2k
}
495
496
/******************************************************************/
497
/* Function : _asn1_remove_node                                   */
498
/* Description: gets free the memory allocated for an NODE_ASN    */
499
/*              element (not the elements pointed by it).         */
500
/* Parameters:                                                    */
501
/*   node: NODE_ASN element pointer.                              */
502
/*   flags: ASN1_DELETE_FLAG_*                                    */
503
/******************************************************************/
504
void
505
_asn1_remove_node (asn1_node node, unsigned int flags)
506
699k
{
507
699k
  if (node == NULL)
508
2.58k
    return;
509
510
696k
  if (node->value != NULL)
511
280k
    {
512
280k
      if (flags & ASN1_DELETE_FLAG_ZEROIZE)
513
0
  {
514
0
    safe_memset (node->value, 0, node->value_len);
515
0
  }
516
517
280k
      if (node->value != node->small_value)
518
75.6k
  free (node->value);
519
280k
    }
520
521
696k
  free (node->numbered_children.nodes);
522
696k
  free (node);
523
696k
}
524
525
/******************************************************************/
526
/* Function : _asn1_find_up                                       */
527
/* Description: return the father of the NODE_ASN element.        */
528
/* Parameters:                                                    */
529
/*   node: NODE_ASN element pointer.                              */
530
/* Return: Null if not found.                                     */
531
/******************************************************************/
532
asn1_node
533
_asn1_find_up (asn1_node_const node)
534
1.31M
{
535
1.31M
  asn1_node_const p;
536
537
1.31M
  if (node == NULL)
538
0
    return NULL;
539
540
1.31M
  p = node;
541
542
2.46M
  while ((p->left != NULL) && (p->left->right == p))
543
1.15M
    p = p->left;
544
545
1.31M
  return p->left;
546
1.31M
}
547
548
static unsigned
549
_asn1_is_up (asn1_node_const up_cand, asn1_node_const down)
550
9.81k
{
551
9.81k
  asn1_node_const d, u;
552
553
9.81k
  if (up_cand == NULL || down == NULL)
554
0
    return 0;
555
556
9.81k
  d = down;
557
558
21.7k
  while ((u = _asn1_find_up (d)) != NULL && u != d)
559
11.9k
    {
560
11.9k
      if (u == up_cand)
561
1
  return 1;
562
11.9k
      d = u;
563
11.9k
    }
564
565
9.81k
  return 0;
566
9.81k
}
567
568
/******************************************************************/
569
/* Function : _asn1_delete_node_from_list                         */
570
/* Description: deletes the list element given                    */
571
/******************************************************************/
572
void
573
_asn1_delete_node_from_list (list_type *list, asn1_node node)
574
10.4k
{
575
10.4k
  list_type *p = list;
576
577
1.06M
  while (p)
578
1.05M
    {
579
1.05M
      if (p->node == node)
580
10.4k
  p->node = NULL;
581
1.05M
      p = p->next;
582
1.05M
    }
583
10.4k
}
584
585
/******************************************************************/
586
/* Function : _asn1_delete_list                                   */
587
/* Description: deletes the list elements (not the elements       */
588
/*  pointed by them).                                             */
589
/******************************************************************/
590
void
591
_asn1_delete_list (list_type *e_list)
592
936
{
593
936
  list_type *p;
594
595
35.8k
  while (e_list)
596
34.8k
    {
597
34.8k
      p = e_list;
598
34.8k
      e_list = e_list->next;
599
34.8k
      free (p);
600
34.8k
    }
601
936
}
602
603
/******************************************************************/
604
/* Function : _asn1_delete_list_and nodes                         */
605
/* Description: deletes the list elements and the elements        */
606
/*  pointed by them.                                              */
607
/******************************************************************/
608
void
609
_asn1_delete_list_and_nodes (list_type *e_list)
610
2.70k
{
611
2.70k
  list_type *p;
612
613
93.0k
  while (e_list)
614
90.3k
    {
615
90.3k
      p = e_list;
616
90.3k
      e_list = e_list->next;
617
90.3k
      _asn1_remove_node (p->node, 0);
618
90.3k
      free (p);
619
90.3k
    }
620
2.70k
}
621
622
623
char *
624
_asn1_ltostr (int64_t v, char str[LTOSTR_MAX_SIZE])
625
4.64M
{
626
4.64M
  uint64_t d, r;
627
4.64M
  char temp[LTOSTR_MAX_SIZE];
628
4.64M
  int count, k, start;
629
4.64M
  uint64_t val;
630
631
4.64M
  if (v < 0)
632
5.52k
    {
633
5.52k
      str[0] = '-';
634
5.52k
      start = 1;
635
5.52k
      val = -((uint64_t) v);
636
5.52k
    }
637
4.64M
  else
638
4.64M
    {
639
4.64M
      val = v;
640
4.64M
      start = 0;
641
4.64M
    }
642
643
4.64M
  count = 0;
644
4.64M
  do
645
4.98M
    {
646
4.98M
      d = val / 10;
647
4.98M
      r = val - d * 10;
648
4.98M
      temp[start + count] = '0' + (char) r;
649
4.98M
      count++;
650
4.98M
      val = d;
651
4.98M
    }
652
4.98M
  while (val && ((start + count) < LTOSTR_MAX_SIZE - 1));
653
654
9.63M
  for (k = 0; k < count; k++)
655
4.98M
    str[k + start] = temp[start + count - k - 1];
656
4.64M
  str[count + start] = 0;
657
4.64M
  return str;
658
4.64M
}
659
660
661
/******************************************************************/
662
/* Function : _asn1_change_integer_value                          */
663
/* Description: converts into DER coding the value assign to an   */
664
/*   INTEGER constant.                                            */
665
/* Parameters:                                                    */
666
/*   node: root of an ASN1element.                                */
667
/* Return:                                                        */
668
/*   ASN1_ELEMENT_NOT_FOUND if NODE is NULL,                       */
669
/*   otherwise ASN1_SUCCESS                                             */
670
/******************************************************************/
671
int
672
_asn1_change_integer_value (asn1_node node)
673
1.21k
{
674
1.21k
  asn1_node p;
675
1.21k
  unsigned char val[SIZEOF_UNSIGNED_LONG_INT];
676
1.21k
  unsigned char val2[SIZEOF_UNSIGNED_LONG_INT + 1];
677
1.21k
  int len;
678
679
1.21k
  if (node == NULL)
680
0
    return ASN1_ELEMENT_NOT_FOUND;
681
682
1.21k
  p = node;
683
19.9k
  while (p)
684
18.7k
    {
685
18.7k
      if ((type_field (p->type) == ASN1_ETYPE_INTEGER)
686
2.93k
    && (p->type & CONST_ASSIGN))
687
2.61k
  {
688
2.61k
    if (p->value)
689
2.50k
      {
690
2.50k
        _asn1_convert_integer (p->value, val, sizeof (val), &len);
691
2.50k
        asn1_octet_der (val, len, val2, &len);
692
2.50k
        _asn1_set_value (p, val2, len);
693
2.50k
      }
694
2.61k
  }
695
696
18.7k
      if (p->down)
697
7.16k
  {
698
7.16k
    p = p->down;
699
7.16k
  }
700
11.5k
      else
701
11.5k
  {
702
11.5k
    if (p == node)
703
127
      p = NULL;
704
11.4k
    else if (p->right)
705
6.94k
      p = p->right;
706
4.48k
    else
707
4.48k
      {
708
7.16k
        while (1)
709
7.16k
    {
710
7.16k
      p = _asn1_find_up (p);
711
7.16k
      if (p == node)
712
1.08k
        {
713
1.08k
          p = NULL;
714
1.08k
          break;
715
1.08k
        }
716
6.07k
      if (p && p->right)
717
3.40k
        {
718
3.40k
          p = p->right;
719
3.40k
          break;
720
3.40k
        }
721
6.07k
    }
722
4.48k
      }
723
11.5k
  }
724
18.7k
    }
725
726
1.21k
  return ASN1_SUCCESS;
727
1.21k
}
728
729
74.0k
#define MAX_CONSTANTS 1024
730
/******************************************************************/
731
/* Function : _asn1_expand_object_id                              */
732
/* Description: expand the IDs of an OBJECT IDENTIFIER constant.  */
733
/* Parameters:                                                    */
734
/*   list: root of an object list                                 */
735
/*   node: root of an ASN1 element.                               */
736
/* Return:                                                        */
737
/*   ASN1_ELEMENT_NOT_FOUND if NODE is NULL,                      */
738
/*   otherwise ASN1_SUCCESS                                       */
739
/******************************************************************/
740
int
741
_asn1_expand_object_id (list_type **list, asn1_node node)
742
1.21k
{
743
1.21k
  asn1_node p, p2, p3, p4, p5;
744
1.21k
  char name_root[ASN1_MAX_NAME_SIZE], name2[2 * ASN1_MAX_NAME_SIZE + 1];
745
1.21k
  int move, tlen, tries;
746
1.21k
  unsigned max_constants;
747
748
1.21k
  if (node == NULL)
749
0
    return ASN1_ELEMENT_NOT_FOUND;
750
751
1.21k
  _asn1_str_cpy (name_root, sizeof (name_root), node->name);
752
753
1.21k
  p = node;
754
1.21k
  move = DOWN;
755
1.21k
  tries = 0;
756
757
44.5k
  while (!((p == node) && (move == UP)))
758
43.5k
    {
759
43.5k
      if (move != UP)
760
38.7k
  {
761
38.7k
    if ((type_field (p->type) == ASN1_ETYPE_OBJECT_ID)
762
14.8k
        && (p->type & CONST_ASSIGN))
763
13.0k
      {
764
13.0k
        p2 = p->down;
765
13.0k
        if (p2 && (type_field (p2->type) == ASN1_ETYPE_CONSTANT))
766
10.8k
    {
767
10.8k
      if (p2->value && !c_isdigit (p2->value[0]))
768
9.92k
        {
769
9.92k
          _asn1_str_cpy (name2, sizeof (name2), name_root);
770
9.92k
          _asn1_str_cat (name2, sizeof (name2), ".");
771
9.92k
          _asn1_str_cat (name2, sizeof (name2),
772
9.92k
             (char *) p2->value);
773
9.92k
          p3 = asn1_find_node (node, name2);
774
9.92k
          if (!p3 || _asn1_is_up (p2, p3) ||
775
9.81k
        (type_field (p3->type) != ASN1_ETYPE_OBJECT_ID) ||
776
9.79k
        !(p3->type & CONST_ASSIGN))
777
133
      return ASN1_ELEMENT_NOT_FOUND;
778
779
9.79k
          _asn1_set_down (p, p2->right);
780
9.79k
          if (p2->down)
781
79
      _asn1_delete_structure (*list, &p2->down, 0);
782
9.79k
          _asn1_delete_node_from_list (*list, p2);
783
9.79k
          _asn1_remove_node (p2, 0);
784
9.79k
          p2 = p;
785
9.79k
          p4 = p3->down;
786
9.79k
          max_constants = 0;
787
87.2k
          while (p4)
788
77.4k
      {
789
77.4k
        if (type_field (p4->type) == ASN1_ETYPE_CONSTANT)
790
74.0k
          {
791
74.0k
            max_constants++;
792
74.0k
            if (max_constants == MAX_CONSTANTS)
793
22
        return ASN1_RECURSION;
794
795
74.0k
            p5 =
796
74.0k
        _asn1_add_single_node (ASN1_ETYPE_CONSTANT);
797
74.0k
            _asn1_set_name (p5, p4->name);
798
74.0k
            if (p4->value)
799
68.2k
        {
800
68.2k
          tlen = _asn1_strlen (p4->value);
801
68.2k
          if (tlen > 0)
802
66.5k
            _asn1_set_value (p5, p4->value, tlen + 1);
803
68.2k
        }
804
74.0k
            _asn1_add_static_node2 (list, p5);
805
806
74.0k
            if (p2 == p)
807
5.81k
        {
808
5.81k
          _asn1_set_right (p5, p->down);
809
5.81k
          _asn1_set_down (p, p5);
810
5.81k
        }
811
68.2k
            else
812
68.2k
        {
813
68.2k
          _asn1_set_right (p5, p2->right);
814
68.2k
          _asn1_set_right (p2, p5);
815
68.2k
        }
816
74.0k
            p2 = p5;
817
74.0k
          }
818
77.4k
        p4 = p4->right;
819
77.4k
      }
820
9.76k
          move = DOWN;
821
822
9.76k
          tries++;
823
9.76k
          if (tries >= EXPAND_OBJECT_ID_MAX_RECURSION)
824
93
      return ASN1_RECURSION;
825
826
9.67k
          continue;
827
9.76k
        }
828
10.8k
    }
829
13.0k
      }
830
28.8k
    move = DOWN;
831
28.8k
  }
832
4.78k
      else
833
4.78k
  move = RIGHT;
834
835
33.6k
      tries = 0;
836
33.6k
      if (move == DOWN)
837
28.8k
  {
838
28.8k
    if (p->down)
839
5.84k
      p = p->down;
840
23.0k
    else
841
23.0k
      move = RIGHT;
842
28.8k
  }
843
844
33.6k
      if (p == node)
845
127
  {
846
127
    move = UP;
847
127
    continue;
848
127
  }
849
850
33.4k
      if (move == RIGHT)
851
27.6k
  {
852
27.6k
    if (p && p->right)
853
22.0k
      p = p->right;
854
5.62k
    else
855
5.62k
      move = UP;
856
27.6k
  }
857
33.4k
      if (move == UP)
858
5.62k
  p = _asn1_find_up (p);
859
33.4k
    }
860
861
  /*******************************/
862
  /*       expand DEFAULT        */
863
  /*******************************/
864
967
  p = node;
865
967
  move = DOWN;
866
867
32.7k
  while (!((p == node) && (move == UP)))
868
31.7k
    {
869
31.7k
      if (move != UP)
870
27.0k
  {
871
27.0k
    if ((type_field (p->type) == ASN1_ETYPE_OBJECT_ID) &&
872
4.61k
        (p->type & CONST_DEFAULT))
873
1.86k
      {
874
1.86k
        p2 = p->down;
875
1.86k
        if (p2 && (type_field (p2->type) == ASN1_ETYPE_DEFAULT))
876
1.46k
    {
877
1.46k
      _asn1_str_cpy (name2, sizeof (name2), name_root);
878
1.46k
      _asn1_str_cat (name2, sizeof (name2), ".");
879
1.46k
      if (p2->value)
880
1.09k
        _asn1_str_cat (name2, sizeof (name2), (char *) p2->value);
881
1.46k
      p3 = asn1_find_node (node, name2);
882
1.46k
      if (!p3 || (type_field (p3->type) != ASN1_ETYPE_OBJECT_ID)
883
1.44k
          || !(p3->type & CONST_ASSIGN))
884
28
        return ASN1_ELEMENT_NOT_FOUND;
885
1.43k
      p4 = p3->down;
886
1.43k
      name2[0] = 0;
887
7.21k
      while (p4)
888
5.77k
        {
889
5.77k
          if (type_field (p4->type) == ASN1_ETYPE_CONSTANT)
890
3.42k
      {
891
3.42k
        if (p4->value == NULL)
892
3
          return ASN1_VALUE_NOT_FOUND;
893
894
3.42k
        if (name2[0])
895
2.33k
          _asn1_str_cat (name2, sizeof (name2), ".");
896
3.42k
        _asn1_str_cat (name2, sizeof (name2),
897
3.42k
           (char *) p4->value);
898
3.42k
      }
899
5.77k
          p4 = p4->right;
900
5.77k
        }
901
1.43k
      tlen = strlen (name2);
902
1.43k
      if (tlen > 0)
903
909
        _asn1_set_value (p2, name2, tlen + 1);
904
1.43k
    }
905
1.86k
      }
906
27.0k
    move = DOWN;
907
27.0k
  }
908
4.69k
      else
909
4.69k
  move = RIGHT;
910
911
31.7k
      if (move == DOWN)
912
27.0k
  {
913
27.0k
    if (p->down)
914
5.51k
      p = p->down;
915
21.5k
    else
916
21.5k
      move = RIGHT;
917
27.0k
  }
918
919
31.7k
      if (p == node)
920
127
  {
921
127
    move = UP;
922
127
    continue;
923
127
  }
924
925
31.6k
      if (move == RIGHT)
926
26.1k
  {
927
26.1k
    if (p && p->right)
928
20.6k
      p = p->right;
929
5.50k
    else
930
5.50k
      move = UP;
931
26.1k
  }
932
31.6k
      if (move == UP)
933
5.50k
  p = _asn1_find_up (p);
934
31.6k
    }
935
936
936
  return ASN1_SUCCESS;
937
967
}
938
939
940
/******************************************************************/
941
/* Function : _asn1_type_set_config                               */
942
/* Description: sets the CONST_SET and CONST_NOT_USED properties  */
943
/*   in the fields of the SET elements.                           */
944
/* Parameters:                                                    */
945
/*   node: root of an ASN1 element.                               */
946
/* Return:                                                        */
947
/*   ASN1_ELEMENT_NOT_FOUND if NODE is NULL,                       */
948
/*   otherwise ASN1_SUCCESS                                             */
949
/******************************************************************/
950
int
951
_asn1_type_set_config (asn1_node node)
952
1.09k
{
953
1.09k
  asn1_node p, p2;
954
1.09k
  int move;
955
956
1.09k
  if (node == NULL)
957
0
    return ASN1_ELEMENT_NOT_FOUND;
958
959
1.09k
  p = node;
960
1.09k
  move = DOWN;
961
962
21.2k
  while (!((p == node) && (move == UP)))
963
20.1k
    {
964
20.1k
      if (move != UP)
965
15.7k
  {
966
15.7k
    if (type_field (p->type) == ASN1_ETYPE_SET)
967
577
      {
968
577
        p2 = p->down;
969
4.61k
        while (p2)
970
4.03k
    {
971
4.03k
      if (type_field (p2->type) != ASN1_ETYPE_TAG)
972
3.69k
        p2->type |= CONST_SET | CONST_NOT_USED;
973
4.03k
      p2 = p2->right;
974
4.03k
    }
975
577
      }
976
15.7k
    move = DOWN;
977
15.7k
  }
978
4.40k
      else
979
4.40k
  move = RIGHT;
980
981
20.1k
      if (move == DOWN)
982
15.7k
  {
983
15.7k
    if (p->down)
984
5.49k
      p = p->down;
985
10.2k
    else
986
10.2k
      move = RIGHT;
987
15.7k
  }
988
989
20.1k
      if (p == node)
990
0
  {
991
0
    move = UP;
992
0
    continue;
993
0
  }
994
995
20.1k
      if (move == RIGHT)
996
14.6k
  {
997
14.6k
    if (p && p->right)
998
9.19k
      p = p->right;
999
5.49k
    else
1000
5.49k
      move = UP;
1001
14.6k
  }
1002
20.1k
      if (move == UP)
1003
5.49k
  p = _asn1_find_up (p);
1004
20.1k
    }
1005
1006
1.09k
  return ASN1_SUCCESS;
1007
1.09k
}
1008
1009
1010
/******************************************************************/
1011
/* Function : _asn1_check_identifier                              */
1012
/* Description: checks the definitions of all the identifiers     */
1013
/*   and the first element of an OBJECT_ID (e.g. {pkix 0 4}).     */
1014
/*   The _asn1_identifierMissing global variable is filled if     */
1015
/*   necessary.                                                   */
1016
/* Parameters:                                                    */
1017
/*   node: root of an ASN1 element.                               */
1018
/* Return:                                                        */
1019
/*   ASN1_ELEMENT_NOT_FOUND      if NODE is NULL,                 */
1020
/*   ASN1_IDENTIFIER_NOT_FOUND   if an identifier is not defined, */
1021
/*   otherwise ASN1_SUCCESS                                       */
1022
/******************************************************************/
1023
int
1024
_asn1_check_identifier (asn1_node_const node)
1025
2.19k
{
1026
2.19k
  asn1_node_const p, p2;
1027
2.19k
  char name2[ASN1_MAX_NAME_SIZE * 2 + 2];
1028
1029
2.19k
  if (node == NULL)
1030
6
    return ASN1_ELEMENT_NOT_FOUND;
1031
1032
2.18k
  p = node;
1033
22.1k
  while (p)
1034
20.9k
    {
1035
20.9k
      if (p->value && type_field (p->type) == ASN1_ETYPE_IDENTIFIER)
1036
2.44k
  {
1037
2.44k
    _asn1_str_cpy (name2, sizeof (name2), node->name);
1038
2.44k
    _asn1_str_cat (name2, sizeof (name2), ".");
1039
2.44k
    _asn1_str_cat (name2, sizeof (name2), (char *) p->value);
1040
2.44k
    p2 = asn1_find_node (node, name2);
1041
2.44k
    if (p2 == NULL)
1042
898
      {
1043
898
        if (p->value)
1044
898
    _asn1_str_cpy (_asn1_identifierMissing,
1045
898
             sizeof (_asn1_identifierMissing),
1046
898
             (char *) p->value);
1047
0
        else
1048
0
    _asn1_strcpy (_asn1_identifierMissing, "(null)");
1049
898
        return ASN1_IDENTIFIER_NOT_FOUND;
1050
898
      }
1051
2.44k
  }
1052
18.5k
      else if ((type_field (p->type) == ASN1_ETYPE_OBJECT_ID) &&
1053
5.79k
         (p->type & CONST_DEFAULT))
1054
2.02k
  {
1055
2.02k
    p2 = p->down;
1056
2.02k
    if (p2 && (type_field (p2->type) == ASN1_ETYPE_DEFAULT))
1057
1.54k
      {
1058
1.54k
        _asn1_str_cpy (name2, sizeof (name2), node->name);
1059
1.54k
        if (p2->value)
1060
1.11k
    {
1061
1.11k
      _asn1_str_cat (name2, sizeof (name2), ".");
1062
1.11k
      _asn1_str_cat (name2, sizeof (name2), (char *) p2->value);
1063
1.11k
      _asn1_str_cpy (_asn1_identifierMissing,
1064
1.11k
         sizeof (_asn1_identifierMissing),
1065
1.11k
         (char *) p2->value);
1066
1.11k
    }
1067
425
        else
1068
425
    _asn1_strcpy (_asn1_identifierMissing, "(null)");
1069
1070
1.54k
        p2 = asn1_find_node (node, name2);
1071
1.54k
        if (!p2 || (type_field (p2->type) != ASN1_ETYPE_OBJECT_ID) ||
1072
1.50k
      !(p2->type & CONST_ASSIGN))
1073
41
    return ASN1_IDENTIFIER_NOT_FOUND;
1074
1.50k
        else
1075
1.50k
    _asn1_identifierMissing[0] = 0;
1076
1.54k
      }
1077
2.02k
  }
1078
16.4k
      else if ((type_field (p->type) == ASN1_ETYPE_OBJECT_ID) &&
1079
3.76k
         (p->type & CONST_ASSIGN))
1080
2.38k
  {
1081
2.38k
    p2 = p->down;
1082
2.38k
    if (p2 && (type_field (p2->type) == ASN1_ETYPE_CONSTANT))
1083
2.10k
      {
1084
2.10k
        if (p2->value && !c_isdigit (p2->value[0]))
1085
1.71k
    {
1086
1.71k
      _asn1_str_cpy (name2, sizeof (name2), node->name);
1087
1.71k
      _asn1_str_cat (name2, sizeof (name2), ".");
1088
1.71k
      _asn1_str_cat (name2, sizeof (name2), (char *) p2->value);
1089
1.71k
      _asn1_str_cpy (_asn1_identifierMissing,
1090
1.71k
         sizeof (_asn1_identifierMissing),
1091
1.71k
         (char *) p2->value);
1092
1093
1.71k
      p2 = asn1_find_node (node, name2);
1094
1.71k
      if (!p2 || (type_field (p2->type) != ASN1_ETYPE_OBJECT_ID)
1095
1.68k
          || !(p2->type & CONST_ASSIGN))
1096
34
        return ASN1_IDENTIFIER_NOT_FOUND;
1097
1.68k
      else
1098
1.68k
        _asn1_identifierMissing[0] = 0;
1099
1.71k
    }
1100
2.10k
      }
1101
2.38k
  }
1102
1103
19.9k
      if (p->down)
1104
7.80k
  {
1105
7.80k
    p = p->down;
1106
7.80k
  }
1107
12.1k
      else if (p->right)
1108
7.54k
  p = p->right;
1109
4.63k
      else
1110
4.63k
  {
1111
7.43k
    while (p)
1112
7.30k
      {
1113
7.30k
        p = _asn1_find_up (p);
1114
7.30k
        if (p == node)
1115
1.08k
    {
1116
1.08k
      p = NULL;
1117
1.08k
      break;
1118
1.08k
    }
1119
6.21k
        if (p && p->right)
1120
3.41k
    {
1121
3.41k
      p = p->right;
1122
3.41k
      break;
1123
3.41k
    }
1124
6.21k
      }
1125
4.63k
  }
1126
19.9k
    }
1127
1128
1.21k
  return ASN1_SUCCESS;
1129
2.18k
}
1130
1131
1132
/******************************************************************/
1133
/* Function : _asn1_set_default_tag                               */
1134
/* Description: sets the default IMPLICIT or EXPLICIT property in */
1135
/*   the tagged elements that don't have this declaration.        */
1136
/* Parameters:                                                    */
1137
/*   node: pointer to a DEFINITIONS element.                      */
1138
/* Return:                                                        */
1139
/*   ASN1_ELEMENT_NOT_FOUND if NODE is NULL or not a pointer to   */
1140
/*     a DEFINITIONS element,                                     */
1141
/*   otherwise ASN1_SUCCESS                                       */
1142
/******************************************************************/
1143
int
1144
_asn1_set_default_tag (asn1_node node)
1145
1.09k
{
1146
1.09k
  asn1_node p;
1147
1148
1.09k
  if ((node == NULL) || (type_field (node->type) != ASN1_ETYPE_DEFINITIONS))
1149
0
    return ASN1_ELEMENT_NOT_FOUND;
1150
1151
1.09k
  p = node;
1152
16.8k
  while (p)
1153
15.7k
    {
1154
15.7k
      if ((type_field (p->type) == ASN1_ETYPE_TAG) &&
1155
1.56k
    !(p->type & CONST_EXPLICIT) && !(p->type & CONST_IMPLICIT))
1156
1.18k
  {
1157
1.18k
    if (node->type & CONST_EXPLICIT)
1158
849
      p->type |= CONST_EXPLICIT;
1159
332
    else
1160
332
      p->type |= CONST_IMPLICIT;
1161
1.18k
  }
1162
1163
15.7k
      if (p->down)
1164
5.49k
  {
1165
5.49k
    p = p->down;
1166
5.49k
  }
1167
10.2k
      else if (p->right)
1168
6.01k
  p = p->right;
1169
4.27k
      else
1170
4.27k
  {
1171
5.49k
    while (1)
1172
5.49k
      {
1173
5.49k
        p = _asn1_find_up (p);
1174
5.49k
        if (p == node)
1175
1.09k
    {
1176
1.09k
      p = NULL;
1177
1.09k
      break;
1178
1.09k
    }
1179
4.40k
        if (p && p->right)
1180
3.18k
    {
1181
3.18k
      p = p->right;
1182
3.18k
      break;
1183
3.18k
    }
1184
4.40k
      }
1185
4.27k
  }
1186
15.7k
    }
1187
1188
1.09k
  return ASN1_SUCCESS;
1189
1.09k
}