aboutsummaryrefslogtreecommitdiff
path: root/NV_ReadLock.c
blob: fd2107e9c1678fd175b8a667d4b361871c41cfff (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
// This file was extracted from the TCG Published
// Trusted Platform Module Library
// Part 3: Commands
// Family "2.0"
// Level 00 Revision 01.16
// October 30, 2014

#include "InternalRoutines.h"
#include "NV_ReadLock_fp.h"
#include "NV_spt_fp.h"
//
//
//     Error Returns                    Meaning
//
//     TPM_RC_ATTRIBUTES                TPMA_NV_READ_STCLEAR is not SET so Index referenced by
//                                      nvIndex may not be write locked
//     TPM_RC_NV_AUTHORIZATION          the authorization was valid but the authorizing entity (authHandle) is
//                                      not allowed to read from the Index referenced by nvIndex
//
TPM_RC
TPM2_NV_ReadLock(
   NV_ReadLock_In    *in                 // IN: input parameter list
   )
{
   TPM_RC            result;
   NV_INDEX          nvIndex;

   // The command needs NV update. Check if NV is available.
   // A TPM_RC_NV_UNAVAILABLE or TPM_RC_NV_RATE error may be returned at
   // this point
   result = NvIsAvailable();
   if(result != TPM_RC_SUCCESS) return result;

// Input Validation

   // Common read access checks. NvReadAccessChecks() returns
   // TPM_RC_NV_AUTHORIZATION, TPM_RC_NV_LOCKED, or TPM_RC_NV_UNINITIALIZED
   // error may be returned at this point
   result = NvReadAccessChecks(in->authHandle, in->nvIndex);
   if(result != TPM_RC_SUCCESS)
   {
       if(result == TPM_RC_NV_AUTHORIZATION)
           return TPM_RC_NV_AUTHORIZATION;
       // Index is already locked for write
       else if(result == TPM_RC_NV_LOCKED)
           return TPM_RC_SUCCESS;

         // If NvReadAccessChecks return TPM_RC_NV_UNINITALIZED, then continue.
         // It is not an error to read lock an uninitialized Index.
   }

   // Get NV index info
   NvGetIndexInfo(in->nvIndex, &nvIndex);

   // if TPMA_NV_READ_STCLEAR is not set, the index can not be read-locked
   if(nvIndex.publicArea.attributes.TPMA_NV_READ_STCLEAR == CLEAR)
       return TPM_RC_ATTRIBUTES + RC_NV_ReadLock_nvIndex;

// Internal Data Update

   // Set the READLOCK attribute
   nvIndex.publicArea.attributes.TPMA_NV_READLOCKED = SET;
   // Write NV info back
   NvWriteIndexInfo(in->nvIndex, &nvIndex);

   return TPM_RC_SUCCESS;
}