35#include <openssl/evp.h>
39#define PROMPT "yubihsm> "
41#define ARGS_BUFFER_SIZE 4096
43#define COMPLETION_CANDIDATES 256
44#define MAX_COMMAND_NAME 32
45#define MAX_ARGUMENTS 32
47#define LOCAL_CONNECTOR_URL "http://127.0.0.1:12345"
51#define SPACES " \f\n\r\t\v"
57#define S_ISLNK S_ISREG
59#include <editline/readline.h>
65#define LIB_SUCCEED_OR_DIE(x, s) \
66 if ((x) != YHR_SUCCESS) { \
67 fprintf(stderr, s "%s\n", yh_strerror(x)); \
72#define COM_SUCCEED_OR_DIE(x, s) \
74 fprintf(stderr, s "\n"); \
79static bool calling_device =
false;
129 fprintf(stderr,
"Failed to allocate memory\n");
161 while (left != NULL) {
166 for (
int i = 0; i < in_size; i++) {
175 right_size = in_size;
177 while (left_size > 0 || (right_size > 0 && right)) {
179 if (left_size == 0) {
183 }
else if (right_size == 0 || !right) {
187 }
else if (strcmp(left->
name, right->
name) <= 0) {
234 "Set the log index", NULL, NULL});
237 "Connect to a connector", NULL, NULL});
239 fmt_nofmt,
"Change debug settings", NULL,
246 fmt_nofmt,
"Toggle crypto debug messages", NULL,
250 "Toggle error debug messages", NULL, NULL});
256 "Toggle intermediate debug messages", NULL,
269 "Decrypt data using RSAES-PKCS#1v1.5", NULL,
274 "e:session,w:key_id,a:algorithm,i:data=-,s:label=",
fmt_binary,
275 fmt_base64,
"Decrypt data using RSAES-OAEP", NULL, NULL});
279 fmt_binary,
"Decrypt data using Yubico-AES-CCM",
284 "e:session,w:key_id,i:pubkey=-",
fmt_PEM,
285 fmt_hex,
"Perform a ECDH key exchange",
293 fmt_base64,
"Encrypt data using Yubico-AES-CCM",
298 "Disconnect from a connector", NULL, NULL});
303 "Send an ECHO command over a given session",
308 "e:session,w:key_id,s:label,d:domains,c:"
309 "capabilities,a:algorithm",
311 "Generate an asymmetric key", NULL, NULL});
313 "e:session,w:key_id,s:label,d:domains,c:"
314 "capabilities,a:algorithm",
319 "e:session,w:key_id,s:label,d:domains,c:"
320 "capabilities,c:delegated_capabilities,a:algorithm",
323 "e:session,w:key_id,s:label,d:domains,c:"
324 "capabilities,a:algorithm,u:nonce_id",
326 "Generate OTP AEAD key", NULL, NULL});
328 fmt_nofmt,
"Retrieve data", NULL, NULL});
330 "e:session,w:object_id,F:file=-",
fmt_nofmt,
336 "Get a global option value", NULL, NULL});
340 "Get pseudo-random bytes", NULL, NULL});
345 "e:session,w:key_id,F:file=-",
fmt_nofmt,
346 fmt_PEM,
"Get a public key", NULL, NULL});
350 "Get information about an object", NULL, NULL});
353 "e:session,w:wrapkey_id,t:type,w:id,F:file=-",
355 "Get an object under wrap", NULL, NULL});
358 "Extract the version number, serial number "
359 "and supported algorithms",
362 "e:session,w:object_id,F:out=-",
fmt_nofmt,
368 fmt_nofmt,
"Display help text", NULL, NULL});
371 fmt_nofmt,
"Display the command history",
375 fmt_nofmt,
"List information", NULL, NULL});
378 "Prints a list of possible capabilities",
382 "Prints a list of possible algorithms",
386 fmt_nofmt,
"Prints a list of possible types",
390 "List the open session", NULL, NULL});
392 "e:session,w:id=0,t:type=any,d:domains=0,c:"
393 "capabilities=0,a:algorithm=any,s:label=",
395 "List objects according to filter", NULL,
400 "Send unencrypted and unauthenticated commands",
404 "Send a plain echo command", NULL, NULL});
408 "e:session,w:key_id,s:label,d:domains,c:"
409 "capabilities,i:key=-",
411 "Store an asymmetric key", NULL, NULL});
413 "e:session,w:key_id,s:label,d:domains,c:"
414 "capabilities,c:delegated_capabilities,i:"
417 "Store an authentication key", NULL, NULL});
419 "e:session,w:object_id,s:label,d:domains,c:"
420 "capabilities,a:algorithm,i:data=-",
422 "Store an opaque object", NULL, NULL});
426 "Set a global option value", NULL, NULL});
428 "e:session,w:key_id,s:label,d:domains,c:"
429 "capabilities,a:algorithm,i:key",
434 "e:session,w:key_id,s:label,d:domains,c:"
435 "capabilities,c:delegated_capabilities,i:key",
439 "e:session,w:wrapkey_id,i:data=-",
441 "Store a wrapped object", NULL, NULL});
443 "e:session,w:object_id,s:label,d:domains,c:"
444 "capabilities,a:algorithm,i:data=-",
446 "Store a template object", NULL, NULL});
448 "e:session,w:key_id,s:label,d:domains,c:"
449 "capabilities,u:nonce_id,i:key",
454 fmt_nofmt,
"Quit yubihsm-shell", NULL, NULL});
457 fmt_nofmt,
"Quit yubihsm-shell", NULL, NULL});
460 fmt_nofmt,
"Manage sessions", NULL, NULL});
463 "Close a session with a connector", NULL,
468 "Open a session with a device using a "
469 "specific Authentication Key",
475 "e:session,w:key_id,a:algorithm,i:data=-,F:out=-",
fmt_binary,
476 fmt_base64,
"Sign data using ECDSA", NULL, NULL});
479 "e:session,w:key_id,a:algorithm,i:data=-,F:out=-",
fmt_binary,
480 fmt_base64,
"Sign data using EDDSA", NULL, NULL});
483 "e:session,w:key_id,a:algorithm,i:data=-,F:out=-",
fmt_binary,
484 fmt_base64,
"Sign data using RSASSA-PKCS#1v1.5", NULL, NULL});
487 "e:session,w:key_id,a:algorithm,i:data=-,F:out=-",
fmt_binary,
488 fmt_base64,
"Sign data using RSASSA-PSS", NULL, NULL});
491 "e:session,w:key_id,i:data=-,F:out=-",
501 "e:session,w:key_id,w:template_id,a:"
502 "algorithm,i:infile=-,F:outfile=-",
505 "Sign SSH certificates", NULL, NULL});
509 "e:session,u:count,w:key_id=0,a:algorithm=any",
516 "e:session,w:key_id,i:key,i:private_id,F:aead",
524 "e:session,w:key_id,s:otp,i:aead",
526 "Decrypt an OTP with AEAD", NULL, NULL});
528 fmt_nofmt,
"Attest device objects", NULL,
533 "e:session,w:key_id,w:attest_id=0",
fmt_nofmt,
534 fmt_PEM,
"Sign attestation certificate", NULL,
541 fmt_nofmt,
"Enable keepalive", NULL, NULL});
544 fmt_nofmt,
"Disable keepalive", NULL, NULL});
548 fmt_nofmt,
"Set preferences", NULL, NULL});
555 "Set output format", NULL, NULL});
558 "Set CA cert to use for https to connector",
562 "Set proxyserver to use for connector",
567 fmt_nofmt,
"Blink the device", NULL, NULL});
570 fmt_nofmt,
"Change objects", NULL, NULL});
575 fmt_nofmt,
"Change an authentication key", NULL,
581 if (t->subcommands != NULL) {
606 for (
rv = history(
g_hist, &ev, H_LAST);
rv != -1;
607 rv = history(
g_hist, &ev, H_PREV)) {
608 fprintf(ctx->
out,
"%4d %s", ev.num, ev.str);
615static const char *fmt_to_string(
cmd_format fmt) {
616 for (
size_t i = 0; i <
sizeof(formats) /
sizeof(formats[0]); i++) {
617 if (formats[i].
format == fmt) {
618 return formats[i].name;
636 if (strncmp(
argv[0].
s, command->name, strlen(
argv[0].
s)) == 0) {
638 printf(
"%-25s%s\n", command->name, command->doc);
639 if (strlen(
argv[0].
s)) {
641 printf(
"%-5s%s",
"", command->args);
643 printf(
" (default input format: %s)",
644 fmt_to_string(command->in_fmt));
649 subcommand = subcommand->
next) {
650 printf(
"%-5s%-25s%s",
"", subcommand->name, subcommand->doc);
651 if (subcommand->args) {
653 printf(
" (default input format: %s)",
654 fmt_to_string(subcommand->in_fmt));
656 printf(
"\n%-30s%s\n",
"", subcommand->args);
665 if (match ==
false) {
666 printf(
"Help for command %s not found\n",
argv[0].
s);
688 size_t response_len =
sizeof(response);
693 &response_cmd, response,
695 if (response_cmd != YHC_ECHO_R) {
709 unsigned char TimerOrWaitFired
715 if (calling_device ==
true || ctx.
connector == NULL) {
719 probe_session(&ctx, i);
723static int set_keepalive(
uint16_t seconds) {
727 static HANDLE timerQueue = NULL;
729 if (timerQueue != NULL) {
730 DeleteTimerQueue(timerQueue);
732 timerQueue = CreateTimerQueue();
733 if (timerQueue == NULL) {
734 fprintf(stderr,
"Failed to setup timer\n");
737 CreateTimerQueueTimer(&timer, timerQueue, timer_handler, NULL, seconds * 1000,
740 fprintf(stderr,
"Failed to start time\n");
744 struct itimerval itimer;
745 itimer.it_interval.tv_sec = seconds;
746 itimer.it_interval.tv_usec = 0;
747 itimer.it_value.tv_sec = seconds;
748 itimer.it_value.tv_usec = 0;
749 if (setitimer(ITIMER_REAL, &itimer, NULL) != 0) {
750 fprintf(stderr,
"Failed to setup timer\n");
755 fprintf(stderr,
"Session keepalive set up to run every %d seconds\n",
769 return set_keepalive(15);
780 return set_keepalive(0);
786 if (strcasecmp(
argv[0].
s,
"default") == 0) {
790 for (
size_t i = 0; i <
sizeof(formats) /
sizeof(formats[0]); i++) {
791 if (strcasecmp(
argv[0].
s, formats[i].
name) == 0) {
792 ctx->
in_fmt = formats[i].format;
796 fprintf(stderr,
"Failed to parse input format\n");
803 if (strcasecmp(
argv[0].
s,
"default") == 0) {
807 for (
size_t i = 0; i <
sizeof(formats) /
sizeof(formats[0]); i++) {
808 if (strcasecmp(
argv[0].
s, formats[i].
name) == 0) {
812 ctx->
out_fmt = formats[i].format;
816 fprintf(stderr,
"Failed to parse output format\n");
820void find_lcp(
const char *items[],
int n_items,
const char **lcp,
826 if (items == NULL || n_items == 0) {
831 for (
int i = 1; i < n_items; i++) {
832 if (strcmp(items[i], items[min]) < 0) {
834 }
else if (strcmp(items[i], items[max]) > 0) {
840 for (
unsigned int i = 0; i < strlen(items[min]) && i < strlen(items[max]);
842 if (items[min][i] != items[max][i]) {
849 *lcp_len = strlen(items[min]);
852int tokenize(
char *line,
char **toks,
int max_toks,
int *cursorc,
int *cursoro,
856 int length = strlen(line);
857 int start_of_word = 0;
858 enum states { SPACE, WORD, QUOTE }
state = SPACE;
861 for (i = 0; i <= length; i++) {
863 if (cursorc && i == *cursorc && tok > 0) {
864 if (
state == SPACE) {
868 *cursoro = i - start_of_word;
875 if (tok >= max_toks) {
881 for (
size_t j = 0;
j < strlen(space);
j++) {
892 start_of_word = i + 1;
893 }
else if (c ==
'#') {
899 toks[tok++] = line + start_of_word;
908 for (
size_t j = 0;
j < strlen(space);
j++) {
921static int compare_strings(
const void *
a,
const void *b) {
922 return strcmp(*(
char *
const *)
a, *(
char *
const *) b);
929 int n_candidates = 0;
931 const LineInfo *li = el_line(el);
932 ioctl(fileno(stdout), TIOCGWINSZ, &w);
933 int lines = (li->cursor - li->buffer + strlen(
PROMPT)) / w.ws_col;
952 printf(
"\ninput data");
957 printf(
"\noutput filename");
983 int cursorc = cursoro;
986 if (cursorc == num_toks) {
989 for (
size_t i = 0; i <
sizeof(yh_capability) /
sizeof(yh_capability[0]);
991 if (strncasecmp(toks[cursorc], yh_capability[i].
name,
992 strlen(toks[cursorc])) == 0) {
993 candidates[n_candidates++] = yh_capability[i].name;
1000 for (
uint16_t i = 0; i <
sizeof(yh_algorithms) /
sizeof(yh_algorithms[0]);
1002 if (strncasecmp(line, yh_algorithms[i].
name, strlen(line)) == 0) {
1003 candidates[n_candidates++] = yh_algorithms[i].name;
1011 for (
uint16_t i = 0; i <
sizeof(yh_types) /
sizeof(yh_types[0]); i++) {
1012 if (strncasecmp(line, yh_types[i].
name, strlen(line)) == 0) {
1013 candidates[n_candidates++] = yh_types[i].name;
1021 for (
uint16_t i = 0; i <
sizeof(yh_options) /
sizeof(yh_options[0]);
1023 if (strncasecmp(line, yh_options[i].
name, strlen(line)) == 0) {
1024 candidates[n_candidates++] = yh_options[i].name;
1032 for (
uint16_t i = 0; i <
sizeof(formats) /
sizeof(formats[0]); i++) {
1033 if (strncasecmp(line, formats[i].
name, strlen(line)) == 0) {
1034 candidates[n_candidates++] = formats[i].name;
1049 printf(
"\nunknown type");
1054 switch (n_candidates) {
1058 while (arg[i] !=
'\0' && arg[i] !=
',') {
1062 printf(
"%*.*s\n", i, i - 1, arg + 1);
1066 el_insertstr(el, candidates[0] + cursoro);
1068 return CC_REDISPLAY;
1074 qsort(candidates, n_candidates,
sizeof(
char *), compare_strings);
1076 find_lcp(candidates, n_candidates, &lcp, &lcp_len);
1078 if (cursoro == lcp_len) {
1082 for (
int i = 0; i < n_candidates; i++) {
1083 printf(
"%s\n", candidates[i]);
1088 strcpy(prefix, lcp + cursoro);
1089 prefix[lcp_len - cursoro] =
'\0';
1091 el_insertstr(el, prefix);
1092 return CC_REDISPLAY;
1097 for (
int i = 0; i <
lines; i++) {
1100 return CC_REDISPLAY;
1104 const char *line,
int cursoro) {
1107 int n_candidates = 0;
1110 for (
Command *command = to_complete; command; command = command->
next) {
1111 if (strncmp(line, command->name, strlen(line)) == 0) {
1114 candidates[n_candidates++] = command->name;
1116 }
else if (n_candidates != 0) {
1123 switch (n_candidates) {
1131 el_insertstr(el, candidates[0] + cursoro);
1132 el_insertstr(el,
" ");
1134 return CC_REDISPLAY;
1141 find_lcp(candidates, n_candidates, &lcp, &lcp_len);
1143 if (cursoro == lcp_len) {
1147 for (
int i = 0; i < n_candidates; i++) {
1148 printf(
"%s\n", candidates[i]);
1153 strcpy(prefix, lcp + cursoro);
1154 prefix[lcp_len - cursoro] =
'\0';
1156 el_insertstr(el, prefix);
1159 return CC_REDISPLAY;
1169 int argc, cursorc, cursoro;
1175 cursorc = li->cursor - li->buffer;
1176 if (li->lastchar - li->buffer > 1024) {
1177 return CC_REDISPLAY;
1179 memcpy(data, li->buffer, li->lastchar - li->buffer);
1190 printf(
"%s\n", command->name);
1192 return CC_REDISPLAY;
1199 bool completing_args =
false;
1201 while (i < cursorc) {
1203 if (completing_args ==
false) {
1205 if (strncmp(
argv[i], command->
name, strlen(
argv[i])) == 0) {
1207 to_complete = command;
1215 completing_args =
true;
1216 if (command->
args != NULL) {
1217 args = command->
args;
1225 command = command->
next;
1226 if (command == NULL) {
1233 while (*args !=
'\0' && *args !=
',') {
1245 if (to_complete && cursorc != 0) {
1253 if (argc == cursorc) {
1254 if (to_complete && completing_args ==
false) {
1258 for (
Command *command = to_complete; command; command = command->
next) {
1259 printf(
"%s\n", command->
name);
1261 return CC_REDISPLAY;
1267 if (completing_args ==
false) {
1268 if (to_complete == NULL) {
1281static char *prompt(EditLine *el) {
1289static FILE *open_file(
const char *
name,
bool input) {
1291 if (strcmp(
name,
"-") == 0) {
1294 return fopen(
name,
"rb");
1297 if (strcmp(
name,
"-") == 0) {
1300 return fopen(
name,
"ab");
1305static bool get_input_data(
const char *
name,
uint8_t *out,
size_t *
len,
1309 int st_res = stat(
name, &sb);
1310 if (strcmp(
name,
"-") == 0 || strncasecmp(
name,
"file:", 5) == 0 ||
1311 (st_res == 0 && S_ISREG(sb.st_mode))) {
1315 if (strncasecmp(
name,
"file:", 5) == 0) {
1318 file = open_file(
name,
true);
1323 if (EVP_read_pw_string((
char *) out, *
len,
"Enter password: ", 0) == 0) {
1324 data_len = strlen((
char *) out);
1327 }
else if (
read_file(file, out, &data_len)) {
1330 if (file != stdin) {
1339 data_len = strlen(
name);
1359 if (out[data_len - 1] ==
'\n') {
1362 if (out[data_len - 1] ==
'\r') {
1365 out[data_len] =
'\0';
1388 memset(buffer, 0x0,
sizeof(buffer));
1398 }
else if (type ==
'w' || type ==
'e') {
1400 }
else if (type ==
'u') {
1406 unsigned long num = strtoul(
value, &endptr, 0);
1408 if ((errno == ERANGE || num > max) || (errno != 0 && num == 0)) {
1412 if (endptr ==
value) {
1418 }
else if (type ==
'e') {
1423 }
else if (type ==
'w') {
1425 }
else if (type ==
'u') {
1436 ctx->
out = open_file(
value,
false);
1445 if (parsed->
x == NULL) {
1449 if (get_input_data(
value, parsed->
x, &parsed->
len, fmt) ==
false) {
1463 if (strcmp(
value,
"stdin:") == 0) {
1465 "Enter hex key: ", 0) != 0) {
1519 bool completing_args =
false;
1521 const char *args =
"";
1524 int n_arguments = 0;
1526 bool invalid_arg =
false;
1534 memset(data, 0x0,
sizeof(data));
1535 memset(arg_data, 0x0,
sizeof(data));
1538 printf(
"Command too long\n");
1548 if (completing_args ==
false) {
1550 match = strncmp(
argv[i], command->
name, strlen(
argv[i]));
1560 completing_args =
true;
1561 func = command->
func;
1562 if (command->
args != NULL) {
1565 args = command->
args;
1566 strncpy(arg_data, args,
1568 int num_args =
tokenize(arg_data, arg_toks, 64, NULL, NULL,
",");
1569 if (num_args + 1 + i !=
1571 for (
int j = 0;
j < num_args;
j++) {
1572 if (
j < argc - 1 - i) {
1575 char *str = strchr(arg_toks[
j],
'=');
1580 argv[
j + 1 + i] = str;
1593 command = command->
next;
1594 if (match < 0 || command == NULL) {
1603 : command->
in_fmt) != 0) {
1607 while (*args !=
'\0' && *args !=
',') {
1620 if (found ==
true) {
1621 func(ctx, arguments,
1624 for (
int i = 0; i < n_arguments; i++) {
1625 if (arguments[i].x != NULL) {
1626 free(arguments[i].x);
1627 arguments[i].
x = NULL;
1632 if (ctx->
out != stdout) {
1637 if (invalid_arg ==
true) {
1639 memset(arg, 0x0,
sizeof(arg));
1641 char *end = strchr(args,
',');
1643 arg[end - args] =
'\0';
1645 printf(
"Invalid argument %d: %s (%s)\n", i,
argv[i], arg);
1646 }
else if (command == NULL) {
1647 printf(
"Command %s%s%s not found\n",
argv[0], i ?
" " :
"",
1649 }
else if (*args !=
'\0' || argc - 1 == 0) {
1650 printf(
"Incomplete command\n");
1651 for (
int i = 0; i < argc; i++) {
1652 arguments[i].
s =
argv[i];
1661static int parse_configured_connectors(
yubihsm_context *ctx,
char **connectors,
1667 if (n_connectors == 0) {
1676 for (
int i = 0; i < n_connectors; i++) {
1704 int rc = EXIT_SUCCESS;
1706 struct gengetopt_args_info args_info;
1709 struct cmdline_parser_params
params;
1713 cmdline_parser_params_init(&
params);
1715 params.check_required = 0;
1717 if (cmdline_parser(argc,
argv, &args_info) != 0) {
1718 return EXIT_FAILURE;
1721 if (stat(args_info.config_file_arg, &sb) == 0) {
1722 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode)) {
1724 if (cmdline_parser_config_file(args_info.config_file_arg, &args_info,
1726 return EXIT_FAILURE;
1731 if (cmdline_parser_required(&args_info,
argv[0]) != 0) {
1732 return EXIT_FAILURE;
1735 if (parse_configured_connectors(&ctx, args_info.connector_arg,
1736 args_info.connector_given) == -1) {
1737 fprintf(stderr,
"Unable to parse connector list");
1742 if (getenv(
"DEBUG") != NULL) {
1749 fprintf(stderr,
"Failed to initialize libyubihsm\n");
1758 fprintf(stderr,
"Failed to initialize libykyh\n");
1769 fprintf(stderr,
"Failed to allocate memory\n");
1776 fprintf(stderr,
"Failed to allocate memory\n");
1784 if (args_info.cacert_given) {
1785 ctx.
cacert = strdup(args_info.cacert_arg);
1787 if (args_info.proxy_given) {
1788 ctx.
proxy = strdup(args_info.proxy_arg);
1792 struct sigaction act;
1793 memset(&act, 0,
sizeof(act));
1794 act.sa_handler = timer_handler;
1795 act.sa_flags = SA_RESTART;
1796 sigaction(SIGALRM, &act, NULL);
1800 sigaddset(&set, SIGALRM);
1801 sigprocmask(SIG_UNBLOCK, &set, NULL);
1804 if (args_info.action_given) {
1807 ctx.
out = open_file(args_info.out_arg,
false);
1808 if (ctx.
out == NULL) {
1809 fprintf(stderr,
"Unable to open output file %s\n", args_info.out_arg);
1816 bool requires_session =
false;
1817 for (
unsigned i = 0; i < args_info.action_given; i++) {
1818 switch (args_info.action_arg[i]) {
1819 case action_arg_getMINUS_deviceMINUS_info:
1820 requires_session =
false;
1824 requires_session =
true;
1827 if (requires_session ==
true) {
1834 if (requires_session ==
true) {
1835 arg[0].
w = args_info.authkey_arg;
1837 arg[1].
len =
sizeof(
buf);
1838 if (get_input_data(args_info.password_given ? args_info.password_arg :
"-",
1840 fprintf(stderr,
"Failed to get password\n");
1847 fprintf(stderr,
"Failed to open session\n");
1859 switch (args_info.informat_arg) {
1860 case informat_arg_base64:
1863 case informat_arg_binary:
1866 case informat_arg_PEM:
1869 case informat_arg_password:
1872 case informat_arg_hex:
1875 case informat__NULL:
1876 case informat_arg_default:
1882 switch (args_info.outformat_arg) {
1883 case outformat_arg_base64:
1886 case outformat_arg_binary:
1889 case outformat_arg_PEM:
1892 case outformat_arg_hex:
1895 case outformat__NULL:
1896 case outformat_arg_default:
1902 calling_device =
true;
1904 for (
unsigned i = 0; i < args_info.action_given; i++) {
1905 switch (args_info.action_arg[i]) {
1906 case action_arg_decryptMINUS_pkcs1v15: {
1907 arg[1].
w = args_info.object_id_arg;
1909 arg[2].
len =
sizeof(
buf);
1910 if (get_input_data(args_info.in_arg, arg[2].
x, &arg[2].
len,
1912 : ctx.
in_fmt) ==
false) {
1913 fprintf(stderr,
"Failed to get input data\n");
1924 case action_arg_deriveMINUS_ecdh: {
1925 arg[1].
w = args_info.object_id_arg;
1927 arg[2].
len =
sizeof(
buf);
1928 if (get_input_data(args_info.in_arg, arg[2].
x, &arg[2].
len,
1930 : ctx.
in_fmt) ==
false) {
1931 fprintf(stderr,
"Failed to get input data\n");
1941 case action_arg_decryptMINUS_oaep:
1942 case action_arg_decryptMINUS_aesccm:
1943 case action_arg_encryptMINUS_aesccm:
1946 case action_arg_generateMINUS_asymmetricMINUS_key: {
1947 if (args_info.algorithm_given == 0) {
1948 fprintf(stderr,
"Missing argument algorithm\n");
1953 arg[1].
w = args_info.object_id_arg;
1954 arg[2].
s = args_info.label_arg;
1955 arg[2].
len = strlen(args_info.label_arg);
1971 case action_arg_generateMINUS_hmacMINUS_key: {
1972 if (args_info.algorithm_given == 0) {
1973 fprintf(stderr,
"Missing argument algorithm\n");
1978 arg[1].
w = args_info.object_id_arg;
1979 arg[2].
s = args_info.label_arg;
1980 arg[2].
len = strlen(args_info.label_arg);
1996 case action_arg_generateMINUS_wrapMINUS_key: {
1997 if (args_info.algorithm_given == 0) {
1998 fprintf(stderr,
"Missing argument algorithm\n");
2003 if (args_info.delegated_given == 0) {
2004 fprintf(stderr,
"Missing delegated capabilities\n");
2009 arg[1].
w = args_info.object_id_arg;
2010 arg[2].
s = args_info.label_arg;
2011 arg[2].
len = strlen(args_info.label_arg);
2031 case action_arg_generateMINUS_otpMINUS_aeadMINUS_key: {
2032 if (args_info.algorithm_given == 0) {
2033 fprintf(stderr,
"Missing argument algorithm\n");
2038 if (args_info.nonce_given == 0) {
2039 fprintf(stderr,
"Missing argument nonce\n");
2044 arg[1].
w = args_info.object_id_arg;
2045 arg[2].
s = args_info.label_arg;
2046 arg[2].
len = strlen(args_info.label_arg);
2058 arg[6].
d = args_info.nonce_arg;
2064 case action_arg_getMINUS_opaque: {
2065 arg[1].
w = args_info.object_id_arg;
2073 case action_arg_getMINUS_pseudoMINUS_random: {
2074 arg[1].
w = args_info.count_arg;
2082 case action_arg_getMINUS_storageMINUS_info:
2087 case action_arg_getMINUS_publicMINUS_key: {
2088 arg[1].
w = args_info.object_id_arg;
2089 arg[2].
s = args_info.out_arg;
2090 arg[2].
len = strlen(args_info.out_arg);
2098 case action_arg_getMINUS_objectMINUS_info: {
2099 if (args_info.object_type_given == 0) {
2100 fprintf(stderr,
"Missing argument object type\n");
2105 arg[1].
w = args_info.object_id_arg;
2113 case action_arg_getMINUS_wrapped: {
2114 if (args_info.object_type_given == 0) {
2115 fprintf(stderr,
"Missing argument object-type\n");
2120 if (args_info.wrap_id_given == 0) {
2121 fprintf(stderr,
"Missing argument wrap-id\n");
2126 arg[1].
w = args_info.object_id_arg;
2130 arg[3].
w = args_info.wrap_id_arg;
2132 arg[4].
s = args_info.out_arg;
2133 arg[4].
len = strlen(args_info.out_arg);
2141 case action_arg_getMINUS_deviceMINUS_info:
2146 case action_arg_getMINUS_template: {
2147 arg[1].
w = args_info.object_id_arg;
2155 case action_arg_listMINUS_objects: {
2156 if (args_info.algorithm_given == 0) {
2157 fprintf(stderr,
"Missing argument algorithm\n");
2162 if (args_info.object_type_given == 0) {
2163 fprintf(stderr,
"Missing argument object-type\n");
2168 arg[1].
w = args_info.object_id_arg;
2183 arg[6].
s = args_info.label_arg;
2184 arg[6].
len = strlen(args_info.label_arg);
2190 case action_arg_putMINUS_authenticationMINUS_key: {
2191 if (args_info.new_password_given == 0) {
2192 fprintf(stderr,
"Missing argument new-password\n");
2197 arg[1].
w = args_info.object_id_arg;
2198 arg[2].
s = args_info.label_arg;
2199 arg[2].
len = strlen(args_info.label_arg);
2212 arg[6].
x = (
uint8_t *) args_info.new_password_arg;
2213 arg[6].
len = strlen(args_info.new_password_arg);
2219 case action_arg_putMINUS_asymmetricMINUS_key: {
2220 arg[1].
w = args_info.object_id_arg;
2221 arg[2].
s = args_info.label_arg;
2222 arg[2].
len = strlen(args_info.label_arg);
2233 arg[5].
len =
sizeof(
buf);
2234 if (get_input_data(args_info.in_arg, arg[5].
x, &arg[5].
len,
2237 fprintf(stderr,
"Failed to get input data\n");
2245 case action_arg_putMINUS_opaque: {
2246 if (args_info.algorithm_given == 0) {
2247 fprintf(stderr,
"Missing argument algorithm\n");
2252 arg[1].
w = args_info.object_id_arg;
2253 arg[2].
s = args_info.label_arg;
2254 arg[2].
len = strlen(args_info.label_arg);
2268 arg[6].
len =
sizeof(
buf);
2269 if (get_input_data(args_info.in_arg, arg[6].
x, &arg[6].
len,
2271 : ctx.
in_fmt) ==
false) {
2272 fprintf(stderr,
"Failed to get input data\n");
2281 case action_arg_setMINUS_option:
2284 case action_arg_putMINUS_hmacMINUS_key:
2288 case action_arg_putMINUS_wrapMINUS_key: {
2290 if (args_info.delegated_given == 0) {
2291 fprintf(stderr,
"Missing delegated capabilities\n");
2296 arg[1].
w = args_info.object_id_arg;
2298 arg[2].
s = args_info.label_arg;
2299 arg[2].
len = strlen(args_info.label_arg);
2314 arg[6].
len =
sizeof(
buf);
2315 if (get_input_data(args_info.in_arg, arg[6].
x, &arg[6].
len,
2318 fprintf(stderr,
"Failed to get input data\n");
2327 case action_arg_putMINUS_wrapped: {
2328 if (args_info.wrap_id_given == 0) {
2329 fprintf(stderr,
"Missing argument wrap-id\n");
2334 arg[1].
w = args_info.wrap_id_arg;
2336 arg[2].
len =
sizeof(
buf);
2337 if (get_input_data(args_info.in_arg, arg[2].
x, &arg[2].
len,
2339 : ctx.
in_fmt) ==
false) {
2340 fprintf(stderr,
"Failed to get input data\n");
2349 case action_arg_putMINUS_template: {
2350 if (args_info.algorithm_given == 0) {
2351 fprintf(stderr,
"Missing argument algorithm\n");
2356 arg[1].
w = args_info.object_id_arg;
2357 arg[2].
s = args_info.label_arg;
2358 arg[2].
len = strlen(args_info.label_arg);
2372 arg[6].
len =
sizeof(
buf);
2373 if (get_input_data(args_info.in_arg, arg[6].
x, &arg[6].
len,
2375 : ctx.
in_fmt) ==
false) {
2376 fprintf(stderr,
"Failed to get input data\n");
2385 case action_arg_putMINUS_otpMINUS_aeadMINUS_key:
2389 case action_arg_signMINUS_eddsa:
2390 case action_arg_signMINUS_ecdsa: {
2391 if (args_info.algorithm_given == 0) {
2392 fprintf(stderr,
"Missing argument algorithm\n");
2397 arg[1].
w = args_info.object_id_arg;
2402 arg[3].
len =
sizeof(
buf);
2403 if (get_input_data(args_info.in_arg, arg[3].
x, &arg[3].
len,
2405 : ctx.
in_fmt) ==
false) {
2406 fprintf(stderr,
"Failed to get input data\n");
2411 if (args_info.action_arg[i] == action_arg_signMINUS_ecdsa) {
2424 case action_arg_signMINUS_pkcs1v15: {
2425 if (args_info.algorithm_given == 0) {
2426 fprintf(stderr,
"Missing argument algorithm\n");
2431 arg[1].
w = args_info.object_id_arg;
2436 arg[3].
len =
sizeof(
buf);
2437 if (get_input_data(args_info.in_arg, arg[3].
x, &arg[3].
len,
2439 : ctx.
in_fmt) ==
false) {
2440 fprintf(stderr,
"Failed to get input data\n");
2451 case action_arg_signMINUS_pss: {
2452 if (args_info.algorithm_given == 0) {
2453 fprintf(stderr,
"Missing argument algorithm\n");
2458 arg[1].
w = args_info.object_id_arg;
2463 arg[3].
len =
sizeof(
buf);
2464 if (get_input_data(args_info.in_arg, arg[3].
x, &arg[3].
len,
2466 : ctx.
in_fmt) ==
false) {
2467 fprintf(stderr,
"Failed to get input data\n");
2478 case action_arg_signMINUS_hmac:
2482 case action_arg_reset: {
2487 case action_arg_deleteMINUS_object: {
2488 if (args_info.object_type_given == 0) {
2489 fprintf(stderr,
"Missing argument object type\n");
2496 arg[1].
w = args_info.object_id_arg;
2502 case action_arg_signMINUS_sshMINUS_certificate: {
2503 if (args_info.template_id_given == 0) {
2504 fprintf(stderr,
"Missing argument template-id\n");
2509 if (args_info.algorithm_given == 0) {
2510 fprintf(stderr,
"Missing argument algorithm\n");
2515 arg[1].
w = args_info.object_id_arg;
2516 arg[2].
w = args_info.template_id_arg;
2521 arg[4].
len =
sizeof(
buf);
2522 if (get_input_data(args_info.in_arg, arg[4].
x, &arg[4].
len,
2526 fprintf(stderr,
"Failed to get input data\n");
2538 case action_arg_benchmark:
2541 case action_arg_createMINUS_otpMINUS_aead:
2545 case action_arg_randomizeMINUS_otpMINUS_aead: {
2546 arg[1].
w = args_info.object_id_arg;
2547 arg[2].
s = args_info.out_arg;
2548 arg[2].
len = strlen(args_info.out_arg);
2557 case action_arg_decryptMINUS_otp:
2561 case action_arg_signMINUS_attestationMINUS_certificate: {
2562 if (args_info.attestation_id_given == 0) {
2563 fprintf(stderr,
"Missing argument attestation-id\n");
2568 arg[1].
w = args_info.object_id_arg;
2569 arg[2].
w = args_info.attestation_id_arg;
2578 case action_arg_getMINUS_logs: {
2585 case action_arg_setMINUS_logMINUS_index: {
2586 if (args_info.log_index_given == 0) {
2587 fprintf(stderr,
"Missing argument log-index\n");
2592 arg[1].
w = args_info.log_index_arg;
2600 case action_arg_blinkMINUS_device: {
2601 if(args_info.duration_arg < 0 || args_info.duration_arg > 0xff) {
2602 fprintf(stderr,
"Duration must be in [0, 256]\n");
2607 arg[1].
w = args_info.duration_arg;
2614 printf(
"ERROR !%u \n", args_info.action_given);
2618 if (
rc == EXIT_FAILURE) {
2623 calling_device =
false;
2625 if (requires_session ==
true) {
2638 history(
g_hist, &ev, H_SETSIZE, 100);
2640 el = el_init(*
argv, stdin, stdout, stderr);
2642 el_set(el, EL_EDITOR,
"emacs");
2645 el_set(el, EL_PROMPT_ESC, prompt,
'\1');
2647 el_set(el, EL_PROMPT, prompt);
2650 el_set(el, EL_HIST, history,
g_hist);
2653 el_set(el, EL_ADDFN,
"yh_complete",
"Complete argument",
yubihsm_complete);
2656 el_set(el, EL_BIND,
"^I",
"yh_complete", NULL);
2658 el_source(el, NULL);
2667 char *
buf = fgets(data,
sizeof(data), stdin);
2672 const char *
buf = el_gets(el, &num);
2678 fprintf(stdout,
"\n");
2679 }
else if (num > 0 &&
buf[0] !=
'\n' &&
buf[0] !=
'\r') {
2683 calling_device =
true;
2685 calling_device =
false;
2697 cmdline_parser_free(&args_info);
2699 if (ctx.
out != stdout && ctx.
out != NULL) {
Catch::Generators::GeneratorWrapper< std::string > lines(std::string)
int yh_com_sign_ssh_certificate(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_change_authentication_key(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_put_asymmetric(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_derive_ecdh(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_encrypt_aesccm(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_generate_otp_aead_key(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_blink(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_sign_pss(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_open_session(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_set_proxy(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_list_objects(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_generate_hmac(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_put_opaque(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_decrypt_oaep(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_noop(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_put_authentication(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_audit(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_get_storage(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_generate_wrap(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_decrypt_aesccm(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_otp_aead_random(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_reset(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_list_types(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_put_wrapped(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_debug_info(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_put_otp_aead_key(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_hmac(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_connect(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_pecho(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_sign_ecdsa(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_sign_pkcs1v1_5(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_generate_asymmetric(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_get_pubkey(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_get_option(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_close_session(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_put_wrapkey(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_list_capabilities(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_list_sessions(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_get_wrapped(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_disconnect(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_debug_raw(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_debug_none(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_sign_eddsa(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_get_template(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_list_algorithms(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_get_device_info(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_put_template(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_delete(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_debug_intermediate(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_otp_decrypt(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_get_opaque(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_debug_all(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_set_cacert(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_benchmark(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_put_option(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_otp_aead_create(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_decrypt_pkcs1v1_5(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_set_log_index(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_put_hmac(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_debug_error(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_sign_attestation_certificate(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_get_random(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_debug_crypto(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_echo(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_get_object_info(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int CommandFunction(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
struct sysio::chain::eosvmoc::code_cache_header __attribute__((packed))
lib::function< void(lib::error_code const &)> timer_handler
The type and signature of the callback passed to the read method.
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
CommandList msort_list(CommandList list)
int yh_com_help(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
#define LIB_SUCCEED_OR_DIE(x, s)
int tokenize(char *line, char **toks, int max_toks, int *cursorc, int *cursoro, const char *space)
int validate_and_call(yubihsm_context *ctx, CommandList l, const char *line)
unsigned char complete_command(EditLine *el, Command *to_complete, const char *line, int cursoro)
void create_command_list(CommandList *c)
#define COMPLETION_CANDIDATES
int validate_arg(yubihsm_context *ctx, char type, const char *value, Argument *parsed, cmd_format fmt)
int yh_com_keepalive_on(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_set_outformat(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
unsigned char complete_arg(EditLine *el, const char *arg, char *line, int cursoro)
int yh_com_keepalive_off(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_quit(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
int yh_com_set_informat(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
#define LOCAL_CONNECTOR_URL
#define COM_SUCCEED_OR_DIE(x, s)
int yh_com_history(yubihsm_context *ctx, Argument *argv, cmd_format fmt)
unsigned char yubihsm_complete(EditLine *el, int ch)
Command * register_command(CommandList list, Command command)
void register_subcommand(Command *parent, Command command)
void find_lcp(const char *items[], int n_items, const char **lcp, int *lcp_len)
Capabilities representation.
yh_session * sessions[YH_MAX_SESSIONS]
account_query_db::get_accounts_by_authorizers_params params
bool base64_decode(const char *in, uint8_t *out, size_t *len)
bool read_file(FILE *fp, uint8_t *buf, size_t *buf_len)
bool hex_decode(const char *in, uint8_t *out, size_t *len)
ykyh_rc ykyh_done(ykyh_state *state)
ykyh_rc ykyh_init(ykyh_state **state, int verbose)
yh_rc yh_destroy_session(yh_session **session)
yh_rc yh_set_verbosity(yh_connector *connector, uint8_t verbosity)
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)
yh_rc yh_string_to_option(const char *string, yh_option *option)
yh_rc yh_string_to_type(const char *string, yh_object_type *type)
yh_rc yh_util_close_session(yh_session *session)
yh_rc yh_string_to_domains(const char *domains, uint16_t *result)
yh_rc yh_string_to_capabilities(const char *capability, yh_capabilities *result)
yh_rc yh_string_to_algo(const char *string, yh_algorithm *algo)
#define YH_VERB_ALL
Debug level all. All previous options enabled.
#define YH_MSG_BUF_SIZE
Maximum length of message buffer.
#define YH_MAX_SESSIONS
Max sessions the device may hold.
@ YHR_GENERIC_ERROR
Return value when encountering an unknown error.
@ YHR_SUCCESS
Returned value when function was successful.
memset(pInfo->slotDescription, ' ', 64)
memcpy((char *) pInfo->slotDescription, s, l)