OpenSsl11XNativeJna.java

  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. package org.apache.commons.crypto.jna;

  19. import java.nio.ByteBuffer;

  20. import org.apache.commons.crypto.Crypto;

  21. import com.sun.jna.Native;
  22. import com.sun.jna.NativeLong;
  23. import com.sun.jna.ptr.PointerByReference;

  24. final class OpenSsl11XNativeJna  implements OpenSslInterfaceNativeJna {

  25.     static final boolean INIT_OK;

  26.     static final Throwable INIT_ERROR;

  27.     static {
  28.         boolean ok = false;
  29.         Throwable thrown = null;
  30.         try {
  31.             final String libName = System.getProperty(Crypto.CONF_PREFIX + OpenSslNativeJna.class.getSimpleName(), "crypto");
  32.             OpenSslJna.debug("Native.register('%s')", libName);
  33.             Native.register(libName);
  34.             ok = true;
  35.         } catch (final Exception | UnsatisfiedLinkError e) {
  36.             thrown = e;
  37.         } finally {
  38.             INIT_OK = ok;
  39.             INIT_ERROR = thrown;
  40.         }
  41.     }

  42.     // Try to keep methods aligned across versions

  43.     /**
  44.      * Gets engine by id
  45.      *
  46.      * @param id
  47.      *            engine id
  48.      * @return engine instance
  49.      */
  50.     public static native PointerByReference ENGINE_by_id(String id);

  51.     /**
  52.      * Releases all functional references.
  53.      *
  54.      * @param e
  55.      *            engine reference.
  56.      * @return 0 on success, 1 otherwise.
  57.      */
  58.     public static native int ENGINE_finish(PointerByReference e);

  59.     /**
  60.      * Frees the structural reference
  61.      *
  62.      * @param e
  63.      *            engine reference.
  64.      * @return 0 on success, 1 otherwise.
  65.      */
  66.     public static native int ENGINE_free(PointerByReference e);

  67.     /**
  68.      * Obtains a functional reference from an existing structural reference.
  69.      *
  70.      * @param e
  71.      *            engine reference
  72.      * @return zero if the ENGINE was not already operational and couldn't be successfully
  73.      *         initialized
  74.      */
  75.     public static native int ENGINE_init(PointerByReference e);

  76.     /**
  77.      * Sets the engine as the default for random number generation.
  78.      *
  79.      * @param e
  80.      *            engine reference
  81.      * @param flags
  82.      *            ENGINE_METHOD_RAND
  83.      * @return zero if failed.
  84.      */
  85.     public static native int ENGINE_set_default(PointerByReference e, int flags);

  86.     /**
  87.      * Generates a human-readable string representing the error code e.
  88.      *
  89.      * @see <a href="https://www.openssl.org/docs/man1.1.0/man3/ERR_error_string.html">ERR_error_string</a>
  90.      *
  91.      * @param err
  92.      *            the error code
  93.      * @param null_
  94.      *            buf is NULL, the error string is placed in a static buffer
  95.      * @return the human-readable error messages.
  96.      */
  97.     public static native String ERR_error_string(NativeLong err, char[] null_);

  98.     /**
  99.      * @return the earliest error code from the thread's error queue without modifying it.
  100.      */
  101.     public static native NativeLong ERR_peek_error();

  102.     /**
  103.      * @return an OpenSSL AES EVP cipher instance with a 128-bit key CBC mode
  104.      */
  105.     public static native PointerByReference EVP_aes_128_cbc();

  106.     /**
  107.      * @return an OpenSSL AES EVP cipher instance with a 128-bit key CTR mode
  108.      */
  109.     public static native PointerByReference EVP_aes_128_ctr();

  110.     /**
  111.      * @return an OpenSSL AES EVP cipher instance with a 192-bit key CBC mode
  112.      */
  113.     public static native PointerByReference EVP_aes_192_cbc();

  114.     /**
  115.      * @return an OpenSSL AES EVP cipher instance with a 192-bit key CTR mode
  116.      */
  117.     public static native PointerByReference EVP_aes_192_ctr();

  118.     /**
  119.      * @return an OpenSSL AES EVP cipher instance with a 256-bit key CBC mode
  120.      */
  121.     public static native PointerByReference EVP_aes_256_cbc();

  122.     /**
  123.      * @return an OpenSSL AES EVP cipher instance with a 256-bit key CTR mode
  124.      */
  125.     public static native PointerByReference EVP_aes_256_ctr();

  126.     /**
  127.      * Clears all information from a cipher context and free up any allocated memory associate with
  128.      * it, including ctx itself.
  129.      *
  130.      * @param c
  131.      *            openssl evp cipher
  132.      */
  133.     public static native void EVP_CIPHER_CTX_free(PointerByReference c);

  134.     /**
  135.      * Creates a cipher context.
  136.      *
  137.      * @return a pointer to a newly created EVP_CIPHER_CTX for success and NULL for failure.
  138.      */
  139.     public static native PointerByReference EVP_CIPHER_CTX_new();

  140.     /**
  141.      * Clears all information from a cipher context and free up any allocated * memory associate
  142.      * with it.
  143.      *
  144.      * @param c
  145.      *            openssl evp cipher
  146.      */

  147.     /**
  148.      * Enables or disables padding
  149.      *
  150.      * @param c
  151.      *            cipher context
  152.      * @param pad
  153.      *            If the pad parameter is zero then no padding is performed
  154.      * @return always returns 1
  155.      */
  156.     public static native int EVP_CIPHER_CTX_set_padding(PointerByReference c, int pad);

  157.     /**
  158.      * Finishes a multiple-part operation.
  159.      *
  160.      * @param ctx
  161.      *            cipher context
  162.      * @param bout
  163.      *            output byte buffer
  164.      * @param outl
  165.      *            output length
  166.      * @return 1 for success and 0 for failure.
  167.      */
  168.     public static native int EVP_CipherFinal_ex(PointerByReference ctx, ByteBuffer bout,
  169.             int[] outl);

  170.     // ENGINE API: https://www.openssl.org/docs/man1.1.1/man3/ENGINE_add.html
  171.     // (The above page includes all the ENGINE functions used below)

  172.     /**
  173.      * Init a cipher.
  174.      *
  175.      * @param ctx
  176.      *            cipher context
  177.      * @param cipher
  178.      *            evp cipher instance
  179.      * @param impl
  180.      *            engine
  181.      * @param key
  182.      *            key
  183.      * @param iv
  184.      *            iv
  185.      * @param enc
  186.      *            1 for encryption, 0 for decryption
  187.      * @return 1 for success and 0 for failure.
  188.      */
  189.     public static native int EVP_CipherInit_ex(PointerByReference ctx, PointerByReference cipher,
  190.             PointerByReference impl, byte[] key, byte[] iv, int enc);

  191.     /**
  192.      * Continues a multiple-part encryption/decryption operation.
  193.      *
  194.      * @param ctx
  195.      *            cipher context
  196.      * @param bout
  197.      *            output byte buffer
  198.      * @param outl
  199.      *            output length
  200.      * @param in
  201.      *            input byte buffer
  202.      * @param inl
  203.      *            input length
  204.      * @return 1 for success and 0 for failure.
  205.      */
  206.     public static native int EVP_CipherUpdate(PointerByReference ctx, ByteBuffer bout, int[] outl,
  207.             ByteBuffer in, int inl);

  208.     /**
  209.      * Retrieves version/build information about OpenSSL library.
  210.      *
  211.      * @see <a href="https://www.openssl.org/docs/man1.1.1/man3/OpenSSL_version.html">OpenSSL_version</a>
  212.      * @param type
  213.      *            type can be OPENSSL_VERSION, OPENSSL_CFLAGS, OPENSSL_BUILT_ON...
  214.      * @return A pointer to a constant string describing the version of the OpenSSL library or
  215.      *         giving information about the library build.
  216.      */
  217.     public static native String OpenSSL_version(int type);

  218.     /**
  219.      * Generates random data
  220.      *
  221.      * @param buf
  222.      *            the bytes for generated random.
  223.      * @param num
  224.      *            buffer length
  225.      * @return 1 on success, 0 otherwise.
  226.      */
  227.     public static native int RAND_bytes(ByteBuffer buf, int num);

  228.     // Random generator
  229.     /**
  230.      * OpenSSL uses for random number generation
  231.      *
  232.      * @return pointers to the respective methods
  233.      */
  234.     public static native PointerByReference RAND_get_rand_method();

  235.     // ================== instance interface methods ==================

  236.     @Override
  237.     public PointerByReference _ENGINE_by_id(final String string) {
  238.         return ENGINE_by_id(string);
  239.     }

  240.     @Override
  241.     public int _ENGINE_cleanup() {
  242.         return 0; // Not available
  243.     }

  244.     @Override
  245.     public int _ENGINE_finish(final PointerByReference rdrandEngine) {
  246.         return ENGINE_finish(rdrandEngine);
  247.     }

  248.     @Override
  249.     public int _ENGINE_free(final PointerByReference rdrandEngine) {
  250.         return ENGINE_free(rdrandEngine);
  251.     }

  252.     @Override
  253.     public int _ENGINE_init(final PointerByReference rdrandEngine) {
  254.         return ENGINE_init(rdrandEngine);
  255.     }

  256.     @Override
  257.     public void _ENGINE_load_rdrand() {
  258.         // Not available
  259.     }

  260.     @Override
  261.     public int _ENGINE_set_default(final PointerByReference rdrandEngine, final int flags) {
  262.         return ENGINE_set_default(rdrandEngine, flags);
  263.     }

  264.     @Override
  265.     public String _ERR_error_string(final NativeLong err, final char[] buff) {
  266.         return ERR_error_string(err, buff);
  267.     }

  268.     @Override
  269.     public NativeLong _ERR_peek_error() {
  270.         return ERR_peek_error();
  271.     }

  272.     @Override
  273.     public PointerByReference _EVP_aes_128_cbc() {
  274.         return EVP_aes_128_cbc();
  275.     }

  276.     @Override
  277.     public PointerByReference _EVP_aes_128_ctr() {
  278.         return EVP_aes_128_ctr();
  279.     }

  280.     @Override
  281.     public PointerByReference _EVP_aes_192_cbc() {
  282.         return EVP_aes_192_cbc();
  283.     }

  284.     @Override
  285.     public PointerByReference _EVP_aes_192_ctr() {
  286.         return EVP_aes_192_ctr();
  287.     }

  288.     @Override
  289.     public PointerByReference _EVP_aes_256_cbc() {
  290.         return EVP_aes_256_cbc();
  291.     }

  292.     @Override
  293.     public PointerByReference _EVP_aes_256_ctr() {
  294.         return EVP_aes_256_ctr();
  295.     }

  296.     @Override
  297.     public void _EVP_CIPHER_CTX_cleanup(final PointerByReference context) {
  298.         // Not available
  299.     }

  300.     @Override
  301.     public void _EVP_CIPHER_CTX_free(final PointerByReference context) {
  302.         EVP_CIPHER_CTX_free(context);
  303.     }

  304.     @Override
  305.     public PointerByReference _EVP_CIPHER_CTX_new() {
  306.         return EVP_CIPHER_CTX_new();
  307.     }

  308.     @Override
  309.     public int _EVP_CIPHER_CTX_set_padding(final PointerByReference context, final int padding) {
  310.         return EVP_CIPHER_CTX_set_padding(context, padding);
  311.     }

  312.     @Override
  313.     public int _EVP_CipherFinal_ex(final PointerByReference context, final ByteBuffer outBuffer, final int[] outlen) {
  314.         return EVP_CipherFinal_ex(context, outBuffer, outlen);
  315.     }

  316.     @Override
  317.     public int _EVP_CipherInit_ex(final PointerByReference context, final PointerByReference algo, final PointerByReference impl, final byte[] encoded,
  318.             final byte[] iv, final int cipherMode) {
  319.         return EVP_CipherInit_ex(context, algo, impl, encoded, iv, cipherMode);
  320.     }

  321.     @Override
  322.     public int _EVP_CipherUpdate(final PointerByReference context, final ByteBuffer outBuffer, final int[] outlen, final ByteBuffer inBuffer,
  323.             final int remaining) {
  324.         return EVP_CipherUpdate(context, outBuffer, outlen, inBuffer, remaining);
  325.     }

  326.     @Override
  327.     public Throwable _INIT_ERROR() {
  328.         return INIT_ERROR;
  329.     }

  330.     @Override
  331.     public boolean _INIT_OK() {
  332.         return INIT_OK;
  333.     }

  334.     @Override
  335.     public String _OpenSSL_version(final int i) {
  336.         return OpenSSL_version(i);
  337.     }

  338.     @Override
  339.     public int _RAND_bytes(final ByteBuffer buf, final int length) {
  340.         return RAND_bytes(buf, length) ;
  341.     }

  342.     @Override
  343.     public PointerByReference _RAND_get_rand_method() {
  344.         return RAND_get_rand_method();
  345.     }

  346.     @Override
  347.     public PointerByReference _RAND_SSLeay() {
  348.         return null; // Not available
  349.     }

  350. }