YPC  0.2.0
keymgr_interface.h
1 #pragma once
2 #include "ypc/common/limits.h"
3 #include "ypc/core_t/analyzer/analyzer_context.h"
4 #include "ypc/core_t/analyzer/var/enclave_hash_var.h"
5 #include "ypc/core_t/analyzer/var/keymgr_var.h"
6 #include "ypc/core_t/analyzer/var/request_key_var.h"
7 #include "ypc/core_t/ecommon/package.h"
8 #include "ypc/corecommon/package.h"
9 #include "ypc/stbox/ebyte.h"
10 #include "ypc/stbox/stx_status.h"
11 #include "ypc/stbox/tsgx/channel/dh_session_initiator.h"
12 #include "ypc/stbox/tsgx/ocall.h"
13 
14 namespace ypc {
15 namespace internal {
16 
17 template <typename Crypto>
18 class keymgr_interface : virtual public enclave_hash_var,
19  virtual public keymgr_var,
20  virtual public request_key_var<true>,
21  virtual public analyzer_context {
22  typedef Crypto ecc;
23 
24 public:
25  uint32_t request_private_key_for_public_key(const stbox::bytes &pubkey,
26  stbox::bytes &private_key,
27  stbox::bytes &dian_pkey) {
28  stbox::bytes request_msg = ypc::make_bytes<stbox::bytes>::for_package<
29  request_skey_from_pkey_pkg_t, nt<stbox::bytes>::pkey>(pubkey);
30 
31  stbox::bytes recv_bytes;
32  auto status = keymgr_var::m_keymgr_session->send_request_recv_response(
33  (char *)request_msg.data(), request_msg.size(),
34  utc::max_keymgr_response_buf_size, recv_bytes);
35  if (recv_bytes.size() !=
36  ecc::get_private_key_size() + ecc::get_public_key_size()) {
37  return stbox::stx_status::ecc_invalid_skey_size;
38  }
39  private_key = stbox::bytes(recv_bytes.data(), ecc::get_private_key_size());
40  dian_pkey = stbox::bytes(recv_bytes.data() + private_key.size(),
41  ecc::get_public_key_size());
42 
43  if (status != stbox::stx_status::success) {
44  LOG(ERROR) << "error for m_keymgr_session->send_request_recv_response: "
45  << stbox::status_string(status);
46  return status;
47  }
48  stbox::bytes check_pkey;
49  status = (stbox::stx_status)ecc::generate_pkey_from_skey(private_key,
50  check_pkey);
51  if (status) {
52  LOG(ERROR) << "error for generate_ecc_pkey_from_skey: " << status;
53  return status;
54  }
55 
56  if (pubkey != check_pkey) {
57  LOG(ERROR) << "check failed, invalid private key";
58  return stbox::stx_status::kmgr_session_inconsistency_pkey_skey;
59  }
60  return status;
61  }
62 };
63 } // namespace internal
64 } // namespace ypc
ypc::internal::keymgr_var
Definition: keymgr_var.h:8
ypc::nt
Definition: nt_cols.h:6
ypc::internal::enclave_hash_var
Definition: enclave_hash_var.h:7
ypc::internal::request_key_var
Definition: request_key_var.h:7
ypc::analyzer_context
Definition: analyzer_context.h:5
ypc::internal::keymgr_interface
Definition: keymgr_interface.h:18
ypc::crypto::crypto_pack
Definition: crypto_pack.h:14
ypc::make_bytes
Definition: package.h:7