Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sysio::wallet::detail::yubihsm_wallet_impl Struct Reference
Collaboration diagram for sysio::wallet::detail::yubihsm_wallet_impl:

Public Types

using key_map_type = map<public_key_type,uint16_t>
 

Public Member Functions

 yubihsm_wallet_impl (const string &ep, const uint16_t ak)
 
 ~yubihsm_wallet_impl ()
 
bool is_locked () const
 
key_map_type::iterator populate_key_map_with_keyid (const uint16_t key_id)
 
void unlock (const string &password)
 
void lock ()
 
void prime_keepalive_timer ()
 
std::optional< signature_typetry_sign_digest (const digest_type d, const public_key_type public_key)
 
public_key_type create ()
 

Public Attributes

yh_connectorconnector = nullptr
 
yh_sessionsession = nullptr
 
string endpoint
 
uint16_t authkey
 
map< public_key_type, uint16_t_keys
 
yh_capabilities authkey_caps
 
uint16_t authkey_domains
 
boost::asio::steady_timer keepalive_timer {appbase::app().get_io_service()}
 
fc::ec_key key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)
 

Detailed Description

Definition at line 20 of file yubihsm_wallet.cpp.

Member Typedef Documentation

◆ key_map_type

Constructor & Destructor Documentation

◆ yubihsm_wallet_impl()

sysio::wallet::detail::yubihsm_wallet_impl::yubihsm_wallet_impl ( const string & ep,
const uint16_t ak )
inline

Definition at line 23 of file yubihsm_wallet.cpp.

23 : endpoint(ep), authkey(ak) {
24 yh_rc rc;
25 if((rc = yh_init()))
26 FC_THROW("yubihsm init failure: ${c}", ("c", yh_strerror(rc)));
27 }
#define FC_THROW( ...)
const char * yh_strerror(yh_rc err)
Definition error.c:65
yh_rc yh_init(void)
Definition yubihsm.c:3857
yh_rc
Definition yubihsm.h:170
yh_rc rc
Here is the call graph for this function:

◆ ~yubihsm_wallet_impl()

sysio::wallet::detail::yubihsm_wallet_impl::~yubihsm_wallet_impl ( )
inline

XXX Probably a race condition on timer shutdown and appbase destruction

Definition at line 29 of file yubihsm_wallet.cpp.

29 {
30 lock();
31 yh_exit();
32 //bizarre, is there no way to destroy a yh_connector??
33
35 }
yh_rc yh_exit(void)
Definition yubihsm.c:3910
Here is the call graph for this function:

Member Function Documentation

◆ create()

public_key_type sysio::wallet::detail::yubihsm_wallet_impl::create ( )
inline

Definition at line 174 of file yubihsm_wallet.cpp.

174 {
175 if(!yh_check_capability(&authkey_caps, "generate-asymmetric-key"))
176 FC_THROW_EXCEPTION(chain::wallet_exception, "Given authkey cannot create keys");
177
178 yh_rc rc;
179 uint16_t new_key_id = 0;
180 yh_capabilities creation_caps = {};
181 if(yh_string_to_capabilities("sign-ecdsa:export-wrapped", &creation_caps))
182 FC_THROW_EXCEPTION(chain::wallet_exception, "Cannot create caps mask");
183
184 try {
185 if((rc = yh_util_generate_ec_key(session, &new_key_id, "kiod created key", authkey_domains, &creation_caps, YH_ALGO_EC_P256)))
186 FC_THROW_EXCEPTION(chain::wallet_exception, "yh_util_generate_ec_key failed: ${m}", ("m", yh_strerror(rc)));
187 return populate_key_map_with_keyid(new_key_id)->first;
188 }
189 catch(chain::wallet_exception& e) {
190 lock();
191 throw;
192 }
193 }
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
unsigned short uint16_t
Definition stdint.h:125
key_map_type::iterator populate_key_map_with_keyid(const uint16_t key_id)
Capabilities representation.
Definition yubihsm.h:162
yh_rc yh_util_generate_ec_key(yh_session *session, uint16_t *key_id, const char *label, uint16_t domains, const yh_capabilities *capabilities, yh_algorithm algorithm)
Definition yubihsm.c:1913
yh_rc yh_string_to_capabilities(const char *capability, yh_capabilities *result)
Definition yubihsm.c:4115
bool yh_check_capability(const yh_capabilities *capabilities, const char *capability)
Definition yubihsm.c:4198
@ YH_ALGO_EC_P256
ecp256
Definition yubihsm.h:414
Here is the call graph for this function:

◆ is_locked()

bool sysio::wallet::detail::yubihsm_wallet_impl::is_locked ( ) const
inline

Definition at line 37 of file yubihsm_wallet.cpp.

37 {
38 return !connector;
39 }

◆ lock()

void sysio::wallet::detail::yubihsm_wallet_impl::lock ( )
inline

Definition at line 104 of file yubihsm_wallet.cpp.

104 {
105 if(session) {
108 }
109 session = nullptr;
110 if(connector)
112 //it would seem like this would leak-- there is no destroy() call for it. But I clearly can't reuse connectors
113 // as that fails with a "Unable to find a suitable connector"
114 connector = nullptr;
115
116 _keys.clear();
117 keepalive_timer.cancel();
118 }
map< public_key_type, uint16_t > _keys
yh_rc yh_destroy_session(yh_session **session)
Definition yubihsm.c:890
yh_rc yh_util_close_session(yh_session *session)
Definition yubihsm.c:1257
yh_rc yh_disconnect(yh_connector *connector)
Definition yubihsm.c:4097
Here is the call graph for this function:
Here is the caller graph for this function:

◆ populate_key_map_with_keyid()

key_map_type::iterator sysio::wallet::detail::yubihsm_wallet_impl::populate_key_map_with_keyid ( const uint16_t key_id)
inline

XXX This is junky and common with SE wallet; commonize it

Definition at line 41 of file yubihsm_wallet.cpp.

41 {
42 yh_rc rc;
43 size_t blob_sz = 128;
44 uint8_t blob[blob_sz];
45 if((rc = yh_util_get_public_key(session, key_id, blob, &blob_sz, nullptr)))
46 FC_THROW_EXCEPTION(chain::wallet_exception, "yh_util_get_public_key failed: ${m}", ("m", yh_strerror(rc)));
47 if(blob_sz != 64)
48 FC_THROW_EXCEPTION(chain::wallet_exception, "unexpected pubkey size from yh_util_get_public_key");
49
51 char serialized_pub_key[sizeof(public_key_data) + 1];
52 serialized_pub_key[0] = 0x01; //means R1 key
53 serialized_pub_key[1] = 0x02 + (blob[63]&1); //R1 header; even or odd Y
54 memcpy(serialized_pub_key+2, blob, 32); //copy in the 32 bytes of X
55
56 public_key_type pub_key;
57 fc::datastream<const char *> ds(serialized_pub_key, sizeof(serialized_pub_key));
58 fc::raw::unpack(ds, pub_key);
59
60 return _keys.emplace(pub_key, key_id).first;
61 }
static const Segment ds(Segment::ds)
fc::array< char, 33 > public_key_data
void unpack(Stream &s, std::deque< T > &value)
Definition raw.hpp:540
unsigned char uint8_t
Definition stdint.h:124
yh_rc yh_util_get_public_key(yh_session *session, uint16_t id, uint8_t *data, size_t *data_len, yh_algorithm *algorithm)
Definition yubihsm.c:1216
uint16_t key_id
memcpy((char *) pInfo->slotDescription, s, l)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ prime_keepalive_timer()

void sysio::wallet::detail::yubihsm_wallet_impl::prime_keepalive_timer ( )
inline

Definition at line 120 of file yubihsm_wallet.cpp.

120 {
121 keepalive_timer.expires_at(std::chrono::steady_clock::now() + std::chrono::seconds(20));
122 keepalive_timer.async_wait([this](const boost::system::error_code& ec){
123 if(ec || !session)
124 return;
125
126 uint8_t data, resp;
127 yh_cmd resp_cmd;
128 size_t resp_sz = 1;
129 if(yh_send_secure_msg(session, YHC_ECHO, &data, 1, &resp_cmd, &resp, &resp_sz))
130 lock();
131 else
133 });
134 }
yh_rc yh_send_secure_msg(yh_session *session, yh_cmd cmd, const uint8_t *data, size_t data_len, yh_cmd *response_cmd, uint8_t *response, size_t *response_len)
Definition yubihsm.c:416
yh_cmd
Definition yubihsm.h:243
Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_sign_digest()

