@@ -209,12 +209,11 @@ static encoded_encrypt_template_t *lookup_encrypted_template(
209209
210210int encrypt_aes_192_cbc (EVP_CIPHER_CTX * ctx , uint8_t * buf , uint16_t buflen ,
211211 uint8_t * dest , uint16_t destlen , uint32_t seqno ,
212- char * encryptkey ) {
212+ const uint8_t * encryptkey ) {
213213
214214 uint8_t IV_128 [16 ];
215215 uint8_t key [24 ];
216216 uint32_t swapseqno ;
217- size_t keylen = strlen (encryptkey );
218217 int len , i ;
219218
220219 assert (buflen <= destlen );
@@ -226,11 +225,9 @@ int encrypt_aes_192_cbc(EVP_CIPHER_CTX *ctx, uint8_t *buf, uint16_t buflen,
226225 memcpy (& (IV_128 [i ]), & swapseqno , sizeof (uint32_t ));
227226 }
228227
229- if (keylen > 24 ) {
230- keylen = 24 ;
231- }
232- memset (key , 0 , 24 );
233- memcpy (key , encryptkey , keylen );
228+ /* The key is 24 bytes for AES-192. */
229+ memcpy (key , encryptkey , 24 );
230+
234231
235232 /* Trust that we have correctly pre-padded the data to encrypt */
236233 EVP_CIPHER_CTX_set_padding (ctx , 0 );
@@ -241,6 +238,17 @@ int encrypt_aes_192_cbc(EVP_CIPHER_CTX *ctx, uint8_t *buf, uint16_t buflen,
241238 return -1 ;
242239 }
243240
241+ /* ETSI-IP.nl uses AES-192-CBC with application-layer padding (if any).
242+ * Disable PKCS#7 inside the cipher so ciphertext length == input length. */
243+ EVP_CIPHER_CTX_set_padding (ctx , 0 );
244+
245+ /* Sanity: with padding disabled, input MUST be a multiple of 16. */
246+ if ((buflen & 0x0F ) != 0 ) {
247+ logger (LOG_INFO , "OpenLI: AES-192-CBC called with non-block-aligned input (%u bytes)" , buflen );
248+ return -1 ;
249+ }
250+
251+
244252 if (EVP_EncryptUpdate (ctx , dest , & len , buf , (int )buflen ) != 1 ) {
245253 logger (LOG_INFO , "OpenLI: unable to perform EVP encryption operation -- openssl error %s" , ERR_error_string (ERR_get_error (), NULL ));
246254 return -1 ;
@@ -251,6 +259,13 @@ int encrypt_aes_192_cbc(EVP_CIPHER_CTX *ctx, uint8_t *buf, uint16_t buflen,
251259 return -1 ;
252260 }
253261
262+ /* Cleanse local key copy */
263+ #if defined(__GLIBC__ ) && defined(_GNU_SOURCE )
264+ explicit_bzero (key , sizeof (key ));
265+ #else
266+ volatile uint8_t * p = key ;
267+ for (size_t i = 0 ; i < sizeof (key ); ++ i ) p [i ] = 0 ;
268+ #endif
254269 return 0 ;
255270
256271}
@@ -388,7 +403,6 @@ int create_preencrypted_message_body(wandder_encoder_t *encoder,
388403 * mediator itself.
389404 */
390405
391-
392406 /* Lookup the template for a message of this length and encryption method */
393407 tplate = lookup_encrypted_template (& (encrypt -> saved_encryption_templates ),
394408 enclen , job -> encryptmethod , & is_new );
0 commit comments