Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*
 * 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.
 */

#include <stdio.h>
#include <errno.h>
#include <limits.h>

#include "tpm_tspi.h"
#include "tpm_utils.h"
#include "tpm_nvcommon.h"

static unsigned int nvindex;
static BOOL nvindex_set;
static unsigned int nvperm;
static unsigned int nvsize;
static const char *ownerpass;
static BOOL ownerWellKnown;
static BOOL askOwnerPass;
static const char *datapass;
static BOOL dataWellKnown;
static BOOL askDataPass;
static int end;
static UINT32 selectedPcrsRead[24];
static UINT32 selectedPcrsWrite[24];
static UINT32 selectedPcrsReadLen = 0;
static UINT32 selectedPcrsWriteLen = 0;
static const char *filename;

TSS_HCONTEXT hContext = 0;


static int parse(const int aOpt, const char *aArg)
{
	switch (aOpt) {
	case 'i':
		if (parseHexOrDecimal(aArg, &nvindex, 0, UINT_MAX,
				      "NVRAM index") != 0)
			return -1;
		nvindex_set = TRUE;
		break;

	case 'p':
		if (!strcmp(aArg, "?")) {
			displayStringsAndValues(permvalues, "");
			end = 1;
			return 0;
		}
		if (parseStringWithValues(aArg, permvalues, &nvperm, UINT_MAX,
				          "NVRAM permission") != 0)
			return -1;
		break;

	case 's':
		if (parseHexOrDecimal(aArg, &nvsize, 0, UINT_MAX,
				      "NVRAM index size") != 0)
			return -1;
		break;

	case 'o':
		ownerpass = aArg;
		if (!ownerpass)
			askOwnerPass = TRUE;
		else
			askOwnerPass = FALSE;
		ownerWellKnown = FALSE;
		break;

	case 'y':
		ownerWellKnown = TRUE;
		ownerpass = NULL;
		askOwnerPass = FALSE;
		break;

	case 'a':
		datapass = aArg;
		if (!datapass)
			askDataPass = TRUE;
		else
			askDataPass = FALSE;
		dataWellKnown = FALSE;
		break;

	case 'z':
		dataWellKnown = TRUE;
		datapass = NULL;
		askDataPass = FALSE;
		break;

	case 'u':
		useUnicode = TRUE;
		break;

	case 'r':
		if (aArg && atoi(aArg) >= 0 && atoi(aArg) < 24) {
			selectedPcrsRead[selectedPcrsReadLen++] = atoi(aArg);
		} else
			return -1;
		break;

	case 'w':
		if (aArg && atoi(aArg) >= 0 && atoi(aArg) < 24) {
			selectedPcrsWrite[selectedPcrsWriteLen++] = atoi(aArg);
		} else
			return -1;
		break;

	case 'f':
		filename = aArg;
		break;

	default:
		return -1;
	}
	return 0;
}

static void help(const char* aCmd)
{
	logCmdHelp(aCmd);
	logUnicodeCmdOption();
	logCmdOption("-y, --owner-well-known",
		     _("Use 20 bytes of zeros (TSS_WELL_KNOWN_SECRET) as the "
		       "TPM owner secret"));
	logCmdOption("-z, --data-well-known",
		     _("Use 20 bytes of zeros (TSS_WELL_KNOWN_SECRET) as the "
		       "NVRAM area's secret"));
	logOwnerPassCmdOption();
	logCmdOption("-a, --pwda",
		     _("NVRAM area password"));
	logNVIndexCmdOption();
	logCmdOption("-s, --size",
		     _("Size of the NVRAM area"));
	logCmdOption("-r, --rpcrs",
		     _("PCRs to seal the NVRAM area to for reading (use multiple times)"));
	logCmdOption("-w, --wpcrs",
		     _("PCRs to seal the NVRAM area to for writing (use multiple times)"));
	logCmdOption("-f, --filename",
		     _("File containing PCR info for the NVRAM area"));

	logCmdOption("-p, --permissions",
		     _("Permissions of the NVRAM area"));
        displayStringsAndValues(permvalues, "                ");
}

void logInvalidPcrInfoFile()
{
	logError(_("Invalid PCR info file. Format is:\n"
		 "[r/w] [PCR IDX] [SHA-1 ascii]\n\nExample:\n"
		 "r 9 00112233445566778899AABBCCDDEEFF00112233"));
}

