/* crypto/store/str_lib.c -*- mode:C; c-file-style: "eay" -*- */
/*
 * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
 * 2003.
 */
/* ====================================================================
 * Copyright (c) 2003 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    openssl-core@openssl.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */

#include <string.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#ifndef OPENSSL_NO_ENGINE
# include <openssl/engine.h>
#endif
#include <openssl/sha.h>
#include <openssl/x509.h>
#include "str_locl.h"

const char *const STORE_object_type_string[STORE_OBJECT_TYPE_NUM + 1] = {
    0,
    "X.509 Certificate",
    "X.509 CRL",
    "Private Key",
    "Public Key",
    "Number",
    "Arbitrary Data"
};

const int STORE_param_sizes[STORE_PARAM_TYPE_NUM + 1] = {
    0,
    sizeof(int),                /* EVP_TYPE */
    sizeof(size_t),             /* BITS */
    -1,                         /* KEY_PARAMETERS */
    0                           /* KEY_NO_PARAMETERS */
};

const int STORE_attr_sizes[STORE_ATTR_TYPE_NUM + 1] = {
    0,
    -1,                         /* FRIENDLYNAME: C string */
    SHA_DIGEST_LENGTH,          /* KEYID: SHA1 digest, 160 bits */
    SHA_DIGEST_LENGTH,          /* ISSUERKEYID: SHA1 digest, 160 bits */
    SHA_DIGEST_LENGTH,          /* SUBJECTKEYID: SHA1 digest, 160 bits */
    SHA_DIGEST_LENGTH,          /* ISSUERSERIALHASH: SHA1 digest, 160 bits */
    sizeof(X509_NAME *),        /* ISSUER: X509_NAME * */
    sizeof(BIGNUM *),           /* SERIAL: BIGNUM * */
    sizeof(X509_NAME *),        /* SUBJECT: X509_NAME * */
    SHA_DIGEST_LENGTH,          /* CERTHASH: SHA1 digest, 160 bits */
    -1,                         /* EMAIL: C string */
    -1,                         /* FILENAME: C string */
};

STORE *STORE_new_method(const STORE_METHOD *method)
{
    STORE *ret;

    if (method == NULL) {
        STOREerr(STORE_F_STORE_NEW_METHOD, ERR_R_PASSED_NULL_PARAMETER);
        return NULL;
    }

    ret = (STORE *)OPENSSL_malloc(sizeof(STORE));
    if (ret == NULL) {
        STOREerr(STORE_F_STORE_NEW_METHOD, ERR_R_MALLOC_FAILURE);
        return NULL;
    }

    ret->meth = method;

    CRYPTO_new_ex_data(CRYPTO_EX_INDEX_STORE, ret, &ret->ex_data);
    if (ret->meth->init && !ret->meth->init(ret)) {
        STORE_free(ret);
        ret = NULL;
    }
    return ret;
}

STORE *STORE_new_engine(ENGINE *engine)
{
    STORE *ret = NULL;
    ENGINE *e = engine;
    const STORE_METHOD *meth = 0;

#ifdef OPENSSL_NO_ENGINE
    e = NULL;
#else
    if (engine) {
        if (!ENGINE_init(engine)) {
            STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_ENGINE_LIB);
            return NULL;
        }
        e = engine;
    } else {
        STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
        return NULL;
    }
    if (e) {
        meth = ENGINE_get_STORE(e);
        if (!meth) {
            STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_ENGINE_LIB);
            ENGINE_finish(e);
            return NULL;
        }
    }
#endif

    ret = STORE_new_method(meth);
    if (ret == NULL) {
        STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_STORE_LIB);
        return NULL;
    }

    ret->engine = e;

    return (ret);
}

void STORE_free(STORE *store)
{
    if (store == NULL)
        return;
    if (store->meth->clean)
        store->meth->clean(store);
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_STORE, store, &store->ex_data);
    OPENSSL_free(store);
}

int STORE_ctrl(STORE *store, int cmd, long i, void *p, void (*f) (void))
{
    if (store == NULL) {
        STOREerr(STORE_F_STORE_CTRL, ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }
    if (store->meth->ctrl)
        return store->meth->ctrl(store, cmd, i, p, f);
    STOREerr(STORE_F_STORE_CTRL, STORE_R_NO_CONTROL_FUNCTION);
    return 0;
}

int STORE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
                           CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
{
    return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_STORE, argl, argp,
                                   new_func, dup_func, free_func);
}

int STORE_set_ex_data(STORE *r, int idx, void *arg)
{
    return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
}

void *STORE_get_ex_data(STORE *r, int idx)
{
    return (CRYPTO_get_ex_data(&r->ex_data, idx));
}

const STORE_METHOD *STORE_get_method(STORE *store)
{
    return store->meth;
}

const STORE_METHOD *STORE_set_method(STORE *store, const STORE_METHOD *meth)
{
    store->meth = meth;
    return store->meth;
}

/* API helpers */

#define check_store(s,fncode,fnname,fnerrcode) \
        do \
                { \
                if ((s) == NULL || (s)->meth == NULL) \
                        { \
                        STOREerr((fncode), ERR_R_PASSED_NULL_PARAMETER); \
                        return 0; \
                        } \
                if ((s)->meth->fnname == NULL) \
                        { \
                        STOREerr((fncode), (fnerrcode)); \
                        return 0; \
                        } \
                } \
        while(0)

/* API functions */

X509 *STORE_get_certificate(STORE *s, OPENSSL_ITEM attributes[],
                            OPENSSL_ITEM parameters[])
{
    STORE_OBJECT *object;
    X509 *x;

    check_store(s, STORE_F_STORE_GET_CERTIFICATE,
                get_object, STORE_R_NO_GET_OBJECT_FUNCTION);

    object = s->meth->get_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
                                 attributes, parameters);
    if (!object || !object->data.x509.certificate) {
        STOREerr(STORE_F_STORE_GET_CERTIFICATE,
                 STORE_R_FAILED_GETTING_CERTIFICATE);
        return 0;
    }
    CRYPTO_add(&object->data.x509.certificate->references, 1,
               CRYPTO_LOCK_X509);
#ifdef REF_PRINT
    REF_PRINT("X509", data);
#endif
    x = object->data.x509.certificate;
    STORE_OBJECT_free(object);
    return x;
}

int STORE_store_certificate(STORE *s, X509 *data, OPENSSL_ITEM attributes[],
                            OPENSSL_ITEM parameters[])
{
    STORE_OBJECT *object;
    int i;

    check_store(s, STORE_F_STORE_CERTIFICATE,
                store_object, STORE_R_NO_STORE_OBJECT_FUNCTION);

    object = STORE_OBJECT_new();
    if (!object) {
        STOREerr(STORE_F_STORE_STORE_CERTIFICATE, ERR_R_MALLOC_FAILURE);
        return 0;
    }

    CRYPTO_add(&data->references, 1, CRYPTO_LOCK_X509);
#ifdef REF_PRINT
    REF_PRINT("X509", data);
#endif
    object->data.x509.certificate = data;

    i = s->meth->store_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
                              object, attributes, parameters);

    STORE_OBJECT_free(object);

    if (!i) {
        STOREerr(STORE_F_STORE_STORE_CERTIFICATE,
                 STORE_R_FAILED_STORING_CERTIFICATE);
        return 0;
    }
    return 1;
}

