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 <stdio.h>
14 #include <string.h>
15 
16 #include "trousers/tss.h"
17 #include "trousers_types.h"
18 #include "tcs_tsp.h"
19 #include "tcs_utils.h"
20 #include "tcs_int_literals.h"
21 #include "capabilities.h"
22 #include "tcslog.h"
23 #include "tcsps.h"
24 #include "req_mgr.h"
25 
26 
27 TSS_RESULT
TCSP_ReadCurrentTicks_Internal(TCS_CONTEXT_HANDLE hContext,UINT32 * pulCurrentTime,BYTE ** prgbCurrentTime)28 TCSP_ReadCurrentTicks_Internal(TCS_CONTEXT_HANDLE hContext,
29 			       UINT32*            pulCurrentTime,
30 			       BYTE**             prgbCurrentTime)
31 {
32 	TSS_RESULT result;
33 	UINT32 paramSize;
34 	UINT64 offset = 0;
35 	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
36 
37 	if ((result = ctx_verify_context(hContext)))
38 		return result;
39 
40 	if ((result = tpm_rqu_build(TPM_ORD_GetTicks, &offset, txBlob, NULL)))
41 		return result;
42 
43 	if ((result = req_mgr_submit_req(txBlob)))
44 		goto done;
45 
46 	result = UnloadBlob_Header(txBlob, &paramSize);
47 	if (!result)
48 		result = tpm_rsp_parse(TPM_ORD_GetTicks, txBlob, paramSize, pulCurrentTime,
49 				       prgbCurrentTime, NULL);
50 
51 done:
52 	return result;
53 }
54 
55 TSS_RESULT
TCSP_TickStampBlob_Internal(TCS_CONTEXT_HANDLE hContext,TCS_KEY_HANDLE hKey,TPM_NONCE * antiReplay,TPM_DIGEST * digestToStamp,TPM_AUTH * privAuth,UINT32 * pulSignatureLength,BYTE ** prgbSignature,UINT32 * pulTickCountLength,BYTE ** prgbTickCount)56 TCSP_TickStampBlob_Internal(TCS_CONTEXT_HANDLE hContext,
57 			    TCS_KEY_HANDLE     hKey,
58 			    TPM_NONCE*         antiReplay,
59 			    TPM_DIGEST*        digestToStamp,
60 			    TPM_AUTH*          privAuth,
61 			    UINT32*            pulSignatureLength,
62 			    BYTE**             prgbSignature,
63 			    UINT32*            pulTickCountLength,
64 			    BYTE**             prgbTickCount)
65 {
66 	TSS_RESULT result;
67 	UINT32 paramSize;
68 	UINT64 offset = 0;
69 	TPM_KEY_HANDLE keySlot;
70 	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
71 
72 	if ((result = ctx_verify_context(hContext)))
73 		return result;
74 
75 	if (privAuth) {
76 		if ((result = auth_mgr_check(hContext, &privAuth->AuthHandle)))
77 			goto done;
78 	}
79 
80 	if ((result = ensureKeyIsLoaded(hContext, hKey, &keySlot)))
81 		goto done;
82 
83 	if ((result = tpm_rqu_build(TPM_ORD_TickStampBlob, &offset, txBlob, keySlot, antiReplay,
84 				    digestToStamp, privAuth)))
85 		return result;
86 
87 	if ((result = req_mgr_submit_req(txBlob)))
88 		goto done;
89 
90 	if ((result = UnloadBlob_Header(txBlob, &paramSize))) {
91 		LogDebugFn("UnloadBlob_Header failed: rc=0x%x", result);
92 		goto done;
93 	}
94 
95 	if (!result) {
96 		result = tpm_rsp_parse(TPM_ORD_TickStampBlob, txBlob, paramSize, pulTickCountLength,
97 				       prgbTickCount, pulSignatureLength, prgbSignature, privAuth);
98 	}
99 done:
100 	return result;
101 }
102 
103 void
UnloadBlob_CURRENT_TICKS(UINT64 * offset,BYTE * b,TPM_CURRENT_TICKS * t)104 UnloadBlob_CURRENT_TICKS(UINT64 *offset, BYTE *b, TPM_CURRENT_TICKS *t)
105 {
106 	if (!t) {
107 		UnloadBlob_UINT16(offset, NULL, b);
108 		UnloadBlob_UINT64(offset, NULL, b);
109 		UnloadBlob_UINT16(offset, NULL, b);
110 		UnloadBlob(offset, sizeof(TPM_NONCE), b, NULL);
111 
112 		return;
113 	}
114 
115 	UnloadBlob_UINT16(offset, &t->tag, b);
116 	UnloadBlob_UINT64(offset, &t->currentTicks, b);
117 	UnloadBlob_UINT16(offset, &t->tickRate, b);
118 	UnloadBlob(offset, sizeof(TPM_NONCE), b, (BYTE *)&t->tickNonce);
119 }
120