Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
wrap.c File Reference
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <yubihsm.h>
Include dependency graph for wrap.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 * key_label = "label"
 
const uint8_t password [] = "password"
 

Macro Definition Documentation

◆ DEFAULT_CONNECTOR_URL

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

Definition at line 29 of file wrap.c.

Function Documentation

◆ main()

int main ( void )

Definition at line 35 of file wrap.c.

35 {
36 yh_connector *connector = NULL;
37 yh_session *session = NULL;
39
40 uint16_t authkey = 1;
41
42 const char *connector_url;
43
44 connector_url = getenv("DEFAULT_CONNECTOR_URL");
45 if (connector_url == NULL) {
46 connector_url = DEFAULT_CONNECTOR_URL;
47 }
48
49 yrc = yh_init();
50 assert(yrc == YHR_SUCCESS);
51
52 yrc = yh_init_connector(connector_url, &connector);
53 assert(yrc == YHR_SUCCESS);
54
55 yrc = yh_connect(connector, 0);
56 assert(yrc == YHR_SUCCESS);
57
58 yrc = yh_create_session_derived(connector, authkey, password,
59 sizeof(password), false, &session);
60 assert(yrc == YHR_SUCCESS);
61
63 assert(yrc == YHR_SUCCESS);
64
65 uint8_t session_id;
66 yrc = yh_get_session_id(session, &session_id);
67 assert(yrc == YHR_SUCCESS);
68
69 printf("Successfully established session %02d\n", session_id);
70
72 yrc =
73 yh_string_to_capabilities("export-wrapped:import-wrapped", &capabilities);
74 assert(yrc == YHR_SUCCESS);
75
77 yrc = yh_string_to_capabilities("sign-ecdsa:exportable-under-wrap",
78 &delegated_capabilities); // delegated
79 // capabilities has
80 // to match the
81 // capabilities of
82 // the object we
83 // want to export
84 assert(yrc == YHR_SUCCESS);
85
86 uint16_t domain_five = 0;
87 yrc = yh_string_to_domains("5", &domain_five);
88 assert(yrc == YHR_SUCCESS);
89
90 uint16_t wrapping_key_id = 0; // ID 0 lets the device generate an ID
91 yrc =
92 yh_util_generate_wrap_key(session, &wrapping_key_id, key_label, domain_five,
95 assert(yrc == YHR_SUCCESS);
96
97 printf("Generated wrapping key with ID %04x\n", wrapping_key_id);
98
100 yrc = yh_string_to_capabilities("sign-ecdsa:exportable-under-wrap",
101 &capabilities);
102 assert(yrc == YHR_SUCCESS);
103
104 uint16_t key_id_before = 0; // ID 0 lets the device generate an ID
105 yrc = yh_util_generate_ec_key(session, &key_id_before, key_label, domain_five,
107 assert(yrc == YHR_SUCCESS);
108
109 printf("Generated ec key with ID %04x\n", key_id_before);
110
111 uint8_t public_key_before[512];
112 size_t public_key_before_len = sizeof(public_key_before);
113 yrc = yh_util_get_public_key(session, key_id_before, public_key_before,
114 &public_key_before_len, NULL);
115 assert(yrc == YHR_SUCCESS);
116
117 printf("Public key before (%zu bytes) is:", public_key_before_len);
118 for (unsigned int i = 0; i < public_key_before_len; i++) {
119 printf(" %02x", public_key_before[i]);
120 }
121 printf("\n");
122
123 uint8_t wrapped_object[512];
124 size_t wrapped_object_len = sizeof(wrapped_object);
125 yh_object_type object_type_after;
126 yrc =
128 key_id_before, wrapped_object, &wrapped_object_len);
129 assert(yrc == YHR_SUCCESS);
130
131 printf("Wrapped object (%zu bytes) is:", wrapped_object_len);
132 for (unsigned int i = 0; i < wrapped_object_len; i++) {
133 printf(" %02x", wrapped_object[i]);
134 }
135 printf("\n");
136
138 assert(yrc == YHR_SUCCESS);
139
140 printf("Successfully deleted ec key with ID %04x\n", key_id_before);
141
142 uint8_t public_key_after[512];
143 size_t public_key_after_len = sizeof(public_key_after);
144 yrc = yh_util_get_public_key(session, key_id_before, public_key_after,
145 &public_key_after_len, NULL);
147
148 printf("Unable to get public key for ec key with ID %04x\n", key_id_before);
149
150 uint16_t key_id_after;
151 yrc = yh_util_import_wrapped(session, wrapping_key_id, wrapped_object,
152 wrapped_object_len, &object_type_after,
153 &key_id_after);
154 assert(yrc == YHR_SUCCESS);
155
156 printf("Successfully imported wrapped object with ID %04x\n", key_id_after);
157
158 if (object_type_after != YH_ASYMMETRIC_KEY) {
159 printf("Unexpected odbject type\n");
160 exit(EXIT_FAILURE);
161 }
162
163 if (key_id_before != key_id_after) {
164 printf("ID %04x and %04x do not match\n", key_id_before, key_id_after);
165 exit(EXIT_FAILURE);
166 } else {
167 printf("ID %04x and %04x match\n", key_id_before, key_id_after);
168 }
169
170 yrc = yh_util_get_public_key(session, key_id_after, public_key_after,
171 &public_key_after_len, NULL);
172 assert(yrc == YHR_SUCCESS);
173
174 printf("Public key after (%zu bytes) is:", public_key_after_len);
175 for (unsigned int i = 0; i < public_key_after_len; i++) {
176 printf(" %02x", public_key_after[i]);
177 }
178 printf("\n");
179
180 if (public_key_before_len != public_key_after_len ||
181 memcmp(public_key_before, public_key_after, public_key_before_len) != 0) {
182 printf("Public key before and after do not match\n");
183 exit(EXIT_FAILURE);
184 } else {
185 printf("Public key before and after match\n");
186 }
187
189
190 yrc =
191 yh_util_get_object_info(session, key_id_after, YH_ASYMMETRIC_KEY, &object);
192 assert(yrc == YHR_SUCCESS);
193
195 assert(yrc == YHR_SUCCESS);
196
198 assert(yrc == YHR_SUCCESS);
199
200 yh_disconnect(connector);
201 assert(yrc == YHR_SUCCESS);
202
203 yrc = yh_exit();
204 assert(yrc == YHR_SUCCESS);
205
206 return 0;
207}
CK_SESSION_HANDLE session
LOGGING_API void printf(Category category, const char *format,...)
Definition Logging.cpp:30
unsigned short uint16_t
Definition stdint.h:125
unsigned char uint8_t
Definition stdint.h:124
Capabilities representation.
Definition yubihsm.h:162
uint8_t capabilities[YH_CAPABILITIES_LEN]
Capabilities is represented as an 8 byte uint8_t array.
Definition yubihsm.h:164
#define DEFAULT_CONNECTOR_URL
Definition wrap.c:29
const char * key_label
Definition wrap.c:32
yh_rc yh_destroy_session(yh_session **session)
Definition yubihsm.c:890
yh_rc yh_util_generate_wrap_key(yh_session *session, uint16_t *key_id, const char *label, uint16_t domains, const yh_capabilities *capabilities, yh_algorithm algorithm, const yh_capabilities *delegated_capabilities)
Definition yubihsm.c:2458
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_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_get_object_info(yh_session *session, uint16_t id, yh_object_type type, yh_object_descriptor *object)
Definition yubihsm.c:1128
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_export_wrapped(yh_session *session, uint16_t wrapping_key_id, yh_object_type target_type, uint16_t target_id, uint8_t *out, size_t *out_len)
Definition yubihsm.c:2265
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_disconnect(yh_connector *connector)
Definition yubihsm.c:4097
yh_rc yh_util_import_wrapped(yh_session *session, uint16_t wrapping_key_id, const uint8_t *in, size_t in_len, yh_object_type *target_type, uint16_t *target_id)
Definition yubihsm.c:2309
yh_rc yh_util_delete_object(yh_session *session, uint16_t id, yh_object_type type)
Definition yubihsm.c:2222
yh_rc yh_get_session_id(yh_session *session, uint8_t *sid)
Definition yubihsm.c:2915
yh_object_type
Definition yubihsm.h:359
@ YH_ASYMMETRIC_KEY
Asymmetric Key is the private key of an asymmetric key-pair.
Definition yubihsm.h:366
@ YH_ALGO_AES256_CCM_WRAP
aes256-ccm-wrap
Definition yubihsm.h:474
@ YH_ALGO_EC_P256
ecp256
Definition yubihsm.h:414
#define YH_CAPABILITIES_LEN
Length of capabilities array.
Definition yubihsm.h:119
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
@ YHR_DEVICE_OBJECT_NOT_FOUND
Return value when the object not found on the device.
Definition yubihsm.h:218
yh_capabilities capabilities
yh_object_descriptor object
yh_rc yrc
memset(pInfo->slotDescription, ' ', 64)
yh_capabilities delegated_capabilities
Here is the call graph for this function:

Variable Documentation

◆ key_label

const char* key_label = "label"

Definition at line 32 of file wrap.c.

◆ password

const uint8_t password[] = "password"

Definition at line 33 of file wrap.c.