@@ -291,11 +291,7 @@ static int der_write_to_bio_as_pem(const unsigned char* der, int derSz,
291291#endif
292292#endif
293293
294- #if defined(OPENSSL_EXTRA ) && \
295- ((!defined(NO_RSA ) && defined(WOLFSSL_KEY_GEN )) || \
296- (!defined(NO_DH ) && defined(WOLFSSL_DH_EXTRA )) || \
297- (defined(HAVE_ECC ) && defined(WOLFSSL_KEY_GEN )))
298- #if !defined(NO_FILESYSTEM )
294+ #if defined(OPENSSL_EXTRA ) && !defined(NO_FILESYSTEM )
299295/* Write the DER data as PEM into file pointer.
300296 *
301297 * @param [in] der Buffer containing DER data.
@@ -325,8 +321,7 @@ static int der_write_to_file_as_pem(const unsigned char* der, int derSz,
325321 XFREE (pem , NULL , DYNAMIC_TYPE_TMP_BUFFER );
326322 return ret ;
327323}
328- #endif
329- #endif
324+ #endif /* OPENSSL_EXTRA && !NO_FILESYSTEM */
330325
331326#if defined(OPENSSL_EXTRA ) && defined(WOLFSSL_KEY_GEN ) && \
332327 defined(WOLFSSL_PEM_TO_DER )
@@ -6166,10 +6161,8 @@ int wolfSSL_PEM_write_bio_PrivateKey(WOLFSSL_BIO* bio, WOLFSSL_EVP_PKEY* key,
61666161#endif /* !NO_BIO */
61676162
61686163#ifndef NO_FILESYSTEM
6164+ #ifndef NO_CERTS
61696165/* Writes a public key to a file pointer encoded in PEM format.
6170- *
6171- * Mirrors wolfSSL_PEM_read_PUBKEY: convert the EVP_PKEY to public-key DER and
6172- * write it with the generic PUBLIC KEY PEM type.
61736166 *
61746167 * @param [in] fp File pointer to write to.
61756168 * @param [in] key Public key to write in PEM format.
@@ -6178,30 +6171,39 @@ int wolfSSL_PEM_write_bio_PrivateKey(WOLFSSL_BIO* bio, WOLFSSL_EVP_PKEY* key,
61786171 */
61796172int wolfSSL_PEM_write_PUBKEY (XFILE fp , WOLFSSL_EVP_PKEY * key )
61806173{
6181- int ret = 0 ;
6182- #if !defined(NO_ASN ) && !defined(NO_PWDBASED )
6183- unsigned char * der = NULL ;
6184- int derSz ;
6185- #endif
6174+ int err = 0 ;
6175+ unsigned char * derBuf = NULL ;
6176+ int derSz = 0 ;
61866177
61876178 WOLFSSL_ENTER ("wolfSSL_PEM_write_PUBKEY" );
61886179
6180+ /* Validate parameters. */
61896181 if ((fp == XBADFILE ) || (key == NULL )) {
61906182 WOLFSSL_MSG ("Bad Function Arguments" );
6191- return 0 ;
6183+ err = 1 ;
61926184 }
61936185
6194- #if !defined(NO_ASN ) && !defined(NO_PWDBASED )
6195- derSz = wolfSSL_i2d_PUBKEY (key , & der );
6196- if (derSz > 0 ) {
6197- ret = der_write_to_file_as_pem (der , derSz , fp , PUBLICKEY_TYPE , NULL );
6186+ /* Encode the public key as DER. */
6187+ if (!err ) {
6188+ derSz = wolfSSL_i2d_PUBKEY (key , & derBuf );
6189+ if (derSz <= 0 ) {
6190+ WOLFSSL_MSG ("Failed to convert key to DER" );
6191+ err = 1 ;
6192+ }
61986193 }
6199- XFREE (der , NULL , DYNAMIC_TYPE_PUBLIC_KEY );
6200- #else
6201- WOLFSSL_MSG ("i2d_PUBKEY not supported in this build" );
6202- #endif
62036194
6204- return ret ;
6195+ /* Write DER buffer to file as PEM. */
6196+ if ((!err ) && (der_write_to_file_as_pem (derBuf , derSz , fp ,
6197+ PUBLICKEY_TYPE , NULL ) != 1 )) {
6198+ WOLFSSL_MSG ("Failed to write DER to file as PEM" );
6199+ err = 1 ;
6200+ }
6201+
6202+ /* Dispose of the DER encoding. */
6203+ XFREE (derBuf , NULL , DYNAMIC_TYPE_TMP_BUFFER );
6204+
6205+ WOLFSSL_LEAVE ("wolfSSL_PEM_write_PUBKEY" , err );
6206+ return !err ;
62056207}
62066208
62076209/* Writes a private key to a file pointer encoded in PEM format.
@@ -6225,6 +6227,14 @@ int wolfSSL_PEM_write_PrivateKey(XFILE fp, WOLFSSL_EVP_PKEY* key,
62256227{
62266228 int err = 0 ;
62276229 int type = 0 ;
6230+ unsigned char * derBuf = NULL ;
6231+ int derSz = 0 ;
6232+
6233+ (void )cipher ;
6234+ (void )passwd ;
6235+ (void )len ;
6236+ (void )cb ;
6237+ (void )arg ;
62286238
62296239 WOLFSSL_ENTER ("wolfSSL_PEM_write_PrivateKey" );
62306240
@@ -6234,50 +6244,52 @@ int wolfSSL_PEM_write_PrivateKey(XFILE fp, WOLFSSL_EVP_PKEY* key,
62346244 err = 1 ;
62356245 }
62366246
6247+ /* Determine PEM type from key type, mirroring wolfSSL_PEM_read_PrivateKey's
6248+ * keyFormat switch. */
62376249 if (!err ) {
6238- /* Set PEM type based on key type, inverse of PEM_read_PrivateKey. */
62396250 switch (key -> type ) {
6240- #ifndef NO_RSA
62416251 case WC_EVP_PKEY_RSA :
62426252 type = PRIVATEKEY_TYPE ;
62436253 break ;
6244- #endif
6245- #ifndef NO_DSA
62466254 case WC_EVP_PKEY_DSA :
62476255 type = DSA_PRIVATEKEY_TYPE ;
62486256 break ;
6249- #endif
6250- #ifdef HAVE_ECC
62516257 case WC_EVP_PKEY_EC :
62526258 type = ECC_PRIVATEKEY_TYPE ;
62536259 break ;
6254- #endif
6255- #ifndef NO_DH
62566260 case WC_EVP_PKEY_DH :
62576261 type = DH_PRIVATEKEY_TYPE ;
62586262 break ;
6259- #endif
62606263 default :
6261- type = WOLFSSL_FATAL_ERROR ;
6264+ WOLFSSL_MSG ("Unknown key type" );
6265+ err = 1 ;
62626266 break ;
62636267 }
62646268 }
62656269
6266- if ((!err ) && (type == WOLFSSL_FATAL_ERROR )) {
6267- err = 1 ;
6270+ /* Encode the private key as DER. */
6271+ if (!err ) {
6272+ derSz = wolfSSL_i2d_PrivateKey (key , & derBuf );
6273+ if (derSz <= 0 ) {
6274+ WOLFSSL_MSG ("Error encoding private key as DER" );
6275+ err = 1 ;
6276+ }
62686277 }
62696278
6270- /* Write DER data as the selected PEM private key type. */
6271- if ((!err ) && (der_write_to_file_as_pem ((byte * )key -> pkey .ptr , key -> pkey_sz ,
6272- fp , type , NULL ) != 1 )) {
6279+ /* Write DER buffer to file as PEM. */
6280+ if ((!err ) && (der_write_to_file_as_pem (derBuf , derSz , fp , type ,
6281+ NULL ) != 1 )) {
6282+ WOLFSSL_MSG ("Error writing DER to file as PEM" );
62736283 err = 1 ;
62746284 }
6275-
62766285
6277- WOLFSSL_LEAVE ("wolfSSL_PEM_write_PrivateKey" , err );
6286+ /* Dispose of the DER encoding. */
6287+ XFREE (derBuf , NULL , DYNAMIC_TYPE_TMP_BUFFER );
62786288
6289+ WOLFSSL_LEAVE ("wolfSSL_PEM_write_PrivateKey" , err );
62796290 return !err ;
62806291}
6292+ #endif /* !NO_CERTS */
62816293#endif /* !NO_FILESYSTEM */
62826294
62836295#ifndef NO_BIO
0 commit comments