Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
2946 lines (2498 loc) · 96.1 KB
/
Copy pathmain.c
File metadata and controls
2946 lines (2498 loc) · 96.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2015-2018 Yubico AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <yubihsm.h>
#include <errno.h>
#include <limits.h>
#include <locale.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "util.h"
#include "parsing.h"
#include "commands.h"
#include "insecure_memzero.h"
#include <openssl/evp.h>
//#include <ctype.h>
#define PROMPT "yubihsm> "
#define ARGS_BUFFER_SIZE 4096
#define COMPLETION_CANDIDATES 256
#define MAX_COMMAND_NAME 32
#define MAX_ARGUMENTS 32
#define LOCAL_CONNECTOR_URL "http://127.0.0.1:12345"
#include "yubihsm-shell.h"
#define SPACES " \f\n\r\t\v"
#ifdef __WIN32
#include <windows.h>
#include <fcntl.h>
#include <io.h>
// TODO: cheat on windows, cheat better?
#define S_ISLNK S_ISREG
#else
#include <strings.h>
#include <unistd.h>
#include <sys/time.h>
#include <editline/readline.h>
#include <histedit.h>
History *g_hist;
#endif
#ifdef _MSVC
#define S_ISREG(m) (((m) &S_IFMT) == S_IFREG)
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#endif
#define UNUSED(x) (void) (x)
#define LIB_SUCCEED_OR_DIE(x, s) \
if ((x) != YHR_SUCCESS) { \
fprintf(stderr, s "%s\n", yh_strerror(x)); \
rc = EXIT_FAILURE; \
break; \
}
#define COM_SUCCEED_OR_DIE(x, s) \
if ((x) != 0) { \
fprintf(stderr, s "\n"); \
rc = EXIT_FAILURE; \
break; \
}
static bool calling_device = false;
static yubihsm_context ctx = {0};
int yh_com_help(yubihsm_context *ctx, Argument *argv, cmd_format in_fmt,
cmd_format fmt);
int yh_com_history(yubihsm_context *ctx, Argument *argv, cmd_format in_fmt,
cmd_format fmt);
int yh_com_quit(yubihsm_context *ctx, Argument *argv, cmd_format in_fmt,
cmd_format fmt);
int yh_com_set_informat(yubihsm_context *ctx, Argument *argv, cmd_format in_fmt,
cmd_format fmt);
int yh_com_set_outformat(yubihsm_context *ctx, Argument *argv,
cmd_format in_fmt, cmd_format fmt);
typedef struct Command Command;
// NOTE(adma): supported values for the argument list are as follow
// u: unsigned integer (either decimal or hex if starts with 0x)
// w: unsigned short
// b: unsigned byte
// f: out filename
// F: in filename
// s: string
// e: session
struct Command {
char *name; /* Command name to match against */
CommandFunction *func; /* Documentation for the function */
char *args; /* Argument string */
cmd_format in_fmt; /* Default input format for command */
cmd_format out_fmt; /* Default output format for command */
char *doc; /* Function to call to do the job */
Command *subcommands; /* List of subcommands */
Command *next; /* Pointer to next command */
};
typedef Command *CommandList;
// NOTE(adma): push command to list and return the new head
Command *register_command(CommandList list, Command command) {
Command *c = calloc(1, sizeof(Command));
if (c == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
exit(EXIT_FAILURE);
}
assert(strlen(command.name) <= MAX_COMMAND_NAME);
memcpy(c, &command, sizeof(Command));
c->next = list;
return c;
}
void register_subcommand(Command *parent, Command command) {
Command *c = malloc(sizeof(Command));
if (c == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
exit(EXIT_FAILURE);
}
memcpy(c, &command, sizeof(Command));
c->next = parent->subcommands;
parent->subcommands = c;
}
CommandList msort_list(CommandList list) {
Command *left;
Command *right;
Command *e;
int in_size;
int left_size;
int right_size;
// NOTE(adma): do nothing on an empty list
if (!list) {
return NULL;
}
in_size = 1;
while (1) {
Command *tail = NULL;
left = list;
list = NULL;
int n_merges = 0;
while (left != NULL) {
n_merges++;
right = left;
left_size = 0;
for (int i = 0; i < in_size; i++) {
left_size++;
right = right->next;
if (right == NULL) {
break;
}
}
right_size = in_size;
while (left_size > 0 || (right_size > 0 && right)) {
if (left_size == 0) {
e = right;
right = right->next;
right_size--;
} else if (right_size == 0 || !right) {
e = left;
left = left->next;
left_size--;
} else if (strcmp(left->name, right->name) <= 0) {
e = left;
left = left->next;
left_size--;
} else {
e = right;
right = right->next;
right_size--;
}
if (tail) {
tail->next = e;
} else {
list = e;
}
tail = e;
}
left = right;
}
if (tail != NULL) {
tail->next = NULL;
}
if (n_merges <= 1) {
return list;
}
in_size *= 2;
}
}
void create_command_list(CommandList *c) {
// NOTE(adma): initialize
*c = NULL;
*c = register_command(*c, (Command){"audit", yh_com_noop, NULL, fmt_nofmt,
fmt_nofmt, "Deal with audit log", NULL,
NULL});
register_subcommand(*c, (Command){"get", yh_com_audit, "e:session,F:file=-",
fmt_ASCII, fmt_nofmt, "Extract log entries",
NULL, NULL});
register_subcommand(*c, (Command){"set", yh_com_set_log_index,
"e:session,w:index", fmt_nofmt, fmt_nofmt,
"Set the log index", NULL, NULL});
*c = register_command(*c, (Command){"connect", yh_com_connect, NULL,
fmt_nofmt, fmt_nofmt,
"Connect to a connector", NULL, NULL});
*c = register_command(*c, (Command){"debug", yh_com_noop, NULL, fmt_nofmt,
fmt_nofmt, "Change debug settings", NULL,
NULL});
register_subcommand(*c, (Command){"all", yh_com_debug_all, NULL, fmt_nofmt,
fmt_nofmt, "Enable all debug messages",
NULL, NULL});
register_subcommand(*c,
(Command){"crypto", yh_com_debug_crypto, NULL, fmt_nofmt,
fmt_nofmt, "Toggle crypto debug messages", NULL,
NULL});
register_subcommand(*c, (Command){"error", yh_com_debug_error, NULL,
fmt_nofmt, fmt_nofmt,
"Toggle error debug messages", NULL, NULL});
register_subcommand(*c, (Command){"info", yh_com_debug_info, NULL, fmt_nofmt,
fmt_nofmt, "Toggle info debug messages",
NULL, NULL});
register_subcommand(*c, (Command){"intermediate", yh_com_debug_intermediate,
NULL, fmt_nofmt, fmt_nofmt,
"Toggle intermediate debug messages", NULL,
NULL});
register_subcommand(*c, (Command){"none", yh_com_debug_none, NULL, fmt_nofmt,
fmt_nofmt, "Disable all debug messages",
NULL, NULL});
register_subcommand(*c, (Command){"raw", yh_com_debug_raw, NULL, fmt_nofmt,
fmt_nofmt, "Toggle raw debug messages",
NULL, NULL});
*c = register_command(*c, (Command){"decrypt", yh_com_noop, NULL, fmt_nofmt,
fmt_nofmt, "Decrypt data", NULL, NULL});
register_subcommand(*c, (Command){"pkcs1v1_5", yh_com_decrypt_pkcs1v1_5,
"e:session,w:key_id,i:data=-", fmt_binary,
fmt_base64,
"Decrypt data using RSAES-PKCS#1v1.5", NULL,
NULL});
register_subcommand(
*c,
(Command){"oaep", yh_com_decrypt_oaep,
"e:session,w:key_id,a:algorithm,i:data=-,s:label=", fmt_binary,
fmt_base64, "Decrypt data using RSAES-OAEP", NULL, NULL});
register_subcommand(*c,
(Command){"aesccm", yh_com_decrypt_aesccm,
"e:session,w:key_id,i:data=-", fmt_base64,
fmt_binary, "Decrypt data using Yubico-AES-CCM",
NULL, NULL});
*c = register_command(*c, (Command){"derive", yh_com_noop, NULL, fmt_nofmt,
fmt_nofmt, "Drive data", NULL, NULL});
register_subcommand(*c, (Command){"ecdh", yh_com_derive_ecdh,
"e:session,w:key_id,i:pubkey=-", fmt_PEM,
fmt_hex, "Perform a ECDH key exchange",
NULL, NULL});
*c = register_command(*c, (Command){"encrypt", yh_com_noop, NULL, fmt_nofmt,
fmt_nofmt, "Encrypt data", NULL, NULL});
register_subcommand(*c,
(Command){"aesccm", yh_com_encrypt_aesccm,
"e:session,w:key_id,i:data=-", fmt_binary,
fmt_base64, "Encrypt data using Yubico-AES-CCM",
NULL, NULL});
*c =
register_command(*c, (Command){"disconnect", yh_com_disconnect, NULL,
fmt_nofmt, fmt_nofmt,
"Disconnect from a connector", NULL, NULL});
*c =
register_command(*c,
(Command){"echo", yh_com_echo, "e:session,b:byte,w:count",
fmt_nofmt, fmt_nofmt,
"Send an ECHO command over a given session",
NULL, NULL});
*c = register_command(*c, (Command){"generate", yh_com_noop, NULL, fmt_nofmt,
fmt_nofmt, "Generate key", NULL, NULL});
register_subcommand(*c, (Command){"asymmetric", yh_com_generate_asymmetric,
"e:session,w:key_id,s:label,d:domains,c:"
"capabilities,a:algorithm",
fmt_nofmt, fmt_nofmt,
"Generate an asymmetric key", NULL, NULL});
register_subcommand(*c, (Command){"hmackey", yh_com_generate_hmac,
"e:session,w:key_id,s:label,d:domains,c:"
"capabilities,a:algorithm",
fmt_nofmt, fmt_nofmt, "Generate HMAC key",
NULL, NULL});
register_subcommand(
*c, (Command){"wrapkey", yh_com_generate_wrap,
"e:session,w:key_id,s:label,d:domains,c:"
"capabilities,c:delegated_capabilities,a:algorithm",
fmt_nofmt, fmt_nofmt, "Generate wrap key", NULL, NULL});
register_subcommand(*c, (Command){"otpaeadkey", yh_com_generate_otp_aead_key,
"e:session,w:key_id,s:label,d:domains,c:"
"capabilities,a:algorithm,u:nonce_id",
fmt_nofmt, fmt_nofmt,
"Generate OTP AEAD key", NULL, NULL});
*c = register_command(*c, (Command){"get", yh_com_noop, NULL, fmt_nofmt,
fmt_nofmt, "Retrieve data", NULL, NULL});
register_subcommand(*c, (Command){"opaque", yh_com_get_opaque,
"e:session,w:object_id,F:file=-", fmt_nofmt,
fmt_binary, "Get an opaque object", NULL,
NULL});
register_subcommand(*c, (Command){"option", yh_com_get_option,
"e:session,o:option", fmt_nofmt,
fmt_nofmt, // FIXME: output
"Get a global option value", NULL, NULL});
register_subcommand(*c,
(Command){"random", yh_com_get_random,
"e:session,w:count,F:out=-", fmt_nofmt, fmt_hex,
"Get pseudo-random bytes", NULL, NULL});
register_subcommand(*c, (Command){"storage", yh_com_get_storage, "e:session",
fmt_nofmt, fmt_nofmt, "Get storages stats",
NULL, NULL});
register_subcommand(*c, (Command){"pubkey", yh_com_get_pubkey,
"e:session,w:key_id,F:file=-", fmt_nofmt,
fmt_PEM, "Get a public key", NULL, NULL});
register_subcommand(*c,
(Command){"objectinfo", yh_com_get_object_info,
"e:session,w:id,t:type", fmt_nofmt, fmt_nofmt,
"Get information about an object", NULL, NULL});
register_subcommand(*c,
(Command){"wrapped", yh_com_get_wrapped,
"e:session,w:wrapkey_id,t:type,w:id,F:file=-",
fmt_nofmt, fmt_base64,
"Get an object under wrap", NULL, NULL});
register_subcommand(*c, (Command){"deviceinfo", yh_com_get_device_info, NULL,
fmt_nofmt, fmt_nofmt,
"Extract the version number, serial number "
"and supported algorithms",
NULL, NULL});
register_subcommand(*c, (Command){"template", yh_com_get_template,
"e:session,w:object_id,F:out=-", fmt_nofmt,
fmt_base64, "Get a template object", NULL,
NULL});
#ifdef USE_ASYMMETRIC_AUTH
register_subcommand(*c, (Command){"devicepubkey", yh_com_get_device_pubkey,
NULL, fmt_nofmt, fmt_PEM,
"Get the device public key for asymmetric "
"authentication",
NULL, NULL});
#endif
*c =
register_command(*c, (Command){"help", yh_com_help, "s:command=", fmt_nofmt,
fmt_nofmt, "Display help text", NULL, NULL});
*c =
register_command(*c, (Command){"history", yh_com_history, NULL, fmt_nofmt,
fmt_nofmt, "Display the command history",
NULL, NULL});
*c =
register_command(*c, (Command){"list", yh_com_noop, NULL, fmt_nofmt,
fmt_nofmt, "List information", NULL, NULL});
register_subcommand(*c, (Command){"capabilities", yh_com_list_capabilities,
NULL, fmt_nofmt, fmt_nofmt,
"Prints a list of possible capabilities",
NULL, NULL});
register_subcommand(*c, (Command){"algorithms", yh_com_list_algorithms, NULL,
fmt_nofmt, fmt_nofmt,
"Prints a list of possible algorithms",
NULL, NULL});
register_subcommand(*c,
(Command){"types", yh_com_list_types, NULL, fmt_nofmt,
fmt_nofmt, "Prints a list of possible types",
NULL, NULL});
register_subcommand(*c, (Command){"sessions", yh_com_list_sessions, NULL,
fmt_nofmt, fmt_nofmt,
"List the open session", NULL, NULL});
register_subcommand(*c, (Command){"objects", yh_com_list_objects,
"e:session,w:id=0,t:type=any,d:domains=0,c:"
"capabilities=0,a:algorithm=any,s:label=",
fmt_nofmt, fmt_nofmt,
"List objects according to filter", NULL,
NULL});
*c =
register_command(*c,
(Command){"plain", yh_com_noop, NULL, fmt_nofmt, fmt_nofmt,
"Send unencrypted and unauthenticated commands",
NULL, NULL});
register_subcommand(*c, (Command){"echo", yh_com_pecho, "b:byte,w:count",
fmt_nofmt, fmt_nofmt,
"Send a plain echo command", NULL, NULL});
*c = register_command(*c, (Command){"put", yh_com_noop, NULL, fmt_nofmt,
fmt_nofmt, "Store data", NULL, NULL});
register_subcommand(*c, (Command){"asymmetric", yh_com_put_asymmetric,
"e:session,w:key_id,s:label,d:domains,c:"
"capabilities,i:key=-",
fmt_PEM, fmt_nofmt,
"Store an asymmetric key", NULL, NULL});
register_subcommand(*c, (Command){"authkey", yh_com_put_authentication,
"e:session,w:key_id,s:label,d:domains,c:"
"capabilities,c:delegated_capabilities,i:"
"password=-",
fmt_password, fmt_nofmt,
"Store an authentication key", NULL, NULL});
#ifdef USE_ASYMMETRIC_AUTH
register_subcommand(*c,
(Command){"authkey_asym", yh_com_put_authentication_asym,
"e:session,w:key_id,s:label,d:domains,c:"
"capabilities,c:delegated_capabilities,i:"
"password=-",
fmt_password, fmt_nofmt,
"Store an asymmetric authentication key", NULL,
NULL});
#endif
register_subcommand(*c, (Command){"opaque", yh_com_put_opaque,
"e:session,w:object_id,s:label,d:domains,c:"
"capabilities,a:algorithm,i:data=-",
fmt_binary, fmt_nofmt,
"Store an opaque object", NULL, NULL});
register_subcommand(*c,
(Command){"option", yh_com_put_option,
"e:session,o:option,i:data", fmt_hex, fmt_nofmt,
"Set a global option value", NULL, NULL});
register_subcommand(*c, (Command){"hmackey", yh_com_put_hmac,
"e:session,w:key_id,s:label,d:domains,c:"
"capabilities,a:algorithm,i:key",
fmt_hex, fmt_nofmt, "Store a HMAC key",
NULL, NULL});
register_subcommand(*c,
(Command){"wrapkey", yh_com_put_wrapkey,
"e:session,w:key_id,s:label,d:domains,c:"
"capabilities,c:delegated_capabilities,i:key",
fmt_hex, fmt_nofmt, "Store a wrapping key",
NULL, NULL});
register_subcommand(*c, (Command){"wrapped", yh_com_put_wrapped,
"e:session,w:wrapkey_id,i:data=-",
fmt_base64, fmt_nofmt,
"Store a wrapped object", NULL, NULL});
register_subcommand(*c, (Command){"template", yh_com_put_template,
"e:session,w:object_id,s:label,d:domains,c:"
"capabilities,a:algorithm,i:data=-",
fmt_base64, fmt_nofmt,
"Store a template object", NULL, NULL});
register_subcommand(*c, (Command){"otpaeadkey", yh_com_put_otp_aead_key,
"e:session,w:key_id,s:label,d:domains,c:"
"capabilities,u:nonce_id,i:key",
fmt_hex, fmt_nofmt, "Store a OTP AEAD key",
NULL, NULL});
*c = register_command(*c,
(Command){"quit", yh_com_quit, NULL, fmt_nofmt,
fmt_nofmt, "Quit yubihsm-shell", NULL, NULL});
*c = register_command(*c,
(Command){"exit", yh_com_quit, NULL, fmt_nofmt,
fmt_nofmt, "Quit yubihsm-shell", NULL, NULL});
*c =
register_command(*c, (Command){"session", yh_com_noop, NULL, fmt_nofmt,
fmt_nofmt, "Manage sessions", NULL, NULL});
register_subcommand(*c, (Command){"close", yh_com_close_session, "e:session",
fmt_nofmt, fmt_nofmt,
"Close a session with a connector", NULL,
NULL});
register_subcommand(*c, (Command){"open", yh_com_open_session,
"w:authkey,i:password=-", fmt_password,
fmt_nofmt,
"Open a session with a device using a "
"specific Authentication Key",
NULL, NULL});
#ifdef USE_ASYMMETRIC_AUTH
register_subcommand(*c, (Command){"open_asym", yh_com_open_session_asym,
"w:authkey,i:password=-", fmt_password,
fmt_nofmt,
"Open a session with a device using a "
"specific Asymmetric Authentication Key",
NULL, NULL});
#endif
#ifdef YKHSMAUTH_ENABLED
register_subcommand(*c, (Command){"ykopen", yh_com_open_yksession,
"w:authkey,s:label,i:password=-",
fmt_password, fmt_nofmt,
"Open a session with a device using an "
"Authentication in a YubiKey",
NULL, NULL});
#endif
*c = register_command(*c, (Command){"sign", yh_com_noop, NULL, fmt_nofmt,
fmt_nofmt, "Sign data", NULL, NULL});
register_subcommand(
*c, (Command){"ecdsa", yh_com_sign_ecdsa,
"e:session,w:key_id,a:algorithm,i:data=-,F:out=-", fmt_binary,
fmt_base64, "Sign data using ECDSA", NULL, NULL});
register_subcommand(
*c, (Command){"eddsa", yh_com_sign_eddsa,
"e:session,w:key_id,a:algorithm,i:data=-,F:out=-", fmt_binary,
fmt_base64, "Sign data using EDDSA", NULL, NULL});
register_subcommand(
*c, (Command){"pkcs1v1_5", yh_com_sign_pkcs1v1_5,
"e:session,w:key_id,a:algorithm,i:data=-,F:out=-", fmt_binary,
fmt_base64, "Sign data using RSASSA-PKCS#1v1.5", NULL, NULL});
register_subcommand(
*c, (Command){"pss", yh_com_sign_pss,
"e:session,w:key_id,a:algorithm,i:data=-,F:out=-", fmt_binary,
fmt_base64, "Sign data using RSASSA-PSS", NULL, NULL});
*c =
register_command(*c, (Command){"hmac", yh_com_hmac,
"e:session,w:key_id,i:data=-,F:out=-",
fmt_hex, fmt_hex, "Hmac data", NULL, NULL});
*c = register_command(*c,
(Command){"reset", yh_com_reset, "e:session", fmt_nofmt,
fmt_nofmt, "Reset device", NULL, NULL});
*c = register_command(*c, (Command){"delete", yh_com_delete,
"e:session,w:id,t:type", fmt_nofmt,
fmt_nofmt, "Delete data", NULL, NULL});
*c =
register_command(*c, (Command){"certify", yh_com_sign_ssh_certificate,
"e:session,w:key_id,w:template_id,a:"
"algorithm,i:infile=-,F:outfile=-",
fmt_binary,
fmt_binary, // TODO: correct default formats?
"Sign SSH certificates", NULL, NULL});
*c =
register_command(*c,
(Command){"benchmark", yh_com_benchmark,
"e:session,u:count,w:key_id=0,a:algorithm=any",
fmt_nofmt, fmt_nofmt, "Run a set of benchmarks",
NULL, NULL});
*c = register_command(*c, (Command){"otp", yh_com_noop, NULL, fmt_nofmt,
fmt_nofmt, "OTP commands", NULL, NULL});
register_subcommand(*c,
(Command){"aead_create", yh_com_otp_aead_create,
"e:session,w:key_id,i:key,i:private_id,F:aead",
fmt_hex, fmt_binary, "Create an OTP AEAD", NULL,
NULL});
register_subcommand(*c, (Command){"aead_random", yh_com_otp_aead_random,
"e:session,w:key_id,F:aead", fmt_nofmt,
fmt_binary, "Create a random OTP AEAD",
NULL, NULL});
register_subcommand(*c, (Command){"decrypt", yh_com_otp_decrypt,
"e:session,w:key_id,s:otp,i:aead",
fmt_binary, fmt_nofmt,
"Decrypt an OTP with AEAD", NULL, NULL});
register_subcommand(
*c,
(Command){"rewrap", yh_com_otp_rewrap,
"e:session,w:id_from,w:id_to,i:aead_in,F:aead_out", fmt_binary,
fmt_binary, "Rewrap an OTP aead to a different key", NULL, NULL});
*c = register_command(*c, (Command){"attest", yh_com_noop, NULL, fmt_nofmt,
fmt_nofmt, "Attest device objects", NULL,
NULL});
register_subcommand(*c,
(Command){"asymmetric",
yh_com_sign_attestation_certificate,
"e:session,w:key_id,w:attest_id=0,F:file=-",
fmt_nofmt, fmt_PEM,
"Sign attestation certificate", NULL, NULL});
*c = register_command(*c, (Command){"keepalive", yh_com_noop, NULL, fmt_nofmt,
fmt_nofmt, "Change keepalive settings",
NULL, NULL});
register_subcommand(*c, (Command){"on", yh_com_keepalive_on, NULL, fmt_nofmt,
fmt_nofmt, "Enable keepalive", NULL, NULL});
register_subcommand(*c,
(Command){"off", yh_com_keepalive_off, NULL, fmt_nofmt,
fmt_nofmt, "Disable keepalive", NULL, NULL});
*c =
register_command(*c, (Command){"set", yh_com_noop, NULL, fmt_nofmt,
fmt_nofmt, "Set preferences", NULL, NULL});
register_subcommand(*c, (Command){"informat", yh_com_set_informat, "I:format",
fmt_nofmt, fmt_nofmt, "Set input format",
NULL, NULL});
register_subcommand(*c, (Command){"outformat", yh_com_set_outformat,
"I:format", fmt_nofmt, fmt_nofmt,
"Set output format", NULL, NULL});
register_subcommand(*c, (Command){"cacert", yh_com_set_cacert, "s:file",
fmt_nofmt, fmt_nofmt,
"Set CA cert to use for https to connector",
NULL, NULL});
register_subcommand(*c, (Command){"proxy", yh_com_set_proxy, "s:proxy",
fmt_nofmt, fmt_nofmt,
"Set proxyserver to use for connector",
NULL, NULL});
*c =
register_command(*c, (Command){"blink", yh_com_blink,
"e:session,b:seconds=10", fmt_nofmt,
fmt_nofmt, "Blink the device", NULL, NULL});
*c = register_command(*c, (Command){"change", yh_com_noop, NULL, fmt_nofmt,
fmt_nofmt, "Change objects", NULL, NULL});
register_subcommand(*c,
(Command){"authkey", yh_com_change_authentication_key,
"e:session,w:key_id,i:password=-", fmt_password,
fmt_nofmt, "Change an authentication key", NULL,
NULL});
#ifdef USE_ASYMMETRIC_AUTH
register_subcommand(*c, (Command){"authkey_asym",
yh_com_change_authentication_key_asym,
"e:session,w:key_id,i:password=-",
fmt_password, fmt_nofmt,
"Change an asymmetric authentication key",
NULL, NULL});
#endif
*c = msort_list(*c);
for (Command *t = *c; t != NULL; t = t->next) {
if (t->subcommands != NULL) {
t->subcommands = msort_list(t->subcommands);
}
}
}
// NOTE(adma): the prototype for el functions is fixed, no way to pass
// in parameters, we must use globals
CommandList g_commands;
static bool g_running = true;
static cmd_format g_in_fmt, g_out_fmt;
// NOTE(adma): Print the command history
// argc = 0
int yh_com_history(yubihsm_context *ctx, Argument *argv, cmd_format in_fmt,
cmd_format fmt) {
UNUSED(in_fmt);
UNUSED(fmt);
UNUSED(ctx);
UNUSED(argv);
#ifndef __WIN32
HistEvent ev;
int rv;
for (rv = history(g_hist, &ev, H_LAST); rv != -1;
rv = history(g_hist, &ev, H_PREV)) {
fprintf(ctx->out, "%4d %s", ev.num, ev.str);
}
#endif
return 0;
}
static const char *fmt_to_string(cmd_format fmt) {
for (size_t i = 0; i < sizeof(formats) / sizeof(formats[0]); i++) {
if (formats[i].format == fmt) {
return formats[i].name;
}
}
return "No format";
}
// NOTE(adma): Print information about a command
// argc = 1
// arg 0: s:command
int yh_com_help(yubihsm_context *ctx, Argument *argv, cmd_format in_fmt,
cmd_format fmt) {
UNUSED(in_fmt);
UNUSED(fmt);
UNUSED(ctx);
bool match = false;
// TODO(adma): once we have optional commands we can have a real help
for (Command *command = g_commands; command; command = command->next) {
if (strncmp(argv[0].s, command->name, strlen(argv[0].s)) == 0) {
match = true;
printf("%-25s%s\n", command->name, command->doc);
if (strlen(argv[0].s)) {
if (command->args) {
printf("%-5s%s", "", command->args);
if (command->in_fmt != fmt_nofmt) {
printf(" (default input format: %s)",
fmt_to_string(command->in_fmt));
}
printf("\n");
}
for (Command *subcommand = command->subcommands; subcommand;
subcommand = subcommand->next) {
printf("%-5s%-25s%s", "", subcommand->name, subcommand->doc);
if (subcommand->args) {
if (subcommand->in_fmt != fmt_nofmt) {
printf(" (default input format: %s)",
fmt_to_string(subcommand->in_fmt));
}
printf("\n%-30s%s\n", "", subcommand->args);
} else {
printf("\n");
}
}
}
}
}
if (match == false) {
printf("Help for command %s not found\n", argv[0].s);
}
return 0;
}
// NOTE(adma): Quit
// argc = 0
int yh_com_quit(yubihsm_context *ctx, Argument *argv, cmd_format in_fmt,
cmd_format fmt) {
UNUSED(in_fmt);
UNUSED(fmt);
UNUSED(ctx);
UNUSED(argv);
g_running = false;
return 0;
}
static bool probe_session(yubihsm_context *ctx, size_t index) {
if (ctx->sessions[index]) {
uint8_t data = 0xff;
uint8_t response[YH_MSG_BUF_SIZE];
size_t response_len = sizeof(response);
yh_cmd response_cmd;
yh_rc yrc;
// silently ignore transmit errors..?
if ((yrc = yh_send_secure_msg(ctx->sessions[index], YHC_ECHO, &data, 1,
&response_cmd, response, &response_len)) !=
YHR_SUCCESS) {
yh_destroy_session(&ctx->sessions[index]);
fprintf(stderr, "Failed to probe session %zu: %s\n", index,
yh_strerror(yrc));
return false;
}
return true;
} else {
return false;
}
}
#ifdef __WIN32
static void WINAPI timer_handler(void *lpParam,
unsigned char TimerOrWaitFired) {
UNUSED(TimerOrWaitFired);
#else
static void timer_handler(int signo __attribute__((unused))) {
#endif
if (calling_device == true || ctx.connector == NULL) {
return;
}
for (size_t i = 0; i < sizeof(ctx.sessions) / sizeof(ctx.sessions[0]); i++) {
probe_session(&ctx, i);
}
}
static int set_keepalive(uint16_t seconds) {
#ifdef __WIN32
HANDLE timer;
static HANDLE timerQueue = NULL;
if (timerQueue != NULL) {
DeleteTimerQueue(timerQueue);
}
timerQueue = CreateTimerQueue();
if (timerQueue == NULL) {
fprintf(stderr, "Failed to setup timer\n");
return 1;
}
CreateTimerQueueTimer(&timer, timerQueue, timer_handler, NULL, seconds * 1000,
seconds * 1000, 0);
if (timer == NULL) {
fprintf(stderr, "Failed to start time\n");
return 1;
}
#else
struct itimerval itimer;
itimer.it_interval.tv_sec = seconds;
itimer.it_interval.tv_usec = 0;
itimer.it_value.tv_sec = seconds;
itimer.it_value.tv_usec = 0;
if (setitimer(ITIMER_REAL, &itimer, NULL) != 0) {
fprintf(stderr, "Failed to setup timer\n");
return 1;
}
#endif
fprintf(stderr, "Session keepalive set up to run every %d seconds\n",
seconds);
return 0;
}
// NOTE: Enable keepalive
// argc = 0
int yh_com_keepalive_on(yubihsm_context *ctx, Argument *argv, cmd_format in_fmt,
cmd_format fmt) {
UNUSED(ctx);
UNUSED(argv);
UNUSED(in_fmt);
UNUSED(fmt);
return set_keepalive(15);
}
// NOTE: Disable keepalive
// argc = 0
int yh_com_keepalive_off(yubihsm_context *ctx, Argument *argv,
cmd_format in_fmt, cmd_format fmt) {
UNUSED(ctx);
UNUSED(argv);
UNUSED(in_fmt);
UNUSED(fmt);
return set_keepalive(0);
}
int yh_com_set_informat(yubihsm_context *ctx, Argument *argv, cmd_format in_fmt,
cmd_format fmt) {
UNUSED(ctx);
UNUSED(in_fmt);
UNUSED(fmt);
for (size_t i = 0; i < sizeof(formats) / sizeof(formats[0]); i++) {
if (strcasecmp(argv[0].s, formats[i].name) == 0) {
g_in_fmt = formats[i].format;
return 0;
}
}
fprintf(stderr, "Failed to parse input format\n");
return -1;
}
int yh_com_set_outformat(yubihsm_context *ctx, Argument *argv,
cmd_format in_fmt, cmd_format fmt) {
UNUSED(ctx);
UNUSED(in_fmt);
UNUSED(fmt);
for (size_t i = 0; i < sizeof(formats) / sizeof(formats[0]); i++) {
if (strcasecmp(argv[0].s, formats[i].name) == 0) {
if (formats[i].format == fmt_password) {
break;
}
g_out_fmt = formats[i].format;
return 0;
}
}
fprintf(stderr, "Failed to parse output format\n");
return -1;
}
void find_lcp(const char *items[], int n_items, const char **lcp,
int *lcp_len) {
int min = 0;
int max = 0;
if (items == NULL || n_items == 0) {
*lcp = NULL;
*lcp_len = 0;
}
for (int i = 1; i < n_items; i++) {
if (strcmp(items[i], items[min]) < 0) {
min = i;
} else if (strcmp(items[i], items[max]) > 0) {
max = i;
}
}
*lcp = items[min];
for (size_t i = 0; i < strlen(items[min]) && i < strlen(items[max]); i++) {
if (items[min][i] != items[max][i]) {
*lcp_len = i;
return;
}
}
*lcp_len = strlen(items[min]);
}
int tokenize(char *line, char **toks, int max_toks, int *cursorc, int *cursoro,
const char *space) {
int i;
int tok = 0;
int length = strlen(line);
int start_of_word = 0;
enum states { SPACE, WORD, QUOTE } state = SPACE;
toks[0] = line; // set up as fall-through
for (i = 0; i <= length; i++) {
char c = line[i];
if (cursorc && i == *cursorc && tok > 0) {
if (state == SPACE) {
*cursoro = 0;
*cursorc = tok;
} else {
*cursoro = i - start_of_word;
*cursorc = tok - 1;
}
}
if (c == '\0') {
break;
}
if (tok >= max_toks) {
return -1;
}
switch (state) {
case SPACE: {
bool found = false;
for (size_t j = 0; j < strlen(space); j++) {
if (c == space[j]) {
found = true;
break;
}
}
if (found) {
break;
}
if (c == '"') {
state = QUOTE;
start_of_word = i + 1;
} else if (c == '#') {
return tok;
} else {
state = WORD;
start_of_word = i;
}
toks[tok++] = line + start_of_word;
} break;
case QUOTE:
if (c == '"') {
line[i] = '\0';
state = SPACE;
}
break;
case WORD:
for (size_t j = 0; j < strlen(space); j++) {
if (c == space[j]) {
line[i] = '\0';
state = SPACE;
}
}
break;
}
}
return tok;
}
#ifndef __WIN32
static int compare_strings(const void *a, const void *b) {
return strcmp(*(char *const *) a, *(char *const *) b);
}
unsigned char complete_arg(EditLine *el, const char *arg, char *line,