Coverage Report

Created: 2025-06-22 06:56

/src/openssl/crypto/engine/eng_openssl.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2001-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
/* We need to use some engine deprecated APIs */
12
#define OPENSSL_SUPPRESS_DEPRECATED
13
14
/*
15
 * RC4 and SHA-1 low level APIs and EVP _meth_ APISs are deprecated for public
16
 * use, but still ok for internal use.
17
 */
18
#include "internal/deprecated.h"
19
20
#include <stdio.h>
21
#include <openssl/crypto.h>
22
#include "internal/cryptlib.h"
23
#include "crypto/engine.h"
24
#include <openssl/pem.h>
25
#include <openssl/evp.h>
26
#include <openssl/rand.h>
27
#include <openssl/rsa.h>
28
#include <openssl/dsa.h>
29
#include <openssl/dh.h>
30
31
#include <openssl/hmac.h>
32
#include <openssl/x509v3.h>
33
34
/*
35
 * This testing gunk is implemented (and explained) lower down. It also
36
 * assumes the application explicitly calls "ENGINE_load_openssl()" because
37
 * this is no longer automatic in ENGINE_load_builtin_engines().
38
 */
39
#define TEST_ENG_OPENSSL_RC4
40
#ifndef OPENSSL_NO_STDIO
41
# define TEST_ENG_OPENSSL_PKEY
42
#endif
43
/* #define TEST_ENG_OPENSSL_HMAC */
44
/* #define TEST_ENG_OPENSSL_HMAC_INIT */
45
/* #define TEST_ENG_OPENSSL_RC4_OTHERS */
46
#ifndef OPENSSL_NO_STDIO
47
# define TEST_ENG_OPENSSL_RC4_P_INIT
48
#endif
49
/* #define TEST_ENG_OPENSSL_RC4_P_CIPHER */
50
#define TEST_ENG_OPENSSL_SHA
51
/* #define TEST_ENG_OPENSSL_SHA_OTHERS */
52
/* #define TEST_ENG_OPENSSL_SHA_P_INIT */
53
/* #define TEST_ENG_OPENSSL_SHA_P_UPDATE */
54
/* #define TEST_ENG_OPENSSL_SHA_P_FINAL */
55
56
/* Now check what of those algorithms are actually enabled */
57
#ifdef OPENSSL_NO_RC4
58
# undef TEST_ENG_OPENSSL_RC4
59
# undef TEST_ENG_OPENSSL_RC4_OTHERS
60
# undef TEST_ENG_OPENSSL_RC4_P_INIT
61
# undef TEST_ENG_OPENSSL_RC4_P_CIPHER
62
#endif
63
64
static int openssl_destroy(ENGINE *e);
65
66
#ifdef TEST_ENG_OPENSSL_RC4
67
static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
68
                           const int **nids, int nid);
69
#endif
70
#ifdef TEST_ENG_OPENSSL_SHA
71
static int openssl_digests(ENGINE *e, const EVP_MD **digest,
72
                           const int **nids, int nid);
73
#endif
74
75
#ifdef TEST_ENG_OPENSSL_PKEY
76
static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
77
                                      UI_METHOD *ui_method,
78
                                      void *callback_data);
79
#endif
80
81
#ifdef TEST_ENG_OPENSSL_HMAC
82
static int ossl_register_hmac_meth(void);
83
static int ossl_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
84
                           const int **nids, int nid);
85
#endif
86
87
/* The constants used when creating the ENGINE */
88
static const char *engine_openssl_id = "openssl";
89
static const char *engine_openssl_name = "Software engine support";
90
91
/*
92
 * This internal function is used by ENGINE_openssl() and possibly by the
93
 * "dynamic" ENGINE support too
94
 */
