Coverage Report

Created: 2025-07-11 06:59

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