sl@0: /* crypto/x509/by_dir.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include "cryptlib.h" sl@0: sl@0: #ifndef NO_SYS_TYPES_H sl@0: # include sl@0: #endif sl@0: #ifdef MAC_OS_pre_X sl@0: # include sl@0: #else sl@0: # include sl@0: #endif sl@0: sl@0: #include sl@0: #include sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: sl@0: typedef struct lookup_dir_st sl@0: { sl@0: BUF_MEM *buffer; sl@0: int num_dirs; sl@0: char **dirs; sl@0: int *dirs_type; sl@0: int num_dirs_alloced; sl@0: } BY_DIR; sl@0: sl@0: static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl, sl@0: char **ret); sl@0: static int new_dir(X509_LOOKUP *lu); sl@0: static void free_dir(X509_LOOKUP *lu); sl@0: static int add_cert_dir(BY_DIR *ctx,const char *dir,int type); sl@0: static int get_cert_by_subject(X509_LOOKUP *xl,int type,X509_NAME *name, sl@0: X509_OBJECT *ret); sl@0: #ifndef EMULATOR sl@0: X509_LOOKUP_METHOD x509_dir_lookup= sl@0: { sl@0: "Load certs from files in a directory", sl@0: new_dir, /* new */ sl@0: free_dir, /* free */ sl@0: NULL, /* init */ sl@0: NULL, /* shutdown */ sl@0: dir_ctrl, /* ctrl */ sl@0: get_cert_by_subject, /* get_by_subject */ sl@0: NULL, /* get_by_issuer_serial */ sl@0: NULL, /* get_by_fingerprint */ sl@0: NULL, /* get_by_alias */ sl@0: }; sl@0: #else sl@0: GET_GLOBAL_VAR_FROM_TLS(x509_dir_lookup,by_dir,X509_LOOKUP_METHOD) sl@0: #define x509_dir_lookup (*GET_WSD_VAR_NAME(x509_dir_lookup,by_dir, g)()) sl@0: const X509_LOOKUP_METHOD temp_g_x509_dir_lookup= sl@0: { sl@0: "Load certs from files in a directory", sl@0: new_dir, /* new */ sl@0: free_dir, /* free */ sl@0: NULL, /* init */ sl@0: NULL, /* shutdown */ sl@0: dir_ctrl, /* ctrl */ sl@0: get_cert_by_subject, /* get_by_subject */ sl@0: NULL, /* get_by_issuer_serial */ sl@0: NULL, /* get_by_fingerprint */ sl@0: NULL, /* get_by_alias */ sl@0: }; sl@0: #endif sl@0: sl@0: EXPORT_C X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void) sl@0: { sl@0: return(&x509_dir_lookup); sl@0: } sl@0: sl@0: static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl, sl@0: char **retp) sl@0: { sl@0: int ret=0; sl@0: BY_DIR *ld; sl@0: char *dir = NULL; sl@0: sl@0: ld=(BY_DIR *)ctx->method_data; sl@0: sl@0: switch (cmd) sl@0: { sl@0: case X509_L_ADD_DIR: sl@0: if (argl == X509_FILETYPE_DEFAULT) sl@0: { sl@0: dir=(char *)Getenv(X509_get_default_cert_dir_env()); sl@0: if (dir) sl@0: ret=add_cert_dir(ld,dir,X509_FILETYPE_PEM); sl@0: else sl@0: ret=add_cert_dir(ld,X509_get_default_cert_dir(), sl@0: X509_FILETYPE_PEM); sl@0: if (!ret) sl@0: { sl@0: X509err(X509_F_DIR_CTRL,X509_R_LOADING_CERT_DIR); sl@0: } sl@0: } sl@0: else sl@0: ret=add_cert_dir(ld,argp,(int)argl); sl@0: break; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: static int new_dir(X509_LOOKUP *lu) sl@0: { sl@0: BY_DIR *a; sl@0: sl@0: if ((a=(BY_DIR *)OPENSSL_malloc(sizeof(BY_DIR))) == NULL) sl@0: return(0); sl@0: if ((a->buffer=BUF_MEM_new()) == NULL) sl@0: { sl@0: OPENSSL_free(a); sl@0: return(0); sl@0: } sl@0: a->num_dirs=0; sl@0: a->dirs=NULL; sl@0: a->dirs_type=NULL; sl@0: a->num_dirs_alloced=0; sl@0: lu->method_data=(char *)a; sl@0: return(1); sl@0: } sl@0: sl@0: static void free_dir(X509_LOOKUP *lu) sl@0: { sl@0: BY_DIR *a; sl@0: int i; sl@0: sl@0: a=(BY_DIR *)lu->method_data; sl@0: for (i=0; inum_dirs; i++) sl@0: if (a->dirs[i] != NULL) OPENSSL_free(a->dirs[i]); sl@0: if (a->dirs != NULL) OPENSSL_free(a->dirs); sl@0: if (a->dirs_type != NULL) OPENSSL_free(a->dirs_type); sl@0: if (a->buffer != NULL) BUF_MEM_free(a->buffer); sl@0: OPENSSL_free(a); sl@0: } sl@0: sl@0: static int add_cert_dir(BY_DIR *ctx, const char *dir, int type) sl@0: { sl@0: int j,len; sl@0: int *ip; sl@0: const char *s,*ss,*p; sl@0: char **pp; sl@0: sl@0: if (dir == NULL || !*dir) sl@0: { sl@0: X509err(X509_F_ADD_CERT_DIR,X509_R_INVALID_DIRECTORY); sl@0: return 0; sl@0: } sl@0: sl@0: s=dir; sl@0: p=s; sl@0: for (;;p++) sl@0: { sl@0: if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0')) sl@0: { sl@0: ss=s; sl@0: s=p+1; sl@0: len=(int)(p-ss); sl@0: if (len == 0) continue; sl@0: for (j=0; jnum_dirs; j++) sl@0: if (strlen(ctx->dirs[j]) == (size_t)len && sl@0: strncmp(ctx->dirs[j],ss,(unsigned int)len) == 0) sl@0: break; sl@0: if (jnum_dirs) sl@0: continue; sl@0: if (ctx->num_dirs_alloced < (ctx->num_dirs+1)) sl@0: { sl@0: ctx->num_dirs_alloced+=10; sl@0: pp=(char **)OPENSSL_malloc(ctx->num_dirs_alloced* sl@0: sizeof(char *)); sl@0: ip=(int *)OPENSSL_malloc(ctx->num_dirs_alloced* sl@0: sizeof(int)); sl@0: if ((pp == NULL) || (ip == NULL)) sl@0: { sl@0: X509err(X509_F_ADD_CERT_DIR,ERR_R_MALLOC_FAILURE); sl@0: return(0); sl@0: } sl@0: memcpy(pp,ctx->dirs,(ctx->num_dirs_alloced-10)* sl@0: sizeof(char *)); sl@0: memcpy(ip,ctx->dirs_type,(ctx->num_dirs_alloced-10)* sl@0: sizeof(int)); sl@0: if (ctx->dirs != NULL) sl@0: OPENSSL_free(ctx->dirs); sl@0: if (ctx->dirs_type != NULL) sl@0: OPENSSL_free(ctx->dirs_type); sl@0: ctx->dirs=pp; sl@0: ctx->dirs_type=ip; sl@0: } sl@0: ctx->dirs_type[ctx->num_dirs]=type; sl@0: ctx->dirs[ctx->num_dirs]=(char *)OPENSSL_malloc((unsigned int)len+1); sl@0: if (ctx->dirs[ctx->num_dirs] == NULL) return(0); sl@0: strncpy(ctx->dirs[ctx->num_dirs],ss,(unsigned int)len); sl@0: ctx->dirs[ctx->num_dirs][len]='\0'; sl@0: ctx->num_dirs++; sl@0: } sl@0: if (*p == '\0') break; sl@0: } sl@0: return(1); sl@0: } sl@0: sl@0: static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name, sl@0: X509_OBJECT *ret) sl@0: { sl@0: BY_DIR *ctx; sl@0: union { sl@0: struct { sl@0: X509 st_x509; sl@0: X509_CINF st_x509_cinf; sl@0: } x509; sl@0: struct { sl@0: X509_CRL st_crl; sl@0: X509_CRL_INFO st_crl_info; sl@0: } crl; sl@0: } data; sl@0: int ok=0; sl@0: int i,j,k; sl@0: unsigned long h; sl@0: BUF_MEM *b=NULL; sl@0: struct stat st; sl@0: X509_OBJECT stmp,*tmp; sl@0: const char *postfix=""; sl@0: sl@0: if (name == NULL) return(0); sl@0: sl@0: stmp.type=type; sl@0: if (type == X509_LU_X509) sl@0: { sl@0: data.x509.st_x509.cert_info= &data.x509.st_x509_cinf; sl@0: data.x509.st_x509_cinf.subject=name; sl@0: stmp.data.x509= &data.x509.st_x509; sl@0: postfix=""; sl@0: } sl@0: else if (type == X509_LU_CRL) sl@0: { sl@0: data.crl.st_crl.crl= &data.crl.st_crl_info; sl@0: data.crl.st_crl_info.issuer=name; sl@0: stmp.data.crl= &data.crl.st_crl; sl@0: postfix="r"; sl@0: } sl@0: else sl@0: { sl@0: X509err(X509_F_GET_CERT_BY_SUBJECT,X509_R_WRONG_LOOKUP_TYPE); sl@0: goto finish; sl@0: } sl@0: sl@0: if ((b=BUF_MEM_new()) == NULL) sl@0: { sl@0: X509err(X509_F_GET_CERT_BY_SUBJECT,ERR_R_BUF_LIB); sl@0: goto finish; sl@0: } sl@0: sl@0: ctx=(BY_DIR *)xl->method_data; sl@0: sl@0: h=X509_NAME_hash(name); sl@0: for (i=0; inum_dirs; i++) sl@0: { sl@0: j=strlen(ctx->dirs[i])+1+8+6+1+1; sl@0: if (!BUF_MEM_grow(b,j)) sl@0: { sl@0: X509err(X509_F_GET_CERT_BY_SUBJECT,ERR_R_MALLOC_FAILURE); sl@0: goto finish; sl@0: } sl@0: k=0; sl@0: for (;;) sl@0: { sl@0: char c = '/'; sl@0: #ifdef OPENSSL_SYS_VMS sl@0: c = ctx->dirs[i][strlen(ctx->dirs[i])-1]; sl@0: if (c != ':' && c != '>' && c != ']') sl@0: { sl@0: /* If no separator is present, we assume the sl@0: directory specifier is a logical name, and sl@0: add a colon. We really should use better sl@0: VMS routines for merging things like this, sl@0: but this will do for now... sl@0: -- Richard Levitte */ sl@0: c = ':'; sl@0: } sl@0: else sl@0: { sl@0: c = '\0'; sl@0: } sl@0: #endif sl@0: if (c == '\0') sl@0: { sl@0: /* This is special. When c == '\0', no sl@0: directory separator should be added. */ sl@0: BIO_snprintf(b->data,b->max, sl@0: "%s%08lx.%s%d",ctx->dirs[i],h, sl@0: postfix,k); sl@0: } sl@0: else sl@0: { sl@0: BIO_snprintf(b->data,b->max, sl@0: "%s%c%08lx.%s%d",ctx->dirs[i],c,h, sl@0: postfix,k); sl@0: } sl@0: k++; sl@0: if (stat(b->data,&st) < 0) sl@0: break; sl@0: /* found one. */ sl@0: if (type == X509_LU_X509) sl@0: { sl@0: if ((X509_load_cert_file(xl,b->data, sl@0: ctx->dirs_type[i])) == 0) sl@0: break; sl@0: } sl@0: else if (type == X509_LU_CRL) sl@0: { sl@0: if ((X509_load_crl_file(xl,b->data, sl@0: ctx->dirs_type[i])) == 0) sl@0: break; sl@0: } sl@0: /* else case will caught higher up */ sl@0: } sl@0: sl@0: /* we have added it to the cache so now pull sl@0: * it out again */ sl@0: CRYPTO_r_lock(CRYPTO_LOCK_X509_STORE); sl@0: j = sk_X509_OBJECT_find(xl->store_ctx->objs,&stmp); sl@0: if(j != -1) tmp=sk_X509_OBJECT_value(xl->store_ctx->objs,j); sl@0: else tmp = NULL; sl@0: CRYPTO_r_unlock(CRYPTO_LOCK_X509_STORE); sl@0: sl@0: if (tmp != NULL) sl@0: { sl@0: ok=1; sl@0: ret->type=tmp->type; sl@0: memcpy(&ret->data,&tmp->data,sizeof(ret->data)); sl@0: /* If we were going to up the reference count, sl@0: * we would need to do it on a perl 'type' sl@0: * basis */ sl@0: /* CRYPTO_add(&tmp->data.x509->references,1, sl@0: CRYPTO_LOCK_X509);*/ sl@0: goto finish; sl@0: } sl@0: } sl@0: finish: sl@0: if (b != NULL) BUF_MEM_free(b); sl@0: return(ok); sl@0: } sl@0: