-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasn.c
More file actions
9834 lines (8081 loc) · 270 KB
/
Copy pathasn.c
File metadata and controls
9834 lines (8081 loc) · 270 KB
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
/* asn.c
*
* Copyright (C) 2006-2016 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
/*
ASN Options:
* NO_ASN_TIME: Disables time parts of the ASN code for systems without an RTC
or wishing to save space.
* IGNORE_NAME_CONSTRAINTS: Skip ASN name checks.
*/
#ifndef NO_ASN
#ifdef HAVE_RTP_SYS
#include "os.h" /* dc_rtc_api needs */
#include "dc_rtc_api.h" /* to get current time */
#endif
#include <wolfssl/wolfcrypt/asn.h>
#include <wolfssl/wolfcrypt/coding.h>
#include <wolfssl/wolfcrypt/md2.h>
#include <wolfssl/wolfcrypt/hmac.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/pwdbased.h>
#include <wolfssl/wolfcrypt/des3.h>
#include <wolfssl/wolfcrypt/logging.h>
#include <wolfssl/wolfcrypt/random.h>
#include <wolfssl/wolfcrypt/hash.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#include <wolfcrypt/src/misc.c>
#endif
#ifndef NO_RC4
#include <wolfssl/wolfcrypt/arc4.h>
#endif
#ifdef HAVE_NTRU
#include "libntruencrypt/ntru_crypto.h"
#endif
#if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)
#include <wolfssl/wolfcrypt/sha512.h>
#endif
#ifndef NO_SHA256
#include <wolfssl/wolfcrypt/sha256.h>
#endif
#ifdef HAVE_ECC
#include <wolfssl/wolfcrypt/ecc.h>
#endif
#ifdef WOLFSSL_DEBUG_ENCODING
#if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
#if MQX_USE_IO_OLD
#include <fio.h>
#else
#include <nio.h>
#endif
#else
#include <stdio.h>
#endif
#endif
#ifdef _MSC_VER
/* 4996 warning to use MS extensions e.g., strcpy_s instead of XSTRNCPY */
#pragma warning(disable: 4996)
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef NO_ASN_TIME
#if defined(HAVE_RTP_SYS)
/* uses parital <time.h> structures */
#define XTIME(tl) (0)
#define XGMTIME(c, t) rtpsys_gmtime((c))
#elif defined(MICRIUM)
#if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
#define XVALIDATE_DATE(d, f, t) NetSecure_ValidateDateHandler((d), (f), (t))
#else
#define XVALIDATE_DATE(d, f, t) (0)
#endif
#define NO_TIME_H
/* since Micrium not defining XTIME or XGMTIME, CERT_GEN not available */
#elif defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
#include <time.h>
#define XTIME(t1) pic32_time((t1))
#define XGMTIME(c, t) gmtime((c))
#elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
#define XTIME(t1) mqx_time((t1))
#define HAVE_GMTIME_R
#elif defined(FREESCALE_KSDK_BM) || defined(FREESCALE_FREE_RTOS)
#include <time.h>
#define XTIME(t1) ksdk_time((t1))
#define XGMTIME(c, t) gmtime((c))
#elif defined(ATMEL_AWS_WOLFSSL)
#include <time.h>
#define XTIME(t1) atmel_time((t1))
#define XGMTIME(c, t) atmel_gmtime((c))
#define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t))
#define USE_WOLF_VALIDDATE
#elif defined(USER_TIME)
/* user time, and gmtime compatible functions, there is a gmtime
implementation here that WINCE uses, so really just need some ticks
since the EPOCH
*/
#define WOLFSSL_GMTIME
#define USE_WOLF_TM
#define USE_WOLF_TIME_T
#elif defined(TIME_OVERRIDES)
/* user would like to override time() and gmtime() functionality */
#ifndef HAVE_TIME_T_TYPE
#define USE_WOLF_TIME_T
#endif
#ifndef HAVE_TM_TYPE
#define USE_WOLF_TM
#endif
#define NEED_TMP_TIME
#elif defined(IDIRECT_DEV_TIME)
/*Gets the timestamp from cloak software owned by VT iDirect
in place of time() from <time.h> */
#include <time.h>
#define XTIME(t1) idirect_time((t1))
#define XGMTIME(c, t) gmtime((c))
#elif defined(_WIN32_WCE)
#include <windows.h>
#define XTIME(t1) windows_time((t1))
#define WOLFSSL_GMTIME
#else
#include <time.h>
#define XTIME(t1) atmel_time((t1))
#define XGMTIME(c, t) atmel_gmtime((c))
#define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t))
#define USE_WOLF_VALIDDATE
/* default */
/* uses complete <time.h> facility */
#include <time.h>
#endif
/* Map default time functions */
#if !defined(XTIME) && !defined(TIME_OVERRIDES) && !defined(USER_TIME)
#define XTIME(tl) time((tl))
#endif
#if !defined(XGMTIME) && !defined(TIME_OVERRIDES)
#if defined(WOLFSSL_GMTIME) || !defined(HAVE_GMTIME_R)
#define XGMTIME(c, t) gmtime((c))
#else
#define XGMTIME(c, t) gmtime_r((c), (t))
#define NEED_TMP_TIME
#endif
#endif
#if !defined(XVALIDATE_DATE) && !defined(HAVE_VALIDATE_DATE)
#define USE_WOLF_VALIDDATE
#define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t))
#endif
/* wolf struct tm and time_t */
#if defined(USE_WOLF_TM)
struct tm {
int tm_sec; /* seconds after the minute [0-60] */
int tm_min; /* minutes after the hour [0-59] */
int tm_hour; /* hours since midnight [0-23] */
int tm_mday; /* day of the month [1-31] */
int tm_mon; /* months since January [0-11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday [0-6] */
int tm_yday; /* days since January 1 [0-365] */
int tm_isdst; /* Daylight Savings Time flag */
long tm_gmtoff; /* offset from CUT in seconds */
char *tm_zone; /* timezone abbreviation */
};
#endif /* USE_WOLF_TM */
#if defined(USE_WOLF_TIME_T)
typedef long time_t;
#endif
/* forward declarations */
#if defined(USER_TIME)
struct tm* gmtime(const time_t* timer);
extern time_t XTIME(time_t * timer);
#ifdef STACK_TRAP
/* for stack trap tracking, don't call os gmtime on OS X/linux,
uses a lot of stack spce */
extern time_t time(time_t * timer);
#define XTIME(tl) time((tl))
#endif /* STACK_TRAP */
#elif defined(TIME_OVERRIDES)
extern time_t XTIME(time_t * timer);
extern struct tm* XGMTIME(const time_t* timer, struct tm* tmp);
#endif
#if defined(_WIN32_WCE)
time_t windows_time(time_t* timer)
{
SYSTEMTIME sysTime;
FILETIME fTime;
ULARGE_INTEGER intTime;
time_t localTime;
if (timer == NULL)
timer = &localTime;
GetSystemTime(&sysTime);
SystemTimeToFileTime(&sysTime, &fTime);
XMEMCPY(&intTime, &fTime, sizeof(FILETIME));
/* subtract EPOCH */
intTime.QuadPart -= 0x19db1ded53e8000;
/* to secs */
intTime.QuadPart /= 10000000;
*timer = (time_t)intTime.QuadPart;
return *timer;
}
#endif /* _WIN32_WCE */
#if defined(WOLFSSL_GMTIME)
struct tm* gmtime(const time_t* timer)
{
#define YEAR0 1900
#define EPOCH_YEAR 1970
#define SECS_DAY (24L * 60L * 60L)
#define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) %400)))
#define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
static const int _ytab[2][12] =
{
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
static struct tm st_time;
struct tm* ret = &st_time;
time_t secs = *timer;
unsigned long dayclock, dayno;
int year = EPOCH_YEAR;
dayclock = (unsigned long)secs % SECS_DAY;
dayno = (unsigned long)secs / SECS_DAY;
ret->tm_sec = (int) dayclock % 60;
ret->tm_min = (int)(dayclock % 3600) / 60;
ret->tm_hour = (int) dayclock / 3600;
ret->tm_wday = (int) (dayno + 4) % 7; /* day 0 a Thursday */
while(dayno >= (unsigned long)YEARSIZE(year)) {
dayno -= YEARSIZE(year);
year++;
}
ret->tm_year = year - YEAR0;
ret->tm_yday = (int)dayno;
ret->tm_mon = 0;
while(dayno >= (unsigned long)_ytab[LEAPYEAR(year)][ret->tm_mon]) {
dayno -= _ytab[LEAPYEAR(year)][ret->tm_mon];
ret->tm_mon++;
}
ret->tm_mday = (int)++dayno;
ret->tm_isdst = 0;
return ret;
}
#endif /* WOLFSSL_GMTIME */
#if defined(HAVE_RTP_SYS)
#define YEAR0 1900
struct tm* rtpsys_gmtime(const time_t* timer) /* has a gmtime() but hangs */
{
static struct tm st_time;
struct tm* ret = &st_time;
DC_RTC_CALENDAR cal;
dc_rtc_time_get(&cal, TRUE);
ret->tm_year = cal.year - YEAR0; /* gm starts at 1900 */
ret->tm_mon = cal.month - 1; /* gm starts at 0 */
ret->tm_mday = cal.day;
ret->tm_hour = cal.hour;
ret->tm_min = cal.minute;
ret->tm_sec = cal.second;
return ret;
}
#endif /* HAVE_RTP_SYS */
#if defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
/*
* time() is just a stub in Microchip libraries. We need our own
* implementation. Use SNTP client to get seconds since epoch.
*/
time_t pic32_time(time_t* timer)
{
#ifdef MICROCHIP_TCPIP_V5
DWORD sec = 0;
#else
uint32_t sec = 0;
#endif
time_t localTime;
if (timer == NULL)
timer = &localTime;
#ifdef MICROCHIP_MPLAB_HARMONY
sec = TCPIP_SNTP_UTCSecondsGet();
#else
sec = SNTPGetUTCSeconds();
#endif
*timer = (time_t) sec;
return *timer;
}
#endif /* MICROCHIP_TCPIP */
#if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
time_t mqx_time(time_t* timer)
{
time_t localTime;
TIME_STRUCT time_s;
if (timer == NULL)
timer = &localTime;
_time_get(&time_s);
*timer = (time_t) time_s.SECONDS;
return *timer;
}
#endif /* FREESCALE_MQX */
#if defined(FREESCALE_KSDK_BM) || defined(FREESCALE_FREE_RTOS)
#include "fsl_pit_driver.h"
time_t ksdk_time(time_t* timer)
{
time_t localTime;
if (timer == NULL)
timer = &localTime;
*timer = (PIT_DRV_ReadTimerUs(PIT_INSTANCE, PIT_CHANNEL)) / 1000000;
return *timer;
}
#endif /* FREESCALE_KSDK_BM */
/*
time_t atmel_time(time_t* timer)
{
return 1472449263;
}
struct tm* atmel_gmtime(const time_t* timer)
{
static struct tm st_time;
struct tm* ret = &st_time;
ret->tm_year = 116;
ret->tm_mon = 7;
ret->tm_mday = 28;
ret->tm_hour = 1;
ret->tm_min = 1;
ret->tm_sec = 1;
return ret;
}
*/
#ifdef ATMEL_AWS_WOLFSSL
#include "aws_net_interface.h"
#define YEAR0 1900
time_t atmel_time(time_t* timer)
{
time_t sec = 0;
sec = (time_t) aws_net_get_ntp_seconds();
if (timer != NULL)
timer = &sec;
return sec;
}
t_time_date* atmel_gmtime(const time_t* timer)
{
static t_time_date st_time, cal;
t_time_date* ret = &st_time;
aws_net_get_current_time(&cal);
ret->tm_year = cal.tm_year - YEAR0; /* gm starts at 1900 */
ret->tm_mon = cal.tm_mon - 1; /* gm starts at 0 */
ret->tm_mday = cal.tm_mday;
ret->tm_hour = cal.tm_hour;
ret->tm_min = cal.tm_min;
ret->tm_sec = cal.tm_sec;
return ret;
}
#endif /* ATMEL_AWS_WOLFSSL */
#if defined(WOLFSSL_TIRTOS)
time_t XTIME(time_t * timer)
{
time_t sec = 0;
sec = (time_t) Seconds_get();
if (timer != NULL)
*timer = sec;
return sec;
}
#endif /* WOLFSSL_TIRTOS */
static INLINE word32 btoi(byte b)
{
return b - 0x30;
}
/* two byte date/time, add to value */
static INLINE void GetTime(int* value, const byte* date, int* idx)
{
int i = *idx;
*value += btoi(date[i++]) * 10;
*value += btoi(date[i++]);
*idx = i;
}
#if defined(MICRIUM)
CPU_INT32S NetSecure_ValidateDateHandler(CPU_INT08U *date, CPU_INT08U format,
CPU_INT08U dateType)
{
CPU_BOOLEAN rtn_code;
CPU_INT32S i;
CPU_INT32S val;
CPU_INT16U year;
CPU_INT08U month;
CPU_INT16U day;
CPU_INT08U hour;
CPU_INT08U min;
CPU_INT08U sec;
i = 0;
year = 0u;
if (format == ASN_UTC_TIME) {
if (btoi(date[0]) >= 5)
year = 1900;
else
year = 2000;
}
else { /* format == GENERALIZED_TIME */
year += btoi(date[i++]) * 1000;
year += btoi(date[i++]) * 100;
}
val = year;
GetTime(&val, date, &i);
year = (CPU_INT16U)val;
val = 0;
GetTime(&val, date, &i);
month = (CPU_INT08U)val;
val = 0;
GetTime(&val, date, &i);
day = (CPU_INT16U)val;
val = 0;
GetTime(&val, date, &i);
hour = (CPU_INT08U)val;
val = 0;
GetTime(&val, date, &i);
min = (CPU_INT08U)val;
val = 0;
GetTime(&val, date, &i);
sec = (CPU_INT08U)val;
return NetSecure_ValidateDate(year, month, day, hour, min, sec, dateType);
}
#endif /* MICRIUM */
#if defined(IDIRECT_DEV_TIME)
extern time_t getTimestamp();
time_t idirect_time(time_t * timer)
{
time_t sec = getTimestamp();
if (timer != NULL)
*timer = sec;
return sec;
}
#endif /* IDIRECT_DEV_TIME */
#endif /* !NO_ASN_TIME */
WOLFSSL_LOCAL int GetLength(const byte* input, word32* inOutIdx, int* len,
word32 maxIdx)
{
int length = 0;
word32 i = *inOutIdx;
byte b;
*len = 0; /* default length */
if ( (i+1) > maxIdx) { /* for first read */
WOLFSSL_MSG("GetLength bad index on input");
return BUFFER_E;
}
b = input[i++];
if (b >= ASN_LONG_LENGTH) {
word32 bytes = b & 0x7F;
if ( (i+bytes) > maxIdx) { /* for reading bytes */
WOLFSSL_MSG("GetLength bad long length");
return BUFFER_E;
}
while (bytes--) {
b = input[i++];
length = (length << 8) | b;
}
}
else
length = b;
if ( (i+length) > maxIdx) { /* for user of length */
WOLFSSL_MSG("GetLength value exceeds buffer length");
return BUFFER_E;
}
*inOutIdx = i;
if (length > 0)
*len = length;
return length;
}
WOLFSSL_LOCAL int GetSequence(const byte* input, word32* inOutIdx, int* len,
word32 maxIdx)
{
int length = -1;
word32 idx = *inOutIdx;
if (input[idx++] != (ASN_SEQUENCE | ASN_CONSTRUCTED) ||
GetLength(input, &idx, &length, maxIdx) < 0)
return ASN_PARSE_E;
*len = length;
*inOutIdx = idx;
return length;
}
WOLFSSL_LOCAL int GetSet(const byte* input, word32* inOutIdx, int* len,
word32 maxIdx)
{
int length = -1;
word32 idx = *inOutIdx;
if (input[idx++] != (ASN_SET | ASN_CONSTRUCTED) ||
GetLength(input, &idx, &length, maxIdx) < 0)
return ASN_PARSE_E;
*len = length;
*inOutIdx = idx;
return length;
}
/* Windows header clash for WinCE using GetVersion */
WOLFSSL_LOCAL int GetMyVersion(const byte* input, word32* inOutIdx,
int* version)
{
word32 idx = *inOutIdx;
WOLFSSL_ENTER("GetMyVersion");
if (input[idx++] != ASN_INTEGER)
return ASN_PARSE_E;
if (input[idx++] != 0x01)
return ASN_VERSION_E;
*version = input[idx++];
*inOutIdx = idx;
return *version;
}
#ifndef NO_PWDBASED
/* Get small count integer, 32 bits or less */
static int GetShortInt(const byte* input, word32* inOutIdx, int* number)
{
word32 idx = *inOutIdx;
word32 len;
*number = 0;
if (input[idx++] != ASN_INTEGER)
return ASN_PARSE_E;
len = input[idx++];
if (len > 4)
return ASN_PARSE_E;
while (len--) {
*number = *number << 8 | input[idx++];
}
*inOutIdx = idx;
return *number;
}
#endif /* !NO_PWDBASED */
#ifndef NO_ASN_TIME
/* May not have one, not an error */
static int GetExplicitVersion(const byte* input, word32* inOutIdx, int* version)
{
word32 idx = *inOutIdx;
WOLFSSL_ENTER("GetExplicitVersion");
if (input[idx++] == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED)) {
*inOutIdx = ++idx; /* eat header */
return GetMyVersion(input, inOutIdx, version);
}
/* go back as is */
*version = 0;
return 0;
}
#endif /* !NO_ASN_TIME */
WOLFSSL_LOCAL int GetInt(mp_int* mpi, const byte* input, word32* inOutIdx,
word32 maxIdx)
{
word32 i = *inOutIdx;
byte b = input[i++];
int length;
if (b != ASN_INTEGER)
return ASN_PARSE_E;
if (GetLength(input, &i, &length, maxIdx) < 0)
return ASN_PARSE_E;
if ( (b = input[i++]) == 0x00)
length--;
else
i--;
if (mp_init(mpi) != MP_OKAY)
return MP_INIT_E;
if (mp_read_unsigned_bin(mpi, (byte*)input + i, length) != 0) {
mp_clear(mpi);
return ASN_GETINT_E;
}
*inOutIdx = i + length;
return 0;
}
/* hashType */
static const byte hashMd2hOid[] = {42, 134, 72, 134, 247, 13, 2, 2};
static const byte hashMd5hOid[] = {42, 134, 72, 134, 247, 13, 2, 5};
static const byte hashSha1hOid[] = {43, 14, 3, 2, 26};
static const byte hashSha256hOid[] = {96, 134, 72, 1, 101, 3, 4, 2, 1};
static const byte hashSha384hOid[] = {96, 134, 72, 1, 101, 3, 4, 2, 2};
static const byte hashSha512hOid[] = {96, 134, 72, 1, 101, 3, 4, 2, 3};
/* sigType */
#ifndef NO_DSA
static const byte sigSha1wDsaOid[] = {42, 134, 72, 206, 56, 4, 3};
#endif /* NO_DSA */
#ifndef NO_RSA
static const byte sigMd2wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1, 2};
static const byte sigMd5wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1, 4};
static const byte sigSha1wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1, 5};
static const byte sigSha256wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1,11};
static const byte sigSha384wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1,12};
static const byte sigSha512wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1,13};
#endif /* NO_RSA */
#ifdef HAVE_ECC
static const byte sigSha1wEcdsaOid[] = {42, 134, 72, 206, 61, 4, 1};
static const byte sigSha256wEcdsaOid[] = {42, 134, 72, 206, 61, 4, 3, 2};
static const byte sigSha384wEcdsaOid[] = {42, 134, 72, 206, 61, 4, 3, 3};
static const byte sigSha512wEcdsaOid[] = {42, 134, 72, 206, 61, 4, 3, 4};
#endif /* HAVE_ECC */
/* keyType */
#ifndef NO_DSA
static const byte keyDsaOid[] = {42, 134, 72, 206, 56, 4, 1};
#endif /* NO_DSA */
#ifndef NO_RSA
static const byte keyRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1, 1};
#endif /* NO_RSA */
#ifdef HAVE_NTRU
static const byte keyNtruOid[] = {43, 6, 1, 4, 1, 193, 22, 1, 1, 1, 1};
#endif /* HAVE_NTRU */
#ifdef HAVE_ECC
static const byte keyEcdsaOid[] = {42, 134, 72, 206, 61, 2, 1};
#endif /* HAVE_ECC */
/* curveType */
#ifdef HAVE_ECC
#if defined(HAVE_ALL_CURVES) || defined(HAVE_ECC192)
static const byte curve192v1Oid[] = {42, 134, 72, 206, 61, 3, 1, 1};
#endif /* HAVE_ALL_CURVES || HAVE_ECC192 */
#if defined(HAVE_ALL_CURVES) || !defined(NO_ECC256)
static const byte curve256v1Oid[] = {42, 134, 72, 206, 61, 3, 1, 7};
#endif /* HAVE_ALL_CURVES || HAVE_ECC256 */
#if defined(HAVE_ALL_CURVES) || defined(HAVE_ECC160)
static const byte curve160r1Oid[] = {43, 129, 4, 0, 2};
#endif /* HAVE_ALL_CURVES || HAVE_ECC160 */
#if defined(HAVE_ALL_CURVES) || defined(HAVE_ECC224)
static const byte curve224r1Oid[] = {43, 129, 4, 0, 33};
#endif /* HAVE_ALL_CURVES || HAVE_ECC224 */
#if defined(HAVE_ALL_CURVES) || defined(HAVE_ECC384)
static const byte curve384r1Oid[] = {43, 129, 4, 0, 34};
#endif /* HAVE_ALL_CURVES || HAVE_ECC384 */
#if defined(HAVE_ALL_CURVES) || defined(HAVE_ECC521)
static const byte curve521r1Oid[] = {43, 129, 4, 0, 35};
#endif /* HAVE_ALL_CURVES || HAVE_ECC521 */
#endif /* HAVE_ECC */
/* blkType */
static const byte blkDesCbcOid[] = {43, 14, 3, 2, 7};
static const byte blkDes3CbcOid[] = {42, 134, 72, 134, 247, 13, 3, 7};
/* ocspType */
#ifdef HAVE_OCSP
static const byte ocspBasicOid[] = {43, 6, 1, 5, 5, 7, 48, 1, 1};
static const byte ocspNonceOid[] = {43, 6, 1, 5, 5, 7, 48, 1, 2};
#endif /* HAVE_OCSP */
/* certExtType */
static const byte extBasicCaOid[] = {85, 29, 19};
static const byte extAltNamesOid[] = {85, 29, 17};
static const byte extCrlDistOid[] = {85, 29, 31};
static const byte extAuthInfoOid[] = {43, 6, 1, 5, 5, 7, 1, 1};
static const byte extAuthKeyOid[] = {85, 29, 35};
static const byte extSubjKeyOid[] = {85, 29, 14};
static const byte extCertPolicyOid[] = {85, 29, 32};
static const byte extKeyUsageOid[] = {85, 29, 15};
static const byte extInhibitAnyOid[] = {85, 29, 54};
static const byte extExtKeyUsageOid[] = {85, 29, 37};
static const byte extNameConsOid[] = {85, 29, 30};
/* certAuthInfoType */
static const byte extAuthInfoOcspOid[] = {43, 6, 1, 5, 5, 7, 48, 1};
static const byte extAuthInfoCaIssuerOid[] = {43, 6, 1, 5, 5, 7, 48, 2};
/* certPolicyType */
static const byte extCertPolicyAnyOid[] = {85, 29, 32, 0};
/* certKeyUseType */
static const byte extAltNamesHwNameOid[] = {43, 6, 1, 5, 5, 7, 8, 4};
/* certKeyUseType */
static const byte extExtKeyUsageAnyOid[] = {85, 29, 37, 0};
static const byte extExtKeyUsageServerAuthOid[] = {43, 6, 1, 5, 5, 7, 3, 1};
static const byte extExtKeyUsageClientAuthOid[] = {43, 6, 1, 5, 5, 7, 3, 2};
static const byte extExtKeyUsageOcspSignOid[] = {43, 6, 1, 5, 5, 7, 3, 9};
/* kdfType */
static const byte pbkdf2Oid[] = {42, 134, 72, 134, 247, 13, 1, 5, 12};
static const byte* OidFromId(word32 id, word32 type, word32* oidSz)
{
const byte* oid = NULL;
*oidSz = 0;
switch (type) {
case hashType:
switch (id) {
case MD2h:
oid = hashMd2hOid;
*oidSz = sizeof(hashMd2hOid);
break;
case MD5h:
oid = hashMd5hOid;
*oidSz = sizeof(hashMd5hOid);
break;
case SHAh:
oid = hashSha1hOid;
*oidSz = sizeof(hashSha1hOid);
break;
case SHA256h:
oid = hashSha256hOid;
*oidSz = sizeof(hashSha256hOid);
break;
case SHA384h:
oid = hashSha384hOid;
*oidSz = sizeof(hashSha384hOid);
break;
case SHA512h:
oid = hashSha512hOid;
*oidSz = sizeof(hashSha512hOid);
break;
}
break;
case sigType:
switch (id) {
#ifndef NO_DSA
case CTC_SHAwDSA:
oid = sigSha1wDsaOid;
*oidSz = sizeof(sigSha1wDsaOid);
break;
#endif /* NO_DSA */
#ifndef NO_RSA
case CTC_MD2wRSA:
oid = sigMd2wRsaOid;
*oidSz = sizeof(sigMd2wRsaOid);
break;
case CTC_MD5wRSA:
oid = sigMd5wRsaOid;
*oidSz = sizeof(sigMd5wRsaOid);
break;
case CTC_SHAwRSA:
oid = sigSha1wRsaOid;
*oidSz = sizeof(sigSha1wRsaOid);
break;
case CTC_SHA256wRSA:
oid = sigSha256wRsaOid;
*oidSz = sizeof(sigSha256wRsaOid);
break;
case CTC_SHA384wRSA:
oid = sigSha384wRsaOid;
*oidSz = sizeof(sigSha384wRsaOid);
break;
case CTC_SHA512wRSA:
oid = sigSha512wRsaOid;
*oidSz = sizeof(sigSha512wRsaOid);
break;
#endif /* NO_RSA */
#ifdef HAVE_ECC
case CTC_SHAwECDSA:
oid = sigSha1wEcdsaOid;
*oidSz = sizeof(sigSha1wEcdsaOid);
break;
case CTC_SHA256wECDSA:
oid = sigSha256wEcdsaOid;
*oidSz = sizeof(sigSha256wEcdsaOid);
break;
case CTC_SHA384wECDSA:
oid = sigSha384wEcdsaOid;
*oidSz = sizeof(sigSha384wEcdsaOid);
break;
case CTC_SHA512wECDSA:
oid = sigSha512wEcdsaOid;
*oidSz = sizeof(sigSha512wEcdsaOid);
break;
#endif /* HAVE_ECC */
default:
break;
}
break;
case keyType:
switch (id) {
#ifndef NO_DSA
case DSAk:
oid = keyDsaOid;
*oidSz = sizeof(keyDsaOid);
break;
#endif /* NO_DSA */
#ifndef NO_RSA
case RSAk:
oid = keyRsaOid;
*oidSz = sizeof(keyRsaOid);
break;
#endif /* NO_RSA */
#ifdef HAVE_NTRU
case NTRUk:
oid = keyNtruOid;
*oidSz = sizeof(keyNtruOid);
break;
#endif /* HAVE_NTRU */
#ifdef HAVE_ECC
case ECDSAk:
oid = keyEcdsaOid;
*oidSz = sizeof(keyEcdsaOid);
break;
#endif /* HAVE_ECC */
default:
break;
}
break;
#ifdef HAVE_ECC
case curveType:
switch (id) {
#if defined(HAVE_ALL_CURVES) || !defined(NO_ECC256)
case ECC_256R1:
oid = curve256v1Oid;
*oidSz = sizeof(curve256v1Oid);
break;
#endif /* HAVE_ALL_CURVES || HAVE_ECC256 */
#if defined(HAVE_ALL_CURVES) || defined(HAVE_ECC384)
case ECC_384R1:
oid = curve384r1Oid;
*oidSz = sizeof(curve384r1Oid);
break;
#endif /* HAVE_ALL_CURVES || HAVE_ECC384 */
#if defined(HAVE_ALL_CURVES) || defined(HAVE_ECC521)
case ECC_521R1:
oid = curve521r1Oid;
*oidSz = sizeof(curve521r1Oid);
break;
#endif /* HAVE_ALL_CURVES || HAVE_ECC521 */
#if defined(HAVE_ALL_CURVES) || defined(HAVE_ECC160)
case ECC_160R1:
oid = curve160r1Oid;