Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
yubihsm_usb.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#include <stdint.h>
18#include <string.h>
19#ifdef __WIN32
20#include <winsock.h>
21#else
22#include <arpa/inet.h>
23#endif
24
25#include "yubihsm.h"
26#include "internal.h"
27#include "yubihsm_usb.h"
28#include "debug_lib.h"
29
32
33static void backend_set_verbosity(uint8_t verbosity, FILE *output) {
34 _yh_verbosity = verbosity;
35 _yh_output = output;
36}
37
38static yh_rc backend_init(uint8_t verbosity, FILE *output) {
39 backend_set_verbosity(verbosity, output);
40 return YHR_SUCCESS;
41}
42
43static yh_rc backend_connect(yh_connector *connector, int timeout) {
44 unsigned long serial = 0;
45
47 yh_backend *backend = NULL;
48
49 (void) timeout;
50
51 if (parse_usb_url(connector->api_url, &serial) == false) {
52 DBG_ERR("Failed to parse URL: '%s'", connector->api_url);
53 goto out;
54 }
55
56 backend = connector->connection;
57 usb_set_serial(backend, serial);
58 if (usb_open_device(backend) == false) {
59 DBG_ERR("No device returned");
60 goto out;
61 }
62
64 connector->has_device = 1;
65out:
66 return ret;
67}
68
69static void backend_disconnect(yh_backend *connection) {
70 usb_destroy(&connection);
71}
72
73static yh_rc backend_send_msg(yh_backend *connection, Msg *msg, Msg *response) {
74 int32_t trf_len = msg->st.len + 3;
76 unsigned long read_len;
77 msg->st.len = htons(msg->st.len);
78
79 for (int i = 0; i <= 1; i++) {
80 if (ret != YHR_GENERIC_ERROR) {
81 DBG_INFO("Reconnecting device");
82 usb_close(connection);
83 if (usb_open_device(connection) == false) {
84 DBG_ERR("Failed reconnecting device");
86 }
87 }
88 if (usb_write(connection, msg->raw, trf_len) == 0) {
90 DBG_ERR("USB write failed");
91 continue;
92 }
93
94 read_len = SCP_MSG_BUF_SIZE;
95 if (usb_read(connection, response->raw, &read_len) == 0) {
97 DBG_ERR("USB read failed");
98 continue;
99 }
101 break;
102 }
103
104 if (ret != YHR_SUCCESS) {
105 return ret;
106 }
107
108 if (read_len < 3) {
109 DBG_ERR("Not enough data received; %lu", read_len);
110 return YHR_WRONG_LENGTH;
111 }
112
113 response->st.len = ntohs(response->st.len);
114
115 if (response->st.len != read_len - 3) {
116 DBG_ERR("Wrong length received, %d vs %lu", response->st.len, read_len);
117 return YHR_WRONG_LENGTH;
118 }
119
120 return YHR_SUCCESS;
121}
122
123static void backend_cleanup(void) {}
124
125static yh_rc backend_option(yh_backend *connection, yh_connector_option opt,
126 const void *val) {
127 (void) connection;
128 (void) opt;
129 (void) val;
130
131 DBG_ERR("Backend options not (yet?) supported for USB");
132 return YHR_CONNECTOR_ERROR;
133}
134
139
140#ifdef STATIC
141struct backend_functions *usb_backend_functions(void) {
142#else
144#endif
145 return &f;
146}
#define YH_INTERNAL
Definition aes.h:58
#define DBG_ERR(...)
Definition debug_lib.h:76
#define DBG_INFO(...)
Definition debug_lib.h:63
bool YH_INTERNAL parse_usb_url(const char *url, unsigned long *serial)
Definition lib_util.c:140
#define SCP_MSG_BUF_SIZE
Definition scp.h:52
signed int int32_t
Definition stdint.h:123
unsigned char uint8_t
Definition stdint.h:124
yh_rc(* backend_init)(uint8_t verbosity, FILE *output)
Definition internal.h:66
void(* backend_set_verbosity)(uint8_t verbosity, FILE *output)
Definition internal.h:74
void(* backend_disconnect)(yh_backend *connection)
Definition internal.h:69
yh_rc(* backend_send_msg)(yh_backend *connection, Msg *msg, Msg *response)
Definition internal.h:70
yh_rc(* backend_option)(yh_backend *connection, yh_connector_option opt, const void *val)
Definition internal.h:72
yh_backend *(* backend_create)(void)
Definition internal.h:67
void(* backend_cleanup)(void)
Definition internal.h:71
yh_rc(* backend_connect)(yh_connector *connector, int timeout)
Definition internal.h:68
char * api_url
Definition internal.h:42
bool has_device
Definition internal.h:43
yh_backend * connection
Definition internal.h:40
Definition scp.h:56
struct _Msg::@103 st
uint8_t raw[3+SCP_MSG_BUF_SIZE]
Definition scp.h:62
yh_connector_option
Definition yubihsm.h:500
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_CONNECTOR_ERROR
Return value when connector operation failed.
Definition yubihsm.h:232
@ YHR_WRONG_LENGTH
Definition yubihsm.h:185
@ YHR_CONNECTION_ERROR
Returned value when a connection error was encountered.
Definition yubihsm.h:178
void usb_destroy(yh_backend **state)
void usb_close(yh_backend *state)
void usb_set_serial(yh_backend *state, unsigned long serial)
int usb_read(yh_backend *state, unsigned char *buf, unsigned long *len)
bool usb_open_device(yh_backend *backend)
int usb_write(yh_backend *state, unsigned char *buf, long unsigned len)
uint32_t serial
CK_RV ret
FILE YH_INTERNAL * _yh_output
Definition yubihsm_usb.c:31
struct backend_functions * backend_functions(void)
uint8_t YH_INTERNAL _yh_verbosity
Definition yubihsm_usb.c:30