Coverage Report

Created: 2023-06-08 06:40

/src/openssl111/crypto/evp/pmeth_lib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
#include <stdio.h>
11
#include <stdlib.h>
12
#include "internal/cryptlib.h"
13
#include <openssl/engine.h>
14
#include <openssl/evp.h>
15
#include <openssl/x509v3.h>
16
#include "crypto/asn1.h"
17
#include "crypto/evp.h"
18
#include "internal/numbers.h"
19
20
typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
21
22
static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
23
24
/* This array needs to be in order of NIDs */
25
static const EVP_PKEY_METHOD *standard_methods[] = {
26
#ifndef OPENSSL_NO_RSA
27
    &rsa_pkey_meth,
28
#endif
29
#ifndef OPENSSL_NO_DH
30
    &dh_pkey_meth,
31
#endif
32
#ifndef OPENSSL_NO_DSA
33
    &dsa_pkey_meth,
34
#endif
35
#ifndef OPENSSL_NO_EC
36
    &ec_pkey_meth,
37
#endif
38
    &hmac_pkey_meth,
39
#ifndef OPENSSL_NO_CMAC
40
    &cmac_pkey_meth,
41
#endif
42
#ifndef OPENSSL_NO_RSA
43
    &rsa_pss_pkey_meth,
44
#endif
45
#ifndef OPENSSL_NO_DH
46
    &dhx_pkey_meth,
47
#endif
48
#ifndef OPENSSL_NO_SCRYPT
49
    &scrypt_pkey_meth,
50
#endif
51
    &tls1_prf_pkey_meth,
52
#ifndef OPENSSL_NO_EC
53
    &ecx25519_pkey_meth,
54
    &ecx448_pkey_meth,
55
#endif
56
    &hkdf_pkey_meth,
57
#ifndef OPENSSL_NO_POLY1305
58
    &poly1305_pkey_meth,
59
#endif
60
#ifndef OPENSSL_NO_SIPHASH
61
    &siphash_pkey_meth,
62
#endif
63
#ifndef OPENSSL_NO_EC
64
    &ed25519_pkey_meth,
65
    &ed448_pkey_meth,
66
#endif
67
#ifndef OPENSSL_NO_SM2
68
    &sm2_pkey_meth,
69
#endif
70
};
71
72
DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
73
                           pmeth);
74
75
static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
76
                     const EVP_PKEY_METHOD *const *b)
77
0
{
78
0
    return ((*a)->pkey_id - (*b)->pkey_id);
79
0
}
80
81
IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
82
                             pmeth);