int STORE_modify_certificate(STORE *s, OPENSSL_ITEM search_attributes[],
                             OPENSSL_ITEM add_attributes[],
                             OPENSSL_ITEM modify_attributes[],
                             OPENSSL_ITEM delete_attributes[],
                             OPENSSL_ITEM parameters[])
{
    check_store(s, STORE_F_STORE_MODIFY_CERTIFICATE,
                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);

    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
                                search_attributes, add_attributes,
                                modify_attributes, delete_attributes,
                                parameters)) {
        STOREerr(STORE_F_STORE_MODIFY_CERTIFICATE,
                 STORE_R_FAILED_MODIFYING_CERTIFICATE);
        return 0;
    }
    return 1;
}

int STORE_revoke_certificate(STORE *s, OPENSSL_ITEM attributes[],
                             OPENSSL_ITEM parameters[])
{
    check_store(s, STORE_F_STORE_REVOKE_CERTIFICATE,
                revoke_object, STORE_R_NO_REVOKE_OBJECT_FUNCTION);

    if (!s->meth->revoke_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
                                attributes, parameters)) {
        STOREerr(STORE_F_STORE_REVOKE_CERTIFICATE,
                 STORE_R_FAILED_REVOKING_CERTIFICATE);
        return 0;
    }
    return 1;
}

int STORE_delete_certificate(STORE *s, OPENSSL_ITEM attributes[],
                             OPENSSL_ITEM parameters[])
{
    check_store(s, STORE_F_STORE_DELETE_CERTIFICATE,
                delete_object, STORE_R_NO_DELETE_OBJECT_FUNCTION);

    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
                                attributes, parameters)) {
        STOREerr(STORE_F_STORE_DELETE_CERTIFICATE,
                 STORE_R_FAILED_DELETING_CERTIFICATE);
        return 0;
    }
    return 1;
}

void *STORE_list_certificate_start(STORE *s, OPENSSL_ITEM attributes[],
                                   OPENSSL_ITEM parameters[])
{
    void *handle;

    check_store(s, STORE_F_STORE_LIST_CERTIFICATE_START,
                list_object_start, STORE_R_NO_LIST_OBJECT_START_FUNCTION);

    handle = s->meth->list_object_start(s,
                                        STORE_OBJECT_TYPE_X509_CERTIFICATE,
                                        attributes, parameters);
    if (!handle) {
        STOREerr(STORE_F_STORE_LIST_CERTIFICATE_START,
                 STORE_R_FAILED_LISTING_CERTIFICATES);
        return 0;
    }
    return handle;
}

X509 *STORE_list_certificate_next(STORE *s, void *handle)
{
    STORE_OBJECT *object;
    X509 *x;

    check_store(s, STORE_F_STORE_LIST_CERTIFICATE_NEXT,
                list_object_next, STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);

    object = s->meth->list_object_next(s, handle);
    if (!object || !object->data.x509.certificate) {
        STOREerr(STORE_F_STORE_LIST_CERTIFICATE_NEXT,
                 STORE_R_FAILED_LISTING_CERTIFICATES);
        return 0;
    }
    CRYPTO_add(&object->data.x509.certificate->references, 1,
               CRYPTO_LOCK_X509);
#ifdef REF_PRINT
    REF_PRINT("X509", data);
#endif
    x = object->data.x509.certificate;
    STORE_OBJECT_free(object);
    return x;
}

int STORE_list_certificate_end(STORE *s, void *handle)
{
    check_store(s, STORE_F_STORE_LIST_CERTIFICATE_END,
                list_object_end, STORE_R_NO_LIST_OBJECT_END_FUNCTION);

    if (!s->meth->list_object_end(s, handle)) {
        STOREerr(STORE_F_STORE_LIST_CERTIFICATE_END,
                 STORE_R_FAILED_LISTING_CERTIFICATES);
        return 0;
    }
    return 1;
}

int STORE_list_certificate_endp(STORE *s, void *handle)
{
    check_store(s, STORE_F_STORE_LIST_CERTIFICATE_ENDP,
                list_object_endp, STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);

    if (!s->meth->list_object_endp(s, handle)) {
        STOREerr(STORE_F_STORE_LIST_CERTIFICATE_ENDP,
                 STORE_R_FAILED_LISTING_CERTIFICATES);
        return 0;
    }
    return 1;
}

EVP_PKEY *STORE_generate_key(STORE *s, OPENSSL_ITEM attributes[],
                             OPENSSL_ITEM parameters[])
{
    STORE_OBJECT *object;
    EVP_PKEY *pkey;

    check_store(s, STORE_F_STORE_GENERATE_KEY,
                generate_object, STORE_R_NO_GENERATE_OBJECT_FUNCTION);

    object = s->meth->generate_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
                                      attributes, parameters);
    if (!object || !object->data.key) {
        STOREerr(STORE_F_STORE_GENERATE_KEY, STORE_R_FAILED_GENERATING_KEY);
        return 0;
    }
    CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
#ifdef REF_PRINT
    REF_PRINT("EVP_PKEY", data);
#endif
    pkey = object->data.key;
    STORE_OBJECT_free(object);
    return pkey;
}

EVP_PKEY *STORE_get_private_key(STORE *s, OPENSSL_ITEM attributes[],
                                OPENSSL_ITEM parameters[])
{
    STORE_OBJECT *object;
    EVP_PKEY *pkey;

    check_store(s, STORE_F_STORE_GET_PRIVATE_KEY,
                get_object, STORE_R_NO_GET_OBJECT_FUNCTION);

    object = s->meth->get_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
                                 attributes, parameters);
    if (!object || !object->data.key || !object->data.key) {
        STOREerr(STORE_F_STORE_GET_PRIVATE_KEY, STORE_R_FAILED_GETTING_KEY);
        return 0;
    }
    CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
#ifdef REF_PRINT
    REF_PRINT("EVP_PKEY", data);
#endif
    pkey = object->data.key;
    STORE_OBJECT_free(object);
    return pkey;
}

int STORE_store_private_key(STORE *s, EVP_PKEY *data,
                            OPENSSL_ITEM attributes[],
                            OPENSSL_ITEM parameters[])
{
    STORE_OBJECT *object;
    int i;

    check_store(s, STORE_F_STORE_STORE_PRIVATE_KEY,
                store_object, STORE_R_NO_STORE_OBJECT_FUNCTION);

    object = STORE_OBJECT_new();
    if (!object) {
        STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY, ERR_R_MALLOC_FAILURE);
        return 0;
    }
    object->data.key = EVP_PKEY_new();
    if (!object->data.key) {
        STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY, ERR_R_MALLOC_FAILURE);
        return 0;
    }

    CRYPTO_add(&data->references, 1, CRYPTO_LOCK_EVP_PKEY);
