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 #ifndef OPENSSL_NO_STDIO
28 *PEM_X509_INFO_read_with_libctx(FILE *fp
, STACK_OF(X509_INFO
) *sk
,
29 pem_password_cb
*cb
, void *u
,
30 OPENSSL_CTX
*libctx
, const char *propq
)
33 STACK_OF(X509_INFO
) *ret
;
35 if ((b
= BIO_new(BIO_s_file())) == NULL
) {
36 PEMerr(0, ERR_R_BUF_LIB
);
39 BIO_set_fp(b
, fp
, BIO_NOCLOSE
);
40 ret
= PEM_X509_INFO_read_bio_with_libctx(b
, sk
, cb
, u
, libctx
, propq
);
45 STACK_OF(X509_INFO
) *PEM_X509_INFO_read(FILE *fp
, STACK_OF(X509_INFO
) *sk
,
46 pem_password_cb
*cb
, void *u
)
48 return PEM_X509_INFO_read_with_libctx(fp
, sk
, cb
, u
, NULL
, NULL
);
53 *PEM_X509_INFO_read_bio_with_libctx(BIO
*bp
, STACK_OF(X509_INFO
) *sk
,
54 pem_password_cb
*cb
, void *u
,
55 OPENSSL_CTX
*libctx
, const char *propq
)
58 char *name
= NULL
, *header
= NULL
;
60 unsigned char *data
= NULL
;
61 const unsigned char *p
;
64 STACK_OF(X509_INFO
) *ret
= NULL
;
65 unsigned int i
, raw
, ptype
;
69 if ((ret
= sk_X509_INFO_new_null()) == NULL
) {
70 PEMerr(0, ERR_R_MALLOC_FAILURE
);
76 if ((xi
= X509_INFO_new()) == NULL
)
81 i
= PEM_read_bio(bp
, &name
, &header
, &data
, &len
);
83 error
= ERR_GET_REASON(ERR_peek_last_error());
84 if (error
== PEM_R_NO_START_LINE
) {
91 if ((strcmp(name
, PEM_STRING_X509
) == 0) ||
92 (strcmp(name
, PEM_STRING_X509_OLD
) == 0)) {
93 d2i
= (D2I_OF(void)) d2i_X509
;
94 if (xi
->x509
!= NULL
) {
95 if (!sk_X509_INFO_push(ret
, xi
))
97 if ((xi
= X509_INFO_new()) == NULL
)
101 xi
->x509
= X509_new_with_libctx(libctx
, propq
);
102 if (xi
->x509
== NULL
)
105 } else if ((strcmp(name
, PEM_STRING_X509_TRUSTED
) == 0)) {
106 d2i
= (D2I_OF(void)) d2i_X509_AUX
;
107 if (xi
->x509
!= NULL
) {
108 if (!sk_X509_INFO_push(ret
, xi
))
110 if ((xi
= X509_INFO_new()) == NULL
)
114 xi
->x509
= X509_new_with_libctx(libctx
, propq
);
115 if (xi
->x509
== NULL
)
118 } else if (strcmp(name
, PEM_STRING_X509_CRL
) == 0) {
119 d2i
= (D2I_OF(void)) d2i_X509_CRL
;
120 if (xi
->crl
!= NULL
) {
121 if (!sk_X509_INFO_push(ret
, xi
))
123 if ((xi
= X509_INFO_new()) == NULL
)
129 #ifndef OPENSSL_NO_RSA
130 if (strcmp(name
, PEM_STRING_RSA
) == 0) {
131 d2i
= (D2I_OF(void)) d2i_RSAPrivateKey
;
132 if (xi
->x_pkey
!= NULL
) {
133 if (!sk_X509_INFO_push(ret
, xi
))
135 if ((xi
= X509_INFO_new()) == NULL
)
143 xi
->x_pkey
= X509_PKEY_new();
144 if (xi
->x_pkey
== NULL
)
146 ptype
= EVP_PKEY_RSA
;
147 pp
= &xi
->x_pkey
->dec_pkey
;
148 if ((int)strlen(header
) > 10) /* assume encrypted */
152 #ifndef OPENSSL_NO_DSA
153 if (strcmp(name
, PEM_STRING_DSA
) == 0) {
154 d2i
= (D2I_OF(void)) d2i_DSAPrivateKey
;
155 if (xi
->x_pkey
!= NULL
) {
156 if (!sk_X509_INFO_push(ret
, xi
))
158 if ((xi
= X509_INFO_new()) == NULL
)
166 xi
->x_pkey
= X509_PKEY_new();
167 if (xi
->x_pkey
== NULL
)
169 ptype
= EVP_PKEY_DSA
;
170 pp
= &xi
->x_pkey
->dec_pkey
;
171 if ((int)strlen(header
) > 10) /* assume encrypted */
175 #ifndef OPENSSL_NO_EC
176 if (strcmp(name
, PEM_STRING_ECPRIVATEKEY
) == 0) {
177 d2i
= (D2I_OF(void)) d2i_ECPrivateKey
;
178 if (xi
->x_pkey
!= NULL
) {
179 if (!sk_X509_INFO_push(ret
, xi
))
181 if ((xi
= X509_INFO_new()) == NULL
)
189 xi
->x_pkey
= X509_PKEY_new();
190 if (xi
->x_pkey
== NULL
)
193 pp
= &xi
->x_pkey
->dec_pkey
;
194 if ((int)strlen(header
) > 10) /* assume encrypted */
205 EVP_CIPHER_INFO cipher
;
207 if (!PEM_get_EVP_CIPHER_INFO(header
, &cipher
))
209 if (!PEM_do_header(&cipher
, data
, &len
, cb
, u
))
213 if (!d2i_PrivateKey(ptype
, pp
, &p
, len
)) {
214 PEMerr(0, ERR_R_ASN1_LIB
);
217 } else if (d2i(pp
, &p
, len
) == NULL
) {
218 PEMerr(0, ERR_R_ASN1_LIB
);
221 } else { /* encrypted RSA data */
222 if (!PEM_get_EVP_CIPHER_INFO(header
, &xi
->enc_cipher
))
224 xi
->enc_data
= (char *)data
;
225 xi
->enc_len
= (int)len
;
233 OPENSSL_free(header
);
240 * if the last one hasn't been pushed yet and there is anything in it
241 * then add it to the stack ...
243 if ((xi
->x509
!= NULL
) || (xi
->crl
!= NULL
) ||
244 (xi
->x_pkey
!= NULL
) || (xi
->enc_data
!= NULL
)) {
245 if (!sk_X509_INFO_push(ret
, xi
))
253 for (i
= 0; ((int)i
) < sk_X509_INFO_num(ret
); i
++) {
254 xi
= sk_X509_INFO_value(ret
, i
);
258 sk_X509_INFO_free(ret
);
263 OPENSSL_free(header
);
268 STACK_OF(X509_INFO
) *PEM_X509_INFO_read_bio(BIO
*bp
, STACK_OF(X509_INFO
) *sk
,
269 pem_password_cb
*cb
, void *u
)
271 return PEM_X509_INFO_read_bio_with_libctx(bp
, sk
, cb
, u
, NULL
, NULL
);
275 int PEM_X509_INFO_write_bio(BIO
*bp
, const X509_INFO
*xi
, EVP_CIPHER
*enc
,
276 const unsigned char *kstr
, int klen
,
277 pem_password_cb
*cb
, void *u
)
280 unsigned char *data
= NULL
;
281 const char *objstr
= NULL
;
282 char buf
[PEM_BUFSIZE
];
283 const unsigned char *iv
= NULL
;
286 objstr
= OBJ_nid2sn(EVP_CIPHER_nid(enc
));
289 * Check "Proc-Type: 4,Encrypted\nDEK-Info: objstr,hex-iv\n"
292 || (strlen(objstr
) + 23 + 2 * EVP_CIPHER_iv_length(enc
) + 13)
294 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO
, PEM_R_UNSUPPORTED_CIPHER
);
300 * now for the fun part ... if we have a private key then we have to be
301 * able to handle a not-yet-decrypted key being written out correctly ...
302 * if it is decrypted or it is non-encrypted then we use the base code
304 if (xi
->x_pkey
!= NULL
) {
305 if ((xi
->enc_data
!= NULL
) && (xi
->enc_len
> 0)) {
307 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO
, PEM_R_CIPHER_IS_NULL
);
311 /* copy from weirdo names into more normal things */
312 iv
= xi
->enc_cipher
.iv
;
313 data
= (unsigned char *)xi
->enc_data
;
317 * we take the encryption data from the internal stuff rather
318 * than what the user has passed us ... as we have to match
319 * exactly for some strange reason
321 objstr
= OBJ_nid2sn(EVP_CIPHER_nid(xi
->enc_cipher
.cipher
));
322 if (objstr
== NULL
) {
323 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO
,
324 PEM_R_UNSUPPORTED_CIPHER
);
328 /* Create the right magic header stuff */
330 PEM_proc_type(buf
, PEM_TYPE_ENCRYPTED
);
331 PEM_dek_info(buf
, objstr
, EVP_CIPHER_iv_length(enc
),
334 /* use the normal code to write things out */
335 i
= PEM_write_bio(bp
, PEM_STRING_RSA
, buf
, data
, i
);
340 #ifndef OPENSSL_NO_RSA
341 /* normal optionally encrypted stuff */
342 if (PEM_write_bio_RSAPrivateKey(bp
,
343 EVP_PKEY_get0_RSA(xi
->x_pkey
->dec_pkey
),
344 enc
, kstr
, klen
, cb
, u
) <= 0)
350 /* if we have a certificate then write it out now */
351 if ((xi
->x509
!= NULL
) && (PEM_write_bio_X509(bp
, xi
->x509
) <= 0))
355 * we are ignoring anything else that is loaded into the X509_INFO
356 * structure for the moment ... as I don't need it so I'm not coding it
357 * here and Eric can do it when this makes it into the base library --tjh
363 OPENSSL_cleanse(buf
, PEM_BUFSIZE
);