Blame SOURCES/0006-support-aes256-private-keys-rh1623798.patch

52f25a
From 0590bacaecdfb57d5289a2c3d0628424689353d1 Mon Sep 17 00:00:00 2001
52f25a
From: Beniamino Galvani <bgalvani@redhat.com>
52f25a
Date: Mon, 27 Aug 2018 17:04:34 +0200
52f25a
Subject: [PATCH] libnm-core: support private keys encrypted with
52f25a
 AES-{192,256}-CBC
52f25a
52f25a
https://github.com/NetworkManager/NetworkManager/pull/189
52f25a
(cherry picked from commit 93f85edcce502cfa6d3676f58bf9e8e1a527ea53)
52f25a
(cherry picked from commit 74fc6f30b2fef3b8631128907e036bda88491970)
52f25a
---
52f25a
 Makefile.am                                   |  3 +-
52f25a
 libnm-core/crypto.c                           | 30 +++++++----
52f25a
 libnm-core/crypto.h                           |  6 ++-
52f25a
 libnm-core/crypto_gnutls.c                    | 14 ++++-
52f25a
 libnm-core/crypto_nss.c                       |  9 +++-
52f25a
 ...{test-aes-key.pem => test-aes-128-key.pem} |  0
52f25a
 libnm-core/tests/certs/test-aes-256-key.pem   | 54 +++++++++++++++++++
52f25a
 libnm-core/tests/test-crypto.c                |  7 ++-
52f25a
 libnm-util/tests/test-crypto.c                |  4 +-
52f25a
 9 files changed, 106 insertions(+), 21 deletions(-)
52f25a
 rename libnm-core/tests/certs/{test-aes-key.pem => test-aes-128-key.pem} (100%)
52f25a
 create mode 100644 libnm-core/tests/certs/test-aes-256-key.pem
52f25a
52f25a
diff --git a/Makefile.am b/Makefile.am
52f25a
index cdb5cfc9d..d86fa26c7 100644
52f25a
--- a/Makefile.am
52f25a
+++ b/Makefile.am
52f25a
@@ -749,7 +749,8 @@ EXTRA_DIST += \
52f25a
 	libnm-core/tests/certs/test2_ca_cert.pem \
52f25a
 	libnm-core/tests/certs/test2-cert.p12 \
52f25a
 	libnm-core/tests/certs/test2_key_and_cert.pem \
52f25a
-	libnm-core/tests/certs/test-aes-key.pem \
52f25a
+	libnm-core/tests/certs/test-aes-128-key.pem \
52f25a
+	libnm-core/tests/certs/test-aes-256-key.pem \
52f25a
 	libnm-core/tests/certs/test_ca_cert.der \
52f25a
 	libnm-core/tests/certs/test_ca_cert.pem \
52f25a
 	libnm-core/tests/certs/test-ca-cert.pem \
52f25a
diff --git a/libnm-core/crypto.c b/libnm-core/crypto.c
52f25a
index c4e48475f..319f8055f 100644
52f25a
--- a/libnm-core/crypto.c
52f25a
+++ b/libnm-core/crypto.c
52f25a
@@ -158,7 +158,13 @@ parse_old_openssl_key_file (const guint8 *data,
52f25a
 				goto parse_error;
52f25a
 			}
