loadkey.c 8.14 KB
Newer Older
Cody Cutler's avatar
Cody Cutler committed
1 2
/*
 * Copyright (c) 2008-2010 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file 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 Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
Cody Cutler's avatar
Cody Cutler committed
22 23 24 25 26
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Cody Cutler's avatar
Cody Cutler committed
27
#include <unistd.h>
Cody Cutler's avatar
Cody Cutler committed
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
#include <err.h>
#include <fcntl.h>
#include <openssl/ssl.h>
#include <sys/stat.h>
#include <tss/tspi.h>
#include <tss/platform.h>
#include <tss/tss_typedef.h>
#include <tss/tss_structs.h>

#define TSS_ERROR_CODE(x)       (x & 0xFFF)

#define FATAL(x)	do{printf("**\t");printf(x);printf("\n");}while(0);
//#define FATAL(x)	do{printf(x);printf("\n");exit(1);}while(0);

void check(char *msg, int cin){
	int in = TSS_ERROR_CODE(cin);
	printf("%s: ", msg);
	if(in == TSS_SUCCESS) {
		printf("TSS_SUCCESS\n");
		return;
	} else if(in == TSS_E_INVALID_HANDLE)
		printf("\t\tTSS_E_INVALID_HANDLE\n");
	else if(in == TSS_E_INTERNAL_ERROR)
		printf("\t\tTSS_E_INTERNAL_ERROR\n");
	else if(in == TSS_E_BAD_PARAMETER)
		printf("\t\tTSS_E_BAD_PARAMETER\n");
	else if(in == TSS_E_HASH_INVALID_LENGTH)
		printf("\t\tTSS_E_HASH_INVALID_LENGTH\n");
	else if(in == TSS_E_HASH_NO_DATA)
		printf("\t\tTSS_E_HASH_NO_DATA\n");
	else if(in == TSS_E_INVALID_SIGSCHEME)
		printf("\t\tTSS_E_INVALID_SIGSCHEME\n");
	else if(in == TSS_E_HASH_NO_IDENTIFIER)
		printf("\t\tTSS_E_HASH_NO_IDENTIFIER\n");
	else if(in == TSS_E_PS_KEY_NOTFOUND)
		printf("\t\tTSS_E_PS_KEY_NOTFOUND\n");
	else if(in == TSS_E_BAD_PARAMETER)
		printf("\t\tTSS_E_BAD_PARAMETER\n");
	else
		printf("\t\tNot here: 0x%x\n", in);

	//exit(1);
}

void pquote(TSS_VALIDATION *valdata)
{
	int i;
	TPM_QUOTE_INFO *qinfo = (TPM_QUOTE_INFO*)valdata->rgbData;
	if (valdata->ulDataLength < sizeof(TPM_QUOTE_INFO))
		printf("**too small to fit in TPM_QUOTE_INFO struct\n");
	printf("version: maj: %d min: %d\n", qinfo->version.major,
	    qinfo->version.minor);
	printf("fixed: ");
	for (i = 0; i < 4; i++)
		printf("%c", qinfo->fixed[i]);
	printf("\n");
	printf("SHA1 hash: ");
	for (i = 0; i < 20; i++)
		printf("%02x ", qinfo->compositeHash.digest[i]);
	printf("\n");
	printf("nonce: ");
	for (i = 0; i < 20; i++)
		printf("%02x ", qinfo->externalData.nonce[i]);
	printf("\n");
	printf("valdata len: %d\n ", valdata->ulValidationDataLength);
	for (i = 0; i < valdata->ulValidationDataLength; i++)
		printf("%02x ", valdata->rgbValidationData[i]);
	printf("\n");
}

void pquote2(TSS_VALIDATION *valdata)
{
	int i;

	if (!valdata)
		return;

	TPM_QUOTE_INFO2 *qinfo2 = (TPM_QUOTE_INFO2*)valdata->rgbData;
	
	if (valdata->ulDataLength < sizeof(TPM_QUOTE_INFO2)) {
		printf("**too small to fit in TPM_QUOTE_INFO2 struct\n");
		printf("\tlen: %d TPM_QUOTE_INFO2: %d\n", valdata->ulDataLength,
		    sizeof(TPM_QUOTE_INFO2));
	}
	printf("tag: %d\n", qinfo2->tag);
	printf("fixed: ");
	for (i = 0; i < 4; i++)
		printf("%c", qinfo2->fixed[i]);
	printf("\n");
	printf("nonce: ");
	for (i = 0; i < 20; i++)
		printf("%02x ", qinfo2->externalData.nonce[i]);
	printf("\n");
	printf("size of selection: %d\n",
	    ntohs(qinfo2->infoShort.pcrSelection.sizeOfSelect));
	printf("pcr select: %p\n", qinfo2->infoShort.pcrSelection.pcrSelect);
	printf("locality: 0x%x\n", qinfo2->infoShort.localityAtRelease);
	printf("digest at release: ");
	for (i = 0; i < 20; i++)
		printf("%02x ", qinfo2->infoShort.digestAtRelease.digest[i]);
	printf("\n");

	printf("the whole blob: \n");
	for (i = 0; i < valdata->ulDataLength; i++) {
		if (i % 20 == 0)
			printf("\n");
		printf("\\x%02x", valdata->rgbData[i]);
	}
	printf("\n");
	printf("quote2 size: %d returned size: %d\n", sizeof(TPM_QUOTE_INFO2),
	    valdata->ulDataLength);
	printf("info short %d\n", sizeof(TPM_PCR_INFO_SHORT));
}

Cody Cutler's avatar
Cody Cutler committed
142 143 144 145 146 147 148 149
void usage(char *name)
{
	printf("\n");
	printf("%s -t <tpmpass> -s <srkpass> [-f <keyfile>]\n", name);
	printf("\n");
	exit(EXIT_FAILURE);
}

Cody Cutler's avatar
Cody Cutler committed
150
int
Cody Cutler's avatar
Cody Cutler committed
151
main(int argc, char **argv)
Cody Cutler's avatar
Cody Cutler committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
{
	int ret, i, fd, j;
	TSS_HCONTEXT hContext;
	TSS_HKEY hSRK, hKey;
	TSS_UUID srkUUID = TSS_UUID_SRK;
	TSS_HPOLICY srkpol, hTPMPolicy;
	TSS_HPCRS	pcrs;
	TSS_HTPM	hTPM;
	UINT32		bloblen;
	BYTE		*srkpub, blob[1024];
	TSS_HPCRS	hpcomp;
	TSS_VALIDATION	valdata;
	TSS_HHASH	hHash;
	TPM_QUOTE_INFO *qinfo;
	TPM_QUOTE_INFO2 *qinfo2;
Cody Cutler's avatar
Cody Cutler committed
167
	UINT32		rub1, ch;
Cody Cutler's avatar
Cody Cutler committed
168
	BYTE		*rub2;
Cody Cutler's avatar
Cody Cutler committed
169
	BYTE		*srkpass, *tpmpass, *keyfile;
Cody Cutler's avatar
Cody Cutler committed
170 171 172

	RSA	*rsa;

Cody Cutler's avatar
Cody Cutler committed
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
	srkpass = tpmpass = keyfile = NULL;
	while ((ch = getopt(argc, argv, "f:hs:t:")) != -1) {
		switch (ch) {
			case 's':
				srkpass = optarg;
				break;
			case 't':
				tpmpass = optarg;
				break;
			case 'f':
				keyfile = optarg;
				break;
			case 'h':
			default:
				usage(argv[0]);
				break;
		}
	}

	if (!srkpass || !tpmpass)
		usage(argv[0]);

	if (!keyfile)
		keyfile = "key.blob";
	fd = open(keyfile, O_RDONLY, 0);
Cody Cutler's avatar
Cody Cutler committed
198
	if (fd < 0)
Cody Cutler's avatar
Cody Cutler committed
199
		errx(1, "Couldn't open %s\n", keyfile);
Cody Cutler's avatar
Cody Cutler committed
200 201 202 203 204

	printf("opened; fd %d\n", fd);
	bloblen = read(fd, blob, 1024);
	if (bloblen == -1) {
		perror(NULL);
Cody Cutler's avatar
Cody Cutler committed
205
		errx(1, "error reading %s\n", keyfile);
Cody Cutler's avatar
Cody Cutler committed
206 207
	}

Cody Cutler's avatar
Cody Cutler committed
208
	printf("read %d bytes from %s\n", bloblen, keyfile);
Cody Cutler's avatar
Cody Cutler committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222

	/* create context and connect */
	ret = Tspi_Context_Create(&hContext);
	check("context create", ret);
	ret = Tspi_Context_Connect(hContext, NULL);
	check("context connect", ret);

	ret = Tspi_Context_LoadKeyByUUID(hContext, TSS_PS_TYPE_SYSTEM,
	    srkUUID, &hSRK);
	check("loadkeybyuuid", ret);

	/* srk password */
	ret = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &srkpol);
	check("get policy object", ret);
