xref: /openbsd-src/lib/libcrypto/objects/obj_xref.c (revision 6afed99bad8b21d782d9fe93f955641e631734ef)
1 /* $OpenBSD: obj_xref.c,v 1.10 2023/07/22 18:12:09 tb Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 2006.
4  */
5 /* ====================================================================
6  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 
59 #include <openssl/objects.h>
60 #include "obj_xref.h"
61 
62 DECLARE_STACK_OF(nid_triple)
63 
64 static int
65 sig_cmp(const nid_triple *a, const nid_triple *b)
66 {
67 	return a->sign_id - b->sign_id;
68 }
69 
70 static int
71 sig_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
72 {
73 	nid_triple const *a = a_;
74 	nid_triple const *b = b_;
75 	return sig_cmp(a, b);
76 }
77 
78 static const nid_triple *
79 OBJ_bsearch_sig(nid_triple *key, nid_triple const *base, int num)
80 {
81 	return OBJ_bsearch_(key, base, num, sizeof(nid_triple),
82 	    sig_cmp_BSEARCH_CMP_FN);
83 }
84 
85 static int
86 sigx_cmp(const nid_triple * const *a, const nid_triple * const *b)
87 {
88 	int ret;
89 
90 	ret = (*a)->hash_id - (*b)->hash_id;
91 	if (ret)
92 		return ret;
93 	return (*a)->pkey_id - (*b)->pkey_id;
94 }
95 
96 static int
97 sigx_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
98 {
99 	const nid_triple * const *a = a_;
100 	const nid_triple * const *b = b_;
101 	return sigx_cmp(a, b);
102 }
103 
104 static const nid_triple * const*
105 OBJ_bsearch_sigx(const nid_triple * *key, const nid_triple * const *base, int num)
106 {
107 	return OBJ_bsearch_(key, base, num, sizeof(const nid_triple *),
108 	    sigx_cmp_BSEARCH_CMP_FN);
109 }
110 
111 int
112 OBJ_find_sigid_algs(int signid, int *pdig_nid, int *ppkey_nid)
113 {
114 	nid_triple tmp;
115 	const nid_triple *rv = NULL;
116 	tmp.sign_id = signid;
117 
118 	if ((rv = OBJ_bsearch_sig(&tmp, sigoid_srt,
119 	    sizeof(sigoid_srt) / sizeof(nid_triple))) == NULL)
120 		return 0;
121 	if (pdig_nid)
122 		*pdig_nid = rv->hash_id;
123 	if (ppkey_nid)
124 		*ppkey_nid = rv->pkey_id;
125 	return 1;
126 }
127 LCRYPTO_ALIAS(OBJ_find_sigid_algs);
128 
129 int
130 OBJ_find_sigid_by_algs(int *psignid, int dig_nid, int pkey_nid)
131 {
132 	nid_triple tmp;
133 	const nid_triple *t = &tmp;
134 	const nid_triple *const *rv;
135 
136 	tmp.hash_id = dig_nid;
137 	tmp.pkey_id = pkey_nid;
138 
139 	if ((rv = OBJ_bsearch_sigx(&t, sigoid_srt_xref,
140 	    sizeof(sigoid_srt_xref) / sizeof(nid_triple *))) == NULL)
141 		return 0;
142 	if (psignid)
143 		*psignid = (*rv)->sign_id;
144 	return 1;
145 }
146 LCRYPTO_ALIAS(OBJ_find_sigid_by_algs);
147 
148 int
149 OBJ_add_sigid(int signid, int dig_id, int pkey_id)
150 {
151 	return 0;
152 }
153 LCRYPTO_ALIAS(OBJ_add_sigid);
154 
155 void
156 OBJ_sigid_free(void)
157 {
158 }
159 LCRYPTO_ALIAS(OBJ_sigid_free);
160