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