]>
Commit | Line | Data |
---|---|---|
62867571 | 1 | /* |
6ec5fce2 | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
1241126a | 3 | * |
16742672 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
62867571 RS |
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 | |
1241126a DSH |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
1241126a DSH |
12 | #include <openssl/buffer.h> |
13 | #include <openssl/objects.h> | |
14 | #include <openssl/evp.h> | |
1241126a DSH |
15 | #include <openssl/x509.h> |
16 | #include <openssl/pkcs12.h> | |
17 | #include <openssl/pem.h> | |
3c27208f RS |
18 | #include <openssl/engine.h> |
19 | #include <openssl/dh.h> | |
1427d33c RL |
20 | #include <openssl/store.h> |
21 | #include <openssl/ui.h> | |
f864a939 | 22 | #include <openssl/serializer.h> |
1427d33c | 23 | #include "crypto/store.h" |
25f2138b DMSP |
24 | #include "crypto/asn1.h" |
25 | #include "crypto/evp.h" | |
f864a939 | 26 | #include "pem_local.h" |
1241126a | 27 | |
e4263314 | 28 | int pem_check_suffix(const char *pem_str, const char *suffix); |
1241126a | 29 | |
0f113f3e MC |
30 | EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, |
31 | void *u) | |
32 | { | |
0f113f3e | 33 | EVP_PKEY *ret = NULL; |
1427d33c RL |
34 | OSSL_STORE_CTX *ctx = NULL; |
35 | OSSL_STORE_INFO *info = NULL; | |
36 | UI_METHOD *ui_method = NULL; | |
1241126a | 37 | |
1427d33c | 38 | if ((ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) == NULL) |
0f113f3e | 39 | return NULL; |
1427d33c RL |
40 | |
41 | if ((ctx = ossl_store_attach_pem_bio(bp, ui_method, u)) == NULL) | |
42 | goto err; | |
43 | #ifndef OPENSSL_NO_SECURE_HEAP | |
44 | { | |
45 | int on = 1; | |
46 | if (!OSSL_STORE_ctrl(ctx, OSSL_STORE_C_USE_SECMEM, &on)) | |
0f113f3e | 47 | goto err; |
1427d33c RL |
48 | } |
49 | #endif | |
50 | ||
51 | while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) { | |
52 | if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PKEY) { | |
53 | ret = OSSL_STORE_INFO_get1_PKEY(info); | |
54 | break; | |
0f113f3e | 55 | } |
1427d33c | 56 | OSSL_STORE_INFO_free(info); |
0f113f3e | 57 | } |
1427d33c RL |
58 | |
59 | if (ret != NULL && x != NULL) | |
60 | *x = ret; | |
61 | ||
0f113f3e | 62 | err: |
1427d33c RL |
63 | ossl_store_detach_pem_bio(ctx); |
64 | UI_destroy_method(ui_method); | |
65 | OSSL_STORE_INFO_free(info); | |
26a7d938 | 66 | return ret; |
0f113f3e | 67 | } |
1241126a | 68 | |
f864a939 | 69 | PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio) |
0f113f3e | 70 | { |
f864a939 RL |
71 | IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, PrivateKey); |
72 | ||
73 | IMPLEMENT_PEM_provided_write_body_pass(); | |
74 | IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio); | |
75 | ||
76 | legacy: | |
05dba815 | 77 | if (x->ameth == NULL || x->ameth->priv_encode != NULL) |
f864a939 | 78 | return PEM_write_bio_PKCS8PrivateKey(out, x, enc, |
de0799b0 | 79 | (const char *)kstr, klen, cb, u); |
f864a939 | 80 | return PEM_write_bio_PrivateKey_traditional(out, x, enc, kstr, klen, cb, u); |
05dba815 | 81 | } |
e4263314 | 82 | |
de0799b0 | 83 | int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x, |
05dba815 | 84 | const EVP_CIPHER *enc, |
de0799b0 | 85 | const unsigned char *kstr, int klen, |
05dba815 DSH |
86 | pem_password_cb *cb, void *u) |
87 | { | |
88 | char pem_str[80]; | |
0f113f3e MC |
89 | BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str); |
90 | return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey, | |
91 | pem_str, bp, x, enc, kstr, klen, cb, u); | |
92 | } | |
e4263314 | 93 | |
3e4585c8 | 94 | EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x) |
0f113f3e | 95 | { |
0f113f3e | 96 | EVP_PKEY *ret = NULL; |
1427d33c RL |
97 | OSSL_STORE_CTX *ctx = NULL; |
98 | OSSL_STORE_INFO *info = NULL; | |
db98bbc1 | 99 | |
1427d33c RL |
100 | if ((ctx = ossl_store_attach_pem_bio(bp, UI_null(), NULL)) == NULL) |
101 | goto err; | |
db98bbc1 | 102 | |
1427d33c RL |
103 | while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) { |
104 | if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) { | |
105 | ret = OSSL_STORE_INFO_get1_PARAMS(info); | |
106 | break; | |
0f113f3e | 107 | } |
1427d33c | 108 | OSSL_STORE_INFO_free(info); |
0f113f3e | 109 | } |
1427d33c RL |
110 | |
111 | if (ret != NULL && x != NULL) | |
112 | *x = ret; | |
113 | ||
0f113f3e | 114 | err: |
1427d33c RL |
115 | ossl_store_detach_pem_bio(ctx); |
116 | OSSL_STORE_INFO_free(info); | |
26a7d938 | 117 | return ret; |
0f113f3e | 118 | } |
db98bbc1 | 119 | |
f864a939 | 120 | PEM_write_fnsig(Parameters, EVP_PKEY, BIO, write_bio) |
0f113f3e MC |
121 | { |
122 | char pem_str[80]; | |
f864a939 RL |
123 | IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, Parameters); |
124 | ||
125 | IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio); | |
126 | ||
127 | legacy: | |
0f113f3e MC |
128 | if (!x->ameth || !x->ameth->param_encode) |
129 | return 0; | |
db98bbc1 | 130 | |
0f113f3e MC |
131 | BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str); |
132 | return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode, | |
f864a939 | 133 | pem_str, out, x, NULL, NULL, 0, 0, NULL); |
0f113f3e | 134 | } |
e4263314 | 135 | |
4b618848 | 136 | #ifndef OPENSSL_NO_STDIO |
0f113f3e MC |
137 | EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, |
138 | void *u) | |
139 | { | |
140 | BIO *b; | |
141 | EVP_PKEY *ret; | |
1241126a | 142 | |
0f113f3e MC |
143 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
144 | PEMerr(PEM_F_PEM_READ_PRIVATEKEY, ERR_R_BUF_LIB); | |
26a7d938 | 145 | return 0; |
0f113f3e MC |
146 | } |
147 | BIO_set_fp(b, fp, BIO_NOCLOSE); | |
148 | ret = PEM_read_bio_PrivateKey(b, x, cb, u); | |
149 | BIO_free(b); | |
26a7d938 | 150 | return ret; |
0f113f3e | 151 | } |
e4263314 | 152 | |
de0799b0 RL |
153 | int PEM_write_PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc, |
154 | const unsigned char *kstr, int klen, | |
0f113f3e MC |
155 | pem_password_cb *cb, void *u) |
156 | { | |
157 | BIO *b; | |
158 | int ret; | |
e4263314 | 159 | |
0f113f3e MC |
160 | if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) { |
161 | PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY, ERR_R_BUF_LIB); | |
162 | return 0; | |
163 | } | |
164 | ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u); | |
165 | BIO_free(b); | |
166 | return ret; | |
167 | } | |
e4263314 | 168 | |
1241126a | 169 | #endif |
2ca873e8 DSH |
170 | |
171 | #ifndef OPENSSL_NO_DH | |
172 | ||
173 | /* Transparently read in PKCS#3 or X9.42 DH parameters */ | |
174 | ||
175 | DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u) | |
0f113f3e | 176 | { |
0f113f3e | 177 | DH *ret = NULL; |
1427d33c RL |
178 | EVP_PKEY *pkey = NULL; |
179 | OSSL_STORE_CTX *ctx = NULL; | |
180 | OSSL_STORE_INFO *info = NULL; | |
181 | UI_METHOD *ui_method = NULL; | |
2ca873e8 | 182 | |
1427d33c | 183 | if ((ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) == NULL) |
0f113f3e | 184 | return NULL; |
2ca873e8 | 185 | |
1427d33c RL |
186 | if ((ctx = ossl_store_attach_pem_bio(bp, ui_method, u)) == NULL) |
187 | goto err; | |
188 | ||
189 | while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) { | |
190 | if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) { | |
191 | pkey = OSSL_STORE_INFO_get0_PARAMS(info); | |
192 | if (EVP_PKEY_id(pkey) == EVP_PKEY_DHX | |
193 | || EVP_PKEY_id(pkey) == EVP_PKEY_DH) { | |
194 | ret = EVP_PKEY_get1_DH(pkey); | |
195 | break; | |
196 | } | |
197 | } | |
198 | OSSL_STORE_INFO_free(info); | |
199 | } | |
2ca873e8 | 200 | |
1427d33c RL |
201 | if (ret != NULL && x != NULL) |
202 | *x = ret; | |
203 | ||
204 | err: | |
205 | ossl_store_detach_pem_bio(ctx); | |
206 | UI_destroy_method(ui_method); | |
207 | OSSL_STORE_INFO_free(info); | |
0f113f3e MC |
208 | return ret; |
209 | } | |
2ca873e8 | 210 | |
0f113f3e | 211 | # ifndef OPENSSL_NO_STDIO |
2ca873e8 | 212 | DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u) |
0f113f3e MC |
213 | { |
214 | BIO *b; | |
215 | DH *ret; | |
2ca873e8 | 216 | |
0f113f3e MC |
217 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
218 | PEMerr(PEM_F_PEM_READ_DHPARAMS, ERR_R_BUF_LIB); | |
26a7d938 | 219 | return 0; |
0f113f3e MC |
220 | } |
221 | BIO_set_fp(b, fp, BIO_NOCLOSE); | |
222 | ret = PEM_read_bio_DHparams(b, x, cb, u); | |
223 | BIO_free(b); | |
26a7d938 | 224 | return ret; |
0f113f3e MC |
225 | } |
226 | # endif | |
2ca873e8 DSH |
227 | |
228 | #endif |