Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
logs.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/evp.h>
28
29#include "util.h"
30
31#include <yubihsm.h>
32
33#ifndef DEFAULT_CONNECTOR_URL
34#define DEFAULT_CONNECTOR_URL "http://127.0.0.1:12345"
35#endif
36
37#define N_OPERATIONS 5
38
39const uint8_t password[] = "password";
40
41int main(void) {
42 yh_connector *connector = NULL;
43 yh_session *session = NULL;
45
46 uint16_t authkey = 1;
47
48 const char *connector_url;
49
50 connector_url = getenv("DEFAULT_CONNECTOR_URL");
51 if (connector_url == NULL) {
52 connector_url = DEFAULT_CONNECTOR_URL;
53 }
54
55 yrc = yh_init();
56 assert(yrc == YHR_SUCCESS);
57
58 yrc = yh_init_connector(connector_url, &connector);
59 assert(yrc == YHR_SUCCESS);
60
61 yrc = yh_connect(connector, 0);
62 assert(yrc == YHR_SUCCESS);
63
64 yrc = yh_create_session_derived(connector, authkey, password,
65 sizeof(password), false, &session);
66 assert(yrc == YHR_SUCCESS);
67
69 assert(yrc == YHR_SUCCESS);
70
71 uint8_t session_id;
72 yrc = yh_get_session_id(session, &session_id);
73 assert(yrc == YHR_SUCCESS);
74
75 printf("Successfully established session %02d\n", session_id);
76 printf("Flushing existing logs\n");
77
78 uint16_t unlogged_boot, unlogged_auth;
80 size_t n_items = sizeof(logs) / sizeof(yh_log_entry);
81 yh_log_entry last_previous_log;
82 yh_log_entry *last_previous_log_ptr = &last_previous_log;
83
84 yrc = yh_util_get_log_entries(session, &unlogged_boot, &unlogged_auth, logs,
85 &n_items);
86 assert(yrc == YHR_SUCCESS);
87
88 if (n_items != 0) {
89 memcpy(&last_previous_log, logs + n_items - 1, sizeof(yh_log_entry));
90 } else {
91 last_previous_log_ptr = NULL;
92 }
93
94 uint16_t last_index = logs[n_items - 1].number;
95
96 yrc = yh_util_set_log_index(session, last_index);
97 assert(yrc == YHR_SUCCESS);
98
99 printf("Performing some operations\n");
100
101 for (uint16_t i = 0; i < N_OPERATIONS; i++) {
102 yh_object_descriptor descriptor;
104 &descriptor);
105 assert(yrc == YHR_SUCCESS);
106 }
107
108 printf("Getting logs\n");
109
110 n_items = sizeof(logs) / sizeof(yh_log_entry);
111 yrc = yh_util_get_log_entries(session, &unlogged_boot, &unlogged_auth, logs,
112 &n_items);
113 assert(yrc == YHR_SUCCESS);
114
115 assert(n_items == N_OPERATIONS + 1);
116
117 fprintf(stdout, "%d unlogged boots found\n", unlogged_boot);
118 fprintf(stdout, "%d unlogged authentications found\n", unlogged_auth);
119
120 char digest_buf[(2 * YH_LOG_DIGEST_SIZE) + 1];
121
122 if (n_items == 0) {
123 fprintf(stdout, "No logs to extract\n");
124 return 0;
125 } else if (n_items == 1) {
126 fprintf(stdout, "Found 1 item\n");
127 } else {
128 fprintf(stdout, "Found %zu items\n", n_items);
129 }
130
131 for (uint16_t i = 0; i < n_items; i++) {
132 format_digest(logs[i].digest, digest_buf, YH_LOG_DIGEST_SIZE);
133 fprintf(stdout,
134 "item: %5u -- cmd: 0x%02x -- length: %4u -- session key: "
135 "0x%04x -- target key: 0x%04x -- second key: 0x%04x -- "
136 "result: 0x%02x -- tick: %lu -- hash: %s\n",
137 logs[i].number, logs[i].command, logs[i].length,
138 logs[i].session_key, logs[i].target_key, logs[i].second_key,
139 logs[i].result, (unsigned long) logs[i].systick, digest_buf);
140 }
141
142 bool ret = yh_verify_logs(logs, n_items, last_previous_log_ptr);
143 assert(ret == true);
144
145 printf("Logs correctly verified\n");
146
147 uint8_t option[128];
148 size_t option_len;
149
150 option[0] = YHC_SET_OPTION;
151 option[1] = 0x00;
152 option_len = 2;
153 yrc =
155 assert(yrc == YHR_SUCCESS);
156
157 option_len = sizeof(option);
158 yrc =
160 assert(yrc == YHR_SUCCESS);
161
162 assert(option_len % 2 == 0);
163 bool option_found = false;
164 for (size_t i = 0; i < option_len; i += 2) {
165 if (option[i] == YHC_SET_OPTION) {
166 assert(option[i + 1] == 0);
167 option_found = true;
168 break;
169 }
170 }
171 assert(option_found == true);
172
173 option[0] = YHC_SET_OPTION;
174 option[1] = 0x01;
175 option_len = 2;
176 yrc =
178 assert(yrc == YHR_SUCCESS);
179
180 option_len = sizeof(option);
181 yrc =
183 assert(yrc == YHR_SUCCESS);
184
185 assert(option_len % 2 == 0);
186 option_found = false;
187 for (size_t i = 0; i < option_len; i += 2) {
188 if (option[i] == YHC_SET_OPTION) {
189 assert(option[i + 1] == 1);
190 option_found = true;
191 break;
192 }
193 }
194 assert(option_found == true);
195
197 assert(yrc == YHR_SUCCESS);
198
200 assert(yrc == YHR_SUCCESS);
201
202 yh_disconnect(connector);
203 assert(yrc == YHR_SUCCESS);
204
205 yrc = yh_exit();
206 assert(yrc == YHR_SUCCESS);
207
208 return EXIT_SUCCESS;
209}
CK_SESSION_HANDLE session
#define DEFAULT_CONNECTOR_URL
Definition logs.c:34
#define N_OPERATIONS
Definition logs.c:37
int main(void)
Definition logs.c:41
unsigned short uint16_t
Definition stdint.h:125
unsigned char uint8_t
Definition stdint.h:124
Definition yubihsm.h:516
uint16_t number
Monotonically increasing index.
Definition yubihsm.h:518
void format_digest(uint8_t *digest, char *str, uint16_t len)
Definition util.c:326
bool yh_verify_logs(yh_log_entry *logs, size_t n_items, yh_log_entry *last_previous_log)
Definition yubihsm.c:4480
yh_rc yh_util_get_option(yh_session *session, yh_option option, uint8_t *out, size_t *out_len)
Definition yubihsm.c:3584
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_get_log_entries(yh_session *session, uint16_t *unlogged_boot, uint16_t *unlogged_auth, yh_log_entry *out, size_t *n_items)
Definition yubihsm.c:2531
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_util_set_option(yh_session *session, yh_option option, size_t len, uint8_t *val)
Definition yubihsm.c:3537
yh_rc yh_init_connector(const char *url, yh_connector **connector)
Definition yubihsm.c:4024
yh_rc yh_util_set_log_index(yh_session *session, uint16_t index)
Definition yubihsm.c:2606
yh_rc yh_connect(yh_connector *connector, int timeout)
Definition yubihsm.c:4079
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_option option
Definition yubihsm.h:685
#define YH_LOG_DIGEST_SIZE
Size that the log digest is truncated to.
Definition yubihsm.h:127
@ YH_AUTHENTICATION_KEY
Authentication Key is used to establish Sessions with a device.
Definition yubihsm.h:364
@ YH_OPTION_COMMAND_AUDIT
Enable/Disable logging of specific commands.
Definition yubihsm.h:494
#define YH_MAX_LOG_ENTRIES
Max log entries the device may hold.
Definition yubihsm.h:121
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
CK_RV ret
yh_rc yrc
memcpy((char *) pInfo->slotDescription, s, l)