Coverage Report

Created: 2024-11-25 06:31

/src/libtasn1/lib/coding.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2002-2024 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, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
 * 02110-1301, USA
20
 */
21
22
23
/*****************************************************/
24
/* File: coding.c                                    */
25
/* Description: Functions to create a DER coding of  */
26
/*   an ASN1 type.                                   */
27
/*****************************************************/
28
29
#include <int.h>
30
#include "parser_aux.h"
31
#include <gstr.h>
32
#include "element.h"
33
#include "minmax.h"
34
#include <structure.h>
35
36
#define MAX_TAG_LEN 16
37
38
/******************************************************/
39
/* Function : _asn1_error_description_value_not_found */
40
/* Description: creates the ErrorDescription string   */
41
/* for the ASN1_VALUE_NOT_FOUND error.                */
42
/* Parameters:                                        */
43
/*   node: node of the tree where the value is NULL.  */
44
/*   ErrorDescription: string returned.               */
45
/* Return:                                            */
46
/******************************************************/
47
static void
48
_asn1_error_description_value_not_found (asn1_node node,
49
           char *ErrorDescription)
50
1
{
51
52
1
  if (ErrorDescription == NULL)
53
1
    return;
54
55
0
  Estrcpy (ErrorDescription, ":: value of element '");
56
0
  _asn1_hierarchical_name (node, ErrorDescription + strlen (ErrorDescription),
57
0
         ASN1_MAX_ERROR_DESCRIPTION_SIZE - 40);
58
0
  Estrcat (ErrorDescription, "' not found");
59
60
0
}
61
62
/**
63
 * asn1_length_der:
64
 * @len: value to convert.
65
 * @der: buffer to hold the returned encoding (may be %NULL).
66
 * @der_len: number of meaningful bytes of ANS (der[0]..der[der_len-1]).
67
 *
68
 * Creates the DER encoding of the provided length value.
69
 * The @der buffer must have enough room for the output. The maximum
70
 * length this function will encode is %ASN1_MAX_LENGTH_SIZE.
71
 *
72
 * To know the size of the DER encoding use a %NULL value for @der.
73
 **/
74
void
75
asn1_length_der (unsigned long int len, unsigned char *der, int *der_len)
76
2.87M
{
77
2.87M
  int k;
78
2.87M
  unsigned char temp[ASN1_MAX_LENGTH_SIZE];
79
#if SIZEOF_UNSIGNED_LONG_INT > 8
80
  len &= 0xFFFFFFFFFFFFFFFF;
81
#endif
82
83
2.87M
  if (len < 128)
84
2.73M
    {
85
      /* short form */
86
2.73M
      if (der != NULL)
87
1.51M
  der[0] = (unsigned char) len;
88
2.73M
      *der_len = 1;
89
2.73M
    }
90
135k
  else
91
135k
    {
92
      /* Long form */
93
135k
      k = 0;
94
389k
      while (len)
95
254k
  {
96
254k
    temp[k++] = len & 0xFF;
97
254k
    len = len >> 8;
98
254k
  }
99
135k
      *der_len = k + 1;
100
135k
      if (der != NULL)
101
90.5k
  {
102
90.5k
    der[0] = ((unsigned char) k & 0x7F) + 128;
103
260k
    while (k--)
104
170k
      der[*der_len - 1 - k] = temp[k];
105
90.5k
  }
106
135k
    }
107
2.87M
}
108
109
/******************************************************/
110
/* Function : _asn1_tag_der                           */
111
/* Description: creates the DER coding for the CLASS  */
112
/* and TAG parameters.                                */
113
/* It is limited by the ASN1_MAX_TAG_SIZE variable    */
114
/* Parameters:                                        */
115
/*   class: value to convert.                         */
116
/*   tag_value: value to convert.                     */
117
/*   ans: string returned.                            */
118
/*   ans_len: number of meaningful bytes of ANS       */
119
/*            (ans[0]..ans[ans_len-1]).               */
120
/* Return:                                            */
121
/******************************************************/
122
static void
123
_asn1_tag_der (unsigned char class, unsigned int tag_value,
124
         unsigned char ans[ASN1_MAX_TAG_SIZE], int *ans_len)
125
937k
{
126
937k
  int k;
127
937k
  unsigned char temp[ASN1_MAX_TAG_SIZE];
128
129
937k
  if (tag_value < 31)
130
937k
    {
131
      /* short form */
132
937k
      ans[0] = (class & 0xE0) + ((unsigned char) (tag_value & 0x1F));
133
937k
      *ans_len = 1;
134
937k
    }
135
0
  else
136
0
    {
137
      /* Long form */
138
0
      ans[0] = (class & 0xE0) + 31;
139
0
      k = 0;
140
0
      while (tag_value != 0)
141
0
  {
142
0
    temp[k++] = tag_value & 0x7F;
143
0
    tag_value >>= 7;
144
145
0
    if (k > ASN1_MAX_TAG_SIZE - 1)
146
0
      break;   /* will not encode larger tags */
147
0
  }
148
0
      *ans_len = k + 1;
149
0
      while (k--)
150
0
  ans[*ans_len - 1 - k] = temp[k] + 128;
151
0
      ans[*ans_len - 1] -= 128;
152
0
    }
153
937k
}
154
155
/**
156
 * asn1_octet_der:
157
 * @str: the input data.
158
 * @str_len: STR length (str[0]..str[*str_len-1]).
159
 * @der: encoded string returned.
160
 * @der_len: number of meaningful bytes of DER (der[0]..der[der_len-1]).
161
 *
162
 * Creates a length-value DER encoding for the input data.
163
 * The DER encoding of the input data will be placed in the @der variable.
164
 *
165
 * Note that the OCTET STRING tag is not included in the output.
166
 *
167
 * This function does not return any value because it is expected
168
 * that @der_len will contain enough bytes to store the string
169
 * plus the DER encoding. The DER encoding size can be obtained using
170
 * asn1_length_der().
171
 **/
172
void
173
asn1_octet_der (const unsigned char *str, int str_len,
174
    unsigned char *der, int *der_len)
175
1.06M
{
176
1.06M
  int len_len;
177
178
1.06M
  if (der == NULL || str_len < 0)
179
0
    return;
180
181
1.06M
  asn1_length_der (str_len, der, &len_len);
182
1.06M
  memcpy (der + len_len, str, str_len);
183
1.06M
  *der_len = str_len + len_len;
184
1.06M
}
185
186
187
/**
188
 * asn1_encode_simple_der:
189
 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
190
 * @str: the string data.
191
 * @str_len: the string length
192
 * @tl: the encoded tag and length
193
 * @tl_len: the bytes of the @tl field
194
 *
195
 * Creates the DER encoding for various simple ASN.1 types like strings etc.
196
 * It stores the tag and length in @tl, which should have space for at least
197
 * %ASN1_MAX_TL_SIZE bytes. Initially @tl_len should contain the size of @tl.
198
 *
199
 * The complete DER encoding should consist of the value in @tl appended
200
 * with the provided @str.
201
 *
202
 * Returns: %ASN1_SUCCESS if successful or an error value.
203
 **/
204
int
205
asn1_encode_simple_der (unsigned int etype, const unsigned char *str,
206
      unsigned int str_len, unsigned char *tl,
207
      unsigned int *tl_len)
