2
* Copyright (C) 2006-2007 International Business Machines Corp.
3
* Author(s): Mike Halcrow <mhalcrow@us.ibm.com>
4
* Kent Yoder <kyoder@users.sf.net>
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License as
8
* published by the Free Software Foundation; either version 2 of the
9
* License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22
#include <sys/types.h>
29
#include <trousers/tss.h>
31
#include "../include/ecryptfs.h"
32
#include "../include/decision_graph.h"
37
#define DBGSYSLOG(x, ...) syslog(LOG_DEBUG, x, ##__VA_ARGS__)
38
#define DBG_print_hex(a,b) print_hex(a,b)
41
print_hex(BYTE *buf, uint32_t len)
46
for (j=0; (j < 15) && (i < len); j++, i++)
47
syslog(LOG_INFO, "%02x\n", buf[i] & 0xff);
53
#define DBGSYSLOG(x, ...)
54
#define DBG_print_hex(a,b)
57
static TSS_UUID ecryptfs_tspi_srk_uuid = TSS_UUID_SRK;
59
static struct key_mapper {
62
struct key_mapper *next;
69
static void ecryptfs_tspi_to_hex(char *dst, char *src, int src_size)
73
for (x = 0; x < src_size; x++)
74
sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
77
static int ecryptfs_tspi_generate_signature(char *sig, BYTE *n, uint32_t nbytes)
80
char hash[SHA1_DIGEST_LENGTH];
82
BYTE e[] = { 1, 0, 1 }; /* The e for all TPM RSA keys */
85
len = 10 + nbytes + sizeof(e);
86
if ((data = malloc(3 + len)) == NULL) {
87
syslog(LOG_ERR, "Out of memory\n");
93
data[i++] = (char)(len >> 8);
94
data[i++] = (char)len;
101
data[i++] = (char)((nbytes * 8) >> 8);
102
data[i++] = (char)(nbytes * 8);
103
memcpy(&data[i], n, nbytes);
105
data[i++] = (char)((sizeof(e) * 8) >> 8);
106
data[i++] = (char)(sizeof(e) * 8);
107
memcpy(&data[i], e, sizeof(e));
109
SHA1(data, len + 3, hash);
110
ecryptfs_tspi_to_hex(sig, hash, ECRYPTFS_SIG_SIZE);
111
sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
118
ecryptfs_tspi_deserialize(struct tspi_data *tspi_data, unsigned char *blob)
122
memcpy(&tspi_data->uuid, blob, sizeof(TSS_UUID));
127
static int ecryptfs_tspi_get_key_sig(unsigned char *sig, unsigned char *blob)
129
struct tspi_data tspi_data;
137
ecryptfs_tspi_deserialize(&tspi_data, blob);
138
if ((result = Tspi_Context_Create(&h_ctx)) != TSS_SUCCESS) {
139
syslog(LOG_ERR, "Tspi_Context_Create failed: [%s]\n",
140
Trspi_Error_String(result));
144
DBG_print_hex((BYTE *)&tspi_data.uuid, sizeof(TSS_UUID));
145
if ((result = Tspi_Context_GetKeyByUUID(h_ctx, TSS_PS_TYPE_USER,
146
tspi_data.uuid, &hKey))
148
syslog(LOG_ERR, "Tspi_Context_GetKeyByUUID failed: [%s]\n",
149
Trspi_Error_String(result));
153
if ((result = Tspi_GetAttribData(hKey, TSS_TSPATTRIB_RSAKEY_INFO,
154
TSS_TSPATTRIB_KEYINFO_RSA_MODULUS,
157
syslog(LOG_ERR, "Tspi_GetAttribUint32 failed: [%s]\n",
158
Trspi_Error_String(result));
162
rc = ecryptfs_tspi_generate_signature(sig, n, size_n);
167
static pthread_mutex_t encrypt_lock = PTHREAD_MUTEX_INITIALIZER;
170
ecryptfs_tspi_encrypt(char *to, size_t *to_size, char *from, size_t from_size,
171
unsigned char *blob, int blob_type)
173
static TSS_HPOLICY h_srk_policy = 0;
174
static TSS_HKEY h_srk = 0;
176
TSS_HCONTEXT h_encrypt_ctx;
178
TSS_HENCDATA h_encdata;
179
uint32_t encdata_size;
181
struct tspi_data tspi_data;
184
pthread_mutex_lock(&encrypt_lock);
186
ecryptfs_tspi_deserialize(&tspi_data, blob);
187
DBG_print_hex((BYTE *)&tspi_data.uuid, sizeof(TSS_UUID));
188
if ((result = Tspi_Context_Create(&h_encrypt_ctx)) != TSS_SUCCESS) {
189
syslog(LOG_ERR, "Tspi_Context_Create failed: [%s]\n",
190
Trspi_Error_String(result));
194
if ((result = Tspi_Context_Connect(h_encrypt_ctx, NULL))
196
syslog(LOG_ERR, "Tspi_Context_Connect failed: [%s]\n",
197
Trspi_Error_String(result));
201
if ((result = Tspi_Context_LoadKeyByUUID(h_encrypt_ctx,
203
ecryptfs_tspi_srk_uuid,
204
&h_srk)) != TSS_SUCCESS) {
205
syslog(LOG_ERR, "Tspi_Context_LoadKeyByUUID failed: [%s]\n",
206
Trspi_Error_String(result));
210
if ((result = Tspi_GetPolicyObject(h_srk, TSS_POLICY_USAGE,
213
syslog(LOG_ERR, "Tspi_GetPolicyObject failed: [%s]\n",
214
Trspi_Error_String(result));
218
if ((result = Tspi_Policy_SetSecret(h_srk_policy, TSS_SECRET_MODE_PLAIN,
221
syslog(LOG_ERR, "Tspi_Policy_SetSecret failed: [%s]\n",
222
Trspi_Error_String(result));
226
if ((result = Tspi_Context_CreateObject(h_encrypt_ctx,
227
TSS_OBJECT_TYPE_ENCDATA,
228
TSS_ENCDATA_SEAL, &h_encdata))
230
syslog(LOG_ERR, "Tspi_Context_CreateObject failed: [%s]\n",
231
Trspi_Error_String(result));
235
if ((result = Tspi_Context_LoadKeyByUUID(h_encrypt_ctx,
237
tspi_data.uuid, &hKey))
239
syslog(LOG_ERR, "Tspi_Context_LoadKeyByUUID failed: [%s]\n",
240
Trspi_Error_String(result));
244
if ((result = Tspi_Data_Seal(h_encdata, hKey, from_size, from, 0))
246
syslog(LOG_ERR, "Tspi_Data_Seal failed: [%s]\n",
247
Trspi_Error_String(result));
251
if ((result = Tspi_GetAttribData(h_encdata, TSS_TSPATTRIB_ENCDATA_BLOB,
252
TSS_TSPATTRIB_ENCDATABLOB_BLOB,
253
&encdata_size, &encdata))
255
syslog(LOG_ERR, "Tspi_GetAttribData failed: [%s]\n",
256
Trspi_Error_String(result));
260
(*to_size) = encdata_size;
261
Tspi_Context_FreeMemory(h_encrypt_ctx, encdata);
263
pthread_mutex_unlock(&encrypt_lock);
267
static pthread_mutex_t decrypt_lock = PTHREAD_MUTEX_INITIALIZER;
270
ecryptfs_tspi_decrypt(char *to, size_t *to_size, char *from, size_t from_size,
271
unsigned char *blob, int blob_type)
273
static TSS_HCONTEXT h_decrypt_context = 0;
274
static TSS_HPOLICY h_srk_policy = 0;
275
static TSS_HKEY h_srk = 0;
276
static TSS_HENCDATA h_encdata;
277
uint32_t encdata_bytes;
279
struct tspi_data tspi_data;
280
struct key_mapper *walker, *new_mapper;
284
pthread_mutex_lock(&decrypt_lock);
285
ecryptfs_tspi_deserialize(&tspi_data, blob);
286
if (h_decrypt_context == 0) {
287
if ((result = Tspi_Context_Create(&h_decrypt_context))
289
syslog(LOG_ERR, "Tspi_Context_Create failed: [%s]\n",
290
Trspi_Error_String(result));
294
DBGSYSLOG("New TSP context: 0x%x", h_decrypt_context);
295
if ((result = Tspi_Context_Connect(h_decrypt_context, NULL))
297
syslog(LOG_ERR, "Tspi_Context_Connect failed: [%s]\n",
298
Trspi_Error_String(result));
302
if ((result = Tspi_Context_LoadKeyByUUID(h_decrypt_context,
304
ecryptfs_tspi_srk_uuid,
305
&h_srk)) != TSS_SUCCESS) {
307
"Tspi_Context_LoadKeyByUUID failed: [%s]\n",
308
Trspi_Error_String(result));
312
if ((result = Tspi_GetPolicyObject(h_srk, TSS_POLICY_USAGE,
315
syslog(LOG_ERR, "Tspi_GetPolicyObject failed: [%s]\n",
316
Trspi_Error_String(result));
320
if ((result = Tspi_Policy_SetSecret(h_srk_policy,
321
TSS_SECRET_MODE_PLAIN, 0, NULL))
323
syslog(LOG_ERR, "Tspi_Policy_SetSecret failed: [%s]\n",
324
Trspi_Error_String(result));
328
if ((result = Tspi_Context_CreateObject(h_decrypt_context,
329
TSS_OBJECT_TYPE_ENCDATA,
330
TSS_ENCDATA_SEAL, &h_encdata))
333
"Tspi_Context_CreateObject failed: [%s]\n",
334
Trspi_Error_String(result));
339
for (walker = mapper; walker; walker = walker->next)
340
if (!memcmp(&walker->uuid, &tspi_data.uuid, sizeof(TSS_UUID)))
343
if ((new_mapper = calloc(1, sizeof(struct key_mapper)))
345
syslog(LOG_ERR, "calloc failed: [%s]\n",
350
if ((result = Tspi_Context_LoadKeyByUUID(h_decrypt_context,
356
"Tspi_Context_LoadKeyByUUID failed: [%s]\n",
357
Trspi_Error_String(result));
361
DBGSYSLOG("New key object: [0x%x]\n", new_mapper->hKey);
362
memcpy(&new_mapper->uuid, &tspi_data.uuid, sizeof(TSS_UUID));
363
new_mapper->next = mapper;
364
walker = mapper = new_mapper;
366
if ((result = Tspi_SetAttribData(h_encdata, TSS_TSPATTRIB_ENCDATA_BLOB,
367
TSS_TSPATTRIB_ENCDATABLOB_BLOB,
368
from_size, (BYTE *)from))
370
syslog(LOG_ERR, "Tspi_SetAttribData failed: [%s]\n",
371
Trspi_Error_String(result));
375
if ((result = Tspi_Data_Unseal(h_encdata, walker->hKey,
376
&encdata_bytes, &encdata))
378
syslog(LOG_ERR, "Tspi_Data_Unseal failed: [%s]\n",
379
Trspi_Error_String(result));
383
(*to_size) = encdata_bytes;
385
memcpy(to, encdata, encdata_bytes);
386
Tspi_Context_FreeMemory(h_decrypt_context, encdata);
390
Tspi_Context_Close(h_decrypt_context);
391
h_decrypt_context = 0;
395
pthread_mutex_unlock(&decrypt_lock);
399
#define ECRYPTFS_KEY_MOD_PARAM_TSPI_UUID 1
400
static struct key_mod_param tspi_params[] = {
401
{.id = ECRYPTFS_KEY_MOD_PARAM_TSPI_UUID,
402
.flags = ECRYPTFS_PARAM_FLAG_ECHO_INPUT,
404
.description = "uuid",
405
.suggested_val = NULL,
412
.suggested_val = NULL,
417
static uint32_t tspi_num_params = 1;
420
* Convert user input string into TSS_UUID data type
422
static void string_to_uuid(TSS_UUID *uuid, char *str)
424
BYTE tmp[(sizeof(unsigned long) * 2 + 1)];
428
tmp[sizeof(unsigned long) * 2] = '\0';
429
for (i = 0; i < (sizeof(TSS_UUID) * 2);
430
i += (sizeof(unsigned long) * 2)) {
431
memcpy(tmp, &str[i], sizeof(unsigned long) * 2);
432
l = strtoul(tmp, NULL, 16);
434
memcpy(&((BYTE *)uuid)[i/2], &l, sizeof(unsigned long));
438
static int ecryptfs_tspi_init(char **alias)
442
if (asprintf(alias, "tspi") == -1) {
443
syslog(LOG_ERR, "Out of memory\n");
452
ecryptfs_tspi_get_params(struct key_mod_param **params, uint32_t *num_params)
454
(*params) = tspi_params;
455
(*num_params) = tspi_num_params;
459
static int ecryptfs_tspi_serialize(unsigned char *blob, size_t *blob_size,
460
struct tspi_data *tspi_data)
464
(*blob_size) = sizeof(TSS_UUID);
467
memcpy(blob, &tspi_data->uuid, sizeof(TSS_UUID));
473
ecryptfs_tspi_init_from_param_vals(struct tspi_data *tspi_data,
474
struct key_mod_param_val *param_vals,
475
uint32_t num_param_vals)
481
if (num_param_vals != tspi_num_params) {
483
syslog(LOG_ERR, "Require [%d] param vals; got [%d]\n",
484
tspi_num_params, num_param_vals);
487
for (i = 0; i < num_param_vals; i++)
488
tspi_params[i].val = ¶m_vals[i];
489
memset(tspi_data, 0, sizeof(struct tspi_data));
490
for (i = 0; i < num_param_vals; i++)
491
if (strcmp(tspi_params[i].option, "uuid") == 0) {
492
string_to_uuid(&tspi_data->uuid,
493
tspi_params[i].val->val);
498
syslog(LOG_ERR, "uuid parameter must be set\n");
505
static int ecryptfs_tspi_get_blob(unsigned char *blob, size_t *blob_size,
506
struct key_mod_param_val *param_vals,
507
uint32_t num_param_vals)
509
struct tspi_data tspi_data;
512
if ((rc = ecryptfs_tspi_init_from_param_vals(&tspi_data, param_vals,
514
syslog(LOG_ERR, "Error parsing parameter values; rc = [%d]\n",
519
if ((rc = ecryptfs_tspi_serialize(NULL, blob_size,
521
syslog(LOG_ERR, "Error serializing tspi; rc = [%d]\n",
527
if ((rc = ecryptfs_tspi_serialize(blob, blob_size, &tspi_data))) {
528
syslog(LOG_ERR, "Error serializing tspi; rc = [%d]\n", rc);
535
static int ecryptfs_tspi_destroy(unsigned char *blob)
540
static int ecryptfs_tspi_finalize(void)
545
static struct ecryptfs_key_mod_ops ecryptfs_tspi_ops = {
549
&ecryptfs_tspi_get_params,
551
&ecryptfs_tspi_get_blob,
553
&ecryptfs_tspi_get_key_sig,
555
&ecryptfs_tspi_encrypt,
556
&ecryptfs_tspi_decrypt,
557
&ecryptfs_tspi_destroy,
558
&ecryptfs_tspi_finalize
561
struct ecryptfs_key_mod_ops *get_key_mod_ops(void)
563
return &ecryptfs_tspi_ops;