@@ -2061,63 +2061,35 @@ class MessageHandler {
20612061 return false ;
20622062 }
20632063
2064- let derEncoded ;
2065- let smimeType ;
2064+ let cipherConfig = {
2065+ 'AES-CBC' : { fn : SMIMEEncryptor . encryptCBC , smimeType : 'enveloped-data' } ,
2066+ 'AES-GCM' : { fn : SMIMEEncryptor . encryptGCM , smimeType : 'authEnveloped-data' }
2067+ } [ cipher ] ;
2068+ let encryptFn = cipherConfig . fn ;
2069+ let smimeType = cipherConfig . smimeType ;
20662070
2067- if ( cipher === 'AES-CBC' ) {
2068- // AES-256-CBC via Node.js crypto primitives
2069- let result ;
2070- try {
2071- result = await SMIMEEncryptor . encryptCBC ( validCerts , raw , { keyTransport } ) ;
2072- } catch ( err ) {
2073- log . error ( 'SMIME' , 'AES-CBC encryption failed: %s' , err . message ) ;
2074- this . loggelf ( {
2075- short_message : '[ENCRYPTFAIL] AES-CBC encryption failed' ,
2076- _mail_action : 'encrypt_fail' ,
2077- _error : err . message ,
2078- _source : 'smime_encrypt'
2079- } ) ;
2080- return false ;
2081- }
2082- if ( ! result ) {
2083- log . error ( 'SMIME' , 'AES-CBC encryption returned no result' ) ;
2084- this . loggelf ( {
2085- short_message : '[ENCRYPTFAIL] AES-CBC encryption returned no result' ,
2086- _mail_action : 'encrypt_fail' ,
2087- _error : 'No result from encryptCBC' ,
2088- _source : 'smime_encrypt'
2089- } ) ;
2090- return false ;
2091- }
2092- derEncoded = result ;
2093- smimeType = 'enveloped-data' ;
2094- } else {
2095- // AES-256-GCM via custom AuthEnvelopedData builder
2096- let result ;
2097- try {
2098- result = await SMIMEEncryptor . encryptGCM ( validCerts , raw , { keyTransport } ) ;
2099- } catch ( err ) {
2100- log . error ( 'SMIME' , 'AES-GCM encryption failed: %s' , err . message ) ;
2101- this . loggelf ( {
2102- short_message : '[ENCRYPTFAIL] AES-GCM encryption failed' ,
2103- _mail_action : 'encrypt_fail' ,
2104- _error : err . message ,
2105- _source : 'smime_encrypt'
2106- } ) ;
2107- return false ;
2108- }
2109- if ( ! result ) {
2110- log . error ( 'SMIME' , 'AES-GCM encryption returned no result' ) ;
2111- this . loggelf ( {
2112- short_message : '[ENCRYPTFAIL] AES-GCM encryption returned no result' ,
2113- _mail_action : 'encrypt_fail' ,
2114- _error : 'No result from encryptGCM' ,
2115- _source : 'smime_encrypt'
2116- } ) ;
2117- return false ;
2118- }
2119- derEncoded = result ;
2120- smimeType = 'authEnveloped-data' ;
2071+ let derEncoded ;
2072+ try {
2073+ derEncoded = await encryptFn ( validCerts , raw , { keyTransport } ) ;
2074+ } catch ( err ) {
2075+ log . error ( 'SMIME' , '%s encryption failed: %s' , cipher , err . message ) ;
2076+ this . loggelf ( {
2077+ short_message : '[ENCRYPTFAIL] ' + cipher + ' encryption failed' ,
2078+ _mail_action : 'encrypt_fail' ,
2079+ _error : err . message ,
2080+ _source : 'smime_encrypt'
2081+ } ) ;
2082+ return false ;
2083+ }
2084+ if ( ! derEncoded ) {
2085+ log . error ( 'SMIME' , '%s encryption returned no result' , cipher ) ;
2086+ this . loggelf ( {
2087+ short_message : '[ENCRYPTFAIL] ' + cipher + ' encryption returned no result' ,
2088+ _mail_action : 'encrypt_fail' ,
2089+ _error : 'No result from ' + cipher ,
2090+ _source : 'smime_encrypt'
2091+ } ) ;
2092+ return false ;
21212093 }
21222094
21232095 let b64Encoded = Buffer . from ( derEncoded ) . toString ( 'base64' ) ;
0 commit comments