os/ossrv/ssl/libcrypto/src/crypto/store/str_lib.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/store/str_lib.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1824 @@
     1.4 +/* crypto/store/str_lib.c -*- mode:C; c-file-style: "eay" -*- */
     1.5 +/* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
     1.6 + * project 2003.
     1.7 + */
     1.8 +/* ====================================================================
     1.9 + * Copyright (c) 2003 The OpenSSL Project.  All rights reserved.
    1.10 + *
    1.11 + * Redistribution and use in source and binary forms, with or without
    1.12 + * modification, are permitted provided that the following conditions
    1.13 + * are met:
    1.14 + *
    1.15 + * 1. Redistributions of source code must retain the above copyright
    1.16 + *    notice, this list of conditions and the following disclaimer. 
    1.17 + *
    1.18 + * 2. Redistributions in binary form must reproduce the above copyright
    1.19 + *    notice, this list of conditions and the following disclaimer in
    1.20 + *    the documentation and/or other materials provided with the
    1.21 + *    distribution.
    1.22 + *
    1.23 + * 3. All advertising materials mentioning features or use of this
    1.24 + *    software must display the following acknowledgment:
    1.25 + *    "This product includes software developed by the OpenSSL Project
    1.26 + *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
    1.27 + *
    1.28 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
    1.29 + *    endorse or promote products derived from this software without
    1.30 + *    prior written permission. For written permission, please contact
    1.31 + *    openssl-core@openssl.org.
    1.32 + *
    1.33 + * 5. Products derived from this software may not be called "OpenSSL"
    1.34 + *    nor may "OpenSSL" appear in their names without prior written
    1.35 + *    permission of the OpenSSL Project.
    1.36 + *
    1.37 + * 6. Redistributions of any form whatsoever must retain the following
    1.38 + *    acknowledgment:
    1.39 + *    "This product includes software developed by the OpenSSL Project
    1.40 + *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
    1.41 + *
    1.42 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
    1.43 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.44 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.45 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
    1.46 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.47 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    1.48 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    1.49 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.50 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.51 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.52 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    1.53 + * OF THE POSSIBILITY OF SUCH DAMAGE.
    1.54 + * ====================================================================
    1.55 + *
    1.56 + * This product includes cryptographic software written by Eric Young
    1.57 + * (eay@cryptsoft.com).  This product includes software written by Tim
    1.58 + * Hudson (tjh@cryptsoft.com).
    1.59 + *
    1.60 + */
    1.61 +
    1.62 +#include <string.h>
    1.63 +#include <openssl/bn.h>
    1.64 +#include <openssl/err.h>
    1.65 +#ifndef OPENSSL_NO_ENGINE
    1.66 +#include <openssl/engine.h>
    1.67 +#endif
    1.68 +#include <openssl/sha.h>
    1.69 +#include <openssl/x509.h>
    1.70 +#include "str_locl.h"
    1.71 +
    1.72 +const char * const STORE_object_type_string[STORE_OBJECT_TYPE_NUM+1] =
    1.73 +	{
    1.74 +	0,
    1.75 +	"X.509 Certificate",
    1.76 +	"X.509 CRL",
    1.77 +	"Private Key",
    1.78 +	"Public Key",
    1.79 +	"Number",
    1.80 +	"Arbitrary Data"
    1.81 +	};
    1.82 +
    1.83 +const int STORE_param_sizes[STORE_PARAM_TYPE_NUM+1] =
    1.84 +	{
    1.85 +	0,
    1.86 +	sizeof(int),		/* EVP_TYPE */
    1.87 +	sizeof(size_t),		/* BITS */
    1.88 +	-1,			/* KEY_PARAMETERS */
    1.89 +	0			/* KEY_NO_PARAMETERS */
    1.90 +	};	
    1.91 +
    1.92 +const int STORE_attr_sizes[STORE_ATTR_TYPE_NUM+1] =
    1.93 +	{
    1.94 +	0,
    1.95 +	-1,			/* FRIENDLYNAME:	C string */
    1.96 +	SHA_DIGEST_LENGTH,	/* KEYID:		SHA1 digest, 160 bits */
    1.97 +	SHA_DIGEST_LENGTH,	/* ISSUERKEYID:		SHA1 digest, 160 bits */
    1.98 +	SHA_DIGEST_LENGTH,	/* SUBJECTKEYID:	SHA1 digest, 160 bits */
    1.99 +	SHA_DIGEST_LENGTH,	/* ISSUERSERIALHASH:	SHA1 digest, 160 bits */
   1.100 +	sizeof(X509_NAME *),	/* ISSUER:		X509_NAME * */
   1.101 +	sizeof(BIGNUM *),	/* SERIAL:		BIGNUM * */
   1.102 +	sizeof(X509_NAME *),	/* SUBJECT:		X509_NAME * */
   1.103 +	SHA_DIGEST_LENGTH,	/* CERTHASH:		SHA1 digest, 160 bits */
   1.104 +	-1,			/* EMAIL:		C string */
   1.105 +	-1,			/* FILENAME:		C string */
   1.106 +	};	
   1.107 +
   1.108 +EXPORT_C STORE *STORE_new_method(const STORE_METHOD *method)
   1.109 +	{
   1.110 +	STORE *ret;
   1.111 +
   1.112 +	if (method == NULL)
   1.113 +		{
   1.114 +		STOREerr(STORE_F_STORE_NEW_METHOD,ERR_R_PASSED_NULL_PARAMETER);
   1.115 +		return NULL;
   1.116 +		}
   1.117 +
   1.118 +	ret=(STORE *)OPENSSL_malloc(sizeof(STORE));
   1.119 +	if (ret == NULL)
   1.120 +		{
   1.121 +		STOREerr(STORE_F_STORE_NEW_METHOD,ERR_R_MALLOC_FAILURE);
   1.122 +		return NULL;
   1.123 +		}
   1.124 +
   1.125 +	ret->meth=method;
   1.126 +
   1.127 +	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_STORE, ret, &ret->ex_data);
   1.128 +	if (ret->meth->init && !ret->meth->init(ret))
   1.129 +		{
   1.130 +		STORE_free(ret);
   1.131 +		ret = NULL;
   1.132 +		}
   1.133 +	return ret;
   1.134 +	}
   1.135 +
   1.136 +EXPORT_C STORE *STORE_new_engine(ENGINE *engine)
   1.137 +	{
   1.138 +	STORE *ret = NULL;
   1.139 +	ENGINE *e = engine;
   1.140 +	const STORE_METHOD *meth = 0;
   1.141 +
   1.142 +#ifdef OPENSSL_NO_ENGINE
   1.143 +	e = NULL;
   1.144 +#else
   1.145 +	if (engine)
   1.146 +		{
   1.147 +		if (!ENGINE_init(engine))
   1.148 +			{
   1.149 +			STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_ENGINE_LIB);
   1.150 +			return NULL;
   1.151 +			}
   1.152 +		e = engine;
   1.153 +		}
   1.154 +	else
   1.155 +		{
   1.156 +		STOREerr(STORE_F_STORE_NEW_ENGINE,ERR_R_PASSED_NULL_PARAMETER);
   1.157 +		return NULL;
   1.158 +		}
   1.159 +	if(e)
   1.160 +		{
   1.161 +		meth = ENGINE_get_STORE(e);
   1.162 +		if(!meth)
   1.163 +			{
   1.164 +			STOREerr(STORE_F_STORE_NEW_ENGINE,
   1.165 +				ERR_R_ENGINE_LIB);
   1.166 +			ENGINE_finish(e);
   1.167 +			return NULL;
   1.168 +			}
   1.169 +		}
   1.170 +#endif
   1.171 +
   1.172 +	ret = STORE_new_method(meth);
   1.173 +	if (ret == NULL)
   1.174 +		{
   1.175 +		STOREerr(STORE_F_STORE_NEW_ENGINE,ERR_R_STORE_LIB);
   1.176 +		return NULL;
   1.177 +		}
   1.178 +
   1.179 +	ret->engine = e;
   1.180 +
   1.181 +	return(ret);
   1.182 +	}
   1.183 +
   1.184 +EXPORT_C void STORE_free(STORE *store)
   1.185 +	{
   1.186 +	if (store == NULL)
   1.187 +		return;
   1.188 +	if (store->meth->clean)
   1.189 +		store->meth->clean(store);
   1.190 +	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_STORE, store, &store->ex_data);
   1.191 +	OPENSSL_free(store);
   1.192 +	}
   1.193 +
   1.194 +EXPORT_C int STORE_ctrl(STORE *store, int cmd, long i, void *p, void (*f)(void))
   1.195 +	{
   1.196 +	if (store == NULL)
   1.197 +		{
   1.198 +		STOREerr(STORE_F_STORE_CTRL,ERR_R_PASSED_NULL_PARAMETER);
   1.199 +		return 0;
   1.200 +		}
   1.201 +	if (store->meth->ctrl)
   1.202 +		return store->meth->ctrl(store, cmd, i, p, f);
   1.203 +	STOREerr(STORE_F_STORE_CTRL,STORE_R_NO_CONTROL_FUNCTION);
   1.204 +	return 0;
   1.205 +	}
   1.206 +
   1.207 +
   1.208 +EXPORT_C int STORE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
   1.209 +	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
   1.210 +        {
   1.211 +	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_STORE, argl, argp,
   1.212 +				new_func, dup_func, free_func);
   1.213 +        }
   1.214 +
   1.215 +EXPORT_C int STORE_set_ex_data(STORE *r, int idx, void *arg)
   1.216 +	{
   1.217 +	return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
   1.218 +	}
   1.219 +
   1.220 +EXPORT_C void *STORE_get_ex_data(STORE *r, int idx)
   1.221 +	{
   1.222 +	return(CRYPTO_get_ex_data(&r->ex_data,idx));
   1.223 +	}
   1.224 +
   1.225 +EXPORT_C const STORE_METHOD *STORE_get_method(STORE *store)
   1.226 +	{
   1.227 +	return store->meth;
   1.228 +	}
   1.229 +
   1.230 +EXPORT_C const STORE_METHOD *STORE_set_method(STORE *store, const STORE_METHOD *meth)
   1.231 +	{
   1.232 +	store->meth=meth;
   1.233 +	return store->meth;
   1.234 +	}
   1.235 +
   1.236 +
   1.237 +/* API helpers */
   1.238 +
   1.239 +#define check_store(s,fncode,fnname,fnerrcode) \
   1.240 +	do \
   1.241 +		{ \
   1.242 +		if ((s) == NULL || (s)->meth == NULL) \
   1.243 +			{ \
   1.244 +			STOREerr((fncode), ERR_R_PASSED_NULL_PARAMETER); \
   1.245 +			return 0; \
   1.246 +			} \
   1.247 +		if ((s)->meth->fnname == NULL) \
   1.248 +			{ \
   1.249 +			STOREerr((fncode), (fnerrcode)); \
   1.250 +			return 0; \
   1.251 +			} \
   1.252 +		} \
   1.253 +	while(0)
   1.254 +
   1.255 +/* API functions */
   1.256 +
   1.257 +EXPORT_C X509 *STORE_get_certificate(STORE *s, OPENSSL_ITEM attributes[],
   1.258 +	OPENSSL_ITEM parameters[])
   1.259 +	{
   1.260 +	STORE_OBJECT *object;
   1.261 +	X509 *x;
   1.262 +
   1.263 +	check_store(s,STORE_F_STORE_GET_CERTIFICATE,
   1.264 +		get_object,STORE_R_NO_GET_OBJECT_FUNCTION);
   1.265 +
   1.266 +	object = s->meth->get_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
   1.267 +		attributes, parameters);
   1.268 +	if (!object || !object->data.x509.certificate)
   1.269 +		{
   1.270 +		STOREerr(STORE_F_STORE_GET_CERTIFICATE,
   1.271 +			STORE_R_FAILED_GETTING_CERTIFICATE);
   1.272 +		return 0;
   1.273 +		}
   1.274 +	CRYPTO_add(&object->data.x509.certificate->references,1,CRYPTO_LOCK_X509);
   1.275 +#ifdef REF_PRINT
   1.276 +	REF_PRINT("X509",data);
   1.277 +#endif
   1.278 +	x = object->data.x509.certificate;
   1.279 +	STORE_OBJECT_free(object);
   1.280 +	return x;
   1.281 +	}
   1.282 +
   1.283 +EXPORT_C int STORE_store_certificate(STORE *s, X509 *data, OPENSSL_ITEM attributes[],
   1.284 +	OPENSSL_ITEM parameters[])
   1.285 +	{
   1.286 +	STORE_OBJECT *object;
   1.287 +	int i;
   1.288 +
   1.289 +	check_store(s,STORE_F_STORE_CERTIFICATE,
   1.290 +		store_object,STORE_R_NO_STORE_OBJECT_FUNCTION);
   1.291 +
   1.292 +	object = STORE_OBJECT_new();
   1.293 +	if (!object)
   1.294 +		{
   1.295 +		STOREerr(STORE_F_STORE_STORE_CERTIFICATE,
   1.296 +			ERR_R_MALLOC_FAILURE);
   1.297 +		return 0;
   1.298 +		}
   1.299 +	
   1.300 +	CRYPTO_add(&data->references,1,CRYPTO_LOCK_X509);
   1.301 +#ifdef REF_PRINT
   1.302 +	REF_PRINT("X509",data);
   1.303 +#endif
   1.304 +	object->data.x509.certificate = data;
   1.305 +
   1.306 +	i = s->meth->store_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
   1.307 +		object, attributes, parameters);
   1.308 +
   1.309 +	STORE_OBJECT_free(object);
   1.310 +
   1.311 +	if (!i)
   1.312 +		{
   1.313 +		STOREerr(STORE_F_STORE_STORE_CERTIFICATE,
   1.314 +			STORE_R_FAILED_STORING_CERTIFICATE);
   1.315 +		return 0;
   1.316 +		}
   1.317 +	return 1;
   1.318 +	}
   1.319 +
   1.320 +EXPORT_C int STORE_modify_certificate(STORE *s, OPENSSL_ITEM search_attributes[],
   1.321 +	OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
   1.322 +	OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
   1.323 +	{
   1.324 +	check_store(s,STORE_F_STORE_MODIFY_CERTIFICATE,
   1.325 +		modify_object,STORE_R_NO_MODIFY_OBJECT_FUNCTION);
   1.326 +
   1.327 +	if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
   1.328 +		    search_attributes, add_attributes, modify_attributes,
   1.329 +		    delete_attributes, parameters))
   1.330 +		{
   1.331 +		STOREerr(STORE_F_STORE_MODIFY_CERTIFICATE,
   1.332 +			STORE_R_FAILED_MODIFYING_CERTIFICATE);
   1.333 +		return 0;
   1.334 +		}
   1.335 +	return 1;
   1.336 +	}
   1.337 +
   1.338 +EXPORT_C int STORE_revoke_certificate(STORE *s, OPENSSL_ITEM attributes[],
   1.339 +	OPENSSL_ITEM parameters[])
   1.340 +	{
   1.341 +	check_store(s,STORE_F_STORE_REVOKE_CERTIFICATE,
   1.342 +		revoke_object,STORE_R_NO_REVOKE_OBJECT_FUNCTION);
   1.343 +
   1.344 +	if (!s->meth->revoke_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
   1.345 +		    attributes, parameters))
   1.346 +		{
   1.347 +		STOREerr(STORE_F_STORE_REVOKE_CERTIFICATE,
   1.348 +			STORE_R_FAILED_REVOKING_CERTIFICATE);
   1.349 +		return 0;
   1.350 +		}
   1.351 +	return 1;
   1.352 +	}
   1.353 +
   1.354 +EXPORT_C int STORE_delete_certificate(STORE *s, OPENSSL_ITEM attributes[],
   1.355 +	OPENSSL_ITEM parameters[])
   1.356 +	{
   1.357 +	check_store(s,STORE_F_STORE_DELETE_CERTIFICATE,
   1.358 +		delete_object,STORE_R_NO_DELETE_OBJECT_FUNCTION);
   1.359 +
   1.360 +	if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
   1.361 +		    attributes, parameters))
   1.362 +		{
   1.363 +		STOREerr(STORE_F_STORE_DELETE_CERTIFICATE,
   1.364 +			STORE_R_FAILED_DELETING_CERTIFICATE);
   1.365 +		return 0;
   1.366 +		}
   1.367 +	return 1;
   1.368 +	}
   1.369 +
   1.370 +EXPORT_C void *STORE_list_certificate_start(STORE *s, OPENSSL_ITEM attributes[],
   1.371 +	OPENSSL_ITEM parameters[])
   1.372 +	{
   1.373 +	void *handle;
   1.374 +
   1.375 +	check_store(s,STORE_F_STORE_LIST_CERTIFICATE_START,
   1.376 +		list_object_start,STORE_R_NO_LIST_OBJECT_START_FUNCTION);
   1.377 +
   1.378 +	handle = s->meth->list_object_start(s,
   1.379 +		STORE_OBJECT_TYPE_X509_CERTIFICATE, attributes, parameters);
   1.380 +	if (!handle)
   1.381 +		{
   1.382 +		STOREerr(STORE_F_STORE_LIST_CERTIFICATE_START,
   1.383 +			STORE_R_FAILED_LISTING_CERTIFICATES);
   1.384 +		return 0;
   1.385 +		}
   1.386 +	return handle;
   1.387 +	}
   1.388 +
   1.389 +EXPORT_C X509 *STORE_list_certificate_next(STORE *s, void *handle)
   1.390 +	{
   1.391 +	STORE_OBJECT *object;
   1.392 +	X509 *x;
   1.393 +
   1.394 +	check_store(s,STORE_F_STORE_LIST_CERTIFICATE_NEXT,
   1.395 +		list_object_next,STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
   1.396 +
   1.397 +	object = s->meth->list_object_next(s, handle);
   1.398 +	if (!object || !object->data.x509.certificate)
   1.399 +		{
   1.400 +		STOREerr(STORE_F_STORE_LIST_CERTIFICATE_NEXT,
   1.401 +			STORE_R_FAILED_LISTING_CERTIFICATES);
   1.402 +		return 0;
   1.403 +		}
   1.404 +	CRYPTO_add(&object->data.x509.certificate->references,1,CRYPTO_LOCK_X509);
   1.405 +#ifdef REF_PRINT
   1.406 +	REF_PRINT("X509",data);
   1.407 +#endif
   1.408 +	x = object->data.x509.certificate;
   1.409 +	STORE_OBJECT_free(object);
   1.410 +	return x;
   1.411 +	}
   1.412 +
   1.413 +EXPORT_C int STORE_list_certificate_end(STORE *s, void *handle)
   1.414 +	{
   1.415 +	check_store(s,STORE_F_STORE_LIST_CERTIFICATE_END,
   1.416 +		list_object_end,STORE_R_NO_LIST_OBJECT_END_FUNCTION);
   1.417 +
   1.418 +	if (!s->meth->list_object_end(s, handle))
   1.419 +		{
   1.420 +		STOREerr(STORE_F_STORE_LIST_CERTIFICATE_END,
   1.421 +			STORE_R_FAILED_LISTING_CERTIFICATES);
   1.422 +		return 0;
   1.423 +		}
   1.424 +	return 1;
   1.425 +	}
   1.426 +
   1.427 +EXPORT_C int STORE_list_certificate_endp(STORE *s, void *handle)
   1.428 +	{
   1.429 +	check_store(s,STORE_F_STORE_LIST_CERTIFICATE_ENDP,
   1.430 +		list_object_endp,STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
   1.431 +
   1.432 +	if (!s->meth->list_object_endp(s, handle))
   1.433 +		{
   1.434 +		STOREerr(STORE_F_STORE_LIST_CERTIFICATE_ENDP,
   1.435 +			STORE_R_FAILED_LISTING_CERTIFICATES);
   1.436 +		return 0;
   1.437 +		}
   1.438 +	return 1;
   1.439 +	}
   1.440 +
   1.441 +EXPORT_C EVP_PKEY *STORE_generate_key(STORE *s, OPENSSL_ITEM attributes[],
   1.442 +	OPENSSL_ITEM parameters[])
   1.443 +	{
   1.444 +	STORE_OBJECT *object;
   1.445 +	EVP_PKEY *pkey;
   1.446 +
   1.447 +	check_store(s,STORE_F_STORE_GENERATE_KEY,
   1.448 +		generate_object,STORE_R_NO_GENERATE_OBJECT_FUNCTION);
   1.449 +
   1.450 +	object = s->meth->generate_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
   1.451 +		attributes, parameters);
   1.452 +	if (!object || !object->data.key)
   1.453 +		{
   1.454 +		STOREerr(STORE_F_STORE_GENERATE_KEY,
   1.455 +			STORE_R_FAILED_GENERATING_KEY);
   1.456 +		return 0;
   1.457 +		}
   1.458 +	CRYPTO_add(&object->data.key->references,1,CRYPTO_LOCK_EVP_PKEY);
   1.459 +#ifdef REF_PRINT
   1.460 +	REF_PRINT("EVP_PKEY",data);
   1.461 +#endif
   1.462 +	pkey = object->data.key;
   1.463 +	STORE_OBJECT_free(object);
   1.464 +	return pkey;
   1.465 +	}
   1.466 +
   1.467 +EXPORT_C EVP_PKEY *STORE_get_private_key(STORE *s, OPENSSL_ITEM attributes[],
   1.468 +	OPENSSL_ITEM parameters[])
   1.469 +	{
   1.470 +	STORE_OBJECT *object;
   1.471 +	EVP_PKEY *pkey;
   1.472 +
   1.473 +	check_store(s,STORE_F_STORE_GET_PRIVATE_KEY,
   1.474 +		get_object,STORE_R_NO_GET_OBJECT_FUNCTION);
   1.475 +
   1.476 +	object = s->meth->get_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
   1.477 +		attributes, parameters);
   1.478 +	if (!object || !object->data.key || !object->data.key)
   1.479 +		{
   1.480 +		STOREerr(STORE_F_STORE_GET_PRIVATE_KEY,
   1.481 +			STORE_R_FAILED_GETTING_KEY);
   1.482 +		return 0;
   1.483 +		}
   1.484 +	CRYPTO_add(&object->data.key->references,1,CRYPTO_LOCK_EVP_PKEY);
   1.485 +#ifdef REF_PRINT
   1.486 +	REF_PRINT("EVP_PKEY",data);
   1.487 +#endif
   1.488 +	pkey = object->data.key;
   1.489 +	STORE_OBJECT_free(object);
   1.490 +	return pkey;
   1.491 +	}
   1.492 +
   1.493 +EXPORT_C int STORE_store_private_key(STORE *s, EVP_PKEY *data, OPENSSL_ITEM attributes[],
   1.494 +	OPENSSL_ITEM parameters[])
   1.495 +	{
   1.496 +	STORE_OBJECT *object;
   1.497 +	int i;
   1.498 +
   1.499 +	check_store(s,STORE_F_STORE_STORE_PRIVATE_KEY,
   1.500 +		store_object,STORE_R_NO_STORE_OBJECT_FUNCTION);
   1.501 +
   1.502 +	object = STORE_OBJECT_new();
   1.503 +	if (!object)
   1.504 +		{
   1.505 +		STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY,
   1.506 +			ERR_R_MALLOC_FAILURE);
   1.507 +		return 0;
   1.508 +		}
   1.509 +	object->data.key = EVP_PKEY_new();
   1.510 +	if (!object->data.key)
   1.511 +		{
   1.512 +		STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY,
   1.513 +			ERR_R_MALLOC_FAILURE);
   1.514 +		return 0;
   1.515 +		}
   1.516 +	
   1.517 +	CRYPTO_add(&data->references,1,CRYPTO_LOCK_EVP_PKEY);
   1.518 +#ifdef REF_PRINT
   1.519 +	REF_PRINT("EVP_PKEY",data);
   1.520 +#endif
   1.521 +	object->data.key = data;
   1.522 +
   1.523 +	i = s->meth->store_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY, object,
   1.524 +		attributes, parameters);
   1.525 +
   1.526 +	STORE_OBJECT_free(object);
   1.527 +
   1.528 +	if (!i)
   1.529 +		{
   1.530 +		STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY,
   1.531 +			STORE_R_FAILED_STORING_KEY);
   1.532 +		return 0;
   1.533 +		}
   1.534 +	return i;
   1.535 +	}
   1.536 +
   1.537 +EXPORT_C int STORE_modify_private_key(STORE *s, OPENSSL_ITEM search_attributes[],
   1.538 +	OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
   1.539 +	OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
   1.540 +	{
   1.541 +	check_store(s,STORE_F_STORE_MODIFY_PRIVATE_KEY,
   1.542 +		modify_object,STORE_R_NO_MODIFY_OBJECT_FUNCTION);
   1.543 +
   1.544 +	if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
   1.545 +		    search_attributes, add_attributes, modify_attributes,
   1.546 +		    delete_attributes, parameters))
   1.547 +		{
   1.548 +		STOREerr(STORE_F_STORE_MODIFY_PRIVATE_KEY,
   1.549 +			STORE_R_FAILED_MODIFYING_PRIVATE_KEY);
   1.550 +		return 0;
   1.551 +		}
   1.552 +	return 1;
   1.553 +	}
   1.554 +
   1.555 +EXPORT_C int STORE_revoke_private_key(STORE *s, OPENSSL_ITEM attributes[],
   1.556 +	OPENSSL_ITEM parameters[])
   1.557 +	{
   1.558 +	int i;
   1.559 +
   1.560 +	check_store(s,STORE_F_STORE_REVOKE_PRIVATE_KEY,
   1.561 +		revoke_object,STORE_R_NO_REVOKE_OBJECT_FUNCTION);
   1.562 +
   1.563 +	i = s->meth->revoke_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
   1.564 +		attributes, parameters);
   1.565 +
   1.566 +	if (!i)
   1.567 +		{
   1.568 +		STOREerr(STORE_F_STORE_REVOKE_PRIVATE_KEY,
   1.569 +			STORE_R_FAILED_REVOKING_KEY);
   1.570 +		return 0;
   1.571 +		}
   1.572 +	return i;
   1.573 +	}
   1.574 +
   1.575 +EXPORT_C int STORE_delete_private_key(STORE *s, OPENSSL_ITEM attributes[],
   1.576 +	OPENSSL_ITEM parameters[])
   1.577 +	{
   1.578 +	check_store(s,STORE_F_STORE_DELETE_PRIVATE_KEY,
   1.579 +		delete_object,STORE_R_NO_DELETE_OBJECT_FUNCTION);
   1.580 +	
   1.581 +	if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
   1.582 +		    attributes, parameters))
   1.583 +		{
   1.584 +		STOREerr(STORE_F_STORE_DELETE_PRIVATE_KEY,
   1.585 +			STORE_R_FAILED_DELETING_KEY);
   1.586 +		return 0;
   1.587 +		}
   1.588 +	return 1;
   1.589 +	}
   1.590 +
   1.591 +EXPORT_C void *STORE_list_private_key_start(STORE *s, OPENSSL_ITEM attributes[],
   1.592 +	OPENSSL_ITEM parameters[])
   1.593 +	{
   1.594 +	void *handle;
   1.595 +
   1.596 +	check_store(s,STORE_F_STORE_LIST_PRIVATE_KEY_START,
   1.597 +		list_object_start,STORE_R_NO_LIST_OBJECT_START_FUNCTION);
   1.598 +
   1.599 +	handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
   1.600 +		attributes, parameters);
   1.601 +	if (!handle)
   1.602 +		{
   1.603 +		STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_START,
   1.604 +			STORE_R_FAILED_LISTING_KEYS);
   1.605 +		return 0;
   1.606 +		}
   1.607 +	return handle;
   1.608 +	}
   1.609 +
   1.610 +EXPORT_C EVP_PKEY *STORE_list_private_key_next(STORE *s, void *handle)
   1.611 +	{
   1.612 +	STORE_OBJECT *object;
   1.613 +	EVP_PKEY *pkey;
   1.614 +
   1.615 +	check_store(s,STORE_F_STORE_LIST_PRIVATE_KEY_NEXT,
   1.616 +		list_object_next,STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
   1.617 +
   1.618 +	object = s->meth->list_object_next(s, handle);
   1.619 +	if (!object || !object->data.key || !object->data.key)
   1.620 +		{
   1.621 +		STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_NEXT,
   1.622 +			STORE_R_FAILED_LISTING_KEYS);
   1.623 +		return 0;
   1.624 +		}
   1.625 +	CRYPTO_add(&object->data.key->references,1,CRYPTO_LOCK_EVP_PKEY);
   1.626 +#ifdef REF_PRINT
   1.627 +	REF_PRINT("EVP_PKEY",data);
   1.628 +#endif
   1.629 +	pkey = object->data.key;
   1.630 +	STORE_OBJECT_free(object);
   1.631 +	return pkey;
   1.632 +	}
   1.633 +
   1.634 +EXPORT_C int STORE_list_private_key_end(STORE *s, void *handle)
   1.635 +	{
   1.636 +	check_store(s,STORE_F_STORE_LIST_PRIVATE_KEY_END,
   1.637 +		list_object_end,STORE_R_NO_LIST_OBJECT_END_FUNCTION);
   1.638 +
   1.639 +	if (!s->meth->list_object_end(s, handle))
   1.640 +		{
   1.641 +		STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_END,
   1.642 +			STORE_R_FAILED_LISTING_KEYS);
   1.643 +		return 0;
   1.644 +		}
   1.645 +	return 1;
   1.646 +	}
   1.647 +
   1.648 +EXPORT_C int STORE_list_private_key_endp(STORE *s, void *handle)
   1.649 +	{
   1.650 +	check_store(s,STORE_F_STORE_LIST_PRIVATE_KEY_ENDP,
   1.651 +		list_object_endp,STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
   1.652 +
   1.653 +	if (!s->meth->list_object_endp(s, handle))
   1.654 +		{
   1.655 +		STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_ENDP,
   1.656 +			STORE_R_FAILED_LISTING_KEYS);
   1.657 +		return 0;
   1.658 +		}
   1.659 +	return 1;
   1.660 +	}
   1.661 +
   1.662 +EXPORT_C EVP_PKEY *STORE_get_public_key(STORE *s, OPENSSL_ITEM attributes[],
   1.663 +	OPENSSL_ITEM parameters[])
   1.664 +	{
   1.665 +	STORE_OBJECT *object;
   1.666 +	EVP_PKEY *pkey;
   1.667 +
   1.668 +	check_store(s,STORE_F_STORE_GET_PUBLIC_KEY,
   1.669 +		get_object,STORE_R_NO_GET_OBJECT_FUNCTION);
   1.670 +
   1.671 +	object = s->meth->get_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
   1.672 +		attributes, parameters);
   1.673 +	if (!object || !object->data.key || !object->data.key)
   1.674 +		{
   1.675 +		STOREerr(STORE_F_STORE_GET_PUBLIC_KEY,
   1.676 +			STORE_R_FAILED_GETTING_KEY);
   1.677 +		return 0;
   1.678 +		}
   1.679 +	CRYPTO_add(&object->data.key->references,1,CRYPTO_LOCK_EVP_PKEY);
   1.680 +#ifdef REF_PRINT
   1.681 +	REF_PRINT("EVP_PKEY",data);
   1.682 +#endif
   1.683 +	pkey = object->data.key;
   1.684 +	STORE_OBJECT_free(object);
   1.685 +	return pkey;
   1.686 +	}
   1.687 +
   1.688 +EXPORT_C int STORE_store_public_key(STORE *s, EVP_PKEY *data, OPENSSL_ITEM attributes[],
   1.689 +	OPENSSL_ITEM parameters[])
   1.690 +	{
   1.691 +	STORE_OBJECT *object;
   1.692 +	int i;
   1.693 +
   1.694 +	check_store(s,STORE_F_STORE_STORE_PUBLIC_KEY,
   1.695 +		store_object,STORE_R_NO_STORE_OBJECT_FUNCTION);
   1.696 +
   1.697 +	object = STORE_OBJECT_new();
   1.698 +	if (!object)
   1.699 +		{
   1.700 +		STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY,
   1.701 +			ERR_R_MALLOC_FAILURE);
   1.702 +		return 0;
   1.703 +		}
   1.704 +	object->data.key = EVP_PKEY_new();
   1.705 +	if (!object->data.key)
   1.706 +		{
   1.707 +		STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY,
   1.708 +			ERR_R_MALLOC_FAILURE);
   1.709 +		return 0;
   1.710 +		}
   1.711 +	
   1.712 +	CRYPTO_add(&data->references,1,CRYPTO_LOCK_EVP_PKEY);
   1.713 +#ifdef REF_PRINT
   1.714 +	REF_PRINT("EVP_PKEY",data);
   1.715 +#endif
   1.716 +	object->data.key = data;
   1.717 +
   1.718 +	i = s->meth->store_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY, object,
   1.719 +		attributes, parameters);
   1.720 +
   1.721 +	STORE_OBJECT_free(object);
   1.722 +
   1.723 +	if (!i)
   1.724 +		{
   1.725 +		STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY,
   1.726 +			STORE_R_FAILED_STORING_KEY);
   1.727 +		return 0;
   1.728 +		}
   1.729 +	return i;
   1.730 +	}
   1.731 +
   1.732 +EXPORT_C int STORE_modify_public_key(STORE *s, OPENSSL_ITEM search_attributes[],
   1.733 +	OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
   1.734 +	OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
   1.735 +	{
   1.736 +	check_store(s,STORE_F_STORE_MODIFY_PUBLIC_KEY,
   1.737 +		modify_object,STORE_R_NO_MODIFY_OBJECT_FUNCTION);
   1.738 +
   1.739 +	if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
   1.740 +		    search_attributes, add_attributes, modify_attributes,
   1.741 +		    delete_attributes, parameters))
   1.742 +		{
   1.743 +		STOREerr(STORE_F_STORE_MODIFY_PUBLIC_KEY,
   1.744 +			STORE_R_FAILED_MODIFYING_PUBLIC_KEY);
   1.745 +		return 0;
   1.746 +		}
   1.747 +	return 1;
   1.748 +	}
   1.749 +
   1.750 +EXPORT_C int STORE_revoke_public_key(STORE *s, OPENSSL_ITEM attributes[],
   1.751 +	OPENSSL_ITEM parameters[])
   1.752 +	{
   1.753 +	int i;
   1.754 +
   1.755 +	check_store(s,STORE_F_STORE_REVOKE_PUBLIC_KEY,
   1.756 +		revoke_object,STORE_R_NO_REVOKE_OBJECT_FUNCTION);
   1.757 +
   1.758 +	i = s->meth->revoke_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
   1.759 +		attributes, parameters);
   1.760 +
   1.761 +	if (!i)
   1.762 +		{
   1.763 +		STOREerr(STORE_F_STORE_REVOKE_PUBLIC_KEY,
   1.764 +			STORE_R_FAILED_REVOKING_KEY);
   1.765 +		return 0;
   1.766 +		}
   1.767 +	return i;
   1.768 +	}
   1.769 +
   1.770 +EXPORT_C int STORE_delete_public_key(STORE *s, OPENSSL_ITEM attributes[],
   1.771 +	OPENSSL_ITEM parameters[])
   1.772 +	{
   1.773 +	check_store(s,STORE_F_STORE_DELETE_PUBLIC_KEY,
   1.774 +		delete_object,STORE_R_NO_DELETE_OBJECT_FUNCTION);
   1.775 +	
   1.776 +	if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
   1.777 +		    attributes, parameters))
   1.778 +		{
   1.779 +		STOREerr(STORE_F_STORE_DELETE_PUBLIC_KEY,
   1.780 +			STORE_R_FAILED_DELETING_KEY);
   1.781 +		return 0;
   1.782 +		}
   1.783 +	return 1;
   1.784 +	}
   1.785 +
   1.786 +EXPORT_C void *STORE_list_public_key_start(STORE *s, OPENSSL_ITEM attributes[],
   1.787 +	OPENSSL_ITEM parameters[])
   1.788 +	{
   1.789 +	void *handle;
   1.790 +
   1.791 +	check_store(s,STORE_F_STORE_LIST_PUBLIC_KEY_START,
   1.792 +		list_object_start,STORE_R_NO_LIST_OBJECT_START_FUNCTION);
   1.793 +
   1.794 +	handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
   1.795 +		attributes, parameters);
   1.796 +	if (!handle)
   1.797 +		{
   1.798 +		STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_START,
   1.799 +			STORE_R_FAILED_LISTING_KEYS);
   1.800 +		return 0;
   1.801 +		}
   1.802 +	return handle;
   1.803 +	}
   1.804 +
   1.805 +EXPORT_C EVP_PKEY *STORE_list_public_key_next(STORE *s, void *handle)
   1.806 +	{
   1.807 +	STORE_OBJECT *object;
   1.808 +	EVP_PKEY *pkey;
   1.809 +
   1.810 +	check_store(s,STORE_F_STORE_LIST_PUBLIC_KEY_NEXT,
   1.811 +		list_object_next,STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
   1.812 +
   1.813 +	object = s->meth->list_object_next(s, handle);
   1.814 +	if (!object || !object->data.key || !object->data.key)
   1.815 +		{
   1.816 +		STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_NEXT,
   1.817 +			STORE_R_FAILED_LISTING_KEYS);
   1.818 +		return 0;
   1.819 +		}
   1.820 +	CRYPTO_add(&object->data.key->references,1,CRYPTO_LOCK_EVP_PKEY);
   1.821 +#ifdef REF_PRINT
   1.822 +	REF_PRINT("EVP_PKEY",data);
   1.823 +#endif
   1.824 +	pkey = object->data.key;
   1.825 +	STORE_OBJECT_free(object);
   1.826 +	return pkey;
   1.827 +	}
   1.828 +
   1.829 +EXPORT_C int STORE_list_public_key_end(STORE *s, void *handle)
   1.830 +	{
   1.831 +	check_store(s,STORE_F_STORE_LIST_PUBLIC_KEY_END,
   1.832 +		list_object_end,STORE_R_NO_LIST_OBJECT_END_FUNCTION);
   1.833 +
   1.834 +	if (!s->meth->list_object_end(s, handle))
   1.835 +		{
   1.836 +		STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_END,
   1.837 +			STORE_R_FAILED_LISTING_KEYS);
   1.838 +		return 0;
   1.839 +		}
   1.840 +	return 1;
   1.841 +	}
   1.842 +
   1.843 +EXPORT_C int STORE_list_public_key_endp(STORE *s, void *handle)
   1.844 +	{
   1.845 +	check_store(s,STORE_F_STORE_LIST_PUBLIC_KEY_ENDP,
   1.846 +		list_object_endp,STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
   1.847 +
   1.848 +	if (!s->meth->list_object_endp(s, handle))
   1.849 +		{
   1.850 +		STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_ENDP,
   1.851 +			STORE_R_FAILED_LISTING_KEYS);
   1.852 +		return 0;
   1.853 +		}
   1.854 +	return 1;
   1.855 +	}
   1.856 +
   1.857 +EXPORT_C X509_CRL *STORE_generate_crl(STORE *s, OPENSSL_ITEM attributes[],
   1.858 +	OPENSSL_ITEM parameters[])
   1.859 +	{
   1.860 +	STORE_OBJECT *object;
   1.861 +	X509_CRL *crl;
   1.862 +
   1.863 +	check_store(s,STORE_F_STORE_GENERATE_CRL,
   1.864 +		generate_object,STORE_R_NO_GENERATE_CRL_FUNCTION);
   1.865 +
   1.866 +	object = s->meth->generate_object(s, STORE_OBJECT_TYPE_X509_CRL,
   1.867 +		attributes, parameters);
   1.868 +	if (!object || !object->data.crl)
   1.869 +		{
   1.870 +		STOREerr(STORE_F_STORE_GENERATE_CRL,
   1.871 +			STORE_R_FAILED_GENERATING_CRL);
   1.872 +		return 0;
   1.873 +		}
   1.874 +	CRYPTO_add(&object->data.crl->references,1,CRYPTO_LOCK_X509_CRL);
   1.875 +#ifdef REF_PRINT
   1.876 +	REF_PRINT("X509_CRL",data);
   1.877 +#endif
   1.878 +	crl = object->data.crl;
   1.879 +	STORE_OBJECT_free(object);
   1.880 +	return crl;
   1.881 +	}
   1.882 +
   1.883 +EXPORT_C X509_CRL *STORE_get_crl(STORE *s, OPENSSL_ITEM attributes[],
   1.884 +	OPENSSL_ITEM parameters[])
   1.885 +	{
   1.886 +	STORE_OBJECT *object;
   1.887 +	X509_CRL *crl;
   1.888 +
   1.889 +	check_store(s,STORE_F_STORE_GET_CRL,
   1.890 +		get_object,STORE_R_NO_GET_OBJECT_FUNCTION);
   1.891 +
   1.892 +	object = s->meth->get_object(s, STORE_OBJECT_TYPE_X509_CRL,
   1.893 +		attributes, parameters);
   1.894 +	if (!object || !object->data.crl)
   1.895 +		{
   1.896 +		STOREerr(STORE_F_STORE_GET_CRL,
   1.897 +			STORE_R_FAILED_GETTING_KEY);
   1.898 +		return 0;
   1.899 +		}
   1.900 +	CRYPTO_add(&object->data.crl->references,1,CRYPTO_LOCK_X509_CRL);
   1.901 +#ifdef REF_PRINT
   1.902 +	REF_PRINT("X509_CRL",data);
   1.903 +#endif
   1.904 +	crl = object->data.crl;
   1.905 +	STORE_OBJECT_free(object);
   1.906 +	return crl;
   1.907 +	}
   1.908 +
   1.909 +EXPORT_C int STORE_store_crl(STORE *s, X509_CRL *data, OPENSSL_ITEM attributes[],
   1.910 +	OPENSSL_ITEM parameters[])
   1.911 +	{
   1.912 +	STORE_OBJECT *object;
   1.913 +	int i;
   1.914 +
   1.915 +	check_store(s,STORE_F_STORE_STORE_CRL,
   1.916 +		store_object,STORE_R_NO_STORE_OBJECT_FUNCTION);
   1.917 +
   1.918 +	object = STORE_OBJECT_new();
   1.919 +	if (!object)
   1.920 +		{
   1.921 +		STOREerr(STORE_F_STORE_STORE_CRL,
   1.922 +			ERR_R_MALLOC_FAILURE);
   1.923 +		return 0;
   1.924 +		}
   1.925 +	
   1.926 +	CRYPTO_add(&data->references,1,CRYPTO_LOCK_X509_CRL);
   1.927 +#ifdef REF_PRINT
   1.928 +	REF_PRINT("X509_CRL",data);
   1.929 +#endif
   1.930 +	object->data.crl = data;
   1.931 +
   1.932 +	i = s->meth->store_object(s, STORE_OBJECT_TYPE_X509_CRL, object,
   1.933 +		attributes, parameters);
   1.934 +
   1.935 +	STORE_OBJECT_free(object);
   1.936 +
   1.937 +	if (!i)
   1.938 +		{
   1.939 +		STOREerr(STORE_F_STORE_STORE_CRL,
   1.940 +			STORE_R_FAILED_STORING_KEY);
   1.941 +		return 0;
   1.942 +		}
   1.943 +	return i;
   1.944 +	}
   1.945 +
   1.946 +EXPORT_C int STORE_modify_crl(STORE *s, OPENSSL_ITEM search_attributes[],
   1.947 +	OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
   1.948 +	OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
   1.949 +	{
   1.950 +	check_store(s,STORE_F_STORE_MODIFY_CRL,
   1.951 +		modify_object,STORE_R_NO_MODIFY_OBJECT_FUNCTION);
   1.952 +
   1.953 +	if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_X509_CRL,
   1.954 +		    search_attributes, add_attributes, modify_attributes,
   1.955 +		    delete_attributes, parameters))
   1.956 +		{
   1.957 +		STOREerr(STORE_F_STORE_MODIFY_CRL,
   1.958 +			STORE_R_FAILED_MODIFYING_CRL);
   1.959 +		return 0;
   1.960 +		}
   1.961 +	return 1;
   1.962 +	}
   1.963 +
   1.964 +EXPORT_C int STORE_delete_crl(STORE *s, OPENSSL_ITEM attributes[],
   1.965 +	OPENSSL_ITEM parameters[])
   1.966 +	{
   1.967 +	check_store(s,STORE_F_STORE_DELETE_CRL,
   1.968 +		delete_object,STORE_R_NO_DELETE_OBJECT_FUNCTION);
   1.969 +	
   1.970 +	if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_X509_CRL,
   1.971 +		    attributes, parameters))
   1.972 +		{
   1.973 +		STOREerr(STORE_F_STORE_DELETE_CRL,
   1.974 +			STORE_R_FAILED_DELETING_KEY);
   1.975 +		return 0;
   1.976 +		}
   1.977 +	return 1;
   1.978 +	}
   1.979 +
   1.980 +EXPORT_C void *STORE_list_crl_start(STORE *s, OPENSSL_ITEM attributes[],
   1.981 +	OPENSSL_ITEM parameters[])
   1.982 +	{
   1.983 +	void *handle;
   1.984 +
   1.985 +	check_store(s,STORE_F_STORE_LIST_CRL_START,
   1.986 +		list_object_start,STORE_R_NO_LIST_OBJECT_START_FUNCTION);
   1.987 +
   1.988 +	handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_X509_CRL,
   1.989 +		attributes, parameters);
   1.990 +	if (!handle)
   1.991 +		{
   1.992 +		STOREerr(STORE_F_STORE_LIST_CRL_START,
   1.993 +			STORE_R_FAILED_LISTING_KEYS);
   1.994 +		return 0;
   1.995 +		}
   1.996 +	return handle;
   1.997 +	}
   1.998 +
   1.999 +EXPORT_C X509_CRL *STORE_list_crl_next(STORE *s, void *handle)
  1.1000 +	{
  1.1001 +	STORE_OBJECT *object;
  1.1002 +	X509_CRL *crl;
  1.1003 +
  1.1004 +	check_store(s,STORE_F_STORE_LIST_CRL_NEXT,
  1.1005 +		list_object_next,STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
  1.1006 +
  1.1007 +	object = s->meth->list_object_next(s, handle);
  1.1008 +	if (!object || !object->data.crl)
  1.1009 +		{
  1.1010 +		STOREerr(STORE_F_STORE_LIST_CRL_NEXT,
  1.1011 +			STORE_R_FAILED_LISTING_KEYS);
  1.1012 +		return 0;
  1.1013 +		}
  1.1014 +	CRYPTO_add(&object->data.crl->references,1,CRYPTO_LOCK_X509_CRL);
  1.1015 +#ifdef REF_PRINT
  1.1016 +	REF_PRINT("X509_CRL",data);
  1.1017 +#endif
  1.1018 +	crl = object->data.crl;
  1.1019 +	STORE_OBJECT_free(object);
  1.1020 +	return crl;
  1.1021 +	}
  1.1022 +
  1.1023 +EXPORT_C int STORE_list_crl_end(STORE *s, void *handle)
  1.1024 +	{
  1.1025 +	check_store(s,STORE_F_STORE_LIST_CRL_END,
  1.1026 +		list_object_end,STORE_R_NO_LIST_OBJECT_END_FUNCTION);
  1.1027 +
  1.1028 +	if (!s->meth->list_object_end(s, handle))
  1.1029 +		{
  1.1030 +		STOREerr(STORE_F_STORE_LIST_CRL_END,
  1.1031 +			STORE_R_FAILED_LISTING_KEYS);
  1.1032 +		return 0;
  1.1033 +		}
  1.1034 +	return 1;
  1.1035 +	}
  1.1036 +
  1.1037 +EXPORT_C int STORE_list_crl_endp(STORE *s, void *handle)
  1.1038 +	{
  1.1039 +	check_store(s,STORE_F_STORE_LIST_CRL_ENDP,
  1.1040 +		list_object_endp,STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
  1.1041 +
  1.1042 +	if (!s->meth->list_object_endp(s, handle))
  1.1043 +		{
  1.1044 +		STOREerr(STORE_F_STORE_LIST_CRL_ENDP,
  1.1045 +			STORE_R_FAILED_LISTING_KEYS);
  1.1046 +		return 0;
  1.1047 +		}
  1.1048 +	return 1;
  1.1049 +	}
  1.1050 +
  1.1051 +EXPORT_C int STORE_store_number(STORE *s, BIGNUM *data, OPENSSL_ITEM attributes[],
  1.1052 +	OPENSSL_ITEM parameters[])
  1.1053 +	{
  1.1054 +	STORE_OBJECT *object;
  1.1055 +	int i;
  1.1056 +
  1.1057 +	check_store(s,STORE_F_STORE_STORE_NUMBER,
  1.1058 +		store_object,STORE_R_NO_STORE_OBJECT_NUMBER_FUNCTION);
  1.1059 +
  1.1060 +	object = STORE_OBJECT_new();
  1.1061 +	if (!object)
  1.1062 +		{
  1.1063 +		STOREerr(STORE_F_STORE_STORE_NUMBER,
  1.1064 +			ERR_R_MALLOC_FAILURE);
  1.1065 +		return 0;
  1.1066 +		}
  1.1067 +	
  1.1068 +	object->data.number = data;
  1.1069 +
  1.1070 +	i = s->meth->store_object(s, STORE_OBJECT_TYPE_NUMBER, object,
  1.1071 +		attributes, parameters);
  1.1072 +
  1.1073 +	STORE_OBJECT_free(object);
  1.1074 +
  1.1075 +	if (!i)
  1.1076 +		{
  1.1077 +		STOREerr(STORE_F_STORE_STORE_NUMBER,
  1.1078 +			STORE_R_FAILED_STORING_NUMBER);
  1.1079 +		return 0;
  1.1080 +		}
  1.1081 +	return 1;
  1.1082 +	}
  1.1083 +
  1.1084 +EXPORT_C int STORE_modify_number(STORE *s, OPENSSL_ITEM search_attributes[],
  1.1085 +	OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
  1.1086 +	OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
  1.1087 +	{
  1.1088 +	check_store(s,STORE_F_STORE_MODIFY_NUMBER,
  1.1089 +		modify_object,STORE_R_NO_MODIFY_OBJECT_FUNCTION);
  1.1090 +
  1.1091 +	if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_NUMBER,
  1.1092 +		    search_attributes, add_attributes, modify_attributes,
  1.1093 +		    delete_attributes, parameters))
  1.1094 +		{
  1.1095 +		STOREerr(STORE_F_STORE_MODIFY_NUMBER,
  1.1096 +			STORE_R_FAILED_MODIFYING_NUMBER);
  1.1097 +		return 0;
  1.1098 +		}
  1.1099 +	return 1;
  1.1100 +	}
  1.1101 +
  1.1102 +EXPORT_C BIGNUM *STORE_get_number(STORE *s, OPENSSL_ITEM attributes[],
  1.1103 +	OPENSSL_ITEM parameters[])
  1.1104 +	{
  1.1105 +	STORE_OBJECT *object;
  1.1106 +	BIGNUM *n;
  1.1107 +
  1.1108 +	check_store(s,STORE_F_STORE_GET_NUMBER,
  1.1109 +		get_object,STORE_R_NO_GET_OBJECT_NUMBER_FUNCTION);
  1.1110 +
  1.1111 +	object = s->meth->get_object(s, STORE_OBJECT_TYPE_NUMBER, attributes,
  1.1112 +		parameters);
  1.1113 +	if (!object || !object->data.number)
  1.1114 +		{
  1.1115 +		STOREerr(STORE_F_STORE_GET_NUMBER,
  1.1116 +			STORE_R_FAILED_GETTING_NUMBER);
  1.1117 +		return 0;
  1.1118 +		}
  1.1119 +	n = object->data.number;
  1.1120 +	object->data.number = NULL;
  1.1121 +	STORE_OBJECT_free(object);
  1.1122 +	return n;
  1.1123 +	}
  1.1124 +
  1.1125 +EXPORT_C int STORE_delete_number(STORE *s, OPENSSL_ITEM attributes[],
  1.1126 +	OPENSSL_ITEM parameters[])
  1.1127 +	{
  1.1128 +	check_store(s,STORE_F_STORE_DELETE_NUMBER,
  1.1129 +		delete_object,STORE_R_NO_DELETE_NUMBER_FUNCTION);
  1.1130 +
  1.1131 +	if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_NUMBER, attributes,
  1.1132 +		    parameters))
  1.1133 +		{
  1.1134 +		STOREerr(STORE_F_STORE_DELETE_NUMBER,
  1.1135 +			STORE_R_FAILED_DELETING_NUMBER);
  1.1136 +		return 0;
  1.1137 +		}
  1.1138 +	return 1;
  1.1139 +	}
  1.1140 +
  1.1141 +EXPORT_C int STORE_store_arbitrary(STORE *s, BUF_MEM *data, OPENSSL_ITEM attributes[],
  1.1142 +	OPENSSL_ITEM parameters[])
  1.1143 +	{
  1.1144 +	STORE_OBJECT *object;
  1.1145 +	int i;
  1.1146 +
  1.1147 +	check_store(s,STORE_F_STORE_STORE_ARBITRARY,
  1.1148 +		store_object,STORE_R_NO_STORE_OBJECT_ARBITRARY_FUNCTION);
  1.1149 +
  1.1150 +	object = STORE_OBJECT_new();
  1.1151 +	if (!object)
  1.1152 +		{
  1.1153 +		STOREerr(STORE_F_STORE_STORE_ARBITRARY,
  1.1154 +			ERR_R_MALLOC_FAILURE);
  1.1155 +		return 0;
  1.1156 +		}
  1.1157 +	
  1.1158 +	object->data.arbitrary = data;
  1.1159 +
  1.1160 +	i = s->meth->store_object(s, STORE_OBJECT_TYPE_ARBITRARY, object,
  1.1161 +		attributes, parameters);
  1.1162 +
  1.1163 +	STORE_OBJECT_free(object);
  1.1164 +
  1.1165 +	if (!i)
  1.1166 +		{
  1.1167 +		STOREerr(STORE_F_STORE_STORE_ARBITRARY,
  1.1168 +			STORE_R_FAILED_STORING_ARBITRARY);
  1.1169 +		return 0;
  1.1170 +		}
  1.1171 +	return 1;
  1.1172 +	}
  1.1173 +
  1.1174 +EXPORT_C int STORE_modify_arbitrary(STORE *s, OPENSSL_ITEM search_attributes[],
  1.1175 +	OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
  1.1176 +	OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
  1.1177 +	{
  1.1178 +	check_store(s,STORE_F_STORE_MODIFY_ARBITRARY,
  1.1179 +		modify_object,STORE_R_NO_MODIFY_OBJECT_FUNCTION);
  1.1180 +
  1.1181 +	if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_ARBITRARY,
  1.1182 +		    search_attributes, add_attributes, modify_attributes,
  1.1183 +		    delete_attributes, parameters))
  1.1184 +		{
  1.1185 +		STOREerr(STORE_F_STORE_MODIFY_ARBITRARY,
  1.1186 +			STORE_R_FAILED_MODIFYING_ARBITRARY);
  1.1187 +		return 0;
  1.1188 +		}
  1.1189 +	return 1;
  1.1190 +	}
  1.1191 +
  1.1192 +EXPORT_C BUF_MEM *STORE_get_arbitrary(STORE *s, OPENSSL_ITEM attributes[],
  1.1193 +	OPENSSL_ITEM parameters[])
  1.1194 +	{
  1.1195 +	STORE_OBJECT *object;
  1.1196 +	BUF_MEM *b;
  1.1197 +
  1.1198 +	check_store(s,STORE_F_STORE_GET_ARBITRARY,
  1.1199 +		get_object,STORE_R_NO_GET_OBJECT_ARBITRARY_FUNCTION);
  1.1200 +
  1.1201 +	object = s->meth->get_object(s, STORE_OBJECT_TYPE_ARBITRARY,
  1.1202 +		attributes, parameters);
  1.1203 +	if (!object || !object->data.arbitrary)
  1.1204 +		{
  1.1205 +		STOREerr(STORE_F_STORE_GET_ARBITRARY,
  1.1206 +			STORE_R_FAILED_GETTING_ARBITRARY);
  1.1207 +		return 0;
  1.1208 +		}
  1.1209 +	b = object->data.arbitrary;
  1.1210 +	object->data.arbitrary = NULL;
  1.1211 +	STORE_OBJECT_free(object);
  1.1212 +	return b;
  1.1213 +	}
  1.1214 +
  1.1215 +EXPORT_C int STORE_delete_arbitrary(STORE *s, OPENSSL_ITEM attributes[],
  1.1216 +	OPENSSL_ITEM parameters[])
  1.1217 +	{
  1.1218 +	check_store(s,STORE_F_STORE_DELETE_ARBITRARY,
  1.1219 +		delete_object,STORE_R_NO_DELETE_ARBITRARY_FUNCTION);
  1.1220 +
  1.1221 +	if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_ARBITRARY, attributes,
  1.1222 +		    parameters))
  1.1223 +		{
  1.1224 +		STOREerr(STORE_F_STORE_DELETE_ARBITRARY,
  1.1225 +			STORE_R_FAILED_DELETING_ARBITRARY);
  1.1226 +		return 0;
  1.1227 +		}
  1.1228 +	return 1;
  1.1229 +	}
  1.1230 +
  1.1231 +EXPORT_C STORE_OBJECT *STORE_OBJECT_new(void)
  1.1232 +	{
  1.1233 +	STORE_OBJECT *object = OPENSSL_malloc(sizeof(STORE_OBJECT));
  1.1234 +	if (object) memset(object, 0, sizeof(STORE_OBJECT));
  1.1235 +	return object;
  1.1236 +	}
  1.1237 +EXPORT_C void STORE_OBJECT_free(STORE_OBJECT *data)
  1.1238 +	{
  1.1239 +	if (!data) return;
  1.1240 +	switch (data->type)
  1.1241 +		{
  1.1242 +	case STORE_OBJECT_TYPE_X509_CERTIFICATE:
  1.1243 +		X509_free(data->data.x509.certificate);
  1.1244 +		break;
  1.1245 +	case STORE_OBJECT_TYPE_X509_CRL:
  1.1246 +		X509_CRL_free(data->data.crl);
  1.1247 +		break;
  1.1248 +	case STORE_OBJECT_TYPE_PRIVATE_KEY:
  1.1249 +	case STORE_OBJECT_TYPE_PUBLIC_KEY:
  1.1250 +		EVP_PKEY_free(data->data.key);
  1.1251 +		break;
  1.1252 +	case STORE_OBJECT_TYPE_NUMBER:
  1.1253 +		BN_free(data->data.number);
  1.1254 +		break;
  1.1255 +	case STORE_OBJECT_TYPE_ARBITRARY:
  1.1256 +		BUF_MEM_free(data->data.arbitrary);
  1.1257 +		break;
  1.1258 +		}
  1.1259 +	OPENSSL_free(data);
  1.1260 +	}
  1.1261 +
  1.1262 +IMPLEMENT_STACK_OF(STORE_OBJECT*)
  1.1263 +
  1.1264 +
  1.1265 +struct STORE_attr_info_st
  1.1266 +	{
  1.1267 +	unsigned char set[(STORE_ATTR_TYPE_NUM + 8) / 8];
  1.1268 +	union
  1.1269 +		{
  1.1270 +		char *cstring;
  1.1271 +		unsigned char *sha1string;
  1.1272 +		X509_NAME *dn;
  1.1273 +		BIGNUM *number;
  1.1274 +		void *any;
  1.1275 +		} values[STORE_ATTR_TYPE_NUM+1];
  1.1276 +	size_t value_sizes[STORE_ATTR_TYPE_NUM+1];
  1.1277 +	};
  1.1278 +
  1.1279 +#define ATTR_IS_SET(a,i)	((i) > 0 && (i) < STORE_ATTR_TYPE_NUM \
  1.1280 +				&& ((a)->set[(i) / 8] & (1 << ((i) % 8))))
  1.1281 +#define SET_ATTRBIT(a,i)	((a)->set[(i) / 8] |= (1 << ((i) % 8)))
  1.1282 +#define CLEAR_ATTRBIT(a,i)	((a)->set[(i) / 8] &= ~(1 << ((i) % 8)))
  1.1283 +
  1.1284 +EXPORT_C STORE_ATTR_INFO *STORE_ATTR_INFO_new(void)
  1.1285 +	{
  1.1286 +	return (STORE_ATTR_INFO *)OPENSSL_malloc(sizeof(STORE_ATTR_INFO));
  1.1287 +	}
  1.1288 +static void STORE_ATTR_INFO_attr_free(STORE_ATTR_INFO *attrs,
  1.1289 +	STORE_ATTR_TYPES code)
  1.1290 +	{
  1.1291 +	if (ATTR_IS_SET(attrs,code))
  1.1292 +		{
  1.1293 +		switch(code)
  1.1294 +			{
  1.1295 +		case STORE_ATTR_FRIENDLYNAME:
  1.1296 +		case STORE_ATTR_EMAIL:
  1.1297 +		case STORE_ATTR_FILENAME:
  1.1298 +			STORE_ATTR_INFO_modify_cstr(attrs, code, NULL, 0);
  1.1299 +			break;
  1.1300 +		case STORE_ATTR_KEYID:
  1.1301 +		case STORE_ATTR_ISSUERKEYID:
  1.1302 +		case STORE_ATTR_SUBJECTKEYID:
  1.1303 +		case STORE_ATTR_ISSUERSERIALHASH:
  1.1304 +		case STORE_ATTR_CERTHASH:
  1.1305 +			STORE_ATTR_INFO_modify_sha1str(attrs, code, NULL, 0);
  1.1306 +			break;
  1.1307 +		case STORE_ATTR_ISSUER:
  1.1308 +		case STORE_ATTR_SUBJECT:
  1.1309 +			STORE_ATTR_INFO_modify_dn(attrs, code, NULL);
  1.1310 +			break;
  1.1311 +		case STORE_ATTR_SERIAL:
  1.1312 +			STORE_ATTR_INFO_modify_number(attrs, code, NULL);
  1.1313 +			break;
  1.1314 +		default:
  1.1315 +			break;
  1.1316 +			}
  1.1317 +		}
  1.1318 +	}
  1.1319 +EXPORT_C int STORE_ATTR_INFO_free(STORE_ATTR_INFO *attrs)
  1.1320 +	{
  1.1321 +	if (attrs)
  1.1322 +		{
  1.1323 +		STORE_ATTR_TYPES i;
  1.1324 +		for(i = 0; i++ < STORE_ATTR_TYPE_NUM;)
  1.1325 +			STORE_ATTR_INFO_attr_free(attrs, i);
  1.1326 +		OPENSSL_free(attrs);
  1.1327 +		}
  1.1328 +	return 1;
  1.1329 +	}
  1.1330 +EXPORT_C char *STORE_ATTR_INFO_get0_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code)
  1.1331 +	{
  1.1332 +	if (!attrs)
  1.1333 +		{
  1.1334 +		STOREerr(STORE_F_STORE_ATTR_INFO_GET0_CSTR,
  1.1335 +			ERR_R_PASSED_NULL_PARAMETER);
  1.1336 +		return NULL;
  1.1337 +		}
  1.1338 +	if (ATTR_IS_SET(attrs,code))
  1.1339 +		return attrs->values[code].cstring;
  1.1340 +	STOREerr(STORE_F_STORE_ATTR_INFO_GET0_CSTR,
  1.1341 +		STORE_R_NO_VALUE);
  1.1342 +	return NULL;
  1.1343 +	}
  1.1344 +EXPORT_C unsigned char *STORE_ATTR_INFO_get0_sha1str(STORE_ATTR_INFO *attrs,
  1.1345 +	STORE_ATTR_TYPES code)
  1.1346 +	{
  1.1347 +	if (!attrs)
  1.1348 +		{
  1.1349 +		STOREerr(STORE_F_STORE_ATTR_INFO_GET0_SHA1STR,
  1.1350 +			ERR_R_PASSED_NULL_PARAMETER);
  1.1351 +		return NULL;
  1.1352 +		}
  1.1353 +	if (ATTR_IS_SET(attrs,code))
  1.1354 +		return attrs->values[code].sha1string;
  1.1355 +	STOREerr(STORE_F_STORE_ATTR_INFO_GET0_SHA1STR,
  1.1356 +		STORE_R_NO_VALUE);
  1.1357 +	return NULL;
  1.1358 +	}
  1.1359 +EXPORT_C X509_NAME *STORE_ATTR_INFO_get0_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code)
  1.1360 +	{
  1.1361 +	if (!attrs)
  1.1362 +		{
  1.1363 +		STOREerr(STORE_F_STORE_ATTR_INFO_GET0_DN,
  1.1364 +			ERR_R_PASSED_NULL_PARAMETER);
  1.1365 +		return NULL;
  1.1366 +		}
  1.1367 +	if (ATTR_IS_SET(attrs,code))
  1.1368 +		return attrs->values[code].dn;
  1.1369 +	STOREerr(STORE_F_STORE_ATTR_INFO_GET0_DN,
  1.1370 +		STORE_R_NO_VALUE);
  1.1371 +	return NULL;
  1.1372 +	}
  1.1373 +EXPORT_C BIGNUM *STORE_ATTR_INFO_get0_number(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code)
  1.1374 +	{
  1.1375 +	if (!attrs)
  1.1376 +		{
  1.1377 +		STOREerr(STORE_F_STORE_ATTR_INFO_GET0_NUMBER,
  1.1378 +			ERR_R_PASSED_NULL_PARAMETER);
  1.1379 +		return NULL;
  1.1380 +		}
  1.1381 +	if (ATTR_IS_SET(attrs,code))
  1.1382 +		return attrs->values[code].number;
  1.1383 +	STOREerr(STORE_F_STORE_ATTR_INFO_GET0_NUMBER,
  1.1384 +		STORE_R_NO_VALUE);
  1.1385 +	return NULL;
  1.1386 +	}
  1.1387 +EXPORT_C int STORE_ATTR_INFO_set_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
  1.1388 +	char *cstr, size_t cstr_size)
  1.1389 +	{
  1.1390 +	if (!attrs)
  1.1391 +		{
  1.1392 +		STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR,
  1.1393 +			ERR_R_PASSED_NULL_PARAMETER);
  1.1394 +		return 0;
  1.1395 +		}
  1.1396 +	if (!ATTR_IS_SET(attrs,code))
  1.1397 +		{
  1.1398 +		if ((attrs->values[code].cstring = BUF_strndup(cstr, cstr_size)))
  1.1399 +			return 1;
  1.1400 +		STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR,
  1.1401 +			ERR_R_MALLOC_FAILURE);
  1.1402 +		return 0;
  1.1403 +		}
  1.1404 +	STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR, STORE_R_ALREADY_HAS_A_VALUE);
  1.1405 +	return 0;
  1.1406 +	}
  1.1407 +EXPORT_C int STORE_ATTR_INFO_set_sha1str(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
  1.1408 +	unsigned char *sha1str, size_t sha1str_size)
  1.1409 +	{
  1.1410 +	if (!attrs)
  1.1411 +		{
  1.1412 +		STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR,
  1.1413 +			ERR_R_PASSED_NULL_PARAMETER);
  1.1414 +		return 0;
  1.1415 +		}
  1.1416 +	if (!ATTR_IS_SET(attrs,code))
  1.1417 +		{
  1.1418 +		if ((attrs->values[code].sha1string =
  1.1419 +			    (unsigned char *)BUF_memdup(sha1str,
  1.1420 +				    sha1str_size)))
  1.1421 +			return 1;
  1.1422 +		STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR,
  1.1423 +			ERR_R_MALLOC_FAILURE);
  1.1424 +		return 0;
  1.1425 +		}
  1.1426 +	STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR, STORE_R_ALREADY_HAS_A_VALUE);
  1.1427 +	return 0;
  1.1428 +	}
  1.1429 +EXPORT_C int STORE_ATTR_INFO_set_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
  1.1430 +	X509_NAME *dn)
  1.1431 +	{
  1.1432 +	if (!attrs)
  1.1433 +		{
  1.1434 +		STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN,
  1.1435 +			ERR_R_PASSED_NULL_PARAMETER);
  1.1436 +		return 0;
  1.1437 +		}
  1.1438 +	if (!ATTR_IS_SET(attrs,code))
  1.1439 +		{
  1.1440 +		if ((attrs->values[code].dn = X509_NAME_dup(dn)))
  1.1441 +			return 1;
  1.1442 +		STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN,
  1.1443 +			ERR_R_MALLOC_FAILURE);
  1.1444 +		return 0;
  1.1445 +		}
  1.1446 +	STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN, STORE_R_ALREADY_HAS_A_VALUE);
  1.1447 +	return 0;
  1.1448 +	}
  1.1449 +EXPORT_C int STORE_ATTR_INFO_set_number(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
  1.1450 +	BIGNUM *number)
  1.1451 +	{
  1.1452 +	if (!attrs)
  1.1453 +		{
  1.1454 +		STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER,
  1.1455 +			ERR_R_PASSED_NULL_PARAMETER);
  1.1456 +		return 0;
  1.1457 +		}
  1.1458 +	if (!ATTR_IS_SET(attrs,code))
  1.1459 +		{
  1.1460 +		if ((attrs->values[code].number = BN_dup(number)))
  1.1461 +			return 1;
  1.1462 +		STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER,
  1.1463 +			ERR_R_MALLOC_FAILURE);
  1.1464 +		return 0;
  1.1465 +		}
  1.1466 +	STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER, STORE_R_ALREADY_HAS_A_VALUE);
  1.1467 +	return 0;
  1.1468 +	}
  1.1469 +EXPORT_C int STORE_ATTR_INFO_modify_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
  1.1470 +	char *cstr, size_t cstr_size)
  1.1471 +	{
  1.1472 +	if (!attrs)
  1.1473 +		{
  1.1474 +		STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_CSTR,
  1.1475 +			ERR_R_PASSED_NULL_PARAMETER);
  1.1476 +		return 0;
  1.1477 +		}
  1.1478 +	if (ATTR_IS_SET(attrs,code))
  1.1479 +		{
  1.1480 +		OPENSSL_free(attrs->values[code].cstring);
  1.1481 +		attrs->values[code].cstring = NULL;
  1.1482 +		CLEAR_ATTRBIT(attrs, code);
  1.1483 +		}
  1.1484 +	return STORE_ATTR_INFO_set_cstr(attrs, code, cstr, cstr_size);
  1.1485 +	}
  1.1486 +EXPORT_C int STORE_ATTR_INFO_modify_sha1str(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
  1.1487 +	unsigned char *sha1str, size_t sha1str_size)
  1.1488 +	{
  1.1489 +	if (!attrs)
  1.1490 +		{
  1.1491 +		STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_SHA1STR,
  1.1492 +			ERR_R_PASSED_NULL_PARAMETER);
  1.1493 +		return 0;
  1.1494 +		}
  1.1495 +	if (ATTR_IS_SET(attrs,code))
  1.1496 +		{
  1.1497 +		OPENSSL_free(attrs->values[code].sha1string);
  1.1498 +		attrs->values[code].sha1string = NULL;
  1.1499 +		CLEAR_ATTRBIT(attrs, code);
  1.1500 +		}
  1.1501 +	return STORE_ATTR_INFO_set_sha1str(attrs, code, sha1str, sha1str_size);
  1.1502 +	}
  1.1503 +EXPORT_C int STORE_ATTR_INFO_modify_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
  1.1504 +	X509_NAME *dn)
  1.1505 +	{
  1.1506 +	if (!attrs)
  1.1507 +		{
  1.1508 +		STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_DN,
  1.1509 +			ERR_R_PASSED_NULL_PARAMETER);
  1.1510 +		return 0;
  1.1511 +		}
  1.1512 +	if (ATTR_IS_SET(attrs,code))
  1.1513 +		{
  1.1514 +		OPENSSL_free(attrs->values[code].dn);
  1.1515 +		attrs->values[code].dn = NULL;
  1.1516 +		CLEAR_ATTRBIT(attrs, code);
  1.1517 +		}
  1.1518 +	return STORE_ATTR_INFO_set_dn(attrs, code, dn);
  1.1519 +	}
  1.1520 +EXPORT_C int STORE_ATTR_INFO_modify_number(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
  1.1521 +	BIGNUM *number)
  1.1522 +	{
  1.1523 +	if (!attrs)
  1.1524 +		{
  1.1525 +		STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_NUMBER,
  1.1526 +			ERR_R_PASSED_NULL_PARAMETER);
  1.1527 +		return 0;
  1.1528 +		}
  1.1529 +	if (ATTR_IS_SET(attrs,code))
  1.1530 +		{
  1.1531 +		OPENSSL_free(attrs->values[code].number);
  1.1532 +		attrs->values[code].number = NULL;
  1.1533 +		CLEAR_ATTRBIT(attrs, code);
  1.1534 +		}
  1.1535 +	return STORE_ATTR_INFO_set_number(attrs, code, number);
  1.1536 +	}
  1.1537 +
  1.1538 +struct attr_list_ctx_st
  1.1539 +	{
  1.1540 +	OPENSSL_ITEM *attributes;
  1.1541 +	};
  1.1542 +EXPORT_C void *STORE_parse_attrs_start(OPENSSL_ITEM *attributes)
  1.1543 +	{
  1.1544 +	if (attributes)
  1.1545 +		{
  1.1546 +		struct attr_list_ctx_st *context =
  1.1547 +			(struct attr_list_ctx_st *)OPENSSL_malloc(sizeof(struct attr_list_ctx_st));
  1.1548 +		if (context)
  1.1549 +			context->attributes = attributes;
  1.1550 +		else
  1.1551 +			STOREerr(STORE_F_STORE_PARSE_ATTRS_START,
  1.1552 +				ERR_R_MALLOC_FAILURE);
  1.1553 +		return context;
  1.1554 +		}
  1.1555 +	STOREerr(STORE_F_STORE_PARSE_ATTRS_START, ERR_R_PASSED_NULL_PARAMETER);
  1.1556 +	return 0;
  1.1557 +	}
  1.1558 +EXPORT_C STORE_ATTR_INFO *STORE_parse_attrs_next(void *handle)
  1.1559 +	{
  1.1560 +	struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;
  1.1561 +
  1.1562 +	if (context && context->attributes)
  1.1563 +		{
  1.1564 +		STORE_ATTR_INFO *attrs = NULL;
  1.1565 +
  1.1566 +		while(context->attributes
  1.1567 +			&& context->attributes->code != STORE_ATTR_OR
  1.1568 +			&& context->attributes->code != STORE_ATTR_END)
  1.1569 +			{
  1.1570 +			switch(context->attributes->code)
  1.1571 +				{
  1.1572 +			case STORE_ATTR_FRIENDLYNAME:
  1.1573 +			case STORE_ATTR_EMAIL:
  1.1574 +			case STORE_ATTR_FILENAME:
  1.1575 +				if (!attrs) attrs = STORE_ATTR_INFO_new();
  1.1576 +				if (attrs == NULL)
  1.1577 +					{
  1.1578 +					STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
  1.1579 +						ERR_R_MALLOC_FAILURE);
  1.1580 +					goto err;
  1.1581 +					}
  1.1582 +				STORE_ATTR_INFO_set_cstr(attrs,
  1.1583 +					context->attributes->code,
  1.1584 +					context->attributes->value,
  1.1585 +					context->attributes->value_size);
  1.1586 +				break;
  1.1587 +			case STORE_ATTR_KEYID:
  1.1588 +			case STORE_ATTR_ISSUERKEYID:
  1.1589 +			case STORE_ATTR_SUBJECTKEYID:
  1.1590 +			case STORE_ATTR_ISSUERSERIALHASH:
  1.1591 +			case STORE_ATTR_CERTHASH:
  1.1592 +				if (!attrs) attrs = STORE_ATTR_INFO_new();
  1.1593 +				if (attrs == NULL)
  1.1594 +					{
  1.1595 +					STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
  1.1596 +						ERR_R_MALLOC_FAILURE);
  1.1597 +					goto err;
  1.1598 +					}
  1.1599 +				STORE_ATTR_INFO_set_sha1str(attrs,
  1.1600 +					context->attributes->code,
  1.1601 +					context->attributes->value,
  1.1602 +					context->attributes->value_size);
  1.1603 +				break;
  1.1604 +			case STORE_ATTR_ISSUER:
  1.1605 +			case STORE_ATTR_SUBJECT:
  1.1606 +				if (!attrs) attrs = STORE_ATTR_INFO_new();
  1.1607 +				if (attrs == NULL)
  1.1608 +					{
  1.1609 +					STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
  1.1610 +						ERR_R_MALLOC_FAILURE);
  1.1611 +					goto err;
  1.1612 +					}
  1.1613 +				STORE_ATTR_INFO_modify_dn(attrs,
  1.1614 +					context->attributes->code,
  1.1615 +					context->attributes->value);
  1.1616 +				break;
  1.1617 +			case STORE_ATTR_SERIAL:
  1.1618 +				if (!attrs) attrs = STORE_ATTR_INFO_new();
  1.1619 +				if (attrs == NULL)
  1.1620 +					{
  1.1621 +					STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
  1.1622 +						ERR_R_MALLOC_FAILURE);
  1.1623 +					goto err;
  1.1624 +					}
  1.1625 +				STORE_ATTR_INFO_modify_number(attrs,
  1.1626 +					context->attributes->code,
  1.1627 +					context->attributes->value);
  1.1628 +				break;
  1.1629 +				}
  1.1630 +			context->attributes++;
  1.1631 +			}
  1.1632 +		if (context->attributes->code == STORE_ATTR_OR)
  1.1633 +			context->attributes++;
  1.1634 +		return attrs;
  1.1635 +	err:
  1.1636 +		while(context->attributes
  1.1637 +			&& context->attributes->code != STORE_ATTR_OR
  1.1638 +			&& context->attributes->code != STORE_ATTR_END)
  1.1639 +			context->attributes++;
  1.1640 +		if (context->attributes->code == STORE_ATTR_OR)
  1.1641 +			context->attributes++;
  1.1642 +		return NULL;
  1.1643 +		}
  1.1644 +	STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT, ERR_R_PASSED_NULL_PARAMETER);
  1.1645 +	return NULL;
  1.1646 +	}
  1.1647 +EXPORT_C int STORE_parse_attrs_end(void *handle)
  1.1648 +	{
  1.1649 +	struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;
  1.1650 +
  1.1651 +	if (context && context->attributes)
  1.1652 +		{
  1.1653 +#if 0
  1.1654 +		OPENSSL_ITEM *attributes = context->attributes;
  1.1655 +#endif
  1.1656 +		OPENSSL_free(context);
  1.1657 +		return 1;
  1.1658 +		}
  1.1659 +	STOREerr(STORE_F_STORE_PARSE_ATTRS_END, ERR_R_PASSED_NULL_PARAMETER);
  1.1660 +	return 0;
  1.1661 +	}
  1.1662 +
  1.1663 +EXPORT_C int STORE_parse_attrs_endp(void *handle)
  1.1664 +	{
  1.1665 +	struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;
  1.1666 +
  1.1667 +	if (context && context->attributes)
  1.1668 +		{
  1.1669 +		return context->attributes->code == STORE_ATTR_END;
  1.1670 +		}
  1.1671 +	STOREerr(STORE_F_STORE_PARSE_ATTRS_ENDP, ERR_R_PASSED_NULL_PARAMETER);
  1.1672 +	return 0;
  1.1673 +	}
  1.1674 +
  1.1675 +static int attr_info_compare_compute_range(
  1.1676 +	unsigned char *abits, unsigned char *bbits,
  1.1677 +	unsigned int *alowp, unsigned int *ahighp,
  1.1678 +	unsigned int *blowp, unsigned int *bhighp)
  1.1679 +	{
  1.1680 +	unsigned int alow = (unsigned int)-1, ahigh = 0;
  1.1681 +	unsigned int blow = (unsigned int)-1, bhigh = 0;
  1.1682 +	int i, res = 0;
  1.1683 +
  1.1684 +	for (i = 0; i < (STORE_ATTR_TYPE_NUM + 8) / 8; i++, abits++, bbits++)
  1.1685 +		{
  1.1686 +		if (res == 0)
  1.1687 +			{
  1.1688 +			if (*abits < *bbits) res = -1;
  1.1689 +			if (*abits > *bbits) res = 1;
  1.1690 +			}
  1.1691 +		if (*abits)
  1.1692 +			{
  1.1693 +			if (alow == (unsigned int)-1)
  1.1694 +				{
  1.1695 +				alow = i * 8;
  1.1696 +				if (!(*abits & 0x01)) alow++;
  1.1697 +				if (!(*abits & 0x02)) alow++;
  1.1698 +				if (!(*abits & 0x04)) alow++;
  1.1699 +				if (!(*abits & 0x08)) alow++;
  1.1700 +				if (!(*abits & 0x10)) alow++;
  1.1701 +				if (!(*abits & 0x20)) alow++;
  1.1702 +				if (!(*abits & 0x40)) alow++;
  1.1703 +				}
  1.1704 +			ahigh = i * 8 + 7;
  1.1705 +			if (!(*abits & 0x80)) ahigh++;
  1.1706 +			if (!(*abits & 0x40)) ahigh++;
  1.1707 +			if (!(*abits & 0x20)) ahigh++;
  1.1708 +			if (!(*abits & 0x10)) ahigh++;
  1.1709 +			if (!(*abits & 0x08)) ahigh++;
  1.1710 +			if (!(*abits & 0x04)) ahigh++;
  1.1711 +			if (!(*abits & 0x02)) ahigh++;
  1.1712 +			}
  1.1713 +		if (*bbits)
  1.1714 +			{
  1.1715 +			if (blow == (unsigned int)-1)
  1.1716 +				{
  1.1717 +				blow = i * 8;
  1.1718 +				if (!(*bbits & 0x01)) blow++;
  1.1719 +				if (!(*bbits & 0x02)) blow++;
  1.1720 +				if (!(*bbits & 0x04)) blow++;
  1.1721 +				if (!(*bbits & 0x08)) blow++;
  1.1722 +				if (!(*bbits & 0x10)) blow++;
  1.1723 +				if (!(*bbits & 0x20)) blow++;
  1.1724 +				if (!(*bbits & 0x40)) blow++;
  1.1725 +				}
  1.1726 +			bhigh = i * 8 + 7;
  1.1727 +			if (!(*bbits & 0x80)) bhigh++;
  1.1728 +			if (!(*bbits & 0x40)) bhigh++;
  1.1729 +			if (!(*bbits & 0x20)) bhigh++;
  1.1730 +			if (!(*bbits & 0x10)) bhigh++;
  1.1731 +			if (!(*bbits & 0x08)) bhigh++;
  1.1732 +			if (!(*bbits & 0x04)) bhigh++;
  1.1733 +			if (!(*bbits & 0x02)) bhigh++;
  1.1734 +			}
  1.1735 +		}
  1.1736 +	if (ahigh + alow < bhigh + blow) res = -1;
  1.1737 +	if (ahigh + alow > bhigh + blow) res = 1;
  1.1738 +	if (alowp) *alowp = alow;
  1.1739 +	if (ahighp) *ahighp = ahigh;
  1.1740 +	if (blowp) *blowp = blow;
  1.1741 +	if (bhighp) *bhighp = bhigh;
  1.1742 +	return res;
  1.1743 +	}
  1.1744 +
  1.1745 +EXPORT_C int STORE_ATTR_INFO_compare(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
  1.1746 +	{
  1.1747 +	if (a == b) return 0;
  1.1748 +	if (!a) return -1;
  1.1749 +	if (!b) return 1;
  1.1750 +	return attr_info_compare_compute_range(a->set, b->set, 0, 0, 0, 0);
  1.1751 +	}
  1.1752 +EXPORT_C int STORE_ATTR_INFO_in_range(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
  1.1753 +	{
  1.1754 +	unsigned int alow, ahigh, blow, bhigh;
  1.1755 +
  1.1756 +	if (a == b) return 1;
  1.1757 +	if (!a) return 0;
  1.1758 +	if (!b) return 0;
  1.1759 +	attr_info_compare_compute_range(a->set, b->set,
  1.1760 +		&alow, &ahigh, &blow, &bhigh);
  1.1761 +	if (alow >= blow && ahigh <= bhigh)
  1.1762 +		return 1;
  1.1763 +	return 0;
  1.1764 +	}
  1.1765 +EXPORT_C int STORE_ATTR_INFO_in(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
  1.1766 +	{
  1.1767 +	unsigned char *abits, *bbits;
  1.1768 +	int i;
  1.1769 +
  1.1770 +	if (a == b) return 1;
  1.1771 +	if (!a) return 0;
  1.1772 +	if (!b) return 0;
  1.1773 +	abits = a->set;
  1.1774 +	bbits = b->set;
  1.1775 +	for (i = 0; i < (STORE_ATTR_TYPE_NUM + 8) / 8; i++, abits++, bbits++)
  1.1776 +		{
  1.1777 +		if (*abits && (*bbits & *abits) != *abits)
  1.1778 +			return 0;
  1.1779 +		}
  1.1780 +	return 1;
  1.1781 +	}
  1.1782 +EXPORT_C int STORE_ATTR_INFO_in_ex(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
  1.1783 +	{
  1.1784 +	STORE_ATTR_TYPES i;
  1.1785 +
  1.1786 +	if (a == b) return 1;
  1.1787 +	if (!STORE_ATTR_INFO_in(a, b)) return 0;
  1.1788 +	for (i = 1; i < STORE_ATTR_TYPE_NUM; i++)
  1.1789 +		if (ATTR_IS_SET(a, i))
  1.1790 +			{
  1.1791 +			switch(i)
  1.1792 +				{
  1.1793 +			case STORE_ATTR_FRIENDLYNAME:
  1.1794 +			case STORE_ATTR_EMAIL:
  1.1795 +			case STORE_ATTR_FILENAME:
  1.1796 +				if (strcmp(a->values[i].cstring,
  1.1797 +					    b->values[i].cstring))
  1.1798 +					return 0;
  1.1799 +				break;
  1.1800 +			case STORE_ATTR_KEYID:
  1.1801 +			case STORE_ATTR_ISSUERKEYID:
  1.1802 +			case STORE_ATTR_SUBJECTKEYID:
  1.1803 +			case STORE_ATTR_ISSUERSERIALHASH:
  1.1804 +			case STORE_ATTR_CERTHASH:
  1.1805 +				if (memcmp(a->values[i].sha1string,
  1.1806 +					    b->values[i].sha1string,
  1.1807 +					    a->value_sizes[i]))
  1.1808 +					return 0;
  1.1809 +				break;
  1.1810 +			case STORE_ATTR_ISSUER:
  1.1811 +			case STORE_ATTR_SUBJECT:
  1.1812 +				if (X509_NAME_cmp(a->values[i].dn,
  1.1813 +					    b->values[i].dn))
  1.1814 +					return 0;
  1.1815 +				break;
  1.1816 +			case STORE_ATTR_SERIAL:
  1.1817 +				if (BN_cmp(a->values[i].number,
  1.1818 +					    b->values[i].number))
  1.1819 +					return 0;
  1.1820 +				break;
  1.1821 +			default:
  1.1822 +				break;
  1.1823 +				}
  1.1824 +			}
  1.1825 +
  1.1826 +	return 1;
  1.1827 +	}