xref: /freebsd/crypto/openssl/crypto/x509/by_file.c (revision 47902a71f34d554fd82251d01e50d76a18f550e4)
174664626SKris Kennaway /* crypto/x509/by_file.c */
274664626SKris Kennaway /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
374664626SKris Kennaway  * All rights reserved.
474664626SKris Kennaway  *
574664626SKris Kennaway  * This package is an SSL implementation written
674664626SKris Kennaway  * by Eric Young (eay@cryptsoft.com).
774664626SKris Kennaway  * The implementation was written so as to conform with Netscapes SSL.
874664626SKris Kennaway  *
974664626SKris Kennaway  * This library is free for commercial and non-commercial use as long as
1074664626SKris Kennaway  * the following conditions are aheared to.  The following conditions
1174664626SKris Kennaway  * apply to all code found in this distribution, be it the RC4, RSA,
1274664626SKris Kennaway  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1374664626SKris Kennaway  * included with this distribution is covered by the same copyright terms
1474664626SKris Kennaway  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1574664626SKris Kennaway  *
1674664626SKris Kennaway  * Copyright remains Eric Young's, and as such any Copyright notices in
1774664626SKris Kennaway  * the code are not to be removed.
1874664626SKris Kennaway  * If this package is used in a product, Eric Young should be given attribution
1974664626SKris Kennaway  * as the author of the parts of the library used.
2074664626SKris Kennaway  * This can be in the form of a textual message at program startup or
2174664626SKris Kennaway  * in documentation (online or textual) provided with the package.
2274664626SKris Kennaway  *
2374664626SKris Kennaway  * Redistribution and use in source and binary forms, with or without
2474664626SKris Kennaway  * modification, are permitted provided that the following conditions
2574664626SKris Kennaway  * are met:
2674664626SKris Kennaway  * 1. Redistributions of source code must retain the copyright
2774664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
2874664626SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
2974664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
3074664626SKris Kennaway  *    documentation and/or other materials provided with the distribution.
3174664626SKris Kennaway  * 3. All advertising materials mentioning features or use of this software
3274664626SKris Kennaway  *    must display the following acknowledgement:
3374664626SKris Kennaway  *    "This product includes cryptographic software written by
3474664626SKris Kennaway  *     Eric Young (eay@cryptsoft.com)"
3574664626SKris Kennaway  *    The word 'cryptographic' can be left out if the rouines from the library
3674664626SKris Kennaway  *    being used are not cryptographic related :-).
3774664626SKris Kennaway  * 4. If you include any Windows specific code (or a derivative thereof) from
3874664626SKris Kennaway  *    the apps directory (application code) you must include an acknowledgement:
3974664626SKris Kennaway  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4074664626SKris Kennaway  *
4174664626SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4274664626SKris Kennaway  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4374664626SKris Kennaway  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4474664626SKris Kennaway  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4574664626SKris Kennaway  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4674664626SKris Kennaway  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4774664626SKris Kennaway  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4874664626SKris Kennaway  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4974664626SKris Kennaway  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5074664626SKris Kennaway  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5174664626SKris Kennaway  * SUCH DAMAGE.
5274664626SKris Kennaway  *
5374664626SKris Kennaway  * The licence and distribution terms for any publically available version or
5474664626SKris Kennaway  * derivative of this code cannot be changed.  i.e. this code cannot simply be
5574664626SKris Kennaway  * copied and put under another distribution licence
5674664626SKris Kennaway  * [including the GNU Public Licence.]
5774664626SKris Kennaway  */
5874664626SKris Kennaway 
5974664626SKris Kennaway #include <stdio.h>
6074664626SKris Kennaway #include <time.h>
6174664626SKris Kennaway #include <errno.h>
6274664626SKris Kennaway 
6374664626SKris Kennaway #include "cryptlib.h"
6474664626SKris Kennaway #include <openssl/lhash.h>
6574664626SKris Kennaway #include <openssl/buffer.h>
6674664626SKris Kennaway #include <openssl/x509.h>
6774664626SKris Kennaway #include <openssl/pem.h>
6874664626SKris Kennaway 
695c87c606SMark Murray #ifndef OPENSSL_NO_STDIO
7074664626SKris Kennaway 
7174664626SKris Kennaway static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argc,
7274664626SKris Kennaway                         long argl, char **ret);
736f9291ceSJung-uk Kim X509_LOOKUP_METHOD x509_file_lookup = {
7474664626SKris Kennaway     "Load file into cache",
7574664626SKris Kennaway     NULL,                       /* new */
7674664626SKris Kennaway     NULL,                       /* free */
7774664626SKris Kennaway     NULL,                       /* init */
7874664626SKris Kennaway     NULL,                       /* shutdown */
7974664626SKris Kennaway     by_file_ctrl,               /* ctrl */
8074664626SKris Kennaway     NULL,                       /* get_by_subject */
8174664626SKris Kennaway     NULL,                       /* get_by_issuer_serial */
8274664626SKris Kennaway     NULL,                       /* get_by_fingerprint */
8374664626SKris Kennaway     NULL,                       /* get_by_alias */
8474664626SKris Kennaway };
8574664626SKris Kennaway 
8674664626SKris Kennaway X509_LOOKUP_METHOD *X509_LOOKUP_file(void)
8774664626SKris Kennaway {
8874664626SKris Kennaway     return (&x509_file_lookup);
8974664626SKris Kennaway }
9074664626SKris Kennaway 
916f9291ceSJung-uk Kim static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp,
926f9291ceSJung-uk Kim                         long argl, char **ret)
9374664626SKris Kennaway {
94f579bf8eSKris Kennaway     int ok = 0;
95*47902a71SJung-uk Kim     const char *file;
9674664626SKris Kennaway 
976f9291ceSJung-uk Kim     switch (cmd) {
9874664626SKris Kennaway     case X509_L_FILE_LOAD:
996f9291ceSJung-uk Kim         if (argl == X509_FILETYPE_DEFAULT) {
100*47902a71SJung-uk Kim             file = getenv(X509_get_default_cert_file_env());
1015c87c606SMark Murray             if (file)
1025c87c606SMark Murray                 ok = (X509_load_cert_crl_file(ctx, file,
1035c87c606SMark Murray                                               X509_FILETYPE_PEM) != 0);
1045c87c606SMark Murray 
1055c87c606SMark Murray             else
1066f9291ceSJung-uk Kim                 ok = (X509_load_cert_crl_file
1076f9291ceSJung-uk Kim                       (ctx, X509_get_default_cert_file(),
108f579bf8eSKris Kennaway                        X509_FILETYPE_PEM) != 0);
1095c87c606SMark Murray 
1106f9291ceSJung-uk Kim             if (!ok) {
11174664626SKris Kennaway                 X509err(X509_F_BY_FILE_CTRL, X509_R_LOADING_DEFAULTS);
11274664626SKris Kennaway             }
1136f9291ceSJung-uk Kim         } else {
114f579bf8eSKris Kennaway             if (argl == X509_FILETYPE_PEM)
115f579bf8eSKris Kennaway                 ok = (X509_load_cert_crl_file(ctx, argp,
116f579bf8eSKris Kennaway                                               X509_FILETYPE_PEM) != 0);
117f579bf8eSKris Kennaway             else
118f579bf8eSKris Kennaway                 ok = (X509_load_cert_file(ctx, argp, (int)argl) != 0);
11974664626SKris Kennaway         }
12074664626SKris Kennaway         break;
12174664626SKris Kennaway     }
122f579bf8eSKris Kennaway     return (ok);
12374664626SKris Kennaway }
12474664626SKris Kennaway 
12574664626SKris Kennaway int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type)
12674664626SKris Kennaway {
12774664626SKris Kennaway     int ret = 0;
12874664626SKris Kennaway     BIO *in = NULL;
12974664626SKris Kennaway     int i, count = 0;
13074664626SKris Kennaway     X509 *x = NULL;
13174664626SKris Kennaway 
1326f9291ceSJung-uk Kim     if (file == NULL)
1336f9291ceSJung-uk Kim         return (1);
13474664626SKris Kennaway     in = BIO_new(BIO_s_file_internal());
13574664626SKris Kennaway 
1366f9291ceSJung-uk Kim     if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
13774664626SKris Kennaway         X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_SYS_LIB);
13874664626SKris Kennaway         goto err;
13974664626SKris Kennaway     }
14074664626SKris Kennaway 
1416f9291ceSJung-uk Kim     if (type == X509_FILETYPE_PEM) {
1426f9291ceSJung-uk Kim         for (;;) {
143*47902a71SJung-uk Kim             x = PEM_read_bio_X509_AUX(in, NULL, NULL, "");
1446f9291ceSJung-uk Kim             if (x == NULL) {
1453b4e3dcbSSimon L. B. Nielsen                 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
1466f9291ceSJung-uk Kim                      PEM_R_NO_START_LINE) && (count > 0)) {
14774664626SKris Kennaway                     ERR_clear_error();
14874664626SKris Kennaway                     break;
1496f9291ceSJung-uk Kim                 } else {
1506f9291ceSJung-uk Kim                     X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_PEM_LIB);
15174664626SKris Kennaway                     goto err;
15274664626SKris Kennaway                 }
15374664626SKris Kennaway             }
15474664626SKris Kennaway             i = X509_STORE_add_cert(ctx->store_ctx, x);
1556f9291ceSJung-uk Kim             if (!i)
1566f9291ceSJung-uk Kim                 goto err;
15774664626SKris Kennaway             count++;
15874664626SKris Kennaway             X509_free(x);
15974664626SKris Kennaway             x = NULL;
16074664626SKris Kennaway         }
16174664626SKris Kennaway         ret = count;
1626f9291ceSJung-uk Kim     } else if (type == X509_FILETYPE_ASN1) {
16374664626SKris Kennaway         x = d2i_X509_bio(in, NULL);
1646f9291ceSJung-uk Kim         if (x == NULL) {
16574664626SKris Kennaway             X509err(X509_F_X509_LOAD_CERT_FILE, ERR_R_ASN1_LIB);
16674664626SKris Kennaway             goto err;
16774664626SKris Kennaway         }
16874664626SKris Kennaway         i = X509_STORE_add_cert(ctx->store_ctx, x);
1696f9291ceSJung-uk Kim         if (!i)
1706f9291ceSJung-uk Kim             goto err;
17174664626SKris Kennaway         ret = i;
1726f9291ceSJung-uk Kim     } else {
17374664626SKris Kennaway         X509err(X509_F_X509_LOAD_CERT_FILE, X509_R_BAD_X509_FILETYPE);
17474664626SKris Kennaway         goto err;
17574664626SKris Kennaway     }
17674664626SKris Kennaway  err:
1776f9291ceSJung-uk Kim     if (x != NULL)
1786f9291ceSJung-uk Kim         X509_free(x);
1796f9291ceSJung-uk Kim     if (in != NULL)
1806f9291ceSJung-uk Kim         BIO_free(in);
18174664626SKris Kennaway     return (ret);
18274664626SKris Kennaway }
18374664626SKris Kennaway 
18474664626SKris Kennaway int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type)
18574664626SKris Kennaway {
18674664626SKris Kennaway     int ret = 0;
18774664626SKris Kennaway     BIO *in = NULL;
18874664626SKris Kennaway     int i, count = 0;
18974664626SKris Kennaway     X509_CRL *x = NULL;
19074664626SKris Kennaway 
1916f9291ceSJung-uk Kim     if (file == NULL)
1926f9291ceSJung-uk Kim         return (1);
19374664626SKris Kennaway     in = BIO_new(BIO_s_file_internal());
19474664626SKris Kennaway 
1956f9291ceSJung-uk Kim     if ((in == NULL) || (BIO_read_filename(in, file) <= 0)) {
19674664626SKris Kennaway         X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_SYS_LIB);
19774664626SKris Kennaway         goto err;
19874664626SKris Kennaway     }
19974664626SKris Kennaway 
2006f9291ceSJung-uk Kim     if (type == X509_FILETYPE_PEM) {
2016f9291ceSJung-uk Kim         for (;;) {
202*47902a71SJung-uk Kim             x = PEM_read_bio_X509_CRL(in, NULL, NULL, "");
2036f9291ceSJung-uk Kim             if (x == NULL) {
2043b4e3dcbSSimon L. B. Nielsen                 if ((ERR_GET_REASON(ERR_peek_last_error()) ==
2056f9291ceSJung-uk Kim                      PEM_R_NO_START_LINE) && (count > 0)) {
20674664626SKris Kennaway                     ERR_clear_error();
20774664626SKris Kennaway                     break;
2086f9291ceSJung-uk Kim                 } else {
2096f9291ceSJung-uk Kim                     X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_PEM_LIB);
21074664626SKris Kennaway                     goto err;
21174664626SKris Kennaway                 }
21274664626SKris Kennaway             }
21374664626SKris Kennaway             i = X509_STORE_add_crl(ctx->store_ctx, x);
2146f9291ceSJung-uk Kim             if (!i)
2156f9291ceSJung-uk Kim                 goto err;
21674664626SKris Kennaway             count++;
21774664626SKris Kennaway             X509_CRL_free(x);
21874664626SKris Kennaway             x = NULL;
21974664626SKris Kennaway         }
22074664626SKris Kennaway         ret = count;
2216f9291ceSJung-uk Kim     } else if (type == X509_FILETYPE_ASN1) {
22274664626SKris Kennaway         x = d2i_X509_CRL_bio(in, NULL);
2236f9291ceSJung-uk Kim         if (x == NULL) {
22474664626SKris Kennaway             X509err(X509_F_X509_LOAD_CRL_FILE, ERR_R_ASN1_LIB);
22574664626SKris Kennaway             goto err;
22674664626SKris Kennaway         }
22774664626SKris Kennaway         i = X509_STORE_add_crl(ctx->store_ctx, x);
2286f9291ceSJung-uk Kim         if (!i)
2296f9291ceSJung-uk Kim             goto err;
23074664626SKris Kennaway         ret = i;
2316f9291ceSJung-uk Kim     } else {
23274664626SKris Kennaway         X509err(X509_F_X509_LOAD_CRL_FILE, X509_R_BAD_X509_FILETYPE);
23374664626SKris Kennaway         goto err;
23474664626SKris Kennaway     }
23574664626SKris Kennaway  err:
2366f9291ceSJung-uk Kim     if (x != NULL)
2376f9291ceSJung-uk Kim         X509_CRL_free(x);
2386f9291ceSJung-uk Kim     if (in != NULL)
2396f9291ceSJung-uk Kim         BIO_free(in);
24074664626SKris Kennaway     return (ret);
24174664626SKris Kennaway }
24274664626SKris Kennaway 
243f579bf8eSKris Kennaway int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type)
244f579bf8eSKris Kennaway {
245f579bf8eSKris Kennaway     STACK_OF(X509_INFO) *inf;
246f579bf8eSKris Kennaway     X509_INFO *itmp;
247f579bf8eSKris Kennaway     BIO *in;
248f579bf8eSKris Kennaway     int i, count = 0;
249f579bf8eSKris Kennaway     if (type != X509_FILETYPE_PEM)
250f579bf8eSKris Kennaway         return X509_load_cert_file(ctx, file, type);
251f579bf8eSKris Kennaway     in = BIO_new_file(file, "r");
252f579bf8eSKris Kennaway     if (!in) {
253f579bf8eSKris Kennaway         X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_SYS_LIB);
254f579bf8eSKris Kennaway         return 0;
255f579bf8eSKris Kennaway     }
256*47902a71SJung-uk Kim     inf = PEM_X509_INFO_read_bio(in, NULL, NULL, "");
257f579bf8eSKris Kennaway     BIO_free(in);
258f579bf8eSKris Kennaway     if (!inf) {
259f579bf8eSKris Kennaway         X509err(X509_F_X509_LOAD_CERT_CRL_FILE, ERR_R_PEM_LIB);
260f579bf8eSKris Kennaway         return 0;
261f579bf8eSKris Kennaway     }
262f579bf8eSKris Kennaway     for (i = 0; i < sk_X509_INFO_num(inf); i++) {
263f579bf8eSKris Kennaway         itmp = sk_X509_INFO_value(inf, i);
264f579bf8eSKris Kennaway         if (itmp->x509) {
265f579bf8eSKris Kennaway             X509_STORE_add_cert(ctx->store_ctx, itmp->x509);
266f579bf8eSKris Kennaway             count++;
26750ef0093SJacques Vidrine         }
26850ef0093SJacques Vidrine         if (itmp->crl) {
269f579bf8eSKris Kennaway             X509_STORE_add_crl(ctx->store_ctx, itmp->crl);
270f579bf8eSKris Kennaway             count++;
271f579bf8eSKris Kennaway         }
272f579bf8eSKris Kennaway     }
273f579bf8eSKris Kennaway     sk_X509_INFO_pop_free(inf, X509_INFO_free);
274f579bf8eSKris Kennaway     return count;
275f579bf8eSKris Kennaway }
276f579bf8eSKris Kennaway 
2775c87c606SMark Murray #endif                          /* OPENSSL_NO_STDIO */
278