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