#ifdef REF_PRINT
    REF_PRINT("EVP_PKEY", data);
#endif
    object->data.key = data;

    i = s->meth->store_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY, object,
                              attributes, parameters);

    STORE_OBJECT_free(object);

    if (!i) {
        STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY, STORE_R_FAILED_STORING_KEY);
        return 0;
    }
    return i;
}

int STORE_modify_private_key(STORE *s, OPENSSL_ITEM search_attributes[],
                             OPENSSL_ITEM add_attributes[],
                             OPENSSL_ITEM modify_attributes[],
                             OPENSSL_ITEM delete_attributes[],
                             OPENSSL_ITEM parameters[])
{
    check_store(s, STORE_F_STORE_MODIFY_PRIVATE_KEY,
                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);

    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
                                search_attributes, add_attributes,
                                modify_attributes, delete_attributes,
                                parameters)) {
        STOREerr(STORE_F_STORE_MODIFY_PRIVATE_KEY,
                 STORE_R_FAILED_MODIFYING_PRIVATE_KEY);
        return 0;
    }
    return 1;
}

int STORE_revoke_private_key(STORE *s, OPENSSL_ITEM attributes[],
                             OPENSSL_ITEM parameters[])
{
    int i;

    check_store(s, STORE_F_STORE_REVOKE_PRIVATE_KEY,
                revoke_object, STORE_R_NO_REVOKE_OBJECT_FUNCTION);

    i = s->meth->revoke_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
                               attributes, parameters);

    if (!i) {
        STOREerr(STORE_F_STORE_REVOKE_PRIVATE_KEY,
                 STORE_R_FAILED_REVOKING_KEY);
        return 0;
    }
    return i;
}

int STORE_delete_private_key(STORE *s, OPENSSL_ITEM attributes[],
                             OPENSSL_ITEM parameters[])
{
    check_store(s, STORE_F_STORE_DELETE_PRIVATE_KEY,
                delete_object, STORE_R_NO_DELETE_OBJECT_FUNCTION);

    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
                                attributes, parameters)) {
        STOREerr(STORE_F_STORE_DELETE_PRIVATE_KEY,
                 STORE_R_FAILED_DELETING_KEY);
        return 0;
    }
    return 1;
}

void *STORE_list_private_key_start(STORE *s, OPENSSL_ITEM attributes[],
                                   OPENSSL_ITEM parameters[])
{
    void *handle;

    check_store(s, STORE_F_STORE_LIST_PRIVATE_KEY_START,
                list_object_start, STORE_R_NO_LIST_OBJECT_START_FUNCTION);

    handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
                                        attributes, parameters);
    if (!handle) {
        STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_START,
                 STORE_R_FAILED_LISTING_KEYS);
        return 0;
    }
    return handle;
}

EVP_PKEY *STORE_list_private_key_next(STORE *s, void *handle)
{
    STORE_OBJECT *object;
    EVP_PKEY *pkey;

    check_store(s, STORE_F_STORE_LIST_PRIVATE_KEY_NEXT,
                list_object_next, STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);

    object = s->meth->list_object_next(s, handle);
    if (!object || !object->data.key || !object->data.key) {
        STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_NEXT,
                 STORE_R_FAILED_LISTING_KEYS);
        return 0;
    }
    CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
#ifdef REF_PRINT
    REF_PRINT("EVP_PKEY", data);
#endif
    pkey = object->data.key;
    STORE_OBJECT_free(object);
    return pkey;
}

int STORE_list_private_key_end(STORE *s, void *handle)
{
    check_store(s, STORE_F_STORE_LIST_PRIVATE_KEY_END,
                list_object_end, STORE_R_NO_LIST_OBJECT_END_FUNCTION);

    if (!s->meth->list_object_end(s, handle)) {
        STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_END,
                 STORE_R_FAILED_LISTING_KEYS);
        return 0;
    }
    return 1;
}

int STORE_list_private_key_endp(STORE *s, void *handle)
{
    check_store(s, STORE_F_STORE_LIST_PRIVATE_KEY_ENDP,
                list_object_endp, STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);

    if (!s->meth->list_object_endp(s, handle)) {
        STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_ENDP,
                 STORE_R_FAILED_LISTING_KEYS);
        return 0;
    }
    return 1;
}

EVP_PKEY *STORE_get_public_key(STORE *s, OPENSSL_ITEM attributes[],
                               OPENSSL_ITEM parameters[])
{
    STORE_OBJECT *object;
    EVP_PKEY *pkey;

    check_store(s, STORE_F_STORE_GET_PUBLIC_KEY,
                get_object, STORE_R_NO_GET_OBJECT_FUNCTION);

    object = s->meth->get_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
                                 attributes, parameters);
    if (!object || !object->data.key || !object->data.key) {
        STOREerr(STORE_F_STORE_GET_PUBLIC_KEY, STORE_R_FAILED_GETTING_KEY);
        return 0;
    }
    CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
#ifdef REF_PRINT
    REF_PRINT("EVP_PKEY", data);
#endif
    pkey = object->data.key;
    STORE_OBJECT_free(object);
    return pkey;
}

int STORE_store_public_key(STORE *s, EVP_PKEY *data,
                           OPENSSL_ITEM attributes[],
                           OPENSSL_ITEM parameters[])
{
    STORE_OBJECT *object;
    int i;

    check_store(s, STORE_F_STORE_STORE_PUBLIC_KEY,
                store_object, STORE_R_NO_STORE_OBJECT_FUNCTION);

    object = STORE_OBJECT_new();
    if (!object) {
        STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY, ERR_R_MALLOC_FAILURE);
        return 0;
    }
    object->data.key = EVP_PKEY_new();
    if (!object->data.key) {
        STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY, ERR_R_MALLOC_FAILURE);
        return 0;
    }

    CRYPTO_add(&data->references, 1, CRYPTO_LOCK_EVP_PKEY);
#ifdef REF_PRINT
    REF_PRINT("EVP_PKEY", data);
#endif
    object->data.key = data;

    i = s->meth->store_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY, object,
                              attributes, parameters);

    STORE_OBJECT_free(object);

    if (!i) {
        STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY, STORE_R_FAILED_STORING_KEY);
        return 0;
    }
    return i;
}

int STORE_modify_public_key(STORE *s, OPENSSL_ITEM search_attributes[],
                            OPENSSL_ITEM add_attributes[],
                            OPENSSL_ITEM modify_attributes[],
                            OPENSSL_ITEM delete_attributes[],
                            OPENSSL_ITEM parameters[])
{
    check_store(s, STORE_F_STORE_MODIFY_PUBLIC_KEY,
                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);

    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
                                search_attributes, add_attributes,
                                modify_attributes, delete_attributes,
                                parameters)) {
        STOREerr(STORE_F_STORE_MODIFY_PUBLIC_KEY,
                 STORE_R_FAILED_MODIFYING_PUBLIC_KEY);
        return 0;
    }
    return 1;
}

