Skip to content

Support OpenSSL backend and implement message digest - #1492

Open
KostasTsiounis wants to merge 3 commits into
IBM:mainfrom
KostasTsiounis:openssl_backend
Open

Support OpenSSL backend and implement message digest#1492
KostasTsiounis wants to merge 3 commits into
IBM:mainfrom
KostasTsiounis:openssl_backend

Conversation

@KostasTsiounis

@KostasTsiounis KostasTsiounis commented May 22, 2026

Copy link
Copy Markdown
Member

With this change, all appropriate code is added to support the use of OpenSSL as a backend. That includes:

  • Main Java class to load the libraries required and offer native methods.
  • Adapter Java classes to smoothly integrate without changes with existing code.
  • Makefiles to compile and link required C code to use OpenSSL.
  • Additional test tags and general test setup to allow the execution of appropriate tests with the new backend.

Additionally, the first set of algorithms, namely the message digests, are implemented. The aforementioned tags are, also, added to the corresponding tests.

Signed-off-by: Kostas Tsiounis kostas.tsiounis@ibm.com

@johnpeck-us-ibm

Copy link
Copy Markdown
Member

I know we talked about contexts and one for FIPS and one for non-FIPS. Could we use the Property query instead?
sha256 = EVP_MD_fetch(NULL, "SHA2-256", "fips=yes"); - FIPS
sha256 = EVP_MD_fetch(NULL, "SHA2-256", "provider=default"); - Non-FIPS.
Note the NULL for the context.

Does using this have any performance ramifications?

@KostasTsiounis
KostasTsiounis force-pushed the openssl_backend branch 2 times, most recently from 1b76bd7 to 31add61 Compare July 7, 2026 20:28
@KostasTsiounis
KostasTsiounis force-pushed the openssl_backend branch 2 times, most recently from 8920c23 to 0e09aab Compare July 21, 2026 15:39
@jasonkatonica
jasonkatonica requested a review from thu-ibm July 21, 2026 18:14
@KostasTsiounis
KostasTsiounis marked this pull request as ready for review July 31, 2026 00:05
@SuppressWarnings("restricted")
protected static boolean loadIfExists(File libraryFile) {
String libraryName = libraryFile.getAbsolutePath();
System.out.println("Library name: " + libraryName);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to remove this System.out.println and the others or convert them to debugs.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left from debug. Removed.

}

public static NativeOCKAdapterNonFIPS getInstance() {
System.out.println("Using OCK non-FIPS adapter.");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left from debug. Removed.


@Override
public void validateLibraryLocation() throws ProviderException, OpenSSLException {
/*if (NativeOpenSSLImplementation.requirePreloadOSSL == false) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not need this to maybe help us determine where the OpenSSL DLL is coming from?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add a JNI method to do this, but the question is whether we need this given that it's not like OCK that is provided through a binary. In this case, the user provides their own OpenSSL library.

Comment thread src/main/native/openssl/Digest.c Outdated

if (!(digestAlgoChars = (const char *)(*env)->GetStringUTFChars(env, digestAlgo, NULL))) {
throwOSSLException(env, 0, "DIGEST_create: GetStringUTFChars() failed");
goto cleanup;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a return here instead of goto.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

}

// Everything succeeded. Set digestId to created EVP_MD_CTX.
digestId = (jlong)((intptr_t)mdCtx);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be (intptr_t) (intptr_t*) instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type intptr_t is supposed to hold pointers itself, so we don't need a intptr_t *.


try {
//long osslContextId = NativeOpenSSLImplementation.initializeOSSL(this.useFIPSMode);
long osslContextId = 0;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears in this case OpenSSL context is never initialized since the real call is commented out

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is because the OpenSSL context is not yet needed in the use case I'm introducing, given that we decided not to do FIPS at the moment. So, I only left this here for future use and is not needed now.

}

public static NativeOpenSSLAdapterNonFIPS getInstance() {
System.out.println("Using OpenSSL non-FIPS adapter.");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using System.out.println pollutes stdout in production

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, those were leftovers from debugging. Removing them.

* and must match those defined in native/openssl/ExceptionCodes.h.
*/
public static final int GKR_FIPS_MODE_INVALID = 0x00000001;
public static final int GKR_OCK_ATTACH_FAILED = 0x00000002;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error constants like GKR_OCK_ATTACH_FAILED are OCK-specific

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the 2 that are irrelevant and kept the two that are used in common parts. Code here can be reworked in a future PR to not use these constants at all.

import java.util.Hashtable;
import java.util.Map;

public class OpenSSLException extends NativeException {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NativeException appears to be an OCK-specific base class

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean here. NativeException is supposed to be a superclass for all exceptions coming from native code. So, native code that uses OCK throws an OCKException, whereas the new digest code that uses OpenSSL throws an OpenSSLException.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In NativeException.java, the naming in the following gives me the impression they are used for OCK.

public static final int GKR_FIPS_MODE_INVALID = -1;

    public static final int GKR_OCK_ATTACH_FAILED = -1;
    public static final int GKR_DECRYPT_FINAL_BAD_PADDING_ERROR = -1;
    public static final int GKR_UNSPECIFIED = -1;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I can see why. But I only kept them for the reasons mentioned in #1492 (comment). We'll remove them in the future.

#include "Utils.h"
#include <string.h>

int debug = 0; // FIXME

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All gslogMessage calls always print to stderr regardless of the flag value

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is following the same pattern that all OCK native code has. Do you think we should change this to have different levels of logging? We could open an issue to do this everywhere if we want.

Comment thread src/test/java/ibm/jceplus/junit/tests/TestArguments.java
With this change, all appropriate code is added to support
the use of OpenSSL as a backend. That includes:
- Main Java class to load the libraries required and offer
  native methods.
- Adapter Java classes to smoothly integrate without changes
  with existing code.
- Makefiles to compile and link required C code to use
  OpenSSL.
- Additional test tags and general test setup to allow the
  execution of appropriate tests with the new backend.

Additionally, the first set of algorithms, namely the
message digests, are implemented. The aforementioned
tags are, also, added to the corresponding tests.

Signed-off-by: Kostas Tsiounis <kostas.tsiounis@ibm.com>
@KostasTsiounis KostasTsiounis changed the title Openssl backend Support OpenSSL backend and implement message digest Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants