import { createMessage , getSignature , verifyMessage } from 'pmcrypto' ; import { VERIFICATION_STATUS } from '@proton/shared/lib/mail/constants' ; param { string } ID param { number } verified param addressID returns { * } param message const { SIGNED_AND_VALID , SIGNED_AND_INVALID , NOT_VERIFIED , NOT_SIGNED , SIGNED_NO_PUB_KEY } = VERIFICATION_STATUS ; param { String } Attachment . ID returns { * } export const getVerificationStatusFromCache = ( { ID = '' } , cache ) => { return cache . has ( ID ) ? cache . get ( ID ) : NOT_VERIFIED ; } ; param { Uint8Array } decryptedAttachment param { Array } signatures param { Array } publicKeys returns { Promise . < * [ ] > } const verifyAllSignatures = ( decryptedAttachment , signatures , publicKeys ) => { const attMessage = createMessage ( decryptedAttachment ) ; const asyncSigVerifiers = signatures . map ( ( signature ) => { return verifyMessage ( { message : attMessage , publicKeys , signature , } ) . then ( ( { verified } ) => verified ) ; } ) ; return Promise . all ( asyncSigVerifiers ) ; } ; param { Object } attachment param { UInt8Array } decryptedAttachment param { Message } message param { embeddedSigs } A returns { Promise . < * > } export const verify = async ( attachment , decryptedAttachment , { publicKeys } , embeddedSigs = [ ] , cache ) => { const { ID , Signature } = attachment ; const signatures = Signature ? [ await getSignature ( Signature ) ] : embeddedSigs ; if ( ! signatures . length ) { cache . set ( ID , NOT_SIGNED ) ; return cache . get ( ID ) ; } const statusPerSig = await verifyAllSignatures ( decryptedAttachment , signatures , publicKeys ) ; const pmcryptoVerified = statusPerSig . reduce ( ( acc , status ) => ( acc === SIGNED_AND_VALID ? SIGNED_AND_VALID : status ) , NOT_SIGNED ) ; const verified = ( ! publicKeys || ! publicKeys . length ) && pmcryptoVerified === SIGNED_AND_INVALID ? SIGNED_NO_PUB_KEY : pmcryptoVerified ; cache . set ( ID , verified ) ; return verified ; } ;