208
193
{
209
193
  int tag_len, len_len;
210
193
  unsigned tlen;
211
193
  unsigned char der_tag[ASN1_MAX_TAG_SIZE];
212
193
  unsigned char der_length[ASN1_MAX_LENGTH_SIZE];
213
193
  unsigned char *p;
214
215
193
  if (str == NULL)
216
0
    return ASN1_VALUE_NOT_VALID;
217
218
193
  if (ETYPE_OK (etype) == 0)
219
0
    return ASN1_VALUE_NOT_VALID;
220
221
  /* doesn't handle constructed classes */
222
193
  if (ETYPE_CLASS (etype) != ASN1_CLASS_UNIVERSAL)
223
0
    return ASN1_VALUE_NOT_VALID;
224
225
193
  _asn1_tag_der (ETYPE_CLASS (etype), ETYPE_TAG (etype), der_tag, &tag_len);
226
227
193
  asn1_length_der (str_len, der_length, &len_len);
228
229
193
  if (tag_len <= 0 || len_len <= 0)
230
0
    return ASN1_VALUE_NOT_VALID;
231
232
193
  tlen = tag_len + len_len;
233
234
193
  if (*tl_len < tlen)
235
0
    return ASN1_MEM_ERROR;
236
237
193
  p = tl;
238
193
  memcpy (p, der_tag, tag_len);
239
193
  p += tag_len;
240
193
  memcpy (p, der_length, len_len);
241
242
193
  *tl_len = tlen;
243
244
193
  return ASN1_SUCCESS;
245
193
}
246
247
/******************************************************/
248
/* Function : _asn1_time_der                          */
249
/* Description: creates the DER coding for a TIME     */
250
/* type (length included).                            */
251
/* Parameters:                                        */
252
/*   str: TIME null-terminated string.                */
253
/*   der: string returned.                            */
254
/*   der_len: number of meaningful bytes of DER       */
255
/*            (der[0]..der[ans_len-1]). Initially it  */
256
/*            if must store the length of DER.        */
257
/* Return:                                            */
258
/*   ASN1_MEM_ERROR when DER isn't big enough         */
259
/*   ASN1_SUCCESS otherwise                           */
260
/******************************************************/
261
static int
262
_asn1_time_der (unsigned char *str, int str_len, unsigned char *der,
263
    int *der_len)
264
17.3k
{
265
17.3k
  int len_len;
266
17.3k
  int max_len;
267
268
17.3k
  max_len = *der_len;
269
270
17.3k
  asn1_length_der (str_len, (max_len > 0) ? der : NULL, &len_len);
271
272
17.3k
  if ((len_len + str_len) <= max_len)
273
8.68k
    memcpy (der + len_len, str, str_len);
274
17.3k
  *der_len = len_len + str_len;
275
276
17.3k
  if ((*der_len) > max_len)
277
8.68k
    return ASN1_MEM_ERROR;
278
279
8.68k
  return ASN1_SUCCESS;
280
17.3k
}
281
282
283
/*
284
void
285
_asn1_get_utctime_der(unsigned char *der,int *der_len,unsigned char *str)
286
{
287
  int len_len,str_len;
288
  char temp[20];
289
290
  if(str==NULL) return;
291
  str_len=asn1_get_length_der(der,*der_len,&len_len);
292
  if (str_len<0) return;
293
  memcpy(temp,der+len_len,str_len);
294
  *der_len=str_len+len_len;
295
  switch(str_len)
296
  {
297
  case 11:
298
    temp[10]=0;
299
    strcat(temp,"00+0000");
300
    break;
301
  case 13:
302
    temp[12]=0;
303
    strcat(temp,"+0000");
304
    break;
305
  case 15:
306
    temp[15]=0;
307
    memmove(temp+12,temp+10,6);
308
    temp[10]=temp[11]='0';
309
    break;
310
  case 17:
311
    temp[17]=0;
312
    break;
313
  default:
314
    return;
315
  }
316
  strcpy(str,temp);
317
}
318
*/
319
320
static void
321
encode_val (uint64_t val, unsigned char *der, int max_len, int *der_len)
322
1.10M
{
323
1.10M
  int first, k;
324
1.10M
  unsigned char bit7;
325
326
1.10M
  first = 0;
327
11.0M
  for (k = sizeof (val); k >= 0; k--)
328
9.98M
    {
329
9.98M
      bit7 = (val >> (k * 7)) & 0x7F;
330
9.98M
      if (bit7 || first || !k)
331
1.34M
  {
332
1.34M
    if (k)
333
233k
      bit7 |= 0x80;
334
1.34M
    if (max_len > (*der_len))
335
671k
      der[*der_len] = bit7;
336
1.34M
    (*der_len)++;
337
1.34M
    first = 1;
338
1.34M
  }
339
9.98M
    }
340
1.10M
}
341
342
/******************************************************/
343
/* Function : _asn1_object_id_der                     */
344
/* Description: creates the DER coding for an         */
345
/* OBJECT IDENTIFIER  type (length included).         */
346
/* Parameters:                                        */
347
/*   str: OBJECT IDENTIFIER null-terminated string.   */
348
/*   der: string returned.                            */
349
/*   der_len: number of meaningful bytes of DER       */
350
/*            (der[0]..der[ans_len-1]). Initially it  */
351
/*            must store the length of DER.           */
352
/* Return:                                            */
353
/*   ASN1_MEM_ERROR when DER isn't big enough         */
354
/*   ASN1_SUCCESS if successful                       */
355
/*   or an error value.                               */
356
/******************************************************/
357
static int
358
_asn1_object_id_der (const char *str, unsigned char *der, int *der_len)
359
190k
{
360
190k
  int len_len, counter, max_len;
361
190k
  char *temp, *n_end, *n_start;
362
190k
  uint64_t val, val1 = 0;
363
190k
  int str_len = _asn1_strlen (str);
364
365
190k
  max_len = *der_len;
366
190k
  *der_len = 0;
367
368
190k
  if (der == NULL && max_len > 0)
369
0
    return ASN1_VALUE_NOT_VALID;
370
371
190k
  temp = malloc (str_len + 2);
372
190k
  if (temp == NULL)
373
0
    return ASN1_MEM_ALLOC_ERROR;
374
375
190k
  memcpy (temp, str, str_len);
376
190k
  temp[str_len] = '.';
377
190k
  temp[str_len + 1] = 0;
378
379
190k
  counter = 0;
380
190k
  n_start = temp;
381
1.49M
  while ((n_end = strchr (n_start, '.')))
382
1.30M
    {
383
1.30M
      *n_end = 0;
384
1.30M
      val = _asn1_strtou64 (n_start, NULL, 10);
385
1.30M
      counter++;
386
387
1.30M
      if (counter == 1)
388
190k
  {
389
190k
    val1 = val;
390
190k
  }
391
1.10M
      else if (counter == 2)
392
190k
  {
393
190k
    uint64_t val0;
394
395
190k
    if (val1 > 2)
396
0
      {
397
0
        free (temp);
398
0
        return ASN1_VALUE_NOT_VALID;
399
0
      }
400
190k
    else if ((val1 == 0 || val1 == 1) && val > 39)
401
0
      {
402
0
        free (temp);
403
0
        return ASN1_VALUE_NOT_VALID;
404
0
      }
405
406
190k
    val0 = 40 * val1 + val;
407
190k
    encode_val (val0, der, max_len, der_len);
408
190k
  }
409
919k
      else
410
919k
  {
411
919k
    encode_val (val, der, max_len, der_len);
412
919k
  }
413
1.30M
      n_start = n_end + 1;
414
1.30M
    }
415
416
190k
  asn1_length_der (*der_len, NULL, &len_len);
417
190k
  if (max_len >= (*der_len + len_len))
418
95.3k
    {
419
95.3k
      memmove (der + len_len, der, *der_len);
420
95.3k
      asn1_length_der (*der_len, der, &len_len);
421
95.3k
    }
422
190k
  *der_len += len_len;
423
424
190k
  free (temp);
425
426
190k
  if (max_len < (*der_len))
427
95.3k
    return ASN1_MEM_ERROR;
428
429
95.3k
  return ASN1_SUCCESS;
430
190k
}
431
432
/**
433
 * asn1_object_id_der:
434
 * @str: An object identifier in numeric, dot format.
435
 * @der: buffer to hold the returned encoding (may be %NULL).
436
 * @der_len: initially the size of @der; will hold the final size.
437
 * @flags: must be zero
438
 *
439
 * Creates the DER encoding of the provided object identifier.
440
 *
441
 * Returns: %ASN1_SUCCESS if DER encoding was OK, %ASN1_VALUE_NOT_VALID
442
 *   if @str is not a valid OID, %ASN1_MEM_ERROR if the @der
443
 *   vector isn't big enough and in this case @der_len will contain the
444
 *   length needed.
445
 **/
