41 {
45
47
48 const char *connector_url;
49
50 connector_url = getenv("DEFAULT_CONNECTOR_URL");
51 if (connector_url == NULL) {
53 }
54
57
60
63
65 sizeof(password),
false, &
session);
67
70
74
75 printf(
"Successfully established session %02d\n", session_id);
76
80
84
89
91
93 size_t public_key_len = sizeof(public_key);
97
98 printf(
"Public key (%zd bytes) is:", public_key_len);
99 for (size_t i = 0; i < public_key_len; i++) {
100 printf(
" %02x", public_key[i]);
101 }
103
104 EC_KEY *eckey = EC_KEY_new();
107 EC_GROUP *group = EC_GROUP_new_by_curve_name(nid);
108
109 EC_GROUP_set_asn1_flag(group, nid);
110 EC_KEY_set_group(eckey, group);
111 point = EC_POINT_new(group);
112
113 memmove(public_key + 1, public_key, public_key_len);
114 public_key[0] = 0x04;
115 public_key_len++;
116
117 EC_POINT_oct2point(group,
point, public_key, public_key_len, NULL);
118
119 EC_KEY_set_public_key(eckey,
point);
120
121
122 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
123 assert(pctx != NULL);
124
125
126 assert(EVP_PKEY_paramgen_init(pctx) == 1);
127
128
129 assert(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_X9_62_prime256v1) ==
130 1);
131
132
134 assert(EVP_PKEY_paramgen(pctx, &
params) == 1);
135
136
137 EVP_PKEY_CTX *kctx = EVP_PKEY_CTX_new(
params, NULL);
138 assert(kctx != NULL);
139
140
141 EVP_PKEY *pkey = NULL;
142 assert(EVP_PKEY_keygen_init(kctx) == 1);
143 assert(EVP_PKEY_keygen(kctx, &pkey) == 1);
144
145
146 EVP_PKEY *peerkey = EVP_PKEY_new();
147 assert(peerkey != NULL);
148 assert(EVP_PKEY_set1_EC_KEY(peerkey, eckey) == 1);
149
150
151 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pkey, NULL);
152 assert(ctx != NULL);
153
154
155 assert(EVP_PKEY_derive_init(ctx) == 1);
156
157
158 assert(EVP_PKEY_derive_set_peer(ctx, peerkey) == 1);
159
161 size_t secret_len = sizeof(secret_len);
162
163
164 assert(EVP_PKEY_derive(ctx, NULL, &secret_len));
165
166
167 assert(EVP_PKEY_derive(ctx, secret, &secret_len) == 1);
168
169 EC_KEY *eckey2 = EVP_PKEY_get1_EC_KEY(pkey);
170 const EC_POINT *
pub = EC_KEY_get0_public_key(eckey2);
171
173 size_t pubkey_len =
sizeof(
pubkey);
174
175 pubkey_len = EC_POINT_point2oct(group, pub, POINT_CONVERSION_UNCOMPRESSED,
176 pubkey, pubkey_len, NULL);
177 assert(pubkey_len == 65);
178
180 size_t computed_secret_len = sizeof(computed_secret);
182 computed_secret, &computed_secret_len);
184
185 assert(computed_secret_len == secret_len);
186 assert(memcmp(secret, computed_secret, computed_secret_len) == 0);
187
188 printf(
"Secrets match\n");
189
190 EVP_PKEY_CTX_free(ctx);
191 EVP_PKEY_free(peerkey);
192 EVP_PKEY_free(pkey);
193 EVP_PKEY_CTX_free(kctx);
195 EVP_PKEY_CTX_free(pctx);
196 EC_POINT_free(
point);
197 EC_KEY_free(eckey);
198 EC_KEY_free(eckey2);
199 EC_GROUP_free(group);
200
203
206
209
212
213 return 0;
214}
#define DEFAULT_CONNECTOR_URL
CK_SESSION_HANDLE session
LOGGING_API void printf(Category category, const char *format,...)
Capabilities representation.
account_query_db::get_accounts_by_authorizers_params params
int algo2nid(yh_algorithm algo)
yh_rc yh_destroy_session(yh_session **session)
yh_rc yh_util_derive_ecdh(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_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_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_string_to_capabilities(const char *capability, yh_capabilities *result)
yh_rc yh_disconnect(yh_connector *connector)
yh_rc yh_get_session_id(yh_session *session, uint8_t *sid)
@ YHR_GENERIC_ERROR
Return value when encountering an unknown error.
@ YHR_SUCCESS
Returned value when function was successful.
yh_capabilities capabilities