2 * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/store.h>
11 #include "internal/cryptlib.h"
12 #include "crypto/x509.h"
13 #include "x509_local.h"
15 /* Generic object loader, given expected type and criterion */
16 static int cache_objects(X509_LOOKUP
*lctx
, const char *uri
,
17 const OSSL_STORE_SEARCH
*criterion
,
18 int depth
, OPENSSL_CTX
*libctx
, const char *propq
)
21 OSSL_STORE_CTX
*ctx
= NULL
;
22 X509_STORE
*xstore
= X509_LOOKUP_get_store(lctx
);
24 if ((ctx
= OSSL_STORE_open_with_libctx(uri
, libctx
, propq
,
25 NULL
, NULL
, NULL
, NULL
)) == NULL
)
29 * We try to set the criterion, but don't care if it was valid or not.
30 * For a OSSL_STORE, it merely serves as an optimization, the expectation
31 * being that if the criterion couldn't be used, we will get *everything*
32 * from the container that the URI represents rather than the subset that
33 * the criterion indicates, so the biggest harm is that we cache more
34 * objects certs and CRLs than we may expect, but that's ok.
36 * Specifically for OpenSSL's own file: scheme, the only workable
37 * criterion is the BY_NAME one, which it can only apply on directories,
38 * but it's possible that the URI is a single file rather than a directory,
39 * and in that case, the BY_NAME criterion is pointless.
41 * We could very simply not apply any criterion at all here, and just let
42 * the code that selects certs and CRLs from the cached objects do its job,
43 * but it's a nice optimization when it can be applied (such as on an
44 * actual directory with a thousand CA certs).
46 if (criterion
!= NULL
)
47 OSSL_STORE_find(ctx
, criterion
);
50 OSSL_STORE_INFO
*info
= OSSL_STORE_load(ctx
);
53 /* NULL means error or "end of file". Either way, we break. */
57 infotype
= OSSL_STORE_INFO_get_type(info
);
60 if (infotype
== OSSL_STORE_INFO_NAME
) {
62 * This is an entry in the "directory" represented by the current
63 * uri. if |depth| allows, dive into it.
66 ok
= cache_objects(lctx
, OSSL_STORE_INFO_get0_NAME(info
),
67 criterion
, depth
- 1, libctx
, propq
);
70 * We know that X509_STORE_add_{cert|crl} increments the object's
71 * refcount, so we can safely use OSSL_STORE_INFO_get0_{cert,crl}
75 case OSSL_STORE_INFO_CERT
:
76 ok
= X509_STORE_add_cert(xstore
,
77 OSSL_STORE_INFO_get0_CERT(info
));
79 case OSSL_STORE_INFO_CRL
:
80 ok
= X509_STORE_add_crl(xstore
,
81 OSSL_STORE_INFO_get0_CRL(info
));
86 OSSL_STORE_INFO_free(info
);
90 OSSL_STORE_close(ctx
);
96 /* Because OPENSSL_free is a macro and for C type match */
97 static void free_uri(OPENSSL_STRING data
)
102 static void by_store_free(X509_LOOKUP
*ctx
)
104 STACK_OF(OPENSSL_STRING
) *uris
= X509_LOOKUP_get_method_data(ctx
);
105 sk_OPENSSL_STRING_pop_free(uris
, free_uri
);
108 static int by_store_ctrl_with_libctx(X509_LOOKUP
*ctx
, int cmd
,
109 const char *argp
, long argl
,
111 OPENSSL_CTX
*libctx
, const char *propq
)
114 case X509_L_ADD_STORE
:
115 /* If no URI is given, use the default cert dir as default URI */
117 argp
= ossl_safe_getenv(X509_get_default_cert_dir_env());
119 argp
= X509_get_default_cert_dir();
122 STACK_OF(OPENSSL_STRING
) *uris
= X509_LOOKUP_get_method_data(ctx
);
125 uris
= sk_OPENSSL_STRING_new_null();
126 X509_LOOKUP_set_method_data(ctx
, uris
);
128 return sk_OPENSSL_STRING_push(uris
, OPENSSL_strdup(argp
)) > 0;
130 case X509_L_LOAD_STORE
:
131 /* This is a shortcut for quick loading of specific containers */
132 return cache_objects(ctx
, argp
, NULL
, 0, libctx
, propq
);
138 static int by_store_ctrl(X509_LOOKUP
*ctx
, int cmd
,
139 const char *argp
, long argl
, char **retp
)
141 return by_store_ctrl_with_libctx(ctx
, cmd
, argp
, argl
, retp
, NULL
, NULL
);
144 static int by_store(X509_LOOKUP
*ctx
, X509_LOOKUP_TYPE type
,
145 const OSSL_STORE_SEARCH
*criterion
, X509_OBJECT
*ret
,
146 OPENSSL_CTX
*libctx
, const char *propq
)
148 STACK_OF(OPENSSL_STRING
) *uris
= X509_LOOKUP_get_method_data(ctx
);
152 for (i
= 0; i
< sk_OPENSSL_STRING_num(uris
); i
++) {
153 ok
= cache_objects(ctx
, sk_OPENSSL_STRING_value(uris
, i
), criterion
,
154 1 /* depth */, libctx
, propq
);
162 static int by_store_subject_with_libctx(X509_LOOKUP
*ctx
, X509_LOOKUP_TYPE type
,
163 const X509_NAME
*name
, X509_OBJECT
*ret
,
164 OPENSSL_CTX
*libctx
, const char *propq
)
166 OSSL_STORE_SEARCH
*criterion
=
167 OSSL_STORE_SEARCH_by_name((X509_NAME
*)name
); /* won't modify it */
168 int ok
= by_store(ctx
, type
, criterion
, ret
, libctx
, propq
);
169 STACK_OF(X509_OBJECT
) *store_objects
=
170 X509_STORE_get0_objects(X509_LOOKUP_get_store(ctx
));
171 X509_OBJECT
*tmp
= NULL
;
173 OSSL_STORE_SEARCH_free(criterion
);
176 tmp
= X509_OBJECT_retrieve_by_subject(store_objects
, type
, name
);
181 * This could also be done like this:
188 * However, we want to exercise the documented API to the max, so
189 * we do it the hard way.
191 * To be noted is that X509_OBJECT_set1_* increment the refcount,
192 * but so does X509_STORE_CTX_get_by_subject upon return of this
193 * function, so we must ensure the the refcount is decremented
194 * before we return, or we will get a refcount leak. We cannot do
195 * this with X509_OBJECT_free(), though, as that will free a bit
200 ok
= X509_OBJECT_set1_X509(ret
, tmp
->data
.x509
);
202 X509_free(tmp
->data
.x509
);
205 ok
= X509_OBJECT_set1_X509_CRL(ret
, tmp
->data
.crl
);
207 X509_CRL_free(tmp
->data
.crl
);
216 static int by_store_subject(X509_LOOKUP
*ctx
, X509_LOOKUP_TYPE type
,
217 const X509_NAME
*name
, X509_OBJECT
*ret
)
219 return by_store_subject_with_libctx(ctx
, type
, name
, ret
, NULL
, NULL
);
223 * We lack the implementations for get_by_issuer_serial, get_by_fingerprint
224 * and get_by_alias. There's simply not enough support in the X509_LOOKUP
225 * or X509_STORE APIs.
228 static X509_LOOKUP_METHOD x509_store_lookup
= {
229 "Load certs from STORE URIs",
231 by_store_free
, /* free */
234 by_store_ctrl
, /* ctrl */
235 by_store_subject
, /* get_by_subject */
236 NULL
, /* get_by_issuer_serial */
237 NULL
, /* get_by_fingerprint */
238 NULL
, /* get_by_alias */
239 by_store_subject_with_libctx
,
240 by_store_ctrl_with_libctx
243 X509_LOOKUP_METHOD
*X509_LOOKUP_store(void)
245 return &x509_store_lookup
;