1160814Ssimon/* crypto/store/str_lib.c -*- mode:C; c-file-style: "eay" -*- */
2296341Sdelphij/*
3296341Sdelphij * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
4296341Sdelphij * 2003.
5160814Ssimon */
6160814Ssimon/* ====================================================================
7160814Ssimon * Copyright (c) 2003 The OpenSSL Project.  All rights reserved.
8160814Ssimon *
9160814Ssimon * Redistribution and use in source and binary forms, with or without
10160814Ssimon * modification, are permitted provided that the following conditions
11160814Ssimon * are met:
12160814Ssimon *
13160814Ssimon * 1. Redistributions of source code must retain the above copyright
14296341Sdelphij *    notice, this list of conditions and the following disclaimer.
15160814Ssimon *
16160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
17160814Ssimon *    notice, this list of conditions and the following disclaimer in
18160814Ssimon *    the documentation and/or other materials provided with the
19160814Ssimon *    distribution.
20160814Ssimon *
21160814Ssimon * 3. All advertising materials mentioning features or use of this
22160814Ssimon *    software must display the following acknowledgment:
23160814Ssimon *    "This product includes software developed by the OpenSSL Project
24160814Ssimon *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25160814Ssimon *
26160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27160814Ssimon *    endorse or promote products derived from this software without
28160814Ssimon *    prior written permission. For written permission, please contact
29160814Ssimon *    openssl-core@openssl.org.
30160814Ssimon *
31160814Ssimon * 5. Products derived from this software may not be called "OpenSSL"
32160814Ssimon *    nor may "OpenSSL" appear in their names without prior written
33160814Ssimon *    permission of the OpenSSL Project.
34160814Ssimon *
35160814Ssimon * 6. Redistributions of any form whatsoever must retain the following
36160814Ssimon *    acknowledgment:
37160814Ssimon *    "This product includes software developed by the OpenSSL Project
38160814Ssimon *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39160814Ssimon *
40160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43160814Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
52160814Ssimon * ====================================================================
53160814Ssimon *
54160814Ssimon * This product includes cryptographic software written by Eric Young
55160814Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
56160814Ssimon * Hudson (tjh@cryptsoft.com).
57160814Ssimon *
58160814Ssimon */
59160814Ssimon
60160814Ssimon#include <string.h>
61160814Ssimon#include <openssl/bn.h>
62160814Ssimon#include <openssl/err.h>
63160814Ssimon#ifndef OPENSSL_NO_ENGINE
64296341Sdelphij# include <openssl/engine.h>
65160814Ssimon#endif
66160814Ssimon#include <openssl/sha.h>
67160814Ssimon#include <openssl/x509.h>
68160814Ssimon#include "str_locl.h"
69160814Ssimon
70296341Sdelphijconst char *const STORE_object_type_string[STORE_OBJECT_TYPE_NUM + 1] = {
71296341Sdelphij    0,
72296341Sdelphij    "X.509 Certificate",
73296341Sdelphij    "X.509 CRL",
74296341Sdelphij    "Private Key",
75296341Sdelphij    "Public Key",
76296341Sdelphij    "Number",
77296341Sdelphij    "Arbitrary Data"
78296341Sdelphij};
79160814Ssimon
80296341Sdelphijconst int STORE_param_sizes[STORE_PARAM_TYPE_NUM + 1] = {
81296341Sdelphij    0,
82296341Sdelphij    sizeof(int),                /* EVP_TYPE */
83296341Sdelphij    sizeof(size_t),             /* BITS */
84296341Sdelphij    -1,                         /* KEY_PARAMETERS */
85296341Sdelphij    0                           /* KEY_NO_PARAMETERS */
86296341Sdelphij};
87160814Ssimon
88296341Sdelphijconst int STORE_attr_sizes[STORE_ATTR_TYPE_NUM + 1] = {
89296341Sdelphij    0,
90296341Sdelphij    -1,                         /* FRIENDLYNAME: C string */
91296341Sdelphij    SHA_DIGEST_LENGTH,          /* KEYID: SHA1 digest, 160 bits */
92296341Sdelphij    SHA_DIGEST_LENGTH,          /* ISSUERKEYID: SHA1 digest, 160 bits */
93296341Sdelphij    SHA_DIGEST_LENGTH,          /* SUBJECTKEYID: SHA1 digest, 160 bits */
94296341Sdelphij    SHA_DIGEST_LENGTH,          /* ISSUERSERIALHASH: SHA1 digest, 160 bits */
95296341Sdelphij    sizeof(X509_NAME *),        /* ISSUER: X509_NAME * */
96296341Sdelphij    sizeof(BIGNUM *),           /* SERIAL: BIGNUM * */
97296341Sdelphij    sizeof(X509_NAME *),        /* SUBJECT: X509_NAME * */
98296341Sdelphij    SHA_DIGEST_LENGTH,          /* CERTHASH: SHA1 digest, 160 bits */
99296341Sdelphij    -1,                         /* EMAIL: C string */
100296341Sdelphij    -1,                         /* FILENAME: C string */
101296341Sdelphij};
102160814Ssimon
103160814SsimonSTORE *STORE_new_method(const STORE_METHOD *method)
104296341Sdelphij{
105296341Sdelphij    STORE *ret;
106160814Ssimon
107296341Sdelphij    if (method == NULL) {
108296341Sdelphij        STOREerr(STORE_F_STORE_NEW_METHOD, ERR_R_PASSED_NULL_PARAMETER);
109296341Sdelphij        return NULL;
110296341Sdelphij    }
111160814Ssimon
112296341Sdelphij    ret = (STORE *)OPENSSL_malloc(sizeof(STORE));
113296341Sdelphij    if (ret == NULL) {
114296341Sdelphij        STOREerr(STORE_F_STORE_NEW_METHOD, ERR_R_MALLOC_FAILURE);
115296341Sdelphij        return NULL;
116296341Sdelphij    }
117160814Ssimon
118296341Sdelphij    ret->meth = method;
119160814Ssimon
120296341Sdelphij    CRYPTO_new_ex_data(CRYPTO_EX_INDEX_STORE, ret, &ret->ex_data);
121296341Sdelphij    if (ret->meth->init && !ret->meth->init(ret)) {
122296341Sdelphij        STORE_free(ret);
123296341Sdelphij        ret = NULL;
124296341Sdelphij    }
125296341Sdelphij    return ret;
126296341Sdelphij}
127160814Ssimon
128160814SsimonSTORE *STORE_new_engine(ENGINE *engine)
129296341Sdelphij{
130296341Sdelphij    STORE *ret = NULL;
131296341Sdelphij    ENGINE *e = engine;
132296341Sdelphij    const STORE_METHOD *meth = 0;
133160814Ssimon
134160814Ssimon#ifdef OPENSSL_NO_ENGINE
135296341Sdelphij    e = NULL;
136160814Ssimon#else
137296341Sdelphij    if (engine) {
138296341Sdelphij        if (!ENGINE_init(engine)) {
139296341Sdelphij            STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_ENGINE_LIB);
140296341Sdelphij            return NULL;
141296341Sdelphij        }
142296341Sdelphij        e = engine;
143296341Sdelphij    } else {
144296341Sdelphij        STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
145296341Sdelphij        return NULL;
146296341Sdelphij    }
147296341Sdelphij    if (e) {
148296341Sdelphij        meth = ENGINE_get_STORE(e);
149296341Sdelphij        if (!meth) {
150296341Sdelphij            STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_ENGINE_LIB);
151296341Sdelphij            ENGINE_finish(e);
152296341Sdelphij            return NULL;
153296341Sdelphij        }
154296341Sdelphij    }
155160814Ssimon#endif
156160814Ssimon
157296341Sdelphij    ret = STORE_new_method(meth);
158296341Sdelphij    if (ret == NULL) {
159296341Sdelphij        STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_STORE_LIB);
160296341Sdelphij        return NULL;
161296341Sdelphij    }
162160814Ssimon
163296341Sdelphij    ret->engine = e;
164160814Ssimon
165296341Sdelphij    return (ret);
166296341Sdelphij}
167160814Ssimon
168160814Ssimonvoid STORE_free(STORE *store)
169296341Sdelphij{
170296341Sdelphij    if (store == NULL)
171296341Sdelphij        return;
172296341Sdelphij    if (store->meth->clean)
173296341Sdelphij        store->meth->clean(store);
174296341Sdelphij    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_STORE, store, &store->ex_data);
175296341Sdelphij    OPENSSL_free(store);
176296341Sdelphij}
177160814Ssimon
178296341Sdelphijint STORE_ctrl(STORE *store, int cmd, long i, void *p, void (*f) (void))
179296341Sdelphij{
180296341Sdelphij    if (store == NULL) {
181296341Sdelphij        STOREerr(STORE_F_STORE_CTRL, ERR_R_PASSED_NULL_PARAMETER);
182296341Sdelphij        return 0;
183296341Sdelphij    }
184296341Sdelphij    if (store->meth->ctrl)
185296341Sdelphij        return store->meth->ctrl(store, cmd, i, p, f);
186296341Sdelphij    STOREerr(STORE_F_STORE_CTRL, STORE_R_NO_CONTROL_FUNCTION);
187296341Sdelphij    return 0;
188296341Sdelphij}
189160814Ssimon
190160814Ssimonint STORE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
191296341Sdelphij                           CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
192296341Sdelphij{
193296341Sdelphij    return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_STORE, argl, argp,
194296341Sdelphij                                   new_func, dup_func, free_func);
195296341Sdelphij}
196160814Ssimon
197160814Ssimonint STORE_set_ex_data(STORE *r, int idx, void *arg)
198296341Sdelphij{
199296341Sdelphij    return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
200296341Sdelphij}
201160814Ssimon
202160814Ssimonvoid *STORE_get_ex_data(STORE *r, int idx)
203296341Sdelphij{
204296341Sdelphij    return (CRYPTO_get_ex_data(&r->ex_data, idx));
205296341Sdelphij}
206160814Ssimon
207160814Ssimonconst STORE_METHOD *STORE_get_method(STORE *store)
208296341Sdelphij{
209296341Sdelphij    return store->meth;
210296341Sdelphij}
211160814Ssimon
212160814Ssimonconst STORE_METHOD *STORE_set_method(STORE *store, const STORE_METHOD *meth)
213296341Sdelphij{
214296341Sdelphij    store->meth = meth;
215296341Sdelphij    return store->meth;
216296341Sdelphij}
217160814Ssimon
218160814Ssimon/* API helpers */
219160814Ssimon
220160814Ssimon#define check_store(s,fncode,fnname,fnerrcode) \
221296341Sdelphij        do \
222296341Sdelphij                { \
223296341Sdelphij                if ((s) == NULL || (s)->meth == NULL) \
224296341Sdelphij                        { \
225296341Sdelphij                        STOREerr((fncode), ERR_R_PASSED_NULL_PARAMETER); \
226296341Sdelphij                        return 0; \
227296341Sdelphij                        } \
228296341Sdelphij                if ((s)->meth->fnname == NULL) \
229296341Sdelphij                        { \
230296341Sdelphij                        STOREerr((fncode), (fnerrcode)); \
231296341Sdelphij                        return 0; \
232296341Sdelphij                        } \
233296341Sdelphij                } \
234296341Sdelphij        while(0)
235160814Ssimon
236160814Ssimon/* API functions */
237160814Ssimon
238160814SsimonX509 *STORE_get_certificate(STORE *s, OPENSSL_ITEM attributes[],
239296341Sdelphij                            OPENSSL_ITEM parameters[])
240296341Sdelphij{
241296341Sdelphij    STORE_OBJECT *object;
242296341Sdelphij    X509 *x;
243160814Ssimon
244296341Sdelphij    check_store(s, STORE_F_STORE_GET_CERTIFICATE,
245296341Sdelphij                get_object, STORE_R_NO_GET_OBJECT_FUNCTION);
246160814Ssimon
247296341Sdelphij    object = s->meth->get_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
248296341Sdelphij                                 attributes, parameters);
249296341Sdelphij    if (!object || !object->data.x509.certificate) {
250296341Sdelphij        STOREerr(STORE_F_STORE_GET_CERTIFICATE,
251296341Sdelphij                 STORE_R_FAILED_GETTING_CERTIFICATE);
252296341Sdelphij        return 0;
253296341Sdelphij    }
254296341Sdelphij    CRYPTO_add(&object->data.x509.certificate->references, 1,
255296341Sdelphij               CRYPTO_LOCK_X509);
256160814Ssimon#ifdef REF_PRINT
257296341Sdelphij    REF_PRINT("X509", data);
258160814Ssimon#endif
259296341Sdelphij    x = object->data.x509.certificate;
260296341Sdelphij    STORE_OBJECT_free(object);
261296341Sdelphij    return x;
262296341Sdelphij}
263160814Ssimon
264160814Ssimonint STORE_store_certificate(STORE *s, X509 *data, OPENSSL_ITEM attributes[],
265296341Sdelphij                            OPENSSL_ITEM parameters[])
266296341Sdelphij{
267296341Sdelphij    STORE_OBJECT *object;
268296341Sdelphij    int i;
269160814Ssimon
270296341Sdelphij    check_store(s, STORE_F_STORE_CERTIFICATE,
271296341Sdelphij                store_object, STORE_R_NO_STORE_OBJECT_FUNCTION);
272160814Ssimon
273296341Sdelphij    object = STORE_OBJECT_new();
274296341Sdelphij    if (!object) {
275296341Sdelphij        STOREerr(STORE_F_STORE_STORE_CERTIFICATE, ERR_R_MALLOC_FAILURE);
276296341Sdelphij        return 0;
277296341Sdelphij    }
278296341Sdelphij
279296341Sdelphij    CRYPTO_add(&data->references, 1, CRYPTO_LOCK_X509);
280160814Ssimon#ifdef REF_PRINT
281296341Sdelphij    REF_PRINT("X509", data);
282160814Ssimon#endif
283296341Sdelphij    object->data.x509.certificate = data;
284160814Ssimon
285296341Sdelphij    i = s->meth->store_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
286296341Sdelphij                              object, attributes, parameters);
287160814Ssimon
288296341Sdelphij    STORE_OBJECT_free(object);
289160814Ssimon
290296341Sdelphij    if (!i) {
291296341Sdelphij        STOREerr(STORE_F_STORE_STORE_CERTIFICATE,
292296341Sdelphij                 STORE_R_FAILED_STORING_CERTIFICATE);
293296341Sdelphij        return 0;
294296341Sdelphij    }
295296341Sdelphij    return 1;
296296341Sdelphij}
297160814Ssimon
298160814Ssimonint STORE_modify_certificate(STORE *s, OPENSSL_ITEM search_attributes[],
299296341Sdelphij                             OPENSSL_ITEM add_attributes[],
300296341Sdelphij                             OPENSSL_ITEM modify_attributes[],
301296341Sdelphij                             OPENSSL_ITEM delete_attributes[],
302296341Sdelphij                             OPENSSL_ITEM parameters[])
303296341Sdelphij{
304296341Sdelphij    check_store(s, STORE_F_STORE_MODIFY_CERTIFICATE,
305296341Sdelphij                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
306160814Ssimon
307296341Sdelphij    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
308296341Sdelphij                                search_attributes, add_attributes,
309296341Sdelphij                                modify_attributes, delete_attributes,
310296341Sdelphij                                parameters)) {
311296341Sdelphij        STOREerr(STORE_F_STORE_MODIFY_CERTIFICATE,
312296341Sdelphij                 STORE_R_FAILED_MODIFYING_CERTIFICATE);
313296341Sdelphij        return 0;
314296341Sdelphij    }
315296341Sdelphij    return 1;
316296341Sdelphij}
317160814Ssimon
318160814Ssimonint STORE_revoke_certificate(STORE *s, OPENSSL_ITEM attributes[],
319296341Sdelphij                             OPENSSL_ITEM parameters[])
320296341Sdelphij{
321296341Sdelphij    check_store(s, STORE_F_STORE_REVOKE_CERTIFICATE,
322296341Sdelphij                revoke_object, STORE_R_NO_REVOKE_OBJECT_FUNCTION);
323160814Ssimon
324296341Sdelphij    if (!s->meth->revoke_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
325296341Sdelphij                                attributes, parameters)) {
326296341Sdelphij        STOREerr(STORE_F_STORE_REVOKE_CERTIFICATE,
327296341Sdelphij                 STORE_R_FAILED_REVOKING_CERTIFICATE);
328296341Sdelphij        return 0;
329296341Sdelphij    }
330296341Sdelphij    return 1;
331296341Sdelphij}
332160814Ssimon
333160814Ssimonint STORE_delete_certificate(STORE *s, OPENSSL_ITEM attributes[],
334296341Sdelphij                             OPENSSL_ITEM parameters[])
335296341Sdelphij{
336296341Sdelphij    check_store(s, STORE_F_STORE_DELETE_CERTIFICATE,
337296341Sdelphij                delete_object, STORE_R_NO_DELETE_OBJECT_FUNCTION);
338160814Ssimon
339296341Sdelphij    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
340296341Sdelphij                                attributes, parameters)) {
341296341Sdelphij        STOREerr(STORE_F_STORE_DELETE_CERTIFICATE,
342296341Sdelphij                 STORE_R_FAILED_DELETING_CERTIFICATE);
343296341Sdelphij        return 0;
344296341Sdelphij    }
345296341Sdelphij    return 1;
346296341Sdelphij}
347160814Ssimon
348160814Ssimonvoid *STORE_list_certificate_start(STORE *s, OPENSSL_ITEM attributes[],
349296341Sdelphij                                   OPENSSL_ITEM parameters[])
350296341Sdelphij{
351296341Sdelphij    void *handle;
352160814Ssimon
353296341Sdelphij    check_store(s, STORE_F_STORE_LIST_CERTIFICATE_START,
354296341Sdelphij                list_object_start, STORE_R_NO_LIST_OBJECT_START_FUNCTION);
355160814Ssimon
356296341Sdelphij    handle = s->meth->list_object_start(s,
357296341Sdelphij                                        STORE_OBJECT_TYPE_X509_CERTIFICATE,
358296341Sdelphij                                        attributes, parameters);
359296341Sdelphij    if (!handle) {
360296341Sdelphij        STOREerr(STORE_F_STORE_LIST_CERTIFICATE_START,
361296341Sdelphij                 STORE_R_FAILED_LISTING_CERTIFICATES);
362296341Sdelphij        return 0;
363296341Sdelphij    }
364296341Sdelphij    return handle;
365296341Sdelphij}
366160814Ssimon
367160814SsimonX509 *STORE_list_certificate_next(STORE *s, void *handle)
368296341Sdelphij{
369296341Sdelphij    STORE_OBJECT *object;
370296341Sdelphij    X509 *x;
371160814Ssimon
372296341Sdelphij    check_store(s, STORE_F_STORE_LIST_CERTIFICATE_NEXT,
373296341Sdelphij                list_object_next, STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
374160814Ssimon
375296341Sdelphij    object = s->meth->list_object_next(s, handle);
376296341Sdelphij    if (!object || !object->data.x509.certificate) {
377296341Sdelphij        STOREerr(STORE_F_STORE_LIST_CERTIFICATE_NEXT,
378296341Sdelphij                 STORE_R_FAILED_LISTING_CERTIFICATES);
379296341Sdelphij        return 0;
380296341Sdelphij    }
381296341Sdelphij    CRYPTO_add(&object->data.x509.certificate->references, 1,
382296341Sdelphij               CRYPTO_LOCK_X509);
383160814Ssimon#ifdef REF_PRINT
384296341Sdelphij    REF_PRINT("X509", data);
385160814Ssimon#endif
386296341Sdelphij    x = object->data.x509.certificate;
387296341Sdelphij    STORE_OBJECT_free(object);
388296341Sdelphij    return x;
389296341Sdelphij}
390160814Ssimon
391160814Ssimonint STORE_list_certificate_end(STORE *s, void *handle)
392296341Sdelphij{
393296341Sdelphij    check_store(s, STORE_F_STORE_LIST_CERTIFICATE_END,
394296341Sdelphij                list_object_end, STORE_R_NO_LIST_OBJECT_END_FUNCTION);
395160814Ssimon
396296341Sdelphij    if (!s->meth->list_object_end(s, handle)) {
397296341Sdelphij        STOREerr(STORE_F_STORE_LIST_CERTIFICATE_END,
398296341Sdelphij                 STORE_R_FAILED_LISTING_CERTIFICATES);
399296341Sdelphij        return 0;
400296341Sdelphij    }
401296341Sdelphij    return 1;
402296341Sdelphij}
403160814Ssimon
404160814Ssimonint STORE_list_certificate_endp(STORE *s, void *handle)
405296341Sdelphij{
406296341Sdelphij    check_store(s, STORE_F_STORE_LIST_CERTIFICATE_ENDP,
407296341Sdelphij                list_object_endp, STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
408160814Ssimon
409296341Sdelphij    if (!s->meth->list_object_endp(s, handle)) {
410296341Sdelphij        STOREerr(STORE_F_STORE_LIST_CERTIFICATE_ENDP,
411296341Sdelphij                 STORE_R_FAILED_LISTING_CERTIFICATES);
412296341Sdelphij        return 0;
413296341Sdelphij    }
414296341Sdelphij    return 1;
415296341Sdelphij}
416160814Ssimon
417160814SsimonEVP_PKEY *STORE_generate_key(STORE *s, OPENSSL_ITEM attributes[],
418296341Sdelphij                             OPENSSL_ITEM parameters[])
419296341Sdelphij{
420296341Sdelphij    STORE_OBJECT *object;
421296341Sdelphij    EVP_PKEY *pkey;
422160814Ssimon
423296341Sdelphij    check_store(s, STORE_F_STORE_GENERATE_KEY,
424296341Sdelphij                generate_object, STORE_R_NO_GENERATE_OBJECT_FUNCTION);
425160814Ssimon
426296341Sdelphij    object = s->meth->generate_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
427296341Sdelphij                                      attributes, parameters);
428296341Sdelphij    if (!object || !object->data.key) {
429296341Sdelphij        STOREerr(STORE_F_STORE_GENERATE_KEY, STORE_R_FAILED_GENERATING_KEY);
430296341Sdelphij        return 0;
431296341Sdelphij    }
432296341Sdelphij    CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
433160814Ssimon#ifdef REF_PRINT
434296341Sdelphij    REF_PRINT("EVP_PKEY", data);
435160814Ssimon#endif
436296341Sdelphij    pkey = object->data.key;
437296341Sdelphij    STORE_OBJECT_free(object);
438296341Sdelphij    return pkey;
439296341Sdelphij}
440160814Ssimon
441160814SsimonEVP_PKEY *STORE_get_private_key(STORE *s, OPENSSL_ITEM attributes[],
442296341Sdelphij                                OPENSSL_ITEM parameters[])
443296341Sdelphij{
444296341Sdelphij    STORE_OBJECT *object;
445296341Sdelphij    EVP_PKEY *pkey;
446160814Ssimon
447296341Sdelphij    check_store(s, STORE_F_STORE_GET_PRIVATE_KEY,
448296341Sdelphij                get_object, STORE_R_NO_GET_OBJECT_FUNCTION);
449160814Ssimon
450296341Sdelphij    object = s->meth->get_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
451296341Sdelphij                                 attributes, parameters);
452296341Sdelphij    if (!object || !object->data.key || !object->data.key) {
453296341Sdelphij        STOREerr(STORE_F_STORE_GET_PRIVATE_KEY, STORE_R_FAILED_GETTING_KEY);
454296341Sdelphij        return 0;
455296341Sdelphij    }
456296341Sdelphij    CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
457160814Ssimon#ifdef REF_PRINT
458296341Sdelphij    REF_PRINT("EVP_PKEY", data);
459160814Ssimon#endif
460296341Sdelphij    pkey = object->data.key;
461296341Sdelphij    STORE_OBJECT_free(object);
462296341Sdelphij    return pkey;
463296341Sdelphij}
464160814Ssimon
465296341Sdelphijint STORE_store_private_key(STORE *s, EVP_PKEY *data,
466296341Sdelphij                            OPENSSL_ITEM attributes[],
467296341Sdelphij                            OPENSSL_ITEM parameters[])
468296341Sdelphij{
469296341Sdelphij    STORE_OBJECT *object;
470296341Sdelphij    int i;
471160814Ssimon
472296341Sdelphij    check_store(s, STORE_F_STORE_STORE_PRIVATE_KEY,
473296341Sdelphij                store_object, STORE_R_NO_STORE_OBJECT_FUNCTION);
474160814Ssimon
475296341Sdelphij    object = STORE_OBJECT_new();
476296341Sdelphij    if (!object) {
477296341Sdelphij        STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY, ERR_R_MALLOC_FAILURE);
478296341Sdelphij        return 0;
479296341Sdelphij    }
480296341Sdelphij    object->data.key = EVP_PKEY_new();
481296341Sdelphij    if (!object->data.key) {
482296341Sdelphij        STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY, ERR_R_MALLOC_FAILURE);
483296341Sdelphij        return 0;
484296341Sdelphij    }
485296341Sdelphij
486296341Sdelphij    CRYPTO_add(&data->references, 1, CRYPTO_LOCK_EVP_PKEY);
487160814Ssimon#ifdef REF_PRINT
488296341Sdelphij    REF_PRINT("EVP_PKEY", data);
489160814Ssimon#endif
490296341Sdelphij    object->data.key = data;
491160814Ssimon
492296341Sdelphij    i = s->meth->store_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY, object,
493296341Sdelphij                              attributes, parameters);
494160814Ssimon
495296341Sdelphij    STORE_OBJECT_free(object);
496160814Ssimon
497296341Sdelphij    if (!i) {
498296341Sdelphij        STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY, STORE_R_FAILED_STORING_KEY);
499296341Sdelphij        return 0;
500296341Sdelphij    }
501296341Sdelphij    return i;
502296341Sdelphij}
503160814Ssimon
504160814Ssimonint STORE_modify_private_key(STORE *s, OPENSSL_ITEM search_attributes[],
505296341Sdelphij                             OPENSSL_ITEM add_attributes[],
506296341Sdelphij                             OPENSSL_ITEM modify_attributes[],
507296341Sdelphij                             OPENSSL_ITEM delete_attributes[],
508296341Sdelphij                             OPENSSL_ITEM parameters[])
509296341Sdelphij{
510296341Sdelphij    check_store(s, STORE_F_STORE_MODIFY_PRIVATE_KEY,
511296341Sdelphij                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
512160814Ssimon
513296341Sdelphij    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
514296341Sdelphij                                search_attributes, add_attributes,
515296341Sdelphij                                modify_attributes, delete_attributes,
516296341Sdelphij                                parameters)) {
517296341Sdelphij        STOREerr(STORE_F_STORE_MODIFY_PRIVATE_KEY,
518296341Sdelphij                 STORE_R_FAILED_MODIFYING_PRIVATE_KEY);
519296341Sdelphij        return 0;
520296341Sdelphij    }
521296341Sdelphij    return 1;
522296341Sdelphij}
523160814Ssimon
524160814Ssimonint STORE_revoke_private_key(STORE *s, OPENSSL_ITEM attributes[],
525296341Sdelphij                             OPENSSL_ITEM parameters[])
526296341Sdelphij{
527296341Sdelphij    int i;
528160814Ssimon
529296341Sdelphij    check_store(s, STORE_F_STORE_REVOKE_PRIVATE_KEY,
530296341Sdelphij                revoke_object, STORE_R_NO_REVOKE_OBJECT_FUNCTION);
531160814Ssimon
532296341Sdelphij    i = s->meth->revoke_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
533296341Sdelphij                               attributes, parameters);
534160814Ssimon
535296341Sdelphij    if (!i) {
536296341Sdelphij        STOREerr(STORE_F_STORE_REVOKE_PRIVATE_KEY,
537296341Sdelphij                 STORE_R_FAILED_REVOKING_KEY);
538296341Sdelphij        return 0;
539296341Sdelphij    }
540296341Sdelphij    return i;
541296341Sdelphij}
542160814Ssimon
543160814Ssimonint STORE_delete_private_key(STORE *s, OPENSSL_ITEM attributes[],
544296341Sdelphij                             OPENSSL_ITEM parameters[])
545296341Sdelphij{
546296341Sdelphij    check_store(s, STORE_F_STORE_DELETE_PRIVATE_KEY,
547296341Sdelphij                delete_object, STORE_R_NO_DELETE_OBJECT_FUNCTION);
548160814Ssimon
549296341Sdelphij    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
550296341Sdelphij                                attributes, parameters)) {
551296341Sdelphij        STOREerr(STORE_F_STORE_DELETE_PRIVATE_KEY,
552296341Sdelphij                 STORE_R_FAILED_DELETING_KEY);
553296341Sdelphij        return 0;
554296341Sdelphij    }
555296341Sdelphij    return 1;
556296341Sdelphij}
557296341Sdelphij
558160814Ssimonvoid *STORE_list_private_key_start(STORE *s, OPENSSL_ITEM attributes[],
559296341Sdelphij                                   OPENSSL_ITEM parameters[])
560296341Sdelphij{
561296341Sdelphij    void *handle;
562160814Ssimon
563296341Sdelphij    check_store(s, STORE_F_STORE_LIST_PRIVATE_KEY_START,
564296341Sdelphij                list_object_start, STORE_R_NO_LIST_OBJECT_START_FUNCTION);
565160814Ssimon
566296341Sdelphij    handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
567296341Sdelphij                                        attributes, parameters);
568296341Sdelphij    if (!handle) {
569296341Sdelphij        STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_START,
570296341Sdelphij                 STORE_R_FAILED_LISTING_KEYS);
571296341Sdelphij        return 0;
572296341Sdelphij    }
573296341Sdelphij    return handle;
574296341Sdelphij}
575160814Ssimon
576160814SsimonEVP_PKEY *STORE_list_private_key_next(STORE *s, void *handle)
577296341Sdelphij{
578296341Sdelphij    STORE_OBJECT *object;
579296341Sdelphij    EVP_PKEY *pkey;
580160814Ssimon
581296341Sdelphij    check_store(s, STORE_F_STORE_LIST_PRIVATE_KEY_NEXT,
582296341Sdelphij                list_object_next, STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
583160814Ssimon
584296341Sdelphij    object = s->meth->list_object_next(s, handle);
585296341Sdelphij    if (!object || !object->data.key || !object->data.key) {
586296341Sdelphij        STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_NEXT,
587296341Sdelphij                 STORE_R_FAILED_LISTING_KEYS);
588296341Sdelphij        return 0;
589296341Sdelphij    }
590296341Sdelphij    CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
591160814Ssimon#ifdef REF_PRINT
592296341Sdelphij    REF_PRINT("EVP_PKEY", data);
593160814Ssimon#endif
594296341Sdelphij    pkey = object->data.key;
595296341Sdelphij    STORE_OBJECT_free(object);
596296341Sdelphij    return pkey;
597296341Sdelphij}
598160814Ssimon
599160814Ssimonint STORE_list_private_key_end(STORE *s, void *handle)
600296341Sdelphij{
601296341Sdelphij    check_store(s, STORE_F_STORE_LIST_PRIVATE_KEY_END,
602296341Sdelphij                list_object_end, STORE_R_NO_LIST_OBJECT_END_FUNCTION);
603160814Ssimon
604296341Sdelphij    if (!s->meth->list_object_end(s, handle)) {
605296341Sdelphij        STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_END,
606296341Sdelphij                 STORE_R_FAILED_LISTING_KEYS);
607296341Sdelphij        return 0;
608296341Sdelphij    }
609296341Sdelphij    return 1;
610296341Sdelphij}
611160814Ssimon
612160814Ssimonint STORE_list_private_key_endp(STORE *s, void *handle)
613296341Sdelphij{
614296341Sdelphij    check_store(s, STORE_F_STORE_LIST_PRIVATE_KEY_ENDP,
615296341Sdelphij                list_object_endp, STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
616160814Ssimon
617296341Sdelphij    if (!s->meth->list_object_endp(s, handle)) {
618296341Sdelphij        STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_ENDP,
619296341Sdelphij                 STORE_R_FAILED_LISTING_KEYS);
620296341Sdelphij        return 0;
621296341Sdelphij    }
622296341Sdelphij    return 1;
623296341Sdelphij}
624160814Ssimon
625160814SsimonEVP_PKEY *STORE_get_public_key(STORE *s, OPENSSL_ITEM attributes[],
626296341Sdelphij                               OPENSSL_ITEM parameters[])
627296341Sdelphij{
628296341Sdelphij    STORE_OBJECT *object;
629296341Sdelphij    EVP_PKEY *pkey;
630160814Ssimon
631296341Sdelphij    check_store(s, STORE_F_STORE_GET_PUBLIC_KEY,
632296341Sdelphij                get_object, STORE_R_NO_GET_OBJECT_FUNCTION);
633160814Ssimon
634296341Sdelphij    object = s->meth->get_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
635296341Sdelphij                                 attributes, parameters);
636296341Sdelphij    if (!object || !object->data.key || !object->data.key) {
637296341Sdelphij        STOREerr(STORE_F_STORE_GET_PUBLIC_KEY, STORE_R_FAILED_GETTING_KEY);
638296341Sdelphij        return 0;
639296341Sdelphij    }
640296341Sdelphij    CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
641160814Ssimon#ifdef REF_PRINT
642296341Sdelphij    REF_PRINT("EVP_PKEY", data);
643160814Ssimon#endif
644296341Sdelphij    pkey = object->data.key;
645296341Sdelphij    STORE_OBJECT_free(object);
646296341Sdelphij    return pkey;
647296341Sdelphij}
648160814Ssimon
649296341Sdelphijint STORE_store_public_key(STORE *s, EVP_PKEY *data,
650296341Sdelphij                           OPENSSL_ITEM attributes[],
651296341Sdelphij                           OPENSSL_ITEM parameters[])
652296341Sdelphij{
653296341Sdelphij    STORE_OBJECT *object;
654296341Sdelphij    int i;
655160814Ssimon
656296341Sdelphij    check_store(s, STORE_F_STORE_STORE_PUBLIC_KEY,
657296341Sdelphij                store_object, STORE_R_NO_STORE_OBJECT_FUNCTION);
658160814Ssimon
659296341Sdelphij    object = STORE_OBJECT_new();
660296341Sdelphij    if (!object) {
661296341Sdelphij        STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY, ERR_R_MALLOC_FAILURE);
662296341Sdelphij        return 0;
663296341Sdelphij    }
664296341Sdelphij    object->data.key = EVP_PKEY_new();
665296341Sdelphij    if (!object->data.key) {
666296341Sdelphij        STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY, ERR_R_MALLOC_FAILURE);
667296341Sdelphij        return 0;
668296341Sdelphij    }
669296341Sdelphij
670296341Sdelphij    CRYPTO_add(&data->references, 1, CRYPTO_LOCK_EVP_PKEY);
671160814Ssimon#ifdef REF_PRINT
672296341Sdelphij    REF_PRINT("EVP_PKEY", data);
673160814Ssimon#endif
674296341Sdelphij    object->data.key = data;
675160814Ssimon
676296341Sdelphij    i = s->meth->store_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY, object,
677296341Sdelphij                              attributes, parameters);
678160814Ssimon
679296341Sdelphij    STORE_OBJECT_free(object);
680160814Ssimon
681296341Sdelphij    if (!i) {
682296341Sdelphij        STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY, STORE_R_FAILED_STORING_KEY);
683296341Sdelphij        return 0;
684296341Sdelphij    }
685296341Sdelphij    return i;
686296341Sdelphij}
687160814Ssimon
688160814Ssimonint STORE_modify_public_key(STORE *s, OPENSSL_ITEM search_attributes[],
689296341Sdelphij                            OPENSSL_ITEM add_attributes[],
690296341Sdelphij                            OPENSSL_ITEM modify_attributes[],
691296341Sdelphij                            OPENSSL_ITEM delete_attributes[],
692296341Sdelphij                            OPENSSL_ITEM parameters[])
693296341Sdelphij{
694296341Sdelphij    check_store(s, STORE_F_STORE_MODIFY_PUBLIC_KEY,
695296341Sdelphij                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
696160814Ssimon
697296341Sdelphij    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
698296341Sdelphij                                search_attributes, add_attributes,
699296341Sdelphij                                modify_attributes, delete_attributes,
700296341Sdelphij                                parameters)) {
701296341Sdelphij        STOREerr(STORE_F_STORE_MODIFY_PUBLIC_KEY,
702296341Sdelphij                 STORE_R_FAILED_MODIFYING_PUBLIC_KEY);
703296341Sdelphij        return 0;
704296341Sdelphij    }
705296341Sdelphij    return 1;
706296341Sdelphij}
707160814Ssimon
708160814Ssimonint STORE_revoke_public_key(STORE *s, OPENSSL_ITEM attributes[],
709296341Sdelphij                            OPENSSL_ITEM parameters[])
710296341Sdelphij{
711296341Sdelphij    int i;
712160814Ssimon
713296341Sdelphij    check_store(s, STORE_F_STORE_REVOKE_PUBLIC_KEY,
714296341Sdelphij                revoke_object, STORE_R_NO_REVOKE_OBJECT_FUNCTION);
715160814Ssimon
716296341Sdelphij    i = s->meth->revoke_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
717296341Sdelphij                               attributes, parameters);
718160814Ssimon
719296341Sdelphij    if (!i) {
720296341Sdelphij        STOREerr(STORE_F_STORE_REVOKE_PUBLIC_KEY,
721296341Sdelphij                 STORE_R_FAILED_REVOKING_KEY);
722296341Sdelphij        return 0;
723296341Sdelphij    }
724296341Sdelphij    return i;
725296341Sdelphij}
726160814Ssimon
727160814Ssimonint STORE_delete_public_key(STORE *s, OPENSSL_ITEM attributes[],
728296341Sdelphij                            OPENSSL_ITEM parameters[])
729296341Sdelphij{
730296341Sdelphij    check_store(s, STORE_F_STORE_DELETE_PUBLIC_KEY,
731296341Sdelphij                delete_object, STORE_R_NO_DELETE_OBJECT_FUNCTION);
732160814Ssimon
733296341Sdelphij    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
734296341Sdelphij                                attributes, parameters)) {
735296341Sdelphij        STOREerr(STORE_F_STORE_DELETE_PUBLIC_KEY,
736296341Sdelphij                 STORE_R_FAILED_DELETING_KEY);
737296341Sdelphij        return 0;
738296341Sdelphij    }
739296341Sdelphij    return 1;
740296341Sdelphij}
741296341Sdelphij
742160814Ssimonvoid *STORE_list_public_key_start(STORE *s, OPENSSL_ITEM attributes[],
743296341Sdelphij                                  OPENSSL_ITEM parameters[])
744296341Sdelphij{
745296341Sdelphij    void *handle;
746160814Ssimon
747296341Sdelphij    check_store(s, STORE_F_STORE_LIST_PUBLIC_KEY_START,
748296341Sdelphij                list_object_start, STORE_R_NO_LIST_OBJECT_START_FUNCTION);
749160814Ssimon
750296341Sdelphij    handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
751296341Sdelphij                                        attributes, parameters);
752296341Sdelphij    if (!handle) {
753296341Sdelphij        STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_START,
754296341Sdelphij                 STORE_R_FAILED_LISTING_KEYS);
755296341Sdelphij        return 0;
756296341Sdelphij    }
757296341Sdelphij    return handle;
758296341Sdelphij}
759160814Ssimon
760160814SsimonEVP_PKEY *STORE_list_public_key_next(STORE *s, void *handle)
761296341Sdelphij{
762296341Sdelphij    STORE_OBJECT *object;
763296341Sdelphij    EVP_PKEY *pkey;
764160814Ssimon
765296341Sdelphij    check_store(s, STORE_F_STORE_LIST_PUBLIC_KEY_NEXT,
766296341Sdelphij                list_object_next, STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
767160814Ssimon
768296341Sdelphij    object = s->meth->list_object_next(s, handle);
769296341Sdelphij    if (!object || !object->data.key || !object->data.key) {
770296341Sdelphij        STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_NEXT,
771296341Sdelphij                 STORE_R_FAILED_LISTING_KEYS);
772296341Sdelphij        return 0;
773296341Sdelphij    }
774296341Sdelphij    CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
775160814Ssimon#ifdef REF_PRINT
776296341Sdelphij    REF_PRINT("EVP_PKEY", data);
777160814Ssimon#endif
778296341Sdelphij    pkey = object->data.key;
779296341Sdelphij    STORE_OBJECT_free(object);
780296341Sdelphij    return pkey;
781296341Sdelphij}
782160814Ssimon
783160814Ssimonint STORE_list_public_key_end(STORE *s, void *handle)
784296341Sdelphij{
785296341Sdelphij    check_store(s, STORE_F_STORE_LIST_PUBLIC_KEY_END,
786296341Sdelphij                list_object_end, STORE_R_NO_LIST_OBJECT_END_FUNCTION);
787160814Ssimon
788296341Sdelphij    if (!s->meth->list_object_end(s, handle)) {
789296341Sdelphij        STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_END,
790296341Sdelphij                 STORE_R_FAILED_LISTING_KEYS);
791296341Sdelphij        return 0;
792296341Sdelphij    }
793296341Sdelphij    return 1;
794296341Sdelphij}
795160814Ssimon
796160814Ssimonint STORE_list_public_key_endp(STORE *s, void *handle)
797296341Sdelphij{
798296341Sdelphij    check_store(s, STORE_F_STORE_LIST_PUBLIC_KEY_ENDP,
799296341Sdelphij                list_object_endp, STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
800160814Ssimon
801296341Sdelphij    if (!s->meth->list_object_endp(s, handle)) {
802296341Sdelphij        STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_ENDP,
803296341Sdelphij                 STORE_R_FAILED_LISTING_KEYS);
804296341Sdelphij        return 0;
805296341Sdelphij    }
806296341Sdelphij    return 1;
807296341Sdelphij}
808160814Ssimon
809160814SsimonX509_CRL *STORE_generate_crl(STORE *s, OPENSSL_ITEM attributes[],
810296341Sdelphij                             OPENSSL_ITEM parameters[])
811296341Sdelphij{
812296341Sdelphij    STORE_OBJECT *object;
813296341Sdelphij    X509_CRL *crl;
814160814Ssimon
815296341Sdelphij    check_store(s, STORE_F_STORE_GENERATE_CRL,
816296341Sdelphij                generate_object, STORE_R_NO_GENERATE_CRL_FUNCTION);
817160814Ssimon
818296341Sdelphij    object = s->meth->generate_object(s, STORE_OBJECT_TYPE_X509_CRL,
819296341Sdelphij                                      attributes, parameters);
820296341Sdelphij    if (!object || !object->data.crl) {
821296341Sdelphij        STOREerr(STORE_F_STORE_GENERATE_CRL, STORE_R_FAILED_GENERATING_CRL);
822296341Sdelphij        return 0;
823296341Sdelphij    }
824296341Sdelphij    CRYPTO_add(&object->data.crl->references, 1, CRYPTO_LOCK_X509_CRL);
825160814Ssimon#ifdef REF_PRINT
826296341Sdelphij    REF_PRINT("X509_CRL", data);
827160814Ssimon#endif
828296341Sdelphij    crl = object->data.crl;
829296341Sdelphij    STORE_OBJECT_free(object);
830296341Sdelphij    return crl;
831296341Sdelphij}
832160814Ssimon
833160814SsimonX509_CRL *STORE_get_crl(STORE *s, OPENSSL_ITEM attributes[],
834296341Sdelphij                        OPENSSL_ITEM parameters[])
835296341Sdelphij{
836296341Sdelphij    STORE_OBJECT *object;
837296341Sdelphij    X509_CRL *crl;
838160814Ssimon
839296341Sdelphij    check_store(s, STORE_F_STORE_GET_CRL,
840296341Sdelphij                get_object, STORE_R_NO_GET_OBJECT_FUNCTION);
841160814Ssimon
842296341Sdelphij    object = s->meth->get_object(s, STORE_OBJECT_TYPE_X509_CRL,
843296341Sdelphij                                 attributes, parameters);
844296341Sdelphij    if (!object || !object->data.crl) {
845296341Sdelphij        STOREerr(STORE_F_STORE_GET_CRL, STORE_R_FAILED_GETTING_KEY);
846296341Sdelphij        return 0;
847296341Sdelphij    }
848296341Sdelphij    CRYPTO_add(&object->data.crl->references, 1, CRYPTO_LOCK_X509_CRL);
849160814Ssimon#ifdef REF_PRINT
850296341Sdelphij    REF_PRINT("X509_CRL", data);
851160814Ssimon#endif
852296341Sdelphij    crl = object->data.crl;
853296341Sdelphij    STORE_OBJECT_free(object);
854296341Sdelphij    return crl;
855296341Sdelphij}
856160814Ssimon
857160814Ssimonint STORE_store_crl(STORE *s, X509_CRL *data, OPENSSL_ITEM attributes[],
858296341Sdelphij                    OPENSSL_ITEM parameters[])
859296341Sdelphij{
860296341Sdelphij    STORE_OBJECT *object;
861296341Sdelphij    int i;
862160814Ssimon
863296341Sdelphij    check_store(s, STORE_F_STORE_STORE_CRL,
864296341Sdelphij                store_object, STORE_R_NO_STORE_OBJECT_FUNCTION);
865160814Ssimon
866296341Sdelphij    object = STORE_OBJECT_new();
867296341Sdelphij    if (!object) {
868296341Sdelphij        STOREerr(STORE_F_STORE_STORE_CRL, ERR_R_MALLOC_FAILURE);
869296341Sdelphij        return 0;
870296341Sdelphij    }
871296341Sdelphij
872296341Sdelphij    CRYPTO_add(&data->references, 1, CRYPTO_LOCK_X509_CRL);
873160814Ssimon#ifdef REF_PRINT
874296341Sdelphij    REF_PRINT("X509_CRL", data);
875160814Ssimon#endif
876296341Sdelphij    object->data.crl = data;
877160814Ssimon
878296341Sdelphij    i = s->meth->store_object(s, STORE_OBJECT_TYPE_X509_CRL, object,
879296341Sdelphij                              attributes, parameters);
880160814Ssimon
881296341Sdelphij    STORE_OBJECT_free(object);
882160814Ssimon
883296341Sdelphij    if (!i) {
884296341Sdelphij        STOREerr(STORE_F_STORE_STORE_CRL, STORE_R_FAILED_STORING_KEY);
885296341Sdelphij        return 0;
886296341Sdelphij    }
887296341Sdelphij    return i;
888296341Sdelphij}
889160814Ssimon
890160814Ssimonint STORE_modify_crl(STORE *s, OPENSSL_ITEM search_attributes[],
891296341Sdelphij                     OPENSSL_ITEM add_attributes[],
892296341Sdelphij                     OPENSSL_ITEM modify_attributes[],
893296341Sdelphij                     OPENSSL_ITEM delete_attributes[],
894296341Sdelphij                     OPENSSL_ITEM parameters[])
895296341Sdelphij{
896296341Sdelphij    check_store(s, STORE_F_STORE_MODIFY_CRL,
897296341Sdelphij                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
898160814Ssimon
899296341Sdelphij    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_X509_CRL,
900296341Sdelphij                                search_attributes, add_attributes,
901296341Sdelphij                                modify_attributes, delete_attributes,
902296341Sdelphij                                parameters)) {
903296341Sdelphij        STOREerr(STORE_F_STORE_MODIFY_CRL, STORE_R_FAILED_MODIFYING_CRL);
904296341Sdelphij        return 0;
905296341Sdelphij    }
906296341Sdelphij    return 1;
907296341Sdelphij}
908160814Ssimon
909160814Ssimonint STORE_delete_crl(STORE *s, OPENSSL_ITEM attributes[],
910296341Sdelphij                     OPENSSL_ITEM parameters[])
911296341Sdelphij{
912296341Sdelphij    check_store(s, STORE_F_STORE_DELETE_CRL,
913296341Sdelphij                delete_object, STORE_R_NO_DELETE_OBJECT_FUNCTION);
914160814Ssimon
915296341Sdelphij    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_X509_CRL,
916296341Sdelphij                                attributes, parameters)) {
917296341Sdelphij        STOREerr(STORE_F_STORE_DELETE_CRL, STORE_R_FAILED_DELETING_KEY);
918296341Sdelphij        return 0;
919296341Sdelphij    }
920296341Sdelphij    return 1;
921296341Sdelphij}
922296341Sdelphij
923160814Ssimonvoid *STORE_list_crl_start(STORE *s, OPENSSL_ITEM attributes[],
924296341Sdelphij                           OPENSSL_ITEM parameters[])
925296341Sdelphij{
926296341Sdelphij    void *handle;
927160814Ssimon
928296341Sdelphij    check_store(s, STORE_F_STORE_LIST_CRL_START,
929296341Sdelphij                list_object_start, STORE_R_NO_LIST_OBJECT_START_FUNCTION);
930160814Ssimon
931296341Sdelphij    handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_X509_CRL,
932296341Sdelphij                                        attributes, parameters);
933296341Sdelphij    if (!handle) {
934296341Sdelphij        STOREerr(STORE_F_STORE_LIST_CRL_START, STORE_R_FAILED_LISTING_KEYS);
935296341Sdelphij        return 0;
936296341Sdelphij    }
937296341Sdelphij    return handle;
938296341Sdelphij}
939160814Ssimon
940160814SsimonX509_CRL *STORE_list_crl_next(STORE *s, void *handle)
941296341Sdelphij{
942296341Sdelphij    STORE_OBJECT *object;
943296341Sdelphij    X509_CRL *crl;
944160814Ssimon
945296341Sdelphij    check_store(s, STORE_F_STORE_LIST_CRL_NEXT,
946296341Sdelphij                list_object_next, STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
947160814Ssimon
948296341Sdelphij    object = s->meth->list_object_next(s, handle);
949296341Sdelphij    if (!object || !object->data.crl) {
950296341Sdelphij        STOREerr(STORE_F_STORE_LIST_CRL_NEXT, STORE_R_FAILED_LISTING_KEYS);
951296341Sdelphij        return 0;
952296341Sdelphij    }
953296341Sdelphij    CRYPTO_add(&object->data.crl->references, 1, CRYPTO_LOCK_X509_CRL);
954160814Ssimon#ifdef REF_PRINT
955296341Sdelphij    REF_PRINT("X509_CRL", data);
956160814Ssimon#endif
957296341Sdelphij    crl = object->data.crl;
958296341Sdelphij    STORE_OBJECT_free(object);
959296341Sdelphij    return crl;
960296341Sdelphij}
961160814Ssimon
962160814Ssimonint STORE_list_crl_end(STORE *s, void *handle)
963296341Sdelphij{
964296341Sdelphij    check_store(s, STORE_F_STORE_LIST_CRL_END,
965296341Sdelphij                list_object_end, STORE_R_NO_LIST_OBJECT_END_FUNCTION);
966160814Ssimon
967296341Sdelphij    if (!s->meth->list_object_end(s, handle)) {
968296341Sdelphij        STOREerr(STORE_F_STORE_LIST_CRL_END, STORE_R_FAILED_LISTING_KEYS);
969296341Sdelphij        return 0;
970296341Sdelphij    }
971296341Sdelphij    return 1;
972296341Sdelphij}
973160814Ssimon
974160814Ssimonint STORE_list_crl_endp(STORE *s, void *handle)
975296341Sdelphij{
976296341Sdelphij    check_store(s, STORE_F_STORE_LIST_CRL_ENDP,
977296341Sdelphij                list_object_endp, STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
978160814Ssimon
979296341Sdelphij    if (!s->meth->list_object_endp(s, handle)) {
980296341Sdelphij        STOREerr(STORE_F_STORE_LIST_CRL_ENDP, STORE_R_FAILED_LISTING_KEYS);
981296341Sdelphij        return 0;
982296341Sdelphij    }
983296341Sdelphij    return 1;
984296341Sdelphij}
985160814Ssimon
986160814Ssimonint STORE_store_number(STORE *s, BIGNUM *data, OPENSSL_ITEM attributes[],
987296341Sdelphij                       OPENSSL_ITEM parameters[])
988296341Sdelphij{
989296341Sdelphij    STORE_OBJECT *object;
990296341Sdelphij    int i;
991160814Ssimon
992296341Sdelphij    check_store(s, STORE_F_STORE_STORE_NUMBER,
993296341Sdelphij                store_object, STORE_R_NO_STORE_OBJECT_NUMBER_FUNCTION);
994160814Ssimon
995296341Sdelphij    object = STORE_OBJECT_new();
996296341Sdelphij    if (!object) {
997296341Sdelphij        STOREerr(STORE_F_STORE_STORE_NUMBER, ERR_R_MALLOC_FAILURE);
998296341Sdelphij        return 0;
999296341Sdelphij    }
1000160814Ssimon
1001296341Sdelphij    object->data.number = data;
1002160814Ssimon
1003296341Sdelphij    i = s->meth->store_object(s, STORE_OBJECT_TYPE_NUMBER, object,
1004296341Sdelphij                              attributes, parameters);
1005160814Ssimon
1006296341Sdelphij    STORE_OBJECT_free(object);
1007160814Ssimon
1008296341Sdelphij    if (!i) {
1009296341Sdelphij        STOREerr(STORE_F_STORE_STORE_NUMBER, STORE_R_FAILED_STORING_NUMBER);
1010296341Sdelphij        return 0;
1011296341Sdelphij    }
1012296341Sdelphij    return 1;
1013296341Sdelphij}
1014296341Sdelphij
1015160814Ssimonint STORE_modify_number(STORE *s, OPENSSL_ITEM search_attributes[],
1016296341Sdelphij                        OPENSSL_ITEM add_attributes[],
1017296341Sdelphij                        OPENSSL_ITEM modify_attributes[],
1018296341Sdelphij                        OPENSSL_ITEM delete_attributes[],
1019296341Sdelphij                        OPENSSL_ITEM parameters[])
1020296341Sdelphij{
1021296341Sdelphij    check_store(s, STORE_F_STORE_MODIFY_NUMBER,
1022296341Sdelphij                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
1023160814Ssimon
1024296341Sdelphij    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_NUMBER,
1025296341Sdelphij                                search_attributes, add_attributes,
1026296341Sdelphij                                modify_attributes, delete_attributes,
1027296341Sdelphij                                parameters)) {
1028296341Sdelphij        STOREerr(STORE_F_STORE_MODIFY_NUMBER,
1029296341Sdelphij                 STORE_R_FAILED_MODIFYING_NUMBER);
1030296341Sdelphij        return 0;
1031296341Sdelphij    }
1032296341Sdelphij    return 1;
1033296341Sdelphij}
1034160814Ssimon
1035160814SsimonBIGNUM *STORE_get_number(STORE *s, OPENSSL_ITEM attributes[],
1036296341Sdelphij                         OPENSSL_ITEM parameters[])
1037296341Sdelphij{
1038296341Sdelphij    STORE_OBJECT *object;
1039296341Sdelphij    BIGNUM *n;
1040160814Ssimon
1041296341Sdelphij    check_store(s, STORE_F_STORE_GET_NUMBER,
1042296341Sdelphij                get_object, STORE_R_NO_GET_OBJECT_NUMBER_FUNCTION);
1043160814Ssimon
1044296341Sdelphij    object = s->meth->get_object(s, STORE_OBJECT_TYPE_NUMBER, attributes,
1045296341Sdelphij                                 parameters);
1046296341Sdelphij    if (!object || !object->data.number) {
1047296341Sdelphij        STOREerr(STORE_F_STORE_GET_NUMBER, STORE_R_FAILED_GETTING_NUMBER);
1048296341Sdelphij        return 0;
1049296341Sdelphij    }
1050296341Sdelphij    n = object->data.number;
1051296341Sdelphij    object->data.number = NULL;
1052296341Sdelphij    STORE_OBJECT_free(object);
1053296341Sdelphij    return n;
1054296341Sdelphij}
1055160814Ssimon
1056160814Ssimonint STORE_delete_number(STORE *s, OPENSSL_ITEM attributes[],
1057296341Sdelphij                        OPENSSL_ITEM parameters[])
1058296341Sdelphij{
1059296341Sdelphij    check_store(s, STORE_F_STORE_DELETE_NUMBER,
1060296341Sdelphij                delete_object, STORE_R_NO_DELETE_NUMBER_FUNCTION);
1061160814Ssimon
1062296341Sdelphij    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_NUMBER, attributes,
1063296341Sdelphij                                parameters)) {
1064296341Sdelphij        STOREerr(STORE_F_STORE_DELETE_NUMBER, STORE_R_FAILED_DELETING_NUMBER);
1065296341Sdelphij        return 0;
1066296341Sdelphij    }
1067296341Sdelphij    return 1;
1068296341Sdelphij}
1069160814Ssimon
1070160814Ssimonint STORE_store_arbitrary(STORE *s, BUF_MEM *data, OPENSSL_ITEM attributes[],
1071296341Sdelphij                          OPENSSL_ITEM parameters[])
1072296341Sdelphij{
1073296341Sdelphij    STORE_OBJECT *object;
1074296341Sdelphij    int i;
1075160814Ssimon
1076296341Sdelphij    check_store(s, STORE_F_STORE_STORE_ARBITRARY,
1077296341Sdelphij                store_object, STORE_R_NO_STORE_OBJECT_ARBITRARY_FUNCTION);
1078160814Ssimon
1079296341Sdelphij    object = STORE_OBJECT_new();
1080296341Sdelphij    if (!object) {
1081296341Sdelphij        STOREerr(STORE_F_STORE_STORE_ARBITRARY, ERR_R_MALLOC_FAILURE);
1082296341Sdelphij        return 0;
1083296341Sdelphij    }
1084160814Ssimon
1085296341Sdelphij    object->data.arbitrary = data;
1086160814Ssimon
1087296341Sdelphij    i = s->meth->store_object(s, STORE_OBJECT_TYPE_ARBITRARY, object,
1088296341Sdelphij                              attributes, parameters);
1089160814Ssimon
1090296341Sdelphij    STORE_OBJECT_free(object);
1091160814Ssimon
1092296341Sdelphij    if (!i) {
1093296341Sdelphij        STOREerr(STORE_F_STORE_STORE_ARBITRARY,
1094296341Sdelphij                 STORE_R_FAILED_STORING_ARBITRARY);
1095296341Sdelphij        return 0;
1096296341Sdelphij    }
1097296341Sdelphij    return 1;
1098296341Sdelphij}
1099296341Sdelphij
1100160814Ssimonint STORE_modify_arbitrary(STORE *s, OPENSSL_ITEM search_attributes[],
1101296341Sdelphij                           OPENSSL_ITEM add_attributes[],
1102296341Sdelphij                           OPENSSL_ITEM modify_attributes[],
1103296341Sdelphij                           OPENSSL_ITEM delete_attributes[],
1104296341Sdelphij                           OPENSSL_ITEM parameters[])
1105296341Sdelphij{
1106296341Sdelphij    check_store(s, STORE_F_STORE_MODIFY_ARBITRARY,
1107296341Sdelphij                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
1108160814Ssimon
1109296341Sdelphij    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_ARBITRARY,
1110296341Sdelphij                                search_attributes, add_attributes,
1111296341Sdelphij                                modify_attributes, delete_attributes,
1112296341Sdelphij                                parameters)) {
1113296341Sdelphij        STOREerr(STORE_F_STORE_MODIFY_ARBITRARY,
1114296341Sdelphij                 STORE_R_FAILED_MODIFYING_ARBITRARY);
1115296341Sdelphij        return 0;
1116296341Sdelphij    }
1117296341Sdelphij    return 1;
1118296341Sdelphij}
1119160814Ssimon
1120160814SsimonBUF_MEM *STORE_get_arbitrary(STORE *s, OPENSSL_ITEM attributes[],
1121296341Sdelphij                             OPENSSL_ITEM parameters[])
1122296341Sdelphij{
1123296341Sdelphij    STORE_OBJECT *object;
1124296341Sdelphij    BUF_MEM *b;
1125160814Ssimon
1126296341Sdelphij    check_store(s, STORE_F_STORE_GET_ARBITRARY,
1127296341Sdelphij                get_object, STORE_R_NO_GET_OBJECT_ARBITRARY_FUNCTION);
1128160814Ssimon
1129296341Sdelphij    object = s->meth->get_object(s, STORE_OBJECT_TYPE_ARBITRARY,
1130296341Sdelphij                                 attributes, parameters);
1131296341Sdelphij    if (!object || !object->data.arbitrary) {
1132296341Sdelphij        STOREerr(STORE_F_STORE_GET_ARBITRARY,
1133296341Sdelphij                 STORE_R_FAILED_GETTING_ARBITRARY);
1134296341Sdelphij        return 0;
1135296341Sdelphij    }
1136296341Sdelphij    b = object->data.arbitrary;
1137296341Sdelphij    object->data.arbitrary = NULL;
1138296341Sdelphij    STORE_OBJECT_free(object);
1139296341Sdelphij    return b;
1140296341Sdelphij}
1141160814Ssimon
1142160814Ssimonint STORE_delete_arbitrary(STORE *s, OPENSSL_ITEM attributes[],
1143296341Sdelphij                           OPENSSL_ITEM parameters[])
1144296341Sdelphij{
1145296341Sdelphij    check_store(s, STORE_F_STORE_DELETE_ARBITRARY,
1146296341Sdelphij                delete_object, STORE_R_NO_DELETE_ARBITRARY_FUNCTION);
1147160814Ssimon
1148296341Sdelphij    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_ARBITRARY, attributes,
1149296341Sdelphij                                parameters)) {
1150296341Sdelphij        STOREerr(STORE_F_STORE_DELETE_ARBITRARY,
1151296341Sdelphij                 STORE_R_FAILED_DELETING_ARBITRARY);
1152296341Sdelphij        return 0;
1153296341Sdelphij    }
1154296341Sdelphij    return 1;
1155296341Sdelphij}
1156160814Ssimon
1157160814SsimonSTORE_OBJECT *STORE_OBJECT_new(void)
1158296341Sdelphij{
1159296341Sdelphij    STORE_OBJECT *object = OPENSSL_malloc(sizeof(STORE_OBJECT));
1160296341Sdelphij    if (object)
1161296341Sdelphij        memset(object, 0, sizeof(STORE_OBJECT));
1162296341Sdelphij    return object;
1163296341Sdelphij}
1164296341Sdelphij
1165160814Ssimonvoid STORE_OBJECT_free(STORE_OBJECT *data)
1166296341Sdelphij{
1167296341Sdelphij    if (!data)
1168296341Sdelphij        return;
1169296341Sdelphij    switch (data->type) {
1170296341Sdelphij    case STORE_OBJECT_TYPE_X509_CERTIFICATE:
1171296341Sdelphij        X509_free(data->data.x509.certificate);
1172296341Sdelphij        break;
1173296341Sdelphij    case STORE_OBJECT_TYPE_X509_CRL:
1174296341Sdelphij        X509_CRL_free(data->data.crl);
1175296341Sdelphij        break;
1176296341Sdelphij    case STORE_OBJECT_TYPE_PRIVATE_KEY:
1177296341Sdelphij    case STORE_OBJECT_TYPE_PUBLIC_KEY:
1178296341Sdelphij        EVP_PKEY_free(data->data.key);
1179296341Sdelphij        break;
1180296341Sdelphij    case STORE_OBJECT_TYPE_NUMBER:
1181296341Sdelphij        BN_free(data->data.number);
1182296341Sdelphij        break;
1183296341Sdelphij    case STORE_OBJECT_TYPE_ARBITRARY:
1184296341Sdelphij        BUF_MEM_free(data->data.arbitrary);
1185296341Sdelphij        break;
1186296341Sdelphij    }
1187296341Sdelphij    OPENSSL_free(data);
1188296341Sdelphij}
1189160814Ssimon
1190160814SsimonIMPLEMENT_STACK_OF(STORE_OBJECT*)
1191160814Ssimon
1192296341Sdelphijstruct STORE_attr_info_st {
1193296341Sdelphij    unsigned char set[(STORE_ATTR_TYPE_NUM + 8) / 8];
1194296341Sdelphij    union {
1195296341Sdelphij        char *cstring;
1196296341Sdelphij        unsigned char *sha1string;
1197296341Sdelphij        X509_NAME *dn;
1198296341Sdelphij        BIGNUM *number;
1199296341Sdelphij        void *any;
1200296341Sdelphij    } values[STORE_ATTR_TYPE_NUM + 1];
1201296341Sdelphij    size_t value_sizes[STORE_ATTR_TYPE_NUM + 1];
1202296341Sdelphij};
1203160814Ssimon
1204296341Sdelphij#define ATTR_IS_SET(a,i)        ((i) > 0 && (i) < STORE_ATTR_TYPE_NUM \
1205296341Sdelphij                                && ((a)->set[(i) / 8] & (1 << ((i) % 8))))
1206296341Sdelphij#define SET_ATTRBIT(a,i)        ((a)->set[(i) / 8] |= (1 << ((i) % 8)))
1207296341Sdelphij#define CLEAR_ATTRBIT(a,i)      ((a)->set[(i) / 8] &= ~(1 << ((i) % 8)))
1208160814Ssimon
1209296341SdelphijSTORE_ATTR_INFO *STORE_ATTR_INFO_new(void)
1210296341Sdelphij{
1211296341Sdelphij    return (STORE_ATTR_INFO *)OPENSSL_malloc(sizeof(STORE_ATTR_INFO));
1212296341Sdelphij}
1213160814Ssimon
1214160814Ssimonstatic void STORE_ATTR_INFO_attr_free(STORE_ATTR_INFO *attrs,
1215296341Sdelphij                                      STORE_ATTR_TYPES code)
1216296341Sdelphij{
1217296341Sdelphij    if (ATTR_IS_SET(attrs, code)) {
1218296341Sdelphij        switch (code) {
1219296341Sdelphij        case STORE_ATTR_FRIENDLYNAME:
1220296341Sdelphij        case STORE_ATTR_EMAIL:
1221296341Sdelphij        case STORE_ATTR_FILENAME:
1222296341Sdelphij            STORE_ATTR_INFO_modify_cstr(attrs, code, NULL, 0);
1223296341Sdelphij            break;
1224296341Sdelphij        case STORE_ATTR_KEYID:
1225296341Sdelphij        case STORE_ATTR_ISSUERKEYID:
1226296341Sdelphij        case STORE_ATTR_SUBJECTKEYID:
1227296341Sdelphij        case STORE_ATTR_ISSUERSERIALHASH:
1228296341Sdelphij        case STORE_ATTR_CERTHASH:
1229296341Sdelphij            STORE_ATTR_INFO_modify_sha1str(attrs, code, NULL, 0);
1230296341Sdelphij            break;
1231296341Sdelphij        case STORE_ATTR_ISSUER:
1232296341Sdelphij        case STORE_ATTR_SUBJECT:
1233296341Sdelphij            STORE_ATTR_INFO_modify_dn(attrs, code, NULL);
1234296341Sdelphij            break;
1235296341Sdelphij        case STORE_ATTR_SERIAL:
1236296341Sdelphij            STORE_ATTR_INFO_modify_number(attrs, code, NULL);
1237296341Sdelphij            break;
1238296341Sdelphij        default:
1239296341Sdelphij            break;
1240296341Sdelphij        }
1241296341Sdelphij    }
1242296341Sdelphij}
1243296341Sdelphij
1244160814Ssimonint STORE_ATTR_INFO_free(STORE_ATTR_INFO *attrs)
1245296341Sdelphij{
1246296341Sdelphij    if (attrs) {
1247296341Sdelphij        STORE_ATTR_TYPES i;
1248296341Sdelphij        for (i = 0; i++ < STORE_ATTR_TYPE_NUM;)
1249296341Sdelphij            STORE_ATTR_INFO_attr_free(attrs, i);
1250296341Sdelphij        OPENSSL_free(attrs);
1251296341Sdelphij    }
1252296341Sdelphij    return 1;
1253296341Sdelphij}
1254296341Sdelphij
1255160814Ssimonchar *STORE_ATTR_INFO_get0_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code)
1256296341Sdelphij{
1257296341Sdelphij    if (!attrs) {
1258296341Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_GET0_CSTR,
1259296341Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1260296341Sdelphij        return NULL;
1261296341Sdelphij    }
1262296341Sdelphij    if (ATTR_IS_SET(attrs, code))
1263296341Sdelphij        return attrs->values[code].cstring;
1264296341Sdelphij    STOREerr(STORE_F_STORE_ATTR_INFO_GET0_CSTR, STORE_R_NO_VALUE);
1265296341Sdelphij    return NULL;
1266296341Sdelphij}
1267296341Sdelphij
1268160814Ssimonunsigned char *STORE_ATTR_INFO_get0_sha1str(STORE_ATTR_INFO *attrs,
1269296341Sdelphij                                            STORE_ATTR_TYPES code)
1270296341Sdelphij{
1271296341Sdelphij    if (!attrs) {
1272296341Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_GET0_SHA1STR,
1273296341Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1274296341Sdelphij        return NULL;
1275296341Sdelphij    }
1276296341Sdelphij    if (ATTR_IS_SET(attrs, code))
1277296341Sdelphij        return attrs->values[code].sha1string;
1278296341Sdelphij    STOREerr(STORE_F_STORE_ATTR_INFO_GET0_SHA1STR, STORE_R_NO_VALUE);
1279296341Sdelphij    return NULL;
1280296341Sdelphij}
1281296341Sdelphij
1282296341SdelphijX509_NAME *STORE_ATTR_INFO_get0_dn(STORE_ATTR_INFO *attrs,
1283296341Sdelphij                                   STORE_ATTR_TYPES code)
1284296341Sdelphij{
1285296341Sdelphij    if (!attrs) {
1286296341Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_GET0_DN,
1287296341Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1288296341Sdelphij        return NULL;
1289296341Sdelphij    }
1290296341Sdelphij    if (ATTR_IS_SET(attrs, code))
1291296341Sdelphij        return attrs->values[code].dn;
1292296341Sdelphij    STOREerr(STORE_F_STORE_ATTR_INFO_GET0_DN, STORE_R_NO_VALUE);
1293296341Sdelphij    return NULL;
1294296341Sdelphij}
1295296341Sdelphij
1296296341SdelphijBIGNUM *STORE_ATTR_INFO_get0_number(STORE_ATTR_INFO *attrs,
1297296341Sdelphij                                    STORE_ATTR_TYPES code)
1298296341Sdelphij{
1299296341Sdelphij    if (!attrs) {
1300296341Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_GET0_NUMBER,
1301296341Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1302296341Sdelphij        return NULL;
1303296341Sdelphij    }
1304296341Sdelphij    if (ATTR_IS_SET(attrs, code))
1305296341Sdelphij        return attrs->values[code].number;
1306296341Sdelphij    STOREerr(STORE_F_STORE_ATTR_INFO_GET0_NUMBER, STORE_R_NO_VALUE);
1307296341Sdelphij    return NULL;
1308296341Sdelphij}
1309296341Sdelphij
1310160814Ssimonint STORE_ATTR_INFO_set_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1311296341Sdelphij                             char *cstr, size_t cstr_size)
1312296341Sdelphij{
1313296341Sdelphij    if (!attrs) {
1314296341Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR,
1315296341Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1316296341Sdelphij        return 0;
1317296341Sdelphij    }
1318296341Sdelphij    if (!ATTR_IS_SET(attrs, code)) {
1319296341Sdelphij        if ((attrs->values[code].cstring = BUF_strndup(cstr, cstr_size)))
1320296341Sdelphij            return 1;
1321296341Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR, ERR_R_MALLOC_FAILURE);
1322296341Sdelphij        return 0;
1323296341Sdelphij    }
1324296341Sdelphij    STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR, STORE_R_ALREADY_HAS_A_VALUE);
1325296341Sdelphij    return 0;
1326296341Sdelphij}
1327296341Sdelphij
1328160814Ssimonint STORE_ATTR_INFO_set_sha1str(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1329296341Sdelphij                                unsigned char *sha1str, size_t sha1str_size)
1330296341Sdelphij{
1331296341Sdelphij    if (!attrs) {
1332296341Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR,
1333296341Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1334296341Sdelphij        return 0;
1335296341Sdelphij    }
1336296341Sdelphij    if (!ATTR_IS_SET(attrs, code)) {
1337296341Sdelphij        if ((attrs->values[code].sha1string =
1338296341Sdelphij             (unsigned char *)BUF_memdup(sha1str, sha1str_size)))
1339296341Sdelphij            return 1;
1340296341Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR, ERR_R_MALLOC_FAILURE);
1341296341Sdelphij        return 0;
1342296341Sdelphij    }
1343296341Sdelphij    STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR,
1344296341Sdelphij             STORE_R_ALREADY_HAS_A_VALUE);
1345296341Sdelphij    return 0;
1346296341Sdelphij}
1347296341Sdelphij
1348160814Ssimonint STORE_ATTR_INFO_set_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1349296341Sdelphij                           X509_NAME *dn)
1350296341Sdelphij{
1351296341Sdelphij    if (!attrs) {
1352296341Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN, ERR_R_PASSED_NULL_PARAMETER);
1353296341Sdelphij        return 0;
1354296341Sdelphij    }
1355296341Sdelphij    if (!ATTR_IS_SET(attrs, code)) {
1356296341Sdelphij        if ((attrs->values[code].dn = X509_NAME_dup(dn)))
1357296341Sdelphij            return 1;
1358296341Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN, ERR_R_MALLOC_FAILURE);
1359296341Sdelphij        return 0;
1360296341Sdelphij    }
1361296341Sdelphij    STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN, STORE_R_ALREADY_HAS_A_VALUE);
1362296341Sdelphij    return 0;
1363296341Sdelphij}
1364296341Sdelphij
1365160814Ssimonint STORE_ATTR_INFO_set_number(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1366296341Sdelphij                               BIGNUM *number)
1367296341Sdelphij{
1368296341Sdelphij    if (!attrs) {
1369296341Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER,
1370296341Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1371296341Sdelphij        return 0;
1372296341Sdelphij    }
1373296341Sdelphij    if (!ATTR_IS_SET(attrs, code)) {
1374296341Sdelphij        if ((attrs->values[code].number = BN_dup(number)))
1375296341Sdelphij            return 1;
1376296341Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER, ERR_R_MALLOC_FAILURE);
1377296341Sdelphij        return 0;
1378296341Sdelphij    }
1379296341Sdelphij    STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER, STORE_R_ALREADY_HAS_A_VALUE);
1380296341Sdelphij    return 0;
1381296341Sdelphij}
1382296341Sdelphij
1383160814Ssimonint STORE_ATTR_INFO_modify_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1384296341Sdelphij                                char *cstr, size_t cstr_size)
1385296341Sdelphij{
1386296341Sdelphij    if (!attrs) {
1387296341Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_CSTR,
1388296341Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1389296341Sdelphij        return 0;
1390296341Sdelphij    }
1391296341Sdelphij    if (ATTR_IS_SET(attrs, code)) {
1392296341Sdelphij        OPENSSL_free(attrs->values[code].cstring);
1393296341Sdelphij        attrs->values[code].cstring = NULL;
1394296341Sdelphij        CLEAR_ATTRBIT(attrs, code);
1395296341Sdelphij    }
1396296341Sdelphij    return STORE_ATTR_INFO_set_cstr(attrs, code, cstr, cstr_size);
1397296341Sdelphij}
1398296341Sdelphij
1399296341Sdelphijint STORE_ATTR_INFO_modify_sha1str(STORE_ATTR_INFO *attrs,
1400296341Sdelphij                                   STORE_ATTR_TYPES code,
1401296341Sdelphij                                   unsigned char *sha1str,
1402296341Sdelphij                                   size_t sha1str_size)
1403296341Sdelphij{
1404296341Sdelphij    if (!attrs) {
1405296341Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_SHA1STR,
1406296341Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1407296341Sdelphij        return 0;
1408296341Sdelphij    }
1409296341Sdelphij    if (ATTR_IS_SET(attrs, code)) {
1410296341Sdelphij        OPENSSL_free(attrs->values[code].sha1string);
1411296341Sdelphij        attrs->values[code].sha1string = NULL;
1412296341Sdelphij        CLEAR_ATTRBIT(attrs, code);
1413296341Sdelphij    }
1414296341Sdelphij    return STORE_ATTR_INFO_set_sha1str(attrs, code, sha1str, sha1str_size);
1415296341Sdelphij}
1416296341Sdelphij
1417160814Ssimonint STORE_ATTR_INFO_modify_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1418296341Sdelphij                              X509_NAME *dn)
1419296341Sdelphij{
1420296341Sdelphij    if (!attrs) {
1421296341Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_DN,
1422296341Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1423296341Sdelphij        return 0;
1424296341Sdelphij    }
1425296341Sdelphij    if (ATTR_IS_SET(attrs, code)) {
1426296341Sdelphij        OPENSSL_free(attrs->values[code].dn);
1427296341Sdelphij        attrs->values[code].dn = NULL;
1428296341Sdelphij        CLEAR_ATTRBIT(attrs, code);
1429296341Sdelphij    }
1430296341Sdelphij    return STORE_ATTR_INFO_set_dn(attrs, code, dn);
1431296341Sdelphij}
1432160814Ssimon
1433296341Sdelphijint STORE_ATTR_INFO_modify_number(STORE_ATTR_INFO *attrs,
1434296341Sdelphij                                  STORE_ATTR_TYPES code, BIGNUM *number)
1435296341Sdelphij{
1436296341Sdelphij    if (!attrs) {
1437296341Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_NUMBER,
1438296341Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1439296341Sdelphij        return 0;
1440296341Sdelphij    }
1441296341Sdelphij    if (ATTR_IS_SET(attrs, code)) {
1442296341Sdelphij        OPENSSL_free(attrs->values[code].number);
1443296341Sdelphij        attrs->values[code].number = NULL;
1444296341Sdelphij        CLEAR_ATTRBIT(attrs, code);
1445296341Sdelphij    }
1446296341Sdelphij    return STORE_ATTR_INFO_set_number(attrs, code, number);
1447296341Sdelphij}
1448296341Sdelphij
1449296341Sdelphijstruct attr_list_ctx_st {
1450296341Sdelphij    OPENSSL_ITEM *attributes;
1451296341Sdelphij};
1452160814Ssimonvoid *STORE_parse_attrs_start(OPENSSL_ITEM *attributes)
1453296341Sdelphij{
1454296341Sdelphij    if (attributes) {
1455296341Sdelphij        struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)
1456296341Sdelphij            OPENSSL_malloc(sizeof(struct attr_list_ctx_st));
1457296341Sdelphij        if (context)
1458296341Sdelphij            context->attributes = attributes;
1459296341Sdelphij        else
1460296341Sdelphij            STOREerr(STORE_F_STORE_PARSE_ATTRS_START, ERR_R_MALLOC_FAILURE);
1461296341Sdelphij        return context;
1462296341Sdelphij    }
1463296341Sdelphij    STOREerr(STORE_F_STORE_PARSE_ATTRS_START, ERR_R_PASSED_NULL_PARAMETER);
1464296341Sdelphij    return 0;
1465296341Sdelphij}
1466296341Sdelphij
1467160814SsimonSTORE_ATTR_INFO *STORE_parse_attrs_next(void *handle)
1468296341Sdelphij{
1469296341Sdelphij    struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;
1470160814Ssimon
1471296341Sdelphij    if (context && context->attributes) {
1472296341Sdelphij        STORE_ATTR_INFO *attrs = NULL;
1473160814Ssimon
1474296341Sdelphij        while (context->attributes
1475296341Sdelphij               && context->attributes->code != STORE_ATTR_OR
1476296341Sdelphij               && context->attributes->code != STORE_ATTR_END) {
1477296341Sdelphij            switch (context->attributes->code) {
1478296341Sdelphij            case STORE_ATTR_FRIENDLYNAME:
1479296341Sdelphij            case STORE_ATTR_EMAIL:
1480296341Sdelphij            case STORE_ATTR_FILENAME:
1481296341Sdelphij                if (!attrs)
1482296341Sdelphij                    attrs = STORE_ATTR_INFO_new();
1483296341Sdelphij                if (attrs == NULL) {
1484296341Sdelphij                    STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
1485296341Sdelphij                             ERR_R_MALLOC_FAILURE);
1486296341Sdelphij                    goto err;
1487296341Sdelphij                }
1488296341Sdelphij                STORE_ATTR_INFO_set_cstr(attrs,
1489296341Sdelphij                                         context->attributes->code,
1490296341Sdelphij                                         context->attributes->value,
1491296341Sdelphij                                         context->attributes->value_size);
1492296341Sdelphij                break;
1493296341Sdelphij            case STORE_ATTR_KEYID:
1494296341Sdelphij            case STORE_ATTR_ISSUERKEYID:
1495296341Sdelphij            case STORE_ATTR_SUBJECTKEYID:
1496296341Sdelphij            case STORE_ATTR_ISSUERSERIALHASH:
1497296341Sdelphij            case STORE_ATTR_CERTHASH:
1498296341Sdelphij                if (!attrs)
1499296341Sdelphij                    attrs = STORE_ATTR_INFO_new();
1500296341Sdelphij                if (attrs == NULL) {
1501296341Sdelphij                    STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
1502296341Sdelphij                             ERR_R_MALLOC_FAILURE);
1503296341Sdelphij                    goto err;
1504296341Sdelphij                }
1505296341Sdelphij                STORE_ATTR_INFO_set_sha1str(attrs,
1506296341Sdelphij                                            context->attributes->code,
1507296341Sdelphij                                            context->attributes->value,
1508296341Sdelphij                                            context->attributes->value_size);
1509296341Sdelphij                break;
1510296341Sdelphij            case STORE_ATTR_ISSUER:
1511296341Sdelphij            case STORE_ATTR_SUBJECT:
1512296341Sdelphij                if (!attrs)
1513296341Sdelphij                    attrs = STORE_ATTR_INFO_new();
1514296341Sdelphij                if (attrs == NULL) {
1515296341Sdelphij                    STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
1516296341Sdelphij                             ERR_R_MALLOC_FAILURE);
1517296341Sdelphij                    goto err;
1518296341Sdelphij                }
1519296341Sdelphij                STORE_ATTR_INFO_modify_dn(attrs,
1520296341Sdelphij                                          context->attributes->code,
1521296341Sdelphij                                          context->attributes->value);
1522296341Sdelphij                break;
1523296341Sdelphij            case STORE_ATTR_SERIAL:
1524296341Sdelphij                if (!attrs)
1525296341Sdelphij                    attrs = STORE_ATTR_INFO_new();
1526296341Sdelphij                if (attrs == NULL) {
1527296341Sdelphij                    STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
1528296341Sdelphij                             ERR_R_MALLOC_FAILURE);
1529296341Sdelphij                    goto err;
1530296341Sdelphij                }
1531296341Sdelphij                STORE_ATTR_INFO_modify_number(attrs,
1532296341Sdelphij                                              context->attributes->code,
1533296341Sdelphij                                              context->attributes->value);
1534296341Sdelphij                break;
1535296341Sdelphij            }
1536296341Sdelphij            context->attributes++;
1537296341Sdelphij        }
1538296341Sdelphij        if (context->attributes->code == STORE_ATTR_OR)
1539296341Sdelphij            context->attributes++;
1540296341Sdelphij        return attrs;
1541296341Sdelphij err:
1542296341Sdelphij        while (context->attributes
1543296341Sdelphij               && context->attributes->code != STORE_ATTR_OR
1544296341Sdelphij               && context->attributes->code != STORE_ATTR_END)
1545296341Sdelphij            context->attributes++;
1546296341Sdelphij        if (context->attributes->code == STORE_ATTR_OR)
1547296341Sdelphij            context->attributes++;
1548296341Sdelphij        return NULL;
1549296341Sdelphij    }
1550296341Sdelphij    STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT, ERR_R_PASSED_NULL_PARAMETER);
1551296341Sdelphij    return NULL;
1552296341Sdelphij}
1553296341Sdelphij
1554160814Ssimonint STORE_parse_attrs_end(void *handle)
1555296341Sdelphij{
1556296341Sdelphij    struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;
1557160814Ssimon
1558296341Sdelphij    if (context && context->attributes) {
1559160814Ssimon#if 0
1560296341Sdelphij        OPENSSL_ITEM *attributes = context->attributes;
1561160814Ssimon#endif
1562296341Sdelphij        OPENSSL_free(context);
1563296341Sdelphij        return 1;
1564296341Sdelphij    }
1565296341Sdelphij    STOREerr(STORE_F_STORE_PARSE_ATTRS_END, ERR_R_PASSED_NULL_PARAMETER);
1566296341Sdelphij    return 0;
1567296341Sdelphij}
1568160814Ssimon
1569160814Ssimonint STORE_parse_attrs_endp(void *handle)
1570296341Sdelphij{
1571296341Sdelphij    struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;
1572160814Ssimon
1573296341Sdelphij    if (context && context->attributes) {
1574296341Sdelphij        return context->attributes->code == STORE_ATTR_END;
1575296341Sdelphij    }
1576296341Sdelphij    STOREerr(STORE_F_STORE_PARSE_ATTRS_ENDP, ERR_R_PASSED_NULL_PARAMETER);
1577296341Sdelphij    return 0;
1578296341Sdelphij}
1579160814Ssimon
1580296341Sdelphijstatic int attr_info_compare_compute_range(const unsigned char *abits,
1581296341Sdelphij                                           const unsigned char *bbits,
1582296341Sdelphij                                           unsigned int *alowp,
1583296341Sdelphij                                           unsigned int *ahighp,
1584296341Sdelphij                                           unsigned int *blowp,
1585296341Sdelphij                                           unsigned int *bhighp)
1586296341Sdelphij{
1587296341Sdelphij    unsigned int alow = (unsigned int)-1, ahigh = 0;
1588296341Sdelphij    unsigned int blow = (unsigned int)-1, bhigh = 0;
1589296341Sdelphij    int i, res = 0;
1590160814Ssimon
1591296341Sdelphij    for (i = 0; i < (STORE_ATTR_TYPE_NUM + 8) / 8; i++, abits++, bbits++) {
1592296341Sdelphij        if (res == 0) {
1593296341Sdelphij            if (*abits < *bbits)
1594296341Sdelphij                res = -1;
1595296341Sdelphij            if (*abits > *bbits)
1596296341Sdelphij                res = 1;
1597296341Sdelphij        }
1598296341Sdelphij        if (*abits) {
1599296341Sdelphij            if (alow == (unsigned int)-1) {
1600296341Sdelphij                alow = i * 8;
1601296341Sdelphij                if (!(*abits & 0x01))
1602296341Sdelphij                    alow++;
1603296341Sdelphij                if (!(*abits & 0x02))
1604296341Sdelphij                    alow++;
1605296341Sdelphij                if (!(*abits & 0x04))
1606296341Sdelphij                    alow++;
1607296341Sdelphij                if (!(*abits & 0x08))
1608296341Sdelphij                    alow++;
1609296341Sdelphij                if (!(*abits & 0x10))
1610296341Sdelphij                    alow++;
1611296341Sdelphij                if (!(*abits & 0x20))
1612296341Sdelphij                    alow++;
1613296341Sdelphij                if (!(*abits & 0x40))
1614296341Sdelphij                    alow++;
1615296341Sdelphij            }
1616296341Sdelphij            ahigh = i * 8 + 7;
1617296341Sdelphij            if (!(*abits & 0x80))
1618296341Sdelphij                ahigh++;
1619296341Sdelphij            if (!(*abits & 0x40))
1620296341Sdelphij                ahigh++;
1621296341Sdelphij            if (!(*abits & 0x20))
1622296341Sdelphij                ahigh++;
1623296341Sdelphij            if (!(*abits & 0x10))
1624296341Sdelphij                ahigh++;
1625296341Sdelphij            if (!(*abits & 0x08))
1626296341Sdelphij                ahigh++;
1627296341Sdelphij            if (!(*abits & 0x04))
1628296341Sdelphij                ahigh++;
1629296341Sdelphij            if (!(*abits & 0x02))
1630296341Sdelphij                ahigh++;
1631296341Sdelphij        }
1632296341Sdelphij        if (*bbits) {
1633296341Sdelphij            if (blow == (unsigned int)-1) {
1634296341Sdelphij                blow = i * 8;
1635296341Sdelphij                if (!(*bbits & 0x01))
1636296341Sdelphij                    blow++;
1637296341Sdelphij                if (!(*bbits & 0x02))
1638296341Sdelphij                    blow++;
1639296341Sdelphij                if (!(*bbits & 0x04))
1640296341Sdelphij                    blow++;
1641296341Sdelphij                if (!(*bbits & 0x08))
1642296341Sdelphij                    blow++;
1643296341Sdelphij                if (!(*bbits & 0x10))
1644296341Sdelphij                    blow++;
1645296341Sdelphij                if (!(*bbits & 0x20))
1646296341Sdelphij                    blow++;
1647296341Sdelphij                if (!(*bbits & 0x40))
1648296341Sdelphij                    blow++;
1649296341Sdelphij            }
1650296341Sdelphij            bhigh = i * 8 + 7;
1651296341Sdelphij            if (!(*bbits & 0x80))
1652296341Sdelphij                bhigh++;
1653296341Sdelphij            if (!(*bbits & 0x40))
1654296341Sdelphij                bhigh++;
1655296341Sdelphij            if (!(*bbits & 0x20))
1656296341Sdelphij                bhigh++;
1657296341Sdelphij            if (!(*bbits & 0x10))
1658296341Sdelphij                bhigh++;
1659296341Sdelphij            if (!(*bbits & 0x08))
1660296341Sdelphij                bhigh++;
1661296341Sdelphij            if (!(*bbits & 0x04))
1662296341Sdelphij                bhigh++;
1663296341Sdelphij            if (!(*bbits & 0x02))
1664296341Sdelphij                bhigh++;
1665296341Sdelphij        }
1666296341Sdelphij    }
1667296341Sdelphij    if (ahigh + alow < bhigh + blow)
1668296341Sdelphij        res = -1;
1669296341Sdelphij    if (ahigh + alow > bhigh + blow)
1670296341Sdelphij        res = 1;
1671296341Sdelphij    if (alowp)
1672296341Sdelphij        *alowp = alow;
1673296341Sdelphij    if (ahighp)
1674296341Sdelphij        *ahighp = ahigh;
1675296341Sdelphij    if (blowp)
1676296341Sdelphij        *blowp = blow;
1677296341Sdelphij    if (bhighp)
1678296341Sdelphij        *bhighp = bhigh;
1679296341Sdelphij    return res;
1680296341Sdelphij}
1681160814Ssimon
1682296341Sdelphijint STORE_ATTR_INFO_compare(const STORE_ATTR_INFO *const *a,
1683296341Sdelphij                            const STORE_ATTR_INFO *const *b)
1684296341Sdelphij{
1685296341Sdelphij    if (a == b)
1686296341Sdelphij        return 0;
1687296341Sdelphij    if (!a)
1688296341Sdelphij        return -1;
1689296341Sdelphij    if (!b)
1690296341Sdelphij        return 1;
1691296341Sdelphij    return attr_info_compare_compute_range((*a)->set, (*b)->set, 0, 0, 0, 0);
1692296341Sdelphij}
1693238405Sjkim
1694160814Ssimonint STORE_ATTR_INFO_in_range(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
1695296341Sdelphij{
1696296341Sdelphij    unsigned int alow, ahigh, blow, bhigh;
1697160814Ssimon
1698296341Sdelphij    if (a == b)
1699296341Sdelphij        return 1;
1700296341Sdelphij    if (!a)
1701296341Sdelphij        return 0;
1702296341Sdelphij    if (!b)
1703296341Sdelphij        return 0;
1704296341Sdelphij    attr_info_compare_compute_range(a->set, b->set,
1705296341Sdelphij                                    &alow, &ahigh, &blow, &bhigh);
1706296341Sdelphij    if (alow >= blow && ahigh <= bhigh)
1707296341Sdelphij        return 1;
1708296341Sdelphij    return 0;
1709296341Sdelphij}
1710238405Sjkim
1711160814Ssimonint STORE_ATTR_INFO_in(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
1712296341Sdelphij{
1713296341Sdelphij    unsigned char *abits, *bbits;
1714296341Sdelphij    int i;
1715160814Ssimon
1716296341Sdelphij    if (a == b)
1717296341Sdelphij        return 1;
1718296341Sdelphij    if (!a)
1719296341Sdelphij        return 0;
1720296341Sdelphij    if (!b)
1721296341Sdelphij        return 0;
1722296341Sdelphij    abits = a->set;
1723296341Sdelphij    bbits = b->set;
1724296341Sdelphij    for (i = 0; i < (STORE_ATTR_TYPE_NUM + 8) / 8; i++, abits++, bbits++) {
1725296341Sdelphij        if (*abits && (*bbits & *abits) != *abits)
1726296341Sdelphij            return 0;
1727296341Sdelphij    }
1728296341Sdelphij    return 1;
1729296341Sdelphij}
1730238405Sjkim
1731160814Ssimonint STORE_ATTR_INFO_in_ex(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
1732296341Sdelphij{
1733296341Sdelphij    STORE_ATTR_TYPES i;
1734160814Ssimon
1735296341Sdelphij    if (a == b)
1736296341Sdelphij        return 1;
1737296341Sdelphij    if (!STORE_ATTR_INFO_in(a, b))
1738296341Sdelphij        return 0;
1739296341Sdelphij    for (i = 1; i < STORE_ATTR_TYPE_NUM; i++)
1740296341Sdelphij        if (ATTR_IS_SET(a, i)) {
1741296341Sdelphij            switch (i) {
1742296341Sdelphij            case STORE_ATTR_FRIENDLYNAME:
1743296341Sdelphij            case STORE_ATTR_EMAIL:
1744296341Sdelphij            case STORE_ATTR_FILENAME:
1745296341Sdelphij                if (strcmp(a->values[i].cstring, b->values[i].cstring))
1746296341Sdelphij                    return 0;
1747296341Sdelphij                break;
1748296341Sdelphij            case STORE_ATTR_KEYID:
1749296341Sdelphij            case STORE_ATTR_ISSUERKEYID:
1750296341Sdelphij            case STORE_ATTR_SUBJECTKEYID:
1751296341Sdelphij            case STORE_ATTR_ISSUERSERIALHASH:
1752296341Sdelphij            case STORE_ATTR_CERTHASH:
1753296341Sdelphij                if (memcmp(a->values[i].sha1string,
1754296341Sdelphij                           b->values[i].sha1string, a->value_sizes[i]))
1755296341Sdelphij                    return 0;
1756296341Sdelphij                break;
1757296341Sdelphij            case STORE_ATTR_ISSUER:
1758296341Sdelphij            case STORE_ATTR_SUBJECT:
1759296341Sdelphij                if (X509_NAME_cmp(a->values[i].dn, b->values[i].dn))
1760296341Sdelphij                    return 0;
1761296341Sdelphij                break;
1762296341Sdelphij            case STORE_ATTR_SERIAL:
1763296341Sdelphij                if (BN_cmp(a->values[i].number, b->values[i].number))
1764296341Sdelphij                    return 0;
1765296341Sdelphij                break;
1766296341Sdelphij            default:
1767296341Sdelphij                break;
1768296341Sdelphij            }
1769296341Sdelphij        }
1770160814Ssimon
1771296341Sdelphij    return 1;
1772296341Sdelphij}
1773