27#include <openssl/evp.h>
28#include <openssl/rsa.h>
36#ifndef DEFAULT_CONNECTOR_URL
37#define DEFAULT_CONNECTOR_URL "http://127.0.0.1:12345"
43const uint8_t data[] =
"sudo make me a sandwich";
45 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60,
46 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09};
55 const char *connector_url;
57 connector_url = getenv(
"DEFAULT_CONNECTOR_URL");
58 if (connector_url == NULL) {
72 sizeof(password),
false, &
session);
82 printf(
"Successfully established session %02d\n", session_id);
89 size_t key_material_len =
sizeof(
key);
90 if (!
read_file(fp, key, &key_material_len)) {
94 &key_material_len,
false);
109 key + (key_material_len / 2));
112 printf(
"Key imported with ID %04x\n",
key_id);
115 size_t public_key_len =
sizeof(public_key);
120 printf(
"Public key (%zu bytes) is:", public_key_len);
121 for (
unsigned int i = 0; i < public_key_len; i++) {
122 printf(
" %02x", public_key[i]);
126 BIGNUM *n = BN_bin2bn(public_key, public_key_len, NULL);
129 BIGNUM *e = BN_bin2bn((
const unsigned char *)
"\x01\x00\x01", 3, NULL);
132 RSA *rsa = RSA_new();
137 RSA_public_encrypt(
sizeof(data), data, encrypted, rsa, RSA_PKCS1_PADDING);
138 assert(
ret == RSA_size(rsa));
141 size_t decrypted_len =
sizeof(decrypted);
146 assert(memcmp(data, decrypted, decrypted_len) == 0);
148 printf(
"PKCS1v1.5 decrypted data matches\n");
150 ret = RSA_public_encrypt(
sizeof(data), data, encrypted, rsa,
151 RSA_PKCS1_OAEP_PADDING);
152 assert(
ret == RSA_size(rsa));
154 decrypted_len =
sizeof(decrypted);
161 assert(memcmp(data, decrypted, decrypted_len) == 0);
163 printf(
"OAEP decrypted data matches\n");
#define DEFAULT_CONNECTOR_URL
const uint8_t sha1_empty_string[]
const char rsa2048_pvtkey_file[]
CK_SESSION_HANDLE session
int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
Capabilities representation.
bool read_file(FILE *fp, uint8_t *buf, size_t *buf_len)
bool read_private_key(uint8_t *buf, size_t len, yh_algorithm *algo, uint8_t *bytes, size_t *bytes_len, bool internal_repr)
yh_rc yh_destroy_session(yh_session **session)
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_util_decrypt_pkcs1v1_5(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_string_to_domains(const char *domains, uint16_t *result)
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_util_decrypt_oaep(yh_session *session, uint16_t key_id, const uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len, const uint8_t *label, size_t label_len, yh_algorithm mgf1Algo)
yh_rc yh_string_to_capabilities(const char *capability, yh_capabilities *result)
yh_rc yh_util_import_rsa_key(yh_session *session, uint16_t *key_id, const char *label, uint16_t domains, const yh_capabilities *capabilities, yh_algorithm algorithm, const uint8_t *p, const uint8_t *q)
yh_rc yh_disconnect(yh_connector *connector)
yh_rc yh_get_session_id(yh_session *session, uint8_t *sid)
@ YH_ALGO_RSA_2048
rsa2048
@ YH_ALGO_MGF1_SHA1
mgf1-sha1
@ YHR_GENERIC_ERROR
Return value when encountering an unknown error.
@ YHR_SUCCESS
Returned value when function was successful.
yh_capabilities capabilities