1 /*
2  * Copyright 2006-2020 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 <openssl/objects.h>
11 #include "obj_xref.h"
12 #include "internal/nelem.h"
13 #include "internal/thread_once.h"
14 #include <openssl/err.h>
15 
16 static STACK_OF(nid_triple) *sig_app, *sigx_app;
17 static CRYPTO_RWLOCK *sig_lock;
18 
sig_cmp(const nid_triple * a,const nid_triple * b)19 static int sig_cmp(const nid_triple *a, const nid_triple *b)
20 {
21     return a->sign_id - b->sign_id;
22 }
23 
24 DECLARE_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, sig);
25 IMPLEMENT_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, sig);
26 
sig_sk_cmp(const nid_triple * const * a,const nid_triple * const * b)27 static int sig_sk_cmp(const nid_triple *const *a, const nid_triple *const *b)
28 {
29     return (*a)->sign_id - (*b)->sign_id;
30 }
31 
32 DECLARE_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, sigx);
33 
sigx_cmp(const nid_triple * const * a,const nid_triple * const * b)34 static int sigx_cmp(const nid_triple *const *a, const nid_triple *const *b)
35 {
36     int ret;
37 
38     ret = (*a)->hash_id - (*b)->hash_id;
39     if (ret != 0)
40         return ret;
41     return (*a)->pkey_id - (*b)->pkey_id;
42 }
43 
44 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, sigx);
45 
46 static CRYPTO_ONCE sig_init = CRYPTO_ONCE_STATIC_INIT;
47 
DEFINE_RUN_ONCE_STATIC(o_sig_init)48 DEFINE_RUN_ONCE_STATIC(o_sig_init)
49 {
50     sig_lock = CRYPTO_THREAD_lock_new();
51     return sig_lock != NULL;
52 }
53 
obj_sig_init(void)54 static ossl_inline int obj_sig_init(void)
55 {
56     return RUN_ONCE(&sig_init, o_sig_init);
57 }
58 
ossl_obj_find_sigid_algs(int signid,int * pdig_nid,int * ppkey_nid,int lock)59 static int ossl_obj_find_sigid_algs(int signid, int *pdig_nid, int *ppkey_nid,
60                                     int lock)
61 {
62     nid_triple tmp;
63     const nid_triple *rv;
64     int idx;
65 
66     if (signid == NID_undef)
67         return 0;
68 
69     tmp.sign_id = signid;
70     rv = OBJ_bsearch_sig(&tmp, sigoid_srt, OSSL_NELEM(sigoid_srt));
71     if (rv == NULL) {
72         if (!obj_sig_init())
73             return 0;
74         if (lock && !CRYPTO_THREAD_read_lock(sig_lock)) {
75             ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK);
76             return 0;
77         }
78         if (sig_app != NULL) {
79             idx = sk_nid_triple_find(sig_app, &tmp);
80             if (idx >= 0)
81                 rv = sk_nid_triple_value(sig_app, idx);
82         }
83         if (lock)
84             CRYPTO_THREAD_unlock(sig_lock);
85         if (rv == NULL)
86             return 0;
87     }
88 
89     if (pdig_nid != NULL)
90         *pdig_nid = rv->hash_id;
91     if (ppkey_nid != NULL)
92         *ppkey_nid = rv->pkey_id;
93     return 1;
94 }
95 
OBJ_find_sigid_algs(int signid,int * pdig_nid,int * ppkey_nid)96 int OBJ_find_sigid_algs(int signid, int *pdig_nid, int *ppkey_nid)
97 {
98     return ossl_obj_find_sigid_algs(signid, pdig_nid, ppkey_nid, 1);
99 }
100 
OBJ_find_sigid_by_algs(int * psignid,int dig_nid,int pkey_nid)101 int OBJ_find_sigid_by_algs(int *psignid, int dig_nid, int pkey_nid)
102 {
103     nid_triple tmp;
104     const nid_triple *t = &tmp;
105     const nid_triple **rv;
106     int idx;
107 
108     if (dig_nid == NID_undef || pkey_nid == NID_undef)
109         return 0;
110 
111     tmp.hash_id = dig_nid;
112     tmp.pkey_id = pkey_nid;
113 
114     rv = OBJ_bsearch_sigx(&t, sigoid_srt_xref, OSSL_NELEM(sigoid_srt_xref));
115     if (rv == NULL) {
116         if (!obj_sig_init())
117             return 0;
118         if (!CRYPTO_THREAD_read_lock(sig_lock)) {
119             ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK);
120             return 0;
121         }
122         if (sigx_app != NULL) {
123             idx = sk_nid_triple_find(sigx_app, &tmp);
124             if (idx >= 0) {
125                 t = sk_nid_triple_value(sigx_app, idx);
126                 rv = &t;
127             }
128         }
129         CRYPTO_THREAD_unlock(sig_lock);
130         if (rv == NULL)
131             return 0;
132     }
133 
134     if (psignid != NULL)
135         *psignid = (*rv)->sign_id;
136     return 1;
137 }
138 
OBJ_add_sigid(int signid,int dig_id,int pkey_id)139 int OBJ_add_sigid(int signid, int dig_id, int pkey_id)
140 {
141     nid_triple *ntr;
142     int dnid = NID_undef, pnid = NID_undef, ret = 0;
143 
144     if (signid == NID_undef || pkey_id == NID_undef)
145         return 0;
146 
147     if (!obj_sig_init())
148         return 0;
149 
150     if ((ntr = OPENSSL_malloc(sizeof(*ntr))) == NULL) {
151         ERR_raise(ERR_LIB_OBJ, ERR_R_MALLOC_FAILURE);
152         return 0;
153     }
154     ntr->sign_id = signid;
155     ntr->hash_id = dig_id;
156     ntr->pkey_id = pkey_id;
157 
158     if (!CRYPTO_THREAD_write_lock(sig_lock)) {
159         ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
160         OPENSSL_free(ntr);
161         return 0;
162     }
163 
164     /* Check that the entry doesn't exist or exists as desired */
165     if (ossl_obj_find_sigid_algs(signid, &dnid, &pnid, 0)) {
166         ret = dnid == dig_id && pnid == pkey_id;
167         goto err;
168     }
169 
170     if (sig_app == NULL) {
171         sig_app = sk_nid_triple_new(sig_sk_cmp);
172         if (sig_app == NULL)
173             goto err;
174     }
175     if (sigx_app == NULL) {
176         sigx_app = sk_nid_triple_new(sigx_cmp);
177         if (sigx_app == NULL)
178             goto err;
179     }
180 
181     /*
182      * Better might be to find where to insert the element and insert it there.
183      * This would avoid the sorting steps below.
184      */
185     if (!sk_nid_triple_push(sig_app, ntr))
186         goto err;
187     if (!sk_nid_triple_push(sigx_app, ntr)) {
188         ntr = NULL;             /* This is referenced by sig_app still */
189         goto err;
190     }
191 
192     sk_nid_triple_sort(sig_app);
193     sk_nid_triple_sort(sigx_app);
194 
195     ntr = NULL;
196     ret = 1;
197  err:
198     OPENSSL_free(ntr);
199     CRYPTO_THREAD_unlock(sig_lock);
200     return ret;
201 }
202 
sid_free(nid_triple * tt)203 static void sid_free(nid_triple *tt)
204 {
205     OPENSSL_free(tt);
206 }
207 
OBJ_sigid_free(void)208 void OBJ_sigid_free(void)
209 {
210     sk_nid_triple_pop_free(sig_app, sid_free);
211     sk_nid_triple_free(sigx_app);
212     CRYPTO_THREAD_lock_free(sig_lock);
213     sig_app = NULL;
214     sigx_app = NULL;
215     sig_lock = NULL;
216 }
217