Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/asn1/ameth_lib.c
Line
Count
Source
1
/*
2
 * Copyright 2006-2022 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
/* We need to use some engine deprecated APIs */
11
#define OPENSSL_SUPPRESS_DEPRECATED
12
13
#include "internal/cryptlib.h"
14
#include <stdio.h>
15
#include <openssl/asn1t.h>
16
#include <openssl/x509.h>
17
#include <openssl/engine.h>
18
#include "crypto/asn1.h"
19
#include "crypto/evp.h"
20
21
#include "standard_methods.h"
22
23
typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
24
static STACK_OF(EVP_PKEY_ASN1_METHOD) *app_methods = NULL;
25
26
DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
27
    const EVP_PKEY_ASN1_METHOD *, ameth);
28
29
static int ameth_cmp(const EVP_PKEY_ASN1_METHOD *const *a,
30
    const EVP_PKEY_ASN1_METHOD *const *b)
31
17.0M
{
32
17.0M
    return ((*a)->pkey_id - (*b)->pkey_id);
33
17.0M
}
34
35
IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
36
    const EVP_PKEY_ASN1_METHOD *, ameth);
37
38
int EVP_PKEY_asn1_get_count(void)
39
5.83M
{
40
5.83M
    int num = OSSL_NELEM(standard_methods);
41
5.83M
    if (app_methods)
42
0
        num += sk_EVP_PKEY_ASN1_METHOD_num(app_methods);
43
5.83M
    return num;
44
5.83M
}
45
46
const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx)
47
77.3M
{
48
77.3M
    int num = OSSL_NELEM(standard_methods);
49
77.3M
    if (idx < 0)
50
0
        return NULL;
51
77.3M
    if (idx < num)
52
77.3M
        return standard_methods[idx];
53
0
    idx -= num;
54
0
    return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
55
77.3M
}
56
57
static const EVP_PKEY_ASN1_METHOD *pkey_asn1_find(int type)
58
5.55M
{
59
5.55M
    EVP_PKEY_ASN1_METHOD tmp;
60
5.55M
    const EVP_PKEY_ASN1_METHOD *t = &tmp, **ret;
61
62
5.55M
    tmp.pkey_id = type;
63
5.55M
    if (app_methods) {
64
0
        int idx;
65
0
        idx = sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp);
66
0
        if (idx >= 0)
67
0
            return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
68
0
    }
69
5.55M
    ret = OBJ_bsearch_ameth(&t, standard_methods, OSSL_NELEM(standard_methods));
70
5.55M
    if (ret == NULL || *ret == NULL)
71
993k
        return NULL;
72
4.56M
    return *ret;
73
5.55M
}
74
75
/*
76
 * Find an implementation of an ASN1 algorithm. If 'pe' is not NULL also
77
 * search through engines and set *pe to a functional reference to the engine
78
 * implementing 'type' or NULL if no engine implements it.
79
 */
80
81
const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type)
82
4.55M
{
83
4.55M
    const EVP_PKEY_ASN1_METHOD *t;
84
85
4.63M
    for (;;) {
86
4.63M
        t = pkey_asn1_find(type);
87
4.63M
        if (!t || !(t->pkey_flags & ASN1_PKEY_ALIAS))
88
4.55M
            break;
89
81.4k
        type = t->pkey_base_id;
90
81.4k
    }
91
4.55M
    if (pe) {
92
4.53M
#ifndef OPENSSL_NO_ENGINE
93
4.53M
        ENGINE *e;
94
        /* type will contain the final unaliased type */
95
4.53M
        e = ENGINE_get_pkey_asn1_meth_engine(type);
96
4.53M
        if (e) {
97
0
            *pe = e;
98
0
            return ENGINE_get_pkey_asn1_meth(e, type);
99
0
        }
100
4.53M
#endif
101
4.53M
        *pe = NULL;
102
4.53M
    }
103
4.55M
    return t;
104
4.55M
}
105
106
const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe,
107
    const char *str, int len)