95
static int bind_helper(ENGINE *e)
96
0
{
97
0
    if (!ENGINE_set_id(e, engine_openssl_id)
98
0
        || !ENGINE_set_name(e, engine_openssl_name)
99
0
        || !ENGINE_set_destroy_function(e, openssl_destroy)
100
0
#ifndef TEST_ENG_OPENSSL_NO_ALGORITHMS
101
0
        || !ENGINE_set_RSA(e, RSA_get_default_method())
102
0
# ifndef OPENSSL_NO_DSA
103
0
        || !ENGINE_set_DSA(e, DSA_get_default_method())
104
0
# endif
105
0
# ifndef OPENSSL_NO_EC
106
0
        || !ENGINE_set_EC(e, EC_KEY_OpenSSL())
107
0
# endif
108
0
# ifndef OPENSSL_NO_DH
109
0
        || !ENGINE_set_DH(e, DH_get_default_method())
110
0
# endif
111
0
        || !ENGINE_set_RAND(e, RAND_OpenSSL())
112
0
# ifdef TEST_ENG_OPENSSL_RC4
113
0
        || !ENGINE_set_ciphers(e, openssl_ciphers)
114
0
# endif
115
0
# ifdef TEST_ENG_OPENSSL_SHA
116
0
        || !ENGINE_set_digests(e, openssl_digests)
117
0
# endif
118
0
#endif
119
0
#ifdef TEST_ENG_OPENSSL_PKEY
120
0
        || !ENGINE_set_load_privkey_function(e, openssl_load_privkey)
121
0
#endif
122
#ifdef TEST_ENG_OPENSSL_HMAC
123
        || !ossl_register_hmac_meth()
124
        || !ENGINE_set_pkey_meths(e, ossl_pkey_meths)
125
#endif
126
0
        )
127
0
        return 0;
128
    /*
129
     * If we add errors to this ENGINE, ensure the error handling is setup
130
     * here
131
     */
132
    /* openssl_load_error_strings(); */
133
0
    return 1;
134
0
}
135
136
static ENGINE *engine_openssl(void)
137
0
{
138
0
    ENGINE *ret = ENGINE_new();
139
0
    if (ret == NULL)
140
0
        return NULL;
141
0
    if (!bind_helper(ret)) {
142
0
        ENGINE_free(ret);
143
0
        return NULL;
144
0
    }
145
0
    return ret;
146
0
}
147
148
void engine_load_openssl_int(void)
149
0
{
150
0
    ENGINE *toadd = engine_openssl();
151
0
    if (!toadd)
152
0
        return;
153
154
0
    ERR_set_mark();
155
0
    ENGINE_add(toadd);
156
    /*
157
     * If the "add" worked, it gets a structural reference. So either way, we
158
     * release our just-created reference.
159
     */
160
0
    ENGINE_free(toadd);
161
    /*
162
     * If the "add" didn't work, it was probably a conflict because it was
163
     * already added (eg. someone calling ENGINE_load_blah then calling
164
     * ENGINE_load_builtin_engines() perhaps).
165
     */
166
0
    ERR_pop_to_mark();
167
0
}
168
169
/*
170
 * This stuff is needed if this ENGINE is being compiled into a
171
 * self-contained shared-library.
172
 */
173
#ifdef ENGINE_DYNAMIC_SUPPORT
174
static int bind_fn(ENGINE *e, const char *id)
175
{
176
    if (id && (strcmp(id, engine_openssl_id) != 0))
177
        return 0;
178
    if (!bind_helper(e))
179
        return 0;
180
    return 1;
181
}
182
183
IMPLEMENT_DYNAMIC_CHECK_FN()
184
    IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
185
#endif                          /* ENGINE_DYNAMIC_SUPPORT */
186
#ifdef TEST_ENG_OPENSSL_RC4
187
/*-
188
 * This section of code compiles an "alternative implementation" of two modes of
189
 * RC4 into this ENGINE. The result is that EVP_CIPHER operation for "rc4"
190
 * should under normal circumstances go via this support rather than the default
191
 * EVP support. There are other symbols to tweak the testing;
192
 *    TEST_ENC_OPENSSL_RC4_OTHERS - print a one line message to stderr each time
193
 *        we're asked for a cipher we don't support (should not happen).
194
 *    TEST_ENG_OPENSSL_RC4_P_INIT - print a one line message to stderr each time
195
 *        the "init_key" handler is called.
196
 *    TEST_ENG_OPENSSL_RC4_P_CIPHER - ditto for the "cipher" handler.
197
 */
198
# include <openssl/rc4.h>
199
0
# define TEST_RC4_KEY_SIZE               16
200
typedef struct {
201
    unsigned char key[TEST_RC4_KEY_SIZE];
202
    RC4_KEY ks;
203
} TEST_RC4_KEY;
204
0
# define test(ctx) ((TEST_RC4_KEY *)EVP_CIPHER_CTX_get_cipher_data(ctx))
205
static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
206
                             const unsigned char *iv, int enc)
