Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
import_ec.c
Go to the documentation of this file.
1/*
2 * Copyright 2015-2018 Yubico AB
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifdef NDEBUG
18#undef NDEBUG
19#endif
20#include <assert.h>
21#include <stdbool.h>
22#include <stdio.h>
23#include <stdint.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include <openssl/ec.h>
28#include <openssl/ecdsa.h>
29#include <openssl/evp.h>
30
31#include "util.h"
32
33#include <yubihsm.h>
34
35#ifndef DEFAULT_CONNECTOR_URL
36#define DEFAULT_CONNECTOR_URL "http://127.0.0.1:12345"
37#endif
38
39const char p256_pvtkey_file[] = "p256_pvtkey.pem";
40const char *key_label = "label";
41const uint8_t password[] = "password";
42const uint8_t data[] = "sudo make me a sandwich";
43
44int main(void) {
45 yh_connector *connector = NULL;
46 yh_session *session = NULL;
48
49 uint16_t authkey = 1;
50
51 const char *connector_url;
52
53 connector_url = getenv("DEFAULT_CONNECTOR_URL");
54 if (connector_url == NULL) {
55 connector_url = DEFAULT_CONNECTOR_URL;
56 }
57
58 yrc = yh_init();
59 assert(yrc == YHR_SUCCESS);
60
61 yrc = yh_init_connector(connector_url, &connector);
62 assert(yrc == YHR_SUCCESS);
63
64 yrc = yh_connect(connector, 0);
65 assert(yrc == YHR_SUCCESS);
66
67 yrc = yh_create_session_derived(connector, authkey, password,
68 sizeof(password), false, &session);
69 assert(yrc == YHR_SUCCESS);
70
72 assert(yrc == YHR_SUCCESS);
73
74 uint8_t session_id;
75 yrc = yh_get_session_id(session, &session_id);
76 assert(yrc == YHR_SUCCESS);
77
78 printf("Successfully established session %02d\n", session_id);
79
80 FILE *fp = fopen(p256_pvtkey_file, "rb");
81 assert(fp != NULL);
82
84 uint8_t key[2048];
85 size_t key_material_len = sizeof(key);
86 if (!read_file(fp, key, &key_material_len)) {
87 assert(false);
88 }
89 bool ret = read_private_key(key, key_material_len, &algorithm, key,
90 &key_material_len, false);
91 assert(ret == true);
92 assert(algorithm == YH_ALGO_EC_P256);
93
96 assert(yrc == YHR_SUCCESS);
97
98 uint16_t domain_five = 0;
99 yrc = yh_string_to_domains("5", &domain_five);
100 assert(yrc == YHR_SUCCESS);
101
102 uint16_t key_id = 0; // ID 0 lets the device generate an ID
104 &capabilities, algorithm, key);
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[128];
129 size_t signature_len = sizeof(signature);
130 yrc = yh_util_sign_ecdsa(session, key_id, hashed_data, hashed_data_len,
131 signature, &signature_len);
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 EC_KEY *eckey = EC_KEY_new();
153 int nid = algo2nid(YH_ALGO_EC_P256);
154 EC_POINT *point;
155 EC_GROUP *group = EC_GROUP_new_by_curve_name(nid);
156
157 EC_GROUP_set_asn1_flag(group, nid);
158 EC_KEY_set_group(eckey, group);
159 point = EC_POINT_new(group);
160
161 memmove(public_key + 1, public_key, public_key_len);
162 public_key[0] = 0x04; // hack to make it a valid ec pubkey..
163 public_key_len++;
164
165 EC_POINT_oct2point(group, point, public_key, public_key_len, NULL);
166
167 EC_KEY_set_public_key(eckey, point);
168
169 if (ECDSA_verify(0, hashed_data, hashed_data_len, signature, signature_len,
170 eckey) == 1) {
171 printf("Signature successfully verified\n");
172 } else {
173 printf("Unable to verify signature\n");
174 }
175
176 EC_POINT_free(point);
177 EC_KEY_free(eckey);
178 EC_GROUP_free(group);
179
181 assert(yrc == YHR_SUCCESS);
182
184 assert(yrc == YHR_SUCCESS);
185
186 yh_disconnect(connector);
187 assert(yrc == YHR_SUCCESS);
188
189 yrc = yh_exit();
190 assert(yrc == YHR_SUCCESS);
191
192 return 0;
193}
CK_SESSION_HANDLE session
#define DEFAULT_CONNECTOR_URL
Definition import_ec.c:36
int main(void)
Definition import_ec.c:44
const char * key_label
Definition import_ec.c:40
const char p256_pvtkey_file[]
Definition import_ec.c:39
unsigned short uint16_t
Definition stdint.h:125
unsigned char uint8_t
Definition stdint.h:124
Capabilities representation.
Definition yubihsm.h:162
int algo2nid(yh_algorithm algo)
Definition util.c:335
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
uint8_t key[16]
Definition yubico_otp.c:41
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_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)
Definition yubihsm.c:1411
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_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_disconnect(yh_connector *connector)
Definition yubihsm.c:4097
yh_rc yh_util_import_ec_key(yh_session *session, uint16_t *key_id, const char *label, uint16_t domains, const yh_capabilities *capabilities, yh_algorithm algorithm, const uint8_t *s)
Definition yubihsm.c:1689
yh_rc yh_get_session_id(yh_session *session, uint8_t *sid)
Definition yubihsm.c:2915
yh_algorithm
Definition yubihsm.h:390
@ YH_ALGO_EC_P256
ecp256
Definition yubihsm.h:414
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