pkcs5: Add the PKCS#12 KDF
PATCH 1/5 pkcs5: Add the PKCS#12 KDF
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 | 121 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+)
ell/pkcs5-private.h +12 -0
@@ -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 +121 -0
141 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH 2/5 pkcs5: Add PKCS#12 algorithms in pkcs5_cipher_from_alg_id
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 | 181 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 178 insertions(+), 3 deletions(-)
ell/pkcs5.c +178 -3
@@ -300,6 +300,15 @@ uint8_t *pkcs12_pbkdf(const char *password, const struct pkcs12_hash *hash,return key; } +/* RFC7292 Appendix A */ +static const struct pkcs12_hash pkcs12_sha1_hash = { + .alg =L_CHECKSUM_SHA1 , + .len =20 , + .u =20 , + .v =64 , + .oid = {5 , {0x2b ,0x0e ,0x03 ,0x02 ,0x1a } }, +}; +/* RFC8018 Section A.2 */ static struct asn1_oid pkcs5_pbkdf2_oid = {9 , {0x2a ,0x86 ,0x48 ,0x86 ,0xf7 ,0x0d ,0x01 ,0x05 ,0x0c }
@@ -327,6 +336,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;
@@ -503,8 +580,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)
@@ -534,6 +611,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,
@@ -576,8 +741,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
PATCH 3/5 pem: Add l_pem_load_container_file
In reply to Andrew ZaborowskiAdd 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
@@ -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
@@ -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
Re: [PATCH 3/5] pem: Add l_pem_load_container_file
Hi Andrew,
Quoted 9 lines · 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_containercan also be used instead ofl_pem_load_certificate_chain()andl_pem_load_private_key().This function can now load binary certificate files which are not PEM
So this really makes it seem that it shouldn't be in pem.c?
Quoted 4 lines
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.
Ok, nitpicking here, but this reasoning really belongs in the metadata, not the commit description.
So P12 files are mostly binary, but can also be PEM encoded, correct? Should we put the pkcs12 DER loading into pkcs12.c or so? We could also consider renaming pkcs5.c to pkcs.c since it seems pkcs5.c is becoming a bit of a jumble of pkcs8, pkcs5 and pkcs12 bits...
Still, I'm really unconvinced that this logically belongs in pem.c. Besides iwd's TLS module, who is going to be using this function? Maybe this belongs in iwd itself?
Quoted 102 lines
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@@ -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 +31
-5@@ -195,7 +195,7 @@ const char *pem_next(const void *buf, size_t buf_len, char **type_label,static uint8_t *pem_load_buffer (const void *buf,size_t buf_len,char **out_type_label,size_t *out_len, -char **out_headers) +char **out_headers,const char **out_endp) {size_t base64_len;const char *base64;@@ -203,7 +203,7 @@ static uint8_t *pem_load_buffer(const void *buf, size_t buf_len,uint8_t *ret; base64 =pem_next (buf, buf_len, &label, &base64_len, -NULL , false); + out_endp, false);if (!base64)return NULL ;@@ -262,7 +262,7 @@ static uint8_t *pem_load_buffer(const void *buf, size_t buf_len, LIB_EXPORT uint8_t *l_pem_load_buffer(const void *buf, size_t buf_len,char **type_label,size_t *out_len) { -return pem_load_buffer (buf, buf_len, type_label, out_len,NULL ); +return pem_load_buffer (buf, buf_len, type_label, out_len,NULL ,NULL ); }struct pem_file_info {@@ -315,7 +315,8 @@ static uint8_t *pem_load_file(const char *filename, char **out_type_label,return NULL ; result =pem_load_buffer (file.data, file.st.st_size, - out_type_label, out_len, out_headers); + out_type_label, out_len, out_headers, +NULL );pem_file_close (&file);return result; }@@ -888,7 +889,7 @@ LIB_EXPORT struct l_key *l_pem_load_private_key_from_data(const void *buf,if (encrypted) *encrypted = false; - content =pem_load_buffer (buf, buf_len, &label, &len, &headers); + content =pem_load_buffer (buf, buf_len, &label, &len, &headers,NULL );if (!content)return NULL ;@@ -933,3 +934,149 @@ LIB_EXPORT struct l_key *l_pem_load_private_key(const char *filename,return pem_load_private_key (content, len, label, passphrase, headers, encrypted); } + +/* +* Look at a file, try to detect which of the few X.509 certificate and/or +* private key container formats it uses and load any certificates in it as +* a certificate chain object, and load the first private key as an l_key +* object. +* +* Currently supported are: +* PEM X.509 certificates +* PEM PKCS#8 encrypted and unenecrypted private keys +* PEM legacy PKCS#1 encrypted and unenecrypted private keys +* Raw X.509 certificates (.cer, .der, .crt) +* +* The raw format contains exactly one certificate, PEM and PKCS#12 files +* can contain any combination of certificates and private keys. +*/ +LIB_EXPORT 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) +{ +struct pem_file_info file; +const char *ptr; +size_t len;
ell/pem.c LIB_EXPORT struct l_key *l_pem_load_private_key(const char *filename, · line 971
Quoted 10 lines
ell/pem.c +10
-0+bool error = false; + +if (out_encrypted) + *out_encrypted = false; + +if (out_certchain) + *out_certchain =NULL ; + +if (out_privkey) + *out_privkey =NULL ;
This kind of goes against our design patterns. As a rule our functions should not side-effect on failure.
ell/pem.c LIB_EXPORT struct l_key *l_pem_load_private_key(const char *filename, · line 974
Quoted 3 lines
ell/pem.c +3
-0+ +if (unlikely (!filename)) +return ;
And failing silently seems wrong as well...?
Quoted 115 lines
ell/pem.c +108
-0+ +if (pem_file_open (&file, filename) <0 ) +return ; + +if (file.st.st_size <1 ) + goto close; + +/* See if we have a DER sequence tag at the start */ +if (file.data[0 ] ==ASN1_ID_SEQUENCE ) { +const uint8_t *seq_data; +const uint8_t *elem_data; +size_t elem_len; +uint8_t tag; + +if (!(seq_data =asn1_der_find_elem (file.data, file.st.st_size, +0 , &tag, &len))) + goto not_der_after_all; + +/* +* See if the first sub-element is another sequence, then, out +* of the formats that we currently support this can only be a +* raw certificate. If integer, it's going to be PKCS#12. If +* we wish to add any more formats we'll probably need to start +* guessing from the filename suffix. +*/ +if (!(elem_data =asn1_der_find_elem (seq_data, len, +0 , &tag, &elem_len))) + goto not_der_after_all; + +if (tag ==ASN1_ID_SEQUENCE ) { +if (out_certchain) { +struct l_cert *cert; + +if (!(cert =l_cert_new_from_der (file.data, + file.st.st_size))) + goto close; + + *out_certchain =certchain_new_from_leaf (cert); + } + + goto close; + } + } + +not_der_after_all: +/* +* RFC 7486 allows whitespace and possibly other data before the +* PEM "encapsulation boundary" so rather than check if the start +* of the data looks like PEM, we fall back to this format if the +* data didn't look like anything else we knew about. +*/ + ptr = (const char *) file.data; + len = file.st.st_size; +while (!error && len) { +uint8_t *der; +size_t der_len; +char *type_label; +char *headers; +const char *endp; + +if (!(der =pem_load_buffer (ptr, len, &type_label, &der_len, + &headers, &endp))) +break ; + + len -= endp - ptr; + ptr = endp; + +if (!strcmp (type_label,"CERTIFICATE" )) { +struct l_cert *cert; + +if (!out_certchain) + goto next; + +if (!(cert =l_cert_new_from_der (der, der_len))) { +l_certchain_free (*out_certchain); + *out_certchain =NULL ; + error = true; + goto next; + } + +if (!*out_certchain) + *out_certchain =certchain_new_from_leaf (cert); +else +certchain_link_issuer (*out_certchain, cert); + + goto next; + } + +/* Only use the first private key found */ +if (out_privkey && !*out_privkey) { + *out_privkey =pem_load_private_key (der, der_len, + type_label, + password, + headers, + out_encrypted); +continue ; + } + +next: +explicit_bzero (der, der_len); +l_free (der); +l_free (type_label); +l_free (headers); + } + +close: +pem_file_close (&file); +}ell/pem.h +0
-0@@ -50,6 +50,11 @@ struct l_key *l_pem_load_private_key_from_data(const void *buf, size_t len, const char *passphrase,
ell/pem.h struct l_key *l_pem_load_private_key_from_data(const void *buf, size_t len, · line 60
Quoted 10 lines
ell/pem.h +5
-0bool *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
Regards,
-Denis
Re: [PATCH 3/5] pem: Add l_pem_load_container_file
Hi Denis,
Quoted 13 lines · Denis Kenzior
Quoted 9 lines · 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_containercan also be used instead ofl_pem_load_certificate_chain()andl_pem_load_private_key().This function can now load binary certificate files which are not PEM
So this really makes it seem that it shouldn't be in
pem.c?
Quoted 7 lines
Quoted 4 lines
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.Ok, nitpicking here, but this reasoning really belongs in the metadata, not the commit description.
Ok, maybe a code comment would have been better (or just omitting this.) Anyway..
Quoted 5 lines
So P12 files are mostly binary, but can also be PEM encoded, correct? Should we put the pkcs12 DER loading into
pkcs12.cor so? We could also consider renamingpkcs5.ctopkcs.csince it seemspkcs5.cis becoming a bit of a jumble of pkcs8, pkcs5 and pkcs12 bits...
I initially wanted to add it in cert.c, that's why I exposed pem_key_from_pkcs8_private_key_info() and pem_key_from_pkcs8_encrypted_private_key_info() in pem-private.h, but I ended up using them only from within pem.c because cert.c didn't seem right for private key stuff. But maybe that is a good idea.
So cert.c could contain the high-level file loading/parsing/decoding, and then rather than pkcs.c I'd call it cert-crypto.c or similar for the low-level crypto stuff?
The reasoning is that PKCS defined some of those formats and algorithms but some of it comes from openssl code, and a lot is really currently defined by RFCs. Initially I think each PKCS standard was republished as an RFC but each of those RFCs has since been obsoleted by sets of newer RFCs.
Quoted 4 lines
Still, I'm really unconvinced that this logically belongs in
pem.c. Besides iwd's TLS module, who is going to be using this function? Maybe this belongs in iwd itself?
I see it more as library code (like gnutls), anyone using TLS will need certificate loading. ell's examples/https-* should also be switched to use this function.
Quoted 108 lines
Quoted 108 lines
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@@ -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 +36
-5@@ -195,7 +195,7 @@ const char *pem_next(const void *buf, size_t buf_len, char **type_label,static uint8_t *pem_load_buffer (const void *buf,size_t buf_len,char **out_type_label,size_t *out_len, -char **out_headers) +char **out_headers,const char **out_endp) {size_t base64_len;const char *base64;@@ -203,7 +203,7 @@ static uint8_t *pem_load_buffer(const void *buf, size_t buf_len,uint8_t *ret; base64 =pem_next (buf, buf_len, &label, &base64_len, -NULL , false); + out_endp, false);if (!base64)return NULL ;@@ -262,7 +262,7 @@ static uint8_t *pem_load_buffer(const void *buf, size_t buf_len, LIB_EXPORT uint8_t *l_pem_load_buffer(const void *buf, size_t buf_len,char **type_label,size_t *out_len) { -return pem_load_buffer (buf, buf_len, type_label, out_len,NULL ); +return pem_load_buffer (buf, buf_len, type_label, out_len,NULL ,NULL ); }struct pem_file_info {@@ -315,7 +315,8 @@ static uint8_t *pem_load_file(const char *filename, char **out_type_label,return NULL ; result =pem_load_buffer (file.data, file.st.st_size, - out_type_label, out_len, out_headers); + out_type_label, out_len, out_headers, +NULL );pem_file_close (&file);return result; }@@ -888,7 +889,7 @@ LIB_EXPORT struct l_key *l_pem_load_private_key_from_data(const void *buf,if (encrypted) *encrypted = false; - content =pem_load_buffer (buf, buf_len, &label, &len, &headers); + content =pem_load_buffer (buf, buf_len, &label, &len, &headers,NULL );if (!content)return NULL ;@@ -933,3 +934,149 @@ LIB_EXPORT struct l_key *l_pem_load_private_key(const char *filename,return pem_load_private_key (content, len, label, passphrase, headers, encrypted); } + +/* +* Look at a file, try to detect which of the few X.509 certificate and/or +* private key container formats it uses and load any certificates in it as +* a certificate chain object, and load the first private key as an l_key +* object. +* +* Currently supported are: +* PEM X.509 certificates +* PEM PKCS#8 encrypted and unenecrypted private keys +* PEM legacy PKCS#1 encrypted and unenecrypted private keys +* Raw X.509 certificates (.cer, .der, .crt) +* +* The raw format contains exactly one certificate, PEM and PKCS#12 files +* can contain any combination of certificates and private keys. +*/ +LIB_EXPORT 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) +{ +struct pem_file_info file; +const char *ptr; +size_t len; +bool error = false; + +if (out_encrypted) + *out_encrypted = false; +
Quoted 8 lines
ell/pem.c LIB_EXPORT struct l_key *l_pem_load_private_key(const char *filename, · line 971
Quoted 5 lines
ell/pem.c +5
-0+if (out_certchain) + *out_certchain =NULL ; + +if (out_privkey) + *out_privkey =NULL ;This kind of goes against our design patterns. As a rule our functions should not side-effect on failure.
In this case this is how we signal the failure to load any certificates and private keys, this just replaces the return value since I needed a python tuple-like type.
So this should be read as something like:
r = (NULL, NULL, false);
Quoted 6 lines
ell/pem.c LIB_EXPORT struct l_key *l_pem_load_private_key(const char *filename, · line 974
Quoted 4 lines
ell/pem.c +3
-0+ +if (unlikely (!filename)) +return ;And failing silently seems wrong as well...?
And this as:
return r;
I can change the actual return type from void to bool if you prefer. I'll still need a way to signal whether the failure is due to encryption. In the existing API we do this by setting *out_encrypted.
Best regards
Re: [PATCH 3/5] pem: Add l_pem_load_container_file
Hi Andrew,
Quoted 12 lines
Quoted 5 lines
So P12 files are mostly binary, but can also be PEM encoded, correct? Should we put the pkcs12 DER loading into
pkcs12.cor so? We could also consider renamingpkcs5.ctopkcs.csince it seemspkcs5.cis becoming a bit of a jumble of pkcs8, pkcs5 and pkcs12 bits...I initially wanted to add it in
cert.c, that's why I exposedpem_key_from_pkcs8_private_key_info()andpem_key_from_pkcs8_encrypted_private_key_info()inpem-private.h, but I ended up using them only from withinpem.cbecausecert.cdidn't seem right for private key stuff. But maybe that is a good idea.
Quoted 3 lines
So
cert.ccould contain the high-level file loading/parsing/decoding, and then rather thanpkcs.cI'd call itcert-crypto.cor similar for the low-level crypto stuff?
Yes, something like this...
Quoted 7 lines
The reasoning is that PKCS defined some of those formats and algorithms but some of it comes from openssl code, and a lot is really currently defined by RFCs. Initially I think each PKCS standard was republished as an RFC but each of those RFCs has since been obsoleted by sets of newer RFCs.
I mean ideally we'd have each file format being handled in a separate file, either named according to the RFC or well known name. So pkcs5.c, pkcs12.c, etc. But if you want to put this all into one file that is also fine since they're all closely related.
Quoted 8 lines
Quoted 4 lines
Still, I'm really unconvinced that this logically belongs in
pem.c. Besides iwd's TLS module, who is going to be using this function? Maybe this belongs in iwd itself?I see it more as library code (like gnutls), anyone using TLS will need certificate loading. ell's examples/https-* should also be switched to use this function.
Hmm, where does this end though since we also need to support the system CA store, CAs loaded externally and given via keyring ids, TPM/TPM2 keys, etc... Not against the idea, but not sure that it is worth it for 1 real user...
Quoted 118 lines
Quoted 109 lines
Quoted 109 lines
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@@ -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 +36
-5@@ -195,7 +195,7 @@ const char *pem_next(const void *buf, size_t buf_len, char **type_label,static uint8_t *pem_load_buffer (const void *buf,size_t buf_len,char **out_type_label,size_t *out_len, -char **out_headers) +char **out_headers,const char **out_endp) {size_t base64_len;const char *base64;@@ -203,7 +203,7 @@ static uint8_t *pem_load_buffer(const void *buf, size_t buf_len,uint8_t *ret; base64 =pem_next (buf, buf_len, &label, &base64_len, -NULL , false); + out_endp, false);if (!base64)return NULL ;@@ -262,7 +262,7 @@ static uint8_t *pem_load_buffer(const void *buf, size_t buf_len, LIB_EXPORT uint8_t *l_pem_load_buffer(const void *buf, size_t buf_len,char **type_label,size_t *out_len) { -return pem_load_buffer (buf, buf_len, type_label, out_len,NULL ); +return pem_load_buffer (buf, buf_len, type_label, out_len,NULL ,NULL ); }struct pem_file_info {@@ -315,7 +315,8 @@ static uint8_t *pem_load_file(const char *filename, char **out_type_label,return NULL ; result =pem_load_buffer (file.data, file.st.st_size, - out_type_label, out_len, out_headers); + out_type_label, out_len, out_headers, +NULL );pem_file_close (&file);return result; }@@ -888,7 +889,7 @@ LIB_EXPORT struct l_key *l_pem_load_private_key_from_data(const void *buf,if (encrypted) *encrypted = false; - content =pem_load_buffer (buf, buf_len, &label, &len, &headers); + content =pem_load_buffer (buf, buf_len, &label, &len, &headers,NULL );if (!content)return NULL ;@@ -933,3 +934,149 @@ LIB_EXPORT struct l_key *l_pem_load_private_key(const char *filename,return pem_load_private_key (content, len, label, passphrase, headers, encrypted); } + +/* +* Look at a file, try to detect which of the few X.509 certificate and/or +* private key container formats it uses and load any certificates in it as +* a certificate chain object, and load the first private key as an l_key +* object. +* +* Currently supported are: +* PEM X.509 certificates +* PEM PKCS#8 encrypted and unenecrypted private keys +* PEM legacy PKCS#1 encrypted and unenecrypted private keys +* Raw X.509 certificates (.cer, .der, .crt) +* +* The raw format contains exactly one certificate, PEM and PKCS#12 files +* can contain any combination of certificates and private keys. +*/ +LIB_EXPORT 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) +{ +struct pem_file_info file; +const char *ptr; +size_t len; +bool error = false; + +if (out_encrypted) + *out_encrypted = false; +Quoted 8 lines
ell/pem.c LIB_EXPORT struct l_key *l_pem_load_private_key(const char *filename, · line 971
Quoted 5 lines
ell/pem.c +5
-0+if (out_certchain) + *out_certchain =NULL ; + +if (out_privkey) + *out_privkey =NULL ;This kind of goes against our design patterns. As a rule our functions should not side-effect on failure.
Quoted 7 lines
In this case this is how we signal the failure to load any certificates and private keys, this just replaces the return value since I needed a python tuple-like type.
So this should be read as something like:
r = (NULL, NULL, false);
ell isn't python though, and this is completely out of sync with the rest of the API. Consistency trumps everything else...
Quoted 8 lines
Quoted 7 lines
ell/pem.c LIB_EXPORT struct l_key *l_pem_load_private_key(const char *filename, · line 974
Quoted 5 lines
ell/pem.c +3
-0+ +if (unlikely (!filename)) +return ;And failing silently seems wrong as well...?
Quoted 8 lines
And this as:
return r;I can change the actual return type from void to bool if you prefer. I'll still need a way to signal whether the failure is due to encryption. In the existing API we do this by setting
*out_encrypted.
The latter would be more consistent and thus more preferable. Maybe we should have made these return an int and set a negative errno for the special cases...
Regarrds
-Denis
Re: [PATCH 3/5] pem: Add l_pem_load_container_file
Quoted 25 lines · Denis Kenzior
Quoted 11 lines
Quoted 4 lines
So P12 files are mostly binary, but can also be PEM encoded, correct? Should we put the pkcs12 DER loading into
pkcs12.cor so? We could also consider renamingpkcs5.ctopkcs.csince it seemspkcs5.cis becoming a bit of a jumble of pkcs8, pkcs5 and pkcs12 bits...I initially wanted to add it in
cert.c, that's why I exposedpem_key_from_pkcs8_private_key_info()andpem_key_from_pkcs8_encrypted_private_key_info()inpem-private.h, but I ended up using them only from withinpem.cbecausecert.cdidn't seem right for private key stuff. But maybe that is a good idea.Quoted 3 lines
So
cert.ccould contain the high-level file loading/parsing/decoding, and then rather thanpkcs.cI'd call itcert-crypto.cor similar for the low-level crypto stuff?Yes, something like this...
Quoted 7 lines
The reasoning is that PKCS defined some of those formats and algorithms but some of it comes from openssl code, and a lot is really currently defined by RFCs. Initially I think each PKCS standard was republished as an RFC but each of those RFCs has since been obsoleted by sets of newer RFCs.
Quoted 4 lines
I mean ideally we'd have each file format being handled in a separate file, either named according to the RFC or well known name. So
pkcs5.c,pkcs12.c, etc. But if you want to put this all into one file that is also fine since they're all closely related.
I'm open to ideas, but we could end up with too many files... In the case of pkcs5 it defines some crypto algorithms but those are not the only ones that can be used (in pkcs8 etc.), the RFCs have added algorithms that were not in any PKCS standards and deprected some too. But then PKCS just stands for public-key-cryptography-standards, so I guess pkcs.c is as good as cert.c, but I'd separate the hardcore crypto stuff into pkcs-crypto.c or cert-crypto.c.
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Still, I'm really unconvinced that this logically belongs in
pem.c. Besides iwd's TLS module, who is going to be using this function? Maybe this belongs in iwd itself?
Quoted 6 lines
Quoted 3 lines
I see it more as library code (like gnutls), anyone using TLS will need certificate loading. ell's examples/https-* should also be switched to use this function.
Hmm, where does this end though since we also need to support the system CA store, CAs loaded externally and given via keyring ids, TPM/TPM2 keys, etc...
I'm ok moving all of the pkcs parsing into IWD if you prefer but I don't see a real reason to keep any of those things outside ell either... maybe except API stability.
Loading files is pretty basic though, we need some of it even for ell's own unit tests to be able to load test data.
Quoted 1 line
Not against the idea, but not sure that it is worth it for 1 real user...
You could also say this about l_tls, l_dhcp, l_genl, etc.
Quoted 140 lines
Quoted 119 lines
Quoted 110 lines
Quoted 110 lines
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@@ -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 +36
-5@@ -195,7 +195,7 @@ const char *pem_next(const void *buf, size_t buf_len, char **type_label,static uint8_t *pem_load_buffer (const void *buf,size_t buf_len,char **out_type_label,size_t *out_len, -char **out_headers) +char **out_headers,const char **out_endp) {size_t base64_len;const char *base64;@@ -203,7 +203,7 @@ static uint8_t *pem_load_buffer(const void *buf, size_t buf_len,uint8_t *ret; base64 =pem_next (buf, buf_len, &label, &base64_len, -NULL , false); + out_endp, false);if (!base64)return NULL ;@@ -262,7 +262,7 @@ static uint8_t *pem_load_buffer(const void *buf, size_t buf_len, LIB_EXPORT uint8_t *l_pem_load_buffer(const void *buf, size_t buf_len,char **type_label,size_t *out_len) { -return pem_load_buffer (buf, buf_len, type_label, out_len,NULL ); +return pem_load_buffer (buf, buf_len, type_label, out_len,NULL ,NULL ); }struct pem_file_info {@@ -315,7 +315,8 @@ static uint8_t *pem_load_file(const char *filename, char **out_type_label,return NULL ; result =pem_load_buffer (file.data, file.st.st_size, - out_type_label, out_len, out_headers); + out_type_label, out_len, out_headers, +NULL );pem_file_close (&file);return result; }@@ -888,7 +889,7 @@ LIB_EXPORT struct l_key *l_pem_load_private_key_from_data(const void *buf,if (encrypted) *encrypted = false; - content =pem_load_buffer (buf, buf_len, &label, &len, &headers); + content =pem_load_buffer (buf, buf_len, &label, &len, &headers,NULL );if (!content)return NULL ;@@ -933,3 +934,149 @@ LIB_EXPORT struct l_key *l_pem_load_private_key(const char *filename,return pem_load_private_key (content, len, label, passphrase, headers, encrypted); } + +/* +* Look at a file, try to detect which of the few X.509 certificate and/or +* private key container formats it uses and load any certificates in it as +* a certificate chain object, and load the first private key as an l_key +* object. +* +* Currently supported are: +* PEM X.509 certificates +* PEM PKCS#8 encrypted and unenecrypted private keys +* PEM legacy PKCS#1 encrypted and unenecrypted private keys +* Raw X.509 certificates (.cer, .der, .crt) +* +* The raw format contains exactly one certificate, PEM and PKCS#12 files +* can contain any combination of certificates and private keys. +*/ +LIB_EXPORT 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) +{ +struct pem_file_info file; +const char *ptr; +size_t len; +bool error = false; + +if (out_encrypted) + *out_encrypted = false; +Quoted 8 lines
ell/pem.c LIB_EXPORT struct l_key *l_pem_load_private_key(const char *filename, · line 971
Quoted 5 lines
ell/pem.c +5
-0+if (out_certchain) + *out_certchain =NULL ; + +if (out_privkey) + *out_privkey =NULL ;This kind of goes against our design patterns. As a rule our functions should not side-effect on failure.
Quoted 7 lines
In this case this is how we signal the failure to load any certificates and private keys, this just replaces the return value since I needed a python tuple-like type.
So this should be read as something like:
r = (NULL, NULL, false);ell isn't python though, and this is completely out of sync with the rest of the API. Consistency trumps everything else...
Quoted 10 lines
Quoted 7 lines
ell/pem.c LIB_EXPORT struct l_key *l_pem_load_private_key(const char *filename, · line 974
Quoted 5 lines
ell/pem.c +3
-0+ +if (unlikely (!filename)) +return ;And failing silently seems wrong as well...?
And this as:
Quoted 8 lines
Quoted 6 lines
return r;I can change the actual return type from void to bool if you prefer. I'll still need a way to signal whether the failure is due to encryption. In the existing API we do this by setting
*out_encrypted.The latter would be more consistent and thus more preferable.
Ok.
Quoted 2 lines
Maybe we should
have made these return an int and set a negative errno for the special cases...
I can return the errno too... but we start being a little redundant.
You can also treat those situations as a "nothing was loaded" reply rather than an error, and the caller can decide what it wants to do with it.
Best regards
PATCH 4/5 pem: Add PKCS#12 parsing
In reply to Andrew ZaborowskiAdd 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 | 624 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 625 insertions(+), 1 deletion(-)
ell/asn1-private.h +1 -1
@@ -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 +624 -0
663 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH 5/5 unit: Add l_pem_load_container_file tests
In reply to Andrew Zaborowski--- unit/test-pem.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+)
unit/test-pem.c +118 -0
@@ -337,6 +337,74 @@ static void test_encrypted_pkey(const void *data)l_key_free (pkey2); } +static bool test_cert_count (struct l_cert *cert,void *user_data) +{ +int *count = user_data; + + (*count)++; +return false; +} + +struct test_load_file_params { +const char *path; +bool expect_cert; +bool expect_certchain; +bool expect_privkey; +bool expect_encrypted; +}; + +#define TEST_LOAD_PARAMS (fn, cert, certchain, privkey, encrypted) \ + (&(struct test_load_file_params) { \ + CERTDIR fn, (cert), (certchain), (privkey), (encrypted) }) + +static void test_load_file (const void *data) +{ +const struct test_load_file_params *params = data; +struct l_certchain *certchain; +struct l_key *privkey; +bool encrypted; + +l_pem_load_container_file (params->path, false, &certchain, + &privkey, &encrypted); +assert (encrypted == params->expect_encrypted); + +if (encrypted) { +/* +* Depending on the format the certificates may be encrypted +* or unencrypted even when the private key was encrypted. +*/ +if (certchain) { +assert (params->expect_privkey); +l_certchain_free (certchain); + certchain =NULL ; + } + +assert (!privkey); + +l_pem_load_container_file (params->path,"abc" , &certchain, + &privkey, &encrypted); + } + +assert (!!certchain == params->expect_cert); +assert (!!privkey == params->expect_privkey); + +if (certchain) { +int count =0 ; + +l_certchain_walk_from_leaf (certchain, test_cert_count, &count); +assert (count == (params->expect_certchain ?3 :1 )); + +if (params->expect_certchain) +assert (l_certchain_verify (certchain,NULL ,NULL )); + } + +if (certchain) +l_certchain_free (certchain); + +if (privkey) +l_key_free (privkey); +} +int main (int argc,char *argv[]) {l_test_init (&argc, &argv);
@@ -409,6 +477,56 @@ int main(int argc, char *argv[]) CERTDIR "cert-client-key-pkcs1-aes256.pem"); } +l_test_add ("detect-format/PEM PKCS#1 unencrypted private key" , + test_load_file, +TEST_LOAD_PARAMS ("cert-client-key-pkcs1.pem" , + false, false, true, false)); +l_test_add ("detect-format/PEM PKCS#1 encrypted private key" , + test_load_file, +TEST_LOAD_PARAMS ("cert-client-key-pkcs1-des.pem" , + false, false, true, true)); +l_test_add ("detect-format/PEM PKCS#8 unencrypted private key" , + test_load_file, +TEST_LOAD_PARAMS ("cert-client-key-pkcs8.pem" , + false, false, true, false)); +l_test_add ("detect-format/PEM PKCS#8 encrypted private key" , + test_load_file, +TEST_LOAD_PARAMS ("cert-client-key-pkcs8-sha1-des.pem" , + false, false, true, true)); +l_test_add ("detect-format/PEM X.509 certificate" , + test_load_file, +TEST_LOAD_PARAMS ("cert-client.pem" , + true, false, false, false)); +l_test_add ("detect-format/DER X.509 certificate" , + test_load_file, +TEST_LOAD_PARAMS ("cert-client.crt" , + true, false, false, false)); +l_test_add ("detect-format/PEM combined" , + test_load_file, +TEST_LOAD_PARAMS ("cert-entity-combined.pem" , + true, true, true, true)); +l_test_add ("detect-format/DER PKCS#12 combined" , + test_load_file, +TEST_LOAD_PARAMS ("cert-entity-pkcs12-nomac.p12" , + true, false, true, true)); + +l_test_add ("pkcs#12/Combined RC2-based ciphers + SHA1" , + test_load_file, +TEST_LOAD_PARAMS ("cert-entity-pkcs12-rc2-sha1.p12" , + true, true, true, true)); +l_test_add ("pkcs#12/Combined DES-based ciphers + SHA256" , + test_load_file, +TEST_LOAD_PARAMS ("cert-entity-pkcs12-des-sha256.p12" , + true, true, true, true)); +l_test_add ("pkcs#12/Combined RC4-based ciphers + SHA384" , + test_load_file, +TEST_LOAD_PARAMS ("cert-entity-pkcs12-rc4-sha384.p12" , + true, true, true, true)); +l_test_add ("pkcs#12/Combined PKCS#5 ciphers + SHA512" , + test_load_file, +TEST_LOAD_PARAMS ("cert-entity-pkcs12-pkcs5-sha512.p12" , + true, true, true, true)); + done:return l_test_run (); }
Signature · 1 line
2.27.0
Hi Andrew,
Quoted 9 lines · Andrew Zaborowski
Add the key derivation algorithm used with PKCS#12 to
pkcs5.cso 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 | 121 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+)
Patch 1 & 2 applied, thanks.
Regards,
-Denis