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 6974664626SKris Kennaway #ifndef 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); 7374664626SKris Kennaway X509_LOOKUP_METHOD x509_file_lookup= 7474664626SKris Kennaway { 7574664626SKris Kennaway "Load file into cache", 7674664626SKris Kennaway NULL, /* new */ 7774664626SKris Kennaway NULL, /* free */ 7874664626SKris Kennaway NULL, /* init */ 7974664626SKris Kennaway NULL, /* shutdown */ 8074664626SKris Kennaway by_file_ctrl, /* ctrl */ 8174664626SKris Kennaway NULL, /* get_by_subject */ 8274664626SKris Kennaway NULL, /* get_by_issuer_serial */ 8374664626SKris Kennaway NULL, /* get_by_fingerprint */ 8474664626SKris Kennaway NULL, /* get_by_alias */ 8574664626SKris Kennaway }; 8674664626SKris Kennaway 8774664626SKris Kennaway X509_LOOKUP_METHOD *X509_LOOKUP_file(void) 8874664626SKris Kennaway { 8974664626SKris Kennaway return(&x509_file_lookup); 9074664626SKris Kennaway } 9174664626SKris Kennaway 9274664626SKris Kennaway static int by_file_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl, 9374664626SKris Kennaway char **ret) 9474664626SKris Kennaway { 95f579bf8eSKris Kennaway int ok=0; 9674664626SKris Kennaway char *file; 9774664626SKris Kennaway 9874664626SKris Kennaway switch (cmd) 9974664626SKris Kennaway { 10074664626SKris Kennaway case X509_L_FILE_LOAD: 10174664626SKris Kennaway if (argl == X509_FILETYPE_DEFAULT) 10274664626SKris Kennaway { 103f579bf8eSKris Kennaway ok = (X509_load_cert_crl_file(ctx,X509_get_default_cert_file(), 104f579bf8eSKris Kennaway X509_FILETYPE_PEM) != 0); 105f579bf8eSKris Kennaway if (!ok) 10674664626SKris Kennaway { 10774664626SKris Kennaway X509err(X509_F_BY_FILE_CTRL,X509_R_LOADING_DEFAULTS); 10874664626SKris Kennaway } 10974664626SKris Kennaway else 11074664626SKris Kennaway { 11174664626SKris Kennaway file=(char *)Getenv(X509_get_default_cert_file_env()); 112f579bf8eSKris Kennaway ok = (X509_load_cert_crl_file(ctx,file, 113f579bf8eSKris Kennaway X509_FILETYPE_PEM) != 0); 11474664626SKris Kennaway } 11574664626SKris Kennaway } 11674664626SKris Kennaway else 11774664626SKris Kennaway { 118f579bf8eSKris Kennaway if(argl == X509_FILETYPE_PEM) 119f579bf8eSKris Kennaway ok = (X509_load_cert_crl_file(ctx,argp, 120f579bf8eSKris Kennaway X509_FILETYPE_PEM) != 0); 121f579bf8eSKris Kennaway else 122f579bf8eSKris Kennaway ok = (X509_load_cert_file(ctx,argp,(int)argl) != 0); 12374664626SKris Kennaway } 12474664626SKris Kennaway break; 12574664626SKris Kennaway } 126f579bf8eSKris Kennaway return(ok); 12774664626SKris Kennaway } 12874664626SKris Kennaway 12974664626SKris Kennaway int X509_load_cert_file(X509_LOOKUP *ctx, const char *file, int type) 13074664626SKris Kennaway { 13174664626SKris Kennaway int ret=0; 13274664626SKris Kennaway BIO *in=NULL; 13374664626SKris Kennaway int i,count=0; 13474664626SKris Kennaway X509 *x=NULL; 13574664626SKris Kennaway 13674664626SKris Kennaway if (file == NULL) return(1); 13774664626SKris Kennaway in=BIO_new(BIO_s_file_internal()); 13874664626SKris Kennaway 13974664626SKris Kennaway if ((in == NULL) || (BIO_read_filename(in,file) <= 0)) 14074664626SKris Kennaway { 14174664626SKris Kennaway X509err(X509_F_X509_LOAD_CERT_FILE,ERR_R_SYS_LIB); 14274664626SKris Kennaway goto err; 14374664626SKris Kennaway } 14474664626SKris Kennaway 14574664626SKris Kennaway if (type == X509_FILETYPE_PEM) 14674664626SKris Kennaway { 14774664626SKris Kennaway for (;;) 14874664626SKris Kennaway { 149f579bf8eSKris Kennaway x=PEM_read_bio_X509_AUX(in,NULL,NULL,NULL); 15074664626SKris Kennaway if (x == NULL) 15174664626SKris Kennaway { 15274664626SKris Kennaway if ((ERR_GET_REASON(ERR_peek_error()) == 15374664626SKris Kennaway PEM_R_NO_START_LINE) && (count > 0)) 15474664626SKris Kennaway { 15574664626SKris Kennaway ERR_clear_error(); 15674664626SKris Kennaway break; 15774664626SKris Kennaway } 15874664626SKris Kennaway else 15974664626SKris Kennaway { 16074664626SKris Kennaway X509err(X509_F_X509_LOAD_CERT_FILE, 16174664626SKris Kennaway ERR_R_PEM_LIB); 16274664626SKris Kennaway goto err; 16374664626SKris Kennaway } 16474664626SKris Kennaway } 16574664626SKris Kennaway i=X509_STORE_add_cert(ctx->store_ctx,x); 16674664626SKris Kennaway if (!i) goto err; 16774664626SKris Kennaway count++; 16874664626SKris Kennaway X509_free(x); 16974664626SKris Kennaway x=NULL; 17074664626SKris Kennaway } 17174664626SKris Kennaway ret=count; 17274664626SKris Kennaway } 17374664626SKris Kennaway else if (type == X509_FILETYPE_ASN1) 17474664626SKris Kennaway { 17574664626SKris Kennaway x=d2i_X509_bio(in,NULL); 17674664626SKris Kennaway if (x == NULL) 17774664626SKris Kennaway { 17874664626SKris Kennaway X509err(X509_F_X509_LOAD_CERT_FILE,ERR_R_ASN1_LIB); 17974664626SKris Kennaway goto err; 18074664626SKris Kennaway } 18174664626SKris Kennaway i=X509_STORE_add_cert(ctx->store_ctx,x); 18274664626SKris Kennaway if (!i) goto err; 18374664626SKris Kennaway ret=i; 18474664626SKris Kennaway } 18574664626SKris Kennaway else 18674664626SKris Kennaway { 18774664626SKris Kennaway X509err(X509_F_X509_LOAD_CERT_FILE,X509_R_BAD_X509_FILETYPE); 18874664626SKris Kennaway goto err; 18974664626SKris Kennaway } 19074664626SKris Kennaway err: 19174664626SKris Kennaway if (x != NULL) X509_free(x); 19274664626SKris Kennaway if (in != NULL) BIO_free(in); 19374664626SKris Kennaway return(ret); 19474664626SKris Kennaway } 19574664626SKris Kennaway 19674664626SKris Kennaway int X509_load_crl_file(X509_LOOKUP *ctx, const char *file, int type) 19774664626SKris Kennaway { 19874664626SKris Kennaway int ret=0; 19974664626SKris Kennaway BIO *in=NULL; 20074664626SKris Kennaway int i,count=0; 20174664626SKris Kennaway X509_CRL *x=NULL; 20274664626SKris Kennaway 20374664626SKris Kennaway if (file == NULL) return(1); 20474664626SKris Kennaway in=BIO_new(BIO_s_file_internal()); 20574664626SKris Kennaway 20674664626SKris Kennaway if ((in == NULL) || (BIO_read_filename(in,file) <= 0)) 20774664626SKris Kennaway { 20874664626SKris Kennaway X509err(X509_F_X509_LOAD_CRL_FILE,ERR_R_SYS_LIB); 20974664626SKris Kennaway goto err; 21074664626SKris Kennaway } 21174664626SKris Kennaway 21274664626SKris Kennaway if (type == X509_FILETYPE_PEM) 21374664626SKris Kennaway { 21474664626SKris Kennaway for (;;) 21574664626SKris Kennaway { 21674664626SKris Kennaway x=PEM_read_bio_X509_CRL(in,NULL,NULL,NULL); 21774664626SKris Kennaway if (x == NULL) 21874664626SKris Kennaway { 21974664626SKris Kennaway if ((ERR_GET_REASON(ERR_peek_error()) == 22074664626SKris Kennaway PEM_R_NO_START_LINE) && (count > 0)) 22174664626SKris Kennaway { 22274664626SKris Kennaway ERR_clear_error(); 22374664626SKris Kennaway break; 22474664626SKris Kennaway } 22574664626SKris Kennaway else 22674664626SKris Kennaway { 22774664626SKris Kennaway X509err(X509_F_X509_LOAD_CRL_FILE, 22874664626SKris Kennaway ERR_R_PEM_LIB); 22974664626SKris Kennaway goto err; 23074664626SKris Kennaway } 23174664626SKris Kennaway } 23274664626SKris Kennaway i=X509_STORE_add_crl(ctx->store_ctx,x); 23374664626SKris Kennaway if (!i) goto err; 23474664626SKris Kennaway count++; 23574664626SKris Kennaway X509_CRL_free(x); 23674664626SKris Kennaway x=NULL; 23774664626SKris Kennaway } 23874664626SKris Kennaway ret=count; 23974664626SKris Kennaway } 24074664626SKris Kennaway else if (type == X509_FILETYPE_ASN1) 24174664626SKris Kennaway { 24274664626SKris Kennaway x=d2i_X509_CRL_bio(in,NULL); 24374664626SKris Kennaway if (x == NULL) 24474664626SKris Kennaway { 24574664626SKris Kennaway X509err(X509_F_X509_LOAD_CRL_FILE,ERR_R_ASN1_LIB); 24674664626SKris Kennaway goto err; 24774664626SKris Kennaway } 24874664626SKris Kennaway i=X509_STORE_add_crl(ctx->store_ctx,x); 24974664626SKris Kennaway if (!i) goto err; 25074664626SKris Kennaway ret=i; 25174664626SKris Kennaway } 25274664626SKris Kennaway else 25374664626SKris Kennaway { 25474664626SKris Kennaway X509err(X509_F_X509_LOAD_CRL_FILE,X509_R_BAD_X509_FILETYPE); 25574664626SKris Kennaway goto err; 25674664626SKris Kennaway } 25774664626SKris Kennaway err: 25874664626SKris Kennaway if (x != NULL) X509_CRL_free(x); 25974664626SKris Kennaway if (in != NULL) BIO_free(in); 26074664626SKris Kennaway return(ret); 26174664626SKris Kennaway } 26274664626SKris Kennaway 263f579bf8eSKris Kennaway int X509_load_cert_crl_file(X509_LOOKUP *ctx, const char *file, int type) 264f579bf8eSKris Kennaway { 265f579bf8eSKris Kennaway STACK_OF(X509_INFO) *inf; 266f579bf8eSKris Kennaway X509_INFO *itmp; 267f579bf8eSKris Kennaway BIO *in; 268f579bf8eSKris Kennaway int i, count = 0; 269f579bf8eSKris Kennaway if(type != X509_FILETYPE_PEM) 270f579bf8eSKris Kennaway return X509_load_cert_file(ctx, file, type); 271f579bf8eSKris Kennaway in = BIO_new_file(file, "r"); 272f579bf8eSKris Kennaway if(!in) { 273f579bf8eSKris Kennaway X509err(X509_F_X509_LOAD_CERT_CRL_FILE,ERR_R_SYS_LIB); 274f579bf8eSKris Kennaway return 0; 275f579bf8eSKris Kennaway } 276f579bf8eSKris Kennaway inf = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL); 277f579bf8eSKris Kennaway BIO_free(in); 278f579bf8eSKris Kennaway if(!inf) { 279f579bf8eSKris Kennaway X509err(X509_F_X509_LOAD_CERT_CRL_FILE,ERR_R_PEM_LIB); 280f579bf8eSKris Kennaway return 0; 281f579bf8eSKris Kennaway } 282f579bf8eSKris Kennaway for(i = 0; i < sk_X509_INFO_num(inf); i++) { 283f579bf8eSKris Kennaway itmp = sk_X509_INFO_value(inf, i); 284f579bf8eSKris Kennaway if(itmp->x509) { 285f579bf8eSKris Kennaway X509_STORE_add_cert(ctx->store_ctx, itmp->x509); 286f579bf8eSKris Kennaway count++; 287f579bf8eSKris Kennaway } else if(itmp->crl) { 288f579bf8eSKris Kennaway X509_STORE_add_crl(ctx->store_ctx, itmp->crl); 289f579bf8eSKris Kennaway count++; 290f579bf8eSKris Kennaway } 291f579bf8eSKris Kennaway } 292f579bf8eSKris Kennaway sk_X509_INFO_pop_free(inf, X509_INFO_free); 293f579bf8eSKris Kennaway return count; 294f579bf8eSKris Kennaway } 295f579bf8eSKris Kennaway 296f579bf8eSKris Kennaway 29774664626SKris Kennaway #endif /* NO_STDIO */ 29874664626SKris Kennaway 299