446
int
447
asn1_object_id_der (const char *str, unsigned char *der, int *der_len,
448
        unsigned flags)
449
0
{
450
0
  unsigned char tag_der[MAX_TAG_LEN];
451
0
  int tag_len = 0, r;
452
0
  int max_len = *der_len;
453
454
0
  *der_len = 0;
455
456
0
  _asn1_tag_der (ETYPE_CLASS (ASN1_ETYPE_OBJECT_ID),
457
0
     ETYPE_TAG (ASN1_ETYPE_OBJECT_ID), tag_der, &tag_len);
458
459
0
  if (max_len > tag_len)
460
0
    {
461
0
      memcpy (der, tag_der, tag_len);
462
0
    }
463
0
  max_len -= tag_len;
464
0
  der += tag_len;
465
466
0
  r = _asn1_object_id_der (str, der, &max_len);
467
0
  if (r == ASN1_MEM_ERROR || r == ASN1_SUCCESS)
468
0
    {
469
0
      *der_len = max_len + tag_len;
470
0
    }
471
472
0
  return r;
473
0
}
474
475
static const unsigned char bit_mask[] =
476
  { 0xFF, 0xFE, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x80 };
477
478
/**
479
 * asn1_bit_der:
480
 * @str: BIT string.
481
 * @bit_len: number of meaningful bits in STR.
482
 * @der: string returned.
483
 * @der_len: number of meaningful bytes of DER
484
 *   (der[0]..der[ans_len-1]).
485
 *
486
 * Creates a length-value DER encoding for the input data
487
 * as it would have been for a BIT STRING.
488
 * The DER encoded data will be copied in @der.
489
 *
490
 * Note that the BIT STRING tag is not included in the output.
491
 *
492
 * This function does not return any value because it is expected
493
 * that @der_len will contain enough bytes to store the string
494
 * plus the DER encoding. The DER encoding size can be obtained using
495
 * asn1_length_der().
496
 **/
497
void
498
asn1_bit_der (const unsigned char *str, int bit_len,
499
        unsigned char *der, int *der_len)
500
3.14k
{
501
3.14k
  int len_len, len_byte, len_pad;
502
503
3.14k
  if (der == NULL)
504
0
    return;
505
506
3.14k
  len_byte = bit_len >> 3;
507
3.14k
  len_pad = 8 - (bit_len & 7);
508
3.14k
  if (len_pad == 8)
509
3.14k
    len_pad = 0;
510
0
  else
511
0
    len_byte++;
512
3.14k
  asn1_length_der (len_byte + 1, der, &len_len);
513
3.14k
  der[len_len] = len_pad;
514
515
3.14k
  if (str)
516
3.14k
    memcpy (der + len_len + 1, str, len_byte);
517
3.14k
  der[len_len + len_byte] &= bit_mask[len_pad];
518
3.14k
  *der_len = len_byte + len_len + 1;
519
3.14k
}
520
521
522
/******************************************************/
523
/* Function : _asn1_complete_explicit_tag             */
524
/* Description: add the length coding to the EXPLICIT */
525
/* tags.                                              */
526
/* Parameters:                                        */
527
/*   node: pointer to the tree element.               */
528
/*   der: string with the DER coding of the whole tree*/
529
/*   counter: number of meaningful bytes of DER       */
530
/*            (der[0]..der[*counter-1]).              */
531
/*   max_len: size of der vector                      */
532
/* Return:                                            */
533
/*   ASN1_MEM_ERROR if der vector isn't big enough,   */
534
/*   otherwise ASN1_SUCCESS.                          */
535
/******************************************************/
536
static int
537
_asn1_complete_explicit_tag (asn1_node node, unsigned char *der,
538
           int *counter, int *max_len)
