int
main_v1_2( char version )
{
	char		*function = "Tspi_GetAttribData21";
	TSS_HCONTEXT	hContext;
	TSS_HKEY	hSRK, hKey;
	TSS_HPOLICY	hSrkPolicy;
	BYTE		*rgbDataToSeal = "This is a test";
	BYTE		rgbPcrValue[20];
	TSS_HPCRS	hPcrComposite;
	UINT32		AttribDataSize;
	BYTE*		AttribData;
	UINT32		ulDataLength = strlen(rgbDataToSeal);
	TSS_RESULT	result,resultFree;
	TSS_FLAG        keyInitFlags = TSS_KEY_SIZE_2048 |
				       TSS_KEY_TYPE_STORAGE |
				       TSS_KEY_NO_AUTHORIZATION;
	TSS_FLAG	pcrsInitFlags = 0;

	print_begin_test( function );

	if (version == TESTSUITE_TEST_TSS_1_2) {
		keyInitFlags |= TSS_KEY_STRUCT_KEY12;
		pcrsInitFlags |= TSS_PCRS_STRUCT_INFO_LONG;
	}

	memset(rgbPcrValue, 0x5a, sizeof(rgbPcrValue));

		// Create Context
	result = Tspi_Context_Create( &hContext );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Create", result );
		print_end_test(function);
		exit( result );
	}

		// Connect to Context
	result = Tspi_Context_Connect( hContext, get_server(GLOBALSERVER) );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Connect", result );
		print_end_test(function);
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Context_CreateObject( hContext, TSS_OBJECT_TYPE_RSAKEY, keyInitFlags, &hKey );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (hKey)", result );
		print_end_test(function);
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

		//Load Key by UUID
	result = Tspi_Context_LoadKeyByUUID( hContext, TSS_PS_TYPE_SYSTEM,
				SRK_UUID, &hSRK );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_LoadKeyByUUID (hSRK)", result );
		print_end_test(function);
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

#ifndef TESTSUITE_NOAUTH_SRK
	result = Tspi_GetPolicyObject( hSRK, TSS_POLICY_USAGE, &hSrkPolicy );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_GetPolicyObject", result );
		print_end_test(function);
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Policy_SetSecret( hSrkPolicy, TESTSUITE_SRK_SECRET_MODE,
				TESTSUITE_SRK_SECRET_LEN, TESTSUITE_SRK_SECRET );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Policy_SetSecret", result );
		print_end_test(function);
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}
#endif

	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_PCRS, pcrsInitFlags,
					   &hPcrComposite );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (hPcrComposite)",
				result );
		print_end_test(function);
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_PcrComposite_SetPcrValue( hPcrComposite, 8, 20, rgbPcrValue );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_PcrComposite_SetPcrValue", result );
		print_end_test(function);
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_PcrComposite_SetPcrLocality( hPcrComposite, TPM_LOC_ZERO );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_PcrComposite_SetPcrLocality", result );
		print_end_test(function);
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Key_CreateKey(hKey, hSRK, hPcrComposite);
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Key_CreateKey", result );
		print_end_test(function);
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}


	// Checking flag and subflags

		//Call GetAttribData for subFlag TSS_TSPATTRIB_KEYPCRLONG_DIGEST_ATCREATION
	result = Tspi_GetAttribData(hKey,
			TSS_TSPATTRIB_KEY_PCR_LONG,
			TSS_TSPATTRIB_KEYPCRLONG_DIGEST_ATCREATION, 
			&AttribDataSize, &AttribData);
	if ( result != TSS_SUCCESS ) 
	{
		print_error("Tspi_GetAttribData - subflag TSS_TSPATTRIB_KEYPCRLONG_DIGEST_ATCREATION",
							result );
		print_end_test(function);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	else
	{
		resultFree = Tspi_Context_FreeMemory(hContext, AttribData);
		if ( resultFree != TSS_SUCCESS )
		{
			print_error( "Tspi_Context_FreeMemory", resultFree );
			print_end_test(function);
			exit(resultFree);
		}
		print_success( "Tspi_GetAttribData - subflag TSS_TSPATTRIB_KEYPCRLONG_DIGEST_ATCREATION -",
						result );
	}

		//Call GetAttribData for subFlag TSS_TSPATTRIB_KEYPCRLONG_DIGEST_ATRELEASE
	result = Tspi_GetAttribData(hKey,
			TSS_TSPATTRIB_KEY_PCR_LONG,
			TSS_TSPATTRIB_KEYPCRLONG_DIGEST_ATRELEASE,
			&AttribDataSize, &AttribData);
	if ( result != TSS_SUCCESS ) 
	{
		print_error( "Tspi_GetAttribData - subflag TSS_TSPATTRIB_KEYPCRLONG_DIGEST_ATRELEASE",
							result );
		print_end_test(function);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	else
	{
		resultFree = Tspi_Context_FreeMemory(hContext, AttribData);
		if ( resultFree != TSS_SUCCESS )
		{
			print_error( "Tspi_Context_FreeMemory", resultFree );
			print_end_test(function);
			exit(resultFree);
		}
		print_success("Tspi_GetAttribData - subflag TSS_TSPATTRIB_KEYPCRLONG_DIGEST_ATRELEASE -",
						result );
	}

		//Call GetAttribData for subFlag TSS_TSPATTRIB_KEYPCRLONG_CREATION_SELECTION
	result = Tspi_GetAttribData(hKey,
			TSS_TSPATTRIB_KEY_PCR_LONG,
			TSS_TSPATTRIB_KEYPCRLONG_CREATION_SELECTION,
			&AttribDataSize, &AttribData);
	if ( result != TSS_SUCCESS ) 
	{
		print_error( "Tspi_GetAttribData - subflag TSS_TSPATTRIB_KEYPCRLONG_CREATION_SELECTION",
						result );
		print_end_test(function);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	else
	{
		resultFree = Tspi_Context_FreeMemory(hContext, AttribData);
		if ( resultFree != TSS_SUCCESS )
		{
			print_error( "Tspi_Context_FreeMemory", resultFree );
			print_end_test(function);
			exit(resultFree);
		}
		print_success( "Tspi_GetAttribData - subflag TSS_TSPATTRIB_KEYPCRLONG_CREATION_SELECTION -",
						result );
	}

		//Call GetAttribData for subFlag TSS_TSPATTRIB_KEYPCRLONG_RELEASE_SELECTION
	result = Tspi_GetAttribData(hKey,
			TSS_TSPATTRIB_KEY_PCR_LONG,
			TSS_TSPATTRIB_KEYPCRLONG_RELEASE_SELECTION,
			&AttribDataSize, &AttribData);
	if ( result != TSS_SUCCESS ) 
	{
		print_error( "Tspi_GetAttribData - subflag TSS_TSPATTRIB_KEYPCRLONG_RELEASE_SELECTION",
						result );
		print_end_test(function);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	else
	{
		resultFree = Tspi_Context_FreeMemory(hContext, AttribData);
		if ( resultFree != TSS_SUCCESS )
		{
			print_error( "Tspi_Context_FreeMemory", resultFree );
			print_end_test(function);
			exit(resultFree);
		}
		print_success( "Tspi_GetAttribData - subflag TSS_TSPATTRIB_KEYPCRLONG_RELEASE_SELECTION -",
						result );
	}

	print_end_test(function);

	Tspi_Context_Close(hContext);
	exit( 0 );

}
main_v1_1(void){

	char		*nameOfFunction = "Tspi_Key_CreateKeyWithPcrs01";
	TSS_HCONTEXT	hContext;
	TSS_HTPM	hTPM;
	TSS_FLAG	initFlags;
	TSS_HKEY	hKey;
	TSS_HKEY	hSRK;
	TSS_HPCRS	hPcrs;
	TSS_RESULT	result;
	TSS_HPOLICY	srkUsagePolicy, keyUsagePolicy;
	BYTE		*pcrValue;
	UINT32		pcrLen;
	BYTE		pcrData[] = "09876543210987654321";
	initFlags	= TSS_KEY_TYPE_BIND | TSS_KEY_SIZE_2048  |
			TSS_KEY_VOLATILE | TSS_KEY_AUTHORIZATION |
			TSS_KEY_NOT_MIGRATABLE;

	print_begin_test(nameOfFunction);


	result = connect_load_all(&hContext, &hSRK, &hTPM);
	if (result != TSS_SUCCESS) {
		print_error("connect_load_all", result);
		exit(result);
	}

		//Create Object
	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_RSAKEY,
				initFlags, &hKey);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_CreateObject", result);
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Get Policy Object
	result = Tspi_GetPolicyObject(hKey, TSS_POLICY_USAGE, &keyUsagePolicy);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_GetPolicyObject", result);
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Set Secret
	result = Tspi_Policy_SetSecret(keyUsagePolicy, TESTSUITE_KEY_SECRET_MODE,
				TESTSUITE_KEY_SECRET_LEN, TESTSUITE_KEY_SECRET);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Policy_SetSecret", result);
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(result);
	}

		//Create PCRs Object
	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_PCRS,
					   0, &hPcrs);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_CreateObject", result);
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Get PCR vals from TPM
	result = Tspi_TPM_PcrRead(hTPM, 1, &pcrLen, &pcrValue);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_TPM_PcrRead", result);
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Set PCR vals in the object
	result = Tspi_PcrComposite_SetPcrValue(hPcrs, 1, pcrLen, pcrValue);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_CreateObject", result);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	Tspi_Context_FreeMemory(hContext, pcrValue);

		//Get PCR vals from TPM
	result = Tspi_TPM_PcrRead(hTPM, 15, &pcrLen, &pcrValue);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_TPM_PcrRead", result);
		Tspi_Context_Close(hContext);
		exit(result);
	}
		//Set PCR vals in the object
	result = Tspi_PcrComposite_SetPcrValue(hPcrs, 15, pcrLen, pcrValue);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_CreateObject", result);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	Tspi_Context_FreeMemory(hContext, pcrValue);

		//Create Key
	result = Tspi_Key_CreateKey(hKey, hSRK, hPcrs);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Key_CreateKey", result);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	/* if the key loads, the key creation is successful */
	result = Tspi_Key_LoadKey(hKey, hSRK);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Key_LoadKey ", result);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	/* now, encrypt and decrypt some data to see if the key "works" */
	result = bind_and_unbind(hContext, hKey);
	if (result != TSS_SUCCESS) {
		print_error("bind_and_unbind ", result);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	/* now, change a PCR value that the key is set to */
	result = Tspi_TPM_PcrExtend(hTPM, 15, 20, pcrData, NULL,
				    &pcrLen, &pcrValue);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_TPM_PcrExtend ", result);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	/* now, encrypt and decrypt some data, which should fail, since
	 * the PCR changed */
	result = bind_and_unbind(hContext, hKey);
	if (result != TCPA_E_WRONGPCRVAL){
		if(!checkNonAPI(result)){
			print_error(nameOfFunction, result);
			print_end_test(nameOfFunction);
			Tspi_Context_CloseObject(hContext, hKey);
			Tspi_Context_Close(hContext);
			exit(result);
		}
		else{
			print_error_nonapi(nameOfFunction, result);
			print_end_test(nameOfFunction);
			Tspi_Context_CloseObject(hContext, hKey);
			Tspi_Context_Close(hContext);
			exit(result);
		}
	}
	else{
		print_success(nameOfFunction, result);
		print_end_test(nameOfFunction);
		Tspi_Context_CloseObject(hContext, hKey);
		Tspi_Context_Close(hContext);
		exit(0);
	}
}
예제 #3
0
int TSPI_SealCurrPCR(TSS_HCONTEXT c, uint32_t keyhandle, uint32_t pcrmap,
			 unsigned char *keyauth,
			 unsigned char *dataauth,
			 unsigned char *data, unsigned int datalen,
			 unsigned char *blob, unsigned int *bloblen)
{

#define CHECK_ERROR(r,m) if (r != TSS_SUCCESS) { fprintf(stderr, m ": 0x%08x\n", r); return -1;}

	TSS_RESULT r = 0;
	TSS_HTPM tpm;
	TSS_HPCRS pcrComposite;
	TSS_UUID uuid;
	TSS_UUID srk_uuid = TSS_UUID_SRK;
	TSS_HKEY key;
	TSS_HENCDATA seal;
	TSS_HPOLICY key_policy, seal_policy;
	unsigned char *cipher;
	unsigned int cipher_len;

	/* Get the PCR values into composite object */
	r = Tspi_Context_GetTpmObject(c, &tpm);
	CHECK_ERROR(r, "Error Getting TPM");
	r = Tspi_Context_CreateObject(c, TSS_OBJECT_TYPE_PCRS, TSS_PCRS_STRUCT_INFO_LONG, &pcrComposite);
	CHECK_ERROR(r, "Error Creating PCR-Composite");
	r = Tspi_PcrComposite_SetPcrLocality(pcrComposite, TPM_LOC_ZERO | TPM_LOC_ONE |
				TPM_LOC_TWO | TPM_LOC_THREE | TPM_LOC_FOUR);
	CHECK_ERROR(r, "Error Setting Localities");

	for (uint32_t pcrmask = 1, pcr = 0; pcr < NUM_PCRS; pcr++, pcrmask <<= 1) {
		if ((pcrmap & pcrmask) != 0) {
			uint32_t pcrval_size;
			uint8_t *pcrval;
			if (pcrvals[pcr] == NULL) {
			     r = Tspi_TPM_PcrRead(tpm, pcr, &pcrval_size, &pcrval);
			     CHECK_ERROR(r, "Error Reading PCR");
			     r = Tspi_PcrComposite_SetPcrValue(pcrComposite, pcr, pcrval_size, pcrval);
			     CHECK_ERROR(r, "Error Setting Composite");
			     r = Tspi_Context_FreeMemory(c, pcrval);
			     CHECK_ERROR(r, "Error Freeing Memory");
			}
			else {
			     pcrval = pcrvals[pcr];
			     r = Tspi_PcrComposite_SetPcrValue(pcrComposite, pcr, LEN, pcrval);
			     CHECK_ERROR(r, "Error Setting Composite");
			}
		}
	}

	/* Get the SRK and Policy Ready */
	if (keyhandle = 0x40000000) {
		uuid = srk_uuid;
	} else {
		fprintf(stderr, "Error, only SRK currently supported\n");
		r = 1;
		return -1;
	}
	r = Tspi_Context_LoadKeyByUUID(c, TSS_PS_TYPE_SYSTEM, uuid, &key);
	CHECK_ERROR(r, "Error Loading Key");
	r = Tspi_Context_CreateObject(c, TSS_OBJECT_TYPE_POLICY, TSS_POLICY_USAGE, &key_policy);
	CHECK_ERROR(r, "Error Creating Policy");
	r = Tspi_Policy_SetSecret(key_policy, TSS_SECRET_MODE_SHA1, keylen, keyauth);
	CHECK_ERROR(r, "Error Setting Secret");
	r = Tspi_Policy_AssignToObject(key_policy, key);
	CHECK_ERROR(r, "Error Assigning Policy");

	/* Get the Encdata Ready */
	r = Tspi_Context_CreateObject(c, TSS_OBJECT_TYPE_ENCDATA, TSS_ENCDATA_SEAL, &seal);
	CHECK_ERROR(r, "Error Creating EncData");
	r = Tspi_Context_CreateObject(c, TSS_OBJECT_TYPE_POLICY, TSS_POLICY_USAGE, &seal_policy);
	CHECK_ERROR(r, "Error Creating Policy");
	r = Tspi_Policy_SetSecret(seal_policy, TSS_SECRET_MODE_SHA1, keylen, dataauth);
	CHECK_ERROR(r, "Error Setting Secret");
	r = Tspi_Policy_AssignToObject(seal_policy, seal);
	CHECK_ERROR(r, "Error Assigning Policy");

	/* Seal the Data */
	r = Tspi_Data_Seal(seal, key, datalen, data, pcrComposite);
	CHECK_ERROR(r, "Error Sealing Data");
	r = Tspi_GetAttribData(seal, TSS_TSPATTRIB_ENCDATA_BLOB,
                                   TSS_TSPATTRIB_ENCDATABLOB_BLOB,
                                   &cipher_len, &cipher);
	CHECK_ERROR(r, "Error Getting Sealed Data");

	/* Return that stuff */
	if (cipher_len > bloblen) {
		sprintf(stderr, "Internal Error, cipher too long");
		r = 1;
		return -1;
	}
	memcpy(blob, cipher, cipher_len);
	*bloblen = cipher_len;

	/* Note: Do not even bother to return cipher directly. Would be freed during Context_Close anyways */
	Tspi_Context_FreeMemory(c, cipher);

	return (r == 0)? 0 : -1;
}
int main(int argc, char **argv)
{
	TSS_HKEY hKey, hSRK;
	TSS_HCONTEXT hContext;
	TSS_HTPM hTPM;
	TSS_RESULT result;
	TSS_HPOLICY hPolicy;
	TSS_HPCRS hPcrs;
	UINT32 ulPcrValueLength, subCap, subCapLength;
	UINT32 pulRespDataLength, numPcrs;
	BYTE *pNumPcrs, *rgbPcrValue, *uuidString, *pcrsSelectedValues[24];
	int i, c, *pcrsSelected = NULL, numPcrsSelected = 0;
	TSS_UUID *uuid;
	BYTE wellknown[] = TSS_WELL_KNOWN_SECRET;

	while (1) {
		c = getopt(argc, argv, "p:");
		if (c == -1)
			break;
		switch (c) {
			case 'p':
				numPcrsSelected++;
				pcrsSelected = realloc(pcrsSelected,
						       (sizeof(int) 
							* numPcrsSelected));
				if (pcrsSelected == NULL) {
					PRINT_ERR("Malloc of %zd bytes failed.",
						  (sizeof(int)
						   * numPcrsSelected));
					return -1;
				}
				pcrsSelected[numPcrsSelected - 1] =
					atoi(optarg);
				break;
			default:
				usage(argv[0]);
				break;
		}
	}
	if (numPcrsSelected == 0)
		printf("Warning: Key will not be bound to any PCR's!\n");
	if (numPcrsSelected > 24) {
		PRINT_ERR("Too many PCRs selected! Exiting.");
		return -EINVAL;
	}
        result = Tspi_Context_Create(&hContext);
        if (result != TSS_SUCCESS) {
                PRINT_TSS_ERR("Tspi_Context_Create", result);
                return result;
        }
        result = Tspi_Context_Connect(hContext, NULL);
        if (result != TSS_SUCCESS) {
                PRINT_TSS_ERR("Tspi_Context_Connect", result);
                Tspi_Context_Close(hContext);
                return result;
        }
	result = Tspi_Context_GetTpmObject(hContext, &hTPM);
	if (result != TSS_SUCCESS) {
		PRINT_TSS_ERR("Tspi_Context_GetTpmObject", result);
		Tspi_Context_Close(hContext);
		return result;
	}
	subCap = TSS_TPMCAP_PROP_PCR;
	subCapLength = sizeof(UINT32);
	result = Tspi_TPM_GetCapability(hTPM, TSS_TPMCAP_PROPERTY,
					subCapLength, (BYTE *)&subCap,
					&pulRespDataLength, &pNumPcrs );
	if (result != TSS_SUCCESS) {
		PRINT_TSS_ERR("Tspi_TPM_GetCapability", result);
		Tspi_Context_Close(hContext);
		return result;
	}
	numPcrs = *(UINT32 *)pNumPcrs;
	for (i = 0; i < (int)numPcrsSelected; i++) {
		if (pcrsSelected[i] > (int)numPcrs) {
			fprintf(stderr, "%d: invalid PCR register. PCRs range "
				"from 0 - %u\n", pcrsSelected[i], numPcrs);
			return -1;
		}
	}
	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_PCRS, 0,
					   &hPcrs);
	if (result != TSS_SUCCESS) {
		PRINT_TSS_ERR("Tspi_Context_CreateObject", result);
		return result;
	}
	for (i = 0; i < numPcrsSelected; i++) {
		result = Tspi_TPM_PcrRead(hTPM, pcrsSelected[i],
					  &ulPcrValueLength, &rgbPcrValue);
		if (result != TSS_SUCCESS) {
			PRINT_TSS_ERR("Tspi_TPM_PcrRead", result);
			Tspi_Context_Close(hContext);
			return result;
		}
		result = Tspi_PcrComposite_SetPcrValue(hPcrs, pcrsSelected[i],
						       ulPcrValueLength,
						       rgbPcrValue);
		if (result != TSS_SUCCESS) {
			PRINT_TSS_ERR("Tspi_PcrComposite_SetPcrValue", result );
			Tspi_Context_Close( hContext );
			return result;
		}

		pcrsSelectedValues[i] = rgbPcrValue;
	}
        result = Tspi_Context_LoadKeyByUUID(hContext, TSS_PS_TYPE_SYSTEM,
					    SRK_UUID, &hSRK);
        if (result != TSS_SUCCESS) {
                PRINT_TSS_ERR("Tspi_Context_LoadKeyByUUID", result);
                Tspi_Context_Close(hContext);
                return result;
        }
	result = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &hPolicy);
        if (result != TSS_SUCCESS) {
                PRINT_TSS_ERR("Tspi_GetPolicyObject", result);
                Tspi_Context_Close(hContext);
                return result;
        }

	result = Tspi_Policy_SetSecret(hPolicy, TSS_SECRET_MODE_SHA1,
				       sizeof(wellknown), wellknown);
        if (result != TSS_SUCCESS) {
                PRINT_TSS_ERR("Tspi_GetPolicyObject", result);
                Tspi_Context_Close(hContext);
                return result;
        }
	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_RSAKEY,
					   (TSS_KEY_TYPE_STORAGE
					    | TSS_KEY_SIZE_2048
					    | TSS_KEY_VOLATILE
					    | TSS_KEY_NO_AUTHORIZATION
					    | TSS_KEY_NOT_MIGRATABLE), &hKey);
	if (result != TSS_SUCCESS) {
                PRINT_TSS_ERR("Tspi_Context_CreateObject", result);
                Tspi_Context_Close(hContext);
                return result;
	}
	result = Tspi_Key_CreateKey(hKey, hSRK, hPcrs);
	if (result != TSS_SUCCESS) {
                PRINT_TSS_ERR("Tspi_Key_CreateKey", result);
                Tspi_Context_Close(hContext);
                return result;
	}
	result = Tspi_TPM_GetRandom(hTPM, (UINT32)16, (BYTE **)&uuid);
	if (result != TSS_SUCCESS) {
		PRINT_TSS_ERR("Tspi_TPM_GetRandom", result);
		Tspi_Context_Close(hContext);
		return result;
	}
	result = Tspi_Context_RegisterKey(hContext, hKey, TSS_PS_TYPE_USER,
					  *uuid, TSS_PS_TYPE_SYSTEM, SRK_UUID);
	if (result != TSS_SUCCESS) {
		PRINT_TSS_ERR("Tspi_Context_RegisterKey", result);
		Tspi_Context_Close(hContext);
		return result;
	}
	printf("Success: Key created bound to:\n");
	for (i = 0; i < numPcrsSelected; i++) {
		uuidString = (unsigned char *) 
			      util_bytes_to_string((char *)
						   pcrsSelectedValues[i], 20);
		if (uuidString == NULL) {
			PRINT_ERR("malloc of 41 bytes failed");
			Tspi_Context_Close(hContext);
			return result;
		}

		printf("PCR %d: %s\n", pcrsSelected[i], uuidString);
		free(uuidString);
		Tspi_Context_FreeMemory(hContext, pcrsSelectedValues[i]);
	}
	uuidString = (BYTE *)util_bytes_to_string((char *)uuid, 16);
	if (uuidString == NULL) {
		PRINT_ERR("malloc of 33 bytes failed");
		Tspi_Context_Close(hContext);
		return result;
	}
	printf("And registered in persistent storage with UUID "
	       "(tspi_uuid parameter): %s\n", uuidString);
	Tspi_Context_FreeMemory(hContext, (BYTE *)uuid);
	free(uuidString);
	Tspi_Context_Close(hContext);
	return 0;
}
int 
main(int argc, char **argv)
{

#define BUF_LEN	(1024*1024)
#define KEY_SIZE 64
	TSS_RESULT result;
	TSS_HCONTEXT hContext;
	TSS_HKEY hSRK, hKey;
	TSS_HPOLICY hPolicy;
	TSS_HTPM hTPM;
	TSS_HENCDATA hEncData;
	TSS_HPCRS hPcrs;
	UINT32 u32PcrValLen, u32EncDataLen;
	BYTE *rgbPcrVal, *rgbEncData;
	BYTE *random;
	FILE *fpIn = NULL, *fpOut = NULL;
	int len, size;
	char *pBufIn = NULL, *pBufOut = NULL;
	unsigned int salt[] = {12345, 54321};
	EVP_CIPHER_CTX en;
	TSS_UUID UUID_K1 =  {0, 0, 0, 0, 0, {8, 0, 0, 0, 0, 1}} ;

	if (argc < 3) {
		usage(argv[0]);
		return 0;
	}

	result = Tspi_Context_Create(&hContext);
	if (TSS_SUCCESS != result) {
		print_error("Tspi_Context_Create", result);
		Tspi_Context_Close(hContext);
		return result;
	}

	result = Tspi_Context_Connect(hContext, get_server(GLOBALSERVER));
	if (TSS_SUCCESS != result) {
		print_error("Tspi_Context_Connect", result);
		Tspi_Context_Close(hContext);
		return result;
	}

	result = Tspi_Context_GetTpmObject(hContext, &hTPM);
	if (TSS_SUCCESS != result) {
		print_error("Tspi_Context_GetTpmObject", result);
		Tspi_Context_Close(hContext);
		return result;
	}

	result = Tspi_Context_LoadKeyByUUID(hContext, 
					TSS_PS_TYPE_SYSTEM, 
					SRK_UUID, 
					&hSRK);
	if (TSS_SUCCESS != result) {
		print_error("Tspi_Context_LoadKeyByUUID", result);
		Tspi_Context_Close(hContext);
		return result;
	}

#ifndef TESTSUITE_NOAUTH_SRK
	result = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &hPolicy);
	if (TSS_SUCCESS != result) {
		print_error("Tspi_GetPolicyObject", result);
		Tspi_Context_Close(hContext);
		return result;
	}

	result = Tspi_Policy_SetSecret(hPolicy, 
						TESTSUITE_SRK_SECRET_MODE, 
						TESTSUITE_SRK_SECRET_LEN, 
						TESTSUITE_SRK_SECRET);
	if (TSS_SUCCESS != result) {
		print_error("Tspi_Policy_SetSecret", result);
		Tspi_Context_Close(hContext);
		return result;
	}
