Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ext/crypto): implement importKey (jwk) for RSA-PSS , RSA-OAEP and RSA-PKCS1-v1_5 #12022

Closed
wants to merge 51 commits into from
Closed
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
7486443
importKey RSA-PSS with jwk
cryptographix Sep 13, 2021
efb343a
importKey RSA-PSS with pkcs8 and spki
cryptographix Sep 13, 2021
f1ad887
fmt
cryptographix Sep 13, 2021
1f7d5bf
remove pkcs8 and spki, minor corrections as per @littledivy
cryptographix Sep 13, 2021
7ff8a6a
lint
cryptographix Sep 13, 2021
1933d10
Merge branch 'main' into crypto-rsa-pss-import
cryptographix Sep 13, 2021
38b4519
merge main, import jwk private RSA-PSS as pkcs1/raw
cryptographix Sep 13, 2021
6f4daf8
Merge branch 'denoland:main' into crypto-rsa-pss-import
cryptographix Sep 13, 2021
0bcf563
Merge branch 'denoland:main' into crypto-rsa-pss-import
cryptographix Sep 13, 2021
e25c5cb
Added key_type to KeyData, set correctly on generate and import
cryptographix Sep 13, 2021
f617e42
Merge branch 'denoland:main' into crypto-rsa-pss-import
cryptographix Sep 13, 2021
4427912
fix lint tests
cryptographix Sep 14, 2021
7242a53
Merge branch 'main' into crypto-rsa-pss-import
cryptographix Sep 14, 2021
3d7eaed
Import jwk for RSA-PKCS1_v15 and OAEP
cryptographix Sep 15, 2021
9e664f8
improve tests
cryptographix Sep 15, 2021
e9d2e3c
fix wpt/expections
cryptographix Sep 15, 2021
bd51c61
Merge branch 'main' into crypto-rsa-pss-import
cryptographix Sep 16, 2021
1a64887
Apply suggestions from code review
cryptographix Sep 16, 2021
f1dcd17
Merge branch 'denoland:main' into crypto-rsa-pss-import
cryptographix Sep 16, 2021
2a095fe
Merge branch 'crypto-rsa-pss-import' of https://github.com/SeanWykes/…
cryptographix Sep 16, 2021
68822c0
Code review suggestions
cryptographix Sep 16, 2021
a26d4f9
upd wpt/expectation.json
cryptographix Sep 16, 2021
fb6028e
merge upstream
cryptographix Nov 23, 2021
f9e5844
Merge branch 'denoland-main' into crypto-rsa-pss-import
cryptographix Nov 23, 2021
4e3de73
fmt
cryptographix Nov 23, 2021
3ef027a
Merge branch 'denoland:main' into crypto-rsa-pss-import
cryptographix Nov 23, 2021
315d31a
Added enum as param to op_crypto_import_key, prep for tfr RSA js to rust
cryptographix Nov 24, 2021
5ef5ccc
Rust import OAEP working - needs cleanup
cryptographix Nov 25, 2021
5b72c2d
Import RSA JWK and HMAC via rust .. ok
cryptographix Nov 25, 2021
45ec05b
Merge branch 'main' into crypto-rsa-pss-import
cryptographix Nov 25, 2021
ffaee25
wpt expectation, fix typo OAEP/OEAP, lint
cryptographix Nov 25, 2021
31119c4
lint
cryptographix Nov 25, 2021
95d0c34
validate jwk RSA component presence
cryptographix Nov 25, 2021
0aee06f
Merge branch 'denoland:main' into crypto-rsa-pss-import
cryptographix Nov 26, 2021
e6cf5a2
RSA jwk export OK
cryptographix Nov 26, 2021
f57fc7e
JWK import/export for P-256. First go
cryptographix Nov 29, 2021
3dbd5e0
Merge branch 'main' into crypto-rsa-pss-import
cryptographix Dec 1, 2021
d3eac86
fix merge
cryptographix Dec 1, 2021
69ce309
fixes
cryptographix Dec 1, 2021
1d61adc
Merge branch 'denoland:main' into crypto-rsa-pss-import
cryptographix Dec 2, 2021
609fbc0
cleanup
cryptographix Dec 3, 2021
97d5a4a
disable not-yet-implemented formats for export ECDSA/ECDH
cryptographix Dec 3, 2021
ce92fe2
refac
cryptographix Dec 3, 2021
1a58e19
refac EC + fix EC jwk import keyUsages checks
cryptographix Dec 3, 2021
47b4905
Merge branch 'main' into crypto-rsa-pss-import
cryptographix Dec 7, 2021
4ecaebb
Merge branch 'denoland:main' into crypto-rsa-pss-import
cryptographix Dec 7, 2021
adaf97b
disable EC JWK due to p256@0.10.0-pre1/elliptic-curve@0.11.5 incompat…
cryptographix Dec 7, 2021
f74f279
fix disable ec, fmt,wpt
cryptographix Dec 7, 2021
fc44ac0
moved EC import/export code to new PR #13013
cryptographix Dec 7, 2021
bc48020
revert changes to Cargo.lock
cryptographix Dec 7, 2021
471fed5
upd Cargo.lock
cryptographix Dec 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 200 additions & 0 deletions cli/tests/unit/webcrypto_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,203 @@ unitTest(async function testHkdfDeriveBits() {
);
assertEquals(result.byteLength, 128 / 8);
});

