@@ -2266,15 +2266,16 @@ static int we_rsa_pkey_ctrl(EVP_PKEY_CTX *ctx, int type, int num, void *ptr)
22662266/**
22672267 * Extra operations for working with RSA.
22682268 * Supported operations include:
2269- * - "rsa_padding_mode": set the padding mode
2270- * - "rsa_pss_saltlen": set RSA-PSS salt length to use
2271- * - "rsa_keygen_bits": set size of RSA keys to generate in bits
2272- * - "rsa_mgf1_md": set the RSA-PSS MGF1 hash to use
2269+ * - rsa_padding_mode: set the padding mode.
2270+ * - rsa_pss_saltlen: set RSA-PSS salt length to use.
2271+ * - rsa_keygen_bits: set size of RSA keys to generate in bits.
2272+ * - rsa_mgf1_md: set the RSA-PSS MGF1 hash to use.
2273+ * - rsa_oaep_md: set the digest to use with OAEP padding.
2274+ * - rsa_keygen_pubexp: set public exponent to use when making a key.
22732275 *
2274- * @param ctx [in] Public key context of operation.
2275- * @param type [in] Type of operation to perform.
2276- * @param num [in] Integer parameter.
2277- * @param ptr [in] Pointer parameter.
2276+ * @param ctx [in] Public key context of operation.
2277+ * @param type [in] Type of operation to perform.
2278+ * @param value [in] Control string dependent value.
22782279 * @returns 1 on success and 0 on failure.
22792280 */
22802281static int we_rsa_pkey_ctrl_str (EVP_PKEY_CTX * ctx , const char * type ,
@@ -2296,22 +2297,22 @@ static int we_rsa_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
22962297 ret = 0 ;
22972298 }
22982299
2299- if ((ret == 1 ) && (XSTRNCMP (type , "rsa_padding_mode" , 17 ) == 0 )) {
2300+ if ((ret == 1 ) && (XSTRCMP (type , "rsa_padding_mode" ) == 0 )) {
23002301 /* Padding mode. */
2301- if (XSTRNCMP (value , "none" , 5 ) == 0 ) {
2302+ if (XSTRCMP (value , "none" ) == 0 ) {
23022303 rsa -> padMode = RSA_NO_PADDING ;
23032304 }
2304- else if (XSTRNCMP (value , "pkcs1" , 6 ) == 0 ) {
2305+ else if (XSTRCMP (value , "pkcs1" ) == 0 ) {
23052306 rsa -> padMode = RSA_PKCS1_PADDING ;
23062307 }
2307- else if (XSTRNCMP (value , "oaep" , 5 ) == 0 ) {
2308+ else if (XSTRCMP (value , "oaep" ) == 0 ) {
23082309 rsa -> padMode = RSA_PKCS1_OAEP_PADDING ;
23092310 }
2310- else if (XSTRNCMP (value , "pss" , 4 ) == 0 ) {
2311+ else if (XSTRCMP (value , "pss" ) == 0 ) {
23112312 rsa -> padMode = RSA_PKCS1_PSS_PADDING ;
23122313 }
23132314 #ifdef WE_HAVE_RSA_X931
2314- else if (XSTRNCMP (value , "x931 ", 5 ) == 0 ) {
2315+ else if (XSTRCMP (value , "x931 ") == 0 ) {
23152316 rsa -> padMode = RSA_X931_PADDING ;
23162317 }
23172318 #endif
@@ -2326,19 +2327,19 @@ static int we_rsa_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
23262327 rsa -> md = EVP_sha1 ();
23272328 }
23282329 }
2329- else if ((ret == 1 ) && (XSTRNCMP (type , "rsa_pss_saltlen" , 16 ) == 0 )) {
2330+ else if ((ret == 1 ) && (XSTRCMP (type , "rsa_pss_saltlen" ) == 0 )) {
23302331 /* RSA-PSS salt length. */
23312332 if (rsa -> padMode != RSA_PKCS1_PSS_PADDING ) {
23322333 ret = 0 ;
23332334 }
23342335 #if OPENSSL_VERSION_NUMBER >= 0x10101000L
2335- else if (XSTRNCMP (value , "digest ", 7 ) == 0 ) {
2336+ else if (XSTRCMP (value , "digest ") == 0 ) {
23362337 rsa -> saltLen = RSA_PSS_SALTLEN_DIGEST ;
23372338 }
2338- else if (XSTRNCMP (value , "max ", 4 ) == 0 ) {
2339+ else if (XSTRCMP (value , "max ") == 0 ) {
23392340 rsa -> saltLen = RSA_PSS_SALTLEN_MAX ;
23402341 }
2341- else if (XSTRNCMP (value , "auto", 5 ) == 0 ) {
2342+ else if (XSTRCMP (value , "auto") == 0 ) {
23422343 #if OPENSSL_VERSION_NUMBER >= 0x10101000L
23432344 rsa -> saltLen = RSA_PSS_SALTLEN_AUTO ;
23442345 #else
@@ -2347,10 +2348,20 @@ static int we_rsa_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
23472348 }
23482349 #endif
23492350 else {
2350- rsa -> saltLen = XATOI (value );
2351+ int len ;
2352+
2353+ len = XATOI (value );
2354+ WOLFENGINE_MSG (WE_LOG_PK , "Setting PSS salt length to %d" , len );
2355+ if (len < 0 ) {
2356+ WOLFENGINE_ERROR_MSG (WE_LOG_PK , "Negative PSS salt length." );
2357+ ret = 0 ;
2358+ }
2359+ else {
2360+ rsa -> saltLen = len ;
2361+ }
23512362 }
23522363 }
2353- else if ((ret == 1 ) && (XSTRNCMP (type , "rsa_keygen_bits ", 16 ) == 0 )) {
2364+ else if ((ret == 1 ) && (XSTRCMP (type , "rsa_keygen_bits ") == 0 )) {
23542365 /* Size, in bits, of RSA key to generate. */
23552366 bits = XATOI (value );
23562367 ret = we_check_rsa_key_size (bits , 0 );
@@ -2362,7 +2373,7 @@ static int we_rsa_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
23622373 rsa -> bits = bits ;
23632374 }
23642375 }
2365- else if ((ret == 1 ) && (XSTRNCMP (type , "rsa_mgf1_md ", 12 ) == 0 )) {
2376+ else if ((ret == 1 ) && (XSTRCMP (type , "rsa_mgf1_md ") == 0 )) {
23662377 if ((rsa -> padMode != RSA_PKCS1_OAEP_PADDING ) &&
23672378 (rsa -> padMode != RSA_PKCS1_PSS_PADDING )) {
23682379 WOLFENGINE_ERROR_MSG (WE_LOG_PK , "Setting MGF1 and not PSS or OAEP" );
@@ -2376,7 +2387,7 @@ static int we_rsa_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
23762387 }
23772388 }
23782389 }
2379- else if ((ret == 1 ) && (XSTRNCMP (type , "rsa_oaep_md ", 12 ) == 0 )) {
2390+ else if ((ret == 1 ) && (XSTRCMP (type , "rsa_oaep_md ") == 0 )) {
23802391 if (rsa -> padMode != RSA_PKCS1_OAEP_PADDING ) {
23812392 WOLFENGINE_ERROR_MSG (WE_LOG_PK , "Setting MD and not OAEP" );
23822393 ret = -2 ;
@@ -2392,6 +2403,23 @@ static int we_rsa_pkey_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
23922403 }
23932404 }
23942405 }
2406+ else if ((ret == 1 ) && (XSTRCMP (type , "rsa_keygen_pubexp ") == 0 )) {
2407+ int e ;
2408+
2409+ e = XATOI (value );
2410+ WOLFENGINE_MSG (WE_LOG_PK , "Setting public exponent (e) to %d" , e );
2411+ if (e < 0 ) {
2412+ WOLFENGINE_ERROR_MSG (WE_LOG_PK , "Negative public exponent." );
2413+ ret = 0 ;
2414+ }
2415+ else if (e == 0 ) {
2416+ WOLFENGINE_ERROR_MSG (WE_LOG_PK , "Zero public exponent." );
2417+ ret = 0 ;
2418+ }
2419+ else {
2420+ rsa -> pubExp = e ;
2421+ }
2422+ }
23952423 else {
23962424 /* Unsupported string. */
23972425 XSNPRINTF (errBuff , sizeof (errBuff ), "Unsupported ctrl string: %s" ,
0 commit comments