Skip to content

Commit 9c3c470

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 MD5 APIs 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 at a later date. 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 9c3c470

File tree

1 file changed

+62
-7
lines changed

1 file changed

+62
-7
lines changed

crypto.c

Lines changed: 62 additions & 7 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,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
225233
hmac_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,6 +294,36 @@ 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
*/
@@ -287,6 +341,7 @@ hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len,
287341
MD5_Update(&context, digest, 16); /* then results of 1st
288342
* hash */
289343
MD5_Final(digest, &context); /* finish up 2nd pass */
344+
#endif
290345
}
291346

292347
/*

0 commit comments

Comments
 (0)