import { KeyringWebCrypto , MultiKeyringWebCrypto , WebCryptoMaterialsManager , DecryptResult , needs , } from '@aws-crypto/client-browser' ; import { DecryptionFixture , KeyInfoTuple , TestVectorResult , } from '@aws-crypto/integration-vectors' ; import { decryptMaterialsManagerWebCrypto } from './decrypt_materials_manager_web_crypto' ; import { fromBase64 } from '@aws-sdk/util-base64-browser' ; export const expectedNotActualPlaintextMessage = `Decrypted plaintext did not match expected plaintext` ; export const notSupportedDecryptMessages = [ '192-bit AES keys are not supported' , 'Unsupported right now' , ] ; export const bitFlippedDerTagsVectors = [ '82f59ef1-7002-486d-9679-f4b358e6f05e' , '516abc51-d740-4715-8888-73ebf1c3b674' , '6911b1b6-238c-4c82-a27c-31a21a770380' , '9e5dd64e-fa70-42cf-9f32-7168710ca193' , '233e79f7-aab8-4381-9fdc-2b2b4d0efd91' , '9905767c-a214-4483-8e69-425a8b1ec833' , 'b40cecee-bff8-45c0-9679-a7533ce8ad75' , '1f665dd8-08f3-43b0-96ab-deca39634bea' , 'f673bdf3-40a8-4551-bc7f-866b289e4d03' , ] ; export async function testPositiveDecryptFixture ( name , expectedPlainText , cipher , keyInfo , _decrypt , _getCmm ) { let decryptResult ; try { const cmm = await _getCmm ( keyInfo ) ; decryptResult = await _decrypt ( cmm , cipher ) ; if ( expectedPlainText . toString ( ) . localeCompare ( decryptResult . plaintext . toString ( ) ) == 0 ) { return { result : true , name } ; } throw new Error ( expectedNotActualPlaintextMessage + `\n expected plaintext: ${ expectedPlainText }` + `\n actual plaintext: ${ decryptResult . plaintext }` ) ; } catch ( err ) { return { result : false , name , err } ; } } export async function testNegativeDecryptFixture ( name , errorDescription , cipher , keyInfos , _decrypt , _getCmm ) { let decryptResult ; try { const cmm = await _getCmm ( keyInfos ) ; decryptResult = await _decrypt ( cmm , cipher ) ; } catch ( err ) { needs ( err instanceof Error , `Thrown object must be an error but was ${ typeof err }` ) ; if ( notSupportedDecryptMessages . includes ( err . message ) ) { return { result : false , name , err : err } ; } return { result : true , name } ; } return { result : false , name , err : new Error ( `Expected to fail with ${ errorDescription }, but succeeded with decrypt result:` + `\n header: ${ decryptResult . messageHeader }` + `\n plaintext: ${ decryptResult . plaintext }` ) , } ; } export async function decryptionIntegrationBrowserTest ( { cipherText , description , keysInfo , name , result } , decrypt , _expect ) { let testResult = { name : name , result : false , err : new Error ( `Unsupported 'result' in decryption test fixture ${ result }` ) , } ; if ( 'plainText' in result ) { console . log ( `start positive test: ${ name }; ${ description }` ) ; testResult = await testPositiveDecryptFixture ( name , fromBase64 ( result . plainText ) , fromBase64 ( cipherText ) , keysInfo , decrypt , decryptMaterialsManagerWebCrypto ) ; } if ( 'errorDescription' in result ) { const errorDescription = result . errorDescription ; console . log ( `start negative test: ${ name }; ${ description }; expected error ${ errorDescription }` ) ; testResult = await testNegativeDecryptFixture ( name , errorDescription , fromBase64 ( cipherText ) , keysInfo , decrypt , decryptMaterialsManagerWebCrypto ) ; } return evaluateTestResultIgnoreUnsupported ( testResult , _expect ) ; } export function evaluateTestResultIgnoreUnsupported ( { err , name , result } , _expect ) { if ( bitFlippedDerTagsVectors . includes ( name ) ) { return _expect ( result ) . toEqual ( false ) ; } if ( err && err [ 'message' ] ) { const message = err . message ; if ( notSupportedDecryptMessages . includes ( message ) ) { return _expect ( result ) . toEqual ( false ) ; } } if ( ! result ) { console . error ( `${ name } FAILED` ) ; throw err ; } return _expect ( result ) . toEqual ( true , err ) ; }