1 /* $OpenBSD: ecs_lib.c,v 1.13 2018/04/14 07:09:21 tb Exp $ */
2 /* ====================================================================
3  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. All advertising materials mentioning features or use of this
18  *    software must display the following acknowledgment:
19  *    "This product includes software developed by the OpenSSL Project
20  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21  *
22  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23  *    endorse or promote products derived from this software without
24  *    prior written permission. For written permission, please contact
25  *    openssl-core@OpenSSL.org.
26  *
27  * 5. Products derived from this software may not be called "OpenSSL"
28  *    nor may "OpenSSL" appear in their names without prior written
29  *    permission of the OpenSSL Project.
30  *
31  * 6. Redistributions of any form whatsoever must retain the following
32  *    acknowledgment:
33  *    "This product includes software developed by the OpenSSL Project
34  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This product includes cryptographic software written by Eric Young
51  * (eay@cryptsoft.com).  This product includes software written by Tim
52  * Hudson (tjh@cryptsoft.com).
53  *
54  */
55 
56 #include <string.h>
57 
58 #include <openssl/opensslconf.h>
59 
60 #include "ecs_locl.h"
61 #ifndef OPENSSL_NO_ENGINE
62 #include <openssl/engine.h>
63 #endif
64 #include <openssl/err.h>
65 #include <openssl/bn.h>
66 
67 static const ECDSA_METHOD *default_ECDSA_method = NULL;
68 
69 static void *ecdsa_data_new(void);
70 static void *ecdsa_data_dup(void *);
71 static void  ecdsa_data_free(void *);
72 
73 void
74 ECDSA_set_default_method(const ECDSA_METHOD *meth)
75 {
76 	default_ECDSA_method = meth;
77 }
78 
79 const ECDSA_METHOD *
80 ECDSA_get_default_method(void)
81 {
82 	if (!default_ECDSA_method) {
83 		default_ECDSA_method = ECDSA_OpenSSL();
84 	}
85 	return default_ECDSA_method;
86 }
87 
88 int
89 ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth)
90 {
91 	ECDSA_DATA *ecdsa;
92 
93 	ecdsa = ecdsa_check(eckey);
94 
95 	if (ecdsa == NULL)
96 		return 0;
97 
98 #ifndef OPENSSL_NO_ENGINE
99 	ENGINE_finish(ecdsa->engine);
100 	ecdsa->engine = NULL;
101 #endif
102 	ecdsa->meth = meth;
103 
104 	return 1;
105 }
106 
107 static ECDSA_DATA *
108 ECDSA_DATA_new_method(ENGINE *engine)
109 {
110 	ECDSA_DATA *ret;
111 
112 	ret = malloc(sizeof(ECDSA_DATA));
113 	if (ret == NULL) {
114 		ECDSAerror(ERR_R_MALLOC_FAILURE);
115 		return (NULL);
116 	}
117 
118 	ret->init = NULL;
119 
120 	ret->meth = ECDSA_get_default_method();
121 	ret->engine = engine;
122 #ifndef OPENSSL_NO_ENGINE
123 	if (!ret->engine)
124 		ret->engine = ENGINE_get_default_ECDSA();
125 	if (ret->engine) {
126 		ret->meth = ENGINE_get_ECDSA(ret->engine);
127 		if (ret->meth == NULL) {
128 			ECDSAerror(ERR_R_ENGINE_LIB);
129 			ENGINE_finish(ret->engine);
130 			free(ret);
131 			return NULL;
132 		}
133 	}
134 #endif
135 
136 	ret->flags = ret->meth->flags;
137 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ECDSA, ret, &ret->ex_data);
138 	return (ret);
139 }
140 
141 static void *
142 ecdsa_data_new(void)
143 {
144 	return (void *)ECDSA_DATA_new_method(NULL);
145 }
146 
147 static void *
148 ecdsa_data_dup(void *data)
149 {
150 	ECDSA_DATA *r = (ECDSA_DATA *)data;
151 
152 	/* XXX: dummy operation */
153 	if (r == NULL)
154 		return NULL;
155 
156 	return ecdsa_data_new();
157 }
158 
159 static void
160 ecdsa_data_free(void *data)
161 {
162 	ECDSA_DATA *r = (ECDSA_DATA *)data;
163 
164 #ifndef OPENSSL_NO_ENGINE
165 	ENGINE_finish(r->engine);
166 #endif
167 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDSA, r, &r->ex_data);
168 
169 	freezero(r, sizeof(ECDSA_DATA));
170 }
171 
172 ECDSA_DATA *
173 ecdsa_check(EC_KEY *key)
174 {
175 	ECDSA_DATA *ecdsa_data;
176 
177 	void *data = EC_KEY_get_key_method_data(key, ecdsa_data_dup,
178 	    ecdsa_data_free, ecdsa_data_free);
179 	if (data == NULL) {
180 		ecdsa_data = (ECDSA_DATA *)ecdsa_data_new();
181 		if (ecdsa_data == NULL)
182 			return NULL;
183 		data = EC_KEY_insert_key_method_data(key, (void *)ecdsa_data,
184 		    ecdsa_data_dup, ecdsa_data_free, ecdsa_data_free);
185 		if (data != NULL) {
186 			/* Another thread raced us to install the key_method
187 			 * data and won. */
188 			ecdsa_data_free(ecdsa_data);
189 			ecdsa_data = (ECDSA_DATA *)data;
190 		}
191 	} else
192 		ecdsa_data = (ECDSA_DATA *)data;
193 
194 	return ecdsa_data;
195 }
196 
197 int
198 ECDSA_size(const EC_KEY *r)
199 {
200 	int ret, i;
201 	ASN1_INTEGER bs;
202 	BIGNUM	*order = NULL;
203 	unsigned char buf[4];
204 	const EC_GROUP *group;
205 
206 	if (r == NULL)
207 		return 0;
208 	group = EC_KEY_get0_group(r);
209 	if (group == NULL)
210 		return 0;
211 
212 	if ((order = BN_new()) == NULL)
213 		return 0;
214 	if (!EC_GROUP_get_order(group, order, NULL)) {
215 		BN_clear_free(order);
216 		return 0;
217 	}
218 	i = BN_num_bits(order);
219 	bs.length = (i + 7) / 8;
220 	bs.data = buf;
221 	bs.type = V_ASN1_INTEGER;
222 	/* If the top bit is set the asn1 encoding is 1 larger. */
223 	buf[0] = 0xff;
224 
225 	i = i2d_ASN1_INTEGER(&bs, NULL);
226 	i += i; /* r and s */
227 	ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
228 	BN_clear_free(order);
229 	return (ret);
230 }
231 
232 int
233 ECDSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
234     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
235 {
236 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ECDSA, argl, argp,
237 	    new_func, dup_func, free_func);
238 }
239 
240 int
241 ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg)
242 {
243 	ECDSA_DATA *ecdsa;
244 	ecdsa = ecdsa_check(d);
245 	if (ecdsa == NULL)
246 		return 0;
247 	return (CRYPTO_set_ex_data(&ecdsa->ex_data, idx, arg));
248 }
249 
250 void *
251 ECDSA_get_ex_data(EC_KEY *d, int idx)
252 {
253 	ECDSA_DATA *ecdsa;
254 	ecdsa = ecdsa_check(d);
255 	if (ecdsa == NULL)
256 		return NULL;
257 	return (CRYPTO_get_ex_data(&ecdsa->ex_data, idx));
258 }
259