| 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 |
//** Includes |
| 36 |
#include "Tpm.h" |
| 37 |
#include "TpmASN1.h" |
| 38 |
#include "TpmASN1_fp.h" |
| 39 |
#define _X509_SPT_ |
| 40 |
#include "X509.h" |
| 41 |
#include "X509_spt_fp.h" |
| 42 |
#if ALG_RSA |
| 43 |
# include "X509_RSA_fp.h" |
| 44 |
#endif // ALG_RSA |
| 45 |
#if ALG_ECC |
| 46 |
# include "X509_ECC_fp.h" |
| 47 |
#endif // ALG_ECC |
| 48 |
#if ALG_SM2 |
| 49 |
//# include "X509_SM2_fp.h" |
| 50 |
#endif // ALG_RSA |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
//** Unmarshaling Functions |
| 55 |
|
| 56 |
//*** X509FindExtensionOID() |
| 57 |
// This will search a list of X508 extensions to find an extension with the |
| 58 |
// requested OID. If the extension is found, the output context ('ctx') is set up |
| 59 |
// to point to the OID in the extension. |
| 60 |
// Return Type: BOOL |
| 61 |
// TRUE(1) success |
| 62 |
// FALSE(0) failure (could be catastrophic) |
| 63 |
BOOL |
| 64 |
X509FindExtensionByOID( |
| 65 |
ASN1UnmarshalContext *ctxIn, // IN: the context to search |
| 66 |
ASN1UnmarshalContext *ctx, // OUT: the extension context |
| 67 |
const BYTE *OID // IN: oid to search for |
| 68 |
) |
| 69 |
{ |
| 70 |
INT16 length; |
| 71 |
// |
| 72 |
pAssert(ctxIn != NULL); |
| 73 |
// Make the search non-destructive of the input if ctx provided. Otherwise, use |
| 74 |
// the provided context. |
| 75 |
if (ctx == NULL) |
| 76 |
ctx = ctxIn; |
| 77 |
else if(ctx != ctxIn) |
| 78 |
*ctx = *ctxIn; |
| 79 |
for(;ctx->size > ctx->offset; ctx->offset += length) |
| 80 |
{ |
| 81 |
VERIFY((length = ASN1NextTag(ctx)) >= 0); |
| 82 |
// If this is not a constructed sequence, then it doesn't belong |
| 83 |
// in the extensions. |
| 84 |
VERIFY(ctx->tag == ASN1_CONSTRUCTED_SEQUENCE); |
| 85 |
// Make sure that this entry could hold the OID |
| 86 |
if (length >= OID_SIZE(OID)) |
| 87 |
{ |
| 88 |
// See if this is a match for the provided object identifier. |
| 89 |
if (MemoryEqual(OID, &(ctx->buffer[ctx->offset]), OID_SIZE(OID))) |
| 90 |
{ |
| 91 |
// Return with ' ctx' set to point to the start of the OID with the size |
| 92 |
// set to be the size of the SEQUENCE |
| 93 |
ctx->buffer += ctx->offset; |
| 94 |
ctx->offset = 0; |
| 95 |
ctx->size = length; |
| 96 |
return TRUE; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
VERIFY(ctx->offset == ctx->size); |
| 101 |
return FALSE; |
| 102 |
Error: |
| 103 |
ctxIn->size = -1; |
| 104 |
ctx->size = -1; |
| 105 |
return FALSE; |
| 106 |
} |
| 107 |
|
| 108 |
//*** X509GetExtensionBits() |
| 109 |
// This function will extract a bit field from an extension. If the extension doesn't |
| 110 |
// contain a bit string, it will fail. |
| 111 |
// Return Type: BOOL |
| 112 |
// TRUE(1) success |
| 113 |
// FALSE(0) failure |
| 114 |
UINT32 |
| 115 |
X509GetExtensionBits( |
| 116 |
ASN1UnmarshalContext *ctx, |
| 117 |
UINT32 *value |
| 118 |
) |
| 119 |
{ |
| 120 |
INT16 length; |
| 121 |
// |
| 122 |
while (((length = ASN1NextTag(ctx)) > 0) && (ctx->size > ctx->offset)) |
| 123 |
{ |
| 124 |
// Since this is an extension, the extension value will be in an OCTET STRING |
| 125 |
if (ctx->tag == ASN1_OCTET_STRING) |
| 126 |
{ |
| 127 |
return ASN1GetBitStringValue(ctx, value); |
| 128 |
} |
| 129 |
ctx->offset += length; |
| 130 |
} |
| 131 |
ctx->size = -1; |
| 132 |
return FALSE; |
| 133 |
} |
| 134 |
|
| 135 |
//***X509ProcessExtensions() |
| 136 |
// This function is used to process the TPMA_OBJECT and KeyUsage extensions. It is not |
| 137 |
// in the CertifyX509.c code because it makes the code harder to follow. |
| 138 |
// Return Type: TPM_RC |
| 139 |
// TPM_RCS_ATTRIBUTES the attributes of object are not consistent with |
| 140 |
// the extension setting |
| 141 |
// TPM_RC_VALUE problem parsing the extensions |
| 142 |
TPM_RC |
| 143 |
X509ProcessExtensions( |
| 144 |
OBJECT *object, // IN: The object with the attributes to |
| 145 |
// check |
| 146 |
stringRef *extension // IN: The start and length of the extensions |
| 147 |
) |
| 148 |
{ |
| 149 |
ASN1UnmarshalContext ctx; |
| 150 |
ASN1UnmarshalContext extensionCtx; |
| 151 |
INT16 length; |
| 152 |
UINT32 value; |
| 153 |
// |
| 154 |
if(!ASN1UnmarshalContextInitialize(&ctx, extension->len, extension->buf) |
| 155 |
|| ((length = ASN1NextTag(&ctx)) < 0) |
| 156 |
|| (ctx.tag != X509_EXTENSIONS)) |
| 157 |
return TPM_RCS_VALUE; |
| 158 |
if( ((length = ASN1NextTag(&ctx)) < 0) |
| 159 |
|| (ctx.tag != (ASN1_CONSTRUCTED_SEQUENCE))) |
| 160 |
return TPM_RCS_VALUE; |
| 161 |
|
| 162 |
// Get the extension for the TPMA_OBJECT if there is one |
| 163 |
if(X509FindExtensionByOID(&ctx, &extensionCtx, OID_TCG_TPMA_OBJECT) && |
| 164 |
X509GetExtensionBits(&extensionCtx, &value)) |
| 165 |
{ |
| 166 |
// If an keyAttributes extension was found, it must be exactly the same as the |
| 167 |
// attributes of the object. |
| 168 |
// This cast will work because we know that a TPMA_OBJECT is in a UINT32. |
| 169 |
// Set RUNTIME_SIZE_CHECKS to YES to force a check to verify this assumption |
| 170 |
// during debug. Doing this is lot easier than having to revisit the code |
| 171 |
// any time a new attribute is added. |
| 172 |
// NOTE: MemoryEqual() is used to avoid type-punned pointer warning/error. |
| 173 |
if(!MemoryEqual(&value, &object->publicArea.objectAttributes, sizeof(value))) |
| 174 |
return TPM_RCS_ATTRIBUTES; |
| 175 |
} |
| 176 |
// Make sure the failure to find the value wasn't because of a fatal error |
| 177 |
else if(extensionCtx.size < 0) |
| 178 |
return TPM_RCS_VALUE; |
| 179 |
|
| 180 |
// Get the keyUsage extension. This one is required |
| 181 |
if(X509FindExtensionByOID(&ctx, &extensionCtx, OID_KEY_USAGE_EXTENSTION) && |
| 182 |
X509GetExtensionBits(&extensionCtx, &value)) |
| 183 |
{ |
| 184 |
x509KeyUsageUnion keyUsage; |
| 185 |
TPMA_OBJECT attributes = object->publicArea.objectAttributes; |
| 186 |
// |
| 187 |
keyUsage.integer = value; |
| 188 |
// For KeyUsage: |
| 189 |
// the 'sign' attribute is SET if Key Usage includes signing |
| 190 |
if( ( (keyUsageSign.integer & keyUsage.integer) != 0 |
| 191 |
&& !IS_ATTRIBUTE(attributes, TPMA_OBJECT, sign)) |
| 192 |
// OR the 'decrypt' attribute is Set if Key Usage includes decryption uses |
| 193 |
|| ( (keyUsageDecrypt.integer & keyUsage.integer) != 0 |
| 194 |
&& !IS_ATTRIBUTE(attributes, TPMA_OBJECT, decrypt)) |
| 195 |
// OR that 'fixedTPM' is SET if Key Usage is non-repudiation |
| 196 |
|| ( IS_ATTRIBUTE(keyUsage.x509, TPMA_X509_KEY_USAGE, nonrepudiation) |
| 197 |
&& !IS_ATTRIBUTE(attributes, TPMA_OBJECT, fixedTPM)) |
| 198 |
// OR that 'restricted' is SET if Key Usage is key agreement |
| 199 |
|| ( IS_ATTRIBUTE(keyUsage.x509, TPMA_X509_KEY_USAGE, keyAgreement) |
| 200 |
&& !IS_ATTRIBUTE(attributes, TPMA_OBJECT, restricted)) |
| 201 |
) |
| 202 |
return TPM_RCS_ATTRIBUTES; |
| 203 |
} |
| 204 |
else |
| 205 |
// The KeyUsage extension is required |
| 206 |
return TPM_RCS_VALUE; |
| 207 |
|
| 208 |
return TPM_RC_SUCCESS; |
| 209 |
} |
| 210 |
|
| 211 |
//** Marshaling Functions |
| 212 |
|
| 213 |
//*** X509AddSigningAlgorithm() |
| 214 |
// This creates the singing algorithm data. |
| 215 |
// Return Type: INT16 |
| 216 |
// > 0 number of octets added |
| 217 |
// <= 0 failure |
| 218 |
INT16 |
| 219 |
X509AddSigningAlgorithm( |
| 220 |
ASN1MarshalContext *ctx, |
| 221 |
OBJECT *signKey, |
| 222 |
TPMT_SIG_SCHEME *scheme |
| 223 |
) |
| 224 |
{ |
| 225 |
switch(signKey->publicArea.type) |
| 226 |
{ |
| 227 |
#if ALG_RSA |
| 228 |
case ALG_RSA_VALUE: |
| 229 |
return X509AddSigningAlgorithmRSA(signKey, scheme, ctx); |
| 230 |
#endif // ALG_RSA |
| 231 |
#if ALG_ECC |
| 232 |
case ALG_ECC_VALUE: |
| 233 |
return X509AddSigningAlgorithmECC(signKey, scheme, ctx); |
| 234 |
#endif // ALG_ECC |
| 235 |
#if ALG_SM2 |
| 236 |
case ALG_SM2: |
| 237 |
return X509AddSigningAlgorithmSM2(signKey, scheme,ctx); |
| 238 |
#endif // ALG_SM2 |
| 239 |
default: |
| 240 |
break; |
| 241 |
} |
| 242 |
return 0; |
| 243 |
} |
| 244 |
|
| 245 |
//*** X509AddPublicKey() |
| 246 |
// This function will add the publicKey description to the DER data. If fillPtr is |
| 247 |
// NULL, then no data is transferred and this function will indicate if the TPM |
| 248 |
// has the values for DER-encoding of the public key. |
| 249 |
// Return Type: INT16 |
| 250 |
// > 0 number of octets added |
| 251 |
// == 0 failure |
| 252 |
INT16 |
| 253 |
X509AddPublicKey( |
| 254 |
ASN1MarshalContext *ctx, |
| 255 |
OBJECT *object |
| 256 |
) |
| 257 |
{ |
| 258 |
switch(object->publicArea.type) |
| 259 |
{ |
| 260 |
#if ALG_RSA |
| 261 |
case ALG_RSA_VALUE: |
| 262 |
return X509AddPublicRSA(object, ctx); |
| 263 |
#endif |
| 264 |
#if ALG_ECC |
| 265 |
case ALG_ECC_VALUE: |
| 266 |
return X509AddPublicECC(object, ctx); |
| 267 |
#endif |
| 268 |
#if ALG_SM2 |
| 269 |
case ALG_SM2_VALUE: |
| 270 |
break; |
| 271 |
#endif |
| 272 |
default: |
| 273 |
break; |
| 274 |
} |
| 275 |
return FALSE; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
//*** X509PushAlgorithmIdentifierSequence() |
| 280 |
// Return Type: INT16 |
| 281 |
// > 0 number of bytes added |
| 282 |
// == 0 failure |
| 283 |
INT16 |
| 284 |
X509PushAlgorithmIdentifierSequence( |
| 285 |
ASN1MarshalContext *ctx, |
| 286 |
const BYTE *OID |
| 287 |
) |
| 288 |
{ |
| 289 |
ASN1StartMarshalContext(ctx); // hash algorithm |
| 290 |
ASN1PushNull(ctx); |
| 291 |
ASN1PushOID(ctx, OID); |
| 292 |
return ASN1EndEncapsulation(ctx, ASN1_CONSTRUCTED_SEQUENCE); |
| 293 |
} |
| 294 |
|
| 295 |
|