• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stefanberger / libtpms / #2040

14 Dec 2025 07:31PM UTC coverage: 77.163% (-0.01%) from 77.176%
#2040

push

travis-ci

web-flow
Merge 7788dcce7 into 4f71e9b45

1062 of 1261 new or added lines in 87 files covered. (84.22%)

1813 existing lines in 64 files now uncovered.

36347 of 47104 relevant lines covered (77.16%)

126364.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.83
/src/tpm2/ObjectCommands.c
1
/********************************************************************************/
2
/*                                                                                */
3
/*                             Object Commands                                        */
4
/*                             Written by Ken Goldman                                */
5
/*                       IBM Thomas J. Watson Research Center                        */
6
/*                                                                                */
7
/*  Licenses and Notices                                                        */
8
/*                                                                                */
9
/*  1. Copyright Licenses:                                                        */
10
/*                                                                                */
11
/*  - Trusted Computing Group (TCG) grants to the user of the source code in        */
12
/*    this specification (the "Source Code") a worldwide, irrevocable,                 */
13
/*    nonexclusive, royalty free, copyright license to reproduce, create         */
14
/*    derivative works, distribute, display and perform the Source Code and        */
15
/*    derivative works thereof, and to grant others the rights granted herein.        */
16
/*                                                                                */
17
/*  - The TCG grants to the user of the other parts of the specification         */
18
/*    (other than the Source Code) the rights to reproduce, distribute,         */
19
/*    display, and perform the specification solely for the purpose of                 */
20
/*    developing products based on such documents.                                */
21
/*                                                                                */
22
/*  2. Source Code Distribution Conditions:                                        */
23
/*                                                                                */
24
/*  - Redistributions of Source Code must retain the above copyright licenses,         */
25
/*    this list of conditions and the following disclaimers.                        */
26
/*                                                                                */
27
/*  - Redistributions in binary form must reproduce the above copyright         */
28
/*    licenses, this list of conditions        and the following disclaimers in the         */
29
/*    documentation and/or other materials provided with the distribution.        */
30
/*                                                                                */
31
/*  3. Disclaimers:                                                                */
32
/*                                                                                */
33
/*  - THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF        */
34
/*  LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITH        */
35
/*  RESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES)        */
36
/*  THAT MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE.                */
37
/*  Contact TCG Administration (admin@trustedcomputinggroup.org) for                 */
38
/*  information on specification licensing rights available through TCG         */
39
/*  membership agreements.                                                        */
40
/*                                                                                */
41
/*  - THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED         */
42
/*    WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR         */
43
/*    FITNESS FOR A PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR                 */
44
/*    NONINFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY                 */
45
/*    OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE.                */
46
/*                                                                                */
47
/*  - Without limitation, TCG and its members and licensors disclaim all         */
48
/*    liability, including liability for infringement of any proprietary         */
49
/*    rights, relating to use of information in this specification and to the        */
50
/*    implementation of this specification, and TCG disclaims all liability for        */
51
/*    cost of procurement of substitute goods or services, lost profits, loss         */
52
/*    of use, loss of data or any incidental, consequential, direct, indirect,         */
53
/*    or special damages, whether under contract, tort, warranty or otherwise,         */
54
/*    arising in any way out of use or reliance upon this specification or any         */
55
/*    information herein.                                                        */
56
/*                                                                                */
57
/*  (c) Copyright IBM Corp. and others, 2016 - 2024                                */
58
/*                                                                                */
59
/********************************************************************************/
60

61
#include "Tpm.h"
62
#include "Load_fp.h"
63
#if CC_Load  // Conditional expansion of this file
64
#include "Object_spt_fp.h"
65

66
/*(See part 3 specification)
67
// Load an ordinary or temporary object
68
*/
69
//  Return Type: TPM_RC
70
//      TPM_RC_ATTRIBUTES       'inPulblic' attributes are not allowed with selected
71
//                              parent
72
//      TPM_RC_BINDING          'inPrivate' and 'inPublic' are not
73
//                              cryptographically bound
74
//      TPM_RC_HASH             incorrect hash selection for signing key or
75
//                              the 'nameAlg' for 'inPublic' is not valid
76
//      TPM_RC_INTEGRITY        HMAC on 'inPrivate' was not valid
77
//      TPM_RC_KDF              KDF selection not allowed
78
//      TPM_RC_KEY              the size of the object's 'unique' field is not
79
//                              consistent with the indicated size in the object's
80
//                              parameters
81
//      TPM_RC_OBJECT_MEMORY    no available object slot
82
//      TPM_RC_SCHEME           the signing scheme is not valid for the key
83
//      TPM_RC_SENSITIVE        the 'inPrivate' did not unmarshal correctly
84
//      TPM_RC_SIZE             'inPrivate' missing, or 'authPolicy' size for
85
//                              'inPublic' or is not valid
86
//      TPM_RC_SYMMETRIC        symmetric algorithm not provided when required
87
//      TPM_RC_TYPE             'parentHandle' is not a storage key, or the object
88
//                              to load is a storage key but its parameters do not
89
//                              match the parameters of the parent.
90
//      TPM_RC_VALUE            decryption failure
91
TPM_RC
92
TPM2_Load(
407✔
93
          Load_In         *in,            // IN: input parameter list
94
          Load_Out        *out            // OUT: output parameter list
95
          )
96
{
97
    TPM_RC                   result = TPM_RC_SUCCESS;
407✔
98
    TPMT_SENSITIVE           sensitive = {0}; // libtpms changed (valgrind)
407✔
99
    OBJECT                  *parentObject;
407✔
100
    OBJECT                  *newObject;
407✔
101
    // Input Validation
102
    // Don't get invested in loading if there is no place to put it.
103
    newObject = FindEmptyObjectSlot(&out->objectHandle);
407✔
104
    if(newObject == NULL)
407✔
105
        return TPM_RC_OBJECT_MEMORY;
106
    if(in->inPrivate.t.size == 0)
407✔
107
        return TPM_RCS_SIZE + RC_Load_inPrivate;
108
    parentObject = HandleToObject(in->parentHandle);
407✔
109
    pAssert_RC(parentObject != NULL);
407✔
110
    // Is the object that is being used as the parent actually a parent.
111
    if(!ObjectIsParent(parentObject))
407✔
112
        return TPM_RCS_TYPE + RC_Load_parentHandle;
113
    // Compute the name of object. If there isn't one, it is because the nameAlg is
114
    // not valid.
115
    PublicMarshalAndComputeName(&in->inPublic.publicArea, &out->name);
407✔
116
    if(out->name.t.size == 0)
407✔
117
        return TPM_RCS_HASH + RC_Load_inPublic;
118
    // Retrieve sensitive data.
119
    result = PrivateToSensitive(&in->inPrivate.b, &out->name.b, parentObject,
814✔
120
                                in->inPublic.publicArea.nameAlg,
407✔
121
                                &sensitive);
122
    if(result != TPM_RC_SUCCESS)
407✔
123
        return RcSafeAddToResult(result, RC_Load_inPrivate);
3✔
124
    // Internal Data Update
125
    // Load and validate object
126
    result = ObjectLoad(newObject, parentObject,
404✔
127
                        &in->inPublic.publicArea, &sensitive,
128
                        RC_Load_inPublic, RC_Load_inPrivate,
129
                        &out->name);
130
    if(result == TPM_RC_SUCCESS)
404✔
131
        {
132
            // Set the common OBJECT attributes for a loaded object.
133
            ObjectSetLoadedAttributes(newObject, in->parentHandle,
404✔
134
                                      parentObject->seedCompatLevel); // libtpms added
404✔
135
        }
136
    return result;
137
}
138
#endif // CC_Load
139
#include "Tpm.h"
140
#include "LoadExternal_fp.h"
141
#if CC_LoadExternal  // Conditional expansion of this file
142
#include "Object_spt_fp.h"
143
TPM_RC
144
TPM2_LoadExternal(
550✔
145
                  LoadExternal_In     *in,            // IN: input parameter list
146
                  LoadExternal_Out    *out            // OUT: output parameter list
147
                  )
