os/ossrv/ssl/libcrypto/src/crypto/x509/by_dir.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* crypto/x509/by_dir.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
 /*
sl@0
    59
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    60
 */
sl@0
    61
sl@0
    62
#include <stdio.h>
sl@0
    63
#include <time.h>
sl@0
    64
#include <errno.h>
sl@0
    65
sl@0
    66
#include "cryptlib.h"
sl@0
    67
sl@0
    68
#ifndef NO_SYS_TYPES_H
sl@0
    69
# include <sys/types.h>
sl@0
    70
#endif
sl@0
    71
#ifdef MAC_OS_pre_X
sl@0
    72
# include <stat.h>
sl@0
    73
#else
sl@0
    74
# include <sys/stat.h>
sl@0
    75
#endif
sl@0
    76
sl@0
    77
#include <openssl/lhash.h>
sl@0
    78
#include <openssl/x509.h>
sl@0
    79
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0
    80
#include "libcrypto_wsd_macros.h"
sl@0
    81
#include "libcrypto_wsd.h"
sl@0
    82
#endif
sl@0
    83
sl@0
    84
sl@0
    85
typedef struct lookup_dir_st
sl@0
    86
	{
sl@0
    87
	BUF_MEM *buffer;
sl@0
    88
	int num_dirs;
sl@0
    89
	char **dirs;
sl@0
    90
	int *dirs_type;
sl@0
    91
	int num_dirs_alloced;
sl@0
    92
	} BY_DIR;
sl@0
    93
sl@0
    94
static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
sl@0
    95
	char **ret);
sl@0
    96
static int new_dir(X509_LOOKUP *lu);
sl@0
    97
static void free_dir(X509_LOOKUP *lu);
sl@0
    98
static int add_cert_dir(BY_DIR *ctx,const char *dir,int type);
sl@0
    99
static int get_cert_by_subject(X509_LOOKUP *xl,int type,X509_NAME *name,
sl@0
   100
	X509_OBJECT *ret);
sl@0
   101
#ifndef EMULATOR	
sl@0
   102
X509_LOOKUP_METHOD x509_dir_lookup=
sl@0
   103
	{
sl@0
   104
	"Load certs from files in a directory",
sl@0
   105
	new_dir,		/* new */
sl@0
   106
	free_dir,		/* free */
sl@0
   107
	NULL, 			/* init */
sl@0
   108
	NULL,			/* shutdown */
sl@0
   109
	dir_ctrl,		/* ctrl */
sl@0
   110
	get_cert_by_subject,	/* get_by_subject */
sl@0
   111
	NULL,			/* get_by_issuer_serial */
sl@0
   112
	NULL,			/* get_by_fingerprint */
sl@0
   113
	NULL,			/* get_by_alias */
sl@0
   114
	};
sl@0
   115
#else
sl@0
   116
GET_GLOBAL_VAR_FROM_TLS(x509_dir_lookup,by_dir,X509_LOOKUP_METHOD)
sl@0
   117
#define x509_dir_lookup (*GET_WSD_VAR_NAME(x509_dir_lookup,by_dir, g)())
sl@0
   118
const X509_LOOKUP_METHOD temp_g_x509_dir_lookup=
sl@0
   119
	{
sl@0
   120
	"Load certs from files in a directory",
sl@0
   121
	new_dir,		/* new */
sl@0
   122
	free_dir,		/* free */
sl@0
   123
	NULL, 			/* init */
sl@0
   124
	NULL,			/* shutdown */
sl@0
   125
	dir_ctrl,		/* ctrl */
sl@0
   126
	get_cert_by_subject,	/* get_by_subject */
sl@0
   127
	NULL,			/* get_by_issuer_serial */
sl@0
   128
	NULL,			/* get_by_fingerprint */
sl@0
   129
	NULL,			/* get_by_alias */
sl@0
   130
	};
sl@0
   131
#endif	
sl@0
   132
sl@0
   133
EXPORT_C X509_LOOKUP_METHOD *X509_LOOKUP_hash_dir(void)
sl@0
   134
	{
sl@0
   135
	return(&x509_dir_lookup);
sl@0
   136
	}
sl@0
   137
sl@0
   138
static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
sl@0
   139
	     char **retp)
