]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/evp_pkey.c
Copyright consolidation 05/10
[thirdparty/openssl.git] / crypto / evp / evp_pkey.c
CommitLineData
0f113f3e 1/*
aa6bb135 2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
cfcefcbe 3 *
aa6bb135
RS
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
cfcefcbe
DSH
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
b39fc560 12#include "internal/cryptlib.h"
ec577822
BM
13#include <openssl/x509.h>
14#include <openssl/rand.h>
5fe736e5 15#include "internal/asn1_int.h"
3aeb9348 16#include "internal/evp_int.h"
54dbf423 17#include "internal/x509_int.h"
cfcefcbe
DSH
18
19/* Extract a private key from a PKCS8 structure */
20
8afca8d9 21EVP_PKEY *EVP_PKCS82PKEY(PKCS8_PRIV_KEY_INFO *p8)
cfcefcbe 22{
0f113f3e
MC
23 EVP_PKEY *pkey = NULL;
24 ASN1_OBJECT *algoid;
25 char obj_tmp[80];
26
27 if (!PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8))
28 return NULL;
29
75ebbd9a 30 if ((pkey = EVP_PKEY_new()) == NULL) {
0f113f3e
MC
31 EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_MALLOC_FAILURE);
32 return NULL;
33 }
34
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);
39 goto error;
40 }
41
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);
45 goto error;
46 }
47 } else {
48 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_METHOD_NOT_SUPPORTED);
49 goto error;
50 }
51
52 return pkey;
53
54 error:
55 EVP_PKEY_free(pkey);
56 return NULL;
cfcefcbe
DSH
57}
58
59/* Turn a private key into a PKCS8 structure */
60
54dbf423 61PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey)
cfcefcbe 62{
54dbf423
DSH
63 PKCS8_PRIV_KEY_INFO *p8 = PKCS8_PRIV_KEY_INFO_new();
64 if (p8 == NULL) {
65 EVPerr(EVP_F_EVP_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
66 return NULL;
67 }
0f113f3e
MC
68
69 if (pkey->ameth) {
70 if (pkey->ameth->priv_encode) {
71 if (!pkey->ameth->priv_encode(p8, pkey)) {
54dbf423 72 EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_PRIVATE_KEY_ENCODE_ERROR);
0f113f3e
MC
73 goto error;
74 }
75 } else {
54dbf423 76 EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_METHOD_NOT_SUPPORTED);
0f113f3e
MC
77 goto error;
78 }
79 } else {
54dbf423 80 EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
0f113f3e
MC
81 goto error;
82 }
54dbf423 83 RAND_add(p8->pkey->data, p8->pkey->length, 0.0);
0f113f3e
MC
84 return p8;
85 error:
86 PKCS8_PRIV_KEY_INFO_free(p8);
87 return NULL;
cfcefcbe
DSH
88}
89
b6995add
DSH
90/* EVP_PKEY attribute functions */
91
92int EVP_PKEY_get_attr_count(const EVP_PKEY *key)
93{
0f113f3e 94 return X509at_get_attr_count(key->attributes);
b6995add
DSH
95}
96
0f113f3e 97int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos)
b6995add 98{
0f113f3e 99 return X509at_get_attr_by_NID(key->attributes, nid, lastpos);
b6995add
DSH
100}
101
102int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, ASN1_OBJECT *obj,
0f113f3e 103 int lastpos)
b6995add 104{
0f113f3e 105 return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos);
b6995add
DSH
106}
107
108X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc)
109{
0f113f3e 110 return X509at_get_attr(key->attributes, loc);
b6995add
DSH
111}
112
113X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc)
114{
0f113f3e 115 return X509at_delete_attr(key->attributes, loc);
b6995add
DSH
116}
117
118int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr)
119{
0f113f3e
MC
120 if (X509at_add1_attr(&key->attributes, attr))
121 return 1;
122 return 0;
b6995add
DSH
123}
124
125int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
0f113f3e
MC
126 const ASN1_OBJECT *obj, int type,
127 const unsigned char *bytes, int len)
b6995add 128{
0f113f3e
MC
129 if (X509at_add1_attr_by_OBJ(&key->attributes, obj, type, bytes, len))
130 return 1;
131 return 0;
b6995add
DSH
132}
133
134int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
0f113f3e
MC
135 int nid, int type,
136 const unsigned char *bytes, int len)
b6995add 137{
0f113f3e
MC
138 if (X509at_add1_attr_by_NID(&key->attributes, nid, type, bytes, len))
139 return 1;
140 return 0;
b6995add
DSH
141}
142
143int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
0f113f3e
MC
144 const char *attrname, int type,
145 const unsigned char *bytes, int len)
b6995add 146{
0f113f3e
MC
147 if (X509at_add1_attr_by_txt(&key->attributes, attrname, type, bytes, len))
148 return 1;
149 return 0;
b6995add 150}