2 * Copyright 1995-2016 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 #ifndef OPENSSL_NO_STDIO
27 STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk,
28 pem_password_cb *cb, void *u)
31 STACK_OF(X509_INFO) *ret;
33 if ((b = BIO_new(BIO_s_file())) == NULL) {
34 PEMerr(PEM_F_PEM_X509_INFO_READ, ERR_R_BUF_LIB);
37 BIO_set_fp(b, fp, BIO_NOCLOSE);
38 ret = PEM_X509_INFO_read_bio(b, sk, cb, u);
44 STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk,
45 pem_password_cb *cb, void *u)
48 char *name = NULL, *header = NULL;
50 unsigned char *data = NULL;
51 const unsigned char *p;
54 STACK_OF(X509_INFO) *ret = NULL;
55 unsigned int i, raw, ptype;
59 if ((ret = sk_X509_INFO_new_null()) == NULL) {
60 PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_MALLOC_FAILURE);
66 if ((xi = X509_INFO_new()) == NULL)
71 i = PEM_read_bio(bp, &name, &header, &data, &len);
73 error = ERR_GET_REASON(ERR_peek_last_error());
74 if (error == PEM_R_NO_START_LINE) {
81 if ((strcmp(name, PEM_STRING_X509) == 0) ||
82 (strcmp(name, PEM_STRING_X509_OLD) == 0)) {
83 d2i = (D2I_OF(void)) d2i_X509;
84 if (xi->x509 != NULL) {
85 if (!sk_X509_INFO_push(ret, xi))
87 if ((xi = X509_INFO_new()) == NULL)
92 } else if ((strcmp(name, PEM_STRING_X509_TRUSTED) == 0)) {
93 d2i = (D2I_OF(void)) d2i_X509_AUX;
94 if (xi->x509 != NULL) {
95 if (!sk_X509_INFO_push(ret, xi))
97 if ((xi = X509_INFO_new()) == NULL)
102 } else if (strcmp(name, PEM_STRING_X509_CRL) == 0) {
103 d2i = (D2I_OF(void)) d2i_X509_CRL;
104 if (xi->crl != NULL) {
105 if (!sk_X509_INFO_push(ret, xi))
107 if ((xi = X509_INFO_new()) == NULL)
113 #ifndef OPENSSL_NO_RSA
114 if (strcmp(name, PEM_STRING_RSA) == 0) {
115 d2i = (D2I_OF(void)) d2i_RSAPrivateKey;
116 if (xi->x_pkey != NULL) {
117 if (!sk_X509_INFO_push(ret, xi))
119 if ((xi = X509_INFO_new()) == NULL)
127 xi->x_pkey = X509_PKEY_new();
128 if (xi->x_pkey == NULL)
130 ptype = EVP_PKEY_RSA;
131 pp = &xi->x_pkey->dec_pkey;
132 if ((int)strlen(header) > 10) /* assume encrypted */
136 #ifndef OPENSSL_NO_DSA
137 if (strcmp(name, PEM_STRING_DSA) == 0) {
138 d2i = (D2I_OF(void)) d2i_DSAPrivateKey;
139 if (xi->x_pkey != NULL) {
140 if (!sk_X509_INFO_push(ret, xi))
142 if ((xi = X509_INFO_new()) == NULL)
150 xi->x_pkey = X509_PKEY_new();
151 if (xi->x_pkey == NULL)
153 ptype = EVP_PKEY_DSA;
154 pp = &xi->x_pkey->dec_pkey;
155 if ((int)strlen(header) > 10) /* assume encrypted */
159 #ifndef OPENSSL_NO_EC
160 if (strcmp(name, PEM_STRING_ECPRIVATEKEY) == 0) {
161 d2i = (D2I_OF(void)) d2i_ECPrivateKey;
162 if (xi->x_pkey != NULL) {
163 if (!sk_X509_INFO_push(ret, xi))
165 if ((xi = X509_INFO_new()) == NULL)
173 xi->x_pkey = X509_PKEY_new();
174 if (xi->x_pkey == NULL)
177 pp = &xi->x_pkey->dec_pkey;
178 if ((int)strlen(header) > 10) /* assume encrypted */
189 EVP_CIPHER_INFO cipher;
191 if (!PEM_get_EVP_CIPHER_INFO(header, &cipher))
193 if (!PEM_do_header(&cipher, data, &len, cb, u))
197 if (!d2i_PrivateKey(ptype, pp, &p, len)) {
198 PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_ASN1_LIB);
201 } else if (d2i(pp, &p, len) == NULL) {
202 PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_ASN1_LIB);
205 } else { /* encrypted RSA data */
206 if (!PEM_get_EVP_CIPHER_INFO(header, &xi->enc_cipher))
208 xi->enc_data = (char *)data;
209 xi->enc_len = (int)len;
217 OPENSSL_free(header);
224 * if the last one hasn't been pushed yet and there is anything in it
225 * then add it to the stack ...
227 if ((xi->x509 != NULL) || (xi->crl != NULL) ||
228 (xi->x_pkey != NULL) || (xi->enc_data != NULL)) {
229 if (!sk_X509_INFO_push(ret, xi))
237 for (i = 0; ((int)i) < sk_X509_INFO_num(ret); i++) {
238 xi = sk_X509_INFO_value(ret, i);
242 sk_X509_INFO_free(ret);
247 OPENSSL_free(header);
253 int PEM_X509_INFO_write_bio(BIO *bp, const X509_INFO *xi, EVP_CIPHER *enc,
254 const unsigned char *kstr, int klen,
255 pem_password_cb *cb, void *u)
258 unsigned char *data = NULL;
259 const char *objstr = NULL;
260 char buf[PEM_BUFSIZE];
261 const unsigned char *iv = NULL;
264 objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
267 * Check "Proc-Type: 4,Encrypted\nDEK-Info: objstr,hex-iv\n"
270 || (strlen(objstr) + 23 + 2 * EVP_CIPHER_iv_length(enc) + 13)
272 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO, PEM_R_UNSUPPORTED_CIPHER);
278 * now for the fun part ... if we have a private key then we have to be
279 * able to handle a not-yet-decrypted key being written out correctly ...
280 * if it is decrypted or it is non-encrypted then we use the base code
282 if (xi->x_pkey != NULL) {
283 if ((xi->enc_data != NULL) && (xi->enc_len > 0)) {
285 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO, PEM_R_CIPHER_IS_NULL);
289 /* copy from weirdo names into more normal things */
290 iv = xi->enc_cipher.iv;
291 data = (unsigned char *)xi->enc_data;
295 * we take the encryption data from the internal stuff rather
296 * than what the user has passed us ... as we have to match
297 * exactly for some strange reason
299 objstr = OBJ_nid2sn(EVP_CIPHER_nid(xi->enc_cipher.cipher));
300 if (objstr == NULL) {
301 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO,
302 PEM_R_UNSUPPORTED_CIPHER);
306 /* Create the right magic header stuff */
308 PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
309 PEM_dek_info(buf, objstr, EVP_CIPHER_iv_length(enc),
312 /* use the normal code to write things out */
313 i = PEM_write_bio(bp, PEM_STRING_RSA, buf, data, i);
318 #ifndef OPENSSL_NO_RSA
319 /* normal optionally encrypted stuff */
320 if (PEM_write_bio_RSAPrivateKey(bp,
321 EVP_PKEY_get0_RSA(xi->x_pkey->dec_pkey),
322 enc, kstr, klen, cb, u) <= 0)
328 /* if we have a certificate then write it out now */
329 if ((xi->x509 != NULL) && (PEM_write_bio_X509(bp, xi->x509) <= 0))
333 * we are ignoring anything else that is loaded into the X509_INFO
334 * structure for the moment ... as I don't need it so I'm not coding it
335 * here and Eric can do it when this makes it into the base library --tjh
341 OPENSSL_cleanse(buf, PEM_BUFSIZE);