Home | History | Annotate | Line # | Download | only in engines
      1 /*
      2  * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5  * this file except in compliance with the License.  You can obtain a copy
      6  * in the file LICENSE in the source distribution or at
      7  * https://www.openssl.org/source/license.html
      8  */
      9 
     10 /*
     11  * This is the OSSLTEST engine. It provides deliberately crippled digest
     12  * implementations for test purposes. It is highly insecure and must NOT be
     13  * used for any purpose except testing
     14  */
     15 
     16 /* We need to use some engine deprecated APIs */
     17 #define OPENSSL_SUPPRESS_DEPRECATED
     18 
     19 /*
     20  * SHA low level APIs are deprecated for public use, but still ok for
     21  * internal use.  Note, that due to symbols not being exported, only the
     22  * #defines and type definitions can be accessed, function calls are not
     23  * available.  The digest lengths, block sizes and sizeof(CTX) are used herein
     24  * for several different digests.
     25  */
     26 #include "internal/deprecated.h"
     27 
     28 #include <stdio.h>
     29 #include <string.h>
     30 #include "internal/common.h" /* for CHECK_AND_SKIP_CASE_PREFIX */
     31 
     32 #include <openssl/engine.h>
     33 #include <openssl/sha.h>
     34 #include <openssl/md5.h>
     35 #include <openssl/rsa.h>
     36 #include <openssl/evp.h>
     37 #include <openssl/modes.h>
     38 #include <openssl/aes.h>
     39 #include <openssl/rand.h>
     40 #include <openssl/crypto.h>
     41 #include <openssl/pem.h>
     42 #include <crypto/evp.h>
     43 
     44 /* clang-format off */
     45 #include "e_ossltest_err.c"
     46 /* clang-format on */
     47 
     48 /* Engine Id and Name */
     49 static const char *engine_ossltest_id = "ossltest";
     50 static const char *engine_ossltest_name = "OpenSSL Test engine support";
     51 
     52 /* Engine Lifetime functions */
     53 static int ossltest_destroy(ENGINE *e);
     54 static int ossltest_init(ENGINE *e);
     55 static int ossltest_finish(ENGINE *e);
     56 void ENGINE_load_ossltest(void);
     57 
     58 /* Set up digests */
     59 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
     60     const int **nids, int nid);
     61 static const RAND_METHOD *ossltest_rand_method(void);
     62 
     63 /* MD5 */
     64 static int digest_md5_init(EVP_MD_CTX *ctx);
     65 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
     66     size_t count);
     67 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
     68 
     69 static EVP_MD *_hidden_md5_md = NULL;
     70 static const EVP_MD *digest_md5(void)
     71 {
     72     if (_hidden_md5_md == NULL) {
     73         EVP_MD *md;
     74 
     75         if ((md = EVP_MD_meth_new(NID_md5, NID_md5WithRSAEncryption)) == NULL
     76             || !EVP_MD_meth_set_result_size(md, MD5_DIGEST_LENGTH)
     77             || !EVP_MD_meth_set_input_blocksize(md, MD5_CBLOCK)
     78             || !EVP_MD_meth_set_app_datasize(md,
     79                 sizeof(EVP_MD *) + sizeof(MD5_CTX))
     80             || !EVP_MD_meth_set_flags(md, 0)
     81             || !EVP_MD_meth_set_init(md, digest_md5_init)
     82             || !EVP_MD_meth_set_update(md, digest_md5_update)
     83             || !EVP_MD_meth_set_final(md, digest_md5_final)) {
     84             EVP_MD_meth_free(md);
     85             md = NULL;
     86         }
     87         _hidden_md5_md = md;
     88     }
     89     return _hidden_md5_md;
     90 }
     91 
     92 /* SHA1 */
     93 static int digest_sha1_init(EVP_MD_CTX *ctx);
     94 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
     95     size_t count);
     96 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
     97 
     98 static EVP_MD *_hidden_sha1_md = NULL;
     99 static const EVP_MD *digest_sha1(void)
    100 {
    101     if (_hidden_sha1_md == NULL) {
    102         EVP_MD *md;
    103 
    104         if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
    105             || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
    106             || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
    107             || !EVP_MD_meth_set_app_datasize(md,
    108                 sizeof(EVP_MD *) + sizeof(SHA_CTX))
    109             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
    110             || !EVP_MD_meth_set_init(md, digest_sha1_init)
    111             || !EVP_MD_meth_set_update(md, digest_sha1_update)
    112             || !EVP_MD_meth_set_final(md, digest_sha1_final)) {
    113             EVP_MD_meth_free(md);
    114             md = NULL;
    115         }
    116         _hidden_sha1_md = md;
    117     }
    118     return _hidden_sha1_md;
    119 }
    120 
    121 /* SHA256 */
    122 static int digest_sha256_init(EVP_MD_CTX *ctx);
    123 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
    124     size_t count);
    125 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md);
    126 
    127 static EVP_MD *_hidden_sha256_md = NULL;
    128 static const EVP_MD *digest_sha256(void)
    129 {
    130     if (_hidden_sha256_md == NULL) {
    131         EVP_MD *md;
    132 
    133         if ((md = EVP_MD_meth_new(NID_sha256, NID_sha256WithRSAEncryption)) == NULL
    134             || !EVP_MD_meth_set_result_size(md, SHA256_DIGEST_LENGTH)
    135             || !EVP_MD_meth_set_input_blocksize(md, SHA256_CBLOCK)
    136             || !EVP_MD_meth_set_app_datasize(md,
    137                 sizeof(EVP_MD *) + sizeof(SHA256_CTX))
    138             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
    139             || !EVP_MD_meth_set_init(md, digest_sha256_init)
    140             || !EVP_MD_meth_set_update(md, digest_sha256_update)
    141             || !EVP_MD_meth_set_final(md, digest_sha256_final)) {
    142             EVP_MD_meth_free(md);
    143             md = NULL;
    144         }
    145         _hidden_sha256_md = md;
    146     }
    147     return _hidden_sha256_md;
    148 }
    149 
    150 /* SHA384/SHA512 */
    151 static int digest_sha384_init(EVP_MD_CTX *ctx);
    152 static int digest_sha384_update(EVP_MD_CTX *ctx, const void *data,
    153     size_t count);
    154 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md);
    155 
    156 static int digest_sha512_init(EVP_MD_CTX *ctx);
    157 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
    158     size_t count);
    159 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md);
    160 
    161 static EVP_MD *_hidden_sha384_md = NULL;
    162 static const EVP_MD *digest_sha384(void)
    163 {
    164     if (_hidden_sha384_md == NULL) {
    165         EVP_MD *md;
    166 
    167         if ((md = EVP_MD_meth_new(NID_sha384, NID_sha384WithRSAEncryption)) == NULL
    168             || !EVP_MD_meth_set_result_size(md, SHA384_DIGEST_LENGTH)
    169             || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
    170             || !EVP_MD_meth_set_app_datasize(md,
    171                 sizeof(EVP_MD *) + sizeof(SHA512_CTX))
    172             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
    173             || !EVP_MD_meth_set_init(md, digest_sha384_init)
    174             || !EVP_MD_meth_set_update(md, digest_sha384_update)
    175             || !EVP_MD_meth_set_final(md, digest_sha384_final)) {
    176             EVP_MD_meth_free(md);
    177             md = NULL;
    178         }
    179         _hidden_sha384_md = md;
    180     }
    181     return _hidden_sha384_md;
    182 }
    183 static EVP_MD *_hidden_sha512_md = NULL;
    184 static const EVP_MD *digest_sha512(void)
    185 {
    186     if (_hidden_sha512_md == NULL) {
    187         EVP_MD *md;
    188 
    189         if ((md = EVP_MD_meth_new(NID_sha512, NID_sha512WithRSAEncryption)) == NULL
    190             || !EVP_MD_meth_set_result_size(md, SHA512_DIGEST_LENGTH)
    191             || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
    192             || !EVP_MD_meth_set_app_datasize(md,
    193                 sizeof(EVP_MD *) + sizeof(SHA512_CTX))
    194             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
    195             || !EVP_MD_meth_set_init(md, digest_sha512_init)
    196             || !EVP_MD_meth_set_update(md, digest_sha512_update)
    197             || !EVP_MD_meth_set_final(md, digest_sha512_final)) {
    198             EVP_MD_meth_free(md);
    199             md = NULL;
    200         }
    201         _hidden_sha512_md = md;
    202     }
    203     return _hidden_sha512_md;
    204 }
    205 static void destroy_digests(void)
    206 {
    207     EVP_MD_meth_free(_hidden_md5_md);
    208     _hidden_md5_md = NULL;
    209     EVP_MD_meth_free(_hidden_sha1_md);
    210     _hidden_sha1_md = NULL;
    211     EVP_MD_meth_free(_hidden_sha256_md);
    212     _hidden_sha256_md = NULL;
    213     EVP_MD_meth_free(_hidden_sha384_md);
    214     _hidden_sha384_md = NULL;
    215     EVP_MD_meth_free(_hidden_sha512_md);
    216     _hidden_sha512_md = NULL;
    217 }
    218 static int ossltest_digest_nids(const int **nids)
    219 {
    220     static int digest_nids[6] = { 0, 0, 0, 0, 0, 0 };
    221     static int pos = 0;
    222     static int init = 0;
    223 
    224     if (!init) {
    225         const EVP_MD *md;
    226         if ((md = digest_md5()) != NULL)
    227             digest_nids[pos++] = EVP_MD_get_type(md);
    228         if ((md = digest_sha1()) != NULL)
    229             digest_nids[pos++] = EVP_MD_get_type(md);
    230         if ((md = digest_sha256()) != NULL)
    231             digest_nids[pos++] = EVP_MD_get_type(md);
    232         if ((md = digest_sha384()) != NULL)
    233             digest_nids[pos++] = EVP_MD_get_type(md);
    234         if ((md = digest_sha512()) != NULL)
    235             digest_nids[pos++] = EVP_MD_get_type(md);
    236         digest_nids[pos] = 0;
    237         init = 1;
    238     }
    239     *nids = digest_nids;
    240     return pos;
    241 }
    242 
    243 /* Setup ciphers */
    244 static int ossltest_ciphers(ENGINE *, const EVP_CIPHER **,
    245     const int **, int);
    246 
    247 static int ossltest_cipher_nids[] = {
    248     NID_aes_128_cbc, NID_aes_128_gcm,
    249     NID_aes_128_cbc_hmac_sha1, 0
    250 };
    251 
    252 /* AES128 */
    253 
    254 static int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx,
    255     const unsigned char *key,
    256     const unsigned char *iv, int enc);
    257 static int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
    258     const unsigned char *in, size_t inl);
    259 static int ossltest_aes128_gcm_init_key(EVP_CIPHER_CTX *ctx,
    260     const unsigned char *key,
    261     const unsigned char *iv, int enc);
    262 static int ossltest_aes128_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
    263     const unsigned char *in, size_t inl);
    264 static int ossltest_aes128_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
    265     void *ptr);
    266 static int ossltest_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
    267     const unsigned char *key,
    268     const unsigned char *iv,
    269     int enc);
    270 static int ossltest_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
    271     unsigned char *out,
    272     const unsigned char *in,
    273     size_t inl);
    274 static int ossltest_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
    275     int arg, void *ptr);
    276 
    277 typedef struct {
    278     size_t payload_length; /* AAD length in decrypt case */
    279     unsigned int tls_ver;
    280 } EVP_AES_HMAC_SHA1;
    281 
    282 static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
    283 static const EVP_CIPHER *ossltest_aes_128_cbc(void)
    284 {
    285     if (_hidden_aes_128_cbc == NULL
    286         && ((_hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
    287                  16 /* block size */,
    288                  16 /* key len */))
    289                 == NULL
    290             || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc, 16)
    291             || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
    292                 EVP_CIPH_FLAG_DEFAULT_ASN1
    293                     | EVP_CIPH_CBC_MODE)
    294             || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
    295                 ossltest_aes128_init_key)
    296             || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
    297                 ossltest_aes128_cbc_cipher)
    298             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
    299                 EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc())))) {
    300         EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
    301         _hidden_aes_128_cbc = NULL;
    302     }
    303     return _hidden_aes_128_cbc;
    304 }
    305 
    306 static EVP_CIPHER *_hidden_aes_128_gcm = NULL;
    307 
    308 #define AES_GCM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1      \
    309     | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
    310     | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT   \
    311     | EVP_CIPH_CUSTOM_COPY | EVP_CIPH_FLAG_AEAD_CIPHER \
    312     | EVP_CIPH_GCM_MODE)
    313 
    314 static const EVP_CIPHER *ossltest_aes_128_gcm(void)
    315 {
    316     if (_hidden_aes_128_gcm == NULL
    317         && ((_hidden_aes_128_gcm = EVP_CIPHER_meth_new(NID_aes_128_gcm,
    318                  1 /* block size */,
    319                  16 /* key len */))
    320                 == NULL
    321             || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_gcm, 12)
    322             || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_gcm, AES_GCM_FLAGS)
    323             || !EVP_CIPHER_meth_set_init(_hidden_aes_128_gcm,
    324                 ossltest_aes128_gcm_init_key)
    325             || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_gcm,
    326                 ossltest_aes128_gcm_cipher)
    327             || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_gcm,
    328                 ossltest_aes128_gcm_ctrl)
    329             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_gcm,
    330                 EVP_CIPHER_impl_ctx_size(EVP_aes_128_gcm())))) {
    331         EVP_CIPHER_meth_free(_hidden_aes_128_gcm);
    332         _hidden_aes_128_gcm = NULL;
    333     }
    334     return _hidden_aes_128_gcm;
    335 }
    336 
    337 static EVP_CIPHER *_hidden_aes_128_cbc_hmac_sha1 = NULL;
    338 
    339 static const EVP_CIPHER *ossltest_aes_128_cbc_hmac_sha1(void)
    340 {
    341     if (_hidden_aes_128_cbc_hmac_sha1 == NULL
    342         && ((_hidden_aes_128_cbc_hmac_sha1
    343                 = EVP_CIPHER_meth_new(NID_aes_128_cbc_hmac_sha1,
    344                     16 /* block size */,
    345                     16 /* key len */))
    346                 == NULL
    347             || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc_hmac_sha1, 16)
    348             || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc_hmac_sha1,
    349                 EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_FLAG_AEAD_CIPHER)
    350             || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc_hmac_sha1,
    351                 ossltest_aes128_cbc_hmac_sha1_init_key)
    352             || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc_hmac_sha1,
    353                 ossltest_aes128_cbc_hmac_sha1_cipher)
    354             || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_cbc_hmac_sha1,
    355                 ossltest_aes128_cbc_hmac_sha1_ctrl)
    356             || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_aes_128_cbc_hmac_sha1,
    357                 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv)
    358             || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_aes_128_cbc_hmac_sha1,
    359                 EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv)
    360             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc_hmac_sha1,
    361                 sizeof(EVP_AES_HMAC_SHA1)))) {
    362         EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
    363         _hidden_aes_128_cbc_hmac_sha1 = NULL;
    364     }
    365     return _hidden_aes_128_cbc_hmac_sha1;
    366 }
    367 
    368 static void destroy_ciphers(void)
    369 {
    370     EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
    371     EVP_CIPHER_meth_free(_hidden_aes_128_gcm);
    372     EVP_CIPHER_meth_free(_hidden_aes_128_cbc_hmac_sha1);
    373     _hidden_aes_128_cbc = NULL;
    374     _hidden_aes_128_gcm = NULL;
    375     _hidden_aes_128_cbc_hmac_sha1 = NULL;
    376 }
    377 
    378 /* Key loading */
    379 static EVP_PKEY *load_key(ENGINE *eng, const char *key_id, int pub,
    380     UI_METHOD *ui_method, void *ui_data)
    381 {
    382     BIO *in;
    383     EVP_PKEY *key;
    384 
    385     if (!CHECK_AND_SKIP_CASE_PREFIX(key_id, "ot:"))
    386         return NULL;
    387 
    388     fprintf(stderr, "[ossltest]Loading %s key %s\n",
    389         pub ? "Public" : "Private", key_id);
    390     in = BIO_new_file(key_id, "r");
    391     if (!in)
    392         return NULL;
    393     if (pub)
    394         key = PEM_read_bio_PUBKEY(in, NULL, 0, NULL);
    395     else
    396         key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL);
    397     BIO_free(in);
    398     return key;
    399 }
    400 
    401 static EVP_PKEY *ossltest_load_privkey(ENGINE *eng, const char *key_id,
    402     UI_METHOD *ui_method, void *ui_data)
    403 {
    404     return load_key(eng, key_id, 0, ui_method, ui_data);
    405 }
    406 
    407 static EVP_PKEY *ossltest_load_pubkey(ENGINE *eng, const char *key_id,
    408     UI_METHOD *ui_method, void *ui_data)
    409 {
    410     return load_key(eng, key_id, 1, ui_method, ui_data);
    411 }
    412 
    413 static int bind_ossltest(ENGINE *e)
    414 {
    415     /* Ensure the ossltest error handling is set up */
    416     ERR_load_OSSLTEST_strings();
    417 
    418     if (!ENGINE_set_id(e, engine_ossltest_id)
    419         || !ENGINE_set_name(e, engine_ossltest_name)
    420         || !ENGINE_set_digests(e, ossltest_digests)
    421         || !ENGINE_set_ciphers(e, ossltest_ciphers)
    422         || !ENGINE_set_RAND(e, ossltest_rand_method())
    423         || !ENGINE_set_destroy_function(e, ossltest_destroy)
    424         || !ENGINE_set_load_privkey_function(e, ossltest_load_privkey)
    425         || !ENGINE_set_load_pubkey_function(e, ossltest_load_pubkey)
    426         || !ENGINE_set_init_function(e, ossltest_init)
    427         || !ENGINE_set_finish_function(e, ossltest_finish)) {
    428         OSSLTESTerr(OSSLTEST_F_BIND_OSSLTEST, OSSLTEST_R_INIT_FAILED);
    429         return 0;
    430     }
    431 
    432     return 1;
    433 }
    434 
    435 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
    436 static int bind_helper(ENGINE *e, const char *id)
    437 {
    438     if (id && (strcmp(id, engine_ossltest_id) != 0))
    439         return 0;
    440     if (!bind_ossltest(e))
    441         return 0;
    442     return 1;
    443 }
    444 
    445 IMPLEMENT_DYNAMIC_CHECK_FN()
    446 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
    447 #endif
    448 
    449 static ENGINE *engine_ossltest(void)
    450 {
    451     ENGINE *ret = ENGINE_new();
    452     if (ret == NULL)
    453         return NULL;
    454     if (!bind_ossltest(ret)) {
    455         ENGINE_free(ret);
    456         return NULL;
    457     }
    458     return ret;
    459 }
    460 
    461 void ENGINE_load_ossltest(void)
    462 {
    463     /* Copied from eng_[openssl|dyn].c */
    464     ENGINE *toadd = engine_ossltest();
    465     if (!toadd)
    466         return;
    467     ENGINE_add(toadd);
    468     ENGINE_free(toadd);
    469     ERR_clear_error();
    470 }
    471 
    472 static int ossltest_init(ENGINE *e)
    473 {
    474     return 1;
    475 }
    476 
    477 static int ossltest_finish(ENGINE *e)
    478 {
    479     return 1;
    480 }
    481 
    482 static int ossltest_destroy(ENGINE *e)
    483 {
    484     destroy_digests();
    485     destroy_ciphers();
    486     ERR_unload_OSSLTEST_strings();
    487     return 1;
    488 }
    489 
    490 static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
    491     const int **nids, int nid)
    492 {
    493     int ok = 1;
    494     if (!digest) {
    495         /* We are returning a list of supported nids */
    496         return ossltest_digest_nids(nids);
    497     }
    498     /* We are being asked for a specific digest */
    499     switch (nid) {
    500     case NID_md5:
    501         *digest = digest_md5();
    502         break;
    503     case NID_sha1:
    504         *digest = digest_sha1();
    505         break;
    506     case NID_sha256:
    507         *digest = digest_sha256();
    508         break;
    509     case NID_sha384:
    510         *digest = digest_sha384();
    511         break;
    512     case NID_sha512:
    513         *digest = digest_sha512();
    514         break;
    515     default:
    516         ok = 0;
    517         *digest = NULL;
    518         break;
    519     }
    520     return ok;
    521 }
    522 
    523 static int ossltest_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
    524     const int **nids, int nid)
    525 {
    526     int ok = 1;
    527     if (!cipher) {
    528         /* We are returning a list of supported nids */
    529         *nids = ossltest_cipher_nids;
    530         return (sizeof(ossltest_cipher_nids) - 1)
    531             / sizeof(ossltest_cipher_nids[0]);
    532     }
    533     /* We are being asked for a specific cipher */
    534     switch (nid) {
    535     case NID_aes_128_cbc:
    536         *cipher = ossltest_aes_128_cbc();
    537         break;
    538     case NID_aes_128_gcm:
    539         *cipher = ossltest_aes_128_gcm();
    540         break;
    541     case NID_aes_128_cbc_hmac_sha1:
    542         *cipher = ossltest_aes_128_cbc_hmac_sha1();
    543         break;
    544     default:
    545         ok = 0;
    546         *cipher = NULL;
    547         break;
    548     }
    549     return ok;
    550 }
    551 
    552 static void fill_known_data(unsigned char *md, unsigned int len)
    553 {
    554     unsigned int i;
    555 
    556     for (i = 0; i < len; i++) {
    557         md[i] = (unsigned char)(i & 0xff);
    558     }
    559 }
    560 
    561 /*
    562  * MD5 implementation. We go through the motions of doing MD5 by deferring to
    563  * the standard implementation. Then we overwrite the result with a will defined
    564  * value, so that all "MD5" digests using the test engine always end up with
    565  * the same value.
    566  */
    567 static int digest_md5_init(EVP_MD_CTX *ctx)
    568 {
    569     return EVP_MD_meth_get_init(EVP_md5())(ctx);
    570 }
    571 
    572 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
    573     size_t count)
    574 {
    575     return EVP_MD_meth_get_update(EVP_md5())(ctx, data, count);
    576 }
    577 
    578 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
    579 {
    580     int ret = EVP_MD_meth_get_final(EVP_md5())(ctx, md);
    581 
    582     if (ret > 0) {
    583         fill_known_data(md, MD5_DIGEST_LENGTH);
    584     }
    585     return ret;
    586 }
    587 
    588 /*
    589  * SHA1 implementation.
    590  */
    591 static int digest_sha1_init(EVP_MD_CTX *ctx)
    592 {
    593     return EVP_MD_meth_get_init(EVP_sha1())(ctx);
    594 }
    595 
    596 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
    597     size_t count)
    598 {
    599     return EVP_MD_meth_get_update(EVP_sha1())(ctx, data, count);
    600 }
    601 
    602 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
    603 {
    604     int ret = EVP_MD_meth_get_final(EVP_sha1())(ctx, md);
    605 
    606     if (ret > 0) {
    607         fill_known_data(md, SHA_DIGEST_LENGTH);
    608     }
    609     return ret;
    610 }
    611 
    612 /*
    613  * SHA256 implementation.
    614  */
    615 static int digest_sha256_init(EVP_MD_CTX *ctx)
    616 {
    617     return EVP_MD_meth_get_init(EVP_sha256())(ctx);
    618 }
    619 
    620 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
    621     size_t count)
    622 {
    623     return EVP_MD_meth_get_update(EVP_sha256())(ctx, data, count);
    624 }
    625 
    626 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md)
    627 {
    628     int ret = EVP_MD_meth_get_final(EVP_sha256())(ctx, md);
    629 
    630     if (ret > 0) {
    631         fill_known_data(md, SHA256_DIGEST_LENGTH);
    632     }
    633     return ret;
    634 }
    635 
    636 /*
    637  * SHA384 implementation.
    638  */
    639 static int digest_sha384_init(EVP_MD_CTX *ctx)
    640 {
    641     return EVP_MD_meth_get_init(EVP_sha384())(ctx);
    642 }
    643 
    644 static int digest_sha384_update(EVP_MD_CTX *ctx, const void *data,
    645     size_t count)
    646 {
    647     return EVP_MD_meth_get_update(EVP_sha384())(ctx, data, count);
    648 }
    649 
    650 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md)
    651 {
    652     int ret = EVP_MD_meth_get_final(EVP_sha384())(ctx, md);
    653 
    654     if (ret > 0) {
    655         fill_known_data(md, SHA384_DIGEST_LENGTH);
    656     }
    657     return ret;
    658 }
    659 
    660 /*
    661  * SHA512 implementation.
    662  */
    663 static int digest_sha512_init(EVP_MD_CTX *ctx)
    664 {
    665     return EVP_MD_meth_get_init(EVP_sha512())(ctx);
    666 }
    667 
    668 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
    669     size_t count)
    670 {
    671     return EVP_MD_meth_get_update(EVP_sha512())(ctx, data, count);
    672 }
    673 
    674 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md)
    675 {
    676     int ret = EVP_MD_meth_get_final(EVP_sha512())(ctx, md);
    677 
    678     if (ret > 0) {
    679         fill_known_data(md, SHA512_DIGEST_LENGTH);
    680     }
    681     return ret;
    682 }
    683 
    684 /*
    685  * AES128 Implementation
    686  */
    687 
    688 static int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx,
    689     const unsigned char *key,
    690     const unsigned char *iv, int enc)
    691 {
    692     return EVP_CIPHER_meth_get_init(EVP_aes_128_cbc())(ctx, key, iv, enc);
    693 }
    694 
    695 static int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
    696     const unsigned char *in, size_t inl)
    697 {
    698     unsigned char *tmpbuf;
    699     int ret;
    700 
    701     tmpbuf = OPENSSL_malloc(inl);
    702 
    703     /* OPENSSL_malloc will return NULL if inl == 0 */
    704     if (tmpbuf == NULL && inl > 0)
    705         return -1;
    706 
    707     /* Remember what we were asked to encrypt */
    708     if (tmpbuf != NULL)
    709         memcpy(tmpbuf, in, inl);
    710 
    711     /* Go through the motions of encrypting it */
    712     ret = EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())(ctx, out, in, inl);
    713 
    714     /* Throw it all away and just use the plaintext as the output */
    715     if (tmpbuf != NULL)
    716         memcpy(out, tmpbuf, inl);
    717     OPENSSL_free(tmpbuf);
    718 
    719     return ret;
    720 }
    721 
    722 static int ossltest_aes128_gcm_init_key(EVP_CIPHER_CTX *ctx,
    723     const unsigned char *key,
    724     const unsigned char *iv, int enc)
    725 {
    726     return EVP_CIPHER_meth_get_init(EVP_aes_128_gcm())(ctx, key, iv, enc);
    727 }
    728 
    729 static int ossltest_aes128_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
    730     const unsigned char *in, size_t inl)
    731 {
    732     unsigned char *tmpbuf = OPENSSL_malloc(inl);
    733 
    734     /* OPENSSL_malloc will return NULL if inl == 0 */
    735     if (tmpbuf == NULL && inl > 0)
    736         return -1;
    737 
    738     /* Remember what we were asked to encrypt */
    739     if (tmpbuf != NULL)
    740         memcpy(tmpbuf, in, inl);
    741 
    742     /* Go through the motions of encrypting it */
    743     EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_gcm())(ctx, out, in, inl);
    744 
    745     /* Throw it all away and just use the plaintext as the output */
    746     if (tmpbuf != NULL && out != NULL)
    747         memcpy(out, tmpbuf, inl);
    748     OPENSSL_free(tmpbuf);
    749 
    750     return inl;
    751 }
    752 
    753 static int ossltest_aes128_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
    754     void *ptr)
    755 {
    756     /* Pass the ctrl down */
    757     int ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_gcm())(ctx, type, arg, ptr);
    758 
    759     if (ret <= 0)
    760         return ret;
    761 
    762     switch (type) {
    763     case EVP_CTRL_AEAD_GET_TAG:
    764         /* Always give the same tag */
    765         memset(ptr, 0, EVP_GCM_TLS_TAG_LEN);
    766         break;
    767 
    768     default:
    769         break;
    770     }
    771 
    772     return 1;
    773 }
    774 
    775 #define NO_PAYLOAD_LENGTH ((size_t)-1)
    776 #define data(ctx) ((EVP_AES_HMAC_SHA1 *)EVP_CIPHER_CTX_get_cipher_data(ctx))
    777 
    778 static int ossltest_aes128_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
    779     const unsigned char *inkey,
    780     const unsigned char *iv,
    781     int enc)
    782 {
    783     EVP_AES_HMAC_SHA1 *key = data(ctx);
    784     key->payload_length = NO_PAYLOAD_LENGTH;
    785     return 1;
    786 }
    787 
    788 static int ossltest_aes128_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx,
    789     unsigned char *out,
    790     const unsigned char *in,
    791     size_t len)
    792 {
    793     EVP_AES_HMAC_SHA1 *key = data(ctx);
    794     unsigned int l;
    795     size_t plen = key->payload_length;
    796 
    797     key->payload_length = NO_PAYLOAD_LENGTH;
    798 
    799     if (len % AES_BLOCK_SIZE)
    800         return 0;
    801 
    802     if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
    803         if (plen == NO_PAYLOAD_LENGTH)
    804             plen = len;
    805         else if (len != ((plen + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
    806             return 0;
    807 
    808         memmove(out, in, plen);
    809 
    810         if (plen != len) { /* "TLS" mode of operation */
    811             /* calculate HMAC and append it to payload */
    812             fill_known_data(out + plen, SHA_DIGEST_LENGTH);
    813 
    814             /* pad the payload|hmac */
    815             plen += SHA_DIGEST_LENGTH;
    816             for (l = len - plen - 1; plen < len; plen++)
    817                 out[plen] = l;
    818         }
    819     } else {
    820         /* decrypt HMAC|padding at once */
    821         memmove(out, in, len);
    822 
    823         if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
    824             unsigned int maxpad, pad;
    825 
    826             if (key->tls_ver >= TLS1_1_VERSION) {
    827                 if (len < (AES_BLOCK_SIZE + SHA_DIGEST_LENGTH + 1))
    828                     return 0;
    829 
    830                 /* omit explicit iv */
    831                 in += AES_BLOCK_SIZE;
    832                 out += AES_BLOCK_SIZE;
    833                 len -= AES_BLOCK_SIZE;
    834             } else if (len < (SHA_DIGEST_LENGTH + 1))
    835                 return 0;
    836 
    837             /* figure out payload length */
    838             pad = out[len - 1];
    839             maxpad = len - (SHA_DIGEST_LENGTH + 1);
    840             if (pad > maxpad)
    841                 return 0;
    842             for (plen = len - pad - 1; plen < len; plen++)
    843                 if (out[plen] != pad)
    844                     return 0;
    845         }
    846     }
    847 
    848     return 1;
    849 }
    850 
    851 static int ossltest_aes128_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type,
    852     int arg, void *ptr)
    853 {
    854     EVP_AES_HMAC_SHA1 *key = data(ctx);
    855 
    856     switch (type) {
    857     case EVP_CTRL_AEAD_SET_MAC_KEY:
    858         return 1;
    859 
    860     case EVP_CTRL_AEAD_TLS1_AAD: {
    861         unsigned char *p = ptr;
    862         unsigned int len;
    863 
    864         if (arg != EVP_AEAD_TLS1_AAD_LEN)
    865             return -1;
    866 
    867         len = p[arg - 2] << 8 | p[arg - 1];
    868         key->tls_ver = p[arg - 4] << 8 | p[arg - 3];
    869 
    870         if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
    871             key->payload_length = len;
    872             if (key->tls_ver >= TLS1_1_VERSION) {
    873                 if (len < AES_BLOCK_SIZE)
    874                     return 0;
    875                 len -= AES_BLOCK_SIZE;
    876                 p[arg - 2] = len >> 8;
    877                 p[arg - 1] = len;
    878             }
    879 
    880             return (int)(((len + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
    881                 - len);
    882         } else {
    883             key->payload_length = arg;
    884 
    885             return SHA_DIGEST_LENGTH;
    886         }
    887     }
    888     default:
    889         return -1;
    890     }
    891 }
    892 
    893 static int ossltest_rand_bytes(unsigned char *buf, int num)
    894 {
    895     unsigned char val = 1;
    896 
    897     while (--num >= 0)
    898         *buf++ = val++;
    899     return 1;
    900 }
    901 
    902 static int ossltest_rand_status(void)
    903 {
    904     return 1;
    905 }
    906 
    907 static const RAND_METHOD *ossltest_rand_method(void)
    908 {
    909 
    910     static RAND_METHOD osslt_rand_meth = {
    911         NULL,
    912         ossltest_rand_bytes,
    913         NULL,
    914         NULL,
    915         ossltest_rand_bytes,
    916         ossltest_rand_status
    917     };
    918 
    919     return &osslt_rand_meth;
    920 }
    921