int STORE_revoke_public_key(STORE *s, OPENSSL_ITEM attributes[],
                            OPENSSL_ITEM parameters[])
{
    int i;

    check_store(s, STORE_F_STORE_REVOKE_PUBLIC_KEY,
                revoke_object, STORE_R_NO_REVOKE_OBJECT_FUNCTION);

    i = s->meth->revoke_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
                               attributes, parameters);

    if (!i) {
        STOREerr(STORE_F_STORE_REVOKE_PUBLIC_KEY,
                 STORE_R_FAILED_REVOKING_KEY);
        return 0;
    }
    return i;
}

int STORE_delete_public_key(STORE *s, OPENSSL_ITEM attributes[],
                            OPENSSL_ITEM parameters[])
{
    check_store(s, STORE_F_STORE_DELETE_PUBLIC_KEY,
                delete_object, STORE_R_NO_DELETE_OBJECT_FUNCTION);

    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
                                attributes, parameters)) {
        STOREerr(STORE_F_STORE_DELETE_PUBLIC_KEY,
                 STORE_R_FAILED_DELETING_KEY);
        return 0;
    }
    return 1;
}

void *STORE_list_public_key_start(STORE *s, OPENSSL_ITEM attributes[],
                                  OPENSSL_ITEM parameters[])
{
    void *handle;

    check_store(s, STORE_F_STORE_LIST_PUBLIC_KEY_START,
                list_object_start, STORE_R_NO_LIST_OBJECT_START_FUNCTION);

    handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
                                        attributes, parameters);
    if (!handle) {
        STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_START,
                 STORE_R_FAILED_LISTING_KEYS);
        return 0;
    }
    return handle;
}

EVP_PKEY *STORE_list_public_key_next(STORE *s, void *handle)
{
    STORE_OBJECT *object;
    EVP_PKEY *pkey;

    check_store(s, STORE_F_STORE_LIST_PUBLIC_KEY_NEXT,
                list_object_next, STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);

    object = s->meth->list_object_next(s, handle);
    if (!object || !object->data.key || !object->data.key) {
        STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_NEXT,
                 STORE_R_FAILED_LISTING_KEYS);
        return 0;
    }
    CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
#ifdef REF_PRINT
    REF_PRINT("EVP_PKEY", data);
#endif
    pkey = object->data.key;
    STORE_OBJECT_free(object);
    return pkey;
}

int STORE_list_public_key_end(STORE *s, void *handle)
{
    check_store(s, STORE_F_STORE_LIST_PUBLIC_KEY_END,
                list_object_end, STORE_R_NO_LIST_OBJECT_END_FUNCTION);

    if (!s->meth->list_object_end(s, handle)) {
        STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_END,
                 STORE_R_FAILED_LISTING_KEYS);
        return 0;
    }
    return 1;
}

int STORE_list_public_key_endp(STORE *s, void *handle)
{
    check_store(s, STORE_F_STORE_LIST_PUBLIC_KEY_ENDP,
                list_object_endp, STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);

    if (!s->meth->list_object_endp(s, handle)) {
        STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_ENDP,
                 STORE_R_FAILED_LISTING_KEYS);
        return 0;
    }
    return 1;
}

X509_CRL *STORE_generate_crl(STORE *s, OPENSSL_ITEM attributes[],
                             OPENSSL_ITEM parameters[])
{
    STORE_OBJECT *object;
    X509_CRL *crl;

    check_store(s, STORE_F_STORE_GENERATE_CRL,
                generate_object, STORE_R_NO_GENERATE_CRL_FUNCTION);

    object = s->meth->generate_object(s, STORE_OBJECT_TYPE_X509_CRL,
                                      attributes, parameters);
    if (!object || !object->data.crl) {
        STOREerr(STORE_F_STORE_GENERATE_CRL, STORE_R_FAILED_GENERATING_CRL);
        return 0;
    }
    CRYPTO_add(&object->data.crl->references, 1, CRYPTO_LOCK_X509_CRL);
#ifdef REF_PRINT
    REF_PRINT("X509_CRL", data);
#endif
    crl = object->data.crl;
    STORE_OBJECT_free(object);
    return crl;
}

X509_CRL *STORE_get_crl(STORE *s, OPENSSL_ITEM attributes[],
                        OPENSSL_ITEM parameters[])
{
    STORE_OBJECT *object;
    X509_CRL *crl;

    check_store(s, STORE_F_STORE_GET_CRL,
                get_object, STORE_R_NO_GET_OBJECT_FUNCTION);

    object = s->meth->get_object(s, STORE_OBJECT_TYPE_X509_CRL,
                                 attributes, parameters);
    if (!object || !object->data.crl) {
        STOREerr(STORE_F_STORE_GET_CRL, STORE_R_FAILED_GETTING_KEY);
        return 0;
    }
    CRYPTO_add(&object->data.crl->references, 1, CRYPTO_LOCK_X509_CRL);
#ifdef REF_PRINT
    REF_PRINT("X509_CRL", data);
#endif
    crl = object->data.crl;
    STORE_OBJECT_free(object);
    return crl;
}

int STORE_store_crl(STORE *s, X509_CRL *data, OPENSSL_ITEM attributes[],
                    OPENSSL_ITEM parameters[])
{
    STORE_OBJECT *object;
    int i;

    check_store(s, STORE_F_STORE_STORE_CRL,
                store_object, STORE_R_NO_STORE_OBJECT_FUNCTION);

    object = STORE_OBJECT_new();
    if (!object) {
        STOREerr(STORE_F_STORE_STORE_CRL, ERR_R_MALLOC_FAILURE);
        return 0;
    }

    CRYPTO_add(&data->references, 1, CRYPTO_LOCK_X509_CRL);
#ifdef REF_PRINT
    REF_PRINT("X509_CRL", data);
#endif
    object->data.crl = data;

    i = s->meth->store_object(s, STORE_OBJECT_TYPE_X509_CRL, object,
                              attributes, parameters);

    STORE_OBJECT_free(object);

    if (!i) {
        STOREerr(STORE_F_STORE_STORE_CRL, STORE_R_FAILED_STORING_KEY);
        return 0;
    }
    return i;
}

int STORE_modify_crl(STORE *s, OPENSSL_ITEM search_attributes[],
                     OPENSSL_ITEM add_attributes[],
                     OPENSSL_ITEM modify_attributes[],
                     OPENSSL_ITEM delete_attributes[],
                     OPENSSL_ITEM parameters[])
{
    check_store(s, STORE_F_STORE_MODIFY_CRL,
                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);

    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_X509_CRL,
                                search_attributes, add_attributes,
                                modify_attributes, delete_attributes,
                                parameters)) {
        STOREerr(STORE_F_STORE_MODIFY_CRL, STORE_R_FAILED_MODIFYING_CRL);
        return 0;
    }
    return 1;
}

int STORE_delete_crl(STORE *s, OPENSSL_ITEM attributes[],
                     OPENSSL_ITEM parameters[])
{
    check_store(s, STORE_F_STORE_DELETE_CRL,
                delete_object, STORE_R_NO_DELETE_OBJECT_FUNCTION);

    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_X509_CRL,
                                attributes, parameters)) {
        STOREerr(STORE_F_STORE_DELETE_CRL, STORE_R_FAILED_DELETING_KEY);
        return 0;
    }
    return 1;
}

