Skip to content

Commit 54bce87

Browse files
authored
Merge pull request #73 from ColtonWilley/wp_rsa_pss_encoding
Add PSS encoding for PKCS8 private keys now that wolfSSL supports it
2 parents c992acb + e2c0e24 commit 54bce87

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

include/wolfprovider/settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
#endif
115115
#ifndef NO_RSA
116116
#define WP_HAVE_RSA
117+
#if defined(WC_RSA_PSS) && LIBWOLFSSL_VERSION_HEX >= 0x05005000
118+
#define WOLFSSL_RSA_PSS_ENCODING
119+
#endif
117120
#endif
118121

119122
#ifdef HAVE_ECC

src/wp_rsa_kmgmt.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,17 +2215,25 @@ static int wp_rsa_encode_pub(const wp_Rsa* rsa, unsigned char* keyData,
22152215
}
22162216

22172217
/* RSAk defined in private header <wolfssl/wolfcrypt/asn.h> */
2218-
#define RSAk 645
2218+
#define RSAk 645
2219+
#if LIBWOLFSSL_VERSION_HEX >= 0x05005000
2220+
#define RSAPSSk 654
2221+
#define RSA_ALGO_ID(ctx) (ctx->type == RSA_FLAG_TYPE_RSASSAPSS \
2222+
? RSAPSSk : RSAk)
2223+
#else
2224+
#define RSA_ALGO_ID(ctx) RSAk
2225+
#endif
22192226

