-
-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #160 from mark-adams/add-test-vectors
Added test vectors from the IETF JOSE Cookbook for HMAC, RSA, and EC.
- Loading branch information
Showing
9 changed files
with
241 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import json | ||
import os | ||
|
||
from jwt.utils import base64url_decode | ||
|
||
from tests.utils import ensure_bytes, int_from_bytes | ||
|
||
BASE_PATH = os.path.dirname(os.path.abspath(__file__)) | ||
|
||
|
||
def decode_value(val): | ||
decoded = base64url_decode(ensure_bytes(val)) | ||
return int_from_bytes(decoded, 'big') | ||
|
||
|
||
def load_hmac_key(): | ||
with open(os.path.join(BASE_PATH, 'jwk_hmac.json'), 'r') as infile: | ||
keyobj = json.load(infile) | ||
|
||
return base64url_decode(ensure_bytes(keyobj['k'])) | ||
|
||
try: | ||
from cryptography.hazmat.primitives.asymmetric import rsa | ||
from cryptography.hazmat.primitives.asymmetric import ec | ||
from cryptography.hazmat.backends import default_backend | ||
|
||
has_crypto = True | ||
except ImportError: | ||
has_crypto = False | ||
|
||
if has_crypto: | ||
def load_rsa_key(): | ||
with open(os.path.join(BASE_PATH, 'jwk_rsa_key.json'), 'r') as infile: | ||
keyobj = json.load(infile) | ||
|
||
return rsa.RSAPrivateNumbers( | ||
p=decode_value(keyobj['p']), | ||
q=decode_value(keyobj['q']), | ||
d=decode_value(keyobj['d']), | ||
dmp1=decode_value(keyobj['dp']), | ||
dmq1=decode_value(keyobj['dq']), | ||
iqmp=decode_value(keyobj['qi']), | ||
public_numbers=load_rsa_pub_key().public_numbers() | ||
).private_key(default_backend()) | ||
|
||
def load_rsa_pub_key(): | ||
with open(os.path.join(BASE_PATH, 'jwk_rsa_pub.json'), 'r') as infile: | ||
keyobj = json.load(infile) | ||
|
||
return rsa.RSAPublicNumbers( | ||
n=decode_value(keyobj['n']), | ||
e=decode_value(keyobj['e']) | ||
).public_key(default_backend()) | ||
|
||
def load_ec_key(): | ||
with open(os.path.join(BASE_PATH, 'jwk_ec_key.json'), 'r') as infile: | ||
keyobj = json.load(infile) | ||
|
||
return ec.EllipticCurvePrivateNumbers( | ||
private_value=decode_value(keyobj['d']), | ||
public_numbers=load_ec_pub_key().public_numbers() | ||
) | ||
|
||
def load_ec_pub_key(): | ||
with open(os.path.join(BASE_PATH, 'jwk_ec_pub.json'), 'r') as infile: | ||
keyobj = json.load(infile) | ||
|
||
return ec.EllipticCurvePublicNumbers( | ||
x=decode_value(keyobj['x']), | ||
y=decode_value(keyobj['y']), | ||
curve=ec.SECP521R1() | ||
).public_key(default_backend()) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"kty": "EC", | ||
"kid": "[email protected]", | ||
"use": "sig", | ||
"crv": "P-521", | ||
"x": "AHKZLLOsCOzz5cY97ewNUajB957y-C-U88c3v13nmGZx6sYl_oJXu9A5RkTKqjqvjyekWF-7ytDyRXYgCF5cj0Kt", | ||
"y": "AdymlHvOiLxXkEhayXQnNCvDX4h9htZaCJN34kfmC6pV5OhQHiraVySsUdaQkAgDPrwQrJmbnX9cwlGfP-HqHZR1", | ||
"d": "AAhRON2r9cqXX1hg-RoI6R1tX5p2rUAYdmpHZoC1XNM56KtscrX6zbKipQrCW9CGZH3T4ubpnoTKLDYJ_fF3_rJt" | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"kty": "EC", | ||
"kid": "[email protected]", | ||
"use": "sig", | ||
"crv": "P-521", | ||
"x": "AHKZLLOsCOzz5cY97ewNUajB957y-C-U88c3v13nmGZx6sYl_oJXu9A5RkTKqjqvjyekWF-7ytDyRXYgCF5cj0Kt", | ||
"y": "AdymlHvOiLxXkEhayXQnNCvDX4h9htZaCJN34kfmC6pV5OhQHiraVySsUdaQkAgDPrwQrJmbnX9cwlGfP-HqHZR1" | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"kty": "oct", | ||
"kid": "018c0ae5-4d9b-471b-bfd6-eef314bc7037", | ||
"use": "sig", | ||
"alg": "HS256", | ||
"k": "hJtXIZ2uSN5kbQfbtTNWbpdmhkV8FJG-Onbc6mxCcYg" | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"kty": "RSA", | ||
"kid": "[email protected]", | ||
"use": "sig", | ||
"n": "n4EPtAOCc9AlkeQHPzHStgAbgs7bTZLwUBZdR8_KuKPEHLd4rHVTeT-O-XV2jRojdNhxJWTDvNd7nqQ0VEiZQHz_AJmSCpMaJMRBSFKrKb2wqVwGU_NsYOYL-QtiWN2lbzcEe6XC0dApr5ydQLrHqkHHig3RBordaZ6Aj-oBHqFEHYpPe7Tpe-OfVfHd1E6cS6M1FZcD1NNLYD5lFHpPI9bTwJlsde3uhGqC0ZCuEHg8lhzwOHrtIQbS0FVbb9k3-tVTU4fg_3L_vniUFAKwuCLqKnS2BYwdq_mzSnbLY7h_qixoR7jig3__kRhuaxwUkRz5iaiQkqgc5gHdrNP5zw", | ||
"e": "AQAB", | ||
"d": "bWU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3PiqvXQN0zwMeE-sBvZgi289XP9XCQF3VWqPzMKnIgQp7_Tugo6-NZBKCQsMf3HaEGBjTVJs_jcK8-TRXvaKe-7ZMaQj8VfBdYkssbu0NKDDhjJ-GtiseaDVWt7dcH0cfwxgFUHpQh7FoCrjFJ6h6ZEpMF6xmujs4qMpPz8aaI4" | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"kty": "RSA", | ||
"kid": "[email protected]", | ||
"use": "sig", | ||
"n": "n4EPtAOCc9AlkeQHPzHStgAbgs7bTZLwUBZdR8_KuKPEHLd4rHVTeT-O-XV2jRojdNhxJWTDvNd7nqQ0VEiZQHz_AJmSCpMaJMRBSFKrKb2wqVwGU_NsYOYL-QtiWN2lbzcEe6XC0dApr5ydQLrHqkHHig3RBordaZ6Aj-oBHqFEHYpPe7Tpe-OfVfHd1E6cS6M1FZcD1NNLYD5lFHpPI9bTwJlsde3uhGqC0ZCuEHg8lhzwOHrtIQbS0FVbb9k3-tVTU4fg_3L_vniUFAKwuCLqKnS2BYwdq_mzSnbLY7h_qixoR7jig3__kRhuaxwUkRz5iaiQkqgc5gHdrNP5zw", | ||
"e": "AQAB" | ||
} |
This file contains 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 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 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