void *STORE_list_crl_start(STORE *s, OPENSSL_ITEM attributes[],
                           OPENSSL_ITEM parameters[])
{
    void *handle;

    check_store(s, STORE_F_STORE_LIST_CRL_START,
                list_object_start, STORE_R_NO_LIST_OBJECT_START_FUNCTION);

    handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_X509_CRL,
                                        attributes, parameters);
    if (!handle) {
        STOREerr(STORE_F_STORE_LIST_CRL_START, STORE_R_FAILED_LISTING_KEYS);
        return 0;
    }
    return handle;
}

X509_CRL *STORE_list_crl_next(STORE *s, void *handle)
{
    STORE_OBJECT *object;
    X509_CRL *crl;

    check_store(s, STORE_F_STORE_LIST_CRL_NEXT,
                list_object_next, STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);

    object = s->meth->list_object_next(s, handle);
    if (!object || !object->data.crl) {
        STOREerr(STORE_F_STORE_LIST_CRL_NEXT, STORE_R_FAILED_LISTING_KEYS);
        return 0;
    }
    CRYPTO_add(&object->data.crl->references, 1, CRYPTO_LOCK_X509_CRL);
#ifdef REF_PRINT
    REF_PRINT("X509_CRL", data);
#endif
    crl = object->data.crl;
    STORE_OBJECT_free(object);
    return crl;
}

int STORE_list_crl_end(STORE *s, void *handle)
{
    check_store(s, STORE_F_STORE_LIST_CRL_END,
                list_object_end, STORE_R_NO_LIST_OBJECT_END_FUNCTION);

    if (!s->meth->list_object_end(s, handle)) {
        STOREerr(STORE_F_STORE_LIST_CRL_END, STORE_R_FAILED_LISTING_KEYS);
        return 0;
    }
    return 1;
}

int STORE_list_crl_endp(STORE *s, void *handle)
{
    check_store(s, STORE_F_STORE_LIST_CRL_ENDP,
                list_object_endp, STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);

    if (!s->meth->list_object_endp(s, handle)) {
        STOREerr(STORE_F_STORE_LIST_CRL_ENDP, STORE_R_FAILED_LISTING_KEYS);
        return 0;
    }
    return 1;
}

int STORE_store_number(STORE *s, BIGNUM *data, OPENSSL_ITEM attributes[],
                       OPENSSL_ITEM parameters[])
{
    STORE_OBJECT *object;
    int i;

    check_store(s, STORE_F_STORE_STORE_NUMBER,
                store_object, STORE_R_NO_STORE_OBJECT_NUMBER_FUNCTION);

    object = STORE_OBJECT_new();
    if (!object) {
        STOREerr(STORE_F_STORE_STORE_NUMBER, ERR_R_MALLOC_FAILURE);
        return 0;
    }

    object->data.number = data;

    i = s->meth->store_object(s, STORE_OBJECT_TYPE_NUMBER, object,
                              attributes, parameters);

    STORE_OBJECT_free(object);

    if (!i) {
        STOREerr(STORE_F_STORE_STORE_NUMBER, STORE_R_FAILED_STORING_NUMBER);
        return 0;
    }
    return 1;
}

int STORE_modify_number(STORE *s, OPENSSL_ITEM search_attributes[],
                        OPENSSL_ITEM add_attributes[],
                        OPENSSL_ITEM modify_attributes[],
                        OPENSSL_ITEM delete_attributes[],
                        OPENSSL_ITEM parameters[])
{
    check_store(s, STORE_F_STORE_MODIFY_NUMBER,
                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);

    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_NUMBER,
                                search_attributes, add_attributes,
                                modify_attributes, delete_attributes,
                                parameters)) {
        STOREerr(STORE_F_STORE_MODIFY_NUMBER,
                 STORE_R_FAILED_MODIFYING_NUMBER);
        return 0;
    }
    return 1;
}

BIGNUM *STORE_get_number(STORE *s, OPENSSL_ITEM attributes[],
                         OPENSSL_ITEM parameters[])
{
    STORE_OBJECT *object;
    BIGNUM *n;

    check_store(s, STORE_F_STORE_GET_NUMBER,
                get_object, STORE_R_NO_GET_OBJECT_NUMBER_FUNCTION);

    object = s->meth->get_object(s, STORE_OBJECT_TYPE_NUMBER, attributes,
                                 parameters);
    if (!object || !object->data.number) {
        STOREerr(STORE_F_STORE_GET_NUMBER, STORE_R_FAILED_GETTING_NUMBER);
        return 0;
    }
    n = object->data.number;
    object->data.number = NULL;
    STORE_OBJECT_free(object);
    return n;
}

int STORE_delete_number(STORE *s, OPENSSL_ITEM attributes[],
                        OPENSSL_ITEM parameters[])
{
    check_store(s, STORE_F_STORE_DELETE_NUMBER,
                delete_object, STORE_R_NO_DELETE_NUMBER_FUNCTION);

    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_NUMBER, attributes,
                                parameters)) {
        STOREerr(STORE_F_STORE_DELETE_NUMBER, STORE_R_FAILED_DELETING_NUMBER);
        return 0;
    }
    return 1;
}

int STORE_store_arbitrary(STORE *s, BUF_MEM *data, OPENSSL_ITEM attributes[],
                          OPENSSL_ITEM parameters[])
{
    STORE_OBJECT *object;
    int i;

    check_store(s, STORE_F_STORE_STORE_ARBITRARY,
                store_object, STORE_R_NO_STORE_OBJECT_ARBITRARY_FUNCTION);

    object = STORE_OBJECT_new();
    if (!object) {
        STOREerr(STORE_F_STORE_STORE_ARBITRARY, ERR_R_MALLOC_FAILURE);
        return 0;
    }

    object->data.arbitrary = data;

    i = s->meth->store_object(s, STORE_OBJECT_TYPE_ARBITRARY, object,
                              attributes, parameters);

    STORE_OBJECT_free(object);

    if (!i) {
        STOREerr(STORE_F_STORE_STORE_ARBITRARY,
                 STORE_R_FAILED_STORING_ARBITRARY);
        return 0;
    }
    return 1;
}

int STORE_modify_arbitrary(STORE *s, OPENSSL_ITEM search_attributes[],
                           OPENSSL_ITEM add_attributes[],
                           OPENSSL_ITEM modify_attributes[],
                           OPENSSL_ITEM delete_attributes[],
                           OPENSSL_ITEM parameters[])
{
    check_store(s, STORE_F_STORE_MODIFY_ARBITRARY,
                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);

    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_ARBITRARY,
                                search_attributes, add_attributes,
                                modify_attributes, delete_attributes,
                                parameters)) {
        STOREerr(STORE_F_STORE_MODIFY_ARBITRARY,
                 STORE_R_FAILED_MODIFYING_ARBITRARY);
        return 0;
    }
    return 1;
}

