Skip to content

Commit c460b49

Browse files
authored
Merge pull request #92 from pshipton/0.32ossl3
(0.32) Add openssl version 3.0+ support for Linux platforms
2 parents 0e685e9 + 9e45453 commit c460b49

File tree

8 files changed

+70
-32
lines changed

8 files changed

+70
-32
lines changed

closed/src/java.base/aix/native/libjncrypto/NativeCrypto_md.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* ===========================================================================
3-
* (c) Copyright IBM Corp. 2019, 2019 All Rights Reserved
3+
* (c) Copyright IBM Corp. 2019, 2022 All Rights Reserved
44
* ===========================================================================
55
*
66
* This code is free software; you can redistribute it and/or modify it
@@ -30,7 +30,7 @@
3030
#include "NativeCrypto_md.h"
3131

3232
/* Load the crypto library (return NULL on error) */
33-
void * load_crypto_library() {
33+
void * load_crypto_library(jboolean traceEnabled) {
3434
void * result = NULL;
3535
const char *libname111 = "libcrypto.a(libcrypto64.so.1.1)";
3636
const char *libname110 = "libcrypto.so.1.1";

closed/src/java.base/macosx/native/libjncrypto/NativeCrypto_md.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* ===========================================================================
3-
* (c) Copyright IBM Corp. 2019, 2019 All Rights Reserved
3+
* (c) Copyright IBM Corp. 2019, 2022 All Rights Reserved
44
* ===========================================================================
55
*
66
* This code is free software; you can redistribute it and/or modify it
@@ -30,7 +30,7 @@
3030
#include "NativeCrypto_md.h"
3131

3232
/* Load the crypto library (return NULL on error) */
33-
void * load_crypto_library() {
33+
void * load_crypto_library(jboolean traceEnabled) {
3434
void * result = NULL;
3535

3636
const char *libname = "libcrypto.1.1.dylib";

closed/src/java.base/share/classes/jdk/crypto/jniprovider/NativeCrypto.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* ===========================================================================
3-
* (c) Copyright IBM Corp. 2018, 2019 All Rights Reserved
3+
* (c) Copyright IBM Corp. 2018, 2022 All Rights Reserved
44
* ===========================================================================
55
*
66
* This code is free software; you can redistribute it and/or modify it
@@ -36,15 +36,20 @@ public class NativeCrypto {
3636
//ossl_vers:
3737
// -1 : library load failed
3838
// 0 : openssl 1.0.x
39-
// 1 : openssl 1.1.x
39+
// 1 : openssl 1.1.x or newer
4040
private static final int ossl_ver = AccessController.doPrivileged(
4141
(PrivilegedAction<Integer>) () -> {
4242
int ossl_ver;
43+
boolean traceEnabled = Boolean.getBoolean("jdk.nativeCryptoTrace");
44+
4345
try {
4446
System.loadLibrary("jncrypto"); // check for native library
45-
// load OpenSSL crypto library dynamically
46-
ossl_ver = loadCrypto(Boolean.getBoolean("jdk.nativeCryptoTrace"));
47-
} catch (UnsatisfiedLinkError e) {
47+
// load OpenSSL crypto library dynamically.
48+
ossl_ver = loadCrypto(traceEnabled);
49+
} catch (UnsatisfiedLinkError usle) {
50+
if (traceEnabled) {
51+
System.err.println("UnsatisfiedLinkError: Failure attempting to load jncrypto JNI library");
52+
}
4853
// signal load failure
4954
ossl_ver = -1;
5055
}
@@ -76,8 +81,7 @@ public static NativeCrypto getNativeCrypto() {
7681
}
7782

7883
/* Native digest interfaces */
79-
80-
private static final native int loadCrypto(boolean trace);
84+
private static final native int loadCrypto(boolean traceEnabled);
8185

8286
public final native long DigestCreateContext(long nativeBuffer,
8387
int algoIndex);

closed/src/java.base/share/native/libjncrypto/NativeCrypto.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* ===========================================================================
3-
* (c) Copyright IBM Corp. 2018, 2021 All Rights Reserved
3+
* (c) Copyright IBM Corp. 2018, 2022 All Rights Reserved
44
* ===========================================================================
55
*
66
* This code is free software; you can redistribute it and/or modify it
@@ -38,6 +38,9 @@
3838

3939
#define OPENSSL_VERSION_1_0 "OpenSSL 1.0."
4040
#define OPENSSL_VERSION_1_1 "OpenSSL 1.1."
41+
/* Per new OpenSSL naming convention starting from OpenSSL 3, all major versions are ABI and API compatible. */
42+
#define OPENSSL_VERSION_3_X "OpenSSL 3."
43+
4144
/* needed for OpenSSL 1.0.2 Thread handling routines */
4245
#define CRYPTO_LOCK 1
4346

@@ -47,12 +50,12 @@
4750
# include <pthread.h>
4851
#endif /* defined(WINDOWS) */
4952

50-
/* Header for RSA algorithm using 1.0.2 OpenSSL */
53+
/* Header for RSA algorithm using 1.0.2 OpenSSL. */
5154
int OSSL102_RSA_set0_key(RSA *, BIGNUM *, BIGNUM *, BIGNUM *);
5255
int OSSL102_RSA_set0_factors(RSA *, BIGNUM *, BIGNUM *);
5356
int OSSL102_RSA_set0_crt_params(RSA *, BIGNUM *, BIGNUM *, BIGNUM *);
5457

55-
/* Define literals from OpenSSL 1.1.x so that it compiles with OpenSSL 1.0.x */
58+
/* Define literals from OpenSSL 1.1.x so that it compiles with OpenSSL 1.0.x. */
5659
#ifndef EVP_CTRL_AEAD_GET_TAG
5760
#define EVP_CTRL_AEAD_GET_TAG EVP_CTRL_GCM_GET_TAG
5861
#endif
@@ -65,7 +68,7 @@ int OSSL102_RSA_set0_crt_params(RSA *, BIGNUM *, BIGNUM *, BIGNUM *);
6568
#define EVP_CTRL_AEAD_SET_TAG EVP_CTRL_GCM_SET_TAG
6669
#endif
6770

68-
/* Type definitions of function pointers */
71+
/* Type definitions of function pointers. */
6972
typedef char * OSSL_error_string_n_t(unsigned long, char *, size_t);
7073
typedef char * OSSL_error_string_t(unsigned long, char *);
7174
typedef unsigned long OSSL_get_error_t();
@@ -123,7 +126,7 @@ OSSL_error_string_n_t* OSSL_error_string_n;
123126
OSSL_error_string_t* OSSL_error_string;
124127
OSSL_get_error_t* OSSL_get_error;
125128

126-
/* Define pointers for OpenSSL 1.0.2 threading routines */
129+
/* Define pointers for OpenSSL 1.0.2 threading routines. */
127130
static OSSL_CRYPTO_num_locks_t* OSSL_CRYPTO_num_locks = NULL;
128131
static OSSL_CRYPTO_THREADID_set_numeric_t* OSSL_CRYPTO_THREADID_set_numeric = NULL;
129132
static OSSL_OPENSSL_malloc_t* OSSL_OPENSSL_malloc = NULL;
@@ -181,13 +184,13 @@ OSSL_BN_free_t* OSSL_BN_free;
181184
OSSL_cipher_t* OSSL_chacha20;
182185
OSSL_cipher_t* OSSL_chacha20_poly1305;
183186

184-
/* Structure for OpenSSL Digest context */
187+
/* Structure for OpenSSL Digest context. */
185188
typedef struct OpenSSLMDContext {
186189
EVP_MD_CTX *ctx;
187190
const EVP_MD *digestAlg;
188191
} OpenSSLMDContext;
189192

190-
/* Handle errors from OpenSSL calls */
193+
/* Handle errors from OpenSSL calls. */
191194
static void printErrors(void)
192195
{
193196
unsigned long errCode = 0;
@@ -219,19 +222,20 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_loadCrypto
219222
int ossl_ver;
220223

221224
/* Load OpenSSL Crypto library */
222-
crypto_library = load_crypto_library();
225+
crypto_library = load_crypto_library(trace);
223226
if (NULL == crypto_library) {
224227
if (trace) {
225228
fprintf(stderr, "Error loading OpenSSL: FAILED TO LOAD OPENSSL CRYPTO LIBRARY\n");
229+
fflush(stderr);
226230
}
227231
return -1;
228232
}
229233

230234
/*
231-
* Different symbols are used by OpenSSL with 1.0 and 1.1.
232-
* The symbol 'OpenSSL_version' is used by OpenSSL 1.1 where as
235+
* Different symbols are used by OpenSSL with 1.0 and 1.1 and later.
236+
* The symbol 'OpenSSL_version' is used by OpenSSL 1.1 and later where as
233237
* the symbol "SSLeay_version" is used by OpenSSL 1.0.
234-
* Currently only openssl 1.0.x and 1.1.x are supported.
238+
* Currently only openssl 1.0.x, 1.1.x and 3.x.x are supported.
235239
*/
236240
OSSL_version = (OSSL_version_t*)find_crypto_symbol(crypto_library, "OpenSSL_version");
237241

@@ -241,6 +245,7 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_loadCrypto
241245
if (NULL == OSSL_version) {
242246
if (trace) {
243247
fprintf(stderr, "Error loading OpenSSL: Error finding the OpenSSL version symbol in the crypto library\n");
248+
fflush(stderr);
244249
}
245250
unload_crypto_library(crypto_library);
246251
crypto_library = NULL;
@@ -251,6 +256,7 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_loadCrypto
251256
if (0 != strncmp(openssl_version, OPENSSL_VERSION_1_0, strlen(OPENSSL_VERSION_1_0))) {
252257
if (trace) {
253258
fprintf(stderr, "Error loading OpenSSL: Incompatible OpenSSL version found: %s\n", openssl_version);
259+
fflush(stderr);
254260
}
255261
unload_crypto_library(crypto_library);
256262
crypto_library = NULL;
@@ -260,10 +266,13 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_loadCrypto
260266
}
261267
} else {
262268
openssl_version = (*OSSL_version)(0); /* get OPENSSL_VERSION */
263-
/* Ensure the OpenSSL version is "OpenSSL 1.1.x". */
264-
if (0 != strncmp(openssl_version, OPENSSL_VERSION_1_1, strlen(OPENSSL_VERSION_1_1))) {
269+
/* Ensure the OpenSSL version is "OpenSSL 1.1.x" or "OpenSSL 3.x.x". */
270+
if ((0 != strncmp(openssl_version, OPENSSL_VERSION_1_1, strlen(OPENSSL_VERSION_1_1)))
271+
&& (0 != strncmp(openssl_version, OPENSSL_VERSION_3_X, strlen(OPENSSL_VERSION_3_X)))
272+
) {
265273
if (trace) {
266274
fprintf(stderr, "Error loading OpenSSL: Incompatible OpenSSL version found: %s\n", openssl_version);
275+
fflush(stderr);
267276
}
268277
unload_crypto_library(crypto_library);
269278
crypto_library = NULL;

closed/src/java.base/share/native/libjncrypto/NativeCrypto_md.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* ===========================================================================
3-
* (c) Copyright IBM Corp. 2019, 2019 All Rights Reserved
3+
* (c) Copyright IBM Corp. 2019, 2022 All Rights Reserved
44
* ===========================================================================
55
*
66
* This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,9 @@
2525
#ifndef NATIVECRYPTO_MD_H
2626
#define NATIVECRYPTO_MD_H
2727

28-
void * load_crypto_library();
28+
#include <jni.h>
29+
30+
void * load_crypto_library(jboolean traceEnabled);
2931
void unload_crypto_library(void *handle);
3032
void * find_crypto_symbol(void *handle, const char *symname);
3133
void get_library_path(void * handle, char * library_path);

closed/src/java.base/unix/native/libjncrypto/NativeCrypto_md.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* ===========================================================================
3-
* (c) Copyright IBM Corp. 2019, 2019 All Rights Reserved
3+
* (c) Copyright IBM Corp. 2019, 2022 All Rights Reserved
44
* ===========================================================================
55
*
66
* This code is free software; you can redistribute it and/or modify it
@@ -22,6 +22,7 @@
2222
* ===========================================================================
2323
*/
2424

25+
#include <link.h>
2526
#include <stdio.h>
2627
#include <stdlib.h>
2728
#include <string.h>
@@ -30,12 +31,14 @@
3031
#include "NativeCrypto_md.h"
3132

3233
/* Load the crypto library (return NULL on error) */
33-
void * load_crypto_library() {
34+
void * load_crypto_library(jboolean traceEnabled)
35+
{
3436
void * result = NULL;
3537
size_t i = 0;
3638

37-
// Library names for OpenSSL 1.1.1, 1.1.0, 1.0.2 and symbolic links
39+
// Library names for OpenSSL 3.x, 1.1.1, 1.1.0, 1.0.2 and symbolic links
3840
static const char * const libNames[] = {
41+
"libcrypto.so.3", // 3.x library name
3942
"libcrypto.so.1.1", // 1.1.x library name
4043
"libcrypto.so.1.0.0", // 1.0.x library name
4144
"libcrypto.so.10", // 1.0.x library name on RHEL
@@ -50,6 +53,12 @@ void * load_crypto_library() {
5053
result = dlopen (libName, RTLD_NOW);
5154
}
5255

56+
if (traceEnabled && (NULL != result)) {
57+
struct link_map *map = NULL;
58+
dlinfo(result, RTLD_DI_LINKMAP, &map);
59+
fprintf(stderr, "Attempt to load OpenSSL %s\n", map->l_name);
60+
fflush(stderr);
61+
}
5362
return result;
5463
}
5564

closed/src/java.base/windows/native/libjncrypto/NativeCrypto_md.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* ===========================================================================
3-
* (c) Copyright IBM Corp. 2019, 2019 All Rights Reserved
3+
* (c) Copyright IBM Corp. 2019, 2022 All Rights Reserved
44
* ===========================================================================
55
*
66
* This code is free software; you can redistribute it and/or modify it
@@ -27,7 +27,7 @@
2727
#include "NativeCrypto_md.h"
2828

2929
/* Load the crypto library (return NULL on error) */
30-
void * load_crypto_library() {
30+
void * load_crypto_library(jboolean traceEnabled) {
3131
void * result = NULL;
3232
const char *libname = "libcrypto-1_1-x64.dll";
3333
const char *oldname = "libeay32.dll";

test/jdk/com/sun/crypto/provider/Cipher/AEAD/GCMParameterSpecTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
* questions.
2222
*/
2323

24+
/*
25+
* ===========================================================================
26+
* (c) Copyright IBM Corp. 2022, 2022 All Rights Reserved
27+
* ===========================================================================
28+
*/
29+
2430
import java.security.NoSuchAlgorithmException;
2531
import java.security.NoSuchProviderException;
2632
import java.util.Arrays;
@@ -36,7 +42,15 @@
3642
*/
3743
public class GCMParameterSpecTest {
3844

39-
private static final int[] IV_LENGTHS = { 96, 8, 1024 };
45+
/*
46+
* OpenSSL3 only supports IV lengths up to 16 bytes.
47+
* When the IV length is set to be larger than 16 bytes, an error is thrown.
48+
* According to the OpenSSL docs([1]), in OpenSSL1.1.1 and older, there is
49+
* no error thrown but unpredictable behavior will happen for large IV sizes.
50+
*
51+
* [1] https://www.openssl.org/docs/man1.1.1/man3/EVP_CIPHER_CTX_block_size.html
52+
*/
53+
private static final int[] IV_LENGTHS = { 96, 8 };
4054
private static final int[] KEY_LENGTHS = { 128, 192, 256 };
4155
private static final int[] DATA_LENGTHS = { 0, 128, 1024 };
4256
private static final int[] AAD_LENGTHS = { 0, 128, 1024 };

0 commit comments

Comments
 (0)