Home | History | Annotate | Line # | Download | only in tspi
      1 
      2 /*
      3  * Licensed Materials - Property of IBM
      4  *
      5  * trousers - An open source TCG Software Stack
      6  *
      7  * (C) Copyright International Business Machines Corp. 2007
      8  *
      9  */
     10 
     11 
     12 #include <stdlib.h>
     13 #include <string.h>
     14 
     15 #include "trousers/tss.h"
     16 #include "trousers/trousers.h"
     17 #include "trousers_types.h"
     18 #include "spi_utils.h"
     19 #include "capabilities.h"
     20 #include "tsplog.h"
     21 #include "obj.h"
     22 
     23 
     24 #ifdef TSS_BUILD_TRANSPORT
     25 TSS_RESULT
     26 Transport_Sign(TSS_HCONTEXT tspContext,    /* in */
     27 	       TCS_KEY_HANDLE keyHandle,   /* in */
     28 	       UINT32 areaToSignSize,      /* in */
     29 	       BYTE * areaToSign,  /* in */
     30 	       TPM_AUTH * privAuth,        /* in, out */
     31 	       UINT32 * sigSize,   /* out */
     32 	       BYTE ** sig)        /* out */
     33 {
     34 	UINT64 offset;
     35 	TSS_RESULT result;
     36 	UINT32 handlesLen, decLen, dataLen;
     37 	TCS_HANDLE *handles, handle;
     38 	TPM_DIGEST pubKeyHash;
     39 	Trspi_HashCtx hashCtx;
     40 	BYTE *dec, *data;
     41 
     42 
     43 	if ((result = obj_context_transport_init(tspContext)))
     44 		return result;
     45 
     46 	LogDebugFn("Executing in a transport session");
     47 
     48 	if ((result = obj_tcskey_get_pubkeyhash(keyHandle, pubKeyHash.digest)))
     49 		return result;
     50 
     51 	result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
     52 	result |= Trspi_Hash_DIGEST(&hashCtx, pubKeyHash.digest);
     53 	if ((result |= Trspi_HashFinal(&hashCtx, pubKeyHash.digest)))
     54 		return result;
     55 
     56 	handlesLen = 1;
     57 	handle = keyHandle;
     58 	handles = &handle;
     59 
     60 	dataLen = sizeof(UINT32) + areaToSignSize;
     61 	if ((data = malloc(dataLen)) == NULL) {
     62 		LogError("malloc of %u bytes failed", dataLen);
     63 		return TSPERR(TSS_E_OUTOFMEMORY);
     64 	}
     65 
     66 	offset = 0;
     67 	Trspi_LoadBlob_UINT32(&offset, areaToSignSize, data);
     68 	Trspi_LoadBlob(&offset, areaToSignSize, data, areaToSign);
     69 
     70 	if ((result = obj_context_transport_execute(tspContext, TPM_ORD_Sign, dataLen, data,
     71 						    &pubKeyHash, &handlesLen, &handles,
     72 						    privAuth, NULL, &decLen, &dec))) {
     73 		free(data);
     74 		return result;
     75 	}
     76 	free(data);
     77 
     78 	offset = 0;
     79 	Trspi_UnloadBlob_UINT32(&offset, sigSize, dec);
     80 
     81 	if ((*sig = malloc(*sigSize)) == NULL) {
     82 		free(dec);
     83 		LogError("malloc of %u bytes failed", *sigSize);
     84 		*sigSize = 0;
     85 		return TSPERR(TSS_E_OUTOFMEMORY);
     86 	}
     87 	Trspi_UnloadBlob(&offset, *sigSize, dec, *sig);
     88 
     89 	return result;
     90 }
     91 #endif
     92 
     93