int
parseNVPermsFile(FILE *f, TSS_HCONTEXT *hContext, TSS_HNVSTORE *nvObject,
		 TSS_HPCRS *hPcrsRead, TSS_HPCRS *hPcrsWrite)
{
	UINT32 pcrSize;
	char rw;
	unsigned int pcr, n;
	char hash_ascii[65], hash_bin[32], save;
	int rc = -1;

	while (!feof(f)) {
		errno = 0;
		n = fscanf(f, "%c %u %s\n", &rw, &pcr, hash_ascii);
		if (n != 3) {
			logInvalidPcrInfoFile();
			goto out;
		} else if (errno != 0) {
			perror("fscanf");
			goto out;
		}

		if (rw != 'r' && rw != 'w') {
			logInvalidPcrInfoFile();
			goto out;
		}

		if (pcr > 15) {
			logError(_("Cannot seal NVRAM area to PCR > 15\n"));
			goto out;
		}

		for (n = 0; n < strlen(hash_ascii); n += 2) {
			save = hash_ascii[n + 2];
			hash_ascii[n + 2] = '\0';
			hash_bin[n/2] = strtoul(&hash_ascii[n], NULL, 16);
			hash_ascii[n + 2] = save;
		}
		pcrSize = n/2;

		if (rw == 'r') {
			if (*hPcrsRead == NULL_HPCRS)
				if (contextCreateObject(*hContext, TSS_OBJECT_TYPE_PCRS,
							TSS_PCRS_STRUCT_INFO_SHORT,
							hPcrsRead) != TSS_SUCCESS)
					goto out;

			if (pcrcompositeSetPcrValue(*hPcrsRead, pcr, pcrSize, (BYTE *)hash_bin)
					!= TSS_SUCCESS)
				goto out;
		} else {
			if (*hPcrsWrite == NULL_HPCRS)
				if (contextCreateObject(*hContext, TSS_OBJECT_TYPE_PCRS,
							TSS_PCRS_STRUCT_INFO_SHORT,
							hPcrsWrite) != TSS_SUCCESS)
					goto out;

			if (pcrcompositeSetPcrValue(*hPcrsWrite, pcr, pcrSize, (BYTE *)hash_bin)
					!= TSS_SUCCESS)
				goto out;
		}
	}

	rc = 0;
out:
	return rc;
}

