4369a3
From eb0d8e708a1f958aecd2d6e2ff2450af488d4c2a Mon Sep 17 00:00:00 2001
4369a3
From: "djm@openbsd.org" <djm@openbsd.org>
4369a3
Date: Mon, 15 Jul 2019 13:16:29 +0000
4369a3
Subject: [PATCH] upstream: support PKCS8 as an optional format for storage of
4369a3
4369a3
private keys, enabled via "ssh-keygen -m PKCS8" on operations that save
4369a3
private keys to disk.
4369a3
4369a3
The OpenSSH native key format remains the default, but PKCS8 is a
4369a3
superior format to PEM if interoperability with non-OpenSSH software
4369a3
is required, as it may use a less terrible KDF (IIRC PEM uses a single
4369a3
round of MD5 as a KDF).
4369a3
4369a3
adapted from patch by Jakub Jelen via bz3013; ok markus
4369a3
4369a3
OpenBSD-Commit-ID: 027824e3bc0b1c243dc5188504526d73a55accb1
4369a3
---
4369a3
 authfile.c   |  6 ++--
4369a3
 ssh-keygen.1 |  9 +++---
4369a3
 ssh-keygen.c | 25 +++++++++--------
4369a3
 sshkey.c     | 78 +++++++++++++++++++++++++++++++++++++---------------
4369a3
 sshkey.h     | 11 ++++++--
4369a3
 5 files changed, 87 insertions(+), 42 deletions(-)
4369a3
4369a3
diff --git a/authfile.c b/authfile.c
4369a3
index 2166c1689..851c1a8a1 100644
4369a3
--- a/authfile.c
4369a3
+++ b/authfile.c
4369a3
@@ -74,7 +74,7 @@ sshkey_save_private_blob(struct sshbuf *keybuf, const char *filename)
4369a3
 int
4369a3
 sshkey_save_private(struct sshkey *key, const char *filename,
4369a3
     const char *passphrase, const char *comment,
4369a3
-    int force_new_format, const char *new_format_cipher, int new_format_rounds)
4369a3
+    int format, const char *openssh_format_cipher, int openssh_format_rounds)
4369a3
 {
4369a3
 	struct sshbuf *keyblob = NULL;
4369a3
 	int r;
4369a3
@@ -82,7 +82,7 @@ sshkey_save_private(struct sshkey *key, const char *filename,
4369a3
 	if ((keyblob = sshbuf_new()) == NULL)
4369a3
 		return SSH_ERR_ALLOC_FAIL;
4369a3
 	if ((r = sshkey_private_to_fileblob(key, keyblob, passphrase, comment,
4369a3
-	    force_new_format, new_format_cipher, new_format_rounds)) != 0)
4369a3
+	    format, openssh_format_cipher, openssh_format_rounds)) != 0)
4369a3
 		goto out;
4369a3
 	if ((r = sshkey_save_private_blob(keyblob, filename)) != 0)
4369a3
 		goto out;
4369a3
diff --git a/ssh-keygen.1 b/ssh-keygen.1
4369a3
index f42127c60..8184a1797 100644
4369a3
--- a/ssh-keygen.1
4369a3
+++ b/ssh-keygen.1
4369a3
@@ -419,11 +419,12 @@ The supported key formats are:
4369a3
 .Dq RFC4716
4369a3
 (RFC 4716/SSH2 public or private key),
4369a3
 .Dq PKCS8
4369a3
-(PEM PKCS8 public key)
4369a3
+(PKCS8 public or private key)
4369a3
 or
4369a3
 .Dq PEM
4369a3
 (PEM public key).
4369a3
-The default conversion format is
4369a3
+By default OpenSSH will write newly-generated private keys in its own
4369a3
+format, but when converting public keys for export the default format is
4369a3
 .Dq RFC4716 .
4369a3
 Setting a format of
4369a3
 .Dq PEM
4369a3
diff --git a/ssh-keygen.c b/ssh-keygen.c
4369a3
index b019a02ff..5dcad1f61 100644
4369a3
--- a/ssh-keygen.c
4369a3
+++ b/ssh-keygen.c
4369a3
@@ -147,11 +147,11 @@ static char *key_type_name = NULL;
4369a3
 /* Load key from this PKCS#11 provider */
4369a3
 static char *pkcs11provider = NULL;
4369a3
 
4369a3
-/* Use new OpenSSH private key format when writing SSH2 keys instead of PEM */
4369a3
-static int use_new_format = 1;
4369a3
+/* Format for writing private keys */
4369a3
+static int private_key_format = SSHKEY_PRIVATE_OPENSSH;
4369a3
 
4369a3
 /* Cipher for new-format private keys */
4369a3
-static char *new_format_cipher = NULL;
4369a3
+static char *openssh_format_cipher = NULL;
4369a3
 
