| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/engine/tb_asnmth.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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 "e_os.h" | - | ||||||||||||
| 11 | #include "eng_int.h" | - | ||||||||||||
| 12 | #include <openssl/evp.h> | - | ||||||||||||
| 13 | #include "internal/asn1_int.h" | - | ||||||||||||
| 14 | - | |||||||||||||
| 15 | /* | - | ||||||||||||
| 16 | * If this symbol is defined then ENGINE_get_pkey_asn1_meth_engine(), the | - | ||||||||||||
| 17 | * function that is used by EVP to hook in pkey_asn1_meth code and cache | - | ||||||||||||
| 18 | * defaults (etc), will display brief debugging summaries to stderr with the | - | ||||||||||||
| 19 | * 'nid'. | - | ||||||||||||
| 20 | */ | - | ||||||||||||
| 21 | /* #define ENGINE_PKEY_ASN1_METH_DEBUG */ | - | ||||||||||||
| 22 | - | |||||||||||||
| 23 | static ENGINE_TABLE *pkey_asn1_meth_table = NULL; | - | ||||||||||||
| 24 | - | |||||||||||||
| 25 | void ENGINE_unregister_pkey_asn1_meths(ENGINE *e) | - | ||||||||||||
| 26 | { | - | ||||||||||||
| 27 | engine_table_unregister(&pkey_asn1_meth_table, e); | - | ||||||||||||
| 28 | } never executed: end of block | 0 | ||||||||||||
| 29 | - | |||||||||||||
| 30 | static void engine_unregister_all_pkey_asn1_meths(void) | - | ||||||||||||
| 31 | { | - | ||||||||||||
| 32 | engine_table_cleanup(&pkey_asn1_meth_table); | - | ||||||||||||
| 33 | } never executed: end of block | 0 | ||||||||||||
| 34 | - | |||||||||||||
| 35 | int ENGINE_register_pkey_asn1_meths(ENGINE *e) | - | ||||||||||||
| 36 | { | - | ||||||||||||
| 37 | if (e->pkey_asn1_meths) {
| 0-3748 | ||||||||||||
| 38 | const int *nids; | - | ||||||||||||
| 39 | int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0); | - | ||||||||||||
| 40 | if (num_nids > 0)
| 0 | ||||||||||||
| 41 | return engine_table_register(&pkey_asn1_meth_table, never executed: return engine_table_register(&pkey_asn1_meth_table, engine_unregister_all_pkey_asn1_meths, e, nids, num_nids, 0); | 0 | ||||||||||||
| 42 | engine_unregister_all_pkey_asn1_meths, never executed: return engine_table_register(&pkey_asn1_meth_table, engine_unregister_all_pkey_asn1_meths, e, nids, num_nids, 0); | 0 | ||||||||||||
| 43 | e, nids, num_nids, 0); never executed: return engine_table_register(&pkey_asn1_meth_table, engine_unregister_all_pkey_asn1_meths, e, nids, num_nids, 0); | 0 | ||||||||||||
| 44 | } never executed: end of block | 0 | ||||||||||||
| 45 | return 1; executed 3748 times by 1 test: return 1;Executed by:
| 3748 | ||||||||||||
| 46 | } | - | ||||||||||||
| 47 | - | |||||||||||||
| 48 | void ENGINE_register_all_pkey_asn1_meths(void) | - | ||||||||||||
| 49 | { | - | ||||||||||||
| 50 | ENGINE *e; | - | ||||||||||||
| 51 | - | |||||||||||||
| 52 | for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
| 0 | ||||||||||||
| 53 | ENGINE_register_pkey_asn1_meths(e); never executed: ENGINE_register_pkey_asn1_meths(e); | 0 | ||||||||||||
| 54 | } never executed: end of block | 0 | ||||||||||||
| 55 | - | |||||||||||||
| 56 | int ENGINE_set_default_pkey_asn1_meths(ENGINE *e) | - | ||||||||||||
| 57 | { | - | ||||||||||||
| 58 | if (e->pkey_asn1_meths) {
| 0-367 | ||||||||||||
| 59 | const int *nids; | - | ||||||||||||
| 60 | int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0); | - | ||||||||||||
| 61 | if (num_nids > 0)
| 0 | ||||||||||||
| 62 | return engine_table_register(&pkey_asn1_meth_table, never executed: return engine_table_register(&pkey_asn1_meth_table, engine_unregister_all_pkey_asn1_meths, e, nids, num_nids, 1); | 0 | ||||||||||||
| 63 | engine_unregister_all_pkey_asn1_meths, never executed: return engine_table_register(&pkey_asn1_meth_table, engine_unregister_all_pkey_asn1_meths, e, nids, num_nids, 1); | 0 | ||||||||||||
| 64 | e, nids, num_nids, 1); never executed: return engine_table_register(&pkey_asn1_meth_table, engine_unregister_all_pkey_asn1_meths, e, nids, num_nids, 1); | 0 | ||||||||||||
| 65 | } never executed: end of block | 0 | ||||||||||||
| 66 | return 1; executed 367 times by 1 test: return 1;Executed by:
| 367 | ||||||||||||
| 67 | } | - | ||||||||||||
| 68 | - | |||||||||||||
| 69 | /* | - | ||||||||||||
| 70 | * Exposed API function to get a functional reference from the implementation | - | ||||||||||||
| 71 | * table (ie. try to get a functional reference from the tabled structural | - | ||||||||||||
| 72 | * references) for a given pkey_asn1_meth 'nid' | - | ||||||||||||
| 73 | */ | - | ||||||||||||
| 74 | ENGINE *ENGINE_get_pkey_asn1_meth_engine(int nid) | - | ||||||||||||
| 75 | { | - | ||||||||||||
| 76 | return engine_table_select(&pkey_asn1_meth_table, nid); executed 100211 times by 1 test: return engine_table_select(&pkey_asn1_meth_table, nid);Executed by:
| 100211 | ||||||||||||
| 77 | } | - | ||||||||||||
| 78 | - | |||||||||||||
| 79 | /* | - | ||||||||||||
| 80 | * Obtains a pkey_asn1_meth implementation from an ENGINE functional | - | ||||||||||||
| 81 | * reference | - | ||||||||||||
| 82 | */ | - | ||||||||||||
| 83 | const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth(ENGINE *e, int nid) | - | ||||||||||||
| 84 | { | - | ||||||||||||
| 85 | EVP_PKEY_ASN1_METHOD *ret; | - | ||||||||||||
| 86 | ENGINE_PKEY_ASN1_METHS_PTR fn = ENGINE_get_pkey_asn1_meths(e); | - | ||||||||||||
| 87 | if (!fn || !fn(e, &ret, NULL, nid)) {
| 0 | ||||||||||||
| 88 | ENGINEerr(ENGINE_F_ENGINE_GET_PKEY_ASN1_METH, | - | ||||||||||||
| 89 | ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD); | - | ||||||||||||
| 90 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 91 | } | - | ||||||||||||
| 92 | return ret; never executed: return ret; | 0 | ||||||||||||
| 93 | } | - | ||||||||||||
| 94 | - | |||||||||||||
| 95 | /* Gets the pkey_asn1_meth callback from an ENGINE structure */ | - | ||||||||||||
| 96 | ENGINE_PKEY_ASN1_METHS_PTR ENGINE_get_pkey_asn1_meths(const ENGINE *e) | - | ||||||||||||
| 97 | { | - | ||||||||||||
| 98 | return e->pkey_asn1_meths; never executed: return e->pkey_asn1_meths; | 0 | ||||||||||||
| 99 | } | - | ||||||||||||
| 100 | - | |||||||||||||
| 101 | /* Sets the pkey_asn1_meth callback in an ENGINE structure */ | - | ||||||||||||
| 102 | int ENGINE_set_pkey_asn1_meths(ENGINE *e, ENGINE_PKEY_ASN1_METHS_PTR f) | - | ||||||||||||
| 103 | { | - | ||||||||||||
| 104 | e->pkey_asn1_meths = f; | - | ||||||||||||
| 105 | return 1; never executed: return 1; | 0 | ||||||||||||
| 106 | } | - | ||||||||||||
| 107 | - | |||||||||||||
| 108 | /* | - | ||||||||||||
| 109 | * Internal function to free up EVP_PKEY_ASN1_METHOD structures before an | - | ||||||||||||
| 110 | * ENGINE is destroyed | - | ||||||||||||
| 111 | */ | - | ||||||||||||
| 112 | - | |||||||||||||
| 113 | void engine_pkey_asn1_meths_free(ENGINE *e) | - | ||||||||||||
| 114 | { | - | ||||||||||||
| 115 | int i; | - | ||||||||||||
| 116 | EVP_PKEY_ASN1_METHOD *pkm; | - | ||||||||||||
| 117 | if (e->pkey_asn1_meths) {
| 0-2844 | ||||||||||||
| 118 | const int *pknids; | - | ||||||||||||
| 119 | int npknids; | - | ||||||||||||
| 120 | npknids = e->pkey_asn1_meths(e, NULL, &pknids, 0); | - | ||||||||||||
| 121 | for (i = 0; i < npknids; i++) {
| 0 | ||||||||||||
| 122 | if (e->pkey_asn1_meths(e, &pkm, NULL, pknids[i])) {
| 0 | ||||||||||||
| 123 | EVP_PKEY_asn1_free(pkm); | - | ||||||||||||
| 124 | } never executed: end of block | 0 | ||||||||||||
| 125 | } never executed: end of block | 0 | ||||||||||||
| 126 | } never executed: end of block | 0 | ||||||||||||
| 127 | } executed 2844 times by 1 test: end of blockExecuted by:
| 2844 | ||||||||||||
| 128 | - | |||||||||||||
| 129 | /* | - | ||||||||||||
| 130 | * Find a method based on a string. This does a linear search through all | - | ||||||||||||
| 131 | * implemented algorithms. This is OK in practice because only a small number | - | ||||||||||||
| 132 | * of algorithms are likely to be implemented in an engine and it is not used | - | ||||||||||||
| 133 | * for speed critical operations. | - | ||||||||||||
| 134 | */ | - | ||||||||||||
| 135 | - | |||||||||||||
| 136 | const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth_str(ENGINE *e, | - | ||||||||||||
| 137 | const char *str, | - | ||||||||||||
| 138 | int len) | - | ||||||||||||
| 139 | { | - | ||||||||||||
| 140 | int i, nidcount; | - | ||||||||||||
| 141 | const int *nids; | - | ||||||||||||
| 142 | EVP_PKEY_ASN1_METHOD *ameth; | - | ||||||||||||
| 143 | if (!e->pkey_asn1_meths)
| 0 | ||||||||||||
| 144 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 145 | if (len == -1)
| 0 | ||||||||||||
| 146 | len = strlen(str); never executed: len = strlen(str); | 0 | ||||||||||||
| 147 | nidcount = e->pkey_asn1_meths(e, NULL, &nids, 0); | - | ||||||||||||
| 148 | for (i = 0; i < nidcount; i++) {
| 0 | ||||||||||||
| 149 | e->pkey_asn1_meths(e, &ameth, NULL, nids[i]); | - | ||||||||||||
| 150 | if (((int)strlen(ameth->pem_str) == len)
| 0 | ||||||||||||
| 151 | && strncasecmp(ameth->pem_str, str, len) == 0)
| 0 | ||||||||||||
| 152 | return ameth; never executed: return ameth; | 0 | ||||||||||||
| 153 | } never executed: end of block | 0 | ||||||||||||
| 154 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 155 | } | - | ||||||||||||
| 156 | - | |||||||||||||
| 157 | typedef struct { | - | ||||||||||||
| 158 | ENGINE *e; | - | ||||||||||||
| 159 | const EVP_PKEY_ASN1_METHOD *ameth; | - | ||||||||||||
| 160 | const char *str; | - | ||||||||||||
| 161 | int len; | - | ||||||||||||
| 162 | } ENGINE_FIND_STR; | - | ||||||||||||
| 163 | - | |||||||||||||
| 164 | static void look_str_cb(int nid, STACK_OF(ENGINE) *sk, ENGINE *def, void *arg) | - | ||||||||||||
| 165 | { | - | ||||||||||||
| 166 | ENGINE_FIND_STR *lk = arg; | - | ||||||||||||
| 167 | int i; | - | ||||||||||||
| 168 | if (lk->ameth)
| 0 | ||||||||||||
| 169 | return; never executed: return; | 0 | ||||||||||||
| 170 | for (i = 0; i < sk_ENGINE_num(sk); i++) {
| 0 | ||||||||||||
| 171 | ENGINE *e = sk_ENGINE_value(sk, i); | - | ||||||||||||
| 172 | EVP_PKEY_ASN1_METHOD *ameth; | - | ||||||||||||
| 173 | e->pkey_asn1_meths(e, &ameth, NULL, nid); | - | ||||||||||||
| 174 | if (ameth != NULL
| 0 | ||||||||||||
| 175 | && ((int)strlen(ameth->pem_str) == lk->len)
| 0 | ||||||||||||
| 176 | && strncasecmp(ameth->pem_str, lk->str, lk->len) == 0) {
| 0 | ||||||||||||
| 177 | lk->e = e; | - | ||||||||||||
| 178 | lk->ameth = ameth; | - | ||||||||||||
| 179 | return; never executed: return; | 0 | ||||||||||||
| 180 | } | - | ||||||||||||
| 181 | } never executed: end of block | 0 | ||||||||||||
| 182 | } never executed: end of block | 0 | ||||||||||||
| 183 | - | |||||||||||||
| 184 | const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe, | - | ||||||||||||
| 185 | const char *str, | - | ||||||||||||
| 186 | int len) | - | ||||||||||||
| 187 | { | - | ||||||||||||
| 188 | ENGINE_FIND_STR fstr; | - | ||||||||||||
| 189 | fstr.e = NULL; | - | ||||||||||||
| 190 | fstr.ameth = NULL; | - | ||||||||||||
| 191 | fstr.str = str; | - | ||||||||||||
| 192 | fstr.len = len; | - | ||||||||||||
| 193 | - | |||||||||||||
| 194 | if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
| 0-9796 | ||||||||||||
| 195 | ENGINEerr(ENGINE_F_ENGINE_PKEY_ASN1_FIND_STR, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
| 196 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 197 | } | - | ||||||||||||
| 198 | - | |||||||||||||
| 199 | CRYPTO_THREAD_write_lock(global_engine_lock); | - | ||||||||||||
| 200 | engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr); | - | ||||||||||||
| 201 | /* If found obtain a structural reference to engine */ | - | ||||||||||||
| 202 | if (fstr.e) {
| 0-9796 | ||||||||||||
| 203 | fstr.e->struct_ref++; | - | ||||||||||||
| 204 | engine_ref_debug(fstr.e, 0, 1); | - | ||||||||||||
| 205 | } never executed: end of block | 0 | ||||||||||||
| 206 | *pe = fstr.e; | - | ||||||||||||
| 207 | CRYPTO_THREAD_unlock(global_engine_lock); | - | ||||||||||||
| 208 | return fstr.ameth; executed 9796 times by 1 test: return fstr.ameth;Executed by:
| 9796 | ||||||||||||
| 209 | } | - | ||||||||||||
| Source code | Switch to Preprocessed file |