108
4.78M
{
109
4.78M
    int i;
110
4.78M
    const EVP_PKEY_ASN1_METHOD *ameth = NULL;
111
112
4.78M
    if (len == -1)
113
953k
        len = strlen(str);
114
4.78M
    if (pe) {
115
4.78M
#ifndef OPENSSL_NO_ENGINE
116
4.78M
        ENGINE *e;
117
4.78M
        ameth = ENGINE_pkey_asn1_find_str(&e, str, len);
118
4.78M
        if (ameth) {
119
            /*
120
             * Convert structural into functional reference
121
             */
122
0
            if (!ENGINE_init(e))
123
0
                ameth = NULL;
124
0
            ENGINE_free(e);
125
0
            *pe = e;
126
0
            return ameth;
127
0
        }
128
4.78M
#endif
129
4.78M
        *pe = NULL;
130
4.78M
    }
131
67.0M
    for (i = EVP_PKEY_asn1_get_count(); i-- > 0;) {
132
63.7M
        ameth = EVP_PKEY_asn1_get0(i);
133
63.7M
        if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
134
20.4M
            continue;
135
43.3M
        if ((int)strlen(ameth->pem_str) == len
136
2.50M
            && OPENSSL_strncasecmp(ameth->pem_str, str, len) == 0)
137
1.48M
            return ameth;
138
43.3M
    }
139
3.29M
    return NULL;
140
4.78M
}
141
142
int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth)
143
0
{
144
0
    EVP_PKEY_ASN1_METHOD tmp = {
145
0
        0,
146
0
    };
147
148
    /*
149
     * One of the following must be true:
150
     *
151
     * pem_str == NULL AND ASN1_PKEY_ALIAS is set
152
     * pem_str != NULL AND ASN1_PKEY_ALIAS is clear
153
     *
154
     * Anything else is an error and may lead to a corrupt ASN1 method table
155
     */
156
0
    if (!((ameth->pem_str == NULL
157
0
              && (ameth->pkey_flags & ASN1_PKEY_ALIAS) != 0)
158
0
            || (ameth->pem_str != NULL
159
0
                && (ameth->pkey_flags & ASN1_PKEY_ALIAS) == 0))) {
160
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
161
0
        return 0;
162
0
    }
163
164
0
    if (app_methods == NULL) {
165
0
        app_methods = sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp);
166
0
        if (app_methods == NULL)
167
0
            return 0;
168
0
    }
169
170
0
    tmp.pkey_id = ameth->pkey_id;
171
0
    if (sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp) >= 0) {
172
0
        ERR_raise(ERR_LIB_EVP,
173
0
            EVP_R_PKEY_APPLICATION_ASN1_METHOD_ALREADY_REGISTERED);
174
0
        return 0;
175
0
    }
176
177
0
    if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods, ameth))
178
0
        return 0;
179
0
    sk_EVP_PKEY_ASN1_METHOD_sort(app_methods);
180
0
    return 1;
181
0
}
182
183
int EVP_PKEY_asn1_add_alias(int to, int from)
184
0
{
185
0
    EVP_PKEY_ASN1_METHOD *ameth;
186
0
    ameth = EVP_PKEY_asn1_new(from, ASN1_PKEY_ALIAS, NULL, NULL);
187
0
    if (ameth == NULL)
188
0
        return 0;
189
0
    ameth->pkey_base_id = to;
190
0
    if (!EVP_PKEY_asn1_add0(ameth)) {
191
0
        EVP_PKEY_asn1_free(ameth);
192
0
        return 0;
193
0
    }
194
0
    return 1;
195
0
}
196
197
int EVP_PKEY_asn1_get0_info(int *ppkey_id, int *ppkey_base_id,
198
    int *ppkey_flags, const char **pinfo,
199
    const char **ppem_str,
200
    const EVP_PKEY_ASN1_METHOD *ameth)
201
1.50k
{
202
1.50k
    if (!ameth)
203
0
        return 0;
204
1.50k
    if (ppkey_id)
205
1.50k
        *ppkey_id = ameth->pkey_id;
206
1.50k
    if (ppkey_base_id)
207
1.50k
        *ppkey_base_id = ameth->pkey_base_id;
208
1.50k
    if (ppkey_flags)
209
1.50k
        *ppkey_flags = ameth->pkey_flags;
210
1.50k
    if (pinfo)
211
0
        *pinfo = ameth->info;
212
1.50k
    if (ppem_str)
213
1.50k
        *ppem_str = ameth->pem_str;
214
1.50k
    return 1;
215
1.50k
}
216
217
const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(const EVP_PKEY *pkey)
218
0
{
219
0
    return pkey->ameth;
220
0
}
221
222
EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
223
    const char *pem_str, const char *info)
