Home | History | Annotate | Line # | Download | only in engines
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2015-2024 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6      1.1  christos  * in the file LICENSE in the source distribution or at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  */
      9      1.1  christos 
     10      1.1  christos /* We need to use some engine deprecated APIs */
     11      1.1  christos #define OPENSSL_SUPPRESS_DEPRECATED
     12      1.1  christos 
     13      1.1  christos /*
     14      1.1  christos  * SHA-1 low level APIs are deprecated for public use, but still ok for
     15      1.1  christos  * internal use.  Note, that due to symbols not being exported, only the
     16      1.1  christos  * #defines and structures can be accessed, in this case SHA_CBLOCK and
     17      1.1  christos  * sizeof(SHA_CTX).
     18      1.1  christos  */
     19      1.1  christos #include "internal/deprecated.h"
     20      1.1  christos 
     21      1.1  christos #include <openssl/opensslconf.h>
     22      1.1  christos #if defined(_WIN32)
     23  1.1.1.2  christos #include <windows.h>
     24      1.1  christos #endif
     25      1.1  christos 
     26      1.1  christos #include <stdio.h>
     27      1.1  christos #include <string.h>
     28      1.1  christos 
     29      1.1  christos #include <openssl/engine.h>
     30      1.1  christos #include <openssl/sha.h>
     31      1.1  christos #include <openssl/aes.h>
     32      1.1  christos #include <openssl/rsa.h>
     33      1.1  christos #include <openssl/evp.h>
     34      1.1  christos #include <openssl/async.h>
     35      1.1  christos #include <openssl/bn.h>
     36      1.1  christos #include <openssl/crypto.h>
     37      1.1  christos #include <openssl/ssl.h>
     38      1.1  christos #include <openssl/modes.h>
     39      1.1  christos 
     40      1.1  christos #if defined(OPENSSL_SYS_UNIX) && defined(OPENSSL_THREADS)
     41  1.1.1.2  christos #undef ASYNC_POSIX
     42  1.1.1.2  christos #define ASYNC_POSIX
     43  1.1.1.2  christos #include <unistd.h>
     44      1.1  christos #elif defined(_WIN32)
     45  1.1.1.2  christos #undef ASYNC_WIN
     46  1.1.1.2  christos #define ASYNC_WIN
     47      1.1  christos #endif
     48      1.1  christos 
     49  1.1.1.2  christos /* clang-format off */
     50      1.1  christos #include "e_dasync_err.c"
     51  1.1.1.2  christos /* clang-format on */
     52      1.1  christos 
     53      1.1  christos /* Engine Id and Name */
     54      1.1  christos static const char *engine_dasync_id = "dasync";
     55      1.1  christos static const char *engine_dasync_name = "Dummy Async engine support";
     56      1.1  christos 
     57      1.1  christos /* Engine Lifetime functions */
     58      1.1  christos static int dasync_destroy(ENGINE *e);
     59      1.1  christos static int dasync_init(ENGINE *e);
     60      1.1  christos static int dasync_finish(ENGINE *e);
     61      1.1  christos void engine_load_dasync_int(void);
     62      1.1  christos 
     63      1.1  christos /* Set up digests. Just SHA1 for now */
     64      1.1  christos static int dasync_digests(ENGINE *e, const EVP_MD **digest,
     65  1.1.1.2  christos     const int **nids, int nid);
     66      1.1  christos 
     67      1.1  christos static void dummy_pause_job(void);
     68      1.1  christos 
     69      1.1  christos /* SHA1 */
     70      1.1  christos static int dasync_sha1_init(EVP_MD_CTX *ctx);
     71      1.1  christos static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
     72  1.1.1.2  christos     size_t count);
     73      1.1  christos static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
     74      1.1  christos 
     75      1.1  christos /*
     76      1.1  christos  * Holds the EVP_MD object for sha1 in this engine. Set up once only during
     77      1.1  christos  * engine bind and can then be reused many times.
     78      1.1  christos  */
     79      1.1  christos static EVP_MD *_hidden_sha1_md = NULL;
     80      1.1  christos static const EVP_MD *dasync_sha1(void)
     81      1.1  christos {
     82      1.1  christos     return _hidden_sha1_md;
     83      1.1  christos }
     84      1.1  christos static void destroy_digests(void)
     85      1.1  christos {
     86      1.1  christos     EVP_MD_meth_free(_hidden_sha1_md);
     87      1.1  christos     _hidden_sha1_md = NULL;
     88      1.1  christos }
     89      1.1  christos 
     90      1.1  christos static int dasync_digest_nids(const int **nids)
     91      1.1  christos {
     92      1.1  christos     static int digest_nids[2] = { 0, 0 };
     93      1.1  christos     static int pos = 0;
     94      1.1  christos     static int init = 0;
     95      1.1  christos 
     96      1.1  christos     if (!init) {
     97      1.1  christos         const EVP_MD *md;
     98      1.1  christos         if ((md = dasync_sha1()) != NULL)
     99      1.1  christos             digest_nids[pos++] = EVP_MD_get_type(md);
    100      1.1  christos         digest_nids[pos] = 0;
    101      1.1  christos         init = 1;
    102      1.1  christos     }
    103      1.1  christos     *nids = digest_nids;
    104      1.1  christos     return pos;
    105      1.1  christos }
    106      1.1  christos 
    107      1.1  christos /* RSA */
    108      1.1  christos static int dasync_pkey(ENGINE *e, EVP_PKEY_METHOD **pmeth,
    109  1.1.1.2  christos     const int **pnids, int nid);
    110      1.1  christos 
    111      1.1  christos static int dasync_rsa_init(EVP_PKEY_CTX *ctx);
    112      1.1  christos static void dasync_rsa_cleanup(EVP_PKEY_CTX *ctx);
    113      1.1  christos static int dasync_rsa_paramgen_init(EVP_PKEY_CTX *ctx);
    114      1.1  christos static int dasync_rsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
    115      1.1  christos static int dasync_rsa_keygen_init(EVP_PKEY_CTX *ctx);
    116      1.1  christos static int dasync_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
    117      1.1  christos static int dasync_rsa_encrypt_init(EVP_PKEY_CTX *ctx);
    118      1.1  christos static int dasync_rsa_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
    119  1.1.1.2  christos     size_t *outlen, const unsigned char *in,
    120  1.1.1.2  christos     size_t inlen);
    121      1.1  christos static int dasync_rsa_decrypt_init(EVP_PKEY_CTX *ctx);
    122      1.1  christos static int dasync_rsa_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
    123  1.1.1.2  christos     size_t *outlen, const unsigned char *in,
    124  1.1.1.2  christos     size_t inlen);
    125      1.1  christos static int dasync_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
    126      1.1  christos static int dasync_rsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
    127  1.1.1.2  christos     const char *value);
    128      1.1  christos 
    129      1.1  christos static EVP_PKEY_METHOD *dasync_rsa;
    130      1.1  christos static const EVP_PKEY_METHOD *dasync_rsa_orig;
    131      1.1  christos 
    132      1.1  christos /* AES */
    133      1.1  christos 
    134      1.1  christos static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
    135  1.1.1.2  christos     void *ptr);
    136      1.1  christos static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
    137  1.1.1.2  christos     const unsigned char *iv, int enc);
    138      1.1  christos static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
    139  1.1.1.2  christos     const unsigned char *in, size_t inl);
    140      1.1  christos static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx);
    141      1.1  christos 
    142      1.1  christos static int dasync_aes256_ctr_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
    143  1.1.1.2  christos     void *ptr);
    144      1.1  christos static int dasync_aes256_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
    145  1.1.1.2  christos     const unsigned char *iv, int enc);
    146      1.1  christos static int dasync_aes256_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
    147  1.1.1.2  christos     const unsigned char *in, size_t inl);
    148      1.1  christos static int dasync_aes256_ctr_cleanup(EVP_CIPHER_CTX *ctx);
    149      1.1  christos 
    150      1.1  christos static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
    151  1.1.1.2  christos     int arg, void *ptr);
    152      1.1  christos static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
    153  1.1.1.2  christos     const unsigned char *key,
    154  1.1.1.2  christos     const unsigned char *iv,
    155  1.1.1.2  christos     int enc);
    156      1.1  christos static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
    157  1.1.1.2  christos     unsigned char *out,
    158  1.1.1.2  christos     const unsigned char *in,
    159  1.1.1.2  christos     size_t inl);
    160      1.1  christos static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx);
    161      1.1  christos 
    162      1.1  christos struct dasync_pipeline_ctx {
    163      1.1  christos     void *inner_cipher_data;
    164      1.1  christos     unsigned int numpipes;
    165      1.1  christos     unsigned char **inbufs;
    166      1.1  christos     unsigned char **outbufs;
    167      1.1  christos     size_t *lens;
    168      1.1  christos     unsigned char tlsaad[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
    169      1.1  christos     unsigned int aadctr;
    170      1.1  christos };
    171      1.1  christos 
    172      1.1  christos /*
    173      1.1  christos  * Holds the EVP_CIPHER object for aes_128_cbc in this engine. Set up once only
    174      1.1  christos  * during engine bind and can then be reused many times.
    175      1.1  christos  */
    176      1.1  christos static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
    177      1.1  christos static const EVP_CIPHER *dasync_aes_128_cbc(void)
    178      1.1  christos {
    179      1.1  christos     return _hidden_aes_128_cbc;
    180      1.1  christos }
    181      1.1  christos 
    182      1.1  christos static EVP_CIPHER *_hidden_aes_256_ctr = NULL;
    183      1.1  christos static const EVP_CIPHER *dasync_aes_256_ctr(void)
    184      1.1  christos {
    185      1.1  christos     return _hidden_aes_256_ctr;
    186      1.1  christos }
    187      1.1  christos 
    188      1.1  christos /*
    189      1.1  christos  * Holds the EVP_CIPHER object for aes_128_cbc_hmac_sha1 in this engine. Set up
    190      1.1  christos  * once only during engine bind and can then be reused many times.
    191      1.1  christos  *
    192      1.1  christos  * This 'stitched' cipher depends on the EVP_aes_128_cbc_hmac_sha1() cipher,
    193      1.1  christos  * which is implemented only if the AES-NI instruction set extension is available
    194      1.1  christos  * (see OPENSSL_IA32CAP(3)). If that's not the case, then this cipher will not
    195      1.1  christos  * be available either.
    196      1.1  christos  *
    197      1.1  christos  * Note: Since it is a legacy mac-then-encrypt cipher, modern TLS peers (which
    198      1.1  christos  * negotiate the encrypt-then-mac extension) won't negotiate it anyway.
    199      1.1  christos  */
    200      1.1  christos static EVP_CIPHER *_hidden_aes_128_cbc_hmac_sha1 = NULL;
    201      1.1  christos static const EVP_CIPHER *dasync_aes_128_cbc_hmac_sha1(void)
    202      1.1  christos {
    203      1.1  christos     return _hidden_aes_128_cbc_hmac_sha1;
    204      1.1  christos }
    205      1.1  christos 
    206      1.1  christos static void destroy_ciphers(void)
    207      1.1  christos {
    208      1.1  christos     EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
    209      1.1  christos     EVP_CIPHER_meth_free(_hidden_aes_256_ctr);
    210      1.1  christos     EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
    211      1.1  christos     _hidden_aes_128_cbc = NULL;
    212      1.1  christos     _hidden_aes_256_ctr = NULL;
    213      1.1  christos     _hidden_aes_128_cbc_hmac_sha1 = NULL;
    214      1.1  christos }
    215      1.1  christos 
    216      1.1  christos static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
    217  1.1.1.2  christos     const int **nids, int nid);
    218      1.1  christos 
    219      1.1  christos static int dasync_cipher_nids[] = {
    220      1.1  christos     NID_aes_128_cbc,
    221      1.1  christos     NID_aes_256_ctr,
    222      1.1  christos     NID_aes_128_cbc_hmac_sha1,
    223      1.1  christos     0
    224      1.1  christos };
    225      1.1  christos 
    226      1.1  christos static int bind_dasync(ENGINE *e)
    227      1.1  christos {
    228      1.1  christos     /* Setup RSA */
    229      1.1  christos     if ((dasync_rsa_orig = EVP_PKEY_meth_find(EVP_PKEY_RSA)) == NULL
    230      1.1  christos         || (dasync_rsa = EVP_PKEY_meth_new(EVP_PKEY_RSA,
    231  1.1.1.2  christos                 EVP_PKEY_FLAG_AUTOARGLEN))
    232  1.1.1.2  christos             == NULL)
    233      1.1  christos         return 0;
    234      1.1  christos     EVP_PKEY_meth_set_init(dasync_rsa, dasync_rsa_init);
    235      1.1  christos     EVP_PKEY_meth_set_cleanup(dasync_rsa, dasync_rsa_cleanup);
    236      1.1  christos     EVP_PKEY_meth_set_paramgen(dasync_rsa, dasync_rsa_paramgen_init,
    237  1.1.1.2  christos         dasync_rsa_paramgen);
    238      1.1  christos     EVP_PKEY_meth_set_keygen(dasync_rsa, dasync_rsa_keygen_init,
    239  1.1.1.2  christos         dasync_rsa_keygen);
    240      1.1  christos     EVP_PKEY_meth_set_encrypt(dasync_rsa, dasync_rsa_encrypt_init,
    241  1.1.1.2  christos         dasync_rsa_encrypt);
    242      1.1  christos     EVP_PKEY_meth_set_decrypt(dasync_rsa, dasync_rsa_decrypt_init,
    243  1.1.1.2  christos         dasync_rsa_decrypt);
    244      1.1  christos     EVP_PKEY_meth_set_ctrl(dasync_rsa, dasync_rsa_ctrl,
    245  1.1.1.2  christos         dasync_rsa_ctrl_str);
    246      1.1  christos 
    247      1.1  christos     /* Ensure the dasync error handling is set up */
    248      1.1  christos     ERR_load_DASYNC_strings();
    249      1.1  christos 
    250      1.1  christos     if (!ENGINE_set_id(e, engine_dasync_id)
    251      1.1  christos         || !ENGINE_set_name(e, engine_dasync_name)
    252      1.1  christos         || !ENGINE_set_pkey_meths(e, dasync_pkey)
    253      1.1  christos         || !ENGINE_set_digests(e, dasync_digests)
    254      1.1  christos         || !ENGINE_set_ciphers(e, dasync_ciphers)
    255      1.1  christos         || !ENGINE_set_destroy_function(e, dasync_destroy)
    256      1.1  christos         || !ENGINE_set_init_function(e, dasync_init)
    257      1.1  christos         || !ENGINE_set_finish_function(e, dasync_finish)) {
    258      1.1  christos         DASYNCerr(DASYNC_F_BIND_DASYNC, DASYNC_R_INIT_FAILED);
    259      1.1  christos         return 0;
    260      1.1  christos     }
    261      1.1  christos 
    262      1.1  christos     /*
    263      1.1  christos      * Set up the EVP_CIPHER and EVP_MD objects for the ciphers/digests
    264      1.1  christos      * supplied by this engine
    265      1.1  christos      */
    266      1.1  christos     _hidden_sha1_md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption);
    267      1.1  christos     if (_hidden_sha1_md == NULL
    268      1.1  christos         || !EVP_MD_meth_set_result_size(_hidden_sha1_md, SHA_DIGEST_LENGTH)
    269      1.1  christos         || !EVP_MD_meth_set_input_blocksize(_hidden_sha1_md, SHA_CBLOCK)
    270      1.1  christos         || !EVP_MD_meth_set_app_datasize(_hidden_sha1_md,
    271  1.1.1.2  christos             sizeof(EVP_MD *) + sizeof(SHA_CTX))
    272      1.1  christos         || !EVP_MD_meth_set_flags(_hidden_sha1_md, EVP_MD_FLAG_DIGALGID_ABSENT)
    273      1.1  christos         || !EVP_MD_meth_set_init(_hidden_sha1_md, dasync_sha1_init)
    274      1.1  christos         || !EVP_MD_meth_set_update(_hidden_sha1_md, dasync_sha1_update)
    275      1.1  christos         || !EVP_MD_meth_set_final(_hidden_sha1_md, dasync_sha1_final)) {
    276      1.1  christos         EVP_MD_meth_free(_hidden_sha1_md);
    277      1.1  christos         _hidden_sha1_md = NULL;
    278      1.1  christos     }
    279      1.1  christos 
    280      1.1  christos     _hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
    281  1.1.1.2  christos         16 /* block size */,
    282  1.1.1.2  christos         16 /* key len */);
    283      1.1  christos     if (_hidden_aes_128_cbc == NULL
    284  1.1.1.2  christos         || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc, 16)
    285  1.1.1.2  christos         || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
    286  1.1.1.2  christos             EVP_CIPH_FLAG_DEFAULT_ASN1
    287  1.1.1.2  christos                 | EVP_CIPH_CBC_MODE
    288  1.1.1.2  christos                 | EVP_CIPH_FLAG_PIPELINE
    289  1.1.1.2  christos                 | EVP_CIPH_CUSTOM_COPY)
    290  1.1.1.2  christos         || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
    291  1.1.1.2  christos             dasync_aes128_init_key)
    292  1.1.1.2  christos         || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
    293  1.1.1.2  christos             dasync_aes128_cbc_cipher)
    294  1.1.1.2  christos         || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc,
    295  1.1.1.2  christos             dasync_aes128_cbc_cleanup)
    296  1.1.1.2  christos         || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc,
    297  1.1.1.2  christos             dasync_aes128_cbc_ctrl)
    298  1.1.1.2  christos         || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
    299  1.1.1.2  christos             sizeof(struct dasync_pipeline_ctx))) {
    300      1.1  christos         EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
    301      1.1  christos         _hidden_aes_128_cbc = NULL;
    302      1.1  christos     }
    303      1.1  christos 
    304      1.1  christos     _hidden_aes_256_ctr = EVP_CIPHER_meth_new(NID_aes_256_ctr,
    305  1.1.1.2  christos         1 /* block size */,
    306  1.1.1.2  christos         32 /* key len */);
    307      1.1  christos     if (_hidden_aes_256_ctr == NULL
    308  1.1.1.2  christos         || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_256_ctr, 16)
    309  1.1.1.2  christos         || !EVP_CIPHER_meth_set_flags(_hidden_aes_256_ctr,
    310  1.1.1.2  christos             EVP_CIPH_FLAG_DEFAULT_ASN1
    311  1.1.1.2  christos                 | EVP_CIPH_CTR_MODE
    312  1.1.1.2  christos                 | EVP_CIPH_FLAG_PIPELINE
    313  1.1.1.2  christos                 | EVP_CIPH_CUSTOM_COPY)
    314  1.1.1.2  christos         || !EVP_CIPHER_meth_set_init(_hidden_aes_256_ctr,
    315  1.1.1.2  christos             dasync_aes256_init_key)
    316  1.1.1.2  christos         || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_256_ctr,
    317  1.1.1.2  christos             dasync_aes256_ctr_cipher)
    318  1.1.1.2  christos         || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_256_ctr,
    319  1.1.1.2  christos             dasync_aes256_ctr_cleanup)
    320  1.1.1.2  christos         || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_256_ctr,
    321  1.1.1.2  christos             dasync_aes256_ctr_ctrl)
    322  1.1.1.2  christos         || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_256_ctr,
    323  1.1.1.2  christos             sizeof(struct dasync_pipeline_ctx))) {
    324      1.1  christos         EVP_CIPHER_meth_free(_hidden_aes_256_ctr);
    325      1.1  christos         _hidden_aes_256_ctr = NULL;
    326      1.1  christos     }
    327      1.1  christos 
    328      1.1  christos     _hidden_aes_128_cbc_hmac_sha1 = EVP_CIPHER_meth_new(
    329  1.1.1.2  christos         NID_aes_128_cbc_hmac_sha1,
    330  1.1.1.2  christos         16 /* block size */,
    331  1.1.1.2  christos         16 /* key len */);
    332      1.1  christos     if (_hidden_aes_128_cbc_hmac_sha1 == NULL
    333  1.1.1.2  christos         || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1, 16)
    334  1.1.1.2  christos         || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1,
    335  1.1.1.2  christos             EVP_CIPH_CBC_MODE
    336  1.1.1.2  christos                 | EVP_CIPH_FLAG_DEFAULT_ASN1
    337  1.1.1.2  christos                 | EVP_CIPH_FLAG_AEAD_CIPHER
    338  1.1.1.2  christos                 | EVP_CIPH_FLAG_PIPELINE
    339  1.1.1.2  christos                 | EVP_CIPH_CUSTOM_COPY)
    340  1.1.1.2  christos         || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1,
    341  1.1.1.2  christos             dasync_aes128_cbc_hmac_sha1_init_key)
    342  1.1.1.2  christos         || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1,
    343  1.1.1.2  christos             dasync_aes128_cbc_hmac_sha1_cipher)
    344  1.1.1.2  christos         || !EVP_CIPHER_meth_set_cleanup(_hidden_aes_128_cbc_hmac_sha1,
    345  1.1.1.2  christos             dasync_aes128_cbc_hmac_sha1_cleanup)
    346  1.1.1.2  christos         || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1,
    347  1.1.1.2  christos             dasync_aes128_cbc_hmac_sha1_ctrl)
    348  1.1.1.2  christos         || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1,
    349  1.1.1.2  christos             sizeof(struct dasync_pipeline_ctx))) {
    350      1.1  christos         EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
    351      1.1  christos         _hidden_aes_128_cbc_hmac_sha1 = NULL;
    352      1.1  christos     }
    353      1.1  christos 
    354      1.1  christos     return 1;
    355      1.1  christos }
    356      1.1  christos 
    357      1.1  christos static void destroy_pkey(void)
    358      1.1  christos {
    359      1.1  christos     /*
    360      1.1  christos      * We don't actually need to free the dasync_rsa method since this is
    361      1.1  christos      * automatically freed for us by libcrypto.
    362      1.1  christos      */
    363      1.1  christos     dasync_rsa_orig = NULL;
    364      1.1  christos     dasync_rsa = NULL;
    365      1.1  christos }
    366      1.1  christos 
    367  1.1.1.2  christos #ifndef OPENSSL_NO_DYNAMIC_ENGINE
    368      1.1  christos static int bind_helper(ENGINE *e, const char *id)
    369      1.1  christos {
    370      1.1  christos     if (id && (strcmp(id, engine_dasync_id) != 0))
    371      1.1  christos         return 0;
    372      1.1  christos     if (!bind_dasync(e))
    373      1.1  christos         return 0;
    374      1.1  christos     return 1;
    375      1.1  christos }
    376      1.1  christos 
    377      1.1  christos IMPLEMENT_DYNAMIC_CHECK_FN()
    378  1.1.1.2  christos IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
    379  1.1.1.2  christos #endif
    380      1.1  christos 
    381      1.1  christos static ENGINE *engine_dasync(void)
    382      1.1  christos {
    383      1.1  christos     ENGINE *ret = ENGINE_new();
    384      1.1  christos     if (!ret)
    385      1.1  christos         return NULL;
    386      1.1  christos     if (!bind_dasync(ret)) {
    387      1.1  christos         ENGINE_free(ret);
    388      1.1  christos         return NULL;
    389      1.1  christos     }
    390      1.1  christos     return ret;
    391      1.1  christos }
    392      1.1  christos 
    393      1.1  christos void engine_load_dasync_int(void)
    394      1.1  christos {
    395      1.1  christos     ENGINE *toadd = engine_dasync();
    396      1.1  christos     if (!toadd)
    397      1.1  christos         return;
    398      1.1  christos     ERR_set_mark();
    399      1.1  christos     ENGINE_add(toadd);
    400      1.1  christos     /*
    401      1.1  christos      * If the "add" worked, it gets a structural reference. So either way, we
    402      1.1  christos      * release our just-created reference.
    403      1.1  christos      */
    404      1.1  christos     ENGINE_free(toadd);
    405      1.1  christos     /*
    406      1.1  christos      * If the "add" didn't work, it was probably a conflict because it was
    407      1.1  christos      * already added (eg. someone calling ENGINE_load_blah then calling
    408      1.1  christos      * ENGINE_load_builtin_engines() perhaps).
    409      1.1  christos      */
    410      1.1  christos     ERR_pop_to_mark();
    411      1.1  christos }
    412      1.1  christos 
    413      1.1  christos static int dasync_init(ENGINE *e)
    414      1.1  christos {
    415      1.1  christos     return 1;
    416      1.1  christos }
    417      1.1  christos 
    418      1.1  christos static int dasync_finish(ENGINE *e)
    419      1.1  christos {
    420      1.1  christos     return 1;
    421      1.1  christos }
    422      1.1  christos 
    423      1.1  christos static int dasync_destroy(ENGINE *e)
    424      1.1  christos {
    425      1.1  christos     destroy_digests();
    426      1.1  christos     destroy_ciphers();
    427      1.1  christos     destroy_pkey();
    428      1.1  christos     ERR_unload_DASYNC_strings();
    429      1.1  christos     return 1;
    430      1.1  christos }
    431      1.1  christos 
    432      1.1  christos static int dasync_pkey(ENGINE *e, EVP_PKEY_METHOD **pmeth,
    433  1.1.1.2  christos     const int **pnids, int nid)
    434      1.1  christos {
    435      1.1  christos     static const int rnid = EVP_PKEY_RSA;
    436      1.1  christos 
    437      1.1  christos     if (pmeth == NULL) {
    438      1.1  christos         *pnids = &rnid;
    439      1.1  christos         return 1;
    440      1.1  christos     }
    441      1.1  christos 
    442      1.1  christos     if (nid == EVP_PKEY_RSA) {
    443      1.1  christos         *pmeth = dasync_rsa;
    444      1.1  christos         return 1;
    445      1.1  christos     }
    446      1.1  christos 
    447      1.1  christos     *pmeth = NULL;
    448      1.1  christos     return 0;
    449      1.1  christos }
    450      1.1  christos 
    451      1.1  christos static int dasync_digests(ENGINE *e, const EVP_MD **digest,
    452  1.1.1.2  christos     const int **nids, int nid)
    453      1.1  christos {
    454      1.1  christos     int ok = 1;
    455      1.1  christos     if (!digest) {
    456      1.1  christos         /* We are returning a list of supported nids */
    457      1.1  christos         return dasync_digest_nids(nids);
    458      1.1  christos     }
    459      1.1  christos     /* We are being asked for a specific digest */
    460      1.1  christos     switch (nid) {
    461      1.1  christos     case NID_sha1:
    462      1.1  christos         *digest = dasync_sha1();
    463      1.1  christos         break;
    464      1.1  christos     default:
    465      1.1  christos         ok = 0;
    466      1.1  christos         *digest = NULL;
    467      1.1  christos         break;
    468      1.1  christos     }
    469      1.1  christos     return ok;
    470      1.1  christos }
    471      1.1  christos 
    472      1.1  christos static int dasync_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
    473  1.1.1.2  christos     const int **nids, int nid)
    474      1.1  christos {
    475      1.1  christos     int ok = 1;
    476      1.1  christos     if (cipher == NULL) {
    477      1.1  christos         /* We are returning a list of supported nids */
    478      1.1  christos         *nids = dasync_cipher_nids;
    479  1.1.1.2  christos         return (sizeof(dasync_cipher_nids) - 1) / sizeof(dasync_cipher_nids[0]);
    480      1.1  christos     }
    481      1.1  christos     /* We are being asked for a specific cipher */
    482      1.1  christos     switch (nid) {
    483      1.1  christos     case NID_aes_128_cbc:
    484      1.1  christos         *cipher = dasync_aes_128_cbc();
    485      1.1  christos         break;
    486      1.1  christos     case NID_aes_256_ctr:
    487      1.1  christos         *cipher = dasync_aes_256_ctr();
    488      1.1  christos         break;
    489      1.1  christos     case NID_aes_128_cbc_hmac_sha1:
    490      1.1  christos         *cipher = dasync_aes_128_cbc_hmac_sha1();
    491      1.1  christos         break;
    492      1.1  christos     default:
    493      1.1  christos         ok = 0;
    494      1.1  christos         *cipher = NULL;
    495      1.1  christos         break;
    496      1.1  christos     }
    497      1.1  christos     return ok;
    498      1.1  christos }
    499      1.1  christos 
    500      1.1  christos static void wait_cleanup(ASYNC_WAIT_CTX *ctx, const void *key,
    501  1.1.1.2  christos     OSSL_ASYNC_FD readfd, void *pvwritefd)
    502      1.1  christos {
    503      1.1  christos     OSSL_ASYNC_FD *pwritefd = (OSSL_ASYNC_FD *)pvwritefd;
    504      1.1  christos #if defined(ASYNC_WIN)
    505      1.1  christos     CloseHandle(readfd);
    506      1.1  christos     CloseHandle(*pwritefd);
    507      1.1  christos #elif defined(ASYNC_POSIX)
    508      1.1  christos     close(readfd);
    509      1.1  christos     close(*pwritefd);
    510      1.1  christos #endif
    511      1.1  christos     OPENSSL_free(pwritefd);
    512      1.1  christos }
    513      1.1  christos 
    514      1.1  christos #define DUMMY_CHAR 'X'
    515      1.1  christos 
    516  1.1.1.2  christos static void dummy_pause_job(void)
    517  1.1.1.2  christos {
    518      1.1  christos     ASYNC_JOB *job;
    519      1.1  christos     ASYNC_WAIT_CTX *waitctx;
    520      1.1  christos     ASYNC_callback_fn callback;
    521      1.1  christos     void *callback_arg;
    522  1.1.1.2  christos     OSSL_ASYNC_FD pipefds[2] = { 0, 0 };
    523      1.1  christos     OSSL_ASYNC_FD *writefd;
    524      1.1  christos #if defined(ASYNC_WIN)
    525      1.1  christos     DWORD numwritten, numread;
    526      1.1  christos     char buf = DUMMY_CHAR;
    527      1.1  christos #elif defined(ASYNC_POSIX)
    528      1.1  christos     char buf = DUMMY_CHAR;
    529      1.1  christos #endif
    530      1.1  christos 
    531      1.1  christos     if ((job = ASYNC_get_current_job()) == NULL)
    532      1.1  christos         return;
    533      1.1  christos 
    534      1.1  christos     waitctx = ASYNC_get_wait_ctx(job);
    535      1.1  christos 
    536      1.1  christos     if (ASYNC_WAIT_CTX_get_callback(waitctx, &callback, &callback_arg) && callback != NULL) {
    537      1.1  christos         /*
    538      1.1  christos          * In the Dummy async engine we are cheating. We call the callback that the job
    539      1.1  christos          * is complete before the call to ASYNC_pause_job(). A real
    540      1.1  christos          * async engine would only call the callback when the job was actually complete
    541      1.1  christos          */
    542      1.1  christos         (*callback)(callback_arg);
    543      1.1  christos         ASYNC_pause_job();
    544      1.1  christos         return;
    545      1.1  christos     }
    546      1.1  christos 
    547      1.1  christos     if (ASYNC_WAIT_CTX_get_fd(waitctx, engine_dasync_id, &pipefds[0],
    548  1.1.1.2  christos             (void **)&writefd)) {
    549      1.1  christos         pipefds[1] = *writefd;
    550      1.1  christos     } else {
    551      1.1  christos         writefd = OPENSSL_malloc(sizeof(*writefd));
    552      1.1  christos         if (writefd == NULL)
    553      1.1  christos             return;
    554      1.1  christos #if defined(ASYNC_WIN)
    555      1.1  christos         if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0) {
    556      1.1  christos             OPENSSL_free(writefd);
    557      1.1  christos             return;
    558      1.1  christos         }
    559      1.1  christos #elif defined(ASYNC_POSIX)
    560      1.1  christos         if (pipe(pipefds) != 0) {
    561      1.1  christos             OPENSSL_free(writefd);
    562      1.1  christos             return;
    563      1.1  christos         }
    564      1.1  christos #endif
    565      1.1  christos         *writefd = pipefds[1];
    566      1.1  christos 
    567      1.1  christos         if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, engine_dasync_id, pipefds[0],
    568  1.1.1.2  christos                 writefd, wait_cleanup)) {
    569      1.1  christos             wait_cleanup(waitctx, engine_dasync_id, pipefds[0], writefd);
    570      1.1  christos             return;
    571      1.1  christos         }
    572      1.1  christos     }
    573      1.1  christos     /*
    574      1.1  christos      * In the Dummy async engine we are cheating. We signal that the job
    575      1.1  christos      * is complete by waking it before the call to ASYNC_pause_job(). A real
    576      1.1  christos      * async engine would only wake when the job was actually complete
    577      1.1  christos      */
    578      1.1  christos #if defined(ASYNC_WIN)
    579      1.1  christos     WriteFile(pipefds[1], &buf, 1, &numwritten, NULL);
    580      1.1  christos #elif defined(ASYNC_POSIX)
    581      1.1  christos     if (write(pipefds[1], &buf, 1) < 0)
    582      1.1  christos         return;
    583      1.1  christos #endif
    584      1.1  christos 
    585      1.1  christos     /* Ignore errors - we carry on anyway */
    586      1.1  christos     ASYNC_pause_job();
    587      1.1  christos 
    588      1.1  christos     /* Clear the wake signal */
    589      1.1  christos #if defined(ASYNC_WIN)
    590      1.1  christos     ReadFile(pipefds[0], &buf, 1, &numread, NULL);
    591      1.1  christos #elif defined(ASYNC_POSIX)
    592      1.1  christos     if (read(pipefds[0], &buf, 1) < 0)
    593      1.1  christos         return;
    594      1.1  christos #endif
    595      1.1  christos }
    596      1.1  christos 
    597      1.1  christos /*
    598      1.1  christos  * SHA1 implementation. At the moment we just defer to the standard
    599      1.1  christos  * implementation
    600      1.1  christos  */
    601      1.1  christos static int dasync_sha1_init(EVP_MD_CTX *ctx)
    602      1.1  christos {
    603      1.1  christos     dummy_pause_job();
    604      1.1  christos 
    605      1.1  christos     return EVP_MD_meth_get_init(EVP_sha1())(ctx);
    606      1.1  christos }
    607      1.1  christos 
    608      1.1  christos static int dasync_sha1_update(EVP_MD_CTX *ctx, const void *data,
    609  1.1.1.2  christos     size_t count)
    610      1.1  christos {
    611      1.1  christos     dummy_pause_job();
    612      1.1  christos 
    613      1.1  christos     return EVP_MD_meth_get_update(EVP_sha1())(ctx, data, count);
    614      1.1  christos }
    615      1.1  christos 
    616      1.1  christos static int dasync_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
    617      1.1  christos {
    618      1.1  christos     dummy_pause_job();
    619      1.1  christos 
    620      1.1  christos     return EVP_MD_meth_get_final(EVP_sha1())(ctx, md);
    621      1.1  christos }
    622      1.1  christos 
    623      1.1  christos /* Cipher helper functions */
    624      1.1  christos 
    625      1.1  christos static int dasync_cipher_ctrl_helper(EVP_CIPHER_CTX *ctx, int type, int arg,
    626  1.1.1.2  christos     void *ptr, int aeadcapable,
    627  1.1.1.2  christos     const EVP_CIPHER *ciph)
    628      1.1  christos {
    629      1.1  christos     int ret;
    630  1.1.1.2  christos     struct dasync_pipeline_ctx *pipe_ctx = (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
    631      1.1  christos 
    632      1.1  christos     if (pipe_ctx == NULL)
    633      1.1  christos         return 0;
    634      1.1  christos 
    635      1.1  christos     switch (type) {
    636  1.1.1.2  christos     case EVP_CTRL_COPY: {
    637  1.1.1.2  christos         size_t sz = EVP_CIPHER_impl_ctx_size(ciph);
    638  1.1.1.2  christos         void *inner_cipher_data = OPENSSL_malloc(sz);
    639      1.1  christos 
    640  1.1.1.2  christos         if (inner_cipher_data == NULL)
    641  1.1.1.2  christos             return -1;
    642  1.1.1.2  christos         memcpy(inner_cipher_data, pipe_ctx->inner_cipher_data, sz);
    643  1.1.1.2  christos         pipe_ctx->inner_cipher_data = inner_cipher_data;
    644  1.1.1.2  christos     } break;
    645  1.1.1.2  christos 
    646  1.1.1.2  christos     case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS:
    647  1.1.1.2  christos         pipe_ctx->numpipes = arg;
    648  1.1.1.2  christos         pipe_ctx->outbufs = (unsigned char **)ptr;
    649  1.1.1.2  christos         break;
    650      1.1  christos 
    651  1.1.1.2  christos     case EVP_CTRL_SET_PIPELINE_INPUT_BUFS:
    652  1.1.1.2  christos         pipe_ctx->numpipes = arg;
    653  1.1.1.2  christos         pipe_ctx->inbufs = (unsigned char **)ptr;
    654  1.1.1.2  christos         break;
    655      1.1  christos 
    656  1.1.1.2  christos     case EVP_CTRL_SET_PIPELINE_INPUT_LENS:
    657  1.1.1.2  christos         pipe_ctx->numpipes = arg;
    658  1.1.1.2  christos         pipe_ctx->lens = (size_t *)ptr;
    659  1.1.1.2  christos         break;
    660      1.1  christos 
    661  1.1.1.2  christos     case EVP_CTRL_AEAD_SET_MAC_KEY:
    662  1.1.1.2  christos         if (!aeadcapable)
    663  1.1.1.2  christos             return -1;
    664  1.1.1.2  christos         EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
    665  1.1.1.2  christos         ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_cbc_hmac_sha1())(ctx, type, arg, ptr);
    666  1.1.1.2  christos         EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
    667  1.1.1.2  christos         return ret;
    668  1.1.1.2  christos 
    669  1.1.1.2  christos     case EVP_CTRL_AEAD_TLS1_AAD: {
    670  1.1.1.2  christos         unsigned char *p = ptr;
    671  1.1.1.2  christos         unsigned int len;
    672  1.1.1.2  christos 
    673  1.1.1.2  christos         if (!aeadcapable || arg != EVP_AEAD_TLS1_AAD_LEN)
    674  1.1.1.2  christos             return -1;
    675      1.1  christos 
    676  1.1.1.2  christos         if (pipe_ctx->aadctr >= SSL_MAX_PIPELINES)
    677  1.1.1.2  christos             return -1;
    678  1.1.1.2  christos 
    679  1.1.1.2  christos         memcpy(pipe_ctx->tlsaad[pipe_ctx->aadctr], ptr,
    680  1.1.1.2  christos             EVP_AEAD_TLS1_AAD_LEN);
    681  1.1.1.2  christos         pipe_ctx->aadctr++;
    682  1.1.1.2  christos 
    683  1.1.1.2  christos         len = p[arg - 2] << 8 | p[arg - 1];
    684  1.1.1.2  christos 
    685  1.1.1.2  christos         if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
    686  1.1.1.2  christos             if ((p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
    687  1.1.1.2  christos                 if (len < AES_BLOCK_SIZE)
    688  1.1.1.2  christos                     return 0;
    689  1.1.1.2  christos                 len -= AES_BLOCK_SIZE;
    690      1.1  christos             }
    691  1.1.1.2  christos 
    692  1.1.1.2  christos             return ((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE)
    693  1.1.1.2  christos                        & -AES_BLOCK_SIZE)
    694  1.1.1.2  christos                 - len;
    695  1.1.1.2  christos         } else {
    696  1.1.1.2  christos             return SHA_DIGEST_LENGTH;
    697      1.1  christos         }
    698  1.1.1.2  christos     }
    699      1.1  christos 
    700  1.1.1.2  christos     default:
    701  1.1.1.2  christos         return 0;
    702      1.1  christos     }
    703      1.1  christos 
    704      1.1  christos     return 1;
    705      1.1  christos }
    706      1.1  christos 
    707      1.1  christos static int dasync_cipher_init_key_helper(EVP_CIPHER_CTX *ctx,
    708  1.1.1.2  christos     const unsigned char *key,
    709  1.1.1.2  christos     const unsigned char *iv, int enc,
    710  1.1.1.2  christos     const EVP_CIPHER *cipher)
    711      1.1  christos {
    712      1.1  christos     int ret;
    713  1.1.1.2  christos     struct dasync_pipeline_ctx *pipe_ctx = (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
    714      1.1  christos 
    715      1.1  christos     if (pipe_ctx->inner_cipher_data == NULL
    716  1.1.1.2  christos         && EVP_CIPHER_impl_ctx_size(cipher) != 0) {
    717      1.1  christos         pipe_ctx->inner_cipher_data = OPENSSL_zalloc(
    718      1.1  christos             EVP_CIPHER_impl_ctx_size(cipher));
    719      1.1  christos         if (pipe_ctx->inner_cipher_data == NULL)
    720      1.1  christos             return 0;
    721      1.1  christos     }
    722      1.1  christos 
    723      1.1  christos     pipe_ctx->numpipes = 0;
    724      1.1  christos     pipe_ctx->aadctr = 0;
    725      1.1  christos 
    726      1.1  christos     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
    727      1.1  christos     ret = EVP_CIPHER_meth_get_init(cipher)(ctx, key, iv, enc);
    728      1.1  christos     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
    729      1.1  christos 
    730      1.1  christos     return ret;
    731      1.1  christos }
    732      1.1  christos 
    733      1.1  christos static int dasync_cipher_helper(EVP_CIPHER_CTX *ctx, unsigned char *out,
    734  1.1.1.2  christos     const unsigned char *in, size_t inl,
    735  1.1.1.2  christos     const EVP_CIPHER *cipher)
    736      1.1  christos {
    737      1.1  christos     int ret = 1;
    738      1.1  christos     unsigned int i, pipes;
    739  1.1.1.2  christos     struct dasync_pipeline_ctx *pipe_ctx = (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
    740      1.1  christos 
    741      1.1  christos     pipes = pipe_ctx->numpipes;
    742      1.1  christos     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx->inner_cipher_data);
    743      1.1  christos     if (pipes == 0) {
    744      1.1  christos         if (pipe_ctx->aadctr != 0) {
    745      1.1  christos             if (pipe_ctx->aadctr != 1)
    746      1.1  christos                 return -1;
    747  1.1.1.2  christos             EVP_CIPHER_meth_get_ctrl(cipher)(ctx, EVP_CTRL_AEAD_TLS1_AAD,
    748  1.1.1.2  christos                 EVP_AEAD_TLS1_AAD_LEN,
    749  1.1.1.2  christos                 pipe_ctx->tlsaad[0]);
    750      1.1  christos         }
    751  1.1.1.2  christos         ret = EVP_CIPHER_meth_get_do_cipher(cipher)(ctx, out, in, inl);
    752      1.1  christos     } else {
    753      1.1  christos         if (pipe_ctx->aadctr > 0 && pipe_ctx->aadctr != pipes)
    754      1.1  christos             return -1;
    755      1.1  christos         for (i = 0; i < pipes; i++) {
    756      1.1  christos             if (pipe_ctx->aadctr > 0) {
    757  1.1.1.2  christos                 EVP_CIPHER_meth_get_ctrl(cipher)(ctx, EVP_CTRL_AEAD_TLS1_AAD,
    758  1.1.1.2  christos                     EVP_AEAD_TLS1_AAD_LEN,
    759  1.1.1.2  christos                     pipe_ctx->tlsaad[i]);
    760      1.1  christos             }
    761  1.1.1.2  christos             ret = ret && EVP_CIPHER_meth_get_do_cipher(cipher)(ctx, pipe_ctx->outbufs[i], pipe_ctx->inbufs[i], pipe_ctx->lens[i]);
    762      1.1  christos         }
    763      1.1  christos         pipe_ctx->numpipes = 0;
    764      1.1  christos     }
    765      1.1  christos     pipe_ctx->aadctr = 0;
    766      1.1  christos     EVP_CIPHER_CTX_set_cipher_data(ctx, pipe_ctx);
    767      1.1  christos     return ret;
    768      1.1  christos }
    769      1.1  christos 
    770      1.1  christos static int dasync_cipher_cleanup_helper(EVP_CIPHER_CTX *ctx,
    771  1.1.1.2  christos     const EVP_CIPHER *cipher)
    772      1.1  christos {
    773  1.1.1.2  christos     struct dasync_pipeline_ctx *pipe_ctx = (struct dasync_pipeline_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx);
    774      1.1  christos 
    775      1.1  christos     OPENSSL_clear_free(pipe_ctx->inner_cipher_data,
    776  1.1.1.2  christos         EVP_CIPHER_impl_ctx_size(cipher));
    777      1.1  christos 
    778      1.1  christos     return 1;
    779      1.1  christos }
    780      1.1  christos 
    781      1.1  christos /*
    782      1.1  christos  * AES128 CBC Implementation
    783      1.1  christos  */
    784      1.1  christos 
    785      1.1  christos static int dasync_aes128_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
    786  1.1.1.2  christos     void *ptr)
    787      1.1  christos {
    788      1.1  christos     return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0, EVP_aes_128_cbc());
    789      1.1  christos }
    790      1.1  christos 
    791      1.1  christos static int dasync_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
    792  1.1.1.2  christos     const unsigned char *iv, int enc)
    793      1.1  christos {
    794      1.1  christos     return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_128_cbc());
    795      1.1  christos }
    796      1.1  christos 
    797      1.1  christos static int dasync_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
    798  1.1.1.2  christos     const unsigned char *in, size_t inl)
    799      1.1  christos {
    800      1.1  christos     return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc());
    801      1.1  christos }
    802      1.1  christos 
    803      1.1  christos static int dasync_aes128_cbc_cleanup(EVP_CIPHER_CTX *ctx)
    804      1.1  christos {
    805      1.1  christos     return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc());
    806      1.1  christos }
    807      1.1  christos 
    808      1.1  christos static int dasync_aes256_ctr_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
    809  1.1.1.2  christos     void *ptr)
    810      1.1  christos {
    811      1.1  christos     return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 0, EVP_aes_256_ctr());
    812      1.1  christos }
    813      1.1  christos 
    814      1.1  christos static int dasync_aes256_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
    815  1.1.1.2  christos     const unsigned char *iv, int enc)
    816      1.1  christos {
    817      1.1  christos     return dasync_cipher_init_key_helper(ctx, key, iv, enc, EVP_aes_256_ctr());
    818      1.1  christos }
    819      1.1  christos 
    820      1.1  christos static int dasync_aes256_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
    821  1.1.1.2  christos     const unsigned char *in, size_t inl)
    822      1.1  christos {
    823      1.1  christos     return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_256_ctr());
    824      1.1  christos }
    825      1.1  christos 
    826      1.1  christos static int dasync_aes256_ctr_cleanup(EVP_CIPHER_CTX *ctx)
    827      1.1  christos {
    828      1.1  christos     return dasync_cipher_cleanup_helper(ctx, EVP_aes_256_ctr());
    829      1.1  christos }
    830      1.1  christos 
    831      1.1  christos /*
    832      1.1  christos  * AES128 CBC HMAC SHA1 Implementation
    833      1.1  christos  */
    834      1.1  christos 
    835      1.1  christos static int dasync_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
    836  1.1.1.2  christos     int arg, void *ptr)
    837      1.1  christos {
    838      1.1  christos     return dasync_cipher_ctrl_helper(ctx, type, arg, ptr, 1, EVP_aes_128_cbc_hmac_sha1());
    839      1.1  christos }
    840      1.1  christos 
    841      1.1  christos static int dasync_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
    842  1.1.1.2  christos     const unsigned char *key,
    843  1.1.1.2  christos     const unsigned char *iv,
    844  1.1.1.2  christos     int enc)
    845      1.1  christos {
    846      1.1  christos     /*
    847      1.1  christos      * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
    848      1.1  christos      * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
    849      1.1  christos      */
    850      1.1  christos     return dasync_cipher_init_key_helper(ctx, key, iv, enc,
    851  1.1.1.2  christos         EVP_aes_128_cbc_hmac_sha1());
    852      1.1  christos }
    853      1.1  christos 
    854      1.1  christos static int dasync_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
    855  1.1.1.2  christos     unsigned char *out,
    856  1.1.1.2  christos     const unsigned char *in,
    857  1.1.1.2  christos     size_t inl)
    858      1.1  christos {
    859      1.1  christos     return dasync_cipher_helper(ctx, out, in, inl, EVP_aes_128_cbc_hmac_sha1());
    860      1.1  christos }
    861      1.1  christos 
    862      1.1  christos static int dasync_aes128_cbc_hmac_sha1_cleanup(EVP_CIPHER_CTX *ctx)
    863      1.1  christos {
    864      1.1  christos     /*
    865      1.1  christos      * We can safely assume that EVP_aes_128_cbc_hmac_sha1() != NULL,
    866      1.1  christos      * see comment before the definition of dasync_aes_128_cbc_hmac_sha1().
    867      1.1  christos      */
    868      1.1  christos     return dasync_cipher_cleanup_helper(ctx, EVP_aes_128_cbc_hmac_sha1());
    869      1.1  christos }
    870      1.1  christos 
    871      1.1  christos /*
    872      1.1  christos  * RSA implementation
    873      1.1  christos  */
    874      1.1  christos static int dasync_rsa_init(EVP_PKEY_CTX *ctx)
    875      1.1  christos {
    876      1.1  christos     static int (*pinit)(EVP_PKEY_CTX *ctx);
    877      1.1  christos 
    878      1.1  christos     if (pinit == NULL)
    879      1.1  christos         EVP_PKEY_meth_get_init(dasync_rsa_orig, &pinit);
    880      1.1  christos     return pinit(ctx);
    881      1.1  christos }
    882      1.1  christos 
    883      1.1  christos static void dasync_rsa_cleanup(EVP_PKEY_CTX *ctx)
    884      1.1  christos {
    885      1.1  christos     static void (*pcleanup)(EVP_PKEY_CTX *ctx);
    886      1.1  christos 
    887      1.1  christos     if (pcleanup == NULL)
    888      1.1  christos         EVP_PKEY_meth_get_cleanup(dasync_rsa_orig, &pcleanup);
    889      1.1  christos     pcleanup(ctx);
    890      1.1  christos }
    891      1.1  christos 
    892      1.1  christos static int dasync_rsa_paramgen_init(EVP_PKEY_CTX *ctx)
    893      1.1  christos {
    894      1.1  christos     static int (*pparamgen_init)(EVP_PKEY_CTX *ctx);
    895      1.1  christos 
    896      1.1  christos     if (pparamgen_init == NULL)
    897      1.1  christos         EVP_PKEY_meth_get_paramgen(dasync_rsa_orig, &pparamgen_init, NULL);
    898      1.1  christos     return pparamgen_init != NULL ? pparamgen_init(ctx) : 1;
    899      1.1  christos }
    900      1.1  christos 
    901      1.1  christos static int dasync_rsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
    902      1.1  christos {
    903      1.1  christos     static int (*pparamgen)(EVP_PKEY_CTX *c, EVP_PKEY *pkey);
    904      1.1  christos 
    905      1.1  christos     if (pparamgen == NULL)
    906      1.1  christos         EVP_PKEY_meth_get_paramgen(dasync_rsa_orig, NULL, &pparamgen);
    907      1.1  christos     return pparamgen != NULL ? pparamgen(ctx, pkey) : 1;
    908      1.1  christos }
    909      1.1  christos 
    910      1.1  christos static int dasync_rsa_keygen_init(EVP_PKEY_CTX *ctx)
    911      1.1  christos {
    912      1.1  christos     static int (*pkeygen_init)(EVP_PKEY_CTX *ctx);
    913      1.1  christos 
    914      1.1  christos     if (pkeygen_init == NULL)
    915      1.1  christos         EVP_PKEY_meth_get_keygen(dasync_rsa_orig, &pkeygen_init, NULL);
    916      1.1  christos     return pkeygen_init != NULL ? pkeygen_init(ctx) : 1;
    917      1.1  christos }
    918      1.1  christos 
    919      1.1  christos static int dasync_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
    920      1.1  christos {
    921      1.1  christos     static int (*pkeygen)(EVP_PKEY_CTX *c, EVP_PKEY *pkey);
    922      1.1  christos 
    923      1.1  christos     if (pkeygen == NULL)
    924      1.1  christos         EVP_PKEY_meth_get_keygen(dasync_rsa_orig, NULL, &pkeygen);
    925      1.1  christos     return pkeygen(ctx, pkey);
    926      1.1  christos }
    927      1.1  christos 
    928      1.1  christos static int dasync_rsa_encrypt_init(EVP_PKEY_CTX *ctx)
    929      1.1  christos {
    930      1.1  christos     static int (*pencrypt_init)(EVP_PKEY_CTX *ctx);
    931      1.1  christos 
    932      1.1  christos     if (pencrypt_init == NULL)
    933      1.1  christos         EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, &pencrypt_init, NULL);
    934      1.1  christos     return pencrypt_init != NULL ? pencrypt_init(ctx) : 1;
    935      1.1  christos }
    936      1.1  christos 
    937      1.1  christos static int dasync_rsa_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
    938  1.1.1.2  christos     size_t *outlen, const unsigned char *in,
    939  1.1.1.2  christos     size_t inlen)
    940      1.1  christos {
    941      1.1  christos     static int (*pencryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out,
    942  1.1.1.2  christos         size_t *outlen, const unsigned char *in,
    943  1.1.1.2  christos         size_t inlen);
    944      1.1  christos 
    945      1.1  christos     if (pencryptfn == NULL)
    946      1.1  christos         EVP_PKEY_meth_get_encrypt(dasync_rsa_orig, NULL, &pencryptfn);
    947      1.1  christos     return pencryptfn(ctx, out, outlen, in, inlen);
    948      1.1  christos }
    949      1.1  christos 
    950      1.1  christos static int dasync_rsa_decrypt_init(EVP_PKEY_CTX *ctx)
    951      1.1  christos {
    952      1.1  christos     static int (*pdecrypt_init)(EVP_PKEY_CTX *ctx);
    953      1.1  christos 
    954      1.1  christos     if (pdecrypt_init == NULL)
    955      1.1  christos         EVP_PKEY_meth_get_decrypt(dasync_rsa_orig, &pdecrypt_init, NULL);
    956      1.1  christos     return pdecrypt_init != NULL ? pdecrypt_init(ctx) : 1;
    957      1.1  christos }
    958      1.1  christos 
    959      1.1  christos static int dasync_rsa_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
    960  1.1.1.2  christos     size_t *outlen, const unsigned char *in,
    961  1.1.1.2  christos     size_t inlen)
    962      1.1  christos {
    963      1.1  christos     static int (*pdecrypt)(EVP_PKEY_CTX *ctx, unsigned char *out,
    964  1.1.1.2  christos         size_t *outlen, const unsigned char *in,
    965  1.1.1.2  christos         size_t inlen);
    966      1.1  christos 
    967      1.1  christos     if (pdecrypt == NULL)
    968      1.1  christos         EVP_PKEY_meth_get_decrypt(dasync_rsa_orig, NULL, &pdecrypt);
    969      1.1  christos     return pdecrypt(ctx, out, outlen, in, inlen);
    970      1.1  christos }
    971      1.1  christos 
    972      1.1  christos static int dasync_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
    973      1.1  christos {
    974      1.1  christos     static int (*pctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
    975      1.1  christos 
    976      1.1  christos     if (pctrl == NULL)
    977      1.1  christos         EVP_PKEY_meth_get_ctrl(dasync_rsa_orig, &pctrl, NULL);
    978      1.1  christos     return pctrl(ctx, type, p1, p2);
    979      1.1  christos }
    980      1.1  christos 
    981      1.1  christos static int dasync_rsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
    982  1.1.1.2  christos     const char *value)
    983      1.1  christos {
    984      1.1  christos     static int (*pctrl_str)(EVP_PKEY_CTX *ctx, const char *type,
    985  1.1.1.2  christos         const char *value);
    986      1.1  christos 
    987      1.1  christos     if (pctrl_str == NULL)
    988      1.1  christos         EVP_PKEY_meth_get_ctrl(dasync_rsa_orig, NULL, &pctrl_str);
    989      1.1  christos     return pctrl_str(ctx, type, value);
    990      1.1  christos }
    991