]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/pem/pem_info.c
Drop OPENSSL_NO_RSA everywhere
[thirdparty/openssl.git] / crypto / pem / pem_info.c
CommitLineData
62867571 1/*
33388b44 2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 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
d02b48c6
RE
8 */
9
f41ac0ee
P
10/*
11 * DSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
d02b48c6 16#include <stdio.h>
b39fc560 17#include "internal/cryptlib.h"
ec577822
BM
18#include <openssl/buffer.h>
19#include <openssl/objects.h>
20#include <openssl/evp.h>
21#include <openssl/x509.h>
22#include <openssl/pem.h>
3c27208f
RS
23#include <openssl/rsa.h>
24#include <openssl/dsa.h>
d02b48c6 25
4b618848 26#ifndef OPENSSL_NO_STDIO
6725682d 27STACK_OF(X509_INFO)
d8652be0 28*PEM_X509_INFO_read_ex(FILE *fp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb,
b4250010 29 void *u, OSSL_LIB_CTX *libctx, const char *propq)
0f113f3e
MC
30{
31 BIO *b;
32 STACK_OF(X509_INFO) *ret;
d02b48c6 33
0f113f3e 34 if ((b = BIO_new(BIO_s_file())) == NULL) {
9311d0c4 35 ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
26a7d938 36 return 0;
0f113f3e
MC
37 }
38 BIO_set_fp(b, fp, BIO_NOCLOSE);
d8652be0 39 ret = PEM_X509_INFO_read_bio_ex(b, sk, cb, u, libctx, propq);
0f113f3e 40 BIO_free(b);
26a7d938 41 return ret;
0f113f3e 42}
6725682d
SL
43
44STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk,
45 pem_password_cb *cb, void *u)
46{
d8652be0 47 return PEM_X509_INFO_read_ex(fp, sk, cb, u, NULL, NULL);
6725682d 48}
d02b48c6
RE
49#endif
50
6725682d 51STACK_OF(X509_INFO)
d8652be0 52*PEM_X509_INFO_read_bio_ex(BIO *bp, STACK_OF(X509_INFO) *sk,
b4250010 53 pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx,
d8652be0 54 const char *propq)
0f113f3e
MC
55{
56 X509_INFO *xi = NULL;
57 char *name = NULL, *header = NULL;
58 void *pp;
59 unsigned char *data = NULL;
60 const unsigned char *p;
61 long len, error = 0;
62 int ok = 0;
63 STACK_OF(X509_INFO) *ret = NULL;
64 unsigned int i, raw, ptype;
65 d2i_of_void *d2i = 0;
d02b48c6 66
0f113f3e
MC
67 if (sk == NULL) {
68 if ((ret = sk_X509_INFO_new_null()) == NULL) {
9311d0c4 69 ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
70 goto err;
71 }
72 } else
73 ret = sk;
d02b48c6 74
0f113f3e
MC
75 if ((xi = X509_INFO_new()) == NULL)
76 goto err;
77 for (;;) {
78 raw = 0;
79 ptype = 0;
80 i = PEM_read_bio(bp, &name, &header, &data, &len);
81 if (i == 0) {
82 error = ERR_GET_REASON(ERR_peek_last_error());
83 if (error == PEM_R_NO_START_LINE) {
84 ERR_clear_error();
85 break;
86 }
87 goto err;
88 }
89 start:
90 if ((strcmp(name, PEM_STRING_X509) == 0) ||
91 (strcmp(name, PEM_STRING_X509_OLD) == 0)) {
92 d2i = (D2I_OF(void)) d2i_X509;
93 if (xi->x509 != NULL) {
94 if (!sk_X509_INFO_push(ret, xi))
95 goto err;
96 if ((xi = X509_INFO_new()) == NULL)
97 goto err;
98 goto start;
99 }
d8652be0 100 xi->x509 = X509_new_ex(libctx, propq);
6725682d
SL
101 if (xi->x509 == NULL)
102 goto err;
0f113f3e
MC
103 pp = &(xi->x509);
104 } else if ((strcmp(name, PEM_STRING_X509_TRUSTED) == 0)) {
105 d2i = (D2I_OF(void)) d2i_X509_AUX;
106 if (xi->x509 != NULL) {
107 if (!sk_X509_INFO_push(ret, xi))
108 goto err;
109 if ((xi = X509_INFO_new()) == NULL)
110 goto err;
111 goto start;
112 }
d8652be0 113 xi->x509 = X509_new_ex(libctx, propq);
6725682d
SL
114 if (xi->x509 == NULL)
115 goto err;
0f113f3e
MC
116 pp = &(xi->x509);
117 } else if (strcmp(name, PEM_STRING_X509_CRL) == 0) {
118 d2i = (D2I_OF(void)) d2i_X509_CRL;
119 if (xi->crl != NULL) {
120 if (!sk_X509_INFO_push(ret, xi))
121 goto err;
122 if ((xi = X509_INFO_new()) == NULL)
123 goto err;
124 goto start;
125 }
126 pp = &(xi->crl);
3a1ee3c1 127 } else if (strcmp(name, PEM_STRING_RSA) == 0) {
0f113f3e
MC
128 d2i = (D2I_OF(void)) d2i_RSAPrivateKey;
129 if (xi->x_pkey != NULL) {
130 if (!sk_X509_INFO_push(ret, xi))
131 goto err;
132 if ((xi = X509_INFO_new()) == NULL)
133 goto err;
134 goto start;
135 }
d02b48c6 136
0f113f3e
MC
137 xi->enc_data = NULL;
138 xi->enc_len = 0;
d02b48c6 139
0f113f3e 140 xi->x_pkey = X509_PKEY_new();
90945fa3
MC
141 if (xi->x_pkey == NULL)
142 goto err;
0f113f3e
MC
143 ptype = EVP_PKEY_RSA;
144 pp = &xi->x_pkey->dec_pkey;
145 if ((int)strlen(header) > 10) /* assume encrypted */
146 raw = 1;
147 } else
cf1b7d96 148#ifndef OPENSSL_NO_DSA
0f113f3e
MC
149 if (strcmp(name, PEM_STRING_DSA) == 0) {
150 d2i = (D2I_OF(void)) d2i_DSAPrivateKey;
151 if (xi->x_pkey != NULL) {
152 if (!sk_X509_INFO_push(ret, xi))
153 goto err;
154 if ((xi = X509_INFO_new()) == NULL)
155 goto err;
156 goto start;
157 }
d02b48c6 158
0f113f3e
MC
159 xi->enc_data = NULL;
160 xi->enc_len = 0;
d02b48c6 161
0f113f3e 162 xi->x_pkey = X509_PKEY_new();
90945fa3
MC
163 if (xi->x_pkey == NULL)
164 goto err;
0f113f3e
MC
165 ptype = EVP_PKEY_DSA;
166 pp = &xi->x_pkey->dec_pkey;
167 if ((int)strlen(header) > 10) /* assume encrypted */
168 raw = 1;
169 } else
4d94ae00 170#endif
14a7cfb3 171#ifndef OPENSSL_NO_EC
0f113f3e
MC
172 if (strcmp(name, PEM_STRING_ECPRIVATEKEY) == 0) {
173 d2i = (D2I_OF(void)) d2i_ECPrivateKey;
174 if (xi->x_pkey != NULL) {
175 if (!sk_X509_INFO_push(ret, xi))
176 goto err;
177 if ((xi = X509_INFO_new()) == NULL)
178 goto err;
179 goto start;
180 }
181
182 xi->enc_data = NULL;
183 xi->enc_len = 0;
184
185 xi->x_pkey = X509_PKEY_new();
90945fa3
MC
186 if (xi->x_pkey == NULL)
187 goto err;
0f113f3e
MC
188 ptype = EVP_PKEY_EC;
189 pp = &xi->x_pkey->dec_pkey;
190 if ((int)strlen(header) > 10) /* assume encrypted */
191 raw = 1;
192 } else
d02b48c6 193#endif
0f113f3e
MC
194 {
195 d2i = NULL;
196 pp = NULL;
197 }
d02b48c6 198
0f113f3e
MC
199 if (d2i != NULL) {
200 if (!raw) {
201 EVP_CIPHER_INFO cipher;
d02b48c6 202
0f113f3e
MC
203 if (!PEM_get_EVP_CIPHER_INFO(header, &cipher))
204 goto err;
205 if (!PEM_do_header(&cipher, data, &len, cb, u))
206 goto err;
207 p = data;
208 if (ptype) {
209 if (!d2i_PrivateKey(ptype, pp, &p, len)) {
9311d0c4 210 ERR_raise(ERR_LIB_PEM, ERR_R_ASN1_LIB);
0f113f3e
MC
211 goto err;
212 }
213 } else if (d2i(pp, &p, len) == NULL) {
9311d0c4 214 ERR_raise(ERR_LIB_PEM, ERR_R_ASN1_LIB);
0f113f3e
MC
215 goto err;
216 }
217 } else { /* encrypted RSA data */
218 if (!PEM_get_EVP_CIPHER_INFO(header, &xi->enc_cipher))
219 goto err;
220 xi->enc_data = (char *)data;
221 xi->enc_len = (int)len;
222 data = NULL;
223 }
224 } else {
225 /* unknown */
226 }
b548a1f1 227 OPENSSL_free(name);
0f113f3e 228 name = NULL;
b548a1f1 229 OPENSSL_free(header);
0f113f3e 230 header = NULL;
b548a1f1 231 OPENSSL_free(data);
0f113f3e
MC
232 data = NULL;
233 }
d02b48c6 234
0f113f3e
MC
235 /*
236 * if the last one hasn't been pushed yet and there is anything in it
237 * then add it to the stack ...
238 */
239 if ((xi->x509 != NULL) || (xi->crl != NULL) ||
240 (xi->x_pkey != NULL) || (xi->enc_data != NULL)) {
241 if (!sk_X509_INFO_push(ret, xi))
242 goto err;
243 xi = NULL;
244 }
245 ok = 1;
246 err:
222561fe 247 X509_INFO_free(xi);
0f113f3e
MC
248 if (!ok) {
249 for (i = 0; ((int)i) < sk_X509_INFO_num(ret); i++) {
250 xi = sk_X509_INFO_value(ret, i);
251 X509_INFO_free(xi);
252 }
253 if (ret != sk)
254 sk_X509_INFO_free(ret);
255 ret = NULL;
256 }
d02b48c6 257
b548a1f1
RS
258 OPENSSL_free(name);
259 OPENSSL_free(header);
260 OPENSSL_free(data);
26a7d938 261 return ret;
0f113f3e 262}
d02b48c6 263
6725682d
SL
264STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk,
265 pem_password_cb *cb, void *u)
266{
d8652be0 267 return PEM_X509_INFO_read_bio_ex(bp, sk, cb, u, NULL, NULL);
6725682d
SL
268}
269
d02b48c6 270/* A TJH addition */
de0799b0
RL
271int PEM_X509_INFO_write_bio(BIO *bp, const X509_INFO *xi, EVP_CIPHER *enc,
272 const unsigned char *kstr, int klen,
0f113f3e
MC
273 pem_password_cb *cb, void *u)
274{
0f113f3e
MC
275 int i, ret = 0;
276 unsigned char *data = NULL;
277 const char *objstr = NULL;
278 char buf[PEM_BUFSIZE];
de0799b0 279 const unsigned char *iv = NULL;
0f113f3e
MC
280
281 if (enc != NULL) {
282 objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
e40ada04
MC
283 if (objstr == NULL
284 /*
285 * Check "Proc-Type: 4,Encrypted\nDEK-Info: objstr,hex-iv\n"
286 * fits into buf
287 */
288 || (strlen(objstr) + 23 + 2 * EVP_CIPHER_iv_length(enc) + 13)
289 > sizeof(buf)) {
9311d0c4 290 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER);
0f113f3e
MC
291 goto err;
292 }
293 }
d02b48c6 294
0f113f3e
MC
295 /*
296 * now for the fun part ... if we have a private key then we have to be
297 * able to handle a not-yet-decrypted key being written out correctly ...
298 * if it is decrypted or it is non-encrypted then we use the base code
299 */
300 if (xi->x_pkey != NULL) {
301 if ((xi->enc_data != NULL) && (xi->enc_len > 0)) {
302 if (enc == NULL) {
9311d0c4 303 ERR_raise(ERR_LIB_PEM, PEM_R_CIPHER_IS_NULL);
0f113f3e
MC
304 goto err;
305 }
8bbf6ac0 306
0f113f3e
MC
307 /* copy from weirdo names into more normal things */
308 iv = xi->enc_cipher.iv;
309 data = (unsigned char *)xi->enc_data;
310 i = xi->enc_len;
d02b48c6 311
0f113f3e
MC
312 /*
313 * we take the encryption data from the internal stuff rather
314 * than what the user has passed us ... as we have to match
315 * exactly for some strange reason
316 */
317 objstr = OBJ_nid2sn(EVP_CIPHER_nid(xi->enc_cipher.cipher));
318 if (objstr == NULL) {
9311d0c4 319 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER);
0f113f3e
MC
320 goto err;
321 }
d02b48c6 322
df443918 323 /* Create the right magic header stuff */
0f113f3e
MC
324 buf[0] = '\0';
325 PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
6c2ff56e 326 PEM_dek_info(buf, objstr, EVP_CIPHER_iv_length(enc),
de0799b0 327 (const char *)iv);
d02b48c6 328
0f113f3e
MC
329 /* use the normal code to write things out */
330 i = PEM_write_bio(bp, PEM_STRING_RSA, buf, data, i);
331 if (i <= 0)
332 goto err;
333 } else {
334 /* Add DSA/DH */
0f113f3e
MC
335 /* normal optionally encrypted stuff */
336 if (PEM_write_bio_RSAPrivateKey(bp,
3aeb9348 337 EVP_PKEY_get0_RSA(xi->x_pkey->dec_pkey),
0f113f3e
MC
338 enc, kstr, klen, cb, u) <= 0)
339 goto err;
0f113f3e
MC
340 }
341 }
d02b48c6 342
0f113f3e
MC
343 /* if we have a certificate then write it out now */
344 if ((xi->x509 != NULL) && (PEM_write_bio_X509(bp, xi->x509) <= 0))
345 goto err;
d02b48c6 346
0f113f3e
MC
347 /*
348 * we are ignoring anything else that is loaded into the X509_INFO
349 * structure for the moment ... as I don't need it so I'm not coding it
350 * here and Eric can do it when this makes it into the base library --tjh
351 */
d02b48c6 352
0f113f3e 353 ret = 1;
d02b48c6 354
0f113f3e 355 err:
0f113f3e 356 OPENSSL_cleanse(buf, PEM_BUFSIZE);
26a7d938 357 return ret;
0f113f3e 358}