int main(int argc, char **argv)
{
	TSS_HTPM hTpm;
	TSS_HNVSTORE nvObject;
	TSS_FLAG fNvAttrs;
	TSS_HPOLICY hTpmPolicy, hDataPolicy;
	int iRc = -1;
	BYTE well_known_secret[] = TSS_WELL_KNOWN_SECRET;
	int opswd_len = -1;
	int dpswd_len = -1;
	TSS_HPCRS hPcrsRead = 0, hPcrsWrite = 0;
	struct option hOpts[] = {
		{"index"           , required_argument, NULL, 'i'},
		{"size"            , required_argument, NULL, 's'},
		{"permissions"     , required_argument, NULL, 'p'},
		{"rpcrs"           , required_argument, NULL, 'r'},
		{"wpcrs"           , required_argument, NULL, 'w'},
		{"filename"        , required_argument, NULL, 'f'},
		{"pwdo"            , optional_argument, NULL, 'o'},
		{"pwda"            , optional_argument, NULL, 'a'},
		{"use-unicode"     ,       no_argument, NULL, 'u'},
		{"data-well-known" ,       no_argument, NULL, 'z'},
		{"owner-well-known",       no_argument, NULL, 'y'},
		{NULL              ,       no_argument, NULL, 0},
	};
	TSS_FLAG initFlag = TSS_PCRS_STRUCT_INFO_SHORT;
	UINT32 localityValue = TPM_LOC_ZERO | TPM_LOC_ONE | TPM_LOC_TWO |
			       TPM_LOC_THREE | TPM_LOC_FOUR;

	initIntlSys();

	if (genericOptHandler
		    (argc, argv, "i:s:p:o:a:r:w:f:yzu", hOpts,
		     sizeof(hOpts) / sizeof(struct option), parse, help) != 0)
		goto out;

	if (end) {
		iRc = 0;
		goto out;
	}

	if (!nvindex_set) {
		logError(_("You must provide an index for the NVRAM area.\n"));
		goto out;
	}

	if (nvperm == 0 &&
	    (UINT32)nvindex != TPM_NV_INDEX_LOCK &&
	    (UINT32)nvindex != TPM_NV_INDEX0) {
		logError(_("You must provide permission bits for the NVRAM area.\n"));
		goto out;
	}

	logDebug("permissions = 0x%08x\n", nvperm);

	if (contextCreate(&hContext) != TSS_SUCCESS)
		goto out;

	if (contextConnect(hContext) != TSS_SUCCESS)
		goto out_close;

	if (contextGetTpm(hContext, &hTpm) != TSS_SUCCESS)
		goto out_close;

	fNvAttrs = 0;

	if (contextCreateObject(hContext,
				TSS_OBJECT_TYPE_NV,
				fNvAttrs,
				&nvObject) != TSS_SUCCESS)
		goto out_close;

	if (askOwnerPass) {
		ownerpass = _GETPASSWD(_("Enter owner password: "), &opswd_len,
			FALSE, useUnicode );
		if (!ownerpass) {
			logError(_("Failed to get owner password\n"));
			goto out_close;
		}
	}

	if (ownerpass || ownerWellKnown) {
		if (policyGet(hTpm, &hTpmPolicy) != TSS_SUCCESS)
			goto out_close;
		if (ownerpass) {
			if (opswd_len < 0)
				opswd_len = strlen(ownerpass);
			if (policySetSecret(hTpmPolicy, opswd_len,
					    (BYTE *)ownerpass) != TSS_SUCCESS)
				goto out_close;
		} else {
			if (policySetSecret(hTpmPolicy, TCPA_SHA1_160_HASH_LEN,
					    (BYTE *)well_known_secret) != TSS_SUCCESS)
				goto out_close;
		}
	}

	if (askDataPass) {
		datapass = _GETPASSWD(_("Enter NVRAM data password: "), &dpswd_len,
			TRUE, useUnicode );
		if (!datapass) {
			logError(_("Failed to get NVRAM data password\n"));
			goto out_close;
		}
	}

	if (datapass || dataWellKnown) {
		if (contextCreateObject
		    (hContext, TSS_OBJECT_TYPE_POLICY, TSS_POLICY_USAGE,
		     &hDataPolicy) != TSS_SUCCESS)
			goto out_close;

		if (datapass) {
			if (dpswd_len < 0)
				dpswd_len = strlen(datapass);
			if (policySetSecret(hDataPolicy, dpswd_len,
				    (BYTE *)datapass) != TSS_SUCCESS)
				goto out_close;
		} else {
			if (policySetSecret(hDataPolicy, TCPA_SHA1_160_HASH_LEN,
				    (BYTE *)well_known_secret) != TSS_SUCCESS)
				goto out_close;
		}

		if (Tspi_Policy_AssignToObject(hDataPolicy, nvObject) !=
		    TSS_SUCCESS)
			goto out_close;
	}

	if (Tspi_SetAttribUint32(nvObject,
				 TSS_TSPATTRIB_NV_INDEX,
				 0,
				 nvindex) != TSS_SUCCESS)
		goto out_close_obj;

	if (Tspi_SetAttribUint32(nvObject,
				 TSS_TSPATTRIB_NV_PERMISSIONS,
				 0,
				 nvperm) != TSS_SUCCESS)
		goto out_close_obj;

	if (Tspi_SetAttribUint32(nvObject,
				 TSS_TSPATTRIB_NV_DATASIZE,
				 0,
				 nvsize) != TSS_SUCCESS)
		goto out_close_obj;

	if (selectedPcrsReadLen) {
		UINT32 pcrSize;
		BYTE *pcrValue;
		UINT32 i;

		for (i = 0; i < selectedPcrsReadLen; i++) {
			if (selectedPcrsRead[i] > 15) {
				logError(_("Cannot seal NVRAM area to PCR > 15\n"));
				goto out_close;
			}
		}

		if (contextCreateObject(hContext, TSS_OBJECT_TYPE_PCRS, initFlag,
					&hPcrsRead) != TSS_SUCCESS)
			goto out_close;

		for (i = 0; i < selectedPcrsReadLen; i++) {
			if (tpmPcrRead(hTpm, selectedPcrsRead[i], &pcrSize, &pcrValue) !=
			    TSS_SUCCESS)
				goto out_close;

			if (pcrcompositeSetPcrValue(hPcrsRead, selectedPcrsRead[i],
						    pcrSize, pcrValue)
					!= TSS_SUCCESS)
				goto out_close;
		}
	}

	if (selectedPcrsWriteLen) {
		UINT32 pcrSize;
		BYTE *pcrValue;
		UINT32 i;

		for (i = 0; i < selectedPcrsWriteLen; i++) {
			if (selectedPcrsWrite[i] > 15) {
				logError(_("Cannot seal NVRAM area to PCR > 15\n"));
				goto out_close;
			}
		}

		if (contextCreateObject(hContext, TSS_OBJECT_TYPE_PCRS, initFlag,
					&hPcrsWrite) != TSS_SUCCESS)
			goto out_close;

		for (i = 0; i < selectedPcrsWriteLen; i++) {
			if (tpmPcrRead(hTpm, selectedPcrsWrite[i], &pcrSize, &pcrValue) !=
			    TSS_SUCCESS)
				goto out_close;

			if (pcrcompositeSetPcrValue(hPcrsWrite, selectedPcrsWrite[i],
						    pcrSize, pcrValue)
					!= TSS_SUCCESS)
				goto out_close;
		}
	}

	if (filename) {
		FILE *f;

		f = fopen(filename, "r");
		if (!f) {
			logError(_("Could not access file '%s'\n"), filename);
			goto out_close_obj;
		}

		if (parseNVPermsFile(f, &hContext, &nvObject, &hPcrsRead, &hPcrsWrite)
		    != TSS_SUCCESS)
			goto out_close_obj;
	}

	if (hPcrsRead)
		if (pcrcompositeSetPcrLocality(hPcrsRead, localityValue) != TSS_SUCCESS)
			goto out_close;

	if (hPcrsWrite)
		if (pcrcompositeSetPcrLocality(hPcrsWrite, localityValue) != TSS_SUCCESS)
			goto out_close;

	if (NVDefineSpace(nvObject, hPcrsRead, hPcrsWrite) != TSS_SUCCESS)
		goto out_close;

	logMsg(_("Successfully created NVRAM area at index 0x%x (%u).\n"),
	       nvindex, nvindex);

	iRc = 0;

	goto out_close;

      out_close_obj:
	contextCloseObject(hContext, nvObject);

      out_close:
	contextClose(hContext);

      out:
	return iRc;
}