83
84
const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
85
0
{
86
0
    EVP_PKEY_METHOD tmp;
87
0
    const EVP_PKEY_METHOD *t = &tmp, **ret;
88
0
    tmp.pkey_id = type;
89
0
    if (app_pkey_methods) {
90
0
        int idx;
91
0
        idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
92
0
        if (idx >= 0)
93
0
            return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
94
0
    }
95
0
    ret = OBJ_bsearch_pmeth(&t, standard_methods,
96
0
                            sizeof(standard_methods) /
97
0
                            sizeof(EVP_PKEY_METHOD *));
98
0
    if (!ret || !*ret)
99
0
        return NULL;
100
0
    return *ret;
101
0
}
102
103
static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
104
0
{
105
0
    EVP_PKEY_CTX *ret;
106
0
    const EVP_PKEY_METHOD *pmeth;
107
108
0
    if (id == -1) {
109
0
        if (pkey == NULL)
110
0
            return 0;
111
0
        id = pkey->type;
112
0
    }
113
0
#ifndef OPENSSL_NO_ENGINE
114
0
    if (e == NULL && pkey != NULL)
115
0
        e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
116
    /* Try to find an ENGINE which implements this method */
117
0
    if (e) {
118
0
        if (!ENGINE_init(e)) {
119
0
            EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
120
0
            return NULL;
121
0
        }
122
0
    } else {
123
0
        e = ENGINE_get_pkey_meth_engine(id);
124
0
    }
125
126
    /*
127
     * If an ENGINE handled this method look it up. Otherwise use internal
128
     * tables.
129
     */
130
0
    if (e)
131
0
        pmeth = ENGINE_get_pkey_meth(e, id);
132
0
    else
133
0
#endif
134
0
        pmeth = EVP_PKEY_meth_find(id);
135
136
0
    if (pmeth == NULL) {
137
0
#ifndef OPENSSL_NO_ENGINE
138
0
        ENGINE_finish(e);
139
0
#endif
140
0
        EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
141
0
        return NULL;
142
0
    }
143
144
0
    ret = OPENSSL_zalloc(sizeof(*ret));
145
0
    if (ret == NULL) {
146
0
#ifndef OPENSSL_NO_ENGINE
147
0
        ENGINE_finish(e);
148
0
#endif
149
0
        EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
150
0
        return NULL;
151
0
    }
152
0
    ret->engine = e;
153
0
    ret->pmeth = pmeth;
154
0
    ret->operation = EVP_PKEY_OP_UNDEFINED;
155
0
    ret->pkey = pkey;
156
0
    if (pkey != NULL)
157
0
        EVP_PKEY_up_ref(pkey);
158
159
0
    if (pmeth->init) {
160
0
        if (pmeth->init(ret) <= 0) {
161
0
            ret->pmeth = NULL;
162
0
            EVP_PKEY_CTX_free(ret);
163
0
            return NULL;
164
0
        }
165
0
    }
166
167
0
    return ret;
168
0
}
169
170
EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
171
0
{
172
0
    EVP_PKEY_METHOD *pmeth;
173
174
0
    pmeth = OPENSSL_zalloc(sizeof(*pmeth));
175
0
    if (pmeth == NULL) {
176
0
        EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
177
0
        return NULL;
178
0
    }
179
180
0
    pmeth->pkey_id = id;
181
0
    pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
182
0
    return pmeth;
183
0
}
184
185
void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
186
                             const EVP_PKEY_METHOD *meth)
187
0
{
188
0
    if (ppkey_id)
189
0
        *ppkey_id = meth->pkey_id;
190
0
    if (pflags)
191
0
        *pflags = meth->flags;
192
0
}
193
194
void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
195
0
{
196
197
0
    dst->init = src->init;
198
0
    dst->copy = src->copy;
199
0
    dst->cleanup = src->cleanup;
200
201
0
    dst->paramgen_init = src->paramgen_init;
202
0
    dst->paramgen = src->paramgen;
203
204
0
    dst->keygen_init = src->keygen_init;
205
0
    dst->keygen = src->keygen;
206
207
0
    dst->sign_init = src->sign_init;
208
0
    dst->sign = src->sign;
209
210
0
    dst->verify_init = src->verify_init;
211
0
    dst->verify = src->verify;
212
213
0
    dst->verify_recover_init = src->verify_recover_init;
214
0
    dst->verify_recover = src->verify_recover;
215
216
0
    dst->signctx_init = src->signctx_init;
217
0
    dst->signctx = src->signctx;
218
219
0
    dst->verifyctx_init = src->verifyctx_init;
220
0
    dst->verifyctx = src->verifyctx;
221
222
0
    dst->encrypt_init = src->encrypt_init;
223
0
    dst->encrypt = src->encrypt;
224
225
0
    dst->decrypt_init = src->decrypt_init;
226
0
    dst->decrypt = src->decrypt;
227
228
0
    dst->derive_init = src->derive_init;
229
0
    dst->derive = src->derive;
230
231
0
    dst->ctrl = src->ctrl;
232
0
    dst->ctrl_str = src->ctrl_str;
233
234
0
    dst->check = src->check;
235
0
}
236
237
void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
238
0
{
239
0
    if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
240
0
        OPENSSL_free(pmeth);
241
0
}
242
243
EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
244
0
{
245
0
    return int_ctx_new(pkey, e, -1);
246
0
}
247
248
EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
249
0
{
250
0
    return int_ctx_new(NULL, e, id);
251
0
}
252
253
EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
254
0
{
255
0
    EVP_PKEY_CTX *rctx;
256
0
    if (!pctx->pmeth || !pctx->pmeth->copy)
257
0
        return NULL;
258
0
#ifndef OPENSSL_NO_ENGINE
259
    /* Make sure it's safe to copy a pkey context using an ENGINE */
260
0
    if (pctx->engine && !ENGINE_init(pctx->engine)) {
261
0
        EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
262
0
        return 0;
263
0
    }
264
0
#endif
265
0
    rctx = OPENSSL_malloc(sizeof(*rctx));
266
0
    if (rctx == NULL) {
267
0
        EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
268
0
        return NULL;
269
0
    }
270
271
0
    rctx->pmeth = pctx->pmeth;
272
0
#ifndef OPENSSL_NO_ENGINE
273
0
    rctx->engine = pctx->engine;
274
0
#endif
275
276
0
    if (pctx->pkey)
277
0
        EVP_PKEY_up_ref(pctx->pkey);
278
279
0
    rctx->pkey = pctx->pkey;
280
281
0
    if (pctx->peerkey)
282
0
        EVP_PKEY_up_ref(pctx->peerkey);
283
284
0
    rctx->peerkey = pctx->peerkey;
285
286
0
    rctx->data = NULL;
287
0
    rctx->app_data = NULL;
288
0
    rctx->operation = pctx->operation;
289
290
0
    if (pctx->pmeth->copy(rctx, pctx) > 0)
291
0
        return rctx;
292
293
0
    rctx->pmeth = NULL;
294
0
    EVP_PKEY_CTX_free(rctx);
295
0
    return NULL;
296
297
0
}
298
299
int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
300
0
{
301
0
    if (app_pkey_methods == NULL) {
302
0
        app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
303
0
        if (app_pkey_methods == NULL){
304
0
            EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
305
0
            return 0;
306
0
        }
307
0
    }
308
0
    if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
309
0
        EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
310
0
        return 0;
311
0
    }
