2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
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>
23 #include <openssl/rsa.h>
24 #include <openssl/dsa.h>
26 DEFINE_STACK_OF(X509_INFO)
28 #ifndef OPENSSL_NO_STDIO
29 STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk,
30 pem_password_cb *cb, void *u)
33 STACK_OF(X509_INFO) *ret;
35 if ((b = BIO_new(BIO_s_file())) == NULL) {
36 PEMerr(PEM_F_PEM_X509_INFO_READ, ERR_R_BUF_LIB);
39 BIO_set_fp(b, fp, BIO_NOCLOSE);
40 ret = PEM_X509_INFO_read_bio(b, sk, cb, u);
46 STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk,
47 pem_password_cb *cb, void *u)
50 char *name = NULL, *header = NULL;
52 unsigned char *data = NULL;
53 const unsigned char *p;
56 STACK_OF(X509_INFO) *ret = NULL;
57 unsigned int i, raw, ptype;
61 if ((ret = sk_X509_INFO_new_null()) == NULL) {
62 PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_MALLOC_FAILURE);
68 if ((xi = X509_INFO_new()) == NULL)
73 i = PEM_read_bio(bp, &name, &header, &data, &len);
75 error = ERR_GET_REASON(ERR_peek_last_error());
76 if (error == PEM_R_NO_START_LINE) {
83 if ((strcmp(name, PEM_STRING_X509) == 0) ||
84 (strcmp(name, PEM_STRING_X509_OLD) == 0)) {
85 d2i = (D2I_OF(void)) d2i_X509;
86 if (xi->x509 != NULL) {
87 if (!sk_X509_INFO_push(ret, xi))
89 if ((xi = X509_INFO_new()) == NULL)
94 } else if ((strcmp(name, PEM_STRING_X509_TRUSTED) == 0)) {
95 d2i = (D2I_OF(void)) d2i_X509_AUX;
96 if (xi->x509 != NULL) {
97 if (!sk_X509_INFO_push(ret, xi))
99 if ((xi = X509_INFO_new()) == NULL)
104 } else if (strcmp(name, PEM_STRING_X509_CRL) == 0) {
105 d2i = (D2I_OF(void)) d2i_X509_CRL;
106 if (xi->crl != NULL) {
107 if (!sk_X509_INFO_push(ret, xi))
109 if ((xi = X509_INFO_new()) == NULL)
115 #ifndef OPENSSL_NO_RSA
116 if (strcmp(name, PEM_STRING_RSA) == 0) {
117 d2i = (D2I_OF(void)) d2i_RSAPrivateKey;
118 if (xi->x_pkey != NULL) {
119 if (!sk_X509_INFO_push(ret, xi))
121 if ((xi = X509_INFO_new()) == NULL)
129 xi->x_pkey = X509_PKEY_new();
130 if (xi->x_pkey == NULL)
132 ptype = EVP_PKEY_RSA;
133 pp = &xi->x_pkey->dec_pkey;
134 if ((int)strlen(header) > 10) /* assume encrypted */
138 #ifndef OPENSSL_NO_DSA
139 if (strcmp(name, PEM_STRING_DSA) == 0) {
140 d2i = (D2I_OF(void)) d2i_DSAPrivateKey;
141 if (xi->x_pkey != NULL) {
142 if (!sk_X509_INFO_push(ret, xi))
144 if ((xi = X509_INFO_new()) == NULL)
152 xi->x_pkey = X509_PKEY_new();
153 if (xi->x_pkey == NULL)
155 ptype = EVP_PKEY_DSA;
156 pp = &xi->x_pkey->dec_pkey;
157 if ((int)strlen(header) > 10) /* assume encrypted */
161 #ifndef OPENSSL_NO_EC
162 if (strcmp(name, PEM_STRING_ECPRIVATEKEY) == 0) {
163 d2i = (D2I_OF(void)) d2i_ECPrivateKey;
164 if (xi->x_pkey != NULL) {
165 if (!sk_X509_INFO_push(ret, xi))
167 if ((xi = X509_INFO_new()) == NULL)
175 xi->x_pkey = X509_PKEY_new();
176 if (xi->x_pkey == NULL)
179 pp = &xi->x_pkey->dec_pkey;
180 if ((int)strlen(header) > 10) /* assume encrypted */
191 EVP_CIPHER_INFO cipher;
193 if (!PEM_get_EVP_CIPHER_INFO(header, &cipher))
195 if (!PEM_do_header(&cipher, data, &len, cb, u))
199 if (!d2i_PrivateKey(ptype, pp, &p, len)) {
200 PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_ASN1_LIB);
203 } else if (d2i(pp, &p, len) == NULL) {
204 PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_ASN1_LIB);
207 } else { /* encrypted RSA data */
208 if (!PEM_get_EVP_CIPHER_INFO(header, &xi->enc_cipher))
210 xi->enc_data = (char *)data;
211 xi->enc_len = (int)len;
219 OPENSSL_free(header);
226 * if the last one hasn't been pushed yet and there is anything in it
227 * then add it to the stack ...
229 if ((xi->x509 != NULL) || (xi->crl != NULL) ||
230 (xi->x_pkey != NULL) || (xi->enc_data != NULL)) {
231 if (!sk_X509_INFO_push(ret, xi))
239 for (i = 0; ((int)i) < sk_X509_INFO_num(ret); i++) {
240 xi = sk_X509_INFO_value(ret, i);
244 sk_X509_INFO_free(ret);
249 OPENSSL_free(header);
255 int PEM_X509_INFO_write_bio(BIO *bp, const X509_INFO *xi, EVP_CIPHER *enc,
256 const unsigned char *kstr, int klen,
257 pem_password_cb *cb, void *u)
260 unsigned char *data = NULL;
261 const char *objstr = NULL;
262 char buf[PEM_BUFSIZE];
263 const unsigned char *iv = NULL;
266 objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
269 * Check "Proc-Type: 4,Encrypted\nDEK-Info: objstr,hex-iv\n"
272 || (strlen(objstr) + 23 + 2 * EVP_CIPHER_iv_length(enc) + 13)
274 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO, PEM_R_UNSUPPORTED_CIPHER);
280 * now for the fun part ... if we have a private key then we have to be
281 * able to handle a not-yet-decrypted key being written out correctly ...
282 * if it is decrypted or it is non-encrypted then we use the base code
284 if (xi->x_pkey != NULL) {
285 if ((xi->enc_data != NULL) && (xi->enc_len > 0)) {
287 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO, PEM_R_CIPHER_IS_NULL);
291 /* copy from weirdo names into more normal things */
292 iv = xi->enc_cipher.iv;
293 data = (unsigned char *)xi->enc_data;
297 * we take the encryption data from the internal stuff rather
298 * than what the user has passed us ... as we have to match
299 * exactly for some strange reason
301 objstr = OBJ_nid2sn(EVP_CIPHER_nid(xi->enc_cipher.cipher));
302 if (objstr == NULL) {
303 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO,
304 PEM_R_UNSUPPORTED_CIPHER);
308 /* Create the right magic header stuff */
310 PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
311 PEM_dek_info(buf, objstr, EVP_CIPHER_iv_length(enc),
314 /* use the normal code to write things out */
315 i = PEM_write_bio(bp, PEM_STRING_RSA, buf, data, i);
320 #ifndef OPENSSL_NO_RSA
321 /* normal optionally encrypted stuff */
322 if (PEM_write_bio_RSAPrivateKey(bp,
323 EVP_PKEY_get0_RSA(xi->x_pkey->dec_pkey),
324 enc, kstr, klen, cb, u) <= 0)
330 /* if we have a certificate then write it out now */
331 if ((xi->x509 != NULL) && (PEM_write_bio_X509(bp, xi->x509) <= 0))
335 * we are ignoring anything else that is loaded into the X509_INFO
336 * structure for the moment ... as I don't need it so I'm not coding it
337 * here and Eric can do it when this makes it into the base library --tjh
343 OPENSSL_cleanse(buf, PEM_BUFSIZE);