Add EVP_PKEY_set1_engine() function.
[oweals/openssl.git] / crypto / evp / p_lib.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/bn.h>
13 #include <openssl/err.h>
14 #include <openssl/objects.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509.h>
17 #include <openssl/rsa.h>
18 #include <openssl/dsa.h>
19 #include <openssl/dh.h>
20 #include <openssl/engine.h>
21
22 #include "internal/asn1_int.h"
23 #include "internal/evp_int.h"
24
25 static void EVP_PKEY_free_it(EVP_PKEY *x);
26
27 int EVP_PKEY_bits(const EVP_PKEY *pkey)
28 {
29     if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
30         return pkey->ameth->pkey_bits(pkey);
31     return 0;
32 }
33
34 int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
35 {
36     if (pkey == NULL)
37         return 0;
38     if (!pkey->ameth || !pkey->ameth->pkey_security_bits)
39         return -2;
40     return pkey->ameth->pkey_security_bits(pkey);
41 }
42
43 int EVP_PKEY_size(EVP_PKEY *pkey)
44 {
45     if (pkey && pkey->ameth && pkey->ameth->pkey_size)
46         return pkey->ameth->pkey_size(pkey);
47     return 0;
48 }
49
50 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
51 {
52 #ifndef OPENSSL_NO_DSA
53     if (pkey->type == EVP_PKEY_DSA) {
54         int ret = pkey->save_parameters;
55
56         if (mode >= 0)
57             pkey->save_parameters = mode;
58         return (ret);
59     }
60 #endif
61 #ifndef OPENSSL_NO_EC
62     if (pkey->type == EVP_PKEY_EC) {
63         int ret = pkey->save_parameters;
64
65         if (mode >= 0)
66             pkey->save_parameters = mode;
67         return (ret);
68     }
69 #endif
70     return (0);
71 }
72
73 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
74 {
75     if (to->type == EVP_PKEY_NONE) {
76         if (EVP_PKEY_set_type(to, from->type) == 0)
77             return 0;
78     } else if (to->type != from->type) {
79         EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
80         goto err;
81     }
82
83     if (EVP_PKEY_missing_parameters(from)) {
84         EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
85         goto err;
86     }
87
88     if (!EVP_PKEY_missing_parameters(to)) {
89         if (EVP_PKEY_cmp_parameters(to, from) == 1)
90             return 1;
91         EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
92         return 0;
93     }
94
95     if (from->ameth && from->ameth->param_copy)
96         return from->ameth->param_copy(to, from);
97  err:
98     return 0;
99 }
100
101 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
102 {
103     if (pkey->ameth && pkey->ameth->param_missing)
104         return pkey->ameth->param_missing(pkey);
105     return 0;
106 }
107
108 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
109 {
110     if (a->type != b->type)
111         return -1;
112     if (a->ameth && a->ameth->param_cmp)
113         return a->ameth->param_cmp(a, b);
114     return -2;
115 }
116
117 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
118 {
119     if (a->type != b->type)
120         return -1;
121
122     if (a->ameth) {
123         int ret;
124         /* Compare parameters if the algorithm has them */
125         if (a->ameth->param_cmp) {
126             ret = a->ameth->param_cmp(a, b);
127             if (ret <= 0)
128                 return ret;
129         }
130
131         if (a->ameth->pub_cmp)
132             return a->ameth->pub_cmp(a, b);
133     }
134
135     return -2;
136 }
137
138 EVP_PKEY *EVP_PKEY_new(void)
139 {
140     EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
141
142     if (ret == NULL) {
143         EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
144         return NULL;
145     }
146     ret->type = EVP_PKEY_NONE;
147     ret->save_type = EVP_PKEY_NONE;
148     ret->references = 1;
149     ret->save_parameters = 1;
150     ret->lock = CRYPTO_THREAD_lock_new();
151     if (ret->lock == NULL) {
152         EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
153         OPENSSL_free(ret);
154         return NULL;
155     }
156     return ret;
157 }
158
159 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
160 {
161     int i;
162
163     if (CRYPTO_atomic_add(&pkey->references, 1, &i, pkey->lock) <= 0)
164         return 0;
165
166     REF_PRINT_COUNT("EVP_PKEY", pkey);
167     REF_ASSERT_ISNT(i < 2);
168     return ((i > 1) ? 1 : 0);
169 }
170
171 /*
172  * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
173  * is NULL just return 1 or 0 if the algorithm exists.
174  */
175
176 static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
177 {
178     const EVP_PKEY_ASN1_METHOD *ameth;
179     ENGINE *e = NULL;
180     if (pkey) {
181         if (pkey->pkey.ptr)
182             EVP_PKEY_free_it(pkey);
183         /*
184          * If key type matches and a method exists then this lookup has
185          * succeeded once so just indicate success.
186          */
187         if ((type == pkey->save_type) && pkey->ameth)
188             return 1;
189 #ifndef OPENSSL_NO_ENGINE
190         /* If we have ENGINEs release them */
191         ENGINE_finish(pkey->engine);
192         pkey->engine = NULL;
193         ENGINE_finish(pkey->pmeth_engine);
194         pkey->pmeth_engine = NULL;
195 #endif
196     }
197     if (str)
198         ameth = EVP_PKEY_asn1_find_str(&e, str, len);
199     else
200         ameth = EVP_PKEY_asn1_find(&e, type);
201 #ifndef OPENSSL_NO_ENGINE
202     if (pkey == NULL)
203         ENGINE_finish(e);
204 #endif
205     if (ameth == NULL) {
206         EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
207         return 0;
208     }
209     if (pkey) {
210         pkey->ameth = ameth;
211         pkey->engine = e;
212
213         pkey->type = pkey->ameth->pkey_id;
214         pkey->save_type = type;
215     }
216     return 1;
217 }
218
219 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
220 {
221     return pkey_set_type(pkey, type, NULL, -1);
222 }
223
224 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
225 {
226     return pkey_set_type(pkey, EVP_PKEY_NONE, str, len);
227 }
228 #ifndef OPENSSL_NO_ENGINE
229 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
230 {
231     if (e != NULL) {
232         if (!ENGINE_init(e)) {
233             EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
234             return 0;
235         }
236         if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
237             ENGINE_finish(e);
238             EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
239             return 0;
240         }
241     }
242     ENGINE_finish(pkey->pmeth_engine);
243     pkey->pmeth_engine = e;
244     return 1;
245 }
246 #endif
247 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
248 {
249     if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
250         return 0;
251     pkey->pkey.ptr = key;
252     return (key != NULL);
253 }
254
255 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
256 {
257     return pkey->pkey.ptr;
258 }
259
260 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
261 {
262     ASN1_OCTET_STRING *os = NULL;
263     if (pkey->type != EVP_PKEY_HMAC) {
264         EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
265         return NULL;
266     }
267     os = EVP_PKEY_get0(pkey);
268     *len = os->length;
269     return os->data;
270 }
271
272 #ifndef OPENSSL_NO_RSA
273 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
274 {
275     int ret = EVP_PKEY_assign_RSA(pkey, key);
276     if (ret)
277         RSA_up_ref(key);
278     return ret;
279 }
280
281 RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
282 {
283     if (pkey->type != EVP_PKEY_RSA) {
284         EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
285         return NULL;
286     }
287     return pkey->pkey.rsa;
288 }
289
290 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
291 {
292     RSA *ret = EVP_PKEY_get0_RSA(pkey);
293     if (ret != NULL)
294         RSA_up_ref(ret);
295     return ret;
296 }
297 #endif
298
299 #ifndef OPENSSL_NO_DSA
300 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
301 {
302     int ret = EVP_PKEY_assign_DSA(pkey, key);
303     if (ret)
304         DSA_up_ref(key);
305     return ret;
306 }
307
308 DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
309 {
310     if (pkey->type != EVP_PKEY_DSA) {
311         EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
312         return NULL;
313     }
314     return pkey->pkey.dsa;
315 }
316
317 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
318 {
319     DSA *ret = EVP_PKEY_get0_DSA(pkey);
320     if (ret != NULL)
321         DSA_up_ref(ret);
322     return ret;
323 }
324 #endif
325
326 #ifndef OPENSSL_NO_EC
327
328 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
329 {
330     int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
331     if (ret)
332         EC_KEY_up_ref(key);
333     return ret;
334 }
335
336 EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
337 {
338     if (pkey->type != EVP_PKEY_EC) {
339         EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
340         return NULL;
341     }
342     return pkey->pkey.ec;
343 }
344
345 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
346 {
347     EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
348     if (ret != NULL)
349         EC_KEY_up_ref(ret);
350     return ret;
351 }
352 #endif
353
354 #ifndef OPENSSL_NO_DH
355
356 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
357 {
358     int ret = EVP_PKEY_assign_DH(pkey, key);
359     if (ret)
360         DH_up_ref(key);
361     return ret;
362 }
363
364 DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey)
365 {
366     if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
367         EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
368         return NULL;
369     }
370     return pkey->pkey.dh;
371 }
372
373 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
374 {
375     DH *ret = EVP_PKEY_get0_DH(pkey);
376     if (ret != NULL)
377         DH_up_ref(ret);
378     return ret;
379 }
380 #endif
381
382 int EVP_PKEY_type(int type)
383 {
384     int ret;
385     const EVP_PKEY_ASN1_METHOD *ameth;
386     ENGINE *e;
387     ameth = EVP_PKEY_asn1_find(&e, type);
388     if (ameth)
389         ret = ameth->pkey_id;
390     else
391         ret = NID_undef;
392 #ifndef OPENSSL_NO_ENGINE
393     ENGINE_finish(e);
394 #endif
395     return ret;
396 }
397
398 int EVP_PKEY_id(const EVP_PKEY *pkey)
399 {
400     return pkey->type;
401 }
402
403 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
404 {
405     return EVP_PKEY_type(pkey->type);
406 }
407
408 void EVP_PKEY_free(EVP_PKEY *x)
409 {
410     int i;
411
412     if (x == NULL)
413         return;
414
415     CRYPTO_atomic_add(&x->references, -1, &i, x->lock);
416     REF_PRINT_COUNT("EVP_PKEY", x);
417     if (i > 0)
418         return;
419     REF_ASSERT_ISNT(i < 0);
420     EVP_PKEY_free_it(x);
421     CRYPTO_THREAD_lock_free(x->lock);
422     sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
423     OPENSSL_free(x);
424 }
425
426 static void EVP_PKEY_free_it(EVP_PKEY *x)
427 {
428     /* internal function; x is never NULL */
429     if (x->ameth && x->ameth->pkey_free) {
430         x->ameth->pkey_free(x);
431         x->pkey.ptr = NULL;
432     }
433 #ifndef OPENSSL_NO_ENGINE
434     ENGINE_finish(x->engine);
435     x->engine = NULL;
436     ENGINE_finish(x->pmeth_engine);
437     x->pmeth_engine = NULL;
438 #endif
439 }
440
441 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
442                      const char *kstr)
443 {
444     BIO_indent(out, indent, 128);
445     BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
446                kstr, OBJ_nid2ln(pkey->type));
447     return 1;
448 }
449
450 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
451                           int indent, ASN1_PCTX *pctx)
452 {
453     if (pkey->ameth && pkey->ameth->pub_print)
454         return pkey->ameth->pub_print(out, pkey, indent, pctx);
455
456     return unsup_alg(out, pkey, indent, "Public Key");
457 }
458
459 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
460                            int indent, ASN1_PCTX *pctx)
461 {
462     if (pkey->ameth && pkey->ameth->priv_print)
463         return pkey->ameth->priv_print(out, pkey, indent, pctx);
464
465     return unsup_alg(out, pkey, indent, "Private Key");
466 }
467
468 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
469                           int indent, ASN1_PCTX *pctx)
470 {
471     if (pkey->ameth && pkey->ameth->param_print)
472         return pkey->ameth->param_print(out, pkey, indent, pctx);
473     return unsup_alg(out, pkey, indent, "Parameters");
474 }
475
476 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
477 {
478     if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
479         return -2;
480     return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
481 }
482
483 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
484 {
485     return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
486 }
487
488 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
489                                const unsigned char *pt, size_t ptlen)
490 {
491     if (ptlen > INT_MAX)
492         return 0;
493     if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
494                            (void *)pt) <= 0)
495         return 0;
496     return 1;
497 }
498
499 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
500 {
501     int rv;
502     rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
503     if (rv <= 0)
504         return 0;
505     return rv;
506 }