/src/openssl31/providers/implementations/encode_decode/endecoder_common.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2020-2022 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 | | #include <openssl/buffer.h> |
12 | | #include "internal/asn1.h" |
13 | | #include "prov/bio.h" |
14 | | #include "endecoder_local.h" |
15 | | |
16 | | OSSL_FUNC_keymgmt_new_fn * |
17 | | ossl_prov_get_keymgmt_new(const OSSL_DISPATCH *fns) |
18 | 0 | { |
19 | | /* Pilfer the keymgmt dispatch table */ |
20 | 0 | for (; fns->function_id != 0; fns++) |
21 | 0 | if (fns->function_id == OSSL_FUNC_KEYMGMT_NEW) |
22 | 0 | return OSSL_FUNC_keymgmt_new(fns); |
23 | | |
24 | 0 | return NULL; |
25 | 0 | } |
26 | | |
27 | | OSSL_FUNC_keymgmt_free_fn * |
28 | | ossl_prov_get_keymgmt_free(const OSSL_DISPATCH *fns) |
29 | 0 | { |
30 | | /* Pilfer the keymgmt dispatch table */ |
31 | 0 | for (; fns->function_id != 0; fns++) |
32 | 0 | if (fns->function_id == OSSL_FUNC_KEYMGMT_FREE) |
33 | 0 | return OSSL_FUNC_keymgmt_free(fns); |
34 | | |
35 | 0 | return NULL; |
36 | 0 | } |
37 | | |
38 | | OSSL_FUNC_keymgmt_import_fn * |
39 | | ossl_prov_get_keymgmt_import(const OSSL_DISPATCH *fns) |
40 | 0 | { |
41 | | /* Pilfer the keymgmt dispatch table */ |
42 | 0 | for (; fns->function_id != 0; fns++) |
43 | 0 | if (fns->function_id == OSSL_FUNC_KEYMGMT_IMPORT) |
44 | 0 | return OSSL_FUNC_keymgmt_import(fns); |
45 | | |
46 | 0 | return NULL; |
47 | 0 | } |
48 | | |
49 | | OSSL_FUNC_keymgmt_export_fn * |
50 | | ossl_prov_get_keymgmt_export(const OSSL_DISPATCH *fns) |
51 | 0 | { |
52 | | /* Pilfer the keymgmt dispatch table */ |
53 | 0 | for (; fns->function_id != 0; fns++) |
54 | 0 | if (fns->function_id == OSSL_FUNC_KEYMGMT_EXPORT) |
55 | 0 | return OSSL_FUNC_keymgmt_export(fns); |
56 | | |
57 | 0 | return NULL; |
58 | 0 | } |
59 | | |
60 | | void *ossl_prov_import_key(const OSSL_DISPATCH *fns, void *provctx, |
61 | | int selection, const OSSL_PARAM params[]) |
62 | 0 | { |
63 | 0 | OSSL_FUNC_keymgmt_new_fn *kmgmt_new = ossl_prov_get_keymgmt_new(fns); |
64 | 0 | OSSL_FUNC_keymgmt_free_fn *kmgmt_free = ossl_prov_get_keymgmt_free(fns); |
65 | 0 | OSSL_FUNC_keymgmt_import_fn *kmgmt_import = |
66 | 0 | ossl_prov_get_keymgmt_import(fns); |
67 | 0 | void *key = NULL; |
68 | |
|
69 | 0 | if (kmgmt_new != NULL && kmgmt_import != NULL && kmgmt_free != NULL) { |
70 | 0 | if ((key = kmgmt_new(provctx)) == NULL |
71 | 0 | || !kmgmt_import(key, selection, params)) { |
72 | 0 | kmgmt_free(key); |
73 | 0 | key = NULL; |
74 | 0 | } |
75 | 0 | } |
76 | 0 | return key; |
77 | 0 | } |
78 | | |
79 | | void ossl_prov_free_key(const OSSL_DISPATCH *fns, void *key) |
80 | 0 | { |
81 | 0 | OSSL_FUNC_keymgmt_free_fn *kmgmt_free = ossl_prov_get_keymgmt_free(fns); |
82 | |
|
83 | 0 | if (kmgmt_free != NULL) |
84 | 0 | kmgmt_free(key); |
85 | 0 | } |
86 | | |
87 | | int ossl_read_der(PROV_CTX *provctx, OSSL_CORE_BIO *cin, unsigned char **data, |
88 | | long *len) |
89 | 3.69M | { |
90 | 3.69M | BUF_MEM *mem = NULL; |
91 | 3.69M | BIO *in = ossl_bio_new_from_core_bio(provctx, cin); |
92 | 3.69M | int ok; |
93 | | |
94 | 3.69M | if (in == NULL) |
95 | 0 | return 0; |
96 | 3.69M | ok = (asn1_d2i_read_bio(in, &mem) >= 0); |
97 | 3.69M | if (ok) { |
98 | 3.20M | *data = (unsigned char *)mem->data; |
99 | 3.20M | *len = (long)mem->length; |
100 | 3.20M | OPENSSL_free(mem); |
101 | 3.20M | } |
102 | 3.69M | BIO_free(in); |
103 | 3.69M | return ok; |
104 | 3.69M | } |