539
1.03M
{
540
1.03M
  asn1_node p;
541
1.03M
  int is_tag_implicit, len2, len3;
542
1.03M
  unsigned char temp[SIZEOF_UNSIGNED_INT];
543
544
1.03M
  if (der == NULL && *max_len > 0)
545
0
    return ASN1_VALUE_NOT_VALID;
546
547
1.03M
  is_tag_implicit = 0;
548
549
1.03M
  if (node->type & CONST_TAG)
550
18.1k
    {
551
18.1k
      p = node->down;
552
18.1k
      if (p == NULL)
553
0
  return ASN1_DER_ERROR;
554
      /* When there are nested tags we must complete them reverse to
555
         the order they were created. This is because completing a tag
556
         modifies all data within it, including the incomplete tags
557
         which store buffer positions -- simon@josefsson.org 2002-09-06
558
       */
559
79.4k
      while (p->right)
560
61.3k
  p = p->right;
561
97.5k
      while (p && p != node->down->left)
562
79.4k
  {
563
79.4k
    if (type_field (p->type) == ASN1_ETYPE_TAG)
564
18.1k
      {
565
18.1k
        if (p->type & CONST_EXPLICIT)
566
16.9k
    {
567
16.9k
      len2 = strtol (p->name, NULL, 10);
568
16.9k
      _asn1_set_name (p, NULL);
569
570
16.9k
      asn1_length_der (*counter - len2, temp, &len3);
571
16.9k
      if (len3 <= (*max_len))
572
8.46k
        {
573
8.46k
          memmove (der + len2 + len3, der + len2,
574
8.46k
             *counter - len2);
575
8.46k
          memcpy (der + len2, temp, len3);
576
8.46k
        }
577
16.9k
      *max_len -= len3;
578
16.9k
      *counter += len3;
579
16.9k
      is_tag_implicit = 0;
580
16.9k
    }
581
1.21k
        else
582
1.21k
    {   /* CONST_IMPLICIT */
583
1.21k
      if (!is_tag_implicit)
584
1.21k
        {
585
1.21k
          is_tag_implicit = 1;
586
1.21k
        }
587
1.21k
    }
588
18.1k
      }
589
79.4k
    p = p->left;
590
79.4k
  }
591
18.1k
    }
592
593
1.03M
  if (*max_len < 0)
594
516k
    return ASN1_MEM_ERROR;
595
596
516k
  return ASN1_SUCCESS;
597
1.03M
}
598
599
const tag_and_class_st _asn1_tags[] = {
600
  [ASN1_ETYPE_GENERALSTRING] =
601
    {ASN1_TAG_GENERALSTRING, ASN1_CLASS_UNIVERSAL, "type:GENERALSTRING"},
602
  [ASN1_ETYPE_NUMERIC_STRING] =
603
    {ASN1_TAG_NUMERIC_STRING, ASN1_CLASS_UNIVERSAL, "type:NUMERIC_STR"},
604
  [ASN1_ETYPE_IA5_STRING] =
605
    {ASN1_TAG_IA5_STRING, ASN1_CLASS_UNIVERSAL, "type:IA5_STR"},
606
  [ASN1_ETYPE_TELETEX_STRING] =
607
    {ASN1_TAG_TELETEX_STRING, ASN1_CLASS_UNIVERSAL, "type:TELETEX_STR"},
608
  [ASN1_ETYPE_PRINTABLE_STRING] =
609
    {ASN1_TAG_PRINTABLE_STRING, ASN1_CLASS_UNIVERSAL, "type:PRINTABLE_STR"},
610
  [ASN1_ETYPE_UNIVERSAL_STRING] =
611
    {ASN1_TAG_UNIVERSAL_STRING, ASN1_CLASS_UNIVERSAL, "type:UNIVERSAL_STR"},
612
  [ASN1_ETYPE_BMP_STRING] =
613
    {ASN1_TAG_BMP_STRING, ASN1_CLASS_UNIVERSAL, "type:BMP_STR"},
614
  [ASN1_ETYPE_UTF8_STRING] =
615
    {ASN1_TAG_UTF8_STRING, ASN1_CLASS_UNIVERSAL, "type:UTF8_STR"},
616
  [ASN1_ETYPE_VISIBLE_STRING] =
617
    {ASN1_TAG_VISIBLE_STRING, ASN1_CLASS_UNIVERSAL, "type:VISIBLE_STR"},
618
  [ASN1_ETYPE_OCTET_STRING] =
619
    {ASN1_TAG_OCTET_STRING, ASN1_CLASS_UNIVERSAL, "type:OCT_STR"},
620
  [ASN1_ETYPE_BIT_STRING] =
621
    {ASN1_TAG_BIT_STRING, ASN1_CLASS_UNIVERSAL, "type:BIT_STR"},
622
  [ASN1_ETYPE_OBJECT_ID] =
623
    {ASN1_TAG_OBJECT_ID, ASN1_CLASS_UNIVERSAL, "type:OBJ_ID"},
624
  [ASN1_ETYPE_NULL] = {ASN1_TAG_NULL, ASN1_CLASS_UNIVERSAL, "type:NULL"},
625
  [ASN1_ETYPE_BOOLEAN] =
626
    {ASN1_TAG_BOOLEAN, ASN1_CLASS_UNIVERSAL, "type:BOOLEAN"},
627
  [ASN1_ETYPE_INTEGER] =
628
    {ASN1_TAG_INTEGER, ASN1_CLASS_UNIVERSAL, "type:INTEGER"},
629
  [ASN1_ETYPE_ENUMERATED] =
630
    {ASN1_TAG_ENUMERATED, ASN1_CLASS_UNIVERSAL, "type:ENUMERATED"},
631
  [ASN1_ETYPE_SEQUENCE] =
632
    {ASN1_TAG_SEQUENCE, ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED,
633
     "type:SEQUENCE"},
634
  [ASN1_ETYPE_SEQUENCE_OF] =
635
    {ASN1_TAG_SEQUENCE, ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED,
636
     "type:SEQ_OF"},
637
  [ASN1_ETYPE_SET] =
638
    {ASN1_TAG_SET, ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED, "type:SET"},
639
  [ASN1_ETYPE_SET_OF] =
640
    {ASN1_TAG_SET, ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED,
641
     "type:SET_OF"},
642
  [ASN1_ETYPE_GENERALIZED_TIME] =
643
    {ASN1_TAG_GENERALIZEDTime, ASN1_CLASS_UNIVERSAL, "type:GENERALIZED_TIME"},
644
  [ASN1_ETYPE_UTC_TIME] =
645
    {ASN1_TAG_UTCTime, ASN1_CLASS_UNIVERSAL, "type:UTC_TIME"},
646
};
647
648
unsigned int _asn1_tags_size = sizeof (_asn1_tags) / sizeof (_asn1_tags[0]);
649
650
/******************************************************/
651
/* Function : _asn1_insert_tag_der                    */
652
/* Description: creates the DER coding of tags of one */
653
/* NODE.                                              */
654
/* Parameters:                                        */
655
/*   node: pointer to the tree element.               */
656
/*   der: string returned                             */
657
/*   counter: number of meaningful bytes of DER       */
658
/*            (counter[0]..der[*counter-1]).          */
659
/*   max_len: size of der vector                      */
660
/* Return:                                            */
661
/*   ASN1_GENERIC_ERROR if the type is unknown,       */
662
/*   ASN1_MEM_ERROR if der vector isn't big enough,   */
663
/*   otherwise ASN1_SUCCESS.                          */
664
/******************************************************/
665
static int
666
_asn1_insert_tag_der (asn1_node node, unsigned char *der, int *counter,
667
          int *max_len)
