2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include "internal/cryptlib.h"
13 #include <openssl/x509.h>
14 #include <openssl/rand.h>
15 #include "internal/asn1_int.h"
16 #include "internal/evp_int.h"
17 #include "internal/x509_int.h"
19 /* Extract a private key from a PKCS8 structure */
21 EVP_PKEY
*EVP_PKCS82PKEY(PKCS8_PRIV_KEY_INFO
*p8
)
23 EVP_PKEY
*pkey
= NULL
;
27 if (!PKCS8_pkey_get0(&algoid
, NULL
, NULL
, NULL
, p8
))
30 if ((pkey
= EVP_PKEY_new()) == NULL
) {
31 EVPerr(EVP_F_EVP_PKCS82PKEY
, ERR_R_MALLOC_FAILURE
);
35 if (!EVP_PKEY_set_type(pkey
, OBJ_obj2nid(algoid
))) {
36 EVPerr(EVP_F_EVP_PKCS82PKEY
, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM
);
37 i2t_ASN1_OBJECT(obj_tmp
, 80, algoid
);
38 ERR_add_error_data(2, "TYPE=", obj_tmp
);
42 if (pkey
->ameth
->priv_decode
) {
43 if (!pkey
->ameth
->priv_decode(pkey
, p8
)) {
44 EVPerr(EVP_F_EVP_PKCS82PKEY
, EVP_R_PRIVATE_KEY_DECODE_ERROR
);
48 EVPerr(EVP_F_EVP_PKCS82PKEY
, EVP_R_METHOD_NOT_SUPPORTED
);
59 /* Turn a private key into a PKCS8 structure */
61 PKCS8_PRIV_KEY_INFO
*EVP_PKEY2PKCS8(EVP_PKEY
*pkey
)
63 PKCS8_PRIV_KEY_INFO
*p8
= PKCS8_PRIV_KEY_INFO_new();
65 EVPerr(EVP_F_EVP_PKEY2PKCS8
, ERR_R_MALLOC_FAILURE
);
70 if (pkey
->ameth
->priv_encode
) {
71 if (!pkey
->ameth
->priv_encode(p8
, pkey
)) {
72 EVPerr(EVP_F_EVP_PKEY2PKCS8
, EVP_R_PRIVATE_KEY_ENCODE_ERROR
);
76 EVPerr(EVP_F_EVP_PKEY2PKCS8
, EVP_R_METHOD_NOT_SUPPORTED
);
80 EVPerr(EVP_F_EVP_PKEY2PKCS8
, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM
);
83 RAND_add(p8
->pkey
->data
, p8
->pkey
->length
, 0.0);
86 PKCS8_PRIV_KEY_INFO_free(p8
);
90 /* EVP_PKEY attribute functions */
92 int EVP_PKEY_get_attr_count(const EVP_PKEY
*key
)
94 return X509at_get_attr_count(key
->attributes
);
97 int EVP_PKEY_get_attr_by_NID(const EVP_PKEY
*key
, int nid
, int lastpos
)
99 return X509at_get_attr_by_NID(key
->attributes
, nid
, lastpos
);
102 int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY
*key
, ASN1_OBJECT
*obj
,
105 return X509at_get_attr_by_OBJ(key
->attributes
, obj
, lastpos
);
108 X509_ATTRIBUTE
*EVP_PKEY_get_attr(const EVP_PKEY
*key
, int loc
)
110 return X509at_get_attr(key
->attributes
, loc
);
113 X509_ATTRIBUTE
*EVP_PKEY_delete_attr(EVP_PKEY
*key
, int loc
)
115 return X509at_delete_attr(key
->attributes
, loc
);
118 int EVP_PKEY_add1_attr(EVP_PKEY
*key
, X509_ATTRIBUTE
*attr
)
120 if (X509at_add1_attr(&key
->attributes
, attr
))
125 int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY
*key
,
126 const ASN1_OBJECT
*obj
, int type
,
127 const unsigned char *bytes
, int len
)
129 if (X509at_add1_attr_by_OBJ(&key
->attributes
, obj
, type
, bytes
, len
))
134 int EVP_PKEY_add1_attr_by_NID(EVP_PKEY
*key
,
136 const unsigned char *bytes
, int len
)
138 if (X509at_add1_attr_by_NID(&key
->attributes
, nid
, type
, bytes
, len
))
143 int EVP_PKEY_add1_attr_by_txt(EVP_PKEY
*key
,
144 const char *attrname
, int type
,
145 const unsigned char *bytes
, int len
)
147 if (X509at_add1_attr_by_txt(&key
->attributes
, attrname
, type
, bytes
, len
))