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 "crypto/asn1.h"
16 #include "crypto/evp.h"
17 #include "crypto/x509.h"
19 /* Extract a private key from a PKCS8 structure */
21 EVP_PKEY *EVP_PKCS82PKEY_with_libctx(const PKCS8_PRIV_KEY_INFO *p8,
22 OPENSSL_CTX *libctx, const char *propq)
24 EVP_PKEY *pkey = NULL;
25 const ASN1_OBJECT *algoid;
28 if (!PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8))
31 if ((pkey = EVP_PKEY_new()) == NULL) {
32 EVPerr(0, ERR_R_MALLOC_FAILURE);
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);
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);
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);
54 EVPerr(0, EVP_R_METHOD_NOT_SUPPORTED);
65 EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8)
67 return EVP_PKCS82PKEY_with_libctx(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 = PKCS8_PRIV_KEY_INFO_new();
76 EVPerr(EVP_F_EVP_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
80 /* Force a key downgrade if that's possible */
81 /* TODO(3.0) Is there a better way for provider-native keys? */
82 if (EVP_PKEY_get0(pkey) == NULL)
86 if (pkey->ameth->priv_encode) {
87 if (!pkey->ameth->priv_encode(p8, pkey)) {
88 EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_PRIVATE_KEY_ENCODE_ERROR);
92 EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_METHOD_NOT_SUPPORTED);
96 EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
101 PKCS8_PRIV_KEY_INFO_free(p8);
105 /* EVP_PKEY attribute functions */
107 int EVP_PKEY_get_attr_count(const EVP_PKEY *key)
109 return X509at_get_attr_count(key->attributes);
112 int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos)
114 return X509at_get_attr_by_NID(key->attributes, nid, lastpos);
117 int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, const ASN1_OBJECT *obj,
120 return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos);
123 X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc)
125 return X509at_get_attr(key->attributes, loc);
128 X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc)
130 return X509at_delete_attr(key->attributes, loc);
133 int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr)
135 if (X509at_add1_attr(&key->attributes, attr))
140 int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
141 const ASN1_OBJECT *obj, int type,
142 const unsigned char *bytes, int len)
144 if (X509at_add1_attr_by_OBJ(&key->attributes, obj, type, bytes, len))
149 int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
151 const unsigned char *bytes, int len)
153 if (X509at_add1_attr_by_NID(&key->attributes, nid, type, bytes, len))
158 int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
159 const char *attrname, int type,
160 const unsigned char *bytes, int len)
162 if (X509at_add1_attr_by_txt(&key->attributes, attrname, type, bytes, len))