668
1.09M
{
669
1.09M
  asn1_node p;
670
1.09M
  int tag_len, is_tag_implicit;
671
1.09M
  unsigned char class, class_implicit =
672
1.09M
    0, temp[MAX (SIZEOF_UNSIGNED_INT * 3 + 1, LTOSTR_MAX_SIZE)];
673
1.09M
  unsigned long tag_implicit = 0;
674
1.09M
  unsigned char tag_der[MAX_TAG_LEN];
675
676
1.09M
  is_tag_implicit = 0;
677
678
1.09M
  if (node->type & CONST_TAG)
679
18.1k
    {
680
18.1k
      p = node->down;
681
97.6k
      while (p)
682
79.4k
  {
683
79.4k
    if (type_field (p->type) == ASN1_ETYPE_TAG)
684
18.1k
      {
685
18.1k
        if (p->type & CONST_APPLICATION)
686
0
    class = ASN1_CLASS_APPLICATION;
687
18.1k
        else if (p->type & CONST_UNIVERSAL)
688
0
    class = ASN1_CLASS_UNIVERSAL;
689
18.1k
        else if (p->type & CONST_PRIVATE)
690
0
    class = ASN1_CLASS_PRIVATE;
691
18.1k
        else
692
18.1k
    class = ASN1_CLASS_CONTEXT_SPECIFIC;
693
694
18.1k
        if (p->type & CONST_EXPLICIT)
695
16.9k
    {
696
16.9k
      if (is_tag_implicit)
697
0
        _asn1_tag_der (class_implicit, tag_implicit, tag_der,
698
0
           &tag_len);
699
16.9k
      else
700
16.9k
        _asn1_tag_der (class | ASN1_CLASS_STRUCTURED,
701
16.9k
           _asn1_strtoul (p->value, NULL, 10),
702
16.9k
           tag_der, &tag_len);
703
704
16.9k
      *max_len -= tag_len;
705
16.9k
      if (der && *max_len >= 0)
706
8.47k
        memcpy (der + *counter, tag_der, tag_len);
707
16.9k
      *counter += tag_len;
708
709
16.9k
      _asn1_ltostr (*counter, (char *) temp);
710
16.9k
      _asn1_set_name (p, (const char *) temp);
711
712
16.9k
      is_tag_implicit = 0;
713
16.9k
    }
714
1.21k
        else
715
1.21k
    {   /* CONST_IMPLICIT */
716
1.21k
      if (!is_tag_implicit)
717
1.21k
        {
718
1.21k
          if ((type_field (node->type) == ASN1_ETYPE_SEQUENCE) ||
719
1.21k
        (type_field (node->type) == ASN1_ETYPE_SEQUENCE_OF)
720
1.21k
        || (type_field (node->type) == ASN1_ETYPE_SET)
721
1.21k
        || (type_field (node->type) == ASN1_ETYPE_SET_OF))
722
0
      class |= ASN1_CLASS_STRUCTURED;
723
1.21k
          class_implicit = class;
724
1.21k
          tag_implicit = _asn1_strtoul (p->value, NULL, 10);
725
1.21k
          is_tag_implicit = 1;
726
1.21k
        }
727
1.21k
    }
728
18.1k
      }
729
79.4k
    p = p->right;
730
79.4k
  }
731
18.1k
    }
732
733
1.09M
  if (is_tag_implicit)
734
1.21k
    {
735
1.21k
      _asn1_tag_der (class_implicit, tag_implicit, tag_der, &tag_len);
736
1.21k
    }
737
1.09M
  else
738
1.09M
    {
739
1.09M
      unsigned type = type_field (node->type);
740
1.09M
      switch (type)
741
1.09M
  {
742
918k
  CASE_HANDLED_ETYPES:
743
918k
    _asn1_tag_der (_asn1_tags[type].class, _asn1_tags[type].tag,
744
918k
       tag_der, &tag_len);
745
918k
    break;
746
0
  case ASN1_ETYPE_TAG:
747
34.8k
  case ASN1_ETYPE_CHOICE:
748
179k
  case ASN1_ETYPE_ANY:
749
179k
    tag_len = 0;
750
179k
    break;
751
0
  default:
752
0
    return ASN1_GENERIC_ERROR;
753
1.09M
  }
754
1.09M
    }
755
756
1.09M
  *max_len -= tag_len;
757
1.09M
  if (der && *max_len >= 0)
758
549k
    memcpy (der + *counter, tag_der, tag_len);
759
1.09M
  *counter += tag_len;
760
761
1.09M
  if (*max_len < 0)
762
549k
    return ASN1_MEM_ERROR;
763
764
549k
  return ASN1_SUCCESS;
765
1.09M
}
766
767
/******************************************************/
768
/* Function : _asn1_ordering_set                      */
769
/* Description: puts the elements of a SET type in    */
770
/* the correct order according to DER rules.          */
771
/* Parameters:                                        */
772
/*   der: string with the DER coding.                 */
773
/*   node: pointer to the SET element.                */
774
/* Return:                                            */
775
/*    ASN1_SUCCESS if successful                      */
776
/*    or an error value.                              */
777
/******************************************************/
778
static int
779
_asn1_ordering_set (unsigned char *der, int der_len, asn1_node node)
780
0
{
781
0
  struct vet
782
0
  {
783
0
    int end;
784
0
    unsigned long value;
785
0
    struct vet *next, *prev;
786
0
  };
787
788
0
  int counter, len, len2;
789
0
  struct vet *first, *last, *p_vet, *p2_vet;
790
0
  asn1_node p;
791
0
  unsigned char class, *temp;
792
0
  unsigned long tag, t;
793
0
  int err;
794
795
0
  counter = 0;
796
797
0
  if (type_field (node->type) != ASN1_ETYPE_SET)
798
0
    return ASN1_VALUE_NOT_VALID;
799
800
0
  p = node->down;
801
0
  while (p && ((type_field (p->type) == ASN1_ETYPE_TAG) ||
802
0
         (type_field (p->type) == ASN1_ETYPE_SIZE)))
803
0
    p = p->right;
804
805
0
  if ((p == NULL) || (p->right == NULL))
806
0
    return ASN1_SUCCESS;
807
808
0
  first = last = NULL;
809
0
  while (p)
810
0
    {
811
0
      p_vet = malloc (sizeof (struct vet));
812
0
      if (p_vet == NULL)
813
0
  {
814
0
    err = ASN1_MEM_ALLOC_ERROR;
815
0
    goto error;
816
0
  }
817
818
0
      p_vet->next = NULL;
819
0
      p_vet->prev = last;
820
0
      if (first == NULL)
821
0
  first = p_vet;
822
0
      else
823
0
  last->next = p_vet;
824
0
      last = p_vet;
825
826
      /* tag value calculation */
827
0
      err = asn1_get_tag_der (der + counter, der_len - counter, &class, &len2,
828
0
            &tag);
829
0
      if (err != ASN1_SUCCESS)
830
0
  goto error;
831
832
0
      t = ((unsigned int) class) << 24;
833
0
      p_vet->value = t | tag;
834
0
      counter += len2;
835
836
      /* extraction and length */
837
0
      len2 = asn1_get_length_der (der + counter, der_len - counter, &len);
838
0
      if (len2 < 0)
839
0
  {
840
0
    err = ASN1_DER_ERROR;
841
0
    goto error;
842
0
  }
843
0
      counter += len + len2;
844
845
0
      p_vet->end = counter;
846
0
      p = p->right;
847
0
    }
848
849
0
  p_vet = first;
850
851
0
  while (p_vet)
852
0
    {
853
0
      p2_vet = p_vet->next;
854
0
      counter = 0;
855
0
      while (p2_vet)
856
0
  {
857
0
    if (p_vet->value > p2_vet->value)
858
0
      {
859
        /* change position */
860
0
        temp = malloc (p_vet->end - counter);
861
0
        if (temp == NULL)
862
0
    {
863
0
      err = ASN1_MEM_ALLOC_ERROR;
864
0
      goto error;
865
0
    }
866
867
0
        memcpy (temp, der + counter, p_vet->end - counter);
868
0
        memcpy (der + counter, der + p_vet->end,
869
0
          p2_vet->end - p_vet->end);
870
0
        memcpy (der + counter + p2_vet->end - p_vet->end, temp,
871
0
          p_vet->end - counter);
872
0
        free (temp);
873
874
0
        tag = p_vet->value;
875
0
        p_vet->value = p2_vet->value;
876
0
        p2_vet->value = tag;
877
878
0
        p_vet->end = counter + (p2_vet->end - p_vet->end);
879
0
      }
880
0
    counter = p_vet->end;
881
882
0
    p2_vet = p2_vet->next;
883
0
    p_vet = p_vet->next;
884
0
  }
885
886
0
      if (p_vet != first)
887
0
  p_vet->prev->next = NULL;
888
0
      else
889
0
  first = NULL;
890
0
      free (p_vet);
891
0
      p_vet = first;
892
0
    }
893
0
  return ASN1_SUCCESS;
894
895
0
error:
896
0
  while (first != NULL)
897
0
    {
898
0
      p_vet = first;
899
0
      first = first->next;
900
0
      free (p_vet);
901
0
    }
902
0
  return err;
903
0
}
904
905
struct vet
906
{
907
  unsigned char *ptr;
908
  int size;
909
};
910
911
static int
912
setof_compar (const void *_e1, const void *_e2)
913
2.01k
{
914
2.01k
  unsigned length;
915
2.01k
  const struct vet *e1 = _e1, *e2 = _e2;
916
2.01k
  int rval;
917
918
  /* The encodings of the component values of a set-of value shall
919
   * appear in ascending order, the encodings being compared
920
   * as octet strings with the shorter components being
921
   * padded at their trailing end with 0-octets.
922
   * The padding octets are for comparison purposes and
923
   * do not appear in the encodings.
924
   */
925
2.01k
  length = MIN (e1->size, e2->size);
926
927
2.01k
  rval = memcmp (e1->ptr, e2->ptr, length);
928
2.01k
  if (rval == 0 && e1->size != e2->size)
929
0
    {
930
0
      if (e1->size > e2->size)
931
0
  rval = 1;
932
0
      else if (e2->size > e1->size)
933
0
  rval = -1;
934
0
    }
935
936
2.01k
  return rval;
937
2.01k
}
938
939
/******************************************************/
940
/* Function : _asn1_ordering_set_of                   */
941
/* Description: puts the elements of a SET OF type in */
942
/* the correct order according to DER rules.          */
943
/* Parameters:                                        */
944
/*   der: string with the DER coding.                 */
945
/*   node: pointer to the SET OF element.             */
946
/* Return:                                            */
947
/*    ASN1_SUCCESS if successful                      */
948
/*    or an error value.                              */
949
/******************************************************/
950
static int
951
_asn1_ordering_set_of (unsigned char *der, int der_len, asn1_node node)
952
13.8k
{
953
13.8k
  int counter, len, len2;
954
13.8k
  struct vet *list = NULL, *tlist;
955
13.8k
  unsigned list_size = 0;
956
13.8k
  struct vet *p_vet;
957
13.8k
  asn1_node p;
958
13.8k
  unsigned char class;
959
13.8k
  unsigned i;
960
13.8k
  unsigned char *out = NULL;
961
13.8k
  int err;
962
963
13.8k
  counter = 0;
964
965
13.8k
  if (type_field (node->type) != ASN1_ETYPE_SET_OF)
966
0
    return ASN1_VALUE_NOT_VALID;
967
968
13.8k
  p = node->down;
969
13.8k
  while (p && ((type_field (p->type) == ASN1_ETYPE_TAG) ||
970
13.8k
         (type_field (p->type) == ASN1_ETYPE_SIZE)))
971
0
    p = p->right;
972
13.8k
  if (p == NULL)
973
0
    return ASN1_VALUE_NOT_VALID;
974
13.8k
  p = p->right;
975
976
13.8k
  if ((p == NULL) || (p->right == NULL))
977
13.6k
    return ASN1_SUCCESS;
978
979
1.64k
  while (p)
980
1.38k
    {
981
1.38k
      list_size++;
982
1.38k
      tlist = realloc (list, list_size * sizeof (struct vet));
983
1.38k
      if (tlist == NULL)
984
0
  {
985
0
    err = ASN1_MEM_ALLOC_ERROR;
986
0
    goto error;
987
0
  }
988
1.38k
      list = tlist;
989
1.38k
      p_vet = &list[list_size - 1];
990
991
1.38k
      p_vet->ptr = der + counter;
992
1.38k
      p_vet->size = 0;
993
994
      /* extraction of tag and length */
995
1.38k
      if (der_len - counter > 0)
996
1.38k
  {
997
1.38k
    err = asn1_get_tag_der (der + counter, der_len - counter, &class,
998
1.38k
          &len, NULL);
999
1.38k
    if (err != ASN1_SUCCESS)
1000
0
      goto error;
1001
1.38k
    counter += len;
1002
1.38k
    p_vet->size += len;
1003
1004
1.38k
    len2 = asn1_get_length_der (der + counter, der_len - counter, &len);
1005
1.38k
    if (len2 < 0)
1006
0
      {
1007
0
        err = ASN1_DER_ERROR;
1008
0
        goto error;
1009
0
      }
1010
1.38k
    counter += len + len2;
1011
1.38k
    p_vet->size += len + len2;
1012
1013
1.38k
  }
1014
0
      else
1015
0
  {
1016
0
    err = ASN1_DER_ERROR;
1017
0
    goto error;
1018
0
  }
1019
1.38k
      p = p->right;
1020
1.38k
    }
1021
1022
252
  if (counter > der_len)
1023
0
    {
1024
0
      err = ASN1_DER_ERROR;
1025
0
      goto error;
1026
0
    }
1027
1028
252
  qsort (list, list_size, sizeof (struct vet), setof_compar);
1029
1030
252
  out = malloc (der_len);
1031
252
  if (out == NULL)
1032
0
    {
1033
0
      err = ASN1_MEM_ERROR;
1034
0
      goto error;
1035
0
    }
1036
1037
  /* the sum of p_vet->size == der_len */
1038
252
  counter = 0;
1039
1.64k
  for (i = 0; i < list_size; i++)
1040
1.38k
    {
1041
1.38k
      p_vet = &list[i];
1042
1.38k
      memcpy (out + counter, p_vet->ptr, p_vet->size);
1043
1.38k
      counter += p_vet->size;
1044
1.38k
    }
1045
252
  memcpy (der, out, der_len);
1046
252
  free (out);
1047
1048
252
  err = ASN1_SUCCESS;
1049
1050
252
error:
1051
252
  free (list);
1052
252
  return err;
1053
252
}
1054
1055
/**
1056
 * asn1_der_coding:
1057
 * @element: pointer to an ASN1 element
1058
 * @name: the name of the structure you want to encode (it must be
1059
 *   inside *POINTER).
1060
 * @ider: vector that will contain the DER encoding. DER must be a
1061
 *   pointer to memory cells already allocated.
1062
 * @len: number of bytes of *@ider: @ider[0]..@ider[len-1], Initially
1063
 *   holds the sizeof of der vector.
1064
 * @ErrorDescription: return the error description or an empty
1065
 *   string if success.
1066
 *
1067
 * Creates the DER encoding for the NAME structure (inside *POINTER
1068
 * structure).
1069
 *
1070
 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
1071
 *   if @name is not a valid element, %ASN1_VALUE_NOT_FOUND if there
1072
 *   is an element without a value, %ASN1_MEM_ERROR if the @ider
1073
 *   vector isn't big enough and in this case @len will contain the
1074
 *   length needed.
1075
 **/
