summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/store/str_lib.c
diff options
context:
space:
mode:
authorjsing <>2015-02-10 11:16:33 +0000
committerjsing <>2015-02-10 11:16:33 +0000
commit8e3767cefee0b61a7599b3ce219675e54c312702 (patch)
tree5ad70c8f182ce58b9f8b931518d6ce999b68a736 /src/lib/libcrypto/store/str_lib.c
parenta23c986bc79db61aa706aa1951c3b2a0c0acdaee (diff)
downloadopenbsd-8e3767cefee0b61a7599b3ce219675e54c312702.tar.gz
openbsd-8e3767cefee0b61a7599b3ce219675e54c312702.tar.bz2
openbsd-8e3767cefee0b61a7599b3ce219675e54c312702.zip
Remove crypto/store - part of which is "currently highly experimental".
This code is not compiled in and OPENSSL_NO_STORE is already defined in opensslfeatures.h. No symbol removal for libcrypto. ok beck@
Diffstat (limited to 'src/lib/libcrypto/store/str_lib.c')
-rw-r--r--src/lib/libcrypto/store/str_lib.c1824
1 files changed, 0 insertions, 1824 deletions
diff --git a/src/lib/libcrypto/store/str_lib.c b/src/lib/libcrypto/store/str_lib.c
deleted file mode 100644
index 2ea3ad958a..0000000000
--- a/src/lib/libcrypto/store/str_lib.c
+++ /dev/null
@@ -1,1824 +0,0 @@
1/* $OpenBSD: str_lib.c,v 1.12 2014/11/18 03:28:05 tedu Exp $ */
2/* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
3 * project 2003.
4 */
5/* ====================================================================
6 * Copyright (c) 2003 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <string.h>
60
61#include <openssl/opensslconf.h>
62
63#include <openssl/bn.h>
64#include <openssl/err.h>
65#ifndef OPENSSL_NO_ENGINE
66#include <openssl/engine.h>
67#endif
68#include <openssl/sha.h>
69#include <openssl/x509.h>
70#include "str_locl.h"
71
72const char * const STORE_object_type_string[STORE_OBJECT_TYPE_NUM + 1] = {
73 0,
74 "X.509 Certificate",
75 "X.509 CRL",
76 "Private Key",
77 "Public Key",
78 "Number",
79 "Arbitrary Data"
80};
81
82const int STORE_param_sizes[STORE_PARAM_TYPE_NUM + 1] = {
83 0,
84 sizeof(int), /* EVP_TYPE */
85 sizeof(size_t), /* BITS */
86 -1, /* KEY_PARAMETERS */
87 0 /* KEY_NO_PARAMETERS */
88};
89
90const int STORE_attr_sizes[STORE_ATTR_TYPE_NUM + 1] = {
91 0,
92 -1, /* FRIENDLYNAME: C string */
93 SHA_DIGEST_LENGTH, /* KEYID: SHA1 digest, 160 bits */
94 SHA_DIGEST_LENGTH, /* ISSUERKEYID: SHA1 digest, 160 bits */
95 SHA_DIGEST_LENGTH, /* SUBJECTKEYID: SHA1 digest, 160 bits */
96 SHA_DIGEST_LENGTH, /* ISSUERSERIALHASH: SHA1 digest, 160 bits */
97 sizeof(X509_NAME *), /* ISSUER: X509_NAME * */
98 sizeof(BIGNUM *), /* SERIAL: BIGNUM * */
99 sizeof(X509_NAME *), /* SUBJECT: X509_NAME * */
100 SHA_DIGEST_LENGTH, /* CERTHASH: SHA1 digest, 160 bits */
101 -1, /* EMAIL: C string */
102 -1, /* FILENAME: C string */
103};
104
105STORE *
106STORE_new_method(const STORE_METHOD *method)
107{
108 STORE *ret;
109
110 if (method == NULL) {
111 STOREerr(STORE_F_STORE_NEW_METHOD, ERR_R_PASSED_NULL_PARAMETER);
112 return NULL;
113 }
114
115 ret = malloc(sizeof(STORE));
116 if (ret == NULL) {
117 STOREerr(STORE_F_STORE_NEW_METHOD, ERR_R_MALLOC_FAILURE);
118 return NULL;
119 }
120
121 ret->meth = method;
122
123 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_STORE, ret, &ret->ex_data);
124 if (ret->meth->init && !ret->meth->init(ret)) {
125 STORE_free(ret);
126 ret = NULL;
127 }
128 return ret;
129}
130
131STORE *
132STORE_new_engine(ENGINE *engine)
133{
134 STORE *ret = NULL;
135 ENGINE *e = engine;
136 const STORE_METHOD *meth = 0;
137
138#ifdef OPENSSL_NO_ENGINE
139 e = NULL;
140#else
141 if (engine) {
142 if (!ENGINE_init(engine)) {
143 STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_ENGINE_LIB);
144 return NULL;
145 }
146 e = engine;
147 } else {
148 STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
149 return NULL;
150 }
151 if (e) {
152 meth = ENGINE_get_STORE(e);
153 if (!meth) {
154 STOREerr(STORE_F_STORE_NEW_ENGINE,
155 ERR_R_ENGINE_LIB);
156 ENGINE_finish(e);
157 return NULL;
158 }
159 }
160#endif
161
162 ret = STORE_new_method(meth);
163 if (ret == NULL) {
164 STOREerr(STORE_F_STORE_NEW_ENGINE, ERR_R_STORE_LIB);
165 return NULL;
166 }
167
168 ret->engine = e;
169
170 return (ret);
171}
172
173void
174STORE_free(STORE *store)
175{
176 if (store == NULL)
177 return;
178 if (store->meth->clean)
179 store->meth->clean(store);
180 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_STORE, store, &store->ex_data);
181 free(store);
182}
183
184int
185STORE_ctrl(STORE *store, int cmd, long i, void *p, void (*f)(void))
186{
187 if (store == NULL) {
188 STOREerr(STORE_F_STORE_CTRL, ERR_R_PASSED_NULL_PARAMETER);
189 return 0;
190 }
191 if (store->meth->ctrl)
192 return store->meth->ctrl(store, cmd, i, p, f);
193 STOREerr(STORE_F_STORE_CTRL, STORE_R_NO_CONTROL_FUNCTION);
194 return 0;
195}
196
197
198int
199STORE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
200 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
201{
202 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_STORE, argl, argp,
203 new_func, dup_func, free_func);
204}
205
206int
207STORE_set_ex_data(STORE *r, int idx, void *arg)
208{
209 return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
210}
211
212void *
213STORE_get_ex_data(STORE *r, int idx)
214{
215 return (CRYPTO_get_ex_data(&r->ex_data, idx));
216}
217
218const STORE_METHOD *
219STORE_get_method(STORE *store)
220{
221 return store->meth;
222}
223
224const STORE_METHOD *
225STORE_set_method(STORE *store, const STORE_METHOD *meth)
226{
227 store->meth = meth;
228 return store->meth;
229}
230
231
232/* API helpers */
233
234#define check_store(s,fncode,fnname,fnerrcode) \
235 do \
236 { \
237 if ((s) == NULL || (s)->meth == NULL) \
238 { \
239 STOREerr((fncode), ERR_R_PASSED_NULL_PARAMETER); \
240 return 0; \
241 } \
242 if ((s)->meth->fnname == NULL) \
243 { \
244 STOREerr((fncode), (fnerrcode)); \
245 return 0; \
246 } \
247 } \
248 while(0)
249
250/* API functions */
251
252X509 *STORE_get_certificate(STORE *s, OPENSSL_ITEM attributes[],
253 OPENSSL_ITEM parameters[])
254{
255 STORE_OBJECT *object;
256 X509 *x;
257
258 check_store(s, STORE_F_STORE_GET_CERTIFICATE,
259 get_object, STORE_R_NO_GET_OBJECT_FUNCTION);
260
261 object = s->meth->get_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
262 attributes, parameters);
263 if (!object || !object->data.x509.certificate) {
264 STOREerr(STORE_F_STORE_GET_CERTIFICATE,
265 STORE_R_FAILED_GETTING_CERTIFICATE);
266 return 0;
267 }
268 CRYPTO_add(&object->data.x509.certificate->references,
269 1, CRYPTO_LOCK_X509);
270 x = object->data.x509.certificate;
271 STORE_OBJECT_free(object);
272 return x;
273}
274
275int
276STORE_store_certificate(STORE *s, X509 *data, OPENSSL_ITEM attributes[],
277 OPENSSL_ITEM parameters[])
278{
279 STORE_OBJECT *object;
280 int i;
281
282 check_store(s, STORE_F_STORE_CERTIFICATE,
283 store_object, STORE_R_NO_STORE_OBJECT_FUNCTION);
284
285 object = STORE_OBJECT_new();
286 if (!object) {
287 STOREerr(STORE_F_STORE_STORE_CERTIFICATE,
288 ERR_R_MALLOC_FAILURE);
289 return 0;
290 }
291
292 CRYPTO_add(&data->references, 1, CRYPTO_LOCK_X509);
293 object->data.x509.certificate = data;
294
295 i = s->meth->store_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
296 object, attributes, parameters);
297
298 STORE_OBJECT_free(object);
299
300 if (!i) {
301 STOREerr(STORE_F_STORE_STORE_CERTIFICATE,
302 STORE_R_FAILED_STORING_CERTIFICATE);
303 return 0;
304 }
305 return 1;
306}
307
308int
309STORE_modify_certificate(STORE *s, OPENSSL_ITEM search_attributes[],
310 OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
311 OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
312{
313 check_store(s, STORE_F_STORE_MODIFY_CERTIFICATE,
314 modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
315
316 if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
317 search_attributes, add_attributes, modify_attributes,
318 delete_attributes, parameters)) {
319 STOREerr(STORE_F_STORE_MODIFY_CERTIFICATE,
320 STORE_R_FAILED_MODIFYING_CERTIFICATE);
321 return 0;
322 }
323 return 1;
324}
325
326int
327STORE_revoke_certificate(STORE *s, OPENSSL_ITEM attributes[],
328 OPENSSL_ITEM parameters[])
329{
330 check_store(s, STORE_F_STORE_REVOKE_CERTIFICATE,
331 revoke_object, STORE_R_NO_REVOKE_OBJECT_FUNCTION);
332
333 if (!s->meth->revoke_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
334 attributes, parameters)) {
335 STOREerr(STORE_F_STORE_REVOKE_CERTIFICATE,
336 STORE_R_FAILED_REVOKING_CERTIFICATE);
337 return 0;
338 }
339 return 1;
340}
341
342int
343STORE_delete_certificate(STORE *s, OPENSSL_ITEM attributes[],
344 OPENSSL_ITEM parameters[])
345{
346 check_store(s, STORE_F_STORE_DELETE_CERTIFICATE,
347 delete_object, STORE_R_NO_DELETE_OBJECT_FUNCTION);
348
349 if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_X509_CERTIFICATE,
350 attributes, parameters)) {
351 STOREerr(STORE_F_STORE_DELETE_CERTIFICATE,
352 STORE_R_FAILED_DELETING_CERTIFICATE);
353 return 0;
354 }
355 return 1;
356}
357
358void *
359STORE_list_certificate_start(STORE *s, OPENSSL_ITEM attributes[],
360 OPENSSL_ITEM parameters[])
361{
362 void *handle;
363
364 check_store(s, STORE_F_STORE_LIST_CERTIFICATE_START,
365 list_object_start, STORE_R_NO_LIST_OBJECT_START_FUNCTION);
366
367 handle = s->meth->list_object_start(s,
368 STORE_OBJECT_TYPE_X509_CERTIFICATE, attributes, parameters);
369 if (!handle) {
370 STOREerr(STORE_F_STORE_LIST_CERTIFICATE_START,
371 STORE_R_FAILED_LISTING_CERTIFICATES);
372 return 0;
373 }
374 return handle;
375}
376
377X509 *
378STORE_list_certificate_next(STORE *s, void *handle)
379{
380 STORE_OBJECT *object;
381 X509 *x;
382
383 check_store(s, STORE_F_STORE_LIST_CERTIFICATE_NEXT,
384 list_object_next, STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
385
386 object = s->meth->list_object_next(s, handle);
387 if (!object || !object->data.x509.certificate) {
388 STOREerr(STORE_F_STORE_LIST_CERTIFICATE_NEXT,
389 STORE_R_FAILED_LISTING_CERTIFICATES);
390 return 0;
391 }
392 CRYPTO_add(&object->data.x509.certificate->references,
393 1, CRYPTO_LOCK_X509);
394 x = object->data.x509.certificate;
395 STORE_OBJECT_free(object);
396 return x;
397}
398
399int
400STORE_list_certificate_end(STORE *s, void *handle)
401{
402 check_store(s, STORE_F_STORE_LIST_CERTIFICATE_END,
403 list_object_end, STORE_R_NO_LIST_OBJECT_END_FUNCTION);
404
405 if (!s->meth->list_object_end(s, handle)) {
406 STOREerr(STORE_F_STORE_LIST_CERTIFICATE_END,
407 STORE_R_FAILED_LISTING_CERTIFICATES);
408 return 0;
409 }
410 return 1;
411}
412
413int
414STORE_list_certificate_endp(STORE *s, void *handle)
415{
416 check_store(s, STORE_F_STORE_LIST_CERTIFICATE_ENDP,
417 list_object_endp, STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
418
419 if (!s->meth->list_object_endp(s, handle)) {
420 STOREerr(STORE_F_STORE_LIST_CERTIFICATE_ENDP,
421 STORE_R_FAILED_LISTING_CERTIFICATES);
422 return 0;
423 }
424 return 1;
425}
426
427EVP_PKEY *
428STORE_generate_key(STORE *s, OPENSSL_ITEM attributes[],
429 OPENSSL_ITEM parameters[])
430{
431 STORE_OBJECT *object;
432 EVP_PKEY *pkey;
433
434 check_store(s, STORE_F_STORE_GENERATE_KEY,
435 generate_object, STORE_R_NO_GENERATE_OBJECT_FUNCTION);
436
437 object = s->meth->generate_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
438 attributes, parameters);
439 if (!object || !object->data.key) {
440 STOREerr(STORE_F_STORE_GENERATE_KEY,
441 STORE_R_FAILED_GENERATING_KEY);
442 return 0;
443 }
444 CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
445 pkey = object->data.key;
446 STORE_OBJECT_free(object);
447 return pkey;
448}
449
450EVP_PKEY *
451STORE_get_private_key(STORE *s, OPENSSL_ITEM attributes[],
452 OPENSSL_ITEM parameters[])
453{
454 STORE_OBJECT *object;
455 EVP_PKEY *pkey;
456
457 check_store(s, STORE_F_STORE_GET_PRIVATE_KEY,
458 get_object, STORE_R_NO_GET_OBJECT_FUNCTION);
459
460 object = s->meth->get_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
461 attributes, parameters);
462 if (!object || !object->data.key) {
463 STOREerr(STORE_F_STORE_GET_PRIVATE_KEY,
464 STORE_R_FAILED_GETTING_KEY);
465 return 0;
466 }
467 CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
468 pkey = object->data.key;
469 STORE_OBJECT_free(object);
470 return pkey;
471}
472
473int
474STORE_store_private_key(STORE *s, EVP_PKEY *data, OPENSSL_ITEM attributes[],
475 OPENSSL_ITEM parameters[])
476{
477 STORE_OBJECT *object;
478 int i;
479
480 check_store(s, STORE_F_STORE_STORE_PRIVATE_KEY,
481 store_object, STORE_R_NO_STORE_OBJECT_FUNCTION);
482
483 object = STORE_OBJECT_new();
484 if (!object) {
485 STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY,
486 ERR_R_MALLOC_FAILURE);
487 return 0;
488 }
489 object->data.key = EVP_PKEY_new();
490 if (!object->data.key) {
491 STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY,
492 ERR_R_MALLOC_FAILURE);
493 return 0;
494 }
495
496 CRYPTO_add(&data->references, 1, CRYPTO_LOCK_EVP_PKEY);
497 object->data.key = data;
498
499 i = s->meth->store_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY, object,
500 attributes, parameters);
501
502 STORE_OBJECT_free(object);
503
504 if (!i) {
505 STOREerr(STORE_F_STORE_STORE_PRIVATE_KEY,
506 STORE_R_FAILED_STORING_KEY);
507 return 0;
508 }
509 return i;
510}
511
512int
513STORE_modify_private_key(STORE *s, OPENSSL_ITEM search_attributes[],
514 OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
515 OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
516{
517 check_store(s, STORE_F_STORE_MODIFY_PRIVATE_KEY,
518 modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
519
520 if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
521 search_attributes, add_attributes, modify_attributes,
522 delete_attributes, parameters)) {
523 STOREerr(STORE_F_STORE_MODIFY_PRIVATE_KEY,
524 STORE_R_FAILED_MODIFYING_PRIVATE_KEY);
525 return 0;
526 }
527 return 1;
528}
529
530int
531STORE_revoke_private_key(STORE *s, OPENSSL_ITEM attributes[],
532 OPENSSL_ITEM parameters[])
533{
534 int i;
535
536 check_store(s, STORE_F_STORE_REVOKE_PRIVATE_KEY,
537 revoke_object, STORE_R_NO_REVOKE_OBJECT_FUNCTION);
538
539 i = s->meth->revoke_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
540 attributes, parameters);
541
542 if (!i) {
543 STOREerr(STORE_F_STORE_REVOKE_PRIVATE_KEY,
544 STORE_R_FAILED_REVOKING_KEY);
545 return 0;
546 }
547 return i;
548}
549
550int
551STORE_delete_private_key(STORE *s, OPENSSL_ITEM attributes[],
552 OPENSSL_ITEM parameters[])
553{
554 check_store(s, STORE_F_STORE_DELETE_PRIVATE_KEY,
555 delete_object, STORE_R_NO_DELETE_OBJECT_FUNCTION);
556
557 if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
558 attributes, parameters)) {
559 STOREerr(STORE_F_STORE_DELETE_PRIVATE_KEY,
560 STORE_R_FAILED_DELETING_KEY);
561 return 0;
562 }
563 return 1;
564}
565
566void *
567STORE_list_private_key_start(STORE *s, OPENSSL_ITEM attributes[],
568 OPENSSL_ITEM parameters[])
569{
570 void *handle;
571
572 check_store(s, STORE_F_STORE_LIST_PRIVATE_KEY_START,
573 list_object_start, STORE_R_NO_LIST_OBJECT_START_FUNCTION);
574
575 handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_PRIVATE_KEY,
576 attributes, parameters);
577 if (!handle) {
578 STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_START,
579 STORE_R_FAILED_LISTING_KEYS);
580 return 0;
581 }
582 return handle;
583}
584
585EVP_PKEY *
586STORE_list_private_key_next(STORE *s, void *handle)
587{
588 STORE_OBJECT *object;
589 EVP_PKEY *pkey;
590
591 check_store(s, STORE_F_STORE_LIST_PRIVATE_KEY_NEXT,
592 list_object_next, STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
593
594 object = s->meth->list_object_next(s, handle);
595 if (!object || !object->data.key) {
596 STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_NEXT,
597 STORE_R_FAILED_LISTING_KEYS);
598 return 0;
599 }
600 CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
601 pkey = object->data.key;
602 STORE_OBJECT_free(object);
603 return pkey;
604}
605
606int
607STORE_list_private_key_end(STORE *s, void *handle)
608{
609 check_store(s, STORE_F_STORE_LIST_PRIVATE_KEY_END,
610 list_object_end, STORE_R_NO_LIST_OBJECT_END_FUNCTION);
611
612 if (!s->meth->list_object_end(s, handle)) {
613 STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_END,
614 STORE_R_FAILED_LISTING_KEYS);
615 return 0;
616 }
617 return 1;
618}
619
620int
621STORE_list_private_key_endp(STORE *s, void *handle)
622{
623 check_store(s, STORE_F_STORE_LIST_PRIVATE_KEY_ENDP,
624 list_object_endp, STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
625
626 if (!s->meth->list_object_endp(s, handle)) {
627 STOREerr(STORE_F_STORE_LIST_PRIVATE_KEY_ENDP,
628 STORE_R_FAILED_LISTING_KEYS);
629 return 0;
630 }
631 return 1;
632}
633
634EVP_PKEY *
635STORE_get_public_key(STORE *s, OPENSSL_ITEM attributes[],
636 OPENSSL_ITEM parameters[])
637{
638 STORE_OBJECT *object;
639 EVP_PKEY *pkey;
640
641 check_store(s, STORE_F_STORE_GET_PUBLIC_KEY,
642 get_object, STORE_R_NO_GET_OBJECT_FUNCTION);
643
644 object = s->meth->get_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
645 attributes, parameters);
646 if (!object || !object->data.key) {
647 STOREerr(STORE_F_STORE_GET_PUBLIC_KEY,
648 STORE_R_FAILED_GETTING_KEY);
649 return 0;
650 }
651 CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
652 pkey = object->data.key;
653 STORE_OBJECT_free(object);
654 return pkey;
655}
656
657int
658STORE_store_public_key(STORE *s, EVP_PKEY *data, OPENSSL_ITEM attributes[],
659 OPENSSL_ITEM parameters[])
660{
661 STORE_OBJECT *object;
662 int i;
663
664 check_store(s, STORE_F_STORE_STORE_PUBLIC_KEY,
665 store_object, STORE_R_NO_STORE_OBJECT_FUNCTION);
666
667 object = STORE_OBJECT_new();
668 if (!object) {
669 STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY,
670 ERR_R_MALLOC_FAILURE);
671 return 0;
672 }
673 object->data.key = EVP_PKEY_new();
674 if (!object->data.key) {
675 STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY,
676 ERR_R_MALLOC_FAILURE);
677 return 0;
678 }
679
680 CRYPTO_add(&data->references, 1, CRYPTO_LOCK_EVP_PKEY);
681 object->data.key = data;
682
683 i = s->meth->store_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY, object,
684 attributes, parameters);
685
686 STORE_OBJECT_free(object);
687
688 if (!i) {
689 STOREerr(STORE_F_STORE_STORE_PUBLIC_KEY,
690 STORE_R_FAILED_STORING_KEY);
691 return 0;
692 }
693 return i;
694}
695
696int
697STORE_modify_public_key(STORE *s, OPENSSL_ITEM search_attributes[],
698 OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
699 OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
700{
701 check_store(s, STORE_F_STORE_MODIFY_PUBLIC_KEY,
702 modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
703
704 if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
705 search_attributes, add_attributes, modify_attributes,
706 delete_attributes, parameters)) {
707 STOREerr(STORE_F_STORE_MODIFY_PUBLIC_KEY,
708 STORE_R_FAILED_MODIFYING_PUBLIC_KEY);
709 return 0;
710 }
711 return 1;
712}
713
714int
715STORE_revoke_public_key(STORE *s, OPENSSL_ITEM attributes[],
716 OPENSSL_ITEM parameters[])
717{
718 int i;
719
720 check_store(s, STORE_F_STORE_REVOKE_PUBLIC_KEY,
721 revoke_object, STORE_R_NO_REVOKE_OBJECT_FUNCTION);
722
723 i = s->meth->revoke_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
724 attributes, parameters);
725
726 if (!i) {
727 STOREerr(STORE_F_STORE_REVOKE_PUBLIC_KEY,
728 STORE_R_FAILED_REVOKING_KEY);
729 return 0;
730 }
731 return i;
732}
733
734int
735STORE_delete_public_key(STORE *s, OPENSSL_ITEM attributes[],
736 OPENSSL_ITEM parameters[])
737{
738 check_store(s, STORE_F_STORE_DELETE_PUBLIC_KEY,
739 delete_object, STORE_R_NO_DELETE_OBJECT_FUNCTION);
740
741 if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
742 attributes, parameters)) {
743 STOREerr(STORE_F_STORE_DELETE_PUBLIC_KEY,
744 STORE_R_FAILED_DELETING_KEY);
745 return 0;
746 }
747 return 1;
748}
749
750void *
751STORE_list_public_key_start(STORE *s, OPENSSL_ITEM attributes[],
752 OPENSSL_ITEM parameters[])
753{
754 void *handle;
755
756 check_store(s, STORE_F_STORE_LIST_PUBLIC_KEY_START,
757 list_object_start, STORE_R_NO_LIST_OBJECT_START_FUNCTION);
758
759 handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_PUBLIC_KEY,
760 attributes, parameters);
761 if (!handle) {
762 STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_START,
763 STORE_R_FAILED_LISTING_KEYS);
764 return 0;
765 }
766 return handle;
767}
768
769EVP_PKEY *
770STORE_list_public_key_next(STORE *s, void *handle)
771{
772 STORE_OBJECT *object;
773 EVP_PKEY *pkey;
774
775 check_store(s, STORE_F_STORE_LIST_PUBLIC_KEY_NEXT,
776 list_object_next, STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
777
778 object = s->meth->list_object_next(s, handle);
779 if (!object || !object->data.key) {
780 STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_NEXT,
781 STORE_R_FAILED_LISTING_KEYS);
782 return 0;
783 }
784 CRYPTO_add(&object->data.key->references, 1, CRYPTO_LOCK_EVP_PKEY);
785 pkey = object->data.key;
786 STORE_OBJECT_free(object);
787 return pkey;
788}
789
790int
791STORE_list_public_key_end(STORE *s, void *handle)
792{
793 check_store(s, STORE_F_STORE_LIST_PUBLIC_KEY_END,
794 list_object_end, STORE_R_NO_LIST_OBJECT_END_FUNCTION);
795
796 if (!s->meth->list_object_end(s, handle)) {
797 STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_END,
798 STORE_R_FAILED_LISTING_KEYS);
799 return 0;
800 }
801 return 1;
802}
803
804int
805STORE_list_public_key_endp(STORE *s, void *handle)
806{
807 check_store(s, STORE_F_STORE_LIST_PUBLIC_KEY_ENDP,
808 list_object_endp, STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
809
810 if (!s->meth->list_object_endp(s, handle)) {
811 STOREerr(STORE_F_STORE_LIST_PUBLIC_KEY_ENDP,
812 STORE_R_FAILED_LISTING_KEYS);
813 return 0;
814 }
815 return 1;
816}
817
818X509_CRL *
819STORE_generate_crl(STORE *s, OPENSSL_ITEM attributes[],
820 OPENSSL_ITEM parameters[])
821{
822 STORE_OBJECT *object;
823 X509_CRL *crl;
824
825 check_store(s, STORE_F_STORE_GENERATE_CRL,
826 generate_object, STORE_R_NO_GENERATE_CRL_FUNCTION);
827
828 object = s->meth->generate_object(s, STORE_OBJECT_TYPE_X509_CRL,
829 attributes, parameters);
830 if (!object || !object->data.crl) {
831 STOREerr(STORE_F_STORE_GENERATE_CRL,
832 STORE_R_FAILED_GENERATING_CRL);
833 return 0;
834 }
835 CRYPTO_add(&object->data.crl->references, 1, CRYPTO_LOCK_X509_CRL);
836 crl = object->data.crl;
837 STORE_OBJECT_free(object);
838 return crl;
839}
840
841X509_CRL *
842STORE_get_crl(STORE *s, OPENSSL_ITEM attributes[],
843 OPENSSL_ITEM parameters[])
844{
845 STORE_OBJECT *object;
846 X509_CRL *crl;
847
848 check_store(s, STORE_F_STORE_GET_CRL,
849 get_object, STORE_R_NO_GET_OBJECT_FUNCTION);
850
851 object = s->meth->get_object(s, STORE_OBJECT_TYPE_X509_CRL,
852 attributes, parameters);
853 if (!object || !object->data.crl) {
854 STOREerr(STORE_F_STORE_GET_CRL,
855 STORE_R_FAILED_GETTING_KEY);
856 return 0;
857 }
858 CRYPTO_add(&object->data.crl->references, 1, CRYPTO_LOCK_X509_CRL);
859 crl = object->data.crl;
860 STORE_OBJECT_free(object);
861 return crl;
862}
863
864int
865STORE_store_crl(STORE *s, X509_CRL *data, OPENSSL_ITEM attributes[],
866 OPENSSL_ITEM parameters[])
867{
868 STORE_OBJECT *object;
869 int i;
870
871 check_store(s, STORE_F_STORE_STORE_CRL,
872 store_object, STORE_R_NO_STORE_OBJECT_FUNCTION);
873
874 object = STORE_OBJECT_new();
875 if (!object) {
876 STOREerr(STORE_F_STORE_STORE_CRL,
877 ERR_R_MALLOC_FAILURE);
878 return 0;
879 }
880
881 CRYPTO_add(&data->references, 1, CRYPTO_LOCK_X509_CRL);
882 object->data.crl = data;
883
884 i = s->meth->store_object(s, STORE_OBJECT_TYPE_X509_CRL, object,
885 attributes, parameters);
886
887 STORE_OBJECT_free(object);
888
889 if (!i) {
890 STOREerr(STORE_F_STORE_STORE_CRL,
891 STORE_R_FAILED_STORING_KEY);
892 return 0;
893 }
894 return i;
895}
896
897int
898STORE_modify_crl(STORE *s, OPENSSL_ITEM search_attributes[],
899 OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
900 OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
901{
902 check_store(s, STORE_F_STORE_MODIFY_CRL,
903 modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
904
905 if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_X509_CRL,
906 search_attributes, add_attributes, modify_attributes,
907 delete_attributes, parameters)) {
908 STOREerr(STORE_F_STORE_MODIFY_CRL,
909 STORE_R_FAILED_MODIFYING_CRL);
910 return 0;
911 }
912 return 1;
913}
914
915int
916STORE_delete_crl(STORE *s, OPENSSL_ITEM attributes[],
917 OPENSSL_ITEM parameters[])
918{
919 check_store(s, STORE_F_STORE_DELETE_CRL,
920 delete_object, STORE_R_NO_DELETE_OBJECT_FUNCTION);
921
922 if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_X509_CRL,
923 attributes, parameters)) {
924 STOREerr(STORE_F_STORE_DELETE_CRL,
925 STORE_R_FAILED_DELETING_KEY);
926 return 0;
927 }
928 return 1;
929}
930
931void *
932STORE_list_crl_start(STORE *s, OPENSSL_ITEM attributes[],
933 OPENSSL_ITEM parameters[])
934{
935 void *handle;
936
937 check_store(s, STORE_F_STORE_LIST_CRL_START,
938 list_object_start, STORE_R_NO_LIST_OBJECT_START_FUNCTION);
939
940 handle = s->meth->list_object_start(s, STORE_OBJECT_TYPE_X509_CRL,
941 attributes, parameters);
942 if (!handle) {
943 STOREerr(STORE_F_STORE_LIST_CRL_START,
944 STORE_R_FAILED_LISTING_KEYS);
945 return 0;
946 }
947 return handle;
948}
949
950X509_CRL *
951STORE_list_crl_next(STORE *s, void *handle)
952{
953 STORE_OBJECT *object;
954 X509_CRL *crl;
955
956 check_store(s, STORE_F_STORE_LIST_CRL_NEXT,
957 list_object_next, STORE_R_NO_LIST_OBJECT_NEXT_FUNCTION);
958
959 object = s->meth->list_object_next(s, handle);
960 if (!object || !object->data.crl) {
961 STOREerr(STORE_F_STORE_LIST_CRL_NEXT,
962 STORE_R_FAILED_LISTING_KEYS);
963 return 0;
964 }
965 CRYPTO_add(&object->data.crl->references, 1, CRYPTO_LOCK_X509_CRL);
966 crl = object->data.crl;
967 STORE_OBJECT_free(object);
968 return crl;
969}
970
971int
972STORE_list_crl_end(STORE *s, void *handle)
973{
974 check_store(s, STORE_F_STORE_LIST_CRL_END,
975 list_object_end, STORE_R_NO_LIST_OBJECT_END_FUNCTION);
976
977 if (!s->meth->list_object_end(s, handle)) {
978 STOREerr(STORE_F_STORE_LIST_CRL_END,
979 STORE_R_FAILED_LISTING_KEYS);
980 return 0;
981 }
982 return 1;
983}
984
985int
986STORE_list_crl_endp(STORE *s, void *handle)
987{
988 check_store(s, STORE_F_STORE_LIST_CRL_ENDP,
989 list_object_endp, STORE_R_NO_LIST_OBJECT_ENDP_FUNCTION);
990
991 if (!s->meth->list_object_endp(s, handle)) {
992 STOREerr(STORE_F_STORE_LIST_CRL_ENDP,
993 STORE_R_FAILED_LISTING_KEYS);
994 return 0;
995 }
996 return 1;
997}
998
999int
1000STORE_store_number(STORE *s, BIGNUM *data, OPENSSL_ITEM attributes[],
1001 OPENSSL_ITEM parameters[])
1002{
1003 STORE_OBJECT *object;
1004 int i;
1005
1006 check_store(s, STORE_F_STORE_STORE_NUMBER,
1007 store_object, STORE_R_NO_STORE_OBJECT_NUMBER_FUNCTION);
1008
1009 object = STORE_OBJECT_new();
1010 if (!object) {
1011 STOREerr(STORE_F_STORE_STORE_NUMBER,
1012 ERR_R_MALLOC_FAILURE);
1013 return 0;
1014 }
1015
1016 object->data.number = data;
1017
1018 i = s->meth->store_object(s, STORE_OBJECT_TYPE_NUMBER, object,
1019 attributes, parameters);
1020
1021 STORE_OBJECT_free(object);
1022
1023 if (!i) {
1024 STOREerr(STORE_F_STORE_STORE_NUMBER,
1025 STORE_R_FAILED_STORING_NUMBER);
1026 return 0;
1027 }
1028 return 1;
1029}
1030
1031int
1032STORE_modify_number(STORE *s, OPENSSL_ITEM search_attributes[],
1033 OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
1034 OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
1035{
1036 check_store(s, STORE_F_STORE_MODIFY_NUMBER,
1037 modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
1038
1039 if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_NUMBER,
1040 search_attributes, add_attributes, modify_attributes,
1041 delete_attributes, parameters)) {
1042 STOREerr(STORE_F_STORE_MODIFY_NUMBER,
1043 STORE_R_FAILED_MODIFYING_NUMBER);
1044 return 0;
1045 }
1046 return 1;
1047}
1048
1049BIGNUM *
1050STORE_get_number(STORE *s, OPENSSL_ITEM attributes[],
1051 OPENSSL_ITEM parameters[])
1052{
1053 STORE_OBJECT *object;
1054 BIGNUM *n;
1055
1056 check_store(s, STORE_F_STORE_GET_NUMBER,
1057 get_object, STORE_R_NO_GET_OBJECT_NUMBER_FUNCTION);
1058
1059 object = s->meth->get_object(s, STORE_OBJECT_TYPE_NUMBER, attributes,
1060 parameters);
1061 if (!object || !object->data.number) {
1062 STOREerr(STORE_F_STORE_GET_NUMBER,
1063 STORE_R_FAILED_GETTING_NUMBER);
1064 return 0;
1065 }
1066 n = object->data.number;
1067 object->data.number = NULL;
1068 STORE_OBJECT_free(object);
1069 return n;
1070}
1071
1072int
1073STORE_delete_number(STORE *s, OPENSSL_ITEM attributes[],
1074 OPENSSL_ITEM parameters[])
1075{
1076 check_store(s, STORE_F_STORE_DELETE_NUMBER,
1077 delete_object, STORE_R_NO_DELETE_NUMBER_FUNCTION);
1078
1079 if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_NUMBER, attributes,
1080 parameters)) {
1081 STOREerr(STORE_F_STORE_DELETE_NUMBER,
1082 STORE_R_FAILED_DELETING_NUMBER);
1083 return 0;
1084 }
1085 return 1;
1086}
1087
1088int
1089STORE_store_arbitrary(STORE *s, BUF_MEM *data, OPENSSL_ITEM attributes[],
1090 OPENSSL_ITEM parameters[])
1091{
1092 STORE_OBJECT *object;
1093 int i;
1094
1095 check_store(s, STORE_F_STORE_STORE_ARBITRARY,
1096 store_object, STORE_R_NO_STORE_OBJECT_ARBITRARY_FUNCTION);
1097
1098 object = STORE_OBJECT_new();
1099 if (!object) {
1100 STOREerr(STORE_F_STORE_STORE_ARBITRARY,
1101 ERR_R_MALLOC_FAILURE);
1102 return 0;
1103 }
1104
1105 object->data.arbitrary = data;
1106
1107 i = s->meth->store_object(s, STORE_OBJECT_TYPE_ARBITRARY, object,
1108 attributes, parameters);
1109
1110 STORE_OBJECT_free(object);
1111
1112 if (!i) {
1113 STOREerr(STORE_F_STORE_STORE_ARBITRARY,
1114 STORE_R_FAILED_STORING_ARBITRARY);
1115 return 0;
1116 }
1117 return 1;
1118}
1119
1120int
1121STORE_modify_arbitrary(STORE *s, OPENSSL_ITEM search_attributes[],
1122 OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
1123 OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
1124{
1125 check_store(s, STORE_F_STORE_MODIFY_ARBITRARY,
1126 modify_object, STORE_R_NO_MODIFY_OBJECT_FUNCTION);
1127
1128 if (!s->meth->modify_object(s, STORE_OBJECT_TYPE_ARBITRARY,
1129 search_attributes, add_attributes, modify_attributes,
1130 delete_attributes, parameters)) {
1131 STOREerr(STORE_F_STORE_MODIFY_ARBITRARY,
1132 STORE_R_FAILED_MODIFYING_ARBITRARY);
1133 return 0;
1134 }
1135 return 1;
1136}
1137
1138BUF_MEM *
1139STORE_get_arbitrary(STORE *s, OPENSSL_ITEM attributes[],
1140 OPENSSL_ITEM parameters[])
1141{
1142 STORE_OBJECT *object;
1143 BUF_MEM *b;
1144
1145 check_store(s, STORE_F_STORE_GET_ARBITRARY,
1146 get_object, STORE_R_NO_GET_OBJECT_ARBITRARY_FUNCTION);
1147
1148 object = s->meth->get_object(s, STORE_OBJECT_TYPE_ARBITRARY,
1149 attributes, parameters);
1150 if (!object || !object->data.arbitrary) {
1151 STOREerr(STORE_F_STORE_GET_ARBITRARY,
1152 STORE_R_FAILED_GETTING_ARBITRARY);
1153 return 0;
1154 }
1155 b = object->data.arbitrary;
1156 object->data.arbitrary = NULL;
1157 STORE_OBJECT_free(object);
1158 return b;
1159}
1160
1161int
1162STORE_delete_arbitrary(STORE *s, OPENSSL_ITEM attributes[],
1163 OPENSSL_ITEM parameters[])
1164{
1165 check_store(s, STORE_F_STORE_DELETE_ARBITRARY,
1166 delete_object, STORE_R_NO_DELETE_ARBITRARY_FUNCTION);
1167
1168 if (!s->meth->delete_object(s, STORE_OBJECT_TYPE_ARBITRARY, attributes,
1169 parameters)) {
1170 STOREerr(STORE_F_STORE_DELETE_ARBITRARY,
1171 STORE_R_FAILED_DELETING_ARBITRARY);
1172 return 0;
1173 }
1174 return 1;
1175}
1176
1177STORE_OBJECT *
1178STORE_OBJECT_new(void)
1179{
1180 return calloc(1, sizeof(STORE_OBJECT));
1181}
1182
1183void
1184STORE_OBJECT_free(STORE_OBJECT *data)
1185{
1186 if (!data)
1187 return;
1188 switch (data->type) {
1189 case STORE_OBJECT_TYPE_X509_CERTIFICATE:
1190 X509_free(data->data.x509.certificate);
1191 break;
1192 case STORE_OBJECT_TYPE_X509_CRL:
1193 X509_CRL_free(data->data.crl);
1194 break;
1195 case STORE_OBJECT_TYPE_PRIVATE_KEY:
1196 case STORE_OBJECT_TYPE_PUBLIC_KEY:
1197 EVP_PKEY_free(data->data.key);
1198 break;
1199 case STORE_OBJECT_TYPE_NUMBER:
1200 BN_free(data->data.number);
1201 break;
1202 case STORE_OBJECT_TYPE_ARBITRARY:
1203 BUF_MEM_free(data->data.arbitrary);
1204 break;
1205 }
1206 free(data);
1207}
1208
1209IMPLEMENT_STACK_OF(STORE_OBJECT*)
1210
1211
1212struct STORE_attr_info_st {
1213 unsigned char set[(STORE_ATTR_TYPE_NUM + 8) / 8];
1214 union {
1215 char *cstring;
1216 unsigned char *sha1string;
1217 X509_NAME *dn;
1218 BIGNUM *number;
1219 void *any;
1220 } values[STORE_ATTR_TYPE_NUM + 1];
1221 size_t value_sizes[STORE_ATTR_TYPE_NUM + 1];
1222};
1223
1224#define ATTR_IS_SET(a,i) ((i) > 0 && (i) < STORE_ATTR_TYPE_NUM \
1225 && ((a)->set[(i) / 8] & (1 << ((i) % 8))))
1226#define SET_ATTRBIT(a,i) ((a)->set[(i) / 8] |= (1 << ((i) % 8)))
1227#define CLEAR_ATTRBIT(a,i) ((a)->set[(i) / 8] &= ~(1 << ((i) % 8)))
1228
1229STORE_ATTR_INFO *
1230STORE_ATTR_INFO_new(void)
1231{
1232 return malloc(sizeof(STORE_ATTR_INFO));
1233}
1234
1235static void
1236STORE_ATTR_INFO_attr_free(STORE_ATTR_INFO *attrs,
1237 STORE_ATTR_TYPES code)
1238{
1239 if (ATTR_IS_SET(attrs, code)) {
1240 switch (code) {
1241 case STORE_ATTR_FRIENDLYNAME:
1242 case STORE_ATTR_EMAIL:
1243 case STORE_ATTR_FILENAME:
1244 STORE_ATTR_INFO_modify_cstr(attrs, code, NULL, 0);
1245 break;
1246 case STORE_ATTR_KEYID:
1247 case STORE_ATTR_ISSUERKEYID:
1248 case STORE_ATTR_SUBJECTKEYID:
1249 case STORE_ATTR_ISSUERSERIALHASH:
1250 case STORE_ATTR_CERTHASH:
1251 STORE_ATTR_INFO_modify_sha1str(attrs, code, NULL, 0);
1252 break;
1253 case STORE_ATTR_ISSUER:
1254 case STORE_ATTR_SUBJECT:
1255 STORE_ATTR_INFO_modify_dn(attrs, code, NULL);
1256 break;
1257 case STORE_ATTR_SERIAL:
1258 STORE_ATTR_INFO_modify_number(attrs, code, NULL);
1259 break;
1260 default:
1261 break;
1262 }
1263 }
1264}
1265
1266int
1267STORE_ATTR_INFO_free(STORE_ATTR_INFO *attrs)
1268{
1269 if (attrs) {
1270 STORE_ATTR_TYPES i;
1271 for (i = 0; i++ < STORE_ATTR_TYPE_NUM; )
1272 STORE_ATTR_INFO_attr_free(attrs, i);
1273 free(attrs);
1274 }
1275 return 1;
1276}
1277
1278char *
1279STORE_ATTR_INFO_get0_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code)
1280{
1281 if (!attrs) {
1282 STOREerr(STORE_F_STORE_ATTR_INFO_GET0_CSTR,
1283 ERR_R_PASSED_NULL_PARAMETER);
1284 return NULL;
1285 }
1286 if (ATTR_IS_SET(attrs, code))
1287 return attrs->values[code].cstring;
1288 STOREerr(STORE_F_STORE_ATTR_INFO_GET0_CSTR, STORE_R_NO_VALUE);
1289 return NULL;
1290}
1291
1292unsigned char *
1293STORE_ATTR_INFO_get0_sha1str(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code)
1294{
1295 if (!attrs) {
1296 STOREerr(STORE_F_STORE_ATTR_INFO_GET0_SHA1STR,
1297 ERR_R_PASSED_NULL_PARAMETER);
1298 return NULL;
1299 }
1300 if (ATTR_IS_SET(attrs, code))
1301 return attrs->values[code].sha1string;
1302 STOREerr(STORE_F_STORE_ATTR_INFO_GET0_SHA1STR, STORE_R_NO_VALUE);
1303 return NULL;
1304}
1305
1306X509_NAME *
1307STORE_ATTR_INFO_get0_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code)
1308{
1309 if (!attrs) {
1310 STOREerr(STORE_F_STORE_ATTR_INFO_GET0_DN,
1311 ERR_R_PASSED_NULL_PARAMETER);
1312 return NULL;
1313 }
1314 if (ATTR_IS_SET(attrs, code))
1315 return attrs->values[code].dn;
1316 STOREerr(STORE_F_STORE_ATTR_INFO_GET0_DN, STORE_R_NO_VALUE);
1317 return NULL;
1318}
1319
1320BIGNUM *
1321STORE_ATTR_INFO_get0_number(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code)
1322{
1323 if (!attrs) {
1324 STOREerr(STORE_F_STORE_ATTR_INFO_GET0_NUMBER,
1325 ERR_R_PASSED_NULL_PARAMETER);
1326 return NULL;
1327 }
1328 if (ATTR_IS_SET(attrs, code))
1329 return attrs->values[code].number;
1330 STOREerr(STORE_F_STORE_ATTR_INFO_GET0_NUMBER, STORE_R_NO_VALUE);
1331 return NULL;
1332}
1333
1334int
1335STORE_ATTR_INFO_set_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1336 char *cstr, size_t cstr_size)
1337{
1338 if (!attrs) {
1339 STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR,
1340 ERR_R_PASSED_NULL_PARAMETER);
1341 return 0;
1342 }
1343 if (!ATTR_IS_SET(attrs, code)) {
1344 if ((attrs->values[code].cstring = strndup(cstr, cstr_size)))
1345 return 1;
1346 STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR,
1347 ERR_R_MALLOC_FAILURE);
1348 return 0;
1349 }
1350 STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR, STORE_R_ALREADY_HAS_A_VALUE);
1351 return 0;
1352}
1353
1354int
1355STORE_ATTR_INFO_set_sha1str(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1356 unsigned char *sha1str, size_t sha1str_size)
1357{
1358 if (!attrs) {
1359 STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR,
1360 ERR_R_PASSED_NULL_PARAMETER);
1361 return 0;
1362 }
1363 if (!ATTR_IS_SET(attrs, code)) {
1364 if ((attrs->values[code].sha1string =
1365 (unsigned char *)BUF_memdup(sha1str,
1366 sha1str_size)))
1367 return 1;
1368 STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR,
1369 ERR_R_MALLOC_FAILURE);
1370 return 0;
1371 }
1372 STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR,
1373 STORE_R_ALREADY_HAS_A_VALUE);
1374 return 0;
1375}
1376
1377int
1378STORE_ATTR_INFO_set_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1379 X509_NAME *dn)
1380{
1381 if (!attrs) {
1382 STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN,
1383 ERR_R_PASSED_NULL_PARAMETER);
1384 return 0;
1385 }
1386 if (!ATTR_IS_SET(attrs, code)) {
1387 if ((attrs->values[code].dn = X509_NAME_dup(dn)))
1388 return 1;
1389 STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN,
1390 ERR_R_MALLOC_FAILURE);
1391 return 0;
1392 }
1393 STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN, STORE_R_ALREADY_HAS_A_VALUE);
1394 return 0;
1395}
1396
1397int
1398STORE_ATTR_INFO_set_number(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1399 BIGNUM *number)
1400{
1401 if (!attrs) {
1402 STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER,
1403 ERR_R_PASSED_NULL_PARAMETER);
1404 return 0;
1405 }
1406 if (!ATTR_IS_SET(attrs, code)) {
1407 if ((attrs->values[code].number = BN_dup(number)))
1408 return 1;
1409 STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER,
1410 ERR_R_MALLOC_FAILURE);
1411 return 0;
1412 }
1413 STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER,
1414 STORE_R_ALREADY_HAS_A_VALUE);
1415 return 0;
1416}
1417
1418int
1419STORE_ATTR_INFO_modify_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1420 char *cstr, size_t cstr_size)
1421{
1422 if (!attrs) {
1423 STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_CSTR,
1424 ERR_R_PASSED_NULL_PARAMETER);
1425 return 0;
1426 }
1427 if (ATTR_IS_SET(attrs, code)) {
1428 free(attrs->values[code].cstring);
1429 attrs->values[code].cstring = NULL;
1430 CLEAR_ATTRBIT(attrs, code);
1431 }
1432 return STORE_ATTR_INFO_set_cstr(attrs, code, cstr, cstr_size);
1433}
1434
1435int
1436STORE_ATTR_INFO_modify_sha1str(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1437 unsigned char *sha1str, size_t sha1str_size)
1438{
1439 if (!attrs) {
1440 STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_SHA1STR,
1441 ERR_R_PASSED_NULL_PARAMETER);
1442 return 0;
1443 }
1444 if (ATTR_IS_SET(attrs, code)) {
1445 free(attrs->values[code].sha1string);
1446 attrs->values[code].sha1string = NULL;
1447 CLEAR_ATTRBIT(attrs, code);
1448 }
1449 return STORE_ATTR_INFO_set_sha1str(attrs, code, sha1str, sha1str_size);
1450}
1451
1452int
1453STORE_ATTR_INFO_modify_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1454 X509_NAME *dn)
1455{
1456 if (!attrs) {
1457 STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_DN,
1458 ERR_R_PASSED_NULL_PARAMETER);
1459 return 0;
1460 }
1461 if (ATTR_IS_SET(attrs, code)) {
1462 free(attrs->values[code].dn);
1463 attrs->values[code].dn = NULL;
1464 CLEAR_ATTRBIT(attrs, code);
1465 }
1466 return STORE_ATTR_INFO_set_dn(attrs, code, dn);
1467}
1468
1469int
1470STORE_ATTR_INFO_modify_number(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code,
1471 BIGNUM *number)
1472{
1473 if (!attrs) {
1474 STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_NUMBER,
1475 ERR_R_PASSED_NULL_PARAMETER);
1476 return 0;
1477 }
1478 if (ATTR_IS_SET(attrs, code)) {
1479 free(attrs->values[code].number);
1480 attrs->values[code].number = NULL;
1481 CLEAR_ATTRBIT(attrs, code);
1482 }
1483 return STORE_ATTR_INFO_set_number(attrs, code, number);
1484}
1485
1486struct attr_list_ctx_st {
1487 OPENSSL_ITEM *attributes;
1488};
1489
1490void *
1491STORE_parse_attrs_start(OPENSSL_ITEM *attributes)
1492{
1493 if (attributes) {
1494 struct attr_list_ctx_st *context =
1495 malloc(sizeof(struct attr_list_ctx_st));
1496
1497 if (context)
1498 context->attributes = attributes;
1499 else
1500 STOREerr(STORE_F_STORE_PARSE_ATTRS_START,
1501 ERR_R_MALLOC_FAILURE);
1502 return context;
1503 }
1504 STOREerr(STORE_F_STORE_PARSE_ATTRS_START, ERR_R_PASSED_NULL_PARAMETER);
1505 return 0;
1506}
1507
1508STORE_ATTR_INFO *
1509STORE_parse_attrs_next(void *handle)
1510{
1511 struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;
1512
1513 if (context && context->attributes) {
1514 STORE_ATTR_INFO *attrs = NULL;
1515
1516 while (context->attributes &&
1517 context->attributes->code != STORE_ATTR_OR &&
1518 context->attributes->code != STORE_ATTR_END) {
1519 switch (context->attributes->code) {
1520 case STORE_ATTR_FRIENDLYNAME:
1521 case STORE_ATTR_EMAIL:
1522 case STORE_ATTR_FILENAME:
1523 if (!attrs)
1524 attrs = STORE_ATTR_INFO_new();
1525 if (attrs == NULL) {
1526 STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
1527 ERR_R_MALLOC_FAILURE);
1528 goto err;
1529 }
1530 STORE_ATTR_INFO_set_cstr(attrs,
1531 context->attributes->code,
1532 context->attributes->value,
1533 context->attributes->value_size);
1534 break;
1535 case STORE_ATTR_KEYID:
1536 case STORE_ATTR_ISSUERKEYID:
1537 case STORE_ATTR_SUBJECTKEYID:
1538 case STORE_ATTR_ISSUERSERIALHASH:
1539 case STORE_ATTR_CERTHASH:
1540 if (!attrs)
1541 attrs = STORE_ATTR_INFO_new();
1542 if (attrs == NULL) {
1543 STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
1544 ERR_R_MALLOC_FAILURE);
1545 goto err;
1546 }
1547 STORE_ATTR_INFO_set_sha1str(attrs,
1548 context->attributes->code,
1549 context->attributes->value,
1550 context->attributes->value_size);
1551 break;
1552 case STORE_ATTR_ISSUER:
1553 case STORE_ATTR_SUBJECT:
1554 if (!attrs)
1555 attrs = STORE_ATTR_INFO_new();
1556 if (attrs == NULL) {
1557 STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
1558 ERR_R_MALLOC_FAILURE);
1559 goto err;
1560 }
1561 STORE_ATTR_INFO_modify_dn(attrs,
1562 context->attributes->code,
1563 context->attributes->value);
1564 break;
1565 case STORE_ATTR_SERIAL:
1566 if (!attrs)
1567 attrs = STORE_ATTR_INFO_new();
1568 if (attrs == NULL) {
1569 STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT,
1570 ERR_R_MALLOC_FAILURE);
1571 goto err;
1572 }
1573 STORE_ATTR_INFO_modify_number(attrs,
1574 context->attributes->code,
1575 context->attributes->value);
1576 break;
1577 }
1578 context->attributes++;
1579 }
1580 if (context->attributes->code == STORE_ATTR_OR)
1581 context->attributes++;
1582 return attrs;
1583
1584err:
1585 while (context->attributes &&
1586 context->attributes->code != STORE_ATTR_OR &&
1587 context->attributes->code != STORE_ATTR_END)
1588 context->attributes++;
1589 if (context->attributes->code == STORE_ATTR_OR)
1590 context->attributes++;
1591 return NULL;
1592 }
1593 STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT, ERR_R_PASSED_NULL_PARAMETER);
1594 return NULL;
1595}
1596
1597int
1598STORE_parse_attrs_end(void *handle)
1599{
1600 struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;
1601
1602 if (context && context->attributes) {
1603#if 0
1604 OPENSSL_ITEM *attributes = context->attributes;
1605#endif
1606 free(context);
1607 return 1;
1608 }
1609 STOREerr(STORE_F_STORE_PARSE_ATTRS_END, ERR_R_PASSED_NULL_PARAMETER);
1610 return 0;
1611}
1612
1613int
1614STORE_parse_attrs_endp(void *handle)
1615{
1616 struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle;
1617
1618 if (context && context->attributes) {
1619 return context->attributes->code == STORE_ATTR_END;
1620 }
1621 STOREerr(STORE_F_STORE_PARSE_ATTRS_ENDP, ERR_R_PASSED_NULL_PARAMETER);
1622 return 0;
1623}
1624
1625static int
1626attr_info_compare_compute_range(const unsigned char *abits,
1627 const unsigned char *bbits, unsigned int *alowp, unsigned int *ahighp,
1628 unsigned int *blowp, unsigned int *bhighp)
1629{
1630 unsigned int alow = (unsigned int) - 1, ahigh = 0;
1631 unsigned int blow = (unsigned int) - 1, bhigh = 0;
1632 int i, res = 0;
1633
1634 for (i = 0; i < (STORE_ATTR_TYPE_NUM + 8) / 8; i++, abits++, bbits++) {
1635 if (res == 0) {
1636 if (*abits < *bbits)
1637 res = -1;
1638 if (*abits > *bbits)
1639 res = 1;
1640 }
1641 if (*abits) {
1642 if (alow == (unsigned int) - 1) {
1643 alow = i * 8;
1644 if (!(*abits & 0x01))
1645 alow++;
1646 if (!(*abits & 0x02))
1647 alow++;
1648 if (!(*abits & 0x04))
1649 alow++;
1650 if (!(*abits & 0x08))
1651 alow++;
1652 if (!(*abits & 0x10))
1653 alow++;
1654 if (!(*abits & 0x20))
1655 alow++;
1656 if (!(*abits & 0x40))
1657 alow++;
1658 }
1659 ahigh = i * 8 + 7;
1660 if (!(*abits & 0x80))
1661 ahigh++;
1662 if (!(*abits & 0x40))
1663 ahigh++;
1664 if (!(*abits & 0x20))
1665 ahigh++;
1666 if (!(*abits & 0x10))
1667 ahigh++;
1668 if (!(*abits & 0x08))
1669 ahigh++;
1670 if (!(*abits & 0x04))
1671 ahigh++;
1672 if (!(*abits & 0x02))
1673 ahigh++;
1674 }
1675 if (*bbits) {
1676 if (blow == (unsigned int) - 1) {
1677 blow = i * 8;
1678 if (!(*bbits & 0x01))
1679 blow++;
1680 if (!(*bbits & 0x02))
1681 blow++;
1682 if (!(*bbits & 0x04))
1683 blow++;
1684 if (!(*bbits & 0x08))
1685 blow++;
1686 if (!(*bbits & 0x10))
1687 blow++;
1688 if (!(*bbits & 0x20))
1689 blow++;
1690 if (!(*bbits & 0x40))
1691 blow++;
1692 }
1693 bhigh = i * 8 + 7;
1694 if (!(*bbits & 0x80))
1695 bhigh++;
1696 if (!(*bbits & 0x40))
1697 bhigh++;
1698 if (!(*bbits & 0x20))
1699 bhigh++;
1700 if (!(*bbits & 0x10))
1701 bhigh++;
1702 if (!(*bbits & 0x08))
1703 bhigh++;
1704 if (!(*bbits & 0x04))
1705 bhigh++;
1706 if (!(*bbits & 0x02))
1707 bhigh++;
1708 }
1709 }
1710 if (ahigh + alow < bhigh + blow)
1711 res = -1;
1712 if (ahigh + alow > bhigh + blow)
1713 res = 1;
1714 if (alowp)
1715 *alowp = alow;
1716 if (ahighp)
1717 *ahighp = ahigh;
1718 if (blowp)
1719 *blowp = blow;
1720 if (bhighp)
1721 *bhighp = bhigh;
1722 return res;
1723}
1724
1725int
1726STORE_ATTR_INFO_compare(const STORE_ATTR_INFO * const *a,
1727 const STORE_ATTR_INFO * const *b)
1728{
1729 if (a == b)
1730 return 0;
1731 if (!a)
1732 return -1;
1733 if (!b)
1734 return 1;
1735 return attr_info_compare_compute_range((*a)->set, (*b)->set,
1736 0, 0, 0, 0);
1737}
1738
1739int
1740STORE_ATTR_INFO_in_range(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
1741{
1742 unsigned int alow, ahigh, blow, bhigh;
1743
1744 if (a == b)
1745 return 1;
1746 if (!a)
1747 return 0;
1748 if (!b)
1749 return 0;
1750 attr_info_compare_compute_range(a->set, b->set,
1751 &alow, &ahigh, &blow, &bhigh);
1752 if (alow >= blow && ahigh <= bhigh)
1753 return 1;
1754 return 0;
1755}
1756
1757int
1758STORE_ATTR_INFO_in(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
1759{
1760 unsigned char *abits, *bbits;
1761 int i;
1762
1763 if (a == b)
1764 return 1;
1765 if (!a)
1766 return 0;
1767 if (!b)
1768 return 0;
1769 abits = a->set;
1770 bbits = b->set;
1771 for (i = 0; i < (STORE_ATTR_TYPE_NUM + 8) / 8; i++, abits++, bbits++) {
1772 if (*abits && (*bbits & *abits) != *abits)
1773 return 0;
1774 }
1775 return 1;
1776}
1777
1778int
1779STORE_ATTR_INFO_in_ex(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b)
1780{
1781 STORE_ATTR_TYPES i;
1782
1783 if (a == b)
1784 return 1;
1785 if (!STORE_ATTR_INFO_in(a, b))
1786 return 0;
1787 for (i = 1; i < STORE_ATTR_TYPE_NUM; i++)
1788 if (ATTR_IS_SET(a, i)) {
1789 switch (i) {
1790 case STORE_ATTR_FRIENDLYNAME:
1791 case STORE_ATTR_EMAIL:
1792 case STORE_ATTR_FILENAME:
1793 if (strcmp(a->values[i].cstring,
1794 b->values[i].cstring))
1795 return 0;
1796 break;
1797 case STORE_ATTR_KEYID:
1798 case STORE_ATTR_ISSUERKEYID:
1799 case STORE_ATTR_SUBJECTKEYID:
1800 case STORE_ATTR_ISSUERSERIALHASH:
1801 case STORE_ATTR_CERTHASH:
1802 if (memcmp(a->values[i].sha1string,
1803 b->values[i].sha1string,
1804 a->value_sizes[i]))
1805 return 0;
1806 break;
1807 case STORE_ATTR_ISSUER:
1808 case STORE_ATTR_SUBJECT:
1809 if (X509_NAME_cmp(a->values[i].dn,
1810 b->values[i].dn))
1811 return 0;
1812 break;
1813 case STORE_ATTR_SERIAL:
1814 if (BN_cmp(a->values[i].number,
1815 b->values[i].number))
1816 return 0;
1817 break;
1818 default:
1819 break;
1820 }
1821 }
1822
1823 return 1;
1824}