Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
change_authkey.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 <yubihsm.h>
28
29#ifndef DEFAULT_CONNECTOR_URL
30#define DEFAULT_CONNECTOR_URL "http://127.0.0.1:12345"
31#endif
32
33const char *key_label = "label";
34const uint8_t password1[] = "password";
35const uint8_t password2[] = "letmein!";
36const uint8_t password3[] = "PASSWORD";
37
38int main(void) {
39 yh_connector *connector = NULL;
40 yh_session *session = NULL;
42
43 uint16_t authkey = 1;
44
45 const char *connector_url;
46
47 connector_url = getenv("DEFAULT_CONNECTOR_URL");
48 if (connector_url == NULL) {
49 connector_url = DEFAULT_CONNECTOR_URL;
50 }
51
52 yrc = yh_init();
53 assert(yrc == YHR_SUCCESS);
54
55 yrc = yh_init_connector(connector_url, &connector);
56 assert(yrc == YHR_SUCCESS);
57
58 yrc = yh_connect(connector, 0);
59 assert(yrc == YHR_SUCCESS);
60
61 yrc = yh_create_session_derived(connector, authkey, password1,
62 sizeof(password1), false, &session);
63 assert(yrc == YHR_SUCCESS);
64
66 assert(yrc == YHR_SUCCESS);
67
68 uint8_t session_id;
69 yrc = yh_get_session_id(session, &session_id);
70 assert(yrc == YHR_SUCCESS);
71
72 printf("Successfully established session %02d using Authentication Key "
73 "%04x\n",
74 session_id, authkey);
75
77 yrc = yh_string_to_capabilities("change-authentication-key", &capabilities);
78 assert(yrc == YHR_SUCCESS);
79
80 uint16_t domain_five = 0;
81 yrc = yh_string_to_domains("5", &domain_five);
82 assert(yrc == YHR_SUCCESS);
83
84 uint16_t key_id = 0; // ID 0 lets the device generate an ID
86 domain_five, &capabilities,
88 sizeof(password2));
89 assert(yrc == YHR_SUCCESS);
90
91 printf("Imported Authentication Key with ID %04x and password \"%s\"\n",
93
95 sizeof(password3));
96 assert(yrc == YHR_DEVICE_INVALID_ID);
97
98 printf("Unable to change Authentication Key with ID %04x from this session\n",
99 key_id);
100
102 assert(yrc == YHR_SUCCESS);
103
105 assert(yrc == YHR_SUCCESS);
106
107 printf("Closed session %02d\n", session_id);
108
110 sizeof(password2), false, &session);
111 assert(yrc == YHR_SUCCESS);
112
114 assert(yrc == YHR_SUCCESS);
115
116 yrc = yh_get_session_id(session, &session_id);
117 assert(yrc == YHR_SUCCESS);
118
119 printf("Successfully established session %02d using Authentication Key "
120 "%04x\n",
121 session_id, key_id);
122
124 sizeof(password3));
125 assert(yrc == YHR_SUCCESS);
126
127 printf("Successfully changed Authentication Key with ID %04x to password "
128 "\"%s\"\n",
130
132 assert(yrc == YHR_SUCCESS);
133
135 assert(yrc == YHR_SUCCESS);
136
137 printf("Closed session %02d\n", session_id);
138
140 sizeof(password2), false, &session);
141 assert(yrc == YHR_CRYPTOGRAM_MISMATCH);
142
143 printf("Unable to open session with Authentication Key %04x and password "
144 "\"%s\"\n",
146
148 assert(yrc == YHR_SUCCESS);
149
151 sizeof(password3), false, &session);
152 assert(yrc == YHR_SUCCESS);
153
155 assert(yrc == YHR_SUCCESS);
156
157 yrc = yh_get_session_id(session, &session_id);
158 assert(yrc == YHR_SUCCESS);
159
160 printf("Successfully established session %02d using Authentcation Key %04x "
161 "and password \"%s\"\n",
162 session_id, key_id, password3);
163
165 assert(yrc == YHR_SUCCESS);
166
168 assert(yrc == YHR_SUCCESS);
169
170 yh_disconnect(connector);
171 assert(yrc == YHR_SUCCESS);
172
173 yrc = yh_exit();
174 assert(yrc == YHR_SUCCESS);
175
176 return 0;
177}
const uint8_t password2[]
#define DEFAULT_CONNECTOR_URL
const uint8_t password1[]
int main(void)
const char * key_label
const uint8_t password3[]
CK_SESSION_HANDLE session
unsigned short uint16_t
Definition stdint.h:125
unsigned char uint8_t
Definition stdint.h:124
Capabilities representation.
Definition yubihsm.h:162
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_import_authentication_key_derived(yh_session *session, uint16_t *key_id, const char *label, uint16_t domains, const yh_capabilities *capabilities, const yh_capabilities *delegated_capabilities, const uint8_t *password, size_t password_len)
Definition yubihsm.c:3064
yh_rc yh_util_change_authentication_key_derived(yh_session *session, uint16_t *key_id, const uint8_t *password, size_t password_len)
Definition yubihsm.c:3155
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_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_get_session_id(yh_session *session, uint8_t *sid)
Definition yubihsm.c:2915
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_INVALID_ID
Return value when an invalid Object ID is used.
Definition yubihsm.h:220
@ YHR_CRYPTOGRAM_MISMATCH
Returned value when failing to verify cryptogram.
Definition yubihsm.h:189
yh_capabilities capabilities
yh_rc yrc
uint16_t key_id