View Javadoc
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  
19  package org.apache.commons.crypto.jna;
20  
21  import java.nio.ByteBuffer;
22  
23  import com.sun.jna.Native;
24  import com.sun.jna.NativeLong;
25  import com.sun.jna.ptr.PointerByReference;
26  
27  class OpenSslNativeJna {
28  
29      static final int OPENSSL_INIT_ENGINE_RDRAND = 0x00000200;
30  
31      static final int OOSL_JNA_ENCRYPT_MODE = 1;
32      static final int OOSL_JNA_DECRYPT_MODE = 0;
33  
34      static final boolean INIT_OK;
35  
36      static final Throwable INIT_ERROR;
37  
38      static {
39          boolean ok = false;
40          Throwable thrown = null;
41          try {
42              Native.register("crypto");
43              ERR_load_crypto_strings();
44              ok = true;
45          } catch (Exception e) {
46              thrown = e;
47          } catch (UnsatisfiedLinkError e) {
48              thrown = e;
49          } finally {
50              INIT_OK = ok;
51              INIT_ERROR = thrown;
52          }
53      }
54  
55      //misc
56      /**
57       * @return OPENSSL_VERSION_NUMBER which is a numeric release version
58       * * identifier
59       */
60      public static native NativeLong SSLeay();
61  
62      /**
63       * Retrieves version/build information about OpenSSL library.
64       *
65       * @param type type can be SSLEAY_VERSION, SSLEAY_CFLAGS, SSLEAY_BUILT_ON...
66       * @return A pointer to a constant string describing the version of the
67       * OpenSSL library or giving information about the library build.
68       */
69      public static native String SSLeay_version(int type);
70  
71      /**
72       * Registers the error strings for all libcrypto functions.
73       */
74      public static native void ERR_load_crypto_strings();
75  
76      /**
77       * @return the earliest error code from the thread's error queue without
78       * modifying it.
79       */
80      public static native NativeLong ERR_peek_error();
81  
82  
83  
84      /**
85       * Generates a human-readable string representing the error code e.
86       * @see <a>https://www.openssl.org/docs/manmaster/crypto/ERR_error_string.html</a>
87       *
88       * @param err the error code
89       * @param null_ buf is NULL, the error string is placed in a static buffer
90       * @return the human-readable error messages.
91       */
92      public static native String ERR_error_string(NativeLong err, char[] null_);
93      //String ERR_lib_error_string(NativeLong err);
94      //String ERR_func_error_string(NativeLong err);
95  
96      //en-/decryption
97      /**
98       * Creates a cipher context.
99       *
100      * @return a pointer to a newly created EVP_CIPHER_CTX for success and
101      * NULL for failure.
102      */
103     public static native PointerByReference EVP_CIPHER_CTX_new();
104 
105 
106     /**
107      * EVP_CIPHER_CTX_init() remains as an alias for EVP_CIPHER_CTX_reset
108      * @param p cipher context
109      */
110     public static native void EVP_CIPHER_CTX_init(PointerByReference p);
111 
112     /**
113      * Enables or disables padding
114      * @param c cipher context
115      * @param pad If the pad parameter is zero then no padding is performed
116      * @return always returns 1
117      */
118     public static native int EVP_CIPHER_CTX_set_padding(PointerByReference c, int pad);
119 
120     /**
121      * @return an openssl AES evp cipher instance with a 128-bit key CBC mode
122      */
123     public static native PointerByReference EVP_aes_128_cbc();
124 
125     /**
126      * @return an openssl AES evp cipher instance with a 128-bit key CTR mode
127      */
128     public static native PointerByReference EVP_aes_128_ctr();
129 
130     /**
131      * @return an openssl AES evp cipher instance with a 192-bit key CBC mode
132      */
133     public static native PointerByReference EVP_aes_192_cbc();
134 
135     /**
136      * @return an openssl AES evp cipher instance with a 192-bit key CTR mode
137      */
138     public static native PointerByReference EVP_aes_192_ctr();
139 
140     /**
141      * @return an openssl AES evp cipher instance with a 256-bit key CBC mode
142      */
143     public static native PointerByReference EVP_aes_256_cbc();
144 
145     /**
146      * @return an openssl AES evp cipher instance with a 256-bit key CTR mode
147      */
148     public static native PointerByReference EVP_aes_256_ctr();
149 
150     /**
151      * Init a cipher.
152      * @param ctx cipher context
153      * @param cipher evp cipher instance
154      * @param impl engine
155      * @param key key
156      * @param iv iv
157      * @param enc 1 for encryption, 0 for decryption
158      * @return 1 for success and 0 for failure.
159      */
160     public static native int EVP_CipherInit_ex(PointerByReference ctx, PointerByReference cipher, PointerByReference impl, byte key[], byte iv[], int enc);
161 
162 
163     /**
164      * Continues a multiple-part encryption/decryption operation.
165      *
166      * @param ctx cipher context
167      * @param bout output byte buffer
168      * @param outl output length
169      * @param in input byte buffer
170      * @param inl input length
171      * @return 1 for success and 0 for failure.
172      */
173     public static native int EVP_CipherUpdate(PointerByReference ctx, ByteBuffer bout, int[] outl, ByteBuffer in, int inl);
174 
175     /**
176      * Finishes a multiple-part operation.
177      *
178      * @param ctx cipher context
179      * @param bout output byte buffer
180      * @param outl output length
181      * @return 1 for success and 0 for failure.
182      */
183     public static native int EVP_CipherFinal_ex(PointerByReference ctx, ByteBuffer bout, int[] outl);
184 
185     /**
186      * Clears all information from a cipher context and free up any allocated
187      * memory associate with it, including ctx itself.
188      * @param c openssl evp cipher
189      */
190     public static native void EVP_CIPHER_CTX_free(PointerByReference c);
191 
192     /**
193      * Clears all information from a cipher context and free up any allocated
194      * * memory associate with it.
195      * @param c openssl evp cipher
196      */
197     public static native void EVP_CIPHER_CTX_cleanup(PointerByReference c);
198 
199     //Random generator
200     /**
201      * OpenSSL uses for random number generation
202      * @return pointers to the respective methods
203      */
204     public static native PointerByReference RAND_get_rand_method();
205 
206     /**
207      * OpenSSL uses for random number generation.
208      * @return pointers to the respective methods
209      */
210     public static native PointerByReference RAND_SSLeay();
211 
212     /**
213      * Generates random data
214      * @param buf the bytes for generated random.
215      * @param num buffer length
216      * @return 1 on success, 0 otherwise.
217      */
218     public static native int RAND_bytes(ByteBuffer buf, int num);
219 
220     /**
221      * Releases all functional references.
222      *
223      * @param e engine reference.
224      * @return 0 on success, 1 otherwise.
225      */
226     public static native int ENGINE_finish(PointerByReference e);
227 
228     /**
229      * Frees the structural reference
230      * @param e engine reference.
231      * @return 0 on success, 1 otherwise.
232      */
233     public static native int ENGINE_free(PointerByReference e);
234 
235     /**
236      * Cleanups before program exit, it will avoid memory leaks.
237      * @return 0 on success, 1 otherwise.
238      */
239     public static native int ENGINE_cleanup();
240 
241     /**
242      * Obtains a functional reference from an existing structural reference.
243      * @param e engine reference
244      * @return zero if the ENGINE was not already operational and couldn't be successfully initialised
245      */
246     public static native int ENGINE_init(PointerByReference e);
247 
248     /**
249      * Sets the engine as the default for random number generation.
250      * @param e  engine reference
251      * @param flags ENGINE_METHOD_RAND
252      * @return zero if failed.
253      */
254     public static native int ENGINE_set_default(PointerByReference e, int flags);
255 
256     /**
257      * Gets engine by id
258      * @param id engine id
259      * @return engine instance
260      */
261     public static native PointerByReference ENGINE_by_id(String id);
262 
263     /**
264      * Initializes the engine.
265      */
266     public static native void ENGINE_load_rdrand();
267 
268     //TODO callback multithreading
269     /*public interface Id_function_cb extends Callback {
270         long invoke ();
271     }
272 
273     public interface Locking_function_cb extends Callback {
274         void invoke(int mode, int n, String file, int line);
275     }
276 
277     public static final Id_function_cb default_id_function = new Id_function_cb() {
278 
279         @Override
280         public long invoke() {
281             //id always positive
282             long id = Thread.currentThread().getId();
283             return id;
284         }
285     };
286 
287     int CRYPTO_num_locks();
288     void CRYPTO_set_id_callback(Id_function_cb id_function);
289     void CRYPTO_set_locking_callback(Locking_function_cb locking_function);*/
290 }