312
0
    sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
313
0
    return 1;
314
0
}
315
316
void evp_app_cleanup_int(void)
317
2
{
318
2
    if (app_pkey_methods != NULL)
319
0
        sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
320
2
}
321
322
int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
323
0
{
324
0
    const EVP_PKEY_METHOD *ret;
325
326
0
    ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
327
328
0
    return ret == NULL ? 0 : 1;
329
0
}
330
331
size_t EVP_PKEY_meth_get_count(void)
332
0
{
333
0
    size_t rv = OSSL_NELEM(standard_methods);
334
335
0
    if (app_pkey_methods)
336
0
        rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
337
0
    return rv;
338
0
}
339
340
const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
341
0
{
342
0
    if (idx < OSSL_NELEM(standard_methods))
343
0
        return standard_methods[idx];
344
0
    if (app_pkey_methods == NULL)
345
0
        return NULL;
346
0
    idx -= OSSL_NELEM(standard_methods);
347
0
    if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
348
0
        return NULL;
349
0
    return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
350
0
}
351
352
void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
353
0
{
354
0
    if (ctx == NULL)
355
0
        return;
356
0
    if (ctx->pmeth && ctx->pmeth->cleanup)
357
0
        ctx->pmeth->cleanup(ctx);
358
0
    EVP_PKEY_free(ctx->pkey);
359
0
    EVP_PKEY_free(ctx->peerkey);
360
0
#ifndef OPENSSL_NO_ENGINE
361
0
    ENGINE_finish(ctx->engine);
362
0
#endif
363
0
    OPENSSL_free(ctx);
364
0
}
365
366
int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
367
                      int cmd, int p1, void *p2)
368
0
{
369
0
    int ret;
370
371
0
    if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
372
0
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
373
0
        return -2;
374
0
    }
375
0
    if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
376
0
        return -1;
377
378
    /* Skip the operation checks since this is called in a very early stage */
379
0
    if (ctx->pmeth->digest_custom != NULL)
380
0
        goto doit;
381
382
0
    if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
383
0
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
384
0
        return -1;
385
0
    }
