Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
info.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 <stdio.h>
22#include <stdlib.h>
23
24#include <yubihsm.h>
25
26#ifndef DEFAULT_CONNECTOR_URL
27#define DEFAULT_CONNECTOR_URL "http://127.0.0.1:12345"
28#endif
29
30int main(void) {
31 yh_connector *connector = NULL;
33
34 const char *connector_url;
35
36 connector_url = getenv("DEFAULT_CONNECTOR_URL");
37 if (connector_url == NULL) {
38 connector_url = DEFAULT_CONNECTOR_URL;
39 }
40
41 yrc = yh_init();
42 if (yrc != YHR_SUCCESS) {
43 fprintf(stderr, "unable to initialize yubihsm: %s\n", yh_strerror(yrc));
44 exit(EXIT_FAILURE);
45 }
46
47 yrc = yh_init_connector(connector_url, &connector);
48 assert(yrc == YHR_SUCCESS);
49
50 yrc = yh_connect(connector, 0);
51 assert(yrc == YHR_SUCCESS);
52
53 char *received_url;
54 yrc = yh_get_connector_address(connector, &received_url);
55 assert(yrc == YHR_SUCCESS);
56
57 printf("Successfully connected to %s, device is ", received_url);
58 if (yh_connector_has_device(connector) == false) {
59 printf("not present\n");
60 exit(EXIT_FAILURE); // This won't happen since we manged to connect
61 }
62 printf("present\n");
63
64 uint8_t c_major, c_minor, c_patch;
65 yrc = yh_get_connector_version(connector, &c_major, &c_minor, &c_patch);
66 assert(yrc == YHR_SUCCESS);
67 printf("Connector Version: %hhu.%hhu.%hhu\n", c_major, c_minor, c_patch);
68
69 uint8_t d_major, d_minor, d_patch;
71 uint8_t log_total, log_used;
73 size_t n_algorithms = sizeof(algorithms);
74 yrc =
75 yh_util_get_device_info(connector, &d_major, &d_minor, &d_patch, &serial,
76 &log_total, &log_used, algorithms, &n_algorithms);
77 assert(yrc == YHR_SUCCESS);
78
79 printf("Device Version: %hhu.%hhu.%hhu\n", d_major, d_minor, d_patch);
80 printf("Serial: %d\n", serial);
81 printf("Log: %d/%d (used/total)\n", log_used, log_total);
82 printf("Supported algorithms:\n");
83 for (size_t i = 0; i < n_algorithms; i++) {
84 const char *str;
85 yh_algo_to_string(algorithms[i], &str);
86 printf("%s\n", str);
87 }
88
89 yrc = yh_disconnect(connector);
90 assert(yrc == YHR_SUCCESS);
91
92 yrc = yh_exit();
93 assert(yrc == YHR_SUCCESS);
94
95 return 0;
96}
#define DEFAULT_CONNECTOR_URL
Definition info.c:27
int main(void)
Definition info.c:30
const char * yh_strerror(yh_rc err)
Definition error.c:65
unsigned int uint32_t
Definition stdint.h:126
unsigned char uint8_t
Definition stdint.h:124
bool yh_connector_has_device(yh_connector *connector)
Definition yubihsm.c:906
yh_rc yh_algo_to_string(yh_algorithm algo, char const **result)
Definition yubihsm.c:4384
yh_rc yh_get_connector_version(yh_connector *connector, uint8_t *major, uint8_t *minor, uint8_t *patch)
Definition yubihsm.c:911
yh_rc yh_exit(void)
Definition yubihsm.c:3910
yh_rc yh_init(void)
Definition yubihsm.c:3857
yh_rc yh_util_get_device_info(yh_connector *connector, uint8_t *major, uint8_t *minor, uint8_t *patch, uint32_t *serial, uint8_t *log_total, uint8_t *log_used, yh_algorithm *algorithms, size_t *n_algorithms)
Definition yubihsm.c:938
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_disconnect(yh_connector *connector)
Definition yubihsm.c:4097
yh_rc yh_get_connector_address(yh_connector *connector, char **const address)
Definition yubihsm.c:926
yh_algorithm
Definition yubihsm.h:390
#define YH_MAX_ALGORITHM_COUNT
Max number of algorithms defined here.
Definition yubihsm.h:383
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
uint32_t serial
yh_rc yrc