Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
import_rsa.c File Reference
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include "util.h"
#include "openssl-compat.h"
#include <yubihsm.h>
Include dependency graph for import_rsa.c:

Go to the source code of this file.

Macros

#define DEFAULT_CONNECTOR_URL   "http://127.0.0.1:12345"
 

Functions

int main (void)
 

Variables

const char rsa2048_pvtkey_file [] = "rsa2048_pvtkey.pem"
 
const char * key_label = "label"
 
const uint8_t password [] = "password"
 
const uint8_t data [] = "sudo make me a sandwich"
 

Macro Definition Documentation

◆ DEFAULT_CONNECTOR_URL

#define DEFAULT_CONNECTOR_URL   "http://127.0.0.1:12345"

Definition at line 35 of file import_rsa.c.

Function Documentation

◆ main()

int main ( void )

Definition at line 43 of file import_rsa.c.

43 {
44 yh_connector *connector = NULL;
45 yh_session *session = NULL;
47
48 uint16_t authkey = 1;
49
50 const char *connector_url;
51
52 connector_url = getenv("DEFAULT_CONNECTOR_URL");
53 if (connector_url == NULL) {
54 connector_url = DEFAULT_CONNECTOR_URL;
55 }
56
57 yrc = yh_init();
58 assert(yrc == YHR_SUCCESS);
59
60 yrc = yh_init_connector(connector_url, &connector);
61 assert(yrc == YHR_SUCCESS);
62
63 yrc = yh_connect(connector, 0);
64 assert(yrc == YHR_SUCCESS);
65
66 yrc = yh_create_session_derived(connector, authkey, password,
67 sizeof(password), false, &session);
68 assert(yrc == YHR_SUCCESS);
69
71 assert(yrc == YHR_SUCCESS);
72
73 uint8_t session_id;
74 yrc = yh_get_session_id(session, &session_id);
75 assert(yrc == YHR_SUCCESS);
76
77 printf("Successfully established session %02d\n", session_id);
78
79 FILE *fp = fopen(rsa2048_pvtkey_file, "rb");
80 assert(fp != NULL);
81
83 uint8_t privkey[2048];
84 size_t key_material_len = sizeof(privkey);
85 if (!read_file(fp, privkey, &key_material_len)) {
86 assert(false);
87 }
88 bool ret = read_private_key(privkey, key_material_len, &algorithm, privkey,
89 &key_material_len, false);
90 assert(ret == true);
91 assert(algorithm == YH_ALGO_RSA_2048);
92
95 assert(yrc == YHR_SUCCESS);
96
97 uint16_t domain_five = 0;
98 yh_string_to_domains("5", &domain_five);
99 assert(yrc == YHR_SUCCESS);
100
101 uint16_t key_id = 0; // ID 0 lets the device generate an ID
103 &capabilities, algorithm, privkey,
104 privkey + (key_material_len / 2));
105 assert(yrc == YHR_SUCCESS);
106
107 printf("Key imported with ID %04x\n", key_id);
108
109 printf("Data to sign (%zu bytes) is: %s\n", sizeof(data) - 1, data);
110
111 EVP_MD_CTX *mdctx = NULL;
112 uint8_t hashed_data[32];
113 unsigned int hashed_data_len;
114
115 mdctx = EVP_MD_CTX_create();
116 assert(mdctx != NULL);
117 EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL);
118 EVP_DigestUpdate(mdctx, data, sizeof(data) - 1);
119 EVP_DigestFinal_ex(mdctx, hashed_data, &hashed_data_len);
120 EVP_MD_CTX_destroy(mdctx);
121
122 printf("Hash of data (%d bytes) is:", EVP_MD_size(EVP_sha256()));
123 for (unsigned int i = 0; i < hashed_data_len; i++) {
124 printf(" %02x", hashed_data[i]);
125 }
126 printf("\n");
127
128 uint8_t signature[512];
129 size_t signature_len = sizeof(signature);
130 yrc = yh_util_sign_pss(session, key_id, hashed_data, hashed_data_len,
131 signature, &signature_len, 32, YH_ALGO_MGF1_SHA256);
132 assert(yrc == YHR_SUCCESS);
133
134 printf("Signature (%zu bytes) is:", signature_len);
135 for (unsigned int i = 0; i < signature_len; i++) {
136 printf(" %02x", signature[i]);
137 }
138 printf("\n");
139
140 uint8_t public_key[512];
141 size_t public_key_len = sizeof(public_key);
142 yrc =
143 yh_util_get_public_key(session, key_id, public_key, &public_key_len, NULL);
144 assert(yrc == YHR_SUCCESS);
145
146 printf("Public key (%zu bytes) is:", public_key_len);
147 for (unsigned int i = 0; i < public_key_len; i++) {
148 printf(" %02x", public_key[i]);
149 }
150 printf("\n");
151
152 BIGNUM *n = BN_bin2bn(public_key, public_key_len, NULL);
153 assert(n != NULL);
154
155 BIGNUM *e = BN_bin2bn((const unsigned char *) "\x01\x00\x01", 3, NULL);
156 assert(e != NULL);
157
158 RSA *rsa = RSA_new();
159 assert(RSA_set0_key(rsa, n, e, NULL) != 0);
160
161 EVP_PKEY *key = EVP_PKEY_new();
162 assert(EVP_PKEY_assign_RSA(key, rsa) == 1);
163 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(key, NULL);
164 assert(ctx != NULL);
165 assert(EVP_PKEY_verify_init(ctx) == 1);
166 assert(EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()) == 1);
167 assert(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) == 1);
168 assert(EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, 32) == 1);
169 assert(EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, EVP_sha256()) == 1);
170
171 if (EVP_PKEY_verify(ctx, signature, signature_len, hashed_data,
172 hashed_data_len) == 1) {
173 printf("Signature successfully verified\n");
174 } else {
175 printf("Unable to verify signature\n");
176 }
177
178 EVP_PKEY_CTX_free(ctx);
179 EVP_PKEY_free(key);
180
182 assert(yrc == YHR_SUCCESS);
183
185 assert(yrc == YHR_SUCCESS);
186
187 yh_disconnect(connector);
188 assert(yrc == YHR_SUCCESS);
189
190 yrc = yh_exit();
191 assert(yrc == YHR_SUCCESS);
192
193 return 0;
194}
CK_SESSION_HANDLE session
#define DEFAULT_CONNECTOR_URL
Definition import_rsa.c:35
const char * key_label
Definition import_rsa.c:39
const char rsa2048_pvtkey_file[]
Definition import_rsa.c:38
bignum_st BIGNUM
Definition bigint.hpp:7
LOGGING_API void printf(Category category, const char *format,...)
Definition Logging.cpp:30
fc::array< char, 72 > signature
Definition elliptic.hpp:25
int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
unsigned short uint16_t
Definition stdint.h:125
unsigned char uint8_t
Definition stdint.h:124
Capabilities representation.
Definition yubihsm.h:162
bool read_file(FILE *fp, uint8_t *buf, size_t *buf_len)
Definition util.c:476
bool read_private_key(uint8_t *buf, size_t len, yh_algorithm *algo, uint8_t *bytes, size_t *bytes_len, bool internal_repr)
Definition util.c:116
yh_rc yh_destroy_session(yh_session **session)
Definition yubihsm.c:890
yh_rc yh_exit(void)
Definition yubihsm.c:3910
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_init(void)
Definition yubihsm.c:3857
yh_rc yh_util_close_session(yh_session *session)
Definition yubihsm.c:1257
yh_rc yh_authenticate_session(yh_session *session)
Definition yubihsm.c:2927
yh_rc yh_util_sign_pss(yh_session *session, uint16_t key_id, const uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len, size_t salt_len, yh_algorithm mgf1Algo)
Definition yubihsm.c:1346
yh_rc yh_string_to_domains(const char *domains, uint16_t *result)
Definition yubihsm.c:4535
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_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
yh_rc yh_string_to_capabilities(const char *capability, yh_capabilities *result)
Definition yubihsm.c:4115
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)
Definition yubihsm.c:1655
yh_rc yh_disconnect(yh_connector *connector)
Definition yubihsm.c:4097
yh_rc yh_get_session_id(yh_session *session, uint8_t *sid)
Definition yubihsm.c:2915
yh_algorithm
Definition yubihsm.h:390
@ YH_ALGO_RSA_2048
rsa2048
Definition yubihsm.h:408
@ YH_ALGO_MGF1_SHA256
mgf1-sha256
Definition yubihsm.h:456
yh_algorithm algorithm
Definition yubihsm.h:619
yh_rc
Definition yubihsm.h:170
@ YHR_GENERIC_ERROR
Return value when encountering an unknown error.
Definition yubihsm.h:228
@ YHR_SUCCESS
Returned value when function was successful.
Definition yubihsm.h:172
yh_capabilities capabilities
CK_RV ret
EVP_MD_CTX * mdctx
yh_rc yrc
uint16_t key_id
Here is the call graph for this function:

Variable Documentation

◆ data

const uint8_t data[] = "sudo make me a sandwich"

Definition at line 41 of file import_rsa.c.

◆ key_label

const char* key_label = "label"

Definition at line 39 of file import_rsa.c.

◆ password

const uint8_t password[] = "password"

Definition at line 40 of file import_rsa.c.

◆ rsa2048_pvtkey_file

const char rsa2048_pvtkey_file[] = "rsa2048_pvtkey.pem"

Definition at line 38 of file import_rsa.c.