3333 * SUCH DAMAGE.
3434 */
3535
36+ #include <openssl/opensslv.h>
37+ #if (OPENSSL_VERSION_NUMBER >= 0x300000L )
38+ #define IS_OPENSSL3 1
39+ #endif
40+
3641#include <openssl/x509.h>
3742#include <openssl/md5.h>
3843#include <openssl/ssl.h>
3944#include <openssl/err.h>
4045#include <openssl/pem.h>
4146#include <openssl/rand.h>
4247
48+ #include <assert.h>
4349#include <strings.h>
4450#include <string.h>
4551#include <syslog.h>
@@ -115,8 +121,10 @@ smtp_init_crypto(int fd, int feature, struct smtp_features* features)
115121
116122 /* XXX clean up on error/close */
117123 /* Init SSL library */
124+ #if (OPENSSL_VERSION_NUMBER < 0x10100000L )
118125 SSL_library_init ();
119126 SSL_load_error_strings ();
127+ #endif
120128
121129 // Allow any possible version
122130#if (OPENSSL_VERSION_NUMBER >= 0x10100000L )
@@ -225,7 +233,12 @@ void
225233hmac_md5 (unsigned char * text , int text_len , unsigned char * key , int key_len ,
226234 unsigned char * digest )
227235{
228- MD5_CTX context ;
236+ #ifdef IS_OPENSSL3
237+ const EVP_MD * md ;
238+ EVP_MD_CTX * context ;
239+ #else
240+ MD5_CTX context ;
241+ #endif
229242 unsigned char k_ipad [65 ]; /* inner padding -
230243 * key XORd with ipad
231244 */
@@ -234,15 +247,26 @@ hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
234247 */
235248 unsigned char tk [16 ];
236249 int i ;
237- /* if key is longer than 64 bytes reset it to key=MD5(key) */
238- if (key_len > 64 ) {
239250
240- MD5_CTX tctx ;
251+ #ifdef IS_OPENSSL3
252+ context = EVP_MD_CTX_new ();
253+ assert (context != NULL );
241254
242- MD5_Init ( & tctx );
243- MD5_Update ( & tctx , key , key_len );
244- MD5_Final ( tk , & tctx );
255+ md = EVP_md5 ( );
256+ assert ( md != NULL );
257+ #endif
245258
259+ /* if key is longer than 64 bytes reset it to key=MD5(key) */
260+ if (key_len > 64 ) {
261+ #ifdef IS_OPENSSL3
262+ EVP_DigestInit_ex (context , md , NULL );
263+ EVP_DigestUpdate (context , key , key_len );
264+ EVP_DigestFinal_ex (context , tk , NULL );
265+ #else
266+ MD5_Init (& context );
267+ MD5_Update (& context , key , key_len );
268+ MD5_Final (tk , & context );
269+ #endif
246270 key = tk ;
247271 key_len = 16 ;
248272 }
@@ -270,13 +294,44 @@ hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
270294 k_ipad [i ] ^= 0x36 ;
271295 k_opad [i ] ^= 0x5c ;
272296 }
297+
298+ #ifdef IS_OPENSSL3
299+ /**
300+ * Perform inner MD5.
301+ */
302+
303+ /* Init context for first pass. */
304+ EVP_DigestInit_ex (context , md , NULL );
305+ /* Start with inner pad. */
306+ EVP_DigestUpdate (context , k_ipad , 64 );
307+ /* Update with text of datagram. */
308+ EVP_DigestUpdate (context , text , text_len );
309+ /* Finish up first pass. */
310+ EVP_DigestFinal_ex (context , digest , NULL );
311+
312+ /**
313+ * Perform outer MD5.
314+ */
315+
316+ /* Re-init context for second pass. */
317+ EVP_DigestInit_ex (context , md , NULL );
318+ /* Start with outer pad. */
319+ EVP_DigestUpdate (context , k_opad , 64 );
320+ /* Update with results of first hash. */
321+ EVP_DigestUpdate (context , digest , 16 );
322+ /* Finish up second pass. */
323+ EVP_DigestFinal_ex (context , digest , NULL );
324+
325+ EVP_MD_CTX_free (context );
326+ #else
273327 /*
274328 * perform inner MD5
275329 */
276330 MD5_Init (& context ); /* init context for 1st
277331 * pass */
278332 MD5_Update (& context , k_ipad , 64 ); /* start with inner pad */
279333 MD5_Update (& context , text , text_len ); /* then text of datagram */
334+
280335 MD5_Final (digest , & context ); /* finish up 1st pass */
281336 /*
282337 * perform outer MD5
@@ -287,6 +342,7 @@ hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
287342 MD5_Update (& context , digest , 16 ); /* then results of 1st
288343 * hash */
289344 MD5_Final (digest , & context ); /* finish up 2nd pass */
345+ #endif
290346}
291347
292348/*
0 commit comments