You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm having a hard time authenticating a token using a x5c. (MS OAuth/Azure)
Below is the code...
// Trying to isolate the problem by only checking the signature. let validation_config = jsonwebtoken::Validation{algorithms:vec![jsonwebtoken::Algorithm::RS256],leeway:0,validate_exp:false,validate_iat:false,validate_nbf:false,aud:None,iss:None,sub:None};let token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIn...";let x5c_cert = "MIIDBTCCAe2gAwIBAgIQKOfEJNDyDplBSXKYcM...";let raw_der = base64::decode_config(der, base64::STANDARD).unwrap();let d = jsonwebtoken::decode::<MsOAuthPayload>(&token,&raw_der,&validation_config);
The above always returns InvalidSignature.
RS265 is the correct algo.
The cert is correct. I tried it on jwt.io by adding a BEGIN/END cert and it validates fine.
I used ssl to convert the BEGIN/END pem to DER and the bytes match up from the base 64 decode.
I'm running into the same issue, too. My use case is the same as yours; I'm attempting to validate a JWT provided by Microsoft's authentication system.
I'm having the same issue with Auth0. Even with the correct certificate and a valid token (verified with jwt.io) jsonwebtoken reports InvalidSignature. (See #80 for more details.)
Side note: Can we change the name of this issue slightly? It doesn't really seem to be about x5c to der conversion at this point. I wasn't aware of it when I initially came across the same thing.
From #80 it does look like it is a common failure with x509 certificates.
Would anyone have some time to look into that? Can start with creating some test certificates and see what is happening.
The current situation with all various combinations of algorithms/key types is becoming unwieldy, hopefully after something like #76 it will be easier to use
@Keats I personally don't have time to look into it at the moment, but I'd be happy to help out/explain my setup/provide example test cases that are breaking for me for whoever would like to take a stab at it.
I have the same issue with EC certificate, created with openssl ecparam -name secp384r1 -genkey -noout -out p384-private-key.pem openssl ec -in p384-private-key.pem -pubout -out p384-public-key.pem
then converted to der format openssl pkcs8 -topk8 -inform PEM -outform DER -in p384-private-key.pem -out p384-private-key.der -nocrypt openssl ec -pubin -inform PEM -in p384-public-key.pem -outform DER -out p384-public-key.der
I can verify the token with jwt.io using the pem file, i'm doing something wrong converting the keys?
@gpit2286 I'm not quite sure this will close the issue; it isn't that the input has the ----BEGIN xxx----- tags, it's that the validation doesn't succeed when a certificate (ie: identity information + public key) is provided to validate against.
I have more information now, at least with the Azure use case.
The x5c field returned by the Azure keys endpoint does have both the certificate chain and public key. If the public key is extracted (ie: with openssl x509 -pubkey ...), verifying the JWT will still fail because the DER has an object ID field prepended to the public key. When the OID field and associated TLV data is stripped off, it works.
I guess the question now is, should this parsing of the key from the cert and cleaning it up for ring's consumption be done by jsonwebtoken or by client code? I could provide a PR to do this, though it may introduce another package dependency.
For what it's worth, I'm using this code to extract the DER-formatted public key from an Azure ADB2C certificate and it seems to be working. It is depending on der-parser 1.1.1, nom 4.3.2, and rusticata-macros 1.1.0
#[macro_use]externcrate nom;#[macro_use]externcrate rusticata_macros;use der_parser::*;use nom::{ErrorKind};/* * Test for a the sequence that contains the key (ie: modulus + exponent) * * TODO: Perhaps this should test the length of the modulus? */fnrsa_key_parser(seq:&[u8]) -> IResult<&[u8],DerObject>{parse_der_sequence_defined!(seq,
parse_der_integer,
parse_der_integer
)}/* * The DER-encoded public key is stored within a bitstring embedded either * in a certificate or an x.509 public key. * * To find the bitstring we need to sift through the DER field sequences * recursively. */fnrec_extract_key(sequence:&Vec<DerObject>) -> Option<Vec<u8>>{for o in sequence {match&o.content{
der_parser::DerObjectContent::BitString(_, bso) => {matchrsa_key_parser(bso.data){Ok(_) => {letmut key:Vec<u8> = Vec::new();
key.extend_from_slice(bso.data);returnSome(key)},Err(_) => (),};},
der_parser::DerObjectContent::Sequence(s) => {matchrec_extract_key(s){Some(k) => returnSome(k),None => ()}},
_ => (),}}returnNone}/* * Extract a DER-formatted key from either a certificate or an x.509 public key */fnextract_key(cert:&[u8]) -> Option<Vec<u8>>{let result = parse_der(cert);let parsed = match result {Ok(p) => p,
_ => returnNone};let(_, der_object) = parsed;match der_object.content{
der_parser::DerObjectContent::Sequence(s) => rec_extract_key(&s),
_ => None}}fncrack_token(jwks:JwksResponse,token:&Adb2cToken) -> Result<Claims,Box<Error>>{let header = decode_header(&token.access_token)?;let kid = match header.kid{Some(k) => k,None => returnErr("Access token header does not have kid field".into())};let key = match jwks.keys.iter().find(|&k| k.kid == kid){Some(k) => k,None => returnErr(format!("Unable to find key id {}", kid).into())};let validation = Validation::new(header.alg);for cert in&key.x5c{let cert_der = base64::decode_config(&cert, base64::STANDARD)?;let key_vec = matchextract_key(&cert_der){Some(k) => k,None => returnErr("Unable to extract key from provided certificate".into())};matchdecode::<Claims>(&token.access_token,&key_vec.as_slice(),&validation){Ok(c) => returnOk(c.claims),Err(e) => {eprintln!("Error decoding: {}", e);}}}Err("No signing key successfully validated JWT claims".into())}
Getting that added to ring is probably not going to happen so I think it would be to be in jsonwebtoken but need to consider API to match #76
I think the first step to decide that would be to check how the JWT libraries in Python/Go/JS/etc handle x509 certs from Amazon/Google/Azure/others as well as generate some dummy x509 for testing.
I'm not sure my problem is related to the one here, but for me just trying to decode an example from jwt.io fails with Invalid Signature. For example, this fails:
Thanks for the info!
I think it would be great for that to be in ring since it seems it fits better there than in webpki. Or it could be a stand-alone small crate but I am guessing it is common enough with RSA to warrant being in ring directly, at least for JWTs since it's almost always in .pem format.
I'm not very familiar with the process of decode jwts in general, and while I'm trying to glean as much understanding I can from the issues in this repo, I am having a hard time understanding the current state of jsonwebtoken and x509. Would anyone be able to clarify the current status of using Auth0 (with RS256/x509) and this lib? I'm hitting the same problems that @samuela has hit, but would rather not jump ship to a something like Node. Thank you!
Reacted by Sergio, Uwe Pachler, Vladyslav Batyrenko and Dmitry
That's the same issue, the underlying crypto library is expecting the key to be in a specific format which is not the commonly used one. @LeviSchuck poc is interesting and a PR with that would be useful, even if we end up switching to the built-in ring function later on (briansmith/ring#370 (comment))
No need to feature gate it. Make sure you do the PR on the v7 PR in a standalone file since it still requires thinking about the UX and I have no clue what the usage would be like.
Activity
maxburke commentedon Mar 8, 2019
I'm running into the same issue, too. My use case is the same as yours; I'm attempting to validate a JWT provided by Microsoft's authentication system.
Keats commentedon Mar 8, 2019
You can try to clone jsonwebtoken locally to see where it is failing:
InvalidSignaturewould be caused by any error in that fn https://github.com/Keats/jsonwebtoken/blob/master/src/crypto.rs#L100-L113A first thought on why it doesn't work could be that Microsoft maybe doesn't use URL safe base64?
maxburke commentedon Mar 8, 2019
I'm wondering, could it be because the x5c field has the x509 certificate chain and not just the public key?
gpit2286 commentedon Mar 8, 2019
samuela commentedon Mar 18, 2019
I'm having the same issue with Auth0. Even with the correct certificate and a valid token (verified with jwt.io) jsonwebtoken reports
InvalidSignature. (See #80 for more details.)samuela commentedon Mar 18, 2019
Side note: Can we change the name of this issue slightly? It doesn't really seem to be about x5c to der conversion at this point. I wasn't aware of it when I initially came across the same thing.
Keats commentedon Mar 18, 2019
From #80 it does look like it is a common failure with x509 certificates.
Would anyone have some time to look into that? Can start with creating some test certificates and see what is happening.
The current situation with all various combinations of algorithms/key types is becoming unwieldy, hopefully after something like #76 it will be easier to use
[-]x5c to der[/-][+]Decoding with x509 certs[/+]samuela commentedon Mar 18, 2019
@Keats I personally don't have time to look into it at the moment, but I'd be happy to help out/explain my setup/provide example test cases that are breaking for me for whoever would like to take a stab at it.
Guara92 commentedon Apr 9, 2019
I have the same issue with EC certificate, created with
openssl ecparam -name secp384r1 -genkey -noout -out p384-private-key.pemopenssl ec -in p384-private-key.pem -pubout -out p384-public-key.pemthen converted to der format
openssl pkcs8 -topk8 -inform PEM -outform DER -in p384-private-key.pem -out p384-private-key.der -nocryptopenssl ec -pubin -inform PEM -in p384-public-key.pem -outform DER -out p384-public-key.derI can verify the token with jwt.io using the pem file, i'm doing something wrong converting the keys?
maxburke commentedon May 9, 2019
@gpit2286 I'm not quite sure this will close the issue; it isn't that the input has the
----BEGIN xxx-----tags, it's that the validation doesn't succeed when a certificate (ie: identity information + public key) is provided to validate against.maxburke commentedon May 11, 2019
I have more information now, at least with the Azure use case.
The
x5cfield returned by the Azure keys endpoint does have both the certificate chain and public key. If the public key is extracted (ie: withopenssl x509 -pubkey ...), verifying the JWT will still fail because the DER has an object ID field prepended to the public key. When the OID field and associated TLV data is stripped off, it works.I guess the question now is, should this parsing of the key from the cert and cleaning it up for ring's consumption be done by jsonwebtoken or by client code? I could provide a PR to do this, though it may introduce another package dependency.
Thoughts? (@Keats ?)
maxburke commentedon May 14, 2019
For what it's worth, I'm using this code to extract the DER-formatted public key from an Azure ADB2C certificate and it seems to be working. It is depending on der-parser 1.1.1, nom 4.3.2, and rusticata-macros 1.1.0
Keats commentedon May 14, 2019
Hmm that is adding quite a few dependencies :/
Getting that added to
ringis probably not going to happen so I think it would be to be injsonwebtokenbut need to consider API to match #76I think the first step to decide that would be to check how the JWT libraries in Python/Go/JS/etc handle x509 certs from Amazon/Google/Azure/others as well as generate some dummy x509 for testing.
vmalloc commentedon Jul 4, 2019
I'm not sure my problem is related to the one here, but for me just trying to decode an example from jwt.io fails with
Invalid Signature. For example, this fails:Where both
tokenandpubkeyare a direct copy-paste from jwt.io (pubkeybeginning with-----BEGIN PUBLIC KEY-----...)Is this related to this issue? Am I doing something wrong?
Thanks in advance!
6 remaining items
briansmith commentedon Aug 12, 2019
This is the wrapper header
This is the algorithm identifier.
This is the part that ring needs.
Notice it is the same as this.
Keats commentedon Aug 12, 2019
Thanks for the info!
I think it would be great for that to be in
ringsince it seems it fits better there than in webpki. Or it could be a stand-alone small crate but I am guessing it is common enough with RSA to warrant being in ring directly, at least for JWTs since it's almost always in .pem format.LeviSchuck commentedon Aug 12, 2019
Here's a proof of concept of a RS256 JWT with only ring, pem, and base64 as dependencies.
a 2048 bit rsa PEM key is saved as
private.pemI do think it would be best if PEM support were in
ring, but it can be done in the immediate like the above.brochington commentedon Aug 31, 2019
I'm not very familiar with the process of decode jwts in general, and while I'm trying to glean as much understanding I can from the issues in this repo, I am having a hard time understanding the current state of
jsonwebtokenand x509. Would anyone be able to clarify the current status of using Auth0 (with RS256/x509) and this lib? I'm hitting the same problems that @samuela has hit, but would rather not jump ship to a something like Node. Thank you!upachler commentedon Oct 3, 2019
Looks like I'm having the same issue when attempting to validate JWTs issued by Keycloak. jsonwebtoken is unable to parse the public key....
Keats commentedon Oct 4, 2019
That's the same issue, the underlying crypto library is expecting the key to be in a specific format which is not the commonly used one.
@LeviSchuck poc is interesting and a PR with that would be useful, even if we end up switching to the built-in ring function later on (briansmith/ring#370 (comment))
LeviSchuck commentedon Oct 4, 2019
Sure, I can see if I can throw something together this weekend.
Should this be feature gated, so pem isn't drawn in as a dependency unless requested?
Keats commentedon Oct 4, 2019
No need to feature gate it. Make sure you do the PR on the v7 PR in a standalone file since it still requires thinking about the UX and I have no clue what the usage would be like.
Dowwie commentedon Oct 29, 2019
@LeviSchuck apologies if this has already been done but what is the status on the work for supporting PEM? this is blocking v7 release..
LeviSchuck commentedon Oct 29, 2019
Hey there, sorry I had not gotten to this as planned.
I should be able to get to this tonight.
LeviSchuck commentedon Oct 29, 2019
So do I make a PR and then @Keats merges it into #91 ?
EDIT: It looks like it would be merged onto
nextand then that'd be included on the 7.0.0 PRLeviSchuck commentedon Oct 30, 2019
There's absolutely no documentation on the functions yet. But I got tests that pass and I dabbled on the README a bit. LeviSchuck@755e4cb
I would appreciate some feedback. It took more time than I anticipated with the nuances of PKCS1 and PKCS8 support with RSA and EC.
Keats commentedon Oct 30, 2019
Can you do a PR on the 7.0 PR? That will be easier to give feedback
Keats commentedon Jan 29, 2020
x509 is not supported and the issue changed topic quite a bit.