|
| 1 | +import { expect } from 'chai'; |
| 2 | +import type { CryptoKey, CryptoKeyPair, JWK } from 'react-native-quick-crypto'; |
| 3 | +import { subtle } from 'react-native-quick-crypto'; |
| 4 | +import { test } from '../util'; |
| 5 | + |
| 6 | +const SUITE = 'subtle.importKey/exportKey'; |
| 7 | + |
| 8 | +// Issue #806: Ensure JWK exports are RFC 7517 compliant (valid base64url, no periods) |
| 9 | +test(SUITE, 'JWK export - RFC 7517 - RSA-OAEP', async () => { |
| 10 | + const { publicKey, privateKey } = (await subtle.generateKey( |
| 11 | + { |
| 12 | + name: 'RSA-OAEP', |
| 13 | + modulusLength: 2048, |
| 14 | + publicExponent: new Uint8Array([1, 0, 1]), |
| 15 | + hash: 'SHA-256', |
| 16 | + }, |
| 17 | + true, |
| 18 | + ['encrypt', 'decrypt'], |
| 19 | + )) as CryptoKeyPair; |
| 20 | + |
| 21 | + const exportedPub = (await subtle.exportKey('jwk', publicKey)) as JWK; |
| 22 | + expect(exportedPub.n).to.match(/^[A-Za-z0-9_-]+$/); |
| 23 | + expect(exportedPub.e).to.match(/^[A-Za-z0-9_-]+$/); |
| 24 | + |
| 25 | + const exportedPriv = (await subtle.exportKey('jwk', privateKey)) as JWK; |
| 26 | + expect(exportedPriv.n).to.match(/^[A-Za-z0-9_-]+$/); |
| 27 | + expect(exportedPriv.e).to.match(/^[A-Za-z0-9_-]+$/); |
| 28 | + expect(exportedPriv.d).to.match(/^[A-Za-z0-9_-]+$/); |
| 29 | + expect(exportedPriv.p).to.match(/^[A-Za-z0-9_-]+$/); |
| 30 | + expect(exportedPriv.q).to.match(/^[A-Za-z0-9_-]+$/); |
| 31 | + expect(exportedPriv.dp).to.match(/^[A-Za-z0-9_-]+$/); |
| 32 | + expect(exportedPriv.dq).to.match(/^[A-Za-z0-9_-]+$/); |
| 33 | + expect(exportedPriv.qi).to.match(/^[A-Za-z0-9_-]+$/); |
| 34 | + |
| 35 | + // Verify roundtrip |
| 36 | + const imported = await subtle.importKey( |
| 37 | + 'jwk', |
| 38 | + exportedPriv, |
| 39 | + { name: 'RSA-OAEP', hash: 'SHA-256' }, |
| 40 | + true, |
| 41 | + ['decrypt'], |
| 42 | + ); |
| 43 | + expect(imported.type).to.equal('private'); |
| 44 | +}); |
| 45 | + |
| 46 | +test(SUITE, 'JWK export - RFC 7517 - ECDSA P-256', async () => { |
| 47 | + const { publicKey, privateKey } = (await subtle.generateKey( |
| 48 | + { name: 'ECDSA', namedCurve: 'P-256' }, |
| 49 | + true, |
| 50 | + ['sign', 'verify'], |
| 51 | + )) as CryptoKeyPair; |
| 52 | + |
| 53 | + const exportedPub = (await subtle.exportKey('jwk', publicKey)) as JWK; |
| 54 | + expect(exportedPub.x).to.match(/^[A-Za-z0-9_-]+$/); |
| 55 | + expect(exportedPub.y).to.match(/^[A-Za-z0-9_-]+$/); |
| 56 | + |
| 57 | + const exportedPriv = (await subtle.exportKey('jwk', privateKey)) as JWK; |
| 58 | + expect(exportedPriv.x).to.match(/^[A-Za-z0-9_-]+$/); |
| 59 | + expect(exportedPriv.y).to.match(/^[A-Za-z0-9_-]+$/); |
| 60 | + expect(exportedPriv.d).to.match(/^[A-Za-z0-9_-]+$/); |
| 61 | + |
| 62 | + const imported = await subtle.importKey( |
| 63 | + 'jwk', |
| 64 | + exportedPriv, |
| 65 | + { name: 'ECDSA', namedCurve: 'P-256' }, |
| 66 | + true, |
| 67 | + ['sign'], |
| 68 | + ); |
| 69 | + expect(imported.type).to.equal('private'); |
| 70 | +}); |
| 71 | + |
| 72 | +test(SUITE, 'JWK export - RFC 7517 - ECDSA P-384', async () => { |
| 73 | + const { privateKey } = (await subtle.generateKey( |
| 74 | + { name: 'ECDSA', namedCurve: 'P-384' }, |
| 75 | + true, |
| 76 | + ['sign', 'verify'], |
| 77 | + )) as CryptoKeyPair; |
| 78 | + |
| 79 | + const exported = (await subtle.exportKey('jwk', privateKey)) as JWK; |
| 80 | + expect(exported.x).to.match(/^[A-Za-z0-9_-]+$/); |
| 81 | + expect(exported.y).to.match(/^[A-Za-z0-9_-]+$/); |
| 82 | + expect(exported.d).to.match(/^[A-Za-z0-9_-]+$/); |
| 83 | +}); |
| 84 | + |
| 85 | +test(SUITE, 'JWK export - RFC 7517 - ECDSA P-521', async () => { |
| 86 | + const { privateKey } = (await subtle.generateKey( |
| 87 | + { name: 'ECDSA', namedCurve: 'P-521' }, |
| 88 | + true, |
| 89 | + ['sign', 'verify'], |
| 90 | + )) as CryptoKeyPair; |
| 91 | + |
| 92 | + const exported = (await subtle.exportKey('jwk', privateKey)) as JWK; |
| 93 | + expect(exported.x).to.match(/^[A-Za-z0-9_-]+$/); |
| 94 | + expect(exported.y).to.match(/^[A-Za-z0-9_-]+$/); |
| 95 | + expect(exported.d).to.match(/^[A-Za-z0-9_-]+$/); |
| 96 | +}); |
| 97 | + |
| 98 | +test(SUITE, 'JWK export - RFC 7517 - ECDH P-256', async () => { |
| 99 | + const { privateKey } = (await subtle.generateKey( |
| 100 | + { name: 'ECDH', namedCurve: 'P-256' }, |
| 101 | + true, |
| 102 | + ['deriveKey'], |
| 103 | + )) as CryptoKeyPair; |
| 104 | + |
| 105 | + const exported = (await subtle.exportKey('jwk', privateKey)) as JWK; |
| 106 | + expect(exported.x).to.match(/^[A-Za-z0-9_-]+$/); |
| 107 | + expect(exported.y).to.match(/^[A-Za-z0-9_-]+$/); |
| 108 | + expect(exported.d).to.match(/^[A-Za-z0-9_-]+$/); |
| 109 | +}); |
| 110 | + |
| 111 | +// Test exact scenario from issue #806 |
| 112 | +test(SUITE, 'JWK export - issue #806 - no trailing periods', async () => { |
| 113 | + const { privateKey } = (await subtle.generateKey( |
| 114 | + { |
| 115 | + name: 'RSA-OAEP', |
| 116 | + modulusLength: 2048, |
| 117 | + publicExponent: new Uint8Array([1, 0, 1]), |
| 118 | + hash: 'SHA-256', |
| 119 | + }, |
| 120 | + true, |
| 121 | + ['encrypt', 'decrypt'], |
| 122 | + )) as CryptoKeyPair; |
| 123 | + |
| 124 | + const jwk = (await subtle.exportKey('jwk', privateKey as CryptoKey)) as JWK; |
| 125 | + |
| 126 | + // All fields must be valid base64url (only A-Za-z0-9_-) |
| 127 | + const fields = ['n', 'e', 'd', 'p', 'q', 'dp', 'dq', 'qi'] as const; |
| 128 | + for (const field of fields) { |
| 129 | + expect(jwk[field]).to.match(/^[A-Za-z0-9_-]+$/); |
| 130 | + } |
| 131 | + |
| 132 | + // Critical: can we import this JWK? |
| 133 | + const imported = await subtle.importKey( |
| 134 | + 'jwk', |
| 135 | + jwk, |
| 136 | + { name: 'RSA-OAEP', hash: 'SHA-256' }, |
| 137 | + true, |
| 138 | + ['decrypt'], |
| 139 | + ); |
| 140 | + expect(imported.type).to.equal('private'); |
| 141 | +}); |
0 commit comments