std::optional< signature_type > sysio::wallet::detail::yubihsm_wallet_impl::try_sign_digest ( const digest_type d,
const public_key_type public_key )
inline

XXX a lot of this below is similar to SE wallet; commonize it in non-junky way

Definition at line 136 of file yubihsm_wallet.cpp.

136 {
137 auto it = _keys.find(public_key);
138 if(it == _keys.end())
139 return std::optional<signature_type>();
140
141 size_t der_sig_sz = 128;
142 uint8_t der_sig[der_sig_sz];
143 yh_rc rc;
144 if((rc = yh_util_sign_ecdsa(session, it->second, (uint8_t*)d.data(), d.data_size(), der_sig, &der_sig_sz))) {
145 lock();
146 FC_THROW_EXCEPTION(chain::wallet_exception, "yh_util_sign_ecdsa failed: ${m}", ("m", yh_strerror(rc)));
147 }
148
150 fc::ecdsa_sig sig = ECDSA_SIG_new();
151 BIGNUM *r = BN_new(), *s = BN_new();
152 BN_bin2bn(der_sig+4, der_sig[3], r);
153 BN_bin2bn(der_sig+6+der_sig[3], der_sig[4+der_sig[3]+1], s);
154 ECDSA_SIG_set0(sig, r, s);
155
156 char pub_key_shim_data[64];
157 fc::datastream<char *> eds(pub_key_shim_data, sizeof(pub_key_shim_data));
158 fc::raw::pack(eds, it->first);
159 public_key_data* kd = (public_key_data*)(pub_key_shim_data+1);
160
161 compact_signature compact_sig;
162 compact_sig = signature_from_ecdsa(key, *kd, sig, d);
163
164 char serialized_signature[sizeof(compact_sig) + 1];
165 serialized_signature[0] = 0x01;
166 memcpy(serialized_signature+1, compact_sig.data, sizeof(compact_sig));
167
168 signature_type final_signature;
169 fc::datastream<const char *> ds(serialized_signature, sizeof(serialized_signature));
170 fc::raw::unpack(ds, final_signature);
171 return final_signature;
172 }
const mie::Vuint & r
Definition bn.cpp:28
bignum_st BIGNUM
Definition bigint.hpp:7
compact_signature signature_from_ecdsa(const EC_KEY *key, const public_key_data &pub, fc::ecdsa_sig &sig, const fc::sha256 &d)
fc::array< unsigned char, 65 > compact_signature
void pack(Stream &s, const std::deque< T > &value)
Definition raw.hpp:531
int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
yh_rc yh_util_sign_ecdsa(yh_session *session, uint16_t key_id, const uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len)
Definition yubihsm.c:1411
CK_ULONG d
char * s
Here is the call graph for this function:

◆ unlock()

void sysio::wallet::detail::yubihsm_wallet_impl::unlock ( const string & password)
inline

Definition at line 63 of file yubihsm_wallet.cpp.

