rand: set up EVP and DRBG infrastructure for RAND from providers.
[openssl.git] / crypto / evp / evp_pkey.c
1 /*
2  * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 <stdlib.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/x509.h>
14 #include <openssl/rand.h>
15 #include "crypto/asn1.h"
16 #include "crypto/evp.h"
17 #include "crypto/x509.h"
18
19 /* Extract a private key from a PKCS8 structure */
20
21 EVP_PKEY *evp_pkcs82pkey_int(const PKCS8_PRIV_KEY_INFO *p8, OPENSSL_CTX *libctx,
22                              const char *propq)
23 {
24     EVP_PKEY *pkey = NULL;
25     const ASN1_OBJECT *algoid;
26     char obj_tmp[80];
27
28     if (!PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8))
29         return NULL;
30
31     if ((pkey = EVP_PKEY_new()) == NULL) {
32         EVPerr(0, ERR_R_MALLOC_FAILURE);
33         return NULL;
34     }
35
36     if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(algoid))) {
37         EVPerr(0, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
38         i2t_ASN1_OBJECT(obj_tmp, 80, algoid);
39         ERR_add_error_data(2, "TYPE=", obj_tmp);
40         goto error;
41     }
42
43     if (pkey->ameth->priv_decode_with_libctx != NULL) {
44         if (!pkey->ameth->priv_decode_with_libctx(pkey, p8, libctx, propq)) {
45             EVPerr(0, EVP_R_PRIVATE_KEY_DECODE_ERROR);
46             goto error;
47         }
48     } else if (pkey->ameth->priv_decode != NULL) {
49         if (!pkey->ameth->priv_decode(pkey, p8)) {
50             EVPerr(0, EVP_R_PRIVATE_KEY_DECODE_ERROR);
51             goto error;
52         }
53     } else {
54         EVPerr(0, EVP_R_METHOD_NOT_SUPPORTED);
55         goto error;
56     }
57
58     return pkey;
59
60  error:
61     EVP_PKEY_free(pkey);
62     return NULL;
63 }
64
65 EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8)
66 {
67     return evp_pkcs82pkey_int(p8, NULL, NULL);
68 }
69
70 /* Turn a private key into a PKCS8 structure */
71
72 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(const EVP_PKEY *pkey)
73 {
74     PKCS8_PRIV_KEY_INFO *p8 = PKCS8_PRIV_KEY_INFO_new();
75     if (p8  == NULL) {
76         EVPerr(EVP_F_EVP_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
77         return NULL;
78     }
79
80     if (pkey->ameth) {
81         if (pkey->ameth->priv_encode) {
82             if (!pkey->ameth->priv_encode(p8, pkey)) {
83                 EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_PRIVATE_KEY_ENCODE_ERROR);
84                 goto error;
85             }
86         } else {
87             EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_METHOD_NOT_SUPPORTED);
88             goto error;
89         }
90     } else {
91         EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
92         goto error;
93     }
94     return p8;
95  error:
96     PKCS8_PRIV_KEY_INFO_free(p8);
97     return NULL;
98 }
99
100 /* EVP_PKEY attribute functions */
101
102 int EVP_PKEY_get_attr_count(const EVP_PKEY *key)
103 {
104     return X509at_get_attr_count(key->attributes);
105 }
106
107 int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos)
108 {
109     return X509at_get_attr_by_NID(key->attributes, nid, lastpos);
110 }
111
112 int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, const ASN1_OBJECT *obj,
113                              int lastpos)
114 {
115     return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos);
116 }
117
118 X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc)
119 {
120     return X509at_get_attr(key->attributes, loc);
121 }
122
123 X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc)
124 {
125     return X509at_delete_attr(key->attributes, loc);
126 }
127
128 int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr)
129 {
130     if (X509at_add1_attr(&key->attributes, attr))
131         return 1;
132     return 0;
133 }
134
135 int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
136                               const ASN1_OBJECT *obj, int type,
137                               const unsigned char *bytes, int len)
138 {
139     if (X509at_add1_attr_by_OBJ(&key->attributes, obj, type, bytes, len))
140         return 1;
141     return 0;
142 }
143
144 int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
145                               int nid, int type,
146                               const unsigned char *bytes, int len)
147 {
148     if (X509at_add1_attr_by_NID(&key->attributes, nid, type, bytes, len))
149         return 1;
150     return 0;
151 }
152
153 int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
154                               const char *attrname, int type,
155                               const unsigned char *bytes, int len)
156 {
157     if (X509at_add1_attr_by_txt(&key->attributes, attrname, type, bytes, len))
158         return 1;
159     return 0;
160 }