Skip to content
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

Handle ASN.1 type detection errors #3855

Merged
merged 5 commits into from
Feb 28, 2023
Merged

Handle ASN.1 type detection errors #3855

merged 5 commits into from
Feb 28, 2023

Conversation

lrstewart
Copy link
Contributor

@lrstewart lrstewart commented Feb 28, 2023

Description of changes:

s2n-tls is rejecting a customer's private ECDSA key because it doesn't set the publicKey field of the ASN.1 structure, which is marked as OPTIONAL in both RFC5915 and SECG's SEC 1. This missing field causes d2i_AutoPrivateKey to incorrectly identify the type of the private key.

libcrypto also offers PEM_read_bio_PrivateKey, which is able to parse the customer's key. However, s2n-tls prefers its own strict PEM parsing.

So I see two options:

  1. Update s2n-tls's PEM parsing to extract the private key type. Fall back to using that type with d2i_PrivateKey if d2i_AutoPrivateKey fails.
  2. Fall back to PEM_read_bio_PrivateKey if d2i_AutoPrivateKey fails. This should be fine, since the input must have already passed s2n-tls's PEM parsing in order to fail d2i_AutoPrivateKey.

I initially went with the second option, and it worked. But it seemed like overkill, and I worried it might be more permissive than our current logic. So I switched to the first option, which also worked and seemed smaller / cleaner.

Call-outs:

  • You can see my implementation of the second option here, if you think that might be preferrable to this.

Testing:

I generated an EC key pair and then ripped the publicKey field out of the private key. The pem test fails without this fix, but passes with it.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@github-actions github-actions bot added the s2n-core team label Feb 28, 2023
@lrstewart lrstewart force-pushed the fix branch 3 times, most recently from 7ed39da to a75fa27 Compare February 28, 2023 07:58
@lrstewart lrstewart marked this pull request as ready for review February 28, 2023 17:56
@@ -129,14 +129,29 @@ int s2n_pkey_free(struct s2n_pkey *key)
return S2N_SUCCESS;
}

int s2n_asn1der_to_private_key(struct s2n_pkey *priv_key, struct s2n_blob *asn1der)
int s2n_asn1der_to_private_key(struct s2n_pkey *priv_key, struct s2n_blob *asn1der, int type_hint)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bummer that they didn't use an enum here...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I considered using an internal one, and that would have avoided the CBMC issues. But we'd just have to immediately convert back to the int to use the enum.

@@ -0,0 +1,13 @@
-----BEGIN CERTIFICATE-----
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking but it would be nice to have these cert generators scripted and reproducible. I started one in pems/sni but it would be good to add one to the root.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'd be pretty difficult for this one. I had to manually modify it to remove the last entry. If we scripted it, the script would need to parse the base64, parse the ASN.1, remove the last field of the ASN.1, update the overall length, and then re-encode in base64.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hopefully anyone in the future will find this comment if they need to reconstruct it, I guess 😄

@lrstewart lrstewart enabled auto-merge (squash) February 28, 2023 21:53
@lrstewart lrstewart merged commit 8062414 into aws:main Feb 28, 2023
@lrstewart lrstewart deleted the fix branch March 1, 2023 00:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants