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/params.h>
31 #include "internal/asn1.h"
33 #include "file_store_local.h"
36 * newctx and freectx are not strictly necessary. However, the method creator,
37 * ossl_decoder_from_dispatch(), demands that they exist, so we make sure to
41 static OSSL_FUNC_decoder_newctx_fn der2obj_newctx
;
42 static OSSL_FUNC_decoder_freectx_fn der2obj_freectx
;
44 static void *der2obj_newctx(void *provctx
)
49 static void der2obj_freectx(void *vctx
)
53 static OSSL_FUNC_decoder_gettable_params_fn der2obj_gettable_params
;
54 static OSSL_FUNC_decoder_get_params_fn der2obj_get_params
;
55 static OSSL_FUNC_decoder_decode_fn der2obj_decode
;
57 static const OSSL_PARAM
*der2obj_gettable_params(void *provctx
)
59 static const OSSL_PARAM gettables
[] = {
60 { OSSL_DECODER_PARAM_INPUT_TYPE
, OSSL_PARAM_UTF8_PTR
, NULL
, 0, 0 },
67 static int der2obj_get_params(OSSL_PARAM params
[])
71 p
= OSSL_PARAM_locate(params
, OSSL_DECODER_PARAM_INPUT_TYPE
);
72 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_ptr(p
, "DER"))
78 static int der2obj_decode(void *provctx
, OSSL_CORE_BIO
*cin
,
79 OSSL_CALLBACK
*data_cb
, void *data_cbarg
,
80 OSSL_PASSPHRASE_CALLBACK
*pw_cb
, void *pw_cbarg
)
83 * We're called from file_store.c, so we know that OSSL_CORE_BIO is a
88 int ok
= (asn1_d2i_read_bio(in
, &mem
) >= 0);
92 int object_type
= OSSL_OBJECT_UNKNOWN
;
95 OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE
, &object_type
);
97 OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA
,
98 mem
->data
, mem
->length
);
99 params
[2] = OSSL_PARAM_construct_end();
101 ok
= data_cb(params
, data_cbarg
);
102 OPENSSL_free(mem
->data
);
108 static const OSSL_DISPATCH der_to_obj_decoder_functions
[] = {
109 { OSSL_FUNC_DECODER_NEWCTX
, (void (*)(void))der2obj_newctx
},
110 { OSSL_FUNC_DECODER_FREECTX
, (void (*)(void))der2obj_freectx
},
111 { OSSL_FUNC_DECODER_GETTABLE_PARAMS
,
112 (void (*)(void))der2obj_gettable_params
},
113 { OSSL_FUNC_DECODER_GET_PARAMS
, (void (*)(void))der2obj_get_params
},
114 { OSSL_FUNC_DECODER_DECODE
, (void (*)(void))der2obj_decode
},
118 const OSSL_ALGORITHM der_to_obj_algorithm
=
119 { "obj", NULL
, der_to_obj_decoder_functions
};