2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 * This is a decoder that's completely internal to the 'file:' store
12 * implementation. Only code in file_store.c know about this one. Because
13 * of this close relationship, we can cut certain corners, such as making
14 * assumptions about the "provider context", which is currently simply the
15 * provider context that the file_store.c code operates within.
17 * All this does is to read DER from the input if it can, and passes it on
18 * to the data callback as an object abstraction, leaving it to the callback
19 * to figure out what it actually is.
21 * This MUST be made the last decoder in a chain, leaving it to other more
22 * specialized decoders to recognise and process their stuff first.
25 #include <openssl/core_dispatch.h>
26 #include <openssl/core_names.h>
27 #include <openssl/core_object.h>
28 #include <openssl/bio.h>
29 #include <openssl/buffer.h>
30 #include <openssl/err.h>
31 #include <openssl/asn1err.h>
32 #include <openssl/params.h>
33 #include "internal/asn1.h"
35 #include "file_store_local.h"
38 * newctx and freectx are not strictly necessary. However, the method creator,
39 * ossl_decoder_from_dispatch(), demands that they exist, so we make sure to
43 static OSSL_FUNC_decoder_newctx_fn der2obj_newctx;
44 static OSSL_FUNC_decoder_freectx_fn der2obj_freectx;
46 static void *der2obj_newctx(void *provctx)
51 static void der2obj_freectx(void *vctx)
55 static OSSL_FUNC_decoder_gettable_params_fn der2obj_gettable_params;
56 static OSSL_FUNC_decoder_get_params_fn der2obj_get_params;
57 static OSSL_FUNC_decoder_decode_fn der2obj_decode;
59 static const OSSL_PARAM *der2obj_gettable_params(void *provctx)
61 static const OSSL_PARAM gettables[] = {
62 { OSSL_DECODER_PARAM_INPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
69 static int der2obj_get_params(OSSL_PARAM params[])
73 p = OSSL_PARAM_locate(params, OSSL_DECODER_PARAM_INPUT_TYPE);
74 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "DER"))
80 static int der2obj_decode(void *provctx, OSSL_CORE_BIO *cin, int selection,
81 OSSL_CALLBACK *data_cb, void *data_cbarg,
82 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
85 * We're called from file_store.c, so we know that OSSL_CORE_BIO is a
93 ok = (asn1_d2i_read_bio(in, &mem) >= 0);
95 * Prune low-level ASN.1 parse errors from error queue, assuming that
96 * this is called by decoder_process() in a loop trying several formats.
98 err = ERR_peek_last_error();
99 if (ERR_GET_LIB(err) == ERR_LIB_ASN1
100 && (ERR_GET_REASON(err) == ASN1_R_HEADER_TOO_LONG
101 || ERR_GET_REASON(err) == ERR_R_NESTED_ASN1_ERROR))
104 ERR_clear_last_mark();
106 OSSL_PARAM params[3];
107 int object_type = OSSL_OBJECT_UNKNOWN;
110 OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
112 OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
113 mem->data, mem->length);
114 params[2] = OSSL_PARAM_construct_end();
116 ok = data_cb(params, data_cbarg);
117 OPENSSL_free(mem->data);
123 static const OSSL_DISPATCH der_to_obj_decoder_functions[] = {
124 { OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))der2obj_newctx },
125 { OSSL_FUNC_DECODER_FREECTX, (void (*)(void))der2obj_freectx },
126 { OSSL_FUNC_DECODER_GETTABLE_PARAMS,
127 (void (*)(void))der2obj_gettable_params },
128 { OSSL_FUNC_DECODER_GET_PARAMS, (void (*)(void))der2obj_get_params },
129 { OSSL_FUNC_DECODER_DECODE, (void (*)(void))der2obj_decode },
133 const OSSL_ALGORITHM ossl_der_to_obj_algorithm =
134 { "obj", NULL, der_to_obj_decoder_functions };