OpenSsl10XNativeJna.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 OpenSsl10XNativeJna 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.      * Initializes the engine.
  84.      */
  85.     public static native void ENGINE_load_rdrand();

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

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

  108.     // TODO: NOT USED?
  109.     /**
  110.      * Registers the error strings for all libcrypto functions.
  111.      */
  112.     public static native void ERR_load_crypto_strings();

  113.     /**
  114.      * @return the earliest error code from the thread's error queue without modifying it.
  115.      */
  116.     public static native NativeLong ERR_peek_error();

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

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

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

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

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

  137.     /**
  138.      * @return an OpenSSL AES EVP cipher instance with a 256-bit key CTR mode
  139.      */
  140.     public static native PointerByReference EVP_aes_256_ctr();

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

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

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

  165.     /**
  166.      * Creates a cipher context.
  167.      *
  168.      * @return a pointer to a newly created EVP_CIPHER_CTX for success and NULL for failure.
  169.      */
  170.     public static native PointerByReference EVP_CIPHER_CTX_new();

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

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

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

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

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

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

  241.     // Random generator
  242.     /**
  243.      * OpenSSL uses for random number generation
  244.      *
  245.      * @return pointers to the respective methods
  246.      */
  247.     public static native PointerByReference RAND_get_rand_method();

  248.     /**
  249.      * OpenSSL uses for random number generation.
  250.      *
  251.      * @return pointers to the respective methods
  252.      */
  253.     public static native PointerByReference RAND_SSLeay();

  254.     /**
  255.      * @see <a href="https://www.openssl.org/docs/man1.0.2/man3/SSLeay.html">Version Number</a>
  256.      * TODO (does not appear to be used yet)
  257.      * @return OPENSSL_VERSION_NUMBER which is a numeric release version identifier
  258.      */
  259.     public static native NativeLong SSLeay();

  260.     /**
  261.      * Retrieves version/build information about OpenSSL library.
  262.      * This is returned by {@link OpenSslNativeJna#OpenSSLVersion(int)}
  263.      *
  264.      * @see <a href="https://www.openssl.org/docs/man1.0.2/man3/SSLeay_version.html">Version Info</a>
  265.      *
  266.      * @param type
  267.      *            type can be SSLEAY_VERSION, SSLEAY_CFLAGS, SSLEAY_BUILT_ON...
  268.      * @return A pointer to a constant string describing the version of the OpenSSL library or
  269.      *         giving information about the library build.
  270.      */
  271.     public static native String SSLeay_version(int type);


  272.     // ================== instance interface methods ==================

  273.     @Override
  274.     public PointerByReference _ENGINE_by_id(final String string) {
  275.         return ENGINE_by_id(string);
  276.     }

  277.     @Override
  278.     public int _ENGINE_cleanup() {
  279.         return ENGINE_cleanup();
  280.     }

  281.     @Override
  282.     public int _ENGINE_finish(final PointerByReference rdrandEngine) {
  283.         return ENGINE_finish(rdrandEngine);
  284.     }

  285.     @Override
  286.     public int _ENGINE_free(final PointerByReference rdrandEngine) {
  287.         return ENGINE_free(rdrandEngine);
  288.     }

  289.     @Override
  290.     public int _ENGINE_init(final PointerByReference rdrandEngine) {
  291.         return ENGINE_init(rdrandEngine);
  292.     }

  293.     @Override
  294.     public void _ENGINE_load_rdrand() {
  295.         ENGINE_load_rdrand();
  296.     }

  297.     @Override
  298.     public int _ENGINE_set_default(final PointerByReference rdrandEngine, final int flags) {
  299.         return ENGINE_set_default(rdrandEngine, flags);
  300.     }

  301.     @Override
  302.     public String _ERR_error_string(final NativeLong err, final char[] buff) {
  303.         return ERR_error_string(err, buff);
  304.     }

  305.     @Override
  306.     public NativeLong _ERR_peek_error() {
  307.         return ERR_peek_error();
  308.     }

  309.     @Override
  310.     public PointerByReference _EVP_aes_128_cbc() {
  311.         return EVP_aes_128_cbc();
  312.     }

  313.     @Override
  314.     public PointerByReference _EVP_aes_128_ctr() {
  315.         return EVP_aes_128_ctr();
  316.     }

  317.     @Override
  318.     public PointerByReference _EVP_aes_192_cbc() {
  319.         return EVP_aes_192_cbc();
  320.     }

  321.     @Override
  322.     public PointerByReference _EVP_aes_192_ctr() {
  323.         return EVP_aes_192_ctr();
  324.     }

  325.     @Override
  326.     public PointerByReference _EVP_aes_256_cbc() {
  327.         return EVP_aes_256_cbc();
  328.     }

  329.     @Override
  330.     public PointerByReference _EVP_aes_256_ctr() {
  331.         return EVP_aes_256_ctr();
  332.     }

  333.     @Override
  334.     public void _EVP_CIPHER_CTX_cleanup(final PointerByReference context) {
  335.         EVP_CIPHER_CTX_cleanup(context);
  336.     }

  337.     @Override
  338.     public void _EVP_CIPHER_CTX_free(final PointerByReference context) {
  339.         EVP_CIPHER_CTX_free(context);
  340.     }

  341.     @Override
  342.     public PointerByReference _EVP_CIPHER_CTX_new() {
  343.         return EVP_CIPHER_CTX_new();
  344.     }

  345.     @Override
  346.     public int _EVP_CIPHER_CTX_set_padding(final PointerByReference context, final int padding) {
  347.         return EVP_CIPHER_CTX_set_padding(context, padding);
  348.     }

  349.     @Override
  350.     public int _EVP_CipherFinal_ex(final PointerByReference context, final ByteBuffer outBuffer, final int[] outlen) {
  351.         return EVP_CipherFinal_ex(context, outBuffer, outlen);
  352.     }

  353.     @Override
  354.     public int _EVP_CipherInit_ex(final PointerByReference context, final PointerByReference algo, final PointerByReference impl, final byte[] encoded,
  355.             final byte[] iv, final int cipherMode) {
  356.         return EVP_CipherInit_ex(context, algo, impl, encoded, iv, cipherMode);
  357.     }

  358.     @Override
  359.     public int _EVP_CipherUpdate(final PointerByReference context, final ByteBuffer outBuffer, final int[] outlen, final ByteBuffer inBuffer,
  360.             final int remaining) {
  361.         return EVP_CipherUpdate(context, outBuffer, outlen, inBuffer, remaining);
  362.     }

  363.     @Override
  364.     public Throwable _INIT_ERROR() {
  365.         return INIT_ERROR;
  366.     }

  367.     @Override
  368.     public boolean _INIT_OK() {
  369.         return INIT_OK;
  370.     }

  371.     @Override
  372.     public String _OpenSSL_version(final int i) {
  373.         return SSLeay_version(i);
  374.     }

  375.     @Override
  376.     public int _RAND_bytes(final ByteBuffer buf, final int length) {
  377.         return RAND_bytes(buf, length) ;
  378.     }

  379.     @Override
  380.     public PointerByReference _RAND_get_rand_method() {
  381.         return RAND_get_rand_method();
  382.     }

  383.     @Override
  384.     public PointerByReference _RAND_SSLeay() {
  385.         return RAND_SSLeay();
  386.     }
  387. }