summaryrefslogtreecommitdiff
path: root/src/libtpmtss/plugins/tpm/tpm_private_key.c
blob: bd5a8baebacbe52900e52d4698f22c89ed743877 (plain)
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
/*
 * Copyright (C) 2017 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * 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 GNU General Public License
 * for more details.
 */

#include "tpm_private_key.h"

#include <tpm_tss.h>
#include <utils/debug.h>

typedef struct private_tpm_private_key_t private_tpm_private_key_t;

/**
 * Private data of an tpm_private_key_t object.
 */
struct private_tpm_private_key_t {

	/**
	 * Public tpm_private_key_t interface.
	 */
	tpm_private_key_t public;

	/**
	 * Token keyid used to reference optional PIN for TPM key
	 */
	identification_t *keyid;

	/**
	 * Trusted Platform Module
	 */
	tpm_tss_t *tpm;

	/**
	 * TPM key object handle
	 */
	uint32_t handle;

	/**
	 * Hierarchy the TPM key object is attached to
	 */
	uint32_t hierarchy;

	/**
	 * Associated public key
	 */
	public_key_t *pubkey;

	/**
	 * References to this key
	 */
	refcount_t ref;

};


METHOD(private_key_t, get_type, key_type_t,
	private_tpm_private_key_t *this)
{
	return this->pubkey->get_type(this->pubkey);
}

METHOD(private_key_t, get_keysize, int,
	private_tpm_private_key_t *this)
{
	return this->pubkey->get_keysize(this->pubkey);
}

METHOD(private_key_t, sign, bool,
	private_tpm_private_key_t *this, signature_scheme_t scheme,
	chunk_t data, chunk_t *signature)
{
	chunk_t pin = chunk_empty;
	shared_key_t *shared;
	enumerator_t *enumerator;

	/* check for optional PIN */
	enumerator = lib->credmgr->create_shared_enumerator(lib->credmgr,
										SHARED_PIN, this->keyid, NULL);
	if (enumerator->enumerate(enumerator, &shared, NULL, NULL))
	{
		pin = shared->get_key(shared);
	}
	enumerator->destroy(enumerator);

	return this->tpm->sign(this->tpm, this->hierarchy, this->handle, scheme,
						   data, pin, signature);
}

METHOD(private_key_t, decrypt, bool,
	private_tpm_private_key_t *this, encryption_scheme_t scheme,
	chunk_t crypt, chunk_t *plain)
{
	return FALSE;
}

METHOD(private_key_t, get_public_key, public_key_t*,
	private_tpm_private_key_t *this)
{
	return this->pubkey->get_ref(this->pubkey);
}

METHOD(private_key_t, get_fingerprint, bool,
	private_tpm_private_key_t *this, cred_encoding_type_t type,
	chunk_t *fingerprint)
{
	return this->pubkey->get_fingerprint(this->pubkey, type, fingerprint);
}

METHOD(private_key_t, get_encoding, bool,
	private_tpm_private_key_t *this, cred_encoding_type_t type,
	chunk_t *encoding)
{
	return FALSE;
}

METHOD(private_key_t, get_ref, private_key_t*,
	private_tpm_private_key_t *this)
{
	ref_get(&this->ref);
	return &this->public.key;
}

METHOD(private_key_t, destroy, void,
	private_tpm_private_key_t *this)
{
	if (ref_put(&this->ref))
	{
		DESTROY_IF(this->pubkey);
		this->tpm->destroy(this->tpm);
		this->keyid->destroy(this->keyid);
		free(this);
	}
}

/**
 * See header.
 */
tpm_private_key_t *tpm_private_key_connect(key_type_t type, va_list args)
{
	private_tpm_private_key_t *this;
	tpm_tss_t *tpm;
	chunk_t keyid = chunk_empty, pubkey_blob = chunk_empty;
	char handle_str[4];
	size_t len;
	uint32_t hierarchy = 0x4000000B;  /* TPM_RH_ENDORSEMENT */
	uint32_t handle;

	while (TRUE)
	{
		switch (va_arg(args, builder_part_t))
		{
			case BUILD_PKCS11_KEYID:
				keyid = va_arg(args, chunk_t);
				continue;
			case BUILD_PKCS11_SLOT:
				hierarchy = va_arg(args, int);
				continue;
			case BUILD_PKCS11_MODULE:
				va_arg(args, char*);
				continue;
			case BUILD_END:
				break;
			default:
				return NULL;
		}
		break;
	}

	/* convert keyid into 32 bit TPM key object handle */
	if (!keyid.len)
	{
		return NULL;
	}
	len = min(keyid.len, 4);
	memset(handle_str, 0x00, 4);
	memcpy(handle_str + 4 - len, keyid.ptr + keyid.len - len, len);
	handle = untoh32(handle_str);

	/* try to find a TPM 2.0 */
	tpm = tpm_tss_probe(TPM_VERSION_2_0);
	if (!tpm)
	{
		DBG1(DBG_LIB, "no TPM 2.0 found");
		return NULL;	
	}

	INIT(this,
		.public = {
			.key = {
				.get_type = _get_type,
				.sign = _sign,
				.decrypt = _decrypt,
				.get_keysize = _get_keysize,
				.get_public_key = _get_public_key,
				.equals = private_key_equals,
				.belongs_to = private_key_belongs_to,
				.get_fingerprint = _get_fingerprint,
				.has_fingerprint = private_key_has_fingerprint,
				.get_encoding = _get_encoding,
				.get_ref = _get_ref,
				.destroy = _destroy,
			},
		},
		.tpm = tpm,
		.keyid = identification_create_from_encoding(ID_KEY_ID, keyid),
		.handle = handle,
		.hierarchy = hierarchy,
		.ref = 1,
	);

	/* get public key from TPM */
	pubkey_blob = tpm->get_public(tpm, handle);
	if (!pubkey_blob.len)
	{
		destroy(this);
		return NULL;
	}
	this->pubkey = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
								BUILD_BLOB_ASN1_DER, pubkey_blob, BUILD_END);
	chunk_free(&pubkey_blob);

	if (!this->pubkey)
	{
		destroy(this);
		return NULL;
	}

	return &this->public;
}