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