-
Notifications
You must be signed in to change notification settings - Fork 751
/
tpm_specific.c
2802 lines (2386 loc) · 69.3 KB
/
tpm_specific.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
/*
* The Initial Developer of the Original Code is International
* Business Machines Corporation. Portions created by IBM
* Corporation are Copyright (C) 2005 International Business
* Machines Corporation. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the Common Public License as published by
* IBM Corporation; either version 1 of the License, or (at your option)
* any later version.
*
* This program 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
* Common Public License for more details.
*
* You should have received a copy of the Common Public License
* along with this program; if not, a copy can be viewed at
* http://www.opensource.org/licenses/cpl1.0.php.
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2012 Milan Jurik. All rights reserved.
* Copyright (c) 2016 by Delphix. All rights reserved.
* Copyright 2018 Jason King
*/
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <uuid/uuid.h>
#include <fcntl.h>
#include <errno.h>
#include <pwd.h>
#include <syslog.h>
#include <sys/crypto/common.h> /* For CRYPTO_BYTES2BITS */
#include <rsa_impl.h>
#include <padding.h>
#include <tss/platform.h>
#include <tss/tss_defines.h>
#include <tss/tss_typedef.h>
#include <tss/tss_structs.h>
#include <tss/tss_error.h>
#include <tss/tcs_error.h>
#include <tss/tspi.h>
#include <trousers/trousers.h>
#include "tpmtok_int.h"
#include "tpmtok_defs.h"
#define MAX_RSA_KEYLENGTH 512
extern void stlogit(char *fmt, ...);
CK_RV token_rng(TSS_HCONTEXT, CK_BYTE *, CK_ULONG);
int tok_slot2local(CK_SLOT_ID);
CK_RV token_specific_session(CK_SLOT_ID);
CK_RV token_specific_final(TSS_HCONTEXT);
CK_RV
token_specific_rsa_decrypt(
TSS_HCONTEXT,
CK_BYTE *,
CK_ULONG,
CK_BYTE *,
CK_ULONG *,
OBJECT *);
CK_RV
token_specific_rsa_encrypt(
TSS_HCONTEXT,
CK_BYTE *,
CK_ULONG,
CK_BYTE *,
CK_ULONG *,
OBJECT *);
CK_RV
token_specific_rsa_sign(
TSS_HCONTEXT,
CK_BYTE *,
CK_ULONG,
CK_BYTE *,
CK_ULONG *,
OBJECT *);
CK_RV
token_specific_rsa_verify(TSS_HCONTEXT, CK_BYTE *,
CK_ULONG, CK_BYTE *, CK_ULONG, OBJECT *);
CK_RV
token_specific_rsa_generate_keypair(TSS_HCONTEXT,
TEMPLATE *,
TEMPLATE *);
CK_RV
token_specific_sha_init(DIGEST_CONTEXT *);
CK_RV
token_specific_sha_update(DIGEST_CONTEXT *,
CK_BYTE *,
CK_ULONG);
CK_RV
token_specific_sha_final(DIGEST_CONTEXT *,
CK_BYTE *,
CK_ULONG *);
CK_RV token_specific_login(TSS_HCONTEXT, CK_USER_TYPE, CK_CHAR_PTR, CK_ULONG);
CK_RV token_specific_logout(TSS_HCONTEXT);
CK_RV token_specific_init_pin(TSS_HCONTEXT, CK_CHAR_PTR, CK_ULONG);
CK_RV token_specific_set_pin(ST_SESSION_HANDLE, CK_CHAR_PTR,
CK_ULONG, CK_CHAR_PTR, CK_ULONG);
CK_RV token_specific_verify_so_pin(TSS_HCONTEXT, CK_CHAR_PTR, CK_ULONG);
static CK_RV
token_specific_init(char *, CK_SLOT_ID, TSS_HCONTEXT *);
struct token_specific_struct token_specific = {
"TPM_Debug",
&token_specific_init,
NULL,
&token_rng,
&token_specific_session,
&token_specific_final,
&token_specific_rsa_decrypt,
&token_specific_rsa_encrypt,
&token_specific_rsa_sign,
&token_specific_rsa_verify,
&token_specific_rsa_generate_keypair,
NULL,
NULL,
NULL,
&token_specific_login,
&token_specific_logout,
&token_specific_init_pin,
&token_specific_set_pin,
&token_specific_verify_so_pin
};
/* The context we'll use globally to connect to the TSP */
/* TSP key handles */
TSS_HKEY hPublicRootKey = NULL_HKEY;
TSS_HKEY hPublicLeafKey = NULL_HKEY;
TSS_HKEY hPrivateRootKey = NULL_HKEY;
TSS_HKEY hPrivateLeafKey = NULL_HKEY;
TSS_UUID publicRootKeyUUID;
TSS_UUID publicLeafKeyUUID;
TSS_UUID privateRootKeyUUID;
TSS_UUID privateLeafKeyUUID;
/* TSP policy handles */
TSS_HPOLICY hDefaultPolicy = NULL_HPOLICY;
/* PKCS#11 key handles */
int not_initialized = 0;
CK_BYTE current_user_pin_sha[SHA1_DIGEST_LENGTH];
CK_BYTE current_so_pin_sha[SHA1_DIGEST_LENGTH];
static TPM_CAP_VERSION_INFO tpmvinfo;
static CK_RV
verify_user_pin(TSS_HCONTEXT, CK_BYTE *);
static TSS_RESULT
tss_assign_secret_key_policy(TSS_HCONTEXT, TSS_FLAG, TSS_HKEY, CK_CHAR *);
static TSS_RESULT
set_legacy_key_params(TSS_HKEY);
static void
local_uuid_clear(TSS_UUID *uuid)
{
if (uuid == NULL)
return;
(void) memset(uuid, 0, sizeof (TSS_UUID));
}
/* convert from TSS_UUID to uuid_t */
static void
tss_uuid_convert_from(TSS_UUID *uu, uuid_t ptr)
{
uint_t tmp;
uchar_t *out = ptr;
tmp = ntohl(uu->ulTimeLow);
out[3] = (uchar_t)tmp;
tmp >>= 8;
out[2] = (uchar_t)tmp;
tmp >>= 8;
out[1] = (uchar_t)tmp;
tmp >>= 8;
out[0] = (uchar_t)tmp;
tmp = ntohs(uu->usTimeMid);
out[5] = (uchar_t)tmp;
tmp >>= 8;
out[4] = (uchar_t)tmp;
tmp = ntohs(uu->usTimeHigh);
out[7] = (uchar_t)tmp;
tmp >>= 8;
out[6] = (uchar_t)tmp;
tmp = uu->bClockSeqHigh;
out[8] = (uchar_t)tmp;
tmp = uu->bClockSeqLow;
out[9] = (uchar_t)tmp;
(void) memcpy(out+10, uu->rgbNode, 6);
}
/* convert from uuid_t to TSS_UUID */
static void
tss_uuid_convert_to(TSS_UUID *uuid, uuid_t in)
{
uchar_t *ptr;
uint32_t ltmp;
uint16_t stmp;
ptr = in;
ltmp = *ptr++;
ltmp = (ltmp << 8) | *ptr++;
ltmp = (ltmp << 8) | *ptr++;
ltmp = (ltmp << 8) | *ptr++;
uuid->ulTimeLow = ntohl(ltmp);
stmp = *ptr++;
stmp = (stmp << 8) | *ptr++;
uuid->usTimeMid = ntohs(stmp);
stmp = *ptr++;
stmp = (stmp << 8) | *ptr++;
uuid->usTimeHigh = ntohs(stmp);
uuid->bClockSeqHigh = *ptr++;
uuid->bClockSeqLow = *ptr++;
(void) memcpy(uuid->rgbNode, ptr, 6);
}
static void
local_uuid_copy(TSS_UUID *dst, TSS_UUID *src)
{
uuid_t udst, usrc;
tss_uuid_convert_from(dst, udst);
tss_uuid_convert_from(src, usrc);
uuid_copy(udst, usrc);
tss_uuid_convert_to(dst, udst);
}
static void
local_uuid_generate(TSS_UUID *uu)
{
uuid_t newuuid;
uuid_generate(newuuid);
tss_uuid_convert_to(uu, newuuid);
}
static int
local_copy_file(char *dst, char *src)
{
FILE *fdest, *fsrc;
char line[BUFSIZ];
fdest = fopen(dst, "w");
if (fdest == NULL)
return (-1);
fsrc = fopen(src, "r");
if (fsrc == NULL) {
(void) fclose(fdest);
return (-1);
}
while (fread(line, sizeof (line), 1, fsrc))
(void) fprintf(fdest, "%s\n", line);
(void) fclose(fsrc);
(void) fclose(fdest);
return (0);
}
static int
remove_uuid(char *keyname)
{
int ret = 0;
FILE *fp, *newfp;
char fname[MAXPATHLEN];
char line[BUFSIZ], key[BUFSIZ], idstr[BUFSIZ];
char *tmpfname;
char *p = get_tpm_keystore_path();
if (p == NULL)
return (-1);
(void) snprintf(fname, sizeof (fname),
"%s/%s", p, TPMTOK_UUID_INDEX_FILENAME);
fp = fopen(fname, "r");
if (fp == NULL) {
return (-1);
}
tmpfname = tempnam("/tmp", "tpmtok");
newfp = fopen(tmpfname, "w+");
if (newfp == NULL) {
free(tmpfname);
(void) fclose(fp);
return (-1);
}
while (!feof(fp)) {
(void) fgets(line, sizeof (line), fp);
if (sscanf(line, "%1024s %1024s", key, idstr) == 2) {
if (strcmp(key, keyname))
(void) fprintf(newfp, "%s\n", line);
}
}
(void) fclose(fp);
(void) fclose(newfp);
if (local_copy_file(fname, tmpfname) == 0)
(void) unlink(tmpfname);
free(tmpfname);
return (ret);
}
static int
find_uuid(char *keyname, TSS_UUID *uu)
{
int ret = 0, found = 0;
FILE *fp = NULL;
char fname[MAXPATHLEN];
char line[BUFSIZ], key[BUFSIZ], idstr[BUFSIZ];
uuid_t uuid;
char *p = get_tpm_keystore_path();
if (p == NULL)
return (-1);
tss_uuid_convert_from(uu, uuid);
(void) snprintf(fname, sizeof (fname),
"%s/%s", p, TPMTOK_UUID_INDEX_FILENAME);
/* Open UUID Index file */
fp = fopen(fname, "r");
if (fp == NULL) {
if (errno == ENOENT) {
/* initialize the file */
fp = fopen(fname, "w");
if (fp != NULL)
(void) fclose(fp);
}
return (-1);
}
while (!feof(fp)) {
(void) fgets(line, sizeof (line), fp);
if (sscanf(line, "%1024s %1024s", key, idstr) == 2) {
if (strcmp(key, keyname) == 0) {
ret = uuid_parse(idstr, uuid);
if (ret == 0) {
found = 1;
tss_uuid_convert_to(uu,
uuid);
}
break;
}
}
}
(void) fclose(fp);
if (!found)
ret = -1;
return (ret);
}
static int
local_uuid_is_null(TSS_UUID *uu)
{
uuid_t uuid;
int nulluuid;
tss_uuid_convert_from(uu, uuid);
nulluuid = uuid_is_null(uuid);
return (nulluuid);
}
static int
add_uuid(char *keyname, TSS_UUID *uu)
{
FILE *fp = NULL;
char fname[MAXPATHLEN];
char idstr[BUFSIZ];
uuid_t uuid;
char *p = get_tpm_keystore_path();
if (p == NULL)
return (-1);
tss_uuid_convert_from(uu, uuid);
if (uuid_is_null(uuid))
return (-1);
uuid_unparse(uuid, idstr);
(void) snprintf(fname, sizeof (fname),
"%s/%s", p, TPMTOK_UUID_INDEX_FILENAME);
fp = fopen(fname, "a");
if (fp == NULL)
return (-1);
(void) fprintf(fp, "%s %s\n", keyname, idstr);
(void) fclose(fp);
return (0);
}
static UINT32
util_get_keysize_flag(CK_ULONG size)
{
switch (size) {
case 512:
return (TSS_KEY_SIZE_512);
case 1024:
return (TSS_KEY_SIZE_1024);
case 2048:
return (TSS_KEY_SIZE_2048);
default:
break;
}
return (0);
}
/* make sure the public exponent attribute is 65537 */
static CK_ULONG
util_check_public_exponent(TEMPLATE *tmpl)
{
CK_BBOOL flag;
CK_ATTRIBUTE *publ_exp_attr;
CK_BYTE pubexp_bytes[] = { 1, 0, 1 };
CK_ULONG publ_exp, rc = 1;
flag = template_attribute_find(tmpl, CKA_PUBLIC_EXPONENT,
&publ_exp_attr);
if (!flag) {
LogError1("Couldn't find public exponent attribute");
return (CKR_TEMPLATE_INCOMPLETE);
}
switch (publ_exp_attr->ulValueLen) {
case 3:
rc = memcmp(pubexp_bytes, publ_exp_attr->pValue, 3);
break;
case sizeof (CK_ULONG):
publ_exp = *((CK_ULONG *)publ_exp_attr->pValue);
if (publ_exp == 65537)
rc = 0;
break;
default:
break;
}
return (rc);
}
TSS_RESULT
set_public_modulus(TSS_HCONTEXT hContext, TSS_HKEY hKey,
unsigned long size_n, unsigned char *n)
{
UINT64 offset;
UINT32 blob_size;
BYTE *blob, pub_blob[1024];
TCPA_PUBKEY pub_key;
TSS_RESULT result;
/* Get the TCPA_PUBKEY blob from the key object. */
result = Tspi_GetAttribData(hKey, TSS_TSPATTRIB_KEY_BLOB,
TSS_TSPATTRIB_KEYBLOB_PUBLIC_KEY, &blob_size, &blob);
if (result != TSS_SUCCESS) {
stlogit("Tspi_GetAttribData failed: rc=0x%0x - %s\n",
result, Trspi_Error_String(result));
return (result);
}
offset = 0;
result = Trspi_UnloadBlob_PUBKEY(&offset, blob, &pub_key);
if (result != TSS_SUCCESS) {
stlogit("Trspi_UnloadBlob_PUBKEY failed: rc=0x%0x - %s\n",
result, Trspi_Error_String(result));
return (result);
}
Tspi_Context_FreeMemory(hContext, blob);
/* Free the first dangling reference, putting 'n' in its place */
free(pub_key.pubKey.key);
pub_key.pubKey.keyLength = size_n;
pub_key.pubKey.key = n;
offset = 0;
Trspi_LoadBlob_PUBKEY(&offset, pub_blob, &pub_key);
/* Free the second dangling reference */
free(pub_key.algorithmParms.parms);
/* set the public key data in the TSS object */
result = Tspi_SetAttribData(hKey, TSS_TSPATTRIB_KEY_BLOB,
TSS_TSPATTRIB_KEYBLOB_PUBLIC_KEY, (UINT32)offset, pub_blob);
if (result != TSS_SUCCESS) {
stlogit("Tspi_SetAttribData failed: rc=0x%0x - %s\n",
result, Trspi_Error_String(result));
return (result);
}
return (result);
}
/*
* Get details about the TPM to put into the token_info structure.
*/
CK_RV
token_get_tpm_info(TSS_HCONTEXT hContext, TOKEN_DATA *td)
{
TSS_RESULT result;
TPM_CAPABILITY_AREA capArea = TSS_TPMCAP_VERSION_VAL;
UINT32 datalen;
BYTE *data;
TSS_HTPM hTPM;
if ((result = Tspi_Context_GetTpmObject(hContext, &hTPM))) {
stlogit("Tspi_Context_GetTpmObject: 0x%0x - %s",
result, Trspi_Error_String(result));
return (CKR_FUNCTION_FAILED);
}
if ((result = Tspi_TPM_GetCapability(hTPM,
capArea, 0, NULL, &datalen, &data)) != 0 || datalen == 0 ||
data == NULL) {
stlogit("Tspi_Context_GetCapability: 0x%0x - %s",
result, Trspi_Error_String(result));
return (CKR_FUNCTION_FAILED);
}
if (datalen > sizeof (tpmvinfo)) {
Tspi_Context_FreeMemory(hContext, data);
return (CKR_FUNCTION_FAILED);
}
(void) memcpy(&tpmvinfo, (void *)data, datalen);
bzero(td->token_info.manufacturerID,
sizeof (td->token_info.manufacturerID));
(void) memset(td->token_info.manufacturerID, ' ',
sizeof (td->token_info.manufacturerID) - 1);
(void) memcpy(td->token_info.manufacturerID,
tpmvinfo.tpmVendorID, sizeof (tpmvinfo.tpmVendorID));
(void) memset(td->token_info.label, ' ',
sizeof (td->token_info.label) - 1);
(void) memcpy(td->token_info.label, "TPM", 3);
td->token_info.hardwareVersion.major = tpmvinfo.version.major;
td->token_info.hardwareVersion.minor = tpmvinfo.version.minor;
td->token_info.firmwareVersion.major = tpmvinfo.version.revMajor;
td->token_info.firmwareVersion.minor = tpmvinfo.version.revMinor;
Tspi_Context_FreeMemory(hContext, data);
return (CKR_OK);
}
/*ARGSUSED*/
CK_RV
token_specific_session(CK_SLOT_ID slotid)
{
return (CKR_OK);
}
CK_RV
token_rng(TSS_HCONTEXT hContext, CK_BYTE *output, CK_ULONG bytes)
{
TSS_RESULT rc;
TSS_HTPM hTPM;
BYTE *random_bytes = NULL;
if ((rc = Tspi_Context_GetTpmObject(hContext, &hTPM))) {
stlogit("Tspi_Context_GetTpmObject: 0x%0x - %s",
rc, Trspi_Error_String(rc));
return (CKR_FUNCTION_FAILED);
}
if ((rc = Tspi_TPM_GetRandom(hTPM, bytes, &random_bytes))) {
stlogit("Tspi_TPM_GetRandom: 0x%0x - %s",
rc, Trspi_Error_String(rc));
return (CKR_FUNCTION_FAILED);
}
(void) memcpy(output, random_bytes, bytes);
Tspi_Context_FreeMemory(hContext, random_bytes);
return (CKR_OK);
}
TSS_RESULT
open_tss_context(TSS_HCONTEXT *pContext)
{
TSS_RESULT result;
if ((result = Tspi_Context_Create(pContext))) {
stlogit("Tspi_Context_Create: 0x%0x - %s",
result, Trspi_Error_String(result));
return (CKR_FUNCTION_FAILED);
}
if ((result = Tspi_Context_Connect(*pContext, NULL))) {
stlogit("Tspi_Context_Connect: 0x%0x - %s",
result, Trspi_Error_String(result));
Tspi_Context_Close(*pContext);
*pContext = 0;
return (CKR_FUNCTION_FAILED);
}
return (result);
}
/*ARGSUSED*/
static CK_RV
token_specific_init(char *Correlator, CK_SLOT_ID SlotNumber,
TSS_HCONTEXT *hContext)
{
TSS_RESULT result;
result = open_tss_context(hContext);
if (result)
return (CKR_FUNCTION_FAILED);
if ((result = Tspi_Context_GetDefaultPolicy(*hContext,
&hDefaultPolicy))) {
stlogit("Tspi_Context_GetDefaultPolicy: 0x%0x - %s",
result, Trspi_Error_String(result));
return (CKR_FUNCTION_FAILED);
}
local_uuid_clear(&publicRootKeyUUID);
local_uuid_clear(&privateRootKeyUUID);
local_uuid_clear(&publicLeafKeyUUID);
local_uuid_clear(&privateLeafKeyUUID);
result = token_get_tpm_info(*hContext, nv_token_data);
return (result);
}
/*
* Given a modulus and prime from an RSA key, create a TSS_HKEY object by
* wrapping the RSA key with a key from the TPM (SRK or other previously stored
* key).
*/
static CK_RV
token_wrap_sw_key(
TSS_HCONTEXT hContext,
int size_n,
unsigned char *n,
int size_p,
unsigned char *p,
TSS_HKEY hParentKey,
TSS_FLAG initFlags,
TSS_HKEY *phKey)
{
TSS_RESULT result;
UINT32 key_size;
key_size = util_get_keysize_flag(size_n * 8);
if (initFlags == 0) {
return (CKR_FUNCTION_FAILED);
}
/* create the TSS key object */
result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_RSAKEY,
TSS_KEY_MIGRATABLE | initFlags | key_size, phKey);
if (result != TSS_SUCCESS) {
stlogit("Tspi_Context_CreateObject: 0x%0x - %s",
result, Trspi_Error_String(result));
return (CKR_FUNCTION_FAILED);
}
result = set_public_modulus(hContext, *phKey, size_n, n);
if (result != TSS_SUCCESS) {
Tspi_Context_CloseObject(hContext, *phKey);
*phKey = NULL_HKEY;
return (CKR_FUNCTION_FAILED);
}
/* set the private key data in the TSS object */
result = Tspi_SetAttribData(*phKey, TSS_TSPATTRIB_KEY_BLOB,
TSS_TSPATTRIB_KEYBLOB_PRIVATE_KEY, size_p, p);
if (result != TSS_SUCCESS) {
stlogit("Tspi_SetAttribData: 0x%x - %s",
result, Trspi_Error_String(result));
Tspi_Context_CloseObject(hContext, *phKey);
*phKey = NULL_HKEY;
return (CKR_FUNCTION_FAILED);
}
result = tss_assign_secret_key_policy(hContext, TSS_POLICY_MIGRATION,
*phKey, NULL);
if (TPMTOK_TSS_KEY_TYPE(initFlags) == TSS_KEY_TYPE_LEGACY) {
if ((result = Tspi_SetAttribUint32(*phKey,
TSS_TSPATTRIB_KEY_INFO, TSS_TSPATTRIB_KEYINFO_ENCSCHEME,
TSS_ES_RSAESPKCSV15))) {
stlogit("Tspi_SetAttribUint32: 0x%0x - %s\n",
result, Trspi_Error_String(result));
Tspi_Context_CloseObject(hContext, *phKey);
return (CKR_FUNCTION_FAILED);
}
if ((result = Tspi_SetAttribUint32(*phKey,
TSS_TSPATTRIB_KEY_INFO, TSS_TSPATTRIB_KEYINFO_SIGSCHEME,
TSS_SS_RSASSAPKCS1V15_DER))) {
stlogit("Tspi_SetAttribUint32: 0x%0x - %s\n",
result, Trspi_Error_String(result));
Tspi_Context_CloseObject(hContext, *phKey);
return (CKR_FUNCTION_FAILED);
}
}
result = Tspi_Key_WrapKey(*phKey, hParentKey, NULL_HPCRS);
if (result != TSS_SUCCESS) {
stlogit("Tspi_Key_WrapKey: 0x%0x - %s",
result, Trspi_Error_String(result));
Tspi_Context_CloseObject(hContext, *phKey);
*phKey = NULL_HKEY;
return (CKR_FUNCTION_FAILED);
}
return (CKR_OK);
}
/*
* Create a TPM key blob for an imported key. This function is only called when
* a key is in active use, so any failure should trickle through.
*/
static CK_RV
token_wrap_key_object(TSS_HCONTEXT hContext,
CK_OBJECT_HANDLE ckObject,
TSS_HKEY hParentKey, TSS_HKEY *phKey)
{
CK_RV rc = CKR_OK;
CK_ATTRIBUTE *attr = NULL, *new_attr, *prime_attr;
CK_ULONG class, key_type;
OBJECT *obj;
TSS_RESULT result;
TSS_FLAG initFlags = 0;
BYTE *rgbBlob;
UINT32 ulBlobLen;
if ((rc = object_mgr_find_in_map1(hContext, ckObject, &obj))) {
return (rc);
}
/* if the object isn't a key, fail */
if (template_attribute_find(obj->template, CKA_KEY_TYPE,
&attr) == FALSE) {
return (CKR_TEMPLATE_INCOMPLETE);
}
key_type = *((CK_ULONG *)attr->pValue);
if (key_type != CKK_RSA) {
return (CKR_TEMPLATE_INCONSISTENT);
}
if (template_attribute_find(obj->template, CKA_CLASS,
&attr) == FALSE) {
return (CKR_TEMPLATE_INCOMPLETE);
}
class = *((CK_ULONG *)attr->pValue);
if (class == CKO_PRIVATE_KEY) {
/*
* In order to create a full TSS key blob using a PKCS#11
* private key object, we need one of the two primes, the
* modulus and the private exponent and we need the public
* exponent to be correct.
*/
/*
* Check the least likely attribute to exist first, the
* primes.
*/
if (template_attribute_find(obj->template, CKA_PRIME_1,
&prime_attr) == FALSE) {
if (template_attribute_find(obj->template,
CKA_PRIME_2, &prime_attr) == FALSE) {
return (CKR_TEMPLATE_INCOMPLETE);
}
}
/* Make sure the public exponent is usable */
if ((rc = util_check_public_exponent(obj->template))) {
return (CKR_TEMPLATE_INCONSISTENT);
}
/* get the modulus */
if (template_attribute_find(obj->template, CKA_MODULUS,
&attr) == FALSE) {
return (CKR_TEMPLATE_INCOMPLETE);
}
/* make sure the key size is usable */
initFlags = util_get_keysize_flag(attr->ulValueLen * 8);
if (initFlags == 0) {
return (CKR_TEMPLATE_INCONSISTENT);
}
/* generate the software based key */
if ((rc = token_wrap_sw_key(hContext,
(int)attr->ulValueLen, attr->pValue,
(int)prime_attr->ulValueLen, prime_attr->pValue,
hParentKey, TSS_KEY_TYPE_LEGACY | TSS_KEY_NO_AUTHORIZATION,
phKey))) {
return (rc);
}
} else if (class == CKO_PUBLIC_KEY) {
/* Make sure the public exponent is usable */
if ((util_check_public_exponent(obj->template))) {
return (CKR_TEMPLATE_INCONSISTENT);
}
/* grab the modulus to put into the TSS key object */
if (template_attribute_find(obj->template,
CKA_MODULUS, &attr) == FALSE) {
return (CKR_TEMPLATE_INCONSISTENT);
}
/* make sure the key size is usable */
initFlags = util_get_keysize_flag(attr->ulValueLen * 8);
if (initFlags == 0) {
return (CKR_TEMPLATE_INCONSISTENT);
}
initFlags |= TSS_KEY_MIGRATABLE | TSS_KEY_NO_AUTHORIZATION |
TSS_KEY_TYPE_LEGACY;
if ((result = Tspi_Context_CreateObject(hContext,
TSS_OBJECT_TYPE_RSAKEY, initFlags, phKey))) {
stlogit("Tspi_Context_CreateObject: 0x%0x - %s",
result, Trspi_Error_String(result));
return (result);
}
if ((result = set_public_modulus(hContext, *phKey,
attr->ulValueLen, attr->pValue))) {
Tspi_Context_CloseObject(hContext, *phKey);
*phKey = NULL_HKEY;
return (CKR_FUNCTION_FAILED);
}
result = tss_assign_secret_key_policy(hContext,
TSS_POLICY_MIGRATION, *phKey, NULL);
if (result) {
Tspi_Context_CloseObject(hContext, *phKey);
*phKey = NULL_HKEY;
return (CKR_FUNCTION_FAILED);
}
result = set_legacy_key_params(*phKey);
if (result) {
Tspi_Context_CloseObject(hContext, *phKey);
*phKey = NULL_HKEY;
return (CKR_FUNCTION_FAILED);
}
} else {
return (CKR_FUNCTION_FAILED);
}
/* grab the entire key blob to put into the PKCS#11 object */
if ((result = Tspi_GetAttribData(*phKey, TSS_TSPATTRIB_KEY_BLOB,
TSS_TSPATTRIB_KEYBLOB_BLOB, &ulBlobLen, &rgbBlob))) {
stlogit("Tspi_GetAttribData: 0x%0x - %s",
result, Trspi_Error_String(result));
return (CKR_FUNCTION_FAILED);
}
/* insert the key blob into the object */
if ((rc = build_attribute(CKA_IBM_OPAQUE, rgbBlob, ulBlobLen,
&new_attr))) {
Tspi_Context_FreeMemory(hContext, rgbBlob);
return (rc);
}
(void) template_update_attribute(obj->template, new_attr);
Tspi_Context_FreeMemory(hContext, rgbBlob);
/*
* If this is a token object, save it with the new attribute
* so that we don't have to go down this path again.
*/
if (!object_is_session_object(obj)) {
rc = save_token_object(hContext, obj);
}
return (rc);
}
static TSS_RESULT
tss_assign_secret_key_policy(TSS_HCONTEXT hContext, TSS_FLAG policyType,
TSS_HKEY hKey, CK_CHAR *passHash)
{
TSS_RESULT result;
TSS_HPOLICY hPolicy;
if ((result = Tspi_Context_CreateObject(hContext,
TSS_OBJECT_TYPE_POLICY, policyType, &hPolicy))) {
stlogit("Tspi_Context_CreateObject: 0x%0x - %s",
result, Trspi_Error_String(result));
return (result);
}
if ((result = Tspi_Policy_AssignToObject(hPolicy, hKey))) {
stlogit("Tspi_Policy_AssignToObject: 0x%0x - %s",
result, Trspi_Error_String(result));
goto done;
}
if (passHash == NULL) {
result = Tspi_Policy_SetSecret(hPolicy, TSS_SECRET_MODE_NONE,
0, NULL);
} else {
result = Tspi_Policy_SetSecret(hPolicy, TSS_SECRET_MODE_SHA1,
SHA1_DIGEST_LENGTH, passHash);
}
if (result != TSS_SUCCESS) {
stlogit("Tspi_Policy_SetSecret: 0x%0x - %s",
result, Trspi_Error_String(result));
goto done;
}
done:
if (result != TSS_SUCCESS)
Tspi_Context_CloseObject(hContext, hPolicy);
return (result);
}
/*
* Take a key from the TSS store (on-disk) and load it into the TPM, wrapped
* by an already TPM-resident key and protected with a PIN (optional).
*/
static CK_RV
token_load_key(
TSS_HCONTEXT hContext,
CK_OBJECT_HANDLE ckKey,
TSS_HKEY hParentKey,
CK_CHAR_PTR passHash,
TSS_HKEY *phKey)
{
TSS_RESULT result;
CK_RV rc;
/*
* The key blob wasn't found, load the parts of the key
* from the object DB and create a new key object that
* gets loaded into the TPM, wrapped with the parent key.
*/
if ((rc = token_wrap_key_object(hContext, ckKey,
hParentKey, phKey))) {
return (rc);
}
/*
* Assign the PIN hash (optional) to the newly loaded key object,
* if this PIN is incorrect, the TPM will not be able to decrypt
* the private key and use it.
*/
result = tss_assign_secret_key_policy(hContext, TSS_POLICY_USAGE,
*phKey, passHash);
return (result);
}