Expand description
This crate provides functions and types for working with JSON Web Signatures (JWS).
§Details
The primary way to use this library is via the JwtSigner and
JwtVerifier traits, which provide functionality for signing JWTs and
verifying signed JWTs. A default openssl backed implementation of these
traits is available by using the openssl_impl::Es256Signer and
openssl_impl::Es256Verifier structs which implement JwtSigner and
JwtVerifier respectively. These implementations are available under the
default feature openssl which can be disabled and replaced by a custom
implementation.
A custom implementation must implement the Signer trait for signing
JWKs, SignatureVerifier trait for verifying signatures and optionally
the HasJwkKid trait if you need to access the JWK key id. The
JwtSigner and JwtVerifier traits are implemented automatically for
the custom implementation if the custom implementation implements the
Signer and SignatureVerifier traits respectively.
§Examples
§Sign and verify a JWT
use bh_jws_utils::{json_object, Es256Signer, Es256Verifier, JwtSigner, JwtVerifier};
// Construct a new signer
let signer = Es256Signer::generate("dummy-kid".to_string()).unwrap();
// Construct a JWT
let dummy_jwt = json_object!({
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
});
// Sign the JWT
let signed_jwt = signer.sign_jwt(dummy_jwt).unwrap();
// Get the public JWK for verification
let public_jwk = signer.public_jwk().unwrap();
// Verify the JWT
let token: serde_json::Value = Es256Verifier
.verify_jwt_signature(signed_jwt.as_str(), &public_jwk)
.unwrap();Re-exports§
pub use jwt;
Macros§
- json_
object - Helper macro with the same syntax as
serde_json::jsonspecialized for constructing JSON objects.
Structs§
- EcJwk
Private - Struct representing private JWK for keys which use elliptic curve algorithms
It contains public JWK and a private key part
dof JWK. - Es256
Signer Signerimplementation supporting theES256algorithm (ECDSA using the P-256 curve and the SHA-256 hash function).- Es256
Verifier SignatureVerifierimplementation supporting theES256algorithm (ECDSA using the P-256 curve and the SHA-256 hash function).- JwkSet
- Models JWK Set. A JSON object that represents a set of JWKs.
- Signer
With Chain Signerdecorator with an X.509 certificate chain associated with the key pair.
Enums§
- Crypto
Error - Cryptographic error
- Format
Error - Error in JWK format
- Signature
Error - Error in JWS signature
- Signing
Algorithm - Signature algorithms approved for use in the context of EUDI.
Constants§
- SIGNING_
ALG_ ES256 - JWS
"alg"header parameter value for digital signature algorithm ECDSA using P-256 and SHA-256, as specified in RFC7518. - SIGNING_
ALG_ ES384 - JWS
"alg"header parameter value for digital signature algorithm ECDSA using P-384 and SHA-384, as specified in RFC7518. - SIGNING_
ALG_ ES512 - JWS
"alg"header parameter value for digital signature algorithm ECDSA using P-521 and SHA-512, as specified in RFC7518. - SIGNING_
ALG_ PS256 - JWS
"alg"header parameter value for digital signature algorithm RSASSA-PSS using SHA-256 and MGF1 with SHA-256, as specified in RFC7518. - SIGNING_
ALG_ PS384 - JWS
"alg"header parameter value for digital signature algorithm RSASSA-PSS using SHA-384 and MGF1 with SHA-384, as specified in RFC7518. - SIGNING_
ALG_ PS512 - JWS
"alg"header parameter value for digital signature algorithm RSASSA-PSS using SHA-512 and MGF1 with SHA-512, as specified in RFC7518.
Traits§
- HasJwk
Kid - Subtrait for
Signer-s which have an associated JWKkid(Key ID) parameter. This is used to set thekidheader parameter when signing a JWT. - HasX5
Chain - Subtrait for
Signer-s which have an associatedx5chain. - JwtSigner
- An external backend capable of signing JWTs.
- JwtVerifier
- An external backend capable of verifying the signatures of JWTs.
- Signature
Verifier - An external backend for signature verification, to be used for verifying JWS signatures.
- Signer
- An external signing backend, to be used for computing a JWS signature.
Functions§
- base64_
url_ encode - Returns the
base64url-encoded string of the giveninput. - construct_
jws_ payload - Create payload for a
JWS, given its header and claims. - ec_
public_ affine_ coords_ to_ jwk - Constructs the
JwkPublicfrom the coordinates of the public ECDSA key using P-256 curve. - openssl_
ec_ pub_ key_ to_ jwk - Construct a JWK JSON object for provided public key. Note: only ECDSA keys using P-256 curve are supported!
- public_
jwk_ from_ x5chain_ leaf - Retrieve public JWK from the provided x5chain certificate chain leaf.
Type Aliases§
- BoxError
- Type alias for a boxed error.
- Coordinate
- A 32-byte coordinate for the elliptic curve.
- Es256
Signer With Chain Signerimplementation supporting theES256algorithm (ECDSA using the P-256 curve and the SHA-256 hash function). This is a wrapper overEs256Signerthat adds support for producingX5Chain.- JwkPublic
- A JSON object meant to represent a public JWK.