forked from wolfSSL/wolfssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpk_rsa.c
More file actions
3943 lines (3535 loc) · 116 KB
/
pk_rsa.c
File metadata and controls
3943 lines (3535 loc) · 116 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
/* pk_rsa.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
*/
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#include <wolfssl/internal.h>
#ifndef WC_NO_RNG
#include <wolfssl/wolfcrypt/random.h>
#endif
#if !defined(WOLFSSL_PK_RSA_INCLUDED)
#ifndef WOLFSSL_IGNORE_FILE_WARN
#warning pk_rsa.c does not need to be compiled separately from ssl.c
#endif
#else
#ifndef NO_RSA
#include <wolfssl/wolfcrypt/rsa.h>
#endif
/*******************************************************************************
* START OF RSA API
******************************************************************************/
#ifndef NO_RSA
/*
* RSA METHOD
* Could be used to hold function pointers to implementations of RSA operations.
*/
#if defined(OPENSSL_EXTRA)
/* Return a blank RSA method and set the name and flags.
*
* Only one implementation of RSA operations.
* name is duplicated.
*
* @param [in] name Name to use in method.
* @param [in] flags Flags to set into method.
* @return Newly allocated RSA method on success.
* @return NULL on failure.
*/
WOLFSSL_RSA_METHOD *wolfSSL_RSA_meth_new(const char *name, int flags)
{
WOLFSSL_RSA_METHOD* meth = NULL;
int name_len = 0;
int err;
/* Validate name is not NULL. */
if (name == NULL)
return NULL;
/* Allocate an RSA METHOD to return. */
meth = (WOLFSSL_RSA_METHOD*)XMALLOC(sizeof(WOLFSSL_RSA_METHOD), NULL,
DYNAMIC_TYPE_OPENSSL);
if (meth == NULL)
return NULL;
XMEMSET(meth, 0, sizeof(*meth));
meth->flags = flags;
meth->dynamic = 1;
name_len = (int)XSTRLEN(name);
meth->name = (char*)XMALLOC((size_t)(name_len + 1), NULL,
DYNAMIC_TYPE_OPENSSL);
err = (meth->name == NULL);
if (!err) {
XMEMCPY(meth->name, name, (size_t)(name_len + 1));
}
if (err) {
/* meth->name won't be allocated on error. */
XFREE(meth, NULL, DYNAMIC_TYPE_OPENSSL);
meth = NULL;
}
return meth;
}
/* Default RSA method is one with wolfSSL name and no flags.
*
* @return Newly allocated wolfSSL RSA method on success.
* @return NULL on failure.
*/
const WOLFSSL_RSA_METHOD* wolfSSL_RSA_get_default_method(void)
{
static const WOLFSSL_RSA_METHOD wolfssl_rsa_meth = {
0, /* No flags. */
(char*)"wolfSSL RSA",
0 /* Static definition. */
};
return &wolfssl_rsa_meth;
}
/* Dispose of RSA method and allocated data.
*
* @param [in] meth RSA method to free.
*/
void wolfSSL_RSA_meth_free(WOLFSSL_RSA_METHOD *meth)
{
/* Free method if available and dynamically allocated. */
if ((meth != NULL) && meth->dynamic) {
/* Name was duplicated and must be freed. */
XFREE(meth->name, NULL, DYNAMIC_TYPE_OPENSSL);
/* Dispose of RSA method. */
XFREE(meth, NULL, DYNAMIC_TYPE_OPENSSL);
}
}
#ifndef NO_WOLFSSL_STUB
/* Stub function for any RSA method setting function.
*
* Nothing is stored - not even flags or name.
*
* @param [in] meth RSA method.
* @param [in] p A pointer.
* @return 1 to indicate success.
*/
int wolfSSL_RSA_meth_set(WOLFSSL_RSA_METHOD *meth, void* p)
{
WOLFSSL_STUB("RSA_METHOD is not implemented.");
(void)meth;
(void)p;
return 1;
}
#endif /* !NO_WOLFSSL_STUB */
#endif /* OPENSSL_EXTRA */
/*
* RSA constructor/deconstructor APIs
*/
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
/* Dispose of RSA key and allocated data.
*
* Cannot use rsa after this call.
*
* @param [in] rsa RSA key to free.
*/
void wolfSSL_RSA_free(WOLFSSL_RSA* rsa)
{
int doFree = 1;
WOLFSSL_ENTER("wolfSSL_RSA_free");
/* Validate parameter. */
if (rsa == NULL) {
doFree = 0;
}
if (doFree) {
int err;
/* Decrement reference count. */
wolfSSL_RefDec(&rsa->ref, &doFree, &err);
#ifndef WOLFSSL_REFCNT_ERROR_RETURN
(void)err;
#endif
}
if (doFree) {
void* heap = rsa->heap;
/* Dispose of allocated reference counting data. */
wolfSSL_RefFree(&rsa->ref);
#ifdef HAVE_EX_DATA_CLEANUP_HOOKS
wolfSSL_CRYPTO_cleanup_ex_data(&rsa->ex_data);
#endif
if (rsa->internal != NULL) {
#if !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
/* Check if RNG is owned before freeing it. */
if (rsa->ownRng) {
WC_RNG* rng = ((RsaKey*)(rsa->internal))->rng;
if ((rng != NULL) && (rng != wolfssl_get_global_rng())) {
wc_FreeRng(rng);
XFREE(rng, heap, DYNAMIC_TYPE_RNG);
}
/* RNG isn't freed by wolfCrypt RSA free. */
}
#endif
/* Dispose of allocated data in wolfCrypt RSA key. */
wc_FreeRsaKey((RsaKey*)rsa->internal);
/* Dispose of memory for wolfCrypt RSA key. */
XFREE(rsa->internal, heap, DYNAMIC_TYPE_RSA);
}
/* Dispose of external representation of RSA values. */
wolfSSL_BN_clear_free(rsa->iqmp);
wolfSSL_BN_clear_free(rsa->dmq1);
wolfSSL_BN_clear_free(rsa->dmp1);
wolfSSL_BN_clear_free(rsa->q);
wolfSSL_BN_clear_free(rsa->p);
wolfSSL_BN_clear_free(rsa->d);
wolfSSL_BN_free(rsa->e);
wolfSSL_BN_free(rsa->n);
#if defined(OPENSSL_EXTRA)
if (rsa->meth) {
wolfSSL_RSA_meth_free((WOLFSSL_RSA_METHOD*)rsa->meth);
}
#endif
/* Set back to NULLs for safety. */
ForceZero(rsa, sizeof(*rsa));
XFREE(rsa, heap, DYNAMIC_TYPE_RSA);
(void)heap;
}
}
/* Allocate and initialize a new RSA key.
*
* Not OpenSSL API.
*
* @param [in] heap Heap hint for dynamic memory allocation.
* @param [in] devId Device identifier value.
* @return RSA key on success.
* @return NULL on failure.
*/
WOLFSSL_RSA* wolfSSL_RSA_new_ex(void* heap, int devId)
{
WOLFSSL_RSA* rsa = NULL;
RsaKey* key = NULL;
int err = 0;
int rsaKeyInited = 0;
WOLFSSL_ENTER("wolfSSL_RSA_new");
/* Allocate memory for new wolfCrypt RSA key. */
key = (RsaKey*)XMALLOC(sizeof(RsaKey), heap, DYNAMIC_TYPE_RSA);
if (key == NULL) {
WOLFSSL_ERROR_MSG("wolfSSL_RSA_new malloc RsaKey failure");
err = 1;
}
if (!err) {
/* Allocate memory for new RSA key. */
rsa = (WOLFSSL_RSA*)XMALLOC(sizeof(WOLFSSL_RSA), heap,
DYNAMIC_TYPE_RSA);
if (rsa == NULL) {
WOLFSSL_ERROR_MSG("wolfSSL_RSA_new malloc WOLFSSL_RSA failure");
err = 1;
}
}
if (!err) {
/* Clear all fields of RSA key. */
XMEMSET(rsa, 0, sizeof(WOLFSSL_RSA));
/* Cache heap to use for all allocations. */
rsa->heap = heap;
#ifdef OPENSSL_EXTRA
/* Always have a method set. */
rsa->meth = wolfSSL_RSA_get_default_method();
#endif
/* Initialize reference counting. */
wolfSSL_RefInit(&rsa->ref, &err);
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
}
if (!err) {
#endif
/* Initialize wolfCrypt RSA key. */
if (wc_InitRsaKey_ex(key, heap, devId) != 0) {
WOLFSSL_ERROR_MSG("InitRsaKey WOLFSSL_RSA failure");
err = 1;
}
else {
rsaKeyInited = 1;
}
}
#if !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
if (!err) {
WC_RNG* rng;
/* Create a local RNG. */
rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), heap, DYNAMIC_TYPE_RNG);
if ((rng != NULL) && (wc_InitRng_ex(rng, heap, devId) != 0)) {
WOLFSSL_MSG("InitRng failure, attempting to use global RNG");
XFREE(rng, heap, DYNAMIC_TYPE_RNG);
rng = NULL;
}
rsa->ownRng = 1;
if (rng == NULL) {
/* Get the wolfSSL global RNG - not thread safe. */
rng = wolfssl_get_global_rng();
rsa->ownRng = 0;
}
if (rng == NULL) {
/* Couldn't create global either. */
WOLFSSL_ERROR_MSG("wolfSSL_RSA_new no WC_RNG for blinding");
err = 1;
}
else {
/* Set the local or global RNG into the wolfCrypt RSA key. */
(void)wc_RsaSetRNG(key, rng);
/* Won't fail as key and rng are not NULL. */
}
}
#endif /* !HAVE_FIPS && WC_RSA_BLINDING */
if (!err) {
/* Set wolfCrypt RSA key into RSA key. */
rsa->internal = key;
/* Data from external RSA key has not been set into internal one. */
rsa->inSet = 0;
}
if (err) {
/* Dispose of any allocated data on error. */
/* No failure after RNG allocation - no need to free RNG. */
if (rsaKeyInited) {
wc_FreeRsaKey(key);
}
XFREE(key, heap, DYNAMIC_TYPE_RSA);
XFREE(rsa, heap, DYNAMIC_TYPE_RSA);
/* Return NULL. */
rsa = NULL;
}
return rsa;
}
/* Allocate and initialize a new RSA key.
*
* @return RSA key on success.
* @return NULL on failure.
*/
WOLFSSL_RSA* wolfSSL_RSA_new(void)
{
/* Call wolfSSL API to do work. */
return wolfSSL_RSA_new_ex(NULL, INVALID_DEVID);
}
/* Increments ref count of RSA key.
*
* @param [in, out] rsa RSA key.
* @return 1 on success
* @return 0 on error
*/
int wolfSSL_RSA_up_ref(WOLFSSL_RSA* rsa)
{
int err = 0;
if (rsa != NULL) {
wolfSSL_RefInc(&rsa->ref, &err);
}
return !err;
}
#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
#ifdef OPENSSL_EXTRA
#if defined(WOLFSSL_KEY_GEN)
/* Allocate a new RSA key and make it a copy.
*
* Encodes to and from DER to copy.
*
* @param [in] rsa RSA key to duplicate.
* @return RSA key on success.
* @return NULL on error.
*/
WOLFSSL_RSA* wolfSSL_RSAPublicKey_dup(WOLFSSL_RSA *rsa)
{
WOLFSSL_RSA* ret = NULL;
int derSz = 0;
byte* derBuf = NULL;
int err;
WOLFSSL_ENTER("wolfSSL_RSAPublicKey_dup");
err = (rsa == NULL);
if (!err) {
/* Create a new RSA key to return. */
ret = wolfSSL_RSA_new();
if (ret == NULL) {
WOLFSSL_ERROR_MSG("Error creating a new WOLFSSL_RSA structure");
err = 1;
}
}
if (!err) {
/* Encode RSA public key to copy to DER - allocates DER buffer. */
if ((derSz = wolfSSL_RSA_To_Der(rsa, &derBuf, 1, rsa->heap)) < 0) {
WOLFSSL_ERROR_MSG("wolfSSL_RSA_To_Der failed");
err = 1;
}
}
if (!err) {
/* Decode DER of the RSA public key into new key. */
if (wolfSSL_RSA_LoadDer_ex(ret, derBuf, derSz,
WOLFSSL_RSA_LOAD_PUBLIC) != 1) {
WOLFSSL_ERROR_MSG("wolfSSL_RSA_LoadDer_ex failed");
err = 1;
}
}
/* Dispose of any allocated DER buffer. */
XFREE(derBuf, rsa ? rsa->heap : NULL, DYNAMIC_TYPE_ASN1);
if (err) {
/* Disposes of any created RSA key - on error. */
wolfSSL_RSA_free(ret);
ret = NULL;
}
return ret;
}
/* wolfSSL_RSAPrivateKey_dup not supported */
#endif /* WOLFSSL_KEY_GEN */
static int wolfSSL_RSA_To_Der_ex(WOLFSSL_RSA* rsa, byte** outBuf, int publicKey,
void* heap);
/*
* RSA to/from bin APIs
*/
/* Convert RSA public key data to internal.
*
* Creates new RSA key from the DER encoded RSA public key.
*
* @param [out] out Pointer to RSA key to return through. May be NULL.
* @param [in, out] derBuf Pointer to start of DER encoded data.
* @param [in] derSz Length of the data in the DER buffer.
* @return RSA key on success.
* @return NULL on failure.
*/
WOLFSSL_RSA *wolfSSL_d2i_RSAPublicKey(WOLFSSL_RSA **out,
const unsigned char **derBuf, long derSz)
{
WOLFSSL_RSA *rsa = NULL;
int err = 0;
WOLFSSL_ENTER("wolfSSL_d2i_RSAPublicKey");
/* Validate parameters. */
if (derBuf == NULL) {
WOLFSSL_ERROR_MSG("Bad argument");
err = 1;
}
/* Create a new RSA key to return. */
if ((!err) && ((rsa = wolfSSL_RSA_new()) == NULL)) {
WOLFSSL_ERROR_MSG("RSA_new failed");
err = 1;
}
/* Decode RSA key from DER. */
if ((!err) && (wolfSSL_RSA_LoadDer_ex(rsa, *derBuf, (int)derSz,
WOLFSSL_RSA_LOAD_PUBLIC) != 1)) {
WOLFSSL_ERROR_MSG("RSA_LoadDer failed");
err = 1;
}
if ((!err) && (out != NULL)) {
/* Return through parameter too. */
*out = rsa;
/* Move buffer on by the used amount. */
*derBuf += wolfssl_der_length(*derBuf, (int)derSz);
}
if (err) {
/* Dispose of any created RSA key. */
wolfSSL_RSA_free(rsa);
rsa = NULL;
}
return rsa;
}
/* Convert RSA private key data to internal.
*
* Create a new RSA key from the DER encoded RSA private key.
*
* @param [out] out Pointer to RSA key to return through. May be NULL.
* @param [in, out] derBuf Pointer to start of DER encoded data.
* @param [in] derSz Length of the data in the DER buffer.
* @return RSA key on success.
* @return NULL on failure.
*/
WOLFSSL_RSA *wolfSSL_d2i_RSAPrivateKey(WOLFSSL_RSA **out,
const unsigned char **derBuf, long derSz)
{
WOLFSSL_RSA *rsa = NULL;
int err = 0;
WOLFSSL_ENTER("wolfSSL_d2i_RSAPublicKey");
/* Validate parameters. */
if (derBuf == NULL) {
WOLFSSL_ERROR_MSG("Bad argument");
err = 1;
}
/* Create a new RSA key to return. */
if ((!err) && ((rsa = wolfSSL_RSA_new()) == NULL)) {
WOLFSSL_ERROR_MSG("RSA_new failed");
err = 1;
}
/* Decode RSA key from DER. */
if ((!err) && (wolfSSL_RSA_LoadDer_ex(rsa, *derBuf, (int)derSz,
WOLFSSL_RSA_LOAD_PRIVATE) != 1)) {
WOLFSSL_ERROR_MSG("RSA_LoadDer failed");
err = 1;
}
if ((!err) && (out != NULL)) {
/* Return through parameter too. */
*out = rsa;
/* Move buffer on by the used amount. */
*derBuf += wolfssl_der_length(*derBuf, (int)derSz);
}
if (err) {
/* Dispose of any created RSA key. */
wolfSSL_RSA_free(rsa);
rsa = NULL;
}
return rsa;
}
/* Converts an internal RSA structure to DER format for the private key.
*
* If "pp" is null then buffer size only is returned.
* If "*pp" is null then a created buffer is set in *pp and the caller is
* responsible for free'ing it.
*
* @param [in] rsa RSA key.
* @param [in, out] pp On in, pointer to allocated buffer or NULL.
* May be NULL.
* On out, newly allocated buffer or pointer to byte after
* encoding in passed in buffer.
*
* @return Size of DER encoding on success
* @return BAD_FUNC_ARG when rsa is NULL.
* @return 0 on failure.
*/
int wolfSSL_i2d_RSAPrivateKey(WOLFSSL_RSA *rsa, unsigned char **pp)
{
int ret;
WOLFSSL_ENTER("wolfSSL_i2d_RSAPrivateKey");
/* Validate parameters. */
if (rsa == NULL) {
WOLFSSL_ERROR_MSG("Bad Function Arguments");
ret = BAD_FUNC_ARG;
}
/* Encode the RSA key as a DER. Call allocates buffer into pp.
* No heap hint as this gets returned to the user */
else if ((ret = wolfSSL_RSA_To_Der_ex(rsa, pp, 0, NULL)) < 0) {
WOLFSSL_ERROR_MSG("wolfSSL_RSA_To_Der failed");
ret = 0;
}
/* Size of DER encoding. */
return ret;
}
/* Converts an internal RSA structure to DER format for the public key.
*
* If "pp" is null then buffer size only is returned.
* If "*pp" is null then a created buffer is set in *pp and the caller is
* responsible for free'ing it.
*
* @param [in] rsa RSA key.
* @param [in, out] pp On in, pointer to allocated buffer or NULL.
* May be NULL.
* On out, newly allocated buffer or pointer to byte after
* encoding in passed in buffer.
* @return Size of DER encoding on success
* @return BAD_FUNC_ARG when rsa is NULL.
* @return 0 on failure.
*/
int wolfSSL_i2d_RSAPublicKey(WOLFSSL_RSA *rsa, unsigned char **pp)
{
int ret;
WOLFSSL_ENTER("wolfSSL_i2d_RSAPublicKey");
/* check for bad functions arguments */
if (rsa == NULL) {
WOLFSSL_ERROR_MSG("Bad Function Arguments");
ret = BAD_FUNC_ARG;
}
/* Encode the RSA key as a DER. Call allocates buffer into pp.
* No heap hint as this gets returned to the user */
else if ((ret = wolfSSL_RSA_To_Der_ex(rsa, pp, 1, NULL)) < 0) {
WOLFSSL_ERROR_MSG("wolfSSL_RSA_To_Der failed");
ret = 0;
}
return ret;
}
#endif /* OPENSSL_EXTRA */
/*
* RSA to/from BIO APIs
*/
/* wolfSSL_d2i_RSAPublicKey_bio not supported */
#if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
|| defined(WOLFSSL_NGINX) || defined(WOLFSSL_QT)
#if defined(WOLFSSL_KEY_GEN) && !defined(NO_BIO)
/* Read DER data from a BIO.
*
* DER structures start with a constructed sequence. Use this to calculate the
* total length of the DER data.
*
* @param [in] bio BIO object to read from.
* @param [out] out Buffer holding DER encoding.
* @return Number of bytes to DER encoding on success.
* @return 0 on failure.
*/
static int wolfssl_read_der_bio(WOLFSSL_BIO* bio, unsigned char** out)
{
int err = 0;
unsigned char seq[MAX_SEQ_SZ];
unsigned char* der = NULL;
int derLen = 0;
/* Read in a minimal amount to get a SEQUENCE header of any size. */
if (wolfSSL_BIO_read(bio, seq, sizeof(seq)) != sizeof(seq)) {
WOLFSSL_ERROR_MSG("wolfSSL_BIO_read() of sequence failure");
err = 1;
}
/* Calculate complete DER encoding length. */
if ((!err) && ((derLen = wolfssl_der_length(seq, sizeof(seq))) <= 0)) {
WOLFSSL_ERROR_MSG("DER SEQUENCE decode failed");
err = 1;
}
/* Allocate a buffer to read DER data into. */
if ((!err) && ((der = (unsigned char*)XMALLOC((size_t)derLen, bio->heap,
DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
WOLFSSL_ERROR_MSG("Malloc failure");
err = 1;
}
if ((!err) && (derLen <= (int)sizeof(seq))) {
/* Copy the previously read data into the buffer. */
XMEMCPY(der, seq, derLen);
}
else if (!err) {
/* Calculate the unread amount. */
int len = derLen - (int)sizeof(seq);
/* Copy the previously read data into the buffer. */
XMEMCPY(der, seq, sizeof(seq));
/* Read rest of DER data from BIO. */
if (wolfSSL_BIO_read(bio, der + sizeof(seq), len) != len) {
WOLFSSL_ERROR_MSG("wolfSSL_BIO_read() failure");
err = 1;
}
}
if (!err) {
/* Return buffer through parameter. */
*out = der;
}
if (err) {
/* Dispose of any allocated buffer on error. */
XFREE(der, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
derLen = 0;
}
return derLen;
}
/* Reads the RSA private key data from a BIO to the internal form.
*
* Creates new RSA key from the DER encoded RSA private key read from the BIO.
*
* @param [in] bio BIO object to read from.
* @param [out] out Pointer to RSA key to return through. May be NULL.
* @return RSA key on success.
* @return NULL on failure.
*/
WOLFSSL_RSA* wolfSSL_d2i_RSAPrivateKey_bio(WOLFSSL_BIO *bio, WOLFSSL_RSA **out)
{
WOLFSSL_RSA* key = NULL;
unsigned char* der = NULL;
int derLen = 0;
int err;
WOLFSSL_ENTER("wolfSSL_d2i_RSAPrivateKey_bio");
/* Validate parameters. */
err = (bio == NULL);
/* Read just DER encoding from BIO - buffer allocated in call. */
if ((!err) && ((derLen = wolfssl_read_der_bio(bio, &der)) == 0)) {
err = 1;
}
if (!err) {
/* Keep der for call to deallocate. */
const unsigned char* cder = der;
/* Create an RSA key from the data from the BIO. */
key = wolfSSL_d2i_RSAPrivateKey(NULL, &cder, derLen);
err = (key == NULL);
}
if ((!err) && (out != NULL)) {
/* Return the created RSA key through the parameter. */
*out = key;
}
if (err) {
/* Dispose of created key on error. */
wolfSSL_RSA_free(key);
key = NULL;
}
/* Dispose of allocated data. */
XFREE(der, bio ? bio->heap : NULL, DYNAMIC_TYPE_TMP_BUFFER);
return key;
}
#endif /* defined(WOLFSSL_KEY_GEN) && !NO_BIO */
#endif /* OPENSSL_ALL || WOLFSSL_ASIO || WOLFSSL_HAPROXY || WOLFSSL_QT */
/*
* RSA DER APIs
*/
#ifdef OPENSSL_EXTRA
/* Create a DER encoding of key.
*
* Not OpenSSL API.
*
* @param [in] rsa RSA key.
* @param [out] outBuf Allocated buffer containing DER encoding.
* May be NULL.
* @param [in] publicKey Whether to encode as public key.
* @param [in] heap Heap hint.
* @return Encoding size on success.
* @return Negative on failure.
*/
int wolfSSL_RSA_To_Der(WOLFSSL_RSA* rsa, byte** outBuf, int publicKey,
void* heap)
{
byte* p = NULL;
int ret;
if (outBuf != NULL) {
p = *outBuf;
}
ret = wolfSSL_RSA_To_Der_ex(rsa, outBuf, publicKey, heap);
if ((ret > 0) && (p != NULL)) {
*outBuf = p;
}
return ret;
}
/* Create a DER encoding of key.
*
* Buffer allocated with heap and DYNAMIC_TYPE_TMP_BUFFER.
*
* @param [in] rsa RSA key.
* @param [in, out] outBuf On in, pointer to allocated buffer or NULL.
* May be NULL.
* On out, newly allocated buffer or pointer to byte
* after encoding in passed in buffer.
* @param [in] publicKey Whether to encode as public key.
* @param [in] heap Heap hint.
* @return Encoding size on success.
* @return Negative on failure.
*/
static int wolfSSL_RSA_To_Der_ex(WOLFSSL_RSA* rsa, byte** outBuf, int publicKey,
void* heap)
{
int ret = 1;
int derSz = 0;
byte* derBuf = NULL;
WOLFSSL_ENTER("wolfSSL_RSA_To_Der");
/* Unused if memory is disabled. */
(void)heap;
/* Validate parameters. */
if ((rsa == NULL) || ((publicKey != 0) && (publicKey != 1))) {
WOLFSSL_LEAVE("wolfSSL_RSA_To_Der", BAD_FUNC_ARG);
ret = BAD_FUNC_ARG;
}
/* Push external RSA data into internal RSA key if not set. */
if ((ret == 1) && (!rsa->inSet)) {
ret = SetRsaInternal(rsa);
}
/* wc_RsaKeyToPublicDer encode regardless of values. */
if ((ret == 1) && publicKey && (mp_iszero(&((RsaKey*)rsa->internal)->n) ||
mp_iszero(&((RsaKey*)rsa->internal)->e))) {
ret = BAD_FUNC_ARG;
}
if (ret == 1) {
if (publicKey) {
/* Calculate length of DER encoded RSA public key. */
derSz = wc_RsaPublicKeyDerSize((RsaKey*)rsa->internal, 1);
if (derSz < 0) {
WOLFSSL_ERROR_MSG("wc_RsaPublicKeyDerSize failed");
ret = derSz;
}
}
else {
/* Calculate length of DER encoded RSA private key. */
derSz = wc_RsaKeyToDer((RsaKey*)rsa->internal, NULL, 0);
if (derSz < 0) {
WOLFSSL_ERROR_MSG("wc_RsaKeyToDer failed");
ret = derSz;
}
}
}
if ((ret == 1) && (outBuf != NULL)) {
derBuf = *outBuf;
if (derBuf == NULL) {
/* Allocate buffer to hold DER encoded RSA key. */
derBuf = (byte*)XMALLOC((size_t)derSz, heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (derBuf == NULL) {
WOLFSSL_ERROR_MSG("Memory allocation failed");
ret = MEMORY_ERROR;
}
}
}
if ((ret == 1) && (outBuf != NULL)) {
if (publicKey > 0) {
/* RSA public key to DER. */
derSz = wc_RsaKeyToPublicDer((RsaKey*)rsa->internal, derBuf,
(word32)derSz);
}
else {
/* RSA private key to DER. */
derSz = wc_RsaKeyToDer((RsaKey*)rsa->internal, derBuf,
(word32)derSz);
}
if (derSz < 0) {
WOLFSSL_ERROR_MSG("RSA key encoding failed");
ret = derSz;
}
else if ((*outBuf) != NULL) {
derBuf = NULL;
*outBuf += derSz;
}
else {
/* Return allocated buffer. */
*outBuf = derBuf;
}
}
if (ret == 1) {
/* Success - return DER encoding size. */
ret = derSz;
}
if ((outBuf != NULL) && (*outBuf != derBuf)) {
/* Not returning buffer, needs to be disposed of. */
XFREE(derBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
}
WOLFSSL_LEAVE("wolfSSL_RSA_To_Der", ret);
return ret;
}
#endif /* OPENSSL_EXTRA */
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
/* Load the DER encoded private RSA key.
*
* Not OpenSSL API.
*
* @param [in] rsa RSA key.
* @param [in] derBuf Buffer holding DER encoding.
* @param [in] derSz Length of DER encoding.
* @return 1 on success.
* @return -1 on failure.
*/
int wolfSSL_RSA_LoadDer(WOLFSSL_RSA* rsa, const unsigned char* derBuf,
int derSz)
{
/* Call implementation that handles both private and public keys. */
return wolfSSL_RSA_LoadDer_ex(rsa, derBuf, derSz, WOLFSSL_RSA_LOAD_PRIVATE);
}
/* Load the DER encoded public or private RSA key.
*
* Not OpenSSL API.
*
* @param [in] rsa RSA key.
* @param [in] derBuf Buffer holding DER encoding.
* @param [in] derSz Length of DER encoding.
* @param [in] opt Indicates public or private key.
* (WOLFSSL_RSA_LOAD_PUBLIC or WOLFSSL_RSA_LOAD_PRIVATE)
* @return 1 on success.
* @return -1 on failure.
*/
int wolfSSL_RSA_LoadDer_ex(WOLFSSL_RSA* rsa, const unsigned char* derBuf,
int derSz, int opt)
{
int ret = 1;
int res;
word32 idx = 0;
word32 algId;
WOLFSSL_ENTER("wolfSSL_RSA_LoadDer");
/* Validate parameters. */
if ((rsa == NULL) || (rsa->internal == NULL) || (derBuf == NULL) ||
(derSz <= 0)) {
WOLFSSL_ERROR_MSG("Bad function arguments");
ret = WOLFSSL_FATAL_ERROR;
}
if (ret == 1) {
rsa->pkcs8HeaderSz = 0;
/* Check if input buffer has PKCS8 header. In the case that it does not
* have a PKCS8 header then do not error out. */
res = ToTraditionalInline_ex((const byte*)derBuf, &idx, (word32)derSz,
&algId);
if (res >= 0) {
/* Store size of PKCS#8 header for encoding. */
WOLFSSL_MSG("Found PKCS8 header");
rsa->pkcs8HeaderSz = (word16)idx;
}
/* When decoding and not PKCS#8, return will be ASN_PARSE_E. */
else if (res != WC_NO_ERR_TRACE(ASN_PARSE_E)) {
/* Something went wrong while decoding. */
WOLFSSL_ERROR_MSG("Unexpected error with trying to remove PKCS#8 "
"header");
ret = WOLFSSL_FATAL_ERROR;
}
}
if (ret == 1) {
/* Decode private or public key data. */
if (opt == WOLFSSL_RSA_LOAD_PRIVATE) {
res = wc_RsaPrivateKeyDecode(derBuf, &idx, (RsaKey*)rsa->internal,
(word32)derSz);
}
else {
res = wc_RsaPublicKeyDecode(derBuf, &idx, (RsaKey*)rsa->internal,
(word32)derSz);
}
/* Check for error. */
if (res < 0) {
if (opt == WOLFSSL_RSA_LOAD_PRIVATE) {
WOLFSSL_ERROR_MSG("RsaPrivateKeyDecode failed");
}
else {
WOLFSSL_ERROR_MSG("RsaPublicKeyDecode failed");
}
WOLFSSL_ERROR_VERBOSE(res);
ret = WOLFSSL_FATAL_ERROR;
}
}
if (ret == 1) {
/* Set external RSA key data from wolfCrypt key. */
if (SetRsaExternal(rsa) != 1) {
ret = WOLFSSL_FATAL_ERROR;
}
else {
rsa->inSet = 1;
}
}
return ret;
}
#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
#if !defined(NO_BIO) || !defined(NO_FILESYSTEM)
/* Load DER encoded data into WOLFSSL_RSA object.
*
* Creates a new WOLFSSL_RSA object if one is not passed in.
*
* @param [in, out] rsa WOLFSSL_RSA object to load into.
* When rsa or *rsa is NULL a new object is created.
* When not NULL and *rsa is NULL then new object
* returned through pointer.
* @param [in] in DER encoded RSA key data.
* @param [in] inSz Size of DER encoded data in bytes.
* @param [in] opt Public or private key encoded in data. Valid values:
* WOLFSSL_RSA_LOAD_PRIVATE, WOLFSSL_RSA_LOAD_PUBLIC.
* @return NULL on failure.
* @return WOLFSSL_RSA object on success.
*/
static WOLFSSL_RSA* wolfssl_rsa_d2i(WOLFSSL_RSA** rsa, const unsigned char* in,
long inSz, int opt)
{
WOLFSSL_RSA* ret = NULL;
if ((rsa != NULL) && (*rsa != NULL)) {