207
0
{
208
0
    const int n = EVP_CIPHER_CTX_get_key_length(ctx);
209
210
0
# ifdef TEST_ENG_OPENSSL_RC4_P_INIT
211
0
    fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n");
212
0
# endif
213
0
    if (n <= 0)
214
0
        return n;
215
0
    memcpy(&test(ctx)->key[0], key, n);
216
0
    RC4_set_key(&test(ctx)->ks, n, test(ctx)->key);
217
0
    return 1;
218
0
}
219
220
static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
221
                           const unsigned char *in, size_t inl)
222
0
{
223
# ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER
224
    fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n");
225
# endif
226
0
    RC4(&test(ctx)->ks, inl, in, out);
227
0
    return 1;
228
0
}
229
230
static EVP_CIPHER *r4_cipher = NULL;
231
static const EVP_CIPHER *test_r4_cipher(void)
232
0
{
233
0
    if (r4_cipher == NULL) {
234
0
        EVP_CIPHER *cipher;
235
236
0
        if ((cipher = EVP_CIPHER_meth_new(NID_rc4, 1, TEST_RC4_KEY_SIZE)) == NULL
237
0
            || !EVP_CIPHER_meth_set_iv_length(cipher, 0)
238
0
            || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_VARIABLE_LENGTH)
239
0
            || !EVP_CIPHER_meth_set_init(cipher, test_rc4_init_key)
240
0
            || !EVP_CIPHER_meth_set_do_cipher(cipher, test_rc4_cipher)
241
0
            || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(TEST_RC4_KEY))) {
242
0
            EVP_CIPHER_meth_free(cipher);
243
0
            cipher = NULL;
244
0
        }
245
0
        r4_cipher = cipher;
246
0
    }
247
0
    return r4_cipher;
248
0
}
249
static void test_r4_cipher_destroy(void)
250
0
{
251
0
    EVP_CIPHER_meth_free(r4_cipher);
252
0
    r4_cipher = NULL;
253
0
}
254
255
static EVP_CIPHER *r4_40_cipher = NULL;
256
static const EVP_CIPHER *test_r4_40_cipher(void)
257
0
{
258
0
    if (r4_40_cipher == NULL) {
259
0
        EVP_CIPHER *cipher;
260
261
0
        if ((cipher = EVP_CIPHER_meth_new(NID_rc4, 1, 5 /* 40 bits */)) == NULL
262
0
            || !EVP_CIPHER_meth_set_iv_length(cipher, 0)
263
0
            || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_VARIABLE_LENGTH)
264
0
            || !EVP_CIPHER_meth_set_init(cipher, test_rc4_init_key)
265
0
            || !EVP_CIPHER_meth_set_do_cipher(cipher, test_rc4_cipher)
266
0
            || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(TEST_RC4_KEY))) {
267
0
            EVP_CIPHER_meth_free(cipher);
268
0
            cipher = NULL;
269
0
        }
270
0
        r4_40_cipher = cipher;
271
0
    }
272
0
    return r4_40_cipher;
273
0
}
274
static void test_r4_40_cipher_destroy(void)
275
0
{
276
0
    EVP_CIPHER_meth_free(r4_40_cipher);
277
0
    r4_40_cipher = NULL;
278
0
}
279
static int test_cipher_nids(const int **nids)
280
0
{
281
0
    static int cipher_nids[4] = { 0, 0, 0, 0 };
282
0
    static int pos = 0;
283
0
    static int init = 0;
284
285
0
    if (!init) {
286
0
        const EVP_CIPHER *cipher;
287
0
        if ((cipher = test_r4_cipher()) != NULL)
288
0
            cipher_nids[pos++] = EVP_CIPHER_get_nid(cipher);
289
0
        if ((cipher = test_r4_40_cipher()) != NULL)
290
0
            cipher_nids[pos++] = EVP_CIPHER_get_nid(cipher);
291
0
        cipher_nids[pos] = 0;
292
0
        init = 1;
293
0
    }
294
0
    *nids = cipher_nids;
295
0
    return pos;
296
0
}
297
298
static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
299
                           const int **nids, int nid)
