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