63 {
64 yh_rc rc;
65
66 try {
67 if((rc = yh_init_connector(endpoint.c_str(), &connector)))
68 FC_THROW_EXCEPTION(chain::wallet_exception, "Failled to initialize yubihsm connector URL: ${c}", ("c", yh_strerror(rc)));
69 if((rc = yh_connect(connector, 0)))
70 FC_THROW_EXCEPTION(chain::wallet_exception, "Failed to connect to YubiHSM connector: ${m}", ("m", yh_strerror(rc)));
71 if((rc = yh_create_session_derived(connector, authkey, (const uint8_t *)password.data(), password.size(), false, &session)))
72 FC_THROW_EXCEPTION(chain::wallet_exception, "Failed to create YubiHSM session: ${m}", ("m", yh_strerror(rc)));
74 FC_THROW_EXCEPTION(chain::wallet_exception, "Failed to authenticate YubiHSM session: ${m}", ("m", yh_strerror(rc)));
75
76 yh_object_descriptor authkey_desc;
78 FC_THROW_EXCEPTION(chain::wallet_exception, "Failed to get authkey info: ${m}", ("m", yh_strerror(rc)));
79
80 authkey_caps = authkey_desc.capabilities;
81 authkey_domains = authkey_desc.domains;
82
83 if(!yh_check_capability(&authkey_caps, "sign-ecdsa"))
84 FC_THROW_EXCEPTION(chain::wallet_exception, "Given authkey cannot perform signing");
85
86 size_t found_objects_n = 64*1024;
87 yh_object_descriptor found_objs[found_objects_n];
88 yh_capabilities find_caps;
89 yh_string_to_capabilities("sign-ecdsa", &find_caps);
90 if((rc = yh_util_list_objects(session, 0, YH_ASYMMETRIC_KEY, 0, &find_caps, YH_ALGO_EC_P256, nullptr, found_objs, &found_objects_n)))
91 FC_THROW_EXCEPTION(chain::wallet_exception, "yh_util_list_objects failed: ${m}", ("m", yh_strerror(rc)));
92
93 for(size_t i = 0; i < found_objects_n; ++i)
94 populate_key_map_with_keyid(found_objs[i].id);
95 }
96 catch(chain::wallet_exception& e) {
97 lock();
98 throw;
99 }
100
102 }
uint16_t domains
Object domains.
Definition yubihsm.h:548
yh_capabilities capabilities
Object capabilities.
Definition yubihsm.h:542
yh_rc yh_create_session_derived(yh_connector *connector, uint16_t authkey_id, const uint8_t *password, size_t password_len, bool recreate, yh_session **session)
Definition yubihsm.c:593
yh_rc yh_authenticate_session(yh_session *session)
Definition yubihsm.c:2927
yh_rc yh_util_get_object_info(yh_session *session, uint16_t id, yh_object_type type, yh_object_descriptor *object)
Definition yubihsm.c:1128
yh_rc yh_util_list_objects(yh_session *session, uint16_t id, yh_object_type type, uint16_t domains, const yh_capabilities *capabilities, yh_algorithm algorithm, const char *label, yh_object_descriptor *objects, size_t *n_objects)
Definition yubihsm.c:1030
yh_rc yh_init_connector(const char *url, yh_connector **connector)
Definition yubihsm.c:4024
yh_rc yh_connect(yh_connector *connector, int timeout)
Definition yubihsm.c:4079
@ YH_ASYMMETRIC_KEY
Asymmetric Key is the private key of an asymmetric key-pair.
Definition yubihsm.h:366
@ YH_AUTHENTICATION_KEY
Authentication Key is used to establish Sessions with a device.
Definition yubihsm.h:364
Here is the call graph for this function:

Member Data Documentation

◆ _keys

map<public_key_type,uint16_t> sysio::wallet::detail::yubihsm_wallet_impl::_keys

Definition at line 200 of file yubihsm_wallet.cpp.

◆ authkey

uint16_t sysio::wallet::detail::yubihsm_wallet_impl::authkey

Definition at line 198 of file yubihsm_wallet.cpp.

◆ authkey_caps

yh_capabilities sysio::wallet::detail::yubihsm_wallet_impl::authkey_caps

Definition at line 202 of file yubihsm_wallet.cpp.

◆ authkey_domains

uint16_t sysio::wallet::detail::yubihsm_wallet_impl::authkey_domains

Definition at line 203 of file yubihsm_wallet.cpp.

◆ connector

yh_connector* sysio::wallet::detail::yubihsm_wallet_impl::connector = nullptr

Definition at line 195 of file yubihsm_wallet.cpp.

◆ endpoint

string sysio::wallet::detail::yubihsm_wallet_impl::endpoint

Definition at line 197 of file yubihsm_wallet.cpp.

◆ keepalive_timer

boost::asio::steady_timer sysio::wallet::detail::yubihsm_wallet_impl::keepalive_timer {appbase::app().get_io_service()}

Definition at line 205 of file yubihsm_wallet.cpp.

boost::asio::io_service & get_io_service()
application & app()

◆ key

fc::ec_key sysio::wallet::detail::yubihsm_wallet_impl::key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)

Definition at line 206 of file yubihsm_wallet.cpp.

◆ session

yh_session* sysio::wallet::detail::yubihsm_wallet_impl::session = nullptr

Definition at line 196 of file yubihsm_wallet.cpp.


The documentation for this struct was generated from the following file: