2 * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
13 #include <openssl/crypto.h>
14 #include <openssl/err.h>
15 #include <openssl/store.h>
16 #include "internal/thread_once.h"
17 #include "internal/store_int.h"
18 #include "store_locl.h"
20 struct ossl_store_ctx_st {
21 const OSSL_STORE_LOADER *loader;
22 OSSL_STORE_LOADER_CTX *loader_ctx;
23 const UI_METHOD *ui_method;
25 OSSL_STORE_post_process_info_fn post_process;
26 void *post_process_data;
29 OSSL_STORE_CTX *OSSL_STORE_open(const char *uri, const UI_METHOD *ui_method,
31 OSSL_STORE_post_process_info_fn post_process,
32 void *post_process_data)
34 const OSSL_STORE_LOADER *loader = NULL;
35 OSSL_STORE_LOADER_CTX *loader_ctx = NULL;
36 OSSL_STORE_CTX *ctx = NULL;
37 char scheme_copy[256], *p, *schemes[2];
42 * Put the file scheme first. If the uri does represent an existing file,
43 * possible device name and all, then it should be loaded. Only a failed
44 * attempt at loading a local file should have us try something else.
46 schemes[schemes_n++] = "file";
49 * Now, check if we have something that looks like a scheme, and add it
50 * as a second scheme. However, also check if there's an authority start
51 * (://), because that will invalidate the previous file scheme. Also,
52 * check that this isn't actually the file scheme, as there's no point
53 * going through that one twice!
55 OPENSSL_strlcpy(scheme_copy, uri, sizeof(scheme_copy));
56 if ((p = strchr(scheme_copy, ':')) != NULL) {
58 if (strcasecmp(scheme_copy, "file") != 0) {
59 if (strncmp(p, "//", 2) == 0)
60 schemes_n--; /* Invalidate the file scheme */
61 schemes[schemes_n++] = scheme_copy;
67 /* Try each scheme until we find one that could open the URI */
68 for (i = 0; loader_ctx == NULL && i < schemes_n; i++) {
69 if ((loader = ossl_store_get0_loader_int(schemes[i])) != NULL)
70 loader_ctx = loader->open(loader, uri, ui_method, ui_data);
72 if (loader_ctx == NULL)
75 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
76 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_OPEN, ERR_R_MALLOC_FAILURE);
81 ctx->loader_ctx = loader_ctx;
82 ctx->ui_method = ui_method;
83 ctx->ui_data = ui_data;
84 ctx->post_process = post_process;
85 ctx->post_process_data = post_process_data;
88 * If the attempt to open with the 'file' scheme loader failed and the
89 * other scheme loader succeeded, the failure to open with the 'file'
90 * scheme loader leaves an error on the error stack. Let's remove it.
97 ERR_clear_last_mark();
98 if (loader_ctx != NULL) {
100 * We ignore a returned error because we will return NULL anyway in
101 * this case, so if something goes wrong when closing, that'll simply
102 * just add another entry on the error stack.
104 (void)loader->close(loader_ctx);
109 int OSSL_STORE_ctrl(OSSL_STORE_CTX *ctx, int cmd, ...)
115 ret = OSSL_STORE_vctrl(ctx, cmd, args);
121 int OSSL_STORE_vctrl(OSSL_STORE_CTX *ctx, int cmd, va_list args)
123 if (ctx->loader->ctrl != NULL)
124 return ctx->loader->ctrl(ctx->loader_ctx, cmd, args);
128 OSSL_STORE_INFO *OSSL_STORE_load(OSSL_STORE_CTX *ctx)
130 OSSL_STORE_INFO *v = NULL;
133 if (OSSL_STORE_eof(ctx))
136 v = ctx->loader->load(ctx->loader_ctx, ctx->ui_method, ctx->ui_data);
138 if (ctx->post_process != NULL && v != NULL) {
139 v = ctx->post_process(v, ctx->post_process_data);
142 * By returning NULL, the callback decides that this object should
152 int OSSL_STORE_error(OSSL_STORE_CTX *ctx)
154 return ctx->loader->error(ctx->loader_ctx);
157 int OSSL_STORE_eof(OSSL_STORE_CTX *ctx)
159 return ctx->loader->eof(ctx->loader_ctx);
162 int OSSL_STORE_close(OSSL_STORE_CTX *ctx)
164 int loader_ret = ctx->loader->close(ctx->loader_ctx);
171 * Functions to generate OSSL_STORE_INFOs, one function for each type we
172 * support having in them as well as a generic constructor.
174 * In all cases, ownership of the object is transfered to the OSSL_STORE_INFO
175 * and will therefore be freed when the OSSL_STORE_INFO is freed.
177 static OSSL_STORE_INFO *store_info_new(int type, void *data)
179 OSSL_STORE_INFO *info = OPENSSL_zalloc(sizeof(*info));
189 OSSL_STORE_INFO *OSSL_STORE_INFO_new_NAME(char *name)
191 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_NAME, NULL);
194 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_NAME,
195 ERR_R_MALLOC_FAILURE);
199 info->_.name.name = name;
200 info->_.name.desc = NULL;
205 int OSSL_STORE_INFO_set0_NAME_description(OSSL_STORE_INFO *info, char *desc)
207 if (info->type != OSSL_STORE_INFO_NAME) {
208 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_SET0_NAME_DESCRIPTION,
209 ERR_R_PASSED_INVALID_ARGUMENT);
213 info->_.name.desc = desc;
217 OSSL_STORE_INFO *OSSL_STORE_INFO_new_PARAMS(EVP_PKEY *params)
219 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_PARAMS, params);
222 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_PARAMS,
223 ERR_R_MALLOC_FAILURE);
227 OSSL_STORE_INFO *OSSL_STORE_INFO_new_PKEY(EVP_PKEY *pkey)
229 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_PKEY, pkey);
232 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_PKEY,
233 ERR_R_MALLOC_FAILURE);
237 OSSL_STORE_INFO *OSSL_STORE_INFO_new_CERT(X509 *x509)
239 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_CERT, x509);
242 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_CERT,
243 ERR_R_MALLOC_FAILURE);
247 OSSL_STORE_INFO *OSSL_STORE_INFO_new_CRL(X509_CRL *crl)
249 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_CRL, crl);
252 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_CRL,
253 ERR_R_MALLOC_FAILURE);
258 * Functions to try to extract data from a OSSL_STORE_INFO.
260 int OSSL_STORE_INFO_get_type(const OSSL_STORE_INFO *info)
265 const char *OSSL_STORE_INFO_get0_NAME(const OSSL_STORE_INFO *info)
267 if (info->type == OSSL_STORE_INFO_NAME)
268 return info->_.name.name;
272 char *OSSL_STORE_INFO_get1_NAME(const OSSL_STORE_INFO *info)
274 if (info->type == OSSL_STORE_INFO_NAME) {
275 char *ret = OPENSSL_strdup(info->_.name.name);
278 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME,
279 ERR_R_MALLOC_FAILURE);
282 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME,
283 OSSL_STORE_R_NOT_A_NAME);
287 const char *OSSL_STORE_INFO_get0_NAME_description(const OSSL_STORE_INFO *info)
289 if (info->type == OSSL_STORE_INFO_NAME)
290 return info->_.name.desc;
294 char *OSSL_STORE_INFO_get1_NAME_description(const OSSL_STORE_INFO *info)
296 if (info->type == OSSL_STORE_INFO_NAME) {
297 char *ret = OPENSSL_strdup(info->_.name.desc
298 ? info->_.name.desc : "");
301 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME_DESCRIPTION,
302 ERR_R_MALLOC_FAILURE);
305 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME_DESCRIPTION,
306 OSSL_STORE_R_NOT_A_NAME);
310 EVP_PKEY *OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO *info)
312 if (info->type == OSSL_STORE_INFO_PARAMS)
313 return info->_.params;
317 EVP_PKEY *OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO *info)
319 if (info->type == OSSL_STORE_INFO_PARAMS) {
320 EVP_PKEY_up_ref(info->_.params);
321 return info->_.params;
323 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_PARAMS,
324 OSSL_STORE_R_NOT_PARAMETERS);
328 EVP_PKEY *OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO *info)
330 if (info->type == OSSL_STORE_INFO_PKEY)
335 EVP_PKEY *OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO *info)
337 if (info->type == OSSL_STORE_INFO_PKEY) {
338 EVP_PKEY_up_ref(info->_.pkey);
341 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_PKEY,
342 OSSL_STORE_R_NOT_A_KEY);
346 X509 *OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO *info)
348 if (info->type == OSSL_STORE_INFO_CERT)
353 X509 *OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO *info)
355 if (info->type == OSSL_STORE_INFO_CERT) {
356 X509_up_ref(info->_.x509);
359 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_CERT,
360 OSSL_STORE_R_NOT_A_CERTIFICATE);
364 X509_CRL *OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO *info)
366 if (info->type == OSSL_STORE_INFO_CRL)
371 X509_CRL *OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO *info)
373 if (info->type == OSSL_STORE_INFO_CRL) {
374 X509_CRL_up_ref(info->_.crl);
377 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_CRL,
378 OSSL_STORE_R_NOT_A_CRL);
383 * Free the OSSL_STORE_INFO
385 void OSSL_STORE_INFO_free(OSSL_STORE_INFO *info)
388 switch (info->type) {
389 case OSSL_STORE_INFO_EMBEDDED:
390 BUF_MEM_free(info->_.embedded.blob);
391 OPENSSL_free(info->_.embedded.pem_name);
393 case OSSL_STORE_INFO_NAME:
394 OPENSSL_free(info->_.name.name);
395 OPENSSL_free(info->_.name.desc);
397 case OSSL_STORE_INFO_PARAMS:
398 EVP_PKEY_free(info->_.params);
400 case OSSL_STORE_INFO_PKEY:
401 EVP_PKEY_free(info->_.pkey);
403 case OSSL_STORE_INFO_CERT:
404 X509_free(info->_.x509);
406 case OSSL_STORE_INFO_CRL:
407 X509_CRL_free(info->_.crl);
414 /* Internal functions */
415 OSSL_STORE_INFO *ossl_store_info_new_EMBEDDED(const char *new_pem_name,
418 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_EMBEDDED, NULL);
421 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_EMBEDDED,
422 ERR_R_MALLOC_FAILURE);
426 info->_.embedded.blob = embedded;
427 info->_.embedded.pem_name =
428 new_pem_name == NULL ? NULL : OPENSSL_strdup(new_pem_name);
430 if (new_pem_name != NULL && info->_.embedded.pem_name == NULL) {
431 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_EMBEDDED,
432 ERR_R_MALLOC_FAILURE);
433 OSSL_STORE_INFO_free(info);
440 BUF_MEM *ossl_store_info_get0_EMBEDDED_buffer(OSSL_STORE_INFO *info)
442 if (info->type == OSSL_STORE_INFO_EMBEDDED)
443 return info->_.embedded.blob;
447 char *ossl_store_info_get0_EMBEDDED_pem_name(OSSL_STORE_INFO *info)
449 if (info->type == OSSL_STORE_INFO_EMBEDDED)
450 return info->_.embedded.pem_name;
454 OSSL_STORE_CTX *ossl_store_attach_pem_bio(BIO *bp, const UI_METHOD *ui_method,
457 OSSL_STORE_CTX *ctx = NULL;
458 const OSSL_STORE_LOADER *loader = NULL;
459 OSSL_STORE_LOADER_CTX *loader_ctx = NULL;
461 if ((loader = ossl_store_get0_loader_int("file")) == NULL
462 || ((loader_ctx = ossl_store_file_attach_pem_bio_int(bp)) == NULL))
464 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
465 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_ATTACH_PEM_BIO,
466 ERR_R_MALLOC_FAILURE);
470 ctx->loader = loader;
471 ctx->loader_ctx = loader_ctx;
473 ctx->ui_method = ui_method;
474 ctx->ui_data = ui_data;
475 ctx->post_process = NULL;
476 ctx->post_process_data = NULL;
479 if (loader_ctx != NULL)
481 * We ignore a returned error because we will return NULL anyway in
482 * this case, so if something goes wrong when closing, that'll simply
483 * just add another entry on the error stack.
485 (void)loader->close(loader_ctx);
489 int ossl_store_detach_pem_bio(OSSL_STORE_CTX *ctx)
491 int loader_ret = ossl_store_file_detach_pem_bio_int(ctx->loader_ctx);