-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathwp_rsa_kmgmt.c
More file actions
4606 lines (4156 loc) · 139 KB
/
wp_rsa_kmgmt.c
File metadata and controls
4606 lines (4156 loc) · 139 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
/* wp_rsa_kmgmt.c
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfProvider.
*
* wolfProvider 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.
*
* wolfProvider 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 wolfProvider. If not, see <http://www.gnu.org/licenses/>.
*/
#include <openssl/err.h>
#include <openssl/proverr.h>
#include <openssl/core_dispatch.h>
#include <openssl/core_object.h>
#include <openssl/core_names.h>
#include <openssl/params.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <wolfprovider/settings.h>
#include <wolfprovider/alg_funcs.h>
#include <wolfprovider/wp_fips.h>
#include <wolfssl/wolfcrypt/asn.h>
#ifdef WP_HAVE_RSA
/* In 5.8.2 RSA_MIN_SIZE was changed from 1024 to 2048. We still need to
* allow 1024 in some cases, and have extended logic in place for it already.
* For FIPS 1024 bit keys, use existing checks and let wolfssl throw us back */
#define WP_RSA_MIN_SIZE 1024
#define WP_RSA_MAX_SIZE RSA_MAX_SIZE
/** Supported selections (key parts) in this key manager for RSA. */
#define WP_RSA_POSSIBLE_SELECTIONS \
(OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS)
/** RSA number related parameters. */
#define WP_RSA_NUM_PARAMS \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0), \
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0)
/** RSA PSS specific parameters. */
#define WP_RSA_PSS_PARAMS \
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_DIGEST, NULL, 0), \
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_MASKGENFUNC, NULL, 0), \
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_MGF1_DIGEST, NULL, 0), \
OSSL_PARAM_int(OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, NULL)
/** Count of RSA numbers that are in parameters. */
#define WP_RSA_PARAM_NUMS_CNT 8
/** Count of public RSA numbers that are in parameters. */
#define WP_RSA_PARAM_PUB_NUMS_CNT 2
/** Default RSA digest */
#define WP_RSA_DEFAULT_MD "SHA256"
/** Default RSA PSS digest. */
#define WP_RSA_PSS_DIGEST_DEF WC_HASH_TYPE_SHA
/** Default MGF algorithm */
#define WP_RSA_PSS_MGF_DEF WC_MGF1SHA1
#ifndef OFFSETOF
#define OFFSETOF(type, field) ((size_t)&(((type *)0)->field))
#endif
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#endif
/** SHA-256 Algorithm ID DER encoding in PSS parameters. */
static const byte sha256AlgId[] = {
0xa0, 0x0d, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86,
0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01
};
/** SHA-256 Algorithm ID with NULL DER encoding in PSS parameters. */
static const byte sha256AlgIdNull[] = {
0xa0, 0x0f, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
0x00
};
/** SHA-384 Algorithm ID DER encoding in PSS parameters. */
static const byte sha384AlgId[] = {
0xa0, 0x0d, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86,
0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02
};
/** SHA-384 Algorithm ID with NULL DER encoding in PSS parameters. */
static const byte sha384AlgIdNull[] = {
0xa0, 0x0f, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05,
0x00
};
/** SHA-512 Algorithm ID DER encoding in PSS parameters. */
static const byte sha512AlgId[] = {
0xa0, 0x0d, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86,
0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
};
/** SHA-512 Algorithm ID with NULL DER encoding in PSS parameters. */
static const byte sha512AlgIdNull[] = {
0xa0, 0x0f, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05,
0x00
};
/** MGF1 SHA-256 Algorithm ID DER encoding in PSS parameters. */
static const byte mgf1Sha256AlgId[] = {
0xa1, 0x1a, 0x30, 0x18, 0x06, 0x09, 0x2a, 0x86,
0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x08, 0x30,
0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
0x03, 0x04, 0x02, 0x01
};
/** MGF1 SHA-256 Algorithm ID with NULL DER encoding in PSS parameters. */
static const byte mgf1Sha256AlgIdNull[] = {
0xa1, 0x1c, 0x30, 0x1a, 0x06, 0x09, 0x2a, 0x86,
0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x08, 0x30,
0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
0x03, 0x04, 0x02, 0x01, 0x05, 0x00
};
/** MGF1 SHA-384 Algorithm ID DER encoding in PSS parameters. */
static const byte mgf1Sha384AlgId[] = {
0xa1, 0x1a, 0x30, 0x18, 0x06, 0x09, 0x2a, 0x86,
0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x08, 0x30,
0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
0x03, 0x04, 0x02, 0x02
};
/** MGF1 SHA-384 Algorithm ID with NULL DER encoding in PSS parameters. */
static const byte mgf1Sha384AlgIdNull[] = {
0xa1, 0x1c, 0x30, 0x1a, 0x06, 0x09, 0x2a, 0x86,
0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x08, 0x30,
0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
0x03, 0x04, 0x02, 0x02, 0x05, 0x00
};
/** MGF1 SHA-512 Algorithm ID DER encoding in PSS parameters. */
static const byte mgf1Sha512AlgId[] = {
0xa1, 0x1a, 0x30, 0x18, 0x06, 0x09, 0x2a, 0x86,
0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x08, 0x30,
0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
0x03, 0x04, 0x02, 0x03
};
/** MGF1 SHA-512 Algorithm ID with NULL DER encoding in PSS parameters. */
static const byte mgf1Sha512AlgIdNull[] = {
0xa1, 0x1c, 0x30, 0x1a, 0x06, 0x09, 0x2a, 0x86,
0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x08, 0x30,
0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
0x03, 0x04, 0x02, 0x03, 0x05, 0x00
};
/** Salt length DER encoding in PSS parameters. */
static const byte saltLenDer[] = {
0xa2, 0x03, 0x02, 0x01
};
/** Salt length as 2 bytes DER encoding in PSS parameters. */
static const byte saltLenDer2[] = {
0xa2, 0x04, 0x02, 0x02
};
/** Table of offsets into RsaKey structure of various fields. */
static const size_t wp_rsa_offset[WP_RSA_PARAM_NUMS_CNT] = {
OFFSETOF(RsaKey, n),
OFFSETOF(RsaKey, e),
OFFSETOF(RsaKey, d),
OFFSETOF(RsaKey, p),
OFFSETOF(RsaKey, q),
OFFSETOF(RsaKey, dP),
OFFSETOF(RsaKey, dQ),
OFFSETOF(RsaKey, u)
};
/** Table of parameter keys for RSA numbers. */
static const char* wp_rsa_param_key[WP_RSA_PARAM_NUMS_CNT] = {
OSSL_PKEY_PARAM_RSA_N, OSSL_PKEY_PARAM_RSA_E, OSSL_PKEY_PARAM_RSA_D,
OSSL_PKEY_PARAM_RSA_FACTOR1, OSSL_PKEY_PARAM_RSA_FACTOR2,
OSSL_PKEY_PARAM_RSA_EXPONENT1, OSSL_PKEY_PARAM_RSA_EXPONENT2,
OSSL_PKEY_PARAM_RSA_COEFFICIENT1
};
#define WP_RSA_PARAM_KEY_FACTOR_INDEX1 3
#define WP_RSA_PARAM_KEY_FACTOR_INDEX2 4
#define WP_RSA_PARAM_KEY_EXPONENT_INDEX1 5
#define WP_RSA_PARAM_KEY_EXPONENT_INDEX2 6
#define WP_RSA_PARAM_KEY_COEFFICIENT_INDEX 7
/**
* RSA PSS parameters.
*/
typedef struct wp_RsaPssParams {
/** wolfSSL hash type to use when digesting message. */
enum wc_HashType hashType;
/** wolfSSL MGF to use when performing PSS padding. */
int mgf;
/** Name of hash to use for digesting message. */
char mdName[WP_MAX_MD_NAME_SIZE];
/** Name of hash to use with MGF when performing PSS padding. */
char mgfMdName[WP_MAX_MD_NAME_SIZE];
/** Length of salt. */
int saltLen;
/** Trailer field value for PSS DER. Id for last padding byte. */
int derTrailer;
} wp_RsaPssParams;
/**
* RSA key.
*/
struct wp_Rsa {
/** wolfSSL RSA key object. */
RsaKey key;
#ifndef WP_SINGLE_THREADED
/** Mutex for reference count updating. */
wolfSSL_Mutex mutex;
#endif
/** Count of references to this object. */
int refCnt;
/** Provider context - useful when duplicating. */
WOLFPROV_CTX* provCtx;
/** Type of RSA key: PKCS#1.5 or PSS. */
int type;
/** Number of bits in key. */
int bits;
/** Public key available. */
unsigned int hasPub:1;
/** Private key available. */
unsigned int hasPriv:1;
/** Extra PSS parameters. */
wp_RsaPssParams pssParams;
/** PSS parameters set. */
int pssDefSet;
};
/**
* RSA generation context.
*/
typedef struct wp_RsaGenCtx {
/** wolfSSL random number generator object. */
WC_RNG rng;
/** Provider context - used when creating an RSA key. */
WOLFPROV_CTX* provCtx;
/** Type of RSA key to create: PKCS#1.5 or PSS. */
int type;
/** Number of bits to generate key with. */
size_t bits;
/** Public exponent to generate key with. */
size_t e;
/** Extra PSS parameters to set. */
wp_RsaPssParams pssParams;
/** Default PSS parameters have been set. */
int pssDefSet;
} wp_RsaGenCtx;
/* Prototype for generation initialization. */
static int wp_rsa_gen_set_params(wp_RsaGenCtx* ctx, const OSSL_PARAM params[]);
/**
* Increment reference count for key.
*
* Used in key generation, signing/verify and key exchange.
*
* @param [in, out] rsa RSA key object.
* @return 1 on success.
* @return 0 when multi-threaded and locking fails.
*/
int wp_rsa_up_ref(wp_Rsa* rsa)
{
WOLFPROV_ENTER(WP_LOG_COMP_RSA, "wp_rsa_up_ref");
#ifndef WP_SINGLE_THREADED
int ok = 1;
int rc;
rc = wc_LockMutex(&rsa->mutex);
if (rc < 0) {
ok = 0;
}
if (ok) {
rsa->refCnt++;
wc_UnLockMutex(&rsa->mutex);
}
WOLFPROV_LEAVE(WP_LOG_COMP_RSA, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
#else
rsa->refCnt++;
WOLFPROV_LEAVE(WP_LOG_COMP_RSA, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), 1);
return 1;
#endif
}
/**
* Get the type of RSA key.
*
* @param [in] rsa RSA key object.
* @return RSA_FLAG_TYPE_RSA for PKCS#1.5 RSA.
* @return RSA_FLAG_TYPE_RSASSAPSS for PSS RSA.
*/
int wp_rsa_get_type(wp_Rsa* rsa)
{
return rsa->type;
}
/**
* Get the wolfSSL key object.
*
* @param [in] rsa RSA key object.
* @return Pointer to wolfSSL RSA key object.
*/
RsaKey* wp_rsa_get_key(wp_Rsa* rsa)
{
return &rsa->key;
}
/**
* Get the number of bits to RSA key.
*
* @param [in] rsa RSA key object.
* @return Number of bits in key.
*/
int wp_rsa_get_bits(wp_Rsa* rsa)
{
return rsa->bits;
}
/**
* Get the mutex object from the RSA key object.
*
* @param [in] rsa RSA key object.
* @return Pointer to wolfSSL mutex object.
*/
wolfSSL_Mutex* wp_rsa_get_mutex(wp_Rsa* rsa)
{
return &rsa->mutex;
}
/**
* Check the RSA key size is valid.
*
* @param [in] keySize RSA key size in bits.
* @param [in] allow1024 Whether to allow 1024-bit RSA keys.
* @return 1 when valid.
* @return 0 when not valid.
*/
static int wp_rsa_check_key_size_int(int keySize, int allow1024)
{
int ok = 1;
WOLFPROV_ENTER(WP_LOG_COMP_RSA, "wp_rsa_check_key_size_int");
if ((keySize < WP_RSA_MIN_SIZE) || (keySize > WP_RSA_MAX_SIZE)) {
WOLFPROV_MSG(WP_LOG_COMP_RSA, "RSA key size invalid: %d\n", keySize);
ok = 0;
}
#ifdef HAVE_FIPS
if (!allow1024 && keySize < 2048) {
ok = 0;
}
else if (keySize > 4096) {
ok = 0;
}
#else
(void)allow1024;
#endif
WOLFPROV_LEAVE(WP_LOG_COMP_RSA, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Check the RSA key size is valid.
*
* @param [in] rsa RSA key object.
* @param [in] allow1024 Whether to allow 1024-bit RSA keys.
* @return 1 when valid.
* @return 0 when not valid.
*/
int wp_rsa_check_key_size(wp_Rsa* rsa, int allow1024)
{
return wp_rsa_check_key_size_int(rsa->bits, allow1024);
}
/**
* Check the RSA key size is valid.
*
* @param [in] rsagen RSA generation context object.
* @return 1 when valid.
* @return 0 when not valid.
*/
static int wp_rsagen_check_key_size(wp_RsaGenCtx* rsagen)
{
return wp_rsa_check_key_size_int((int)rsagen->bits, 0);
}
/**
* Get the PSS digests.
*
* @param [in] rsa RSA key object.
* @return Pointer to wolfSSL RSA key object.
*/
void wp_rsa_get_pss_mds(wp_Rsa* rsa, char** mdName, char** mgfMdName)
{
if (mdName != NULL) {
*mdName = rsa->pssParams.mdName;
}
if (mgfMdName != NULL) {
*mgfMdName = rsa->pssParams.mgfMdName;
}
}
/**
* Get the PSS salt length set from parameters.
*
* @param [in] rsa RSA key object.
* @return Length in bytes of salt.
*/
int wp_rsa_get_pss_salt_len(wp_Rsa* rsa)
{
return rsa->pssParams.saltLen;
}
/**
* Get whether PSS parameters set.
*
* @param [in] rsa RSA key object.
* @return PSS parameters set.
*/
int wp_rsa_get_pss_params_set(wp_Rsa* rsa)
{
return rsa->pssDefSet;
}
/**
* Create a new RSA key. Base function.
*
* @param [in] provCtx Provider context.
* @param [in] type Type of RSA key: PKCS#1.5 or PSS.
* @return NULL on failure.
* @return New RSA key object on success.
*/
static wp_Rsa* wp_rsa_base_new(WOLFPROV_CTX* provCtx, int type)
{
wp_Rsa* rsa = NULL;
if (wolfssl_prov_is_running()) {
rsa = (wp_Rsa*)OPENSSL_zalloc(sizeof(*rsa));
}
if (rsa != NULL) {
int ok = 1;
int rc;
rc = wc_InitRsaKey(&rsa->key, NULL);
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_InitRsaKey", rc);
ok = 0;
}
#ifndef SINGLE_THREADED
if (ok) {
rc = wc_InitMutex(&rsa->mutex);
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_InitMutex", rc);
wc_FreeRsaKey(&rsa->key);
ok = 0;
}
}
#endif
if (ok) {
rsa->provCtx = provCtx;
rsa->type = type;
rsa->refCnt = 1;
}
if (!ok) {
OPENSSL_free(rsa);
rsa = NULL;
}
}
return rsa;
}
/**
* Dispose of RSA key object.
*
* @param [in, out] rsa RSA key object.
*/
void wp_rsa_free(wp_Rsa* rsa)
{
if (rsa != NULL) {
int cnt;
#ifndef WP_SINGLE_THREADED
int rc;
rc = wc_LockMutex(&rsa->mutex);
cnt = --rsa->refCnt;
if (rc == 0) {
wc_UnLockMutex(&rsa->mutex);
}
#else
cnt = --rsa->refCnt;
#endif
if (cnt == 0) {
#ifndef WP_SINGLE_THREADED
wc_FreeMutex(&rsa->mutex);
#endif
wc_FreeRsaKey(&rsa->key);
OPENSSL_free(rsa);
}
}
}
/**
* Duplicate specific parts of an RSA key object.
*
* @param [in] src Source RSA key object.
* @param [in] selection Parts of key to include.
* @return NULL on failure.
* @return New RSA key object on success.
*/
static wp_Rsa* wp_rsa_dup(const wp_Rsa* src, int selection)
{
wp_Rsa* dst = NULL;
if (wolfssl_prov_is_running() &&
(selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
/* Create a new rsa object. */
dst = wp_rsa_base_new(src->provCtx, src->type);
}
if (dst != NULL) {
int ok = 1;
int rc;
int i;
int cnt;
int copyPriv = (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0;
/* Determine number of multi-precision numbers to copy. */
if (copyPriv) {
cnt = WP_RSA_PARAM_NUMS_CNT;
}
else {
cnt = WP_RSA_PARAM_PUB_NUMS_CNT;
}
for (i = 0; ok && (i < cnt); i++) {
mp_int* src_mp = (mp_int*)(((byte*)&src->key) + wp_rsa_offset[i]);
mp_int* dst_mp = (mp_int*)(((byte*)&dst->key) + wp_rsa_offset[i]);
rc = mp_copy(src_mp, dst_mp);
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "mp_copy", rc);
ok = 0;
break;
}
}
if (ok) {
dst->bits = src->bits;
dst->hasPub = 1;
dst->hasPriv = copyPriv;
dst->pssParams = src->pssParams;
dst->pssDefSet = src->pssDefSet;
}
if (!ok) {
wp_rsa_free(dst);
dst = NULL;
}
}
return dst;
}
/**
* Set the PSS defaults.
*
* @param [in, out] pss PSS parameters object.
* @return 1 on success.
*/
static int wp_rsa_pss_params_set_pss_defaults(wp_RsaPssParams* pss)
{
WOLFPROV_ENTER(WP_LOG_COMP_RSA, "wp_rsa_pss_params_set_pss_defaults");
pss->hashType = WP_RSA_PSS_DIGEST_DEF;
pss->mgf = WP_RSA_PSS_MGF_DEF;
XSTRNCPY(pss->mdName, "SHA-1", sizeof(pss->mdName));
XSTRNCPY(pss->mgfMdName, "SHA-1", sizeof(pss->mdName));
pss->saltLen = WP_RSA_DEFAULT_SALT_LEN;
pss->derTrailer = 1; /* Default: RFC8017 A.2.3 */
WOLFPROV_LEAVE(WP_LOG_COMP_RSA, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), 1);
return 1;
}
/**
* Setup the MGF1 digest algorithm based on name and properties.
*
* @param [in, out] pss RSA PSS parameters object.
* @param [in] mdName Name of digest.
* @param [in] mdProps Digest properties.
* @param [in] libCtx Library context.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_rsa_pss_params_setup_mgf1_md(wp_RsaPssParams* pss,
const char* mdName, const char* mdProps, OSSL_LIB_CTX* libCtx)
{
int ok = 1;
WOLFPROV_ENTER(WP_LOG_COMP_RSA, "wp_rsa_pss_params_setup_mgf1_md");
OPENSSL_strlcpy(pss->mgfMdName, mdName, sizeof(pss->mgfMdName));
pss->mgf = wp_name_to_wc_mgf(libCtx, mdName, mdProps);
if (pss->mgf == WC_MGF1NONE) {
ok = 0;
}
WOLFPROV_LEAVE(WP_LOG_COMP_RSA, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Setup the digest based on name and properties.
*
* @param [in, out] pss RSA PSS parameters object.
* @param [in] mdName Name of digest.
* @param [in] mdProps Digest properties.
* @param [in] libCtx Library context.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_rsa_pss_params_setup_md(wp_RsaPssParams* pss, const char* mdName,
const char* mdProps, OSSL_LIB_CTX* libCtx)
{
int ok = 1;
WOLFPROV_ENTER(WP_LOG_COMP_RSA, "wp_rsa_pss_params_setup_md");
pss->hashType = wp_name_to_wc_hash_type(libCtx, mdName, mdProps);
if ((pss->hashType == WC_HASH_TYPE_NONE) ||
(pss->hashType == WC_HASH_TYPE_MD5)) {
ok = 0;
}
if (ok) {
OPENSSL_strlcpy(pss->mdName, mdName, sizeof(pss->mdName));
if (!wp_rsa_pss_params_setup_mgf1_md(pss, mdName, mdProps, libCtx)) {
ok = 0;
}
}
WOLFPROV_LEAVE(WP_LOG_COMP_RSA, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Set the digest to use into RSA PSS parameters object.
*
* @param [in, out] pss RSA PSS parameters object.
* @param [in] p Parameter object.
* @param [in] propsParam Parameter containing properties.
* @param [in] libCtx Library context.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_rsa_pss_params_set_digest(wp_RsaPssParams* pss,
const OSSL_PARAM* p, const OSSL_PARAM* propsParam, OSSL_LIB_CTX* libCtx)
{
int ok = 1;
char mdName[WP_MAX_MD_NAME_SIZE] = "";
char* pMdName = mdName;
char mdProps[WP_MAX_MD_NAME_SIZE] = "";
char* pMdProps = NULL;
WOLFPROV_ENTER(WP_LOG_COMP_RSA, "wp_rsa_pss_params_set_digest");
if (!OSSL_PARAM_get_utf8_string(p, &pMdName, sizeof(mdName))) {
ok = 0;
}
if (ok && propsParam != NULL) {
pMdProps = mdProps;
if (!OSSL_PARAM_get_utf8_string(propsParam, &pMdProps,
sizeof(mdProps))) {
ok = 0;
}
}
if (ok) {
ok = wp_rsa_pss_params_setup_md(pss, mdName, mdProps, libCtx);
}
WOLFPROV_LEAVE(WP_LOG_COMP_RSA, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Set the digest to use into RSA PSS parameters object.
*
* @param [in, out] pss RSA PSS parameters object.
* @param [in] p Parameter object.
* @param [in] propsParam Parameter containing properties.
* @param [in] libCtx Library context.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_rsa_pss_params_set_mgf1_digest(wp_RsaPssParams* pss,
const OSSL_PARAM* p, const OSSL_PARAM* propsParam, OSSL_LIB_CTX* libCtx)
{
int ok = 1;
char mdName[WP_MAX_MD_NAME_SIZE] = "";
char* pMdName = mdName;
char mdProps[WP_MAX_MD_NAME_SIZE] = "";
char* pMdProps = NULL;
WOLFPROV_ENTER(WP_LOG_COMP_RSA, "wp_rsa_pss_params_set_mgf1_digest");
if (!OSSL_PARAM_get_utf8_string(p, &pMdName, sizeof(mdName))) {
ok = 0;
}
if (ok && propsParam != NULL) {
pMdProps = mdProps;
if (!OSSL_PARAM_get_utf8_string(propsParam, &pMdProps,
sizeof(mdProps))) {
ok = 0;
}
}
if (ok) {
ok = wp_rsa_pss_params_setup_mgf1_md(pss, mdName, mdProps, libCtx);
}
WOLFPROV_LEAVE(WP_LOG_COMP_RSA, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Set PSS parameters from the parameter array.
*
* @param [in, out] pss RSA PSS parameters object.
* @param [in, out] defaultsSet Whether default PSS parameters have been set.
* @param [in] params Array of parameters and values.
* @param [in] libCtx Library context.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_rsa_pss_params_set_params(wp_RsaPssParams* pss,
int* defaultsSet, const OSSL_PARAM params[], OSSL_LIB_CTX* libCtx)
{
int ok = 1;
const OSSL_PARAM* p;
const OSSL_PARAM* propsParam = NULL;
WOLFPROV_ENTER(WP_LOG_COMP_RSA, "wp_rsa_pss_params_set_params");
if (!defaultsSet) {
if (!wp_rsa_pss_params_set_pss_defaults(pss)) {
ok = 0;
}
else {
*defaultsSet = 1;
}
}
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST);
if (p != NULL) {
propsParam = OSSL_PARAM_locate_const(params,
OSSL_PKEY_PARAM_RSA_DIGEST_PROPS);
if (!wp_rsa_pss_params_set_digest(pss, p, propsParam, libCtx)) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MASKGENFUNC);
if ((p != NULL) && (p->data_type != OSSL_PARAM_UTF8_STRING)) {
ok = 0;
}
if ((p != NULL) && ok && (XSTRNCASECMP(p->data, SN_mgf1,
p->data_size) != 0)) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MGF1_DIGEST);
if ((p != NULL) && (propsParam == NULL)) {
propsParam = OSSL_PARAM_locate_const(params,
OSSL_PKEY_PARAM_RSA_DIGEST_PROPS);
}
if ((p != NULL) && !wp_rsa_pss_params_set_mgf1_digest(pss, p,
propsParam, libCtx)) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PSS_SALTLEN);
if ((p != NULL) && (!OSSL_PARAM_get_int(p, &pss->saltLen))) {
ok = 0;
}
}
WOLFPROV_LEAVE(WP_LOG_COMP_RSA, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Load the RSA key.
*
* Return the RSA key object taken out of the reference.
*
* @param [in, out] pRsa Pointer to a RSA key object.
* @param [in] size Size of data structure that is the RSA key object.
* Unused.
* @param [in] type Expected RSA type: PKCS#1.5 or PSS.
* @return NULL when no RSA key object at reference or not matching type.
* @return RSA key object from reference on success.
*/
static const wp_Rsa* wp_rsa_base_load(const wp_Rsa** pRsa, size_t size,
int type)
{
const wp_Rsa* rsa = *pRsa;
/* TODO: validate the object is a wp_Rsa? */
(void)size;
if (rsa->type != type) {
rsa = NULL;
}
else {
*pRsa = NULL;
}
return rsa;
}
/**
* Get the security bits for an RSA key.
*
* @param [in] rsa RSA key object.
* @return Security bits on success.
* @return 0 on failure.
*/
static int wp_rsa_get_security_bits(wp_Rsa* rsa)
{
int bits = 0;
if (rsa->bits >= 8192) {
bits = 192;
}
else if (rsa->bits >= 3072) {
bits = 128;
}
else if (rsa->bits >= 2048) {
bits = 112;
}
else if (rsa->bits >= 1024) {
bits = 80;
}
return bits;
}
/**
* Get the key data into the parameters.
*
* @param [in] rsa RSA key object.
* @param [in, out] params Array of parameters and values.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_rsa_get_params_key_data(wp_Rsa* rsa, OSSL_PARAM params[])
{
int ok = 1;
int i;
WOLFPROV_ENTER(WP_LOG_COMP_RSA, "wp_rsa_get_params_key_data");
for (i = 0; ok && (i < WP_RSA_PARAM_NUMS_CNT); i++) {
OSSL_PARAM* p = OSSL_PARAM_locate(params, wp_rsa_param_key[i]);
if (p != NULL) {
size_t oLen;
mp_int* mp = (mp_int*)(((byte*)&rsa->key) + wp_rsa_offset[i]);
oLen = mp_unsigned_bin_size(mp);
if (oLen > p->data_size) {
ok = 0;
}
if (ok && (p->data != NULL) &&
(!wp_mp_to_unsigned_bin_le(mp, p->data, oLen))) {
ok = 0;
}
p->return_size = oLen;
}
}
WOLFPROV_LEAVE(WP_LOG_COMP_RSA, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Convert a wolfCrypt hashType to the equivalent OpenSSL digest name.
*
* @param [in] hashType WolfProvider digest id.
* @param [out] osslDigest Corresponding OpenSSL digest name.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_digest_to_ossl_digest(enum wc_HashType hashType,
const char** osslDigest)
{
int ok = 1;
switch (hashType) {
case WC_HASH_TYPE_MD5:
*osslDigest = OSSL_DIGEST_NAME_MD5;
break;
case WC_HASH_TYPE_SHA:
*osslDigest = OSSL_DIGEST_NAME_SHA1;
break;
case WC_HASH_TYPE_SHA256:
*osslDigest = OSSL_DIGEST_NAME_SHA2_256;
break;
case WC_HASH_TYPE_SHA384:
*osslDigest = OSSL_DIGEST_NAME_SHA2_384;
break;
case WC_HASH_TYPE_SHA512:
*osslDigest = OSSL_DIGEST_NAME_SHA2_512;
break;
case WC_HASH_TYPE_NONE:
case WC_HASH_TYPE_MD2:
case WC_HASH_TYPE_MD4:
case WC_HASH_TYPE_SHA224:
case WC_HASH_TYPE_MD5_SHA:
case WC_HASH_TYPE_SHA3_224:
case WC_HASH_TYPE_SHA3_256:
case WC_HASH_TYPE_SHA3_384:
case WC_HASH_TYPE_SHA3_512:
case WC_HASH_TYPE_BLAKE2B:
case WC_HASH_TYPE_BLAKE2S:
#ifndef WOLFSSL_NOSHA512_224
case WC_HASH_TYPE_SHA512_224:
#endif
#ifndef WOLFSSL_NOSHA512_256
case WC_HASH_TYPE_SHA512_256:
#endif
#ifdef WOLFSSL_SHAKE128
case WC_HASH_TYPE_SHAKE128:
#endif
#ifdef WOLFSSL_SHAKE256
case WC_HASH_TYPE_SHAKE256:
#endif
#ifdef WOLFSSL_SM3
case WC_HASH_TYPE_SM3:
#endif
ok = 0;
}
return ok;
}
/**
* Get the PSS parameters into the parameters array.
*
* @param [in] pss PSS object.
* @param [in, out] params Array of parameters and values.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_rsa_get_params_pss(wp_RsaPssParams* pss, OSSL_PARAM params[])
{
int ok = 1;
OSSL_PARAM* p;
const char* osslDigest = NULL;
WOLFPROV_ENTER(WP_LOG_COMP_RSA, "wp_rsa_get_params_pss");
if (pss->hashType != WP_RSA_PSS_DIGEST_DEF) {
p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_RSA_DIGEST);