Skip to content

Commit 5134ec4

Browse files
Merge pull request #96 from SparkiDev/alg_id_sig_enc_dec
RSA/RSA-PSS/ECC/ECX: fixes for DER encoding
2 parents 22f3584 + 8f12081 commit 5134ec4

File tree

11 files changed

+721
-64
lines changed

11 files changed

+721
-64
lines changed

include/wolfprovider/alg_funcs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ int wp_rsa_get_bits(wp_Rsa* rsa);
185185
RsaKey* wp_rsa_get_key(wp_Rsa* rsa);
186186
void wp_rsa_get_pss_mds(wp_Rsa* rsa, char** mdName, char** mgfMdName);
187187
int wp_rsa_get_pss_salt_len(wp_Rsa* rsa);
188+
int wp_rsa_get_pss_params_set(wp_Rsa* rsa);
188189
int wp_rsa_check_key_size(wp_Rsa* rsa, int allow1024);
190+
int wp_rsa_pss_encode_alg_id(const wp_Rsa* rsa, const char* mdName,
191+
const char* mgf1Name, int saltLen, byte* pssAlgId, word32* len);
189192

190193
/* Internal ECC types and functions. */
191194
typedef struct wp_Ecc wp_Ecc;

include/wolfprovider/internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@
9999
/** Maximum salt length for PKCS. */
100100
#define WP_MAX_SALT_SIZE 64
101101

102+
/** Default salt length for PSS. */
103+
#define WP_RSA_DEFAULT_SALT_LEN 20
102104

103105
/* These values are taken from ssl.h.
104106
* Can't include this header as it re-declares OpenSSL types.

include/wolfprovider/wp_logging.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,18 @@ int wolfProv_SetLogComponents(int componentMask);
142142
WOLFPROV_ERROR_FUNC_NULL_LINE(type, funcName, ret, __FILE__, __LINE__)
143143

144144
void WOLFPROV_ENTER(int type, const char* msg);
145-
void WOLFPROV_LEAVE(int type, const char* msg, int ret);
145+
/* Call the extended version of the API with the function name of the caller. */
146+
#ifdef _WIN32
147+
#define WOLFPROV_LEAVE(type, msg, ret) \
148+
WOLFPROV_LEAVE_EX(type, __FUNCTION__, msg, ret)
149+
#elif __STDC__ && __STDC_VERSION__ >= 199901L
150+
#define WOLFPROV_LEAVE(type, msg, ret) \
151+
WOLFPROV_LEAVE_EX(type, __func__, msg, ret)
152+
#else
153+
#define WOLFPROV_LEAVE(type, msg, ret) \
154+
WOLFPROV_LEAVE_EX(type, "", msg, ret)
155+
#endif
156+
void WOLFPROV_LEAVE_EX(int type, const char* func, const char* msg, int ret);
146157
void WOLFPROV_MSG(int type, const char* fmt, ...);
147158
void WOLFPROV_MSG_VERBOSE(int type, const char* fmt, ...);
148159
void WOLFPROV_ERROR_LINE(int type, int err, const char* file, int line);

