-
Notifications
You must be signed in to change notification settings - Fork 711
/
framework-pkcs15.c
6820 lines (5920 loc) · 208 KB
/
framework-pkcs15.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
/*
* framework-pkcs15.c: PKCS#15 framework and related objects
*
* Copyright (C) 2002 Timo Teräs <timo.teras@iki.fi>
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "common/constant-time.h"
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "libopensc/log.h"
#include "libopensc/internal.h"
#include "libopensc/asn1.h"
#include "libopensc/cardctl.h"
#include "ui/notify.h"
#include "common/compat_strnlen.h"
#ifdef ENABLE_OPENSSL
#include <openssl/sha.h>
#include <openssl/crypto.h>
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#include <openssl/evp.h>
#endif
#else
#define SHA_DIGEST_LENGTH 20
#endif
#include "sc-pkcs11.h"
#ifdef USE_PKCS15_INIT
#include "pkcs15init/pkcs15-init.h"
#endif
struct pkcs15_slot_data {
struct sc_pkcs15_object *auth_obj;
};
#define slot_data(p) ((struct pkcs15_slot_data *) (p))
#define slot_data_auth(p) (((p) && slot_data(p)) ? slot_data(p)->auth_obj : NULL)
#define slot_data_auth_info(p) (((p) && slot_data_auth(p))? \
(struct sc_pkcs15_auth_info *) slot_data_auth(p)->data : NULL)
#define check_attribute_buffer(attr,size) \
if (attr->pValue == NULL_PTR) { \
attr->ulValueLen = size; \
return CKR_OK; \
} \
if (attr->ulValueLen < size) { \
attr->ulValueLen = size; \
return CKR_BUFFER_TOO_SMALL; \
} \
attr->ulValueLen = size;
#define MAX_OBJECTS 128
struct pkcs15_fw_data {
struct sc_pkcs15_card * p15_card;
struct pkcs15_any_object * objects[MAX_OBJECTS];
unsigned int num_objects;
unsigned int locked;
unsigned char *user_puk;
unsigned int user_puk_len;
};
struct pkcs15_any_object {
struct sc_pkcs11_object base;
unsigned int refcount;
size_t size;
struct sc_pkcs15_object * p15_object;
struct pkcs15_pubkey_object * related_pubkey;
struct pkcs15_cert_object * related_cert;
struct pkcs15_prkey_object * related_privkey;
};
struct pkcs15_cert_object {
struct pkcs15_any_object base;
struct sc_pkcs15_cert_info * cert_info;
struct sc_pkcs15_cert * cert_data;
};
#define cert_flags base.base.flags
#define cert_p15obj base.p15_object
#define cert_pubkey base.related_pubkey
#define cert_issuer base.related_cert
#define cert_prvkey base.related_privkey
struct pkcs15_prkey_object {
struct pkcs15_any_object base;
struct sc_pkcs15_prkey_info * prv_info;
struct sc_pkcs15_pubkey * pub_data;
};
#define prv_flags base.base.flags
#define prv_p15obj base.p15_object
#define prv_pubkey base.related_pubkey
#define prv_next base.related_privkey
struct pkcs15_pubkey_object {
struct pkcs15_any_object base;
struct sc_pkcs15_pubkey_info * pub_info; /* NULL for key extracted from cert */
struct sc_pkcs15_pubkey * pub_data;
};
#define pub_flags base.base.flags
#define pub_p15obj base.p15_object
#define pub_genfrom base.related_cert
#define __p15_type(obj) (((obj) && (obj)->p15_object)? ((obj)->p15_object->type) : (unsigned int)-1)
#define is_privkey(obj) ((__p15_type(obj) & SC_PKCS15_TYPE_CLASS_MASK) == SC_PKCS15_TYPE_PRKEY)
#define is_pubkey(obj) ((__p15_type(obj) & SC_PKCS15_TYPE_CLASS_MASK) == SC_PKCS15_TYPE_PUBKEY)
#define is_cert(obj) (__p15_type(obj) == SC_PKCS15_TYPE_CERT_X509)
struct pkcs15_data_object {
struct pkcs15_any_object base;
struct sc_pkcs15_data_info *info;
struct sc_pkcs15_data *value;
};
#define data_flags base.base.flags
#define data_p15obj base.p15_object
#define is_data(obj) (__p15_type(obj) == SC_PKCS15_TYPE_DATA_OBJECT)
struct pkcs15_profile_object {
struct pkcs15_any_object base;
unsigned long profile_id;
};
struct pkcs15_skey_object {
struct pkcs15_any_object base;
struct sc_pkcs15_skey_info *info;
struct sc_pkcs15_skey *valueXXXX;
};
#define is_skey(obj) ((__p15_type(obj) & SC_PKCS15_TYPE_CLASS_MASK) == SC_PKCS15_TYPE_SKEY)
#define skey_flags base.base.flags
#define skey_p15obj base.p15_object
extern struct sc_pkcs11_object_ops pkcs15_cert_ops;
extern struct sc_pkcs11_object_ops pkcs15_prkey_ops;
extern struct sc_pkcs11_object_ops pkcs15_pubkey_ops;
extern struct sc_pkcs11_object_ops pkcs15_dobj_ops;
extern struct sc_pkcs11_object_ops pkcs15_profile_ops;
extern struct sc_pkcs11_object_ops pkcs15_skey_ops;
const CK_BYTE gostr3410_paramset_A_encoded_oid[] = { 0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x23, 0x01 };
const unsigned int gostr3410_paramset_A_oid[] = {1, 2, 643, 2, 2, 35, 1, (unsigned int)-1};
const CK_BYTE gostr3410_paramset_B_encoded_oid[] = { 0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x23, 0x02 };
const unsigned int gostr3410_paramset_B_oid[] = {1, 2, 643, 2, 2, 35, 2, (unsigned int)-1};
const CK_BYTE gostr3410_paramset_C_encoded_oid[] = { 0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x23, 0x03 };
const unsigned int gostr3410_paramset_C_oid[] = {1, 2, 643, 2, 2, 35, 3, (unsigned int)-1};
static const struct {
const CK_BYTE *encoded_oid;
const unsigned int encoded_oid_size;
const unsigned int *oid;
const unsigned int oid_size;
unsigned char oid_id;
} gostr3410_param_oid [] = {
{ &gostr3410_paramset_A_encoded_oid[0],
sizeof(gostr3410_paramset_A_encoded_oid),
&gostr3410_paramset_A_oid[0],
sizeof(gostr3410_paramset_A_oid),
SC_PKCS15_PARAMSET_GOSTR3410_A },
{ &gostr3410_paramset_B_encoded_oid[0],
sizeof(gostr3410_paramset_B_encoded_oid),
&gostr3410_paramset_B_oid[0],
sizeof(gostr3410_paramset_B_oid),
SC_PKCS15_PARAMSET_GOSTR3410_B },
{ &gostr3410_paramset_C_encoded_oid[0],
sizeof(gostr3410_paramset_C_encoded_oid),
&gostr3410_paramset_C_oid[0],
sizeof(gostr3410_paramset_C_oid),
SC_PKCS15_PARAMSET_GOSTR3410_C }
};
const CK_BYTE gostr3411_94_cryptopro_paramset_encoded_oid[] = { 0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x1e, 0x01 };
const unsigned int gostr3411_94_cryptopro_paramset_oid[] = {1, 2, 643, 2, 2, 30, 1, (unsigned int)-1};
#ifdef USE_PKCS15_INIT
static const struct {
const CK_BYTE *encoded_oid;
const unsigned int encoded_oid_size;
const unsigned int *oid;
const unsigned int oid_size;
} gostr3410_hash_param_oid [] = {
{ &gostr3411_94_cryptopro_paramset_encoded_oid[0],
sizeof(gostr3411_94_cryptopro_paramset_encoded_oid),
&gostr3411_94_cryptopro_paramset_oid[0],
sizeof(gostr3411_94_cryptopro_paramset_oid)}
};
#endif
static int __pkcs15_release_object(struct pkcs15_any_object *);
static CK_RV register_mechanisms(struct sc_pkcs11_card *p11card);
static CK_RV get_public_exponent(struct sc_pkcs15_pubkey *,
CK_ATTRIBUTE_PTR);
static CK_RV get_modulus(struct sc_pkcs15_pubkey *,
CK_ATTRIBUTE_PTR);
static CK_RV get_modulus_bits(struct sc_pkcs15_pubkey *,
CK_ATTRIBUTE_PTR);
static CK_RV get_usage_bit(unsigned int usage, CK_ATTRIBUTE_PTR attr);
static CK_RV get_gostr3410_params(const u8 *, size_t, CK_ATTRIBUTE_PTR);
static CK_RV get_ec_pubkey_point(struct sc_pkcs15_pubkey *, CK_ATTRIBUTE_PTR);
static CK_RV get_ec_pubkey_params(struct sc_pkcs15_pubkey *, CK_ATTRIBUTE_PTR);
static int lock_card(struct pkcs15_fw_data *);
static int unlock_card(struct pkcs15_fw_data *);
static int reselect_app_df(sc_pkcs15_card_t *p15card);
#ifdef USE_PKCS15_INIT
static CK_RV set_gost3410_params(struct sc_pkcs15init_prkeyargs *,
struct sc_pkcs15init_pubkeyargs *,
CK_ATTRIBUTE_PTR, CK_ULONG, CK_ATTRIBUTE_PTR, CK_ULONG);
static CK_RV pkcs15_create_slot(struct sc_pkcs11_card *p11card, struct pkcs15_fw_data *fw_data,
struct sc_pkcs15_object *auth, struct sc_app_info *app,
struct sc_pkcs11_slot **out);
static int pkcs11_get_pin_callback(struct sc_profile *profile, int id,
const struct sc_pkcs15_auth_info *info, const char *label,
unsigned char *pinbuf, size_t *pinsize);
static struct sc_pkcs15init_callbacks pkcs15init_callbacks = {
pkcs11_get_pin_callback, /* get_pin() */
NULL
};
static char *pkcs15init_sopin = NULL;
static size_t pkcs15init_sopin_len = 0;
static int pkcs11_get_pin_callback(struct sc_profile *profile, int id,
const struct sc_pkcs15_auth_info *info, const char *label,
unsigned char *pinbuf, size_t *pinsize)
{
char *secret = NULL;
size_t len = 0;
if (info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
return SC_ERROR_NOT_SUPPORTED;
sc_log(context, "pkcs11_get_pin_callback() auth-method %X", info->auth_method);
if (info->auth_method == SC_AC_CHV) {
unsigned int flags = info->attrs.pin.flags;
sc_log(context, "pkcs11_get_pin_callback() PIN flags %X", flags);
if ((flags & SC_PKCS15_PIN_FLAG_SO_PIN) && !(flags & SC_PKCS15_PIN_FLAG_UNBLOCKING_PIN)) {
if (pkcs15init_sopin_len)
secret = pkcs15init_sopin;
}
}
if (secret)
len = strlen(secret);
sc_log(context, "pkcs11_get_pin_callback() secret '%s'", secret ? secret : "<null>");
if (!secret)
return SC_ERROR_OBJECT_NOT_FOUND;
if (len > *pinsize)
return SC_ERROR_BUFFER_TOO_SMALL;
memcpy(pinbuf, secret, len + 1);
*pinsize = len;
return 0;
}
#endif
/* Returns FW data corresponding to the given application or,
* if application info is not supplied, returns first available FW data. */
static struct pkcs15_fw_data *
get_fw_data(struct sc_pkcs11_card *p11card, struct sc_app_info *app_info, int *out_idx)
{
struct pkcs15_fw_data *out = NULL;
int idx;
if (!p11card)
return NULL;
for (idx=0; p11card && idx < SC_PKCS11_FRAMEWORK_DATA_MAX_NUM; idx++) {
struct pkcs15_fw_data *fw_data = (struct pkcs15_fw_data *) p11card->fws_data[idx];
struct sc_file *file_app = NULL;
if (!fw_data || !fw_data->p15_card)
continue;
file_app = fw_data->p15_card->file_app;
if (app_info && file_app) {
if (file_app->path.len != app_info->path.len)
continue;
if (file_app->path.aid.len != app_info->path.aid.len)
continue;
if (memcmp(file_app->path.aid.value, app_info->path.aid.value, app_info->path.aid.len))
continue;
if (memcmp(file_app->path.value, app_info->path.value, app_info->path.len))
continue;
}
out = fw_data;
if (out_idx)
*out_idx = idx;
break;
}
return out;
}
/* PKCS#15 Framework */
static CK_RV
pkcs15_bind(struct sc_pkcs11_card *p11card, struct sc_app_info *app_info)
{
struct pkcs15_fw_data *fw_data = NULL;
struct sc_aid *aid = app_info ? &app_info->aid : NULL;
int rc, idx;
CK_RV ck_rv;
sc_log(context, "Bind PKCS#15 '%s' application", app_info ? app_info->label : "<anonymous>");
if (!p11card)
return CKR_TOKEN_NOT_RECOGNIZED;
for (idx=0; idx<SC_PKCS11_FRAMEWORK_DATA_MAX_NUM; idx++)
if (!p11card->fws_data[idx])
break;
if (idx == SC_PKCS11_FRAMEWORK_DATA_MAX_NUM)
return CKR_USER_TOO_MANY_TYPES;
if (!(fw_data = calloc(1, sizeof(*fw_data))))
return CKR_HOST_MEMORY;
p11card->fws_data[idx] = fw_data;
rc = sc_pkcs15_bind(p11card->card, aid, &fw_data->p15_card);
if (rc != SC_SUCCESS) {
sc_log(context, "sc_pkcs15_bind failed: %d", rc);
return sc_to_cryptoki_error(rc, NULL);
}
/* Mechanisms are registered globally per card. Checking
* p11card->nmechanisms avoids registering the same mechanisms twice for a
* card with multiple slots. */
if (!p11card->nmechanisms) {
ck_rv = register_mechanisms(p11card);
if (ck_rv != CKR_OK) {
sc_log(context, "cannot register mechanisms; CKR 0x%lX",
ck_rv);
return ck_rv;
}
}
return CKR_OK;
}
static CK_RV
pkcs15_unbind(struct sc_pkcs11_card *p11card)
{
unsigned int i, idx;
int rv = SC_SUCCESS;
if (!p11card)
return CKR_TOKEN_NOT_RECOGNIZED;
for (idx=0; p11card && idx<SC_PKCS11_FRAMEWORK_DATA_MAX_NUM; idx++) {
struct pkcs15_fw_data *fw_data = (struct pkcs15_fw_data *) p11card->fws_data[idx];
if (!fw_data)
break;
for (i = 0; i < fw_data->num_objects; i++) {
struct pkcs15_any_object *obj = fw_data->objects[i];
/* use object specific release method if existing */
if (obj->base.ops && obj->base.ops->release)
obj->base.ops->release(obj);
else
__pkcs15_release_object(obj);
}
unlock_card(fw_data);
if (fw_data->p15_card) {
rv = sc_pkcs15_unbind(fw_data->p15_card);
}
fw_data->p15_card = NULL;
free(fw_data);
p11card->fws_data[idx] = NULL;
}
return sc_to_cryptoki_error(rv, NULL);
}
static void
pkcs15_init_token_info(struct sc_pkcs15_card *p15card, CK_TOKEN_INFO_PTR pToken)
{
scconf_block *conf_block = NULL;
char *model = NULL;
conf_block = sc_get_conf_block(p15card->card->ctx, "framework", "pkcs15", 1);
if (conf_block && p15card->file_app) {
scconf_block **blocks = NULL;
char str_path[SC_MAX_AID_STRING_SIZE];
memset(str_path, 0, sizeof(str_path));
sc_bin_to_hex(p15card->file_app->path.value, p15card->file_app->path.len, str_path, sizeof(str_path), 0);
blocks = scconf_find_blocks(p15card->card->ctx->conf, conf_block, "application", str_path);
if (blocks) {
if (blocks[0])
model = (char *)scconf_get_str(blocks[0], "model", NULL);
free(blocks);
}
}
if (model)
strcpy_bp(pToken->model, model, sizeof(pToken->model));
else if (p15card->flags & SC_PKCS15_CARD_FLAG_EMULATED)
strcpy_bp(pToken->model, "PKCS#15 emulated", sizeof(pToken->model));
else
strcpy_bp(pToken->model, "PKCS#15", sizeof(pToken->model));
if (p15card->tokeninfo) {
strcpy_bp(pToken->manufacturerID, p15card->tokeninfo->manufacturer_id, 32);
/* Take the last 16 chars of the serial number (if the are more than 16).
* _Assuming_ that the serial number is a Big Endian counter, this
* will assure that the serial within each type of card will be
* unique in pkcs11 (at least for the first 8^16 cards :-) */
if (p15card->tokeninfo->serial_number != NULL) {
size_t sn_start = strlen(p15card->tokeninfo->serial_number);
if (sn_start <= 16)
sn_start = 0;
else
sn_start -= 16;
strcpy_bp(pToken->serialNumber, p15card->tokeninfo->serial_number + sn_start, 16);
}
}
pToken->ulMaxSessionCount = CK_EFFECTIVELY_INFINITE;
pToken->ulSessionCount = 0; /* FIXME */
pToken->ulMaxRwSessionCount = CK_EFFECTIVELY_INFINITE;
pToken->ulRwSessionCount = 0; /* FIXME */
pToken->ulTotalPublicMemory = CK_UNAVAILABLE_INFORMATION;
pToken->ulFreePublicMemory = CK_UNAVAILABLE_INFORMATION;
pToken->ulTotalPrivateMemory = CK_UNAVAILABLE_INFORMATION;
pToken->ulFreePrivateMemory = CK_UNAVAILABLE_INFORMATION;
pToken->hardwareVersion.major = p15card->card->version.hw_major;
pToken->hardwareVersion.minor = p15card->card->version.hw_minor;
pToken->firmwareVersion.major = p15card->card->version.fw_major;
pToken->firmwareVersion.minor = p15card->card->version.fw_minor;
}
#ifdef USE_PKCS15_INIT
static char *
set_cka_label(CK_ATTRIBUTE_PTR attr, char *label)
{
char *l = (char *)attr->pValue;
unsigned long len = attr->ulValueLen;
if (len >= SC_PKCS15_MAX_LABEL_SIZE)
len = SC_PKCS15_MAX_LABEL_SIZE-1;
memcpy(label, l, len);
label[len] = '\0';
return label;
}
#endif
static int
__pkcs15_create_object(struct pkcs15_fw_data *fw_data,
struct pkcs15_any_object **result,
struct sc_pkcs15_object *p15_object,
struct sc_pkcs11_object_ops *ops,
size_t size)
{
struct pkcs15_any_object *obj;
if (fw_data->num_objects >= MAX_OBJECTS)
return SC_ERROR_TOO_MANY_OBJECTS;
if (!(obj = calloc(1, size)))
return SC_ERROR_OUT_OF_MEMORY;
fw_data->objects[fw_data->num_objects++] = obj;
obj->base.ops = ops;
obj->p15_object = p15_object;
obj->refcount = 1;
obj->size = size;
*result = obj;
return SC_SUCCESS;
}
static int
__pkcs15_release_object(struct pkcs15_any_object *obj)
{
if (--(obj->refcount) != 0)
return obj->refcount;
sc_mem_clear(obj, obj->size);
free(obj);
return 0;
}
#ifdef USE_PKCS15_INIT
static int
__pkcs15_delete_object(struct pkcs15_fw_data *fw_data, struct pkcs15_any_object *obj)
{
unsigned int i;
if (fw_data->num_objects == 0)
return SC_ERROR_INTERNAL;
for (i = 0; i < fw_data->num_objects; ++i) {
if (fw_data->objects[i] == obj) {
fw_data->objects[i] = fw_data->objects[--fw_data->num_objects];
if (__pkcs15_release_object(obj) > 0)
return SC_ERROR_INTERNAL;
return SC_SUCCESS;
}
}
return SC_ERROR_OBJECT_NOT_FOUND;
}
#endif
CK_RV C_GetTokenInfo(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo)
{
struct sc_pkcs11_slot *slot;
struct pkcs15_fw_data *fw_data = NULL;
struct sc_pkcs15_object *auth;
const char *name;
CK_RV rv;
if (pInfo == NULL_PTR)
return CKR_ARGUMENTS_BAD;
rv = sc_pkcs11_lock();
if (rv != CKR_OK)
return rv;
sc_log(context, "C_GetTokenInfo(%lx)", slotID);
rv = slot_get_token(slotID, &slot);
if (rv != CKR_OK) {
sc_log(context, "C_GetTokenInfo() get token: rv 0x%lX", rv);
goto out;
}
if (slot->p11card == NULL) {
if (slot->slot_info.flags & CKF_TOKEN_PRESENT) {
rv = CKR_TOKEN_NOT_RECOGNIZED;
} else {
rv = CKR_TOKEN_NOT_PRESENT;
}
goto out;
}
fw_data = (struct pkcs15_fw_data *) slot->p11card->fws_data[slot->fw_data_idx];
if (!fw_data) {
rv = sc_to_cryptoki_error(SC_ERROR_INTERNAL, "C_GetTokenInfo");
goto out;
}
/* User PIN flags are cleared before re-calculation */
slot->token_info.flags &= ~(CKF_USER_PIN_COUNT_LOW|CKF_USER_PIN_FINAL_TRY|CKF_USER_PIN_LOCKED);
auth = slot_data_auth(slot->fw_data);
sc_log(context,
"C_GetTokenInfo() auth. object %p, token-info flags 0x%lX", auth,
slot->token_info.flags);
if (auth) {
struct sc_pkcs15_card *p15card = NULL;
struct sc_pkcs15_auth_info *pin_info = NULL;
pin_info = (struct sc_pkcs15_auth_info*) auth->data;
p15card = fw_data->p15_card;
if (!p15card) {
rv = sc_to_cryptoki_error(SC_ERROR_INVALID_CARD, "C_GetTokenInfo");
goto out;
}
sc_pkcs15_get_pin_info(p15card, auth);
if (pin_info->tries_left >= 0) {
if (pin_info->tries_left == 1 || pin_info->max_tries == 1)
slot->token_info.flags |= CKF_USER_PIN_FINAL_TRY;
else if (pin_info->tries_left == 0)
slot->token_info.flags |= CKF_USER_PIN_LOCKED;
else if (pin_info->max_tries > 1 && pin_info->tries_left < pin_info->max_tries)
slot->token_info.flags |= CKF_USER_PIN_COUNT_LOW;
}
}
memcpy(pInfo, &slot->token_info, sizeof(CK_TOKEN_INFO));
out:
name = lookup_enum(RV_T, rv);
if (name)
sc_log(context, "C_GetTokenInfo(%lx) returns %s", slotID, name);
else
sc_log(context, "C_GetTokenInfo(%lx) returns 0x%08lX", slotID, rv);
sc_pkcs11_unlock();
return rv;
}
static int
public_key_created(struct pkcs15_fw_data *fw_data, const struct sc_pkcs15_id *id,
struct pkcs15_any_object **obj2)
{
size_t ii;
for(ii=0; ii<fw_data->num_objects; ii++) {
struct pkcs15_any_object *any_object = fw_data->objects[ii];
struct sc_pkcs15_object *p15_object = any_object->p15_object;
if (!p15_object)
continue;
if ((p15_object->type & SC_PKCS15_TYPE_CLASS_MASK) != SC_PKCS15_TYPE_PUBKEY)
continue;
if (sc_pkcs15_compare_id(id, &((struct sc_pkcs15_pubkey_info *)p15_object->data)->id)) {
if (obj2)
*obj2 = any_object;
return SC_SUCCESS;
}
}
return SC_ERROR_OBJECT_NOT_FOUND;
}
static void
pkcs15_cert_extract_label(struct pkcs15_cert_object *cert)
{
if (!cert || !cert->cert_p15obj || !cert->cert_data)
return;
sc_log(context, "pkcs15_cert_extract_label() called. Current label: %s", cert->cert_p15obj->label);
/* if we didn't get a label, set one based on the CN */
if (*cert->cert_p15obj->label == '\0') { /* can't be NULL -- static array */
static const struct sc_object_id cn_oid = {{ 2, 5, 4, 3, -1 }};
u8 *cn_name = NULL;
size_t cn_len = 0;
int rv = sc_pkcs15_get_name_from_dn(context,
cert->cert_data->subject, cert->cert_data->subject_len,
&cn_oid, &cn_name, &cn_len);
if (rv == SC_SUCCESS) {
sc_log(context, "pkcs15_cert_extract_label(): Name from DN is %.*s",
(unsigned int) cn_len, cn_name);
cn_len = MIN(cn_len, SC_PKCS15_MAX_LABEL_SIZE-1);
memcpy(cert->cert_p15obj->label, cn_name, cn_len);
cert->cert_p15obj->label[cn_len] = '\0';
}
free(cn_name);
}
}
static int
__pkcs15_create_cert_object(struct pkcs15_fw_data *fw_data, struct sc_pkcs15_object *cert,
struct pkcs15_any_object **cert_object)
{
struct sc_pkcs15_cert_info *p15_info = NULL;
struct sc_pkcs15_cert *p15_cert = NULL;
struct pkcs15_any_object *any_object = NULL;
struct pkcs15_cert_object *object = NULL;
struct pkcs15_pubkey_object *obj2 = NULL;
struct pkcs15_any_object *any_object2 = NULL;
int rv;
p15_info = (struct sc_pkcs15_cert_info *) cert->data;
if (cert->flags & SC_PKCS15_CO_FLAG_PRIVATE) { /* is the cert private? */
p15_cert = NULL; /* will read cert when needed */
}
else {
rv = sc_pkcs15_read_certificate(fw_data->p15_card, p15_info, 0, &p15_cert);
if (rv < 0)
return rv;
}
/* Certificate object */
rv = __pkcs15_create_object(fw_data, &any_object,
cert, &pkcs15_cert_ops, sizeof(struct pkcs15_cert_object));
object = (struct pkcs15_cert_object *) any_object;
if (rv < 0) {
if (p15_cert != NULL)
sc_pkcs15_free_certificate(p15_cert);
return rv;
}
object->cert_info = p15_info;
object->cert_data = p15_cert;
/* Corresponding public key */
rv = public_key_created(fw_data, &p15_info->id, &any_object2);
if (rv != SC_SUCCESS)
rv = __pkcs15_create_object(fw_data, &any_object2,
NULL, &pkcs15_pubkey_ops, sizeof(struct pkcs15_pubkey_object));
obj2 = (struct pkcs15_pubkey_object *) any_object2;
if (rv < 0)
return rv;
if (p15_cert) {
/* make a copy of public key from the cert */
if (!obj2->pub_data)
rv = sc_pkcs15_pubkey_from_cert(context, &p15_cert->data, &obj2->pub_data);
if (rv < 0)
return rv;
}
obj2->pub_genfrom = object;
object->cert_pubkey = obj2;
/* Find missing labels for certificate */
pkcs15_cert_extract_label(object);
if (cert_object != NULL)
*cert_object = any_object;
return 0;
}
static int
__pkcs15_create_pubkey_object(struct pkcs15_fw_data *fw_data,
struct sc_pkcs15_object *pubkey, struct pkcs15_any_object **pubkey_object)
{
struct pkcs15_any_object *any_object = NULL;
struct pkcs15_pubkey_object *object = NULL;
struct sc_pkcs15_pubkey *p15_key = NULL;
int rv;
/* Read public key from card */
/* Attempt to read pubkey from card or file.
* During initialization process, the key may have been created
* and saved as a file before the certificate has been created.
*/
if (pubkey->flags & SC_PKCS15_CO_FLAG_PRIVATE) { /* is the key private? */
sc_log(context, "No pubkey");
p15_key = NULL; /* will read key when needed */
}
else {
/* if emulation already created pubkey use it */
if (pubkey->emulated && (fw_data->p15_card->flags & SC_PKCS15_CARD_FLAG_EMULATED)) {
sc_log(context, "Use emulated pubkey");
sc_pkcs15_dup_pubkey(context, (struct sc_pkcs15_pubkey *) pubkey->emulated, &p15_key);
}
else {
sc_log(context, "Get pubkey from PKCS#15 object");
rv = sc_pkcs15_read_pubkey(fw_data->p15_card, pubkey, &p15_key);
if (rv < 0)
p15_key = NULL;
}
}
/* Public key object */
rv = __pkcs15_create_object(fw_data, &any_object,
pubkey, &pkcs15_pubkey_ops, sizeof(struct pkcs15_pubkey_object));
object = (struct pkcs15_pubkey_object *) any_object;
if (rv >= 0) {
object->pub_info = (struct sc_pkcs15_pubkey_info *) pubkey->data;
object->pub_data = p15_key;
if (p15_key && object->pub_info->modulus_length == 0 && p15_key->algorithm == SC_ALGORITHM_RSA)
object->pub_info->modulus_length = 8 * p15_key->u.rsa.modulus.len;
} else {
sc_pkcs15_free_pubkey(p15_key);
}
if (object && object->pub_data) {
if ((object->pub_data->alg_id)&&(object->pub_data->algorithm == SC_ALGORITHM_GOSTR3410))
object->pub_data->alg_id->params = &((object->pub_data->u).gostr3410.params);
}
if (pubkey_object != NULL)
*pubkey_object = any_object;
return rv;
}
static int
__pkcs15_create_prkey_object(struct pkcs15_fw_data *fw_data,
struct sc_pkcs15_object *prkey, struct pkcs15_any_object **prkey_object)
{
struct pkcs15_any_object *any_object = NULL;
struct pkcs15_prkey_object *object = NULL;
int rv;
rv = __pkcs15_create_object(fw_data, &any_object,
prkey, &pkcs15_prkey_ops, sizeof(struct pkcs15_prkey_object));
object = (struct pkcs15_prkey_object *) any_object;
if (rv >= 0)
object->prv_info = (struct sc_pkcs15_prkey_info *) prkey->data;
if (prkey_object != NULL)
*prkey_object = any_object;
return rv;
}
static int
__pkcs15_create_data_object(struct pkcs15_fw_data *fw_data,
struct sc_pkcs15_object *object, struct pkcs15_any_object **data_object)
{
struct pkcs15_any_object *any_object = NULL;
struct pkcs15_data_object *dobj = NULL;
int rv;
rv = __pkcs15_create_object(fw_data, &any_object,
object, &pkcs15_dobj_ops, sizeof(struct pkcs15_data_object));
dobj = (struct pkcs15_data_object *) any_object;
if (rv >= 0) {
dobj->info = (struct sc_pkcs15_data_info *) object->data;
dobj->value = NULL;
}
if (data_object != NULL)
*data_object = any_object;
return rv;
}
/* Note, that this is not an actual PKCS #15 object, but just a bogus
* structure to create PKCS #11 object. There is no corresponding
* PKCS #15 object. */
static int
__pkcs15_create_profile_object(struct pkcs15_fw_data *fw_data,
int public_certificates, struct pkcs15_any_object **profile_object)
{
struct pkcs15_profile_object *pobj = NULL;
struct pkcs15_any_object *any_pobj = NULL;
struct sc_pkcs15_object *obj = NULL;
int rv;
obj = calloc(1, sizeof(struct sc_pkcs15_object));
rv = __pkcs15_create_object(fw_data, &any_pobj,
obj, &pkcs15_profile_ops, sizeof(struct pkcs15_profile_object));
if (rv != SC_SUCCESS) {
free(obj);
return rv;
}
pobj = (struct pkcs15_profile_object *) any_pobj;
pobj->profile_id = public_certificates ? CKP_PUBLIC_CERTIFICATES_TOKEN : CKP_AUTHENTICATION_TOKEN;
if (profile_object != NULL)
*profile_object = (struct pkcs15_any_object *) pobj;
return rv;
}
static int
__pkcs15_create_secret_key_object(struct pkcs15_fw_data *fw_data,
struct sc_pkcs15_object *object, struct pkcs15_any_object **skey_object)
{
struct pkcs15_any_object *any_object = NULL;
struct pkcs15_skey_object *skey = NULL;
int rv;
rv = __pkcs15_create_object(fw_data, &any_object,
object, &pkcs15_skey_ops, sizeof(struct pkcs15_skey_object));
skey = (struct pkcs15_skey_object *)any_object;
if (rv >= 0)
skey->info = (struct sc_pkcs15_skey_info *)object->data;
if (skey_object != NULL)
*skey_object = any_object;
return rv;
}
static int
pkcs15_create_pkcs11_objects(struct pkcs15_fw_data *fw_data, int p15_type, const char *name,
int (*create)(struct pkcs15_fw_data *, struct sc_pkcs15_object *,
struct pkcs15_any_object **any_object))
{
struct sc_pkcs15_object *p15_object[MAX_OBJECTS];
int i, count, rv;
rv = count = sc_pkcs15_get_objects(fw_data->p15_card, p15_type, p15_object, MAX_OBJECTS);
if (rv >= 0)
sc_log(context, "Found %d %s%s", count, name, (count == 1)? "" : "s");
for (i = 0; rv >= 0 && i < count; i++)
rv = create(fw_data, p15_object[i], NULL);
return count;
}
static void
__pkcs15_prkey_bind_related(struct pkcs15_fw_data *fw_data, struct pkcs15_prkey_object *pk)
{
struct sc_pkcs15_id *id = &pk->prv_info->id;
unsigned int i;
sc_log(context, "Object is a private key and has id %s", sc_pkcs15_print_id(id));
for (i = 0; i < fw_data->num_objects; i++) {
struct pkcs15_any_object *obj = fw_data->objects[i];
if (obj->base.flags & SC_PKCS11_OBJECT_HIDDEN)
continue;
if (is_privkey(obj) && obj != (struct pkcs15_any_object *) pk) {
/* merge private keys with the same ID and
* different usage bits */
struct pkcs15_prkey_object *other, **pp;
other = (struct pkcs15_prkey_object *) obj;
if (sc_pkcs15_compare_id(&other->prv_info->id, id)) {
obj->base.flags |= SC_PKCS11_OBJECT_HIDDEN;
for (pp = &pk->prv_next; *pp; pp = &(*pp)->prv_next)
;
*pp = (struct pkcs15_prkey_object *) obj;
}
}
else if (is_pubkey(obj) && !pk->prv_pubkey) {
struct pkcs15_pubkey_object *pubkey;
pubkey = (struct pkcs15_pubkey_object *) obj;
if (sc_pkcs15_compare_id(&pubkey->pub_info->id, id)) {
sc_log(context, "Associating object %d as public key", i);
pk->prv_pubkey = pubkey;
if (pubkey->pub_data) {
sc_pkcs15_dup_pubkey(context, pubkey->pub_data, &pk->pub_data);
if (pk->prv_info->modulus_length == 0)
pk->prv_info->modulus_length = pubkey->pub_info->modulus_length;
}
}
}
}
}
static void
__pkcs15_cert_bind_related(struct pkcs15_fw_data *fw_data, struct pkcs15_cert_object *cert)
{
struct sc_pkcs15_cert *c1 = cert->cert_data;
struct sc_pkcs15_id *id = &cert->cert_info->id;
unsigned int i;
sc_log(context, "Object is a certificate and has id %s", sc_pkcs15_print_id(id));
/* Loop over all objects to see if we find the certificate of
* the issuer and the associated private key */
for (i = 0; i < fw_data->num_objects; i++) {
struct pkcs15_any_object *obj = fw_data->objects[i];
if (is_cert(obj) && obj != (struct pkcs15_any_object *) cert) {
struct pkcs15_cert_object *cert2;
struct sc_pkcs15_cert *c2;
cert2 = (struct pkcs15_cert_object *) obj;
c2 = cert2->cert_data;
if (!c1 || !c2 || !c1->issuer_len || !c2->subject_len)
continue;
if (c1->issuer_len == c2->subject_len
&& !memcmp(c1->issuer, c2->subject, c1->issuer_len)) {
sc_log(context, "Associating object %d (id %s) as issuer",
i, sc_pkcs15_print_id(&cert2->cert_info->id));
cert->cert_issuer = (struct pkcs15_cert_object *) obj;
return;
}
} else
if (is_privkey(obj) && !cert->cert_prvkey) {
struct pkcs15_prkey_object *pk;
pk = (struct pkcs15_prkey_object *) obj;
if (sc_pkcs15_compare_id(&pk->prv_info->id, id)) {
sc_log(context, "Associating object %d as private key", i);
cert->cert_prvkey = pk;
}
}
}
}
static void
pkcs15_bind_related_objects(struct pkcs15_fw_data *fw_data)
{
unsigned int i;
/* Loop over all private keys and attached related certificate
* and/or public key
*/
for (i = 0; i < fw_data->num_objects; i++) {
struct pkcs15_any_object *obj = fw_data->objects[i];
if (obj->base.flags & SC_PKCS11_OBJECT_HIDDEN)
continue;
sc_log(context, "Looking for objects related to object %d", i);