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;
35 OSSL_STORE_LOADER_CTX *loader_ctx = NULL;
36 OSSL_STORE_CTX *ctx = NULL;
37 char scheme_copy[256], *p;
39 OPENSSL_strlcpy(scheme_copy, uri, sizeof(scheme_copy));
40 if ((p = strchr(scheme_copy, ':')) != NULL) {
47 if ((loader = ossl_store_get0_loader_int(p)) == NULL
48 || (loader_ctx = loader->open(loader, uri, ui_method, ui_data)) == NULL)
50 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
51 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_OPEN, ERR_R_MALLOC_FAILURE);
56 ctx->loader_ctx = loader_ctx;
58 ctx->ui_method = ui_method;
59 ctx->ui_data = ui_data;
60 ctx->post_process = post_process;
61 ctx->post_process_data = post_process_data;
64 if (loader_ctx != NULL) {
66 * We ignore a returned error because we will return NULL anyway in
67 * this case, so if something goes wrong when closing, that'll simply
68 * just add another entry on the error stack.
70 (void)loader->close(loader_ctx);
75 int OSSL_STORE_ctrl(OSSL_STORE_CTX *ctx, int cmd, ...)
81 if (ctx->loader->ctrl != NULL)
82 ret = ctx->loader->ctrl(ctx->loader_ctx, cmd, args);
88 OSSL_STORE_INFO *OSSL_STORE_load(OSSL_STORE_CTX *ctx)
90 OSSL_STORE_INFO *v = NULL;
93 if (OSSL_STORE_eof(ctx))
96 v = ctx->loader->load(ctx->loader_ctx, ctx->ui_method, ctx->ui_data);
98 if (ctx->post_process != NULL && v != NULL) {
99 v = ctx->post_process(v, ctx->post_process_data);
102 * By returning NULL, the callback decides that this object should
112 int OSSL_STORE_error(OSSL_STORE_CTX *ctx)
114 return ctx->loader->error(ctx->loader_ctx);
117 int OSSL_STORE_eof(OSSL_STORE_CTX *ctx)
119 return ctx->loader->eof(ctx->loader_ctx);
122 int OSSL_STORE_close(OSSL_STORE_CTX *ctx)
124 int loader_ret = ctx->loader->close(ctx->loader_ctx);
131 * Functions to generate OSSL_STORE_INFOs, one function for each type we
132 * support having in them. Along with each of them, one macro that
133 * can be used to determine what types are supported.
135 * In all cases, ownership of the object is transfered to the OSSL_STORE_INFO
136 * and will therefore be freed when the OSSL_STORE_INFO is freed.
138 static OSSL_STORE_INFO *store_info_new(int type, void *data)
140 OSSL_STORE_INFO *info = OPENSSL_zalloc(sizeof(*info));
150 OSSL_STORE_INFO *OSSL_STORE_INFO_new_NAME(char *name)
152 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_NAME, NULL);
155 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_NAME,
156 ERR_R_MALLOC_FAILURE);
160 info->_.name.name = name;
161 info->_.name.desc = NULL;
166 int OSSL_STORE_INFO_set0_NAME_description(OSSL_STORE_INFO *info, char *desc)
168 if (info->type != OSSL_STORE_INFO_NAME) {
169 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_SET0_NAME_DESCRIPTION,
170 ERR_R_PASSED_INVALID_ARGUMENT);
174 info->_.name.desc = desc;
178 OSSL_STORE_INFO *OSSL_STORE_INFO_new_PARAMS(EVP_PKEY *params)
180 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_PARAMS, params);
183 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_PARAMS,
184 ERR_R_MALLOC_FAILURE);
188 OSSL_STORE_INFO *OSSL_STORE_INFO_new_PKEY(EVP_PKEY *pkey)
190 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_PKEY, pkey);
193 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_PKEY,
194 ERR_R_MALLOC_FAILURE);
198 OSSL_STORE_INFO *OSSL_STORE_INFO_new_CERT(X509 *x509)
200 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_CERT, x509);
203 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_CERT,
204 ERR_R_MALLOC_FAILURE);
208 OSSL_STORE_INFO *OSSL_STORE_INFO_new_CRL(X509_CRL *crl)
210 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_CRL, crl);
213 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_CRL,
214 ERR_R_MALLOC_FAILURE);
219 * Functions to try to extract data from a OSSL_STORE_INFO.
221 int OSSL_STORE_INFO_get_type(const OSSL_STORE_INFO *info)
226 const char *OSSL_STORE_INFO_get0_NAME(const OSSL_STORE_INFO *info)
228 if (info->type == OSSL_STORE_INFO_NAME)
229 return info->_.name.name;
233 char *OSSL_STORE_INFO_get1_NAME(const OSSL_STORE_INFO *info)
235 if (info->type == OSSL_STORE_INFO_NAME) {
236 char *ret = OPENSSL_strdup(info->_.name.name);
239 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME,
240 ERR_R_MALLOC_FAILURE);
243 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME,
244 OSSL_STORE_R_NOT_A_NAME);
248 const char *OSSL_STORE_INFO_get0_NAME_description(const OSSL_STORE_INFO *info)
250 if (info->type == OSSL_STORE_INFO_NAME)
251 return info->_.name.desc;
255 char *OSSL_STORE_INFO_get1_NAME_description(const OSSL_STORE_INFO *info)
257 if (info->type == OSSL_STORE_INFO_NAME) {
258 char *ret = OPENSSL_strdup(info->_.name.desc
259 ? info->_.name.desc : "");
262 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME_DESCRIPTION,
263 ERR_R_MALLOC_FAILURE);
266 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME_DESCRIPTION,
267 OSSL_STORE_R_NOT_A_NAME);
271 EVP_PKEY *OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO *info)
273 if (info->type == OSSL_STORE_INFO_PARAMS)
274 return info->_.params;
278 EVP_PKEY *OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO *info)
280 if (info->type == OSSL_STORE_INFO_PARAMS) {
281 EVP_PKEY_up_ref(info->_.params);
282 return info->_.params;
284 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_PARAMS,
285 OSSL_STORE_R_NOT_PARAMETERS);
289 EVP_PKEY *OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO *info)
291 if (info->type == OSSL_STORE_INFO_PKEY)
296 EVP_PKEY *OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO *info)
298 if (info->type == OSSL_STORE_INFO_PKEY) {
299 EVP_PKEY_up_ref(info->_.pkey);
302 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_PKEY,
303 OSSL_STORE_R_NOT_A_KEY);
307 X509 *OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO *info)
309 if (info->type == OSSL_STORE_INFO_CERT)
314 X509 *OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO *info)
316 if (info->type == OSSL_STORE_INFO_CERT) {
317 X509_up_ref(info->_.x509);
320 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_CERT,
321 OSSL_STORE_R_NOT_A_CERTIFICATE);
325 X509_CRL *OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO *info)
327 if (info->type == OSSL_STORE_INFO_CRL)
332 X509_CRL *OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO *info)
334 if (info->type == OSSL_STORE_INFO_CRL) {
335 X509_CRL_up_ref(info->_.crl);
338 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_CRL,
339 OSSL_STORE_R_NOT_A_CRL);
344 * Free the OSSL_STORE_INFO
346 void OSSL_STORE_INFO_free(OSSL_STORE_INFO *info)
349 switch (info->type) {
350 case OSSL_STORE_INFO_EMBEDDED:
351 BUF_MEM_free(info->_.embedded.blob);
352 OPENSSL_free(info->_.embedded.pem_name);
354 case OSSL_STORE_INFO_NAME:
355 OPENSSL_free(info->_.name.name);
356 OPENSSL_free(info->_.name.desc);
358 case OSSL_STORE_INFO_PARAMS:
359 EVP_PKEY_free(info->_.params);
361 case OSSL_STORE_INFO_PKEY:
362 EVP_PKEY_free(info->_.pkey);
364 case OSSL_STORE_INFO_CERT:
365 X509_free(info->_.x509);
367 case OSSL_STORE_INFO_CRL:
368 X509_CRL_free(info->_.crl);
375 /* Internal functions */
376 OSSL_STORE_INFO *ossl_store_info_new_EMBEDDED(const char *new_pem_name,
379 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_EMBEDDED, NULL);
382 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_EMBEDDED,
383 ERR_R_MALLOC_FAILURE);
387 info->_.embedded.blob = embedded;
388 info->_.embedded.pem_name =
389 new_pem_name == NULL ? NULL : OPENSSL_strdup(new_pem_name);
391 if (new_pem_name != NULL && info->_.embedded.pem_name == NULL) {
392 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_EMBEDDED,
393 ERR_R_MALLOC_FAILURE);
394 OSSL_STORE_INFO_free(info);
401 BUF_MEM *ossl_store_info_get0_EMBEDDED_buffer(OSSL_STORE_INFO *info)
403 if (info->type == OSSL_STORE_INFO_EMBEDDED)
404 return info->_.embedded.blob;
408 char *ossl_store_info_get0_EMBEDDED_pem_name(OSSL_STORE_INFO *info)
410 if (info->type == OSSL_STORE_INFO_EMBEDDED)
411 return info->_.embedded.pem_name;
415 OSSL_STORE_CTX *ossl_store_attach_pem_bio(BIO *bp, const UI_METHOD *ui_method,
418 OSSL_STORE_CTX *ctx = NULL;
419 const OSSL_STORE_LOADER *loader = NULL;
420 OSSL_STORE_LOADER_CTX *loader_ctx = NULL;
422 if ((loader = ossl_store_get0_loader_int("file")) == NULL
423 || ((loader_ctx = ossl_store_file_attach_pem_bio_int(bp)) == NULL))
425 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
426 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_ATTACH_PEM_BIO,
427 ERR_R_MALLOC_FAILURE);
431 ctx->loader = loader;
432 ctx->loader_ctx = loader_ctx;
434 ctx->ui_method = ui_method;
435 ctx->ui_data = ui_data;
436 ctx->post_process = NULL;
437 ctx->post_process_data = NULL;
440 if (loader_ctx != NULL)
442 * We ignore a returned error because we will return NULL anyway in
443 * this case, so if something goes wrong when closing, that'll simply
444 * just add another entry on the error stack.
446 (void)loader->close(loader_ctx);
450 int ossl_store_detach_pem_bio(OSSL_STORE_CTX *ctx)
452 int loader_ret = ossl_store_file_detach_pem_bio_int(ctx->loader_ctx);