116116 * @param [in, out] info Info for encryption.
117117 * @param [in] heap Dynamic memory allocation hint.
118118 * @param [out] der Holds DER encoded data.
119+ * @param [out] algId Algorithm identifier for private keys.
119120 * @return 0 on success.
120121 * @return NOT_COMPILED_IN when format is PEM and PEM not supported.
121122 * @return ASN_PARSE_E when format is ASN.1 and invalid DER encoding.
122123 * @return MEMORY_E when dynamic memory allocation fails.
123124 */
124125static int DataToDerBuffer (const unsigned char * buff , word32 len , int format ,
125- int type , EncryptedInfo * info , void * heap , DerBuffer * * der )
126+ int type , EncryptedInfo * info , void * heap , DerBuffer * * der , int * algId )
126127{
127128 int ret ;
128129
@@ -131,7 +132,7 @@ static int DataToDerBuffer(const unsigned char* buff, word32 len, int format,
131132 /* Data in buffer has PEM format - extract DER data. */
132133 if (format == WOLFSSL_FILETYPE_PEM ) {
133134 #ifdef WOLFSSL_PEM_TO_DER
134- ret = PemToDer (buff , len , type , der , heap , info , NULL );
135+ ret = PemToDer (buff , len , type , der , heap , info , algId );
135136 if (ret != 0 ) {
136137 FreeDer (der );
137138 }
@@ -341,7 +342,7 @@ static int ProcessUserChain(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
341342
342343 /* Get a certificate as DER. */
343344 ret = DataToDerBuffer (buff + consumed , (word32 )(sz - consumed ),
344- format , type , info , heap , & part );
345+ format , type , info , heap , & part , NULL );
345346 if (ret == 0 ) {
346347 /* Process the user certificate. */
347348 ret = ProcessUserCert (ctx -> cm , & part , type , verify ,
@@ -604,6 +605,12 @@ static int ProcessBufferTryDecodeEcc(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
604605 idx = 0 ;
605606 ret = wc_EccPublicKeyDecode (der -> buffer , & idx , key , der -> length );
606607 }
608+ #endif
609+ #ifdef WOLFSSL_SM2
610+ if (* keyFormat == SM2k ) {
611+ ret = wc_ecc_set_curve (key , WOLFSSL_SM2_KEY_BITS / 8 ,
612+ ECC_SM2P256V1 );
613+ }
607614 #endif
608615 if (ret == 0 ) {
609616 /* Get the minimum ECC key size from SSL or SSL context object. */
@@ -1317,52 +1324,53 @@ static void ProcessBufferPrivKeyHandleDer(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
13171324 * @param [in] heap Dynamic memory allocation hint.
13181325 * @param [in] type Type of data:
13191326 * PRIVATEKEY_TYPE or ALT_PRIVATEKEY_TYPE.
1327+ * @param [in] algId Algorithm id of key.
13201328 * @return 0 on success.
13211329 * @return WOLFSSL_BAD_FILE when not able to decode.
13221330 */
13231331static int ProcessBufferPrivateKey (WOLFSSL_CTX * ctx , WOLFSSL * ssl ,
1324- DerBuffer * der , int format , EncryptedInfo * info , void * heap , int type )
1332+ DerBuffer * der , int format , EncryptedInfo * info , void * heap , int type ,
1333+ int algId )
13251334{
13261335 int ret ;
1327- int keyFormat = 0 ;
13281336#if (defined(WOLFSSL_ENCRYPTED_KEYS ) && !defined(NO_PWDBASED )) || \
13291337 defined(HAVE_PKCS8 )
1330- word32 algId = 0 ;
1338+ word32 p8AlgId = 0 ;
13311339#endif
13321340
13331341 (void )info ;
13341342 (void )format ;
13351343
13361344#ifdef HAVE_PKCS8
13371345 /* Try and remove PKCS8 header and get algorithm id. */
1338- ret = ToTraditional_ex (der -> buffer , der -> length , & algId );
1346+ ret = ToTraditional_ex (der -> buffer , der -> length , & p8AlgId );
13391347 if (ret > 0 ) {
13401348 /* Header stripped inline. */
13411349 der -> length = ret ;
1342- keyFormat = algId ;
1350+ algId = p8AlgId ;
13431351 }
13441352#endif
13451353
13461354 /* Put the data into the SSL or SSL context object. */
13471355 ProcessBufferPrivKeyHandleDer (ctx , ssl , & der , type );
13481356 /* Try to decode the DER data. */
1349- ret = ProcessBufferTryDecode (ctx , ssl , der , & keyFormat , heap , type );
1357+ ret = ProcessBufferTryDecode (ctx , ssl , der , & algId , heap , type );
13501358
13511359#if defined(WOLFSSL_ENCRYPTED_KEYS ) && !defined(NO_PWDBASED )
13521360 /* If private key type PKCS8 header wasn't already removed (algId == 0). */
1353- if (((ret != 0 ) || (keyFormat == 0 )) && (format != WOLFSSL_FILETYPE_PEM ) &&
1361+ if (((ret != 0 ) || (algId == 0 )) && (format != WOLFSSL_FILETYPE_PEM ) &&
13541362 (info -> passwd_cb != NULL ) && (algId == 0 )) {
13551363 /* Try to decrypt DER data as a PKCS#8 private key. */
13561364 ret = ProcessBufferPrivPkcs8Dec (info , der , heap );
13571365 if (ret >= 0 ) {
13581366 /* Try to decode decrypted data. */
1359- ret = ProcessBufferTryDecode (ctx , ssl , der , & keyFormat , heap , type );
1367+ ret = ProcessBufferTryDecode (ctx , ssl , der , & algId , heap , type );
13601368 }
13611369 }
13621370#endif /* WOLFSSL_ENCRYPTED_KEYS && !NO_PWDBASED */
13631371
1364- /* Check if we were able to determine key format . */
1365- if ((ret == 0 ) && (keyFormat == 0 )) {
1372+ /* Check if we were able to determine algorithm id . */
1373+ if ((ret == 0 ) && (algId == 0 )) {
13661374 #ifdef OPENSSL_EXTRA
13671375 /* Decryption password is probably wrong. */
13681376 if (info -> passwd_cb ) {
@@ -2265,6 +2273,7 @@ int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff, long sz,
22652273#else
22662274 EncryptedInfo info [1 ];
22672275#endif
2276+ int algId = 0 ;
22682277
22692278 WOLFSSL_ENTER ("ProcessBuffer" );
22702279
@@ -2306,7 +2315,8 @@ int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff, long sz,
23062315 #endif
23072316
23082317 /* Get the DER data for a private key or certificate. */
2309- ret = DataToDerBuffer (buff , (word32 )sz , format , type , info , heap , & der );
2318+ ret = DataToDerBuffer (buff , (word32 )sz , format , type , info , heap , & der ,
2319+ & algId );
23102320 if (used != NULL ) {
23112321 /* Update to amount used/consumed. */
23122322 * used = info -> consumed ;
@@ -2321,7 +2331,8 @@ int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff, long sz,
23212331
23222332 if ((ret == 0 ) && IS_PRIVKEY_TYPE (type )) {
23232333 /* Process the private key. */
2324- ret = ProcessBufferPrivateKey (ctx , ssl , der , format , info , heap , type );
2334+ ret = ProcessBufferPrivateKey (ctx , ssl , der , format , info , heap , type ,
2335+ algId );
23252336 #ifdef WOLFSSL_SMALL_STACK
23262337 /* Info no longer needed - keep max memory usage down. */
23272338 XFREE (info , heap , DYNAMIC_TYPE_ENCRYPTEDINFO );
0 commit comments