sl@0
   140
	{
sl@0
   141
	int ret=0;
sl@0
   142
	BY_DIR *ld;
sl@0
   143
	char *dir = NULL;
sl@0
   144
sl@0
   145
	ld=(BY_DIR *)ctx->method_data;
sl@0
   146
sl@0
   147
	switch (cmd)
sl@0
   148
		{
sl@0
   149
	case X509_L_ADD_DIR:
sl@0
   150
		if (argl == X509_FILETYPE_DEFAULT)
sl@0
   151
			{
sl@0
   152
			dir=(char *)Getenv(X509_get_default_cert_dir_env());
sl@0
   153
			if (dir)
sl@0
   154
				ret=add_cert_dir(ld,dir,X509_FILETYPE_PEM);
sl@0
   155
			else
sl@0
   156
				ret=add_cert_dir(ld,X509_get_default_cert_dir(),
sl@0
   157
					X509_FILETYPE_PEM);
sl@0
   158
			if (!ret)
sl@0
   159
				{
sl@0
   160
				X509err(X509_F_DIR_CTRL,X509_R_LOADING_CERT_DIR);
sl@0
   161
				}
sl@0
   162
			}
sl@0
   163
		else
sl@0
   164
			ret=add_cert_dir(ld,argp,(int)argl);
sl@0
   165
		break;
sl@0
   166
		}
sl@0
   167
	return(ret);
sl@0
   168
	}
sl@0
   169
sl@0
   170
static int new_dir(X509_LOOKUP *lu)
sl@0
   171
	{
sl@0
   172
	BY_DIR *a;
sl@0
   173
sl@0
   174
	if ((a=(BY_DIR *)OPENSSL_malloc(sizeof(BY_DIR))) == NULL)
sl@0
   175
		return(0);
sl@0
   176
	if ((a->buffer=BUF_MEM_new()) == NULL)
sl@0
   177
		{
sl@0
   178
		OPENSSL_free(a);
sl@0
   179
		return(0);
sl@0
   180
		}
sl@0
   181
	a->num_dirs=0;
sl@0
   182
	a->dirs=NULL;
sl@0
   183
	a->dirs_type=NULL;
sl@0
   184
	a->num_dirs_alloced=0;
sl@0
   185
	lu->method_data=(char *)a;
sl@0
   186
	return(1);
sl@0
   187
	}
sl@0
   188
sl@0
   189
static void free_dir(X509_LOOKUP *lu)
sl@0
   190
	{
sl@0
   191
	BY_DIR *a;
sl@0
   192
	int i;
sl@0
   193
sl@0
   194
	a=(BY_DIR *)lu->method_data;
sl@0
   195
	for (i=0; i<a->num_dirs; i++)
sl@0
   196
		if (a->dirs[i] != NULL) OPENSSL_free(a->dirs[i]);
sl@0
   197
	if (a->dirs != NULL) OPENSSL_free(a->dirs);
sl@0
   198
	if (a->dirs_type != NULL) OPENSSL_free(a->dirs_type);
sl@0
   199
	if (a->buffer != NULL) BUF_MEM_free(a->buffer);
sl@0
   200
	OPENSSL_free(a);
sl@0
   201
	}
sl@0
   202
sl@0
   203