386
387
0
    if ((optype != -1) && !(ctx->operation & optype)) {
388
0
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
389
0
        return -1;
390
0
    }
391
392
0
 doit:
393
0
    ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
394
395
0
    if (ret == -2)
396
0
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
397
398
0
    return ret;
399
0
}
400
401
int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
402
                             int cmd, uint64_t value)
403
0
{
404
0
    return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
405
0
}
406
407
int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
408
                          const char *name, const char *value)
409
0
{
410
0
    if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
411
0
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
412
0
        return -2;
413
0
    }
414
0
    if (strcmp(name, "digest") == 0)
415
0
        return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
416
0
                               value);
417
0
    return ctx->pmeth->ctrl_str(ctx, name, value);
418
0
}
419
420
/* Utility functions to send a string of hex string to a ctrl */
421
422
int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
423
0
{
424
0
    size_t len;
425
426
0
    len = strlen(str);
427
0
    if (len > INT_MAX)
428
0
        return -1;
429
0
    return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
430
0
}
431
432
int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
433
0
{
434
0
    unsigned char *bin;
435
0
    long binlen;
436
0
    int rv = -1;
437
438
0
    bin = OPENSSL_hexstr2buf(hex, &binlen);
439
0
    if (bin == NULL)
440
0
        return 0;
441
0
    if (binlen <= INT_MAX)
442
0
        rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
443
0
    OPENSSL_free(bin);
444
0
    return rv;
445
0
}
446
447
/* Pass a message digest to a ctrl */
448
int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
449
0
{
450
0
    const EVP_MD *m;
451
452
0
    if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
453
0
        EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
454
0
        return 0;
455
0
    }
456
0
    return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
457
0
}
458
459
int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
460
0
{
461
0
    return ctx->operation;
462
0
}
463
464
void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
465
0
{
466
0
    ctx->keygen_info = dat;
467
0
    ctx->keygen_info_count = datlen;
468
0
}
469
470
void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
471
0
{
472
0
    ctx->data = data;
473
0
}
474
475
void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
476
0
{
477
0
    return ctx->data;
478
0
}
479
480
EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
481
0
{
482
0
    return ctx->pkey;
483
0
}
484
485
EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
486
0
{
487
0
    return ctx->peerkey;
488
0
}
489
490
void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
491
0
{
492
0
    ctx->app_data = data;
493
0
}
494
495
void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
496
0
{
497
0
    return ctx->app_data;
498
0
}
499
500
void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
501
                            int (*init) (EVP_PKEY_CTX *ctx))
502
0
{
503
0
    pmeth->init = init;
504
0
}
505
506
void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
507
                            int (*copy) (EVP_PKEY_CTX *dst,
508
                                         EVP_PKEY_CTX *src))
509
0
{
510
0
    pmeth->copy = copy;
511
0
}
512
513
void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
514
                               void (*cleanup) (EVP_PKEY_CTX *ctx))
515
0
{
516
0
    pmeth->cleanup = cleanup;
517
0
}
518
519
void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
520
                                int (*paramgen_init) (EVP_PKEY_CTX *ctx),
521
                                int (*paramgen) (EVP_PKEY_CTX *ctx,
522
                                                 EVP_PKEY *pkey))
523
0
{
524
0
    pmeth->paramgen_init = paramgen_init;
525
0
    pmeth->paramgen = paramgen;
526
0
}
527
528
void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
529
                              int (*keygen_init) (EVP_PKEY_CTX *ctx),
530
                              int (*keygen) (EVP_PKEY_CTX *ctx,
531
                                             EVP_PKEY *pkey))
532
0
{
533
0
    pmeth->keygen_init = keygen_init;
534
0
    pmeth->keygen = keygen;
535
0
}
536
537
void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
538
                            int (*sign_init) (EVP_PKEY_CTX *ctx),
539
                            int (*sign) (EVP_PKEY_CTX *ctx,
540
                                         unsigned char *sig, size_t *siglen,
541
                                         const unsigned char *tbs,
542
                                         size_t tbslen))