#endif // #ifndef TESTSUITE_NOAUTH_SRK

	result = Tspi_Context_CreateObject(hContext, 
						TSS_OBJECT_TYPE_PCRS, 
						0, 
						&hPcrs);
	if (TSS_SUCCESS != result) {
		print_error("Tspi_Context_CreateObject", result);
		Tspi_Context_Close(hContext);
		return result;
	}

	result = Tspi_Context_CreateObject(hContext, 
						TSS_OBJECT_TYPE_ENCDATA, 
						TSS_ENCDATA_SEAL, 
						&hEncData);
	if (TSS_SUCCESS != result) {
		print_error("Tspi_Context_CreateObject", result);
		Tspi_Context_Close(hContext);
		return result;
	}

	result = set_secret(hContext, hEncData, &hPolicy);
	if (TSS_SUCCESS != result) {
		print_error("set_secret", result);
		Tspi_Context_Close(hContext);
		return result;
	}						

	result = Tspi_Context_LoadKeyByUUID(hContext, 
						TSS_PS_TYPE_SYSTEM, 
						UUID_K1, 
						&hKey);
	if (TSS_SUCCESS != result) {
		print_error("Tspi_Context_LoadKeyByUUID", result);
		Tspi_Context_Close(hContext);
		return -1;
	}

	result = set_popup_secret(hContext, 
						hKey, 
						TSS_POLICY_USAGE, 
						"Input K1's Pin\n", 
						0);
	if (TSS_SUCCESS != result) {
		print_error("set_popup_secret", result);
		Tspi_Context_Close(hContext);
		return result;
	}

	/*result = Tspi_GetPolicyObject(hKey, TSS_POLICY_USAGE, &hPolicy);
	if (TSS_SUCCESS != result) {
		print_error("Tspi_GetPolicyObject", result);
		Tspi_Context_Close(hContext);
		return result;
	}

	result = Tspi_Policy_SetSecret(hPolicy, 
						TESTSUITE_KEY_SECRET_MODE, 
						TESTSUITE_KEY_SECRET_LEN, 
						TESTSUITE_KEY_SECRET);
	if (TSS_SUCCESS != result) {
		print_error("Tspi_Policy_SetSecret", result);
		Tspi_Context_Close(hContext);
		return result;
	}*/

	result = Tspi_TPM_GetRandom(hTPM, KEY_SIZE, &random);
	if (TSS_SUCCESS != result) {
		print_error("Tspi_TPM_GetRandom", result);
		Tspi_Context_Close(hContext);
		return result;
	}

	result = Tspi_TPM_PcrRead(hTPM, 15, &u32PcrValLen, &rgbPcrVal);
	if (TSS_SUCCESS != result) {
		print_error("Tspi_TPM_PcrRead", result);
		Tspi_Context_Close(hContext);
		return result;
	}

	result = Tspi_PcrComposite_SetPcrValue(hPcrs, 15, u32PcrValLen, rgbPcrVal);
	if (TSS_SUCCESS != result) {
		print_error("Tspi_PcrComposite_SetPcrValue", result);
		Tspi_Context_Close(hContext);
		return result;
	}

	result = Tspi_Data_Seal(hEncData, 
					hKey, 
					KEY_SIZE, 
					random, 
					hPcrs);
	if (TSS_SUCCESS != result) {
		print_error("Tspi_Data_Seal", result);
		Tspi_Context_Close(hContext);
		return result;
	}

	result = Tspi_GetAttribData(hEncData, 
						TSS_TSPATTRIB_ENCDATA_BLOB, 
						TSS_TSPATTRIB_ENCDATABLOB_BLOB, 
						&u32EncDataLen, 
						&rgbEncData);
	if (TSS_SUCCESS != result) {
		print_error("Tspi_GetAttribData", result);
		Tspi_Context_Close(hContext);
		return result;
	}

	fpIn = fopen(argv[1], "rb");
	if (!fpIn) {
		printf("open file: %s failed\n", argv[1]);
		Tspi_Context_Close(hContext);
		return result;
	}

	fseek(fpIn, 0, SEEK_END);
	size = ftell(fpIn);

	if (size > BUF_LEN) {
		printf("file is more than 1MB, too big !\n");
		Tspi_Context_Close(hContext);
		fclose(fpIn);
		return -1;
	}

	pBufIn = malloc(size);
	if (!pBufIn) {
		printf("No Memmory\n");
		Tspi_Context_Close(hContext);
	}

	fseek(fpIn, 0, SEEK_SET);
	
	len = fread(pBufIn, 1, size, fpIn);
	if (len != size) {
		printf("fread error");
		Tspi_Context_Close(hContext);
		fclose(fpIn);
		return -1;
	}

	fclose(fpIn);

	if (aes_init(random, KEY_SIZE, (unsigned char *)&salt, &en)) {
		printf("aes_init failed\n");
		Tspi_Context_Close(hContext);
		free(pBufIn);
		return -1;
	}

	pBufOut = aes_encrypt(&en, pBufIn, &size);

	fpOut = fopen(argv[2], "wb");
	if (!fpOut) {
		printf("open file: %s failed\n", argv[2]);
		Tspi_Context_Close(hContext);
		free(pBufIn);
		free(pBufOut);
		return -1;
	}

	len = fwrite(&u32EncDataLen, 1, sizeof(UINT32), fpOut);
	if (sizeof(UINT32) != len) {
		printf("fwrite u32EncDataLen failed\n");
		Tspi_Context_Close(hContext);
		free(pBufIn);
		free(pBufOut);
		fclose(fpOut);
		return -1;
	}

	len = fwrite(rgbEncData, 1, u32EncDataLen, fpOut);
	if (len != u32EncDataLen) {
		printf("fwrite rgbEncData failed\n");
		Tspi_Context_Close(hContext);
		free(pBufIn);
		free(pBufOut);
		fclose(fpOut);
		return -1;
	}

	len = fwrite(&size, 1, sizeof(int), fpOut);
	if (len != sizeof(int)) {
		printf("fwrite failed\n");
		Tspi_Context_Close(hContext);
		free(pBufIn);
		free(pBufOut);
		fclose(fpOut);
		return -1;
	}

	len = fwrite(pBufOut, 1, size, fpOut);
	if (len != size) {
		printf("fwrite failed\n");
		Tspi_Context_Close(hContext);
		free(pBufIn);
		free(pBufOut);
		fclose(fpOut);
		return -1;
	}

	fclose(fpOut);
	free(pBufIn);
	free(pBufOut);

	Tspi_Context_Close(hContext);

	return 0;
	
}
예제 #6
0
int
main_v1_1( void )
{
	char		*function = "Tspi_Data_Unseal04";
	TSS_HCONTEXT	hContext;
	TSS_HTPM	hTPM;
	TSS_HKEY	hSRK, hAuthKey, hNoAuthKey;
	TSS_HPOLICY	hAuthKeyPolicy, hAuthEncDataPolicy;
	BYTE		*rgbPcrValue;
	UINT32		ulPcrLen;
	TSS_HENCDATA	hNoAuthEncData, hAuthEncData;
	BYTE		*prgbDataToUnseal;
	TSS_HPCRS	hPcrs, hNullPcrs = 0;
	UINT32		BlobLength;
	TSS_RESULT	result;
	TSS_FLAG	initFlags = TSS_KEY_TYPE_STORAGE | TSS_KEY_SIZE_2048  |
				    TSS_KEY_VOLATILE | TSS_KEY_NOT_MIGRATABLE;

	print_begin_test( function );

	result = connect_load_all(&hContext, &hSRK, &hTPM);
	if ( result != TSS_SUCCESS )
	{
		print_error( "connect_load_srk", result );
		exit( result );
	}

	result = Tspi_Context_CreateObject(hContext,
					   TSS_OBJECT_TYPE_RSAKEY,
					   initFlags | TSS_KEY_NO_AUTHORIZATION,
					   &hNoAuthKey );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject", result );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Context_CreateObject(hContext,
					   TSS_OBJECT_TYPE_RSAKEY,
					   initFlags | TSS_KEY_AUTHORIZATION,
					   &hAuthKey );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject", result );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Context_CreateObject(hContext,
					   TSS_OBJECT_TYPE_ENCDATA,
					   TSS_ENCDATA_SEAL, &hNoAuthEncData );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject", result );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Context_CreateObject(hContext,
					   TSS_OBJECT_TYPE_ENCDATA,
					   TSS_ENCDATA_SEAL, &hAuthEncData );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject", result );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = set_secret(hContext, hAuthKey, NULL);
	if ( result != TSS_SUCCESS )
	{
		print_error( "set_secret", result );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = set_secret(hContext, hAuthEncData, NULL);
	if ( result != TSS_SUCCESS )
	{
		print_error( "set_secret", result );
		Tspi_Context_Close( hContext );
		exit( result );
	}

		// Create the auth key
	result = Tspi_Key_CreateKey( hAuthKey, hSRK, 0 );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Key_CreateKey", result );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Key_LoadKey( hAuthKey, hSRK );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Key_LoadKey", result );
		Tspi_Context_Close( hContext );
		exit( result );
	}

		// Create the noauth key
	result = Tspi_Key_CreateKey( hNoAuthKey, hSRK, 0 );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Key_CreateKey", result );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Key_LoadKey( hNoAuthKey, hSRK );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Key_LoadKey", result );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_PCRS,
					   0, &hPcrs );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject", result );
		Tspi_Context_Close( hContext );
		exit( result );
	}

