1 /* Microsoft Reference Implementation for TPM 2.0
2  *
3  *  The copyright in this software is being made available under the BSD License,
4  *  included below. This software may be subject to other third party and
5  *  contributor rights, including patent rights, and no such rights are granted
6  *  under this license.
7  *
8  *  Copyright (c) Microsoft Corporation
9  *
10  *  All rights reserved.
11  *
12  *  BSD License
13  *
14  *  Redistribution and use in source and binary forms, with or without modification,
15  *  are permitted provided that the following conditions are met:
16  *
17  *  Redistributions of source code must retain the above copyright notice, this list
18  *  of conditions and the following disclaimer.
19  *
20  *  Redistributions in binary form must reproduce the above copyright notice, this
21  *  list of conditions and the following disclaimer in the documentation and/or
22  *  other materials provided with the distribution.
23  *
24  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #include "Tpm.h"
36 #include "NV_Read_fp.h"
37 
38 #if CC_NV_Read  // Conditional expansion of this file
39 
40 /*(See part 3 specification)
41 // Read of an NV index
42 */
43 //  Return Type: TPM_RC
44 //      TPM_RC_NV_AUTHORIZATION         the authorization was valid but the
45 //                                      authorizing entity ('authHandle')
46 //                                      is not allowed to read from the Index
47 //                                      referenced by 'nvIndex'
48 //      TPM_RC_NV_LOCKED                the Index referenced by 'nvIndex' is
49 //                                      read locked
50 //      TPM_RC_NV_RANGE                 read range defined by 'size' and 'offset'
51 //                                      is outside the range of the Index referenced
52 //                                      by 'nvIndex'
53 //      TPM_RC_NV_UNINITIALIZED         the Index referenced by 'nvIndex' has
54 //                                      not been initialized (written)
55 //      TPM_RC_VALUE                    the read size is larger than the
56 //                                      MAX_NV_BUFFER_SIZE
57 TPM_RC
TPM2_NV_Read(NV_Read_In * in,NV_Read_Out * out)58 TPM2_NV_Read(
59     NV_Read_In      *in,            // IN: input parameter list
60     NV_Read_Out     *out            // OUT: output parameter list
61     )
62 {
63     NV_REF           locator;
64     NV_INDEX        *nvIndex = NvGetIndexInfo(in->nvIndex, &locator);
65     TPM_RC           result;
66 
67 // Input Validation
68     // Common read access checks. NvReadAccessChecks() may return
69     // TPM_RC_NV_AUTHORIZATION, TPM_RC_NV_LOCKED, or TPM_RC_NV_UNINITIALIZED
70     result = NvReadAccessChecks(in->authHandle, in->nvIndex,
71                                 nvIndex->publicArea.attributes);
72     if(result != TPM_RC_SUCCESS)
73         return result;
74 
75     // Make sure the data will fit the return buffer
76     if(in->size > MAX_NV_BUFFER_SIZE)
77         return TPM_RCS_VALUE + RC_NV_Read_size;
78 
79     // Verify that the offset is not too large
80     if(in->offset > nvIndex->publicArea.dataSize)
81         return TPM_RCS_VALUE + RC_NV_Read_offset;
82 
83     // Make sure that the selection is within the range of the Index
84     if(in->size > (nvIndex->publicArea.dataSize - in->offset))
85         return TPM_RC_NV_RANGE;
86 
87 // Command Output
88     // Set the return size
89     out->data.t.size = in->size;
90 
91     // Perform the read
92     NvGetIndexData(nvIndex, locator, in->offset, in->size, out->data.t.buffer);
93 
94     return TPM_RC_SUCCESS;
95 }
96 
97 #endif // CC_NV_Read