1160814Ssimon/* crypto/store/str_lib.c -*- mode:C; c-file-style: "eay" -*- */
2296465Sdelphij/*
3296465Sdelphij * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
4296465Sdelphij * 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
14296465Sdelphij *    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
64296465Sdelphij# include <openssl/engine.h>
65160814Ssimon#endif
66160814Ssimon#include <openssl/sha.h>
67160814Ssimon#include <openssl/x509.h>
68160814Ssimon#include "str_locl.h"
69160814Ssimon
70296465Sdelphijconst char *const STORE_object_type_string[STORE_OBJECT_TYPE_NUM + 1] = {
71296465Sdelphij    0,
72296465Sdelphij    "X.509 Certificate",
73296465Sdelphij    "X.509 CRL",
74296465Sdelphij    "Private Key",
75296465Sdelphij    "Public Key",
76296465Sdelphij    "Number",
77296465Sdelphij    "Arbitrary Data"
78296465Sdelphij};
79160814Ssimon
80296465Sdelphijconst int STORE_param_sizes[STORE_PARAM_TYPE_NUM + 1] = {
81296465Sdelphij    0,
82296465Sdelphij    sizeof(int),                /* EVP_TYPE */
83296465Sdelphij    sizeof(size_t),             /* BITS */
84296465Sdelphij    -1,                         /* KEY_PARAMETERS */
85296465Sdelphij    0                           /* KEY_NO_PARAMETERS */
86296465Sdelphij};
87160814Ssimon
88296465Sdelphijconst int STORE_attr_sizes[STORE_ATTR_TYPE_NUM + 1] = {
89296465Sdelphij    0,
90296465Sdelphij    -1,                         /* FRIENDLYNAME: C string */
91296465Sdelphij    SHA_DIGEST_LENGTH,          /* KEYID: SHA1 digest, 160 bits */
92296465Sdelphij    SHA_DIGEST_LENGTH,          /* ISSUERKEYID: SHA1 digest, 160 bits */
93296465Sdelphij    SHA_DIGEST_LENGTH,          /* SUBJECTKEYID: SHA1 digest, 160 bits */
94296465Sdelphij    SHA_DIGEST_LENGTH,          /* ISSUERSERIALHASH: SHA1 digest, 160 bits */
95296465Sdelphij    sizeof(X509_NAME *),        /* ISSUER: X509_NAME * */
96296465Sdelphij    sizeof(BIGNUM *),           /* SERIAL: BIGNUM * */
97296465Sdelphij    sizeof(X509_NAME *),        /* SUBJECT: X509_NAME * */
98296465Sdelphij    SHA_DIGEST_LENGTH,          /* CERTHASH: SHA1 digest, 160 bits */
99296465Sdelphij    -1,                         /* EMAIL: C string */
100296465Sdelphij    -1,                         /* FILENAME: C string */
101296465Sdelphij};
102160814Ssimon
103160814SsimonSTORE *STORE_new_method(const STORE_METHOD *method)
104296465Sdelphij{
105296465Sdelphij    STORE *ret;
106160814Ssimon
107296465Sdelphij    if (method == NULL) {
108296465Sdelphij        STOREerr(STORE_F_STORE_NEW_METHOD, ERR_R_PASSED_NULL_PARAMETER);
109296465Sdelphij        return NULL;
110296465Sdelphij    }
111160814Ssimon
112296465Sdelphij    ret = (STORE *)OPENSSL_malloc(sizeof(STORE));
113296465Sdelphij    if (ret == NULL) {
114296465Sdelphij        STOREerr(STORE_F_STORE_NEW_METHOD, ERR_R_MALLOC_FAILURE);
115296465Sdelphij        return NULL;
116296465Sdelphij    }
117160814Ssimon
118296465Sdelphij    ret->meth = method;
119160814Ssimon
120296465Sdelphij    CRYPTO_new_ex_data(CRYPTO_EX_INDEX_STORE, ret, &ret->ex_data);
121296465Sdelphij    if (ret->meth->init && !ret->meth->init(ret)) {
122296465Sdelphij        STORE_free(ret);
123296465Sdelphij        ret = NULL;
124296465Sdelphij    }
125296465Sdelphij    return ret;
126296465Sdelphij}
127160814Ssimon
128160814SsimonSTORE *STORE_new_engine(ENGINE *engine)
129296465Sdelphij{
130296465Sdelphij    STORE *ret = NULL;
131296465Sdelphij    ENGINE *e = engine;
132296465Sdelphij    const STORE_METHOD *meth = 0;
133160814Ssimon
134160814Ssimon#ifdef OPENSSL_NO_ENGINE
135296465Sdelphij    e = NULL;
136160814Ssimon#else
137296465Sdelphij    if (engine) {
138296465Sdelphij        if (!ENGINE_init(engine)) {
139296465Sdelphij            STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_ENGINE_LIB);
140296465Sdelphij            return NULL;
141296465Sdelphij        }
142296465Sdelphij        e = engine;
143296465Sdelphij    } else {
144296465Sdelphij        STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
145296465Sdelphij        return NULL;
146296465Sdelphij    }
147296465Sdelphij    if (e) {
148296465Sdelphij        meth = ENGINE_get_STORE(e);
149296465Sdelphij        if (!meth) {
150296465Sdelphij            STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_ENGINE_LIB);
151296465Sdelphij            ENGINE_finish(e);
152296465Sdelphij            return NULL;
153296465Sdelphij        }
154296465Sdelphij    }
155160814Ssimon#endif
156160814Ssimon
157296465Sdelphij    ret = STORE_new_method(meth);
158296465Sdelphij    if (ret == NULL) {
159296465Sdelphij        STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_STORE_LIB);
160296465Sdelphij        return NULL;
161296465Sdelphij    }
162160814Ssimon
163296465Sdelphij    ret->engine = e;
164160814Ssimon
165296465Sdelphij    return (ret);
166296465Sdelphij}
167160814Ssimon
168160814Ssimonvoid STORE_free(STORE *store)
169296465Sdelphij{
170296465Sdelphij    if (store == NULL)
171296465Sdelphij        return;
172296465Sdelphij    if (store->meth->clean)
173296465Sdelphij        store->meth->clean(store);
174296465Sdelphij    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_STORE, store, &store->ex_data);
175296465Sdelphij    OPENSSL_free(store);
176296465Sdelphij}
177160814Ssimon
178296465Sdelphijint STORE_ctrl(STORE *store, int cmd, long i, void *p, void (*f) (void))
179296465Sdelphij{
180296465Sdelphij    if (store == NULL) {
181296465Sdelphij        STOREerr(STORE_F_STORE_CTRL, ERR_R_PASSED_NULL_PARAMETER);
182296465Sdelphij        return 0;
183296465Sdelphij    }
184296465Sdelphij    if (store->meth->ctrl)
185296465Sdelphij        return store->meth->ctrl(store, cmd, i, p, f);
186296465Sdelphij    STOREerr(STORE_F_STORE_CTRL, STORE_R_NO_CONTROL_FUNCTION);
187296465Sdelphij    return 0;
188296465Sdelphij}
189160814Ssimon
190160814Ssimonint STORE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
191296465Sdelphij                           CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
192296465Sdelphij{
193296465Sdelphij    return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_STORE, argl, argp,
194296465Sdelphij                                   new_func, dup_func, free_func);
195296465Sdelphij}
196160814Ssimon
197160814Ssimonint STORE_set_ex_data(STORE *r, int idx, void *arg)
198296465Sdelphij{
199296465Sdelphij    return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
200296465Sdelphij}
201160814Ssimon
202160814Ssimonvoid *STORE_get_ex_data(STORE *r, int idx)
203296465Sdelphij{
204296465Sdelphij    return (CRYPTO_get_ex_data(&r->ex_data, idx));
205296465Sdelphij}
206160814Ssimon
207160814Ssimonconst STORE_METHOD *STORE_get_method(STORE *store)
208296465Sdelphij{
209296465Sdelphij    return store->meth;
210296465Sdelphij}
211160814Ssimon
212160814Ssimonconst STORE_METHOD *STORE_set_method(STORE *store, const STORE_METHOD *meth)
213296465Sdelphij{
214296465Sdelphij    store->meth = meth;
215296465Sdelphij    return store->meth;
216296465Sdelphij}
217160814Ssimon
218160814Ssimon/* API helpers */
219160814Ssimon
220160814Ssimon#define check_store(s,fncode,fnname,fnerrcode) \
221296465Sdelphij        do \
222296465Sdelphij                { \
223296465Sdelphij                if ((s) == NULL || (s)->meth == NULL) \
224296465Sdelphij                        { \
225296465Sdelphij                        STOREerr((fncode), ERR_R_PASSED_NULL_PARAMETER); \
226296465Sdelphij                        return 0; \
227296465Sdelphij                        } \
228296465Sdelphij                if ((s)->meth->fnname == NULL) \
229296465Sdelphij                        { \
230296465Sdelphij                        STOREerr((fncode), (fnerrcode)); \
231296465Sdelphij                        return 0; \
232296465Sdelphij                        } \
233296465Sdelphij                } \
234296465Sdelphij        while(0)
235160814Ssimon
236160814Ssimon/* API functions */
237160814Ssimon
238160814SsimonX509 *STORE_get_certificate(STORE *s, OPENSSL_ITEM attributes[],
239296465Sdelphij                            OPENSSL_ITEM parameters[])
240296465Sdelphij{
241296465Sdelphij    STORE_OBJECT *object;
242296465Sdelphij    X509 *x;
243160814Ssimon
244296465Sdelphij    check_store(s, STORE_F_STORE_GET_CERTIFICATE,
245296465Sdelphij                get_object, STORE_R_NO_GET_OBJECT_FUNCTION);
246160814Ssimon
247296465Sdelphij    object = s->meth->get_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
248296465Sdelphij                                 attributes, parameters);
249296465Sdelphij    if (!object || !object->data.x509.certificate) {
250296465Sdelphij        STOREerr(STORE_F_STORE_GET_CERTIFICATE,
251296465Sdelphij                 STORE_R_FAILED_GETTING_CERTIFICATE);
252296465Sdelphij        return 0;
253296465Sdelphij    }
254296465Sdelphij    CRYPTO_add(&object->data.x509.certificate->references, 1,
255296465Sdelphij               CRYPTO_LOCK_X509);
256160814Ssimon#ifdef REF_PRINT
257296465Sdelphij    REF_PRINT("X509", data);
258160814Ssimon#endif
259296465Sdelphij    x = object->data.x509.certificate;
260296465Sdelphij    STORE_OBJECT_free(object);
261296465Sdelphij    return x;
262296465Sdelphij}
263160814Ssimon
264160814Ssimonint STORE_store_certificate(STORE *s, X509 *data, OPENSSL_ITEM attributes[],
265296465Sdelphij                            OPENSSL_ITEM parameters[])
266296465Sdelphij{
267296465Sdelphij    STORE_OBJECT *object;
268296465Sdelphij    int i;
269160814Ssimon
270296465Sdelphij    check_store(s, STORE_F_STORE_CERTIFICATE,
271296465Sdelphij                store_object, STORE_R_NO_STORE_OBJECT_FUNCTION);
272160814Ssimon
273296465Sdelphij    object = STORE_OBJECT_new();
274296465Sdelphij    if (!object) {
275296465Sdelphij        STOREerr(STORE_F_STORE_STORE_CERTIFICATE, ERR_R_MALLOC_FAILURE);
276296465Sdelphij        return 0;
277296465Sdelphij    }
278296465Sdelphij
279296465Sdelphij    CRYPTO_add(&data->references, 1, CRYPTO_LOCK_X509);
280160814Ssimon#ifdef REF_PRINT
281296465Sdelphij    REF_PRINT("X509", data);
282160814Ssimon#endif
283296465Sdelphij    object->data.x509.certificate = data;
284160814Ssimon
285296465Sdelphij    i = s->meth->store_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
286296465Sdelphij                              object, attributes, parameters);
287160814Ssimon
288296465Sdelphij    STORE_OBJECT_free(object);
289160814Ssimon
290296465Sdelphij    if (!i) {
291296465Sdelphij        STOREerr(STORE_F_STORE_STORE_CERTIFICATE,
292296465Sdelphij                 STORE_R_FAILED_STORING_CERTIFICATE);
293296465Sdelphij        return 0;
294296465Sdelphij    }
295296465Sdelphij    return 1;
296296465Sdelphij}
297160814Ssimon
298160814Ssimonint STORE_modify_certificate(STORE *s, OPENSSL_ITEM search_attributes[],
299296465Sdelphij                             OPENSSL_ITEM add_attributes[],
300296465Sdelphij                             OPENSSL_ITEM modify_attributes[],
301296465Sdelphij                             OPENSSL_ITEM delete_attributes[],
302296465Sdelphij                             OPENSSL_ITEM parameters[])
303296465Sdelphij{
304296465Sdelphij    check_store(s, STORE_F_STORE_MODIFY_CERTIFICATE,
305296465Sdelphij                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
306160814Ssimon
307296465Sdelphij    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
308296465Sdelphij                                search_attributes, add_attributes,
309296465Sdelphij                                modify_attributes, delete_attributes,
310296465Sdelphij                                parameters)) {
311296465Sdelphij        STOREerr(STORE_F_STORE_MODIFY_CERTIFICATE,
312296465Sdelphij                 STORE_R_FAILED_MODIFYING_CERTIFICATE);
313296465Sdelphij        return 0;
314296465Sdelphij    }
315296465Sdelphij    return 1;
316296465Sdelphij}
317160814Ssimon
318160814Ssimonint STORE_revoke_certificate(STORE *s, OPENSSL_ITEM attributes[],
319296465Sdelphij                             OPENSSL_ITEM parameters[])
320296465Sdelphij{
321296465Sdelphij    check_store(s, STORE_F_STORE_REVOKE_CERTIFICATE,
322296465Sdelphij                revoke_object, STORE_R_NO_REVOKE_OBJECT_FUNCTION);
323160814Ssimon
324296465Sdelphij    if (!s->meth->revoke_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
325296465Sdelphij                                attributes, parameters)) {
326296465Sdelphij        STOREerr(STORE_F_STORE_REVOKE_CERTIFICATE,
327296465Sdelphij                 STORE_R_FAILED_REVOKING_CERTIFICATE);
328296465Sdelphij        return 0;
329296465Sdelphij    }
330296465Sdelphij    return 1;
331296465Sdelphij}
332160814Ssimon
333160814Ssimonint STORE_delete_certificate(STORE *s, OPENSSL_ITEM attributes[],
334296465Sdelphij                             OPENSSL_ITEM parameters[])
335296465Sdelphij{
336296465Sdelphij    check_store(s, STORE_F_STORE_DELETE_CERTIFICATE,
337296465Sdelphij                delete_object, STORE_R_NO_DELETE_OBJECT_FUNCTION);
338160814Ssimon
339296465Sdelphij    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
340296465Sdelphij                                attributes, parameters)) {
341296465Sdelphij        STOREerr(STORE_F_STORE_DELETE_CERTIFICATE,
342296465Sdelphij                 STORE_R_FAILED_DELETING_CERTIFICATE);
343296465Sdelphij        return 0;
344296465Sdelphij    }
345296465Sdelphij    return 1;
346296465Sdelphij}
347160814Ssimon
348160814Ssimonvoid *STORE_list_certificate_start(STORE *s, OPENSSL_ITEM attributes[],
349296465Sdelphij                                   OPENSSL_ITEM parameters[])
350296465Sdelphij{
351296465Sdelphij    void *handle;
352160814Ssimon
353296465Sdelphij    check_store(s, STORE_F_STORE_LIST_CERTIFICATE_START,
354296465Sdelphij                list_object_start, STORE_R_NO_LIST_OBJECT_START_FUNCTION);
355160814Ssimon
356296465Sdelphij    handle = s->meth->list_object_start(s,
357296465Sdelphij                                        STORE_OBJECT_TYPE_X509_CERTIFICATE,
358296465Sdelphij                                        attributes, parameters);
359296465Sdelphij    if (!handle) {
360296465Sdelphij        STOREerr(STORE_F_STORE_LIST_CERTIFICATE_START,
361296465Sdelphij                 STORE_R_FAILED_LISTING_CERTIFICATES);
362296465Sdelphij        return 0;
363296465Sdelphij    }
364296465Sdelphij    return handle;
365296465Sdelphij}
366160814Ssimon
367160814SsimonX509 *STORE_list_certificate_next(STORE *s, void *handle)
368296465Sdelphij{
369296465Sdelphij    STORE_OBJECT *object;
370296465Sdelphij    X509 *x;
371160814Ssimon
372296465Sdelphij    check_store(s, STORE_F_STORE_LIST_CERTIFICATE_NEXT,
373296465Sdelphij                list_object_next, STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
374160814Ssimon
375296465Sdelphij    object = s->meth->list_object_next(s, handle);
376296465Sdelphij    if (!object || !object->data.x509.certificate) {
377296465Sdelphij        STOREerr(STORE_F_STORE_LIST_CERTIFICATE_NEXT,
378296465Sdelphij                 STORE_R_FAILED_LISTING_CERTIFICATES);
379296465Sdelphij        return 0;
380296465Sdelphij    }
381296465Sdelphij    CRYPTO_add(&object->data.x509.certificate->references, 1,
382296465Sdelphij               CRYPTO_LOCK_X509);
383160814Ssimon#ifdef REF_PRINT
384296465Sdelphij    REF_PRINT("X509", data);
385160814Ssimon#endif
386296465Sdelphij    x = object->data.x509.certificate;
387296465Sdelphij    STORE_OBJECT_free(object);
388296465Sdelphij    return x;
389296465Sdelphij}
390160814Ssimon
391160814Ssimonint STORE_list_certificate_end(STORE *s, void *handle)
392296465Sdelphij{
393296465Sdelphij    check_store(s, STORE_F_STORE_LIST_CERTIFICATE_END,
394296465Sdelphij                list_object_end, STORE_R_NO_LIST_OBJECT_END_FUNCTION);
395160814Ssimon
396296465Sdelphij    if (!s->meth->list_object_end(s, handle)) {
397296465Sdelphij        STOREerr(STORE_F_STORE_LIST_CERTIFICATE_END,
398296465Sdelphij                 STORE_R_FAILED_LISTING_CERTIFICATES);
399296465Sdelphij        return 0;
400296465Sdelphij    }
401296465Sdelphij    return 1;
402296465Sdelphij}
403160814Ssimon
404160814Ssimonint STORE_list_certificate_endp(STORE *s, void *handle)
405296465Sdelphij{
406296465Sdelphij    check_store(s, STORE_F_STORE_LIST_CERTIFICATE_ENDP,
407296465Sdelphij                list_object_endp, STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
408160814Ssimon
409296465Sdelphij    if (!s->meth->list_object_endp(s, handle)) {
410296465Sdelphij        STOREerr(STORE_F_STORE_LIST_CERTIFICATE_ENDP,
411296465Sdelphij                 STORE_R_FAILED_LISTING_CERTIFICATES);
412296465Sdelphij        return 0;
413296465Sdelphij    }
414296465Sdelphij    return 1;
415296465Sdelphij}
416160814Ssimon
417160814SsimonEVP_PKEY *STORE_generate_key(STORE *s, OPENSSL_ITEM attributes[],
418296465Sdelphij                             OPENSSL_ITEM parameters[])
419296465Sdelphij{
420296465Sdelphij    STORE_OBJECT *object;
421296465Sdelphij    EVP_PKEY *pkey;
422160814Ssimon
423296465Sdelphij    check_store(s, STORE_F_STORE_GENERATE_KEY,
424296465Sdelphij                generate_object, STORE_R_NO_GENERATE_OBJECT_FUNCTION);
425160814Ssimon
426296465Sdelphij    object = s->meth->generate_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
427296465Sdelphij                                      attributes, parameters);
428296465Sdelphij    if (!object || !object->data.key) {
429296465Sdelphij        STOREerr(STORE_F_STORE_GENERATE_KEY, STORE_R_FAILED_GENERATING_KEY);
430296465Sdelphij        return 0;
431296465Sdelphij    }
432296465Sdelphij    CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
433160814Ssimon#ifdef REF_PRINT
434296465Sdelphij    REF_PRINT("EVP_PKEY", data);
435160814Ssimon#endif
436296465Sdelphij    pkey = object->data.key;
437296465Sdelphij    STORE_OBJECT_free(object);
438296465Sdelphij    return pkey;
439296465Sdelphij}
440160814Ssimon
441160814SsimonEVP_PKEY *STORE_get_private_key(STORE *s, OPENSSL_ITEM attributes[],
442296465Sdelphij                                OPENSSL_ITEM parameters[])
443296465Sdelphij{
444296465Sdelphij    STORE_OBJECT *object;
445296465Sdelphij    EVP_PKEY *pkey;
446160814Ssimon
447296465Sdelphij    check_store(s, STORE_F_STORE_GET_PRIVATE_KEY,
448296465Sdelphij                get_object, STORE_R_NO_GET_OBJECT_FUNCTION);
449160814Ssimon
450296465Sdelphij    object = s->meth->get_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
451296465Sdelphij                                 attributes, parameters);
452296465Sdelphij    if (!object || !object->data.key || !object->data.key) {
453296465Sdelphij        STOREerr(STORE_F_STORE_GET_PRIVATE_KEY, STORE_R_FAILED_GETTING_KEY);
454296465Sdelphij        return 0;
455296465Sdelphij    }
456296465Sdelphij    CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
457160814Ssimon#ifdef REF_PRINT
458296465Sdelphij    REF_PRINT("EVP_PKEY", data);
459160814Ssimon#endif
460296465Sdelphij    pkey = object->data.key;
461296465Sdelphij    STORE_OBJECT_free(object);
462296465Sdelphij    return pkey;
463296465Sdelphij}
464160814Ssimon
465296465Sdelphijint STORE_store_private_key(STORE *s, EVP_PKEY *data,
466296465Sdelphij                            OPENSSL_ITEM attributes[],
467296465Sdelphij                            OPENSSL_ITEM parameters[])
468296465Sdelphij{
469296465Sdelphij    STORE_OBJECT *object;
470296465Sdelphij    int i;
471160814Ssimon
472296465Sdelphij    check_store(s, STORE_F_STORE_STORE_PRIVATE_KEY,
473296465Sdelphij                store_object, STORE_R_NO_STORE_OBJECT_FUNCTION);
474160814Ssimon
475296465Sdelphij    object = STORE_OBJECT_new();
476296465Sdelphij    if (!object) {
477296465Sdelphij        STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY, ERR_R_MALLOC_FAILURE);
478296465Sdelphij        return 0;
479296465Sdelphij    }
480296465Sdelphij    object->data.key = EVP_PKEY_new();
481296465Sdelphij    if (!object->data.key) {
482296465Sdelphij        STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY, ERR_R_MALLOC_FAILURE);
483296465Sdelphij        return 0;
484296465Sdelphij    }
485296465Sdelphij
486296465Sdelphij    CRYPTO_add(&data->references, 1, CRYPTO_LOCK_EVP_PKEY);
487160814Ssimon#ifdef REF_PRINT
488296465Sdelphij    REF_PRINT("EVP_PKEY", data);
489160814Ssimon#endif
490296465Sdelphij    object->data.key = data;
491160814Ssimon
492296465Sdelphij    i = s->meth->store_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY, object,
493296465Sdelphij                              attributes, parameters);
494160814Ssimon
495296465Sdelphij    STORE_OBJECT_free(object);
496160814Ssimon
497296465Sdelphij    if (!i) {
498296465Sdelphij        STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY, STORE_R_FAILED_STORING_KEY);
499296465Sdelphij        return 0;
500296465Sdelphij    }
501296465Sdelphij    return i;
502296465Sdelphij}
503160814Ssimon
504160814Ssimonint STORE_modify_private_key(STORE *s, OPENSSL_ITEM search_attributes[],
505296465Sdelphij                             OPENSSL_ITEM add_attributes[],
506296465Sdelphij                             OPENSSL_ITEM modify_attributes[],
507296465Sdelphij                             OPENSSL_ITEM delete_attributes[],
508296465Sdelphij                             OPENSSL_ITEM parameters[])
509296465Sdelphij{
510296465Sdelphij    check_store(s, STORE_F_STORE_MODIFY_PRIVATE_KEY,
511296465Sdelphij                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
512160814Ssimon
513296465Sdelphij    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
514296465Sdelphij                                search_attributes, add_attributes,
515296465Sdelphij                                modify_attributes, delete_attributes,
516296465Sdelphij                                parameters)) {
517296465Sdelphij        STOREerr(STORE_F_STORE_MODIFY_PRIVATE_KEY,
518296465Sdelphij                 STORE_R_FAILED_MODIFYING_PRIVATE_KEY);
519296465Sdelphij        return 0;
520296465Sdelphij    }
521296465Sdelphij    return 1;
522296465Sdelphij}
523160814Ssimon
524160814Ssimonint STORE_revoke_private_key(STORE *s, OPENSSL_ITEM attributes[],
525296465Sdelphij                             OPENSSL_ITEM parameters[])
526296465Sdelphij{
527296465Sdelphij    int i;
528160814Ssimon
529296465Sdelphij    check_store(s, STORE_F_STORE_REVOKE_PRIVATE_KEY,
530296465Sdelphij                revoke_object, STORE_R_NO_REVOKE_OBJECT_FUNCTION);
531160814Ssimon
532296465Sdelphij    i = s->meth->revoke_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
533296465Sdelphij                               attributes, parameters);
534160814Ssimon
535296465Sdelphij    if (!i) {
536296465Sdelphij        STOREerr(STORE_F_STORE_REVOKE_PRIVATE_KEY,
537296465Sdelphij                 STORE_R_FAILED_REVOKING_KEY);
538296465Sdelphij        return 0;
539296465Sdelphij    }
540296465Sdelphij    return i;
541296465Sdelphij}
542160814Ssimon
543160814Ssimonint STORE_delete_private_key(STORE *s, OPENSSL_ITEM attributes[],
544296465Sdelphij                             OPENSSL_ITEM parameters[])
545296465Sdelphij{
546296465Sdelphij    check_store(s, STORE_F_STORE_DELETE_PRIVATE_KEY,
547296465Sdelphij                delete_object, STORE_R_NO_DELETE_OBJECT_FUNCTION);
548160814Ssimon
549296465Sdelphij    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
550296465Sdelphij                                attributes, parameters)) {
551296465Sdelphij        STOREerr(STORE_F_STORE_DELETE_PRIVATE_KEY,
552296465Sdelphij                 STORE_R_FAILED_DELETING_KEY);
553296465Sdelphij        return 0;
554296465Sdelphij    }
555296465Sdelphij    return 1;
556296465Sdelphij}
557296465Sdelphij
558160814Ssimonvoid *STORE_list_private_key_start(STORE *s, OPENSSL_ITEM attributes[],
559296465Sdelphij                                   OPENSSL_ITEM parameters[])
560296465Sdelphij{
561296465Sdelphij    void *handle;
562160814Ssimon
563296465Sdelphij    check_store(s, STORE_F_STORE_LIST_PRIVATE_KEY_START,
564296465Sdelphij                list_object_start, STORE_R_NO_LIST_OBJECT_START_FUNCTION);
565160814Ssimon
566296465Sdelphij    handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
567296465Sdelphij                                        attributes, parameters);
568296465Sdelphij    if (!handle) {
569296465Sdelphij        STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_START,
570296465Sdelphij                 STORE_R_FAILED_LISTING_KEYS);
571296465Sdelphij        return 0;
572296465Sdelphij    }
573296465Sdelphij    return handle;
574296465Sdelphij}
575160814Ssimon
576160814SsimonEVP_PKEY *STORE_list_private_key_next(STORE *s, void *handle)
577296465Sdelphij{
578296465Sdelphij    STORE_OBJECT *object;
579296465Sdelphij    EVP_PKEY *pkey;
580160814Ssimon
581296465Sdelphij    check_store(s, STORE_F_STORE_LIST_PRIVATE_KEY_NEXT,
582296465Sdelphij                list_object_next, STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
583160814Ssimon
584296465Sdelphij    object = s->meth->list_object_next(s, handle);
585296465Sdelphij    if (!object || !object->data.key || !object->data.key) {
586296465Sdelphij        STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_NEXT,
587296465Sdelphij                 STORE_R_FAILED_LISTING_KEYS);
588296465Sdelphij        return 0;
589296465Sdelphij    }
590296465Sdelphij    CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
591160814Ssimon#ifdef REF_PRINT
592296465Sdelphij    REF_PRINT("EVP_PKEY", data);
593160814Ssimon#endif
594296465Sdelphij    pkey = object->data.key;
595296465Sdelphij    STORE_OBJECT_free(object);
596296465Sdelphij    return pkey;
597296465Sdelphij}
598160814Ssimon
599160814Ssimonint STORE_list_private_key_end(STORE *s, void *handle)
600296465Sdelphij{
601296465Sdelphij    check_store(s, STORE_F_STORE_LIST_PRIVATE_KEY_END,
602296465Sdelphij                list_object_end, STORE_R_NO_LIST_OBJECT_END_FUNCTION);
603160814Ssimon
604296465Sdelphij    if (!s->meth->list_object_end(s, handle)) {
605296465Sdelphij        STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_END,
606296465Sdelphij                 STORE_R_FAILED_LISTING_KEYS);
607296465Sdelphij        return 0;
608296465Sdelphij    }
609296465Sdelphij    return 1;
610296465Sdelphij}
611160814Ssimon
612160814Ssimonint STORE_list_private_key_endp(STORE *s, void *handle)
613296465Sdelphij{
614296465Sdelphij    check_store(s, STORE_F_STORE_LIST_PRIVATE_KEY_ENDP,
615296465Sdelphij                list_object_endp, STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
616160814Ssimon
617296465Sdelphij    if (!s->meth->list_object_endp(s, handle)) {
618296465Sdelphij        STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_ENDP,
619296465Sdelphij                 STORE_R_FAILED_LISTING_KEYS);
620296465Sdelphij        return 0;
621296465Sdelphij    }
622296465Sdelphij    return 1;
623296465Sdelphij}
624160814Ssimon
625160814SsimonEVP_PKEY *STORE_get_public_key(STORE *s, OPENSSL_ITEM attributes[],
626296465Sdelphij                               OPENSSL_ITEM parameters[])
627296465Sdelphij{
628296465Sdelphij    STORE_OBJECT *object;
629296465Sdelphij    EVP_PKEY *pkey;
630160814Ssimon
631296465Sdelphij    check_store(s, STORE_F_STORE_GET_PUBLIC_KEY,
632296465Sdelphij                get_object, STORE_R_NO_GET_OBJECT_FUNCTION);
633160814Ssimon
634296465Sdelphij    object = s->meth->get_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
635296465Sdelphij                                 attributes, parameters);
636296465Sdelphij    if (!object || !object->data.key || !object->data.key) {
637296465Sdelphij        STOREerr(STORE_F_STORE_GET_PUBLIC_KEY, STORE_R_FAILED_GETTING_KEY);
638296465Sdelphij        return 0;
639296465Sdelphij    }
640296465Sdelphij    CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
641160814Ssimon#ifdef REF_PRINT
642296465Sdelphij    REF_PRINT("EVP_PKEY", data);
643160814Ssimon#endif
644296465Sdelphij    pkey = object->data.key;
645296465Sdelphij    STORE_OBJECT_free(object);
646296465Sdelphij    return pkey;
647296465Sdelphij}
648160814Ssimon
649296465Sdelphijint STORE_store_public_key(STORE *s, EVP_PKEY *data,
650296465Sdelphij                           OPENSSL_ITEM attributes[],
651296465Sdelphij                           OPENSSL_ITEM parameters[])
652296465Sdelphij{
653296465Sdelphij    STORE_OBJECT *object;
654296465Sdelphij    int i;
655160814Ssimon
656296465Sdelphij    check_store(s, STORE_F_STORE_STORE_PUBLIC_KEY,
657296465Sdelphij                store_object, STORE_R_NO_STORE_OBJECT_FUNCTION);
658160814Ssimon
659296465Sdelphij    object = STORE_OBJECT_new();
660296465Sdelphij    if (!object) {
661296465Sdelphij        STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY, ERR_R_MALLOC_FAILURE);
662296465Sdelphij        return 0;
663296465Sdelphij    }
664296465Sdelphij    object->data.key = EVP_PKEY_new();
665296465Sdelphij    if (!object->data.key) {
666296465Sdelphij        STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY, ERR_R_MALLOC_FAILURE);
667296465Sdelphij        return 0;
668296465Sdelphij    }
669296465Sdelphij
670296465Sdelphij    CRYPTO_add(&data->references, 1, CRYPTO_LOCK_EVP_PKEY);
671160814Ssimon#ifdef REF_PRINT
672296465Sdelphij    REF_PRINT("EVP_PKEY", data);
673160814Ssimon#endif
674296465Sdelphij    object->data.key = data;
675160814Ssimon
676296465Sdelphij    i = s->meth->store_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY, object,
677296465Sdelphij                              attributes, parameters);
678160814Ssimon
679296465Sdelphij    STORE_OBJECT_free(object);
680160814Ssimon
681296465Sdelphij    if (!i) {
682296465Sdelphij        STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY, STORE_R_FAILED_STORING_KEY);
683296465Sdelphij        return 0;
684296465Sdelphij    }
685296465Sdelphij    return i;
686296465Sdelphij}
687160814Ssimon
688160814Ssimonint STORE_modify_public_key(STORE *s, OPENSSL_ITEM search_attributes[],
689296465Sdelphij                            OPENSSL_ITEM add_attributes[],
690296465Sdelphij                            OPENSSL_ITEM modify_attributes[],
691296465Sdelphij                            OPENSSL_ITEM delete_attributes[],
692296465Sdelphij                            OPENSSL_ITEM parameters[])
693296465Sdelphij{
694296465Sdelphij    check_store(s, STORE_F_STORE_MODIFY_PUBLIC_KEY,
695296465Sdelphij                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
696160814Ssimon
697296465Sdelphij    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
698296465Sdelphij                                search_attributes, add_attributes,
699296465Sdelphij                                modify_attributes, delete_attributes,
700296465Sdelphij                                parameters)) {
701296465Sdelphij        STOREerr(STORE_F_STORE_MODIFY_PUBLIC_KEY,
702296465Sdelphij                 STORE_R_FAILED_MODIFYING_PUBLIC_KEY);
703296465Sdelphij        return 0;
704296465Sdelphij    }
705296465Sdelphij    return 1;
706296465Sdelphij}
707160814Ssimon
708160814Ssimonint STORE_revoke_public_key(STORE *s, OPENSSL_ITEM attributes[],
709296465Sdelphij                            OPENSSL_ITEM parameters[])
710296465Sdelphij{
711296465Sdelphij    int i;
712160814Ssimon
713296465Sdelphij    check_store(s, STORE_F_STORE_REVOKE_PUBLIC_KEY,
714296465Sdelphij                revoke_object, STORE_R_NO_REVOKE_OBJECT_FUNCTION);
715160814Ssimon
716296465Sdelphij    i = s->meth->revoke_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
717296465Sdelphij                               attributes, parameters);
718160814Ssimon
719296465Sdelphij    if (!i) {
720296465Sdelphij        STOREerr(STORE_F_STORE_REVOKE_PUBLIC_KEY,
721296465Sdelphij                 STORE_R_FAILED_REVOKING_KEY);
722296465Sdelphij        return 0;
723296465Sdelphij    }
724296465Sdelphij    return i;
725296465Sdelphij}
726160814Ssimon
727160814Ssimonint STORE_delete_public_key(STORE *s, OPENSSL_ITEM attributes[],
728296465Sdelphij                            OPENSSL_ITEM parameters[])
729296465Sdelphij{
730296465Sdelphij    check_store(s, STORE_F_STORE_DELETE_PUBLIC_KEY,
731296465Sdelphij                delete_object, STORE_R_NO_DELETE_OBJECT_FUNCTION);
732160814Ssimon
733296465Sdelphij    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
734296465Sdelphij                                attributes, parameters)) {
735296465Sdelphij        STOREerr(STORE_F_STORE_DELETE_PUBLIC_KEY,
736296465Sdelphij                 STORE_R_FAILED_DELETING_KEY);
737296465Sdelphij        return 0;
738296465Sdelphij    }
739296465Sdelphij    return 1;
740296465Sdelphij}
741296465Sdelphij
742160814Ssimonvoid *STORE_list_public_key_start(STORE *s, OPENSSL_ITEM attributes[],
743296465Sdelphij                                  OPENSSL_ITEM parameters[])
744296465Sdelphij{
745296465Sdelphij    void *handle;
746160814Ssimon
747296465Sdelphij    check_store(s, STORE_F_STORE_LIST_PUBLIC_KEY_START,
748296465Sdelphij                list_object_start, STORE_R_NO_LIST_OBJECT_START_FUNCTION);
749160814Ssimon
750296465Sdelphij    handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
751296465Sdelphij                                        attributes, parameters);
752296465Sdelphij    if (!handle) {
753296465Sdelphij        STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_START,
754296465Sdelphij                 STORE_R_FAILED_LISTING_KEYS);
755296465Sdelphij        return 0;
756296465Sdelphij    }
757296465Sdelphij    return handle;
758296465Sdelphij}
759160814Ssimon
760160814SsimonEVP_PKEY *STORE_list_public_key_next(STORE *s, void *handle)
761296465Sdelphij{
762296465Sdelphij    STORE_OBJECT *object;
763296465Sdelphij    EVP_PKEY *pkey;
764160814Ssimon
765296465Sdelphij    check_store(s, STORE_F_STORE_LIST_PUBLIC_KEY_NEXT,
766296465Sdelphij                list_object_next, STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
767160814Ssimon
768296465Sdelphij    object = s->meth->list_object_next(s, handle);
769296465Sdelphij    if (!object || !object->data.key || !object->data.key) {
770296465Sdelphij        STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_NEXT,
771296465Sdelphij                 STORE_R_FAILED_LISTING_KEYS);
772296465Sdelphij        return 0;
773296465Sdelphij    }
774296465Sdelphij    CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
775160814Ssimon#ifdef REF_PRINT
776296465Sdelphij    REF_PRINT("EVP_PKEY", data);
777160814Ssimon#endif
778296465Sdelphij    pkey = object->data.key;
779296465Sdelphij    STORE_OBJECT_free(object);
780296465Sdelphij    return pkey;
781296465Sdelphij}
782160814Ssimon
783160814Ssimonint STORE_list_public_key_end(STORE *s, void *handle)
784296465Sdelphij{
785296465Sdelphij    check_store(s, STORE_F_STORE_LIST_PUBLIC_KEY_END,
786296465Sdelphij                list_object_end, STORE_R_NO_LIST_OBJECT_END_FUNCTION);
787160814Ssimon
788296465Sdelphij    if (!s->meth->list_object_end(s, handle)) {
789296465Sdelphij        STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_END,
790296465Sdelphij                 STORE_R_FAILED_LISTING_KEYS);
791296465Sdelphij        return 0;
792296465Sdelphij    }
793296465Sdelphij    return 1;
794296465Sdelphij}
795160814Ssimon
796160814Ssimonint STORE_list_public_key_endp(STORE *s, void *handle)
797296465Sdelphij{
798296465Sdelphij    check_store(s, STORE_F_STORE_LIST_PUBLIC_KEY_ENDP,
799296465Sdelphij                list_object_endp, STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
800160814Ssimon
801296465Sdelphij    if (!s->meth->list_object_endp(s, handle)) {
802296465Sdelphij        STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_ENDP,
803296465Sdelphij                 STORE_R_FAILED_LISTING_KEYS);
804296465Sdelphij        return 0;
805296465Sdelphij    }
806296465Sdelphij    return 1;
807296465Sdelphij}
808160814Ssimon
809160814SsimonX509_CRL *STORE_generate_crl(STORE *s, OPENSSL_ITEM attributes[],
810296465Sdelphij                             OPENSSL_ITEM parameters[])
811296465Sdelphij{
812296465Sdelphij    STORE_OBJECT *object;
813296465Sdelphij    X509_CRL *crl;
814160814Ssimon
815296465Sdelphij    check_store(s, STORE_F_STORE_GENERATE_CRL,
816296465Sdelphij                generate_object, STORE_R_NO_GENERATE_CRL_FUNCTION);
817160814Ssimon
818296465Sdelphij    object = s->meth->generate_object(s, STORE_OBJECT_TYPE_X509_CRL,
819296465Sdelphij                                      attributes, parameters);
820296465Sdelphij    if (!object || !object->data.crl) {
821296465Sdelphij        STOREerr(STORE_F_STORE_GENERATE_CRL, STORE_R_FAILED_GENERATING_CRL);
822296465Sdelphij        return 0;
823296465Sdelphij    }
824296465Sdelphij    CRYPTO_add(&object->data.crl->references, 1, CRYPTO_LOCK_X509_CRL);
825160814Ssimon#ifdef REF_PRINT
826296465Sdelphij    REF_PRINT("X509_CRL", data);
827160814Ssimon#endif
828296465Sdelphij    crl = object->data.crl;
829296465Sdelphij    STORE_OBJECT_free(object);
830296465Sdelphij    return crl;
831296465Sdelphij}
832160814Ssimon
833160814SsimonX509_CRL *STORE_get_crl(STORE *s, OPENSSL_ITEM attributes[],
834296465Sdelphij                        OPENSSL_ITEM parameters[])
835296465Sdelphij{
836296465Sdelphij    STORE_OBJECT *object;
837296465Sdelphij    X509_CRL *crl;
838160814Ssimon
839296465Sdelphij    check_store(s, STORE_F_STORE_GET_CRL,
840296465Sdelphij                get_object, STORE_R_NO_GET_OBJECT_FUNCTION);
841160814Ssimon
842296465Sdelphij    object = s->meth->get_object(s, STORE_OBJECT_TYPE_X509_CRL,
843296465Sdelphij                                 attributes, parameters);
844296465Sdelphij    if (!object || !object->data.crl) {
845296465Sdelphij        STOREerr(STORE_F_STORE_GET_CRL, STORE_R_FAILED_GETTING_KEY);
846296465Sdelphij        return 0;
847296465Sdelphij    }
848296465Sdelphij    CRYPTO_add(&object->data.crl->references, 1, CRYPTO_LOCK_X509_CRL);
849160814Ssimon#ifdef REF_PRINT
850296465Sdelphij    REF_PRINT("X509_CRL", data);
851160814Ssimon#endif
852296465Sdelphij    crl = object->data.crl;
853296465Sdelphij    STORE_OBJECT_free(object);
854296465Sdelphij    return crl;
855296465Sdelphij}
856160814Ssimon
857160814Ssimonint STORE_store_crl(STORE *s, X509_CRL *data, OPENSSL_ITEM attributes[],
858296465Sdelphij                    OPENSSL_ITEM parameters[])
859296465Sdelphij{
860296465Sdelphij    STORE_OBJECT *object;
861296465Sdelphij    int i;
862160814Ssimon
863296465Sdelphij    check_store(s, STORE_F_STORE_STORE_CRL,
864296465Sdelphij                store_object, STORE_R_NO_STORE_OBJECT_FUNCTION);
865160814Ssimon
866296465Sdelphij    object = STORE_OBJECT_new();
867296465Sdelphij    if (!object) {
868296465Sdelphij        STOREerr(STORE_F_STORE_STORE_CRL, ERR_R_MALLOC_FAILURE);
869296465Sdelphij        return 0;
870296465Sdelphij    }
871296465Sdelphij
872296465Sdelphij    CRYPTO_add(&data->references, 1, CRYPTO_LOCK_X509_CRL);
873160814Ssimon#ifdef REF_PRINT
874296465Sdelphij    REF_PRINT("X509_CRL", data);
875160814Ssimon#endif
876296465Sdelphij    object->data.crl = data;
877160814Ssimon
878296465Sdelphij    i = s->meth->store_object(s, STORE_OBJECT_TYPE_X509_CRL, object,
879296465Sdelphij                              attributes, parameters);
880160814Ssimon
881296465Sdelphij    STORE_OBJECT_free(object);
882160814Ssimon
883296465Sdelphij    if (!i) {
884296465Sdelphij        STOREerr(STORE_F_STORE_STORE_CRL, STORE_R_FAILED_STORING_KEY);
885296465Sdelphij        return 0;
886296465Sdelphij    }
887296465Sdelphij    return i;
888296465Sdelphij}
889160814Ssimon
890160814Ssimonint STORE_modify_crl(STORE *s, OPENSSL_ITEM search_attributes[],
891296465Sdelphij                     OPENSSL_ITEM add_attributes[],
892296465Sdelphij                     OPENSSL_ITEM modify_attributes[],
893296465Sdelphij                     OPENSSL_ITEM delete_attributes[],
894296465Sdelphij                     OPENSSL_ITEM parameters[])
895296465Sdelphij{
896296465Sdelphij    check_store(s, STORE_F_STORE_MODIFY_CRL,
897296465Sdelphij                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
898160814Ssimon
899296465Sdelphij    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_X509_CRL,
900296465Sdelphij                                search_attributes, add_attributes,
901296465Sdelphij                                modify_attributes, delete_attributes,
902296465Sdelphij                                parameters)) {
903296465Sdelphij        STOREerr(STORE_F_STORE_MODIFY_CRL, STORE_R_FAILED_MODIFYING_CRL);
904296465Sdelphij        return 0;
905296465Sdelphij    }
906296465Sdelphij    return 1;
907296465Sdelphij}
908160814Ssimon
909160814Ssimonint STORE_delete_crl(STORE *s, OPENSSL_ITEM attributes[],
910296465Sdelphij                     OPENSSL_ITEM parameters[])
911296465Sdelphij{
912296465Sdelphij    check_store(s, STORE_F_STORE_DELETE_CRL,
913296465Sdelphij                delete_object, STORE_R_NO_DELETE_OBJECT_FUNCTION);
914160814Ssimon
915296465Sdelphij    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_X509_CRL,
916296465Sdelphij                                attributes, parameters)) {
917296465Sdelphij        STOREerr(STORE_F_STORE_DELETE_CRL, STORE_R_FAILED_DELETING_KEY);
918296465Sdelphij        return 0;
919296465Sdelphij    }
920296465Sdelphij    return 1;
921296465Sdelphij}
922296465Sdelphij
923160814Ssimonvoid *STORE_list_crl_start(STORE *s, OPENSSL_ITEM attributes[],
924296465Sdelphij                           OPENSSL_ITEM parameters[])
925296465Sdelphij{
926296465Sdelphij    void *handle;
927160814Ssimon
928296465Sdelphij    check_store(s, STORE_F_STORE_LIST_CRL_START,
929296465Sdelphij                list_object_start, STORE_R_NO_LIST_OBJECT_START_FUNCTION);
930160814Ssimon
931296465Sdelphij    handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_X509_CRL,
932296465Sdelphij                                        attributes, parameters);
933296465Sdelphij    if (!handle) {
934296465Sdelphij        STOREerr(STORE_F_STORE_LIST_CRL_START, STORE_R_FAILED_LISTING_KEYS);
935296465Sdelphij        return 0;
936296465Sdelphij    }
937296465Sdelphij    return handle;
938296465Sdelphij}
939160814Ssimon
940160814SsimonX509_CRL *STORE_list_crl_next(STORE *s, void *handle)
941296465Sdelphij{
942296465Sdelphij    STORE_OBJECT *object;
943296465Sdelphij    X509_CRL *crl;
944160814Ssimon
945296465Sdelphij    check_store(s, STORE_F_STORE_LIST_CRL_NEXT,
946296465Sdelphij                list_object_next, STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
947160814Ssimon
948296465Sdelphij    object = s->meth->list_object_next(s, handle);
949296465Sdelphij    if (!object || !object->data.crl) {
950296465Sdelphij        STOREerr(STORE_F_STORE_LIST_CRL_NEXT, STORE_R_FAILED_LISTING_KEYS);
951296465Sdelphij        return 0;
952296465Sdelphij    }
953296465Sdelphij    CRYPTO_add(&object->data.crl->references, 1, CRYPTO_LOCK_X509_CRL);
954160814Ssimon#ifdef REF_PRINT
955296465Sdelphij    REF_PRINT("X509_CRL", data);
956160814Ssimon#endif
957296465Sdelphij    crl = object->data.crl;
958296465Sdelphij    STORE_OBJECT_free(object);
959296465Sdelphij    return crl;
960296465Sdelphij}
961160814Ssimon
962160814Ssimonint STORE_list_crl_end(STORE *s, void *handle)
963296465Sdelphij{
964296465Sdelphij    check_store(s, STORE_F_STORE_LIST_CRL_END,
965296465Sdelphij                list_object_end, STORE_R_NO_LIST_OBJECT_END_FUNCTION);
966160814Ssimon
967296465Sdelphij    if (!s->meth->list_object_end(s, handle)) {
968296465Sdelphij        STOREerr(STORE_F_STORE_LIST_CRL_END, STORE_R_FAILED_LISTING_KEYS);
969296465Sdelphij        return 0;
970296465Sdelphij    }
971296465Sdelphij    return 1;
972296465Sdelphij}
973160814Ssimon
974160814Ssimonint STORE_list_crl_endp(STORE *s, void *handle)
975296465Sdelphij{
976296465Sdelphij    check_store(s, STORE_F_STORE_LIST_CRL_ENDP,
977296465Sdelphij                list_object_endp, STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
978160814Ssimon
979296465Sdelphij    if (!s->meth->list_object_endp(s, handle)) {
980296465Sdelphij        STOREerr(STORE_F_STORE_LIST_CRL_ENDP, STORE_R_FAILED_LISTING_KEYS);
981296465Sdelphij        return 0;
982296465Sdelphij    }
983296465Sdelphij    return 1;
984296465Sdelphij}
985160814Ssimon
986160814Ssimonint STORE_store_number(STORE *s, BIGNUM *data, OPENSSL_ITEM attributes[],
987296465Sdelphij                       OPENSSL_ITEM parameters[])
988296465Sdelphij{
989296465Sdelphij    STORE_OBJECT *object;
990296465Sdelphij    int i;
991160814Ssimon
992296465Sdelphij    check_store(s, STORE_F_STORE_STORE_NUMBER,
993296465Sdelphij                store_object, STORE_R_NO_STORE_OBJECT_NUMBER_FUNCTION);
994160814Ssimon
995296465Sdelphij    object = STORE_OBJECT_new();
996296465Sdelphij    if (!object) {
997296465Sdelphij        STOREerr(STORE_F_STORE_STORE_NUMBER, ERR_R_MALLOC_FAILURE);
998296465Sdelphij        return 0;
999296465Sdelphij    }
1000160814Ssimon
1001296465Sdelphij    object->data.number = data;
1002160814Ssimon
1003296465Sdelphij    i = s->meth->store_object(s, STORE_OBJECT_TYPE_NUMBER, object,
1004296465Sdelphij                              attributes, parameters);
1005160814Ssimon
1006296465Sdelphij    STORE_OBJECT_free(object);
1007160814Ssimon
1008296465Sdelphij    if (!i) {
1009296465Sdelphij        STOREerr(STORE_F_STORE_STORE_NUMBER, STORE_R_FAILED_STORING_NUMBER);
1010296465Sdelphij        return 0;
1011296465Sdelphij    }
1012296465Sdelphij    return 1;
1013296465Sdelphij}
1014296465Sdelphij
1015160814Ssimonint STORE_modify_number(STORE *s, OPENSSL_ITEM search_attributes[],
1016296465Sdelphij                        OPENSSL_ITEM add_attributes[],
1017296465Sdelphij                        OPENSSL_ITEM modify_attributes[],
1018296465Sdelphij                        OPENSSL_ITEM delete_attributes[],
1019296465Sdelphij                        OPENSSL_ITEM parameters[])
1020296465Sdelphij{
1021296465Sdelphij    check_store(s, STORE_F_STORE_MODIFY_NUMBER,
1022296465Sdelphij                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
1023160814Ssimon
1024296465Sdelphij    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_NUMBER,
1025296465Sdelphij                                search_attributes, add_attributes,
1026296465Sdelphij                                modify_attributes, delete_attributes,
1027296465Sdelphij                                parameters)) {
1028296465Sdelphij        STOREerr(STORE_F_STORE_MODIFY_NUMBER,
1029296465Sdelphij                 STORE_R_FAILED_MODIFYING_NUMBER);
1030296465Sdelphij        return 0;
1031296465Sdelphij    }
1032296465Sdelphij    return 1;
1033296465Sdelphij}
1034160814Ssimon
1035160814SsimonBIGNUM *STORE_get_number(STORE *s, OPENSSL_ITEM attributes[],
1036296465Sdelphij                         OPENSSL_ITEM parameters[])
1037296465Sdelphij{
1038296465Sdelphij    STORE_OBJECT *object;
1039296465Sdelphij    BIGNUM *n;
1040160814Ssimon
1041296465Sdelphij    check_store(s, STORE_F_STORE_GET_NUMBER,
1042296465Sdelphij                get_object, STORE_R_NO_GET_OBJECT_NUMBER_FUNCTION);
1043160814Ssimon
1044296465Sdelphij    object = s->meth->get_object(s, STORE_OBJECT_TYPE_NUMBER, attributes,
1045296465Sdelphij                                 parameters);
1046296465Sdelphij    if (!object || !object->data.number) {
1047296465Sdelphij        STOREerr(STORE_F_STORE_GET_NUMBER, STORE_R_FAILED_GETTING_NUMBER);
1048296465Sdelphij        return 0;
1049296465Sdelphij    }
1050296465Sdelphij    n = object->data.number;
1051296465Sdelphij    object->data.number = NULL;
1052296465Sdelphij    STORE_OBJECT_free(object);
1053296465Sdelphij    return n;
1054296465Sdelphij}
1055160814Ssimon
1056160814Ssimonint STORE_delete_number(STORE *s, OPENSSL_ITEM attributes[],
1057296465Sdelphij                        OPENSSL_ITEM parameters[])
1058296465Sdelphij{
1059296465Sdelphij    check_store(s, STORE_F_STORE_DELETE_NUMBER,
1060296465Sdelphij                delete_object, STORE_R_NO_DELETE_NUMBER_FUNCTION);
1061160814Ssimon
1062296465Sdelphij    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_NUMBER, attributes,
1063296465Sdelphij                                parameters)) {
1064296465Sdelphij        STOREerr(STORE_F_STORE_DELETE_NUMBER, STORE_R_FAILED_DELETING_NUMBER);
1065296465Sdelphij        return 0;
1066296465Sdelphij    }
1067296465Sdelphij    return 1;
1068296465Sdelphij}
1069160814Ssimon
1070160814Ssimonint STORE_store_arbitrary(STORE *s, BUF_MEM *data, OPENSSL_ITEM attributes[],
1071296465Sdelphij                          OPENSSL_ITEM parameters[])
1072296465Sdelphij{
1073296465Sdelphij    STORE_OBJECT *object;
1074296465Sdelphij    int i;
1075160814Ssimon
1076296465Sdelphij    check_store(s, STORE_F_STORE_STORE_ARBITRARY,
1077296465Sdelphij                store_object, STORE_R_NO_STORE_OBJECT_ARBITRARY_FUNCTION);
1078160814Ssimon
1079296465Sdelphij    object = STORE_OBJECT_new();
1080296465Sdelphij    if (!object) {
1081296465Sdelphij        STOREerr(STORE_F_STORE_STORE_ARBITRARY, ERR_R_MALLOC_FAILURE);
1082296465Sdelphij        return 0;
1083296465Sdelphij    }
1084160814Ssimon
1085296465Sdelphij    object->data.arbitrary = data;
1086160814Ssimon
1087296465Sdelphij    i = s->meth->store_object(s, STORE_OBJECT_TYPE_ARBITRARY, object,
1088296465Sdelphij                              attributes, parameters);
1089160814Ssimon
1090296465Sdelphij    STORE_OBJECT_free(object);
1091160814Ssimon
1092296465Sdelphij    if (!i) {
1093296465Sdelphij        STOREerr(STORE_F_STORE_STORE_ARBITRARY,
1094296465Sdelphij                 STORE_R_FAILED_STORING_ARBITRARY);
1095296465Sdelphij        return 0;
1096296465Sdelphij    }
1097296465Sdelphij    return 1;
1098296465Sdelphij}
1099296465Sdelphij
1100160814Ssimonint STORE_modify_arbitrary(STORE *s, OPENSSL_ITEM search_attributes[],
1101296465Sdelphij                           OPENSSL_ITEM add_attributes[],
1102296465Sdelphij                           OPENSSL_ITEM modify_attributes[],
1103296465Sdelphij                           OPENSSL_ITEM delete_attributes[],
1104296465Sdelphij                           OPENSSL_ITEM parameters[])
1105296465Sdelphij{
1106296465Sdelphij    check_store(s, STORE_F_STORE_MODIFY_ARBITRARY,
1107296465Sdelphij                modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
1108160814Ssimon
1109296465Sdelphij    if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_ARBITRARY,
1110296465Sdelphij                                search_attributes, add_attributes,
1111296465Sdelphij                                modify_attributes, delete_attributes,
1112296465Sdelphij                                parameters)) {
1113296465Sdelphij        STOREerr(STORE_F_STORE_MODIFY_ARBITRARY,
1114296465Sdelphij                 STORE_R_FAILED_MODIFYING_ARBITRARY);
1115296465Sdelphij        return 0;
1116296465Sdelphij    }
1117296465Sdelphij    return 1;
1118296465Sdelphij}
1119160814Ssimon
1120160814SsimonBUF_MEM *STORE_get_arbitrary(STORE *s, OPENSSL_ITEM attributes[],
1121296465Sdelphij                             OPENSSL_ITEM parameters[])
1122296465Sdelphij{
1123296465Sdelphij    STORE_OBJECT *object;
1124296465Sdelphij    BUF_MEM *b;
1125160814Ssimon
1126296465Sdelphij    check_store(s, STORE_F_STORE_GET_ARBITRARY,
1127296465Sdelphij                get_object, STORE_R_NO_GET_OBJECT_ARBITRARY_FUNCTION);
1128160814Ssimon
1129296465Sdelphij    object = s->meth->get_object(s, STORE_OBJECT_TYPE_ARBITRARY,
1130296465Sdelphij                                 attributes, parameters);
1131296465Sdelphij    if (!object || !object->data.arbitrary) {
1132296465Sdelphij        STOREerr(STORE_F_STORE_GET_ARBITRARY,
1133296465Sdelphij                 STORE_R_FAILED_GETTING_ARBITRARY);
1134296465Sdelphij        return 0;
1135296465Sdelphij    }
1136296465Sdelphij    b = object->data.arbitrary;
1137296465Sdelphij    object->data.arbitrary = NULL;
1138296465Sdelphij    STORE_OBJECT_free(object);
1139296465Sdelphij    return b;
1140296465Sdelphij}
1141160814Ssimon
1142160814Ssimonint STORE_delete_arbitrary(STORE *s, OPENSSL_ITEM attributes[],
1143296465Sdelphij                           OPENSSL_ITEM parameters[])
1144296465Sdelphij{
1145296465Sdelphij    check_store(s, STORE_F_STORE_DELETE_ARBITRARY,
1146296465Sdelphij                delete_object, STORE_R_NO_DELETE_ARBITRARY_FUNCTION);
1147160814Ssimon
1148296465Sdelphij    if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_ARBITRARY, attributes,
1149296465Sdelphij                                parameters)) {
1150296465Sdelphij        STOREerr(STORE_F_STORE_DELETE_ARBITRARY,
1151296465Sdelphij                 STORE_R_FAILED_DELETING_ARBITRARY);
1152296465Sdelphij        return 0;
1153296465Sdelphij    }
1154296465Sdelphij    return 1;
1155296465Sdelphij}
1156160814Ssimon
1157160814SsimonSTORE_OBJECT *STORE_OBJECT_new(void)
1158296465Sdelphij{
1159296465Sdelphij    STORE_OBJECT *object = OPENSSL_malloc(sizeof(STORE_OBJECT));
1160296465Sdelphij    if (object)
1161296465Sdelphij        memset(object, 0, sizeof(STORE_OBJECT));
1162296465Sdelphij    return object;
1163296465Sdelphij}
1164296465Sdelphij
1165160814Ssimonvoid STORE_OBJECT_free(STORE_OBJECT *data)
1166296465Sdelphij{
1167296465Sdelphij    if (!data)
1168296465Sdelphij        return;
1169296465Sdelphij    switch (data->type) {
1170296465Sdelphij    case STORE_OBJECT_TYPE_X509_CERTIFICATE:
1171296465Sdelphij        X509_free(data->data.x509.certificate);
1172296465Sdelphij        break;
1173296465Sdelphij    case STORE_OBJECT_TYPE_X509_CRL:
1174296465Sdelphij        X509_CRL_free(data->data.crl);
1175296465Sdelphij        break;
1176296465Sdelphij    case STORE_OBJECT_TYPE_PRIVATE_KEY:
1177296465Sdelphij    case STORE_OBJECT_TYPE_PUBLIC_KEY:
1178296465Sdelphij        EVP_PKEY_free(data->data.key);
1179296465Sdelphij        break;
1180296465Sdelphij    case STORE_OBJECT_TYPE_NUMBER:
1181296465Sdelphij        BN_free(data->data.number);
1182296465Sdelphij        break;
1183296465Sdelphij    case STORE_OBJECT_TYPE_ARBITRARY:
1184296465Sdelphij        BUF_MEM_free(data->data.arbitrary);
1185296465Sdelphij        break;
1186296465Sdelphij    }
1187296465Sdelphij    OPENSSL_free(data);
1188296465Sdelphij}
1189160814Ssimon
1190160814SsimonIMPLEMENT_STACK_OF(STORE_OBJECT*)
1191160814Ssimon
1192296465Sdelphijstruct STORE_attr_info_st {
1193296465Sdelphij    unsigned char set[(STORE_ATTR_TYPE_NUM + 8) / 8];
1194296465Sdelphij    union {
1195296465Sdelphij        char *cstring;
1196296465Sdelphij        unsigned char *sha1string;
1197296465Sdelphij        X509_NAME *dn;
1198296465Sdelphij        BIGNUM *number;
1199296465Sdelphij        void *any;
1200296465Sdelphij    } values[STORE_ATTR_TYPE_NUM + 1];
1201296465Sdelphij    size_t value_sizes[STORE_ATTR_TYPE_NUM + 1];
1202296465Sdelphij};
1203160814Ssimon
1204296465Sdelphij#define ATTR_IS_SET(a,i)        ((i) > 0 && (i) < STORE_ATTR_TYPE_NUM \
1205296465Sdelphij                                && ((a)->set[(i) / 8] & (1 << ((i) % 8))))
1206296465Sdelphij#define SET_ATTRBIT(a,i)        ((a)->set[(i) / 8] |= (1 << ((i) % 8)))
1207296465Sdelphij#define CLEAR_ATTRBIT(a,i)      ((a)->set[(i) / 8] &= ~(1 << ((i) % 8)))
1208160814Ssimon
1209296465SdelphijSTORE_ATTR_INFO *STORE_ATTR_INFO_new(void)
1210296465Sdelphij{
1211296465Sdelphij    return (STORE_ATTR_INFO *)OPENSSL_malloc(sizeof(STORE_ATTR_INFO));
1212296465Sdelphij}
1213160814Ssimon
1214160814Ssimonstatic void STORE_ATTR_INFO_attr_free(STORE_ATTR_INFO *attrs,
1215296465Sdelphij                                      STORE_ATTR_TYPES code)
1216296465Sdelphij{
1217296465Sdelphij    if (ATTR_IS_SET(attrs, code)) {
1218296465Sdelphij        switch (code) {
1219296465Sdelphij        case STORE_ATTR_FRIENDLYNAME:
1220296465Sdelphij        case STORE_ATTR_EMAIL:
1221296465Sdelphij        case STORE_ATTR_FILENAME:
1222296465Sdelphij            STORE_ATTR_INFO_modify_cstr(attrs, code, NULL, 0);
1223296465Sdelphij            break;
1224296465Sdelphij        case STORE_ATTR_KEYID:
1225296465Sdelphij        case STORE_ATTR_ISSUERKEYID:
1226296465Sdelphij        case STORE_ATTR_SUBJECTKEYID:
1227296465Sdelphij        case STORE_ATTR_ISSUERSERIALHASH:
1228296465Sdelphij        case STORE_ATTR_CERTHASH:
1229296465Sdelphij            STORE_ATTR_INFO_modify_sha1str(attrs, code, NULL, 0);
1230296465Sdelphij            break;
1231296465Sdelphij        case STORE_ATTR_ISSUER:
1232296465Sdelphij        case STORE_ATTR_SUBJECT:
1233296465Sdelphij            STORE_ATTR_INFO_modify_dn(attrs, code, NULL);
1234296465Sdelphij            break;
1235296465Sdelphij        case STORE_ATTR_SERIAL:
1236296465Sdelphij            STORE_ATTR_INFO_modify_number(attrs, code, NULL);
1237296465Sdelphij            break;
1238296465Sdelphij        default:
1239296465Sdelphij            break;
1240296465Sdelphij        }
1241296465Sdelphij    }
1242296465Sdelphij}
1243296465Sdelphij
1244160814Ssimonint STORE_ATTR_INFO_free(STORE_ATTR_INFO *attrs)
1245296465Sdelphij{
1246296465Sdelphij    if (attrs) {
1247296465Sdelphij        STORE_ATTR_TYPES i;
1248296465Sdelphij        for (i = 0; i++ < STORE_ATTR_TYPE_NUM;)
1249296465Sdelphij            STORE_ATTR_INFO_attr_free(attrs, i);
1250296465Sdelphij        OPENSSL_free(attrs);
1251296465Sdelphij    }
1252296465Sdelphij    return 1;
1253296465Sdelphij}
1254296465Sdelphij
1255160814Ssimonchar *STORE_ATTR_INFO_get0_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code)
1256296465Sdelphij{
1257296465Sdelphij    if (!attrs) {
1258296465Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_GET0_CSTR,
1259296465Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1260296465Sdelphij        return NULL;
1261296465Sdelphij    }
1262296465Sdelphij    if (ATTR_IS_SET(attrs, code))
1263296465Sdelphij        return attrs->values[code].cstring;
1264296465Sdelphij    STOREerr(STORE_F_STORE_ATTR_INFO_GET0_CSTR, STORE_R_NO_VALUE);
1265296465Sdelphij    return NULL;
1266296465Sdelphij}
1267296465Sdelphij
1268160814Ssimonunsigned char *STORE_ATTR_INFO_get0_sha1str(STORE_ATTR_INFO *attrs,
1269296465Sdelphij                                            STORE_ATTR_TYPES code)
1270296465Sdelphij{
1271296465Sdelphij    if (!attrs) {
1272296465Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_GET0_SHA1STR,
1273296465Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1274296465Sdelphij        return NULL;
1275296465Sdelphij    }
1276296465Sdelphij    if (ATTR_IS_SET(attrs, code))
1277296465Sdelphij        return attrs->values[code].sha1string;
1278296465Sdelphij    STOREerr(STORE_F_STORE_ATTR_INFO_GET0_SHA1STR, STORE_R_NO_VALUE);
1279296465Sdelphij    return NULL;
1280296465Sdelphij}
1281296465Sdelphij
1282296465SdelphijX509_NAME *STORE_ATTR_INFO_get0_dn(STORE_ATTR_INFO *attrs,
1283296465Sdelphij                                   STORE_ATTR_TYPES code)
1284296465Sdelphij{
1285296465Sdelphij    if (!attrs) {
1286296465Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_GET0_DN,
1287296465Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1288296465Sdelphij        return NULL;
1289296465Sdelphij    }
1290296465Sdelphij    if (ATTR_IS_SET(attrs, code))
1291296465Sdelphij        return attrs->values[code].dn;
1292296465Sdelphij    STOREerr(STORE_F_STORE_ATTR_INFO_GET0_DN, STORE_R_NO_VALUE);
1293296465Sdelphij    return NULL;
1294296465Sdelphij}
1295296465Sdelphij
1296296465SdelphijBIGNUM *STORE_ATTR_INFO_get0_number(STORE_ATTR_INFO *attrs,
1297296465Sdelphij                                    STORE_ATTR_TYPES code)
1298296465Sdelphij{
1299296465Sdelphij    if (!attrs) {
1300296465Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_GET0_NUMBER,
1301296465Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1302296465Sdelphij        return NULL;
1303296465Sdelphij    }
1304296465Sdelphij    if (ATTR_IS_SET(attrs, code))
1305296465Sdelphij        return attrs->values[code].number;
1306296465Sdelphij    STOREerr(STORE_F_STORE_ATTR_INFO_GET0_NUMBER, STORE_R_NO_VALUE);
1307296465Sdelphij    return NULL;
1308296465Sdelphij}
1309296465Sdelphij
1310160814Ssimonint STORE_ATTR_INFO_set_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1311296465Sdelphij                             char *cstr, size_t cstr_size)
1312296465Sdelphij{
1313296465Sdelphij    if (!attrs) {
1314296465Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR,
1315296465Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1316296465Sdelphij        return 0;
1317296465Sdelphij    }
1318296465Sdelphij    if (!ATTR_IS_SET(attrs, code)) {
1319296465Sdelphij        if ((attrs->values[code].cstring = BUF_strndup(cstr, cstr_size)))
1320296465Sdelphij            return 1;
1321296465Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR, ERR_R_MALLOC_FAILURE);
1322296465Sdelphij        return 0;
1323296465Sdelphij    }
1324296465Sdelphij    STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR, STORE_R_ALREADY_HAS_A_VALUE);
1325296465Sdelphij    return 0;
1326296465Sdelphij}
1327296465Sdelphij
1328160814Ssimonint STORE_ATTR_INFO_set_sha1str(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1329296465Sdelphij                                unsigned char *sha1str, size_t sha1str_size)
1330296465Sdelphij{
1331296465Sdelphij    if (!attrs) {
1332296465Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR,
1333296465Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1334296465Sdelphij        return 0;
1335296465Sdelphij    }
1336296465Sdelphij    if (!ATTR_IS_SET(attrs, code)) {
1337296465Sdelphij        if ((attrs->values[code].sha1string =
1338296465Sdelphij             (unsigned char *)BUF_memdup(sha1str, sha1str_size)))
1339296465Sdelphij            return 1;
1340296465Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR, ERR_R_MALLOC_FAILURE);
1341296465Sdelphij        return 0;
1342296465Sdelphij    }
1343296465Sdelphij    STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR,
1344296465Sdelphij             STORE_R_ALREADY_HAS_A_VALUE);
1345296465Sdelphij    return 0;
1346296465Sdelphij}
1347296465Sdelphij
1348160814Ssimonint STORE_ATTR_INFO_set_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1349296465Sdelphij                           X509_NAME *dn)
1350296465Sdelphij{
1351296465Sdelphij    if (!attrs) {
1352296465Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN, ERR_R_PASSED_NULL_PARAMETER);
1353296465Sdelphij        return 0;
1354296465Sdelphij    }
1355296465Sdelphij    if (!ATTR_IS_SET(attrs, code)) {
1356296465Sdelphij        if ((attrs->values[code].dn = X509_NAME_dup(dn)))
1357296465Sdelphij            return 1;
1358296465Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN, ERR_R_MALLOC_FAILURE);
1359296465Sdelphij        return 0;
1360296465Sdelphij    }
1361296465Sdelphij    STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN, STORE_R_ALREADY_HAS_A_VALUE);
1362296465Sdelphij    return 0;
1363296465Sdelphij}
1364296465Sdelphij
1365160814Ssimonint STORE_ATTR_INFO_set_number(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1366296465Sdelphij                               BIGNUM *number)
1367296465Sdelphij{
1368296465Sdelphij    if (!attrs) {
1369296465Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER,
1370296465Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1371296465Sdelphij        return 0;
1372296465Sdelphij    }
1373296465Sdelphij    if (!ATTR_IS_SET(attrs, code)) {
1374296465Sdelphij        if ((attrs->values[code].number = BN_dup(number)))
1375296465Sdelphij            return 1;
1376296465Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER, ERR_R_MALLOC_FAILURE);
1377296465Sdelphij        return 0;
1378296465Sdelphij    }
1379296465Sdelphij    STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER, STORE_R_ALREADY_HAS_A_VALUE);
1380296465Sdelphij    return 0;
1381296465Sdelphij}
1382296465Sdelphij
1383160814Ssimonint STORE_ATTR_INFO_modify_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1384296465Sdelphij                                char *cstr, size_t cstr_size)
1385296465Sdelphij{
1386296465Sdelphij    if (!attrs) {
1387296465Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_CSTR,
1388296465Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1389296465Sdelphij        return 0;
1390296465Sdelphij    }
1391296465Sdelphij    if (ATTR_IS_SET(attrs, code)) {
1392296465Sdelphij        OPENSSL_free(attrs->values[code].cstring);
1393296465Sdelphij        attrs->values[code].cstring = NULL;
1394296465Sdelphij        CLEAR_ATTRBIT(attrs, code);
1395296465Sdelphij    }
1396296465Sdelphij    return STORE_ATTR_INFO_set_cstr(attrs, code, cstr, cstr_size);
1397296465Sdelphij}
1398296465Sdelphij
1399296465Sdelphijint STORE_ATTR_INFO_modify_sha1str(STORE_ATTR_INFO *attrs,
1400296465Sdelphij                                   STORE_ATTR_TYPES code,
1401296465Sdelphij                                   unsigned char *sha1str,
1402296465Sdelphij                                   size_t sha1str_size)
1403296465Sdelphij{
1404296465Sdelphij    if (!attrs) {
1405296465Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_SHA1STR,
1406296465Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1407296465Sdelphij        return 0;
1408296465Sdelphij    }
1409296465Sdelphij    if (ATTR_IS_SET(attrs, code)) {
1410296465Sdelphij        OPENSSL_free(attrs->values[code].sha1string);
1411296465Sdelphij        attrs->values[code].sha1string = NULL;
1412296465Sdelphij        CLEAR_ATTRBIT(attrs, code);
1413296465Sdelphij    }
1414296465Sdelphij    return STORE_ATTR_INFO_set_sha1str(attrs, code, sha1str, sha1str_size);
1415296465Sdelphij}
1416296465Sdelphij
1417160814Ssimonint STORE_ATTR_INFO_modify_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1418296465Sdelphij                              X509_NAME *dn)
1419296465Sdelphij{
1420296465Sdelphij    if (!attrs) {
1421296465Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_DN,
1422296465Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1423296465Sdelphij        return 0;
1424296465Sdelphij    }
1425296465Sdelphij    if (ATTR_IS_SET(attrs, code)) {
1426296465Sdelphij        OPENSSL_free(attrs->values[code].dn);
1427296465Sdelphij        attrs->values[code].dn = NULL;
1428296465Sdelphij        CLEAR_ATTRBIT(attrs, code);
1429296465Sdelphij    }
1430296465Sdelphij    return STORE_ATTR_INFO_set_dn(attrs, code, dn);
1431296465Sdelphij}
1432160814Ssimon
1433296465Sdelphijint STORE_ATTR_INFO_modify_number(STORE_ATTR_INFO *attrs,
1434296465Sdelphij                                  STORE_ATTR_TYPES code, BIGNUM *number)
1435296465Sdelphij{
1436296465Sdelphij    if (!attrs) {
1437296465Sdelphij        STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_NUMBER,
1438296465Sdelphij                 ERR_R_PASSED_NULL_PARAMETER);
1439296465Sdelphij        return 0;
1440296465Sdelphij    }
1441296465Sdelphij    if (ATTR_IS_SET(attrs, code)) {
1442296465Sdelphij        OPENSSL_free(attrs->values[code].number);
1443296465Sdelphij        attrs->values[code].number = NULL;
1444296465Sdelphij        CLEAR_ATTRBIT(attrs, code);
1445296465Sdelphij    }
1446296465Sdelphij    return STORE_ATTR_INFO_set_number(attrs, code, number);
1447296465Sdelphij}
1448296465Sdelphij
1449296465Sdelphijstruct attr_list_ctx_st {
1450296465Sdelphij    OPENSSL_ITEM *attributes;
1451296465Sdelphij};
1452160814Ssimonvoid *STORE_parse_attrs_start(OPENSSL_ITEM *attributes)
1453296465Sdelphij{
1454296465Sdelphij    if (attributes) {
1455296465Sdelphij        struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)
1456296465Sdelphij            OPENSSL_malloc(sizeof(struct attr_list_ctx_st));
1457296465Sdelphij        if (context)
1458296465Sdelphij            context->attributes = attributes;
1459296465Sdelphij        else
1460296465Sdelphij            STOREerr(STORE_F_STORE_PARSE_ATTRS_START, ERR_R_MALLOC_FAILURE);
1461296465Sdelphij        return context;
1462296465Sdelphij    }
1463296465Sdelphij    STOREerr(STORE_F_STORE_PARSE_ATTRS_START, ERR_R_PASSED_NULL_PARAMETER);
1464296465Sdelphij    return 0;
1465296465Sdelphij}
1466296465Sdelphij
1467160814SsimonSTORE_ATTR_INFO *STORE_parse_attrs_next(void *handle)
1468296465Sdelphij{
1469296465Sdelphij    struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;
1470160814Ssimon
1471296465Sdelphij    if (context && context->attributes) {
1472296465Sdelphij        STORE_ATTR_INFO *attrs = NULL;
1473160814Ssimon
1474296465Sdelphij        while (context->attributes
1475296465Sdelphij               && context->attributes->code != STORE_ATTR_OR
1476296465Sdelphij               && context->attributes->code != STORE_ATTR_END) {
1477296465Sdelphij            switch (context->attributes->code) {
1478296465Sdelphij            case STORE_ATTR_FRIENDLYNAME:
1479296465Sdelphij            case STORE_ATTR_EMAIL:
1480296465Sdelphij            case STORE_ATTR_FILENAME:
1481296465Sdelphij                if (!attrs)
1482296465Sdelphij                    attrs = STORE_ATTR_INFO_new();
1483296465Sdelphij                if (attrs == NULL) {
1484296465Sdelphij                    STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
1485296465Sdelphij                             ERR_R_MALLOC_FAILURE);
1486296465Sdelphij                    goto err;
1487296465Sdelphij                }
1488296465Sdelphij                STORE_ATTR_INFO_set_cstr(attrs,
1489296465Sdelphij                                         context->attributes->code,
1490296465Sdelphij                                         context->attributes->value,
1491296465Sdelphij                                         context->attributes->value_size);
1492296465Sdelphij                break;
1493296465Sdelphij            case STORE_ATTR_KEYID:
1494296465Sdelphij            case STORE_ATTR_ISSUERKEYID:
1495296465Sdelphij            case STORE_ATTR_SUBJECTKEYID:
1496296465Sdelphij            case STORE_ATTR_ISSUERSERIALHASH:
1497296465Sdelphij            case STORE_ATTR_CERTHASH:
1498296465Sdelphij                if (!attrs)
1499296465Sdelphij                    attrs = STORE_ATTR_INFO_new();
1500296465Sdelphij                if (attrs == NULL) {
1501296465Sdelphij                    STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
1502296465Sdelphij                             ERR_R_MALLOC_FAILURE);
1503296465Sdelphij                    goto err;
1504296465Sdelphij                }
1505296465Sdelphij                STORE_ATTR_INFO_set_sha1str(attrs,
1506296465Sdelphij                                            context->attributes->code,
1507296465Sdelphij                                            context->attributes->value,
1508296465Sdelphij                                            context->attributes->value_size);
1509296465Sdelphij                break;
1510296465Sdelphij            case STORE_ATTR_ISSUER:
1511296465Sdelphij            case STORE_ATTR_SUBJECT:
1512296465Sdelphij                if (!attrs)
1513296465Sdelphij                    attrs = STORE_ATTR_INFO_new();
1514296465Sdelphij                if (attrs == NULL) {
1515296465Sdelphij                    STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
1516296465Sdelphij                             ERR_R_MALLOC_FAILURE);
1517296465Sdelphij                    goto err;
1518296465Sdelphij                }
1519296465Sdelphij                STORE_ATTR_INFO_modify_dn(attrs,
1520296465Sdelphij                                          context->attributes->code,
1521296465Sdelphij                                          context->attributes->value);
1522296465Sdelphij                break;
1523296465Sdelphij            case STORE_ATTR_SERIAL:
1524296465Sdelphij                if (!attrs)
1525296465Sdelphij                    attrs = STORE_ATTR_INFO_new();
1526296465Sdelphij                if (attrs == NULL) {
1527296465Sdelphij                    STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
1528296465Sdelphij                             ERR_R_MALLOC_FAILURE);
1529296465Sdelphij                    goto err;
1530296465Sdelphij                }
1531296465Sdelphij                STORE_ATTR_INFO_modify_number(attrs,
1532296465Sdelphij                                              context->attributes->code,
1533296465Sdelphij                                              context->attributes->value);
1534296465Sdelphij                break;
1535296465Sdelphij            }
1536296465Sdelphij            context->attributes++;
1537296465Sdelphij        }
1538296465Sdelphij        if (context->attributes->code == STORE_ATTR_OR)
1539296465Sdelphij            context->attributes++;
1540296465Sdelphij        return attrs;
1541296465Sdelphij err:
1542296465Sdelphij        while (context->attributes
1543296465Sdelphij               && context->attributes->code != STORE_ATTR_OR
1544296465Sdelphij               && context->attributes->code != STORE_ATTR_END)
1545296465Sdelphij            context->attributes++;
1546296465Sdelphij        if (context->attributes->code == STORE_ATTR_OR)
1547296465Sdelphij            context->attributes++;
1548296465Sdelphij        return NULL;
1549296465Sdelphij    }
1550296465Sdelphij    STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT, ERR_R_PASSED_NULL_PARAMETER);
1551296465Sdelphij    return NULL;
1552296465Sdelphij}
1553296465Sdelphij
1554160814Ssimonint STORE_parse_attrs_end(void *handle)
1555296465Sdelphij{
1556296465Sdelphij    struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;
1557160814Ssimon
1558296465Sdelphij    if (context && context->attributes) {
1559160814Ssimon#if 0
1560296465Sdelphij        OPENSSL_ITEM *attributes = context->attributes;
1561160814Ssimon#endif
1562296465Sdelphij        OPENSSL_free(context);
1563296465Sdelphij        return 1;
1564296465Sdelphij    }
1565296465Sdelphij    STOREerr(STORE_F_STORE_PARSE_ATTRS_END, ERR_R_PASSED_NULL_PARAMETER);
1566296465Sdelphij    return 0;
1567296465Sdelphij}
1568160814Ssimon
1569160814Ssimonint STORE_parse_attrs_endp(void *handle)
1570296465Sdelphij{
1571296465Sdelphij    struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;
1572160814Ssimon
1573296465Sdelphij    if (context && context->attributes) {
1574296465Sdelphij        return context->attributes->code == STORE_ATTR_END;
1575296465Sdelphij    }
1576296465Sdelphij    STOREerr(STORE_F_STORE_PARSE_ATTRS_ENDP, ERR_R_PASSED_NULL_PARAMETER);
1577296465Sdelphij    return 0;
1578296465Sdelphij}
1579160814Ssimon
1580296465Sdelphijstatic int attr_info_compare_compute_range(unsigned char *abits,
1581296465Sdelphij                                           unsigned char *bbits,
1582296465Sdelphij                                           unsigned int *alowp,
1583296465Sdelphij                                           unsigned int *ahighp,
1584296465Sdelphij                                           unsigned int *blowp,
1585296465Sdelphij                                           unsigned int *bhighp)
1586296465Sdelphij{
1587296465Sdelphij    unsigned int alow = (unsigned int)-1, ahigh = 0;
1588296465Sdelphij    unsigned int blow = (unsigned int)-1, bhigh = 0;
1589296465Sdelphij    int i, res = 0;
1590160814Ssimon
1591296465Sdelphij    for (i = 0; i < (STORE_ATTR_TYPE_NUM + 8) / 8; i++, abits++, bbits++) {
1592296465Sdelphij        if (res == 0) {
1593296465Sdelphij            if (*abits < *bbits)
1594296465Sdelphij                res = -1;
1595296465Sdelphij            if (*abits > *bbits)
1596296465Sdelphij                res = 1;
1597296465Sdelphij        }
1598296465Sdelphij        if (*abits) {
1599296465Sdelphij            if (alow == (unsigned int)-1) {
1600296465Sdelphij                alow = i * 8;
1601296465Sdelphij                if (!(*abits & 0x01))
1602296465Sdelphij                    alow++;
1603296465Sdelphij                if (!(*abits & 0x02))
1604296465Sdelphij                    alow++;
1605296465Sdelphij                if (!(*abits & 0x04))
1606296465Sdelphij                    alow++;
1607296465Sdelphij                if (!(*abits & 0x08))
1608296465Sdelphij                    alow++;
1609296465Sdelphij                if (!(*abits & 0x10))
1610296465Sdelphij                    alow++;
1611296465Sdelphij                if (!(*abits & 0x20))
1612296465Sdelphij                    alow++;
1613296465Sdelphij                if (!(*abits & 0x40))
1614296465Sdelphij                    alow++;
1615296465Sdelphij            }
1616296465Sdelphij            ahigh = i * 8 + 7;
1617296465Sdelphij            if (!(*abits & 0x80))
1618296465Sdelphij                ahigh++;
1619296465Sdelphij            if (!(*abits & 0x40))
1620296465Sdelphij                ahigh++;
1621296465Sdelphij            if (!(*abits & 0x20))
1622296465Sdelphij                ahigh++;
1623296465Sdelphij            if (!(*abits & 0x10))
1624296465Sdelphij                ahigh++;
1625296465Sdelphij            if (!(*abits & 0x08))
1626296465Sdelphij                ahigh++;
1627296465Sdelphij            if (!(*abits & 0x04))
1628296465Sdelphij                ahigh++;
1629296465Sdelphij            if (!(*abits & 0x02))
1630296465Sdelphij                ahigh++;
1631296465Sdelphij        }
1632296465Sdelphij        if (*bbits) {
1633296465Sdelphij            if (blow == (unsigned int)-1) {
1634296465Sdelphij                blow = i * 8;
1635296465Sdelphij                if (!(*bbits & 0x01))
1636296465Sdelphij                    blow++;
1637296465Sdelphij                if (!(*bbits & 0x02))
1638296465Sdelphij                    blow++;
1639296465Sdelphij                if (!(*bbits & 0x04))
1640296465Sdelphij                    blow++;
1641296465Sdelphij                if (!(*bbits & 0x08))
1642296465Sdelphij                    blow++;
1643296465Sdelphij                if (!(*bbits & 0x10))
1644296465Sdelphij                    blow++;
1645296465Sdelphij                if (!(*bbits & 0x20))
1646296465Sdelphij                    blow++;
1647296465Sdelphij                if (!(*bbits & 0x40))
1648296465Sdelphij                    blow++;
1649296465Sdelphij            }
1650296465Sdelphij            bhigh = i * 8 + 7;
1651296465Sdelphij            if (!(*bbits & 0x80))
1652296465Sdelphij                bhigh++;
1653296465Sdelphij            if (!(*bbits & 0x40))
1654296465Sdelphij                bhigh++;
1655296465Sdelphij            if (!(*bbits & 0x20))
1656296465Sdelphij                bhigh++;
1657296465Sdelphij            if (!(*bbits & 0x10))
1658296465Sdelphij                bhigh++;
1659296465Sdelphij            if (!(*bbits & 0x08))
1660296465Sdelphij                bhigh++;
1661296465Sdelphij            if (!(*bbits & 0x04))
1662296465Sdelphij                bhigh++;
1663296465Sdelphij            if (!(*bbits & 0x02))
1664296465Sdelphij                bhigh++;
1665296465Sdelphij        }
1666296465Sdelphij    }
1667296465Sdelphij    if (ahigh + alow < bhigh + blow)
1668296465Sdelphij        res = -1;
1669296465Sdelphij    if (ahigh + alow > bhigh + blow)
1670296465Sdelphij        res = 1;
1671296465Sdelphij    if (alowp)
1672296465Sdelphij        *alowp = alow;
1673296465Sdelphij    if (ahighp)
1674296465Sdelphij        *ahighp = ahigh;
1675296465Sdelphij    if (blowp)
1676296465Sdelphij        *blowp = blow;
1677296465Sdelphij    if (bhighp)
1678296465Sdelphij        *bhighp = bhigh;
1679296465Sdelphij    return res;
1680296465Sdelphij}
1681160814Ssimon
1682160814Ssimonint STORE_ATTR_INFO_compare(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
1683296465Sdelphij{
1684296465Sdelphij    if (a == b)
1685296465Sdelphij        return 0;
1686296465Sdelphij    if (!a)
1687296465Sdelphij        return -1;
1688296465Sdelphij    if (!b)
1689296465Sdelphij        return 1;
1690296465Sdelphij    return attr_info_compare_compute_range(a->set, b->set, 0, 0, 0, 0);
1691296465Sdelphij}
1692296465Sdelphij
1693160814Ssimonint STORE_ATTR_INFO_in_range(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
1694296465Sdelphij{
1695296465Sdelphij    unsigned int alow, ahigh, blow, bhigh;
1696160814Ssimon
1697296465Sdelphij    if (a == b)
1698296465Sdelphij        return 1;
1699296465Sdelphij    if (!a)
1700296465Sdelphij        return 0;
1701296465Sdelphij    if (!b)
1702296465Sdelphij        return 0;
1703296465Sdelphij    attr_info_compare_compute_range(a->set, b->set,
1704296465Sdelphij                                    &alow, &ahigh, &blow, &bhigh);
1705296465Sdelphij    if (alow >= blow && ahigh <= bhigh)
1706296465Sdelphij        return 1;
1707296465Sdelphij    return 0;
1708296465Sdelphij}
1709296465Sdelphij
1710160814Ssimonint STORE_ATTR_INFO_in(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
1711296465Sdelphij{
1712296465Sdelphij    unsigned char *abits, *bbits;
1713296465Sdelphij    int i;
1714160814Ssimon
1715296465Sdelphij    if (a == b)
1716296465Sdelphij        return 1;
1717296465Sdelphij    if (!a)
1718296465Sdelphij        return 0;
1719296465Sdelphij    if (!b)
1720296465Sdelphij        return 0;
1721296465Sdelphij    abits = a->set;
1722296465Sdelphij    bbits = b->set;
1723296465Sdelphij    for (i = 0; i < (STORE_ATTR_TYPE_NUM + 8) / 8; i++, abits++, bbits++) {
1724296465Sdelphij        if (*abits && (*bbits & *abits) != *abits)
1725296465Sdelphij            return 0;
1726296465Sdelphij    }
1727296465Sdelphij    return 1;
1728296465Sdelphij}
1729296465Sdelphij
1730160814Ssimonint STORE_ATTR_INFO_in_ex(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
1731296465Sdelphij{
1732296465Sdelphij    STORE_ATTR_TYPES i;
1733160814Ssimon
1734296465Sdelphij    if (a == b)
1735296465Sdelphij        return 1;
1736296465Sdelphij    if (!STORE_ATTR_INFO_in(a, b))
1737296465Sdelphij        return 0;
1738296465Sdelphij    for (i = 1; i < STORE_ATTR_TYPE_NUM; i++)
1739296465Sdelphij        if (ATTR_IS_SET(a, i)) {
1740296465Sdelphij            switch (i) {
1741296465Sdelphij            case STORE_ATTR_FRIENDLYNAME:
1742296465Sdelphij            case STORE_ATTR_EMAIL:
1743296465Sdelphij            case STORE_ATTR_FILENAME:
1744296465Sdelphij                if (strcmp(a->values[i].cstring, b->values[i].cstring))
1745296465Sdelphij                    return 0;
1746296465Sdelphij                break;
1747296465Sdelphij            case STORE_ATTR_KEYID:
1748296465Sdelphij            case STORE_ATTR_ISSUERKEYID:
1749296465Sdelphij            case STORE_ATTR_SUBJECTKEYID:
1750296465Sdelphij            case STORE_ATTR_ISSUERSERIALHASH:
1751296465Sdelphij            case STORE_ATTR_CERTHASH:
1752296465Sdelphij                if (memcmp(a->values[i].sha1string,
1753296465Sdelphij                           b->values[i].sha1string, a->value_sizes[i]))
1754296465Sdelphij                    return 0;
1755296465Sdelphij                break;
1756296465Sdelphij            case STORE_ATTR_ISSUER:
1757296465Sdelphij            case STORE_ATTR_SUBJECT:
1758296465Sdelphij                if (X509_NAME_cmp(a->values[i].dn, b->values[i].dn))
1759296465Sdelphij                    return 0;
1760296465Sdelphij                break;
1761296465Sdelphij            case STORE_ATTR_SERIAL:
1762296465Sdelphij                if (BN_cmp(a->values[i].number, b->values[i].number))
1763296465Sdelphij                    return 0;
1764296465Sdelphij                break;
1765296465Sdelphij            default:
1766296465Sdelphij                break;
1767296465Sdelphij            }
1768296465Sdelphij        }
1769160814Ssimon
1770296465Sdelphij    return 1;
1771296465Sdelphij}
1772