Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/asn1/ameth_lib.c
Line
Count
Source
1
/*
2
 * Copyright 2006-2025 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
#include "internal/cryptlib.h"
11
#include <stdio.h>
12
#include <openssl/asn1t.h>
13
#include <openssl/x509.h>
14
#include "crypto/asn1.h"
15
#include "crypto/evp.h"
16
17
#include "standard_methods.h"
18
19
typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
20
21
DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
22
    const EVP_PKEY_ASN1_METHOD *, ameth);
23
24
static int ameth_cmp(const EVP_PKEY_ASN1_METHOD *const *a,
25
    const EVP_PKEY_ASN1_METHOD *const *b)
26
16.9M
{
27
16.9M
    return ((*a)->pkey_id - (*b)->pkey_id);
28
16.9M
}
29
30
IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
31
    const EVP_PKEY_ASN1_METHOD *, ameth);
32
33
int evp_pkey_asn1_get_count(void)
34
802k
{
35
802k
    int num = OSSL_NELEM(standard_methods);
36
802k
    return num;
37
802k
}
38
39
const EVP_PKEY_ASN1_METHOD *evp_pkey_asn1_get0(int idx)
40
9.86M
{
41
9.86M
    int num = OSSL_NELEM(standard_methods);
42
43
9.86M
    if (idx < 0 || idx >= num)
44
0
        return NULL;
45
46
9.86M
    return standard_methods[idx];
47
9.86M
}
48
49
static const EVP_PKEY_ASN1_METHOD *pkey_asn1_find(int type)
50
851k
{
51
851k
    EVP_PKEY_ASN1_METHOD tmp;
52
851k
    const EVP_PKEY_ASN1_METHOD *t = &tmp, **ret;
53
54
851k
    tmp.pkey_id = type;
55
851k
    ret = OBJ_bsearch_ameth(&t, standard_methods, OSSL_NELEM(standard_methods));
56
851k
    if (ret == NULL || *ret == NULL)
57
107k
        return NULL;
58
743k
    return *ret;
59
851k
}
60
61
/*
62
 * Return ASN1 method for desired `type`, returns NULL if no method is found for
63
 * `type`. If pe is not NULL, the function will set *pe to NULL to indicate no
64
 * engine is used.
65
 */
66
const EVP_PKEY_ASN1_METHOD *evp_pkey_asn1_find(int type)
67
849k
{
68
849k
    const EVP_PKEY_ASN1_METHOD *t;
69
70
851k
    for (;;) {
71
851k
        t = pkey_asn1_find(type);
72
851k
        if (!t || !(t->pkey_flags & ASN1_PKEY_ALIAS))
73
849k
            break;
74
1.86k
        type = t->pkey_base_id;
75
1.86k
    }
76
849k
    return t;
77
849k
}
78
79
const EVP_PKEY_ASN1_METHOD *evp_pkey_asn1_find_str(const char *str, int len)
80
802k
{
81
802k
    int i;
82
802k
    const EVP_PKEY_ASN1_METHOD *ameth = NULL;
83
84
802k
    if (len == -1)
85
0
        len = (int)strlen(str);
86
10.3M
    for (i = evp_pkey_asn1_get_count(); i-- > 0;) {
87
9.86M
        ameth = evp_pkey_asn1_get0(i);
88
9.86M
        if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
89
2.90M
            continue;
90
6.96M
        if ((int)strlen(ameth->pem_str) == len
91
462k
            && OPENSSL_strncasecmp(ameth->pem_str, str, len) == 0)
92
327k
            return ameth;
93
6.96M
    }
94
474k
    return NULL;
95
802k
}
96
97
int evp_pkey_asn1_get0_info(int *ppkey_id, int *ppkey_base_id,
98
    int *ppkey_flags, const char **pinfo,
99
    const char **ppem_str,
100
    const EVP_PKEY_ASN1_METHOD *ameth)
101
330
{
102
330
    if (!ameth)
103
0
        return 0;
104
330
    if (ppkey_id)
105
330
        *ppkey_id = ameth->pkey_id;
106
330
    if (ppkey_base_id)
107
330
        *ppkey_base_id = ameth->pkey_base_id;
108
330
    if (ppkey_flags)
109
330
        *ppkey_flags = ameth->pkey_flags;
110
330
    if (pinfo)
111
0
        *pinfo = ameth->info;
112
330
    if (ppem_str)
113
330
        *ppem_str = ameth->pem_str;
114
330
    return 1;
115
330
}
116
117
const EVP_PKEY_ASN1_METHOD *evp_pkey_get0_asn1(const EVP_PKEY *pkey)
118
0
{
119
0
    return pkey->ameth;
120
0
}