300
0
{
301
0
    if (!cipher) {
302
        /* We are returning a list of supported nids */
303
0
        return test_cipher_nids(nids);
304
0
    }
305
    /* We are being asked for a specific cipher */
306
0
    if (nid == NID_rc4)
307
0
        *cipher = test_r4_cipher();
308
0
    else if (nid == NID_rc4_40)
309
0
        *cipher = test_r4_40_cipher();
310
0
    else {
311
# ifdef TEST_ENG_OPENSSL_RC4_OTHERS
312
        fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for "
313
                "nid %d\n", nid);
314
# endif
315
0
        *cipher = NULL;
316
0
        return 0;
317
0
    }
318
0
    return 1;
319
0
}
320
#endif
321
322
#ifdef TEST_ENG_OPENSSL_SHA
323
/* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */
324
# include <openssl/sha.h>
325
326
static int test_sha1_init(EVP_MD_CTX *ctx)
327
0
{
328
# ifdef TEST_ENG_OPENSSL_SHA_P_INIT
329
    fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n");
330
# endif
331
0
    return SHA1_Init(EVP_MD_CTX_get0_md_data(ctx));
332
0
}
333
334
static int test_sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count)
335
0
{
336
# ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE
337
    fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n");
338
# endif
339
0
    return SHA1_Update(EVP_MD_CTX_get0_md_data(ctx), data, count);
340
0
}
341
342
static int test_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
343
0
{
344
# ifdef TEST_ENG_OPENSSL_SHA_P_FINAL
345
    fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n");
346
# endif
347
0
    return SHA1_Final(md, EVP_MD_CTX_get0_md_data(ctx));
348
0
}
349
350
static EVP_MD *sha1_md = NULL;
351
static const EVP_MD *test_sha_md(void)
352
0
{
353
0
    if (sha1_md == NULL) {
354
0
        EVP_MD *md;
355
356
0
        if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
357
0
            || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
358
0
            || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
359
0
            || !EVP_MD_meth_set_app_datasize(md,
360
0
                                             sizeof(EVP_MD *) + sizeof(SHA_CTX))
361
0
            || !EVP_MD_meth_set_flags(md, 0)
362
0
            || !EVP_MD_meth_set_init(md, test_sha1_init)
363
0
            || !EVP_MD_meth_set_update(md, test_sha1_update)
364
0
            || !EVP_MD_meth_set_final(md, test_sha1_final)) {
365
0
            EVP_MD_meth_free(md);
366
0
            md = NULL;
367
0
        }
368
0
        sha1_md = md;
369
0
    }
370
0
    return sha1_md;
371
0
}
372
static void test_sha_md_destroy(void)
373
0
{
374
0
    EVP_MD_meth_free(sha1_md);
375
0
    sha1_md = NULL;
376
0
}
377
static int test_digest_nids(const int **nids)
378
0
{
379
0
    static int digest_nids[2] = { 0, 0 };
380
0
    static int pos = 0;
381
0
    static int init = 0;
382
383
0
    if (!init) {
384
0
        const EVP_MD *md;
385
0
        if ((md = test_sha_md()) != NULL)
386
0
            digest_nids[pos++] = EVP_MD_get_type(md);
387
0
        digest_nids[pos] = 0;
388
0
        init = 1;
389
0
    }
390
0
    *nids = digest_nids;
391
0
    return pos;
392
0
}
393
394
static int openssl_digests(ENGINE *e, const EVP_MD **digest,
395
                           const int **nids, int nid)
396
0
{
397
0
    if (!digest) {
398
        /* We are returning a list of supported nids */
399
0
        return test_digest_nids(nids);
400
0
    }
401
    /* We are being asked for a specific digest */
402
0
    if (nid == NID_sha1)
403
0
        *digest = test_sha_md();
404
0
    else {
405
# ifdef TEST_ENG_OPENSSL_SHA_OTHERS
406
        fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for "
407
                "nid %d\n", nid);
408
# endif
409
0
        *digest = NULL;
410
0
        return 0;
411
0
    }
412
0
    return 1;
413
0
}
414
#endif
415
416
#ifdef TEST_ENG_OPENSSL_PKEY
417
static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
418
                                      UI_METHOD *ui_method,
419
                                      void *callback_data)
