Copyright consolidation 04/10
[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(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     if (from->ameth && from->ameth->param_copy)
88         return from->ameth->param_copy(to, from);
89  err:
90     return 0;
91 }
92
93 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
94 {
95     if (pkey->ameth && pkey->ameth->param_missing)
96         return pkey->ameth->param_missing(pkey);
97     return 0;
98 }
99
100 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
101 {
102     if (a->type != b->type)
103         return -1;
104     if (a->ameth && a->ameth->param_cmp)
105         return a->ameth->param_cmp(a, b);
106     return -2;
107 }
108
109 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
110 {
111     if (a->type != b->type)
112         return -1;
113
114     if (a->ameth) {
115         int ret;
116         /* Compare parameters if the algorithm has them */
117         if (a->ameth->param_cmp) {
118             ret = a->ameth->param_cmp(a, b);
119             if (ret <= 0)
120                 return ret;
121         }
122
123         if (a->ameth->pub_cmp)
124             return a->ameth->pub_cmp(a, b);
125     }
126
127     return -2;
128 }
129
130 EVP_PKEY *EVP_PKEY_new(void)
131 {
132     EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
133
134     if (ret == NULL) {
135         EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
136         return NULL;
137     }
138     ret->type = EVP_PKEY_NONE;
139     ret->save_type = EVP_PKEY_NONE;
140     ret->references = 1;
141     ret->save_parameters = 1;
142     ret->lock = CRYPTO_THREAD_lock_new();
143     if (ret->lock == NULL) {
144         EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
145         OPENSSL_free(ret);
146         return NULL;
147     }
148     return ret;
149 }
150
151 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
152 {
153     int i;
154
155     if (CRYPTO_atomic_add(&pkey->references, 1, &i, pkey->lock) <= 0)
156         return 0;
157
158     REF_PRINT_COUNT("EVP_PKEY", pkey);
159     REF_ASSERT_ISNT(i < 2);
160     return ((i > 1) ? 1 : 0);
161 }
162
163 /*
164  * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
165  * is NULL just return 1 or 0 if the algorithm exists.
166  */
167
168 static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
169 {
170     const EVP_PKEY_ASN1_METHOD *ameth;
171     ENGINE *e = NULL;
172     if (pkey) {
173         if (pkey->pkey.ptr)
174             EVP_PKEY_free_it(pkey);
175         /*
176          * If key type matches and a method exists then this lookup has
177          * succeeded once so just indicate success.
178          */
179         if ((type == pkey->save_type) && pkey->ameth)
180             return 1;
181 #ifndef OPENSSL_NO_ENGINE
182         /* If we have an ENGINE release it */
183         ENGINE_finish(pkey->engine);
184         pkey->engine = NULL;
185 #endif
186     }
187     if (str)
188         ameth = EVP_PKEY_asn1_find_str(&e, str, len);
189     else
190         ameth = EVP_PKEY_asn1_find(&e, type);
191 #ifndef OPENSSL_NO_ENGINE
192     if (pkey == NULL)
193         ENGINE_finish(e);
194 #endif
195     if (ameth == NULL) {
196         EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
197         return 0;
198     }
199     if (pkey) {
200         pkey->ameth = ameth;
201         pkey->engine = e;
202
203         pkey->type = pkey->ameth->pkey_id;
204         pkey->save_type = type;
205     }
206     return 1;
207 }
208
209 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
210 {
211     return pkey_set_type(pkey, type, NULL, -1);
212 }
213
214 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
215 {
216     return pkey_set_type(pkey, EVP_PKEY_NONE, str, len);
217 }
218
219 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
220 {
221     if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
222         return 0;
223     pkey->pkey.ptr = key;
224     return (key != NULL);
225 }
226
227 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
228 {
229     return pkey->pkey.ptr;
230 }
231
232 #ifndef OPENSSL_NO_RSA
233 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
234 {
235     int ret = EVP_PKEY_assign_RSA(pkey, key);
236     if (ret)
237         RSA_up_ref(key);
238     return ret;
239 }
240
241 RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
242 {
243     if (pkey->type != EVP_PKEY_RSA) {
244         EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
245         return NULL;
246     }
247     return pkey->pkey.rsa;
248 }
249
250 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
251 {
252     RSA *ret = EVP_PKEY_get0_RSA(pkey);
253     if (ret != NULL)
254         RSA_up_ref(ret);
255     return ret;
256 }
257 #endif
258
259 #ifndef OPENSSL_NO_DSA
260 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
261 {
262     int ret = EVP_PKEY_assign_DSA(pkey, key);
263     if (ret)
264         DSA_up_ref(key);
265     return ret;
266 }
267
268 DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
269 {
270     if (pkey->type != EVP_PKEY_DSA) {
271         EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
272         return NULL;
273     }
274     return pkey->pkey.dsa;
275 }
276
277 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
278 {
279     DSA *ret = EVP_PKEY_get0_DSA(pkey);
280     if (ret != NULL)
281         DSA_up_ref(ret);
282     return ret;
283 }
284 #endif
285
286 #ifndef OPENSSL_NO_EC
287
288 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
289 {
290     int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
291     if (ret)
292         EC_KEY_up_ref(key);
293     return ret;
294 }
295
296 EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
297 {
298     if (pkey->type != EVP_PKEY_EC) {
299         EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
300         return NULL;
301     }
302     return pkey->pkey.ec;
303 }
304
305 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
306 {
307     EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
308     if (ret != NULL)
309         EC_KEY_up_ref(ret);
310     return ret;
311 }
312 #endif
313
314 #ifndef OPENSSL_NO_DH
315
316 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
317 {
318     int ret = EVP_PKEY_assign_DH(pkey, key);
319     if (ret)
320         DH_up_ref(key);
321     return ret;
322 }
323
324 DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey)
325 {
326     if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
327         EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
328         return NULL;
329     }
330     return pkey->pkey.dh;
331 }
332
333 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
334 {
335     DH *ret = EVP_PKEY_get0_DH(pkey);
336     if (ret != NULL)
337         DH_up_ref(ret);
338     return ret;
339 }
340 #endif
341
342 int EVP_PKEY_type(int type)
343 {
344     int ret;
345     const EVP_PKEY_ASN1_METHOD *ameth;
346     ENGINE *e;
347     ameth = EVP_PKEY_asn1_find(&e, type);
348     if (ameth)
349         ret = ameth->pkey_id;
350     else
351         ret = NID_undef;
352 #ifndef OPENSSL_NO_ENGINE
353     ENGINE_finish(e);
354 #endif
355     return ret;
356 }
357
358 int EVP_PKEY_id(const EVP_PKEY *pkey)
359 {
360     return pkey->type;
361 }
362
363 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
364 {
365     return EVP_PKEY_type(pkey->type);
366 }
367
368 void EVP_PKEY_free(EVP_PKEY *x)
369 {
370     int i;
371
372     if (x == NULL)
373         return;
374
375     CRYPTO_atomic_add(&x->references, -1, &i, x->lock);
376     REF_PRINT_COUNT("EVP_PKEY", x);
377     if (i > 0)
378         return;
379     REF_ASSERT_ISNT(i < 0);
380     EVP_PKEY_free_it(x);
381     sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
382     OPENSSL_free(x);
383 }
384
385 static void EVP_PKEY_free_it(EVP_PKEY *x)
386 {
387     /* internal function; x is never NULL */
388     if (x->ameth && x->ameth->pkey_free) {
389         x->ameth->pkey_free(x);
390         x->pkey.ptr = NULL;
391     }
392 #ifndef OPENSSL_NO_ENGINE
393     ENGINE_finish(x->engine);
394     x->engine = NULL;
395 #endif
396     CRYPTO_THREAD_lock_free(x->lock);
397 }
398
399 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
400                      const char *kstr)
401 {
402     BIO_indent(out, indent, 128);
403     BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
404                kstr, OBJ_nid2ln(pkey->type));
405     return 1;
406 }
407
408 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
409                           int indent, ASN1_PCTX *pctx)
410 {
411     if (pkey->ameth && pkey->ameth->pub_print)
412         return pkey->ameth->pub_print(out, pkey, indent, pctx);
413
414     return unsup_alg(out, pkey, indent, "Public Key");
415 }
416
417 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
418                            int indent, ASN1_PCTX *pctx)
419 {
420     if (pkey->ameth && pkey->ameth->priv_print)
421         return pkey->ameth->priv_print(out, pkey, indent, pctx);
422
423     return unsup_alg(out, pkey, indent, "Private Key");
424 }
425
426 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
427                           int indent, ASN1_PCTX *pctx)
428 {
429     if (pkey->ameth && pkey->ameth->param_print)
430         return pkey->ameth->param_print(out, pkey, indent, pctx);
431     return unsup_alg(out, pkey, indent, "Parameters");
432 }
433
434 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
435 {
436     if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
437         return -2;
438     return pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID,
439                                   0, pnid);
440 }