Skip to contentSkip to conversation

pkcs5: Fix pbes2 KDF parsing

PATCH In review Andrew Zaborowski 2 participants 4 messages Aug 17, 2017 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 pkcs5: Fix pbes2 KDF parsing

Make the key length field optional -- skip if the tag doesn't match -- and fix the oid comparison.

---
 ell/pkcs5.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
ell/pkcs5.c +11 -9
index cf61a35..7cedf4d 100644
@@ -254,7 +254,7 @@ static struct l_cipher *pkcs5_cipher_from_pbes2_params(
 	const uint8_t *kdf_sequence, *enc_sequence, *oid, *params,
 		*salt, *iter_count_buf, *key_len_buf, *prf_sequence;
 	size_t kdf_len, enc_len, params_len, salt_len, key_len, tmp_len;
-	unsigned int i, iter_count;
+	unsigned int i, iter_count, pos;
 	enum l_checksum_type prf_alg = L_CHECKSUM_NONE;
 	const struct pkcs5_enc_alg_oid *enc_scheme = NULL;
 	uint8_t derived_key[64];
@@ -309,11 +309,14 @@ static struct l_cipher *pkcs5_cipher_from_pbes2_params(
 	while (tmp_len--)
 		iter_count = (iter_count << 8) | *iter_count_buf++;
 
-	key_len_buf = asn1_der_find_elem(params, params_len, 2, &tag, &tmp_len);
-	if (key_len_buf) {
-		if (tag != ASN1_ID_INTEGER || tmp_len != 1)
+	pos = 2;
+	key_len_buf = asn1_der_find_elem(params, params_len, pos,
+						&tag, &tmp_len);
+	if (key_len_buf && tag == ASN1_ID_INTEGER) {
+		if (tmp_len != 1)
 			return NULL;
 
+		pos++;
 		key_len = 0;
 
 		while (tmp_len--)
@@ -321,11 +324,10 @@ static struct l_cipher *pkcs5_cipher_from_pbes2_params(
 	} else
 		key_len = 0;
 
-	prf_sequence = asn1_der_find_elem(params, params_len, 3,
+	prf_sequence = asn1_der_find_elem(params, params_len, pos,
 						&tag, &tmp_len);
-	if (prf_sequence) {
-		if (tag != ASN1_ID_SEQUENCE)
-			return NULL;
+	if (prf_sequence && tag == ASN1_ID_SEQUENCE) {
+		pos++;
 
 		oid = asn1_der_find_elem(prf_sequence, tmp_len, 0,
 						&tag, &tmp_len);
@@ -333,7 +335,7 @@ static struct l_cipher *pkcs5_cipher_from_pbes2_params(
 			return NULL;
 
 		for (i = 0; i < L_ARRAY_SIZE(pkcs5_digest_alg_oids); i++)
-			if (!asn1_oid_eq(&pkcs5_digest_alg_oids[i].oid,
+			if (asn1_oid_eq(&pkcs5_digest_alg_oids[i].oid,
 						tmp_len, oid))
 				prf_alg = pkcs5_digest_alg_oids[i].type;
 
Signature · 1 line
2.11.0
#1 lore raw

PATCH 2/3 unit: More KDF combinations in PEM private key test

Add new test key file using pbeWithSHA1AndDES-CBC and change the v2 test key files to use a different KDF each. The DES-encrypted file keeps using SHA1 (KDF id field not present), DES-EDE3 file now uses SHA224, AES128 file uses SHA256 and AES256 file uses SHA512.

---
 Makefile.am                          |  1 +
 unit/cert-client-key-sha1-des.pem    | 29 +++++++++++++++++++
 unit/cert-client-key-v2-aes128.pem   | 56 ++++++++++++++++++------------------
 unit/cert-client-key-v2-aes256.pem   | 56 ++++++++++++++++++------------------
 unit/cert-client-key-v2-des-ede3.pem | 56 ++++++++++++++++++------------------
 unit/test-pem.c                      |  3 ++
 6 files changed, 117 insertions(+), 84 deletions(-)
 create mode 100644 unit/cert-client-key-sha1-des.pem
Makefile.am +1 -0
index 533153b..4cff295 100644
@@ -235,6 +235,7 @@ unit_test_data_files = unit/settings.test unit/cert-ca.pem \
 			unit/cert-client-key-pkcs8.pem \
 			unit/cert-server-key-pkcs8.pem \
 			unit/cert-client-key-md5-des.pem \
+			unit/cert-client-key-sha1-des.pem \
 			unit/cert-client-key-v2-aes128.pem \
 			unit/cert-client-key-v2-aes256.pem \
 			unit/cert-client-key-v2-des-ede3.pem \
unit/cert-client-key-sha1-des.pem new file +29 -0
new file mode 100644
index 0000000..964babf
@@ -0,0 +1,29 @@
+-----BEGIN ENCRYPTED PRIVATE KEY-----
+MIIE6TAbBgkqhkiG9w0BBQowDgQIws6W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iA+WqMvVY6xq16XqcdzBo7dQBV5G3RUn4mJErp/xx6kEoxfuR4nPac0yqxi4CG
+0tzJ4PJ4nKGJQH465DbP/ooNYOOyY5O0ZLFOxExdhpKpGB+R4vtkae/OFviR/r4m
+n0ZA86idb8U6rw3Ei8GkTNxb8Yx6GH854VkitUHC/L7dtt7qIsdyY3URckN0yjDU
+hNSMut514hDfjSZwjwU7gUzTYdlA/uuofa9UEv84S+KBu+jh/qQsnf1+ncaDELS8
+l6OSxldZ0iV8Tbaxz9QICmZyYSI4NH2qOCnu6aV4mohyJSNFRmf+Fv4UTyanbzBL
+S8tdLp5pMZtre+yF3XHoLcHlej/LGV1UeUdoOI25HeIA5s8+Gi+2Fm6dgCrasmHL
+elhC8sOtTygv4m9KEbxUEJab+z2Tu7JtMsKrzn72GAWlZenkENziklxyRg7z6FAJ
+MUbsB8sHc9xKgE2nAUmIMy6ws+qfgFc3sEIG6QspNnSgkwF9WkF2QGFt3SMeudx5
+lrpY7JQaS+N6qhV+bR2hqUCZMZ4RLRh+GQEmkPZFHK8UNCkD0VbEVjhGbPYp0IF+
+zHs//uVJkfS0ZxWT+w==
+-----END ENCRYPTED PRIVATE KEY-----
unit/cert-client-key-v2-aes128.pem +28 -28

58 lines not on this page · raw message

unit/cert-client-key-v2-aes256.pem +28 -28

58 lines not on this page · raw message

unit/cert-client-key-v2-des-ede3.pem +28 -28

58 lines not on this page · raw message

unit/test-pem.c +3 -0
index 9090a24..79a7498 100644
@@ -137,6 +137,9 @@ int main(int argc, char *argv[])
 	l_test_add("pem/v1 MD5AndDES encrypted Private Key",
 			test_encrypted_pkey,
 			"unit/cert-client-key-md5-des.pem");
+	l_test_add("pem/v1 SHA1AndDES encrypted Private Key",
+			test_encrypted_pkey,
+			"unit/cert-client-key-sha1-des.pem");
 	l_test_add("pem/v2 DES encrypted Private Key", test_encrypted_pkey,
 			"unit/cert-client-key-v2-des.pem");
 	l_test_add("pem/v2 DES EDE3 encrypted Private Key", test_encrypted_pkey,
Signature · 1 line
2.11.0
#2 lore raw

PATCH 3/3 unit: Generate encrypted test keys in gencerts.sh

In reply to Andrew Zaborowski
---
 unit/gencerts.sh | 6 ++++++
 1 file changed, 6 insertions(+)
unit/gencerts.sh +6 -0
index b0249fb..d4c29a7 100755
@@ -17,6 +17,12 @@ openssl pkcs8 -topk8 -nocrypt -in cert-client-key.pem -out cert-client-key-pkcs8
 openssl req -new -extensions cert_ext -config ./gencerts.cnf -subj '/O=Bar Example Organization/CN=Bar Example Organization/emailAddress=bar(a)mail.example' -key cert-client-key.pem -out cert-client.csr
 openssl x509 -req -extensions cert_ext -extfile ./gencerts.cnf -in cert-client.csr -CA cert-ca.pem -CAkey cert-ca-key.pem -CAcreateserial -sha256 -days 10000 -out cert-client.pem
 openssl verify -CAfile cert-ca.pem cert-client.pem
+openssl pkcs8 -in cert-client-key-pkcs8.pem -out cert-client-key-md5-des.pem -topk8 -v1 PBE-MD5-DES -passout pass:abc
+openssl pkcs8 -in cert-client-key-pkcs8.pem -out cert-client-key-sha1-des.pem -topk8 -v1 PBE-SHA1-DES -passout pass:abc
+openssl pkcs8 -in cert-client-key-pkcs8.pem -out cert-client-key-v2-des.pem -topk8 -v2 des-cbc -v2prf hmacWithSHA1 -passout pass:abc
+openssl pkcs8 -in cert-client-key-pkcs8.pem -out cert-client-key-v2-des-ede3.pem -topk8 -v2 des-ede3-cbc -v2prf hmacWithSHA224 -passout pass:abc
+openssl pkcs8 -in cert-client-key-pkcs8.pem -out cert-client-key-v2-aes128.pem -topk8 -v2 aes128 -v2prf hmacWithSHA256 -passout pass:abc
+openssl pkcs8 -in cert-client-key-pkcs8.pem -out cert-client-key-v2-aes256.pem -topk8 -v2 aes256 -v2prf hmacWithSHA512 -passout pass:abc
 
 echo -e "\n*** Intermediate Certificate ***"
 openssl genrsa -out cert-intca-key.pem
Signature · 1 line
2.11.0
#3 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 6 lines · Andrew Zaborowski

Make the key length field optional -- skip if the tag doesn't match -- and fix the oid comparison.

---
  ell/pkcs5.c | 20 +++++++++++---------
  1 file changed, 11 insertions(+), 9 deletions(-)

All three applied, thanks.

Regards,
-Denis