]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/p_lib.c
Copyright consolidation 04/10
[thirdparty/openssl.git] / crypto / evp / p_lib.c
CommitLineData
62867571
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
62867571
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
4d94ae00
BM
12#include <openssl/bn.h>
13#include <openssl/err.h>
ec577822
BM
14#include <openssl/objects.h>
15#include <openssl/evp.h>
ec577822 16#include <openssl/x509.h>
3c27208f
RS
17#include <openssl/rsa.h>
18#include <openssl/dsa.h>
19#include <openssl/dh.h>
20#include <openssl/engine.h>
01b8b3c7 21
5fe736e5 22#include "internal/asn1_int.h"
3aeb9348 23#include "internal/evp_int.h"
18e377b4 24
d02b48c6 25static void EVP_PKEY_free_it(EVP_PKEY *x);
bb2297a4 26
6b691a5c 27int EVP_PKEY_bits(EVP_PKEY *pkey)
0f113f3e
MC
28{
29 if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
30 return pkey->ameth->pkey_bits(pkey);
31 return 0;
32}
58964a49 33
2514fa79 34int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
0f113f3e
MC
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}
2514fa79 42
6b691a5c 43int EVP_PKEY_size(EVP_PKEY *pkey)
0f113f3e
MC
44{
45 if (pkey && pkey->ameth && pkey->ameth->pkey_size)
46 return pkey->ameth->pkey_size(pkey);
47 return 0;
48}
d02b48c6 49
6b691a5c 50int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
0f113f3e 51{
cf1b7d96 52#ifndef OPENSSL_NO_DSA
0f113f3e
MC
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 }
4d94ae00 60#endif
5488bb61 61#ifndef OPENSSL_NO_EC
0f113f3e
MC
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 }
d02b48c6 69#endif
0f113f3e
MC
70 return (0);
71}
d02b48c6 72
a8b72844 73int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
0f113f3e 74{
2986ecdc
DSH
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) {
0f113f3e
MC
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}
d02b48c6 92
af0f0f3e 93int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
0f113f3e
MC
94{
95 if (pkey->ameth && pkey->ameth->param_missing)
96 return pkey->ameth->param_missing(pkey);
97 return 0;
98}
d02b48c6 99
af0f0f3e 100int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
0f113f3e
MC
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}
58964a49 108
af0f0f3e 109int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
0f113f3e
MC
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}
e6526fbf 129
6b691a5c 130EVP_PKEY *EVP_PKEY_new(void)
0f113f3e 131{
a2d0baa2 132 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e 133
0f113f3e
MC
134 if (ret == NULL) {
135 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
03273d61 136 return NULL;
0f113f3e
MC
137 }
138 ret->type = EVP_PKEY_NONE;
139 ret->save_type = EVP_PKEY_NONE;
140 ret->references = 1;
0f113f3e 141 ret->save_parameters = 1;
03273d61
AG
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;
0f113f3e
MC
149}
150
c5ebfcab 151int EVP_PKEY_up_ref(EVP_PKEY *pkey)
2872dbe1 152{
03273d61 153 int i;
c5ebfcab
F
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);
2872dbe1
DSH
161}
162
0f113f3e
MC
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.
01b8b3c7
DSH
166 */
167
168static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
0f113f3e
MC
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;
01b8b3c7 181#ifndef OPENSSL_NO_ENGINE
0f113f3e 182 /* If we have an ENGINE release it */
7c96dbcd
RS
183 ENGINE_finish(pkey->engine);
184 pkey->engine = NULL;
01b8b3c7 185#endif
0f113f3e
MC
186 }
187 if (str)
188 ameth = EVP_PKEY_asn1_find_str(&e, str, len);
189 else
190 ameth = EVP_PKEY_asn1_find(&e, type);
01b8b3c7 191#ifndef OPENSSL_NO_ENGINE
7c96dbcd 192 if (pkey == NULL)
0f113f3e 193 ENGINE_finish(e);
01b8b3c7 194#endif
7c96dbcd 195 if (ameth == NULL) {
0f113f3e
MC
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}
01b8b3c7
DSH
208
209int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
0f113f3e
MC
210{
211 return pkey_set_type(pkey, type, NULL, -1);
212}
01b8b3c7
DSH
213
214int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
0f113f3e
MC
215{
216 return pkey_set_type(pkey, EVP_PKEY_NONE, str, len);
217}
01b8b3c7
DSH
218
219int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
0f113f3e 220{
e34c66c6 221 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
0f113f3e
MC
222 return 0;
223 pkey->pkey.ptr = key;
224 return (key != NULL);
225}
d02b48c6 226
3aeb9348 227void *EVP_PKEY_get0(const EVP_PKEY *pkey)
0f113f3e
MC
228{
229 return pkey->pkey.ptr;
230}
db98bbc1 231
cf1b7d96 232#ifndef OPENSSL_NO_RSA
c7cb16a8 233int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
52664f50 234{
0f113f3e
MC
235 int ret = EVP_PKEY_assign_RSA(pkey, key);
236 if (ret)
237 RSA_up_ref(key);
238 return ret;
52664f50
DSH
239}
240
2872dbe1 241RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
0f113f3e
MC
242{
243 if (pkey->type != EVP_PKEY_RSA) {
2872dbe1 244 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
0f113f3e
MC
245 return NULL;
246 }
0f113f3e 247 return pkey->pkey.rsa;
f769ce3e 248}
2872dbe1
DSH
249
250RSA *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}
f769ce3e
DSH
257#endif
258
cf1b7d96 259#ifndef OPENSSL_NO_DSA
c7cb16a8 260int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
52664f50 261{
0f113f3e
MC
262 int ret = EVP_PKEY_assign_DSA(pkey, key);
263 if (ret)
264 DSA_up_ref(key);
265 return ret;
52664f50
DSH
266}
267
2872dbe1 268DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
0f113f3e
MC
269{
270 if (pkey->type != EVP_PKEY_DSA) {
2872dbe1 271 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
0f113f3e
MC
272 return NULL;
273 }
0f113f3e 274 return pkey->pkey.dsa;
f769ce3e 275}
2872dbe1
DSH
276
277DSA *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}
f769ce3e
DSH
284#endif
285
14a7cfb3 286#ifndef OPENSSL_NO_EC
4d94ae00 287
14a7cfb3 288int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
4d94ae00 289{
0f113f3e
MC
290 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
291 if (ret)
292 EC_KEY_up_ref(key);
293 return ret;
4d94ae00
BM
294}
295
2872dbe1 296EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
4d94ae00 297{
0f113f3e 298 if (pkey->type != EVP_PKEY_EC) {
2872dbe1 299 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
0f113f3e
MC
300 return NULL;
301 }
0f113f3e 302 return pkey->pkey.ec;
4d94ae00 303}
2872dbe1
DSH
304
305EC_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}
4d94ae00
BM
312#endif
313
cf1b7d96 314#ifndef OPENSSL_NO_DH
52664f50 315
c7cb16a8 316int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
52664f50 317{
0f113f3e
MC
318 int ret = EVP_PKEY_assign_DH(pkey, key);
319 if (ret)
320 DH_up_ref(key);
321 return ret;
52664f50
DSH
322}
323
2872dbe1 324DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey)
0f113f3e
MC
325{
326 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
2872dbe1 327 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
0f113f3e
MC
328 return NULL;
329 }
0f113f3e 330 return pkey->pkey.dh;
f769ce3e 331}
2872dbe1
DSH
332
333DH *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}
f769ce3e
DSH
340#endif
341
6b691a5c 342int EVP_PKEY_type(int type)
0f113f3e
MC
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;
01b8b3c7 352#ifndef OPENSSL_NO_ENGINE
7c96dbcd 353 ENGINE_finish(e);
01b8b3c7 354#endif
0f113f3e
MC
355 return ret;
356}
d02b48c6 357
7f57b076 358int EVP_PKEY_id(const EVP_PKEY *pkey)
0f113f3e
MC
359{
360 return pkey->type;
361}
7f57b076
DSH
362
363int EVP_PKEY_base_id(const EVP_PKEY *pkey)
0f113f3e
MC
364{
365 return EVP_PKEY_type(pkey->type);
366}
7f57b076 367
6b691a5c 368void EVP_PKEY_free(EVP_PKEY *x)
0f113f3e
MC
369{
370 int i;
d02b48c6 371
0f113f3e
MC
372 if (x == NULL)
373 return;
d02b48c6 374
03273d61 375 CRYPTO_atomic_add(&x->references, -1, &i, x->lock);
f3f1cf84 376 REF_PRINT_COUNT("EVP_PKEY", x);
0f113f3e
MC
377 if (i > 0)
378 return;
f3f1cf84 379 REF_ASSERT_ISNT(i < 0);
0f113f3e 380 EVP_PKEY_free_it(x);
222561fe 381 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
0f113f3e
MC
382 OPENSSL_free(x);
383}
d02b48c6 384
6b691a5c 385static void EVP_PKEY_free_it(EVP_PKEY *x)
0f113f3e 386{
c5ba2d99 387 /* internal function; x is never NULL */
0f113f3e
MC
388 if (x->ameth && x->ameth->pkey_free) {
389 x->ameth->pkey_free(x);
390 x->pkey.ptr = NULL;
391 }
01b8b3c7 392#ifndef OPENSSL_NO_ENGINE
7c96dbcd
RS
393 ENGINE_finish(x->engine);
394 x->engine = NULL;
01b8b3c7 395#endif
03273d61 396 CRYPTO_THREAD_lock_free(x->lock);
0f113f3e 397}
d02b48c6 398
35208f36 399static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
0f113f3e
MC
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}
35208f36
DSH
407
408int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
0f113f3e
MC
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}
35208f36
DSH
416
417int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
0f113f3e
MC
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}
35208f36
DSH
425
426int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
0f113f3e
MC
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}
03919683
DSH
433
434int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
0f113f3e
MC
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}