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
12 #include <openssl/bio.h>
13 #include <openssl/dsa.h> /* For d2i_DSAPrivateKey */
14 #include <openssl/err.h>
15 #include <openssl/evp.h>
16 #include <openssl/pem.h>
17 #include <openssl/pkcs12.h> /* For the PKCS8 stuff o.O */
18 #include <openssl/rsa.h> /* For d2i_RSAPrivateKey */
19 #include <openssl/safestack.h>
20 #include <openssl/store.h>
21 #include <openssl/ui.h>
22 #include <openssl/x509.h> /* For the PKCS8 stuff o.O */
23 #include "internal/asn1_int.h"
24 #include "store_locl.h"
32 static char *file_get_pass(const UI_METHOD *ui_method, char *pass,
33 size_t maxsize, const char *prompt_info, void *data)
39 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_MALLOC_FAILURE);
43 if (ui_method != NULL)
44 UI_set_method(ui, ui_method);
45 UI_add_user_data(ui, data);
47 if ((prompt = UI_construct_prompt(ui, "pass phrase",
48 prompt_info)) == NULL) {
49 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_MALLOC_FAILURE);
51 } else if (!UI_add_input_string(ui, prompt, UI_INPUT_FLAG_DEFAULT_PWD,
52 pass, 0, maxsize - 1)) {
53 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_UI_LIB);
56 switch (UI_process(ui)) {
58 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS,
59 OSSL_STORE_R_UI_PROCESS_INTERRUPTED_OR_CANCELLED);
63 OSSL_STOREerr(OSSL_STORE_F_FILE_GET_PASS, ERR_R_UI_LIB);
76 struct pem_pass_data {
77 const UI_METHOD *ui_method;
79 const char *prompt_info;
81 static int file_fill_pem_pass_data(struct pem_pass_data *pass_data,
82 const char *prompt_info,
83 const UI_METHOD *ui_method, void *ui_data)
85 if (pass_data == NULL)
87 pass_data->ui_method = ui_method;
88 pass_data->data = ui_data;
89 pass_data->prompt_info = prompt_info;
92 static int file_get_pem_pass(char *buf, int num, int w, void *data)
94 struct pem_pass_data *pass_data = data;
95 char *pass = file_get_pass(pass_data->ui_method, buf, num,
96 pass_data->prompt_info, pass_data->data);
98 return pass == NULL ? 0 : strlen(pass);
102 * The file scheme handlers
106 * The try_decode function is called to check if the blob of data can
107 * be used by this handler, and if it can, decodes it into a supported
108 * OpenSSL type and returns a OSSL_STORE_INFO with the decoded data.
110 * pem_name: If this blob comes from a PEM file, this holds
111 * the PEM name. If it comes from another type of
112 * file, this is NULL.
113 * pem_header: If this blob comes from a PEM file, this holds
114 * the PEM headers. If it comes from another type of
115 * file, this is NULL.
116 * blob: The blob of data to match with what this handler
118 * len: The length of the blob.
119 * handler_ctx: For a handler marked repeatable, this pointer can
120 * be used to create a context for the handler. IT IS
121 * THE HANDLER'S RESPONSIBILITY TO CREATE AND DESTROY
122 * THIS CONTEXT APPROPRIATELY, i.e. create on first call
123 * and destroy when about to return NULL.
124 * ui_method: Application UI method for getting a password, pin
125 * or any other interactive data.
126 * ui_data: Application data to be passed to ui_method when
131 typedef OSSL_STORE_INFO *(*file_try_decode_fn)(const char *pem_name,
132 const char *pem_header,
133 const unsigned char *blob,
134 size_t len, void **handler_ctx,
135 const UI_METHOD *ui_method,
138 * The eof function should return 1 if there's no more data to be found
139 * with the handler_ctx, otherwise 0. This is only used when the handler is
142 typedef int (*file_eof_fn)(void *handler_ctx);
144 * The destroy_ctx function is used to destroy the handler_ctx that was
145 * intiated by a repeatable try_decode fuction. This is only used when
146 * the handler is marked repeatable.
148 typedef void (*file_destroy_ctx_fn)(void **handler_ctx);
150 typedef struct file_handler_st {
152 file_try_decode_fn try_decode;
154 file_destroy_ctx_fn destroy_ctx;
160 static OSSL_STORE_INFO *try_decode_PKCS12(const char *pem_name,
161 const char *pem_header,
162 const unsigned char *blob,
163 size_t len, void **pctx,
164 const UI_METHOD *ui_method,
167 OSSL_STORE_INFO *store_info = NULL;
168 STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
171 /* Initial parsing */
175 if (pem_name != NULL)
176 /* No match, there is no PEM PKCS12 tag */
179 if ((p12 = d2i_PKCS12(NULL, &blob, len)) != NULL) {
181 char tpass[PEM_BUFSIZE];
182 EVP_PKEY *pkey = NULL;
184 STACK_OF(X509) *chain = NULL;
186 if (PKCS12_verify_mac(p12, "", 0)
187 || PKCS12_verify_mac(p12, NULL, 0)) {
190 if ((pass = file_get_pass(ui_method, tpass, PEM_BUFSIZE,
191 "PKCS12 import password",
193 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12,
194 OSSL_STORE_R_PASSPHRASE_CALLBACK_ERROR);
197 if (!PKCS12_verify_mac(p12, pass, strlen(pass))) {
198 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PKCS12,
199 OSSL_STORE_R_ERROR_VERIFYING_PKCS12_MAC);
204 if (PKCS12_parse(p12, pass, &pkey, &cert, &chain)) {
205 OSSL_STORE_INFO *si_pkey = NULL;
206 OSSL_STORE_INFO *si_cert = NULL;
207 OSSL_STORE_INFO *si_ca = NULL;
209 if ((ctx = sk_OSSL_STORE_INFO_new_null()) != NULL
210 && (si_pkey = OSSL_STORE_INFO_new_PKEY(pkey)) != NULL
211 && sk_OSSL_STORE_INFO_push(ctx, si_pkey) != 0
212 && (si_cert = OSSL_STORE_INFO_new_CERT(cert)) != NULL
213 && sk_OSSL_STORE_INFO_push(ctx, si_cert) != 0) {
218 while(sk_X509_num(chain) > 0) {
219 X509 *ca = sk_X509_value(chain, 0);
221 if ((si_ca = OSSL_STORE_INFO_new_CERT(ca)) == NULL
222 || sk_OSSL_STORE_INFO_push(ctx, si_ca) == 0) {
227 (void)sk_X509_shift(chain);
231 OSSL_STORE_INFO_free(si_ca);
232 OSSL_STORE_INFO_free(si_cert);
233 OSSL_STORE_INFO_free(si_pkey);
234 sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free);
237 sk_X509_pop_free(chain, X509_free);
250 store_info = sk_OSSL_STORE_INFO_shift(ctx);
254 static int eof_PKCS12(void *ctx_)
256 STACK_OF(OSSL_STORE_INFO) *ctx = ctx_;
258 return ctx == NULL || sk_OSSL_STORE_INFO_num(ctx) == 0;
260 static void destroy_ctx_PKCS12(void **pctx)
262 STACK_OF(OSSL_STORE_INFO) *ctx = *pctx;
264 sk_OSSL_STORE_INFO_pop_free(ctx, OSSL_STORE_INFO_free);
267 static FILE_HANDLER PKCS12_handler = {
275 int pem_check_suffix(const char *pem_str, const char *suffix);
276 static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name,
277 const char *pem_header,
278 const unsigned char *blob,
279 size_t len, void **pctx,
280 const UI_METHOD *ui_method,
283 OSSL_STORE_INFO *store_info = NULL;
284 EVP_PKEY *pkey = NULL;
285 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
287 if (pem_name != NULL) {
290 if ((slen = pem_check_suffix(pem_name, "PRIVATE KEY")) > 0
291 && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name, slen)) != NULL)
292 pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &blob, len);
296 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
297 ameth = EVP_PKEY_asn1_get0(i);
298 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
300 pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &blob, len);
309 store_info = OSSL_STORE_INFO_new_PKEY(pkey);
310 if (store_info == NULL)
315 static FILE_HANDLER PrivateKey_handler = {
317 try_decode_PrivateKey
320 static OSSL_STORE_INFO *try_decode_PUBKEY(const char *pem_name,
321 const char *pem_header,
322 const unsigned char *blob,
323 size_t len, void **pctx,
324 const UI_METHOD *ui_method,
327 OSSL_STORE_INFO *store_info = NULL;
328 EVP_PKEY *pkey = NULL;
330 if (pem_name != NULL && strcmp(pem_name, PEM_STRING_PUBLIC) != 0)
334 if ((pkey = d2i_PUBKEY(NULL, &blob, len)) != NULL)
335 store_info = OSSL_STORE_INFO_new_PKEY(pkey);
339 static FILE_HANDLER PUBKEY_handler = {
344 static OSSL_STORE_INFO *try_decode_params(const char *pem_name,
345 const char *pem_header,
346 const unsigned char *blob,
347 size_t len, void **pctx,
348 const UI_METHOD *ui_method,
351 OSSL_STORE_INFO *store_info = NULL;
352 EVP_PKEY *pkey = EVP_PKEY_new();
353 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
357 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
361 if (pem_name != NULL) {
364 if ((slen = pem_check_suffix(pem_name, "PARAMETERS")) > 0
365 && EVP_PKEY_set_type_str(pkey, pem_name, slen)
366 && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL
367 && ameth->param_decode != NULL
368 && ameth->param_decode(pkey, &blob, len))
373 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
374 ameth = EVP_PKEY_asn1_get0(i);
375 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
377 if (EVP_PKEY_set_type(pkey, ameth->pkey_id)
378 && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL
379 && ameth->param_decode != NULL
380 && ameth->param_decode(pkey, &blob, len)) {
388 store_info = OSSL_STORE_INFO_new_PARAMS(pkey);
389 if (store_info == NULL)
394 static FILE_HANDLER params_handler = {
399 static OSSL_STORE_INFO *try_decode_X509Certificate(const char *pem_name,
400 const char *pem_header,
401 const unsigned char *blob,
402 size_t len, void **pctx,
403 const UI_METHOD *ui_method,
406 OSSL_STORE_INFO *store_info = NULL;
410 * In most cases, we can try to interpret the serialized data as a trusted
411 * cert (X509 + X509_AUX) and fall back to reading it as a normal cert
412 * (just X509), but if the PEM name specifically declares it as a trusted
413 * cert, then no fallback should be engaged. |ignore_trusted| tells if
414 * the fallback can be used (1) or not (0).
416 int ignore_trusted = 1;
418 if (pem_name != NULL) {
419 if (strcmp(pem_name, PEM_STRING_X509_TRUSTED) == 0)
421 else if (strcmp(pem_name, PEM_STRING_X509_OLD) != 0
422 && strcmp(pem_name, PEM_STRING_X509) != 0)
427 if ((cert = d2i_X509_AUX(NULL, &blob, len)) != NULL
428 || (ignore_trusted && (cert = d2i_X509(NULL, &blob, len)) != NULL))
429 store_info = OSSL_STORE_INFO_new_CERT(cert);
431 if (store_info == NULL)
436 static FILE_HANDLER X509Certificate_handler = {
438 try_decode_X509Certificate
441 static OSSL_STORE_INFO *try_decode_X509CRL(const char *pem_name,
442 const char *pem_header,
443 const unsigned char *blob,
444 size_t len, void **pctx,
445 const UI_METHOD *ui_method,
448 OSSL_STORE_INFO *store_info = NULL;
449 X509_CRL *crl = NULL;
452 && strcmp(pem_name, PEM_STRING_X509_CRL) != 0)
456 if ((crl = d2i_X509_CRL(NULL, &blob, len)) != NULL)
457 store_info = OSSL_STORE_INFO_new_CRL(crl);
459 if (store_info == NULL)
464 static FILE_HANDLER X509CRL_handler = {
469 static const FILE_HANDLER *file_handlers[] = {
471 &X509Certificate_handler,
483 struct ossl_store_loader_ctx_st {
488 /* The following are used when the handler is marked as repeatable */
489 const FILE_HANDLER *last_handler;
490 void *last_handler_ctx;
493 static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader,
495 const UI_METHOD *ui_method,
500 OSSL_STORE_LOADER_CTX *ctx = NULL;
501 const char *path = NULL;
503 if (strncasecmp(uri, "file:", 5) == 0) {
504 if (strncmp(&uri[5], "//localhost/", 12) == 0) {
506 } else if (strncmp(&uri[5], "///", 3) == 0) {
508 } else if (strncmp(&uri[5], "//", 2) != 0) {
511 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
512 OSSL_STORE_R_URI_AUTHORITY_UNSUPPORED);
517 * If the scheme "file" was an explicit part of the URI, the path must
518 * be absolute. So says RFC 8089
520 if (path[0] != '/') {
521 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
522 OSSL_STORE_R_PATH_MUST_BE_ABSOLUTE);
527 /* Windows file: URIs with a drive letter start with a / */
528 if (path[0] == '/' && path[2] == ':' && path[3] == '/')
536 ctx = OPENSSL_zalloc(sizeof(*ctx));
538 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_MALLOC_FAILURE);
542 if ((buff = BIO_new(BIO_f_buffer())) == NULL)
544 if ((ctx->file = BIO_new_file(path, "rb")) == NULL) {
547 ctx->file = BIO_push(buff, ctx->file);
548 if (BIO_buffer_peek(ctx->file, peekbuf, sizeof(peekbuf)-1) > 0) {
549 peekbuf[sizeof(peekbuf)-1] = '\0';
550 if (strstr(peekbuf, "-----BEGIN ") != NULL)
562 static int file_eof(OSSL_STORE_LOADER_CTX *ctx);
563 static int file_error(OSSL_STORE_LOADER_CTX *ctx);
564 static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
565 const UI_METHOD *ui_method, void *ui_data)
567 OSSL_STORE_INFO *result = NULL;
570 if (ctx->last_handler != NULL) {
571 result = ctx->last_handler->try_decode(NULL, NULL, NULL, 0,
572 &ctx->last_handler_ctx,
578 ctx->last_handler->destroy_ctx(&ctx->last_handler_ctx);
579 ctx->last_handler_ctx = NULL;
580 ctx->last_handler = NULL;
587 char *pem_name = NULL; /* PEM record name */
588 char *pem_header = NULL; /* PEM record header */
589 unsigned char *data = NULL; /* DER encoded data */
591 long len = 0; /* DER encoded data length */
596 r = PEM_read_bio(ctx->file, &pem_name, &pem_header, &data, &len);
604 * 10 is the number of characters in "Proc-Type:", which
605 * PEM_get_EVP_CIPHER_INFO() requires to be present.
606 * If the PEM header has less characters than that, it's
607 * not worth spending cycles on it.
609 if (strlen(pem_header) > 10) {
610 EVP_CIPHER_INFO cipher;
611 struct pem_pass_data pass_data;
613 if (!PEM_get_EVP_CIPHER_INFO(pem_header, &cipher)
614 || !file_fill_pem_pass_data(&pass_data, "PEM", ui_method,
616 || !PEM_do_header(&cipher, data, &len, file_get_pem_pass,
623 if ((len = asn1_d2i_read_bio(ctx->file, &mem)) < 0) {
629 data = (unsigned char *)mem->data;
630 len = (long)mem->length;
637 void *handler_ctx = NULL;
638 const FILE_HANDLER **matching_handlers =
639 OPENSSL_zalloc(sizeof(*matching_handlers)
640 * OSSL_NELEM(file_handlers));
642 if (matching_handlers == NULL) {
643 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, ERR_R_MALLOC_FAILURE);
648 for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
649 const FILE_HANDLER *handler = file_handlers[i];
650 void *tmp_handler_ctx = NULL;
651 OSSL_STORE_INFO *tmp_result =
652 handler->try_decode(pem_name, pem_header, data, len,
653 &tmp_handler_ctx, ui_method, ui_data);
655 if (tmp_result == NULL) {
656 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD, OSSL_STORE_R_IS_NOT_A);
657 ERR_add_error_data(1, handler->name);
659 if (matching_handlers)
660 matching_handlers[matchcount] = handler;
663 handler->destroy_ctx(&handler_ctx);
664 handler_ctx = tmp_handler_ctx;
666 if (++matchcount == 1) {
670 /* more than one match => ambiguous, kill any result */
671 OSSL_STORE_INFO_free(result);
672 OSSL_STORE_INFO_free(tmp_result);
673 if (handler->destroy_ctx != NULL)
674 handler->destroy_ctx(&handler_ctx);
682 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
683 OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE);
685 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
686 OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE);
687 else if (matching_handlers[0]->repeatable) {
688 ctx->last_handler = matching_handlers[0];
689 ctx->last_handler_ctx = handler_ctx;
694 OPENSSL_free(matching_handlers);
701 OPENSSL_free(pem_name);
702 OPENSSL_free(pem_header);
707 } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
709 /* We bail out on ambiguity */
717 static int file_error(OSSL_STORE_LOADER_CTX *ctx)
719 return ctx->errcnt > 0;
722 static int file_eof(OSSL_STORE_LOADER_CTX *ctx)
724 if (ctx->last_handler != NULL
725 && !ctx->last_handler->eof(ctx->last_handler_ctx))
727 return BIO_eof(ctx->file);
730 static int file_close(OSSL_STORE_LOADER_CTX *ctx)
732 if (ctx->last_handler != NULL) {
733 ctx->last_handler->destroy_ctx(&ctx->last_handler_ctx);
734 ctx->last_handler_ctx = NULL;
735 ctx->last_handler = NULL;
737 BIO_free_all(ctx->file);
742 static OSSL_STORE_LOADER file_loader =
753 static void store_file_loader_deinit(void)
755 ossl_store_unregister_loader_int(file_loader.scheme);
758 int ossl_store_file_loader_init(void)
760 int ret = ossl_store_register_loader_int(&file_loader);
762 OPENSSL_atexit(store_file_loader_deinit);