Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
import_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!";
36
37int main(void) {
38 yh_connector *connector = NULL;
39 yh_session *session = NULL;
41
42 uint16_t authkey = 1;
43
44 const char *connector_url;
45
46 connector_url = getenv("DEFAULT_CONNECTOR_URL");
47 if (connector_url == NULL) {
48 connector_url = DEFAULT_CONNECTOR_URL;
49 }
50
51 yrc = yh_init();
52 assert(yrc == YHR_SUCCESS);
53
54 yrc = yh_init_connector(connector_url, &connector);
55 assert(yrc == YHR_SUCCESS);
56
57 yrc = yh_connect(connector, 0);
58 assert(yrc == YHR_SUCCESS);
59
60 yrc = yh_create_session_derived(connector, authkey, password1,
61 sizeof(password1), false, &session);
62 assert(yrc == YHR_SUCCESS);
63
65 assert(yrc == YHR_SUCCESS);
66
67 uint8_t session_id;
68 yrc = yh_get_session_id(session, &session_id);
69 assert(yrc == YHR_SUCCESS);
70
71 printf("Successfully established session %02d using Authentication Keys "
72 "%04x\n",
73 session_id, authkey);
74
76 yrc = yh_string_to_capabilities("get-log-entries", &capabilities);
77 assert(yrc == YHR_SUCCESS);
78
79 uint16_t domain_five = 0;
80 yrc = yh_string_to_domains("5", &domain_five);
81 assert(yrc == YHR_SUCCESS);
82
83 uint16_t key_id = 0; // ID 0 lets the device generate an ID
85 domain_five, &capabilities,
87 sizeof(password2));
88 assert(yrc == YHR_SUCCESS);
89
90 printf("Imported Authentication Key with ID %04x\n", key_id);
91
93 assert(yrc == YHR_SUCCESS);
94
96 assert(yrc == YHR_SUCCESS);
97
98 printf("Closed session %02d\n", session_id);
99
101 sizeof(password2), false, &session);
102 assert(yrc == YHR_SUCCESS);
103
105 assert(yrc == YHR_SUCCESS);
106
107 yrc = yh_get_session_id(session, &session_id);
108 assert(yrc == YHR_SUCCESS);
109
110 printf("Successfully established session %02d using Authentication Key "
111 "%04x\n",
112 session_id, key_id);
113
114 printf("Trying to get 16 bytes of random data\n");
115
116 uint8_t data[16];
117 size_t data_len = sizeof(data);
118 yrc = yh_util_get_pseudo_random(session, 16, data, &data_len);
120
121 printf("Unable to get random data: %s\n", yh_strerror(yrc));
122
124 assert(yrc == YHR_SUCCESS);
125
127 assert(yrc == YHR_SUCCESS);
128
129 yh_disconnect(connector);
130 assert(yrc == YHR_SUCCESS);
131
132 yrc = yh_exit();
133 assert(yrc == YHR_SUCCESS);
134
135 return 0;
136}
CK_SESSION_HANDLE session
const uint8_t password2[]
#define DEFAULT_CONNECTOR_URL
const uint8_t password1[]
int main(void)
const char * key_label
const char * yh_strerror(yh_rc err)
Definition error.c:65
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_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 yh_util_get_pseudo_random(yh_session *session, size_t len, uint8_t *out, size_t *out_len)
Definition yubihsm.c:1560
yh_rc
Definition yubihsm.h:170
@ YHR_DEVICE_INSUFFICIENT_PERMISSIONS
Return value when the permissions to perform the operation are wrong.
Definition yubihsm.h:214
@ 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
yh_rc yrc
uint16_t key_id