forked from OpenSC/OpenSC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkcs11-tool.c
6185 lines (5451 loc) · 173 KB
/
pkcs11-tool.c
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
/*
* pkcs11-tool.c: Tool for poking around pkcs11 modules/tokens
*
* Copyright (C) 2002 Olaf Kirch <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef _WIN32
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#else
#include <windows.h>
#include <io.h>
#endif
#ifdef ENABLE_OPENSSL
#include <openssl/opensslv.h>
#include "libopensc/sc-ossl-compat.h"
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
#include <openssl/opensslconf.h>
#include <openssl/crypto.h>
#endif
#if OPENSSL_VERSION_NUMBER >= 0x00907000L
#include <openssl/conf.h>
#endif
#include <openssl/evp.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/asn1t.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#if OPENSSL_VERSION_NUMBER >= 0x00908000L && !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_ECDSA)
#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#endif
#include <openssl/bn.h>
#include <openssl/err.h>
#endif
#include "pkcs11/pkcs11.h"
#include "pkcs11/pkcs11-opensc.h"
#include "libopensc/asn1.h"
#include "common/compat_strlcat.h"
#include "common/compat_strlcpy.h"
#include "util.h"
#ifdef _WIN32
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
#endif
extern void *C_LoadModule(const char *name, CK_FUNCTION_LIST_PTR_PTR);
extern CK_RV C_UnloadModule(void *module);
#define NEED_SESSION_RO 0x01
#define NEED_SESSION_RW 0x02
static struct ec_curve_info {
const char *name;
const char *oid;
const char *oid_encoded;
size_t size;
} ec_curve_infos[] = {
{"secp192r1", "1.2.840.10045.3.1.1", "06082A8648CE3D030101", 192},
{"prime192v1", "1.2.840.10045.3.1.1", "06082A8648CE3D030101", 192},
{"prime192v2", "1.2.840.10045.3.1.2", "06082A8648CE3D030102", 192},
{"prime192v3", "1.2.840.10045.3.1.3", "06082A8648CE3D030103", 192},
{"nistp192", "1.2.840.10045.3.1.1", "06082A8648CE3D030101", 192},
{"ansiX9p192r1", "1.2.840.10045.3.1.1", "06082A8648CE3D030101", 192},
{"secp224r1", "1.3.132.0.33", "06052b81040021", 224},
{"nistp224", "1.3.132.0.33", "06052b81040021", 224},
{"prime256v1", "1.2.840.10045.3.1.7", "06082A8648CE3D030107", 256},
{"secp256r1", "1.2.840.10045.3.1.7", "06082A8648CE3D030107", 256},
{"ansiX9p256r1", "1.2.840.10045.3.1.7", "06082A8648CE3D030107", 256},
{"frp256v1", "1.2.250.1.223.101.256.1", "060a2a817a01815f65820001", 256},
{"secp384r1", "1.3.132.0.34", "06052B81040022", 384},
{"prime384v1", "1.3.132.0.34", "06052B81040022", 384},
{"ansiX9p384r1", "1.3.132.0.34", "06052B81040022", 384},
{"secp521r1", "1.3.132.0.35", "06052B81040023", 521},
{"nistp521", "1.3.132.0.35", "06052B81040023", 521},
{"brainpoolP192r1", "1.3.36.3.3.2.8.1.1.3", "06092B2403030208010103", 192},
{"brainpoolP224r1", "1.3.36.3.3.2.8.1.1.5", "06092B2403030208010105", 224},
{"brainpoolP256r1", "1.3.36.3.3.2.8.1.1.7", "06092B2403030208010107", 256},
{"brainpoolP320r1", "1.3.36.3.3.2.8.1.1.9", "06092B2403030208010109", 320},
{"secp192k1", "1.3.132.0.31", "06052B8104001F", 192},
{"secp256k1", "1.3.132.0.10", "06052B8104000A", 256},
{NULL, NULL, NULL, 0},
};
enum {
OPT_MODULE = 0x100,
OPT_SLOT,
OPT_SLOT_DESCRIPTION,
OPT_SLOT_INDEX,
OPT_TOKEN_LABEL,
OPT_APPLICATION_LABEL,
OPT_APPLICATION_ID,
OPT_ISSUER,
OPT_SUBJECT,
OPT_SO_PIN,
OPT_INIT_TOKEN,
OPT_INIT_PIN,
OPT_ATTR_FROM,
OPT_KEY_TYPE,
OPT_KEY_USAGE_SIGN,
OPT_KEY_USAGE_DECRYPT,
OPT_KEY_USAGE_DERIVE,
OPT_PRIVATE,
OPT_TEST_HOTPLUG,
OPT_UNLOCK_PIN,
OPT_PUK,
OPT_NEW_PIN,
OPT_LOGIN_TYPE,
OPT_TEST_EC,
OPT_DERIVE,
OPT_DERIVE_PASS_DER,
OPT_DECRYPT,
OPT_TEST_FORK,
OPT_GENERATE_KEY,
OPT_GENERATE_RANDOM,
OPT_HASH_ALGORITHM,
OPT_MGF,
OPT_SALT,
};
static const struct option options[] = {
{ "module", 1, NULL, OPT_MODULE },
{ "show-info", 0, NULL, 'I' },
{ "list-slots", 0, NULL, 'L' },
{ "list-token-slots", 0, NULL, 'T' },
{ "list-mechanisms", 0, NULL, 'M' },
{ "list-objects", 0, NULL, 'O' },
{ "sign", 0, NULL, 's' },
{ "decrypt", 0, NULL, OPT_DECRYPT },
{ "hash", 0, NULL, 'h' },
{ "derive", 0, NULL, OPT_DERIVE },
{ "derive-pass-der", 0, NULL, OPT_DERIVE_PASS_DER },
{ "mechanism", 1, NULL, 'm' },
{ "hash-algorithm", 1, NULL, OPT_HASH_ALGORITHM },
{ "mgf", 1, NULL, OPT_MGF },
{ "salt", 1, NULL, OPT_SALT },
{ "login", 0, NULL, 'l' },
{ "login-type", 1, NULL, OPT_LOGIN_TYPE },
{ "pin", 1, NULL, 'p' },
{ "puk", 1, NULL, OPT_PUK },
{ "new-pin", 1, NULL, OPT_NEW_PIN },
{ "so-pin", 1, NULL, OPT_SO_PIN },
{ "init-token", 0, NULL, OPT_INIT_TOKEN },
{ "init-pin", 0, NULL, OPT_INIT_PIN },
{ "change-pin", 0, NULL, 'c' },
{ "unlock-pin", 0, NULL, OPT_UNLOCK_PIN },
{ "keypairgen", 0, NULL, 'k' },
{ "keygen", 0, NULL, OPT_GENERATE_KEY },
{ "key-type", 1, NULL, OPT_KEY_TYPE },
{ "usage-sign", 0, NULL, OPT_KEY_USAGE_SIGN },
{ "usage-decrypt", 0, NULL, OPT_KEY_USAGE_DECRYPT },
{ "usage-derive", 0, NULL, OPT_KEY_USAGE_DERIVE },
{ "write-object", 1, NULL, 'w' },
{ "read-object", 0, NULL, 'r' },
{ "delete-object", 0, NULL, 'b' },
{ "application-label", 1, NULL, OPT_APPLICATION_LABEL },
{ "application-id", 1, NULL, OPT_APPLICATION_ID },
{ "issuer", 1, NULL, OPT_ISSUER },
{ "subject", 1, NULL, OPT_SUBJECT },
{ "type", 1, NULL, 'y' },
{ "id", 1, NULL, 'd' },
{ "label", 1, NULL, 'a' },
{ "slot", 1, NULL, OPT_SLOT },
{ "slot-description", 1, NULL, OPT_SLOT_DESCRIPTION },
{ "slot-index", 1, NULL, OPT_SLOT_INDEX },
{ "token-label", 1, NULL, OPT_TOKEN_LABEL },
{ "set-id", 1, NULL, 'e' },
{ "attr-from", 1, NULL, OPT_ATTR_FROM },
{ "input-file", 1, NULL, 'i' },
{ "output-file", 1, NULL, 'o' },
{ "signature-format", 1, NULL, 'f' },
{ "test", 0, NULL, 't' },
{ "test-hotplug", 0, NULL, OPT_TEST_HOTPLUG },
{ "moz-cert", 1, NULL, 'z' },
{ "verbose", 0, NULL, 'v' },
{ "private", 0, NULL, OPT_PRIVATE },
{ "test-ec", 0, NULL, OPT_TEST_EC },
#ifndef _WIN32
{ "test-fork", 0, NULL, OPT_TEST_FORK },
#endif
{ "generate-random", 1, NULL, OPT_GENERATE_RANDOM },
{ NULL, 0, NULL, 0 }
};
static const char *option_help[] = {
"Specify the module to load (default:" DEFAULT_PKCS11_PROVIDER ")",
"Show global token information",
"List available slots",
"List slots with tokens",
"List mechanisms supported by the token",
"Show objects on token",
"Sign some data",
"Decrypt some data",
"Hash some data",
"Derive a secret key using another key and some data",
"Derive ECDHpass DER encoded pubkey for compatibility with some PKCS#11 implementations",
"Specify mechanism (use -M for a list of supported mechanisms)",
"Specify hash algorithm used with generic RSA-PSS signature",
"Specify MGF (Message Generation Function) used for RSA-PSS signatures (possible values are MGF1-SHA1 to MGF1-SHA512)",
"Specify how many bytes should be used for salt in RSA-PSS signatures (default equals to digest size)",
"Log into the token first",
"Specify login type ('so', 'user', 'context-specific'; default:'user')",
"Supply User PIN on the command line (if used in scripts: careful!)",
"Supply User PUK on the command line",
"Supply new User PIN on the command line",
"Supply SO PIN on the command line (if used in scripts: careful!)",
"Initialize the token, its label and its SO PIN (use with --label and --so-pin)",
"Initialize the User PIN (use with --pin and --login)",
"Change User PIN",
"Unlock User PIN (without '--login' unlock in logged in session; otherwise '--login-type' has to be 'context-specific')",
"Key pair generation",
"Key generation",
"Specify the type and length of the key to create, for example rsa:1024 or EC:prime256v1 or GOSTR3410:A",
"Specify 'sign' key usage flag (sets SIGN in privkey, sets VERIFY in pubkey)",
"Specify 'decrypt' key usage flag (RSA only, set DECRYPT privkey, ENCRYPT in pubkey)",
"Specify 'derive' key usage flag (EC only)",
"Write an object (key, cert, data) to the card",
"Get object's CKA_VALUE attribute (use with --type)",
"Delete an object (use with --type cert/data/privkey/pubkey/secrkey)",
"Specify the application label of the data object (use with --type data)",
"Specify the application ID of the data object (use with --type data)",
"Specify the issuer in hexadecimal format (use with --type cert)",
"Specify the subject in hexadecimal format (use with --type cert/privkey/pubkey)",
"Specify the type of object (e.g. cert, privkey, pubkey, data)",
"Specify the ID of the object",
"Specify the label of the object",
"Specify the ID of the slot to use",
"Specify the description of the slot to use",
"Specify the index of the slot to use",
"Specify the token label of the slot to use",
"Set the CKA_ID of an object, <args>= the (new) CKA_ID",
"Use <arg> to create some attributes when writing an object",
"Specify the input file",
"Specify the output file",
"Format for ECDSA signature <arg>: 'rs' (default), 'sequence', 'openssl'",
"Test (best used with the --login or --pin option)",
"Test hotplug capabilities (C_GetSlotList + C_WaitForSlotEvent)",
"Test Mozilla-like keypair gen and cert req, <arg>=certfile",
"Verbose operation. (Set OPENSC_DEBUG to enable OpenSC specific debugging)",
"Set the CKA_PRIVATE attribute (object is only viewable after a login)",
"Test EC (best used with the --login or --pin option)",
#ifndef _WIN32
"Test forking and calling C_Initialize() in the child",
#endif
"Generate given amount of random data",
};
static const char * app_name = "pkcs11-tool"; /* for utils.c */
static int verbose = 0;
static const char * opt_input = NULL;
static const char * opt_output = NULL;
static const char * opt_module = DEFAULT_PKCS11_PROVIDER;
static int opt_slot_set = 0;
static CK_SLOT_ID opt_slot = 0;
static const char * opt_slot_description = NULL;
static const char * opt_token_label = NULL;
static CK_ULONG opt_slot_index = 0;
static int opt_slot_index_set = 0;
static CK_MECHANISM_TYPE opt_mechanism = 0;
static int opt_mechanism_used = 0;
static const char * opt_file_to_write = NULL;
static const char * opt_object_class_str = NULL;
static CK_OBJECT_CLASS opt_object_class = -1;
static CK_BYTE opt_object_id[100], new_object_id[100];
static const char * opt_attr_from_file = NULL;
static size_t opt_object_id_len = 0, new_object_id_len = 0;
static char * opt_object_label = NULL;
static const char * opt_pin = NULL;
static const char * opt_so_pin = NULL;
static const char * opt_puk = NULL;
static const char * opt_new_pin = NULL;
static char * opt_application_label = NULL;
static char * opt_application_id = NULL;
static char * opt_issuer = NULL;
static char * opt_subject = NULL;
static char * opt_key_type = NULL;
static char * opt_sig_format = NULL;
static int opt_is_private = 0;
static int opt_test_hotplug = 0;
static int opt_login_type = -1;
static int opt_key_usage_sign = 0;
static int opt_key_usage_decrypt = 0;
static int opt_key_usage_derive = 0;
static int opt_key_usage_default = 1; /* uses defaults if no opt_key_usage options */
static int opt_derive_pass_der = 0;
static unsigned long opt_random_bytes = 0;
static CK_MECHANISM_TYPE opt_hash_alg = 0;
static unsigned long opt_mgf = 0;
static long salt_len = 0;
static int salt_len_given = 0; /* 0 - not given, 1 - given with input parameters */
static void *module = NULL;
static CK_FUNCTION_LIST_PTR p11 = NULL;
static CK_SLOT_ID_PTR p11_slots = NULL;
static CK_ULONG p11_num_slots = 0;
static int suppress_warn = 0;
struct flag_info {
CK_FLAGS value;
const char * name;
};
struct mech_info {
CK_MECHANISM_TYPE mech;
const char * name;
const char * short_name;
};
struct x509cert_info {
unsigned char subject[256];
int subject_len;
unsigned char issuer[256];
int issuer_len;
unsigned char serialnum[128];
int serialnum_len;
};
struct rsakey_info {
unsigned char *modulus;
int modulus_len;
unsigned char *public_exponent;
int public_exponent_len;
unsigned char *private_exponent;
int private_exponent_len;
unsigned char *prime_1;
int prime_1_len;
unsigned char *prime_2;
int prime_2_len;
unsigned char *exponent_1;
int exponent_1_len;
unsigned char *exponent_2;
int exponent_2_len;
unsigned char *coefficient;
int coefficient_len;
};
struct gostkey_info {
struct sc_lv_data param_oid;
struct sc_lv_data public;
struct sc_lv_data private;
};
static void show_cryptoki_info(void);
static void list_slots(int, int, int);
static void show_token(CK_SLOT_ID);
static void list_mechs(CK_SLOT_ID);
static void list_objects(CK_SESSION_HANDLE, CK_OBJECT_CLASS);
static int login(CK_SESSION_HANDLE, int);
static void init_token(CK_SLOT_ID);
static void init_pin(CK_SLOT_ID, CK_SESSION_HANDLE);
static int change_pin(CK_SLOT_ID, CK_SESSION_HANDLE);
static int unlock_pin(CK_SLOT_ID slot, CK_SESSION_HANDLE sess, int login_type);
static void show_object(CK_SESSION_HANDLE, CK_OBJECT_HANDLE);
static void show_key(CK_SESSION_HANDLE, CK_OBJECT_HANDLE);
static void show_cert(CK_SESSION_HANDLE, CK_OBJECT_HANDLE);
static void show_dobj(CK_SESSION_HANDLE sess, CK_OBJECT_HANDLE obj);
static void sign_data(CK_SLOT_ID, CK_SESSION_HANDLE, CK_OBJECT_HANDLE);
static void decrypt_data(CK_SLOT_ID, CK_SESSION_HANDLE, CK_OBJECT_HANDLE);
static void hash_data(CK_SLOT_ID, CK_SESSION_HANDLE);
static void derive_key(CK_SLOT_ID, CK_SESSION_HANDLE, CK_OBJECT_HANDLE);
static int gen_keypair(CK_SLOT_ID slot, CK_SESSION_HANDLE,
CK_OBJECT_HANDLE *, CK_OBJECT_HANDLE *, const char *);
static int gen_key(CK_SLOT_ID slot, CK_SESSION_HANDLE, CK_OBJECT_HANDLE *, const char *, char *);
static int write_object(CK_SESSION_HANDLE session);
static int read_object(CK_SESSION_HANDLE session);
static int delete_object(CK_SESSION_HANDLE session);
static void set_id_attr(CK_SESSION_HANDLE session);
static int find_object(CK_SESSION_HANDLE, CK_OBJECT_CLASS,
CK_OBJECT_HANDLE_PTR,
const unsigned char *, size_t id_len, int obj_index);
static int find_mechanism(CK_SLOT_ID, CK_FLAGS, CK_MECHANISM_TYPE_PTR, size_t, CK_MECHANISM_TYPE_PTR);
static int find_slot_by_description(const char *, CK_SLOT_ID_PTR);
static int find_slot_by_token_label(const char *, CK_SLOT_ID_PTR);
static void get_token_info(CK_SLOT_ID, CK_TOKEN_INFO_PTR);
static CK_ULONG get_mechanisms(CK_SLOT_ID,
CK_MECHANISM_TYPE_PTR *, CK_FLAGS);
static void p11_fatal(const char *, CK_RV);
static void p11_warn(const char *, CK_RV);
static const char * p11_slot_info_flags(CK_FLAGS);
static const char * p11_token_info_flags(CK_FLAGS);
static const char * p11_utf8_to_local(CK_UTF8CHAR *, size_t);
static const char * p11_flag_names(struct flag_info *, CK_FLAGS);
static const char * p11_mechanism_to_name(CK_MECHANISM_TYPE);
static CK_MECHANISM_TYPE p11_name_to_mechanism(const char *);
static const char * p11_mgf_to_name(CK_RSA_PKCS_MGF_TYPE);
static CK_MECHANISM_TYPE p11_name_to_mgf(const char *);
static void p11_perror(const char *, CK_RV);
static const char * CKR2Str(CK_ULONG res);
static int p11_test(CK_SESSION_HANDLE session);
static int test_card_detection(int);
static int hex_to_bin(const char *in, CK_BYTE *out, size_t *outlen);
static void pseudo_randomize(unsigned char *data, size_t dataLen);
static CK_SESSION_HANDLE test_kpgen_certwrite(CK_SLOT_ID slot, CK_SESSION_HANDLE session);
static void test_ec(CK_SLOT_ID slot, CK_SESSION_HANDLE session);
#ifndef _WIN32
static void test_fork(void);
#endif
static void generate_random(CK_SESSION_HANDLE session);
static CK_RV find_object_with_attributes(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE *out,
CK_ATTRIBUTE *attrs, CK_ULONG attrsLen, CK_ULONG obj_index);
static CK_ULONG get_private_key_length(CK_SESSION_HANDLE sess, CK_OBJECT_HANDLE prkey);
/* win32 needs this in open(2) */
#ifndef O_BINARY
# define O_BINARY 0
#endif
#define ATTR_METHOD(ATTR, TYPE) \
static TYPE \
get##ATTR(CK_SESSION_HANDLE sess, CK_OBJECT_HANDLE obj) \
{ \
TYPE type = 0; \
CK_ATTRIBUTE attr = { CKA_##ATTR, &type, sizeof(type) }; \
CK_RV rv; \
\
rv = p11->C_GetAttributeValue(sess, obj, &attr, 1); \
if (rv != CKR_OK) \
p11_warn("C_GetAttributeValue(" #ATTR ")", rv); \
return type; \
}
#define VARATTR_METHOD(ATTR, TYPE) \
static TYPE * \
get##ATTR(CK_SESSION_HANDLE sess, CK_OBJECT_HANDLE obj, CK_ULONG_PTR pulCount) \
{ \
CK_ATTRIBUTE attr = { CKA_##ATTR, NULL, 0 }; \
CK_RV rv; \
if (pulCount) \
*pulCount = 0; \
rv = p11->C_GetAttributeValue(sess, obj, &attr, 1); \
if (rv == CKR_OK) { \
if (attr.ulValueLen == (CK_ULONG)(-1)) \
return NULL; \
if (!(attr.pValue = calloc(1, attr.ulValueLen + 1))) \
util_fatal("out of memory in get" #ATTR ": %m"); \
rv = p11->C_GetAttributeValue(sess, obj, &attr, 1); \
if (attr.ulValueLen == (CK_ULONG)(-1)) { \
free(attr.pValue); \
return NULL; \
} \
if (pulCount) \
*pulCount = attr.ulValueLen / sizeof(TYPE); \
} else { \
p11_warn("C_GetAttributeValue(" #ATTR ")", rv); \
} \
return (TYPE *) attr.pValue; \
}
/*
* Define attribute accessors
*/
ATTR_METHOD(CLASS, CK_OBJECT_CLASS);
ATTR_METHOD(ALWAYS_AUTHENTICATE, CK_BBOOL);
ATTR_METHOD(PRIVATE, CK_BBOOL);
ATTR_METHOD(MODIFIABLE, CK_BBOOL);
ATTR_METHOD(ENCRYPT, CK_BBOOL);
ATTR_METHOD(DECRYPT, CK_BBOOL);
ATTR_METHOD(SIGN, CK_BBOOL);
ATTR_METHOD(VERIFY, CK_BBOOL);
ATTR_METHOD(WRAP, CK_BBOOL);
ATTR_METHOD(UNWRAP, CK_BBOOL);
ATTR_METHOD(DERIVE, CK_BBOOL);
ATTR_METHOD(OPENSC_NON_REPUDIATION, CK_BBOOL);
ATTR_METHOD(KEY_TYPE, CK_KEY_TYPE);
ATTR_METHOD(CERTIFICATE_TYPE, CK_CERTIFICATE_TYPE);
ATTR_METHOD(MODULUS_BITS, CK_ULONG);
ATTR_METHOD(VALUE_LEN, CK_ULONG);
VARATTR_METHOD(LABEL, char);
VARATTR_METHOD(APPLICATION, char);
VARATTR_METHOD(ID, unsigned char);
VARATTR_METHOD(OBJECT_ID, unsigned char);
VARATTR_METHOD(MODULUS, CK_BYTE);
#ifdef ENABLE_OPENSSL
VARATTR_METHOD(PUBLIC_EXPONENT, CK_BYTE);
#endif
VARATTR_METHOD(VALUE, unsigned char);
VARATTR_METHOD(GOSTR3410_PARAMS, unsigned char);
VARATTR_METHOD(EC_POINT, unsigned char);
VARATTR_METHOD(EC_PARAMS, unsigned char);
int main(int argc, char * argv[])
{
CK_SESSION_HANDLE session = CK_INVALID_HANDLE;
CK_OBJECT_HANDLE object = CK_INVALID_HANDLE;
int err = 0, c, long_optind = 0;
int do_show_info = 0;
int do_list_slots = 0;
int list_token_slots = 0;
int do_list_mechs = 0;
int do_list_objects = 0;
int do_sign = 0;
int do_decrypt = 0;
int do_hash = 0;
int do_derive = 0;
int do_gen_keypair = 0;
int do_gen_key = 0;
int do_write_object = 0;
int do_read_object = 0;
int do_delete_object = 0;
int do_set_id = 0;
int do_test = 0;
int do_test_kpgen_certwrite = 0;
int do_test_ec = 0;
#ifndef _WIN32
int do_test_fork = 0;
#endif
int need_session = 0;
int opt_login = 0;
int do_init_token = 0;
int do_init_pin = 0;
int do_change_pin = 0;
int do_unlock_pin = 0;
int action_count = 0;
int do_generate_random = 0;
CK_RV rv;
#ifdef _WIN32
char expanded_val[PATH_MAX];
DWORD expanded_len;
if(_setmode(_fileno(stdout), _O_BINARY ) == -1)
util_fatal("Cannot set FMODE to O_BINARY");
if(_setmode(_fileno(stdin), _O_BINARY ) == -1)
util_fatal("Cannot set FMODE to O_BINARY");
#endif
#ifdef ENABLE_OPENSSL
#if (OPENSSL_VERSION_NUMBER >= 0x00907000L && OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
OPENSSL_config(NULL);
#endif
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS
| OPENSSL_INIT_ADD_ALL_CIPHERS
| OPENSSL_INIT_ADD_ALL_DIGESTS
| OPENSSL_INIT_LOAD_CONFIG,
NULL);
#else
/* OpenSSL magic */
OpenSSL_add_all_algorithms();
OPENSSL_malloc_init();
#endif
#endif
while (1) {
c = getopt_long(argc, argv, "ILMOTa:bd:e:hi:klm:o:p:scvf:ty:w:z:r",
options, &long_optind);
if (c == -1)
break;
switch (c) {
case 'I':
do_show_info = 1;
action_count++;
break;
case 'L':
do_list_slots = 1;
action_count++;
break;
case 'T':
do_list_slots = 1;
list_token_slots = 1;
action_count++;
break;
case 'M':
do_list_mechs = 1;
action_count++;
break;
case 'O':
need_session |= NEED_SESSION_RO;
do_list_objects = 1;
action_count++;
break;
case 'h':
need_session |= NEED_SESSION_RO;
do_hash = 1;
action_count++;
break;
case 'k':
need_session |= NEED_SESSION_RW;
do_gen_keypair = 1;
action_count++;
break;
case OPT_GENERATE_KEY:
need_session |= NEED_SESSION_RW;
do_gen_key = 1;
action_count++;
break;
case 'w':
need_session |= NEED_SESSION_RW;
do_write_object = 1;
opt_file_to_write = optarg;
action_count++;
break;
case 'r':
need_session |= NEED_SESSION_RO;
do_read_object = 1;
action_count++;
break;
case 'b':
need_session |= NEED_SESSION_RW;
do_delete_object = 1;
action_count++;
break;
case 'e':
need_session |= NEED_SESSION_RW;
do_set_id = 1;
new_object_id_len = sizeof(new_object_id);
if (!hex_to_bin(optarg, new_object_id, &new_object_id_len)) {
fprintf(stderr, "Invalid ID \"%s\"\n", optarg);
util_print_usage_and_die(app_name, options, option_help, NULL);
}
action_count++;
break;
case OPT_ATTR_FROM:
opt_attr_from_file = optarg;
break;
case 'y':
opt_object_class_str = optarg;
if (strcmp(optarg, "cert") == 0)
opt_object_class = CKO_CERTIFICATE;
else if (strcmp(optarg, "privkey") == 0)
opt_object_class = CKO_PRIVATE_KEY;
else if (strcmp(optarg, "secrkey") == 0)
opt_object_class = CKO_SECRET_KEY;
else if (strcmp(optarg, "pubkey") == 0)
opt_object_class = CKO_PUBLIC_KEY;
else if (strcmp(optarg, "data") == 0)
opt_object_class = CKO_DATA;
else {
fprintf(stderr, "Unsupported object type \"%s\"\n", optarg);
util_print_usage_and_die(app_name, options, option_help, NULL);
}
break;
case 'd':
opt_object_id_len = sizeof(opt_object_id);
if (!hex_to_bin(optarg, opt_object_id, &opt_object_id_len)) {
fprintf(stderr, "Invalid ID \"%s\"\n", optarg);
util_print_usage_and_die(app_name, options, option_help, NULL);
}
break;
case 'a':
opt_object_label = optarg;
break;
case 'i':
opt_input = optarg;
break;
case 'l':
need_session |= NEED_SESSION_RW;
opt_login = 1;
break;
case 'm':
opt_mechanism_used = 1;
opt_mechanism = p11_name_to_mechanism(optarg);
break;
case OPT_HASH_ALGORITHM:
opt_hash_alg = p11_name_to_mechanism(optarg);
break;
case OPT_MGF:
opt_mgf = p11_name_to_mgf(optarg);
break;
case OPT_SALT:
salt_len = (CK_ULONG) strtoul(optarg, NULL, 0);
salt_len_given = 1;
break;
case 'o':
opt_output = optarg;
break;
case 'p':
need_session |= NEED_SESSION_RW;
opt_login = 1;
util_get_pin(optarg, &opt_pin);
break;
case 'c':
do_change_pin = 1;
need_session |= NEED_SESSION_RW;
action_count++;
break;
case OPT_UNLOCK_PIN:
do_unlock_pin = 1;
need_session |= NEED_SESSION_RW;
action_count++;
break;
case 's':
need_session |= NEED_SESSION_RW;
do_sign = 1;
action_count++;
break;
case OPT_DECRYPT:
need_session |= NEED_SESSION_RW;
do_decrypt = 1;
action_count++;
break;
case 'f':
opt_sig_format = optarg;
break;
case 't':
need_session |= NEED_SESSION_RO;
do_test = 1;
action_count++;
break;
case 'z':
do_test_kpgen_certwrite = 1;
opt_file_to_write = optarg;
action_count++;
break;
case 'v':
verbose++;
break;
case OPT_SLOT:
opt_slot = (CK_SLOT_ID) strtoul(optarg, NULL, 0);
opt_slot_set = 1;
if (verbose)
fprintf(stderr, "Using slot with ID 0x%lx\n", opt_slot);
break;
case OPT_SLOT_DESCRIPTION:
if (opt_slot_set) {
fprintf(stderr, "Error: Only one of --slot, --slot-label, --slot-index or --token-label can be used\n");
util_print_usage_and_die(app_name, options, option_help, NULL);
}
opt_slot_description = optarg;
break;
case OPT_SLOT_INDEX:
if (opt_slot_set || opt_slot_description) {
fprintf(stderr, "Error: Only one of --slot, --slot-label, --slot-index or --token-label can be used\n");
util_print_usage_and_die(app_name, options, option_help, NULL);
}
opt_slot_index = (CK_ULONG) strtoul(optarg, NULL, 0);
opt_slot_index_set = 1;
break;
case OPT_TOKEN_LABEL:
if (opt_slot_set || opt_slot_description || opt_slot_index_set) {
fprintf(stderr, "Error: Only one of --slot, --slot-label, --slot-index or --token-label can be used\n");
util_print_usage_and_die(app_name, options, option_help, NULL);
}
opt_token_label = optarg;
break;
case OPT_MODULE:
opt_module = optarg;
break;
case OPT_APPLICATION_LABEL:
opt_application_label = optarg;
break;
case OPT_APPLICATION_ID:
opt_application_id = optarg;
break;
case OPT_ISSUER:
opt_issuer = optarg;
break;
case OPT_SUBJECT:
opt_subject = optarg;
break;
case OPT_NEW_PIN:
util_get_pin(optarg, &opt_new_pin);
break;
case OPT_PUK:
util_get_pin(optarg, &opt_puk);
break;
case OPT_LOGIN_TYPE:
if (!strcmp(optarg, "so"))
opt_login_type = CKU_SO;
else if (!strcmp(optarg, "user"))
opt_login_type = CKU_USER;
else if (!strcmp(optarg, "context-specific"))
opt_login_type = CKU_CONTEXT_SPECIFIC;
else {
fprintf(stderr, "Unsupported login type \"%s\"\n", optarg);
util_print_usage_and_die(app_name, options, option_help, NULL);
}
break;
case OPT_SO_PIN:
util_get_pin(optarg, &opt_so_pin);
break;
case OPT_INIT_TOKEN:
do_init_token = 1;
action_count++;
break ;
case OPT_INIT_PIN:
need_session |= NEED_SESSION_RW;
do_init_pin = 1;
action_count++;
break ;
case OPT_KEY_TYPE:
opt_key_type = optarg;
break;
case OPT_KEY_USAGE_SIGN:
opt_key_usage_sign = 1;
opt_key_usage_default = 0;
break;
case OPT_KEY_USAGE_DECRYPT:
opt_key_usage_decrypt = 1;
opt_key_usage_default = 0;
break;
case OPT_KEY_USAGE_DERIVE:
opt_key_usage_derive = 1;
opt_key_usage_default = 0;
break;
case OPT_PRIVATE:
opt_is_private = 1;
break;
case OPT_TEST_HOTPLUG:
opt_test_hotplug = 1;
action_count++;
break;
case OPT_TEST_EC:
do_test_ec = 1;
action_count++;
break;
case OPT_DERIVE_PASS_DER:
opt_derive_pass_der = 1;
/* fall through */
case OPT_DERIVE:
need_session |= NEED_SESSION_RW;
do_derive = 1;
action_count++;
break;
#ifndef _WIN32
case OPT_TEST_FORK:
do_test_fork = 1;
action_count++;
break;
#endif
case OPT_GENERATE_RANDOM:
need_session |= NEED_SESSION_RO;
opt_random_bytes = strtoul(optarg, NULL, 0);
do_generate_random = 1;
action_count++;
break;
default:
util_print_usage_and_die(app_name, options, option_help, NULL);
}
}
if (optind < argc) {
util_fatal("invalid option(s) given");
}
if (action_count == 0)
util_print_usage_and_die(app_name, options, option_help, NULL);
#ifdef _WIN32
expanded_len = PATH_MAX;
expanded_len = ExpandEnvironmentStringsA(opt_module, expanded_val, expanded_len);
if (0 < expanded_len && expanded_len < sizeof expanded_val)
opt_module = expanded_val;
#endif
module = C_LoadModule(opt_module, &p11);
if (module == NULL)
util_fatal("Failed to load pkcs11 module");
rv = p11->C_Initialize(NULL);
if (rv == CKR_CRYPTOKI_ALREADY_INITIALIZED)
fprintf(stderr, "\n*** Cryptoki library has already been initialized ***\n");
else if (rv != CKR_OK)
p11_fatal("C_Initialize", rv);
#ifndef _WIN32
if (do_test_fork)
test_fork();
#endif
if (do_show_info)
show_cryptoki_info();
list_slots(list_token_slots, 1, do_list_slots);
if (opt_test_hotplug) {
test_card_detection(0);
test_card_detection(1);
}
if (p11_num_slots == 0) {
fprintf(stderr, "No slots.\n");
err = 1;
goto end;
}
if (!opt_slot_set && (action_count > do_list_slots)) {
if (opt_slot_description) {
if (!find_slot_by_description(opt_slot_description, &opt_slot)) {
fprintf(stderr, "No slot named \"%s\" found\n", opt_slot_description);
err = 1;
goto end;
}
if (verbose)
fprintf(stderr, "Using slot with label \"%s\" (0x%lx)\n", opt_slot_description, opt_slot);
} else if (opt_token_label) {
if (!find_slot_by_token_label(opt_token_label, &opt_slot)) {
fprintf(stderr, "No slot with token named \"%s\" found\n", opt_token_label);
err = 1;
goto end;
}
if (verbose)
fprintf(stderr, "Using slot with label \"%s\" (0x%lx)\n", opt_slot_description, opt_slot);
} else if (opt_slot_index_set) {
if (opt_slot_index < p11_num_slots) {
opt_slot = p11_slots[opt_slot_index];
fprintf(stderr, "Using slot with index %lu (0x%lx)\n", opt_slot_index, opt_slot);
} else {
fprintf(stderr, "Slot with index %lu (counting from 0) is not available.\n", opt_slot_index);
fprintf(stderr, "You must specify a valid slot with either --slot, --slot-index or --slot-label.\n");
err = 1;
goto end;
}
} else {
/* use first slot with token present (or default slot on error) */
unsigned int i, found = 0;
for (i = 0; i < p11_num_slots; i++) {
CK_SLOT_INFO info;
rv = p11->C_GetSlotInfo(p11_slots[i], &info);
if (rv != CKR_OK)
p11_fatal("C_GetSlotInfo", rv);
if (info.flags & CKF_TOKEN_PRESENT) {
opt_slot = p11_slots[i];
fprintf(stderr, "Using slot %u with a present token (0x%lx)\n", i, opt_slot);
found = 1;
break;
}
}
if (!found) {
fprintf(stderr, "No slot with a token was found.\n");
err = 1;
goto end;
}
}
}
if (do_list_mechs)
list_mechs(opt_slot);
if (do_sign || do_decrypt) {
CK_TOKEN_INFO info;
get_token_info(opt_slot, &info);
if (!(info.flags & CKF_TOKEN_INITIALIZED))
util_fatal("Token not initialized");
if (info.flags & CKF_LOGIN_REQUIRED)
opt_login++;
}
if (do_init_token)
init_token(opt_slot);
if (need_session) {
int flags = CKF_SERIAL_SESSION;
if (need_session & NEED_SESSION_RW)
flags |= CKF_RW_SESSION;
rv = p11->C_OpenSession(opt_slot, flags,
NULL, NULL, &session);
if (rv != CKR_OK)
p11_fatal("C_OpenSession", rv);
}
if (opt_login) {
int r;
if (opt_login_type == -1)
opt_login_type = do_init_pin ? CKU_SO : CKU_USER;
r = login(session, opt_login_type);
if (r != 0)
return r;