idkey.c 10.3 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
 */

#include <stdio.h>
25
#include <stdlib.h>
Cody Cutler's avatar
Cody Cutler committed
26
#include <unistd.h>
Cody Cutler's avatar
Cody Cutler committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <string.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <iconv.h>
#include <langinfo.h>
#include <sys/stat.h>
#include <tss/tspi.h>
#include <tss/platform.h>
#include <tss/tss_typedef.h>
#include <tss/tss_structs.h>

#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/x509.h>

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

#define FATAL(x)	do{printf("**\t");printf(x);printf("\n"); return 1;}while(0);
#define print_error(x,y)	do{printf(x);}while(0);

Cody Cutler's avatar
Cody Cutler committed
48 49 50 51
/*
 * This code is hideous because it has never been loved properly.
 */

52 53
void check(char *msg, int cin)
{
Cody Cutler's avatar
Cody Cutler committed
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
	int in = TSS_ERROR_CODE(cin);
	printf("%s: ", msg);
	if(in == TSS_SUCCESS)
		printf("TSS_SUCCESS\n");
	else if(in == TSS_E_INVALID_HANDLE)
		printf("TSS_E_INVALID_HANDLE\n");
	else if(in == TSS_E_INTERNAL_ERROR)
		printf("TSS_E_INTERNAL_ERROR\n");
	else if(in == TSS_E_BAD_PARAMETER)
		printf("TSS_E_BAD_PARAMETER\n");
	else if(in == TSS_E_HASH_INVALID_LENGTH)
		printf("TSS_E_HASH_INVALID_LENGTH\n");
	else if(in == TSS_E_HASH_NO_DATA)
		printf("TSS_E_HASH_NO_DATA\n");
	else if(in == TSS_E_INVALID_SIGSCHEME)
		printf("TSS_E_INVALID_SIGSCHEME\n");
	else if(in == TSS_E_HASH_NO_IDENTIFIER)
		printf("TSS_E_HASH_NO_IDENTIFIER\n");
	else if(in == TSS_E_PS_KEY_NOTFOUND)
		printf("TSS_E_PS_KEY_NOTFOUND\n");
	else if(in == TSS_E_BAD_PARAMETER)
		printf("TSS_E_BAD_PARAMETER\n");
	else if(in == TSS_E_PS_KEY_NOTFOUND)
		printf("TSS_E_P_KEY_NOTFOUND\n");
	else
		printf("Not here: 0x%x\n", in);
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
}

void check_fail(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("TSS_E_INVALID_HANDLE\n");
	else if(in == TSS_E_INTERNAL_ERROR)
		printf("TSS_E_INTERNAL_ERROR\n");
	else if(in == TSS_E_BAD_PARAMETER)
		printf("TSS_E_BAD_PARAMETER\n");
	else if(in == TSS_E_HASH_INVALID_LENGTH)
		printf("TSS_E_HASH_INVALID_LENGTH\n");
	else if(in == TSS_E_HASH_NO_DATA)
		printf("TSS_E_HASH_NO_DATA\n");
	else if(in == TSS_E_INVALID_SIGSCHEME)
		printf("TSS_E_INVALID_SIGSCHEME\n");
	else if(in == TSS_E_HASH_NO_IDENTIFIER)
		printf("TSS_E_HASH_NO_IDENTIFIER\n");
	else if(in == TSS_E_PS_KEY_NOTFOUND)
		printf("TSS_E_PS_KEY_NOTFOUND\n");
	else if(in == TSS_E_BAD_PARAMETER)
		printf("TSS_E_BAD_PARAMETER\n");
	else if(in == TSS_E_PS_KEY_NOTFOUND)
		printf("TSS_E_P_KEY_NOTFOUND\n");
	else
		printf("Not here: 0x%x\n", in);
	exit(EXIT_FAILURE);
Cody Cutler's avatar
Cody Cutler committed
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
}

int blob_pubkey(char *out, unsigned int olen, char *in, unsigned int ilen,
    char *p, unsigned int psize)
{
	unsigned int offset, size;

	if (olen < ilen) {
		printf("\t\tblob_pubkey failed: olen < ilen\n");
		return;
	}
	if (psize == 0) {
		printf("\t\tblob_pubkey failed: psize == 0\n");
		return;
	}
	memcpy(out, in, ilen);

	/* Keep all the keyparms the same, overwrite the public key at end.
	 * Check sizes to make sure we get the right offset.  They are
	 * big-endian. */

	offset = 4 + 2 + 2;
	/* Key parms length */
	size = Decode_UINT32(in + offset);
	offset += 4;
	offset += size;

	/* Key length */
	UINT32ToArray(psize, out + offset);
	offset += 4;
	/* out + offset should now be pointing to the public key */
	memcpy(out + offset, p, psize);
	offset += psize;

	return offset;
}