src/wp_dec_epki2pki.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,22 @@ static int wp_epki2pki_decode(wp_Epki2Pki* ctx, OSSL_CORE_BIO* coreBio,
210210
done = 1;
211211
ok = 1;
212212
}
213+
if ((!done) && ok) {
214+
/* Try decrypting without password and look for ASN_PARSE_E to indicate
215+
* that the format is not PKCS#8 encrypted.
216+
* TODO: should be parsing the structure without decrypting to
217+
* determine it is encrypted PKCS#8.
218+
*/
219+
#if LIBWOLFSSL_VERSION_HEX >= 0x05000000
220+
rc = wc_DecryptPKCS8Key(data, len, password, 0);
221+
#else
222+
rc = wp_DecryptPKCS8Key(data, len, password, 0);
223+
#endif
224+
if (rc == ASN_PARSE_E) {
225+
done = 1;
226+
ok = 1;
227+
}
228+
}
213229
if ((!done) && ok && (!pwCb(password, sizeof(password), &passwordLen, NULL,
214230
pwCbArg))) {
215231
done = 1;

src/wp_dec_pem2der.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ static int wp_pem2der_decode_data(const unsigned char* data, word32 len,
294294
dataFormat = "SubjectPublicKeyInfo";
295295
obj = OSSL_OBJECT_PKEY;
296296
}
297-
else if (XMEMCMP(data, "-----BEGIN DH PARAMETERS-----", 29) == 0) {
297+
else if ((XMEMCMP(data, "-----BEGIN DH PARAMETERS-----", 29) == 0) ||
298+
(XMEMCMP(data, "-----BEGIN X9.42 DH PARAMETERS-----", 35) == 0)) {
298299
type = DH_PARAM_TYPE;
299300
dataType = NULL;
300301
dataFormat = "type-specific";

src/wp_ecc_kmgmt.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -866,8 +866,9 @@ static int wp_ecc_match(wp_Ecc* ecc1, wp_Ecc* ecc2, int selection)
866866
if (!wolfssl_prov_is_running()) {
867867
ok = 0;
868868
}
869+
/* Check the curve ID to see whether the parameters are the same. */
869870
if (ok && ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) &&
870-
(ecc1->key.dp->id != ecc2->key.dp->id)) {
871+
(ecc1->curveId != ecc2->curveId)) {
871872
ok = 0;
872873
}
873874
if (ok && ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)) {
@@ -2535,9 +2536,7 @@ static int wp_ecc_encode(wp_EccEncDecCtx* ctx, OSSL_CORE_BIO *cBio,
25352536
if (ok && ((ctx->format == WP_ENC_FORMAT_TYPE_SPECIFIC) ||
25362537
(ctx->format == WP_ENC_FORMAT_X9_62))) {
25372538
if (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) {
2538-
if (ctx->format == WP_ENC_FORMAT_X9_62) {
2539-
pemType = ECC_PRIVATEKEY_TYPE;
2540-
}
2539+
pemType = ECC_PRIVATEKEY_TYPE;
25412540
private = 1;
25422541
if (!wp_ecc_encode_priv(key, derData, &derLen)) {
25432542
ok = 0;

src/wp_ecx_kmgmt.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,8 +2571,22 @@ const OSSL_DISPATCH wp_ed25519_spki_decoder_functions[] = {
25712571
static int wp_Ed25519PublicKeyToDer(ed25519_key* key, byte* output,
25722572
word32 inLen)
25732573
{
2574-
/* Always include the algorithm. */
2575-
return wc_Ed25519PublicKeyToDer(key, output, inLen, 1);
2574+
int ok = 1;
2575+
2576+
/* Check if this is private key only. */
2577+
if (!key->pubKeySet) {
2578+
int rc;
2579+
/* Make the public key to encode. */
2580+
rc = wc_ed25519_make_public(key, key->p, ED25519_PUB_KEY_SIZE);
2581+
ok = key->pubKeySet = (rc == 0);
2582+
}
2583+
if (ok) {
2584+
/* Always include the algorithm. */
2585+
ok = wc_Ed25519PublicKeyToDer(key, output, inLen, 1);
2586+
}
2587+
2588+
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
2589+
return ok;
25762590
}
25772591

25782592
/**
@@ -3202,8 +3216,22 @@ const OSSL_DISPATCH wp_ed448_spki_decoder_functions[] = {
32023216
*/
32033217
static int wp_Ed448PublicKeyToDer(ed448_key* key, byte* output, word32 inLen)
32043218
{
3205-
/* Always include the algorithm. */
3206-
return wc_Ed448PublicKeyToDer(key, output, inLen, 1);
3219+
int ok = 1;
3220+
3221+
/* Check if this is private key only. */
3222+
if (!key->pubKeySet) {
3223+
int rc;
3224+
/* Make the public key to encode. */
3225+
rc = wc_ed448_make_public(key, key->p, ED448_PUB_KEY_SIZE);
3226+
ok = key->pubKeySet = (rc == 0);
3227+
}
3228+
if (ok) {
3229+
/* Always include the algorithm. */
3230+
ok = wc_Ed448PublicKeyToDer(key, output, inLen, 1);
3231+
}
3232+
3233+
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
3234+
return ok;
32073235
}
32083236

32093237
/**

src/wp_ecx_sig.c

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,30 +258,82 @@ static int wp_ecx_digest_verify_init(wp_EcxSigCtx *ctx, const char *mdName,
258258
}
259259

260260
/**
261-
* Put DER encoding of the ECX signature algorithm in the parameter object.
261+
* Put DER encoding of the Ed25519 signature algorithm in the parameter object.
262262
*
263263
* @param [in] ctx ECX signature context object.
264264
* @param [in] p Parameter object.
265265
* @return 1 on success.
266266
* @return 0 on failure.
267267
*/
268-
static int wp_ecx_get_alg_id(wp_EcxSigCtx *ctx, OSSL_PARAM *p)
268+
static int wp_ed25519_get_alg_id(wp_EcxSigCtx *ctx, OSSL_PARAM *p)
269269
{
270-
/* TODO: implement */
270+
/* Ed25519 Algorithm Id: SEQ OBJ 2b 65 70 */
271+
static const byte ed25519AlgId[] = {
272+
0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70,
273+
};
274+
275+
(void)ctx;
276+
277+
return OSSL_PARAM_set_octet_string(p, ed25519AlgId, sizeof(ed25519AlgId));
278+
}
279+
280+
/**
281+
* Put data from Ed25519 signture context object into parameter objects.
282+
*
283+
* @param [in] ctx ECX signature context object.
284+
* @param [in] params Array of parameter objects.
285+
* @return 1 on success.
286+
* @return 0 on failure.
287+
*/
288+
static int wp_ed25519_get_ctx_params(wp_EcxSigCtx *ctx, OSSL_PARAM *params)
289+
{
290+
int ok = 1;
291+
OSSL_PARAM *p;
292+
293+
if (ctx == NULL) {
294+
ok = 0;
295+
}
296+
297+
if (ok) {
298+
p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
299+
if (p != NULL) {
300+
ok = wp_ed25519_get_alg_id(ctx, p);
301+
}
302+
}
303+
304+
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
305+
return ok;
306+
}
307+
308+
/**
309+
* Put DER encoding of the Ed448 signature algorithm in the parameter object.
310+
*
311+
* @param [in] ctx ECX signature context object.
312+
* @param [in] p Parameter object.
313+
* @return 1 on success.
314+
* @return 0 on failure.
315+
*/
316+
static int wp_ed448_get_alg_id(wp_EcxSigCtx *ctx, OSSL_PARAM *p)
317+
{
318+
/* Ed448 Algorithm Id: SEQ OBJ 2b 65 71 */
319+
static const byte ed448AlgId[] = {
320+
0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x71,
321+
};
322+
271323
(void)ctx;
272-
(void)p;
273-
return 0;
324+
325+
return OSSL_PARAM_set_octet_string(p, ed448AlgId, sizeof(ed448AlgId));
274326
}
275327

276328
/**
277-
* Put data from ECX signture context object into parameter objects.
329+
* Put data from Ed448 signture context object into parameter objects.
278330
*
279331
* @param [in] ctx ECX signature context object.
280332
* @param [in] params Array of parameter objects.
281333
* @return 1 on success.
282334
* @return 0 on failure.
283335
*/
284-
static int wp_ecx_get_ctx_params(wp_EcxSigCtx *ctx, OSSL_PARAM *params)
336+
static int wp_ed448_get_ctx_params(wp_EcxSigCtx *ctx, OSSL_PARAM *params)
285337
{
286338
int ok = 1;
287339
OSSL_PARAM *p;
@@ -293,7 +345,7 @@ static int wp_ecx_get_ctx_params(wp_EcxSigCtx *ctx, OSSL_PARAM *params)
293345
if (ok) {
294346
p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
295347
if (p != NULL) {
296-
ok = wp_ecx_get_alg_id(ctx, p);
348+
ok = wp_ed448_get_alg_id(ctx, p);
297349
}
298350
}
299351

@@ -458,7 +510,7 @@ const OSSL_DISPATCH wp_ed25519_signature_functions[] = {
458510
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
459511
(DFUNC)wp_ecx_digest_verify_init },
460512
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY, (DFUNC)wp_ed25519_digest_verify },
461-
{ OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (DFUNC)wp_ecx_get_ctx_params },
513+
{ OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (DFUNC)wp_ed25519_get_ctx_params },
462514
{ OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
463515
(DFUNC)wp_ecx_gettable_ctx_params },
464516
{ 0, NULL }
@@ -606,7 +658,7 @@ const OSSL_DISPATCH wp_ed448_signature_functions[] = {
606658
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
607659
(DFUNC)wp_ecx_digest_verify_init },
608660
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY, (DFUNC)wp_ed448_digest_verify },
609-
{ OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (DFUNC)wp_ecx_get_ctx_params },
661+
{ OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (DFUNC)wp_ed448_get_ctx_params },
610662
{ OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
611663
(DFUNC)wp_ecx_gettable_ctx_params },
612664
{ 0, NULL }

src/wp_logging.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,18 +241,20 @@ void WOLFPROV_ENTER(int component, const char* msg)
241241
}
242242

243243
/**
244-
* Log function used to record function exit.
244+
* Log function used to record function exit. Extended for function name.
245245
*
246246
* @param component [IN] Component type, from wolfProv_LogComponents enum.
247+
* @param func [IN] Name of function that exitting.
247248
* @param msg [IN] Log message.
248249
* @param ret [IN] Value that function will be returning.
249250
*/
250-
void WOLFPROV_LEAVE(int component, const char* msg, int ret)
251+
void WOLFPROV_LEAVE_EX(int component, const char* func, const char* msg,
252+
int ret)
251253
{
252254
if (loggingEnabled) {
253255
char buffer[WOLFPROV_MAX_LOG_WIDTH];
254-
XSNPRINTF(buffer, sizeof(buffer), "wolfProv Leaving %s, return %d",
255-
msg, ret);
256+
XSNPRINTF(buffer, sizeof(buffer), "wolfProv Leaving %s, return %d (%s)",
257+
msg, ret, func);
256258
wolfprovider_log(WP_LOG_LEAVE, component, buffer);
257259
}
258260
}

0 commit comments

Comments
 (0)