/src/openssl30/crypto/objects/obj_xref.c
Line  | Count  | Source (jump to first uncovered line)  | 
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 <openssl/err.h>  | 
14  |  |  | 
15  |  | static STACK_OF(nid_triple) *sig_app, *sigx_app;  | 
16  |  |  | 
17  |  | static int sig_cmp(const nid_triple *a, const nid_triple *b)  | 
18  | 1.52M  | { | 
19  | 1.52M  |     return a->sign_id - b->sign_id;  | 
20  | 1.52M  | }  | 
21  |  |  | 
22  |  | DECLARE_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, sig);  | 
23  |  | IMPLEMENT_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, sig);  | 
24  |  |  | 
25  |  | static int sig_sk_cmp(const nid_triple *const *a, const nid_triple *const *b)  | 
26  | 0  | { | 
27  | 0  |     return (*a)->sign_id - (*b)->sign_id;  | 
28  | 0  | }  | 
29  |  |  | 
30  |  | DECLARE_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, sigx);  | 
31  |  |  | 
32  |  | static int sigx_cmp(const nid_triple *const *a, const nid_triple *const *b)  | 
33  | 0  | { | 
34  | 0  |     int ret;  | 
35  | 0  |     ret = (*a)->hash_id - (*b)->hash_id;  | 
36  | 0  |     if (ret)  | 
37  | 0  |         return ret;  | 
38  | 0  |     return (*a)->pkey_id - (*b)->pkey_id;  | 
39  | 0  | }  | 
40  |  |  | 
41  |  | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, sigx);  | 
42  |  |  | 
43  |  | int OBJ_find_sigid_algs(int signid, int *pdig_nid, int *ppkey_nid)  | 
44  | 48.0k  | { | 
45  | 48.0k  |     nid_triple tmp;  | 
46  | 48.0k  |     const nid_triple *rv = NULL;  | 
47  | 48.0k  |     tmp.sign_id = signid;  | 
48  |  |  | 
49  | 48.0k  |     if (sig_app != NULL) { | 
50  | 0  |         int idx = sk_nid_triple_find(sig_app, &tmp);  | 
51  | 0  |         rv = sk_nid_triple_value(sig_app, idx);  | 
52  | 0  |     }  | 
53  | 48.0k  | #ifndef OBJ_XREF_TEST2  | 
54  | 48.0k  |     if (rv == NULL) { | 
55  | 48.0k  |         rv = OBJ_bsearch_sig(&tmp, sigoid_srt, OSSL_NELEM(sigoid_srt));  | 
56  | 48.0k  |     }  | 
57  | 48.0k  | #endif  | 
58  | 48.0k  |     if (rv == NULL)  | 
59  | 9.21k  |         return 0;  | 
60  | 38.8k  |     if (pdig_nid)  | 
61  | 22.2k  |         *pdig_nid = rv->hash_id;  | 
62  | 38.8k  |     if (ppkey_nid)  | 
63  | 38.8k  |         *ppkey_nid = rv->pkey_id;  | 
64  | 38.8k  |     return 1;  | 
65  | 48.0k  | }  | 
66  |  |  | 
67  |  | int OBJ_find_sigid_by_algs(int *psignid, int dig_nid, int pkey_nid)  | 
68  | 0  | { | 
69  | 0  |     nid_triple tmp;  | 
70  | 0  |     const nid_triple *t = &tmp;  | 
71  | 0  |     const nid_triple **rv = NULL;  | 
72  |  | 
  | 
73  | 0  |     tmp.hash_id = dig_nid;  | 
74  | 0  |     tmp.pkey_id = pkey_nid;  | 
75  |  | 
  | 
76  | 0  |     if (sigx_app) { | 
77  | 0  |         int idx = sk_nid_triple_find(sigx_app, &tmp);  | 
78  | 0  |         if (idx >= 0) { | 
79  | 0  |             t = sk_nid_triple_value(sigx_app, idx);  | 
80  | 0  |             rv = &t;  | 
81  | 0  |         }  | 
82  | 0  |     }  | 
83  | 0  | #ifndef OBJ_XREF_TEST2  | 
84  | 0  |     if (rv == NULL) { | 
85  | 0  |         rv = OBJ_bsearch_sigx(&t, sigoid_srt_xref, OSSL_NELEM(sigoid_srt_xref));  | 
86  | 0  |     }  | 
87  | 0  | #endif  | 
88  | 0  |     if (rv == NULL)  | 
89  | 0  |         return 0;  | 
90  | 0  |     if (psignid)  | 
91  | 0  |         *psignid = (*rv)->sign_id;  | 
92  | 0  |     return 1;  | 
93  | 0  | }  | 
94  |  |  | 
95  |  | int OBJ_add_sigid(int signid, int dig_id, int pkey_id)  | 
96  |  | { | 
97  |  |     nid_triple *ntr;  | 
98  |  |     if (sig_app == NULL)  | 
99  |  |         sig_app = sk_nid_triple_new(sig_sk_cmp);  | 
100  |  |     if (sig_app == NULL)  | 
101  |  |         return 0;  | 
102  |  |     if (sigx_app == NULL)  | 
103  |  |         sigx_app = sk_nid_triple_new(sigx_cmp);  | 
104  |  |     if (sigx_app == NULL)  | 
105  |  |         return 0;  | 
106  |  |     if ((ntr = OPENSSL_malloc(sizeof(*ntr))) == NULL) { | 
107  |  |         ERR_raise(ERR_LIB_OBJ, ERR_R_MALLOC_FAILURE);  | 
108  |  |         return 0;  | 
109  |  |     }  | 
110  |  |     ntr->sign_id = signid;  | 
111  |  |     ntr->hash_id = dig_id;  | 
112  |  |     ntr->pkey_id = pkey_id;  | 
113  |  |  | 
114  |  |     if (!sk_nid_triple_push(sig_app, ntr)) { | 
115  |  |         OPENSSL_free(ntr);  | 
116  |  |         return 0;  | 
117  |  |     }  | 
118  |  |  | 
119  |  |     if (!sk_nid_triple_push(sigx_app, ntr))  | 
120  |  |         return 0;  | 
121  |  |  | 
122  |  |     sk_nid_triple_sort(sig_app);  | 
123  |  |     sk_nid_triple_sort(sigx_app);  | 
124  |  |  | 
125  |  |     return 1;  | 
126  |  | }  | 
127  |  |  | 
128  |  | static void sid_free(nid_triple *tt)  | 
129  | 0  | { | 
130  | 0  |     OPENSSL_free(tt);  | 
131  | 0  | }  | 
132  |  |  | 
133  |  | void OBJ_sigid_free(void)  | 
134  | 133  | { | 
135  | 133  |     sk_nid_triple_pop_free(sig_app, sid_free);  | 
136  | 133  |     sig_app = NULL;  | 
137  | 133  |     sk_nid_triple_free(sigx_app);  | 
138  | 133  |     sigx_app = NULL;  | 
139  | 133  | }  |