@@ -28,11 +28,25 @@ export async function generate(algo) {
2828 case enums . publicKey . x25519 :
2929 try {
3030 const webCrypto = util . getWebCrypto ( ) ;
31- const webCryptoKey = await webCrypto . generateKey ( 'X25519' , true , [ 'deriveKey' , 'deriveBits' ] ) ;
31+ const webCryptoKey = await webCrypto . generateKey ( 'X25519' , true , [ 'deriveKey' , 'deriveBits' ] )
32+ . catch ( err => {
33+ if ( err . name === 'OperationError' ) { // Temporary (hopefully) fix for WebKit on Linux
34+ const newErr = new Error ( 'Unexpected key generation issue' ) ;
35+ newErr . name = 'NotSupportedError' ;
36+ throw newErr ;
37+ }
38+ throw err ;
39+ } ) ;
3240
3341 const privateKey = await webCrypto . exportKey ( 'jwk' , webCryptoKey . privateKey ) ;
3442 const publicKey = await webCrypto . exportKey ( 'jwk' , webCryptoKey . publicKey ) ;
3543
44+ if ( privateKey . x !== publicKey . x ) { // Weird issue with Webkit on Linux: https://bugs.webkit.org/show_bug.cgi?id=289693
45+ const err = new Error ( 'Unexpected mismatching public point' ) ;
46+ err . name = 'NotSupportedError' ;
47+ throw err ;
48+ }
49+
3650 return {
3751 A : new Uint8Array ( b64ToUint8Array ( publicKey . x ) ) ,
3852 k : b64ToUint8Array ( privateKey . d )
@@ -190,15 +204,29 @@ export async function generateEphemeralEncryptionMaterial(algo, recipientA) {
190204 case enums . publicKey . x25519 :
191205 try {
192206 const webCrypto = util . getWebCrypto ( ) ;
207+ const ephemeralKeyPair = await webCrypto . generateKey ( 'X25519' , true , [ 'deriveKey' , 'deriveBits' ] )
208+ . catch ( err => {
209+ if ( err . name === 'OperationError' ) { // Temporary (hopefully) fix for WebKit on Linux
210+ const newErr = new Error ( 'Unexpected key generation issue' ) ;
211+ newErr . name = 'NotSupportedError' ;
212+ throw newErr ;
213+ }
214+ throw err ;
215+ } ) ;
216+ const ephemeralPublicKeyJwt = await webCrypto . exportKey ( 'jwk' , ephemeralKeyPair . publicKey ) ;
217+ const ephemeralPrivateKeyJwt = await webCrypto . exportKey ( 'jwk' , ephemeralKeyPair . privateKey ) ;
218+ if ( ephemeralPrivateKeyJwt . x !== ephemeralPublicKeyJwt . x ) { // Weird issue with Webkit on Linux: https://bugs.webkit.org/show_bug.cgi?id=289693
219+ const err = new Error ( 'Unexpected mismatching public point' ) ;
220+ err . name = 'NotSupportedError' ;
221+ throw err ;
222+ }
193223 const jwk = publicKeyToJWK ( algo , recipientA ) ;
194- const ephemeralKeyPair = await webCrypto . generateKey ( 'X25519' , true , [ 'deriveKey' , 'deriveBits' ] ) ;
195224 const recipientPublicKey = await webCrypto . importKey ( 'jwk' , jwk , 'X25519' , false , [ ] ) ;
196225 const sharedSecretBuffer = await webCrypto . deriveBits (
197226 { name : 'X25519' , public : recipientPublicKey } ,
198227 ephemeralKeyPair . privateKey ,
199228 getPayloadSize ( algo ) * 8 // in bits
200229 ) ;
201- const ephemeralPublicKeyJwt = await webCrypto . exportKey ( 'jwk' , ephemeralKeyPair . publicKey ) ;
202230 return {
203231 sharedSecret : new Uint8Array ( sharedSecretBuffer ) ,
204232 ephemeralPublicKey : new Uint8Array ( b64ToUint8Array ( ephemeralPublicKeyJwt . x ) )
0 commit comments