Exemple #1
0
static uint32_t swapOutKey(uint32_t handle)
{	
	unsigned char labelhash[20];
	char *filename = createKeyFilename(handle);
	STACK_TPM_BUFFER(context);
	uint32_t ret = 0;

	if (NULL == filename) {
		ret = ERR_MEM_ERR;
	}

#if 0
	printf("Swapping OUT key with handle %08x\n",handle);
#endif
	
	TSS_sha1("KEY",3,labelhash);


	if (ret == 0) {
		ret = TPM_SaveContext(handle,
		                      TPM_RT_KEY,
		                      (char *)labelhash,
		                      &context);
	}

	if (ret == 0) {
		FILE * f = fopen(filename, "w+");
		if (f) {
			fwrite(context.buffer, context.used, 1, f);
			fclose(f);
		} else {
			ret = ERR_BAD_FILE;
		}
	}
	
	if (ret == 0) {
		ret = TPM_EvictKey(handle);
#if 0
		printf("Evicted key with handle 0x%08x\n",handle);
	} else {
		printf("DID NOT Evicted key with handle 0x%08x\n",handle);
#endif
	}

#if 0
	if (ret == 0) {
		printf("Swapped out key with handle %08x.\n",handle);
	} else {
		printf("Could NOT swap out key with handle %08x.\n",handle);
	}
#endif
	
	return ret;
}
int main(int argc, char *argv[])
{
	unsigned char labelhash[20];
	int ret;
	char * filename = NULL;
	char * label = NULL;
	uint32_t handle = 0xffffffff;
	uint32_t restype = 0;
	STACK_TPM_BUFFER(context);

	int i = 1;

	TPM_setlog(0);

	while (i < argc) {
		if (!strcmp("-rt",argv[i])) {
			i++;
			if (i < argc) {
				sscanf(argv[i],"%x",&restype);
			} else {
				printf("Missing parameter for -rt.\n");
				usage();
			}
		} else
		if (!strcmp("-of",argv[i])) {
			i++;
			if (i < argc) {
				filename = argv[i];
			} else {
				printf("Missing parameter for -of.\n");
				usage();
			}
		} else
		if (!strcmp("-la",argv[i])) {
			i++;
			if (i < argc) {
				label = argv[i];
			} else {
				printf("Missing parameter for -la.\n");
				usage();
			}
		} else
		if (!strcmp("-ha",argv[i])) {
			i++;
			if (i < argc) {
				sscanf(argv[i],"%x",&handle);
			} else {
				printf("Missing parameter for -ha.\n");
				usage();
			}
		} else
		if (!strcmp("-v",argv[i])) {
			TPM_setlog(1);
		} else
		    if (!strcmp("-h",argv[i])) {
			usage();

		} else {
			printf("\n%s is not a valid option\n", argv[i]);
			usage();
		}
		i++;
	}

	if (NULL == filename || 0xffffffff == handle || NULL == label) {
		printf("Missing argument.\n"); 
		usage(); 
	}
	
	if (NULL != label) {
		TSS_sha1(label,strlen(label),labelhash);
	}
	
   
	ret  = TPM_SaveContext(handle, restype, (char *)labelhash, &context);

	if (0 != ret) {
		printf("SaveContext returned error '%s' (%d).\n",
		       TPM_GetErrMsg(ret),
		       ret);
	} else {
		FILE * f = fopen(filename, "wb");
		if (NULL != f) {
			fwrite(context.buffer,context.used,1,f);
			fclose(f);
		}
	}
	

	exit(ret);
}