#define PCR_NUM	5

	result = Tspi_TPM_PcrRead( hTPM, PCR_NUM, &ulPcrLen, &rgbPcrValue );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_TPM_PcrRead", result );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_PcrComposite_SetPcrValue( hPcrs, PCR_NUM, ulPcrLen,
							rgbPcrValue );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_PcrComposite_SetPcrValue", result );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	/* Now the setup is done, run through several different tests:
	 *
	 *   Test#	Key Auth?	EncData Auth?	Pcr Value Set?
	 *
	 *     1
	 *     2					X
	 *     3			X
	 *     4			X		X
	 *     5	X
	 *     6	X				X
	 *     7	X		X
	 *     8	X		X		X
	 */

	/* test 1 */
	result = seal_and_unseal(hContext, hNoAuthKey, hNoAuthEncData, hNullPcrs);
	if ( result != TSS_SUCCESS )
	{
		print_error("Test 1", result );
		print_error( function, result );
		Tspi_Context_Close( hContext );
		exit( result );
	}
	print_success( "Test 1", result );

	/* test 2 */
	result = seal_and_unseal(hContext, hNoAuthKey, hNoAuthEncData, hPcrs);
	if ( result != TSS_SUCCESS )
	{
		print_error("Test 2", result );
		print_error( function, result );
		Tspi_Context_Close( hContext );
		exit( result );
	}
	print_success( "Test 2", result );

	/* test 3 */
	result = seal_and_unseal(hContext, hNoAuthKey, hAuthEncData, hNullPcrs);
	if ( result != TSS_SUCCESS )
	{
		print_error("Test 3", result );
		print_error( function, result );
		Tspi_Context_Close( hContext );
		exit( result );
	}
	print_success( "Test 3", result );

	/* test 4 */
	result = seal_and_unseal(hContext, hNoAuthKey, hAuthEncData, hPcrs);
	if ( result != TSS_SUCCESS )
	{
		print_error("Test 4", result );
		print_error( function, result );
		Tspi_Context_Close( hContext );
		exit( result );
	}
	print_success( "Test 4", result );

	/* test 5 */
	result = seal_and_unseal(hContext, hAuthKey, hNoAuthEncData, hNullPcrs);
	if ( result != TSS_SUCCESS )
	{
		print_error("Test 5", result );
		print_error( function, result );
		Tspi_Context_Close( hContext );
		exit( result );
	}
	print_success( "Test 5", result );

	/* test 6 */
	result = seal_and_unseal(hContext, hAuthKey, hNoAuthEncData, hPcrs);
	if ( result != TSS_SUCCESS )
	{
		print_error("Test 6", result );
		print_error( function, result );
		Tspi_Context_Close( hContext );
		exit( result );
	}
	print_success( "Test 6", result );

	/* test 7 */
	result = seal_and_unseal(hContext, hAuthKey, hAuthEncData, hNullPcrs);
	if ( result != TSS_SUCCESS )
	{
		print_error("Test 7", result );
		print_error( function, result );
		Tspi_Context_Close( hContext );
		exit( result );
	}
	print_success( "Test 7", result );

	/* test 8 */
	result = seal_and_unseal(hContext, hAuthKey, hAuthEncData, hPcrs);
	if ( result != TSS_SUCCESS )
	{
		print_error("Test 8", result );
		print_error( function, result );
		Tspi_Context_Close( hContext );
		exit( result );
	}
	print_success( "Test 8", result );

	print_success( function, result );
	print_end_test( function );
	Tspi_Context_Close( hContext );
	exit( result );
}
int
main_v1_2(char version)
{
	char		*function = "Tspi_Data_Seal-trans02";
	TSS_HCONTEXT	hContext;
	TSS_HKEY	hSRK;
	TSS_HKEY	hKey, hSigningKey, hWrappingKey;
	TSS_HPOLICY	hSrkPolicy;
	BYTE		*rgbDataToSeal = "This is a test.  1 2 3.";
	BYTE		rgbPcrValue[20];
	TSS_HENCDATA	hEncData;
	TSS_HPCRS	hPcrComposite;
	UINT32		BlobLength;
	UINT32		ulDataLength = strlen(rgbDataToSeal);
	TSS_UUID	uuid;
	TSS_RESULT	result;
	TSS_HTPM	hTPM;

	TSS_FLAG	initFlags = TSS_KEY_TYPE_SIGNING | TSS_KEY_SIZE_2048  |
				TSS_KEY_VOLATILE | TSS_KEY_NO_AUTHORIZATION |
				TSS_KEY_NOT_MIGRATABLE;

	print_begin_test( function );

	memset(rgbPcrValue, 0x5a, sizeof(rgbPcrValue));

	result = connect_load_all(&hContext, &hSRK, &hTPM);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Create ", result);
		exit(result);
	}

	result = Testsuite_Transport_Init(hContext, hSRK, hTPM, TRUE, TRUE, &hWrappingKey,
					  &hSigningKey);
	if (result != TSS_SUCCESS) {
		print_error("Testsuite_Transport_Init", result);
		Tspi_Context_Close(hContext);
		exit(result);
	}

		// create hKey
	result = Tspi_Context_CreateObject( hContext,
						TSS_OBJECT_TYPE_RSAKEY,
						initFlags, &hKey );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (hKey)", result );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Context_CreateObject( hContext,
						TSS_OBJECT_TYPE_ENCDATA,
						TSS_ENCDATA_SEAL, &hEncData );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (hEncData)", result );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_Context_CreateObject( hContext, TSS_OBJECT_TYPE_PCRS,
					0, &hPcrComposite );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (hPcrComposite)",
				result );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_PcrComposite_SetPcrValue( hPcrComposite, 8, 20, rgbPcrValue );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_PcrComposite_SetPcrValue", result );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}
	result = Tspi_PcrComposite_SetPcrValue( hPcrComposite, 9, 20, rgbPcrValue );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_PcrComposite_SetPcrValue", result );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

		// Data Seal
	result = Tspi_Data_Seal( hEncData, hSRK, ulDataLength, rgbDataToSeal, hPcrComposite );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Data_Seal", result );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Testsuite_Transport_Final(hContext, hSigningKey);
	if ( result != TSS_SUCCESS )
	{
		if( !(checkNonAPI(result)) )
		{
			print_error( function, result );
		}
		else
		{
			print_error_nonapi( function, result );
		}
	}
	else
	{
		print_success( function, result );
	}

	print_end_test( function );
	Tspi_Context_FreeMemory( hContext, NULL );
	Tspi_Context_Close( hContext );
	exit(result);
}
int
main_v1_1( void )
{
	char		*function = "Tspi_PcrComposite_SetPcrValue03";
	TSS_HCONTEXT	hContext;
	TSS_HPCRS	hPcrComposite;
	BYTE		rgbPcrValue[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	TSS_RESULT	result;

	print_begin_test( function );

		// Create Context
	result = Tspi_Context_Create( &hContext );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Create", result );
		print_error_exit( function, err_string(result) );
		exit( result );
	}

	result = Tspi_Context_CreateObject( hContext, TSS_OBJECT_TYPE_PCRS,
					0, &hPcrComposite );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (hPcrComposite)",
				result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_PcrComposite_SetPcrValue( hPcrComposite, 5, 0,
							rgbPcrValue );
	if ( TSS_ERROR_CODE(result) != TSS_E_BAD_PARAMETER )
	{
		if( !(checkNonAPI(result)) )
		{
			print_error( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
		else
		{
			print_error_nonapi( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
	}
	else
	{
		print_success( function, result );
	}

	result = Tspi_PcrComposite_SetPcrValue( hPcrComposite, 5, 20,
							NULL );
	if ( TSS_ERROR_CODE(result) != TSS_E_BAD_PARAMETER )
	{
		if( (TSS_ERROR_CODE(result) == TSS_E_INVALID_HANDLE) ||
				(TSS_ERROR_CODE(result) == TSS_E_INTERNAL_ERROR) ||
				(result == TSS_SUCCESS) ||
				(TSS_ERROR_CODE(result) == TSS_E_FAIL) ||
				(TSS_ERROR_CODE(result) == TSS_E_NOTIMPL) ||
				(TSS_ERROR_CODE(result) == TSS_E_PS_KEY_NOTFOUND) ||
				(TSS_ERROR_CODE(result) == TSS_E_KEY_ALREADY_REGISTERED) ||
				(TSS_ERROR_CODE(result) == TSS_E_CANCELED) ||
				(TSS_ERROR_CODE(result) == TSS_E_TIMEOUT) ||
				(TSS_ERROR_CODE(result) == TSS_E_OUTOFMEMORY) ||
				(TSS_ERROR_CODE(result) == TSS_E_TPM_UNEXPECTED) ||
				(TSS_ERROR_CODE(result) == TSS_E_COMM_FAILURE) ||
				(TSS_ERROR_CODE(result) == TSS_E_TPM_UNSUPPORTED_FEATURE) )
		{
			print_error( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
		else
		{
			print_error_nonapi( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
	}
	else
	{
		print_success( function, result );
	}

	result = Tspi_PcrComposite_SetPcrValue( hPcrComposite, 8, 20,
							NULL );
	if ( TSS_ERROR_CODE(result) != TSS_E_BAD_PARAMETER )
	{
		if( (TSS_ERROR_CODE(result) == TSS_E_INVALID_HANDLE) ||
				(TSS_ERROR_CODE(result) == TSS_E_INTERNAL_ERROR) ||
				(result == TSS_SUCCESS) ||
				(TSS_ERROR_CODE(result) == TSS_E_FAIL) ||
				(TSS_ERROR_CODE(result) == TSS_E_NOTIMPL) ||
				(TSS_ERROR_CODE(result) == TSS_E_PS_KEY_NOTFOUND) ||
				(TSS_ERROR_CODE(result) == TSS_E_KEY_ALREADY_REGISTERED) ||
				(TSS_ERROR_CODE(result) == TSS_E_CANCELED) ||
				(TSS_ERROR_CODE(result) == TSS_E_TIMEOUT) ||
				(TSS_ERROR_CODE(result) == TSS_E_OUTOFMEMORY) ||
				(TSS_ERROR_CODE(result) == TSS_E_TPM_UNEXPECTED) ||
				(TSS_ERROR_CODE(result) == TSS_E_COMM_FAILURE) ||
				(TSS_ERROR_CODE(result) == TSS_E_TPM_UNSUPPORTED_FEATURE) )
		{
			print_error( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
		else
		{
			print_error_nonapi( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
	}
	else
	{
		print_success( function, result );
		print_end_test( function );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( 0 );
	}

}
int
main_v1_1( void )
{
	char		*function = "Tspi_PcrComposite_SetPcrValue02";
	TSS_HCONTEXT	hContext;
	TSS_HKEY	hSRK;
	TSS_HPCRS	hPcrComposite;
	TSS_HPCRS	whPcrComposite = -1;
	BYTE		rgbPcrValue[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	TSS_RESULT	result;

	print_begin_test( function );

		// Create Context
	result = Tspi_Context_Create( &hContext );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_Create", result );
		print_error_exit( function, err_string(result) );
		exit( result );
	}

	result = Tspi_Context_CreateObject( hContext, TSS_OBJECT_TYPE_PCRS,
					0, &hPcrComposite );
	if ( result != TSS_SUCCESS )
	{
		print_error( "Tspi_Context_CreateObject (hPcrComposite)",
				result );
		print_error_exit( function, err_string(result) );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( result );
	}

	result = Tspi_PcrComposite_SetPcrValue( whPcrComposite, 8, 20,
						rgbPcrValue );
	if ( TSS_ERROR_CODE(result) != TSS_E_INVALID_HANDLE )
	{
		if( !(checkNonAPI(result)) )
		{
			print_error( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
		else
		{
			print_error_nonapi( function, result );
			print_end_test( function );
			Tspi_Context_FreeMemory( hContext, NULL );
			Tspi_Context_Close( hContext );
			exit(result);
		}
	}
	else
	{
		print_success( function, result );
		print_end_test( function );
		Tspi_Context_FreeMemory( hContext, NULL );
		Tspi_Context_Close( hContext );
		exit( 0 );
	}
}
예제 #10
0
int main_v1_2(char version)
{
	char *function = "Tspi_Data_Unseal01";
	TSS_HCONTEXT hContext;
	TSS_HKEY hSRK, hKey;
	TSS_HTPM hTPM;
	TSS_HPOLICY hKeyPolicy, hEncUsagePolicy, hSRKPolicy;
	BYTE rgbDataToSeal[DATA_SIZE];
	BYTE *rgbPcrValue;
	UINT32 ulPcrLen;
	TSS_HENCDATA hEncData;
	BYTE *prgbDataToUnseal;
	TSS_HPCRS hPcrComposite;
	UINT32 BlobLength;
	UINT32 ulDataLength = DATA_SIZE, ulNewDataLength;
	TSS_UUID uuid;
	TSS_RESULT result;
	TSS_FLAG initFlags = TSS_KEY_TYPE_STORAGE | TSS_KEY_SIZE_2048 |
	    TSS_KEY_VOLATILE | TSS_KEY_AUTHORIZATION |
	    TSS_KEY_NOT_MIGRATABLE;

	print_begin_test(function);

	memset(rgbDataToSeal, 0x5d, DATA_SIZE);

	// Create Context
	result = Tspi_Context_Create(&hContext);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Create", result);
		exit(result);
	}
	// Connect to Context
	result = Tspi_Context_Connect(hContext, get_server(GLOBALSERVER));
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_Connect", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	// create hKey
	result = Tspi_Context_CreateObject(hContext,
					   TSS_OBJECT_TYPE_RSAKEY,
					   initFlags, &hKey);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_CreateObject (hKey)", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	result = Tspi_Context_CreateObject(hContext,
					   TSS_OBJECT_TYPE_ENCDATA,
					   TSS_ENCDATA_SEAL, &hEncData);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_CreateObject (hEncData)",
			    result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	// get the use policy for the encrypted data to set its secret
	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_POLICY, TSS_POLICY_USAGE,
					   &hEncUsagePolicy);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_CreateObject", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	result = Tspi_Policy_SetSecret(hEncUsagePolicy, TESTSUITE_ENCDATA_SECRET_MODE,
				       TESTSUITE_ENCDATA_SECRET_LEN, TESTSUITE_ENCDATA_SECRET);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Policy_SetSecret (hEncUsagePolicy)", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	// Assign the use policy to the object
	result = Tspi_Policy_AssignToObject(hEncUsagePolicy, hEncData);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Policy_AssignToObject", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	//Load Key By UUID
	result = Tspi_Context_LoadKeyByUUID(hContext, TSS_PS_TYPE_SYSTEM,
					    SRK_UUID, &hSRK);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_LoadKeyByUUID (hSRK)", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
#ifndef TESTSUITE_NOAUTH_SRK
	// set the SRK auth data
	result = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &hSRKPolicy);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_GetPolicyObject (hSRK)", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	result =
	    Tspi_Policy_SetSecret(hSRKPolicy, TESTSUITE_SRK_SECRET_MODE,
				  TESTSUITE_SRK_SECRET_LEN,
				  TESTSUITE_SRK_SECRET);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Policy_SetSecret (hSRKPolicy)", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
#endif
	// set the new key's authdata
	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_POLICY, TSS_POLICY_USAGE,
					   &hKeyPolicy);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_CreateObject", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	result =
	    Tspi_Policy_SetSecret(hKeyPolicy, TESTSUITE_KEY_SECRET_MODE, TESTSUITE_KEY_SECRET_LEN,
				  TESTSUITE_KEY_SECRET);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Policy_SetSecret (hKeyPolicy)", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	// Assign the use policy to the object
	result = Tspi_Policy_AssignToObject(hKeyPolicy, hKey);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Policy_AssignToObject", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
	// create the key
	result = Tspi_Key_CreateKey(hKey, hSRK, 0);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Key_CreateKey (hKey)", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	result = Tspi_Key_LoadKey(hKey, hSRK);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Key_LoadKey (hKey)", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_PCRS,
					   TSS_PCRS_STRUCT_INFO_LONG, &hPcrComposite);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_CreateObject (hPcrComposite)",
			    result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	result = Tspi_Context_GetTpmObject(hContext, &hTPM);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_GetTpmObject", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}
#define PCR_NUM	5

	result = Tspi_TPM_PcrRead(hTPM, PCR_NUM, &ulPcrLen, &rgbPcrValue);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_TPM_PcrRead", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	result =
	    Tspi_PcrComposite_SetPcrValue(hPcrComposite, PCR_NUM, ulPcrLen,
					  rgbPcrValue);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_PcrComposite_SetPcrValue", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	result = Tspi_Context_FreeMemory(hContext, rgbPcrValue);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Context_FreeMemory", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	result = Tspi_PcrComposite_SetPcrLocality(hPcrComposite, TPM_LOC_ZERO);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_PcrComposite_SetPcrValue", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	// Tell the TSS to mask the data before sending
	result = Tspi_SetAttribUint32(hEncData, TSS_TSPATTRIB_ENCDATA_SEAL,
				      TSS_TSPATTRIB_ENCDATASEAL_PROTECT_MODE,
				      TSS_TSPATTRIB_ENCDATA_SEAL_PROTECT);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_SetAttribUint32", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	// Data Seal
	result =
	    Tspi_Data_Seal(hEncData, hKey, ulDataLength, rgbDataToSeal, hPcrComposite);
	if (result != TSS_SUCCESS) {
		print_error("Tspi_Data_Seal", result);
		Tspi_Context_FreeMemory(hContext, NULL);
		Tspi_Context_Close(hContext);
		exit(result);
	}

	result = Tspi_Data_Unseal(hEncData, hKey, &ulNewDataLength, &prgbDataToUnseal);
	if (result != TSS_SUCCESS) {
		if (!(checkNonAPI(result))) {
			print_error(function, result);
		} else {
			print_error_nonapi(function, result);
		}
	} else {
		if (ulDataLength != ulNewDataLength) {
			printf
			    ("ERROR length of returned data doesn't match!\n");
			print_end_test(function);
			Tspi_Context_FreeMemory(hContext, NULL);
			Tspi_Context_Close(hContext);
			exit(result);
		} else
		    if (memcmp
			(prgbDataToUnseal, rgbDataToSeal, ulDataLength)) {
			printf
			    ("ERROR data returned from unseal doesn't match!\n");
			print_end_test(function);
			Tspi_Context_FreeMemory(hContext, NULL);
			Tspi_Context_Close(hContext);
			exit(result);
		}

		result =
		    Tspi_Context_FreeMemory(hContext, prgbDataToUnseal);
		if (result != TSS_SUCCESS) {
			print_error("Tspi_Context_FreeMemory ", result);
			Tspi_Context_Close(hContext);
			exit(result);
		}
		print_success(function, result);
	}

	print_end_test(function);
	Tspi_Context_FreeMemory(hContext, NULL);
	Tspi_Context_Close(hContext);
	exit(result);
}