static int add_cert_dir(BY_DIR *ctx, const char *dir, int type)
sl@0
   204
	{
sl@0
   205
	int j,len;
sl@0
   206
	int *ip;
sl@0
   207
	const char *s,*ss,*p;
sl@0
   208
	char **pp;
sl@0
   209
sl@0
   210
	if (dir == NULL || !*dir)
sl@0
   211
	    {
sl@0
   212
	    X509err(X509_F_ADD_CERT_DIR,X509_R_INVALID_DIRECTORY);
sl@0
   213
	    return 0;
sl@0
   214
	    }
sl@0
   215
sl@0
   216
	s=dir;
sl@0
   217
	p=s;
sl@0
   218
	for (;;p++)
sl@0
   219
		{
sl@0
   220
		if ((*p == LIST_SEPARATOR_CHAR) || (*p == '\0'))
sl@0
   221
			{
sl@0
   222
			ss=s;
sl@0
   223
			s=p+1;
sl@0
   224
			len=(int)(p-ss);
sl@0
   225
			if (len == 0) continue;
sl@0
   226
			for (j=0; j<ctx->num_dirs; j++)
sl@0
   227
				if (strlen(ctx->dirs[j]) == (size_t)len &&
sl@0
   228
				    strncmp(ctx->dirs[j],ss,(unsigned int)len) == 0)
sl@0
   229
					break;
sl@0
   230
			if (j<ctx->num_dirs)
sl@0
   231
					continue;
sl@0
   232
			if (ctx->num_dirs_alloced < (ctx->num_dirs+1))
sl@0
   233
				{
sl@0
   234
				ctx->num_dirs_alloced+=10;
sl@0
   235
				pp=(char **)OPENSSL_malloc(ctx->num_dirs_alloced*
sl@0
   236
					sizeof(char *));
sl@0
   237
				ip=(int *)OPENSSL_malloc(ctx->num_dirs_alloced*
sl@0
   238
					sizeof(int));
sl@0
   239
				if ((pp == NULL) || (ip == NULL))
sl@0
   240
					{
sl@0
   241
					X509err(X509_F_ADD_CERT_DIR,ERR_R_MALLOC_FAILURE);
sl@0
   242
					return(0);
sl@0
   243
					}
sl@0
   244
				memcpy(pp,ctx->dirs,(ctx->num_dirs_alloced-10)*
sl@0
   245
					sizeof(char *));
sl@0
   246
				memcpy(ip,ctx->dirs_type,(ctx->num_dirs_alloced-10)*
sl@0
   247
					sizeof(int));
sl@0
   248
				if (ctx->dirs != NULL)
sl@0
   249
					OPENSSL_free(ctx->dirs);
sl@0
   250
				if (ctx->dirs_type != NULL)
sl@0
   251
					OPENSSL_free(ctx->dirs_type);
sl@0
   252
				ctx->dirs=pp;
sl@0
   253
				ctx->dirs_type=ip;
sl@0
   254
				}
sl@0
   255
			ctx->dirs_type[ctx->num_dirs]=type;
sl@0
   256
			ctx->dirs[ctx->num_dirs]=(char *)OPENSSL_malloc((unsigned int)len+1);
sl@0
   257
			if (ctx->dirs[ctx->num_dirs] == NULL) return(0);
sl@0
   258
			strncpy(ctx->dirs[ctx->num_dirs],ss,(unsigned int)len);
sl@0
   259
			ctx->dirs[ctx->num_dirs][len]='\0';
sl@0
   260
			ctx->num_dirs++;
sl@0
   261
			}
sl@0
   262
		if (*p == '\0') break;
sl@0
   263
		}
sl@0
   264
	return(1);
sl@0
   265
	}
sl@0
   266
sl@0
   267
static int get_cert_by_subject(X509_LOOKUP *xl, int type, X509_NAME *name,
sl@0
   268
	     X509_OBJECT *ret)
