This repository was archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathasn1.c
1409 lines (1210 loc) · 30.1 KB
/
asn1.c
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
/*
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#include <gmssl/oid.h>
#include <gmssl/asn1.h>
#include <gmssl/error.h>
#include "endian.h"
/*
## 返回值
解析函数返回错误:
1. 输入数据长度为0,如待解析的对象具有OPTIONAL属性,那么不意味错误。
应显式告知调用方对象编码为空,由调用方判断是否为错误。
2. 输入数据和目标对象类型不符,如待解析的对象具有OPTIONAL属性,那么意味目标对象为空。
3. 长度和负载数据解析出错,这意味绝对的错误。
4. 数据类型具有IMPLICIT属性时,意味着该对象的Tag被修改了,那么解析时调用方必须提供新的Tag。
DEFAULT值在接口上不提供这个功能,这个可以在数据的初始化时完成。
内部接口不支持参数默认值或者多态的接口,例如不允许输入参数为空。
接口具有单一的逻辑可以通过严格的检查避免隐藏错误,提高健壮性。
*/
static char *asn1_tag_index[] = {
"[0]", "[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]",
};
const char *asn1_tag_name(int tag)
{
if (tag < 0 || tag > 0xff) {
error_print();
return NULL;
}
switch (tag & 0xc0) {
case ASN1_TAG_CONTENT_SPECIFIC: return asn1_tag_index[tag & 0xe0];
case ASN1_TAG_APPLICATION: return "Application";
case ASN1_TAG_PRIVATE: return "Private";
}
switch (tag) {
case ASN1_TAG_BOOLEAN: return "BOOLEAN";
case ASN1_TAG_INTEGER: return "INTEGER";
case ASN1_TAG_BIT_STRING: return "BIT STRING";
case ASN1_TAG_OCTET_STRING: return "OCTET STRING";
case ASN1_TAG_NULL: return "NULL";
case ASN1_TAG_OBJECT_IDENTIFIER: return "OBJECT IDENTIFIER";
case ASN1_TAG_ObjectDescriptor: return "ObjectDescriptor";
case ASN1_TAG_EXTERNAL: return "EXTERNAL";
case ASN1_TAG_REAL: return "REAL";
case ASN1_TAG_ENUMERATED: return "ENUMERATED";
case ASN1_TAG_EMBEDDED: return "EMBEDDED";
case ASN1_TAG_UTF8String: return "UTF8String";
case ASN1_TAG_RELATIVE_OID: return "RELATIVE_OID";
case ASN1_TAG_NumericString: return "NumericString";
case ASN1_TAG_PrintableString: return "PrintableString";
case ASN1_TAG_TeletexString: return "TeletexString";
case ASN1_TAG_VideotexString: return "VideotexString";
case ASN1_TAG_IA5String: return "IA5String";
case ASN1_TAG_UTCTime: return "UTCTime";
case ASN1_TAG_GeneralizedTime: return "GeneralizedTime";
case ASN1_TAG_GraphicString: return "GraphicString";
case ASN1_TAG_VisibleString: return "VisibleString";
case ASN1_TAG_GeneralString: return "GeneralString";
case ASN1_TAG_UniversalString: return "UniversalString";
case ASN1_TAG_CHARACTER_STRING: return "CHARACTER STRING";
case ASN1_TAG_BMPString: return "BMPString";
case ASN1_TAG_SEQUENCE: return "SEQUENCE";
case ASN1_TAG_SET: return "SET";
case ASN1_TAG_EXPLICIT: return "EXPLICIT";
}
error_print();
return NULL;
}
int asn1_tag_is_cstring(int tag)
{
switch (tag) {
case ASN1_TAG_UTF8String:
case ASN1_TAG_NumericString:
case ASN1_TAG_PrintableString:
case ASN1_TAG_TeletexString:
case ASN1_TAG_IA5String:
case ASN1_TAG_GeneralString:
return 1;
}
return 0;
}
int asn1_utf8_string_check(const char *a, size_t alen)
{
return 1;
}
int asn1_printable_string_check(const char *a, size_t alen)
{
return 1;
}
int asn1_ia5_string_check(const char *a, size_t alen)
{
return 1;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// DER encoding
/////////////////////////////////////////////////////////////////////////////////////////////
// 这组函数不对输入进行检查
// 还是检查报错比较方便,这样调用的函数更容易实现
// asn.1编解码不考虑效率的问题
int asn1_tag_to_der(int tag, uint8_t **out, size_t *outlen)
{
if (out) {
*(*out)++ = (uint8_t)tag;
}
(*outlen)++;
return 1;
}
int asn1_length_to_der(size_t len, uint8_t **out, size_t *outlen)
{
if (len < 128) {
if (out) {
*(*out)++ = (uint8_t)len;
}
(*outlen)++;
} else {
uint8_t buf[4];
int i;
PUTU32(buf, (uint32_t)len);
if (len < 256) i = 1;
else if (len < 65536) i = 2;
else if (len < (1 << 24)) i = 3;
else i = 4;
if (out) {
*(*out)++ = 0x80 + i;
memcpy(*out, buf + 4 - i, i);
(*out) += i;
}
(*outlen) += 1 + i;
}
return 1;
}
// 提供返回值是为了和其他to_der函数一致
int asn1_data_to_der(const uint8_t *data, size_t datalen, uint8_t **out, size_t *outlen)
{
if (out) {
memcpy(*out, data, datalen);
*out += datalen;
}
*outlen += datalen;
return 1;
}
int asn1_tag_from_der(int tag, const uint8_t **in, size_t *inlen)
{
if (*inlen == 0) {
return 0;
}
if (**in != tag) {
return 0;
}
(*in)++;
(*inlen)--;
return 1;
}
int asn1_tag_get(int *tag, const uint8_t **in, size_t *inlen)
{
if (*inlen == 0) {
return 0;
}
*tag = **in;
return 1;
}
int asn1_length_from_der(size_t *plen, const uint8_t **pin, size_t *pinlen)
{
const uint8_t *in = *pin;
size_t inlen = *pinlen;
size_t len;
if (inlen <= 0) {
return -1;
}
if (*in < 128) {
len = *in++;
inlen--;
} else {
uint8_t buf[4] = {0};
int nbytes = *in++ & 0x7f;
//error_print_msg("nbytes = %d\n", nbytes);
if (nbytes < 1 || nbytes > 4) {
error_print();
return -1;
}
inlen--;
if (inlen < nbytes) {
error_print();
return -1;
}
memcpy(buf + sizeof(buf) - nbytes, in, nbytes);
len = (size_t)GETU32(buf);
in += nbytes;
inlen -= nbytes;
}
*plen = len;
*pin = in;
*pinlen = inlen;
if (inlen < len) {
error_print_msg("inlen = %zu\n", *pinlen);
error_print_msg("length = %zu, left = %zu\n", len, inlen);
return -2; // 特殊错误值用于 test_asn1_length() 的测试
}
return 1;
}
int asn1_data_from_der(const uint8_t **data, size_t datalen, const uint8_t **in, size_t *inlen)
{
if (*inlen < datalen) {
error_print();
return -1;
}
*data = *in;
*in += datalen;
*inlen -= datalen;
return 1;
}
int asn1_header_to_der(int tag, size_t len, uint8_t **out, size_t *outlen)
{
if ((out && !(*out)) || !outlen) {
error_print();
return -1;
}
asn1_tag_to_der(tag, out, outlen);
asn1_length_to_der(len, out, outlen);
return 1;
}
// If data == NULL, out should not be NULL
// 这个实现是不支持OPTIONAL的
int asn1_type_to_der(int tag, const uint8_t *d, size_t dlen, uint8_t **out, size_t *outlen)
{
if (!d) {
if (dlen) {
error_print();
return -1;
}
return 0;
}
if (asn1_tag_to_der(tag, out, outlen) != 1
|| asn1_length_to_der(dlen, out, outlen) != 1
|| asn1_data_to_der(d, dlen, out, outlen) != 1) {
error_print();
return -1;
}
return 1;
}
int asn1_type_from_der(int tag, const uint8_t **d, size_t *dlen, const uint8_t **in, size_t *inlen)
{
int ret;
if ((ret = asn1_tag_from_der(tag, in, inlen)) != 1) {
if (ret < 0) error_print();
else {
*d = NULL;
*dlen = 0;
}
return ret;
}
if (asn1_length_from_der(dlen, in, inlen) != 1
|| asn1_data_from_der(d, *dlen, in, inlen) != 1) {
error_print();
return -1;
}
return 1;
}
int asn1_any_tag_from_der(int *tag, const uint8_t **in, size_t *inlen)
{
if (*inlen == 0) {
return 0;
}
*tag = *(*in)++;
(*inlen)--;
return 1;
}
int asn1_any_type_from_der(int *tag, const uint8_t **data, size_t *datalen, const uint8_t **in, size_t *inlen)
{
int ret;
if ((ret = asn1_any_tag_from_der(tag, in, inlen)) != 1) {
return ret;
}
if (asn1_length_from_der(datalen, in, inlen) != 1) {
error_print();
return -1;
}
*data = *in;
*in += *datalen;
*inlen -= *datalen;
return 1;
}
int asn1_any_to_der(const uint8_t *tlv, size_t tlvlen, uint8_t **out, size_t *outlen)
{
return asn1_data_to_der(tlv, tlvlen, out, outlen);
}
int asn1_any_from_der(const uint8_t **tlv, size_t *tlvlen, const uint8_t **in, size_t *inlen)
{
int ret;
int tag;
const uint8_t *data;
size_t datalen;
*tlv = *in;
*tlvlen = *inlen;
if ((ret = asn1_any_type_from_der(&tag, &data, &datalen, in, inlen)) != 1) {
error_print();
return ret;
}
*tlvlen -= *inlen;
return 1;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ASN1_TRUE 0xff
#define ASN1_FALSE 0x00
const char *asn1_boolean_name(int val)
{
switch (val) {
case 1: return "true";
case 0: return "false";
}
return NULL;
}
int asn1_boolean_from_name(int *val, const char *name)
{
if (strcmp(name, "true") == 0) {
*val = 1;
return 1;
} else if (strcmp(name, "false") == 0) {
*val = 0;
return 1;
}
*val = -1;
return -1;
}
int asn1_boolean_to_der_ex(int tag, int val, uint8_t **out, size_t *outlen)
{
if ((out && !(*out)) || !outlen) {
return -1;
}
if (val < 0) {
return 0;
}
if (out) {
*(*out)++ = tag;
*(*out)++ = 0x01;
*(*out)++ = val ? 0xff : 0x00;
}
(*outlen) += 3;
return 1;
}
int asn1_integer_to_der_ex(int tag, const uint8_t *a, size_t alen, uint8_t **out, size_t *outlen)
{
if (!a || alen <= 0 || alen > INT_MAX || (out && !(*out)) || !outlen) {
error_print();
return -1;
}
if (out)
*(*out)++ = tag;
(*outlen)++;
while (*a == 0 && alen > 1) {
a++;
alen--;
}
if (a[0] & 0x80) {
asn1_length_to_der(alen + 1, out, outlen);
if (out) {
*(*out)++ = 0x00;
memcpy(*out, a, alen);
(*out) += alen;
}
(*outlen) += 1 + alen;
} else {
asn1_length_to_der(alen, out ,outlen);
if (out) {
memcpy(*out, a, alen);
(*out) += alen;
}
(*outlen) += alen;
}
return 1;
}
int asn1_int_to_der_ex(int tag, int a, uint8_t **out, size_t *outlen)
{
int i;
uint8_t buf[4] = {0};
size_t len = 0;
if (a == -1) {
return 0;
}
while (a > 0) {
buf[3 - len] = a & 0xff;
a >>= 8;
len++;
}
if (!len) {
len = 1;
}
return asn1_integer_to_der_ex(tag, buf + 4 - len, len, out, outlen);
}
int asn1_bit_string_to_der_ex(int tag, const uint8_t *bits, size_t nbits, uint8_t **out, size_t *outlen)
{
uint8_t unused = (8 - nbits % 8) % 8;
size_t nbytes = (nbits + 7) / 8;
if (!bits) {
return 0;
}
if (asn1_tag_to_der(tag, out, outlen) != 1
|| asn1_length_to_der(nbytes + 1, out, outlen) != 1
|| asn1_data_to_der(&unused, 1, out, outlen) != 1
|| asn1_data_to_der(bits, nbytes, out, outlen) != 1) {
error_print();
return -1;
}
return 1;
}
int asn1_bit_octets_to_der_ex(int tag, const uint8_t *octs, size_t nocts, uint8_t **out, size_t *outlen)
{
return asn1_bit_string_to_der_ex(tag, octs, nocts << 3, out, outlen);
}
int asn1_bits_to_der_ex(int tag, int bits, uint8_t **out, size_t *outlen)
{
size_t nbits = 0;
uint8_t buf[4] = {0};
int i = 0;
uint8_t mask = 0x80;
if (bits < 0) {
return 0;
}
while (bits > 0) {
if (bits & 1)
buf[i] |= mask;
mask >>= 1;
bits >>= 1;
nbits++;
if (nbits % 8 == 0) {
i++;
mask = 0x80;
}
}
if (!nbits) {
nbits = 1;
}
return asn1_bit_string_to_der_ex(tag, buf, nbits, out, outlen);
}
const char *asn1_null_name(void)
{
return "null";
}
int asn1_null_to_der(uint8_t **out, size_t *outlen)
{
if ((out && !(*out)) || !outlen) {
return -1;
}
if (out) {
*(*out)++ = ASN1_TAG_NULL;
*(*out)++ = 0x00;
}
*outlen += 2;
return 1;
}
static void asn1_oid_node_to_base128(uint32_t a, uint8_t **out, size_t *outlen)
{
uint8_t buf[5];
int n = 0;
buf[n++] = a & 0x7f;
a >>= 7;
while (a) {
buf[n++] = 0x80 | (a & 0x7f);
a >>= 7;
}
while (n--) {
if (out)
*(*out)++ = buf[n];
(*outlen)++;
}
}
// 实际上我们在解析的时候是不知道具体在哪里结束的
// 解析是有可能出错的,如果没有发现最后一个0开头的字节就出错了
// 还有值太大也会出错,我们最多读取5个字节
// { 0x81, 0x82 }
// { 0x81, 0x82, 0x83, 0x84, 0x85, 0x06 }
static int asn1_oid_node_from_base128(uint32_t *a, const uint8_t **in, size_t *inlen)
{
uint8_t buf[5];
int n = 0;
int i;
for (;;) {
if ((*inlen)-- < 1 || n >= 5) {
return -1;
}
buf[n] = *(*in)++;
if ((buf[n++] & 0x80) == 0) {
break;
}
}
// 32 - 7*4 = 4, so the first byte should be like 1000bbbb
if (n == 5 && (buf[0] & 0x70)) {
return -1;
}
*a = 0;
for (i = 0; i < n; i++) {
*a = ((*a) << 7) | (buf[i] & 0x7f);
}
return 1;
}
int asn1_object_identifier_to_octets(const uint32_t *nodes, size_t nodes_cnt, uint8_t *out, size_t *outlen)
{
if (nodes_cnt < 2 || nodes_cnt > 32) {
return -1;
}
if (out)
*out++ = (uint8_t)(nodes[0] * 40 + nodes[1]);
(*outlen) = 1;
nodes += 2;
nodes_cnt -= 2;
while (nodes_cnt--) {
asn1_oid_node_to_base128(*nodes++, &out, outlen);
}
return 1;
}
// 因为这个函数总是被asn1函数调用的,因此给的输入数据长度是已知的
int asn1_object_identifier_from_octets(uint32_t *nodes, size_t *nodes_cnt, const uint8_t *in, size_t inlen)
{
size_t count = 0;
const uint8_t *p = in;
size_t len = inlen;
if (!nodes || !nodes_cnt || !in || inlen <= 0) {
error_print();
return -1;
}
if (inlen < 1) {
error_print();
return -1;
}
// FIXME: 需要支持 nodes = NULL 吗?
if (nodes) {
*nodes++ = (*in) / 40;
*nodes++ = (*in) % 40;
}
in++;
inlen--;
count += 2;
while (inlen) {
uint32_t val;
if (count > 32) {
error_print();
return -1;
}
if (asn1_oid_node_from_base128(&val, &in, &inlen) < 0) {
error_print();
return -1;
}
if (nodes) {
*nodes++ = val;
}
count++;
}
*nodes_cnt = count;
return 1;
}
int asn1_object_identifier_to_der_ex(int tag, const uint32_t *nodes, size_t nodes_cnt, uint8_t **out, size_t *outlen)
{
uint8_t octets[32];
size_t octetslen = 0;
if ((out && !(*out)) || !outlen) {
return -1;
}
if (out)
*(*out)++ = tag;
(*outlen)++;
asn1_object_identifier_to_octets(nodes, nodes_cnt, octets, &octetslen);
asn1_length_to_der(octetslen, out, outlen);
if (out) {
// 注意:If out == NULL, *out ==> Segment Fault
memcpy(*out, octets, octetslen);
*out += octetslen;
}
*outlen += octetslen;
return 1;
}
const ASN1_OID_INFO *asn1_oid_info_from_name(const ASN1_OID_INFO *infos, size_t count, const char *name)
{
size_t i;
for (i = 0; i < count; i++) {
if (strcmp(infos[i].name, name) == 0) {
return &infos[i];
}
}
return NULL;
}
const ASN1_OID_INFO *asn1_oid_info_from_oid(const ASN1_OID_INFO *infos, size_t count, int oid)
{
size_t i;
for (i = 0; i < count; i++) {
if (infos[i].oid == oid) {
return &infos[i];
}
}
return NULL;
}
int asn1_oid_info_from_der_ex(const ASN1_OID_INFO **info, uint32_t *nodes, size_t *nodes_cnt,
const ASN1_OID_INFO *infos, size_t count, const uint8_t **in, size_t *inlen)
{
int ret;
size_t i;
if ((ret = asn1_object_identifier_from_der(nodes, nodes_cnt, in, inlen)) != 1) {
if (ret < 0) error_print();
if (ret == 0) error_print();
return ret;
}
*info = NULL;
for (i = 0; i < count; i++) {
if (*nodes_cnt == infos[i].nodes_cnt
&& memcmp(nodes, infos[i].nodes, (*nodes_cnt) * sizeof(int)) == 0) {
*info = &infos[i];
return 1;
}
}
// 注意,此时虽然返回1,但是*info == NULL,因此调用方应该显式的判断info
return 1;
}
int asn1_oid_info_from_der(const ASN1_OID_INFO **info, const ASN1_OID_INFO *infos, size_t count, const uint8_t **in, size_t *inlen)
{
int ret;
uint32_t nodes[32];
size_t nodes_cnt;
if ((ret = asn1_oid_info_from_der_ex(info, nodes, &nodes_cnt, infos, count, in, inlen)) < 0) {
error_print();
return -1;
} else if (ret > 1) {
error_print();
return -1;
}
return ret;
}
// asn1_oid_from_octets 不返回错误值,只返回 OID_undef
// 但是数据编码仍可能是非法的
// 如果返回 OID_undef,需要通过 asn1_object_identifier_from_octets 判断格式是否正确
// 显然这个函数并不合适,因为在整个gmssl中,我们不提供完整的ASN.1数据库,无法从一个OID中给出解析
int asn1_utf8_string_to_der_ex(int tag, const char *d, size_t dlen, uint8_t **out, size_t *outlen)
{
return asn1_type_to_der(tag, (const uint8_t *)d, dlen, out, outlen);
}
int asn1_printable_string_to_der_ex(int tag, const char *d, size_t dlen, uint8_t **out, size_t *outlen)
{
return asn1_type_to_der(tag, (const uint8_t *)d, dlen, out, outlen);
}
int asn1_ia5_string_to_der_ex(int tag, const char *d, size_t dlen, uint8_t **out, size_t *outlen)
{
return asn1_type_to_der(tag, (const uint8_t *)d, dlen, out, outlen);
}
int asn1_utc_time_to_der_ex(int tag, time_t a, uint8_t **out, size_t *outlen)
{
struct tm tm_val;
char buf[ASN1_UTC_TIME_LEN + 1];
if ((out && !(*out)) || !outlen) {
return -1;
}
gmtime_r(&a, &tm_val);
strftime(buf, sizeof(buf), "%y%m%d%H%M%SZ", &tm_val);
if (out)
*(*out)++ = tag;
(*outlen)++;
asn1_length_to_der(sizeof(buf)-1, out, outlen);
if (out) {
memcpy(*out, buf, sizeof(buf)-1);
(*out) += sizeof(buf)-1;
}
*outlen += sizeof(buf)-1;
return 1;
}
int asn1_generalized_time_to_der_ex(int tag, time_t a, uint8_t **out, size_t *outlen)
{
struct tm tm_val;
char buf[ASN1_GENERALIZED_TIME_LEN + 1];
if ((out && !(*out)) || !outlen) {
error_print();
return -1;
}
gmtime_r(&a, &tm_val);
strftime(buf, sizeof(buf), "%Y%m%d%H%M%SZ", &tm_val);
//printf("%s %d: generalized time : %s\n", __FILE__, __LINE__, buf);
if (out)
*(*out)++ = tag;
(*outlen)++;
asn1_length_to_der(ASN1_GENERALIZED_TIME_LEN, out, outlen);
if (out) {
memcpy(*out, buf, ASN1_GENERALIZED_TIME_LEN);
(*out) += ASN1_GENERALIZED_TIME_LEN;
}
*outlen += ASN1_GENERALIZED_TIME_LEN;
return 1;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// DER decoding
/////////////////////////////////////////////////////////////////////////////////////////////
/*
解码函数的返回值:
ret == 0
当前剩余的数据数据长度为0
或者下一个对象与期待不符,即输入对象的标签不等于输入的tag
当对象为OPTIONAL时,调用方可以通过判断返回值是否为0进行处理
ret < 0
标签正确但是长度或数据解析出错
ret == 1
解析正确
解码函数的输入:
*in != NULL
例如一个SEQUENCE中的属性均为OPTIONAL,解析后指针仍不为空
因此不允许输入空的输入数据指针
处理规则
当返回值 ret <= 0 时,*tag, *in, *inlen 的值保持不变
如果一个类型有 DEFAULT 属性,调用方可以将返回数据预先设置为默认值,
如果该对象未被编码,即返回值为0,那么解码函数不会修改已经设置的默认值
*/
int asn1_boolean_from_der_ex(int tag, int *val, const uint8_t **in, size_t *inlen)
{
if (!val || !in || !(*in) || !inlen) {
return -1;
}
if (*inlen <= 0 || **in != tag) {
*val = -1;
return 0;
}
if (*inlen < 3
|| *(*in + 1) != 0x01
|| (*(*in + 2) != 0 && *(*in + 2) != 0xff)) {
return -1;
}
*val = *(*in + 2) ? 1 : 0;
*in += 3;
*inlen -= 3;
return 1;
}
int asn1_integer_from_der_ex(int tag, const uint8_t **a, size_t *alen, const uint8_t **pin, size_t *pinlen)
{
const uint8_t *in = *pin;
size_t inlen = *pinlen;
size_t len;
if (!a || !alen || !pin || !(*pin) || !pinlen) {
error_print();
return -1;
}
if (inlen-- <= 0 || *in++ != tag) {
return 0;
}
if (asn1_length_from_der(&len, &in, &inlen) != 1
|| len <= 0) {
error_print();
return -1;
}
// 判断 ASN1_INTEGER 是否为负数,我们不支持负整数,返回特性不支持错误
if (*in & 0x80) {
error_print();
return -255;
}
if (*in == 0 && len > 1) {
inlen--;
in++;
len--;
}
if (*in == 0 && len > 1) {
error_print();
return -1;
}
*a = in;
*alen = len;
*pin = in + len;
*pinlen = inlen - len;
return 1;
}
int asn1_int_from_der_ex(int tag, int *a, const uint8_t **in, size_t *inlen)
{
int ret;
const uint8_t *p;
size_t len, i;
unsigned int val = 0;
if (!a || !in || !(*in) || !inlen) {
error_print();
return -1;
}
if ((ret = asn1_integer_from_der_ex(tag, &p, &len, in, inlen)) != 1) {
if (ret < 0) error_print();
else *a = -1;
return ret;
}
if (len > 8) {
error_print();
return -1;
}
for (i = 0; i < len; i++) {
val = (val << 8) | p[i];
}
*a = val;
return 1;
}
int asn1_bit_string_from_der_ex(int tag, const uint8_t **bits, size_t *nbits, const uint8_t **in, size_t *inlen)
{
int ret;
size_t len;
int unused_bits;
if ((ret = asn1_tag_from_der(tag, in, inlen)) != 1) {
if (ret < 0) error_print();
else {
*bits = NULL;
*nbits = 0;
}
return ret;