summaryrefslogtreecommitdiffstats
path: root/src/usr/secureboot/trusted/base/tpmLogMgr.C
blob: d863afc3e974d472804d2c5764ca93261c1d1ca0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/secureboot/trusted/base/tpmLogMgr.C $                 */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,2016                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/**
 * @file TpmLogMgr.C
 *
 * @brief TPM Event log manager
 */

/////////////////////////////////////////////////////////////////
// NOTE: This file is exportable as TSS-Lite for skiboot/PHYP  //
/////////////////////////////////////////////////////////////////

// ----------------------------------------------
// Includes
// ----------------------------------------------
#include <string.h>
#include "tpmLogMgr.H"
#ifdef __HOSTBOOT_MODULE
#include <secureboot/trustedboot_reasoncodes.H>
#include "../trustedbootUtils.H"
#include "../trustedboot.H"
#else
#include "trustedboot_reasoncodes.H"
#include "trustedbootUtils.H"
#include "trustedboot.H"
#include "trustedTypes.H"
#endif

#ifdef __cplusplus
namespace TRUSTEDBOOT
{
#endif

    uint32_t TCG_EfiSpecIdEventStruct_size(TCG_EfiSpecIdEventStruct* val)
    {
        return (sizeof(TCG_EfiSpecIdEventStruct) + val->vendorInfoSize);
    }

    errlHndl_t TpmLogMgr_initialize(TpmLogMgr* val)
    {
        errlHndl_t err = TB_SUCCESS;
        const char vendorInfo[] = "IBM";
        const char eventSignature[] = "Spec ID Event03";
        TCG_EfiSpecIdEventStruct* eventData = NULL;

        TCG_PCR_EVENT eventLogEntry;

        TRACUCOMP( g_trac_trustedboot, ">>initialize()");

        if (NULL == val)
        {
            TRACFCOMP( g_trac_trustedboot,
                       "TPM LOG INIT FAIL");

                /*@
                 * @errortype
                 * @reasoncode     RC_TPMLOGMGR_INIT_FAIL
                 * @severity       ERRL_SEV_UNRECOVERABLE
                 * @moduleid       MOD_TPMLOGMGR_INITIALIZE
                 * @userdata1      0
                 * @userdata2      0
                 * @devdesc        TPM log buffer init failure.
                 */
                err = tpmCreateErrorLog( MOD_TPMLOGMGR_INITIALIZE,
                                         RC_TPMLOGMGR_INIT_FAIL, 0, 0);

        }
        else
        {

            val->logSize = 0;
            mutex_init( &val->logMutex );
            mutex_lock( &val->logMutex );

            // Assign our new event pointer to the start
            val->newEventPtr = val->eventLog;
            memset(val->eventLog, 0, TPMLOG_BUFFER_SIZE);

            eventData = (TCG_EfiSpecIdEventStruct*) eventLogEntry.event;

            // Add the header event log
            // Values here come from the PC ClientSpecificPlatformProfile spec
            eventLogEntry.eventType = EV_NO_ACTION;
            eventLogEntry.pcrIndex = 0;
            eventLogEntry.eventSize = sizeof(TCG_EfiSpecIdEventStruct) +
                sizeof(vendorInfo);

            memcpy(eventData->signature, eventSignature,
                   sizeof(eventSignature));
            eventData->platformClass = htole32(TPM_PLATFORM_SERVER);
            eventData->specVersionMinor = TPM_SPEC_MINOR;
            eventData->specVersionMajor = TPM_SPEC_MAJOR;
            eventData->specErrata = TPM_SPEC_ERRATA;
            eventData->uintnSize = 1;
            eventData->numberOfAlgorithms = htole32(HASH_COUNT);
            eventData->digestSizes[0].algorithmId = htole16(TPM_ALG_SHA256);
            eventData->digestSizes[0].digestSize = htole16(TPM_ALG_SHA256_SIZE);
            eventData->vendorInfoSize = sizeof(vendorInfo);
            memcpy(eventData->vendorInfo, vendorInfo, sizeof(vendorInfo));
            val->newEventPtr = TCG_PCR_EVENT_logMarshal(&eventLogEntry,
                                                        val->newEventPtr);

            // Done, move our pointers
            val->logSize += TCG_PCR_EVENT_marshalSize(&eventLogEntry);

            mutex_unlock( &val->logMutex );

            // Debug display of raw data
            TRACUBIN(g_trac_trustedboot, "tpmInitialize: Header Entry",
                     val->eventLog, val->logSize);

            TRACUCOMP( g_trac_trustedboot,
                       "<<initialize() LS:%d - %s",
                       val->logSize,
                       ((TB_SUCCESS == err) ? "No Error" : "With Error") );
        }
        return err;
    }