420
0
{
421
0
    BIO *in;
422
0
    EVP_PKEY *key;
423
0
    fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n",
424
0
            key_id);
425
# if defined(OPENSSL_SYS_WINDOWS)
426
    in = BIO_new_file(key_id, "rb");
427
# else
428
0
    in = BIO_new_file(key_id, "r");
429
0
# endif
430
0
    if (!in)
431
0
        return NULL;
432
0
    key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL);
433
0
    BIO_free(in);
434
0
    return key;
435
0
}
436
#endif
437
438
#ifdef TEST_ENG_OPENSSL_HMAC
439
440
/*
441
 * Experimental HMAC redirection implementation: mainly copied from
442
 * hm_pmeth.c
443
 */
444
445
/* HMAC pkey context structure */
446
447
typedef struct {
448
    const EVP_MD *md;           /* MD for HMAC use */
449
    ASN1_OCTET_STRING ktmp;     /* Temp storage for key */
450
    HMAC_CTX *ctx;
451
} OSSL_HMAC_PKEY_CTX;
452
453
static int ossl_hmac_init(EVP_PKEY_CTX *ctx)
454
{
455
    OSSL_HMAC_PKEY_CTX *hctx;
456
457
    if ((hctx = OPENSSL_zalloc(sizeof(*hctx))) == NULL)
458
        return 0;
459
    hctx->ktmp.type = V_ASN1_OCTET_STRING;
460
    hctx->ctx = HMAC_CTX_new();
461
    if (hctx->ctx == NULL) {
462
        OPENSSL_free(hctx);
463
        return 0;
464
    }
465
    EVP_PKEY_CTX_set_data(ctx, hctx);
466
    EVP_PKEY_CTX_set0_keygen_info(ctx, NULL, 0);
467
# ifdef TEST_ENG_OPENSSL_HMAC_INIT
468
    fprintf(stderr, "(TEST_ENG_OPENSSL_HMAC) ossl_hmac_init() called\n");
469
# endif
470
    return 1;
471
}
472
473
static void ossl_hmac_cleanup(EVP_PKEY_CTX *ctx);
474
475
static int ossl_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
476
{
477
    OSSL_HMAC_PKEY_CTX *sctx, *dctx;
478
479
    /* allocate memory for dst->data and a new HMAC_CTX in dst->data->ctx */
480
    if (!ossl_hmac_init(dst))
481
        return 0;
482
    sctx = EVP_PKEY_CTX_get_data(src);
483
    dctx = EVP_PKEY_CTX_get_data(dst);
484
    dctx->md = sctx->md;
485
    if (!HMAC_CTX_copy(dctx->ctx, sctx->ctx))
486
        goto err;
487
    if (sctx->ktmp.data) {
488
        if (!ASN1_OCTET_STRING_set(&dctx->ktmp,
489
                                   sctx->ktmp.data, sctx->ktmp.length))
490
            goto err;
491
    }
492
    return 1;
493
err:
494
    /* release HMAC_CTX in dst->data->ctx and memory allocated for dst->data */
495
    ossl_hmac_cleanup(dst);
496
    return 0;
497
}
498
499
static void ossl_hmac_cleanup(EVP_PKEY_CTX *ctx)
500
{
501
    OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
502
503
    if (hctx) {
504
        HMAC_CTX_free(hctx->ctx);
505
        OPENSSL_clear_free(hctx->ktmp.data, hctx->ktmp.length);
506
        OPENSSL_free(hctx);
507
        EVP_PKEY_CTX_set_data(ctx, NULL);
508
    }
509
}
510
511
static int ossl_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
512
{
513
    ASN1_OCTET_STRING *hkey = NULL;
514
    OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
515
    if (!hctx->ktmp.data)
516
        return 0;
517
    hkey = ASN1_OCTET_STRING_dup(&hctx->ktmp);
518
    if (!hkey)
519
        return 0;
520
    EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, hkey);
521
522
    return 1;
523
}
524
525
static int ossl_int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
526
{
527
    OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_get_pkey_ctx(ctx));
528
    if (!HMAC_Update(hctx->ctx, data, count))
529
        return 0;
530
    return 1;
531
}
532
533
static int ossl_hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
534
{
535
    EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
536
    EVP_MD_CTX_set_update_fn(mctx, ossl_int_update);
537
    return 1;
538
}
539
540
static int ossl_hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
541
                             size_t *siglen, EVP_MD_CTX *mctx)
