1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
|
/*
* Copyright (C) 2000-2014 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
* The LIBTASN1 library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
/*****************************************************/
/* File: element.c */
/* Description: Functions with the read and write */
/* functions. */
/*****************************************************/
#include <int.h>
#include "parser_aux.h"
#include <gstr.h>
#include "structure.h"
#include "element.h"
void
_asn1_hierarchical_name (asn1_node node, char *name, int name_size)
{
asn1_node p;
char tmp_name[64];
p = node;
name[0] = 0;
while (p != NULL)
{
if (p->name[0] != 0)
{
_asn1_str_cpy (tmp_name, sizeof (tmp_name), name),
_asn1_str_cpy (name, name_size, p->name);
_asn1_str_cat (name, name_size, ".");
_asn1_str_cat (name, name_size, tmp_name);
}
p = _asn1_find_up (p);
}
if (name[0] == 0)
_asn1_str_cpy (name, name_size, "ROOT");
}
/******************************************************************/
/* Function : _asn1_convert_integer */
/* Description: converts an integer from a null terminated string */
/* to der decoding. The convertion from a null */
/* terminated string to an integer is made with */
/* the 'strtol' function. */
/* Parameters: */
/* value: null terminated string to convert. */
/* value_out: convertion result (memory must be already */
/* allocated). */
/* value_out_size: number of bytes of value_out. */
/* len: number of significant byte of value_out. */
/* Return: ASN1_MEM_ERROR or ASN1_SUCCESS */
/******************************************************************/
int
_asn1_convert_integer (const unsigned char *value, unsigned char *value_out,
int value_out_size, int *len)
{
char negative;
unsigned char val[SIZEOF_UNSIGNED_LONG_INT];
long valtmp;
int k, k2;
valtmp = _asn1_strtol (value, NULL, 10);
for (k = 0; k < SIZEOF_UNSIGNED_LONG_INT; k++)
{
val[SIZEOF_UNSIGNED_LONG_INT - k - 1] = (valtmp >> (8 * k)) & 0xFF;
}
if (val[0] & 0x80)
negative = 1;
else
negative = 0;
for (k = 0; k < SIZEOF_UNSIGNED_LONG_INT - 1; k++)
{
if (negative && (val[k] != 0xFF))
break;
else if (!negative && val[k])
break;
}
if ((negative && !(val[k] & 0x80)) || (!negative && (val[k] & 0x80)))
k--;
*len = SIZEOF_UNSIGNED_LONG_INT - k;
if (SIZEOF_UNSIGNED_LONG_INT - k > value_out_size)
/* VALUE_OUT is too short to contain the value conversion */
return ASN1_MEM_ERROR;
if (value_out != NULL)
{
for (k2 = k; k2 < SIZEOF_UNSIGNED_LONG_INT; k2++)
value_out[k2 - k] = val[k2];
}
#if 0
printf ("_asn1_convert_integer: valueIn=%s, lenOut=%d", value, *len);
for (k = 0; k < SIZEOF_UNSIGNED_LONG_INT; k++)
printf (", vOut[%d]=%d", k, value_out[k]);
printf ("\n");
#endif
return ASN1_SUCCESS;
}
/* Appends a new element into the sequence (or set) defined by this
* node. The new element will have a name of '?number', where number
* is a monotonically increased serial number.
*
* The last element in the list may be provided in @pcache, to avoid
* traversing the list, an expensive operation in long lists.
*
* On success it returns in @pcache the added element (which is the
* tail in the list of added elements).
*/
int
_asn1_append_sequence_set (asn1_node node, struct node_tail_cache_st *pcache)
{
asn1_node p, p2;
char temp[LTOSTR_MAX_SIZE];
long n;
if (!node || !(node->down))
return ASN1_GENERIC_ERROR;
p = node->down;
while ((type_field (p->type) == ASN1_ETYPE_TAG)
|| (type_field (p->type) == ASN1_ETYPE_SIZE))
p = p->right;
p2 = _asn1_copy_structure3 (p);
if (p2 == NULL)
return ASN1_GENERIC_ERROR;
if (pcache == NULL || pcache->tail == NULL || pcache->head != node)
{
while (p->right)
{
p = p->right;
}
}
else
{
p = pcache->tail;
}
_asn1_set_right (p, p2);
if (pcache)
{
pcache->head = node;
pcache->tail = p2;
}
if (p->name[0] == 0)
_asn1_str_cpy (temp, sizeof (temp), "?1");
else
{
n = strtol (p->name + 1, NULL, 0);
n++;
temp[0] = '?';
_asn1_ltostr (n, temp + 1);
}
_asn1_set_name (p2, temp);
/* p2->type |= CONST_OPTION; */
return ASN1_SUCCESS;
}
/**
* asn1_write_value:
* @node_root: pointer to a structure
* @name: the name of the element inside the structure that you want to set.
* @ivalue: vector used to specify the value to set. If len is >0,
* VALUE must be a two's complement form integer. if len=0 *VALUE
* must be a null terminated string with an integer value.
* @len: number of bytes of *value to use to set the value:
* value[0]..value[len-1] or 0 if value is a null terminated string
*
* Set the value of one element inside a structure.
*
* If an element is OPTIONAL and you want to delete it, you must use
* the value=NULL and len=0. Using "pkix.asn":
*
* result=asn1_write_value(cert, "tbsCertificate.issuerUniqueID",
* NULL, 0);
*
* Description for each type:
*
* INTEGER: VALUE must contain a two's complement form integer.
*
* value[0]=0xFF , len=1 -> integer=-1.
* value[0]=0xFF value[1]=0xFF , len=2 -> integer=-1.
* value[0]=0x01 , len=1 -> integer= 1.
* value[0]=0x00 value[1]=0x01 , len=2 -> integer= 1.
* value="123" , len=0 -> integer= 123.
*
* ENUMERATED: As INTEGER (but only with not negative numbers).
*
* BOOLEAN: VALUE must be the null terminated string "TRUE" or
* "FALSE" and LEN != 0.
*
* value="TRUE" , len=1 -> boolean=TRUE.
* value="FALSE" , len=1 -> boolean=FALSE.
*
* OBJECT IDENTIFIER: VALUE must be a null terminated string with
* each number separated by a dot (e.g. "1.2.3.543.1"). LEN != 0.
*
* value="1 2 840 10040 4 3" , len=1 -> OID=dsa-with-sha.
*
* UTCTime: VALUE must be a null terminated string in one of these
* formats: "YYMMDDhhmmssZ", "YYMMDDhhmmssZ",
* "YYMMDDhhmmss+hh'mm'", "YYMMDDhhmmss-hh'mm'",
* "YYMMDDhhmm+hh'mm'", or "YYMMDDhhmm-hh'mm'". LEN != 0.
*
* value="9801011200Z" , len=1 -> time=Jannuary 1st, 1998
* at 12h 00m Greenwich Mean Time
*
* GeneralizedTime: VALUE must be in one of this format:
* "YYYYMMDDhhmmss.sZ", "YYYYMMDDhhmmss.sZ",
* "YYYYMMDDhhmmss.s+hh'mm'", "YYYYMMDDhhmmss.s-hh'mm'",
* "YYYYMMDDhhmm+hh'mm'", or "YYYYMMDDhhmm-hh'mm'" where ss.s
* indicates the seconds with any precision like "10.1" or "01.02".
* LEN != 0
*
* value="2001010112001.12-0700" , len=1 -> time=Jannuary
* 1st, 2001 at 12h 00m 01.12s Pacific Daylight Time
*
* OCTET STRING: VALUE contains the octet string and LEN is the
* number of octets.
*
* value="$\backslash$x01$\backslash$x02$\backslash$x03" ,
* len=3 -> three bytes octet string
*
* GeneralString: VALUE contains the generalstring and LEN is the
* number of octets.
*
* value="$\backslash$x01$\backslash$x02$\backslash$x03" ,
* len=3 -> three bytes generalstring
*
* BIT STRING: VALUE contains the bit string organized by bytes and
* LEN is the number of bits.
*
* value="$\backslash$xCF" , len=6 -> bit string="110011" (six
* bits)
*
* CHOICE: if NAME indicates a choice type, VALUE must specify one of
* the alternatives with a null terminated string. LEN != 0. Using
* "pkix.asn"\:
*
* result=asn1_write_value(cert,
* "certificate1.tbsCertificate.subject", "rdnSequence",
* 1);
*
* ANY: VALUE indicates the der encoding of a structure. LEN != 0.
*
* SEQUENCE OF: VALUE must be the null terminated string "NEW" and
* LEN != 0. With this instruction another element is appended in
* the sequence. The name of this element will be "?1" if it's the
* first one, "?2" for the second and so on.
*
* Using "pkix.asn"\:
*
* result=asn1_write_value(cert,
* "certificate1.tbsCertificate.subject.rdnSequence", "NEW", 1);
*
* SET OF: the same as SEQUENCE OF. Using "pkix.asn":
*
* result=asn1_write_value(cert,
* "tbsCertificate.subject.rdnSequence.?LAST", "NEW", 1);
*
* Returns: %ASN1_SUCCESS if the value was set,
* %ASN1_ELEMENT_NOT_FOUND if @name is not a valid element, and
* %ASN1_VALUE_NOT_VALID if @ivalue has a wrong format.
**/
int
asn1_write_value (asn1_node node_root, const char *name,
const void *ivalue, int len)
{
asn1_node node, p, p2;
unsigned char *temp, *value_temp = NULL, *default_temp = NULL;
int len2, k, k2, negative;
size_t i;
const unsigned char *value = ivalue;
unsigned int type;
node = asn1_find_node (node_root, name);
if (node == NULL)
return ASN1_ELEMENT_NOT_FOUND;
if ((node->type & CONST_OPTION) && (value == NULL) && (len == 0))
{
asn1_delete_structure (&node);
return ASN1_SUCCESS;
}
type = type_field (node->type);
if ((type == ASN1_ETYPE_SEQUENCE_OF || type == ASN1_ETYPE_SET_OF) && (value == NULL) && (len == 0))
{
p = node->down;
while ((type_field (p->type) == ASN1_ETYPE_TAG)
|| (type_field (p->type) == ASN1_ETYPE_SIZE))
p = p->right;
while (p->right)
asn1_delete_structure (&p->right);
return ASN1_SUCCESS;
}
/* Don't allow element deletion for other types */
if (value == NULL)
{
return ASN1_VALUE_NOT_VALID;
}
switch (type)
{
case ASN1_ETYPE_BOOLEAN:
if (!_asn1_strcmp (value, "TRUE"))
{
if (node->type & CONST_DEFAULT)
{
p = node->down;
while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
p = p->right;
if (p->type & CONST_TRUE)
_asn1_set_value (node, NULL, 0);
else
_asn1_set_value (node, "T", 1);
}
else
_asn1_set_value (node, "T", 1);
}
else if (!_asn1_strcmp (value, "FALSE"))
{
if (node->type & CONST_DEFAULT)
{
p = node->down;
while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
p = p->right;
if (p->type & CONST_FALSE)
_asn1_set_value (node, NULL, 0);
else
_asn1_set_value (node, "F", 1);
}
else
_asn1_set_value (node, "F", 1);
}
else
return ASN1_VALUE_NOT_VALID;
break;
case ASN1_ETYPE_INTEGER:
case ASN1_ETYPE_ENUMERATED:
if (len == 0)
{
if ((isdigit (value[0])) || (value[0] == '-'))
{
value_temp = malloc (SIZEOF_UNSIGNED_LONG_INT);
if (value_temp == NULL)
return ASN1_MEM_ALLOC_ERROR;
_asn1_convert_integer (value, value_temp,
SIZEOF_UNSIGNED_LONG_INT, &len);
}
else
{ /* is an identifier like v1 */
if (!(node->type & CONST_LIST))
return ASN1_VALUE_NOT_VALID;
p = node->down;
while (p)
{
if (type_field (p->type) == ASN1_ETYPE_CONSTANT)
{
if (!_asn1_strcmp (p->name, value))
{
value_temp = malloc (SIZEOF_UNSIGNED_LONG_INT);
if (value_temp == NULL)
return ASN1_MEM_ALLOC_ERROR;
_asn1_convert_integer (p->value,
value_temp,
SIZEOF_UNSIGNED_LONG_INT,
&len);
break;
}
}
p = p->right;
}
if (p == NULL)
return ASN1_VALUE_NOT_VALID;
}
}
else
{ /* len != 0 */
value_temp = malloc (len);
if (value_temp == NULL)
return ASN1_MEM_ALLOC_ERROR;
memcpy (value_temp, value, len);
}
if (value_temp[0] & 0x80)
negative = 1;
else
negative = 0;
if (negative && (type_field (node->type) == ASN1_ETYPE_ENUMERATED))
{
free (value_temp);
return ASN1_VALUE_NOT_VALID;
}
for (k = 0; k < len - 1; k++)
if (negative && (value_temp[k] != 0xFF))
break;
else if (!negative && value_temp[k])
break;
if ((negative && !(value_temp[k] & 0x80)) ||
(!negative && (value_temp[k] & 0x80)))
k--;
_asn1_set_value_lv (node, value_temp + k, len - k);
if (node->type & CONST_DEFAULT)
{
p = node->down;
while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
p = p->right;
if ((isdigit (p->value[0])) || (p->value[0] == '-'))
{
default_temp = malloc (SIZEOF_UNSIGNED_LONG_INT);
if (default_temp == NULL)
{
free (value_temp);
return ASN1_MEM_ALLOC_ERROR;
}
_asn1_convert_integer (p->value, default_temp,
SIZEOF_UNSIGNED_LONG_INT, &len2);
}
else
{ /* is an identifier like v1 */
if (!(node->type & CONST_LIST))
{
free (value_temp);
return ASN1_VALUE_NOT_VALID;
}
p2 = node->down;
while (p2)
{
if (type_field (p2->type) == ASN1_ETYPE_CONSTANT)
{
if (!_asn1_strcmp (p2->name, p->value))
{
default_temp = malloc (SIZEOF_UNSIGNED_LONG_INT);
if (default_temp == NULL)
{
free (value_temp);
return ASN1_MEM_ALLOC_ERROR;
}
_asn1_convert_integer (p2->value,
default_temp,
SIZEOF_UNSIGNED_LONG_INT,
&len2);
break;
}
}
p2 = p2->right;
}
if (p2 == NULL)
{
free (value_temp);
return ASN1_VALUE_NOT_VALID;
}
}
if ((len - k) == len2)
{
for (k2 = 0; k2 < len2; k2++)
if (value_temp[k + k2] != default_temp[k2])
{
break;
}
if (k2 == len2)
_asn1_set_value (node, NULL, 0);
}
free (default_temp);
}
free (value_temp);
break;
case ASN1_ETYPE_OBJECT_ID:
for (i = 0; i < _asn1_strlen (value); i++)
if ((!isdigit (value[i])) && (value[i] != '.') && (value[i] != '+'))
return ASN1_VALUE_NOT_VALID;
if (node->type & CONST_DEFAULT)
{
p = node->down;
while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
p = p->right;
if (!_asn1_strcmp (value, p->value))
{
_asn1_set_value (node, NULL, 0);
break;
}
}
_asn1_set_value (node, value, _asn1_strlen (value) + 1);
break;
case ASN1_ETYPE_UTC_TIME:
{
len = _asn1_strlen (value);
if (len < 11)
return ASN1_VALUE_NOT_VALID;
for (k = 0; k < 10; k++)
if (!isdigit (value[k]))
return ASN1_VALUE_NOT_VALID;
switch (len)
{
case 11:
if (value[10] != 'Z')
return ASN1_VALUE_NOT_VALID;
break;
case 13:
if ((!isdigit (value[10])) || (!isdigit (value[11])) ||
(value[12] != 'Z'))
return ASN1_VALUE_NOT_VALID;
break;
case 15:
if ((value[10] != '+') && (value[10] != '-'))
return ASN1_VALUE_NOT_VALID;
for (k = 11; k < 15; k++)
if (!isdigit (value[k]))
return ASN1_VALUE_NOT_VALID;
break;
case 17:
if ((!isdigit (value[10])) || (!isdigit (value[11])))
return ASN1_VALUE_NOT_VALID;
if ((value[12] != '+') && (value[12] != '-'))
return ASN1_VALUE_NOT_VALID;
for (k = 13; k < 17; k++)
if (!isdigit (value[k]))
return ASN1_VALUE_NOT_VALID;
break;
default:
return ASN1_VALUE_NOT_FOUND;
}
_asn1_set_value (node, value, len);
}
break;
case ASN1_ETYPE_GENERALIZED_TIME:
len = _asn1_strlen (value);
_asn1_set_value (node, value, len);
break;
case ASN1_ETYPE_OCTET_STRING:
case ASN1_ETYPE_GENERALSTRING:
case ASN1_ETYPE_NUMERIC_STRING:
case ASN1_ETYPE_IA5_STRING:
case ASN1_ETYPE_TELETEX_STRING:
case ASN1_ETYPE_PRINTABLE_STRING:
case ASN1_ETYPE_UNIVERSAL_STRING:
case ASN1_ETYPE_BMP_STRING:
case ASN1_ETYPE_UTF8_STRING:
case ASN1_ETYPE_VISIBLE_STRING:
if (len == 0)
len = _asn1_strlen (value);
_asn1_set_value_lv (node, value, len);
break;
case ASN1_ETYPE_BIT_STRING:
if (len == 0)
len = _asn1_strlen (value);
asn1_length_der ((len >> 3) + 2, NULL, &len2);
temp = malloc ((len >> 3) + 2 + len2);
if (temp == NULL)
return ASN1_MEM_ALLOC_ERROR;
asn1_bit_der (value, len, temp, &len2);
_asn1_set_value_m (node, temp, len2);
temp = NULL;
break;
case ASN1_ETYPE_CHOICE:
p = node->down;
while (p)
{
if (!_asn1_strcmp (p->name, value))
{
p2 = node->down;
while (p2)
{
if (p2 != p)
{
asn1_delete_structure (&p2);
p2 = node->down;
}
else
p2 = p2->right;
}
break;
}
p = p->right;
}
if (!p)
return ASN1_ELEMENT_NOT_FOUND;
break;
case ASN1_ETYPE_ANY:
_asn1_set_value_lv (node, value, len);
break;
case ASN1_ETYPE_SEQUENCE_OF:
case ASN1_ETYPE_SET_OF:
if (_asn1_strcmp (value, "NEW"))
return ASN1_VALUE_NOT_VALID;
_asn1_append_sequence_set (node, NULL);
break;
default:
return ASN1_ELEMENT_NOT_FOUND;
break;
}
return ASN1_SUCCESS;
}
#define PUT_VALUE( ptr, ptr_size, data, data_size) \
*len = data_size; \
if (ptr_size < data_size) { \
return ASN1_MEM_ERROR; \
} else { \
if (ptr && data_size > 0) \
memcpy (ptr, data, data_size); \
}
#define PUT_STR_VALUE( ptr, ptr_size, data) \
*len = _asn1_strlen (data) + 1; \
if (ptr_size < *len) { \
return ASN1_MEM_ERROR; \
} else { \
/* this strcpy is checked */ \
if (ptr) { \
_asn1_strcpy (ptr, data); \
} \
}
#define PUT_AS_STR_VALUE( ptr, ptr_size, data, data_size) \
*len = data_size + 1; \
if (ptr_size < *len) { \
return ASN1_MEM_ERROR; \
} else { \
/* this strcpy is checked */ \
if (ptr) { \
if (data_size > 0) \
memcpy (ptr, data, data_size); \
ptr[data_size] = 0; \
} \
}
#define ADD_STR_VALUE( ptr, ptr_size, data) \
*len += _asn1_strlen(data); \
if (ptr_size < (int) *len) { \
(*len)++; \
return ASN1_MEM_ERROR; \
} else { \
/* this strcat is checked */ \
if (ptr) _asn1_strcat (ptr, data); \
}
/**
* asn1_read_value:
* @root: pointer to a structure.
* @name: the name of the element inside a structure that you want to read.
* @ivalue: vector that will contain the element's content, must be a
* pointer to memory cells already allocated (may be %NULL).
* @len: number of bytes of *value: value[0]..value[len-1]. Initialy
* holds the sizeof value.
*
* Returns the value of one element inside a structure.
* If an element is OPTIONAL and this returns
* %ASN1_ELEMENT_NOT_FOUND, it means that this element wasn't present
* in the der encoding that created the structure. The first element
* of a SEQUENCE_OF or SET_OF is named "?1". The second one "?2" and
* so on. If the @root provided is a node to specific sequence element,
* then the keyword "?CURRENT" is also acceptable and indicates the
* current sequence element of this node.
*
* Note that there can be valid values with length zero. In these case
* this function will succeed and @len will be zero.
*
* INTEGER: VALUE will contain a two's complement form integer.
*
* integer=-1 -> value[0]=0xFF , len=1.
* integer=1 -> value[0]=0x01 , len=1.
*
* ENUMERATED: As INTEGER (but only with not negative numbers).
*
* BOOLEAN: VALUE will be the null terminated string "TRUE" or
* "FALSE" and LEN=5 or LEN=6.
*
* OBJECT IDENTIFIER: VALUE will be a null terminated string with
* each number separated by a dot (i.e. "1.2.3.543.1").
*
* LEN = strlen(VALUE)+1
*
* UTCTime: VALUE will be a null terminated string in one of these
* formats: "YYMMDDhhmmss+hh'mm'" or "YYMMDDhhmmss-hh'mm'".
* LEN=strlen(VALUE)+1.
*
* GeneralizedTime: VALUE will be a null terminated string in the
* same format used to set the value.
*
* OCTET STRING: VALUE will contain the octet string and LEN will be
* the number of octets.
*
* GeneralString: VALUE will contain the generalstring and LEN will
* be the number of octets.
*
* BIT STRING: VALUE will contain the bit string organized by bytes
* and LEN will be the number of bits.
*
* CHOICE: If NAME indicates a choice type, VALUE will specify the
* alternative selected.
*
* ANY: If NAME indicates an any type, VALUE will indicate the DER
* encoding of the structure actually used.
*
* Returns: %ASN1_SUCCESS if value is returned,
* %ASN1_ELEMENT_NOT_FOUND if @name is not a valid element,
* %ASN1_VALUE_NOT_FOUND if there isn't any value for the element
* selected, and %ASN1_MEM_ERROR if The value vector isn't big enough
* to store the result, and in this case @len will contain the number of
* bytes needed. On the occasion that the stored data are of zero-length
* this function may return %ASN1_SUCCESS even if the provided @len is zero.
**/
int
asn1_read_value (asn1_node root, const char *name, void *ivalue, int *len)
{
return asn1_read_value_type (root, name, ivalue, len, NULL);
}
/**
* asn1_read_value_type:
* @root: pointer to a structure.
* @name: the name of the element inside a structure that you want to read.
* @ivalue: vector that will contain the element's content, must be a
* pointer to memory cells already allocated (may be %NULL).
* @len: number of bytes of *value: value[0]..value[len-1]. Initialy
* holds the sizeof value.
* @etype: The type of the value read (ASN1_ETYPE)
*
* Returns the type and value of one element inside a structure.
* If an element is OPTIONAL and this returns
* %ASN1_ELEMENT_NOT_FOUND, it means that this element wasn't present
* in the der encoding that created the structure. The first element
* of a SEQUENCE_OF or SET_OF is named "?1". The second one "?2" and
* so on. If the @root provided is a node to specific sequence element,
* then the keyword "?CURRENT" is also acceptable and indicates the
* current sequence element of this node.
*
* Note that there can be valid values with length zero. In these case
* this function will succeed and @len will be zero.
*
*
* INTEGER: VALUE will contain a two's complement form integer.
*
* integer=-1 -> value[0]=0xFF , len=1.
* integer=1 -> value[0]=0x01 , len=1.
*
* ENUMERATED: As INTEGER (but only with not negative numbers).
*
* BOOLEAN: VALUE will be the null terminated string "TRUE" or
* "FALSE" and LEN=5 or LEN=6.
*
* OBJECT IDENTIFIER: VALUE will be a null terminated string with
* each number separated by a dot (i.e. "1.2.3.543.1").
*
* LEN = strlen(VALUE)+1
*
* UTCTime: VALUE will be a null terminated string in one of these
* formats: "YYMMDDhhmmss+hh'mm'" or "YYMMDDhhmmss-hh'mm'".
* LEN=strlen(VALUE)+1.
*
* GeneralizedTime: VALUE will be a null terminated string in the
* same format used to set the value.
*
* OCTET STRING: VALUE will contain the octet string and LEN will be
* the number of octets.
*
* GeneralString: VALUE will contain the generalstring and LEN will
* be the number of octets.
*
* BIT STRING: VALUE will contain the bit string organized by bytes
* and LEN will be the number of bits.
*
* CHOICE: If NAME indicates a choice type, VALUE will specify the
* alternative selected.
*
* ANY: If NAME indicates an any type, VALUE will indicate the DER
* encoding of the structure actually used.
*
* Returns: %ASN1_SUCCESS if value is returned,
* %ASN1_ELEMENT_NOT_FOUND if @name is not a valid element,
* %ASN1_VALUE_NOT_FOUND if there isn't any value for the element
* selected, and %ASN1_MEM_ERROR if The value vector isn't big enough
* to store the result, and in this case @len will contain the number of
* bytes needed. On the occasion that the stored data are of zero-length
* this function may return %ASN1_SUCCESS even if the provided @len is zero.
**/
int
asn1_read_value_type (asn1_node root, const char *name, void *ivalue,
int *len, unsigned int *etype)
{
asn1_node node, p, p2;
int len2, len3, result;
int value_size = *len;
unsigned char *value = ivalue;
unsigned type;
node = asn1_find_node (root, name);
if (node == NULL)
return ASN1_ELEMENT_NOT_FOUND;
type = type_field (node->type);
if ((type != ASN1_ETYPE_NULL) &&
(type != ASN1_ETYPE_CHOICE) &&
!(node->type & CONST_DEFAULT) && !(node->type & CONST_ASSIGN) &&
(node->value == NULL))
return ASN1_VALUE_NOT_FOUND;
if (etype)
*etype = type;
switch (type)
{
case ASN1_ETYPE_NULL:
PUT_STR_VALUE (value, value_size, "NULL");
break;
case ASN1_ETYPE_BOOLEAN:
if ((node->type & CONST_DEFAULT) && (node->value == NULL))
{
p = node->down;
while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
p = p->right;
if (p->type & CONST_TRUE)
{
PUT_STR_VALUE (value, value_size, "TRUE");
}
else
{
PUT_STR_VALUE (value, value_size, "FALSE");
}
}
else if (node->value[0] == 'T')
{
PUT_STR_VALUE (value, value_size, "TRUE");
}
else
{
PUT_STR_VALUE (value, value_size, "FALSE");
}
break;
case ASN1_ETYPE_INTEGER:
case ASN1_ETYPE_ENUMERATED:
if ((node->type & CONST_DEFAULT) && (node->value == NULL))
{
p = node->down;
while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
p = p->right;
if ((isdigit (p->value[0])) || (p->value[0] == '-')
|| (p->value[0] == '+'))
{
result = _asn1_convert_integer
(p->value, value, value_size, len);
if (result != ASN1_SUCCESS)
return result;
}
else
{ /* is an identifier like v1 */
p2 = node->down;
while (p2)
{
if (type_field (p2->type) == ASN1_ETYPE_CONSTANT)
{
if (!_asn1_strcmp (p2->name, p->value))
{
result = _asn1_convert_integer
(p2->value, value, value_size,
len);
if (result != ASN1_SUCCESS)
return result;
break;
}
}
p2 = p2->right;
}
}
}
else
{
len2 = -1;
result = asn1_get_octet_der
(node->value, node->value_len, &len2, value, value_size,
len);
if (result != ASN1_SUCCESS)
return result;
}
break;
case ASN1_ETYPE_OBJECT_ID:
if (node->type & CONST_ASSIGN)
{
*len = 0;
if (value)
value[0] = 0;
p = node->down;
while (p)
{
if (type_field (p->type) == ASN1_ETYPE_CONSTANT)
{
ADD_STR_VALUE (value, value_size, p->value);
if (p->right)
{
ADD_STR_VALUE (value, value_size, ".");
}
}
p = p->right;
}
(*len)++;
}
else if ((node->type & CONST_DEFAULT) && (node->value == NULL))
{
p = node->down;
while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
p = p->right;
PUT_STR_VALUE (value, value_size, p->value);
}
else
{
PUT_STR_VALUE (value, value_size, node->value);
}
break;
case ASN1_ETYPE_GENERALIZED_TIME:
case ASN1_ETYPE_UTC_TIME:
PUT_AS_STR_VALUE (value, value_size, node->value, node->value_len);
break;
case ASN1_ETYPE_OCTET_STRING:
case ASN1_ETYPE_GENERALSTRING:
case ASN1_ETYPE_NUMERIC_STRING:
case ASN1_ETYPE_IA5_STRING:
case ASN1_ETYPE_TELETEX_STRING:
case ASN1_ETYPE_PRINTABLE_STRING:
case ASN1_ETYPE_UNIVERSAL_STRING:
case ASN1_ETYPE_BMP_STRING:
case ASN1_ETYPE_UTF8_STRING:
case ASN1_ETYPE_VISIBLE_STRING:
len2 = -1;
result = asn1_get_octet_der
(node->value, node->value_len, &len2, value, value_size,
len);
if (result != ASN1_SUCCESS)
return result;
break;
case ASN1_ETYPE_BIT_STRING:
len2 = -1;
result = asn1_get_bit_der
(node->value, node->value_len, &len2, value, value_size,
len);
if (result != ASN1_SUCCESS)
return result;
break;
case ASN1_ETYPE_CHOICE:
PUT_STR_VALUE (value, value_size, node->down->name);
break;
case ASN1_ETYPE_ANY:
len3 = -1;
len2 = asn1_get_length_der (node->value, node->value_len, &len3);
if (len2 < 0)
return ASN1_DER_ERROR;
PUT_VALUE (value, value_size, node->value + len3, len2);
break;
default:
return ASN1_ELEMENT_NOT_FOUND;
break;
}
return ASN1_SUCCESS;
}
/**
* asn1_read_tag:
* @root: pointer to a structure
* @name: the name of the element inside a structure.
* @tagValue: variable that will contain the TAG value.
* @classValue: variable that will specify the TAG type.
*
* Returns the TAG and the CLASS of one element inside a structure.
* CLASS can have one of these constants: %ASN1_CLASS_APPLICATION,
* %ASN1_CLASS_UNIVERSAL, %ASN1_CLASS_PRIVATE or
* %ASN1_CLASS_CONTEXT_SPECIFIC.
*
* Returns: %ASN1_SUCCESS if successful, %ASN1_ELEMENT_NOT_FOUND if
* @name is not a valid element.
**/
int
asn1_read_tag (asn1_node root, const char *name, int *tagValue,
int *classValue)
{
asn1_node node, p, pTag;
node = asn1_find_node (root, name);
if (node == NULL)
return ASN1_ELEMENT_NOT_FOUND;
p = node->down;
/* pTag will points to the IMPLICIT TAG */
pTag = NULL;
if (node->type & CONST_TAG)
{
while (p)
{
if (type_field (p->type) == ASN1_ETYPE_TAG)
{
if ((p->type & CONST_IMPLICIT) && (pTag == NULL))
pTag = p;
else if (p->type & CONST_EXPLICIT)
pTag = NULL;
}
p = p->right;
}
}
if (pTag)
{
*tagValue = _asn1_strtoul (pTag->value, NULL, 10);
if (pTag->type & CONST_APPLICATION)
*classValue = ASN1_CLASS_APPLICATION;
else if (pTag->type & CONST_UNIVERSAL)
*classValue = ASN1_CLASS_UNIVERSAL;
else if (pTag->type & CONST_PRIVATE)
*classValue = ASN1_CLASS_PRIVATE;
else
*classValue = ASN1_CLASS_CONTEXT_SPECIFIC;
}
else
{
unsigned type = type_field (node->type);
*classValue = ASN1_CLASS_UNIVERSAL;
switch (type)
{
CASE_HANDLED_ETYPES:
*tagValue = _asn1_tags[type].tag;
break;
case ASN1_ETYPE_TAG:
case ASN1_ETYPE_CHOICE:
case ASN1_ETYPE_ANY:
*tagValue = -1;
break;
default:
break;
}
}
return ASN1_SUCCESS;
}
/**
* asn1_read_node_value:
* @node: pointer to a node.
* @data: a point to a asn1_data_node_st
*
* Returns the value a data node inside a asn1_node structure.
* The data returned should be handled as constant values.
*
* Returns: %ASN1_SUCCESS if the node exists.
**/
int
asn1_read_node_value (asn1_node node, asn1_data_node_st * data)
{
data->name = node->name;
data->value = node->value;
data->value_len = node->value_len;
data->type = type_field (node->type);
return ASN1_SUCCESS;
}
|