Skip to content

Commit 41d0a50

Browse files
ngie-eignkhorbenemaste
committed
Make crypto.c compile/link with OpenSSL 3
- Initialize libssl using non-deprecated APIs OpenSSL 3 deprecated `SSL_library_init` and `SSL_load_error_strings` in favor of `OPENSSL_init_ssl`. Use `OPENSSL_init_ssl` when dealing with OpenSSL 1.1 and newer to unbreak the build with OpenSSL 3. - Move to EVP_MD* APIs OpenSSL 3 deprecated all of the `MD5_`* APIs. Move to the equivalent `EVP_MD`* APIs so the code doesn't need to be pinned down to 1.1 compatible APIs and uplifted later. Some work will likely be required to deal with other OpenSSL offshoots, e.g., BoringSSL, LibreSSL, etc. Co-authored-by: Pierre Pronchery <pierre@freebsdfoundation.org> Co-authored-by: Ed Maste <emaste@FreeBSD.org> Signed-off-by: Enji Cooper <yaneurabeya@gmail.com> Sponsored by: The FreeBSD Foundation
1 parent 43fff9a commit 41d0a50

File tree

1 file changed

+66
-9
lines changed

1 file changed

+66
-9
lines changed

crypto.c

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,19 @@
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,16 @@ 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)
125+
error = OPENSSL_init_ssl(0, NULL);
126+
if (error != 1) {
127+
syslog(LOG_WARNING, "remote delivery deferred: SSL init failed: %s", ssl_errstr());
128+
return (1);
129+
}
130+
#else
118131
SSL_library_init();
119132
SSL_load_error_strings();
133+
#endif
120134

121135
// Allow any possible version
122136
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
@@ -225,7 +239,12 @@ void
225239
hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
226240
unsigned char* digest)
227241
{
228-
MD5_CTX context;
242+
#ifdef IS_OPENSSL3
243+
EVP_MD *md;
244+
EVP_MD_CTX *context;
245+
#else
246+
MD5_CTX context;
247+
#endif
229248
unsigned char k_ipad[65]; /* inner padding -
230249
* key XORd with ipad
231250
*/
@@ -234,15 +253,23 @@ hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
234253
*/
235254
unsigned char tk[16];
236255
int i;
237-
/* if key is longer than 64 bytes reset it to key=MD5(key) */
238-
if (key_len > 64) {
239-
240-
MD5_CTX tctx;
241256

242-
MD5_Init(&tctx);
243-
MD5_Update(&tctx, key, key_len);
244-
MD5_Final(tk, &tctx);
257+
#ifdef IS_OPENSSL3
258+
context = EVP_MD_CTX_new();
259+
assert(context != NULL);
260+
#endif
245261

262+
/* if key is longer than 64 bytes reset it to key=MD5(key) */
263+
if (key_len > 64) {
264+
#ifdef IS_OPENSSL3
265+
EVP_DigestInit_ex(context, md, NULL);
266+
EVP_DigestUpdate(context, key, key_len);
267+
EVP_DigestFinal_ex(context, tk, NULL);
268+
#else
269+
MD5_Init(&context);
270+
MD5_Update(&context, key, key_len);
271+
MD5_Final(tk, &context);
272+
#endif
246273
key = tk;
247274
key_len = 16;
248275
}
@@ -270,14 +297,43 @@ hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
270297
k_ipad[i] ^= 0x36;
271298
k_opad[i] ^= 0x5c;
272299
}
300+
301+
#ifdef IS_OPENSSL3
302+
/**
303+
* Perform inner MD5.
304+
*/
305+
306+
/* Init context for first pass. */
307+
EVP_DigestInit_ex(context, md, NULL);
308+
/* Start with inner pad. */
309+
EVP_DigestUpdate(context, k_ipad, 64);
310+
/* Update with text of datagram. */
311+
EVP_DigestUpdate(context, text, text_len);
312+
/* Finish up first pass. */
313+
EVP_DigestFinal_ex(context, digest, NULL);
314+
315+
/**
316+
* Perform outer MD5.
317+
*/
318+
319+
/* Re-init context for second pass. */
320+
EVP_DigestInit_ex(context, md, NULL);
321+
/* Start with outer pad. */
322+
EVP_DigestUpdate(context, k_opad, 64);
323+
/* Update with results of first hash. */
324+
EVP_DigestUpdate(context, digest, 16);
325+
/* Finish up second pass. */
326+
EVP_DigestFinal_ex(context, digest, NULL);
327+
#else
273328
/*
274329
* perform inner MD5
275330
*/
276331
MD5_Init(&context); /* init context for 1st
277332
* pass */
278333
MD5_Update(&context, k_ipad, 64); /* start with inner pad */
279334
MD5_Update(&context, text, text_len); /* then text of datagram */
280-
MD5_Final(digest, &context); /* finish up 1st pass */
335+
336+
MD5_Final(digest, &context); /* finish up 1st pass */
281337
/*
282338
* perform outer MD5
283339
*/
@@ -287,6 +343,7 @@ hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
287343
MD5_Update(&context, digest, 16); /* then results of 1st
288344
* hash */
289345
MD5_Final(digest, &context); /* finish up 2nd pass */
346+
#endif
290347
}
291348

292349
/*

0 commit comments

Comments
 (0)