const jwtRSAKeys = {
"2048": {
size: 2048,
publicJWK: {
kty: "RSA",
// unpadded base64 for rawKey.
n: "09eVwAhT9SPBxdEN-74BBeEANGaVGwqH-YglIc4VV7jfhR2by5ivzVq8NCeQ1_ACDIlTDY8CTMQ5E1c1SEXmo_T7q84XUGXf8U9mx6uRg46sV7fF-hkwJR80BFVsvWxp4ahPlVJYj__94ft7rIVvchb5tyalOjrYFCJoFnSgq-i3ZjU06csI9XnO5klINucD_Qq0vUhO23_Add2HSYoRjab8YiJJR_Eths7Pq6HHd2RSXmwYp5foRnwe0_U75XmesHWDJlJUHYbwCZo0kP9G8g4QbucwU-MSNBkZOO2x2ZtZNexpHd0ThkATbnNlpVG_z2AGNORp_Ve3rlXwrGIXXw",
e: "AQAB",
},
privateJWK: {
kty: "RSA",
// unpadded base64 for rawKey.
n: "09eVwAhT9SPBxdEN-74BBeEANGaVGwqH-YglIc4VV7jfhR2by5ivzVq8NCeQ1_ACDIlTDY8CTMQ5E1c1SEXmo_T7q84XUGXf8U9mx6uRg46sV7fF-hkwJR80BFVsvWxp4ahPlVJYj__94ft7rIVvchb5tyalOjrYFCJoFnSgq-i3ZjU06csI9XnO5klINucD_Qq0vUhO23_Add2HSYoRjab8YiJJR_Eths7Pq6H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SWheARwVsSQzAE0vGvw3zHIPP_lTtChBlCTPctQcURjw4dXcnK1oQ-IT321FNOW3EO-YTsyGcypJqJujlZrLbxYjOjQE8",
qi:
"OQXzi9gypDnpdHatIi0FaUGP8LSzfVH0AUugURJXs4BTJpvA9y4hcpBQLrcl7H_vq6kbGmvC49V-9I5HNVX_AuxGIXKuLZr5WOxPq8gLTqHV7X5ZJDtWIP_nq2NNgCQQyNNRrxebiWlwGK9GnX_unewT6jopI_oFhwp0Q13rBR0",
},
},
};