148
{
149
    TPM_RC               result;
550✔
150
    OBJECT              *object;
550✔
151
    TPMT_SENSITIVE      *sensitive = NULL;
550✔
152
    // Input Validation
153
    // Don't get invested in loading if there is no place to put it.
154
    object = FindEmptyObjectSlot(&out->objectHandle);
550✔
155
    if(object == NULL)
550✔
156
        return TPM_RC_OBJECT_MEMORY;
157
    // If the hierarchy to be associated with this object is turned off, the object
158
    // cannot be loaded.
159
    if(!HierarchyIsEnabled(in->hierarchy))
550✔
160
        return TPM_RCS_HIERARCHY + RC_LoadExternal_hierarchy;
161
    // For loading an object with both public and sensitive
162
    if(in->inPrivate.size != 0)
550✔
163
        {
164
            // An external object with a sensitive area can only be loaded in the
165
            // NULL hierarchy
166
            if(in->hierarchy != TPM_RH_NULL)
394✔
167
                return TPM_RCS_HIERARCHY + RC_LoadExternal_hierarchy;
168
            // An external object with a sensitive area must have fixedTPM == CLEAR
169
            // fixedParent == CLEAR so that it does not appear to be a key created by
170
            // this TPM.
171
            if(IS_ATTRIBUTE(in->inPublic.publicArea.objectAttributes, TPMA_OBJECT, fixedTPM)
390✔
172
               || IS_ATTRIBUTE(in->inPublic.publicArea.objectAttributes, TPMA_OBJECT,
173
                               fixedParent)
174
               || IS_ATTRIBUTE(in->inPublic.publicArea.objectAttributes, TPMA_OBJECT,
390✔
175
                               restricted))
176
                return TPM_RCS_ATTRIBUTES + RC_LoadExternal_inPublic;
177
            // Have sensitive point to something other than NULL so that object
178
            // initialization will load the sensitive part too
179
            sensitive = &in->inPrivate.sensitiveArea;
386✔
180
        }
181
    // Need the name to initialize the object structure
182
    PublicMarshalAndComputeName(&in->inPublic.publicArea, &out->name);
542✔
183
    // Load and validate key
184
    result = ObjectLoad(object, NULL,
542✔
185
                        &in->inPublic.publicArea, sensitive,
186
                        RC_LoadExternal_inPublic, RC_LoadExternal_inPrivate,
187
                        &out->name);
188
    if(result == TPM_RC_SUCCESS)
542✔
189
        {
190
            object->attributes.external = SET;
150✔
191
            // Set the common OBJECT attributes for a loaded object.
192
            ObjectSetLoadedAttributes(object, in->hierarchy,
150✔
193
                                      // if anything can be derived from an external object,
194
                                      // we make sure it always uses the old algorithm
195
                                      SEED_COMPAT_LEVEL_ORIGINAL); // libtpms added
196
        }
197
    return result;
198
}
199
#endif // CC_LoadExternal
200
#include "Tpm.h"
201
#include "ReadPublic_fp.h"
202
#if CC_ReadPublic  // Conditional expansion of this file
203
TPM_RC
204
TPM2_ReadPublic(
131✔
205
                ReadPublic_In   *in,            // IN: input parameter list
206
                ReadPublic_Out  *out            // OUT: output parameter list
207
                )
208
{
209
    OBJECT                  *object = HandleToObject(in->objectHandle);
131✔
210
    // Input Validation
211
    // Can not read public area of a sequence object
212
    if(ObjectIsSequence(object))
131✔
213
        return TPM_RC_SEQUENCE;
214

215
    // deliberately after ObjectIsSequence in case ObjectInSequence decides a
216
    // null object is a non-fatal error
217
    pAssert_RC(object != NULL);
131✔
218

219
    // Command Output
220
    out->outPublic.publicArea = object->publicArea;
131✔
221
    out->name = object->name;
131✔
222
    out->qualifiedName = object->qualifiedName;
131✔
223
    return TPM_RC_SUCCESS;
131✔
224
}
225
#endif // CC_ReadPublic
226
#include "Tpm.h"
227
#include "ActivateCredential_fp.h"
228
#if CC_ActivateCredential  // Conditional expansion of this file
229
#include "Object_spt_fp.h"
230
TPM_RC
231
TPM2_ActivateCredential(
1✔
232
                        ActivateCredential_In   *in,            // IN: input parameter list
233
                        ActivateCredential_Out  *out            // OUT: output parameter list
234
                        )
