Skip to contentSkip to conversation

pem: Wipe more memory in PKCS#1 private key decoding

PATCH In review Andrew Zaborowski 2 participants 15 messages Dec 10, 2020 – Dec 12, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/13 pem: Wipe more memory in PKCS#1 private key decoding

bzero some more potentially sensitive memory that we weren't bzeroing in the recently added openssl legacy PKCS#1 private key format decoding.

---
 ell/pem.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
ell/pem.c +13 -5
index 790f2c2..70d1f62 100644
@@ -581,18 +581,24 @@ static struct l_cipher *cipher_from_dek_info(const char *algid, const char *para
 
 	l_checksum_free(md5);
 
-	if (!ok)
-		return NULL;
+	if (!ok) {
+		cipher = NULL;
+		goto cleanup;
+	}
 
 	cipher  = l_cipher_new(type, key, key_len);
 	if (!cipher)
-		return NULL;
+		goto cleanup;
 
 	if (l_cipher_set_iv(cipher, iv, iv_len))
-		return cipher;
+		goto cleanup;
 
 	l_cipher_free(cipher);
-	return NULL;
+	cipher = NULL;
+
+cleanup:
+	explicit_bzero(key, sizeof(key));
+	return cipher;
 }
 
 static struct l_key *pem_load_private_key(uint8_t *content,
@@ -821,8 +827,10 @@ static struct l_key *pem_load_private_key(uint8_t *content,
 		ptr += sizeof(pkcs1_rsa_encryption);
 		memcpy(ptr, private_key, private_key_len);
 		ptr += private_key_len;
+		explicit_bzero(private_key, private_key_len);
 		l_free(private_key);
 
+		explicit_bzero(content, len);
 		l_free(content);
 		content = one_asymmetric_key;
 		len = ptr - one_asymmetric_key;
Signature · 1 line
2.27.0
#1 lore raw

PATCH 2/13 pem: Factor out the PKCS#8 decoding code

---
 ell/pem-private.h |   7 ++
 ell/pem.c         | 167 ++++++++++++++++++++++++----------------------
 2 files changed, 96 insertions(+), 78 deletions(-)
ell/pem-private.h +7 -0
index 1cf7ee5..6e9458d 100644
@@ -30,3 +30,10 @@
 const char *pem_next(const void *buf, size_t buf_len, char **type_label,
 				size_t *base64_len,
 				const char **endp, bool strict);
+
+struct l_key *pem_key_from_pkcs8_private_key_info(const uint8_t *der,
+							size_t der_len);
+
+struct l_key *pem_key_from_pkcs8_encrypted_private_key_info(const uint8_t *der,
+							size_t der_len,
+							const char *passphrase);
ell/pem.c +89 -78

224 lines not on this page · raw message

Signature · 1 line
2.27.0
#2 lore raw

PATCH 3/13 cipher: Add an RC2 implementation

In reply to Andrew Zaborowski

RC2 is still used by default by tools creating PKCS#12 files (.pfx / .p12). Since it's not available from the kernel crypto API, add a local implementation. This is currently the only cipher implemented locally so checks are needed in all our l_cipher ops to see whether a cipher is handled locally or by the kernel.

Adding this cipher is not a recommendation of using it, it's discouraged where it can be avoided.

---
 ell/cipher.c | 365 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 ell/cipher.h |   1 +
 2 files changed, 363 insertions(+), 3 deletions(-)
ell/cipher.c +362 -3
index 6aec42f..a2db608 100644
@@ -86,14 +86,18 @@ struct af_alg_iv {
 #define ALG_SET_AEAD_AUTHSIZE	5
 #endif
 
-#define is_valid_type(type)  ((type) <= L_CIPHER_DES3_EDE_CBC)
+#define is_valid_type(type)  ((type) <= L_CIPHER_RC2_CBC)
 
 static uint32_t supported_ciphers;
 static uint32_t supported_aead_ciphers;
 
 struct l_cipher {
 	int type;
-	int sk;
+	const struct local_impl *local;
+	union {
+		int sk;
+		void *local_data;
+	};
 };
 
 struct l_aead_cipher {
@@ -101,6 +105,16 @@ struct l_aead_cipher {
 	int sk;
 };
 
+struct local_impl {
+	void *(*cipher_new)(enum l_cipher_type,
+				const void *key, size_t key_length);
+	void (*cipher_free)(void *data);
+	bool (*set_iv)(void *data, const uint8_t *iv, size_t iv_length);
+	ssize_t (*operate)(void *data, __u32 operation,
+				const struct iovec *in, size_t in_cnt,
+				const struct iovec *out, size_t out_cnt);
+};
+
 static int create_alg(const char *alg_type, const char *alg_name,
 			const void *key, size_t key_length, size_t tag_length)
 {
@@ -154,11 +168,23 @@ static const char *cipher_type_to_name(enum l_cipher_type type)
 		return "cbc(des)";
 	case L_CIPHER_DES3_EDE_CBC:
 		return "cbc(des3_ede)";
+	case L_CIPHER_RC2_CBC:
+		return NULL;
 	}
 
 	return NULL;
 }
 
+static const struct local_impl local_rc2_cbc;
+
+static const struct local_impl *local_impl_ciphers[] = {
+	[L_CIPHER_RC2_CBC] = &local_rc2_cbc,
+};
+
+#define HAVE_LOCAL_IMPLEMENTATION(type)			\
+	((type) < L_ARRAY_SIZE(local_impl_ciphers) &&	\
+	 local_impl_ciphers[(type)])
+
 LIB_EXPORT struct l_cipher *l_cipher_new(enum l_cipher_type type,
 						const void *key,
 						size_t key_length)
@@ -176,6 +202,17 @@ LIB_EXPORT struct l_cipher *l_cipher_new(enum l_cipher_type type,
 	cipher->type = type;
 	alg_name = cipher_type_to_name(type);
 
+	if (HAVE_LOCAL_IMPLEMENTATION(type)) {
+		cipher->local = local_impl_ciphers[type];
+		cipher->local_data = cipher->local->cipher_new(type,
+							key, key_length);
+
+		if (!cipher->local_data)
+			goto error_free;
+
+		return cipher;
+	}
+
 	cipher->sk = create_alg("skcipher", alg_name, key, key_length, 0);
 	if (cipher->sk < 0)
 		goto error_free;
@@ -230,7 +267,10 @@ LIB_EXPORT void l_cipher_free(struct l_cipher *cipher)
 	if (unlikely(!cipher))
 		return;
 
-	close(cipher->sk);
+	if (cipher->local)
+		cipher->local->cipher_free(cipher->local_data);
+	else
+		close(cipher->sk);
 
 	l_free(cipher);
 }
@@ -403,6 +443,15 @@ LIB_EXPORT bool l_cipher_encrypt(struct l_cipher *cipher,
 	if (unlikely(!in) || unlikely(!out))
 		return false;
 
+	if (cipher->local) {
+		struct iovec in_iov = { (void *) in, len };
+		struct iovec out_iov = { out, len };
+
+		return cipher->local->operate(cipher->local_data,
+						ALG_OP_ENCRYPT,
+						&in_iov, 1, &out_iov, 1) >= 0;
+	}
+
 	return operate_cipher(cipher->sk, ALG_OP_ENCRYPT, in, len,
 				NULL, 0, NULL, 0, out, len) >= 0;
 }
@@ -417,6 +466,11 @@ LIB_EXPORT bool l_cipher_encryptv(struct l_cipher *cipher,
 	if (unlikely(!in) || unlikely(!out))
 		return false;
 
+	if (cipher->local)
+		return cipher->local->operate(cipher->local_data,
+						ALG_OP_ENCRYPT,
+						in, in_cnt, out, out_cnt) >= 0;
+
 	return operate_cipherv(cipher->sk, ALG_OP_ENCRYPT, in, in_cnt,
 				out, out_cnt) >= 0;
 }
@@ -430,6 +484,15 @@ LIB_EXPORT bool l_cipher_decrypt(struct l_cipher *cipher,
 	if (unlikely(!in) || unlikely(!out))
 		return false;
 
+	if (cipher->local) {
+		struct iovec in_iov = { (void *) in, len };
+		struct iovec out_iov = { out, len };
+
+		return cipher->local->operate(cipher->local_data,
+						ALG_OP_DECRYPT,
+						&in_iov, 1, &out_iov, 1) >= 0;
+	}
+
 	return operate_cipher(cipher->sk, ALG_OP_DECRYPT, in, len,
 				NULL, 0, NULL, 0, out, len) >= 0;
 }
@@ -444,6 +507,11 @@ LIB_EXPORT bool l_cipher_decryptv(struct l_cipher *cipher,
 	if (unlikely(!in) || unlikely(!out))
 		return false;
 
+	if (cipher->local)
+		return cipher->local->operate(cipher->local_data,
+						ALG_OP_DECRYPT,
+						in, in_cnt, out, out_cnt) >= 0;
+
 	return operate_cipherv(cipher->sk, ALG_OP_DECRYPT, in, in_cnt,
 				out, out_cnt) >= 0;
 }
@@ -459,6 +527,13 @@ LIB_EXPORT bool l_cipher_set_iv(struct l_cipher *cipher, const uint8_t *iv,
 	if (unlikely(!cipher))
 		return false;
 
+	if (cipher->local) {
+		if (!cipher->local->set_iv)
+			return false;
+
+		return cipher->local->set_iv(cipher->local_data, iv, iv_length);
+	}
+
 	memset(&c_msg_buf, 0, sizeof(c_msg_buf));
 	memset(&msg, 0, sizeof(struct msghdr));
 
@@ -616,6 +691,10 @@ static void init_supported()
 		supported_ciphers |= 1 << c;
 	}
 
+	for (c = 0; c < L_ARRAY_SIZE(local_impl_ciphers); c++)
+		if (HAVE_LOCAL_IMPLEMENTATION(c))
+			supported_ciphers |= 1 << c;
+
 	strcpy((char *) salg.salg_type, "aead");
 
 	for (a = L_AEAD_CIPHER_AES_CCM; a <= L_AEAD_CIPHER_AES_GCM; a++) {
@@ -649,3 +728,283 @@ LIB_EXPORT bool l_aead_cipher_is_supported(enum l_aead_cipher_type type)
 
 	return supported_aead_ciphers & (1 << type);
 }
+
+struct rc2_state {
+	union {
+		uint16_t xkey[64];
+		uint8_t xkey8[128];
+	};
+	struct rc2_state_ctx {
+		union {
+			uint16_t x[4];
+			uint64_t x64;
+		};
+	} ctx[2];
+};
+
+/* Simplified from the 1996 public-domain implementation */
+static void rc2_keyschedule(struct rc2_state *s,
+				const uint8_t *key, size_t key_len,
+				size_t bits)
+{
+	static const uint8_t permute[256] = {
+		217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157,
+		198,126, 55,131, 43,118, 83,142, 98, 76,100,136, 68,139,251,162,
+		 23,154, 89,245,135,179, 79, 19, 97, 69,109,141,  9,129,125, 50,
+		189,143, 64,235,134,183,123, 11,240,149, 33, 34, 92,107, 78,130,
+		 84,214,101,147,206, 96,178, 28,115, 86,192, 20,167,140,241,220,
+		 18,117,202, 31, 59,190,228,209, 66, 61,212, 48,163, 60,182, 38,
+		111,191, 14,218, 70,105,  7, 87, 39,242, 29,155,188,148, 67,  3,
+		248, 17,199,246,144,239, 62,231,  6,195,213, 47,200,102, 30,215,
+		  8,232,234,222,128, 82,238,247,132,170,114,172, 53, 77,106, 42,
+		150, 26,210,113, 90, 21, 73,116, 75,159,208, 94,  4, 24,164,236,
+		194,224, 65,110, 15, 81,203,204, 36,145,175, 80,161,244,112, 57,
+		153,124, 58,133, 35,184,180,122,252,  2, 54, 91, 37, 85,151, 49,
+		 45, 93,250,152,227,138,146,174,  5,223, 41, 16,103,108,186,201,
+		211,  0,230,207,225,158,168, 44, 99, 22,  1, 63, 88,226,137,169,
+		 13, 56, 52, 27,171, 51,255,176,187, 72, 12, 95,185,177,205, 46,
+		197,243,219, 71,229,165,156,119, 10,166, 32,104,254,127,193,173
+	};
+	uint8_t x;
+	unsigned int i;
+
+	memcpy(&s->xkey8, key, key_len);
+
+	/* Step 1: expand input key to 128 bytes */
+	x = s->xkey8[key_len - 1];
+
+	for (i = 0; key_len < 128; key_len++, i++)
+		s->xkey8[key_len] = x = permute[(x + s->xkey8[i]) & 255];
+
+	/* Step 2: reduce effective key size to "bits" */
+	key_len = (bits + 7) >> 3;
+	i = 128 - key_len;
+	s->xkey8[i] = x = permute[s->xkey8[i] & (255 >> (7 & -bits))];
+
+	while (i--)
+		s->xkey8[i] = x = permute[x ^ s->xkey8[i + key_len]];
+
+	/* Step 3: copy to xkey in little-endian order */
+	for (i = 0; i < 64; i++)
+		s->xkey[i] = L_CPU_TO_LE16(s->xkey[i]);
+}
+
+static uint64_t rc2_operate(struct rc2_state *s, uint64_t in, __u32 operation)
+{
+	int i;
+	union {
+		uint16_t x16[4];
+		uint64_t x64;
+	} x;
+
+	x.x64 = in;
+
+	if (operation == ALG_OP_ENCRYPT) {
+		const uint16_t *xkey = s->xkey;
+
+		for (i = 0; i < 16; i++) {
+			x.x16[0] += (x.x16[1] & ~x.x16[3]) +
+				(x.x16[2] & x.x16[3]) + *xkey++;
+			x.x16[0] = (x.x16[0] << 1) | (x.x16[0] >> 15);
+			x.x16[1] += (x.x16[2] & ~x.x16[0]) +
+				(x.x16[3] & x.x16[0]) + *xkey++;
+			x.x16[1] = (x.x16[1] << 2) | (x.x16[1] >> 14);
+			x.x16[2] += (x.x16[3] & ~x.x16[1]) +
+				(x.x16[0] & x.x16[1]) + *xkey++;
+			x.x16[2] = (x.x16[2] << 3) | (x.x16[2] >> 13);
+			x.x16[3] += (x.x16[0] & ~x.x16[2]) +
+				(x.x16[1] & x.x16[2]) + *xkey++;
+			x.x16[3] = (x.x16[3] << 5) | (x.x16[3] >> 11);
+
+			if (i == 4 || i == 10) {
+				x.x16[0] += s->xkey[x.x16[3] & 63];
+				x.x16[1] += s->xkey[x.x16[0] & 63];
+				x.x16[2] += s->xkey[x.x16[1] & 63];
+				x.x16[3] += s->xkey[x.x16[2] & 63];
+			}
+		}
+	} else {
+		const uint16_t *xkey = s->xkey + 63;
+
+		for (i = 0; i < 16; i++) {
+			x.x16[3] = (x.x16[3] << 11) | (x.x16[3] >> 5);
+			x.x16[3] -= (x.x16[0] & ~x.x16[2]) +
+				(x.x16[1] & x.x16[2]) + *xkey--;
+			x.x16[2] = (x.x16[2] << 13) | (x.x16[2] >> 3);
+			x.x16[2] -= (x.x16[3] & ~x.x16[1]) +
+				(x.x16[0] & x.x16[1]) + *xkey--;
+			x.x16[1] = (x.x16[1] << 14) | (x.x16[1] >> 2);
+			x.x16[1] -= (x.x16[2] & ~x.x16[0]) +
+				(x.x16[3] & x.x16[0]) + *xkey--;
+			x.x16[0] = (x.x16[0] << 15) | (x.x16[0] >> 1);
+			x.x16[0] -= (x.x16[1] & ~x.x16[3]) +
+				(x.x16[2] & x.x16[3]) + *xkey--;
+
+			if (i == 4 || i == 10) {
+				x.x16[3] -= s->xkey[x.x16[2] & 63];
+				x.x16[2] -= s->xkey[x.x16[1] & 63];
+				x.x16[1] -= s->xkey[x.x16[0] & 63];
+				x.x16[0] -= s->xkey[x.x16[3] & 63];
+			}
+		}
+	}
+
+	return x.x64;
+}
+
+static void *local_rc2_cbc_new(enum l_cipher_type type,
+				const void *key, size_t key_length)
+{
+	struct rc2_state *s;
+
+	if (unlikely(key_length == 0 || key_length > 128))
+		return NULL;
+
+	/*
+	 * The key length and the effective "strength" bits are separate
+	 * parameters but they match in our current use cases.
+	 */
+	s = l_new(struct rc2_state, 1);
+	rc2_keyschedule(s, key, key_length, key_length * 8);
+	return s;
+}
+
+static void local_rc2_cbc_free(void *data)
+{
+	explicit_bzero(data, sizeof(struct rc2_state));
+	l_free(data);
+}
+
+static bool local_rc2_cbc_set_iv(void *data,
+				const uint8_t *iv, size_t iv_length)
+{
+	struct rc2_state *s = data;
+
+	if (unlikely(iv_length != 8))
+		return false;
+
+	s->ctx[0].x[0] = l_get_le16(iv + 0);
+	s->ctx[0].x[1] = l_get_le16(iv + 2);
+	s->ctx[0].x[2] = l_get_le16(iv + 4);
+	s->ctx[0].x[3] = l_get_le16(iv + 6);
+	s->ctx[1].x64 = s->ctx[0].x64;
+	return true;
+}
+
+static ssize_t local_rc2_cbc_operate(void *data, __u32 operation,
+					const struct iovec *in, size_t in_cnt,
+					const struct iovec *out, size_t out_cnt)
+{
+	struct rc2_state *s = data;
+	struct iovec cur_in = {};
+	struct iovec cur_out = {};
+	struct rc2_state_ctx *ctx =
+		&s->ctx[operation == ALG_OP_ENCRYPT ? 1 : 0];
+
+#define CONSUME_IN(bytes, eof_ok)		\
+	cur_in.iov_len -= (bytes);		\
+	while (!cur_in.iov_len) {		\
+		if (!in_cnt) {			\
+			if (eof_ok)		\
+				break;		\
+			else			\
+				return -1;	\
+		}				\
+						\
+		cur_in = *in++;			\
+		in_cnt--;			\
+	}
+
+#define CONSUME_OUT(bytes)			\
+	cur_out.iov_len -= (bytes);		\
+	while (!cur_out.iov_len) {		\
+		if (!out_cnt)			\
+			return 0;		\
+						\
+		cur_out = *out++;		\
+		out_cnt--;			\
+	}
+
+	CONSUME_IN(0, true)
+	CONSUME_OUT(0)
+
+	while (cur_in.iov_len) {
+		union {
+			uint16_t x16[4];
+			uint64_t x64;
+		} inblk;
+
+		if (cur_in.iov_len >= 8) {
+#define CUR_IN16 (*(uint16_t **) &cur_in.iov_base)
+			inblk.x16[0] = l_get_le16(CUR_IN16++);
+			inblk.x16[1] = l_get_le16(CUR_IN16++);
+			inblk.x16[2] = l_get_le16(CUR_IN16++);
+			inblk.x16[3] = l_get_le16(CUR_IN16++);
+			CONSUME_IN(8, true)
+		} else {
+			inblk.x16[0] = *(uint8_t *) cur_in.iov_base++;
+			CONSUME_IN(1, false)
+			inblk.x16[0] |= (*(uint8_t *) cur_in.iov_base++) << 8;
+			CONSUME_IN(1, false)
+			inblk.x16[1] = *(uint8_t *) cur_in.iov_base++;
+			CONSUME_IN(1, false)
+			inblk.x16[1] |= (*(uint8_t *) cur_in.iov_base++) << 8;
+			CONSUME_IN(1, false)
+			inblk.x16[2] = *(uint8_t *) cur_in.iov_base++;
+			CONSUME_IN(1, false)
+			inblk.x16[2] |= (*(uint8_t *) cur_in.iov_base++) << 8;
+			CONSUME_IN(1, false)
+			inblk.x16[3] = *(uint8_t *) cur_in.iov_base++;
+			CONSUME_IN(1, false)
+			inblk.x16[3] |= (*(uint8_t *) cur_in.iov_base++) << 8;
+			CONSUME_IN(1, true)
+		}
+
+		if (operation == ALG_OP_ENCRYPT)
+			ctx->x64 = rc2_operate(s, inblk.x64 ^ ctx->x64,
+						operation);
+		else
+			ctx->x64 ^= rc2_operate(s, inblk.x64, operation);
+
+		if (cur_out.iov_len >= 8) {
+#define CUR_OUT16 (*(uint16_t **) &cur_out.iov_base)
+			l_put_le16(ctx->x[0], CUR_OUT16++);
+			l_put_le16(ctx->x[1], CUR_OUT16++);
+			l_put_le16(ctx->x[2], CUR_OUT16++);
+			l_put_le16(ctx->x[3], CUR_OUT16++);
+			CONSUME_OUT(8)
+		} else {
+			*(uint8_t *) cur_out.iov_base++ = ctx->x[0];
+			CONSUME_OUT(1)
+			*(uint8_t *) cur_out.iov_base++ = ctx->x[0] >> 8;
+			CONSUME_OUT(1)
+			*(uint8_t *) cur_out.iov_base++ = ctx->x[1];
+			CONSUME_OUT(1)
+			*(uint8_t *) cur_out.iov_base++ = ctx->x[1] >> 8;
+			CONSUME_OUT(1)
+			*(uint8_t *) cur_out.iov_base++ = ctx->x[2];
+			CONSUME_OUT(1)
+			*(uint8_t *) cur_out.iov_base++ = ctx->x[2] >> 8;
+			CONSUME_OUT(1)
+			*(uint8_t *) cur_out.iov_base++ = ctx->x[3];
+			CONSUME_OUT(1)
+			*(uint8_t *) cur_out.iov_base++ = ctx->x[3] >> 8;
+			CONSUME_OUT(1)
+		}
+
+		/* Save ciphertext as IV for next CBC block */
+		if (operation == ALG_OP_DECRYPT)
+			ctx->x64 = inblk.x64;
+
+		inblk.x64 = 0;
+	}
+
+	return 0;
+}
+
+static const struct local_impl local_rc2_cbc = {
+	local_rc2_cbc_new,
+	local_rc2_cbc_free,
+	local_rc2_cbc_set_iv,
+	local_rc2_cbc_operate,
+};
ell/cipher.h +1 -0
index e1a4bda..ed277a6 100644
@@ -37,6 +37,7 @@ enum l_cipher_type {
 	L_CIPHER_DES = 4,
 	L_CIPHER_DES_CBC,
 	L_CIPHER_DES3_EDE_CBC,
+	L_CIPHER_RC2_CBC,
 };
 
 struct l_cipher *l_cipher_new(enum l_cipher_type type,
Signature · 1 line
2.27.0
#3 lore raw

PATCH 4/13 cipher: Add ARC4 implementation

In reply to Andrew Zaborowski

Re-add the L_CIPHER_ARC4 stream cipher for use in decoding PKCS#12 files. Like RC2 it's implemented completely in cipher.c because the kernel API doesn't support it. This cipher is also discouraged where it can be avoided.

---
 ell/cipher.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/cipher.h |  4 +--
 2 files changed, 100 insertions(+), 2 deletions(-)
ell/cipher.c +98 -0
index a2db608..5d67d7a 100644
@@ -162,6 +162,8 @@ static const char *cipher_type_to_name(enum l_cipher_type type)
 		return "cbc(aes)";
 	case L_CIPHER_AES_CTR:
 		return "ctr(aes)";
+	case L_CIPHER_ARC4:
+		return NULL;
 	case L_CIPHER_DES:
 		return "ecb(des)";
 	case L_CIPHER_DES_CBC:
@@ -175,9 +177,11 @@ static const char *cipher_type_to_name(enum l_cipher_type type)
 	return NULL;
 }
 
+static const struct local_impl local_arc4;
 static const struct local_impl local_rc2_cbc;
 
 static const struct local_impl *local_impl_ciphers[] = {
+	[L_CIPHER_ARC4] = &local_arc4,
 	[L_CIPHER_RC2_CBC] = &local_rc2_cbc,
 };
 
@@ -729,6 +733,100 @@ LIB_EXPORT bool l_aead_cipher_is_supported(enum l_aead_cipher_type type)
 	return supported_aead_ciphers & (1 << type);
 }
 
+/* ARC4 implementation copyright (c) 2001 Niels Möller */
+
+#define SWAP(a, b) do { uint8_t _t = a; a = b; b = _t; } while (0)
+
+static void arc4_set_key(uint8_t *S, const uint8_t *key, size_t key_length)
+{
+	unsigned int i;
+	uint8_t j;
+
+	for (i = 0; i < 256; i++)
+		S[i] = i;
+
+	for (i = j = 0; i < 256; i++) {
+		j += S[i] + key[i % key_length];
+		SWAP(S[i], S[j]);
+	}
+}
+
+struct arc4_state {
+	struct arc4_state_ctx {
+		uint8_t S[256];
+		uint8_t i;
+		uint8_t j;
+	} ctx[2];
+};
+
+static void *local_arc4_new(enum l_cipher_type type,
+				const void *key, size_t key_length)
+{
+	struct arc4_state *s;
+
+	if (unlikely(key_length == 0 || key_length > 256))
+		return NULL;
+
+	s = l_new(struct arc4_state, 1);
+	arc4_set_key(s->ctx[0].S, key, key_length);
+	s->ctx[1] = s->ctx[0];
+	return s;
+}
+
+static void local_arc4_free(void *data)
+{
+	explicit_bzero(data, sizeof(struct arc4_state));
+	l_free(data);
+}
+
+static ssize_t local_arc4_operate(void *data, __u32 operation,
+					const struct iovec *in, size_t in_cnt,
+					const struct iovec *out, size_t out_cnt)
+{
+	struct arc4_state *s = data;
+	struct iovec cur_in;
+	struct iovec cur_out;
+	struct arc4_state_ctx *ctx =
+		&s->ctx[operation == ALG_OP_ENCRYPT ? 1 : 0];
+
+	if (!in_cnt || !out_cnt)
+		return 0;
+
+	cur_in = *in;
+	cur_out = *out;
+
+	while (1) {
+		while (!cur_in.iov_len) {
+			cur_in = *in++;
+
+			if (!--in_cnt)
+				return 0;
+		}
+
+		while (!cur_out.iov_len) {
+			cur_out = *out++;
+
+			if (!--out_cnt)
+				return 0;
+		}
+
+		ctx->j += ctx->S[++ctx->i];
+		SWAP(ctx->S[ctx->i], ctx->S[ctx->j]);
+		*(uint8_t *) cur_out.iov_base++ =
+			*(uint8_t *) cur_in.iov_base++ ^
+			ctx->S[(ctx->S[ctx->i] + ctx->S[ctx->j]) & 0xff];
+		cur_in.iov_len--;
+		cur_out.iov_len--;
+	}
+}
+
+static const struct local_impl local_arc4 = {
+	local_arc4_new,
+	local_arc4_free,
+	NULL,
+	local_arc4_operate,
+};
+
 struct rc2_state {
 	union {
 		uint16_t xkey[64];
ell/cipher.h +2 -2
index ed277a6..f4c31fa 100644
@@ -33,8 +33,8 @@ enum l_cipher_type {
 	L_CIPHER_AES = 0,
 	L_CIPHER_AES_CBC,
 	L_CIPHER_AES_CTR,
-	/* L_CIPHER_ARC4, */
-	L_CIPHER_DES = 4,
+	L_CIPHER_ARC4,
+	L_CIPHER_DES,
 	L_CIPHER_DES_CBC,
 	L_CIPHER_DES3_EDE_CBC,
 	L_CIPHER_RC2_CBC,
Signature · 1 line
2.27.0
#4 lore raw

Re: [PATCH 04/13] cipher: Add ARC4 implementation

Hi Andrew,

Quoted 9 lines · Andrew Zaborowski

Re-add the L_CIPHER_ARC4 stream cipher for use in decoding PKCS#12 files. Like RC2 it's implemented completely in cipher.c because the kernel API doesn't support it. This cipher is also discouraged where it can be avoided.

---
  ell/cipher.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++
  ell/cipher.h |  4 +--
  2 files changed, 100 insertions(+), 2 deletions(-)

Looks like there's a funny character in this patch somewhere and git am complains.

Regards,
-Denis

#5 lore raw

PATCH 5/13 pem: Don't expect padding with stream ciphers

In reply to Andrew Zaborowski

When decoding PKCS#8 encrypted certificates don't try to strip padding when the encryption uses a stream cipher (RC4). Standalone PKCS#8 files encrypted with PKCS#12 ciphers are not common (that I know of..) but they can be found embedded in PKCS#12 files.

---
 ell/pem.c           | 36 +++++++++++++++++++++++-------------
 ell/pkcs5-private.h |  3 ++-
 ell/pkcs5.c         | 13 +++++++++++--
 3 files changed, 36 insertions(+), 16 deletions(-)
ell/pem.c +23 -13
index 42c7975..1b995d5 100644
@@ -619,6 +619,8 @@ struct l_key *pem_key_from_pkcs8_encrypted_private_key_info(const uint8_t *der,
 	int i;
 	struct l_key *pkey;
 	bool r;
+	bool is_block;
+	size_t decrypted_len;
 
 	/* Technically this is BER, not limited to DER */
 	key_info = asn1_der_find_elem(der, der_len, 0, &tag, &key_info_len);
@@ -638,7 +640,8 @@ struct l_key *pem_key_from_pkcs8_encrypted_private_key_info(const uint8_t *der,
 	if (asn1_der_find_elem(der, der_len, 2, &tag, &tmp_len))
 		return NULL;
 
-	alg = pkcs5_cipher_from_alg_id(alg_id, alg_id_len, passphrase);
+	alg = pkcs5_cipher_from_alg_id(alg_id, alg_id_len, passphrase,
+					&is_block);
 	if (!alg)
 		return NULL;
 
@@ -652,21 +655,28 @@ struct l_key *pem_key_from_pkcs8_encrypted_private_key_info(const uint8_t *der,
 		return NULL;
 	}
 
-	/*
-	 * Strip padding as defined in RFC8018 (for PKCS#5 v1) or
-	 * RFC1423 / RFC5652 (for v2).
-	 */
-	pkey = NULL;
-	if (decrypted[data_len - 1] >= data_len ||
-			decrypted[data_len - 1] > 16)
-		goto cleanup;
+	decrypted_len = data_len;
+
+	if (is_block) {
+		/*
+		 * For block ciphers strip padding as defined in RFC8018
+		 * (for PKCS#5 v1) or RFC1423 / RFC5652 (for v2).
+		 */
+		pkey = NULL;
 
-	for (i = 1; i < decrypted[data_len - 1]; i++)
-		if (decrypted[data_len - 1 - i] != decrypted[data_len - 1])
+		if (decrypted[data_len - 1] >= data_len ||
+				decrypted[data_len - 1] > 16)
 			goto cleanup;
 
-	pkey = pem_key_from_pkcs8_private_key_info(decrypted,
-					data_len - decrypted[data_len - 1]);
+		for (i = 1; i < decrypted[data_len - 1]; i++)
+			if (decrypted[data_len - 1 - i] !=
+					decrypted[data_len - 1])
+				goto cleanup;
+
+		decrypted_len -= decrypted[data_len - 1];
+	}
+
+	pkey = pem_key_from_pkcs8_private_key_info(decrypted, decrypted_len);
 
 cleanup:
 	explicit_bzero(decrypted, data_len);
ell/pkcs5-private.h +2 -1
index f4f41f5..27ff41c 100644
@@ -22,4 +22,5 @@
 
 struct l_cipher *pkcs5_cipher_from_alg_id(const uint8_t *id_asn1,
 						size_t id_asn1_len,
-						const char *password);
+						const char *password,
+						bool *out_is_block);
ell/pkcs5.c +11 -2
index f225c71..21fda2b 100644
@@ -415,7 +415,8 @@ static struct l_cipher *pkcs5_cipher_from_pbes2_params(
 
 struct l_cipher *pkcs5_cipher_from_alg_id(const uint8_t *id_asn1,
 						size_t id_asn1_len,
-						const char *password)
+						const char *password,
+						bool *out_is_block)
 {
 	uint8_t tag;
 	const uint8_t *oid, *params, *salt, *iter_count_buf;
@@ -436,9 +437,13 @@ struct l_cipher *pkcs5_cipher_from_alg_id(const uint8_t *id_asn1,
 	if (asn1_der_find_elem(id_asn1, id_asn1_len, 2, &tag, &tmp_len))
 		return NULL;
 
-	if (asn1_oid_eq(&pkcs5_pbes2_oid, oid_len, oid))
+	if (asn1_oid_eq(&pkcs5_pbes2_oid, oid_len, oid)) {
+		if (out_is_block)
+			*out_is_block = true;
+
 		return pkcs5_cipher_from_pbes2_params(params, params_len,
 							password);
+	}
 
 	/* RFC8018 section A.3 */
 
@@ -484,5 +489,9 @@ struct l_cipher *pkcs5_cipher_from_alg_id(const uint8_t *id_asn1,
 	}
 
 	explicit_bzero(derived_key, 16);
+
+	if (out_is_block)
+		*out_is_block = true;
+
 	return cipher;
 }
Signature · 1 line
2.27.0
#6 lore raw

PATCH 6/13 pkcs5: Add the PKCS#12 KDF

In reply to Andrew Zaborowski

Add the key derivation algorithm used with PKCS#12 to pkcs5.c so that it can be found together with the two PKCS#5 KDFs, and so that it can also be used when parsing of the PKCS#12 AlgorithmIdentifiers in the next commit. This KDF is not recommended for new uses.

---
 ell/pkcs5-private.h |  12 ++++
 ell/pkcs5.c         | 146 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 158 insertions(+)
ell/pkcs5-private.h +12 -0
index 27ff41c..9b85fdd 100644
@@ -20,6 +20,18 @@
  *
  */
 
+struct pkcs12_hash {
+	enum l_checksum_type alg;
+	unsigned int len;
+	unsigned int u;
+	unsigned int v;
+	struct asn1_oid oid;
+};
+
+uint8_t *pkcs12_pbkdf(const char *password, const struct pkcs12_hash *hash,
+			const uint8_t *salt, size_t salt_len,
+			unsigned int iterations, uint8_t id, size_t key_len);
+
 struct l_cipher *pkcs5_cipher_from_alg_id(const uint8_t *id_asn1,
 						size_t id_asn1_len,
 						const char *password,
ell/pkcs5.c +146 -0

166 lines not on this page · raw message

Signature · 1 line
2.27.0
#7 lore raw

Re: [PATCH 06/13] pkcs5: Add the PKCS#12 KDF

Hi Andrew,

Quoted 9 lines · Andrew Zaborowski

Add the key derivation algorithm used with PKCS#12 to pkcs5.c so that it can be found together with the two PKCS#5 KDFs, and so that it can also be used when parsing of the PKCS#12 AlgorithmIdentifiers in the next commit. This KDF is not recommended for new uses.

---
  ell/pkcs5-private.h |  12 ++++
  ell/pkcs5.c         | 146 ++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 158 insertions(+)

<snip>

ell/pkcs5.c LIB_EXPORT bool l_pkcs5_pbkdf2(enum l_checksum_type type, const char *password, · line 197

Quoted 12 lines
ell/pkcs5.c +12 -0
+static bool utf8_to_bmpstring(const char *utf, uint8_t *out_buf,
+				unsigned int *out_len)
+{
+	unsigned int n;
+	int utflen;
+	wchar_t cp;
+	uint16_t *ptr = (uint16_t *) out_buf;
+
+	for (n = strlen(utf); n; n -= utflen, utf += utflen) {
+		if ((utflen = l_utf8_get_codepoint(utf, n, &cp)) <= 0 ||
+				cp > 0xffff)
+			return false;

So is this just a UCS2BE conversion? If so, then using l_utf8_to_ucs2be might be clearer.

ell/pkcs5.c LIB_EXPORT bool l_pkcs5_pbkdf2(enum l_checksum_type type, const char *password, · line 206

Quoted 9 lines
ell/pkcs5.c +9 -0
+
+		*ptr++ = L_CPU_TO_BE16(cp);
+	}
+
+	*ptr++ = 0;
+	*out_len = (uint8_t *) ptr - out_buf;
+	return true;
+}
+

Regards,
-Denis

#8 lore raw

Re: [PATCH 06/13] pkcs5: Add the PKCS#12 KDF

Hi Denis,

Quoted 13 lines · Denis Kenzior
Quoted 9 lines · Andrew Zaborowski

Add the key derivation algorithm used with PKCS#12 to pkcs5.c so that it can be found together with the two PKCS#5 KDFs, and so that it can also be used when parsing of the PKCS#12 AlgorithmIdentifiers in the next commit. This KDF is not recommended for new uses.

---
  ell/pkcs5-private.h |  12 ++++
  ell/pkcs5.c         | 146 ++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 158 insertions(+)

<snip>

Quoted 15 lines

ell/pkcs5.c LIB_EXPORT bool l_pkcs5_pbkdf2(enum l_checksum_type type, const char *password, · line 197

Quoted 12 lines
ell/pkcs5.c +12 -0
+static bool utf8_to_bmpstring(const char *utf, uint8_t *out_buf,
+                             unsigned int *out_len)
+{
+     unsigned int n;
+     int utflen;
+     wchar_t cp;
+     uint16_t *ptr = (uint16_t *) out_buf;
+
+     for (n = strlen(utf); n; n -= utflen, utf += utflen) {
+             if ((utflen = l_utf8_get_codepoint(utf, n, &cp)) <= 0 ||
+                             cp > 0xffff)
+                     return false;

So is this just a UCS2BE conversion? If so, then using l_utf8_to_ucs2be might be clearer.

Basically yes. But with l_utf8_to_ucs2be this will look something like:

uint8_t *usc2;

if (!l_utf8_validate(utf))

    return false;
  ucs2 = l_utf8_to_ucs2be(utf, out_len);
  if (!str)
    return false;

  memcpy(out_buf, ucs2, *out_len);
  explicit_bzero(ucs2);
  l_free(ucs2);

I can do that but it seems we're doing less work without using l_utf8_to_ucs2be here.

Best regards

#9 lore raw

Re: [PATCH 06/13] pkcs5: Add the PKCS#12 KDF

Hi Andrew,

Quoted 9 lines
Quoted 2 lines

So is this just a UCS2BE conversion? If so, then using l_utf8_to_ucs2be might be clearer.

Basically yes. But with l_utf8_to_ucs2be this will look something like:

   uint8_t *usc2;

   if (!l_utf8_validate(utf))
     return false;

You'll potentially need this step in either version actually. utf8_get_codepoint isn't really enough to tell whether the entire string is valid utf8. Where is the password coming from?

Also, I would think you'd pre-validate utf8 validity before ever getting to this point?

Quoted 11 lines
   ucs2 = l_utf8_to_ucs2be(utf, out_len);
   if (!str)
     return false;

   memcpy(out_buf, ucs2, *out_len);
   explicit_bzero(ucs2);
   l_free(ucs2);

I can do that but it seems we're doing less work without using l_utf8_to_ucs2be here.

You could add an own buffer version of utf8_to_ucs2be. But even this memcpy version seems to be cleaner than open-coding it.

Regards,
-Denis

#10 lore raw

Re: [PATCH 06/13] pkcs5: Add the PKCS#12 KDF

Hi Denis,

Quoted 5 lines · Denis Kenzior
Quoted 5 lines
Quoted 2 lines

So is this just a UCS2BE conversion? If so, then using l_utf8_to_ucs2be might be clearer.

Basically yes. But with l_utf8_to_ucs2be this will look something like:

Quoted 8 lines
Quoted 4 lines
   uint8_t *usc2;

   if (!l_utf8_validate(utf))
     return false;

You'll potentially need this step in either version actually. utf8_get_codepoint isn't really enough to tell whether the entire string is valid utf8.

Right, but as long as we don't crash and we can decrypt the contents, we don't care. And since I was first using strlen() and then passing the remaining byte count to l_utf8_get_codepoint() it shouldn't crash.

Quoted 1 line

Where is the password coming from?

Currently from the l_settings or the agent, so on second thought we can require that the password parameter to l_pem_load_container_file is valid UTF-8 and skip l_utf8_validate().

Quoted 13 lines

Also, I would think you'd pre-validate utf8 validity before ever getting to this point?

Quoted 9 lines
   ucs2 = l_utf8_to_ucs2be(utf, out_len);
   if (!str)
     return false;

   memcpy(out_buf, ucs2, *out_len);
   explicit_bzero(ucs2);
   l_free(ucs2);
Quoted 5 lines
Quoted 2 lines

I can do that but it seems we're doing less work without using l_utf8_to_ucs2be here.

You could add an own buffer version of utf8_to_ucs2be. But even this memcpy version seems to be cleaner than open-coding it.

Ok, let's use l_utf8_to_ucs2be then.

Best regards

#11 lore raw

PATCH 7/13 pkcs5: Add PKCS#12 algorithms in pkcs5_cipher_from_alg_id

In reply to Andrew Zaborowski

Since pkcs5_cipher_from_alg_id() is used for decrypting PKCS#8 private keys, teach it to also parse PKCS#12 AlgorithmIdentifier asn1 syntax (almost identical to PKCS#5). Ideally the pkcs5_* functions and pkcs5.c should be renamed to not include the PKCS#5 string because the callers are probably not interested in what cipher is used as long as they can use it to decrypt data their input. So ideally they want to call one function that understands all the common algorithm OIDs.

---
 ell/pkcs5.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 169 insertions(+), 3 deletions(-)
ell/pkcs5.c +169 -3
index bb02c33..ea0fcc5 100644
@@ -352,6 +352,74 @@ static const struct pkcs5_pbes1_encryption_oid {
 	/* MD2- and RC2-based schemes 1, 4, 6 and 11 not supported */
 };
 
+/* RFC7292 Appendix C */
+static const struct pkcs12_encryption_oid {
+	enum l_cipher_type cipher_type;
+	unsigned int key_length;
+	unsigned int iv_length;
+	bool copy_k1;	/* Expand the 2-Key 3DES key for 3-Key 3DES */
+	bool is_block;
+	struct asn1_oid oid;
+} pkcs12_encryption_oids[] = {
+	{ /* pbeWithSHAAnd128BitRC4 */
+		.cipher_type = L_CIPHER_ARC4,
+		.key_length = 16,
+		.oid = { 10, {
+			0x2a, 0x86, 0x48, 0x86, 0xf7,
+			0x0d, 0x01, 0x0c, 0x01, 0x01,
+		} }
+	},
+	{ /* pbeWithSHAAnd40BitRC4 */
+		.cipher_type = L_CIPHER_ARC4,
+		.key_length = 5,
+		.oid = { 10, {
+			0x2a, 0x86, 0x48, 0x86, 0xf7,
+			0x0d, 0x01, 0x0c, 0x01, 0x02,
+		} }
+	},
+	{ /* pbeWithSHAAnd3-KeyTripleDES-CBC */
+		.cipher_type = L_CIPHER_DES3_EDE_CBC,
+		.key_length = 24,
+		.iv_length = 8,
+		.is_block = true,
+		.oid = { 10, {
+			0x2a, 0x86, 0x48, 0x86, 0xf7,
+			0x0d, 0x01, 0x0c, 0x01, 0x03,
+		} }
+	},
+	{ /* pbeWithSHAAnd2-KeyTripleDES-CBC */
+		.cipher_type = L_CIPHER_DES3_EDE_CBC,
+		.key_length = 16,
+		.iv_length = 8,
+		.copy_k1 = true,
+		.is_block = true,
+		.oid = { 10, {
+			0x2a, 0x86, 0x48, 0x86, 0xf7,
+			0x0d, 0x01, 0x0c, 0x01, 0x04,
+		} }
+	},
+	{ /* pbeWithSHAAnd128BitRC2-CBC */
+		.cipher_type = L_CIPHER_RC2_CBC,
+		.key_length = 16,
+		.iv_length = 8,
+		.is_block = true,
+		.oid = { 10, {
+			0x2a, 0x86, 0x48, 0x86, 0xf7,
+			0x0d, 0x01, 0x0c, 0x01, 0x05,
+		} }
+	},
+	{ /* pbeWithSHAAnd40BitRC2-CBC */
+		.cipher_type = L_CIPHER_RC2_CBC,
+		.key_length = 5,
+		.iv_length = 8,
+		.is_block = true,
+		.oid = { 10, {
+			0x2a, 0x86, 0x48, 0x86, 0xf7,
+			0x0d, 0x01, 0x0c, 0x01, 0x06,
+		} }
+	},
+};
+
 static const struct pkcs5_digest_alg_oid {
 	enum l_checksum_type type;
 	struct asn1_oid oid;
@@ -528,8 +596,8 @@ static struct l_cipher *pkcs5_cipher_from_pbes2_params(
 	/* RFC8018 section B.2 */
 
 	/*
-	 * Since we don't support RC2/RC5, all our PKCS#5 ciphers only
-	 * have an obligatory OCTET STRING IV parameter and a fixed key
+	 * Since we don't support the RC2/RC5 PBES2 ciphers, our parameters
+	 * only have an obligatory OCTET STRING IV parameter and a fixed key
 	 * length.
 	 */
 	if (tag != ASN1_ID_OCTET_STRING || params_len != enc_scheme->iv_size)
@@ -559,6 +627,94 @@ static struct l_cipher *pkcs5_cipher_from_pbes2_params(
 	return cipher;
 }
 
+static struct l_cipher *pkcs12_cipher_from_alg_id(
+				const struct pkcs12_encryption_oid *scheme,
+				const uint8_t *params, size_t params_len,
+				const char *password, bool *out_is_block)
+{
+	uint8_t tag;
+	const uint8_t *salt;
+	const uint8_t *iterations_data;
+	size_t salt_len;
+	size_t iterations_len;
+	unsigned int iterations;
+	uint8_t *key;
+	size_t key_len;
+	struct l_cipher *cipher;
+
+	/* Same parameters as in PKCS#5 */
+	salt = asn1_der_find_elem(params, params_len, 0, &tag, &salt_len);
+	if (!salt || tag != ASN1_ID_OCTET_STRING)
+		return NULL;
+
+	iterations_data = asn1_der_find_elem(params, params_len, 1,
+						&tag, &iterations_len);
+	if (!iterations_data || tag != ASN1_ID_INTEGER ||
+			iterations_len < 1 || iterations_len > 4)
+		return NULL;
+
+	for (iterations = 0; iterations_len; iterations_len--)
+		iterations = (iterations << 8) | *iterations_data++;
+
+	if (iterations < 1 || iterations > 8192)
+		return NULL;
+
+	if (iterations_data != params + params_len)
+		return NULL;
+
+	key_len = scheme->key_length;
+	key = pkcs12_pbkdf(password, &pkcs12_sha1_hash, salt, salt_len,
+				iterations, 1, key_len);
+	if (!key)
+		return NULL;
+
+	if (scheme->copy_k1) {
+		/*
+		 * 2-Key 3DES is like L_CIPHER_DES3_EDE_CBC except the last
+		 * of the 3 8-byte keys is not generated using a KDF and
+		 * instead is a copy of the first key.  In other words
+		 * the first half of the 16-byte key material is appended
+		 * at the end to produce the 24 bytes for DES3_EDE_CBC.
+		 */
+		uint8_t *key2 = l_malloc(24);
+
+		memcpy(key2, key, 16);
+		memcpy(key2 + 16, key, 8);
+		explicit_bzero(key, key_len);
+		l_free(key);
+		key = key2;
+		key_len = 24;
+	}
+
+	cipher = l_cipher_new(scheme->cipher_type, key, key_len);
+	explicit_bzero(key, key_len);
+	l_free(key);
+
+	if (!cipher)
+		return NULL;
+
+	if (scheme->iv_length) {
+		uint8_t *iv = pkcs12_pbkdf(password, &pkcs12_sha1_hash,
+						salt, salt_len, iterations, 2,
+						scheme->iv_length);
+
+		if (!iv || !l_cipher_set_iv(cipher, iv, scheme->iv_length)) {
+			l_cipher_free(cipher);
+			cipher = NULL;
+		}
+
+		if (iv)
+			explicit_bzero(iv, scheme->iv_length);
+
+		l_free(iv);
+	}
+
+	if (out_is_block)
+		*out_is_block = scheme->is_block;
+
+	return cipher;
+}
+
 struct l_cipher *pkcs5_cipher_from_alg_id(const uint8_t *id_asn1,
 						size_t id_asn1_len,
 						const char *password,
@@ -601,8 +757,18 @@ struct l_cipher *pkcs5_cipher_from_alg_id(const uint8_t *id_asn1,
 		}
 	}
 
-	if (!pbes1_scheme)
+	/* Check if this is a PKCS#12 OID */
+	if (!pbes1_scheme) {
+		for (i = 0; i < L_ARRAY_SIZE(pkcs12_encryption_oids); i++)
+			if (asn1_oid_eq(&pkcs12_encryption_oids[i].oid,
+					oid_len, oid))
+				return pkcs12_cipher_from_alg_id(
+						&pkcs12_encryption_oids[i],
+						params, params_len, password,
+						out_is_block);
+
 		return NULL;
+	}
 
 	salt = asn1_der_find_elem(params, params_len, 0, &tag, &tmp_len);
 	if (!salt || tag != ASN1_ID_OCTET_STRING || tmp_len != 8)
Signature · 1 line
2.27.0
#12 lore raw

PATCH 8/13 pem: Add l_pem_load_container_file

In reply to Andrew Zaborowski

Add a function that takes a file path and detects what X.509 certificate and/or private key format it uses and parses it accordingly. This is to make it easier for clients to support multiple file formats, including raw X.509 certificates -- without this they would have to load the contents of the file and call l_cert_new_from_der(). l_pem_load_container can also be used instead of l_pem_load_certificate_chain() and l_pem_load_private_key().

This function can now load binary certificate files which are not PEM but there wasn't a better place for it than pem.c. I guess the name could also be improved to imply that it's for certificate and private key container files, but I couldn't come up with a name that isn't too long.

This function treats PEM files as containers that can have both private keys and certificates at the same time, openssl can parse such files and also produces such files in some situations and they may have some good uses.

---
 ell/ell.sym |   1 +
 ell/pem.c   | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 ell/pem.h   |   5 ++
 3 files changed, 158 insertions(+), 5 deletions(-)
ell/ell.sym +1 -0
index 49689d1..239ab65 100644
@@ -415,6 +415,7 @@ global:
 	l_pem_load_file;
 	l_pem_load_private_key;
 	l_pem_load_private_key_from_data;
+	l_pem_load_container_file;
 	/* pkcs5 */
 	l_pkcs5_pbkdf1;
 	l_pkcs5_pbkdf2;
ell/pem.c +152 -5

190 lines not on this page · raw message

ell/pem.h +5 -0
index 1c93e7a..ce5af04 100644
@@ -50,6 +50,11 @@ struct l_key *l_pem_load_private_key_from_data(const void *buf, size_t len,
 						const char *passphrase,
 						bool *encrypted);
 
+void l_pem_load_container_file(const char *filename, const char *password,
+				struct l_certchain **out_certchain,
+				struct l_key **out_privkey,
+				bool *out_encrypted);
+
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.27.0
#13 lore raw

PATCH 9/13 pem: Add PKCS#12 parsing

In reply to Andrew Zaborowski

Add ability to load PKCS#12 files in l_pem_load_container_file(). This has only been tested with the files produced by openssl so far.

Extend the static buffer in struct asn1_oid (asn1-private.h) so we can handle some 11-byte OIDs used in PKCS#12 files.

---
 ell/asn1-private.h |   2 +-
 ell/pem.c          | 612 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 613 insertions(+), 1 deletion(-)
ell/asn1-private.h +1 -1
index 2a31241..079570b 100644
@@ -37,7 +37,7 @@
 
 struct asn1_oid {
 	uint8_t asn1_len;
-	uint8_t asn1[10];
+	uint8_t asn1[11];
 };
 
 #define asn1_oid_eq(oid1, oid2_len, oid2_string) \
ell/pem.c +612 -0

636 lines not on this page · raw message

Signature · 1 line
2.27.0
#14 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 6 lines · Andrew Zaborowski

bzero some more potentially sensitive memory that we weren't bzeroing in the recently added openssl legacy PKCS#1 private key format decoding.

---
  ell/pem.c | 18 +++++++++++++-----
  1 file changed, 13 insertions(+), 5 deletions(-)

I went ahead and applied 1, 2, 3 and 5.

Regards,
-Denis