BUF_MEM *STORE_get_arbitrary(STORE *s, OPENSSL_ITEM attributes[],
                             OPENSSL_ITEM parameters[])
{
    STORE_OBJECT *object;
    BUF_MEM *b;

    check_store(s, STORE_F_STORE_GET_ARBITRARY,
                get_object, STORE_R_NO_GET_OBJECT_ARBITRARY_FUNCTION);

    object = s->meth->get_object(s, STORE_OBJECT_TYPE_ARBITRARY,
                                 attributes, parameters);
    if (!object || !object->data.arbitrary) {
        STOREerr(STORE_F_STORE_GET_ARBITRARY,
                 STORE_R_FAILED_GETTING_ARBITRARY);
        return 0;
    }
    b = object->data.arbitrary;
    object->data.arbitrary = NULL;
    STORE_OBJECT_free(object);
    return b;
}

int STORE_delete_arbitrary(STORE *s, OPENSSL_ITEM attributes[],
                           OPENSSL_ITEM parameters[])
{
    check_store(s, STORE_F_STORE_DELETE_ARBITRARY,
                delete_object, STORE_R_NO_DELETE_ARBITRARY_FUNCTION);

    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_ARBITRARY, attributes,
                                parameters)) {
        STOREerr(STORE_F_STORE_DELETE_ARBITRARY,
                 STORE_R_FAILED_DELETING_ARBITRARY);
        return 0;
    }
    return 1;
}

STORE_OBJECT *STORE_OBJECT_new(void)
{
    STORE_OBJECT *object = OPENSSL_malloc(sizeof(STORE_OBJECT));
    if (object)
        memset(object, 0, sizeof(STORE_OBJECT));
    return object;
}

void STORE_OBJECT_free(STORE_OBJECT *data)
{
    if (!data)
        return;
    switch (data->type) {
    case STORE_OBJECT_TYPE_X509_CERTIFICATE:
        X509_free(data->data.x509.certificate);
        break;
    case STORE_OBJECT_TYPE_X509_CRL:
        X509_CRL_free(data->data.crl);
        break;
    case STORE_OBJECT_TYPE_PRIVATE_KEY:
    case STORE_OBJECT_TYPE_PUBLIC_KEY:
        EVP_PKEY_free(data->data.key);
        break;
    case STORE_OBJECT_TYPE_NUMBER:
        BN_free(data->data.number);
        break;
    case STORE_OBJECT_TYPE_ARBITRARY:
        BUF_MEM_free(data->data.arbitrary);
        break;
    }
    OPENSSL_free(data);
}

IMPLEMENT_STACK_OF(STORE_OBJECT*)

struct STORE_attr_info_st {
    unsigned char set[(STORE_ATTR_TYPE_NUM + 8) / 8];
    union {
        char *cstring;
        unsigned char *sha1string;
        X509_NAME *dn;
        BIGNUM *number;
        void *any;
    } values[STORE_ATTR_TYPE_NUM + 1];
    size_t value_sizes[STORE_ATTR_TYPE_NUM + 1];
};

#define ATTR_IS_SET(a,i)        ((i) > 0 && (i) < STORE_ATTR_TYPE_NUM \
                                && ((a)->set[(i) / 8] & (1 << ((i) % 8))))
#define SET_ATTRBIT(a,i)        ((a)->set[(i) / 8] |= (1 << ((i) % 8)))
#define CLEAR_ATTRBIT(a,i)      ((a)->set[(i) / 8] &= ~(1 << ((i) % 8)))

STORE_ATTR_INFO *STORE_ATTR_INFO_new(void)
{
    return (STORE_ATTR_INFO *)OPENSSL_malloc(sizeof(STORE_ATTR_INFO));
}

static void STORE_ATTR_INFO_attr_free(STORE_ATTR_INFO *attrs,
                                      STORE_ATTR_TYPES code)
{
    if (ATTR_IS_SET(attrs, code)) {
        switch (code) {
        case STORE_ATTR_FRIENDLYNAME:
        case STORE_ATTR_EMAIL:
        case STORE_ATTR_FILENAME:
            STORE_ATTR_INFO_modify_cstr(attrs, code, NULL, 0);
            break;
        case STORE_ATTR_KEYID:
        case STORE_ATTR_ISSUERKEYID:
        case STORE_ATTR_SUBJECTKEYID:
        case STORE_ATTR_ISSUERSERIALHASH:
        case STORE_ATTR_CERTHASH:
            STORE_ATTR_INFO_modify_sha1str(attrs, code, NULL, 0);
            break;
        case STORE_ATTR_ISSUER:
        case STORE_ATTR_SUBJECT:
            STORE_ATTR_INFO_modify_dn(attrs, code, NULL);
            break;
        case STORE_ATTR_SERIAL:
            STORE_ATTR_INFO_modify_number(attrs, code, NULL);
            break;
        default:
            break;
        }
    }
}

int STORE_ATTR_INFO_free(STORE_ATTR_INFO *attrs)
{
    if (attrs) {
        STORE_ATTR_TYPES i;
        for (i = 0; i++ < STORE_ATTR_TYPE_NUM;)
            STORE_ATTR_INFO_attr_free(attrs, i);
        OPENSSL_free(attrs);
    }
    return 1;
}

char *STORE_ATTR_INFO_get0_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code)
{
    if (!attrs) {
        STOREerr(STORE_F_STORE_ATTR_INFO_GET0_CSTR,
                 ERR_R_PASSED_NULL_PARAMETER);
        return NULL;
    }
    if (ATTR_IS_SET(attrs, code))
        return attrs->values[code].cstring;
    STOREerr(STORE_F_STORE_ATTR_INFO_GET0_CSTR, STORE_R_NO_VALUE);
    return NULL;
}

unsigned char *STORE_ATTR_INFO_get0_sha1str(STORE_ATTR_INFO *attrs,
                                            STORE_ATTR_TYPES code)
{
    if (!attrs) {
        STOREerr(STORE_F_STORE_ATTR_INFO_GET0_SHA1STR,
                 ERR_R_PASSED_NULL_PARAMETER);
        return NULL;
    }
    if (ATTR_IS_SET(attrs, code))
        return attrs->values[code].sha1string;
    STOREerr(STORE_F_STORE_ATTR_INFO_GET0_SHA1STR, STORE_R_NO_VALUE);
    return NULL;
}

X509_NAME *STORE_ATTR_INFO_get0_dn(STORE_ATTR_INFO *attrs,
                                   STORE_ATTR_TYPES code)
{
    if (!attrs) {
        STOREerr(STORE_F_STORE_ATTR_INFO_GET0_DN,
                 ERR_R_PASSED_NULL_PARAMETER);
        return NULL;
    }
    if (ATTR_IS_SET(attrs, code))
        return attrs->values[code].dn;
    STOREerr(STORE_F_STORE_ATTR_INFO_GET0_DN, STORE_R_NO_VALUE);
    return NULL;
}