TSS_RESULT
make_fake_key(TSS_HCONTEXT hContext, TSS_HKEY *hCAKey, RSA **rsa, int padding)
{
	TSS_RESULT result;
	UINT32 encScheme, size_n, pub_size;
	BYTE n[2048];
	TCPA_PUBKEY pubkey;
	UINT32 blob_size, size;
	BYTE *blob, pub_blob[1024];

	switch (padding) {
		case RSA_PKCS1_PADDING:
			encScheme = TSS_ES_RSAESPKCSV15;
			break;
		case RSA_PKCS1_OAEP_PADDING:
			encScheme = TSS_ES_RSAESOAEP_SHA1_MGF1;
			break;
		case RSA_NO_PADDING:
			encScheme = TSS_ES_NONE;
			break;
		default:
			return TSS_E_INTERNAL_ERROR;
			break;
	}

		//Create CA Key Object
	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_RSAKEY,
	    TSS_KEY_TYPE_LEGACY|TSS_KEY_SIZE_2048, hCAKey);
	if (result != TSS_SUCCESS) {
		check("Tspi_Context_CreateObject", result);
		return result;
	}

		// generate a software key to represent the CA's key
	if ((*rsa = RSA_generate_key(2048, 65537, NULL, NULL)) == NULL) {
		ERR_print_errors_fp(stdout);
		return 254; // ?
	}

		// get the pub CA key
	if ((size_n = BN_bn2bin((*rsa)->n, n)) <= 0) {
		fprintf(stderr, "BN_bn2bin failed\n");
		ERR_print_errors_fp(stdout);
		RSA_free(*rsa);
                return 254; // ?
        }

	result = Tspi_GetAttribData(*hCAKey, TSS_TSPATTRIB_KEY_BLOB,
	    TSS_TSPATTRIB_KEYBLOB_PUBLIC_KEY, &blob_size, &blob);
	if (result != TSS_SUCCESS) {
		check("Tspi_GetAttribData", result);
		return result;
	}

	pub_size = blob_pubkey(pub_blob, 1024, blob, blob_size, n, size_n);

	result = Tspi_SetAttribData(*hCAKey, TSS_TSPATTRIB_KEY_BLOB,
	    TSS_TSPATTRIB_KEYBLOB_PUBLIC_KEY, pub_size, pub_blob);
	if (result != TSS_SUCCESS) {
		check("Tspi_SetAttribData", result);
		return result;
	}

		// set the CA key's algorithm
	result = Tspi_SetAttribUint32(*hCAKey, TSS_TSPATTRIB_KEY_INFO,
				      TSS_TSPATTRIB_KEYINFO_ALGORITHM,
				      TSS_ALG_RSA);
	if (result != TSS_SUCCESS) {
		check("Tspi_SetAttribUint32", result);
		RSA_free(*rsa);
		return result;
	}

		// set the CA key's number of primes
	result = Tspi_SetAttribUint32(*hCAKey, TSS_TSPATTRIB_RSAKEY_INFO,
				      TSS_TSPATTRIB_KEYINFO_RSA_PRIMES,
				      2);
	if (result != TSS_SUCCESS) {
		check("Tspi_SetAttribUint32", result);
		RSA_free(*rsa);
		return result;
	}

		// set the CA key's encryption scheme
	result = Tspi_SetAttribUint32(*hCAKey, TSS_TSPATTRIB_KEY_INFO,
				      TSS_TSPATTRIB_KEYINFO_ENCSCHEME,
				      encScheme);
	if (result != TSS_SUCCESS) {
		check("Tspi_SetAttribUint32", result);
		RSA_free(*rsa);
		return result;
	}

	return TSS_SUCCESS;
}

Cody Cutler's avatar
Cody Cutler committed
245 246 247 248 249 250 251 252
void usage(char *name)
{
	printf("\n");
	printf("%s -t <tpmpass> -s <srkpass>\n", name);
	printf("\n");
	exit(EXIT_FAILURE);
}

Cody Cutler's avatar
Cody Cutler committed
253
int
Cody Cutler's avatar
Cody Cutler committed
254
main(int argc, char **argv)
Cody Cutler's avatar
Cody Cutler committed
255
{
Cody Cutler's avatar
Cody Cutler committed
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
	RSA			*rsa = NULL;
	TSS_HCONTEXT 		hContext;
	TSS_HKEY 		hKey, hSRK, hCAKey;
	TSS_HPOLICY 		hTPMPolicy, hidpol;
	TSS_UUID 		srkUUID = TSS_UUID_SRK;
	TSS_HPOLICY		srkpol;
	TSS_HTPM 		hTPM;
	UINT32			idbloblen, ch;
	int			ret,i, blobos, fd;
	BYTE			*srkpass, *tpmpass;
	BYTE			*blobo, *idblob;

	srkpass = tpmpass = NULL;
	while ((ch = getopt(argc, argv, "hs:t:")) != -1) {
		switch (ch) {
			case 's':
				srkpass = optarg;
				break;
			case 't':
				tpmpass = optarg;
				break;
			case 'h':
			default:
				usage(argv[0]);
				break;
		}
	}

	if (!srkpass || !tpmpass)
		usage(argv[0]);
Cody Cutler's avatar
Cody Cutler committed
286 287 288

	/* create context and connect */
	ret = Tspi_Context_Create(&hContext);
289
	check_fail("context create", ret);
Cody Cutler's avatar
Cody Cutler committed
290
	ret = Tspi_Context_Connect(hContext, NULL);
291
	check_fail("context connect", ret);
Cody Cutler's avatar
Cody Cutler committed
292 293 294

	ret = Tspi_Context_LoadKeyByUUID(hContext, TSS_PS_TYPE_SYSTEM, srkUUID,
	    &hSRK);
295
	check_fail("loadkeybyuuid", ret);
Cody Cutler's avatar
Cody Cutler committed
296 297

	ret = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &srkpol);