Cody Cutler's avatar
Cody Cutler committed
223 224
	ret = Tspi_Policy_SetSecret(srkpol, TSS_SECRET_MODE_PLAIN,
	    strlen(srkpass), srkpass);
Cody Cutler's avatar
Cody Cutler committed
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
	check("policy set secret", ret);

	/* owner TPM password */
	ret = Tspi_Context_GetTpmObject(hContext, &hTPM);
	check("get policy object", ret);

	/* Insert the owner auth into the TPM's policy */
	/*ret = Tspi_GetPolicyObject(hTPM, TSS_POLICY_USAGE, &hTPMPolicy);
	check("get tpm policy", ret);

	ret = Tspi_Policy_SetSecret(hTPMPolicy, TSS_SECRET_MODE_PLAIN,
		3, "123");
	check("set owner secret", ret);*/

	ret = Tspi_Context_LoadKeyByBlob(hContext, hSRK, bloblen, blob, &hKey);
	check("load key by blob", ret);

	/* For Quote */
	ret = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_PCRS, 0,
	    &hpcomp);
	check("create pcrs object", ret);
	ret = Tspi_PcrComposite_SelectPcrIndex(hpcomp, 0);
	check("select pcr index", ret);
	/* For Quote2 */
	/*ret = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_PCRS,
	    TSS_PCRS_STRUCT_INFO_SHORT, &hpcomp);
	check("create pcrs object", ret);
	ret = Tspi_PcrComposite_SelectPcrIndexEx(hpcomp, 9,
	    TSS_PCRS_DIRECTION_RELEASE);
	check("select pcr index ex", ret);*/

	/* nonce */
	memset(&valdata, 0, sizeof(valdata));
	valdata.ulExternalDataLength = 20;
	valdata.rgbExternalData = "\x80\x0\x0\x0\x0\x0\x0\x0"
	    "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x1";

	ret = Tspi_TPM_Quote(hTPM, hKey, hpcomp, &valdata);
	check("quote", ret);
	/*ret = Tspi_TPM_Quote2(hTPM, hKey, TRUE, hpcomp, &valdata, &rub1, &rub2);
	check("quote2", ret);*/

	/* Verify hash */
	/*ret = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_HASH,
	    TSS_HASH_SHA1, &hHash);
	check("create hash", ret);
	ret = Tspi_Hash_UpdateHashValue(hHash, valdata.ulDataLength,
	    valdata.rgbData);
	check("update hash", ret);
	ret = Tspi_Hash_VerifySignature(hHash, hKey,
	    valdata.ulValidationDataLength, valdata.rgbValidationData);
	check("verify hash", ret);*/

	/* Print out Quote info */
	pquote(&valdata);

	/* Print out Quote2 info */
	//pquote2(&valdata);

	/* This nonsense doesn't work */
	/*printf("*****\n");
	rub1 = 0;
	ret = Tspi_PcrComposite_GetPcrValue(hpcomp, 1, &rub1, &rub2);
	check("get pcr value", ret);
	printf("len: %d: ", rub1);
	for (j = 0; j < rub1; j++)
		printf("%02x ", rub2[j]);
	printf("\n");*/

	Tspi_Context_FreeMemory(hContext, NULL);
	Tspi_Context_Close(hContext);

	return 0;
}