4369a3
 /*
4369a3
  * Number of KDF rounds to derive new format keys /
4369a3
@@ -1048,7 +1048,8 @@ do_gen_all_hostkeys(struct passwd *pw)
4369a3
 		snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
4369a3
 		    hostname);
4369a3
 		if ((r = sshkey_save_private(private, prv_tmp, "",
4369a3
-		    comment, use_new_format, new_format_cipher, rounds)) != 0) {
4369a3
+		    comment, private_key_format, openssh_format_cipher,
4369a3
+		    rounds)) != 0) {
4369a3
 			error("Saving key \"%s\" failed: %s",
4369a3
 			    prv_tmp, ssh_err(r));
4369a3
 			goto failnext;
4369a3
@@ -1391,7 +1392,7 @@ do_change_passphrase(struct passwd *pw)
4369a3
 
4369a3
 	/* Save the file using the new passphrase. */
4369a3
 	if ((r = sshkey_save_private(private, identity_file, passphrase1,
4369a3
-	    comment, use_new_format, new_format_cipher, rounds)) != 0) {
4369a3
+	    comment, private_key_format, openssh_format_cipher, rounds)) != 0) {
4369a3
 		error("Saving key \"%s\" failed: %s.",
4369a3
 		    identity_file, ssh_err(r));
4369a3
 		explicit_bzero(passphrase1, strlen(passphrase1));
4369a3
@@ -1480,7 +1481,7 @@ do_change_comment(struct passwd *pw, const char *identity_comment)
4369a3
 	}
4369a3
 
4369a3
 	if (private->type != KEY_ED25519 && private->type != KEY_XMSS &&
4369a3
-	    !use_new_format) {
4369a3
+	    private_key_format != SSHKEY_PRIVATE_OPENSSH) {
4369a3
 		error("Comments are only supported for keys stored in "
4369a3
 		    "the new format (-o).");
4369a3
 		explicit_bzero(passphrase, strlen(passphrase));
4369a3
@@ -1514,7 +1515,8 @@ do_change_comment(struct passwd *pw, const char *identity_comment)
4369a3
 
4369a3
 	/* Save the file using the new passphrase. */
4369a3
 	if ((r = sshkey_save_private(private, identity_file, passphrase,
4369a3
-	    new_comment, use_new_format, new_format_cipher, rounds)) != 0) {
4369a3
+	    new_comment, private_key_format, openssh_format_cipher,
4369a3
+	    rounds)) != 0) {
4369a3
 		error("Saving key \"%s\" failed: %s",
4369a3
 		    identity_file, ssh_err(r));
4369a3
 		explicit_bzero(passphrase, strlen(passphrase));
4369a3
@@ -2525,11 +2527,12 @@ main(int argc, char **argv)
4369a3
 			}
4369a3
 			if (strcasecmp(optarg, "PKCS8") == 0) {
4369a3
 				convert_format = FMT_PKCS8;
4369a3
+				private_key_format = SSHKEY_PRIVATE_PKCS8;
4369a3
 				break;
4369a3
 			}
4369a3
 			if (strcasecmp(optarg, "PEM") == 0) {
4369a3
 				convert_format = FMT_PEM;
4369a3
-				use_new_format = 0;
4369a3
+				private_key_format = SSHKEY_PRIVATE_PEM;
4369a3
 				break;
4369a3
 			}
4369a3
 			fatal("Unsupported conversion format \"%s\"", optarg);
4369a3
@@ -2567,7 +2570,7 @@ main(int argc, char **argv)
4369a3
 			add_cert_option(optarg);
4369a3
 			break;
4369a3
 		case 'Z':
4369a3
-			new_format_cipher = optarg;
4369a3
+			openssh_format_cipher = optarg;
4369a3
 			break;
4369a3
 		case 'C':
4369a3
 			identity_comment = optarg;
4369a3
@@ -2912,7 +2915,7 @@ main(int argc, char **argv)
4369a3
 
4369a3
 	/* Save the key with the given passphrase and comment. */
