OpenSslNativeJna.java

  1.  /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *     http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  */

  18. package org.apache.commons.crypto.jna;

  19. import java.nio.ByteBuffer;

  20. import org.apache.commons.crypto.Crypto;

  21. import com.sun.jna.Function;
  22. import com.sun.jna.NativeLibrary;
  23. import com.sun.jna.NativeLong;
  24. import com.sun.jna.ptr.PointerByReference;

  25. final class OpenSslNativeJna {

  26.     static final int OPENSSL_INIT_ENGINE_RDRAND = 0x00000200;

  27.     static final int OOSL_JNA_ENCRYPT_MODE = 1;
  28.     static final int OOSL_JNA_DECRYPT_MODE = 0;

  29.     static final boolean INIT_OK;

  30.     static final Throwable INIT_ERROR;

  31.     /** Full version from JNA call. */
  32.     static final long VERSION;

  33.     /** Major Minor version from JNA call, without the maintenance level. */
  34.     static final long VERSION_X_Y;

  35.     static final long VERSION_1_0_X = 0x10000000;
  36.     static final long VERSION_1_1_X = 0x10100000;
  37.     static final long VERSION_2_0_X = 0x20000000;
  38.     static final long VERSION_3_0_X = 0x30000000;

  39.     private static final OpenSslInterfaceNativeJna JnaImplementation;

  40.     static {
  41.         OpenSslJna.debug("OpenSslNativeJna static init start");
  42.         final String libraryName = System.getProperty(Crypto.CONF_PREFIX + OpenSslNativeJna.class.getSimpleName(), "crypto");
  43.         OpenSslJna.debug("OpenSslNativeJna NativeLibrary.getInstance('%s')", libraryName);
  44.         @SuppressWarnings("resource") // NativeLibrary.getInstance returns a singleton
  45.         final NativeLibrary crypto = NativeLibrary.getInstance(libraryName);
  46.         OpenSslJna.debug("OpenSslNativeJna NativeLibrary.getInstance('%s') -> %s", libraryName, crypto);
  47.         Function versionFunction = null;
  48.         try {
  49.             versionFunction = crypto.getFunction("SSLeay");
  50.         } catch (final UnsatisfiedLinkError e) {
  51.             versionFunction = crypto.getFunction("OpenSSL_version_num");
  52.         }
  53.         // Must find one of the above two functions; else give up

  54.         VERSION = versionFunction.invokeLong(new Object[]{});
  55.         OpenSslJna.debug(String.format("OpenSslNativeJna detected version 0x%x", VERSION));

  56.         VERSION_X_Y = VERSION & 0xffff0000; // keep only major.minor
  57.         if (VERSION_X_Y == VERSION_1_0_X) {
  58.             OpenSslJna.debug("Creating OpenSsl10XNativeJna");
  59.             JnaImplementation = new OpenSsl10XNativeJna();
  60.         } else if (VERSION_X_Y == VERSION_1_1_X) {
  61.             OpenSslJna.debug("Creating OpenSsl11XNativeJna");
  62.             JnaImplementation = new OpenSsl11XNativeJna();
  63.         } else if (VERSION_X_Y == VERSION_2_0_X) {
  64.             OpenSslJna.debug("Creating OpenSsl20XNativeJna");
  65.             JnaImplementation = new OpenSsl20XNativeJna();
  66. //        } else if (VERSION_X_Y == VERSION_3_0_X) {
  67. //            OpenSslJna.debug("Creating OpenSsl30XNativeJna");
  68. //            JnaImplementation = new OpenSsl30XNativeJna();
  69.         } else {
  70.             // TODO: Throw error?
  71.             OpenSslJna.debug("Creating OpenSsl10XNativeJna");
  72.             JnaImplementation = new OpenSsl10XNativeJna();
  73.         }

  74.         INIT_OK = JnaImplementation._INIT_OK();

  75.         INIT_ERROR = INIT_OK ? null : JnaImplementation._INIT_ERROR();
  76.         OpenSslJna.debug("OpenSslNativeJna INIT_OK = %s, INIT_ERROR = '%s', JnaImplementation = %s", INIT_OK, INIT_ERROR, JnaImplementation.getClass());
  77.         OpenSslJna.debug("OpenSslNativeJna static init end");
  78.     }

  79.     public static PointerByReference ENGINE_by_id(final String string) {
  80.         return JnaImplementation._ENGINE_by_id(string);
  81.     }

  82.     public static int ENGINE_cleanup() {
  83.         return JnaImplementation._ENGINE_cleanup();
  84.     }

  85.     public static int ENGINE_finish(final PointerByReference rdrandEngine) {
  86.         return JnaImplementation._ENGINE_finish(rdrandEngine);
  87.     }

  88.     public static int ENGINE_free(final PointerByReference rdrandEngine) {
  89.         return JnaImplementation._ENGINE_free(rdrandEngine);
  90.     }

  91.     public static int ENGINE_init(final PointerByReference rdrandEngine) {
  92.         return JnaImplementation._ENGINE_init(rdrandEngine);
  93.     }

  94.     public static void ENGINE_load_rdrand() {
  95.         JnaImplementation._ENGINE_load_rdrand();
  96.     }

  97.     public static int ENGINE_set_default(final PointerByReference rdrandEngine, final int eNGINE_METHOD_RAND) {
  98.         return JnaImplementation._ENGINE_set_default(rdrandEngine, eNGINE_METHOD_RAND);
  99.     }

  100.     public static String ERR_error_string(final NativeLong err, final char[] object) {
  101.         return JnaImplementation._ERR_error_string(err, null);
  102.     }

  103.     public static NativeLong ERR_peek_error() {
  104.         return JnaImplementation._ERR_peek_error();
  105.     }

  106.     public static PointerByReference EVP_aes_128_cbc() {
  107.         return JnaImplementation._EVP_aes_128_cbc();
  108.     }

  109.     public static PointerByReference EVP_aes_128_ctr() {
  110.         return JnaImplementation._EVP_aes_128_ctr();
  111.     }

  112.     public static PointerByReference EVP_aes_192_cbc() {
  113.         return JnaImplementation._EVP_aes_192_cbc();
  114.     }

  115.     public static PointerByReference EVP_aes_192_ctr() {
  116.         return JnaImplementation._EVP_aes_192_ctr();
  117.     }

  118.     public static PointerByReference EVP_aes_256_cbc() {
  119.         return JnaImplementation._EVP_aes_256_cbc();
  120.     }

  121.     public static PointerByReference EVP_aes_256_ctr() {
  122.         return JnaImplementation._EVP_aes_256_ctr();
  123.     }

  124.     public static void EVP_CIPHER_CTX_cleanup(final PointerByReference context) {
  125.         JnaImplementation._EVP_CIPHER_CTX_cleanup(context);
  126.     }

  127.     public static void EVP_CIPHER_CTX_free(final PointerByReference context) {
  128.         JnaImplementation._EVP_CIPHER_CTX_free(context);
  129.     }

  130.     public static PointerByReference EVP_CIPHER_CTX_new() {
  131.         return JnaImplementation._EVP_CIPHER_CTX_new();
  132.     }

  133.     public static int EVP_CIPHER_CTX_set_padding(final PointerByReference context, final int padding) {
  134.         return JnaImplementation._EVP_CIPHER_CTX_set_padding(context, padding);
  135.     }

  136.     public static int EVP_CipherFinal_ex(final PointerByReference context, final ByteBuffer outBuffer,
  137.             final int[] outlen) {
  138.         return JnaImplementation._EVP_CipherFinal_ex(context, outBuffer, outlen);
  139.     }

  140.     public static int EVP_CipherInit_ex(final PointerByReference context, final PointerByReference algo,
  141.             final Object object, final byte[] encoded, final byte[] iv, final int cipherMode) {
  142.         return JnaImplementation._EVP_CipherInit_ex(context, algo, null, encoded, iv, cipherMode);
  143.     }

  144.     public static int EVP_CipherUpdate(final PointerByReference context, final ByteBuffer outBuffer,
  145.             final int[] outlen, final ByteBuffer inBuffer, final int remaining) {
  146.         return JnaImplementation._EVP_CipherUpdate(context, outBuffer, outlen, inBuffer, remaining);
  147.     }

  148.     public static String OpenSSLVersion(final int i) {
  149.         return JnaImplementation._OpenSSL_version(i);
  150.     }

  151.     public static int RAND_bytes(final ByteBuffer buf, final int length) {
  152.         return JnaImplementation._RAND_bytes(buf, length);
  153.     }

  154.     public static PointerByReference RAND_get_rand_method() {
  155.         return JnaImplementation._RAND_get_rand_method();
  156.     }

  157.     public static PointerByReference RAND_SSLeay() {
  158.         return JnaImplementation._RAND_SSLeay();
  159.     }

  160.     private OpenSslNativeJna() {
  161.     }
  162. }