2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include "internal/cryptlib.h"
13 #include <openssl/x509.h>
14 #include <openssl/rand.h>
15 #include <openssl/encoder.h>
16 #include "internal/provider.h"
17 #include "crypto/asn1.h"
18 #include "crypto/evp.h"
19 #include "crypto/x509.h"
21 /* Extract a private key from a PKCS8 structure */
23 EVP_PKEY *EVP_PKCS82PKEY_ex(const PKCS8_PRIV_KEY_INFO *p8, OSSL_LIB_CTX *libctx,
26 EVP_PKEY *pkey = NULL;
27 const ASN1_OBJECT *algoid;
30 if (!PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8))
33 if ((pkey = EVP_PKEY_new()) == NULL) {
34 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
38 if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(algoid))) {
39 i2t_ASN1_OBJECT(obj_tmp, 80, algoid);
40 ERR_raise_data(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM,
45 if (pkey->ameth->priv_decode_ex != NULL) {
46 if (!pkey->ameth->priv_decode_ex(pkey, p8, libctx, propq))
48 } else if (pkey->ameth->priv_decode != NULL) {
49 if (!pkey->ameth->priv_decode(pkey, p8)) {
50 ERR_raise(ERR_LIB_EVP, EVP_R_PRIVATE_KEY_DECODE_ERROR);
54 ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED);
65 EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8)
67 return EVP_PKCS82PKEY_ex(p8, NULL, NULL);
70 /* Turn a private key into a PKCS8 structure */
72 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(const EVP_PKEY *pkey)
74 PKCS8_PRIV_KEY_INFO *p8 = NULL;
75 OSSL_ENCODER_CTX *ctx = NULL;
78 * The implementation for provider-native keys is to encode the
79 * key to a DER encoded PKCS#8 structure, then convert it to a
80 * PKCS8_PRIV_KEY_INFO with good old d2i functions.
82 if (evp_pkey_is_provided(pkey)) {
83 int selection = OSSL_KEYMGMT_SELECT_ALL;
84 unsigned char *der = NULL;
86 const unsigned char *pp;
88 if ((ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection,
91 || !OSSL_ENCODER_to_data(ctx, &der, &derlen))
95 p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &pp, (long)derlen);
100 p8 = PKCS8_PRIV_KEY_INFO_new();
102 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
106 if (pkey->ameth != NULL) {
107 if (pkey->ameth->priv_encode != NULL) {
108 if (!pkey->ameth->priv_encode(p8, pkey)) {
109 ERR_raise(ERR_LIB_EVP, EVP_R_PRIVATE_KEY_ENCODE_ERROR);
113 ERR_raise(ERR_LIB_EVP, EVP_R_METHOD_NOT_SUPPORTED);
117 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
123 PKCS8_PRIV_KEY_INFO_free(p8);
126 OSSL_ENCODER_CTX_free(ctx);
131 /* EVP_PKEY attribute functions */
133 int EVP_PKEY_get_attr_count(const EVP_PKEY *key)
135 return X509at_get_attr_count(key->attributes);
138 int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos)
140 return X509at_get_attr_by_NID(key->attributes, nid, lastpos);
143 int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, const ASN1_OBJECT *obj,
146 return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos);
149 X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc)
151 return X509at_get_attr(key->attributes, loc);
154 X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc)
156 return X509at_delete_attr(key->attributes, loc);
159 int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr)
161 if (X509at_add1_attr(&key->attributes, attr))
166 int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
167 const ASN1_OBJECT *obj, int type,
168 const unsigned char *bytes, int len)
170 if (X509at_add1_attr_by_OBJ(&key->attributes, obj, type, bytes, len))
175 int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
177 const unsigned char *bytes, int len)
179 if (X509at_add1_attr_by_NID(&key->attributes, nid, type, bytes, len))
184 int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
185 const char *attrname, int type,
186 const unsigned char *bytes, int len)
188 if (X509at_add1_attr_by_txt(&key->attributes, attrname, type, bytes, len))
193 const char *EVP_PKEY_get0_first_alg_name(const EVP_PKEY *key)
195 const EVP_PKEY_ASN1_METHOD *ameth;
196 const char *name = NULL;
198 if (key->keymgmt != NULL)
199 return EVP_KEYMGMT_get0_first_name(key->keymgmt);
201 /* Otherwise fallback to legacy */
202 ameth = EVP_PKEY_get0_asn1(key);
204 EVP_PKEY_asn1_get0_info(NULL, NULL,
205 NULL, NULL, &name, ameth);