1699 {
1700
1702 int comrc;
1703
1704 int rc = EXIT_SUCCESS;
1705
1706 struct gengetopt_args_info args_info;
1707
1708 struct stat sb;
1709 struct cmdline_parser_params
params;
1710
1712
1713 cmdline_parser_params_init(&
params);
1715 params.check_required = 0;
1716
1717 if (cmdline_parser(argc,
argv, &args_info) != 0) {
1718 return EXIT_FAILURE;
1719 }
1720
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;
1727 }
1728 }
1729 }
1730
1731 if (cmdline_parser_required(&args_info,
argv[0]) != 0) {
1732 return EXIT_FAILURE;
1733 }
1734
1735 if (parse_configured_connectors(&ctx, args_info.connector_arg,
1736 args_info.connector_given) == -1) {
1737 fprintf(stderr, "Unable to parse connector list");
1739 goto main_exit;
1740 }
1741
1742 if (getenv("DEBUG") != NULL) {
1744 }
1746
1749 fprintf(stderr, "Failed to initialize libyubihsm\n");
1751 goto main_exit;
1752 }
1753
1754#ifdef USE_YKYH
1758 fprintf(stderr, "Failed to initialize libykyh\n");
1760 goto main_exit;
1761 }
1762#endif
1763
1766
1769 fprintf(stderr, "Failed to allocate memory\n");
1771 goto main_exit;
1772 }
1773
1776 fprintf(stderr, "Failed to allocate memory\n");
1778 goto main_exit;
1779 }
1780
1782 }
1783
1784 if (args_info.cacert_given) {
1785 ctx.
cacert = strdup(args_info.cacert_arg);
1786 }
1787 if (args_info.proxy_given) {
1788 ctx.
proxy = strdup(args_info.proxy_arg);
1789 }
1790
1791#ifndef __WIN32
1792 struct sigaction act;
1793 memset(&act, 0,
sizeof(act));
1795 act.sa_flags = SA_RESTART;
1796 sigaction(SIGALRM, &act, NULL);
1797
1799 sigemptyset(&set);
1800 sigaddset(&set, SIGALRM);
1801 sigprocmask(SIG_UNBLOCK, &set, NULL);
1802#endif
1803
1804 if (args_info.action_given) {
1806
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);
1811 goto main_exit;
1812 }
1813
1815
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;
1821 break;
1822
1823 default:
1824 requires_session = true;
1825 }
1826
1827 if (requires_session == true) {
1828 break;
1829 }
1830 }
1831
1833
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");
1842 goto main_exit;
1843 }
1844
1846 if (comrc != 0) {
1847 fprintf(stderr, "Failed to open session\n");
1849 goto main_exit;
1850 }
1851 }
1852
1856 }
1857 }
1858
1859 switch (args_info.informat_arg) {
1860 case informat_arg_base64:
1862 break;
1863 case informat_arg_binary:
1865 break;
1866 case informat_arg_PEM:
1868 break;
1869 case informat_arg_password:
1871 break;
1872 case informat_arg_hex:
1874 break;
1875 case informat__NULL:
1876 case informat_arg_default:
1877 default:
1879 break;
1880 }
1881
1882 switch (args_info.outformat_arg) {
1883 case outformat_arg_base64:
1885 break;
1886 case outformat_arg_binary:
1888 break;
1889 case outformat_arg_PEM:
1891 break;
1892 case outformat_arg_hex:
1894 break;
1895 case outformat__NULL:
1896 case outformat_arg_default:
1897 default:
1899 break;
1900 }
1901
1902 calling_device = true;
1903
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");
1915 break;
1916 }
1917 comrc =
1922 } break;
1923
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");
1933 break;
1934 }
1939 } break;
1940
1941 case action_arg_decryptMINUS_oaep:
1942 case action_arg_decryptMINUS_aesccm:
1943 case action_arg_encryptMINUS_aesccm:
1945
1946 case action_arg_generateMINUS_asymmetricMINUS_key: {
1947 if (args_info.algorithm_given == 0) {
1948 fprintf(stderr, "Missing argument algorithm\n");
1950 break;
1951 }
1952
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);
1958
1963
1966
1969 } break;
1970
1971 case action_arg_generateMINUS_hmacMINUS_key: {
1972 if (args_info.algorithm_given == 0) {
1973 fprintf(stderr, "Missing argument algorithm\n");
1975 break;
1976 }
1977
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);
1983
1988
1991
1994 } break;
1995
1996 case action_arg_generateMINUS_wrapMINUS_key: {
1997 if (args_info.algorithm_given == 0) {
1998 fprintf(stderr, "Missing argument algorithm\n");
2000 break;
2001 }
2002
2003 if (args_info.delegated_given == 0) {
2004 fprintf(stderr, "Missing delegated capabilities\n");
2006 break;
2007 }
2008
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);
2014
2019
2022
2026
2029 } break;
2030
2031 case action_arg_generateMINUS_otpMINUS_aeadMINUS_key: {
2032 if (args_info.algorithm_given == 0) {
2033 fprintf(stderr, "Missing argument algorithm\n");
2035 break;
2036 }
2037
2038 if (args_info.nonce_given == 0) {
2039 fprintf(stderr, "Missing argument nonce\n");
2041 break;
2042 }
2043
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);
2049
2054
2057
2058 arg[6].
d = args_info.nonce_arg;
2059
2062 } break;
2063
2064 case action_arg_getMINUS_opaque: {
2065 arg[1].
w = args_info.object_id_arg;
2066
2071 } break;
2072
2073 case action_arg_getMINUS_pseudoMINUS_random: {
2074 arg[1].
w = args_info.count_arg;
2075
2076 comrc =
2080 } break;
2081
2082 case action_arg_getMINUS_storageMINUS_info:
2085 break;
2086
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);
2091
2092 comrc =
2096 } break;
2097
2098 case action_arg_getMINUS_objectMINUS_info: {
2099 if (args_info.object_type_given == 0) {
2100 fprintf(stderr, "Missing argument object type\n");
2102 break;
2103 }
2104
2105 arg[1].
w = args_info.object_id_arg;
2108
2111 } break;
2112
2113 case action_arg_getMINUS_wrapped: {
2114 if (args_info.object_type_given == 0) {
2115 fprintf(stderr, "Missing argument object-type\n");
2117 break;
2118 }
2119
2120 if (args_info.wrap_id_given == 0) {
2121 fprintf(stderr, "Missing argument wrap-id\n");
2123 break;
2124 }
2125
2126 arg[1].
w = args_info.object_id_arg;
2129
2130 arg[3].
w = args_info.wrap_id_arg;
2131
2132 arg[4].
s = args_info.out_arg;
2133 arg[4].
len = strlen(args_info.out_arg);
2134
2139 } break;
2140
2141 case action_arg_getMINUS_deviceMINUS_info:
2144 break;
2145
2146 case action_arg_getMINUS_template: {
2147 arg[1].
w = args_info.object_id_arg;
2148
2153 } break;
2154
2155 case action_arg_listMINUS_objects: {
2156 if (args_info.algorithm_given == 0) {
2157 fprintf(stderr, "Missing argument algorithm\n");
2159 break;
2160 }
2161
2162 if (args_info.object_type_given == 0) {
2163 fprintf(stderr, "Missing argument object-type\n");
2165 break;
2166 }
2167
2168 arg[1].
w = args_info.object_id_arg;
2171
2174
2179
2182
2183 arg[6].
s = args_info.label_arg;
2184 arg[6].
len = strlen(args_info.label_arg);
2185
2188 } break;
2189
2190 case action_arg_putMINUS_authenticationMINUS_key: {
2191 if (args_info.new_password_given == 0) {
2192 fprintf(stderr, "Missing argument new-password\n");
2194 break;
2195 }
2196
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);
2202
2207
2211
2212 arg[6].
x = (
uint8_t *) args_info.new_password_arg;
2213 arg[6].
len = strlen(args_info.new_password_arg);
2214
2217 } break;
2218
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);
2223
2226
2231
2233 arg[5].
len =
sizeof(
buf);
2234 if (get_input_data(args_info.in_arg, arg[5].x, &arg[5].len,
2236 false) {
2237 fprintf(stderr, "Failed to get input data\n");
2239 break;
2240 }
2243 } break;
2244
2245 case action_arg_putMINUS_opaque: {
2246 if (args_info.algorithm_given == 0) {
2247 fprintf(stderr, "Missing argument algorithm\n");
2249 break;
2250 }
2251
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);
2255
2258
2263
2266
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");
2274 break;
2275 }
2276
2279 } break;
2280
2281 case action_arg_setMINUS_option:
2283
2284 case action_arg_putMINUS_hmacMINUS_key:
2286
2287
2288 case action_arg_putMINUS_wrapMINUS_key: {
2289
2290 if (args_info.delegated_given == 0) {
2291 fprintf(stderr, "Missing delegated capabilities\n");
2293 break;
2294 }
2295
2296 arg[1].
w = args_info.object_id_arg;
2297
2298 arg[2].
s = args_info.label_arg;
2299 arg[2].
len = strlen(args_info.label_arg);
2300
2303
2308
2312
2314 arg[6].
len =
sizeof(
buf);
2315 if (get_input_data(args_info.in_arg, arg[6].x, &arg[6].len,
2317 false) {
2318 fprintf(stderr, "Failed to get input data\n");
2320 break;
2321 }
2322
2325 } break;
2326
2327 case action_arg_putMINUS_wrapped: {
2328 if (args_info.wrap_id_given == 0) {
2329 fprintf(stderr, "Missing argument wrap-id\n");
2331 break;
2332 }
2333
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");
2342 break;
2343 }
2344
2347 } break;
2348
2349 case action_arg_putMINUS_template: {
2350 if (args_info.algorithm_given == 0) {
2351 fprintf(stderr, "Missing argument algorithm\n");
2353 break;
2354 }
2355
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);
2359
2362
2367
2370
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");
2378 break;
2379 }
2380
2383 } break;
2384
2385 case action_arg_putMINUS_otpMINUS_aeadMINUS_key:
2387
2388
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");
2394 break;
2395 }
2396
2397 arg[1].
w = args_info.object_id_arg;
2400
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");
2408 break;
2409 }
2410
2411 if (args_info.action_arg[i] == action_arg_signMINUS_ecdsa) {
2415 } else {
2419 }
2420
2422 } break;
2423
2424 case action_arg_signMINUS_pkcs1v15: {
2425 if (args_info.algorithm_given == 0) {
2426 fprintf(stderr, "Missing argument algorithm\n");
2428 break;
2429 }
2430
2431 arg[1].
w = args_info.object_id_arg;
2434
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");
2442 break;
2443 }
2444
2449 } break;
2450
2451 case action_arg_signMINUS_pss: {
2452 if (args_info.algorithm_given == 0) {
2453 fprintf(stderr, "Missing argument algorithm\n");
2455 break;
2456 }
2457
2458 arg[1].
w = args_info.object_id_arg;
2461
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");
2469 break;
2470 }
2471
2476 } break;
2477
2478 case action_arg_signMINUS_hmac:
2480
2481
2482 case action_arg_reset: {
2485 } break;
2486
2487 case action_arg_deleteMINUS_object: {
2488 if (args_info.object_type_given == 0) {
2489 fprintf(stderr, "Missing argument object type\n");
2491 break;
2492 }
2493
2496 arg[1].
w = args_info.object_id_arg;
2497
2500 } break;
2501
2502 case action_arg_signMINUS_sshMINUS_certificate: {
2503 if (args_info.template_id_given == 0) {
2504 fprintf(stderr, "Missing argument template-id\n");
2506 break;
2507 }
2508
2509 if (args_info.algorithm_given == 0) {
2510 fprintf(stderr, "Missing argument algorithm\n");
2512 break;
2513 }
2514
2515 arg[1].
w = args_info.object_id_arg;
2516 arg[2].
w = args_info.template_id_arg;
2519
2521 arg[4].
len =
sizeof(
buf);
2522 if (get_input_data(args_info.in_arg, arg[4].x, &arg[4].len,
2525 false) {
2526 fprintf(stderr, "Failed to get input data\n");
2528 break;
2529 }
2530
2531 comrc =
2536 } break;
2537
2538 case action_arg_benchmark:
2540
2541 case action_arg_createMINUS_otpMINUS_aead:
2543
2544
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);
2549
2550 comrc =
2555 } break;
2556
2557 case action_arg_decryptMINUS_otp:
2559
2560
2561 case action_arg_signMINUS_attestationMINUS_certificate: {
2562 if (args_info.attestation_id_given == 0) {
2563 fprintf(stderr, "Missing argument attestation-id\n");
2565 break;
2566 }
2567
2568 arg[1].
w = args_info.object_id_arg;
2569 arg[2].
w = args_info.attestation_id_arg;
2570
2576 } break;
2577
2578 case action_arg_getMINUS_logs: {
2579 comrc =
2583 } break;
2584
2585 case action_arg_setMINUS_logMINUS_index: {
2586 if (args_info.log_index_given == 0) {
2587 fprintf(stderr, "Missing argument log-index\n");
2589 break;
2590 }
2591
2592 arg[1].
w = args_info.log_index_arg;
2593
2598 } break;
2599
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");
2604 break;
2605 }
2606
2607 arg[1].
w = args_info.duration_arg;
2608
2611 } break;
2612
2613 case action__NULL:
2614 printf(
"ERROR !%u \n", args_info.action_given);
2616 }
2617
2618 if (
rc == EXIT_FAILURE) {
2619 break;
2620 }
2621 }
2622
2623 calling_device = false;
2624
2625 if (requires_session == true) {
2627 }
2628
2629 } else {
2631#ifndef __WIN32
2632 EditLine *el;
2633
2634 HistEvent ev;
2635
2637
2638 history(
g_hist, &ev, H_SETSIZE, 100);
2639
2640 el = el_init(*
argv, stdin, stdout, stderr);
2641
2642 el_set(el, EL_EDITOR, "emacs");
2643
2644#ifdef EL_PROMPT_ESC
2645 el_set(el, EL_PROMPT_ESC, prompt, '\1');
2646#else
2647 el_set(el, EL_PROMPT, prompt);
2648#endif
2649
2650 el_set(el, EL_HIST, history,
g_hist);
2651
2652
2653 el_set(el, EL_ADDFN,
"yh_complete",
"Complete argument",
yubihsm_complete);
2654
2655
2656 el_set(el, EL_BIND, "^I", "yh_complete", NULL);
2657
2658 el_source(el, NULL);
2659#endif
2660
2662
2664#ifdef __WIN32
2666 char data[1025];
2667 char *
buf = fgets(data,
sizeof(data), stdin);
2670 }
2671#else
2672 const char *
buf = el_gets(el, &num);
2673#endif
2674
2676
2678 fprintf(stdout, "\n");
2679 }
else if (num > 0 &&
buf[0] !=
'\n' &&
buf[0] !=
'\r') {
2680#ifndef __WIN32
2682#endif
2683 calling_device = true;
2685 calling_device = false;
2686 }
2687 }
2688
2689#ifndef __WIN32
2690 el_end(el);
2692#endif
2693 }
2694
2695main_exit:
2696
2697 cmdline_parser_free(&args_info);
2698
2699 if (ctx.
out != stdout && ctx.
out != NULL) {
2701 }
2702
2705 }
2708 }
2709
2711
2712#ifdef USE_YKYH
2715#endif
2716
2718}
lib::function< void(lib::error_code const &)> timer_handler
The type and signature of the callback passed to the read method.
#define LIB_SUCCEED_OR_DIE(x, s)
int validate_and_call(yubihsm_context *ctx, CommandList l, const char *line)
void create_command_list(CommandList *c)
#define LOCAL_CONNECTOR_URL
#define COM_SUCCEED_OR_DIE(x, s)
unsigned char yubihsm_complete(EditLine *el, int ch)
Capabilities representation.
yh_session * sessions[YH_MAX_SESSIONS]
account_query_db::get_accounts_by_authorizers_params params
ykyh_rc ykyh_done(ykyh_state *state)
ykyh_rc ykyh_init(ykyh_state **state, int verbose)
yh_rc yh_set_verbosity(yh_connector *connector, uint8_t verbosity)
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_MAX_SESSIONS
Max sessions the device may hold.
@ YHR_GENERIC_ERROR
Return value when encountering an unknown error.
memset(pInfo->slotDescription, ' ', 64)