@@ -292,11 +292,10 @@ static int der_write_to_bio_as_pem(const unsigned char* der, int derSz,
292292#endif
293293#endif
294294
295- #if defined(OPENSSL_EXTRA ) && \
296- ((!defined(NO_RSA ) && defined(WOLFSSL_KEY_GEN )) || \
297- (!defined(NO_DH ) && defined(WOLFSSL_DH_EXTRA )) || \
298- (defined(HAVE_ECC ) && defined(WOLFSSL_KEY_GEN )))
299- #if !defined(NO_FILESYSTEM )
295+ #if !defined(NO_FILESYSTEM ) && \
296+ ((defined(OPENSSL_EXTRA ) && !defined(NO_CERTS ) && !defined(NO_ASN ) && \
297+ !defined(NO_PWDBASED )) || \
298+ defined(WOLFSSL_DH_EXTRA ))
300299/* Write the DER data as PEM into file pointer.
301300 *
302301 * @param [in] der Buffer containing DER data.
@@ -326,8 +325,9 @@ static int der_write_to_file_as_pem(const unsigned char* der, int derSz,
326325 XFREE (pem , NULL , DYNAMIC_TYPE_TMP_BUFFER );
327326 return ret ;
328327}
329- #endif
330- #endif
328+ #endif /* !NO_FILESYSTEM &&
329+ * ((OPENSSL_EXTRA && !NO_CERTS && !NO_ASN && !NO_PWDBASED) ||
330+ * WOLFSSL_DH_EXTRA) */
331331
332332#if defined(OPENSSL_EXTRA ) && defined(WOLFSSL_KEY_GEN ) && \
333333 defined(WOLFSSL_PEM_TO_DER )
@@ -6282,6 +6282,166 @@ int wolfSSL_PEM_write_bio_PrivateKey(WOLFSSL_BIO* bio, WOLFSSL_EVP_PKEY* key,
62826282}
62836283#endif /* !NO_BIO */
62846284
6285+ #if !defined(NO_FILESYSTEM ) && !defined(NO_CERTS ) && defined(OPENSSL_EXTRA ) && \
6286+ !defined(NO_ASN ) && !defined(NO_PWDBASED )
6287+ /* Writes a public key to a file pointer encoded in PEM format.
6288+ *
6289+ * @param [in] fp File pointer to write to.
6290+ * @param [in] key Public key to write in PEM format.
6291+ * @return 1 on success.
6292+ * @return 0 on failure.
6293+ */
6294+ int wolfSSL_PEM_write_PUBKEY (XFILE fp , WOLFSSL_EVP_PKEY * key )
6295+ {
6296+ int err = 0 ;
6297+ unsigned char * derBuf = NULL ;
6298+ int derSz = 0 ;
6299+
6300+ WOLFSSL_ENTER ("wolfSSL_PEM_write_PUBKEY" );
6301+
6302+ if ((fp == XBADFILE ) || (key == NULL )) {
6303+ WOLFSSL_MSG ("Bad Function Arguments" );
6304+ err = 1 ;
6305+ }
6306+
6307+ if (!err ) {
6308+ derSz = wolfSSL_i2d_PUBKEY (key , NULL );
6309+ if (derSz <= 0 ) {
6310+ WOLFSSL_MSG ("Failed to get DER size for key" );
6311+ err = 1 ;
6312+ }
6313+ }
6314+
6315+ if (!err ) {
6316+ unsigned char * tmp ;
6317+ derBuf = (unsigned char * )XMALLOC ((size_t )derSz , NULL ,
6318+ DYNAMIC_TYPE_TMP_BUFFER );
6319+ if (derBuf == NULL ) {
6320+ WOLFSSL_MSG ("Failed to allocate DER buffer" );
6321+ err = 1 ;
6322+ }
6323+ else {
6324+ tmp = derBuf ;
6325+ if (wolfSSL_i2d_PUBKEY (key , & tmp ) <= 0 ) {
6326+ WOLFSSL_MSG ("Failed to convert key to DER" );
6327+ err = 1 ;
6328+ }
6329+ }
6330+ }
6331+
6332+ /* Write DER buffer to file as PEM. */
6333+ if ((!err ) && (der_write_to_file_as_pem (derBuf , derSz , fp ,
6334+ PUBLICKEY_TYPE , NULL ) != 1 )) {
6335+ WOLFSSL_MSG ("Failed to write DER to file as PEM" );
6336+ err = 1 ;
6337+ }
6338+
6339+ /* Dispose of the DER encoding. */
6340+ XFREE (derBuf , NULL , DYNAMIC_TYPE_TMP_BUFFER );
6341+
6342+ WOLFSSL_LEAVE ("wolfSSL_PEM_write_PUBKEY" , err );
6343+ return !err ;
6344+ }
6345+
6346+ /* Writes a private key to a file pointer encoded in PEM format.
6347+ *
6348+ * @param [in] fp File pointer to write to.
6349+ * @param [in] key Private key to write in PEM format.
6350+ * @param [in] cipher Encryption cipher to use. May be NULL.
6351+ * @param [in] passwd Password to use when encrypting. May be NULL.
6352+ * @param [in] len Length of password.
6353+ * @param [in] cb Password callback.
6354+ * @param [in] arg Password callback argument.
6355+ * @return 1 on success.
6356+ * @return 0 on failure.
6357+ */
6358+ int wolfSSL_PEM_write_PrivateKey (XFILE fp , WOLFSSL_EVP_PKEY * key ,
6359+ const WOLFSSL_EVP_CIPHER * cipher , unsigned char * passwd , int len ,
6360+ wc_pem_password_cb * cb , void * arg )
6361+ {
6362+ int err = 0 ;
6363+ int type = 0 ;
6364+ unsigned char * derBuf = NULL ;
6365+ int derSz = 0 ;
6366+
6367+ (void )cipher ;
6368+ (void )passwd ;
6369+ (void )len ;
6370+ (void )cb ;
6371+ (void )arg ;
6372+
6373+ WOLFSSL_ENTER ("wolfSSL_PEM_write_PrivateKey" );
6374+
6375+ /* Validate parameters. */
6376+ if ((fp == XBADFILE ) || (key == NULL )) {
6377+ WOLFSSL_MSG ("Bad Function Arguments" );
6378+ err = 1 ;
6379+ }
6380+
6381+ /* Determine PEM type from key type, mirroring wolfSSL_PEM_read_PrivateKey's
6382+ * keyFormat switch. */
6383+ if (!err ) {
6384+ switch (key -> type ) {
6385+ case WC_EVP_PKEY_RSA :
6386+ type = PRIVATEKEY_TYPE ;
6387+ break ;
6388+ case WC_EVP_PKEY_DSA :
6389+ type = DSA_PRIVATEKEY_TYPE ;
6390+ break ;
6391+ case WC_EVP_PKEY_EC :
6392+ type = ECC_PRIVATEKEY_TYPE ;
6393+ break ;
6394+ case WC_EVP_PKEY_DH :
6395+ type = DH_PRIVATEKEY_TYPE ;
6396+ break ;
6397+ default :
6398+ WOLFSSL_MSG ("Unknown key type" );
6399+ err = 1 ;
6400+ break ;
6401+ }
6402+ }
6403+
6404+ if (!err ) {
6405+ derSz = wolfSSL_i2d_PrivateKey (key , NULL );
6406+ if (derSz <= 0 ) {
6407+ WOLFSSL_MSG ("Failed to get DER size for private key" );
6408+ err = 1 ;
6409+ }
6410+ }
6411+
6412+ if (!err ) {
6413+ unsigned char * tmp ;
6414+ derBuf = (unsigned char * )XMALLOC ((size_t )derSz , NULL ,
6415+ DYNAMIC_TYPE_TMP_BUFFER );
6416+ if (derBuf == NULL ) {
6417+ WOLFSSL_MSG ("Failed to allocate DER buffer" );
6418+ err = 1 ;
6419+ }
6420+ else {
6421+ tmp = derBuf ;
6422+ if (wolfSSL_i2d_PrivateKey (key , & tmp ) <= 0 ) {
6423+ WOLFSSL_MSG ("Error encoding private key as DER" );
6424+ err = 1 ;
6425+ }
6426+ }
6427+ }
6428+
6429+ /* Write DER buffer to file as PEM. */
6430+ if ((!err ) && (der_write_to_file_as_pem (derBuf , derSz , fp , type ,
6431+ NULL ) != 1 )) {
6432+ WOLFSSL_MSG ("Error writing DER to file as PEM" );
6433+ err = 1 ;
6434+ }
6435+
6436+ /* Dispose of the DER encoding. */
6437+ XFREE (derBuf , NULL , DYNAMIC_TYPE_TMP_BUFFER );
6438+
6439+ WOLFSSL_LEAVE ("wolfSSL_PEM_write_PrivateKey" , err );
6440+ return !err ;
6441+ }
6442+ #endif /* !NO_FILESYSTEM && !NO_CERTS && OPENSSL_EXTRA && !NO_ASN &&
6443+ * !NO_PWDBASED */
6444+
62856445#ifndef NO_BIO
62866446/* Create a private key object from the data in the BIO.
62876447 *
0 commit comments