33
33
* SUCH DAMAGE.
34
34
*/
35
35
36
+ #include <openssl/opensslv.h>
37
+ #if (OPENSSL_VERSION_NUMBER >= 0x300000L )
38
+ #define IS_OPENSSL3 1
39
+ #endif
40
+
36
41
#include <openssl/x509.h>
37
42
#include <openssl/md5.h>
38
43
#include <openssl/ssl.h>
39
44
#include <openssl/err.h>
40
45
#include <openssl/pem.h>
41
46
#include <openssl/rand.h>
42
47
48
+ #include <assert.h>
43
49
#include <strings.h>
44
50
#include <string.h>
45
51
#include <syslog.h>
@@ -115,8 +121,10 @@ smtp_init_crypto(int fd, int feature, struct smtp_features* features)
115
121
116
122
/* XXX clean up on error/close */
117
123
/* Init SSL library */
124
+ #if (OPENSSL_VERSION_NUMBER < 0x10100000L )
118
125
SSL_library_init ();
119
126
SSL_load_error_strings ();
127
+ #endif
120
128
121
129
// Allow any possible version
122
130
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L )
@@ -225,7 +233,12 @@ void
225
233
hmac_md5 (unsigned char * text , int text_len , unsigned char * key , int key_len ,
226
234
unsigned char * digest )
227
235
{
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
229
242
unsigned char k_ipad [65 ]; /* inner padding -
230
243
* key XORd with ipad
231
244
*/
@@ -234,15 +247,26 @@ hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
234
247
*/
235
248
unsigned char tk [16 ];
236
249
int i ;
237
- /* if key is longer than 64 bytes reset it to key=MD5(key) */
238
- if (key_len > 64 ) {
239
250
240
- MD5_CTX tctx ;
251
+ #ifdef IS_OPENSSL3
252
+ context = EVP_MD_CTX_new ();
253
+ assert (context != NULL );
241
254
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
245
258
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
246
270
key = tk ;
247
271
key_len = 16 ;
248
272
}
@@ -270,13 +294,44 @@ hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
270
294
k_ipad [i ] ^= 0x36 ;
271
295
k_opad [i ] ^= 0x5c ;
272
296
}
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
273
327
/*
274
328
* perform inner MD5
275
329
*/
276
330
MD5_Init (& context ); /* init context for 1st
277
331
* pass */
278
332
MD5_Update (& context , k_ipad , 64 ); /* start with inner pad */
279
333
MD5_Update (& context , text , text_len ); /* then text of datagram */
334
+
280
335
MD5_Final (digest , & context ); /* finish up 1st pass */
281
336
/*
282
337
* perform outer MD5
@@ -287,6 +342,7 @@ hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
287
342
MD5_Update (& context , digest , 16 ); /* then results of 1st
288
343
* hash */
289
344
MD5_Final (digest , & context ); /* finish up 2nd pass */
345
+ #endif
290
346
}
291
347
292
348
/*
0 commit comments