543
0
{
544
0
    pmeth->sign_init = sign_init;
545
0
    pmeth->sign = sign;
546
0
}
547
548
void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
549
                              int (*verify_init) (EVP_PKEY_CTX *ctx),
550
                              int (*verify) (EVP_PKEY_CTX *ctx,
551
                                             const unsigned char *sig,
552
                                             size_t siglen,
553
                                             const unsigned char *tbs,
554
                                             size_t tbslen))
555
0
{
556
0
    pmeth->verify_init = verify_init;
557
0
    pmeth->verify = verify;
558
0
}
559
560
void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
561
                                      int (*verify_recover_init) (EVP_PKEY_CTX
562
                                                                  *ctx),
563
                                      int (*verify_recover) (EVP_PKEY_CTX
564
                                                             *ctx,
565
                                                             unsigned char
566
                                                             *sig,
567
                                                             size_t *siglen,
568
                                                             const unsigned
569
                                                             char *tbs,
570
                                                             size_t tbslen))
571
0
{
572
0
    pmeth->verify_recover_init = verify_recover_init;
573
0
    pmeth->verify_recover = verify_recover;
574
0
}
575
576
void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
577
                               int (*signctx_init) (EVP_PKEY_CTX *ctx,
578
                                                    EVP_MD_CTX *mctx),
579
                               int (*signctx) (EVP_PKEY_CTX *ctx,
580
                                               unsigned char *sig,
581
                                               size_t *siglen,
582
                                               EVP_MD_CTX *mctx))
583
0
{
584
0
    pmeth->signctx_init = signctx_init;
585
0
    pmeth->signctx = signctx;
586
0
}
587
588
void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
589
                                 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
590
                                                        EVP_MD_CTX *mctx),
591
                                 int (*verifyctx) (EVP_PKEY_CTX *ctx,
592
                                                   const unsigned char *sig,
593
                                                   int siglen,
594
                                                   EVP_MD_CTX *mctx))
595
0
{
596
0
    pmeth->verifyctx_init = verifyctx_init;
597
0
    pmeth->verifyctx = verifyctx;
598
0
}
599
600
void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
601
                               int (*encrypt_init) (EVP_PKEY_CTX *ctx),
602
                               int (*encryptfn) (EVP_PKEY_CTX *ctx,
603
                                                 unsigned char *out,
604
                                                 size_t *outlen,
605
                                                 const unsigned char *in,
606
                                                 size_t inlen))
607
0
{
608
0
    pmeth->encrypt_init = encrypt_init;
609
0
    pmeth->encrypt = encryptfn;
610
0
}
611
612
void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
613
                               int (*decrypt_init) (EVP_PKEY_CTX *ctx),
614
                               int (*decrypt) (EVP_PKEY_CTX *ctx,
615
                                               unsigned char *out,
616
                                               size_t *outlen,
617
                                               const unsigned char *in,
618
                                               size_t inlen))
619
0
{
620
0
    pmeth->decrypt_init = decrypt_init;
621
0
    pmeth->decrypt = decrypt;
622
0
}
623
624
void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
625
                              int (*derive_init) (EVP_PKEY_CTX *ctx),
626
                              int (*derive) (EVP_PKEY_CTX *ctx,
627
                                             unsigned char *key,
628
                                             size_t *keylen))
629
0
{
630
0
    pmeth->derive_init = derive_init;
631
0
    pmeth->derive = derive;
632
0
}
633
634
void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
635
                            int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
636
                                         void *p2),
637
                            int (*ctrl_str) (EVP_PKEY_CTX *ctx,
638
                                             const char *type,
639
                                             const char *value))
640
0
{
641
0
    pmeth->ctrl = ctrl;
642
0
    pmeth->ctrl_str = ctrl_str;
643
0
}
644
645
void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
646
    int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
647
                       const unsigned char *tbs, size_t tbslen))
648
0
{
649
0
    pmeth->digestsign = digestsign;
650
0
}
651
652
void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
653
    int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
654
                         size_t siglen, const unsigned char *tbs,
655
                         size_t tbslen))
