1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <openssl/core.h>
11e1051a39Sopenharmony_ci#include <openssl/buffer.h>
12e1051a39Sopenharmony_ci#include "internal/asn1.h"
13e1051a39Sopenharmony_ci#include "prov/bio.h"
14e1051a39Sopenharmony_ci#include "endecoder_local.h"
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ciOSSL_FUNC_keymgmt_new_fn *
17e1051a39Sopenharmony_ciossl_prov_get_keymgmt_new(const OSSL_DISPATCH *fns)
18e1051a39Sopenharmony_ci{
19e1051a39Sopenharmony_ci    /* Pilfer the keymgmt dispatch table */
20e1051a39Sopenharmony_ci    for (; fns->function_id != 0; fns++)
21e1051a39Sopenharmony_ci        if (fns->function_id == OSSL_FUNC_KEYMGMT_NEW)
22e1051a39Sopenharmony_ci            return OSSL_FUNC_keymgmt_new(fns);
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_ci    return NULL;
25e1051a39Sopenharmony_ci}
26e1051a39Sopenharmony_ci
27e1051a39Sopenharmony_ciOSSL_FUNC_keymgmt_free_fn *
28e1051a39Sopenharmony_ciossl_prov_get_keymgmt_free(const OSSL_DISPATCH *fns)
29e1051a39Sopenharmony_ci{
30e1051a39Sopenharmony_ci    /* Pilfer the keymgmt dispatch table */
31e1051a39Sopenharmony_ci    for (; fns->function_id != 0; fns++)
32e1051a39Sopenharmony_ci        if (fns->function_id == OSSL_FUNC_KEYMGMT_FREE)
33e1051a39Sopenharmony_ci            return OSSL_FUNC_keymgmt_free(fns);
34e1051a39Sopenharmony_ci
35e1051a39Sopenharmony_ci    return NULL;
36e1051a39Sopenharmony_ci}
37e1051a39Sopenharmony_ci
38e1051a39Sopenharmony_ciOSSL_FUNC_keymgmt_import_fn *
39e1051a39Sopenharmony_ciossl_prov_get_keymgmt_import(const OSSL_DISPATCH *fns)
40e1051a39Sopenharmony_ci{
41e1051a39Sopenharmony_ci    /* Pilfer the keymgmt dispatch table */
42e1051a39Sopenharmony_ci    for (; fns->function_id != 0; fns++)
43e1051a39Sopenharmony_ci        if (fns->function_id == OSSL_FUNC_KEYMGMT_IMPORT)
44e1051a39Sopenharmony_ci            return OSSL_FUNC_keymgmt_import(fns);
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_ci    return NULL;
47e1051a39Sopenharmony_ci}
48e1051a39Sopenharmony_ci
49e1051a39Sopenharmony_ciOSSL_FUNC_keymgmt_export_fn *
50e1051a39Sopenharmony_ciossl_prov_get_keymgmt_export(const OSSL_DISPATCH *fns)
51e1051a39Sopenharmony_ci{
52e1051a39Sopenharmony_ci    /* Pilfer the keymgmt dispatch table */
53e1051a39Sopenharmony_ci    for (; fns->function_id != 0; fns++)
54e1051a39Sopenharmony_ci        if (fns->function_id == OSSL_FUNC_KEYMGMT_EXPORT)
55e1051a39Sopenharmony_ci            return OSSL_FUNC_keymgmt_export(fns);
56e1051a39Sopenharmony_ci
57e1051a39Sopenharmony_ci    return NULL;
58e1051a39Sopenharmony_ci}
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_civoid *ossl_prov_import_key(const OSSL_DISPATCH *fns, void *provctx,
61e1051a39Sopenharmony_ci                           int selection, const OSSL_PARAM params[])
62e1051a39Sopenharmony_ci{
63e1051a39Sopenharmony_ci    OSSL_FUNC_keymgmt_new_fn *kmgmt_new = ossl_prov_get_keymgmt_new(fns);
64e1051a39Sopenharmony_ci    OSSL_FUNC_keymgmt_free_fn *kmgmt_free = ossl_prov_get_keymgmt_free(fns);
65e1051a39Sopenharmony_ci    OSSL_FUNC_keymgmt_import_fn *kmgmt_import =
66e1051a39Sopenharmony_ci        ossl_prov_get_keymgmt_import(fns);
67e1051a39Sopenharmony_ci    void *key = NULL;
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_ci    if (kmgmt_new != NULL && kmgmt_import != NULL && kmgmt_free != NULL) {
70e1051a39Sopenharmony_ci        if ((key = kmgmt_new(provctx)) == NULL
71e1051a39Sopenharmony_ci            || !kmgmt_import(key, selection, params)) {
72e1051a39Sopenharmony_ci            kmgmt_free(key);
73e1051a39Sopenharmony_ci            key = NULL;
74e1051a39Sopenharmony_ci        }
75e1051a39Sopenharmony_ci    }
76e1051a39Sopenharmony_ci    return key;
77e1051a39Sopenharmony_ci}
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_civoid ossl_prov_free_key(const OSSL_DISPATCH *fns, void *key)
80e1051a39Sopenharmony_ci{
81e1051a39Sopenharmony_ci    OSSL_FUNC_keymgmt_free_fn *kmgmt_free = ossl_prov_get_keymgmt_free(fns);
82e1051a39Sopenharmony_ci
83e1051a39Sopenharmony_ci    if (kmgmt_free != NULL)
84e1051a39Sopenharmony_ci        kmgmt_free(key);
85e1051a39Sopenharmony_ci}
86e1051a39Sopenharmony_ci
87e1051a39Sopenharmony_ciint ossl_read_der(PROV_CTX *provctx, OSSL_CORE_BIO *cin,  unsigned char **data,
88e1051a39Sopenharmony_ci                  long *len)
89e1051a39Sopenharmony_ci{
90e1051a39Sopenharmony_ci    BUF_MEM *mem = NULL;
91e1051a39Sopenharmony_ci    BIO *in = ossl_bio_new_from_core_bio(provctx, cin);
92e1051a39Sopenharmony_ci    int ok;
93e1051a39Sopenharmony_ci
94e1051a39Sopenharmony_ci    if (in == NULL)
95e1051a39Sopenharmony_ci        return 0;
96e1051a39Sopenharmony_ci    ok = (asn1_d2i_read_bio(in, &mem) >= 0);
97e1051a39Sopenharmony_ci    if (ok) {
98e1051a39Sopenharmony_ci        *data = (unsigned char *)mem->data;
99e1051a39Sopenharmony_ci        *len = (long)mem->length;
100e1051a39Sopenharmony_ci        OPENSSL_free(mem);
101e1051a39Sopenharmony_ci    }
102e1051a39Sopenharmony_ci    BIO_free(in);
103e1051a39Sopenharmony_ci    return ok;
104e1051a39Sopenharmony_ci}
105