66//! ```
77//! use sourceorigin_crypto::{Compact, KeyDefect, Rs256VerifyingKey, VerifyError};
88//!
9- //! let token = Compact::split("eyJhbGciOiJSUzI1NiJ9.e30.c2lnbmF0dXJl")?;
10- //! assert_eq!(token.header(), br#"{"alg":"RS256"}"#);
9+ //! let split = Compact::split("eyJhbGciOiJSUzI1NiJ9.e30.c2lnbmF0dXJl")?;
10+ //! assert_eq!(split.header(), br#"{"alg":"RS256"}"#);
11+ //! // The header passed the caller's checks, so the rest is decoded.
12+ //! let token = split.decode()?;
1113//! // A 1024-bit modulus verifies nothing: the key is at fault, not the token.
1214//! let weak = Rs256VerifyingKey::new(&[0xc5; 128], &[1, 0, 1]);
1315//! assert!(matches!(weak.defect(), Some(KeyDefect::ModulusSize(1024))));
@@ -26,7 +28,7 @@ use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
2628use thiserror:: Error ;
2729
2830/// Longest compact token read, in bytes: decoding is bounded before it starts.
29- pub const MAX_TOKEN_BYTES : usize = 16 * 1024 ;
31+ const MAX_TOKEN_BYTES : usize = 16 * 1024 ;
3032/// Shortest RSA modulus a key may have, in bits (RFC 7518 §3.3).
3133const MIN_MODULUS_BITS : usize = 2048 ;
3234/// Longest RSA modulus a key may have, in bits, as aws-lc verifies.
@@ -40,6 +42,49 @@ const EXPONENT: [u8; 3] = [1, 0, 1];
4042#[ error( "{0}" ) ]
4143pub struct Malformed ( pub & ' static str ) ;
4244
45+ /// A compact JWS split into its three segments, with only its header decoded.
46+ /// The caller checks the header first, then decodes the rest, so a header that
47+ /// fails its checks is reported before a malformed payload or signature, in the
48+ /// order every SDK reports failures.
49+ pub struct Split < ' a > {
50+ /// The decoded protected header, JSON as sent.
51+ header : Vec < u8 > ,
52+ /// `header.payload`, the bytes the signature covers.
53+ signing_input : & ' a str ,
54+ /// The base64url payload.
55+ payload : & ' a str ,
56+ /// The base64url signature.
57+ signature : & ' a str ,
58+ }
59+
60+ impl < ' a > Split < ' a > {
61+ /// The decoded protected header, for the caller to parse under its policy.
62+ #[ must_use]
63+ pub fn header ( & self ) -> & [ u8 ] {
64+ & self . header
65+ }
66+
67+ /// Decodes the payload and signature.
68+ ///
69+ /// # Errors
70+ /// [`Malformed`] for a segment that is not unpadded base64url.
71+ pub fn decode ( self ) -> Result < Compact < ' a > , Malformed > {
72+ Ok ( Compact {
73+ header : self . header ,
74+ signing_input : self . signing_input ,
75+ payload : decode ( self . payload ) ?,
76+ signature : decode ( self . signature ) ?,
77+ } )
78+ }
79+ }
80+
81+ /// A segment's unpadded base64url, decoded.
82+ fn decode ( segment : & str ) -> Result < Vec < u8 > , Malformed > {
83+ URL_SAFE_NO_PAD
84+ . decode ( segment)
85+ . map_err ( |_| Malformed ( "a segment is not base64url" ) )
86+ }
87+
4388/// A compact JWS (RFC 7515 §7.1) split into its decoded parts. Nothing is
4489/// verified: read the header, verify the signature, and only then the payload.
4590#[ derive( Clone ) ]
@@ -55,13 +100,13 @@ pub struct Compact<'a> {
55100}
56101
57102impl < ' a > Compact < ' a > {
58- /// Splits a token of at most [`MAX_TOKEN_BYTES`] into exactly three base64url
59- /// segments and decodes them .
103+ /// Splits a token of at most 16 KiB into exactly three segments
104+ /// and decodes its header .
60105 ///
61106 /// # Errors
62- /// [`Malformed`] for a longer token, another number of segments, or a segment
107+ /// [`Malformed`] for a longer token, another number of segments, or a header
63108 /// that is not unpadded base64url.
64- pub fn split ( token : & ' a str ) -> Result < Self , Malformed > {
109+ pub fn split ( token : & ' a str ) -> Result < Split < ' a > , Malformed > {
65110 if token. len ( ) > MAX_TOKEN_BYTES {
66111 return Err ( Malformed ( "the token exceeds 16 KiB" ) ) ;
67112 }
@@ -74,16 +119,11 @@ impl<'a> Compact<'a> {
74119 ) else {
75120 return Err ( Malformed ( "the token is not three segments" ) ) ;
76121 } ;
77- let decode = |segment : & str | {
78- URL_SAFE_NO_PAD
79- . decode ( segment)
80- . map_err ( |_| Malformed ( "a segment is not base64url" ) )
81- } ;
82- Ok ( Self {
122+ Ok ( Split {
83123 header : decode ( header) ?,
84124 signing_input : & token[ ..header. len ( ) + 1 + payload. len ( ) ] ,
85- payload : decode ( payload ) ? ,
86- signature : decode ( signature ) ? ,
125+ payload,
126+ signature,
87127 } )
88128 }
89129
0 commit comments