001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *   http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 */
018package org.apache.commons.crypto.jna;
019
020import java.util.Objects;
021
022import org.apache.commons.crypto.Crypto;
023import org.apache.commons.crypto.cipher.CryptoCipher;
024import org.apache.commons.crypto.random.CryptoRandom;
025
026/**
027 * Provides access to package protected class objects and a {@link #main(String[])} method that prints version information.
028 */
029public final class OpenSslJna {
030
031    private final static String KEY_DEBUG = Crypto.CONF_PREFIX + "debug";
032
033    /**
034     * Logs debug messages.
035     *
036     * @param format See {@link String#format(String, Object...)}.
037     * @param args   See {@link String#format(String, Object...)}.
038     */
039    static void debug(final Object format, final Object... args) {
040        // TODO Find a better way to do this later.
041        if (Boolean.getBoolean(KEY_DEBUG)) {
042            System.out.println(String.format(Objects.toString(format), args));
043        }
044    }
045
046    /**
047     * @return The cipher class of JNA implementation
048     */
049    public static Class<? extends CryptoCipher> getCipherClass() {
050        return OpenSslJnaCipher.class;
051    }
052
053    /**
054     * @return The random class of JNA implementation
055     */
056    public static Class<? extends CryptoRandom> getRandomClass() {
057        return OpenSslJnaCryptoRandom.class;
058    }
059
060    /**
061     * Logs info-level messages.
062     *
063     * @param format See {@link String#format(String, Object...)}.
064     * @param args   See {@link String#format(String, Object...)}.
065     */
066    private static void info(final String format, final Object... args) {
067        // TODO Find a better way to do this later.
068        System.out.println(String.format(format, args));
069    }
070
071    /**
072     * @return the error of JNA
073     */
074    public static Throwable initialisationError() {
075        return OpenSslNativeJna.INIT_ERROR;
076    }
077
078    /**
079     * @return true if JNA native loads successfully
080     */
081    public static boolean isEnabled() {
082        return OpenSslNativeJna.INIT_OK;
083    }
084
085    public static void main(final String[] args) throws Throwable {
086        info(Crypto.getComponentName() + " OpenSslJna: enabled = %s, version = 0x%08X", isEnabled(), OpenSslNativeJna.VERSION);
087        final Throwable initialisationError = initialisationError();
088        if (initialisationError != null) {
089            info("initialisationError(): %s", initialisationError);
090            System.err.flush(); // helpful for stack traces to not mix in other output.
091            throw initialisationError; // propagate to make error obvious
092        }
093        for (int i = 0; i <= 5; i++) {
094            info("OpenSSLVersion(%d): %s", i, OpenSSLVersion(i));
095        }
096    }
097
098    /**
099     * Retrieves version/build information about OpenSSL library.
100     *
101     * @param type type can be OPENSSL_VERSION, OPENSSL_CFLAGS, OPENSSL_BUILT_ON...
102     * @return A pointer to a constant string describing the version of the
103     * OpenSSL library or giving information about the library build.
104     */
105    static String OpenSSLVersion(final int type) {
106         return OpenSslNativeJna.OpenSSLVersion(type);
107    }
108}