Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
echo.c File Reference
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <yubihsm.h>
Include dependency graph for echo.c:

Go to the source code of this file.

Macros

#define DEFAULT_CONNECTOR_URL   "http://127.0.0.1:12345"
 

Functions

int main (void)
 

Variables

const uint8_t password [] = "password"
 
const uint8_t data [] = "sudo make me a sandwich"
 

Macro Definition Documentation

◆ DEFAULT_CONNECTOR_URL

#define DEFAULT_CONNECTOR_URL   "http://127.0.0.1:12345"

Definition at line 28 of file echo.c.

Function Documentation

◆ main()

int main ( void )

Definition at line 34 of file echo.c.

34 {
35 yh_connector *connector = NULL;
37
38 const char *connector_url;
39
40 connector_url = getenv("DEFAULT_CONNECTOR_URL");
41 if (connector_url == NULL) {
42 connector_url = DEFAULT_CONNECTOR_URL;
43 }
44
45 yrc = yh_init();
46 if (yrc != YHR_SUCCESS) {
47 fprintf(stderr, "unable to initialize yubihsm: %s\n", yh_strerror(yrc));
48 exit(EXIT_FAILURE);
49 }
50
51 yrc = yh_init_connector(connector_url, &connector);
52 assert(yrc == YHR_SUCCESS);
53
54 yrc = yh_connect(connector, 0);
55 assert(yrc == YHR_SUCCESS);
56
57 char *received_url;
58 yrc = yh_get_connector_address(connector, &received_url);
59 assert(yrc == YHR_SUCCESS);
60
61 yh_set_verbosity(connector, YH_VERB_ALL);
62
63 printf("Send a plain (unencrypted, unauthenticated) echo command\n");
64
65 uint16_t data_len = sizeof(data) - 1;
66 uint8_t response[sizeof(data)] = {0};
67 size_t response_len = sizeof(response);
68 yh_cmd response_cmd;
69 yrc = yh_send_plain_msg(connector, YHC_ECHO, data, data_len, &response_cmd,
70 response, &response_len);
71 if (yrc != YHR_SUCCESS) {
72 fprintf(stderr, "Failed to send ECHO command): %s\n", yh_strerror(yrc));
73 exit(EXIT_FAILURE);
74 }
75
76 if (response_cmd == YHC_ERROR) {
77 fprintf(stderr, "Unable to get echo data: %s (%x)\n",
78 yh_strerror(response[0]), response[0]);
79 exit(EXIT_FAILURE);
80 }
81
82 printf("Response (%zu bytes): \"%s\"\n", response_len, response);
83
84 yh_session *session = NULL;
85 uint16_t authkey = 1;
86 yrc = yh_create_session_derived(connector, authkey, password,
87 sizeof(password), false, &session);
88 assert(yrc == YHR_SUCCESS);
89
91 assert(yrc == YHR_SUCCESS);
92
93 uint8_t session_id;
94 yrc = yh_get_session_id(session, &session_id);
95 assert(yrc == YHR_SUCCESS);
96
97 printf("Successfully established session %02d\n", session_id);
98
99 printf("Send a secure echo command\n");
100
101 uint8_t response2[sizeof(data)] = {0};
102 size_t response2_len = sizeof(response);
103 yh_cmd response2_cmd;
104 yrc = yh_send_secure_msg(session, YHC_ECHO, data, data_len, &response2_cmd,
105 response2, &response2_len);
106 if (yrc != YHR_SUCCESS) {
107 fprintf(stderr, "Failed to send ECHO command: %s\n", yh_strerror(yrc));
108 exit(EXIT_FAILURE);
109 }
110
111 if (response_cmd == YHC_ERROR) {
112 fprintf(stderr, "Unable to get echo data: %s (%x)\n",
113 yh_strerror(response[0]), response[0]);
114 exit(EXIT_FAILURE);
115 }
116
117 printf("Response (%zu bytes): \"%s\"\n", response_len, response);
118
119 assert(response_len == response2_len);
120 assert(memcmp(response, response2, response_len) == 0);
121
123 assert(yrc == YHR_SUCCESS);
124
126 assert(yrc == YHR_SUCCESS);
127
128 yh_disconnect(connector);
129 assert(yrc == YHR_SUCCESS);
130
131 yrc = yh_exit();
132 assert(yrc == YHR_SUCCESS);
133
134 return 0;
135}
CK_SESSION_HANDLE session
#define DEFAULT_CONNECTOR_URL
Definition echo.c:28
const uint8_t data[]
Definition echo.c:32
const char * yh_strerror(yh_rc err)
Definition error.c:65
LOGGING_API void printf(Category category, const char *format,...)
Definition Logging.cpp:30
unsigned short uint16_t
Definition stdint.h:125
unsigned char uint8_t
Definition stdint.h:124
yh_rc yh_destroy_session(yh_session **session)
Definition yubihsm.c:890
yh_rc yh_set_verbosity(yh_connector *connector, uint8_t verbosity)
Definition yubihsm.c:3825
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_send_secure_msg(yh_session *session, yh_cmd cmd, const uint8_t *data, size_t data_len, yh_cmd *response_cmd, uint8_t *response, size_t *response_len)
Definition yubihsm.c:416
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_send_plain_msg(yh_connector *connector, yh_cmd cmd, const uint8_t *data, size_t data_len, yh_cmd *response_cmd, uint8_t *response, size_t *response_len)
Definition yubihsm.c:126
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_rc yh_get_session_id(yh_session *session, uint8_t *sid)
Definition yubihsm.c:2915
#define YH_VERB_ALL
Debug level all. All previous options enabled.
Definition yubihsm.h:145
yh_cmd
Definition yubihsm.h:243
@ YHC_ERROR
Definition yubihsm.h:348
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
yh_rc yrc
Here is the call graph for this function:

Variable Documentation

◆ data

const uint8_t data[] = "sudo make me a sandwich"

Definition at line 32 of file echo.c.

◆ password

const uint8_t password[] = "password"

Definition at line 31 of file echo.c.