52f25a
 		} else if (!strncmp (p, DEK_INFO_TAG, strlen (DEK_INFO_TAG))) {
52f25a
+			static const char *const known_ciphers[] = { CIPHER_DES_EDE3_CBC,
52f25a
+			                                             CIPHER_DES_CBC,
52f25a
+			                                             CIPHER_AES_128_CBC,
52f25a
+			                                             CIPHER_AES_192_CBC,
52f25a
+			                                             CIPHER_AES_256_CBC };
52f25a
 			char *comma;
52f25a
+			guint i;
52f25a
 
52f25a
 			if (enc_tags++ != 1 || str->len != 0) {
52f25a
 				g_set_error (error, NM_CRYPTO_ERROR,
52f25a
@@ -187,13 +193,13 @@ parse_old_openssl_key_file (const guint8 *data,
52f25a
 			iv = g_strdup (comma);
52f25a
 
52f25a
 			/* Get the private key cipher */
52f25a
-			if (!strcasecmp (p, "DES-EDE3-CBC")) {
52f25a
-				cipher = g_strdup (p);
52f25a
-			} else if (!strcasecmp (p, "DES-CBC")) {
52f25a
-				cipher = g_strdup (p);
52f25a
-			} else if (!strcasecmp (p, "AES-128-CBC")) {
52f25a
-				cipher = g_strdup (p);
52f25a
-			} else {
52f25a
+			for (i = 0; i < G_N_ELEMENTS (known_ciphers); i++) {
52f25a
+				if (!g_ascii_strcasecmp (p, known_ciphers[i])) {
52f25a
+					cipher = g_strdup (known_ciphers[i]);
52f25a
+					break;
52f25a
+				}
52f25a
+			}
52f25a
+			if (i == G_N_ELEMENTS (known_ciphers)) {
52f25a
 				g_set_error (error, NM_CRYPTO_ERROR,
52f25a
 				             NM_CRYPTO_ERROR_INVALID_DATA,
52f25a
 				             _("Malformed PEM file: unknown private key cipher '%s'."),
52f25a
@@ -383,12 +389,16 @@ crypto_make_des_aes_key (const char *cipher,
52f25a
 	g_return_val_if_fail (password != NULL, NULL);
52f25a
 	g_return_val_if_fail (out_len != NULL, NULL);
52f25a
 
52f25a
-	if (!strcmp (cipher, "DES-EDE3-CBC"))
52f25a
+	if (!strcmp (cipher, CIPHER_DES_EDE3_CBC))
52f25a
 		digest_len = 24;
52f25a
-	else if (!strcmp (cipher, "DES-CBC"))
52f25a
+	else if (!strcmp (cipher, CIPHER_DES_CBC))
52f25a
 		digest_len = 8;
52f25a
-	else if (!strcmp (cipher, "AES-128-CBC"))
52f25a
+	else if (!strcmp (cipher, CIPHER_AES_128_CBC))
52f25a
 		digest_len = 16;
52f25a
+	else if (!strcmp (cipher, CIPHER_AES_192_CBC))
52f25a
+		digest_len = 24;
52f25a
+	else if (!strcmp (cipher, CIPHER_AES_256_CBC))
52f25a
+		digest_len = 32;
52f25a
 	else {
52f25a
 		g_set_error (error, NM_CRYPTO_ERROR,
52f25a
 		             NM_CRYPTO_ERROR_UNKNOWN_CIPHER,
52f25a
diff --git a/libnm-core/crypto.h b/libnm-core/crypto.h
52f25a
index e89f09193..d20d6f310 100644
52f25a
--- a/libnm-core/crypto.h
52f25a
+++ b/libnm-core/crypto.h
52f25a
@@ -30,8 +30,10 @@
52f25a
 
52f25a
 #define MD5_HASH_LEN 20
52f25a
 #define CIPHER_DES_EDE3_CBC "DES-EDE3-CBC"
52f25a
-#define CIPHER_DES_CBC "DES-CBC"
52f25a
-#define CIPHER_AES_CBC "AES-128-CBC"
52f25a
+#define CIPHER_DES_CBC      "DES-CBC"
52f25a
+#define CIPHER_AES_128_CBC  "AES-128-CBC"
52f25a
+#define CIPHER_AES_192_CBC  "AES-192-CBC"
52f25a
+#define CIPHER_AES_256_CBC  "AES-256-CBC"
52f25a
 
52f25a
 typedef enum {
52f25a
 	NM_CRYPTO_KEY_TYPE_UNKNOWN = 0,
52f25a
diff --git a/libnm-core/crypto_gnutls.c b/libnm-core/crypto_gnutls.c
52f25a
index 53a3ba4ad..49181ee72 100644
52f25a
--- a/libnm-core/crypto_gnutls.c
52f25a
+++ b/libnm-core/crypto_gnutls.c
52f25a
@@ -82,9 +82,15 @@ crypto_decrypt (const char *cipher,
52f25a
 	} else if (!strcmp (cipher, CIPHER_DES_CBC)) {
52f25a
 		cipher_mech = GNUTLS_CIPHER_DES_CBC;
52f25a
 		real_iv_len = SALT_LEN;
52f25a
-	} else if (!strcmp (cipher, CIPHER_AES_CBC)) {
52f25a
+	} else if (!strcmp (cipher, CIPHER_AES_128_CBC)) {
52f25a
 		cipher_mech = GNUTLS_CIPHER_AES_128_CBC;
52f25a
 		real_iv_len = 16;
52f25a
+	} else if (!strcmp (cipher, CIPHER_AES_192_CBC)) {
52f25a
+		cipher_mech = GNUTLS_CIPHER_AES_192_CBC;
52f25a
+		real_iv_len = 16;
52f25a
+	} else if (!strcmp (cipher, CIPHER_AES_256_CBC)) {
52f25a
+		cipher_mech = GNUTLS_CIPHER_AES_256_CBC;
52f25a
+		real_iv_len = 16;
52f25a
 	} else {
52f25a
 		g_set_error (error, NM_CRYPTO_ERROR,
52f25a
 		             NM_CRYPTO_ERROR_UNKNOWN_CIPHER,
52f25a
@@ -189,8 +195,12 @@ crypto_encrypt (const char *cipher,
52f25a
 
52f25a
 	if (!strcmp (cipher, CIPHER_DES_EDE3_CBC))
52f25a
 		cipher_mech = GNUTLS_CIPHER_3DES_CBC;
52f25a
-	else if (!strcmp (cipher, CIPHER_AES_CBC))
52f25a
+	else if (!strcmp (cipher, CIPHER_AES_128_CBC))
52f25a
 		cipher_mech = GNUTLS_CIPHER_AES_128_CBC;
52f25a
+	else if (!strcmp (cipher, CIPHER_AES_192_CBC))
52f25a
+		cipher_mech = GNUTLS_CIPHER_AES_192_CBC;
52f25a
+	else if (!strcmp (cipher, CIPHER_AES_256_CBC))
52f25a
+		cipher_mech = GNUTLS_CIPHER_AES_256_CBC;
52f25a
 	else {
52f25a
 		g_set_error (error, NM_CRYPTO_ERROR,
52f25a
 		             NM_CRYPTO_ERROR_UNKNOWN_CIPHER,
52f25a
diff --git a/libnm-core/crypto_nss.c b/libnm-core/crypto_nss.c
52f25a
index 56e91e26f..9a0c43349 100644
52f25a
--- a/libnm-core/crypto_nss.c
52f25a
+++ b/libnm-core/crypto_nss.c
52f25a
@@ -103,7 +103,9 @@ crypto_decrypt (const char *cipher,
52f25a
 	} else if (!strcmp (cipher, CIPHER_DES_CBC)) {
52f25a
 		cipher_mech = CKM_DES_CBC_PAD;
52f25a
 		real_iv_len = 8;
52f25a
-	} else if (!strcmp (cipher, CIPHER_AES_CBC)) {
52f25a
+	} else if (NM_IN_STRSET (cipher, CIPHER_AES_128_CBC,
52f25a
+	                                 CIPHER_AES_192_CBC,
52f25a
+	                                 CIPHER_AES_256_CBC)) {
52f25a
 		cipher_mech = CKM_AES_CBC_PAD;
52f25a
 		real_iv_len = 16;
52f25a
 	} else {
52f25a
@@ -269,7 +271,10 @@ crypto_encrypt (const char *cipher,
52f25a
 
52f25a
 	if (!strcmp (cipher, CIPHER_DES_EDE3_CBC))
52f25a
 		cipher_mech = CKM_DES3_CBC_PAD;
52f25a
-	else if (!strcmp (cipher, CIPHER_AES_CBC))
52f25a
+	else if (NM_IN_STRSET (cipher,
52f25a
+	                       CIPHER_AES_128_CBC,
52f25a
+	                       CIPHER_AES_192_CBC,
52f25a
+	                       CIPHER_AES_256_CBC))
52f25a
 		cipher_mech = CKM_AES_CBC_PAD;
52f25a
 	else {
52f25a
 		g_set_error (error, NM_CRYPTO_ERROR,
52f25a
diff --git a/libnm-core/tests/certs/test-aes-key.pem b/libnm-core/tests/certs/test-aes-128-key.pem
52f25a
similarity index 100%
52f25a
rename from libnm-core/tests/certs/test-aes-key.pem
52f25a
rename to libnm-core/tests/certs/test-aes-128-key.pem
52f25a
diff --git a/libnm-core/tests/certs/test-aes-256-key.pem b/libnm-core/tests/certs/test-aes-256-key.pem
52f25a
new file mode 100644
52f25a
index 000000000..e51bafd3d
52f25a
--- /dev/null
52f25a
+++ b/libnm-core/tests/certs/test-aes-256-key.pem
52f25a
@@ -0,0 +1,54 @@
52f25a
+-----BEGIN RSA PRIVATE KEY-----
52f25a
+Proc-Type: 4,ENCRYPTED
52f25a
+DEK-Info: AES-256-CBC,5FF6BD2D4E57E8933D4A6814DEF5305A
52f25a
+
52f25a
+9Br+xw6XOg7qUqfeE5PJ4g/PAm7eTcPMb4FzSKkaEosLo6oj4f37TwXuojJZeAmi
52f25a
+1EytpqM1vdYHCLdjg+qYaTIq6mzMZIyoaREokcOhcNrq5S0J39gJLVV9LjiXhCAH
52f25a
+GQgDBnbRT6HGz70AyTRLcW9aj6uBzTv/m92sLUw2txFeBXK8n2AA1oHJTgsFNYjf
52f25a
+/ZvTCE1VMQHDPx31Vn5WXSUHNc0hx4MTIwpHqWI17ohr8IiWCs5HXVfVaqrNeNEw
52f25a
+haD7fg8oNxjLs46/4dDWmfWXhDsMFSweZv03gZdyVjwn1IOqeVGmTdLpllfgOW7E
52f25a
++XE8Y/d55s5nkOxu6eXNMtWgjclKBGr2iMxxnODmEsUt2WcV98cPS+25o3hOfy3s
52f25a
+NIcfxtWVRFUtjqf3ragyGLuXFqATkj1slj4LVMeewRJ1g+Z6ti0mwBN+ZrYtKdec
52f25a
+FRNb4zr5FW+3SqkIIJVfxJEYJDB4zODhMg8tySEHLKuT0uz42YQ4aoOHTzO5WDBY
52f25a
+2BI7TjRppXcExPnkAk5jqbKA6BjT9KcAVyypfxDKvCeXKdjDcL6ISOBSm6cQBh8D
52f25a
+HxsFzMy9PF6kKNeiNiEsVPnKYvhvs1hTBtp+IAgJ6KZnCDKplZFxo/mBAlV2KyCT
52f25a
+x+Mhmme3fXdLJkvxlVJAoAhwgXvomVCVTGI3JhcQIqVgxPIKYpqlHVFC7JjG+yQX
52f25a
+tvzCPtr9G9+Ofrm6zXjlDD7zNyl/KfFtEWhO2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D01/gh0N0
52f25a
+HM6GGtm3GbOyZUhw5OFz04xzcyFYo2xaqzgaZieAOcrt2s6XyPVf1gww08/HtTMR
52f25a
+gLg14MUQvRXV6kPJfdu4OLZ//b6J0KnzVyLDRdOrWIj2raLWmKwQN9qv05/yskcD
52f25a
+Y6x7wq3v6iZpFjDc53sslhwp2XRsoWT9X5alVspz8WvP/kqgkTdzpPFdp1vIovOQ
52f25a
+kRXdzzKICDGDJUIcTL8cJ3Dv4XqNR/sVyuB4dfndzQQApbdYTDNpwX0VJDBjMkQy
52f25a
+Up6aiUknxa6Cbp7b1ZfUQY8yNBAIZL+R8dmobT3nAHW61DaASHSxn+elCD2Ja/6b
52f25a
+EiWikskyN6crMAv35ILr5ySsZK97ttNNmRoGFbt8bTjRd83Ie+UfH445kCKsY83x
52f25a
+aDCvWm+bbV6M9rSgjhJ3bWOudiw+EBMGvSamSnS7CYnRmwq4t+4bM2sh2nYKY0qw
52f25a
+-----END RSA PRIVATE KEY-----
52f25a
diff --git a/libnm-core/tests/test-crypto.c b/libnm-core/tests/test-crypto.c
52f25a
index fb99ffea7..5fb26c1fc 100644
52f25a
--- a/libnm-core/tests/test-crypto.c
52f25a
+++ b/libnm-core/tests/test-crypto.c
52f25a
@@ -476,8 +476,11 @@ main (int argc, char **argv)
52f25a
 	g_test_add_data_func ("/libnm/crypto/key/padding-8",
52f25a
 	                      "test2_key_and_cert.pem, 12345testing",
52f25a
 	                      test_key);
52f25a
-	g_test_add_data_func ("/libnm/crypto/key/aes",
52f25a
-	                      "test-aes-key.pem, test-aes-password",
52f25a
+	g_test_add_data_func ("/libnm/crypto/key/aes-128",
52f25a
+	                      "test-aes-128-key.pem, test-aes-password",
52f25a
+	                      test_key);
52f25a
+	g_test_add_data_func ("/libnm/crypto/key/aes-256",
52f25a
+	                      "test-aes-256-key.pem, test-aes-password",
52f25a
 	                      test_key);
52f25a
 	g_test_add_data_func ("/libnm/crypto/key/decrypted",
52f25a
 	                      "test-key-only-decrypted.pem",
52f25a
diff --git a/libnm-util/tests/test-crypto.c b/libnm-util/tests/test-crypto.c
52f25a
index 61bd97745..af6028a52 100644
52f25a
--- a/libnm-util/tests/test-crypto.c
52f25a
+++ b/libnm-util/tests/test-crypto.c
52f25a
@@ -383,8 +383,8 @@ main (int argc, char **argv)
52f25a
 	g_test_add_data_func ("/libnm/crypto/key/padding-8",
52f25a
 	                      "test2_key_and_cert.pem, 12345testing",
52f25a
 	                      test_key);
52f25a
-	g_test_add_data_func ("/libnm/crypto/key/aes",
52f25a
-	                      "test-aes-key.pem, test-aes-password",
52f25a
+	g_test_add_data_func ("/libnm/crypto/key/aes-128",
52f25a
+	                      "test-aes-128-key.pem, test-aes-password",
52f25a
 	                      test_key);
52f25a
 
52f25a
 	g_test_add_data_func ("/libnm/crypto/PKCS#12/1",
52f25a
-- 
52f25a
2.17.1
52f25a