In OpenSSL builds, declare STACK for datatypes ...
[openssl.git] / crypto / pem / pem_info.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
11  * DSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <stdio.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>
25
26 DEFINE_STACK_OF(X509_INFO)
27
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)
31 {
32     BIO *b;
33     STACK_OF(X509_INFO) *ret;
34
35     if ((b = BIO_new(BIO_s_file())) == NULL) {
36         PEMerr(PEM_F_PEM_X509_INFO_READ, ERR_R_BUF_LIB);
37         return 0;
38     }
39     BIO_set_fp(b, fp, BIO_NOCLOSE);
40     ret = PEM_X509_INFO_read_bio(b, sk, cb, u);
41     BIO_free(b);
42     return ret;
43 }
44 #endif
45
46 STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk,
47                                             pem_password_cb *cb, void *u)
48 {
49     X509_INFO *xi = NULL;
50     char *name = NULL, *header = NULL;
51     void *pp;
52     unsigned char *data = NULL;
53     const unsigned char *p;
54     long len, error = 0;
55     int ok = 0;
56     STACK_OF(X509_INFO) *ret = NULL;
57     unsigned int i, raw, ptype;
58     d2i_of_void *d2i = 0;
59
60     if (sk == NULL) {
61         if ((ret = sk_X509_INFO_new_null()) == NULL) {
62             PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_MALLOC_FAILURE);
63             goto err;
64         }
65     } else
66         ret = sk;
67
68     if ((xi = X509_INFO_new()) == NULL)
69         goto err;
70     for (;;) {
71         raw = 0;
72         ptype = 0;
73         i = PEM_read_bio(bp, &name, &header, &data, &len);
74         if (i == 0) {
75             error = ERR_GET_REASON(ERR_peek_last_error());
76             if (error == PEM_R_NO_START_LINE) {
77                 ERR_clear_error();
78                 break;
79             }
80             goto err;
81         }
82  start:
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))
88                     goto err;
89                 if ((xi = X509_INFO_new()) == NULL)
90                     goto err;
91                 goto start;
92             }
93             pp = &(xi->x509);
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))
98                     goto err;
99                 if ((xi = X509_INFO_new()) == NULL)
100                     goto err;
101                 goto start;
102             }
103             pp = &(xi->x509);
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))
108                     goto err;
109                 if ((xi = X509_INFO_new()) == NULL)
110                     goto err;
111                 goto start;
112             }
113             pp = &(xi->crl);
114         } else
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))
120                     goto err;
121                 if ((xi = X509_INFO_new()) == NULL)
122                     goto err;
123                 goto start;
124             }
125
126             xi->enc_data = NULL;
127             xi->enc_len = 0;
128
129             xi->x_pkey = X509_PKEY_new();
130             if (xi->x_pkey == NULL)
131                 goto err;
132             ptype = EVP_PKEY_RSA;
133             pp = &xi->x_pkey->dec_pkey;
134             if ((int)strlen(header) > 10) /* assume encrypted */
135                 raw = 1;
136         } else
137 #endif
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))
143                     goto err;
144                 if ((xi = X509_INFO_new()) == NULL)
145                     goto err;
146                 goto start;
147             }
148
149             xi->enc_data = NULL;
150             xi->enc_len = 0;
151
152             xi->x_pkey = X509_PKEY_new();
153             if (xi->x_pkey == NULL)
154                 goto err;
155             ptype = EVP_PKEY_DSA;
156             pp = &xi->x_pkey->dec_pkey;
157             if ((int)strlen(header) > 10) /* assume encrypted */
158                 raw = 1;
159         } else
160 #endif
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))
166                     goto err;
167                 if ((xi = X509_INFO_new()) == NULL)
168                     goto err;
169                 goto start;
170             }
171
172             xi->enc_data = NULL;
173             xi->enc_len = 0;
174
175             xi->x_pkey = X509_PKEY_new();
176             if (xi->x_pkey == NULL)
177                 goto err;
178             ptype = EVP_PKEY_EC;
179             pp = &xi->x_pkey->dec_pkey;
180             if ((int)strlen(header) > 10) /* assume encrypted */
181                 raw = 1;
182         } else
183 #endif
184         {
185             d2i = NULL;
186             pp = NULL;
187         }
188
189         if (d2i != NULL) {
190             if (!raw) {
191                 EVP_CIPHER_INFO cipher;
192
193                 if (!PEM_get_EVP_CIPHER_INFO(header, &cipher))
194                     goto err;
195                 if (!PEM_do_header(&cipher, data, &len, cb, u))
196                     goto err;
197                 p = data;
198                 if (ptype) {
199                     if (!d2i_PrivateKey(ptype, pp, &p, len)) {
200                         PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_ASN1_LIB);
201                         goto err;
202                     }
203                 } else if (d2i(pp, &p, len) == NULL) {
204                     PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_ASN1_LIB);
205                     goto err;
206                 }
207             } else {            /* encrypted RSA data */
208                 if (!PEM_get_EVP_CIPHER_INFO(header, &xi->enc_cipher))
209                     goto err;
210                 xi->enc_data = (char *)data;
211                 xi->enc_len = (int)len;
212                 data = NULL;
213             }
214         } else {
215             /* unknown */
216         }
217         OPENSSL_free(name);
218         name = NULL;
219         OPENSSL_free(header);
220         header = NULL;
221         OPENSSL_free(data);
222         data = NULL;
223     }
224
225     /*
226      * if the last one hasn't been pushed yet and there is anything in it
227      * then add it to the stack ...
228      */
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))
232             goto err;
233         xi = NULL;
234     }
235     ok = 1;
236  err:
237     X509_INFO_free(xi);
238     if (!ok) {
239         for (i = 0; ((int)i) < sk_X509_INFO_num(ret); i++) {
240             xi = sk_X509_INFO_value(ret, i);
241             X509_INFO_free(xi);
242         }
243         if (ret != sk)
244             sk_X509_INFO_free(ret);
245         ret = NULL;
246     }
247
248     OPENSSL_free(name);
249     OPENSSL_free(header);
250     OPENSSL_free(data);
251     return ret;
252 }
253
254 /* A TJH addition */
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)
258 {
259     int i, ret = 0;
260     unsigned char *data = NULL;
261     const char *objstr = NULL;
262     char buf[PEM_BUFSIZE];
263     const unsigned char *iv = NULL;
264
265     if (enc != NULL) {
266         objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
267         if (objstr == NULL
268                    /*
269                     * Check "Proc-Type: 4,Encrypted\nDEK-Info: objstr,hex-iv\n"
270                     * fits into buf
271                     */
272                 || (strlen(objstr) + 23 + 2 * EVP_CIPHER_iv_length(enc) + 13)
273                    > sizeof(buf)) {
274             PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO, PEM_R_UNSUPPORTED_CIPHER);
275             goto err;
276         }
277     }
278
279     /*
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
283      */
284     if (xi->x_pkey != NULL) {
285         if ((xi->enc_data != NULL) && (xi->enc_len > 0)) {
286             if (enc == NULL) {
287                 PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO, PEM_R_CIPHER_IS_NULL);
288                 goto err;
289             }
290
291             /* copy from weirdo names into more normal things */
292             iv = xi->enc_cipher.iv;
293             data = (unsigned char *)xi->enc_data;
294             i = xi->enc_len;
295
296             /*
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
300              */
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);
305                 goto err;
306             }
307
308             /* Create the right magic header stuff */
309             buf[0] = '\0';
310             PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
311             PEM_dek_info(buf, objstr, EVP_CIPHER_iv_length(enc),
312                          (const char *)iv);
313
314             /* use the normal code to write things out */
315             i = PEM_write_bio(bp, PEM_STRING_RSA, buf, data, i);
316             if (i <= 0)
317                 goto err;
318         } else {
319             /* Add DSA/DH */
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)
325                 goto err;
326 #endif
327         }
328     }
329
330     /* if we have a certificate then write it out now */
331     if ((xi->x509 != NULL) && (PEM_write_bio_X509(bp, xi->x509) <= 0))
332         goto err;
333
334     /*
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
338      */
339
340     ret = 1;
341
342  err:
343     OPENSSL_cleanse(buf, PEM_BUFSIZE);
344     return ret;
345 }