224
0
{
225
0
    EVP_PKEY_ASN1_METHOD *ameth = OPENSSL_zalloc(sizeof(*ameth));
226
227
0
    if (ameth == NULL) {
228
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
229
0
        return NULL;
230
0
    }
231
232
0
    ameth->pkey_id = id;
233
0
    ameth->pkey_base_id = id;
234
0
    ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
235
236
0
    if (info) {
237
0
        ameth->info = OPENSSL_strdup(info);
238
0
        if (ameth->info == NULL)
239
0
            goto err;
240
0
    }
241
242
0
    if (pem_str) {
243
0
        ameth->pem_str = OPENSSL_strdup(pem_str);
244
0
        if (ameth->pem_str == NULL)
245
0
            goto err;
246
0
    }
247
248
0
    return ameth;
249
250
0
err:
251
0
    EVP_PKEY_asn1_free(ameth);
252
0
    ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
253
0
    return NULL;
254
0
}
255
256
void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
257
    const EVP_PKEY_ASN1_METHOD *src)
258
0
{
259
0
    int pkey_id = dst->pkey_id;
260
0
    int pkey_base_id = dst->pkey_base_id;
261
0
    unsigned long pkey_flags = dst->pkey_flags;
262
0
    char *pem_str = dst->pem_str;
263
0
    char *info = dst->info;
264
265
0
    *dst = *src;
266
267
    /* We only copy the function pointers so restore the other values */
268
0
    dst->pkey_id = pkey_id;
269
0
    dst->pkey_base_id = pkey_base_id;
270
0
    dst->pkey_flags = pkey_flags;
271
0
    dst->pem_str = pem_str;
272
0
    dst->info = info;
273
0
}
274
275
void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
276
0
{
277
0
    if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
278
0
        OPENSSL_free(ameth->pem_str);
279
0
        OPENSSL_free(ameth->info);
280
0
        OPENSSL_free(ameth);
281
0
    }
282
0
}
283
284
void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
285
    int (*pub_decode)(EVP_PKEY *pk,
286
        const X509_PUBKEY *pub),
287
    int (*pub_encode)(X509_PUBKEY *pub,
288
        const EVP_PKEY *pk),
289
    int (*pub_cmp)(const EVP_PKEY *a,
290
        const EVP_PKEY *b),
291
    int (*pub_print)(BIO *out,
292
        const EVP_PKEY *pkey,
293
        int indent, ASN1_PCTX *pctx),
294
    int (*pkey_size)(const EVP_PKEY *pk),
295
    int (*pkey_bits)(const EVP_PKEY *pk))
296
0
{
297
0
    ameth->pub_decode = pub_decode;
298
0
    ameth->pub_encode = pub_encode;
299
0
    ameth->pub_cmp = pub_cmp;
300
0
    ameth->pub_print = pub_print;
301
0
    ameth->pkey_size = pkey_size;
302
0
    ameth->pkey_bits = pkey_bits;
303
0
}
304
305
void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
306
    int (*priv_decode)(EVP_PKEY *pk,
307
        const PKCS8_PRIV_KEY_INFO
308
            *p8inf),
309
    int (*priv_encode)(PKCS8_PRIV_KEY_INFO *p8,
310
        const EVP_PKEY *pk),
311
    int (*priv_print)(BIO *out,
312
        const EVP_PKEY *pkey,
313
        int indent,
314
        ASN1_PCTX *pctx))
315
0
{
316
0
    ameth->priv_decode = priv_decode;
317
0
    ameth->priv_encode = priv_encode;
318
0
    ameth->priv_print = priv_print;
319
0
}
320
321
void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
322
    int (*param_decode)(EVP_PKEY *pkey,
323
        const unsigned char **pder,
324
        int derlen),
325
    int (*param_encode)(const EVP_PKEY *pkey,
326
        unsigned char **pder),
327
    int (*param_missing)(const EVP_PKEY *pk),
328
    int (*param_copy)(EVP_PKEY *to,
329
        const EVP_PKEY *from),
