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
20 import java.util.Objects;
21
22 import org.apache.commons.crypto.Crypto;
23 import org.apache.commons.crypto.cipher.CryptoCipher;
24 import org.apache.commons.crypto.random.CryptoRandom;
25
26 /**
27 * Provides access to package protected class objects and a {@link #main(String[])} method that prints version information.
28 */
29 public final class OpenSslJna {
30
31 private final static String KEY_DEBUG = Crypto.CONF_PREFIX + "debug";
32
33 /**
34 * Logs debug messages.
35 *
36 * @param format See {@link String#format(String, Object...)}.
37 * @param args See {@link String#format(String, Object...)}.
38 */
39 static void debug(final Object format, final Object... args) {
40 // TODO Find a better way to do this later.
41 if (Boolean.getBoolean(KEY_DEBUG)) {
42 System.out.println(String.format(Objects.toString(format), args));
43 }
44 }
45
46 /**
47 * @return The cipher class of JNA implementation
48 */
49 public static Class<? extends CryptoCipher> getCipherClass() {
50 return OpenSslJnaCipher.class;
51 }
52
53 /**
54 * @return The random class of JNA implementation
55 */
56 public static Class<? extends CryptoRandom> getRandomClass() {
57 return OpenSslJnaCryptoRandom.class;
58 }
59
60 /**
61 * Logs info-level messages.
62 *
63 * @param format See {@link String#format(String, Object...)}.
64 * @param args See {@link String#format(String, Object...)}.
65 */
66 private static void info(final String format, final Object... args) {
67 // TODO Find a better way to do this later.
68 System.out.println(String.format(format, args));
69 }
70
71 /**
72 * @return the error of JNA
73 */
74 public static Throwable initialisationError() {
75 return OpenSslNativeJna.INIT_ERROR;
76 }
77
78 /**
79 * @return true if JNA native loads successfully
80 */
81 public static boolean isEnabled() {
82 return OpenSslNativeJna.INIT_OK;
83 }
84
85 public static void main(final String[] args) throws Throwable {
86 info(Crypto.getComponentName() + " OpenSslJna: enabled = %s, version = 0x%08X", isEnabled(), OpenSslNativeJna.VERSION);
87 final Throwable initialisationError = initialisationError();
88 if (initialisationError != null) {
89 info("initialisationError(): %s", initialisationError);
90 System.err.flush(); // helpful for stack traces to not mix in other output.
91 throw initialisationError; // propagate to make error obvious
92 }
93 for (int i = 0; i <= 5; i++) {
94 info("OpenSSLVersion(%d): %s", i, OpenSSLVersion(i));
95 }
96 }
97
98 /**
99 * 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 }