Coverage Report

Created: 2026-12-01 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtpms/src/tpm2/SigningCommands.c
Line
Count
Source
1
/********************************************************************************/
2
/*                    */
3
/*    Signing and Signature Verification          */
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 - 2023       */
58
/*                    */
59
/********************************************************************************/
60
61
#include "Tpm.h"
62
#include "VerifySignature_fp.h"
63
64
#include "Tpm.h"
65
#include "VerifySignature_fp.h"
66
67
#if CC_VerifySignature  // Conditional expansion of this file
68
69
/*(See part 3 specification)
70
// This command uses loaded key to validate an asymmetric signature on a message
71
// with the message digest passed to the TPM.
72
*/
73
//  Return Type: TPM_RC
74
//      TPM_RC_ATTRIBUTES         'keyHandle' does not reference a signing key
75
//      TPM_RC_SIGNATURE          signature is not genuine
76
//      TPM_RC_SCHEME             CryptValidateSignature()
77
//      TPM_RC_HANDLE             the input handle is references an HMAC key but
78
//                                the private portion is not loaded
79
TPM_RC
80
TPM2_VerifySignature(VerifySignature_In*  in,  // IN: input parameter list
81
         VerifySignature_Out* out  // OUT: output parameter list
82
         )
83
0
{
84
0
    TPM_RC            result;
85
0
    OBJECT*           signObject = HandleToObject(in->keyHandle);
86
0
    TPMI_RH_HIERARCHY hierarchy;
87
    // Input Validation
88
    // The object to validate the signature must be a signing key.
89
0
    if(!IS_ATTRIBUTE(signObject->publicArea.objectAttributes, TPMA_OBJECT, sign))
90
0
  return TPM_RCS_ATTRIBUTES + RC_VerifySignature_keyHandle;
91
92
    // Validate Signature.  TPM_RC_SCHEME, TPM_RC_HANDLE or TPM_RC_SIGNATURE
93
    // error may be returned by CryptCVerifySignatrue()
94
0
    result = CryptValidateSignature(in->keyHandle, &in->digest, &in->signature);
95
0
    if(result != TPM_RC_SUCCESS)
96
0
  return RcSafeAddToResult(result, RC_VerifySignature_signature);
97
98
    // Command Output
99
100
0
    hierarchy = GetHierarchy(in->keyHandle);
101
0
    if(hierarchy == TPM_RH_NULL || signObject->publicArea.nameAlg == TPM_ALG_NULL)
102
0
  {
103
      // produce empty ticket if hierarchy is TPM_RH_NULL or nameAlg is
104
      // TPM_ALG_NULL
105
0
      out->validation.tag           = TPM_ST_VERIFIED;
106
0
      out->validation.hierarchy     = TPM_RH_NULL;
107
0
      out->validation.digest.t.size = 0;
108
0
  }
109
0
    else
110
0
  {
111
      // Compute ticket
112
0
      result = TicketComputeVerified(
113
0
             hierarchy, &in->digest, &signObject->name, &out->validation);
114
0
      if(result != TPM_RC_SUCCESS)
115
0
    return result;
116
0
  }
117
118
0
    return TPM_RC_SUCCESS;
119
0
}
120
121
#endif  // CC_VerifySignature
122
123
#include "Tpm.h"
124
#include "Sign_fp.h"
125
126
#if CC_Sign  // Conditional expansion of this file
127
128
#  include "Attest_spt_fp.h"
129
130
/*(See part 3 specification)
131
// sign an externally provided hash using an asymmetric signing key
132
*/
133
//  Return Type: TPM_RC
134
//      TPM_RC_BINDING          The public and private portions of the key are not
135
//                              properly bound.
136
//      TPM_RC_KEY              'signHandle' does not reference a signing key;
137
//      TPM_RC_SCHEME           the scheme is not compatible with sign key type,
138
//                              or input scheme is not compatible with default
139
//                              scheme, or the chosen scheme is not a valid
140
//                              sign scheme, or the scheme hashAlg is not a
141
//                              valid hash algorithm
142
//      TPM_RC_TICKET           'validation' is not a valid ticket
143
//      TPM_RC_VALUE            the value to sign is larger than allowed for the
144
//                              type of 'keyHandle'
145
//      TPM_RC_ATTRIBUTES       the key has the x509sign attribute and can't be
146
//                              used in TPM2_Sign()
147
//      TPM_RC_SIZE             the provided 'digest' does not match the size
148
//                              of the scheme hashAlg digest
149
150
TPM_RC
151
TPM2_Sign(Sign_In*  in,  // IN: input parameter list
152
    Sign_Out* out  // OUT: output parameter list
153
    )
154
0
{
155
0
    TPM_RC            result;
156
0
    TPMT_TK_HASHCHECK ticket;
157
0
    OBJECT*           signObject = HandleToObject(in->keyHandle);
158
    //
159
    // Input Validation
160
0
    if(!IsSigningObject(signObject))
161
0
    {
162
0
  return TPM_RCS_KEY + RC_Sign_keyHandle;
163
0
    }
164
165
    // A key that will be used for x.509 signatures can't be used in TPM2_Sign().
166
0
    if(IS_ATTRIBUTE(signObject->publicArea.objectAttributes, TPMA_OBJECT, x509sign))
167
0
    {
168
0
  return TPM_RCS_ATTRIBUTES + RC_Sign_keyHandle;
169
0
    }
170
171
    // Pick a scheme for signing. If the input signing scheme is not compatible
172
    // with the default scheme or the signing key type, return an error. If a
173
    // valid hash algorithm is not specified, return an error.
174
0
    if(!CryptSelectSignScheme(signObject, &in->inScheme))
175
0
    {
176
0
  return TPM_RCS_SCHEME + RC_Sign_inScheme;
177
0
    }
178
179
    // If validation is provided, or the key is restricted, check the ticket
180
0
    if(in->validation.digest.t.size != 0
181
0
       || IS_ATTRIBUTE(
182
0
           signObject->publicArea.objectAttributes, TPMA_OBJECT, restricted))
183
0
  {
184
      // Compute and compare ticket
185
0
      result = TicketComputeHashCheck(in->validation.hierarchy,
186
0
              in->inScheme.details.any.hashAlg,
187
0
              &in->digest,
188
0
              &ticket);
189
0
      if(result != TPM_RC_SUCCESS)
190
0
    return result;
191
192
0
      if(!MemoryEqual2B(&in->validation.digest.b, &ticket.digest.b))
193
0
    return TPM_RCS_TICKET + RC_Sign_validation;
194
0
  }
195
0
    else
196
  // If we don't have a ticket, at least verify that the provided 'digest'
197
  // is the size of the scheme hashAlg digest.
198
  // NOTE: this does not guarantee that the 'digest' is actually produced using
199
  // the indicated hash algorithm, but at least it might be.
200
0
  {
201
0
      if(in->digest.t.size
202
0
         != CryptHashGetDigestSize(in->inScheme.details.any.hashAlg))
203
0
    return TPM_RCS_SIZE + RC_Sign_digest;
204
0
  }
205
206
    // Command Output
207
    // Sign the hash. A TPM_RC_VALUE or TPM_RC_SCHEME
208
    // error may be returned at this point
209
0
    result = CryptSign(signObject, &in->inScheme, &in->digest, &out->signature);
210
211
0
    return result;
212
0
}
213
214
#endif  // CC_Sign