330
    int (*param_cmp)(const EVP_PKEY *a,
331
        const EVP_PKEY *b),
332
    int (*param_print)(BIO *out,
333
        const EVP_PKEY *pkey,
334
        int indent, ASN1_PCTX *pctx))
335
0
{
336
0
    ameth->param_decode = param_decode;
337
0
    ameth->param_encode = param_encode;
338
0
    ameth->param_missing = param_missing;
339
0
    ameth->param_copy = param_copy;
340
0
    ameth->param_cmp = param_cmp;
341
0
    ameth->param_print = param_print;
342
0
}
343
344
void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
345
    void (*pkey_free)(EVP_PKEY *pkey))
346
0
{
347
0
    ameth->pkey_free = pkey_free;
348
0
}
349
350
void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
351
    int (*pkey_ctrl)(EVP_PKEY *pkey, int op,
352
        long arg1, void *arg2))
353
0
{
354
0
    ameth->pkey_ctrl = pkey_ctrl;
355
0
}
356
357
void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
358
    int (*pkey_security_bits)(const EVP_PKEY
359
            *pk))
360
0
{
361
0
    ameth->pkey_security_bits = pkey_security_bits;
362
0
}
363
364
void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
365
    int (*item_verify)(EVP_MD_CTX *ctx,
366
        const ASN1_ITEM *it,
367
        const void *data,
368
        const X509_ALGOR *a,
369
        const ASN1_BIT_STRING *sig,
370
        EVP_PKEY *pkey),
371
    int (*item_sign)(EVP_MD_CTX *ctx,
372
        const ASN1_ITEM *it,
373
        const void *data,
374
        X509_ALGOR *alg1,
375
        X509_ALGOR *alg2,
376
        ASN1_BIT_STRING *sig))
377
0
{
378
0
    ameth->item_sign = item_sign;
379
0
    ameth->item_verify = item_verify;
380
0
}
381
382
void EVP_PKEY_asn1_set_siginf(EVP_PKEY_ASN1_METHOD *ameth,
383
    int (*siginf_set)(X509_SIG_INFO *siginf,
384
        const X509_ALGOR *alg,
385
        const ASN1_STRING *sig))
386
0
{
387
0
    ameth->siginf_set = siginf_set;
388
0
}
389
390
void EVP_PKEY_asn1_set_check(EVP_PKEY_ASN1_METHOD *ameth,
391
    int (*pkey_check)(const EVP_PKEY *pk))
392
0
{
393
0
    ameth->pkey_check = pkey_check;
394
0
}
395
396
void EVP_PKEY_asn1_set_public_check(EVP_PKEY_ASN1_METHOD *ameth,
397
    int (*pkey_pub_check)(const EVP_PKEY *pk))
398
0
{
399
0
    ameth->pkey_public_check = pkey_pub_check;
400
0
}
401
402
void EVP_PKEY_asn1_set_param_check(EVP_PKEY_ASN1_METHOD *ameth,
403
    int (*pkey_param_check)(const EVP_PKEY *pk))
404
0
{
405
0
    ameth->pkey_param_check = pkey_param_check;
406
0
}
407
408
void EVP_PKEY_asn1_set_set_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
409
    int (*set_priv_key)(EVP_PKEY *pk,
410
        const unsigned char
411
            *priv,
412
        size_t len))
413
0
{
414
0
    ameth->set_priv_key = set_priv_key;
415
0
}
416
417
void EVP_PKEY_asn1_set_set_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
418
    int (*set_pub_key)(EVP_PKEY *pk,
419
        const unsigned char *pub,
420
        size_t len))
421
0
{
422
0
    ameth->set_pub_key = set_pub_key;
423
0
}
424
425
void EVP_PKEY_asn1_set_get_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
426
    int (*get_priv_key)(const EVP_PKEY *pk,
427
        unsigned char *priv,
428
        size_t *len))
429
0
{
430
0
    ameth->get_priv_key = get_priv_key;
431
0
}
432
433
void EVP_PKEY_asn1_set_get_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
434
    int (*get_pub_key)(const EVP_PKEY *pk,
435
        unsigned char *pub,
436
        size_t *len))
437
0
{
438
0
    ameth->get_pub_key = get_pub_key;
439
0
}