656
0
{
657
0
    pmeth->digestverify = digestverify;
658
0
}
659
660
void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
661
                             int (*check) (EVP_PKEY *pkey))
662
0
{
663
0
    pmeth->check = check;
664
0
}
665
666
void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
667
                                    int (*check) (EVP_PKEY *pkey))
668
0
{
669
0
    pmeth->public_check = check;
670
0
}
671
672
void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
673
                                   int (*check) (EVP_PKEY *pkey))
674
0
{
675
0
    pmeth->param_check = check;
676
0
}
677
678
void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
679
                                     int (*digest_custom) (EVP_PKEY_CTX *ctx,
680
                                                           EVP_MD_CTX *mctx))
681
0
{
682
0
    pmeth->digest_custom = digest_custom;
683
0
}
684
685
void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
686
                            int (**pinit) (EVP_PKEY_CTX *ctx))
687
0
{
688
0
    *pinit = pmeth->init;
689
0
}
690
691
void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
692
                            int (**pcopy) (EVP_PKEY_CTX *dst,
693
                                           EVP_PKEY_CTX *src))
694
0
{
695
0
    *pcopy = pmeth->copy;
696
0
}
697
698
void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
699
                               void (**pcleanup) (EVP_PKEY_CTX *ctx))
700
0
{
701
0
    *pcleanup = pmeth->cleanup;
702
0
}
703
704
void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
705
                                int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
706
                                int (**pparamgen) (EVP_PKEY_CTX *ctx,
707
                                                   EVP_PKEY *pkey))
708
0
{
709
0
    if (pparamgen_init)
710
0
        *pparamgen_init = pmeth->paramgen_init;
711
0
    if (pparamgen)
712
0
        *pparamgen = pmeth->paramgen;
713
0
}
714
715
void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
716
                              int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
717
                              int (**pkeygen) (EVP_PKEY_CTX *ctx,
718
                                               EVP_PKEY *pkey))
719
0
{
720
0
    if (pkeygen_init)
721
0
        *pkeygen_init = pmeth->keygen_init;
722
0
    if (pkeygen)
723
0
        *pkeygen = pmeth->keygen;
724
0
}
725
726
void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
727
                            int (**psign_init) (EVP_PKEY_CTX *ctx),
728
                            int (**psign) (EVP_PKEY_CTX *ctx,
729
                                           unsigned char *sig, size_t *siglen,
730
                                           const unsigned char *tbs,
731
                                           size_t tbslen))
732
0
{
733
0
    if (psign_init)
734
0
        *psign_init = pmeth->sign_init;
735
0
    if (psign)
736
0
        *psign = pmeth->sign;
737
0
}
738
739
void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
740
                              int (**pverify_init) (EVP_PKEY_CTX *ctx),
741
                              int (**pverify) (EVP_PKEY_CTX *ctx,
742
                                               const unsigned char *sig,
743
                                               size_t siglen,
744
                                               const unsigned char *tbs,
745
                                               size_t tbslen))
746
0
{
747
0
    if (pverify_init)
748
0
        *pverify_init = pmeth->verify_init;
749
0
    if (pverify)
750
0
        *pverify = pmeth->verify;
751
0
}
752
753
void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
754
                                      int (**pverify_recover_init) (EVP_PKEY_CTX
755
                                                                    *ctx),
756
                                      int (**pverify_recover) (EVP_PKEY_CTX
757
                                                               *ctx,
758
                                                               unsigned char
759
                                                               *sig,
760
                                                               size_t *siglen,
761
                                                               const unsigned
762
                                                               char *tbs,
763
                                                               size_t tbslen))
764
0
{
765
0
    if (pverify_recover_init)
766
0
        *pverify_recover_init = pmeth->verify_recover_init;
767
0
    if (pverify_recover)
768
0
        *pverify_recover = pmeth->verify_recover;
769
0
}
770
771
void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
772
                               int (**psignctx_init) (EVP_PKEY_CTX *ctx,
773
                                                      EVP_MD_CTX *mctx),
774
                               int (**psignctx) (EVP_PKEY_CTX *ctx,
775
                                                 unsigned char *sig,
776
                                                 size_t *siglen,
777
                                                 EVP_MD_CTX *mctx))