    errlHndl_t TpmLogMgr_addEvent(TpmLogMgr* val,
                                  TCG_PCR_EVENT2* logEvent)
    {
        errlHndl_t err = TB_SUCCESS;
        size_t newLogSize = TCG_PCR_EVENT2_marshalSize(logEvent);

        TRACUCOMP( g_trac_trustedboot,
                   ">>tpmAddEvent() PCR:%d Type:%d Size:%d",
                   logEvent->pcrIndex, logEvent->eventType, (int)newLogSize);

        mutex_lock( &val->logMutex );

        do
        {

            // Need to ensure we have room for the new event
            // We have to leave room for the log full event as well
            if (NULL == val->newEventPtr ||
                val->logSize + newLogSize > TPMLOG_BUFFER_SIZE)
            {
                TRACFCOMP( g_trac_trustedboot,
                           "TPM LOG ADD FAIL PNULL(%d) LS(%d) New LS(%d)",
                           (NULL == val->newEventPtr ? 0 : 1),
                           (int)val->logSize, (int)newLogSize);

                /*@
                 * @errortype
                 * @reasoncode     RC_TPMLOGMGR_ADDEVENT_FAIL
                 * @severity       ERRL_SEV_UNRECOVERABLE
                 * @moduleid       MOD_TPMLOGMGR_ADDEVENT
                 * @userdata1      Log buffer NULL
                 * @userdata2[0:31] Current Log Size
                 * @userdata2[32:63] New entry size
                 * @devdesc        TPM log buffer add failure.
                 */
                err = tpmCreateErrorLog( MOD_TPMLOGMGR_ADDEVENT,
                                         RC_TPMLOGMGR_ADDEVENT_FAIL,
                                         (NULL == val->newEventPtr ? 0 : 1),
                                         (uint64_t)val->logSize << 32 |
                                         newLogSize);

                break;
            }


            val->newEventPtr = TCG_PCR_EVENT2_logMarshal(logEvent,
                                                         val->newEventPtr);

            if (NULL == val->newEventPtr)
            {
                TRACFCOMP( g_trac_trustedboot,
                           "TPM LOG MARSHAL Fail");

                /*@
                 * @errortype
                 * @reasoncode     RC_TPMLOGMGR_ADDEVENTMARSH_FAIL
                 * @severity       ERRL_SEV_UNRECOVERABLE
                 * @moduleid       MOD_TPMLOGMGR_ADDEVENT
                 * @userdata1      0
                 * @userdata2      0
                 * @devdesc        log buffer malloc failure.
                 */
                err = tpmCreateErrorLog( MOD_TPMLOGMGR_ADDEVENT,
                                         RC_TPMLOGMGR_ADDEVENTMARSH_FAIL,
                                         0,
                                         0);
                break;
            }

            val->logSize += newLogSize;


        } while ( 0 );

        TRACUCOMP( g_trac_trustedboot,
                   "<<tpmAddEvent() LS:%d - %s",
                   (int)val->logSize,
                   ((TB_SUCCESS == err) ? "No Error" : "With Error") );

        mutex_unlock( &val->logMutex );
        return err;
    }

    uint32_t TpmLogMgr_getLogSize(TpmLogMgr* val)
    {
        return val->logSize;
    }

    void TpmLogMgr_dumpLog(TpmLogMgr* val)
    {

        // Debug display of raw data
        TRACUCOMP(g_trac_trustedboot, "tpmDumpLog Size : %d\n",
                  (int)val->logSize);
        TRACUBIN(g_trac_trustedboot, "tpmDumpLog",
                 val->eventLog, val->logSize);
    }


    const uint8_t* TpmLogMgr_getFirstEvent(TpmLogMgr* val)
    {
        TCG_PCR_EVENT event;
        bool err = false;
        const uint8_t* result = NULL;

        // Header event in the log is always first, we skip over that
        const uint8_t* firstEvent = val->eventLog;
        memset(&event, 0, sizeof(TCG_PCR_EVENT));

        firstEvent = TCG_PCR_EVENT_logUnmarshal(&event, firstEvent,
                                                sizeof(TCG_PCR_EVENT),
                                                &err);
        if (NULL != firstEvent && !err &&
            firstEvent < val->newEventPtr)
        {
            result = firstEvent;
        }

        return result;
    }

    const uint8_t* TpmLogMgr_getNextEvent(TpmLogMgr* val,
                                          const uint8_t* i_handle,
                                          TCG_PCR_EVENT2* i_eventLog,
                                          bool* o_err)
    {
        const uint8_t* l_resultPtr = NULL;
        if (NULL == i_handle)
        {
            *o_err = true;
        }
        else
        {
            memset(i_eventLog, 0, sizeof(TCG_PCR_EVENT2));
            TRACUCOMP( g_trac_trustedboot, "TPM getNextEvent 0x%p", i_handle);
            l_resultPtr = TCG_PCR_EVENT2_logUnmarshal(i_eventLog, i_handle,
                                                      sizeof(TCG_PCR_EVENT2),
                                                      o_err);
            if (NULL == l_resultPtr)
            {
                // An error was detected, ensure o_err is set
                *o_err = true;
            }
            else if (l_resultPtr >= val->newEventPtr)
            {
                l_resultPtr = NULL;
            }
        }

        return l_resultPtr;
    }

    TCG_PCR_EVENT2 TpmLogMgr_genLogEventPcrExtend(TPM_Pcr i_pcr,
                                                  TPM_Alg_Id i_algId,
                                                  const uint8_t* i_digest,
                                                  size_t i_digestSize,
                                                  const char* i_logMsg)
    {
        TCG_PCR_EVENT2 eventLog;

        memset(&eventLog, 0, sizeof(eventLog));
        eventLog.pcrIndex = i_pcr;
        eventLog.eventType = EV_ACTION;

        // Update digest information, we only use 1 entry
        eventLog.digests.count = 1;
        eventLog.digests.digests[0].algorithmId = i_algId;
        memcpy(eventLog.digests.digests[0].digest.bytes,
               i_digest,
               (i_digestSize > sizeof(TPMU_HA) ?
                sizeof(TPMU_HA) : i_digestSize));

        // Event field data
        eventLog.event.eventSize = strlen(i_logMsg);
        memcpy(eventLog.event.event, i_logMsg,
               (strlen(i_logMsg) > MAX_TPM_LOG_MSG ?
                MAX_TPM_LOG_MSG : strlen(i_logMsg)) );

        return eventLog;
    }

#ifdef __cplusplus
} // end TRUSTEDBOOT
#endif
OpenPOWER on IntegriCloud