unitTest(async function testImportRsaJwk() {
const subtle = window.crypto.subtle;
assert(subtle);

for (
const [_key, jwkData] of Object.entries(jwtRSAKeys)
) {
const { size, publicJWK, privateJWK } = jwkData;
if (size != 2048) {
continue;
}

// 1. Test import PSS
for (const hash of ["SHA-1", "SHA-256", "SHA-384", "SHA-512"]) {
const hashMapPSS: Record<string, string> = {
"SHA-1": "PS1",
"SHA-256": "PS256",
"SHA-384": "PS384",
"SHA-512": "PS512",
};

const privateKeyPSS = await crypto.subtle.importKey(
"jwk",
{
alg: hashMapPSS[hash],
...privateJWK,
ext: true,
"key_ops": ["sign"],
},
{ name: "RSA-PSS", hash },
true,
["sign"],
);

const publicKeyPSS = await crypto.subtle.importKey(
"jwk",
{
alg: hashMapPSS[hash],
...publicJWK,
ext: true,
"key_ops": ["verify"],
},
{ name: "RSA-PSS", hash },
true,
["verify"],
);

const signaturePSS = await crypto.subtle.sign(
{ name: "RSA-PSS", saltLength: 32 },
privateKeyPSS,
new Uint8Array([1, 2, 3, 4]),
);

const verifyPSS = await crypto.subtle.verify(
{ name: "RSA-PSS", saltLength: 32 },
publicKeyPSS,
signaturePSS,
new Uint8Array([1, 2, 3, 4]),
);
assert(verifyPSS);
}

// 2. Test import PKCS1
for (const hash of ["SHA-1", "SHA-256", "SHA-384", "SHA-512"]) {
const hashMapPKCS1: Record<string, string> = {
"SHA-1": "RS1",
"SHA-256": "RS256",
"SHA-384": "RS384",
"SHA-512": "RS512",
};

const privateKeyPKCS1 = await crypto.subtle.importKey(
"jwk",
{
alg: hashMapPKCS1[hash],
...privateJWK,
ext: true,
"key_ops": ["sign"],
},
{ name: "RSASSA-PKCS1-v1_5", hash },
true,
["sign"],
);

const publicKeyPKCS1 = await crypto.subtle.importKey(
"jwk",
{
alg: hashMapPKCS1[hash],
...publicJWK,
ext: true,
"key_ops": ["verify"],
},
{ name: "RSASSA-PKCS1-v1_5", hash },
true,
["verify"],
);

const signaturePKCS1 = await crypto.subtle.sign(
{ name: "RSASSA-PKCS1-v1_5", saltLength: 32 },
privateKeyPKCS1,
new Uint8Array([1, 2, 3, 4]),
);

const verifyPKCS1 = await crypto.subtle.verify(
{ name: "RSASSA-PKCS1-v1_5", saltLength: 32 },
publicKeyPKCS1,
signaturePKCS1,
new Uint8Array([1, 2, 3, 4]),
);
assert(verifyPKCS1);
}

// 3. Test import OAEP
for (
const { hash, plainText } of hashPlainTextVector
) {
const encryptAlgorithm = { name: "RSA-OAEP" };

const hashMapOAEP: Record<string, string> = {
"SHA-1": "RSA-OAEP",
"SHA-256": "RSA-OAEP-256",
"SHA-384": "RSA-OAEP-384",
"SHA-512": "RSA-OAEP-512",
};

const privateKeyOAEP = await crypto.subtle.importKey(
"jwk",
{
alg: hashMapOAEP[hash],
...privateJWK,
ext: true,
"key_ops": ["decrypt"],
},
{ name: "RSA-OAEP", hash },
true,
["decrypt"],
);

const publicKeyOAEP = await crypto.subtle.importKey(
"jwk",
{
alg: hashMapOAEP[hash],
...publicJWK,
ext: true,
"key_ops": ["encrypt"],
},
{ name: "RSA-OAEP", hash },
true,
["encrypt"],
);
const cipherText = await subtle.encrypt(
encryptAlgorithm,
publicKeyOAEP,
plainText,
);

assert(cipherText);
assert(cipherText.byteLength > 0);
assertEquals(cipherText.byteLength * 8, 2048);
assert(cipherText instanceof ArrayBuffer);

const decrypted = await subtle.decrypt(
encryptAlgorithm,
privateKeyOAEP,
cipherText,
);
assert(decrypted);
assert(decrypted instanceof ArrayBuffer);
assertEquals(new Uint8Array(decrypted), plainText);
}
}
});
Loading