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 org.apache.commons.crypto.Crypto; 021import org.apache.commons.crypto.cipher.CryptoCipher; 022import org.apache.commons.crypto.random.CryptoRandom; 023 024/** 025 * Public class to give access to the package protected class objects 026 */ 027public final class OpenSslJna { 028 029 /** 030 * Logs debug messages. 031 * 032 * @param format See {@link String#format(String, Object...)}. 033 * @param args See {@link String#format(String, Object...)}. 034 */ 035 static void debug(final String format, final Object... args) { 036 // TODO Find a better way to do this later. 037 if (Boolean.getBoolean(Crypto.CONF_PREFIX + "debug")) { 038 System.out.println(String.format(format, args)); 039 } 040 } 041 042 /** 043 * @return The cipher class of JNA implementation 044 */ 045 public static Class<? extends CryptoCipher> getCipherClass() { 046 return OpenSslJnaCipher.class; 047 } 048 049 /** 050 * @return The random class of JNA implementation 051 */ 052 public static Class<? extends CryptoRandom> getRandomClass() { 053 return OpenSslJnaCryptoRandom.class; 054 } 055 056 /** 057 * Logs info-level messages. 058 * 059 * @param format See {@link String#format(String, Object...)}. 060 * @param args See {@link String#format(String, Object...)}. 061 */ 062 private static void info(final String format, final Object... args) { 063 // TODO Find a better way to do this later. 064 System.out.println(String.format(format, args)); 065 } 066 067 /** 068 * @return the error of JNA 069 */ 070 public static Throwable initialisationError() { 071 return OpenSslNativeJna.INIT_ERROR; 072 } 073 074 /** 075 * @return true if JNA native loads successfully 076 */ 077 public static boolean isEnabled() { 078 return OpenSslNativeJna.INIT_OK; 079 } 080 081 public static void main(final String[] args) { 082 info("isEnabled(): %s", isEnabled()); 083 final Throwable initialisationError = initialisationError(); 084 info("initialisationError(): %s", initialisationError); 085 if (initialisationError != null) { 086 System.err.flush(); // helpful for stack traces to not mix in other output. 087 initialisationError.printStackTrace(); 088 } 089 } 090 091 /** 092 * Retrieves version/build information about OpenSSL library. 093 * 094 * @param type type can be OPENSSL_VERSION, OPENSSL_CFLAGS, OPENSSL_BUILT_ON... 095 * @return A pointer to a constant string describing the version of the 096 * OpenSSL library or giving information about the library build. 097 */ 098 static String OpenSSLVersion(final int type) { 099 return OpenSslNativeJna.OpenSSLVersion(type); 100 } 101}