778
0
{
779
0
    if (psignctx_init)
780
0
        *psignctx_init = pmeth->signctx_init;
781
0
    if (psignctx)
782
0
        *psignctx = pmeth->signctx;
783
0
}
784
785
void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
786
                                 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
787
                                                          EVP_MD_CTX *mctx),
788
                                 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
789
                                                     const unsigned char *sig,
790
                                                     int siglen,
791
                                                     EVP_MD_CTX *mctx))
792
0
{
793
0
    if (pverifyctx_init)
794
0
        *pverifyctx_init = pmeth->verifyctx_init;
795
0
    if (pverifyctx)
796
0
        *pverifyctx = pmeth->verifyctx;
797
0
}
798
799
void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
800
                               int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
801
                               int (**pencryptfn) (EVP_PKEY_CTX *ctx,
802
                                                   unsigned char *out,
803
                                                   size_t *outlen,
804
                                                   const unsigned char *in,
805
                                                   size_t inlen))
806
0
{
807
0
    if (pencrypt_init)
808
0
        *pencrypt_init = pmeth->encrypt_init;
809
0
    if (pencryptfn)
810
0
        *pencryptfn = pmeth->encrypt;
811
0
}
812
813
void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
814
                               int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
815
                               int (**pdecrypt) (EVP_PKEY_CTX *ctx,
816
                                                 unsigned char *out,
817
                                                 size_t *outlen,
818
                                                 const unsigned char *in,
819
                                                 size_t inlen))
820
0
{
821
0
    if (pdecrypt_init)
822
0
        *pdecrypt_init = pmeth->decrypt_init;
823
0
    if (pdecrypt)
824
0
        *pdecrypt = pmeth->decrypt;
825
0
}
826
827
void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
828
                              int (**pderive_init) (EVP_PKEY_CTX *ctx),
829
                              int (**pderive) (EVP_PKEY_CTX *ctx,
830
                                               unsigned char *key,
831
                                               size_t *keylen))
832
0
{
833
0
    if (pderive_init)
834
0
        *pderive_init = pmeth->derive_init;
835
0
    if (pderive)
836
0
        *pderive = pmeth->derive;
837
0
}
838
839
void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
840
                            int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
841
                                           void *p2),
842
                            int (**pctrl_str) (EVP_PKEY_CTX *ctx,
843
                                               const char *type,
844
                                               const char *value))
845
0
{
846
0
    if (pctrl)
847
0
        *pctrl = pmeth->ctrl;
848
0
    if (pctrl_str)
849
0
        *pctrl_str = pmeth->ctrl_str;
850
0
}
851
852
void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD *pmeth,
853
    int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
854
                        const unsigned char *tbs, size_t tbslen))
855
0
{
856
0
    if (digestsign)
857
0
        *digestsign = pmeth->digestsign;
858
0
}
859
860
void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD *pmeth,
861
    int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
862
                          size_t siglen, const unsigned char *tbs,
863
                          size_t tbslen))
864
0
{
865
0
    if (digestverify)
866
0
        *digestverify = pmeth->digestverify;
867
0
}
868
869
void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
870
                             int (**pcheck) (EVP_PKEY *pkey))
871
0
{
872
0
    if (pcheck != NULL)
873
0
        *pcheck = pmeth->check;
874
0
}
875
876
void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
877
                                    int (**pcheck) (EVP_PKEY *pkey))
878
0
{
879
0
    if (pcheck != NULL)
880
0
        *pcheck = pmeth->public_check;
881
0
}
882
883
void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
884
                                   int (**pcheck) (EVP_PKEY *pkey))
885
0
{
886
0
    if (pcheck != NULL)
887
0
        *pcheck = pmeth->param_check;
888
0
}
889
890
void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
891
                                     int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
892
                                                             EVP_MD_CTX *mctx))
893
0
{
894
0
    if (pdigest_custom != NULL)
895
0
        *pdigest_custom = pmeth->digest_custom;
896
0
}