298
	check_fail("get policy object", ret);
Cody Cutler's avatar
Cody Cutler committed
299 300 301
	//ret = Tspi_Policy_SetSecret(srkpol, TSS_SECRET_MODE_PLAIN, 4, "1234");
	ret = Tspi_Policy_SetSecret(srkpol, TSS_SECRET_MODE_PLAIN,
	    strlen(srkpass), srkpass);
302
	check_fail("policy set secret", ret);
Cody Cutler's avatar
Cody Cutler committed
303 304

	ret = Tspi_Context_GetTpmObject(hContext, &hTPM);
305
	check_fail("get policy object", ret);
Cody Cutler's avatar
Cody Cutler committed
306 307 308

	//Insert the owner auth into the TPM's policy
	ret = Tspi_GetPolicyObject(hTPM, TSS_POLICY_USAGE, &hTPMPolicy);
309
	check_fail("get tpm policy", ret);
Cody Cutler's avatar
Cody Cutler committed
310 311

	ret = Tspi_Policy_SetSecret(hTPMPolicy, TSS_SECRET_MODE_PLAIN,
Cody Cutler's avatar
Cody Cutler committed
312
		strlen(tpmpass), tpmpass);
313
	check_fail("set owner secret", ret);
Cody Cutler's avatar
Cody Cutler committed
314 315 316 317 318 319 320 321

	ret = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_RSAKEY, 
		  //TSS_KEY_TYPE_STORAGE
		  TSS_KEY_TYPE_IDENTITY
		  //TSS_KEY_TYPE_SIGNING
		| TSS_KEY_SIZE_2048 | TSS_KEY_NO_AUTHORIZATION
		| TSS_KEY_NOT_MIGRATABLE | TSS_KEY_VOLATILE
		, &hKey);
322
	check_fail("create object - key", ret);
Cody Cutler's avatar
Cody Cutler committed
323 324

	ret = Tspi_GetPolicyObject(hKey, TSS_POLICY_USAGE, &hidpol);
325
	check_fail("get id key policy", ret);
Cody Cutler's avatar
Cody Cutler committed
326 327

	ret = Tspi_Policy_SetSecret(hidpol, TSS_SECRET_MODE_PLAIN,
Cody Cutler's avatar
Cody Cutler committed
328
	    strlen(srkpass), srkpass);
329
	check_fail("set idkey secret", ret);
Cody Cutler's avatar
Cody Cutler committed
330 331 332 333 334 335 336

	/* We must create this fake privacy CA key in software so that
	 * Tspi_TPM_CollateIdentityRequest will happily work.  It needs it to
	 * create the cert request which is required in a normal remote
	 * attestion procedure.  It is not needed in our setup though.
	 */
	ret = make_fake_key(hContext, &hCAKey, &rsa, RSA_PKCS1_OAEP_PADDING);
337
	check_fail("ca nonsense", ret);
Cody Cutler's avatar
Cody Cutler committed
338 339 340 341 342 343 344

	/* We do not care about idblob - that is the certificate request that
	 * we are supposed to send to our CA in normal remote attestation.  The
	 * fifth argument is our identity label (it is supposed to be unicode).
	 */
	ret = Tspi_TPM_CollateIdentityRequest(hTPM, hSRK, hCAKey, 8, "id label",
	    hKey, TSS_ALG_3DES, &idbloblen, &idblob);
345
	check_fail("collate id", ret);
Cody Cutler's avatar
Cody Cutler committed
346 347 348 349 350 351

	blobo = NULL;
	/*ret = Tspi_GetAttribData(hKey, TSS_TSPATTRIB_KEY_BLOB,
	    TSS_TSPATTRIB_KEYBLOB_PUBLIC_KEY, &blobos, &blobo);*/
	ret = Tspi_GetAttribData(hKey, TSS_TSPATTRIB_KEY_BLOB,
	    TSS_TSPATTRIB_KEYBLOB_BLOB, &blobos, &blobo);
352
	check_fail("get blob", ret);
Cody Cutler's avatar
Cody Cutler committed
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

	if (!blobo) {
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		FATAL("no blobo");
	}

	printf("size: %d\n", blobos);
	for (i = 0;i < blobos; i++) {
		printf("\\x%x", blobo[i]);
	}
	printf("\n");

	fd = open("key.blob", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
	if (fd == -1) {
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		FATAL("Open\n");
	}
	ret = write(fd, blobo, blobos);
	if (ret != blobos)
		printf("Warning: couldn't write the whole key\n");
	close(fd);

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

	return 0;
}