Debian Codegraph @

simulator/ms-tpm-20-ref/TPMCmd/tpm/src/command/Attestation/CertifyX509.c

raw
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 "CertifyX509_fp.h"
37 #include "X509.h"
38 #include "TpmASN1_fp.h"
39 #include "X509_spt_fp.h"
40 #include "Attest_spt_fp.h"
41
42 #if CC_CertifyX509 // Conditional expansion of this file
43
44 /*(See part 3 specification)
45 // Certify
46 */
47 // return type: TPM_RC
48 // TPM_RC_ATTRIBUTES the attributes of 'objectHandle' are not compatible
49 // with the KeyUsage or TPMA_OBJECT values in the
50 // extensions fields
51 // TPM_RC_BINDING the public and private portions of the key are not
52 // properly bound.
53 // TPM_RC_HASH the hash algorithm in the scheme is not supported
54 // TPM_RC_KEY 'signHandle' does not reference a signing key;
55 // TPM_RC_SCHEME the scheme is not compatible with sign key type,
56 // or input scheme is not compatible with default
57 // scheme, or the chosen scheme is not a valid
58 // sign scheme
59 // TPM_RC_VALUE most likely a problem with the format of
60 // 'partialCertificate'
61 TPM_RC
62 TPM2_CertifyX509(
63 CertifyX509_In *in, // IN: input parameter list
64 CertifyX509_Out *out // OUT: output parameter list
65 )
66 {
67 TPM_RC result;
68 OBJECT *signKey = HandleToObject(in->signHandle);
69 OBJECT *object = HandleToObject(in->objectHandle);
70 HASH_STATE hash;
71 INT16 length; // length for a tagged element
72 ASN1UnmarshalContext ctx;
73 ASN1MarshalContext ctxOut;
74 // certTBS holds an array of pointers and lengths. Each entry references the
75 // corresponding value in a TBSCertificate structure. For example, the 1th
76 // element references the version number
77 stringRef certTBS[REF_COUNT] = {{0}};
78 #define ALLOWED_SEQUENCES (SUBJECT_PUBLIC_KEY_REF - SIGNATURE_REF)
79 stringRef partial[ALLOWED_SEQUENCES] = {{0}};
80 INT16 countOfSequences = 0;
81 INT16 i;
82 //
83 #if CERTIFYX509_DEBUG
84 DebugFileOpen();
85 DebugDumpBuffer(in->partialCertificate.t.size, in->partialCertificate.t.buffer,
86 "partialCertificate");
87 #endif
88
89 // Input Validation
90 // signing key must be able to sign
91 if(!IsSigningObject(signKey))
92 return TPM_RCS_KEY + RC_CertifyX509_signHandle;
93 // Pick a scheme for sign. If the input sign scheme is not compatible with
94 // the default scheme, return an error.
95 if(!CryptSelectSignScheme(signKey, &in->inScheme))
96 return TPM_RCS_SCHEME + RC_CertifyX509_inScheme;
97 // Make sure that the public Key encoding is known
98 if(X509AddPublicKey(NULL, object) == 0)
99 return TPM_RCS_ASYMMETRIC + RC_CertifyX509_objectHandle;
100 // Unbundle 'partialCertificate'.
101 // Initialize the unmarshaling context
102 if(!ASN1UnmarshalContextInitialize(&ctx, in->partialCertificate.t.size,
103 in->partialCertificate.t.buffer))
104 return TPM_RCS_VALUE + RC_CertifyX509_partialCertificate;
105 // Make sure that this is a constructed SEQUENCE
106 length = ASN1NextTag(&ctx);
107 // Must be a constructed SEQUENCE that uses all of the input parameter
108 if((ctx.tag != (ASN1_CONSTRUCTED_SEQUENCE))
109 || ((ctx.offset + length) != in->partialCertificate.t.size))
110 return TPM_RCS_SIZE + RC_CertifyX509_partialCertificate;
111
112 // This scans through the contents of the outermost SEQUENCE. This would be the
113 // 'issuer', 'validity', 'subject', 'issuerUniqueID' (optional),
114 // 'subjectUniqueID' (optional), and 'extensions.'
115 while(ctx.offset < ctx.size)
116 {
117 INT16 startOfElement = ctx.offset;
118 //
119 // Read the next tag and length field.
120 length = ASN1NextTag(&ctx);
121 if(length < 0)
122 break;
123 if(ctx.tag == ASN1_CONSTRUCTED_SEQUENCE)
124 {
125 partial[countOfSequences].buf = &ctx.buffer[startOfElement];
126 ctx.offset += length;
127 partial[countOfSequences].len = (INT16)ctx.offset - startOfElement;
128 if(++countOfSequences > ALLOWED_SEQUENCES)
129 break;
130 }
131 else if(ctx.tag == X509_EXTENSIONS)
132 {
133 if(certTBS[EXTENSIONS_REF].len != 0)
134 return TPM_RCS_VALUE + RC_CertifyX509_partialCertificate;
135 certTBS[EXTENSIONS_REF].buf = &ctx.buffer[startOfElement];
136 ctx.offset += length;
137 certTBS[EXTENSIONS_REF].len =
138 (INT16)ctx.offset - startOfElement;
139 }
140 else
141 return TPM_RCS_VALUE + RC_CertifyX509_partialCertificate;
142 }
143 // Make sure that we used all of the data and found at least the required
144 // number of elements.
145 if((ctx.offset != ctx.size) || (countOfSequences < 3)
146 || (countOfSequences > 4)
147 || (certTBS[EXTENSIONS_REF].buf == NULL))
148 return TPM_RCS_VALUE + RC_CertifyX509_partialCertificate;
149 // Now that we know how many sequences there were, we can put them where they
150 // belong
151 for(i = 0; i < countOfSequences; i++)
152 certTBS[SUBJECT_KEY_REF - i] = partial[countOfSequences - 1 - i];
153
154 // If only three SEQUENCES, then the TPM needs to produce the signature algorithm.
155 // See if it can
156 if((countOfSequences == 3) &&
157 (X509AddSigningAlgorithm(NULL, signKey, &in->inScheme) == 0))
158 return TPM_RCS_SCHEME + RC_CertifyX509_signHandle;
159
160 // Process the extensions
161 result = X509ProcessExtensions(object, &certTBS[EXTENSIONS_REF]);
162 if(result != TPM_RC_SUCCESS)
163 // If the extension has the TPMA_OBJECT extension and the attributes don't
164 // match, then the error code will be TPM_RCS_ATTRIBUTES. Otherwise, the error
165 // indicates a malformed partialCertificate.
166 return result + ((result == TPM_RCS_ATTRIBUTES)
167 ? RC_CertifyX509_objectHandle
168 : RC_CertifyX509_partialCertificate);
169 // Command Output
170 // Create the addedToCertificate values
171
172 // Build the addedToCertificate from the bottom up.
173 // Initialize the context structure
174 ASN1InitialializeMarshalContext(&ctxOut, sizeof(out->addedToCertificate.t.buffer),
175 out->addedToCertificate.t.buffer);
176 // Place a marker for the overall context
177 ASN1StartMarshalContext(&ctxOut); // SEQUENCE for addedToCertificate
178
179 // Add the subject public key descriptor
180 certTBS[SUBJECT_PUBLIC_KEY_REF].len = X509AddPublicKey(&ctxOut, object);
181 certTBS[SUBJECT_PUBLIC_KEY_REF].buf = ctxOut.buffer + ctxOut.offset;
182 // If the caller didn't provide the algorithm identifier, create it
183 if(certTBS[SIGNATURE_REF].len == 0)
184 {
185 certTBS[SIGNATURE_REF].len = X509AddSigningAlgorithm(&ctxOut, signKey,
186 &in->inScheme);
187 certTBS[SIGNATURE_REF].buf = ctxOut.buffer + ctxOut.offset;
188 }
189 // Create the serial number value. Use the out->tbsDigest as scratch.
190 {
191 TPM2B *digest = &out->tbsDigest.b;
192 //
193 digest->size = (INT16)CryptHashStart(&hash, signKey->publicArea.nameAlg);
194 pAssert(digest->size != 0);
195
196 // The serial number size is the smaller of the digest and the vendor-defined
197 // value
198 digest->size = MIN(digest->size, SIZE_OF_X509_SERIAL_NUMBER);
199 // Add all the parts of the certificate other than the serial number
200 // and version number
201 for(i = SIGNATURE_REF; i < REF_COUNT; i++)
202 CryptDigestUpdate(&hash, certTBS[i].len, certTBS[i].buf);
203 // throw in the Name of the signing key...
204 CryptDigestUpdate2B(&hash, &signKey->name.b);
205 // ...and the Name of the signed key.
206 CryptDigestUpdate2B(&hash, &object->name.b);
207 // Done
208 CryptHashEnd2B(&hash, digest);
209 }
210
211 // Add the serial number
212 certTBS[SERIAL_NUMBER_REF].len =
213 ASN1PushInteger(&ctxOut, out->tbsDigest.t.size, out->tbsDigest.t.buffer);
214 certTBS[SERIAL_NUMBER_REF].buf = ctxOut.buffer + ctxOut.offset;
215
216 // Add the static version number
217 ASN1StartMarshalContext(&ctxOut);
218 ASN1PushUINT(&ctxOut, 2);
219 certTBS[VERSION_REF].len =
220 ASN1EndEncapsulation(&ctxOut, ASN1_APPLICAIION_SPECIFIC);
221 certTBS[VERSION_REF].buf = ctxOut.buffer + ctxOut.offset;
222
223 // Create a fake tag and length for the TBS in the space used for
224 // 'addedToCertificate'
225 {
226 for(length = 0, i = 0; i < REF_COUNT; i++)
227 length += certTBS[i].len;
228 // Put a fake tag and length into the buffer for use in the tbsDigest
229 certTBS[ENCODED_SIZE_REF].len =
230 ASN1PushTagAndLength(&ctxOut, ASN1_CONSTRUCTED_SEQUENCE, length);
231 certTBS[ENCODED_SIZE_REF].buf = ctxOut.buffer + ctxOut.offset;
232 // Restore the buffer pointer to add back the number of octets used for the
233 // tag and length
234 ctxOut.offset += certTBS[ENCODED_SIZE_REF].len;
235 }
236 // sanity check
237 if(ctxOut.offset < 0)
238 return TPM_RC_FAILURE;
239 // Create the tbsDigest to sign
240 out->tbsDigest.t.size = CryptHashStart(&hash, in->inScheme.details.any.hashAlg);
241 for(i = 0; i < REF_COUNT; i++)
242 CryptDigestUpdate(&hash, certTBS[i].len, certTBS[i].buf);
243 CryptHashEnd2B(&hash, &out->tbsDigest.b);
244
245 #if CERTIFYX509_DEBUG
246 {
247 BYTE fullTBS[4096];
248 BYTE *fill = fullTBS;
249 int j;
250 for (j = 0; j < REF_COUNT; j++)
251 {
252 MemoryCopy(fill, certTBS[j].buf, certTBS[j].len);
253 fill += certTBS[j].len;
254 }
255 DebugDumpBuffer((int)(fill - &fullTBS[0]), fullTBS, "\nfull TBS");
256 }
257 #endif
258
259 // Finish up the processing of addedToCertificate
260 // Create the actual tag and length for the addedToCertificate structure
261 out->addedToCertificate.t.size =
262 ASN1EndEncapsulation(&ctxOut, ASN1_CONSTRUCTED_SEQUENCE);
263 // Now move all the addedToContext to the start of the buffer
264 MemoryCopy(out->addedToCertificate.t.buffer, ctxOut.buffer + ctxOut.offset,
265 out->addedToCertificate.t.size);
266 #if CERTIFYX509_DEBUG
267 DebugDumpBuffer(out->addedToCertificate.t.size, out->addedToCertificate.t.buffer,
268 "\naddedToCertificate");
269 #endif
270 // only thing missing is the signature
271 result = CryptSign(signKey, &in->inScheme, &out->tbsDigest, &out->signature);
272
273 return result;
274 }
275
276 #endif // CC_CertifyX509