OpenSslJna.java

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

  19. import java.util.Objects;

  20. import org.apache.commons.crypto.Crypto;
  21. import org.apache.commons.crypto.cipher.CryptoCipher;
  22. import org.apache.commons.crypto.random.CryptoRandom;

  23. /**
  24.  * Provides access to package protected class objects and a {@link #main(String[])} method that prints version information.
  25.  */
  26. public final class OpenSslJna {

  27.     private final static String KEY_DEBUG = Crypto.CONF_PREFIX + "debug";

  28.     /**
  29.      * Logs debug messages.
  30.      *
  31.      * @param format See {@link String#format(String, Object...)}.
  32.      * @param args   See {@link String#format(String, Object...)}.
  33.      */
  34.     static void debug(final Object format, final Object... args) {
  35.         // TODO Find a better way to do this later.
  36.         if (Boolean.getBoolean(KEY_DEBUG)) {
  37.             System.out.println(String.format(Objects.toString(format), args));
  38.         }
  39.     }

  40.     /**
  41.      * @return The cipher class of JNA implementation
  42.      */
  43.     public static Class<? extends CryptoCipher> getCipherClass() {
  44.         return OpenSslJnaCipher.class;
  45.     }

  46.     /**
  47.      * @return The random class of JNA implementation
  48.      */
  49.     public static Class<? extends CryptoRandom> getRandomClass() {
  50.         return OpenSslJnaCryptoRandom.class;
  51.     }

  52.     /**
  53.      * Logs info-level messages.
  54.      *
  55.      * @param format See {@link String#format(String, Object...)}.
  56.      * @param args   See {@link String#format(String, Object...)}.
  57.      */
  58.     private static void info(final String format, final Object... args) {
  59.         // TODO Find a better way to do this later.
  60.         System.out.println(String.format(format, args));
  61.     }

  62.     /**
  63.      * @return the error of JNA
  64.      */
  65.     public static Throwable initialisationError() {
  66.         return OpenSslNativeJna.INIT_ERROR;
  67.     }

  68.     /**
  69.      * @return true if JNA native loads successfully
  70.      */
  71.     public static boolean isEnabled() {
  72.         return OpenSslNativeJna.INIT_OK;
  73.     }

  74.     public static void main(final String[] args) throws Throwable {
  75.         info(Crypto.getComponentName() + " OpenSslJna: enabled = %s, version = 0x%08X", isEnabled(), OpenSslNativeJna.VERSION);
  76.         final Throwable initialisationError = initialisationError();
  77.         if (initialisationError != null) {
  78.             info("initialisationError(): %s", initialisationError);
  79.             System.err.flush(); // helpful for stack traces to not mix in other output.
  80.             throw initialisationError; // propagate to make error obvious
  81.         }
  82.         for (int i = 0; i <= 5; i++) {
  83.             info("OpenSSLVersion(%d): %s", i, OpenSSLVersion(i));
  84.         }
  85.     }

  86.     /**
  87.      * Retrieves version/build information about OpenSSL library.
  88.      *
  89.      * @param type type can be OPENSSL_VERSION, OPENSSL_CFLAGS, OPENSSL_BUILT_ON...
  90.      * @return A pointer to a constant string describing the version of the
  91.      * OpenSSL library or giving information about the library build.
  92.      */
  93.     static String OpenSSLVersion(final int type) {
  94.          return OpenSslNativeJna.OpenSSLVersion(type);
  95.     }
  96. }