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 * ui_method: Application UI method for getting a password, pin
120 * or any other interactive data.
121 * ui_data: Application data to be passed to ui_method when
126 typedef OSSL_STORE_INFO *(*file_try_decode_fn)(const char *pem_name,
127 const char *pem_header,
128 const unsigned char *blob,
130 const UI_METHOD *ui_method,
133 typedef struct file_handler_st {
135 file_try_decode_fn try_decode;
138 int pem_check_suffix(const char *pem_str, const char *suffix);
139 static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name,
140 const char *pem_header,
141 const unsigned char *blob,
143 const UI_METHOD *ui_method,
146 OSSL_STORE_INFO *store_info = NULL;
147 EVP_PKEY *pkey = NULL;
148 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
150 if (pem_name != NULL) {
153 if ((slen = pem_check_suffix(pem_name, "PRIVATE KEY")) > 0
154 && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name, slen)) != NULL)
155 pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &blob, len);
159 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
160 ameth = EVP_PKEY_asn1_get0(i);
161 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
163 pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &blob, len);
172 store_info = OSSL_STORE_INFO_new_PKEY(pkey);
173 if (store_info == NULL)
178 static FILE_HANDLER PrivateKey_handler = {
180 try_decode_PrivateKey
183 static OSSL_STORE_INFO *try_decode_PUBKEY(const char *pem_name,
184 const char *pem_header,
185 const unsigned char *blob,
187 const UI_METHOD *ui_method,
190 OSSL_STORE_INFO *store_info = NULL;
191 EVP_PKEY *pkey = NULL;
193 if (pem_name != NULL && strcmp(pem_name, PEM_STRING_PUBLIC) != 0)
197 if ((pkey = d2i_PUBKEY(NULL, &blob, len)) != NULL)
198 store_info = OSSL_STORE_INFO_new_PKEY(pkey);
202 static FILE_HANDLER PUBKEY_handler = {
207 static OSSL_STORE_INFO *try_decode_params(const char *pem_name,
208 const char *pem_header,
209 const unsigned char *blob,
211 const UI_METHOD *ui_method,
214 OSSL_STORE_INFO *store_info = NULL;
215 EVP_PKEY *pkey = EVP_PKEY_new();
216 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
220 OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB);
224 if (pem_name != NULL) {
227 if ((slen = pem_check_suffix(pem_name, "PARAMETERS")) > 0
228 && EVP_PKEY_set_type_str(pkey, pem_name, slen)
229 && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL
230 && ameth->param_decode != NULL
231 && ameth->param_decode(pkey, &blob, len))
236 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
237 ameth = EVP_PKEY_asn1_get0(i);
238 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
240 if (EVP_PKEY_set_type(pkey, ameth->pkey_id)
241 && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL
242 && ameth->param_decode != NULL
243 && ameth->param_decode(pkey, &blob, len)) {
251 store_info = OSSL_STORE_INFO_new_PARAMS(pkey);
252 if (store_info == NULL)
257 static FILE_HANDLER params_handler = {
262 static OSSL_STORE_INFO *try_decode_X509Certificate(const char *pem_name,
263 const char *pem_header,
264 const unsigned char *blob,
266 const UI_METHOD *ui_method,
269 OSSL_STORE_INFO *store_info = NULL;
273 * In most cases, we can try to interpret the serialized data as a trusted
274 * cert (X509 + X509_AUX) and fall back to reading it as a normal cert
275 * (just X509), but if the PEM name specifically declares it as a trusted
276 * cert, then no fallback should be engaged. |ignore_trusted| tells if
277 * the fallback can be used (1) or not (0).
279 int ignore_trusted = 1;
281 if (pem_name != NULL) {
282 if (strcmp(pem_name, PEM_STRING_X509_TRUSTED) == 0)
284 else if (strcmp(pem_name, PEM_STRING_X509_OLD) != 0
285 && strcmp(pem_name, PEM_STRING_X509) != 0)
290 if ((cert = d2i_X509_AUX(NULL, &blob, len)) != NULL
291 || (ignore_trusted && (cert = d2i_X509(NULL, &blob, len)) != NULL))
292 store_info = OSSL_STORE_INFO_new_CERT(cert);
294 if (store_info == NULL)
299 static FILE_HANDLER X509Certificate_handler = {
301 try_decode_X509Certificate
304 static OSSL_STORE_INFO *try_decode_X509CRL(const char *pem_name,
305 const char *pem_header,
306 const unsigned char *blob,
308 const UI_METHOD *ui_method,
311 OSSL_STORE_INFO *store_info = NULL;
312 X509_CRL *crl = NULL;
315 && strcmp(pem_name, PEM_STRING_X509_CRL) != 0)
319 if ((crl = d2i_X509_CRL(NULL, &blob, len)) != NULL)
320 store_info = OSSL_STORE_INFO_new_CRL(crl);
322 if (store_info == NULL)
327 static FILE_HANDLER X509CRL_handler = {
332 static const FILE_HANDLER *file_handlers[] = {
333 &X509Certificate_handler,
345 struct ossl_store_loader_ctx_st {
351 static OSSL_STORE_LOADER_CTX *file_open(const OSSL_STORE_LOADER *loader,
353 const UI_METHOD *ui_method,
358 OSSL_STORE_LOADER_CTX *ctx = NULL;
359 const char *path = NULL;
361 if (strncasecmp(uri, "file:", 5) == 0) {
362 if (strncmp(&uri[5], "//localhost/", 12) == 0) {
364 } else if (strncmp(&uri[5], "///", 3) == 0) {
366 } else if (strncmp(&uri[5], "//", 2) != 0) {
369 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
370 OSSL_STORE_R_URI_AUTHORITY_UNSUPPORED);
375 * If the scheme "file" was an explicit part of the URI, the path must
376 * be absolute. So says RFC 8089
378 if (path[0] != '/') {
379 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN,
380 OSSL_STORE_R_PATH_MUST_BE_ABSOLUTE);
385 /* Windows file: URIs with a drive letter start with a / */
386 if (path[0] == '/' && path[2] == ':' && path[3] == '/')
394 ctx = OPENSSL_zalloc(sizeof(*ctx));
396 OSSL_STOREerr(OSSL_STORE_F_FILE_OPEN, ERR_R_MALLOC_FAILURE);
400 if ((buff = BIO_new(BIO_f_buffer())) == NULL)
402 if ((ctx->file = BIO_new_file(path, "rb")) == NULL) {
405 ctx->file = BIO_push(buff, ctx->file);
406 if (BIO_buffer_peek(ctx->file, peekbuf, sizeof(peekbuf)-1) > 0) {
407 peekbuf[sizeof(peekbuf)-1] = '\0';
408 if (strstr(peekbuf, "-----BEGIN ") != NULL)
420 static int file_eof(OSSL_STORE_LOADER_CTX *ctx);
421 static int file_error(OSSL_STORE_LOADER_CTX *ctx);
422 static OSSL_STORE_INFO *file_load(OSSL_STORE_LOADER_CTX *ctx,
423 const UI_METHOD *ui_method, void *ui_data)
425 OSSL_STORE_INFO *result = NULL;
432 char *pem_name = NULL; /* PEM record name */
433 char *pem_header = NULL; /* PEM record header */
434 unsigned char *data = NULL; /* DER encoded data */
436 long len = 0; /* DER encoded data length */
439 file_try_decode_fn *matching_functions = NULL;
443 r = PEM_read_bio(ctx->file, &pem_name, &pem_header, &data, &len);
451 * 10 is the number of characters in "Proc-Type:", which
452 * PEM_get_EVP_CIPHER_INFO() requires to be present.
453 * If the PEM header has less characters than that, it's
454 * not worth spending cycles on it.
456 if (strlen(pem_header) > 10) {
457 EVP_CIPHER_INFO cipher;
458 struct pem_pass_data pass_data;
460 if (!PEM_get_EVP_CIPHER_INFO(pem_header, &cipher)
461 || !file_fill_pem_pass_data(&pass_data, "PEM", ui_method,
463 || !PEM_do_header(&cipher, data, &len, file_get_pem_pass,
470 #if 0 /* PKCS12 not yet ready */
471 PKCS12 *pkcs12 =NULL;
474 if ((len = asn1_d2i_read_bio(ctx->file, &mem)) < 0) {
480 data = (unsigned char *)mem->data;
481 len = (long)mem->length;
483 #if 0 /* PKCS12 not yet ready */
484 /* Try and see if we loaded a PKCS12 */
485 pkcs12 = d2i_PKCS12(NULL, &data, len);
491 matching_functions = OPENSSL_zalloc(sizeof(*matching_functions)
492 * OSSL_NELEM(file_handlers));
494 for (i = 0; i < OSSL_NELEM(file_handlers); i++) {
495 const FILE_HANDLER *handler = file_handlers[i];
496 OSSL_STORE_INFO *tmp_result =
497 handler->try_decode(pem_name, pem_header, data, len, ui_method,
500 if (tmp_result != NULL) {
501 if (matching_functions)
502 matching_functions[matchcount] = handler->try_decode;
504 if (++matchcount == 1) {
508 /* more than one match => ambiguous, kill any result */
509 OSSL_STORE_INFO_free(result);
510 OSSL_STORE_INFO_free(tmp_result);
517 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
518 OSSL_STORE_R_AMBIGUOUS_CONTENT_TYPE);
520 OSSL_STOREerr(OSSL_STORE_F_FILE_LOAD,
521 OSSL_STORE_R_UNSUPPORTED_CONTENT_TYPE);
527 OPENSSL_free(matching_functions);
528 OPENSSL_free(pem_name);
529 OPENSSL_free(pem_header);
534 } while (matchcount == 0 && !file_eof(ctx) && !file_error(ctx));
536 /* We bail out on ambiguity */
544 static int file_error(OSSL_STORE_LOADER_CTX *ctx)
546 return ctx->errcnt > 0;
549 static int file_eof(OSSL_STORE_LOADER_CTX *ctx)
551 return BIO_eof(ctx->file);
554 static int file_close(OSSL_STORE_LOADER_CTX *ctx)
556 BIO_free_all(ctx->file);
561 static OSSL_STORE_LOADER file_loader =
572 static void store_file_loader_deinit(void)
574 ossl_store_unregister_loader_int(file_loader.scheme);
577 int ossl_store_file_loader_init(void)
579 int ret = ossl_store_register_loader_int(&file_loader);
581 OPENSSL_atexit(store_file_loader_deinit);