Skip to content

Latest commit

 

History

History
310 lines (280 loc) · 8.73 KB

gnutls_tpm.c

File metadata and controls

310 lines (280 loc) · 8.73 KB
 
Jun 14, 2012
Jun 14, 2012
1
2
3
/*
* OpenConnect (SSL + DTLS) VPN client
*
Jan 26, 2015
Jan 26, 2015
4
* Copyright © 2008-2015 Intel Corporation.
Jun 14, 2012
Jun 14, 2012
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
*
* Author: David Woodhouse <dwmw2@infradead.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1, as published by the Free Software Foundation.
*
* 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
* Lesser General Public License for more details.
*/
/*
* TPM code based on client-tpm.c from
* Carolin Latze <latze@angry-red-pla.net> and Tobias Soder
*/
Jul 1, 2014
Jul 1, 2014
23
24
#include <config.h>
Jun 14, 2012
Jun 14, 2012
25
26
27
28
29
30
31
32
33
#include <errno.h>
#include <string.h>
#include <gnutls/gnutls.h>
#include "openconnect-internal.h"
#include "gnutls.h"
#ifdef HAVE_TROUSERS
Oct 3, 2018
Oct 3, 2018
34
35
36
37
38
39
40
41
42
43
#include <trousers/tss.h>
#include <trousers/trousers.h>
struct oc_tpm1_ctx {
TSS_HCONTEXT tpm_context;
TSS_HKEY srk;
TSS_HPOLICY srk_policy;
TSS_HKEY tpm_key;
TSS_HPOLICY tpm_key_policy;
};
Jun 14, 2012
Jun 14, 2012
44
45
46
47
48
49
50
51
52
/* Signing function for TPM privkeys, set with gnutls_privkey_import_ext() */
static int tpm_sign_fn(gnutls_privkey_t key, void *_vpninfo,
const gnutls_datum_t *data, gnutls_datum_t *sig)
{
struct openconnect_info *vpninfo = _vpninfo;
TSS_HHASH hash;
int err;
Jun 13, 2014
Jun 13, 2014
53
vpn_progress(vpninfo, PRG_DEBUG,
Jun 14, 2012
Jun 14, 2012
54
55
56
_("TPM sign function called for %d bytes.\n"),
data->size);
Oct 3, 2018
Oct 3, 2018
57
err = Tspi_Context_CreateObject(vpninfo->tpm1->tpm_context, TSS_OBJECT_TYPE_HASH,
Jun 14, 2012
Jun 14, 2012
58
59
60
61
62
63
64
65
66
67
68
69
TSS_HASH_OTHER, &hash);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to create TPM hash object: %s\n"),
Trspi_Error_String(err));
return GNUTLS_E_PK_SIGN_FAILED;
}
err = Tspi_Hash_SetHashValue(hash, data->size, data->data);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to set value in TPM hash object: %s\n"),
Trspi_Error_String(err));
Oct 3, 2018
Oct 3, 2018
70
Tspi_Context_CloseObject(vpninfo->tpm1->tpm_context, hash);
Jun 14, 2012
Jun 14, 2012
71
72
return GNUTLS_E_PK_SIGN_FAILED;
}
Oct 3, 2018
Oct 3, 2018
73
74
err = Tspi_Hash_Sign(hash, vpninfo->tpm1->tpm_key, &sig->size, &sig->data);
Tspi_Context_CloseObject(vpninfo->tpm1->tpm_context, hash);
Jun 14, 2012
Jun 14, 2012
75
if (err) {
Oct 3, 2018
Oct 3, 2018
76
if (vpninfo->tpm1->tpm_key_policy || err != TPM_E_AUTHFAIL)
Jun 14, 2012
Jun 14, 2012
77
78
79
80
81
82
83
84
85
86
87
vpn_progress(vpninfo, PRG_ERR,
_("TPM hash signature failed: %s\n"),
Trspi_Error_String(err));
if (err == TPM_E_AUTHFAIL)
return GNUTLS_E_INSUFFICIENT_CREDENTIALS;
else
return GNUTLS_E_PK_SIGN_FAILED;
}
return 0;
}
Oct 3, 2018
Oct 3, 2018
88
89
int load_tpm1_key(struct openconnect_info *vpninfo, gnutls_datum_t *fdata,
gnutls_privkey_t *pkey, gnutls_datum_t *pkey_sig)
Jun 14, 2012
Jun 14, 2012
90
91
92
93
94
95
96
97
98
99
100
101
102
103
{
static const TSS_UUID SRK_UUID = TSS_UUID_SRK;
gnutls_datum_t asn1;
unsigned int tss_len;
char *pass;
int ofs, err;
err = gnutls_pem_base64_decode_alloc("TSS KEY BLOB", fdata, &asn1);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Error decoding TSS key blob: %s\n"),
gnutls_strerror(err));
return -EINVAL;
}
Oct 3, 2018
Oct 3, 2018
104
vpninfo->tpm1 = calloc(1, sizeof(*vpninfo->tpm1));
Jun 14, 2012
Jun 14, 2012
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
/* Ick. We have to parse the ASN1 OCTET_STRING for ourselves. */
if (asn1.size < 2 || asn1.data[0] != 0x04 /* OCTET_STRING */) {
vpn_progress(vpninfo, PRG_ERR,
_("Error in TSS key blob\n"));
goto out_blob;
}
tss_len = asn1.data[1];
ofs = 2;
if (tss_len & 0x80) {
int lenlen = tss_len & 0x7f;
if (asn1.size < 2 + lenlen || lenlen > 3) {
vpn_progress(vpninfo, PRG_ERR,
_("Error in TSS key blob\n"));
goto out_blob;
}
tss_len = 0;
while (lenlen) {
tss_len <<= 8;
tss_len |= asn1.data[ofs++];
lenlen--;
}
}
if (tss_len + ofs != asn1.size) {
vpn_progress(vpninfo, PRG_ERR,
_("Error in TSS key blob\n"));
goto out_blob;
}
Oct 3, 2018
Oct 3, 2018
136
err = Tspi_Context_Create(&vpninfo->tpm1->tpm_context);
Jun 14, 2012
Jun 14, 2012
137
138
139
140
141
142
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to create TPM context: %s\n"),
Trspi_Error_String(err));
goto out_blob;
}
Oct 3, 2018
Oct 3, 2018
143
err = Tspi_Context_Connect(vpninfo->tpm1->tpm_context, NULL);
Jun 14, 2012
Jun 14, 2012
144
145
146
147
148
149
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to connect TPM context: %s\n"),
Trspi_Error_String(err));
goto out_context;
}
Oct 3, 2018
Oct 3, 2018
150
151
err = Tspi_Context_LoadKeyByUUID(vpninfo->tpm1->tpm_context, TSS_PS_TYPE_SYSTEM,
SRK_UUID, &vpninfo->tpm1->srk);
Jun 14, 2012
Jun 14, 2012
152
153
154
155
156
157
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to load TPM SRK key: %s\n"),
Trspi_Error_String(err));
goto out_context;
}
Oct 3, 2018
Oct 3, 2018
158
err = Tspi_GetPolicyObject(vpninfo->tpm1->srk, TSS_POLICY_USAGE, &vpninfo->tpm1->srk_policy);
Jun 14, 2012
Jun 14, 2012
159
160
161
162
163
164
165
166
167
168
169
170
171
172
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to load TPM SRK policy object: %s\n"),
Trspi_Error_String(err));
goto out_srk;
}
pass = vpninfo->cert_password;
vpninfo->cert_password = NULL;
while (1) {
static const char nullpass[20];
/* We don't seem to get the error here... */
if (pass)
Oct 3, 2018
Oct 3, 2018
173
err = Tspi_Policy_SetSecret(vpninfo->tpm1->srk_policy,
Jun 14, 2012
Jun 14, 2012
174
175
176
TSS_SECRET_MODE_PLAIN,
strlen(pass), (BYTE *)pass);
else /* Well-known NULL key */
Oct 3, 2018
Oct 3, 2018
177
err = Tspi_Policy_SetSecret(vpninfo->tpm1->srk_policy,
Jun 14, 2012
Jun 14, 2012
178
179
180
181
182
183
184
185
186
TSS_SECRET_MODE_SHA1,
sizeof(nullpass), (BYTE *)nullpass);
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to set TPM PIN: %s\n"),
Trspi_Error_String(err));
goto out_srkpol;
}
Dec 21, 2018
Dec 21, 2018
187
free_pass(&pass);
Jun 14, 2012
Jun 14, 2012
188
189
/* ... we get it here instead. */
Oct 3, 2018
Oct 3, 2018
190
err = Tspi_Context_LoadKeyByBlob(vpninfo->tpm1->tpm_context, vpninfo->tpm1->srk,
Jun 14, 2012
Jun 14, 2012
191
tss_len, asn1.data + ofs,
Oct 3, 2018
Oct 3, 2018
192
&vpninfo->tpm1->tpm_key);
Jun 14, 2012
Jun 14, 2012
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
if (!err)
break;
if (pass)
vpn_progress(vpninfo, PRG_ERR,
_("Failed to load TPM key blob: %s\n"),
Trspi_Error_String(err));
if (err != TPM_E_AUTHFAIL)
goto out_srkpol;
err = request_passphrase(vpninfo, "openconnect_tpm_srk",
&pass, _("Enter TPM SRK PIN:"));
if (err)
goto out_srkpol;
}
gnutls_privkey_init(pkey);
/* This would be nicer if there was a destructor callback. I could
allocate a data structure with the TPM handles and the vpninfo
pointer, and destroy that properly when the key is destroyed. */
gnutls_privkey_import_ext(*pkey, GNUTLS_PK_RSA, vpninfo, tpm_sign_fn, NULL, 0);
retry_sign:
Aug 14, 2017
Aug 14, 2017
217
err = gnutls_privkey_sign_data(*pkey, GNUTLS_DIG_SHA1, 0, fdata, pkey_sig);
Jun 14, 2012
Jun 14, 2012
218
if (err == GNUTLS_E_INSUFFICIENT_CREDENTIALS) {
Oct 3, 2018
Oct 3, 2018
219
220
if (!vpninfo->tpm1->tpm_key_policy) {
err = Tspi_Context_CreateObject(vpninfo->tpm1->tpm_context,
Jun 14, 2012
Jun 14, 2012
221
222
TSS_OBJECT_TYPE_POLICY,
TSS_POLICY_USAGE,
Oct 3, 2018
Oct 3, 2018
223
&vpninfo->tpm1->tpm_key_policy);
Jun 14, 2012
Jun 14, 2012
224
225
226
227
228
229
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to create key policy object: %s\n"),
Trspi_Error_String(err));
goto out_key;
}
Oct 3, 2018
Oct 3, 2018
230
231
err = Tspi_Policy_AssignToObject(vpninfo->tpm1->tpm_key_policy,
vpninfo->tpm1->tpm_key);
Jun 14, 2012
Jun 14, 2012
232
233
234
235
236
237
238
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to assign policy to key: %s\n"),
Trspi_Error_String(err));
goto out_key_policy;
}
}
Mar 10, 2013
Mar 10, 2013
239
err = request_passphrase(vpninfo, "openconnect_tpm_key",
Jun 14, 2012
Jun 14, 2012
240
241
242
243
&pass, _("Enter TPM key PIN:"));
if (err)
goto out_key_policy;
Oct 3, 2018
Oct 3, 2018
244
err = Tspi_Policy_SetSecret(vpninfo->tpm1->tpm_key_policy,
Jun 14, 2012
Jun 14, 2012
245
246
TSS_SECRET_MODE_PLAIN,
strlen(pass), (void *)pass);
Dec 21, 2018
Dec 21, 2018
247
free_pass(&pass);
Jun 14, 2012
Jun 14, 2012
248
249
250
251
252
253
254
255
256
257
if (err) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to set key PIN: %s\n"),
Trspi_Error_String(err));
goto out_key_policy;
}
goto retry_sign;
}
Mar 10, 2013
Mar 10, 2013
258
free(asn1.data);
Jun 14, 2012
Jun 14, 2012
259
260
return 0;
out_key_policy:
Oct 3, 2018
Oct 3, 2018
261
262
Tspi_Context_CloseObject(vpninfo->tpm1->tpm_context, vpninfo->tpm1->tpm_key_policy);
vpninfo->tpm1->tpm_key_policy = 0;
Jun 14, 2012
Jun 14, 2012
263
out_key:
Oct 3, 2018
Oct 3, 2018
264
265
Tspi_Context_CloseObject(vpninfo->tpm1->tpm_context, vpninfo->tpm1->tpm_key);
vpninfo->tpm1->tpm_key = 0;
Jun 14, 2012
Jun 14, 2012
266
out_srkpol:
Oct 3, 2018
Oct 3, 2018
267
268
Tspi_Context_CloseObject(vpninfo->tpm1->tpm_context, vpninfo->tpm1->srk_policy);
vpninfo->tpm1->srk_policy = 0;
Jun 14, 2012
Jun 14, 2012
269
out_srk:
Oct 3, 2018
Oct 3, 2018
270
271
Tspi_Context_CloseObject(vpninfo->tpm1->tpm_context, vpninfo->tpm1->srk);
vpninfo->tpm1->srk = 0;
Jun 14, 2012
Jun 14, 2012
272
out_context:
Oct 3, 2018
Oct 3, 2018
273
274
Tspi_Context_Close(vpninfo->tpm1->tpm_context);
vpninfo->tpm1->tpm_context = 0;
Jun 14, 2012
Jun 14, 2012
275
out_blob:
Mar 10, 2013
Mar 10, 2013
276
free(asn1.data);
Oct 3, 2018
Oct 3, 2018
277
278
free(vpninfo->tpm1);
vpninfo->tpm1 = NULL;
Jun 14, 2012
Jun 14, 2012
279
280
281
return -EIO;
}
Oct 3, 2018
Oct 3, 2018
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
void release_tpm1_ctx(struct openconnect_info *vpninfo)
{
if (!vpninfo->tpm1)
return;
if (vpninfo->tpm1->tpm_key_policy) {
Tspi_Context_CloseObject(vpninfo->tpm1->tpm_context, vpninfo->tpm1->tpm_key_policy);
vpninfo->tpm1->tpm_key = 0;
}
if (vpninfo->tpm1->tpm_key) {
Tspi_Context_CloseObject(vpninfo->tpm1->tpm_context, vpninfo->tpm1->tpm_key);
vpninfo->tpm1->tpm_key = 0;
}
if (vpninfo->tpm1->srk_policy) {
Tspi_Context_CloseObject(vpninfo->tpm1->tpm_context, vpninfo->tpm1->srk_policy);
vpninfo->tpm1->srk_policy = 0;
}
if (vpninfo->tpm1->srk) {
Tspi_Context_CloseObject(vpninfo->tpm1->tpm_context, vpninfo->tpm1->srk);
vpninfo->tpm1->srk = 0;
}
if (vpninfo->tpm1->tpm_context) {
Tspi_Context_Close(vpninfo->tpm1->tpm_context);
vpninfo->tpm1->tpm_context = 0;
}
free(vpninfo->tpm1);
vpninfo->tpm1 = NULL;
};
Jun 14, 2012
Jun 14, 2012
310
#endif /* HAVE_TROUSERS */