2 * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/objects.h>
12 #include "internal/nelem.h"
13 #include "internal/thread_once.h"
14 #include <openssl/err.h>
16 static STACK_OF(nid_triple) *sig_app, *sigx_app;
17 static CRYPTO_RWLOCK *sig_lock;
19 static int sig_cmp(const nid_triple *a, const nid_triple *b)
21 return a->sign_id - b->sign_id;
24 DECLARE_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, sig);
25 IMPLEMENT_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, sig);
27 static int sig_sk_cmp(const nid_triple *const *a, const nid_triple *const *b)
29 return (*a)->sign_id - (*b)->sign_id;
32 DECLARE_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, sigx);
34 static int sigx_cmp(const nid_triple *const *a, const nid_triple *const *b)
38 ret = (*a)->hash_id - (*b)->hash_id;
39 /* The "b" side of the comparison carries the algorithms already
40 * registered. A NID_undef for 'hash_id' there means that the
41 * signature algorithm doesn't need a digest to operate OK. In
42 * such case, any hash_id/digest algorithm on the test side (a),
43 * incl. NID_undef, is acceptable. signature algorithm NID
44 * (pkey_id) must match in any case.
46 if ((ret != 0) && ((*b)->hash_id != NID_undef))
48 return (*a)->pkey_id - (*b)->pkey_id;
51 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, sigx);
53 static CRYPTO_ONCE sig_init = CRYPTO_ONCE_STATIC_INIT;
55 DEFINE_RUN_ONCE_STATIC(o_sig_init)
57 sig_lock = CRYPTO_THREAD_lock_new();
58 return sig_lock != NULL;
61 static ossl_inline int obj_sig_init(void)
63 return RUN_ONCE(&sig_init, o_sig_init);
66 static int ossl_obj_find_sigid_algs(int signid, int *pdig_nid, int *ppkey_nid,
73 if (signid == NID_undef)
77 rv = OBJ_bsearch_sig(&tmp, sigoid_srt, OSSL_NELEM(sigoid_srt));
81 if (lock && !CRYPTO_THREAD_read_lock(sig_lock)) {
82 ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK);
85 if (sig_app != NULL) {
86 idx = sk_nid_triple_find(sig_app, &tmp);
88 rv = sk_nid_triple_value(sig_app, idx);
91 CRYPTO_THREAD_unlock(sig_lock);
97 *pdig_nid = rv->hash_id;
98 if (ppkey_nid != NULL)
99 *ppkey_nid = rv->pkey_id;
103 int OBJ_find_sigid_algs(int signid, int *pdig_nid, int *ppkey_nid)
105 return ossl_obj_find_sigid_algs(signid, pdig_nid, ppkey_nid, 1);
108 int OBJ_find_sigid_by_algs(int *psignid, int dig_nid, int pkey_nid)
111 const nid_triple *t = &tmp;
112 const nid_triple **rv;
115 if (dig_nid == NID_undef || pkey_nid == NID_undef)
118 tmp.hash_id = dig_nid;
119 tmp.pkey_id = pkey_nid;
121 rv = OBJ_bsearch_sigx(&t, sigoid_srt_xref, OSSL_NELEM(sigoid_srt_xref));
125 if (!CRYPTO_THREAD_read_lock(sig_lock)) {
126 ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK);
129 if (sigx_app != NULL) {
130 idx = sk_nid_triple_find(sigx_app, &tmp);
132 t = sk_nid_triple_value(sigx_app, idx);
136 CRYPTO_THREAD_unlock(sig_lock);
142 *psignid = (*rv)->sign_id;
146 int OBJ_add_sigid(int signid, int dig_id, int pkey_id)
149 int dnid = NID_undef, pnid = NID_undef, ret = 0;
151 if (signid == NID_undef || pkey_id == NID_undef)
157 if ((ntr = OPENSSL_malloc(sizeof(*ntr))) == NULL) {
158 ERR_raise(ERR_LIB_OBJ, ERR_R_MALLOC_FAILURE);
161 ntr->sign_id = signid;
162 ntr->hash_id = dig_id;
163 ntr->pkey_id = pkey_id;
165 if (!CRYPTO_THREAD_write_lock(sig_lock)) {
166 ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
171 /* Check that the entry doesn't exist or exists as desired */
172 if (ossl_obj_find_sigid_algs(signid, &dnid, &pnid, 0)) {
173 ret = dnid == dig_id && pnid == pkey_id;
177 if (sig_app == NULL) {
178 sig_app = sk_nid_triple_new(sig_sk_cmp);
182 if (sigx_app == NULL) {
183 sigx_app = sk_nid_triple_new(sigx_cmp);
184 if (sigx_app == NULL)
189 * Better might be to find where to insert the element and insert it there.
190 * This would avoid the sorting steps below.
192 if (!sk_nid_triple_push(sig_app, ntr))
194 if (!sk_nid_triple_push(sigx_app, ntr)) {
195 ntr = NULL; /* This is referenced by sig_app still */
199 sk_nid_triple_sort(sig_app);
200 sk_nid_triple_sort(sigx_app);
206 CRYPTO_THREAD_unlock(sig_lock);
210 static void sid_free(nid_triple *tt)
215 void OBJ_sigid_free(void)
217 sk_nid_triple_pop_free(sig_app, sid_free);
218 sk_nid_triple_free(sigx_app);
219 CRYPTO_THREAD_lock_free(sig_lock);