sl@0
   269
	{
sl@0
   270
	BY_DIR *ctx;
sl@0
   271
	union	{
sl@0
   272
		struct	{
sl@0
   273
			X509 st_x509;
sl@0
   274
			X509_CINF st_x509_cinf;
sl@0
   275
			} x509;
sl@0
   276
		struct	{
sl@0
   277
			X509_CRL st_crl;
sl@0
   278
			X509_CRL_INFO st_crl_info;
sl@0
   279
			} crl;
sl@0
   280
		} data;
sl@0
   281
	int ok=0;
sl@0
   282
	int i,j,k;
sl@0
   283
	unsigned long h;
sl@0
   284
	BUF_MEM *b=NULL;
sl@0
   285
	struct stat st;
sl@0
   286
	X509_OBJECT stmp,*tmp;
sl@0
   287
	const char *postfix="";
sl@0
   288
sl@0
   289
	if (name == NULL) return(0);
sl@0
   290
sl@0
   291
	stmp.type=type;
sl@0
   292
	if (type == X509_LU_X509)
sl@0
   293
		{
sl@0
   294
		data.x509.st_x509.cert_info= &data.x509.st_x509_cinf;
sl@0
   295
		data.x509.st_x509_cinf.subject=name;
sl@0
   296
		stmp.data.x509= &data.x509.st_x509;
sl@0
   297
		postfix="";
sl@0
   298
		}
sl@0
   299
	else if (type == X509_LU_CRL)
sl@0
   300
		{
sl@0
   301
		data.crl.st_crl.crl= &data.crl.st_crl_info;
sl@0
   302
		data.crl.st_crl_info.issuer=name;
sl@0
   303
		stmp.data.crl= &data.crl.st_crl;
sl@0
   304
		postfix="r";
sl@0
   305
		}
sl@0
   306
	else
sl@0
   307
		{
sl@0
   308
		X509err(X509_F_GET_CERT_BY_SUBJECT,X509_R_WRONG_LOOKUP_TYPE);
sl@0
   309
		goto finish;
sl@0
   310
		}
sl@0
   311
sl@0
   312
	if ((b=BUF_MEM_new()) == NULL)
sl@0
   313
		{
sl@0
   314
		X509err(X509_F_GET_CERT_BY_SUBJECT,ERR_R_BUF_LIB);
sl@0
   315
		goto finish;
sl@0
   316
		}
sl@0
   317
	
sl@0
   318
	ctx=(BY_DIR *)xl->method_data;
sl@0
   319
sl@0
   320
	h=X509_NAME_hash(name);
sl@0
   321
	for (i=0; i<ctx->num_dirs; i++)
sl@0
   322
		{
sl@0
   323
		j=strlen(ctx->dirs[i])+1+8+6+1+1;
sl@0
   324
		if (!BUF_MEM_grow(b,j))
sl@0
   325
			{
sl@0
   326
			X509err(X509_F_GET_CERT_BY_SUBJECT,ERR_R_MALLOC_FAILURE);
sl@0
   327
			goto finish;
sl@0
   328
			}
sl@0
   329
		k=0;
sl@0
   330
		for (;;)
sl@0
   331
			{
sl@0
   332
			char c = '/';
sl@0
   333
#ifdef OPENSSL_SYS_VMS
sl@0
   334
			c = ctx->dirs[i][strlen(ctx->dirs[i])-1];
sl@0
   335
			if (c != ':' && c != '>' && c != ']')
sl@0
   336
				{
sl@0
   337
				/* If no separator is present, we assume the
sl@0
   338
				   directory specifier is a logical name, and
sl@0
   339
				   add a colon.  We really should use better
sl@0
   340
				   VMS routines for merging things like this,
sl@0
   341
				   but this will do for now...
sl@0
   342
				   -- Richard Levitte */
sl@0
   343
				c = ':';
sl@0
   344
				}
sl@0
   345
			else
sl@0
   346
				{
sl@0
   347
				c = '\0';
sl@0
   348
				}
sl@0
   349
#endif
sl@0
   350
			if (c == '\0')
sl@0
   351
				{
sl@0
   352
				/* This is special.  When c == '\0', no
sl@0
   353
				   directory separator should be added. */
sl@0
   354
				BIO_snprintf(b->data,b->max,
sl@0
   355
					"%s%08lx.%s%d",ctx->dirs[i],h,
sl@0
   356
					postfix,k);
sl@0
   357
				}
sl@0
   358
			else
sl@0
   359
				{
sl@0
   360
				BIO_snprintf(b->data,b->max,
sl@0
   361
					"%s%c%08lx.%s%d",ctx->dirs[i],c,h,
sl@0
   362
					postfix,k);
sl@0
   363
				}
sl@0
   364
			k++;
sl@0
   365
			if (stat(b->data,&st) < 0)
sl@0
   366
				break;
sl@0
   367
			/* found one. */
sl@0
   368
			if (type == X509_LU_X509)
sl@0
   369
				{
sl@0
   370
				if ((X509_load_cert_file(xl,b->data,
sl@0
   371
					ctx->dirs_type[i])) == 0)
sl@0
   372
					break;
sl@0
   373
				}
sl@0
   374
			else if (type == X509_LU_CRL)
sl@0
   375
				{
sl@0
   376
				if ((X509_load_crl_file(xl,b->data,
sl@0
   377
					ctx->dirs_type[i])) == 0)
sl@0
   378
					break;
sl@0
   379
				}
sl@0
   380
			/* else case will caught higher up */
sl@0
   381
			}
sl@0
   382
sl@0
   383
		/* we have added it to the cache so now pull
sl@0
   384
		 * it out again */
sl@0
   385
		CRYPTO_r_lock(CRYPTO_LOCK_X509_STORE);
sl@0
   386
		j = sk_X509_OBJECT_find(xl->store_ctx->objs,&stmp);
sl@0
   387
		if(j != -1) tmp=sk_X509_OBJECT_value(xl->store_ctx->objs,j);
sl@0
   388
		else tmp = NULL;
sl@0
   389
		CRYPTO_r_unlock(CRYPTO_LOCK_X509_STORE);
sl@0
   390
sl@0
   391
		if (tmp != NULL)
sl@0
   392
			{
sl@0
   393
			ok=1;
sl@0
   394
			ret->type=tmp->type;
sl@0
   395
			memcpy(&ret->data,&tmp->data,sizeof(ret->data));
sl@0
   396
			/* If we were going to up the reference count,
sl@0
   397
			 * we would need to do it on a perl 'type'
sl@0
   398
			 * basis */
sl@0
   399
	/*		CRYPTO_add(&tmp->data.x509->references,1,
sl@0
   400
				CRYPTO_LOCK_X509);*/
sl@0
   401
			goto finish;
sl@0
   402
			}
sl@0
   403
		}
sl@0
   404
finish:
sl@0
   405
	if (b != NULL) BUF_MEM_free(b);
sl@0
   406
	return(ok);
sl@0
   407
	}
sl@0
   408