Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRYPTO-162 openSslCipher support engine. #165

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public class CryptoCipherFactory {
*/
public static final String JCE_PROVIDER_KEY = Crypto.CONF_PREFIX
+ "cipher.jce.provider";

public static final String CIPHER_ENGINE_KEY = Crypto.CONF_PREFIX
+ "cipher.engine";

/**
* The configuration key of the CryptoCipher implementation class.
* <p>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/apache/commons/crypto/cipher/OpenSsl.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,7 @@ protected void finalize() throws Throwable {
clean();
}

public void engineSetDefaultCiphers(String engineId){
Copy link
Member

Choose a reason for hiding this comment

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

Missing Javadoc, fix formatting.

Copy link
Author

Choose a reason for hiding this comment

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

ok

opensslBlockCipher.engineSetDefaultCiphers(engineId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public OpenSslCipher(final Properties props, final String transformation) // NOP
}

openSslEngine = OpenSsl.getInstance(transformation);

String engineId = props.getProperty(CryptoCipherFactory.CIPHER_ENGINE_KEY);
if(engineId != null && !engineId.isEmpty()) {
Copy link
Member

Choose a reason for hiding this comment

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

Fix formatting.

Copy link
Author

Choose a reason for hiding this comment

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

ok

openSslEngine.engineSetDefaultCiphers(engineId);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,9 @@ public void updateAAD(final byte[] aad) {
"The underlying Cipher implementation "
+ "does not support this method");
}

@Override
public void engineSetDefaultCiphers(String engineId) {
OpenSslNative.engineSetDefaultCiphers(engineId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ abstract int doFinal(ByteBuffer input, ByteBuffer output) throws ShortBufferExce

abstract void updateAAD(byte[] aad);

abstract void engineSetDefaultCiphers(String engineId);

public void clean() {
if (context != 0) {
OpenSslNative.clean(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ public void updateAAD(final byte[] aad) {
}
}

@Override
public void engineSetDefaultCiphers(String engineId) {
OpenSslNative.engineSetDefaultCiphers(engineId);
}

private void processAAD() {
if (aadBuffer != null && aadBuffer.size() > 0) {
OpenSslNative.updateByteArray(context, aadBuffer.toByteArray(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,6 @@ public native static int doFinalByteArray(long context, byte[] output,
* @param context The cipher context address
*/
public native static void clean(long context);

sebbASF marked this conversation as resolved.
Show resolved Hide resolved
public native static void engineSetDefaultCiphers(String engineId);
}
39 changes: 39 additions & 0 deletions src/main/native/org/apache/commons/crypto/cipher/OpenSslNative.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ static EVP_CIPHER * (*dlsym_EVP_aes_128_cbc)(void);
static EVP_CIPHER * (*dlsym_EVP_aes_256_gcm)(void);
static EVP_CIPHER * (*dlsym_EVP_aes_192_gcm)(void);
static EVP_CIPHER * (*dlsym_EVP_aes_128_gcm)(void);
static ENGINE * (*dlsym_ENGINE_by_id) (const char *);
static int (*dlsym_ENGINE_init) (ENGINE *);
static int (*dlsym_ENGINE_finish) (ENGINE *);
static int (*dlsym_ENGINE_free) (ENGINE *);
static int (*dlsym_ENGINE_set_default_ciphers) (ENGINE *);
#endif

#ifdef WINDOWS
Expand Down Expand Up @@ -181,6 +186,11 @@ JNIEXPORT void JNICALL Java_org_apache_commons_crypto_cipher_OpenSslNative_initI
LOAD_DYNAMIC_SYMBOL(dlsym_EVP_CipherInit_ex, env, openssl, "EVP_CipherInit_ex");
LOAD_DYNAMIC_SYMBOL(dlsym_EVP_CipherUpdate, env, openssl, "EVP_CipherUpdate");
LOAD_DYNAMIC_SYMBOL(dlsym_EVP_CipherFinal_ex, env, openssl, "EVP_CipherFinal_ex");
LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_by_id, env, openssl, "ENGINE_by_id");
LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_init, env, openssl, "ENGINE_init");
LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_finish, env, openssl, "ENGINE_finish");
LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_free, env, openssl, "ENGINE_free");
LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_set_default_ciphers, env, openssl, "ENGINE_set_default_ciphers");
#endif

#ifdef WINDOWS
Expand Down Expand Up @@ -688,6 +698,35 @@ JNIEXPORT void JNICALL Java_org_apache_commons_crypto_cipher_OpenSslNative_clean
free_context_wrapper(wrapper);
}

JNIEXPORT void JNICALL Java_org_apache_commons_crypto_cipher_OpenSslNative_engineSetDefaultCiphers
(JNIEnv *env, jclass clazz, jstring engineId)
{
const char* eId = (*env)->GetStringUTFChars(env, engineId, 0);
ENGINE *e = dlsym_ENGINE_by_id(eId);

if (!e) {
char msg[64] = {0};
snprintf(msg, sizeof(msg), "Not found engine: %s", eId);
THROW(env, "java/lang/InternalError", msg);
return;
}
(*env)->ReleaseStringUTFChars(env, engineId, eId);

if (dlsym_ENGINE_init(e) != 1) {
dlsym_ENGINE_free(e);
THROW(env, "java/lang/InternalError", "Error in ENGINE_init.");
return;
}

if (dlsym_ENGINE_set_default_ciphers(e) != 1) {
THROW(env, "java/lang/InternalError", "Error in ENGINE_set_default_ciphers.");
return;
}
dlsym_ENGINE_finish(e);
dlsym_ENGINE_free(e);
return;
}

static int check_update_max_output_len(EVP_CTX_Wrapper *wrapper, int input_len, int max_output_len)
{
if (dlsym_EVP_CIPHER_CTX_test_flags(wrapper->ctx, EVP_CIPH_NO_PADDING) == EVP_CIPH_NO_PADDING) {
Expand Down