542
{
543
    unsigned int hlen;
544
    OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
545
    int l = EVP_MD_CTX_get_size(mctx);
546
547
    if (l < 0)
548
        return 0;
549
    *siglen = l;
550
    if (!sig)
551
        return 1;
552
553
    if (!HMAC_Final(hctx->ctx, sig, &hlen))
554
        return 0;
555
    *siglen = (size_t)hlen;
556
    return 1;
557
}
558
559
static int ossl_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
560
{
561
    OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
562
    EVP_PKEY *pk;
563
    ASN1_OCTET_STRING *key;
564
    switch (type) {
565
566
    case EVP_PKEY_CTRL_SET_MAC_KEY:
567
        if ((!p2 && p1 > 0) || (p1 < -1))
568
            return 0;
569
        if (!ASN1_OCTET_STRING_set(&hctx->ktmp, p2, p1))
570
            return 0;
571
        break;
572
573
    case EVP_PKEY_CTRL_MD:
574
        hctx->md = p2;
575
        break;
576
577
    case EVP_PKEY_CTRL_DIGESTINIT:
578
        pk = EVP_PKEY_CTX_get0_pkey(ctx);
579
        key = EVP_PKEY_get0(pk);
580
        if (!HMAC_Init_ex(hctx->ctx, key->data, key->length, hctx->md, NULL))
581
            return 0;
582
        break;
583
584
    default:
585
        return -2;
586
587
    }
588
    return 1;
589
}
590
591
static int ossl_hmac_ctrl_str(EVP_PKEY_CTX *ctx,
592
                              const char *type, const char *value)
593
{
594
    if (!value) {
595
        return 0;
596
    }
597
    if (strcmp(type, "key") == 0) {
598
        void *p = (void *)value;
599
        return ossl_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, -1, p);
600
    }
601
    if (strcmp(type, "hexkey") == 0) {
602
        unsigned char *key;
603
        int r;
604
        long keylen;
605
        key = OPENSSL_hexstr2buf(value, &keylen);
606
        if (!key)
607
            return 0;
608
        r = ossl_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key);
609
        OPENSSL_free(key);
610
        return r;
611
    }
612
    return -2;
613
}
614
615
static EVP_PKEY_METHOD *ossl_hmac_meth;
616
617
static int ossl_register_hmac_meth(void)
618
{
619
    EVP_PKEY_METHOD *meth;
620
    meth = EVP_PKEY_meth_new(EVP_PKEY_HMAC, 0);
621
    if (meth == NULL)
622
        return 0;
623
    EVP_PKEY_meth_set_init(meth, ossl_hmac_init);
624
    EVP_PKEY_meth_set_copy(meth, ossl_hmac_copy);
625
    EVP_PKEY_meth_set_cleanup(meth, ossl_hmac_cleanup);
626
627
    EVP_PKEY_meth_set_keygen(meth, 0, ossl_hmac_keygen);
628
629
    EVP_PKEY_meth_set_signctx(meth, ossl_hmac_signctx_init,
630
                              ossl_hmac_signctx);
631
632
    EVP_PKEY_meth_set_ctrl(meth, ossl_hmac_ctrl, ossl_hmac_ctrl_str);
633
    ossl_hmac_meth = meth;
634
    return 1;
635
}
636
637
static int ossl_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
638
                           const int **nids, int nid)
639
{
640
    static int ossl_pkey_nids[] = {
641
        EVP_PKEY_HMAC,
642
        0
643
    };
644
645
    if (pmeth == NULL) {
646
        *nids = ossl_pkey_nids;
647
        return 1;
648
    }
649
650
    if (nid == EVP_PKEY_HMAC) {
651
        *pmeth = ossl_hmac_meth;
652
        return 1;
653
    }
654
655
    *pmeth = NULL;
656
    return 0;
657
}
658
659
#endif
660
661
int openssl_destroy(ENGINE *e)
662
0
{
663
0
    test_sha_md_destroy();
664
0
#ifdef TEST_ENG_OPENSSL_RC4
665
0
    test_r4_cipher_destroy();
666
0
    test_r4_40_cipher_destroy();
667
0
#endif
668
0
    return 1;
669
0
}
670