235
{
236
    TPM_RC                   result = TPM_RC_SUCCESS;
1✔
237
    OBJECT                  *object;            // decrypt key
1✔
238
    OBJECT                  *activateObject;    // key associated with credential
1✔
239
    TPM2B_DATA               data;          // credential data
1✔
240
    // Input Validation
241
    // Get decrypt key pointer
242
    object = HandleToObject(in->keyHandle);
1✔
243
    // Get certificated object pointer
244
    activateObject = HandleToObject(in->activateHandle);
1✔
245
    // input decrypt key must be an asymmetric, restricted decryption key
246
    if(!CryptIsAsymAlgorithm(object->publicArea.type)
1✔
247
       || !IS_ATTRIBUTE(object->publicArea.objectAttributes, TPMA_OBJECT, decrypt)
248
       || !IS_ATTRIBUTE(object->publicArea.objectAttributes,
1✔
249
                        TPMA_OBJECT, restricted))
250
        return TPM_RCS_TYPE + RC_ActivateCredential_keyHandle;
251
    // Command output
252
    // Decrypt input credential data via asymmetric decryption.  A
253
    // TPM_RC_VALUE, TPM_RC_KEY or unmarshal errors may be returned at this
254
    // point
255
    result = CryptSecretDecrypt(object, NULL, IDENTITY_STRING, &in->secret, &data);
1✔
256
    if(result != TPM_RC_SUCCESS)
1✔
257
        {
UNCOV
258
            if(result == TPM_RC_KEY)
×
259
                return TPM_RC_FAILURE;
UNCOV
260
            return RcSafeAddToResult(result, RC_ActivateCredential_secret);
×
261
        }
262
    // this assertion is deliberately late, after other validation has happened
263
    // soas to not change existing behavior of the function
264
    pAssert_RC(activateObject != NULL);
1✔
265

266
    // Retrieve secret data.  A TPM_RC_INTEGRITY error or unmarshal
267
    // errors may be returned at this point
268
    result = CredentialToSecret(&in->credentialBlob.b,
1✔
269
                                &activateObject->name.b,
270
                                &data.b,
271
                                object,
272
                                &out->certInfo);
273
    if(result != TPM_RC_SUCCESS)
1✔
UNCOV
274
        return RcSafeAddToResult(result, RC_ActivateCredential_credentialBlob);
×
275
    return TPM_RC_SUCCESS;
276
}
277
#endif // CC_ActivateCredential
278
#include "Tpm.h"
279
#include "Unseal_fp.h"
280
#if CC_Unseal  // Conditional expansion of this file
281
TPM_RC
282
TPM2_Unseal(
23✔
283
            Unseal_In           *in,
284
            Unseal_Out          *out
285
            )
286
{
287
    OBJECT                  *object;
23✔
288
    // Input Validation
289
    // Get pointer to loaded object
290
    object = HandleToObject(in->itemHandle);
23✔
291
    pAssert_RC(object != NULL);
23✔
292

293
    // Input handle must be a data object
294
    if(object->publicArea.type != TPM_ALG_KEYEDHASH)
23✔
295
        return TPM_RCS_TYPE + RC_Unseal_itemHandle;
296
    if(IS_ATTRIBUTE(object->publicArea.objectAttributes, TPMA_OBJECT, decrypt)
23✔
297
       || IS_ATTRIBUTE(object->publicArea.objectAttributes, TPMA_OBJECT, sign)
298
       || IS_ATTRIBUTE(object->publicArea.objectAttributes, TPMA_OBJECT, restricted))
23✔
299
        return TPM_RCS_ATTRIBUTES + RC_Unseal_itemHandle;
300
    // Command Output
301
    // Copy data
302
    out->outData = object->sensitive.sensitive.bits;
23✔
303
    return TPM_RC_SUCCESS;
23✔
304
}
305
#endif // CC_Unseal
306

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc