]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/pem/pem_pkey.c
s390x assembly pack: fix bn_mul_comba4
[thirdparty/openssl.git] / crypto / pem / pem_pkey.c
CommitLineData
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>
22#include "crypto/store.h"
25f2138b
DMSP
23#include "crypto/asn1.h"
24#include "crypto/evp.h"
1241126a 25
e4263314 26int pem_check_suffix(const char *pem_str, const char *suffix);
1241126a 27
0f113f3e
MC
28EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
29 void *u)
30{
0f113f3e 31 EVP_PKEY *ret = NULL;
1427d33c
RL
32 OSSL_STORE_CTX *ctx = NULL;
33 OSSL_STORE_INFO *info = NULL;
34 UI_METHOD *ui_method = NULL;
1241126a 35
1427d33c 36 if ((ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) == NULL)
0f113f3e 37 return NULL;
1427d33c
RL
38
39 if ((ctx = ossl_store_attach_pem_bio(bp, ui_method, u)) == NULL)
40 goto err;
41#ifndef OPENSSL_NO_SECURE_HEAP
42 {
43 int on = 1;
44 if (!OSSL_STORE_ctrl(ctx, OSSL_STORE_C_USE_SECMEM, &on))
0f113f3e 45 goto err;
1427d33c
RL
46 }
47#endif
48
49 while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) {
50 if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PKEY) {
51 ret = OSSL_STORE_INFO_get1_PKEY(info);
52 break;
0f113f3e 53 }
1427d33c 54 OSSL_STORE_INFO_free(info);
0f113f3e 55 }
1427d33c
RL
56
57 if (ret != NULL && x != NULL)
58 *x = ret;
59
0f113f3e 60 err:
1427d33c
RL
61 ossl_store_detach_pem_bio(ctx);
62 UI_destroy_method(ui_method);
63 OSSL_STORE_INFO_free(info);
26a7d938 64 return ret;
0f113f3e 65}
1241126a 66
e4263314 67int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
0f113f3e
MC
68 unsigned char *kstr, int klen,
69 pem_password_cb *cb, void *u)
70{
05dba815 71 if (x->ameth == NULL || x->ameth->priv_encode != NULL)
0f113f3e
MC
72 return PEM_write_bio_PKCS8PrivateKey(bp, x, enc,
73 (char *)kstr, klen, cb, u);
05dba815
DSH
74 return PEM_write_bio_PrivateKey_traditional(bp, x, enc, kstr, klen, cb, u);
75}
e4263314 76
05dba815
DSH
77int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x,
78 const EVP_CIPHER *enc,
79 unsigned char *kstr, int klen,
80 pem_password_cb *cb, void *u)
81{
82 char pem_str[80];
0f113f3e
MC
83 BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
84 return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
85 pem_str, bp, x, enc, kstr, klen, cb, u);
86}
e4263314 87
3e4585c8 88EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
0f113f3e 89{
0f113f3e 90 EVP_PKEY *ret = NULL;
1427d33c
RL
91 OSSL_STORE_CTX *ctx = NULL;
92 OSSL_STORE_INFO *info = NULL;
db98bbc1 93
1427d33c
RL
94 if ((ctx = ossl_store_attach_pem_bio(bp, UI_null(), NULL)) == NULL)
95 goto err;
db98bbc1 96
1427d33c
RL
97 while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) {
98 if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
99 ret = OSSL_STORE_INFO_get1_PARAMS(info);
100 break;
0f113f3e 101 }
1427d33c 102 OSSL_STORE_INFO_free(info);
0f113f3e 103 }
1427d33c
RL
104
105 if (ret != NULL && x != NULL)
106 *x = ret;
107
0f113f3e 108 err:
1427d33c
RL
109 ossl_store_detach_pem_bio(ctx);
110 OSSL_STORE_INFO_free(info);
26a7d938 111 return ret;
0f113f3e 112}
db98bbc1 113
3e4585c8 114int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x)
0f113f3e
MC
115{
116 char pem_str[80];
117 if (!x->ameth || !x->ameth->param_encode)
118 return 0;
db98bbc1 119
0f113f3e
MC
120 BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
121 return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode,
122 pem_str, bp, x, NULL, NULL, 0, 0, NULL);
123}
e4263314 124
4b618848 125#ifndef OPENSSL_NO_STDIO
0f113f3e
MC
126EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
127 void *u)
128{
129 BIO *b;
130 EVP_PKEY *ret;
1241126a 131
0f113f3e
MC
132 if ((b = BIO_new(BIO_s_file())) == NULL) {
133 PEMerr(PEM_F_PEM_READ_PRIVATEKEY, ERR_R_BUF_LIB);
26a7d938 134 return 0;
0f113f3e
MC
135 }
136 BIO_set_fp(b, fp, BIO_NOCLOSE);
137 ret = PEM_read_bio_PrivateKey(b, x, cb, u);
138 BIO_free(b);
26a7d938 139 return ret;
0f113f3e 140}
e4263314
DSH
141
142int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
0f113f3e
MC
143 unsigned char *kstr, int klen,
144 pem_password_cb *cb, void *u)
145{
146 BIO *b;
147 int ret;
e4263314 148
0f113f3e
MC
149 if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
150 PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY, ERR_R_BUF_LIB);
151 return 0;
152 }
153 ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
154 BIO_free(b);
155 return ret;
156}
e4263314 157
1241126a 158#endif
2ca873e8
DSH
159
160#ifndef OPENSSL_NO_DH
161
162/* Transparently read in PKCS#3 or X9.42 DH parameters */
163
164DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u)
0f113f3e 165{
0f113f3e 166 DH *ret = NULL;
1427d33c
RL
167 EVP_PKEY *pkey = NULL;
168 OSSL_STORE_CTX *ctx = NULL;
169 OSSL_STORE_INFO *info = NULL;
170 UI_METHOD *ui_method = NULL;
2ca873e8 171
1427d33c 172 if ((ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) == NULL)
0f113f3e 173 return NULL;
2ca873e8 174
1427d33c
RL
175 if ((ctx = ossl_store_attach_pem_bio(bp, ui_method, u)) == NULL)
176 goto err;
177
178 while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) {
179 if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
180 pkey = OSSL_STORE_INFO_get0_PARAMS(info);
181 if (EVP_PKEY_id(pkey) == EVP_PKEY_DHX
182 || EVP_PKEY_id(pkey) == EVP_PKEY_DH) {
183 ret = EVP_PKEY_get1_DH(pkey);
184 break;
185 }
186 }
187 OSSL_STORE_INFO_free(info);
188 }
2ca873e8 189
1427d33c
RL
190 if (ret != NULL && x != NULL)
191 *x = ret;
192
193 err:
194 ossl_store_detach_pem_bio(ctx);
195 UI_destroy_method(ui_method);
196 OSSL_STORE_INFO_free(info);
0f113f3e
MC
197 return ret;
198}
2ca873e8 199
0f113f3e 200# ifndef OPENSSL_NO_STDIO
2ca873e8 201DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u)
0f113f3e
MC
202{
203 BIO *b;
204 DH *ret;
2ca873e8 205
0f113f3e
MC
206 if ((b = BIO_new(BIO_s_file())) == NULL) {
207 PEMerr(PEM_F_PEM_READ_DHPARAMS, ERR_R_BUF_LIB);
26a7d938 208 return 0;
0f113f3e
MC
209 }
210 BIO_set_fp(b, fp, BIO_NOCLOSE);
211 ret = PEM_read_bio_DHparams(b, x, cb, u);
212 BIO_free(b);
26a7d938 213 return ret;
0f113f3e
MC
214}
215# endif
2ca873e8
DSH
216
217#endif