Open rbuffat opened 2 years ago
I believe https://github.com/pyca/cryptography/pull/6910 should provide what you need for this to work. It will be in the next release, but if you have a chance to test it before release that would be helpful, thanks!
@reaperhulk Unfortunately not. I updated the script and output in the original post that wraps the ca certificate in a pkcs12.PKCS12Certificate before exporting.
When listing the certificates of the truststore with keytool -list -keystore cryptography.truststore.p12 -storepass 'a_very_secure_password'
for the truststore it still lists:
Your keystore contains 0 entries
For the keytool.truststore.p12 it lists:
Your keystore contains 1 entry
caroot, Apr 13, 2022, trustedCertEntry,
Certificate fingerprint (SHA-256): BF:E5:79:AB:98:AF:DA:BF:5B:A1:75:A7:27:2E:BE:BC:9E:16:8E:A7:DD:D3:10:3F:D2:41:07:AE:27:5A:2B:2A
when exporting it with cryptography. Maybe there is something wrong with how I serialize the certificate.
I can reproduce this, but I don't understand what structure keytool is actually creating here so it's unclear how to make this work as expected.
@reaperhulk Thanks for looking into it.
When querying the generated trust stores using openssl the following output is shown:
openssl pkcs12 -info -in keytool.truststore.p12:
MAC: sha1, Iteration 100000
MAC length: 20, salt length: 20
PKCS7 Encrypted data: pbeWithSHA1And40BitRC2-CBC, Iteration 50000
Certificate bag
Bag Attributes
friendlyName: CARoot
2.16.840.1.113894.746875.1.1: <Unsupported tag 6>
openssl pkcs12 -info -in cryptography.truststore.p12:
MAC: sha1, Iteration 1
MAC length: 20, salt length: 8
PKCS7 Encrypted data: pbeWithSHA1And3-KeyTripleDES-CBC, Iteration 20000
Certificate bag
Bag Attributes
friendlyName: CAroot
Looking for 2.16.840.1.113894.746875.1.1 I found the following stackoverflow issue: https://stackoverflow.com/questions/52524948/created-java-truststore-p12-using-only-openssl
Per: previous discussion, the PKCS12 truststore bag has to have the required attribute '2.16.840.1.113894.746875.1.1' in order for the JRE to use it as an accepted truststore.
If OpenSSL doesn't support the addition of that OID then we won't be able to add support for this right now unfortunately. At some point we may choose to implement PKCS12 structures in our own ASN.1 code (much as we did for x509), but that's quite a bit of non-trivial work.
The OpenSSL command utility seems not to export this functionality. I'm not sure yet for the openssl API, but it is the first time I'm looking at it.
PKCS12_create contains a call to PKCS12_add_localkeyid. But I'm not yet sure if this would be useful to create the required attribute.
https://github.com/openssl/openssl/blob/1c0eede9827b0962f1d752fa4ab5d436fa039da4/doc/man3/PKCS12_add_localkeyid.pod https://github.com/openssl/openssl/blob/1c0eede9827b0962f1d752fa4ab5d436fa039da4/crypto/pkcs12/p12_crt.c#L69
The OpenSSL API doesn't appear to allow adding it either.
@reaperhulk Much appreciated that you checked! Should we close this issue or leave it open until eventually OpenSSL API supports this?
Are you sure that the OpenSSL API does not support this? According to https://github.com/openssl/openssl/issues/6684#issuecomment-1209334748 it looks like it does.
Interesting, so we may be able to do this with OpenSSL 3. I'll reopen this and investigate a bit when I get a chance.
I try to implement the following keytool command with cryptography to create a truststore using a self created CA certificate:
keytool -keystore truststore.p12 -alias CARoot -import -file /path/to/ca_cert.pem -storepass the_password -noprompt -storetype PKCS12
However, when writing the CA certificate using
pkcs12.serialize_key_and_certificates
the resulting truststore is different and seems to be unusable.Code to reproduce:
Output:
System: Arch Linux, cryptography installed using pip, Version cryptography==36.0.2