forked from wolfSSL/wolfssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasn_orig.c
More file actions
9682 lines (8324 loc) · 291 KB
/
asn_orig.c
File metadata and controls
9682 lines (8324 loc) · 291 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_orig.c
*
* Copyright (C) 2006-2026 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 3 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
*/
/*
* Original (non-template) ASN.1 implementations.
* This file is included from asn.c when building without WOLFSSL_ASN_TEMPLATE.
* It must not be compiled as a separate translation unit.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#ifndef WOLFSSL_ASN_ORIG_INCLUDED
#ifndef WOLFSSL_IGNORE_FILE_WARN
#warning asn_orig.c does not need to be compiled separately from asn.c
#endif
#else
/* Forward declarations for static functions defined later in this file. */
static int SkipObjectId(const byte* input, word32* inOutIdx, word32 maxIdx);
#ifndef NO_DSA
static WC_INLINE void FreeTmpDsas(byte** tmps, void* heap, int ints);
#endif
#ifndef NO_CERTS
static int GetCertHeader(DecodedCert* cert);
static int GetDate(DecodedCert* cert, int dateType, int verify, int maxIdx);
static int GetValidity(DecodedCert* cert, int verify, int maxIdx);
#endif
static word32 SetOctetString8Bit(word32 len, byte* output);
static word32 SetDigest(const byte* digest, word32 digSz, byte* output);
#ifndef NO_CERTS
static void AddAltName(DecodedCert* cert, DNS_entry* dnsEntry);
#if defined(WOLFSSL_SEP)
static int DecodeSepHwAltName(DecodedCert* cert, const byte* input, word32* idxIn, word32 sz);
#endif
static int DecodeConstructedOtherName(DecodedCert* cert, const byte* input, word32* idx, word32 sz, int oid);
#ifdef WOLFSSL_CERT_GEN
#ifdef WOLFSSL_CERT_REQ
static word32 SetPrintableString(word32 len, byte* output);
static word32 SetUTF8String(word32 len, byte* output);
#endif
static int CopyValidity(byte* output, Cert* cert);
static int SetExtensions(byte* out, word32 outSz, int *IdxInOut, const byte* ext, int extSz);
static int SetExtensionsHeader(byte* out, word32 outSz, word32 extSz);
static int SetCaWithPathLen(byte* out, word32 outSz, byte pathLen);
static int SetCaEx(byte* out, word32 outSz, byte isCa);
static int SetCa(byte* out, word32 outSz);
static int SetBC(byte* out, word32 outSz);
#ifdef WOLFSSL_CERT_EXT
static int SetOidValue(byte* out, word32 outSz, const byte *oid, word32 oidSz, byte *in, word32 inSz);
static int SetSKID(byte* output, word32 outSz, const byte *input, word32 length);
static int SetAKID(byte* output, word32 outSz, byte *input, word32 length, byte rawAkid);
static int SetKeyUsage(byte* output, word32 outSz, word16 input);
static int SetOjectIdValue(byte* output, word32 outSz, word32* idx, const byte* oid, word32 oidSz);
#ifndef IGNORE_NETSCAPE_CERT_TYPE
static int SetNsCertType(Cert* cert, byte* output, word32 outSz, byte input);
#endif
static int SetCRLInfo(Cert* cert, byte* output, word32 outSz, byte* input, int inSz);
#endif
#ifdef WOLFSSL_ALT_NAMES
static int SetAltNames(byte *output, word32 outSz, const byte *input, word32 length, int critical);
#endif
#ifdef WOLFSSL_CERT_REQ
static word32 SetReqAttribSingle(byte* output, word32* idx, char* attr, word32 attrSz, const byte* oid, word32 oidSz, byte printable, word32 extSz);
static int SetReqAttrib(byte* output, Cert* cert, word32 extSz);
#ifdef WOLFSSL_CUSTOM_OID
static int SetCustomObjectId(Cert* cert, byte* output, word32 outSz, CertOidField* custom);
#endif
#endif
#endif
#endif
#if defined(HAVE_ECC) || !defined(NO_DSA)
static word32 is_leading_bit_set(const byte* input, word32 sz);
static word32 trim_leading_zeros(const byte** input, word32 sz);
#endif
#ifdef HAVE_ECC
#ifdef WOLFSSL_CUSTOM_CURVES
static int ASNToHexString(const byte* input, word32* inOutIdx, char** out, word32 inSz, void* heap, int heapType);
static int EccKeyParamCopy(char** dst, char* src, void* heap);
#endif
#endif
#if (defined(HAVE_OCSP) || defined(HAVE_CRL)) && !defined(WOLFCRYPT_ONLY)
static int GetBasicDate(const byte* source, word32* idx, byte* date, byte* format, int maxIdx);
#endif
#if defined(HAVE_OCSP) && !defined(WOLFCRYPT_ONLY)
static int GetEnumerated(const byte* input, word32* inOutIdx, int *value, int sz);
#endif
int GetObjectId(const byte* input, word32* inOutIdx, word32* oid,
word32 oidType, word32 maxIdx)
{
int ret, length;
WOLFSSL_ENTER("GetObjectId");
ret = GetASNObjectId(input, inOutIdx, &length, maxIdx);
if (ret != 0)
return ret;
return GetOID(input, inOutIdx, oid, oidType, length);
}
static int SkipObjectId(const byte* input, word32* inOutIdx, word32 maxIdx)
{
word32 idx = *inOutIdx;
int length;
int ret;
ret = GetASNObjectId(input, &idx, &length, maxIdx);
if (ret != 0)
return ret;
idx += (word32)length;
*inOutIdx = idx;
return 0;
}
static int GetAlgoIdImpl(const byte* input, word32* inOutIdx, word32* oid,
word32 oidType, word32 maxIdx, byte *absentParams)
{
int length;
word32 idx = *inOutIdx;
int ret;
*oid = 0;
WOLFSSL_ENTER("GetAlgoId");
if (GetSequence(input, &idx, &length, maxIdx) < 0)
return ASN_PARSE_E;
if (GetObjectId(input, &idx, oid, oidType, maxIdx) < 0)
return ASN_OBJECT_ID_E;
/* could have NULL tag and 0 terminator, but may not */
if (idx < maxIdx) {
word32 localIdx = idx; /*use localIdx to not advance when checking tag*/
byte tag;
if (GetASNTag(input, &localIdx, &tag, maxIdx) == 0) {
if (tag == ASN_TAG_NULL) {
ret = GetASNNull(input, &idx, maxIdx);
if (ret != 0)
return ret;
if (absentParams != NULL) {
*absentParams = FALSE;
}
}
}
}
*inOutIdx = idx;
return 0;
}
#ifndef NO_RSA
static int _RsaPrivateKeyDecode(const byte* input, word32* inOutIdx,
RsaKey* key, int* keySz, word32 inSz)
{
int version, length;
word32 algId = 0;
int i;
if (inOutIdx == NULL || input == NULL || (key == NULL && keySz == NULL)) {
return BAD_FUNC_ARG;
}
/* if has pkcs8 header skip it */
if (ToTraditionalInline_ex(input, inOutIdx, inSz, &algId) < 0) {
/* ignore error, did not have pkcs8 header */
}
if (GetSequence(input, inOutIdx, &length, inSz) < 0)
return ASN_PARSE_E;
if (GetMyVersion(input, inOutIdx, &version, inSz) < 0)
return ASN_PARSE_E;
if (key == NULL) {
/* Modulus */
if (GetASNInt(input, inOutIdx, keySz, inSz) < 0) {
return ASN_PARSE_E;
}
*inOutIdx += (word32)*keySz;
for (i = 1; i < RSA_INTS; i++) {
if (SkipInt(input, inOutIdx, inSz) < 0) {
return ASN_RSA_KEY_E;
}
}
}
else {
key->type = RSA_PRIVATE;
#ifdef WOLFSSL_CHECK_MEM_ZERO
mp_memzero_add("Decode RSA key d", &key->d);
mp_memzero_add("Decode RSA key p", &key->p);
mp_memzero_add("Decode RSA key q", &key->q);
#if (defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
!defined(RSA_LOW_MEM)) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
mp_memzero_add("Decode RSA key dP", &key->dP);
mp_memzero_add("Decode RSA key dQ", &key->dQ);
mp_memzero_add("Decode RSA key u", &key->u);
#endif
#endif
/* Extract all public fields. */
for (i = 0; i < RSA_INT_CNT; i++) {
if (GetInt(GetRsaInt(key, i), input, inOutIdx, inSz) < 0) {
for (i--; i >= 0; i--) {
mp_clear(GetRsaInt(key, i));
}
return ASN_RSA_KEY_E;
}
}
#if RSA_INT_CNT != RSA_MAX_INT_CNT
for (; i < RSA_MAX_INT_CNT; i++) {
if (SkipInt(input, inOutIdx, inSz) < 0) {
for (i = RSA_INT_CNT - 1; i >= 0; i--) {
mp_clear(GetRsaInt(key, i));
}
return ASN_RSA_KEY_E;
}
}
#endif
#if defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_CRYPTOCELL)
if (wc_InitRsaHw(key) != 0) {
return BAD_STATE_E;
}
#endif
}
return 0;
}
#endif
int ToTraditionalInline_ex2(const byte* input, word32* inOutIdx, word32 sz,
word32* algId, word32* eccOid)
{
word32 idx;
int version, length;
int ret;
byte tag;
if (input == NULL || inOutIdx == NULL)
return BAD_FUNC_ARG;
idx = *inOutIdx;
if (GetSequence(input, &idx, &length, sz) < 0)
return ASN_PARSE_E;
if (GetMyVersion(input, &idx, &version, sz) < 0)
return ASN_PARSE_E;
if (GetAlgoId(input, &idx, algId, oidKeyType, sz) < 0)
return ASN_PARSE_E;
if (GetASNTag(input, &idx, &tag, sz) < 0)
return ASN_PARSE_E;
idx = idx - 1; /* reset idx after finding tag */
#if defined(WC_RSA_PSS) && !defined(NO_RSA)
if (*algId == RSAPSSk && tag == (ASN_SEQUENCE | ASN_CONSTRUCTED)) {
word32 seqIdx = idx;
int seqLen;
/* Not set when -1. */
enum wc_HashType hash = WC_HASH_TYPE_NONE;
int mgf = -1;
int saltLen = 0;
if (GetSequence(input, &idx, &seqLen, sz) < 0) {
return ASN_PARSE_E;
}
/* Get the private key parameters. */
ret = DecodeRsaPssParams(input + seqIdx,
seqLen + idx - seqIdx, &hash, &mgf, &saltLen);
if (ret != 0) {
return ASN_PARSE_E;
}
/* TODO: store parameters so that usage can be checked. */
idx += seqLen;
}
#endif /* WC_RSA_PSS && !NO_RSA */
if (tag == ASN_OBJECT_ID) {
if ((*algId == ECDSAk) && (eccOid != NULL)) {
if (GetObjectId(input, &idx, eccOid, oidCurveType, sz) < 0)
return ASN_PARSE_E;
}
else {
if (SkipObjectId(input, &idx, sz) < 0)
return ASN_PARSE_E;
}
}
ret = GetOctetString(input, &idx, &length, sz);
if (ret < 0) {
if (ret == WC_NO_ERR_TRACE(BUFFER_E))
return ASN_PARSE_E;
/* Some private keys don't expect an octet string - ignore error. */
WOLFSSL_MSG("Couldn't find Octet string");
length = 0;
}
*inOutIdx = idx;
return length;
}
#if defined(HAVE_PKCS8)
int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz,
int algoID, const byte* curveOID, word32 oidSz)
{
word32 keyIdx = 0;
word32 tmpSz = 0;
word32 sz;
word32 tmpAlgId = 0;
/* If out is NULL then return the max size needed
* + 2 for ASN_OBJECT_ID and ASN_OCTET_STRING tags */
if (out == NULL && outSz != NULL) {
*outSz = keySz + MAX_SEQ_SZ + MAX_VERSION_SZ + MAX_ALGO_SZ
+ MAX_LENGTH_SZ + MAX_LENGTH_SZ + 2;
if (curveOID != NULL)
*outSz += oidSz + MAX_LENGTH_SZ + 1;
WOLFSSL_MSG("Checking size of PKCS8");
return WC_NO_ERR_TRACE(LENGTH_ONLY_E);
}
WOLFSSL_ENTER("wc_CreatePKCS8Key");
if (key == NULL || out == NULL || outSz == NULL) {
return BAD_FUNC_ARG;
}
/* check the buffer has enough room for largest possible size */
if (curveOID != NULL) {
sz = keySz + MAX_SEQ_SZ + MAX_VERSION_SZ + MAX_ALGO_SZ + MAX_LENGTH_SZ +
MAX_LENGTH_SZ + 3 + oidSz + MAX_LENGTH_SZ;
if ((keySz > sz) || (oidSz > sz) || (*outSz < sz))
return BUFFER_E;
}
else {
oidSz = 0; /* with no curveOID oid size must be 0 */
sz= keySz + MAX_SEQ_SZ + MAX_VERSION_SZ + MAX_ALGO_SZ + MAX_LENGTH_SZ +
MAX_LENGTH_SZ + 2;
if ((keySz > sz) || (*outSz < sz))
return BUFFER_E;
}
/* sanity check: make sure the key doesn't already have a PKCS 8 header */
if (ToTraditionalInline_ex(key, &keyIdx, keySz, &tmpAlgId) >= 0) {
(void)tmpAlgId;
return ASN_PARSE_E;
}
/* PrivateKeyInfo ::= SEQUENCE */
keyIdx = MAX_SEQ_SZ; /* save room for sequence */
/* version Version
* no header information just INTEGER */
sz = (word32)SetMyVersion(PKCS8v0, out + keyIdx, 0);
tmpSz += sz; keyIdx += sz;
/* privateKeyAlgorithm PrivateKeyAlgorithmIdentifier */
sz = 0; /* set sz to 0 and get privateKey oid buffer size needed */
if (curveOID != NULL && oidSz > 0) {
byte buf[MAX_LENGTH_SZ];
sz = SetLength(oidSz, buf);
sz += 1; /* plus one for ASN object id */
}
sz = (word32)SetAlgoID(algoID, out + keyIdx, oidKeyType, (int)(oidSz + sz));
tmpSz += sz; keyIdx += sz;
/* privateKey PrivateKey *
* pkcs8 ecc uses slightly different format. Places curve oid in
* buffer */
if (curveOID != NULL && oidSz > 0) {
sz = (word32)SetObjectId((int)oidSz, out + keyIdx);
keyIdx += sz; tmpSz += sz;
XMEMCPY(out + keyIdx, curveOID, oidSz);
keyIdx += oidSz; tmpSz += oidSz;
}
sz = (word32)SetOctetString(keySz, out + keyIdx);
keyIdx += sz; tmpSz += sz;
XMEMCPY(out + keyIdx, key, keySz);
tmpSz += keySz;
/* attributes optional
* No attributes currently added */
/* rewind and add sequence */
sz = SetSequence(tmpSz, out);
XMEMMOVE(out + sz, out + MAX_SEQ_SZ, tmpSz);
*outSz = tmpSz + sz;
return (int)(tmpSz + sz);
}
#endif
#ifndef NO_PWDBASED
#ifdef HAVE_PKCS8
int DecryptContent(byte* input, word32 sz, const char* password, int passwordSz)
{
word32 inOutIdx = 0, seqEnd, oid, shaOid = 0, seqPkcs5End = sz;
int ret = 0, first, second, length = 0, version, saltSz, id = 0;
int iterations = 0, keySz = 0;
#ifdef WOLFSSL_SMALL_STACK
byte* salt = NULL;
byte* cbcIv = NULL;
#else
byte salt[MAX_SALT_SIZE];
byte cbcIv[MAX_IV_SIZE];
#endif
byte tag;
if (passwordSz < 0) {
WOLFSSL_MSG("Bad password size");
return BAD_FUNC_ARG;
}
if (GetAlgoId(input, &inOutIdx, &oid, oidIgnoreType, sz) < 0) {
ERROR_OUT(ASN_PARSE_E, exit_dc);
}
first = input[inOutIdx - 2]; /* PKCS version always 2nd to last byte */
second = input[inOutIdx - 1]; /* version.algo, algo id last byte */
if (CheckAlgo(first, second, &id, &version, NULL) < 0) {
ERROR_OUT(ASN_INPUT_E, exit_dc); /* Algo ID error */
}
if (version == PKCS5v2) {
if (GetSequence(input, &inOutIdx, &length, sz) < 0) {
ERROR_OUT(ASN_PARSE_E, exit_dc);
}
seqPkcs5End = inOutIdx + length;
if (GetAlgoId(input, &inOutIdx, &oid, oidKdfType, sz) < 0) {
ERROR_OUT(ASN_PARSE_E, exit_dc);
}
if (oid != PBKDF2_OID) {
ERROR_OUT(ASN_PARSE_E, exit_dc);
}
}
if (GetSequence(input, &inOutIdx, &length, sz) <= 0) {
ERROR_OUT(ASN_PARSE_E, exit_dc);
}
/* Find the end of this SEQUENCE so we can check for the OPTIONAL and
* DEFAULT items. */
seqEnd = inOutIdx + (word32)length;
ret = GetOctetString(input, &inOutIdx, &saltSz, seqEnd);
if (ret < 0)
goto exit_dc;
if (saltSz > MAX_SALT_SIZE) {
ERROR_OUT(ASN_PARSE_E, exit_dc);
}
WC_ALLOC_VAR_EX(salt, byte, MAX_SALT_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER,
ERROR_OUT(MEMORY_E,exit_dc));
XMEMCPY(salt, &input[inOutIdx], (size_t)saltSz);
inOutIdx += (word32)saltSz;
if (GetShortInt(input, &inOutIdx, &iterations, seqEnd) < 0) {
ERROR_OUT(ASN_PARSE_E, exit_dc);
}
/* OPTIONAL key length */
if (seqEnd > inOutIdx) {
word32 localIdx = inOutIdx;
if (GetASNTag(input, &localIdx, &tag, seqEnd) < 0) {
ERROR_OUT(ASN_PARSE_E, exit_dc);
}
if (tag == ASN_INTEGER &&
GetShortInt(input, &inOutIdx, &keySz, seqEnd) < 0) {
ERROR_OUT(ASN_PARSE_E, exit_dc);
}
}
/* DEFAULT HMAC is SHA-1 */
if (seqEnd > inOutIdx) {
if (GetAlgoId(input, &inOutIdx, &oid, oidHmacType, seqEnd) < 0) {
ERROR_OUT(ASN_PARSE_E, exit_dc);
}
shaOid = oid;
}
WC_ALLOC_VAR_EX(cbcIv, byte, MAX_IV_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER,
ERROR_OUT(MEMORY_E,exit_dc));
if (version == PKCS5v2) {
/* get encryption algo */
if (GetAlgoId(input, &inOutIdx, &oid, oidBlkType, seqPkcs5End) < 0) {
ERROR_OUT(ASN_PARSE_E, exit_dc);
}
if (CheckAlgoV2((int)oid, &id, NULL) < 0) {
ERROR_OUT(ASN_PARSE_E, exit_dc); /* PKCS v2 algo id error */
}
if (shaOid == 0)
shaOid = oid;
ret = GetOctetString(input, &inOutIdx, &length, seqPkcs5End);
if (ret < 0)
goto exit_dc;
if (length > MAX_IV_SIZE) {
ERROR_OUT(ASN_PARSE_E, exit_dc);
}
XMEMCPY(cbcIv, &input[inOutIdx], (size_t)length);
inOutIdx += (word32)length;
}
if (GetASNTag(input, &inOutIdx, &tag, sz) < 0) {
ERROR_OUT(ASN_PARSE_E, exit_dc);
}
if (tag != (ASN_CONTEXT_SPECIFIC | 0) && tag != ASN_OCTET_STRING) {
ERROR_OUT(ASN_PARSE_E, exit_dc);
}
if (GetLength(input, &inOutIdx, &length, sz) < 0) {
ERROR_OUT(ASN_PARSE_E, exit_dc);
}
ret = wc_CryptKey(password, passwordSz, salt, saltSz, iterations, id,
input + inOutIdx, length, version, cbcIv, 0, (int)shaOid);
exit_dc:
WC_FREE_VAR_EX(salt, NULL, DYNAMIC_TYPE_TMP_BUFFER);
WC_FREE_VAR_EX(cbcIv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (ret == 0) {
XMEMMOVE(input, input + inOutIdx, (size_t)length);
ret = length;
}
return ret;
}
#endif
#ifdef HAVE_PKCS12
int EncryptContent(byte* input, word32 inputSz, byte* out, word32* outSz,
const char* password, int passwordSz, int vPKCS, int vAlgo,
int encAlgId, byte* salt, word32 saltSz, int itt, int hmacOid,
WC_RNG* rng, void* heap)
{
word32 sz;
word32 inOutIdx = 0;
word32 tmpIdx = 0;
word32 totalSz = 0;
word32 seqSz;
word32 innerSz;
int ret;
int version, id = PBE_NONE, blockSz = 0;
#ifdef WOLFSSL_SMALL_STACK
byte* saltTmp = NULL;
byte* cbcIv = NULL;
#else
byte saltTmp[MAX_SALT_SIZE];
byte cbcIv[MAX_IV_SIZE];
#endif
byte seq[MAX_SEQ_SZ];
byte shr[MAX_SHORT_SZ];
word32 maxShr = MAX_SHORT_SZ;
word32 algoSz;
const byte* algoName;
(void)encAlgId;
(void)hmacOid;
(void)heap;
(void)EncryptContentPBES2;
WOLFSSL_ENTER("EncryptContent");
if (CheckAlgo(vPKCS, vAlgo, &id, &version, &blockSz) < 0)
return ASN_INPUT_E; /* Algo ID error */
if (version == PKCS5v2) {
WOLFSSL_MSG("PKCS#5 version 2 not supported yet");
return BAD_FUNC_ARG;
}
if (saltSz > MAX_SALT_SIZE)
return ASN_PARSE_E;
if (outSz == NULL) {
return BAD_FUNC_ARG;
}
/* calculate size */
/* size of constructed string at end */
sz = wc_PkcsPad(NULL, inputSz, (word32)blockSz);
totalSz = ASN_TAG_SZ;
totalSz += SetLength(sz, seq);
totalSz += sz;
/* size of sequence holding object id and sub sequence of salt and itt */
algoName = OidFromId((word32)id, oidPBEType, &algoSz);
if (algoName == NULL) {
WOLFSSL_MSG("Unknown Algorithm");
return 0;
}
innerSz = (word32)SetObjectId((int)algoSz, seq);
innerSz += algoSz;
/* get subsequence of salt and itt */
if (salt == NULL || saltSz == 0) {
sz = 8;
}
else {
sz = saltSz;
}
seqSz = SetOctetString(sz, seq);
seqSz += sz;
tmpIdx = 0;
ret = SetShortInt(shr, &tmpIdx, (word32)itt, maxShr);
if (ret >= 0) {
seqSz += (word32)ret;
}
else {
return ret;
}
innerSz += seqSz + SetSequence(seqSz, seq);
totalSz += innerSz + SetSequence(innerSz, seq);
if (out == NULL) {
*outSz = totalSz;
return WC_NO_ERR_TRACE(LENGTH_ONLY_E);
}
inOutIdx = 0;
if (totalSz > *outSz)
return BUFFER_E;
inOutIdx += SetSequence(innerSz, out + inOutIdx);
inOutIdx += (word32)SetObjectId((int)algoSz, out + inOutIdx);
XMEMCPY(out + inOutIdx, algoName, algoSz);
inOutIdx += algoSz;
inOutIdx += SetSequence(seqSz, out + inOutIdx);
/* create random salt if one not provided */
if (salt == NULL || saltSz == 0) {
saltSz = 8;
WC_ALLOC_VAR_EX(saltTmp, byte, saltSz, heap, DYNAMIC_TYPE_TMP_BUFFER,
return MEMORY_E);
salt = saltTmp;
if ((ret = wc_RNG_GenerateBlock(rng, saltTmp, saltSz)) != 0) {
WOLFSSL_MSG("Error generating random salt");
WC_FREE_VAR_EX(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
}
inOutIdx += SetOctetString(saltSz, out + inOutIdx);
if (saltSz + inOutIdx > *outSz) {
WC_FREE_VAR_EX(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
return BUFFER_E;
}
XMEMCPY(out + inOutIdx, salt, saltSz);
inOutIdx += saltSz;
/* place iteration setting in buffer */
ret = SetShortInt(out, &inOutIdx, (word32)itt, *outSz);
if (ret < 0) {
WC_FREE_VAR_EX(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
if (inOutIdx + 1 > *outSz) {
WC_FREE_VAR_EX(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
return BUFFER_E;
}
out[inOutIdx++] = ASN_CONTEXT_SPECIFIC | 0;
/* get pad size and verify buffer room */
sz = wc_PkcsPad(NULL, inputSz, (word32)blockSz);
if (sz + inOutIdx > *outSz) {
WC_FREE_VAR_EX(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
return BUFFER_E;
}
inOutIdx += SetLength(sz, out + inOutIdx);
/* copy input to output buffer and pad end */
XMEMCPY(out + inOutIdx, input, inputSz);
sz = wc_PkcsPad(out + inOutIdx, inputSz, (word32)blockSz);
#ifdef WOLFSSL_SMALL_STACK
cbcIv = (byte*)XMALLOC(MAX_IV_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER);
if (cbcIv == NULL) {
XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
return MEMORY_E;
}
#endif
/* encrypt */
if ((ret = wc_CryptKey(password, passwordSz, salt, (int)saltSz, itt, id,
out + inOutIdx, (int)sz, version, cbcIv, 1, 0)) < 0) {
WC_FREE_VAR_EX(cbcIv, heap, DYNAMIC_TYPE_TMP_BUFFER);
WC_FREE_VAR_EX(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret; /* encrypt failure */
}
WC_FREE_VAR_EX(cbcIv, heap, DYNAMIC_TYPE_TMP_BUFFER);
WC_FREE_VAR_EX(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
(void)rng;
return (int)(inOutIdx + sz);
}
#endif
#endif
#ifndef NO_RSA
#if defined(WOLFSSL_RENESAS_TSIP_TLS) || defined(WOLFSSL_RENESAS_FSPSM_TLS)
static int RsaPublicKeyDecodeRawIndex(const byte* input, word32* inOutIdx,
word32 inSz, word32* key_n,
word32* key_n_len, word32* key_e,
word32* key_e_len)
{
int ret = 0;
int length = 0;
#if defined(OPENSSL_EXTRA) || defined(RSA_DECODE_EXTRA)
byte b;
#endif
if (input == NULL || inOutIdx == NULL)
return BAD_FUNC_ARG;
if (GetSequence(input, inOutIdx, &length, inSz) < 0)
return ASN_PARSE_E;
#if defined(OPENSSL_EXTRA) || defined(RSA_DECODE_EXTRA)
if ((*inOutIdx + 1) > inSz)
return BUFFER_E;
b = input[*inOutIdx];
if (b != ASN_INTEGER) {
/* not from decoded cert, will have algo id, skip past */
if (GetSequence(input, inOutIdx, &length, inSz) < 0)
return ASN_PARSE_E;
if (SkipObjectId(input, inOutIdx, inSz) < 0)
return ASN_PARSE_E;
/* Option NULL ASN.1 tag */
if (*inOutIdx >= inSz) {
return BUFFER_E;
}
if (input[*inOutIdx] == ASN_TAG_NULL) {
ret = GetASNNull(input, inOutIdx, inSz);
if (ret != 0)
return ret;
}
/* TODO: support RSA PSS */
/* should have bit tag length and seq next */
ret = CheckBitString(input, inOutIdx, NULL, inSz, 1, NULL);
if (ret != 0)
return ret;
if (GetSequence(input, inOutIdx, &length, inSz) < 0)
return ASN_PARSE_E;
}
#endif /* OPENSSL_EXTRA */
/* Get modulus */
ret = GetASNInt(input, inOutIdx, &length, inSz);
*key_n += *inOutIdx;
if (ret < 0) {
return ASN_RSA_KEY_E;
}
if (key_n_len)
*key_n_len = length;
*inOutIdx += length;
/* Get exponent */
ret = GetASNInt(input, inOutIdx, &length, inSz);
*key_e += *inOutIdx;
if (ret < 0) {
return ASN_RSA_KEY_E;
}
if (key_e_len)
*key_e_len = length;
return ret;
}
#endif
int wc_RsaPublicKeyDecode_ex(const byte* input, word32* inOutIdx, word32 inSz,
const byte** n, word32* nSz, const byte** e, word32* eSz)
{
int ret = 0;
int length = 0;
int firstLen = 0;
word32 seqEndIdx = inSz;
#if defined(OPENSSL_EXTRA) || defined(RSA_DECODE_EXTRA)
word32 localIdx;
byte tag;
#endif
if (input == NULL || inOutIdx == NULL)
return BAD_FUNC_ARG;
if (GetSequence(input, inOutIdx, &length, inSz) < 0)
return ASN_PARSE_E;
#if defined(OPENSSL_EXTRA) || defined(RSA_DECODE_EXTRA)
localIdx = *inOutIdx;
if (GetASNTag(input, &localIdx, &tag, inSz) < 0)
return BUFFER_E;
if (tag != ASN_INTEGER) {
/* not from decoded cert, will have algo id, skip past */
if (GetSequence(input, inOutIdx, &length, inSz) < 0)
return ASN_PARSE_E;
if (SkipObjectId(input, inOutIdx, inSz) < 0)
return ASN_PARSE_E;
/* Option NULL ASN.1 tag */
if (*inOutIdx >= inSz) {
return BUFFER_E;
}
localIdx = *inOutIdx;
if (GetASNTag(input, &localIdx, &tag, inSz) < 0)
return ASN_PARSE_E;
if (tag == ASN_TAG_NULL) {
ret = GetASNNull(input, inOutIdx, inSz);
if (ret != 0)
return ret;
}
#ifdef WC_RSA_PSS
/* Skip RSA PSS parameters. */
else if (tag == (ASN_SEQUENCE | ASN_CONSTRUCTED)) {
if (GetSequence(input, inOutIdx, &length, inSz) < 0)
return ASN_PARSE_E;
*inOutIdx += length;
}
#endif
/* should have bit tag length and seq next */
ret = CheckBitString(input, inOutIdx, NULL, inSz, 1, NULL);
if (ret != 0)
return ret;
if (GetSequence(input, inOutIdx, &length, inSz) < 0)
return ASN_PARSE_E;
/* Calculate where the sequence should end for public key validation */
seqEndIdx = *inOutIdx + (word32)length;
}
#endif /* OPENSSL_EXTRA */
/* Get modulus */
ret = GetASNInt(input, inOutIdx, &firstLen, seqEndIdx);
if (ret < 0) {
return ASN_RSA_KEY_E;
}
if (nSz)
*nSz = (word32)firstLen;
if (n)
*n = &input[*inOutIdx];
*inOutIdx += (word32)firstLen;
/* Get exponent */
ret = GetASNInt(input, inOutIdx, &length, seqEndIdx);
if (ret < 0) {
return ASN_RSA_KEY_E;
}
if (eSz)
*eSz = (word32)length;
if (e)
*e = &input[*inOutIdx];
*inOutIdx += (word32)length;
/* Detect if this is an RSA private key being passed as public key.
* An RSA private key has: version (small), modulus (large), exponent,
* followed by more integers (d, p, q, etc.).
* An RSA public key has: modulus (large), exponent, and nothing more.
* If the first integer is small (like version 0) AND there is more data
* remaining in the sequence, this is likely a private key. */
if (firstLen <= MAX_VERSION_SZ && *inOutIdx < seqEndIdx) {
/* First integer is small and there's more data - looks like
* version field of a private key, not a modulus */
return ASN_RSA_KEY_E;
}
return ret;
}
#endif
#ifndef NO_DH
int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz)
{
int ret = 0;
int length;
#ifdef WOLFSSL_DH_EXTRA
#if !defined(HAVE_FIPS) || \
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
word32 oid = 0, temp = 0;
#endif
#endif
WOLFSSL_ENTER("wc_DhKeyDecode");
if (inOutIdx == NULL)
return BAD_FUNC_ARG;
if (key == NULL)
return BAD_FUNC_ARG;
if (GetSequence(input, inOutIdx, &length, inSz) < 0)
return ASN_PARSE_E;
#ifdef WOLFSSL_DH_EXTRA
#if !defined(HAVE_FIPS) || \
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
temp = *inOutIdx;
#endif
#endif
/* Assume input started after 1.2.840.113549.1.3.1 dhKeyAgreement */
if (GetInt(&key->p, input, inOutIdx, inSz) < 0) {
ret = ASN_DH_KEY_E;
}
if (ret == 0 && GetInt(&key->g, input, inOutIdx, inSz) < 0) {
mp_clear(&key->p);
ret = ASN_DH_KEY_E;
}
#ifdef WOLFSSL_DH_EXTRA
#if !defined(HAVE_FIPS) || \
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
/* If ASN_DH_KEY_E: Check if input started at beginning of key */
if (ret == WC_NO_ERR_TRACE(ASN_DH_KEY_E)) {
*inOutIdx = temp;
/* the version (0) - private only (for public skip) */
if (GetASNInt(input, inOutIdx, &length, inSz) == 0) {
*inOutIdx += (word32)length;
}
/* Size of dhKeyAgreement section */
if (GetSequence(input, inOutIdx, &length, inSz) < 0)
return ASN_PARSE_E;
/* Check for dhKeyAgreement */
ret = GetObjectId(input, inOutIdx, &oid, oidKeyType, inSz);
if (oid != DHk || ret < 0)
return ASN_DH_KEY_E;
if (GetSequence(input, inOutIdx, &length, inSz) < 0)
return ASN_PARSE_E;
if (GetInt(&key->p, input, inOutIdx, inSz) < 0) {
return ASN_DH_KEY_E;
}
if (ret == 0 && GetInt(&key->g, input, inOutIdx, inSz) < 0) {
mp_clear(&key->p);
return ASN_DH_KEY_E;