1 // This file was extracted from the TCG Published
2 // Trusted Platform Module Library
3 // Part 3: Commands
4 // Family "2.0"
5 // Level 00 Revision 01.16
6 // October 30, 2014
7 
8 #include "InternalRoutines.h"
9 #include "Attest_spt_fp.h"
10 #include "GetTime_fp.h"
11 //
12 //
13 //     Error Returns               Meaning
14 //
15 //     TPM_RC_KEY                  key referenced by signHandle is not a signing key
16 //     TPM_RC_SCHEME               inScheme is incompatible with signHandle type; or both scheme and
17 //                                 key's default scheme are empty; or scheme is empty while key's
18 //                                 default scheme requires explicit input scheme (split signing); or non-
19 //                                 empty default key scheme differs from scheme
20 //     TPM_RC_VALUE                digest generated for the given scheme is greater than the modulus of
21 //                                 signHandle (for an RSA key); invalid commit status or failed to
22 //                                 generate r value (for an ECC key)
23 //
24 TPM_RC
TPM2_GetTime(GetTime_In * in,GetTime_Out * out)25 TPM2_GetTime(
26    GetTime_In      *in,             // IN: input parameter list
27    GetTime_Out     *out             // OUT: output parameter list
28    )
29 {
30    TPM_RC                 result;
31    TPMS_ATTEST            timeInfo;
32 
33 // Command Output
34 
35    // Filling in attest information
36    // Common fields
37    result = FillInAttestInfo(in->signHandle,
38                              &in->inScheme,
39                              &in->qualifyingData,
40                              &timeInfo);
41    if(result != TPM_RC_SUCCESS)
42    {
43        if(result == TPM_RC_KEY)
44            return TPM_RC_KEY + RC_GetTime_signHandle;
45        else
46            return RcSafeAddToResult(result, RC_GetTime_inScheme);
47    }
48 
49    // GetClock specific fields
50    // Attestation type
51    timeInfo.type = TPM_ST_ATTEST_TIME;
52 
53    // current clock in plain text
54    timeInfo.attested.time.time.time = g_time;
55    TimeFillInfo(&timeInfo.attested.time.time.clockInfo);
56 
57    // Firmware version in plain text
58    timeInfo.attested.time.firmwareVersion
59        = ((UINT64) gp.firmwareV1) << 32;
60    timeInfo.attested.time.firmwareVersion += gp.firmwareV2;
61 
62    // Sign attestation structure. A NULL signature will be returned if
63    // signHandle is TPM_RH_NULL. A TPM_RC_NV_UNAVAILABLE, TPM_RC_NV_RATE,
64    // TPM_RC_VALUE, TPM_RC_SCHEME or TPM_RC_ATTRIBUTES error may be returned at
65    // this point
66    result = SignAttestInfo(in->signHandle,
67                            &in->inScheme,
68                            &timeInfo,
69                            &in->qualifyingData,
70                            &out->timeInfo,
71                            &out->signature);
72    if(result != TPM_RC_SUCCESS)
73        return result;
74 
75    // orderly state should be cleared because of the reporting of clock info
76    // if signing happens
77    if(in->signHandle != TPM_RH_NULL)
78        g_clearOrderly = TRUE;
79 
80    return TPM_RC_SUCCESS;
81 }
82