56fdfa2
commit 2fe812887139ce32eeca52f9a0c141bdc7c4c8af
56fdfa2
Author: Jakub Jelen <jjelen@redhat.com>
56fdfa2
Date:   Wed May 22 17:25:22 2019 +0200
56fdfa2
56fdfa2
    New PEM export format withou MD5
56fdfa2
56fdfa2
diff --git a/sshkey.c b/sshkey.c
56fdfa2
index b95ed0b1..1a271512 100644
56fdfa2
--- a/sshkey.c
56fdfa2
+++ b/sshkey.c
56fdfa2
@@ -3805,26 +3805,28 @@ sshkey_private_pem_to_blob(struct sshkey *key, struct sshbuf *blob,
56fdfa2
 	const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL;
56fdfa2
 	char *bptr;
56fdfa2
 	BIO *bio = NULL;
56fdfa2
+	EVP_PKEY *pkey = NULL;
56fdfa2
 
56fdfa2
 	if (len > 0 && len <= 4)
56fdfa2
 		return SSH_ERR_PASSPHRASE_TOO_SHORT;
56fdfa2
 	if ((bio = BIO_new(BIO_s_mem())) == NULL)
56fdfa2
 		return SSH_ERR_ALLOC_FAIL;
56fdfa2
+	if ((pkey = EVP_PKEY_new()) == NULL) {
56fdfa2
+		BIO_free(bio);
56fdfa2
+		return SSH_ERR_ALLOC_FAIL;
56fdfa2
+	}
56fdfa2
 
56fdfa2
 	switch (key->type) {
56fdfa2
 	case KEY_DSA:
56fdfa2
-		success = PEM_write_bio_DSAPrivateKey(bio, key->dsa,
56fdfa2
-		    cipher, passphrase, len, NULL, NULL);
56fdfa2
+		success = EVP_PKEY_set1_DSA(pkey, key->dsa);
56fdfa2
 		break;
56fdfa2
 #ifdef OPENSSL_HAS_ECC
56fdfa2
 	case KEY_ECDSA:
56fdfa2
-		success = PEM_write_bio_ECPrivateKey(bio, key->ecdsa,
56fdfa2
-		    cipher, passphrase, len, NULL, NULL);
56fdfa2
+		success = EVP_PKEY_set1_EC_KEY(pkey, key->ecdsa);
56fdfa2
 		break;
56fdfa2
 #endif
56fdfa2
 	case KEY_RSA:
56fdfa2
-		success = PEM_write_bio_RSAPrivateKey(bio, key->rsa,
56fdfa2
-		    cipher, passphrase, len, NULL, NULL);
56fdfa2
+		success = EVP_PKEY_set1_RSA(pkey, key->rsa);
56fdfa2
 		break;
56fdfa2
 	default:
56fdfa2
 		success = 0;
56fdfa2
@@ -3834,6 +3836,12 @@ sshkey_private_pem_to_blob(struct sshkey *key, struct sshbuf *blob,
56fdfa2
 		r = SSH_ERR_LIBCRYPTO_ERROR;
56fdfa2
 		goto out;
56fdfa2
 	}
56fdfa2
+	success = PEM_write_bio_PrivateKey(bio, pkey,
56fdfa2
+	    cipher, passphrase, len, NULL, NULL);
56fdfa2
+	if (success == 0) {
56fdfa2
+		r = SSH_ERR_LIBCRYPTO_ERROR;
56fdfa2
+		goto out;
56fdfa2
+	}
56fdfa2
 	if ((blen = BIO_get_mem_data(bio, &bptr)) <= 0) {
56fdfa2
 		r = SSH_ERR_INTERNAL_ERROR;
56fdfa2
 		goto out;
56fdfa2
@@ -3842,6 +3850,7 @@ sshkey_private_pem_to_blob(struct sshkey *key, struct sshbuf *blob,
56fdfa2
 		goto out;
56fdfa2
 	r = 0;
56fdfa2
  out:
56fdfa2
+	EVP_PKEY_free(pkey);
56fdfa2
 	BIO_free(bio);
56fdfa2
 	return r;
56fdfa2
 }
56fdfa2