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_ex(FILE *fp
, STACK_OF(X509_INFO
) *sk
, pem_password_cb
*cb
,
29 void *u
, OSSL_LIB_CTX
*libctx
, const char *propq
)
32 STACK_OF(X509_INFO
) *ret
;
34 if ((b
= BIO_new(BIO_s_file())) == NULL
) {
35 PEMerr(0, ERR_R_BUF_LIB
);
38 BIO_set_fp(b
, fp
, BIO_NOCLOSE
);
39 ret
= PEM_X509_INFO_read_bio_ex(b
, sk
, cb
, u
, libctx
, propq
);
44 STACK_OF(X509_INFO
) *PEM_X509_INFO_read(FILE *fp
, STACK_OF(X509_INFO
) *sk
,
45 pem_password_cb
*cb
, void *u
)
47 return PEM_X509_INFO_read_ex(fp
, sk
, cb
, u
, NULL
, NULL
);
52 *PEM_X509_INFO_read_bio_ex(BIO
*bp
, STACK_OF(X509_INFO
) *sk
,
53 pem_password_cb
*cb
, void *u
, OSSL_LIB_CTX
*libctx
,
57 char *name
= NULL
, *header
= NULL
;
59 unsigned char *data
= NULL
;
60 const unsigned char *p
;
63 STACK_OF(X509_INFO
) *ret
= NULL
;
64 unsigned int i
, raw
, ptype
;
68 if ((ret
= sk_X509_INFO_new_null()) == NULL
) {
69 PEMerr(0, ERR_R_MALLOC_FAILURE
);
75 if ((xi
= X509_INFO_new()) == NULL
)
80 i
= PEM_read_bio(bp
, &name
, &header
, &data
, &len
);
82 error
= ERR_GET_REASON(ERR_peek_last_error());
83 if (error
== PEM_R_NO_START_LINE
) {
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
))
96 if ((xi
= X509_INFO_new()) == NULL
)
100 xi
->x509
= X509_new_ex(libctx
, propq
);
101 if (xi
->x509
== NULL
)
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
))
109 if ((xi
= X509_INFO_new()) == NULL
)
113 xi
->x509
= X509_new_ex(libctx
, propq
);
114 if (xi
->x509
== NULL
)
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
))
122 if ((xi
= X509_INFO_new()) == NULL
)
128 #ifndef OPENSSL_NO_RSA
129 if (strcmp(name
, PEM_STRING_RSA
) == 0) {
130 d2i
= (D2I_OF(void)) d2i_RSAPrivateKey
;
131 if (xi
->x_pkey
!= NULL
) {
132 if (!sk_X509_INFO_push(ret
, xi
))
134 if ((xi
= X509_INFO_new()) == NULL
)
142 xi
->x_pkey
= X509_PKEY_new();
143 if (xi
->x_pkey
== NULL
)
145 ptype
= EVP_PKEY_RSA
;
146 pp
= &xi
->x_pkey
->dec_pkey
;
147 if ((int)strlen(header
) > 10) /* assume encrypted */
151 #ifndef OPENSSL_NO_DSA
152 if (strcmp(name
, PEM_STRING_DSA
) == 0) {
153 d2i
= (D2I_OF(void)) d2i_DSAPrivateKey
;
154 if (xi
->x_pkey
!= NULL
) {
155 if (!sk_X509_INFO_push(ret
, xi
))
157 if ((xi
= X509_INFO_new()) == NULL
)
165 xi
->x_pkey
= X509_PKEY_new();
166 if (xi
->x_pkey
== NULL
)
168 ptype
= EVP_PKEY_DSA
;
169 pp
= &xi
->x_pkey
->dec_pkey
;
170 if ((int)strlen(header
) > 10) /* assume encrypted */
174 #ifndef OPENSSL_NO_EC
175 if (strcmp(name
, PEM_STRING_ECPRIVATEKEY
) == 0) {
176 d2i
= (D2I_OF(void)) d2i_ECPrivateKey
;
177 if (xi
->x_pkey
!= NULL
) {
178 if (!sk_X509_INFO_push(ret
, xi
))
180 if ((xi
= X509_INFO_new()) == NULL
)
188 xi
->x_pkey
= X509_PKEY_new();
189 if (xi
->x_pkey
== NULL
)
192 pp
= &xi
->x_pkey
->dec_pkey
;
193 if ((int)strlen(header
) > 10) /* assume encrypted */
204 EVP_CIPHER_INFO cipher
;
206 if (!PEM_get_EVP_CIPHER_INFO(header
, &cipher
))
208 if (!PEM_do_header(&cipher
, data
, &len
, cb
, u
))
212 if (!d2i_PrivateKey(ptype
, pp
, &p
, len
)) {
213 PEMerr(0, ERR_R_ASN1_LIB
);
216 } else if (d2i(pp
, &p
, len
) == NULL
) {
217 PEMerr(0, ERR_R_ASN1_LIB
);
220 } else { /* encrypted RSA data */
221 if (!PEM_get_EVP_CIPHER_INFO(header
, &xi
->enc_cipher
))
223 xi
->enc_data
= (char *)data
;
224 xi
->enc_len
= (int)len
;
232 OPENSSL_free(header
);
239 * if the last one hasn't been pushed yet and there is anything in it
240 * then add it to the stack ...
242 if ((xi
->x509
!= NULL
) || (xi
->crl
!= NULL
) ||
243 (xi
->x_pkey
!= NULL
) || (xi
->enc_data
!= NULL
)) {
244 if (!sk_X509_INFO_push(ret
, xi
))
252 for (i
= 0; ((int)i
) < sk_X509_INFO_num(ret
); i
++) {
253 xi
= sk_X509_INFO_value(ret
, i
);
257 sk_X509_INFO_free(ret
);
262 OPENSSL_free(header
);
267 STACK_OF(X509_INFO
) *PEM_X509_INFO_read_bio(BIO
*bp
, STACK_OF(X509_INFO
) *sk
,
268 pem_password_cb
*cb
, void *u
)
270 return PEM_X509_INFO_read_bio_ex(bp
, sk
, cb
, u
, NULL
, NULL
);
274 int PEM_X509_INFO_write_bio(BIO
*bp
, const X509_INFO
*xi
, EVP_CIPHER
*enc
,
275 const unsigned char *kstr
, int klen
,
276 pem_password_cb
*cb
, void *u
)
279 unsigned char *data
= NULL
;
280 const char *objstr
= NULL
;
281 char buf
[PEM_BUFSIZE
];
282 const unsigned char *iv
= NULL
;
285 objstr
= OBJ_nid2sn(EVP_CIPHER_nid(enc
));
288 * Check "Proc-Type: 4,Encrypted\nDEK-Info: objstr,hex-iv\n"
291 || (strlen(objstr
) + 23 + 2 * EVP_CIPHER_iv_length(enc
) + 13)
293 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO
, PEM_R_UNSUPPORTED_CIPHER
);
299 * now for the fun part ... if we have a private key then we have to be
300 * able to handle a not-yet-decrypted key being written out correctly ...
301 * if it is decrypted or it is non-encrypted then we use the base code
303 if (xi
->x_pkey
!= NULL
) {
304 if ((xi
->enc_data
!= NULL
) && (xi
->enc_len
> 0)) {
306 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO
, PEM_R_CIPHER_IS_NULL
);
310 /* copy from weirdo names into more normal things */
311 iv
= xi
->enc_cipher
.iv
;
312 data
= (unsigned char *)xi
->enc_data
;
316 * we take the encryption data from the internal stuff rather
317 * than what the user has passed us ... as we have to match
318 * exactly for some strange reason
320 objstr
= OBJ_nid2sn(EVP_CIPHER_nid(xi
->enc_cipher
.cipher
));
321 if (objstr
== NULL
) {
322 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO
,
323 PEM_R_UNSUPPORTED_CIPHER
);
327 /* Create the right magic header stuff */
329 PEM_proc_type(buf
, PEM_TYPE_ENCRYPTED
);
330 PEM_dek_info(buf
, objstr
, EVP_CIPHER_iv_length(enc
),
333 /* use the normal code to write things out */
334 i
= PEM_write_bio(bp
, PEM_STRING_RSA
, buf
, data
, i
);
339 #ifndef OPENSSL_NO_RSA
340 /* normal optionally encrypted stuff */
341 if (PEM_write_bio_RSAPrivateKey(bp
,
342 EVP_PKEY_get0_RSA(xi
->x_pkey
->dec_pkey
),
343 enc
, kstr
, klen
, cb
, u
) <= 0)
349 /* if we have a certificate then write it out now */
350 if ((xi
->x509
!= NULL
) && (PEM_write_bio_X509(bp
, xi
->x509
) <= 0))
354 * we are ignoring anything else that is loaded into the X509_INFO
355 * structure for the moment ... as I don't need it so I'm not coding it
356 * here and Eric can do it when this makes it into the base library --tjh
362 OPENSSL_cleanse(buf
, PEM_BUFSIZE
);