-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Unable to create pkcs12 truststore using pkcs12.serialize_key_and_certificates #7065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I believe #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
For the keytool.truststore.p12 it lists:
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:
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
|
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 |
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 openssl/openssl#6684 (comment) 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. |
Hopefully I'm understanding the issue here correctly, but it looks like OpenSSL now directly supports creating a Java compatible truststore. In the CLI it's a new
Looking behind the scenes this appears to be implemented with a new |
Yes, that’s how they implemented it. However, we are increasingly taking over parsing rather than using OpenSSL (for both safety and performance reasons) so our solution will be implemented within this project. |
We've already moved pkcs12 generation to Rust, so this is at a point
where it just needs a design and PR.
…On Thu, Jan 30, 2025 at 5:16 PM Paul Kehrer ***@***.***> wrote:
Yes, that’s how they implemented it. However, we are increasingly taking over parsing rather than using OpenSSL (for both safety and performance reasons) so our solution will be implemented within this project.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
--
All that is necessary for evil to succeed is for good people to do nothing.
|
Understood. I'd love to help contribute to this, if possible- though I'm no cryptography expert. Reading a bit further onto the subject, it seems that to be considered a valid truststore the pkcs12 structure must contain exclusively certificates with the attribute OID of Not sure if there's a specific implementation in mind, but I've been toying around with some ideas. Seems to me that given the constraints and unique attributes of truststores, you'd want them to have their own serialization function on the Python side. i.e. # Existing serialize function
def serialize_key_and_certificates(
name: bytes | None,
key: PKCS12PrivateKeyTypes | None,
cert: x509.Certificate | None,
cas: Iterable[_PKCS12CATypes] | None,
encryption_algorithm: serialization.KeySerializationEncryption,
) -> bytes:
# ...
def serialize_java_truststore(
certs: Iterable[PKCS12Certificate],
encryption_algorithm: serialization.KeySerializationEncryption,
) -> bytes:
# ... This way the user cannot generate an invalid truststore by including keys or having to manually track/adjust safebag attributes. On the Rust side, I would think Assuming that approach sounds reasonable, I can put together an implementation (as time permits). |
@crbednarz That sounds basically reasonable to me! Thanks for taking a look at this! We'd be thrilled to review a PR with that. |
Alright, PR submitted. #12393 |
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
The text was updated successfully, but these errors were encountered: