OpenSsl20XNativeJna.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 OpenSsl20XNativeJna 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.      * Cleanups before program exit, it will avoid memory leaks.
  53.      *
  54.      * @return 0 on success, 1 otherwise.
  55.      */
  56.     public static native int ENGINE_cleanup();

  57.     /**
  58.      * Releases all functional references.
  59.      *
  60.      * @param e
  61.      *            engine reference.
  62.      * @return 0 on success, 1 otherwise.
  63.      */
  64.     public static native int ENGINE_finish(PointerByReference e);

  65.     /**
  66.      * Frees the structural reference
  67.      *
  68.      * @param e
  69.      *            engine reference.
  70.      * @return 0 on success, 1 otherwise.
  71.      */
  72.     public static native int ENGINE_free(PointerByReference e);

  73.     /**
  74.      * Obtains a functional reference from an existing structural reference.
  75.      *
  76.      * @param e
  77.      *            engine reference
  78.      * @return zero if the ENGINE was not already operational and couldn't be successfully
  79.      *         initialized
  80.      */
  81.     public static native int ENGINE_init(PointerByReference e);

  82.     /**
  83.      * Sets the engine as the default for random number generation.
  84.      *
  85.      * @param e
  86.      *            engine reference.
  87.      * @param flags
  88.      *            ENGINE_METHOD_RAND.
  89.      * @return zero if failed.
  90.      */
  91.     public static native int ENGINE_set_default(PointerByReference e, int flags);

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

  104.     // TODO: NOT USED?
  105.     /**
  106.      * Registers the error strings for all libcrypto functions.
  107.      */
  108.     public static native void ERR_load_crypto_strings();

  109.     /**
  110.      * @return the earliest error code from the thread's error queue without modifying it.
  111.      */
  112.     public static native NativeLong ERR_peek_error();

  113.     /**
  114.      * @return an OpenSSL AES EVP cipher instance with a 128-bit key CBC mode.
  115.      */
  116.     public static native PointerByReference EVP_aes_128_cbc();

  117.     /**
  118.      * @return an OpenSSL AES EVP cipher instance with a 128-bit key CTR mode.
  119.      */
  120.     public static native PointerByReference EVP_aes_128_ctr();

  121.     /**
  122.      * @return an OpenSSL AES EVP cipher instance with a 192-bit key CBC mode.
  123.      */
  124.     public static native PointerByReference EVP_aes_192_cbc();

  125.     /**
  126.      * @return an OpenSSL AES EVP cipher instance with a 192-bit key CTR mode.
  127.      */
  128.     public static native PointerByReference EVP_aes_192_ctr();

  129.     /**
  130.      * @return an OpenSSL AES EVP cipher instance with a 256-bit key CBC mode.
  131.      */
  132.     public static native PointerByReference EVP_aes_256_cbc();

  133.     /**
  134.      * @return an OpenSSL AES EVP cipher instance with a 256-bit key CTR mode.
  135.      */
  136.     public static native PointerByReference EVP_aes_256_ctr();

  137.     /**
  138.      * Clears all information from a cipher context and free up any allocated * memory associate
  139.      * with it.
  140.      *
  141.      * @param c
  142.      *            openssl evp cipher
  143.      */
  144.     public static native void EVP_CIPHER_CTX_cleanup(PointerByReference c);

  145.     /**
  146.      * Clears all information from a cipher context and free up any allocated memory associate with
  147.      * it, including ctx itself.
  148.      *
  149.      * @param c
  150.      *            openssl evp cipher
  151.      */
  152.     public static native void EVP_CIPHER_CTX_free(PointerByReference c);

  153.     // TODO: NOT USED?
  154.     /**
  155.      * EVP_CIPHER_CTX_init() remains as an alias for EVP_CIPHER_CTX_reset.
  156.      *
  157.      * @param p
  158.      *            cipher context
  159.      */
  160.     public static native void EVP_CIPHER_CTX_init(PointerByReference p);

  161.     /**
  162.      * Creates a cipher context.
  163.      *
  164.      * @return a pointer to a newly created EVP_CIPHER_CTX for success and NULL for failure.
  165.      */
  166.     public static native PointerByReference EVP_CIPHER_CTX_new();

  167.     /**
  168.      * Enables or disables padding.
  169.      *
  170.      * @param c
  171.      *            cipher context.
  172.      * @param pad
  173.      *            If the pad parameter is zero then no padding is performed.
  174.      * @return always returns 1
  175.      */
  176.     public static native int EVP_CIPHER_CTX_set_padding(PointerByReference c, int pad);

  177.     /**
  178.      * Finishes a multiple-part operation.
  179.      *
  180.      * @param ctx
  181.      *            cipher context
  182.      * @param bout
  183.      *            output byte buffer
  184.      * @param outl
  185.      *            output length
  186.      * @return 1 for success and 0 for failure.
  187.      */
  188.     public static native int EVP_CipherFinal_ex(PointerByReference ctx, ByteBuffer bout,
  189.             int[] outl);

  190.     /**
  191.      * Init a cipher.
  192.      *
  193.      * @param ctx
  194.      *            cipher context
  195.      * @param cipher
  196.      *            evp cipher instance
  197.      * @param impl
  198.      *            engine
  199.      * @param key
  200.      *            key
  201.      * @param iv
  202.      *            iv
  203.      * @param enc
  204.      *            1 for encryption, 0 for decryption
  205.      * @return 1 for success and 0 for failure.
  206.      */
  207.     public static native int EVP_CipherInit_ex(PointerByReference ctx, PointerByReference cipher,
  208.             PointerByReference impl, byte[] key, byte[] iv, int enc);

  209.     // ENGINE API: https://www.openssl.org/docs/man1.0.2/man3/engine.html

  210.     /**
  211.      * Continues a multiple-part encryption/decryption operation.
  212.      *
  213.      * @param ctx
  214.      *            cipher context
  215.      * @param bout
  216.      *            output byte buffer
  217.      * @param outl
  218.      *            output length
  219.      * @param in
  220.      *            input byte buffer
  221.      * @param inl
  222.      *            input length
  223.      * @return 1 for success and 0 for failure.
  224.      */
  225.     public static native int EVP_CipherUpdate(PointerByReference ctx, ByteBuffer bout, int[] outl,
  226.             ByteBuffer in, int inl);

  227.     /**
  228.      * Generates random data.
  229.      *
  230.      * @param buf
  231.      *            the bytes for generated random.
  232.      * @param num
  233.      *            buffer length.
  234.      * @return 1 on success, 0 otherwise.
  235.      */
  236.     public static native int RAND_bytes(ByteBuffer buf, int num);

  237.     // Random generator
  238.     /**
  239.      * OpenSSL uses for random number generation.
  240.      *
  241.      * @return pointers to the respective methods.
  242.      */
  243.     public static native PointerByReference RAND_get_rand_method();

  244.     /**
  245.      * OpenSSL uses for random number generation.
  246.      *
  247.      * @return pointers to the respective methods.
  248.      */
  249.     public static native PointerByReference RAND_SSLeay();

  250.     /**
  251.      * TODO (does not appear to be used yet)
  252.      * @return OPENSSL_VERSION_NUMBER which is a numeric release version identifier
  253.      */
  254.     public static native NativeLong SSLeay();

  255.     /**
  256.      * Retrieves version/build information about OpenSSL library.
  257.      * This is returned by {@link OpenSslNativeJna#OpenSSLVersion(int)}
  258.      *
  259.      * @param type
  260.      *            type can be SSLEAY_VERSION, SSLEAY_CFLAGS, SSLEAY_BUILT_ON...
  261.      * @return A pointer to a constant string describing the version of the OpenSSL library or
  262.      *         giving information about the library build.
  263.      */
  264.     public static native String SSLeay_version(int type);

  265.     // ================== instance interface methods ==================

  266.     @Override
  267.     public PointerByReference _ENGINE_by_id(final String string) {
  268.         return ENGINE_by_id(string);
  269.     }

  270.     @Override
  271.     public int _ENGINE_cleanup() {
  272.         return ENGINE_cleanup();
  273.     }

  274.     @Override
  275.     public int _ENGINE_finish(final PointerByReference rdrandEngine) {
  276.         return ENGINE_finish(rdrandEngine);
  277.     }

  278.     @Override
  279.     public int _ENGINE_free(final PointerByReference rdrandEngine) {
  280.         return ENGINE_free(rdrandEngine);
  281.     }

  282.     @Override
  283.     public int _ENGINE_init(final PointerByReference rdrandEngine) {
  284.         return ENGINE_init(rdrandEngine);
  285.     }

  286.     @Override
  287.     public void _ENGINE_load_rdrand() {
  288.         // Not available
  289.     }

  290.     @Override
  291.     public int _ENGINE_set_default(final PointerByReference rdrandEngine, final int flags) {
  292.         return ENGINE_set_default(rdrandEngine, flags);
  293.     }

  294.     @Override
  295.     public String _ERR_error_string(final NativeLong err, final char[] buff) {
  296.         return ERR_error_string(err, buff);
  297.     }

  298.     @Override
  299.     public NativeLong _ERR_peek_error() {
  300.         return ERR_peek_error();
  301.     }

  302.     @Override
  303.     public PointerByReference _EVP_aes_128_cbc() {
  304.         return EVP_aes_128_cbc();
  305.     }

  306.     @Override
  307.     public PointerByReference _EVP_aes_128_ctr() {
  308.         return EVP_aes_128_ctr();
  309.     }

  310.     @Override
  311.     public PointerByReference _EVP_aes_192_cbc() {
  312.         return EVP_aes_192_cbc();
  313.     }

  314.     @Override
  315.     public PointerByReference _EVP_aes_192_ctr() {
  316.         return EVP_aes_192_ctr();
  317.     }

  318.     @Override
  319.     public PointerByReference _EVP_aes_256_cbc() {
  320.         return EVP_aes_256_cbc();
  321.     }

  322.     @Override
  323.     public PointerByReference _EVP_aes_256_ctr() {
  324.         return EVP_aes_256_ctr();
  325.     }

  326.     @Override
  327.     public void _EVP_CIPHER_CTX_cleanup(final PointerByReference context) {
  328.         EVP_CIPHER_CTX_cleanup(context);
  329.     }

  330.     @Override
  331.     public void _EVP_CIPHER_CTX_free(final PointerByReference context) {
  332.         EVP_CIPHER_CTX_free(context);
  333.     }

  334.     @Override
  335.     public PointerByReference _EVP_CIPHER_CTX_new() {
  336.         return EVP_CIPHER_CTX_new();
  337.     }

  338.     @Override
  339.     public int _EVP_CIPHER_CTX_set_padding(final PointerByReference context, final int padding) {
  340.         return EVP_CIPHER_CTX_set_padding(context, padding);
  341.     }

  342.     @Override
  343.     public int _EVP_CipherFinal_ex(final PointerByReference context, final ByteBuffer outBuffer, final int[] outlen) {
  344.         return EVP_CipherFinal_ex(context, outBuffer, outlen);
  345.     }

  346.     @Override
  347.     public int _EVP_CipherInit_ex(final PointerByReference context, final PointerByReference algo, final PointerByReference impl, final byte[] encoded,
  348.             final byte[] iv, final int cipherMode) {
  349.         return EVP_CipherInit_ex(context, algo, impl, encoded, iv, cipherMode);
  350.     }

  351.     @Override
  352.     public int _EVP_CipherUpdate(final PointerByReference context, final ByteBuffer outBuffer, final int[] outlen, final ByteBuffer inBuffer,
  353.             final int remaining) {
  354.         return EVP_CipherUpdate(context, outBuffer, outlen, inBuffer, remaining);
  355.     }

  356.     @Override
  357.     public Throwable _INIT_ERROR() {
  358.         return INIT_ERROR;
  359.     }

  360.     @Override
  361.     public boolean _INIT_OK() {
  362.         return INIT_OK;
  363.     }

  364.     @Override
  365.     public String _OpenSSL_version(final int i) {
  366.         return SSLeay_version(i);
  367.     }

  368.     @Override
  369.     public int _RAND_bytes(final ByteBuffer buf, final int length) {
  370.         return RAND_bytes(buf, length) ;
  371.     }

  372.     @Override
  373.     public PointerByReference _RAND_get_rand_method() {
  374.         return RAND_get_rand_method();
  375.     }

  376.     @Override
  377.     public PointerByReference _RAND_SSLeay() {
  378.         return RAND_SSLeay();
  379.     }
  380. }