4369a3
 	if ((r = sshkey_save_private(private, identity_file, passphrase1,
4369a3
-	    comment, use_new_format, new_format_cipher, rounds)) != 0) {
4369a3
+	    comment, private_key_format, openssh_format_cipher, rounds)) != 0) {
4369a3
 		error("Saving key \"%s\" failed: %s",
4369a3
 		    identity_file, ssh_err(r));
4369a3
 		explicit_bzero(passphrase1, strlen(passphrase1));
4369a3
diff --git a/sshkey.c b/sshkey.c
4369a3
index 6b5ff0485..a0cea9257 100644
4369a3
--- a/sshkey.c
4369a3
+++ b/sshkey.c
4369a3
@@ -3975,10 +3975,10 @@ sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase,
4369a3
 
4369a3
 
4369a3
 #ifdef WITH_OPENSSL
4369a3
-/* convert SSH v2 key in OpenSSL PEM format */
4369a3
+/* convert SSH v2 key to PEM or PKCS#8 format */
4369a3
 static int
4369a3
-sshkey_private_pem_to_blob(struct sshkey *key, struct sshbuf *blob,
4369a3
-    const char *_passphrase, const char *comment)
4369a3
+sshkey_private_to_blob_pem_pkcs8(struct sshkey *key, struct sshbuf *blob,
4369a3
+    int format, const char *_passphrase, const char *comment)
4369a3
 {
4369a3
 	int success, r;
4369a3
 	int blen, len = strlen(_passphrase);
4369a3
@@ -3988,26 +3988,46 @@ sshkey_private_pem_to_blob(struct sshkey *key, struct sshbuf *buf,
4369a3
 	const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL;
4369a3
 	char *bptr;
4369a3
 	BIO *bio = NULL;
4369a3
+	EVP_PKEY *pkey = NULL;
4369a3
 
4369a3
 	if (len > 0 && len <= 4)
4369a3
 		return SSH_ERR_PASSPHRASE_TOO_SHORT;
4369a3
-	if ((bio = BIO_new(BIO_s_mem())) == NULL)
4369a3
-		return SSH_ERR_ALLOC_FAIL;
4369a3
+ 	if ((bio = BIO_new(BIO_s_mem())) == NULL) {
4369a3
+		r = SSH_ERR_ALLOC_FAIL;
4369a3
+		goto out;
4369a3
+ 	}
4369a3
+
4369a3
+	if (format == SSHKEY_PRIVATE_PKCS8 && (pkey = EVP_PKEY_new()) == NULL) {
4369a3
+		r = SSH_ERR_ALLOC_FAIL;
4369a3
+		goto out;
4369a3
+ 	}
4369a3
 
4369a3
 	switch (key->type) {
4369a3
 	case KEY_DSA:
4369a3
-		success = PEM_write_bio_DSAPrivateKey(bio, key->dsa,
4369a3
-		    cipher, passphrase, len, NULL, NULL);
4369a3
+		if (format == SSHKEY_PRIVATE_PEM) {
4369a3
+			success = PEM_write_bio_DSAPrivateKey(bio, key->dsa,
4369a3
+			    cipher, passphrase, len, NULL, NULL);
4369a3
+		} else {
4369a3
+			success = EVP_PKEY_set1_DSA(pkey, key->dsa);
4369a3
+		}
4369a3
 		break;
4369a3
 #ifdef OPENSSL_HAS_ECC
4369a3
 	case KEY_ECDSA:
4369a3
-		success = PEM_write_bio_ECPrivateKey(bio, key->ecdsa,
4369a3
-		    cipher, passphrase, len, NULL, NULL);
4369a3
+		if (format == SSHKEY_PRIVATE_PEM) {
4369a3
+			success = PEM_write_bio_ECPrivateKey(bio, key->ecdsa,
4369a3
+			    cipher, passphrase, len, NULL, NULL);
4369a3
+		} else {
4369a3
+			success = EVP_PKEY_set1_EC_KEY(pkey, key->ecdsa);
4369a3
+		}
4369a3
 		break;
4369a3
 #endif
4369a3
 	case KEY_RSA:
4369a3
-		success = PEM_write_bio_RSAPrivateKey(bio, key->rsa,
4369a3
-		    cipher, passphrase, len, NULL, NULL);
4369a3
+		if (format == SSHKEY_PRIVATE_PEM) {
4369a3
+			success = PEM_write_bio_RSAPrivateKey(bio, key->rsa,
4369a3
+			    cipher, passphrase, len, NULL, NULL);
4369a3
+		} else {
4369a3
+			success = EVP_PKEY_set1_RSA(pkey, key->rsa);
4369a3
+		}
4369a3
 		break;
4369a3
 	default:
4369a3
 		success = 0;
4369a3
@@ -4023,6 +4040,13 @@ sshkey_private_pem_to_blob(struct sshkey *key, struct sshbuf *buf,
4369a3
 		r = SSH_ERR_LIBCRYPTO_ERROR;
4369a3
 		goto out;
4369a3
 	}
4369a3
+	if (format == SSHKEY_PRIVATE_PKCS8) {
4369a3
+		if ((success = PEM_write_bio_PrivateKey(bio, pkey, cipher,
4369a3
+		    passphrase, len, NULL, NULL)) == 0) {
4369a3
+			r = SSH_ERR_LIBCRYPTO_ERROR;
4369a3
+			goto out;
4369a3
+		}
4369a3
+	}
4369a3
 	if ((blen = BIO_get_mem_data(bio, &bptr)) <= 0) {
4369a3
 		r = SSH_ERR_INTERNAL_ERROR;
4369a3
 		goto out;
4369a3
@@ -4035,6 +4059,7 @@ sshkey_private_pem_to_blob(struct sshkey *key, struct sshbuf *buf,
4369a3
 		goto out;
4369a3
 	r = 0;
4369a3
  out:
4369a3
+	EVP_PKEY_free(pkey);
4369a3
 	BIO_free(bio);
4369a3
 	return r;
4369a3
 }
4369a3
@@ -4046,29 +4071,38 @@ sshkey_private_pem_to_blob(struct sshkey *key, struct sshbuf *buf,
4369a3
 int
4369a3
 sshkey_private_to_fileblob(struct sshkey *key, struct sshbuf *blob,
4369a3
     const char *passphrase, const char *comment,
4369a3
-    int force_new_format, const char *new_format_cipher, int new_format_rounds)
4369a3
+    int format, const char *openssh_format_cipher, int openssh_format_rounds)
4369a3
 {
4369a3
 	switch (key->type) {
4369a3
 #ifdef WITH_OPENSSL
4369a3
 	case KEY_DSA:
4369a3
 	case KEY_ECDSA:
4369a3
 	case KEY_RSA:
4369a3
-		if (force_new_format) {
4369a3
-			return sshkey_private_to_blob2(key, blob, passphrase,
4369a3
-			    comment, new_format_cipher, new_format_rounds);
4369a3
-		}
4369a3
-		return sshkey_private_pem_to_blob(key, blob,
4369a3
-		    passphrase, comment);
4369a3
+		break; /* see below */
4369a3
 #endif /* WITH_OPENSSL */
4369a3
 	case KEY_ED25519:
4369a3
 #ifdef WITH_XMSS
4369a3
 	case KEY_XMSS:
4369a3
 #endif /* WITH_XMSS */
4369a3
 		return sshkey_private_to_blob2(key, blob, passphrase,
4369a3
-		    comment, new_format_cipher, new_format_rounds);
4369a3
+		    comment, openssh_format_cipher, openssh_format_rounds);
4369a3
 	default:
4369a3
 		return SSH_ERR_KEY_TYPE_UNKNOWN;
4369a3
 	}
4369a3
+
4369a3
+#ifdef WITH_OPENSSL
4369a3
+	switch (format) {
4369a3
+	case SSHKEY_PRIVATE_OPENSSH:
4369a3
+		return sshkey_private_to_blob2(key, blob, passphrase,
4369a3
+		    comment, openssh_format_cipher, openssh_format_rounds);
4369a3
+	case SSHKEY_PRIVATE_PEM:
4369a3
+	case SSHKEY_PRIVATE_PKCS8:
4369a3
+		return sshkey_private_to_blob_pem_pkcs8(key, blob,
4369a3
+		    format, passphrase, comment);
4369a3
+	default:
4369a3
+		return SSH_ERR_INVALID_ARGUMENT;
4369a3
+	}
4369a3
+#endif /* WITH_OPENSSL */
4369a3
 }
4369a3
 
4369a3
 
4369a3
diff --git a/sshkey.h b/sshkey.h
4369a3
index 41d159a1b..d30a69cc9 100644
4369a3
--- a/sshkey.h
4369a3
+++ b/sshkey.h
4369a3
@@ -88,6 +88,13 @@ enum sshkey_serialize_rep {
4369a3
 	SSHKEY_SERIALIZE_INFO = 254,
4369a3
 };
4369a3
 
4369a3
+/* Private key disk formats */
4369a3
+enum sshkey_private_format {
4369a3
+	SSHKEY_PRIVATE_OPENSSH = 0,
4369a3
+	SSHKEY_PRIVATE_PEM = 1,
4369a3
+	SSHKEY_PRIVATE_PKCS8 = 2,
4369a3
+};
4369a3
+
4369a3
 /* key is stored in external hardware */
4369a3
 #define SSHKEY_FLAG_EXT		0x0001
4369a3
 
4369a3
@@ -221,7 +228,7 @@ int	sshkey_private_deserialize(struct sshbuf *buf,  struct sshkey **keyp);
4369a3
 /* private key file format parsing and serialisation */
4369a3
 int	sshkey_private_to_fileblob(struct sshkey *key, struct sshbuf *blob,
4369a3
     const char *passphrase, const char *comment,
4369a3
-    int force_new_format, const char *new_format_cipher, int new_format_rounds);
4369a3
+    int format, const char *openssh_format_cipher, int openssh_format_rounds);
4369a3
 int	sshkey_parse_private_fileblob(struct sshbuf *buffer,
4369a3
     const char *passphrase, struct sshkey **keyp, char **commentp);
4369a3
 int	sshkey_parse_private_fileblob_type(struct sshbuf *blob, int type,
4369a3