Line data Source code
1 : /* SPDX-License-Identifier: BSD-3-Clause 2 : * Copyright (c) 2024 Intel Corporation. All rights reserved. 3 : */ 4 : 5 : #include "keyring_file.h" 6 : #include "spdk/json.h" 7 : #include "spdk/keyring_module.h" 8 : #include "spdk/rpc.h" 9 : #include "spdk/string.h" 10 : #include "spdk/util.h" 11 : 12 : static const struct spdk_json_object_decoder keyring_file_key_opts_decoders[] = { 13 : {"name", offsetof(struct keyring_file_key_opts, name), spdk_json_decode_string}, 14 : {"path", offsetof(struct keyring_file_key_opts, path), spdk_json_decode_string}, 15 : }; 16 : 17 : static void 18 0 : free_keyring_file_key_opts(struct keyring_file_key_opts *opts) 19 : { 20 0 : free(opts->name); 21 0 : free(opts->path); 22 0 : } 23 : 24 : static void 25 0 : rpc_keyring_file_add_key(struct spdk_jsonrpc_request *request, 26 : const struct spdk_json_val *params) 27 : { 28 0 : struct spdk_key_opts opts = {}; 29 0 : struct keyring_file_key_opts kopts = {}; 30 : int rc; 31 : 32 0 : if (spdk_json_decode_object_relaxed(params, keyring_file_key_opts_decoders, 33 : SPDK_COUNTOF(keyring_file_key_opts_decoders), 34 : &kopts)) { 35 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 36 : spdk_strerror(EINVAL)); 37 0 : return; 38 : } 39 : 40 0 : opts.size = SPDK_SIZEOF(&opts, ctx); 41 0 : opts.name = kopts.name; 42 0 : opts.module = &g_keyring_file; 43 0 : opts.ctx = &kopts; 44 0 : rc = spdk_keyring_add_key(&opts); 45 0 : if (rc != 0) { 46 0 : spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc)); 47 0 : goto out; 48 : } 49 : 50 0 : spdk_jsonrpc_send_bool_response(request, true); 51 0 : out: 52 0 : free_keyring_file_key_opts(&kopts); 53 : } 54 0 : SPDK_RPC_REGISTER("keyring_file_add_key", rpc_keyring_file_add_key, SPDK_RPC_RUNTIME) 55 : 56 : struct rpc_keyring_file_remove_key { 57 : char *name; 58 : }; 59 : 60 : static const struct spdk_json_object_decoder rpc_keyring_file_remove_key_decoders[] = { 61 : {"name", offsetof(struct rpc_keyring_file_remove_key, name), spdk_json_decode_string}, 62 : }; 63 : 64 : static void 65 0 : free_rpc_keyring_file_remove_key(struct rpc_keyring_file_remove_key *r) 66 : { 67 0 : free(r->name); 68 0 : } 69 : 70 : static void 71 0 : rpc_keyring_file_remove_key(struct spdk_jsonrpc_request *request, 72 : const struct spdk_json_val *params) 73 : { 74 0 : struct rpc_keyring_file_remove_key req = {}; 75 : 76 0 : if (spdk_json_decode_object(params, rpc_keyring_file_remove_key_decoders, 77 : SPDK_COUNTOF(rpc_keyring_file_remove_key_decoders), 78 : &req)) { 79 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 80 : spdk_strerror(EINVAL)); 81 0 : return; 82 : } 83 : 84 0 : spdk_keyring_remove_key(req.name); 85 0 : spdk_jsonrpc_send_bool_response(request, true); 86 0 : free_rpc_keyring_file_remove_key(&req); 87 : } 88 0 : SPDK_RPC_REGISTER("keyring_file_remove_key", rpc_keyring_file_remove_key, SPDK_RPC_RUNTIME)