Prune low-level ASN.1 parse errors from error queue in decoder_process()
[openssl.git] / providers / implementations / encode_decode / endecoder_common.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 #include <openssl/core.h>
11
12 #include "endecoder_local.h"
13
14 OSSL_FUNC_keymgmt_new_fn *
15 ossl_prov_get_keymgmt_new(const OSSL_DISPATCH *fns)
16 {
17     /* Pilfer the keymgmt dispatch table */
18     for (; fns->function_id != 0; fns++)
19         if (fns->function_id == OSSL_FUNC_KEYMGMT_NEW)
20             return OSSL_FUNC_keymgmt_new(fns);
21
22     return NULL;
23 }
24
25 OSSL_FUNC_keymgmt_free_fn *
26 ossl_prov_get_keymgmt_free(const OSSL_DISPATCH *fns)
27 {
28     /* Pilfer the keymgmt dispatch table */
29     for (; fns->function_id != 0; fns++)
30         if (fns->function_id == OSSL_FUNC_KEYMGMT_FREE)
31             return OSSL_FUNC_keymgmt_free(fns);
32
33     return NULL;
34 }
35
36 OSSL_FUNC_keymgmt_import_fn *
37 ossl_prov_get_keymgmt_import(const OSSL_DISPATCH *fns)
38 {
39     /* Pilfer the keymgmt dispatch table */
40     for (; fns->function_id != 0; fns++)
41         if (fns->function_id == OSSL_FUNC_KEYMGMT_IMPORT)
42             return OSSL_FUNC_keymgmt_import(fns);
43
44     return NULL;
45 }
46
47 OSSL_FUNC_keymgmt_export_fn *
48 ossl_prov_get_keymgmt_export(const OSSL_DISPATCH *fns)
49 {
50     /* Pilfer the keymgmt dispatch table */
51     for (; fns->function_id != 0; fns++)
52         if (fns->function_id == OSSL_FUNC_KEYMGMT_EXPORT)
53             return OSSL_FUNC_keymgmt_export(fns);
54
55     return NULL;
56 }
57
58 void *ossl_prov_import_key(const OSSL_DISPATCH *fns, void *provctx,
59                            int selection, const OSSL_PARAM params[])
60 {
61     OSSL_FUNC_keymgmt_new_fn *kmgmt_new = ossl_prov_get_keymgmt_new(fns);
62     OSSL_FUNC_keymgmt_free_fn *kmgmt_free = ossl_prov_get_keymgmt_free(fns);
63     OSSL_FUNC_keymgmt_import_fn *kmgmt_import =
64         ossl_prov_get_keymgmt_import(fns);
65     void *key = NULL;
66
67     if (kmgmt_new != NULL && kmgmt_import != NULL && kmgmt_free != NULL) {
68         if ((key = kmgmt_new(provctx)) == NULL
69             || !kmgmt_import(key, selection, params)) {
70             kmgmt_free(key);
71             key = NULL;
72         }
73     }
74     return key;
75 }
76
77 void ossl_prov_free_key(const OSSL_DISPATCH *fns, void *key)
78 {
79     OSSL_FUNC_keymgmt_free_fn *kmgmt_free = ossl_prov_get_keymgmt_free(fns);
80
81     if (kmgmt_free != NULL)
82         kmgmt_free(key);
83 }
84