1076
int
1077
asn1_der_coding (asn1_node_const element, const char *name, void *ider,
1078
     int *len, char *ErrorDescription)
1079
137k
{
1080
137k
  asn1_node node, p, p2;
1081
137k
  unsigned char temp[MAX (LTOSTR_MAX_SIZE, SIZEOF_UNSIGNED_LONG_INT * 3 + 1)];
1082
137k
  int counter, counter_old, len2, len3, move, max_len, max_len_old;
1083
137k
  int err;
1084
137k
  unsigned char *der = ider;
1085
137k
  unsigned char dummy;
1086
1087
137k
  if (ErrorDescription)
1088
0
    ErrorDescription[0] = 0;
1089
1090
137k
  node = asn1_find_node (element, name);
1091
137k
  if (node == NULL)
1092
1.61k
    return ASN1_ELEMENT_NOT_FOUND;
1093
1094
  /* Node is now a locally allocated variable.
1095
   * That is because in some point we modify the
1096
   * structure, and I don't know why! --nmav
1097
   */
1098
136k
  node = _asn1_copy_structure3 (node);
1099
136k
  if (node == NULL)
1100
0
    return ASN1_ELEMENT_NOT_FOUND;
1101
1102
136k
  max_len = *len;
1103
1104
136k
  if (der == NULL && max_len > 0)
1105
0
    {
1106
0
      err = ASN1_VALUE_NOT_VALID;
1107
0
      goto error;
1108
0
    }
1109
1110
136k
  counter = 0;
1111
136k
  move = DOWN;
1112
136k
  p = node;
1113
1114
1.54M
  while (1)
1115
1.54M
    {
1116
1117
1.54M
      counter_old = counter;
1118
1.54M
      max_len_old = max_len;
1119
1.54M
      if (move != UP)
1120
1.09M
  {
1121
1.09M
    p->start = counter;
1122
1.09M
    err = _asn1_insert_tag_der (p, der, &counter, &max_len);
1123
1.09M
    if (err != ASN1_SUCCESS && err != ASN1_MEM_ERROR)
1124
0
      goto error;
1125
1.09M
  }
1126
1.54M
      switch (type_field (p->type))
1127
1.54M
  {
1128
0
  case ASN1_ETYPE_NULL:
1129
0
    max_len--;
1130
0
    if (der != NULL && max_len >= 0)
1131
0
      der[counter] = 0;
1132
0
    counter++;
1133
0
    move = RIGHT;
1134
0
    break;
1135
39.9k
  case ASN1_ETYPE_BOOLEAN:
1136
39.9k
    if ((p->type & CONST_DEFAULT) && (p->value == NULL))
1137
31.6k
      {
1138
31.6k
        counter = counter_old;
1139
31.6k
        max_len = max_len_old;
1140
31.6k
      }
1141
8.30k
    else
1142
8.30k
      {
1143
8.30k
        if (p->value == NULL)
1144
0
    {
1145
0
      _asn1_error_description_value_not_found (p,
1146
0
                 ErrorDescription);
1147
0
      err = ASN1_VALUE_NOT_FOUND;
1148
0
      goto error;
1149
0
    }
1150
8.30k
        max_len -= 2;
1151
8.30k
        if (der != NULL && max_len >= 0)
1152
4.15k
    {
1153
4.15k
      der[counter++] = 1;
1154
4.15k
      if (p->value[0] == 'F')
1155
367
        der[counter++] = 0;
1156
3.78k
      else
1157
3.78k
        der[counter++] = 0xFF;
1158
4.15k
    }
1159
4.15k
        else
1160
4.15k
    counter += 2;
1161
8.30k
      }
1162
39.9k
    move = RIGHT;
1163
39.9k
    break;
1164
104k
  case ASN1_ETYPE_INTEGER:
1165
104k
  case ASN1_ETYPE_ENUMERATED:
1166
104k
    if ((p->type & CONST_DEFAULT) && (p->value == NULL))
1167
6
      {
1168
6
        counter = counter_old;
1169
6
        max_len = max_len_old;
1170
6
      }
1171
104k
    else
1172
104k
      {
1173
104k
        if (p->value == NULL)
1174
0
    {
1175
0
      _asn1_error_description_value_not_found (p,
1176
0
                 ErrorDescription);
1177
0
      err = ASN1_VALUE_NOT_FOUND;
1178
0
      goto error;
1179
0
    }
1180
104k
        len2 = asn1_get_length_der (p->value, p->value_len, &len3);
1181
104k
        if (len2 < 0)
1182
0
    {
1183
0
      err = ASN1_DER_ERROR;
1184
0
      goto error;
1185
0
    }
1186
104k
        max_len -= len2 + len3;
1187
104k
        if (der != NULL && max_len >= 0)
1188
52.1k
    memcpy (der + counter, p->value, len3 + len2);
1189
104k
        counter += len3 + len2;
1190
104k
      }
1191
104k
    move = RIGHT;
1192
104k
    break;
1193
190k
  case ASN1_ETYPE_OBJECT_ID:
1194
190k
    if ((p->type & CONST_DEFAULT) && (p->value == NULL))
1195
0
      {
1196
0
        counter = counter_old;
1197
0
        max_len = max_len_old;
1198
0
      }
1199
190k
    else
1200
190k
      {
1201
190k
        if (p->value == NULL)
1202
0
    {
1203
0
      _asn1_error_description_value_not_found (p,
1204
0
                 ErrorDescription);
1205
0
      err = ASN1_VALUE_NOT_FOUND;
1206
0
      goto error;
1207
0
    }
1208
190k
        len2 = max_len;
1209
190k
        err =
1210
190k
    _asn1_object_id_der ((char *) p->value,
1211
190k
             der ? der + counter : &dummy, &len2);
1212
190k
        if (err != ASN1_SUCCESS && err != ASN1_MEM_ERROR)
1213
0
    goto error;
1214
1215
190k
        max_len -= len2;
1216
190k
        counter += len2;
1217
190k
      }
1218
190k
    move = RIGHT;
1219
190k
    break;
1220
9.81k
  case ASN1_ETYPE_GENERALIZED_TIME:
1221
17.3k
  case ASN1_ETYPE_UTC_TIME:
1222
17.3k
    if (p->value == NULL)
1223
0
      {
1224
0
        _asn1_error_description_value_not_found (p, ErrorDescription);
1225
0
        err = ASN1_VALUE_NOT_FOUND;
1226
0
        goto error;
1227
0
      }
1228
17.3k
    len2 = max_len;
1229
17.3k
    err =
1230
17.3k
      _asn1_time_der (p->value, p->value_len,
1231
17.3k
          der ? der + counter : &dummy, &len2);
1232
17.3k
    if (err != ASN1_SUCCESS && err != ASN1_MEM_ERROR)
1233
0
      goto error;
1234
1235
17.3k
    max_len -= len2;
1236
17.3k
    counter += len2;
1237
17.3k
    move = RIGHT;
1238
17.3k
    break;
1239
129k
  case ASN1_ETYPE_OCTET_STRING:
1240
129k
  case ASN1_ETYPE_GENERALSTRING:
1241
129k
  case ASN1_ETYPE_NUMERIC_STRING:
1242
129k
  case ASN1_ETYPE_IA5_STRING:
1243
129k
  case ASN1_ETYPE_TELETEX_STRING:
1244
129k
  case ASN1_ETYPE_PRINTABLE_STRING:
1245
129k
  case ASN1_ETYPE_UNIVERSAL_STRING:
1246
129k
  case ASN1_ETYPE_BMP_STRING:
1247
129k
  case ASN1_ETYPE_UTF8_STRING:
1248
129k
  case ASN1_ETYPE_VISIBLE_STRING:
1249
154k
  case ASN1_ETYPE_BIT_STRING:
1250
154k
    if (p->value == NULL)
1251
1
      {
1252
1
        _asn1_error_description_value_not_found (p, ErrorDescription);
1253
1
        err = ASN1_VALUE_NOT_FOUND;
1254
1
        goto error;
1255
1
      }
1256
154k
    len2 = asn1_get_length_der (p->value, p->value_len, &len3);
1257
154k
    if (len2 < 0)
1258
0
      {
1259
0
        err = ASN1_DER_ERROR;
1260
0
        goto error;
1261
0
      }
1262
154k
    max_len -= len2 + len3;
1263
154k
    if (der != NULL && max_len >= 0)
1264
77.2k
      memcpy (der + counter, p->value, len3 + len2);
1265
154k
    counter += len3 + len2;
1266
154k
    move = RIGHT;
1267
154k
    break;
1268
717k
  case ASN1_ETYPE_SEQUENCE:
1269
717k
  case ASN1_ETYPE_SET:
1270
717k
    if (move != UP)
1271
358k
      {
1272
358k
        p->tmp_ival = counter;
1273
358k
        if (p->down == NULL)
1274
7.38k
    {
1275
7.38k
      move = UP;
1276
7.38k
      continue;
1277
7.38k
    }
1278
351k
        else
1279
351k
    {
1280
351k
      p2 = p->down;
1281
351k
      while (p2 && (type_field (p2->type) == ASN1_ETYPE_TAG))
1282
0
        p2 = p2->right;
1283
351k
      if (p2)
1284
351k
        {
1285
351k
          p = p2;
1286
351k
          move = RIGHT;
1287
351k
          continue;
1288
351k
        }
1289
0
      move = UP;
1290
0
      continue;
1291
351k
    }
1292
358k
      }
1293
358k
    else
1294
358k
      {     /* move==UP */
1295
358k
        len2 = p->tmp_ival;
1296
358k
        p->tmp_ival = 0;
1297
358k
        if ((type_field (p->type) == ASN1_ETYPE_SET) && (max_len >= 0))
1298
0
    {
1299
0
      err =
1300
0
        _asn1_ordering_set (der ? der + len2 : &dummy,
1301
0
          counter - len2, p);
1302
0
      if (err != ASN1_SUCCESS)
1303
0
        goto error;
1304
0
    }
1305
358k
        asn1_length_der (counter - len2, temp, &len3);
1306
358k
        max_len -= len3;
1307
358k
        if (der != NULL && max_len >= 0)
1308
179k
    {
1309
179k
      memmove (der + len2 + len3, der + len2, counter - len2);
1310
179k
      memcpy (der + len2, temp, len3);
1311
179k
    }
1312
358k
        counter += len3;
1313
358k
        move = RIGHT;
1314
358k
      }
1315
358k
    break;
1316
358k
  case ASN1_ETYPE_SEQUENCE_OF:
1317
107k
  case ASN1_ETYPE_SET_OF:
1318
107k
    if (move != UP)
1319
54.3k
      {
1320
54.3k
        p->tmp_ival = counter;
1321
54.3k
        p = p->down;
1322
62.5k
        while ((type_field (p->type) == ASN1_ETYPE_TAG)
1323
62.5k
         || (type_field (p->type) == ASN1_ETYPE_SIZE))
1324
8.15k
    p = p->right;
1325
54.3k
        if (p->right)
1326
53.2k
    {
1327
53.2k
      p = p->right;
1328
53.2k
      move = RIGHT;
1329
53.2k
      continue;
1330
53.2k
    }
1331
1.13k
        else
1332
1.13k
    p = _asn1_find_up (p);
1333
1.13k
        move = UP;
1334
1.13k
      }
1335
54.3k
    if (move == UP)
1336
54.3k
      {
1337
54.3k
        len2 = p->tmp_ival;
1338
54.3k
        p->tmp_ival = 0;
1339
54.3k
        if ((type_field (p->type) == ASN1_ETYPE_SET_OF)
1340
54.3k
      && (counter - len2 > 0) && (max_len >= 0))
1341
13.8k
    {
1342
13.8k
      err =
1343
13.8k
        _asn1_ordering_set_of (der ? der + len2 : &dummy,
1344
13.8k
             counter - len2, p);
1345
13.8k
      if (err != ASN1_SUCCESS)
1346
0
        goto error;
1347
13.8k
    }
1348
54.3k
        asn1_length_der (counter - len2, temp, &len3);
1349
54.3k
        max_len -= len3;
1350
54.3k
        if (der != NULL && max_len >= 0)
1351
27.1k
    {
1352
27.1k
      memmove (der + len2 + len3, der + len2, counter - len2);
1353
27.1k
      memcpy (der + len2, temp, len3);
1354
27.1k
    }
1355
54.3k
        counter += len3;
1356
54.3k
        move = RIGHT;
1357
54.3k
      }
1358
54.3k
    break;
1359
144k
  case ASN1_ETYPE_ANY:
1360
144k
    if (p->value == NULL)
1361
0
      {
1362
0
        _asn1_error_description_value_not_found (p, ErrorDescription);
1363
0
        err = ASN1_VALUE_NOT_FOUND;
1364
0
        goto error;
1365
0
      }
1366
144k
    len2 = asn1_get_length_der (p->value, p->value_len, &len3);
1367
144k
    if (len2 < 0)
1368
0
      {
1369
0
        err = ASN1_DER_ERROR;
1370
0
        goto error;
1371
0
      }
1372
144k
    max_len -= len2;
1373
144k
    if (der != NULL && max_len >= 0)
1374
72.2k
      memcpy (der + counter, p->value + len3, len2);
1375
144k
    counter += len2;
1376
144k
    move = RIGHT;
1377
144k
    break;
1378
69.7k
  default:
1379
69.7k
    move = (move == UP) ? RIGHT : DOWN;
1380
69.7k
    break;
1381
1.54M
  }
1382
1383
1.13M
      if ((move != DOWN) && (counter != counter_old))
1384
1.03M
  {
1385
1.03M
    p->end = counter - 1;
1386
1.03M
    err = _asn1_complete_explicit_tag (p, der, &counter, &max_len);
1387
1.03M
    if (err != ASN1_SUCCESS && err != ASN1_MEM_ERROR)
1388
0
      goto error;
1389
1.03M
  }
1390
1391
1.13M
      if (p == node && move != DOWN)
1392
136k
  break;
1393
1394
998k
      if (move == DOWN)
1395
34.8k
  {
1396
34.8k
    if (p->down)
1397
34.8k
      p = p->down;
1398
0
    else
1399
0
      move = RIGHT;
1400
34.8k
  }
1401
998k
      if (move == RIGHT)
1402
963k
  {
1403
963k
    if (p->right)
1404
523k
      p = p->right;
1405
439k
    else
1406
439k
      move = UP;
1407
963k
  }
1408
998k
      if (move == UP)
1409
439k
  p = _asn1_find_up (p);
1410
998k
    }
1411
1412
136k
  *len = counter;
1413
1414
136k
  if (max_len < 0)
1415
68.1k
    {
1416
68.1k
      err = ASN1_MEM_ERROR;
1417
68.1k
      goto error;
1418
68.1k
    }
1419
1420
68.1k
  err = ASN1_SUCCESS;
1421
1422
136k
error:
1423
136k
  asn1_delete_structure (&node);
1424
136k
  return err;
1425
68.1k
}