BIGNUM *STORE_ATTR_INFO_get0_number(STORE_ATTR_INFO *attrs,
                                    STORE_ATTR_TYPES code)
{
    if (!attrs) {
        STOREerr(STORE_F_STORE_ATTR_INFO_GET0_NUMBER,
                 ERR_R_PASSED_NULL_PARAMETER);
        return NULL;
    }
    if (ATTR_IS_SET(attrs, code))
        return attrs->values[code].number;
    STOREerr(STORE_F_STORE_ATTR_INFO_GET0_NUMBER, STORE_R_NO_VALUE);
    return NULL;
}

int STORE_ATTR_INFO_set_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
                             char *cstr, size_t cstr_size)
{
    if (!attrs) {
        STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR,
                 ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }
    if (!ATTR_IS_SET(attrs, code)) {
        if ((attrs->values[code].cstring = BUF_strndup(cstr, cstr_size)))
            return 1;
        STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR, ERR_R_MALLOC_FAILURE);
        return 0;
    }
    STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR, STORE_R_ALREADY_HAS_A_VALUE);
    return 0;
}

int STORE_ATTR_INFO_set_sha1str(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
                                unsigned char *sha1str, size_t sha1str_size)
{
    if (!attrs) {
        STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR,
                 ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }
    if (!ATTR_IS_SET(attrs, code)) {
        if ((attrs->values[code].sha1string =
             (unsigned char *)BUF_memdup(sha1str, sha1str_size)))
            return 1;
        STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR, ERR_R_MALLOC_FAILURE);
        return 0;
    }
    STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR,
             STORE_R_ALREADY_HAS_A_VALUE);
    return 0;
}

int STORE_ATTR_INFO_set_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
                           X509_NAME *dn)
{
    if (!attrs) {
        STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN, ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }
    if (!ATTR_IS_SET(attrs, code)) {
        if ((attrs->values[code].dn = X509_NAME_dup(dn)))
            return 1;
        STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN, ERR_R_MALLOC_FAILURE);
        return 0;
    }
    STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN, STORE_R_ALREADY_HAS_A_VALUE);
    return 0;
}

int STORE_ATTR_INFO_set_number(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
                               BIGNUM *number)
{
    if (!attrs) {
        STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER,
                 ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }
    if (!ATTR_IS_SET(attrs, code)) {
        if ((attrs->values[code].number = BN_dup(number)))
            return 1;
        STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER, ERR_R_MALLOC_FAILURE);
        return 0;
    }
    STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER, STORE_R_ALREADY_HAS_A_VALUE);
    return 0;
}

int STORE_ATTR_INFO_modify_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
                                char *cstr, size_t cstr_size)
{
    if (!attrs) {
        STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_CSTR,
                 ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }
    if (ATTR_IS_SET(attrs, code)) {
        OPENSSL_free(attrs->values[code].cstring);
        attrs->values[code].cstring = NULL;
        CLEAR_ATTRBIT(attrs, code);
    }
    return STORE_ATTR_INFO_set_cstr(attrs, code, cstr, cstr_size);
}

int STORE_ATTR_INFO_modify_sha1str(STORE_ATTR_INFO *attrs,
                                   STORE_ATTR_TYPES code,
                                   unsigned char *sha1str,
                                   size_t sha1str_size)
{
    if (!attrs) {
        STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_SHA1STR,
                 ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }
    if (ATTR_IS_SET(attrs, code)) {
        OPENSSL_free(attrs->values[code].sha1string);
        attrs->values[code].sha1string = NULL;
        CLEAR_ATTRBIT(attrs, code);
    }
    return STORE_ATTR_INFO_set_sha1str(attrs, code, sha1str, sha1str_size);
}

int STORE_ATTR_INFO_modify_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
                              X509_NAME *dn)
{
    if (!attrs) {
        STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_DN,
                 ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }
    if (ATTR_IS_SET(attrs, code)) {
        OPENSSL_free(attrs->values[code].dn);
        attrs->values[code].dn = NULL;
        CLEAR_ATTRBIT(attrs, code);
    }
    return STORE_ATTR_INFO_set_dn(attrs, code, dn);
}

int STORE_ATTR_INFO_modify_number(STORE_ATTR_INFO *attrs,
                                  STORE_ATTR_TYPES code, BIGNUM *number)
{
    if (!attrs) {
        STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_NUMBER,
                 ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }
    if (ATTR_IS_SET(attrs, code)) {
        OPENSSL_free(attrs->values[code].number);
        attrs->values[code].number = NULL;
        CLEAR_ATTRBIT(attrs, code);
    }
    return STORE_ATTR_INFO_set_number(attrs, code, number);
}

struct attr_list_ctx_st {
    OPENSSL_ITEM *attributes;
};
void *STORE_parse_attrs_start(OPENSSL_ITEM *attributes)
{
    if (attributes) {
        struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)
            OPENSSL_malloc(sizeof(struct attr_list_ctx_st));
        if (context)
            context->attributes = attributes;
        else
            STOREerr(STORE_F_STORE_PARSE_ATTRS_START, ERR_R_MALLOC_FAILURE);
        return context;
    }
    STOREerr(STORE_F_STORE_PARSE_ATTRS_START, ERR_R_PASSED_NULL_PARAMETER);
    return 0;
}

STORE_ATTR_INFO *STORE_parse_attrs_next(void *handle)
{
    struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;

    if (context && context->attributes) {
        STORE_ATTR_INFO *attrs = NULL;

        while (context->attributes
               && context->attributes->code != STORE_ATTR_OR
               && context->attributes->code != STORE_ATTR_END) {
            switch (context->attributes->code) {
            case STORE_ATTR_FRIENDLYNAME:
            case STORE_ATTR_EMAIL:
            case STORE_ATTR_FILENAME:
                if (!attrs)
                    attrs = STORE_ATTR_INFO_new();
                if (attrs == NULL) {
                    STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
                             ERR_R_MALLOC_FAILURE);
                    goto err;
                }
                STORE_ATTR_INFO_set_cstr(attrs,
                                         context->attributes->code,
                                         context->attributes->value,
                                         context->attributes->value_size);
                break;
            case STORE_ATTR_KEYID:
            case STORE_ATTR_ISSUERKEYID:
            case STORE_ATTR_SUBJECTKEYID:
            case STORE_ATTR_ISSUERSERIALHASH:
            case STORE_ATTR_CERTHASH:
                if (!attrs)
                    attrs = STORE_ATTR_INFO_new();
                if (attrs == NULL) {
                    STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
                             ERR_R_MALLOC_FAILURE);
                    goto err;
                }
                STORE_ATTR_INFO_set_sha1str(attrs,
                                            context->attributes->code,
                                            context->attributes->value,
                                            context->attributes->value_size);
                break;
            case STORE_ATTR_ISSUER:
            case STORE_ATTR_SUBJECT:
                if (!attrs)
                    attrs = STORE_ATTR_INFO_new();
                if (attrs == NULL) {
                    STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
                             ERR_R_MALLOC_FAILURE);
                    goto err;
                }
                STORE_ATTR_INFO_modify_dn(attrs,
                                          context->attributes->code,
                                          context->attributes->value);
                break;
            case STORE_ATTR_SERIAL:
                if (!attrs)
                    attrs = STORE_ATTR_INFO_new();
                if (attrs == NULL) {
                    STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
                             ERR_R_MALLOC_FAILURE);
                    goto err;
                }
                STORE_ATTR_INFO_modify_number(attrs,
                                              context->attributes->code,
                                              context->attributes->value);
                break;
            }
            context->attributes++;
        }
        if (context->attributes->code == STORE_ATTR_OR)
            context->attributes++;
        return attrs;
 err:
        while (context->attributes
               && context->attributes->code != STORE_ATTR_OR
               && context->attributes->code != STORE_ATTR_END)
            context->attributes++;
        if (context->attributes->code == STORE_ATTR_OR)
            context->attributes++;
        return NULL;
    }
    STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT, ERR_R_PASSED_NULL_PARAMETER);
    return NULL;
}

