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