blob: 58fd9a98747ac587e26c34d92ee208cd30aaec51 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57#include <openssl/evp.h>
58
59#include <assert.h>
60#include <string.h>
61
62#include <openssl/bio.h>
63#include <openssl/dh.h>
64#include <openssl/dsa.h>
65#include <openssl/ec.h>
66#include <openssl/err.h>
67#include <openssl/mem.h>
68#include <openssl/obj.h>
69#include <openssl/rsa.h>
Adam Langleye9ada862015-05-11 17:20:37 -070070#include <openssl/thread.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080071
72#include "internal.h"
73
74
Adam Langleye9ada862015-05-11 17:20:37 -070075extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meth;
Adam Langleyd9e397b2015-01-22 14:27:53 -080076extern const EVP_PKEY_ASN1_METHOD ec_asn1_meth;
77extern const EVP_PKEY_ASN1_METHOD hmac_asn1_meth;
78extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meth;
79
80EVP_PKEY *EVP_PKEY_new(void) {
81 EVP_PKEY *ret;
82
83 ret = OPENSSL_malloc(sizeof(EVP_PKEY));
84 if (ret == NULL) {
85 OPENSSL_PUT_ERROR(EVP, EVP_PKEY_new, ERR_R_MALLOC_FAILURE);
86 return NULL;
87 }
88
89 memset(ret, 0, sizeof(EVP_PKEY));
90 ret->type = EVP_PKEY_NONE;
91 ret->references = 1;
92
93 return ret;
94}
95
96static void free_it(EVP_PKEY *pkey) {
97 if (pkey->ameth && pkey->ameth->pkey_free) {
98 pkey->ameth->pkey_free(pkey);
99 pkey->pkey.ptr = NULL;
100 pkey->type = EVP_PKEY_NONE;
101 }
102}
103
104void EVP_PKEY_free(EVP_PKEY *pkey) {
105 if (pkey == NULL) {
106 return;
107 }
108
109 if (CRYPTO_add(&pkey->references, -1, CRYPTO_LOCK_EVP_PKEY)) {
110 return;
111 }
112
113 free_it(pkey);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800114 OPENSSL_free(pkey);
115}
116
Adam Langleye9ada862015-05-11 17:20:37 -0700117EVP_PKEY *EVP_PKEY_up_ref(EVP_PKEY *pkey) {
118 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
119 return pkey;
120}
121
Adam Langleyd9e397b2015-01-22 14:27:53 -0800122int EVP_PKEY_is_opaque(const EVP_PKEY *pkey) {
123 if (pkey->ameth && pkey->ameth->pkey_opaque) {
124 return pkey->ameth->pkey_opaque(pkey);
125 }
126 return 0;
127}
128
129int EVP_PKEY_supports_digest(const EVP_PKEY *pkey, const EVP_MD *md) {
130 if (pkey->ameth && pkey->ameth->pkey_supports_digest) {
131 return pkey->ameth->pkey_supports_digest(pkey, md);
132 }
133 return 1;
134}
135
136int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
137 if (a->type != b->type) {
138 return -1;
139 }
140
141 if (a->ameth) {
142 int ret;
143 /* Compare parameters if the algorithm has them */
144 if (a->ameth->param_cmp) {
145 ret = a->ameth->param_cmp(a, b);
Adam Langleye9ada862015-05-11 17:20:37 -0700146 if (ret <= 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800147 return ret;
Adam Langleye9ada862015-05-11 17:20:37 -0700148 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800149 }
150
151 if (a->ameth->pub_cmp) {
152 return a->ameth->pub_cmp(a, b);
153 }
154 }
155
156 return -2;
157}
158
Adam Langleyd9e397b2015-01-22 14:27:53 -0800159int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
160 if (to->type != from->type) {
161 OPENSSL_PUT_ERROR(EVP, EVP_PKEY_copy_parameters, EVP_R_DIFFERENT_KEY_TYPES);
162 goto err;
163 }
164
165 if (EVP_PKEY_missing_parameters(from)) {
166 OPENSSL_PUT_ERROR(EVP, EVP_PKEY_copy_parameters, EVP_R_MISSING_PARAMETERS);
167 goto err;
168 }
169
170 if (from->ameth && from->ameth->param_copy) {
171 return from->ameth->param_copy(to, from);
172 }
173
174err:
175 return 0;
176}
177
178int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) {
179 if (pkey->ameth && pkey->ameth->param_missing) {
180 return pkey->ameth->param_missing(pkey);
181 }
182 return 0;
183}
184
185int EVP_PKEY_size(const EVP_PKEY *pkey) {
186 if (pkey && pkey->ameth && pkey->ameth->pkey_size) {
187 return pkey->ameth->pkey_size(pkey);
188 }
189 return 0;
190}
191
192int EVP_PKEY_bits(EVP_PKEY *pkey) {
193 if (pkey && pkey->ameth && pkey->ameth->pkey_bits) {
194 return pkey->ameth->pkey_bits(pkey);
195 }
196 return 0;
197}
198
199int EVP_PKEY_id(const EVP_PKEY *pkey) {
200 return pkey->type;
201}
202
203/* TODO(fork): remove the first argument. */
204const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pengine, int nid) {
205 switch (nid) {
206 case EVP_PKEY_RSA:
207 case EVP_PKEY_RSA2:
208 return &rsa_asn1_meth;
209 case EVP_PKEY_HMAC:
210 return &hmac_asn1_meth;
211 case EVP_PKEY_EC:
212 return &ec_asn1_meth;
Adam Langleye9ada862015-05-11 17:20:37 -0700213 case EVP_PKEY_DSA:
214 return &dsa_asn1_meth;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800215 default:
216 return NULL;
217 }
218}
219
220int EVP_PKEY_type(int nid) {
221 const EVP_PKEY_ASN1_METHOD *meth = EVP_PKEY_asn1_find(NULL, nid);
222 if (meth == NULL) {
223 return NID_undef;
224 }
225 return meth->pkey_id;
226}
227
228EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, const uint8_t *mac_key,
229 size_t mac_key_len) {
230 EVP_PKEY_CTX *mac_ctx = NULL;
231 EVP_PKEY *ret = NULL;
232
233 mac_ctx = EVP_PKEY_CTX_new_id(type, e);
234 if (!mac_ctx) {
235 return NULL;
236 }
237
Adam Langleye9ada862015-05-11 17:20:37 -0700238 if (!EVP_PKEY_keygen_init(mac_ctx) ||
239 !EVP_PKEY_CTX_ctrl(mac_ctx, -1, EVP_PKEY_OP_KEYGEN,
240 EVP_PKEY_CTRL_SET_MAC_KEY, mac_key_len,
241 (uint8_t *)mac_key) ||
242 !EVP_PKEY_keygen(mac_ctx, &ret)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800243 ret = NULL;
244 goto merr;
245 }
246
247merr:
Adam Langleye9ada862015-05-11 17:20:37 -0700248 if (mac_ctx) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800249 EVP_PKEY_CTX_free(mac_ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700250 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800251 return ret;
252}
253
254int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) {
255 if (EVP_PKEY_assign_RSA(pkey, key)) {
256 RSA_up_ref(key);
257 return 1;
258 }
259 return 0;
260}
261
262int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) {
263 return EVP_PKEY_assign(pkey, EVP_PKEY_RSA, key);
264}
265
266RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey) {
267 if (pkey->type != EVP_PKEY_RSA) {
268 OPENSSL_PUT_ERROR(EVP, EVP_PKEY_get1_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
269 return NULL;
270 }
271 RSA_up_ref(pkey->pkey.rsa);
272 return pkey->pkey.rsa;
273}
274
275int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) {
276 if (EVP_PKEY_assign_DSA(pkey, key)) {
277 DSA_up_ref(key);
278 return 1;
279 }
280 return 0;
281}
282
283int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key) {
284 return EVP_PKEY_assign(pkey, EVP_PKEY_DSA, key);
285}
286
287DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey) {
288 if (pkey->type != EVP_PKEY_DSA) {
289 OPENSSL_PUT_ERROR(EVP, EVP_PKEY_get1_DSA, EVP_R_EXPECTING_A_DSA_KEY);
290 return NULL;
291 }
292 DSA_up_ref(pkey->pkey.dsa);
293 return pkey->pkey.dsa;
294}
295
296int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
297 if (EVP_PKEY_assign_EC_KEY(pkey, key)) {
298 EC_KEY_up_ref(key);
299 return 1;
300 }
301 return 0;
302}
303
304int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
305 return EVP_PKEY_assign(pkey, EVP_PKEY_EC, key);
306}
307
308EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) {
309 if (pkey->type != EVP_PKEY_EC) {
310 OPENSSL_PUT_ERROR(EVP, EVP_PKEY_get1_EC_KEY, EVP_R_EXPECTING_AN_EC_KEY_KEY);
311 return NULL;
312 }
313 EC_KEY_up_ref(pkey->pkey.ec);
314 return pkey->pkey.ec;
315}
316
317int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) {
318 if (EVP_PKEY_assign_DH(pkey, key)) {
319 DH_up_ref(key);
320 return 1;
321 }
322 return 0;
323}
324
325int EVP_PKEY_assign_DH(EVP_PKEY *pkey, DH *key) {
Adam Langleye9ada862015-05-11 17:20:37 -0700326 return EVP_PKEY_assign(pkey, EVP_PKEY_DH, key);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800327}
328
329DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey) {
330 if (pkey->type != EVP_PKEY_DH) {
331 OPENSSL_PUT_ERROR(EVP, EVP_PKEY_get1_DH, EVP_R_EXPECTING_A_DH_KEY);
332 return NULL;
333 }
334 DH_up_ref(pkey->pkey.dh);
335 return pkey->pkey.dh;
336}
337
338int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) {
339 if (!EVP_PKEY_set_type(pkey, type)) {
340 return 0;
341 }
342 pkey->pkey.ptr = key;
343 return key != NULL;
344}
345
346const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pengine,
347 const char *name,
348 size_t len) {
349 if (len == 3 && memcmp(name, "RSA", 3) == 0) {
350 return &rsa_asn1_meth;
351 } else if (len == 4 && memcmp(name, "HMAC", 4) == 0) {
352 return &hmac_asn1_meth;
353 } if (len == 2 && memcmp(name, "EC", 2) == 0) {
354 return &ec_asn1_meth;
355 }
356 return NULL;
357}
358
359int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
360 const EVP_PKEY_ASN1_METHOD *ameth;
361
362 if (pkey && pkey->pkey.ptr) {
363 free_it(pkey);
364 }
365
366 ameth = EVP_PKEY_asn1_find(NULL, type);
367 if (ameth == NULL) {
368 OPENSSL_PUT_ERROR(EVP, EVP_PKEY_set_type, EVP_R_UNSUPPORTED_ALGORITHM);
369 ERR_add_error_dataf("algorithm %d (%s)", type, OBJ_nid2sn(type));
370 return 0;
371 }
372
373 if (pkey) {
374 pkey->ameth = ameth;
375 pkey->type = pkey->ameth->pkey_id;
376 }
377
378 return 1;
379}
380
381
382
383int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
384 if (a->type != b->type) {
385 return -1;
386 }
387 if (a->ameth && a->ameth->param_cmp) {
388 return a->ameth->param_cmp(a, b);
389 }
390 return -2;
391}
392
393static int print_unsupported(BIO *out, const EVP_PKEY *pkey, int indent,
394 const char *kstr) {
395 BIO_indent(out, indent, 128);
396 BIO_printf(out, "%s algorithm \"%s\" unsupported\n", kstr,
397 OBJ_nid2ln(pkey->type));
398 return 1;
399}
400
401int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
402 ASN1_PCTX *pctx) {
403 if (pkey->ameth && pkey->ameth->pub_print) {
404 return pkey->ameth->pub_print(out, pkey, indent, pctx);
405 }
406
407 return print_unsupported(out, pkey, indent, "Public Key");
408}
409
410int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
411 ASN1_PCTX *pctx) {
412 if (pkey->ameth && pkey->ameth->priv_print) {
413 return pkey->ameth->priv_print(out, pkey, indent, pctx);
414 }
415
416 return print_unsupported(out, pkey, indent, "Private Key");
417}
418
419int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
420 ASN1_PCTX *pctx) {
421 if (pkey->ameth && pkey->ameth->param_print) {
422 return pkey->ameth->param_print(out, pkey, indent, pctx);
423 }
424
425 return print_unsupported(out, pkey, indent, "Parameters");
426}
427
428int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
429 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, 0,
430 (void *)md);
431}
432
433int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
434 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_GET_MD,
435 0, (void *)out_md);
436}
437
Adam Langleye9ada862015-05-11 17:20:37 -0700438EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey) {
439 return EVP_PKEY_up_ref(pkey);
440}
441
Adam Langleyd9e397b2015-01-22 14:27:53 -0800442void OpenSSL_add_all_algorithms(void) {}
443
444void EVP_cleanup(void) {}