Skip to content

Commit a3ae2d7

Browse files
committed
Fixed bug in openssl token signature verification
1 parent 35f321e commit a3ae2d7

File tree

3 files changed

+61
-14
lines changed

3 files changed

+61
-14
lines changed

src/error.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@ use crate::algorithm::AlgorithmType;
1111
#[derive(Debug)]
1212
pub enum Error {
1313
AlgorithmMismatch(AlgorithmType, AlgorithmType),
14+
Base64(DecodeError),
15+
Format,
16+
InvalidSignature,
17+
Json(JsonError),
18+
NoClaimsComponent,
19+
NoHeaderComponent,
1420
NoKeyId,
1521
NoKeyWithKeyId(String),
16-
NoHeaderComponent,
17-
NoClaimsComponent,
1822
NoSignatureComponent,
19-
TooManyComponents,
20-
Format,
21-
Base64(DecodeError),
22-
Json(JsonError),
23-
Utf8(FromUtf8Error),
2423
RustCryptoMac(MacError),
2524
RustCryptoMacKeyLength(InvalidKeyLength),
25+
TooManyComponents,
26+
Utf8(FromUtf8Error),
2627
#[cfg(feature = "openssl")]
2728
OpenSsl(openssl::error::ErrorStack),
2829
}
@@ -40,6 +41,7 @@ impl fmt::Display for Error {
4041
NoSignatureComponent => write!(f, "No signature component found in token string"),
4142
TooManyComponents => write!(f, "Too many components found in token string"),
4243
Format => write!(f, "Format"),
44+
InvalidSignature => write!(f, "Invalid signature"),
4345
Base64(ref x) => write!(f, "{}", x),
4446
Json(ref x) => write!(f, "{}", x),
4547
Utf8(ref x) => write!(f, "{}", x),

src/token/verified.rs

+46-7
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ impl<'a, H: JoseHeader, C> VerifyWithKey<Token<H, C, Verified>> for Token<H, C,
3636
signature_str,
3737
} = self.signature;
3838

39-
key.verify(header_str, claims_str, signature_str)?;
40-
41-
Ok(Token {
42-
header: self.header,
43-
claims: self.claims,
44-
signature: Verified,
45-
})
39+
if key.verify(header_str, claims_str, signature_str)? {
40+
Ok(Token {
41+
header: self.header,
42+
claims: self.claims,
43+
signature: Verified,
44+
})
45+
} else {
46+
Err(Error::InvalidSignature)
47+
}
4648
}
4749
}
4850

@@ -159,6 +161,43 @@ mod tests {
159161
name: String,
160162
}
161163

164+
#[test]
165+
#[cfg(feature = "openssl")]
166+
pub fn token_can_not_be_verified_with_a_wrong_key() -> Result<(), Error> {
167+
use crate::{token::signed::SignWithKey, AlgorithmType, Header, PKeyWithDigest, Token};
168+
use openssl::{hash::MessageDigest, pkey::PKey};
169+
170+
let private_pem = include_bytes!("../../test/rs256-private.pem");
171+
let public_pem = include_bytes!("../../test/rs256-public-2.pem");
172+
173+
let rs256_private_key = PKeyWithDigest {
174+
digest: MessageDigest::sha256(),
175+
key: PKey::private_key_from_pem(private_pem).unwrap(),
176+
};
177+
let rs256_public_key = PKeyWithDigest {
178+
digest: MessageDigest::sha256(),
179+
key: PKey::public_key_from_pem(public_pem).unwrap(),
180+
};
181+
182+
let header = Header {
183+
algorithm: AlgorithmType::Rs256,
184+
..Default::default()
185+
};
186+
let mut claims = BTreeMap::new();
187+
claims.insert("sub", "someone");
188+
189+
let signed_token = Token::new(header, claims).sign_with_key(&rs256_private_key)?;
190+
let token_str = signed_token.as_str();
191+
let unverified_token: Token<Header, BTreeMap<String, String>, _> =
192+
Token::parse_unverified(token_str)?;
193+
let verified_token_result = unverified_token.verify_with_key(&rs256_public_key);
194+
assert!(verified_token_result.is_err());
195+
match verified_token_result.err().unwrap() {
196+
Error::InvalidSignature => Ok(()),
197+
other => panic!("Wrong error type: {:?}", other),
198+
}
199+
}
200+
162201
#[test]
163202
pub fn component_errors() {
164203
let key: Hmac<Sha256> = Hmac::new_varkey(b"first").unwrap();

test/rs256-public-2.pem

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3rAEiYZsc3E8FUGiBCt8e4dkt
3+
qc77U8lnpAfi8DaGSoYu9jelD8/za7XIqFD99EgBA+DXbR01jzSU+ILsfG21jS2B
4+
pPE39zuJYj5X7sQvKg1wRGVS0pi+DVLNEJUhTP8B3fUf1TD72t4aYJh4t8ZrgD90
5+
9VQglfyJZ7wuuHYHqQIDAQAB
6+
-----END PUBLIC KEY-----

0 commit comments

Comments
 (0)