9#include <boost/range/adaptor/map.hpp>
10#include <boost/range/algorithm/copy.hpp>
11#include <boost/asio/posix/stream_descriptor.hpp>
12#include <boost/dll/runtime_symbol_info.hpp>
14namespace sysio {
namespace wallet {
48 FC_THROW_EXCEPTION(chain::wallet_exception,
"unexpected pubkey size from yh_util_get_public_key");
52 serialized_pub_key[0] = 0x01;
53 serialized_pub_key[1] = 0x02 + (
blob[63]&1);
63 void unlock(
const string& password) {
86 size_t found_objects_n = 64*1024;
93 for(
size_t i = 0; i < found_objects_n; ++i)
96 catch(chain::wallet_exception& e) {
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){
138 if(it ==
_keys.end())
139 return std::optional<signature_type>();
141 size_t der_sig_sz = 128;
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);
156 char pub_key_shim_data[64];
164 char serialized_signature[
sizeof(compact_sig) + 1];
165 serialized_signature[0] = 0x01;
166 memcpy(serialized_signature+1, compact_sig.
data,
sizeof(compact_sig));
171 return final_signature;
189 catch(chain::wallet_exception& e) {
200 map<public_key_type,uint16_t>
_keys;
206 fc::ec_key key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
219 FC_THROW_EXCEPTION(chain::wallet_exception,
"Obtaining private key for a key stored in YubiHSM is impossible");
223 return my->is_locked();
231 my->unlock(password);
237 FC_THROW_EXCEPTION(chain::wallet_exception,
"YubiHSM wallet cannot have a password set");
241 FC_THROW_EXCEPTION(chain::wallet_exception,
"Getting the private keys from the YubiHSM wallet is impossible");
244 flat_set<public_key_type> keys;
245 boost::copy(my->_keys | boost::adaptors::map_keys, std::inserter(keys, keys.end()));
250 FC_THROW_EXCEPTION(chain::wallet_exception,
"It is not possible to import a key in to the YubiHSM wallet");
254 SYS_ASSERT(
key_type.empty() ||
key_type ==
"R1", chain::unsupported_key_type_exception,
"YubiHSM wallet only supports R1 keys");
255 return my->create().to_string();
260 FC_THROW_EXCEPTION(chain::wallet_exception,
"YubiHSM wallet does not currently support removal of keys");
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
boost::asio::io_service & get_io_service()
contains only the public point of an elliptic curve key.
bool remove_key(string key) override
void check_password(string password) override
map< public_key_type, private_key_type > list_keys() override
void unlock(string password) override
void set_password(string password) override
yubihsm_wallet(const string &connector, const uint16_t authkey)
std::optional< signature_type > try_sign_digest(const digest_type digest, const public_key_type public_key) override
flat_set< public_key_type > list_public_keys() override
bool import_key(string wif_key) override
bool is_locked() const override
string create_key(string key_type) override
private_key_type get_private_key(public_key_type pubkey) const override
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
const char * yh_strerror(yh_rc err)
fc::array< char, 33 > public_key_data
compact_signature signature_from_ecdsa(const EC_KEY *key, const public_key_data &pub, fc::ecdsa_sig &sig, const fc::sha256 &d)
void unpack(Stream &s, std::deque< T > &value)
void pack(Stream &s, const std::deque< T > &value)
fc::sha256 digest(const T &value)
int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
yh_capabilities authkey_caps
void unlock(const string &password)
boost::asio::steady_timer keepalive_timer
void prime_keepalive_timer()
map< public_key_type, uint16_t > key_map_type
std::optional< signature_type > try_sign_digest(const digest_type d, const public_key_type public_key)
map< public_key_type, uint16_t > _keys
yubihsm_wallet_impl(const string &ep, const uint16_t ak)
key_map_type::iterator populate_key_map_with_keyid(const uint16_t key_id)
Capabilities representation.
uint16_t domains
Object domains.
yh_capabilities capabilities
Object capabilities.
yh_rc yh_destroy_session(yh_session **session)
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)
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)
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)
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)
yh_rc yh_util_close_session(yh_session *session)
yh_rc yh_authenticate_session(yh_session *session)
yh_rc yh_util_get_object_info(yh_session *session, uint16_t id, yh_object_type type, yh_object_descriptor *object)
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)
yh_rc yh_init_connector(const char *url, yh_connector **connector)
yh_rc yh_connect(yh_connector *connector, int timeout)
yh_rc yh_util_get_public_key(yh_session *session, uint16_t id, uint8_t *data, size_t *data_len, yh_algorithm *algorithm)
yh_rc yh_string_to_capabilities(const char *capability, yh_capabilities *result)
yh_rc yh_disconnect(yh_connector *connector)
bool yh_check_capability(const yh_capabilities *capabilities, const char *capability)
@ YH_ASYMMETRIC_KEY
Asymmetric Key is the private key of an asymmetric key-pair.
@ YH_AUTHENTICATION_KEY
Authentication Key is used to establish Sessions with a device.
memcpy((char *) pInfo->slotDescription, s, l)