-
-
Notifications
You must be signed in to change notification settings - Fork 36
Allow library users to bring their own crypto providers #268
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
Open
jcgruenhage
wants to merge
9
commits into
djc:main
Choose a base branch
from
jcgruenhage:crypto-providers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
59521d1
Require rustls CryptoProvider for instantiating with DefaultClient
jcgruenhage 7f872c3
Refactor Jwk in preparation of supporting more key types
jcgruenhage 367d86c
Replace deprecated Key::generate function with Key::generate_pkcs8
jcgruenhage 080ae6b
Make SigningAlgorithm non_exhaustive and extend docs
jcgruenhage ffac6f3
Allow library users to bring their own crypto providers
jcgruenhage 69fd8bb
Add examples/certgen to Cargo.toml
jcgruenhage 8d2b820
Extend SigningAlgorithm and JwkInner
jcgruenhage 9d0dfec
Support switching account keys from one provider to another
jcgruenhage b8299f2
Cargo: version 0.8.5 -> 0.9.0
jcgruenhage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -265,62 +265,88 @@ pub(crate) struct Header<'a> { | |
| #[derive(Debug, Serialize)] | ||
| pub(crate) enum KeyOrKeyId<'a> { | ||
| #[serde(rename = "jwk")] | ||
| Key(Jwk), | ||
| Key(Jwk<'a>), | ||
| #[serde(rename = "kid")] | ||
| KeyId(&'a str), | ||
| } | ||
|
|
||
| impl KeyOrKeyId<'_> { | ||
| pub(crate) fn from_key(key: &crypto::EcdsaKeyPair) -> KeyOrKeyId<'static> { | ||
| pub(crate) fn from_key(key: &crypto::EcdsaKeyPair) -> KeyOrKeyId<'_> { | ||
| KeyOrKeyId::Key(Jwk::new(key)) | ||
| } | ||
| } | ||
|
|
||
| /// A JSON Web Key (JWK) as used in JWS headers | ||
| /// | ||
| /// See [RFC 7517](https://www.rfc-editor.org/rfc/rfc7517) for more information. | ||
| #[derive(Debug, Serialize)] | ||
| pub(crate) struct Jwk { | ||
| pub(crate) struct Jwk<'a> { | ||
| /// The algorithm intended for use with this key | ||
| alg: SigningAlgorithm, | ||
| crv: &'static str, | ||
| kty: &'static str, | ||
| /// Key-type-specific parameters | ||
| #[serde(flatten)] | ||
| key: JwkThumbFields<'a>, | ||
| /// The intended use (`"sig"` for signing) | ||
| r#use: &'static str, | ||
| x: String, | ||
| y: String, | ||
| } | ||
|
|
||
| impl Jwk { | ||
| pub(crate) fn new(key: &crypto::EcdsaKeyPair) -> Self { | ||
| impl Jwk<'_> { | ||
| pub(crate) fn new(key: &crypto::EcdsaKeyPair) -> Jwk<'_> { | ||
|
||
| let (x, y) = key.public_key().as_ref()[1..].split_at(32); | ||
| Self { | ||
| Jwk { | ||
| alg: SigningAlgorithm::Es256, | ||
| crv: "P-256", | ||
| kty: "EC", | ||
| key: JwkThumbFields::Ec { | ||
| crv: "P-256", | ||
| kty: "EC", | ||
| x, | ||
| y, | ||
| }, | ||
| r#use: "sig", | ||
| x: BASE64_URL_SAFE_NO_PAD.encode(x), | ||
| y: BASE64_URL_SAFE_NO_PAD.encode(y), | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn thumb_sha256( | ||
| key: &crypto::EcdsaKeyPair, | ||
| ) -> Result<crypto::Digest, serde_json::Error> { | ||
| let jwk = Self::new(key); | ||
| /// Compute the [RFC 7638](https://www.rfc-editor.org/rfc/rfc7638) JWK thumbprint. | ||
| /// | ||
| /// Serializes only the required key-type-specific members in lexicographic order, | ||
| /// then hashes with SHA-256. | ||
| pub(crate) fn thumb_sha256(&self) -> Result<crypto::Digest, serde_json::Error> { | ||
| Ok(crypto::digest( | ||
| &crypto::SHA256, | ||
| &serde_json::to_vec(&JwkThumb { | ||
| crv: jwk.crv, | ||
| kty: jwk.kty, | ||
| x: &jwk.x, | ||
| y: &jwk.y, | ||
| })?, | ||
| &serde_json::to_vec(&self.key)?, | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| /// Key-type-specific JWK parameters | ||
| /// | ||
| /// Each variant's fields are declared in lexicographic order for correct | ||
| /// [RFC 7638](https://www.rfc-editor.org/rfc/rfc7638) thumbprint computation. | ||
| #[derive(Debug, Serialize)] | ||
| struct JwkThumb<'a> { | ||
| crv: &'a str, | ||
| kty: &'a str, | ||
| x: &'a str, | ||
| y: &'a str, | ||
| #[serde(untagged)] | ||
jcgruenhage marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #[non_exhaustive] | ||
| pub(crate) enum JwkThumbFields<'a> { | ||
| /// Elliptic Curve key (P-256, P-384, etc.) | ||
| Ec { | ||
| /// The curve name (e.g., `"P-256"`) | ||
| crv: &'static str, | ||
| /// Key type, must be `"EC"` | ||
| kty: &'static str, | ||
| /// The x coordinate (serialized as base64url) | ||
| #[serde(serialize_with = "base64url::serialize")] | ||
| x: &'a [u8], | ||
| /// The y coordinate (serialized as base64url) | ||
| #[serde(serialize_with = "base64url::serialize")] | ||
| y: &'a [u8], | ||
| }, | ||
| } | ||
|
|
||
| mod base64url { | ||
| use base64::prelude::{BASE64_URL_SAFE_NO_PAD, Engine}; | ||
| use serde::Serializer; | ||
|
|
||
| pub(crate) fn serialize<S: Serializer>(data: &&[u8], serializer: S) -> Result<S::Ok, S::Error> { | ||
| serializer.serialize_str(&BASE64_URL_SAFE_NO_PAD.encode(*data)) | ||
| } | ||
| } | ||
|
|
||
| /// An ACME challenge as described in RFC 8555 (section 7.1.5) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.