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
30 *PEM_X509_INFO_read_with_libctx(FILE *fp
, STACK_OF(X509_INFO
) *sk
,
31 pem_password_cb
*cb
, void *u
,
32 OPENSSL_CTX
*libctx
, const char *propq
)
35 STACK_OF(X509_INFO
) *ret
;
37 if ((b
= BIO_new(BIO_s_file())) == NULL
) {
38 PEMerr(0, ERR_R_BUF_LIB
);
41 BIO_set_fp(b
, fp
, BIO_NOCLOSE
);
42 ret
= PEM_X509_INFO_read_bio_with_libctx(b
, sk
, cb
, u
, libctx
, propq
);
47 STACK_OF(X509_INFO
) *PEM_X509_INFO_read(FILE *fp
, STACK_OF(X509_INFO
) *sk
,
48 pem_password_cb
*cb
, void *u
)
50 return PEM_X509_INFO_read_with_libctx(fp
, sk
, cb
, u
, NULL
, NULL
);
55 *PEM_X509_INFO_read_bio_with_libctx(BIO
*bp
, STACK_OF(X509_INFO
) *sk
,
56 pem_password_cb
*cb
, void *u
,
57 OPENSSL_CTX
*libctx
, const char *propq
)
60 char *name
= NULL
, *header
= NULL
;
62 unsigned char *data
= NULL
;
63 const unsigned char *p
;
66 STACK_OF(X509_INFO
) *ret
= NULL
;
67 unsigned int i
, raw
, ptype
;
71 if ((ret
= sk_X509_INFO_new_null()) == NULL
) {
72 PEMerr(0, ERR_R_MALLOC_FAILURE
);
78 if ((xi
= X509_INFO_new()) == NULL
)
83 i
= PEM_read_bio(bp
, &name
, &header
, &data
, &len
);
85 error
= ERR_GET_REASON(ERR_peek_last_error());
86 if (error
== PEM_R_NO_START_LINE
) {
93 if ((strcmp(name
, PEM_STRING_X509
) == 0) ||
94 (strcmp(name
, PEM_STRING_X509_OLD
) == 0)) {
95 d2i
= (D2I_OF(void)) d2i_X509
;
96 if (xi
->x509
!= NULL
) {
97 if (!sk_X509_INFO_push(ret
, xi
))
99 if ((xi
= X509_INFO_new()) == NULL
)
103 xi
->x509
= X509_new_with_libctx(libctx
, propq
);
104 if (xi
->x509
== NULL
)
107 } else if ((strcmp(name
, PEM_STRING_X509_TRUSTED
) == 0)) {
108 d2i
= (D2I_OF(void)) d2i_X509_AUX
;
109 if (xi
->x509
!= NULL
) {
110 if (!sk_X509_INFO_push(ret
, xi
))
112 if ((xi
= X509_INFO_new()) == NULL
)
116 xi
->x509
= X509_new_with_libctx(libctx
, propq
);
117 if (xi
->x509
== NULL
)
120 } else if (strcmp(name
, PEM_STRING_X509_CRL
) == 0) {
121 d2i
= (D2I_OF(void)) d2i_X509_CRL
;
122 if (xi
->crl
!= NULL
) {
123 if (!sk_X509_INFO_push(ret
, xi
))
125 if ((xi
= X509_INFO_new()) == NULL
)
131 #ifndef OPENSSL_NO_RSA
132 if (strcmp(name
, PEM_STRING_RSA
) == 0) {
133 d2i
= (D2I_OF(void)) d2i_RSAPrivateKey
;
134 if (xi
->x_pkey
!= NULL
) {
135 if (!sk_X509_INFO_push(ret
, xi
))
137 if ((xi
= X509_INFO_new()) == NULL
)
145 xi
->x_pkey
= X509_PKEY_new();
146 if (xi
->x_pkey
== NULL
)
148 ptype
= EVP_PKEY_RSA
;
149 pp
= &xi
->x_pkey
->dec_pkey
;
150 if ((int)strlen(header
) > 10) /* assume encrypted */
154 #ifndef OPENSSL_NO_DSA
155 if (strcmp(name
, PEM_STRING_DSA
) == 0) {
156 d2i
= (D2I_OF(void)) d2i_DSAPrivateKey
;
157 if (xi
->x_pkey
!= NULL
) {
158 if (!sk_X509_INFO_push(ret
, xi
))
160 if ((xi
= X509_INFO_new()) == NULL
)
168 xi
->x_pkey
= X509_PKEY_new();
169 if (xi
->x_pkey
== NULL
)
171 ptype
= EVP_PKEY_DSA
;
172 pp
= &xi
->x_pkey
->dec_pkey
;
173 if ((int)strlen(header
) > 10) /* assume encrypted */
177 #ifndef OPENSSL_NO_EC
178 if (strcmp(name
, PEM_STRING_ECPRIVATEKEY
) == 0) {
179 d2i
= (D2I_OF(void)) d2i_ECPrivateKey
;
180 if (xi
->x_pkey
!= NULL
) {
181 if (!sk_X509_INFO_push(ret
, xi
))
183 if ((xi
= X509_INFO_new()) == NULL
)
191 xi
->x_pkey
= X509_PKEY_new();
192 if (xi
->x_pkey
== NULL
)
195 pp
= &xi
->x_pkey
->dec_pkey
;
196 if ((int)strlen(header
) > 10) /* assume encrypted */
207 EVP_CIPHER_INFO cipher
;
209 if (!PEM_get_EVP_CIPHER_INFO(header
, &cipher
))
211 if (!PEM_do_header(&cipher
, data
, &len
, cb
, u
))
215 if (!d2i_PrivateKey(ptype
, pp
, &p
, len
)) {
216 PEMerr(0, ERR_R_ASN1_LIB
);
219 } else if (d2i(pp
, &p
, len
) == NULL
) {
220 PEMerr(0, ERR_R_ASN1_LIB
);
223 } else { /* encrypted RSA data */
224 if (!PEM_get_EVP_CIPHER_INFO(header
, &xi
->enc_cipher
))
226 xi
->enc_data
= (char *)data
;
227 xi
->enc_len
= (int)len
;
235 OPENSSL_free(header
);
242 * if the last one hasn't been pushed yet and there is anything in it
243 * then add it to the stack ...
245 if ((xi
->x509
!= NULL
) || (xi
->crl
!= NULL
) ||
246 (xi
->x_pkey
!= NULL
) || (xi
->enc_data
!= NULL
)) {
247 if (!sk_X509_INFO_push(ret
, xi
))
255 for (i
= 0; ((int)i
) < sk_X509_INFO_num(ret
); i
++) {
256 xi
= sk_X509_INFO_value(ret
, i
);
260 sk_X509_INFO_free(ret
);
265 OPENSSL_free(header
);
270 STACK_OF(X509_INFO
) *PEM_X509_INFO_read_bio(BIO
*bp
, STACK_OF(X509_INFO
) *sk
,
271 pem_password_cb
*cb
, void *u
)
273 return PEM_X509_INFO_read_bio_with_libctx(bp
, sk
, cb
, u
, NULL
, NULL
);
277 int PEM_X509_INFO_write_bio(BIO
*bp
, const X509_INFO
*xi
, EVP_CIPHER
*enc
,
278 const unsigned char *kstr
, int klen
,
279 pem_password_cb
*cb
, void *u
)
282 unsigned char *data
= NULL
;
283 const char *objstr
= NULL
;
284 char buf
[PEM_BUFSIZE
];
285 const unsigned char *iv
= NULL
;
288 objstr
= OBJ_nid2sn(EVP_CIPHER_nid(enc
));
291 * Check "Proc-Type: 4,Encrypted\nDEK-Info: objstr,hex-iv\n"
294 || (strlen(objstr
) + 23 + 2 * EVP_CIPHER_iv_length(enc
) + 13)
296 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO
, PEM_R_UNSUPPORTED_CIPHER
);
302 * now for the fun part ... if we have a private key then we have to be
303 * able to handle a not-yet-decrypted key being written out correctly ...
304 * if it is decrypted or it is non-encrypted then we use the base code
306 if (xi
->x_pkey
!= NULL
) {
307 if ((xi
->enc_data
!= NULL
) && (xi
->enc_len
> 0)) {
309 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO
, PEM_R_CIPHER_IS_NULL
);
313 /* copy from weirdo names into more normal things */
314 iv
= xi
->enc_cipher
.iv
;
315 data
= (unsigned char *)xi
->enc_data
;
319 * we take the encryption data from the internal stuff rather
320 * than what the user has passed us ... as we have to match
321 * exactly for some strange reason
323 objstr
= OBJ_nid2sn(EVP_CIPHER_nid(xi
->enc_cipher
.cipher
));
324 if (objstr
== NULL
) {
325 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO
,
326 PEM_R_UNSUPPORTED_CIPHER
);
330 /* Create the right magic header stuff */
332 PEM_proc_type(buf
, PEM_TYPE_ENCRYPTED
);
333 PEM_dek_info(buf
, objstr
, EVP_CIPHER_iv_length(enc
),
336 /* use the normal code to write things out */
337 i
= PEM_write_bio(bp
, PEM_STRING_RSA
, buf
, data
, i
);
342 #ifndef OPENSSL_NO_RSA
343 /* normal optionally encrypted stuff */
344 if (PEM_write_bio_RSAPrivateKey(bp
,
345 EVP_PKEY_get0_RSA(xi
->x_pkey
->dec_pkey
),
346 enc
, kstr
, klen
, cb
, u
) <= 0)
352 /* if we have a certificate then write it out now */
353 if ((xi
->x509
!= NULL
) && (PEM_write_bio_X509(bp
, xi
->x509
) <= 0))
357 * we are ignoring anything else that is loaded into the X509_INFO
358 * structure for the moment ... as I don't need it so I'm not coding it
359 * here and Eric can do it when this makes it into the base library --tjh
365 OPENSSL_cleanse(buf
, PEM_BUFSIZE
);