22202227
/**
22212228
* Get the PKCS#8 encoding size for the key.
22222229
*
22232230
* @param [in] rsa RSA key object.
22242231
* @param [out] keyLen Length of encoding in bytes.
2232+
* @param [in] algoId Algorithm ID to use.
22252233
* @return 1 on success.
22262234
* @return 0 on failure.
22272235
*/
2228-
static int wp_rsa_encode_pki_size(const wp_Rsa* rsa, size_t* keyLen)
2236+
static int wp_rsa_encode_pki_size(const wp_Rsa* rsa, size_t* keyLen, int algoId)
22292237
{
22302238
int ok = 1;
22312239
int ret;
@@ -2236,7 +2244,7 @@ static int wp_rsa_encode_pki_size(const wp_Rsa* rsa, size_t* keyLen)
22362244
ok = 0;
22372245
}
22382246
if (ok) {
2239-
ret = wc_CreatePKCS8Key(NULL, &len, NULL, ret, RSAk, NULL, 0);
2247+
ret = wc_CreatePKCS8Key(NULL, &len, NULL, ret, algoId, NULL, 0);
22402248
if (ret != LENGTH_ONLY_E) {
22412249
ok = 0;
22422250
}
@@ -2256,11 +2264,12 @@ static int wp_rsa_encode_pki_size(const wp_Rsa* rsa, size_t* keyLen)
22562264
* @param [out] keyData Buffer to hold encoded data.
22572265
* @param [in, out] keyLen On in, length of buffer in bytes.
22582266
* On out, length of encoding in bytes.
2267+
* @param [in] algoId Algorithm ID to use.
22592268
* @return 1 on success.
22602269
* @return 0 on failure.
22612270
*/
22622271
static int wp_rsa_encode_pki(const wp_Rsa* rsa, unsigned char* keyData,
2263-
size_t* keyLen)
2272+
size_t* keyLen, int algoId)
22642273
{
22652274
int ok = 1;
22662275
int ret;
@@ -2289,7 +2298,7 @@ static int wp_rsa_encode_pki(const wp_Rsa* rsa, unsigned char* keyData,
22892298
pkcs1Len = ret;
22902299
len = (word32)*keyLen;
22912300
ret = wc_CreatePKCS8Key(keyData, &len, pkcs1Data, (word32)pkcs1Len,
2292-
RSAk, NULL, 0);
2301+
algoId, NULL, 0);
22932302
if (ret <= 0) {
22942303
ok = 0;
22952304
}
@@ -2376,7 +2385,7 @@ static int wp_rsa_encode_enc_pki_size(const wp_RsaEncDecCtx* ctx,
23762385
byte fakeSalt[16];
23772386

23782387
/* Get encode private key length. */
2379-
ok = wp_rsa_encode_pki_size(rsa, &len);
2388+
ok = wp_rsa_encode_pki_size(rsa, &len, RSA_ALGO_ID(ctx));
23802389
if (ok) {
23812390
/* Get encrypted encode private key. */
23822391
if (wc_EncryptPKCS8Key(fakeData, len, NULL, &outSz, "", 0, WP_PKCS5,
@@ -2424,7 +2433,7 @@ static int wp_rsa_encode_enc_pki(const wp_RsaEncDecCtx* ctx, const wp_Rsa* rsa,
24242433
/* TODO: support salt length of 8 for DES3. */
24252434

24262435
/* Encode key. */
2427-
ok = wp_rsa_encode_pki_size(rsa, &len);
2436+
ok = wp_rsa_encode_pki_size(rsa, &len, RSA_ALGO_ID(ctx));
24282437
if (ok) {
24292438
/* Allocate buffer for encrypted key to be placed into. */
24302439
encodedKey = XMALLOC(len, NULL, DYNAMIC_TYPE_RSA_BUFFER);
@@ -2434,7 +2443,7 @@ static int wp_rsa_encode_enc_pki(const wp_RsaEncDecCtx* ctx, const wp_Rsa* rsa,
24342443
}
24352444
if (ok) {
24362445
/* Encode key. */
2437-
ok = wp_rsa_encode_pki(rsa, encodedKey, &len);
2446+
ok = wp_rsa_encode_pki(rsa, encodedKey, &len, RSA_ALGO_ID(ctx));
24382447
}
24392448
/* Generate salt. */
24402449
if (ok && (wc_RNG_GenerateBlock(wp_provctx_get_rng(ctx->provCtx), salt,
@@ -2473,12 +2482,13 @@ static int wp_rsa_encode_enc_pki(const wp_RsaEncDecCtx* ctx, const wp_Rsa* rsa,
24732482
* @return 1 on success.
24742483
* @return 0 on failure.
24752484
*/
2476-
static int wp_rsa_encode_epki_size(const wp_Rsa* rsa, size_t* keyLen)
2485+
static int wp_rsa_encode_epki_size(const wp_RsaEncDecCtx* ctx,
2486+
const wp_Rsa* rsa, size_t* keyLen)
24772487
{
24782488
int ok;
24792489
size_t len;
24802490

2481-
ok = wp_rsa_encode_pki_size(rsa, &len);
2491+
ok = wp_rsa_encode_pki_size(rsa, &len, RSA_ALGO_ID(ctx));
24822492
if (ok) {
24832493
*keyLen = ((len + 15) / 16) * 16;
24842494
}
@@ -2509,7 +2519,7 @@ static int wp_rsa_encode_epki(const wp_RsaEncDecCtx* ctx, const wp_Rsa* rsa,
25092519
size_t len = *keyLen;
25102520

25112521
/* Encode key. */
2512-
ok = wp_rsa_encode_pki(rsa, keyData, &len);
2522+
ok = wp_rsa_encode_pki(rsa, keyData, &len, RSA_ALGO_ID(ctx));
25132523
if (ok && (!wp_encrypt_key(ctx->provCtx, ctx->cipherName, keyData, keyLen,
25142524
(word32)len, pwCb, pwCbArg, cipherInfo))) {
25152525
ok = 0;
@@ -2571,13 +2581,13 @@ static int wp_rsa_encode(wp_RsaEncDecCtx* ctx, OSSL_CORE_BIO* cBio,
25712581
}
25722582
else
25732583
#endif
2574-
if (!wp_rsa_encode_pki_size(key, &derLen)) {
2584+
if (!wp_rsa_encode_pki_size(key, &derLen, RSA_ALGO_ID(ctx))) {
25752585
ok = 0;
25762586
}
25772587
}
25782588
#ifdef WOLFSSL_ENCRYPTED_KEYS
25792589
else if (ok && (ctx->format == WP_ENC_FORMAT_EPKI)) {
2580-
if (!wp_rsa_encode_epki_size(key, &derLen)) {
2590+
if (!wp_rsa_encode_epki_size(ctx, key, &derLen)) {
25812591
ok = 0;
25822592
}
25832593
}
@@ -2617,7 +2627,7 @@ static int wp_rsa_encode(wp_RsaEncDecCtx* ctx, OSSL_CORE_BIO* cBio,
26172627
}
26182628
else
26192629
#endif
2620-
if (!wp_rsa_encode_pki(key, derData, &derLen)) {
2630+
if (!wp_rsa_encode_pki(key, derData, &derLen, RSA_ALGO_ID(ctx))) {
26212631
ok = 0;
26222632
}
26232633
}

0 commit comments

Comments
 (0)