@@ -360,11 +360,13 @@ static int der_write_to_file_as_pem(const unsigned char* der, int derSz,
360360 * @param [in] passedSz Size of password in bytes.
361361 * @param [out] cipherInfo PEM cipher information lines.
362362 * @param [in] maxDerSz Maximum size of DER buffer.
363+ * @param [in] hashType Hash algorithm
363364 * @return 1 on success.
364365 * @return 0 on error.
365366 */
366367int EncryptDerKey (byte * der , int * derSz , const WOLFSSL_EVP_CIPHER * cipher ,
367- unsigned char * passwd , int passwdSz , byte * * cipherInfo , int maxDerSz )
368+ unsigned char * passwd , int passwdSz , byte * * cipherInfo , int maxDerSz ,
369+ int hashType )
368370{
369371 int ret = 0 ;
370372 int paddingSz = 0 ;
@@ -433,7 +435,7 @@ int EncryptDerKey(byte *der, int *derSz, const WOLFSSL_EVP_CIPHER* cipher,
433435
434436 /* Encrypt DER buffer. */
435437 ret = wc_BufferKeyEncrypt (info , der , (word32 )* derSz , passwd , passwdSz ,
436- WC_MD5 );
438+ hashType );
437439 if (ret != 0 ) {
438440 WOLFSSL_MSG ("encrypt key failed" );
439441 }
@@ -504,6 +506,14 @@ static int der_to_enc_pem_alloc(unsigned char* der, int derSz,
504506 byte * tmp = NULL ;
505507 byte * cipherInfo = NULL ;
506508 int pemSz = 0 ;
509+ int hashType = WC_HASH_TYPE_NONE ;
510+ #if !defined(NO_SHA256 )
511+ hashType = WC_SHA256 ;
512+ #elif !defined(NO_SHA )
513+ hashType = WC_SHA ;
514+ #elif !defined(NO_MD5 )
515+ hashType = WC_MD5 ;
516+ #endif
507517
508518 /* Macro doesn't always use it. */
509519 (void )heap ;
@@ -536,7 +546,7 @@ static int der_to_enc_pem_alloc(unsigned char* der, int derSz,
536546
537547 /* Encrypt DER inline. */
538548 ret = EncryptDerKey (der , & derSz , cipher , passwd , passwdSz ,
539- & cipherInfo , derSz + blockSz );
549+ & cipherInfo , derSz + blockSz , hashType );
540550 if (ret != 1 ) {
541551 WOLFSSL_ERROR_MSG ("EncryptDerKey failed" );
542552 }
@@ -5978,7 +5988,8 @@ int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa,
59785988 unsigned char * passwd , int passwdSz ,
59795989 unsigned char * * pem , int * pLen )
59805990{
5981- #if defined(WOLFSSL_PEM_TO_DER ) || defined(WOLFSSL_DER_TO_PEM )
5991+ #if (defined(WOLFSSL_PEM_TO_DER ) || defined(WOLFSSL_DER_TO_PEM )) && \
5992+ !defined(NO_MD5 )
59825993 byte * derBuf , * tmp , * cipherInfo = NULL ;
59835994 int der_max_len = 0 , derSz = 0 ;
59845995 const int type = DSA_PRIVATEKEY_TYPE ;
@@ -6024,8 +6035,8 @@ int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa,
60246035 if (passwd != NULL && passwdSz > 0 && cipher != NULL ) {
60256036 int ret ;
60266037
6027- ret = EncryptDerKey (derBuf , & derSz , cipher ,
6028- passwd , passwdSz , & cipherInfo , der_max_len );
6038+ ret = EncryptDerKey (derBuf , & derSz , cipher , passwd , passwdSz ,
6039+ & cipherInfo , der_max_len , WC_MD5 );
60296040 if (ret != 1 ) {
60306041 WOLFSSL_MSG ("EncryptDerKey failed" );
60316042 XFREE (derBuf , NULL , DYNAMIC_TYPE_DER );
@@ -6086,7 +6097,7 @@ int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa,
60866097 (void )pem ;
60876098 (void )pLen ;
60886099 return 0 ;
6089- #endif /* WOLFSSL_PEM_TO_DER || WOLFSSL_DER_TO_PEM */
6100+ #endif /* ( WOLFSSL_PEM_TO_DER || WOLFSSL_DER_TO_PEM) && !NO_MD5 */
60906101}
60916102
60926103#ifndef NO_FILESYSTEM
0 commit comments