| 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 "Attest_spt_fp.h" |
| 37 |
#include "NV_Certify_fp.h" |
| 38 |
|
| 39 |
#if CC_NV_Certify // Conditional expansion of this file |
| 40 |
|
| 41 |
/*(See part 3 specification) |
| 42 |
// certify the contents of an NV index or portion of an NV index |
| 43 |
*/ |
| 44 |
// Return Type: TPM_RC |
| 45 |
// TPM_RC_NV_AUTHORIZATION the authorization was valid but the |
| 46 |
// authorizing entity ('authHandle') |
| 47 |
// is not allowed to read from the Index |
| 48 |
// referenced by 'nvIndex' |
| 49 |
// TPM_RC_KEY 'signHandle' does not reference a signing |
| 50 |
// key |
| 51 |
// TPM_RC_NV_LOCKED Index referenced by 'nvIndex' is locked |
| 52 |
// for reading |
| 53 |
// TPM_RC_NV_RANGE 'offset' plus 'size' extends outside of the |
| 54 |
// data range of the Index referenced by |
| 55 |
// 'nvIndex' |
| 56 |
// TPM_RC_NV_UNINITIALIZED Index referenced by 'nvIndex' has not been |
| 57 |
// written |
| 58 |
// TPM_RC_SCHEME 'inScheme' is not an allowed value for the |
| 59 |
// key definition |
| 60 |
TPM_RC |
| 61 |
TPM2_NV_Certify( |
| 62 |
NV_Certify_In *in, // IN: input parameter list |
| 63 |
NV_Certify_Out *out // OUT: output parameter list |
| 64 |
) |
| 65 |
{ |
| 66 |
TPM_RC result; |
| 67 |
NV_REF locator; |
| 68 |
NV_INDEX *nvIndex = NvGetIndexInfo(in->nvIndex, &locator); |
| 69 |
TPMS_ATTEST certifyInfo; |
| 70 |
OBJECT *signObject = HandleToObject(in->signHandle); |
| 71 |
// Input Validation |
| 72 |
if(!IsSigningObject(signObject)) |
| 73 |
return TPM_RCS_KEY + RC_NV_Certify_signHandle; |
| 74 |
if(!CryptSelectSignScheme(signObject, &in->inScheme)) |
| 75 |
return TPM_RCS_SCHEME + RC_NV_Certify_inScheme; |
| 76 |
|
| 77 |
// Common access checks, NvWriteAccessCheck() may return TPM_RC_NV_AUTHORIZATION |
| 78 |
// or TPM_RC_NV_LOCKED |
| 79 |
result = NvReadAccessChecks(in->authHandle, in->nvIndex, |
| 80 |
nvIndex->publicArea.attributes); |
| 81 |
if(result != TPM_RC_SUCCESS) |
| 82 |
return result; |
| 83 |
|
| 84 |
// make sure that the selection is within the range of the Index (cast to avoid |
| 85 |
// any wrap issues with addition) |
| 86 |
if((UINT32)in->size + (UINT32)in->offset > (UINT32)nvIndex->publicArea.dataSize) |
| 87 |
return TPM_RC_NV_RANGE; |
| 88 |
// Make sure the data will fit the return buffer. |
| 89 |
// NOTE: This check may be modified if the output buffer will not hold the |
| 90 |
// maximum sized NV buffer as part of the certified data. The difference in |
| 91 |
// size could be substantial if the signature scheme was produced a large |
| 92 |
// signature (e.g., RSA 4096). |
| 93 |
if(in->size > MAX_NV_BUFFER_SIZE) |
| 94 |
return TPM_RCS_VALUE + RC_NV_Certify_size; |
| 95 |
|
| 96 |
// Command Output |
| 97 |
|
| 98 |
// Fill in attest information common fields |
| 99 |
FillInAttestInfo(in->signHandle, &in->inScheme, &in->qualifyingData, |
| 100 |
&certifyInfo); |
| 101 |
|
| 102 |
// Get the name of the index |
| 103 |
NvGetIndexName(nvIndex, &certifyInfo.attested.nv.indexName); |
| 104 |
|
| 105 |
// See if this is old format or new format |
| 106 |
if ((in->size != 0) || (in->offset != 0)) |
| 107 |
{ |
| 108 |
// NV certify specific fields |
| 109 |
// Attestation type |
| 110 |
certifyInfo.type = TPM_ST_ATTEST_NV; |
| 111 |
|
| 112 |
// Set the return size |
| 113 |
certifyInfo.attested.nv.nvContents.t.size = in->size; |
| 114 |
|
| 115 |
// Set the offset |
| 116 |
certifyInfo.attested.nv.offset = in->offset; |
| 117 |
|
| 118 |
// Perform the read |
| 119 |
NvGetIndexData(nvIndex, locator, in->offset, in->size, |
| 120 |
certifyInfo.attested.nv.nvContents.t.buffer); |
| 121 |
} |
| 122 |
else |
| 123 |
{ |
| 124 |
HASH_STATE hashState; |
| 125 |
// This is to sign a digest of the data |
| 126 |
certifyInfo.type = TPM_ST_ATTEST_NV_DIGEST; |
| 127 |
// Initialize the hash before calling the function to add the Index data to |
| 128 |
// the hash. |
| 129 |
certifyInfo.attested.nvDigest.nvDigest.t.size = |
| 130 |
CryptHashStart(&hashState, in->inScheme.details.any.hashAlg); |
| 131 |
NvHashIndexData(&hashState, nvIndex, locator, 0, |
| 132 |
nvIndex->publicArea.dataSize); |
| 133 |
CryptHashEnd2B(&hashState, &certifyInfo.attested.nvDigest.nvDigest.b); |
| 134 |
} |
| 135 |
// Sign attestation structure. A NULL signature will be returned if |
| 136 |
// signObject is NULL. |
| 137 |
return SignAttestInfo(signObject, &in->inScheme, &certifyInfo, |
| 138 |
&in->qualifyingData, &out->certifyInfo, &out->signature); |
| 139 |
} |
| 140 |
|
| 141 |
#endif // CC_NV_Certify |