STORE: Add a built-in 'file:' storemgmt implementation (loader)
[openssl.git] / providers / implementations / storemgmt / file_store_der2obj.c
1 /*
2  * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
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.
16  *
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.
20  *
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.
23  */
24
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"
32 #include "prov/bio.h"
33 #include "file_store_local.h"
34
35 /*
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
38  * oblige.
39  */
40
41 static OSSL_FUNC_decoder_newctx_fn der2obj_newctx;
42 static OSSL_FUNC_decoder_freectx_fn der2obj_freectx;
43
44 static void *der2obj_newctx(void *provctx)
45 {
46     return provctx;
47 }
48
49 static void der2obj_freectx(void *vctx)
50 {
51 }
52
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;
56
57 static const OSSL_PARAM *der2obj_gettable_params(void *provctx)
58 {
59     static const OSSL_PARAM gettables[] = {
60         { OSSL_DECODER_PARAM_INPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
61         OSSL_PARAM_END,
62     };
63
64     return gettables;
65 }
66
67 static int der2obj_get_params(OSSL_PARAM params[])
68 {
69     OSSL_PARAM *p;
70
71     p = OSSL_PARAM_locate(params, OSSL_DECODER_PARAM_INPUT_TYPE);
72     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "DER"))
73         return 0;
74
75     return 1;
76 }
77
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)
81 {
82     /*
83      * We're called from file_store.c, so we know that OSSL_CORE_BIO is a
84      * BIO in this case.
85      */
86     BIO *in = (BIO *)cin;
87     BUF_MEM *mem = NULL;
88     int ok = (asn1_d2i_read_bio(in, &mem) >= 0);
89
90     if (ok) {
91         OSSL_PARAM params[3];
92         int object_type = OSSL_OBJECT_UNKNOWN;
93
94         params[0] =
95             OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
96         params[1] =
97             OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
98                                               mem->data, mem->length);
99         params[2] = OSSL_PARAM_construct_end();
100
101         ok = data_cb(params, data_cbarg);
102         OPENSSL_free(mem->data);
103         OPENSSL_free(mem);
104     }
105     return ok;
106 }
107
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 },
115     { 0, NULL }
116 };
117
118 const OSSL_ALGORITHM der_to_obj_algorithm =
119     { "obj", NULL, der_to_obj_decoder_functions };