xref: /freebsd/crypto/openssl/providers/implementations/skeymgmt/generic.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1 /*
2  * Copyright 2025 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_dispatch.h>
11 #include <openssl/core_names.h>
12 #include "crypto/types.h"
13 #include "internal/skey.h"
14 #include "prov/provider_ctx.h"
15 #include "prov/providercommon.h"
16 #include "prov/implementations.h"
17 #include "skeymgmt_lcl.h"
18 
generic_free(void * keydata)19 void generic_free(void *keydata)
20 {
21     PROV_SKEY *generic = keydata;
22 
23     if (generic == NULL)
24         return;
25 
26     OPENSSL_free(generic->data);
27     OPENSSL_free(generic);
28 }
29 
generic_import(void * provctx,int selection,const OSSL_PARAM params[])30 void *generic_import(void *provctx, int selection, const OSSL_PARAM params[])
31 {
32     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
33     const OSSL_PARAM *raw_bytes;
34     PROV_SKEY *generic = NULL;
35     int ok = 0;
36 
37     if (!ossl_prov_is_running())
38         return NULL;
39 
40     if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0)
41         return NULL;
42 
43     raw_bytes = OSSL_PARAM_locate_const(params, OSSL_SKEY_PARAM_RAW_BYTES);
44     if (raw_bytes == NULL)
45         return NULL;
46 
47     generic = OPENSSL_zalloc(sizeof(PROV_SKEY));
48     if (generic == NULL)
49         return NULL;
50 
51     generic->libctx = libctx;
52 
53     generic->type = SKEY_TYPE_GENERIC;
54 
55     if ((generic->data = OPENSSL_memdup(raw_bytes->data, raw_bytes->data_size)) == NULL)
56         goto end;
57     generic->length = raw_bytes->data_size;
58     ok = 1;
59 
60 end:
61     if (ok == 0) {
62         generic_free(generic);
63         generic = NULL;
64     }
65     return generic;
66 }
67 
generic_export(void * keydata,int selection,OSSL_CALLBACK * param_callback,void * cbarg)68 int generic_export(void *keydata, int selection,
69                    OSSL_CALLBACK *param_callback, void *cbarg)
70 {
71     PROV_SKEY *gen = keydata;
72     OSSL_PARAM params[2];
73 
74     if (!ossl_prov_is_running() || gen == NULL)
75         return 0;
76 
77     /* If we use generic SKEYMGMT as a "base class", we shouldn't check the type */
78     if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0)
79         return 0;
80 
81     params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES,
82                                                   gen->data, gen->length);
83     params[1] = OSSL_PARAM_construct_end();
84 
85     return param_callback(params, cbarg);
86 }
87 
88 const OSSL_DISPATCH ossl_generic_skeymgmt_functions[] = {
89     { OSSL_FUNC_SKEYMGMT_FREE, (void (*)(void))generic_free },
90     { OSSL_FUNC_SKEYMGMT_IMPORT, (void (*)(void))generic_import },
91     { OSSL_FUNC_SKEYMGMT_EXPORT, (void (*)(void))generic_export },
92     OSSL_DISPATCH_END
93 };
94