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 v = ctx->loader->load(ctx->loader_ctx, ctx->ui_method, ctx->ui_data);
95 if (ctx->post_process != NULL && v != NULL) {
96 v = ctx->post_process(v, ctx->post_process_data);
99 * By returning NULL, the callback decides that this object should
109 int OSSL_STORE_error(OSSL_STORE_CTX *ctx)
111 return ctx->loader->error(ctx->loader_ctx);
114 int OSSL_STORE_eof(OSSL_STORE_CTX *ctx)
116 return ctx->loader->eof(ctx->loader_ctx);
119 int OSSL_STORE_close(OSSL_STORE_CTX *ctx)
121 int loader_ret = ctx->loader->close(ctx->loader_ctx);
128 * Functions to generate OSSL_STORE_INFOs, one function for each type we
129 * support having in them. Along with each of them, one macro that
130 * can be used to determine what types are supported.
132 * In all cases, ownership of the object is transfered to the OSSL_STORE_INFO
133 * and will therefore be freed when the OSSL_STORE_INFO is freed.
135 static OSSL_STORE_INFO *store_info_new(int type, void *data)
137 OSSL_STORE_INFO *info = OPENSSL_zalloc(sizeof(*info));
147 OSSL_STORE_INFO *OSSL_STORE_INFO_new_NAME(char *name)
149 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_NAME, NULL);
152 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_NAME,
153 ERR_R_MALLOC_FAILURE);
157 info->_.name.name = name;
158 info->_.name.desc = NULL;
163 int OSSL_STORE_INFO_set0_NAME_description(OSSL_STORE_INFO *info, char *desc)
165 if (info->type != OSSL_STORE_INFO_NAME) {
166 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_SET0_NAME_DESCRIPTION,
167 ERR_R_PASSED_INVALID_ARGUMENT);
171 info->_.name.desc = desc;
175 OSSL_STORE_INFO *OSSL_STORE_INFO_new_PARAMS(EVP_PKEY *params)
177 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_PARAMS, params);
180 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_PARAMS,
181 ERR_R_MALLOC_FAILURE);
185 OSSL_STORE_INFO *OSSL_STORE_INFO_new_PKEY(EVP_PKEY *pkey)
187 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_PKEY, pkey);
190 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_PKEY,
191 ERR_R_MALLOC_FAILURE);
195 OSSL_STORE_INFO *OSSL_STORE_INFO_new_CERT(X509 *x509)
197 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_CERT, x509);
200 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_CERT,
201 ERR_R_MALLOC_FAILURE);
205 OSSL_STORE_INFO *OSSL_STORE_INFO_new_CRL(X509_CRL *crl)
207 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_CRL, crl);
210 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_CRL,
211 ERR_R_MALLOC_FAILURE);
216 * Functions to try to extract data from a OSSL_STORE_INFO.
218 int OSSL_STORE_INFO_get_type(const OSSL_STORE_INFO *info)
223 const char *OSSL_STORE_INFO_get0_NAME(const OSSL_STORE_INFO *info)
225 if (info->type == OSSL_STORE_INFO_NAME)
226 return info->_.name.name;
230 char *OSSL_STORE_INFO_get1_NAME(const OSSL_STORE_INFO *info)
232 if (info->type == OSSL_STORE_INFO_NAME) {
233 char *ret = OPENSSL_strdup(info->_.name.name);
236 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME,
237 ERR_R_MALLOC_FAILURE);
240 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME,
241 OSSL_STORE_R_NOT_A_NAME);
245 const char *OSSL_STORE_INFO_get0_NAME_description(const OSSL_STORE_INFO *info)
247 if (info->type == OSSL_STORE_INFO_NAME)
248 return info->_.name.desc;
252 char *OSSL_STORE_INFO_get1_NAME_description(const OSSL_STORE_INFO *info)
254 if (info->type == OSSL_STORE_INFO_NAME) {
255 char *ret = OPENSSL_strdup(info->_.name.desc
256 ? info->_.name.desc : "");
259 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME_DESCRIPTION,
260 ERR_R_MALLOC_FAILURE);
263 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_NAME_DESCRIPTION,
264 OSSL_STORE_R_NOT_A_NAME);
268 EVP_PKEY *OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO *info)
270 if (info->type == OSSL_STORE_INFO_PARAMS)
271 return info->_.params;
275 EVP_PKEY *OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO *info)
277 if (info->type == OSSL_STORE_INFO_PARAMS) {
278 EVP_PKEY_up_ref(info->_.params);
279 return info->_.params;
281 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_PARAMS,
282 OSSL_STORE_R_NOT_PARAMETERS);
286 EVP_PKEY *OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO *info)
288 if (info->type == OSSL_STORE_INFO_PKEY)
293 EVP_PKEY *OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO *info)
295 if (info->type == OSSL_STORE_INFO_PKEY) {
296 EVP_PKEY_up_ref(info->_.pkey);
299 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_PKEY,
300 OSSL_STORE_R_NOT_A_KEY);
304 X509 *OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO *info)
306 if (info->type == OSSL_STORE_INFO_CERT)
311 X509 *OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO *info)
313 if (info->type == OSSL_STORE_INFO_CERT) {
314 X509_up_ref(info->_.x509);
317 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_CERT,
318 OSSL_STORE_R_NOT_A_CERTIFICATE);
322 X509_CRL *OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO *info)
324 if (info->type == OSSL_STORE_INFO_CRL)
329 X509_CRL *OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO *info)
331 if (info->type == OSSL_STORE_INFO_CRL) {
332 X509_CRL_up_ref(info->_.crl);
335 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_GET1_CRL,
336 OSSL_STORE_R_NOT_A_CRL);
341 * Free the OSSL_STORE_INFO
343 void OSSL_STORE_INFO_free(OSSL_STORE_INFO *info)
346 switch (info->type) {
347 case OSSL_STORE_INFO_EMBEDDED:
348 BUF_MEM_free(info->_.embedded.blob);
349 OPENSSL_free(info->_.embedded.pem_name);
351 case OSSL_STORE_INFO_NAME:
352 OPENSSL_free(info->_.name.name);
353 OPENSSL_free(info->_.name.desc);
355 case OSSL_STORE_INFO_PARAMS:
356 EVP_PKEY_free(info->_.params);
358 case OSSL_STORE_INFO_PKEY:
359 EVP_PKEY_free(info->_.pkey);
361 case OSSL_STORE_INFO_CERT:
362 X509_free(info->_.x509);
364 case OSSL_STORE_INFO_CRL:
365 X509_CRL_free(info->_.crl);
372 /* Internal functions */
373 OSSL_STORE_INFO *ossl_store_info_new_EMBEDDED(const char *new_pem_name,
376 OSSL_STORE_INFO *info = store_info_new(OSSL_STORE_INFO_EMBEDDED, NULL);
379 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_EMBEDDED,
380 ERR_R_MALLOC_FAILURE);
384 info->_.embedded.blob = embedded;
385 info->_.embedded.pem_name =
386 new_pem_name == NULL ? NULL : OPENSSL_strdup(new_pem_name);
388 if (new_pem_name != NULL && info->_.embedded.pem_name == NULL) {
389 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_INFO_NEW_EMBEDDED,
390 ERR_R_MALLOC_FAILURE);
391 OSSL_STORE_INFO_free(info);
398 BUF_MEM *ossl_store_info_get0_EMBEDDED_buffer(OSSL_STORE_INFO *info)
400 if (info->type == OSSL_STORE_INFO_EMBEDDED)
401 return info->_.embedded.blob;
405 char *ossl_store_info_get0_EMBEDDED_pem_name(OSSL_STORE_INFO *info)
407 if (info->type == OSSL_STORE_INFO_EMBEDDED)
408 return info->_.embedded.pem_name;
412 OSSL_STORE_CTX *ossl_store_attach_pem_bio(BIO *bp, const UI_METHOD *ui_method,
415 OSSL_STORE_CTX *ctx = NULL;
416 const OSSL_STORE_LOADER *loader = NULL;
417 OSSL_STORE_LOADER_CTX *loader_ctx = NULL;
419 if ((loader = ossl_store_get0_loader_int("file")) == NULL
420 || ((loader_ctx = ossl_store_file_attach_pem_bio_int(bp)) == NULL))
422 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
423 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_ATTACH_PEM_BIO,
424 ERR_R_MALLOC_FAILURE);
428 ctx->loader = loader;
429 ctx->loader_ctx = loader_ctx;
431 ctx->ui_method = ui_method;
432 ctx->ui_data = ui_data;
433 ctx->post_process = NULL;
434 ctx->post_process_data = NULL;
437 if (loader_ctx != NULL)
439 * We ignore a returned error because we will return NULL anyway in
440 * this case, so if something goes wrong when closing, that'll simply
441 * just add another entry on the error stack.
443 (void)loader->close(loader_ctx);
447 int ossl_store_detach_pem_bio(OSSL_STORE_CTX *ctx)
449 int loader_ret = ossl_store_file_detach_pem_bio_int(ctx->loader_ctx);