int STORE_parse_attrs_end(void *handle)
{
    struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;

    if (context && context->attributes) {
#if 0
        OPENSSL_ITEM *attributes = context->attributes;
#endif
        OPENSSL_free(context);
        return 1;
    }
    STOREerr(STORE_F_STORE_PARSE_ATTRS_END, ERR_R_PASSED_NULL_PARAMETER);
    return 0;
}

int STORE_parse_attrs_endp(void *handle)
{
    struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;

    if (context && context->attributes) {
        return context->attributes->code == STORE_ATTR_END;
    }
    STOREerr(STORE_F_STORE_PARSE_ATTRS_ENDP, ERR_R_PASSED_NULL_PARAMETER);
    return 0;
}

static int attr_info_compare_compute_range(const unsigned char *abits,
                                           const unsigned char *bbits,
                                           unsigned int *alowp,
                                           unsigned int *ahighp,
                                           unsigned int *blowp,
                                           unsigned int *bhighp)
{
    unsigned int alow = (unsigned int)-1, ahigh = 0;
    unsigned int blow = (unsigned int)-1, bhigh = 0;
    int i, res = 0;

    for (i = 0; i < (STORE_ATTR_TYPE_NUM + 8) / 8; i++, abits++, bbits++) {
        if (res == 0) {
            if (*abits < *bbits)
                res = -1;
            if (*abits > *bbits)
                res = 1;
        }
        if (*abits) {
            if (alow == (unsigned int)-1) {
                alow = i * 8;
                if (!(*abits & 0x01))
                    alow++;
                if (!(*abits & 0x02))
                    alow++;
                if (!(*abits & 0x04))
                    alow++;
                if (!(*abits & 0x08))
                    alow++;
                if (!(*abits & 0x10))
                    alow++;
                if (!(*abits & 0x20))
                    alow++;
                if (!(*abits & 0x40))
                    alow++;
            }
            ahigh = i * 8 + 7;
            if (!(*abits & 0x80))
                ahigh++;
            if (!(*abits & 0x40))
                ahigh++;
            if (!(*abits & 0x20))
                ahigh++;
            if (!(*abits & 0x10))
                ahigh++;
            if (!(*abits & 0x08))
                ahigh++;
            if (!(*abits & 0x04))
                ahigh++;
            if (!(*abits & 0x02))
                ahigh++;
        }
        if (*bbits) {
            if (blow == (unsigned int)-1) {
                blow = i * 8;
                if (!(*bbits & 0x01))
                    blow++;
                if (!(*bbits & 0x02))
                    blow++;
                if (!(*bbits & 0x04))
                    blow++;
                if (!(*bbits & 0x08))
                    blow++;
                if (!(*bbits & 0x10))
                    blow++;
                if (!(*bbits & 0x20))
                    blow++;
                if (!(*bbits & 0x40))
                    blow++;
            }
            bhigh = i * 8 + 7;
            if (!(*bbits & 0x80))
                bhigh++;
            if (!(*bbits & 0x40))
                bhigh++;
            if (!(*bbits & 0x20))
                bhigh++;
            if (!(*bbits & 0x10))
                bhigh++;
            if (!(*bbits & 0x08))
                bhigh++;
            if (!(*bbits & 0x04))
                bhigh++;
            if (!(*bbits & 0x02))
                bhigh++;
        }
    }
    if (ahigh + alow < bhigh + blow)
        res = -1;
    if (ahigh + alow > bhigh + blow)
        res = 1;
    if (alowp)
        *alowp = alow;
    if (ahighp)
        *ahighp = ahigh;
    if (blowp)
        *blowp = blow;
    if (bhighp)
        *bhighp = bhigh;
    return res;
}

int STORE_ATTR_INFO_compare(const STORE_ATTR_INFO *const *a,
                            const STORE_ATTR_INFO *const *b)
{
    if (a == b)
        return 0;
    if (!a)
        return -1;
    if (!b)
        return 1;
    return attr_info_compare_compute_range((*a)->set, (*b)->set, 0, 0, 0, 0);
}

int STORE_ATTR_INFO_in_range(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
{
    unsigned int alow, ahigh, blow, bhigh;

    if (a == b)
        return 1;
    if (!a)
        return 0;
    if (!b)
        return 0;
    attr_info_compare_compute_range(a->set, b->set,
                                    &alow, &ahigh, &blow, &bhigh);
    if (alow >= blow && ahigh <= bhigh)
        return 1;
    return 0;
}

int STORE_ATTR_INFO_in(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
{
    unsigned char *abits, *bbits;
    int i;

    if (a == b)
        return 1;
    if (!a)
        return 0;
    if (!b)
        return 0;
    abits = a->set;
    bbits = b->set;
    for (i = 0; i < (STORE_ATTR_TYPE_NUM + 8) / 8; i++, abits++, bbits++) {
        if (*abits && (*bbits & *abits) != *abits)
            return 0;
    }
    return 1;
}

int STORE_ATTR_INFO_in_ex(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
{
    STORE_ATTR_TYPES i;

    if (a == b)
        return 1;
    if (!STORE_ATTR_INFO_in(a, b))
        return 0;
    for (i = 1; i < STORE_ATTR_TYPE_NUM; i++)
        if (ATTR_IS_SET(a, i)) {
            switch (i) {
            case STORE_ATTR_FRIENDLYNAME:
            case STORE_ATTR_EMAIL:
            case STORE_ATTR_FILENAME:
                if (strcmp(a->values[i].cstring, b->values[i].cstring))
                    return 0;
                break;
            case STORE_ATTR_KEYID:
            case STORE_ATTR_ISSUERKEYID:
            case STORE_ATTR_SUBJECTKEYID:
            case STORE_ATTR_ISSUERSERIALHASH:
            case STORE_ATTR_CERTHASH:
                if (memcmp(a->values[i].sha1string,
                           b->values[i].sha1string, a->value_sizes[i]))
                    return 0;
                break;
            case STORE_ATTR_ISSUER:
            case STORE_ATTR_SUBJECT:
                if (X509_NAME_cmp(a->values[i].dn, b->values[i].dn))
                    return 0;
                break;
            case STORE_ATTR_SERIAL:
                if (BN_cmp(a->values[i].number, b->values[i].number))
                    return 0;
                break;
            default:
                break;
            }
        }

    return 1;
}
