Debian Codegraph @

simulator/ms-tpm-20-ref/TPMCmd/tpm/src/X509/X509_ECC.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 //** Includes
36 #include "Tpm.h"
37 #include "X509.h"
38 #include "OIDs.h"
39 #include "TpmASN1_fp.h"
40 #include "X509_spt_fp.h"
41 #include "CryptHash_fp.h"
42
43 //** Functions
44
45 //*** X509PushPoint()
46 // This seems like it might be used more than once so...
47 // Return Type: INT16
48 // > 0 number of bytes added
49 // == 0 failure
50 INT16
51 X509PushPoint(
52 ASN1MarshalContext *ctx,
53 TPMS_ECC_POINT *p
54 )
55 {
56 // Push a bit string containing the public key. For now, push the x, and y
57 // coordinates of the public point, bottom up
58 ASN1StartMarshalContext(ctx); // BIT STRING
59 {
60 ASN1PushBytes(ctx, p->y.t.size, p->y.t.buffer);
61 ASN1PushBytes(ctx, p->x.t.size, p->x.t.buffer);
62 ASN1PushByte(ctx, 0x04);
63 }
64 return ASN1EndEncapsulation(ctx, ASN1_BITSTRING); // Ends BIT STRING
65 }
66
67 //*** X509AddSigningAlgorithmECC()
68 // This creates the singing algorithm data.
69 // Return Type: INT16
70 // > 0 number of bytes added
71 // == 0 failure
72 INT16
73 X509AddSigningAlgorithmECC(
74 OBJECT *signKey,
75 TPMT_SIG_SCHEME *scheme,
76 ASN1MarshalContext *ctx
77 )
78 {
79 PHASH_DEF hashDef = CryptGetHashDef(scheme->details.any.hashAlg);
80 //
81 NOT_REFERENCED(signKey);
82 // If the desired hashAlg definition wasn't found...
83 if(hashDef->hashAlg != scheme->details.any.hashAlg)
84 return 0;
85
86 switch(scheme->scheme)
87 {
88 case ALG_ECDSA_VALUE:
89 // Make sure that we have an OID for this hash and ECC
90 if((hashDef->ECDSA)[0] != ASN1_OBJECT_IDENTIFIER)
91 break;
92 // if this is just an implementation check, indicate that this
93 // combination is supported
94 if(!ctx)
95 return 1;
96 ASN1StartMarshalContext(ctx);
97 ASN1PushOID(ctx, hashDef->ECDSA);
98 return ASN1EndEncapsulation(ctx, ASN1_CONSTRUCTED_SEQUENCE);
99 default:
100 break;
101 }
102 return 0;
103 }
104
105
106 //*** X509AddPublicECC()
107 // This function will add the publicKey description to the DER data. If ctx is
108 // NULL, then no data is transferred and this function will indicate if the TPM
109 // has the values for DER-encoding of the public key.
110 // Return Type: INT16
111 // > 0 number of bytes added
112 // == 0 failure
113 INT16
114 X509AddPublicECC(
115 OBJECT *object,
116 ASN1MarshalContext *ctx
117 )
118 {
119 const BYTE *curveOid =
120 CryptEccGetOID(object->publicArea.parameters.eccDetail.curveID);
121 if((curveOid == NULL) || (*curveOid != ASN1_OBJECT_IDENTIFIER))
122 return 0;
123 //
124 //
125 // SEQUENCE (2 elem) 1st
126 // SEQUENCE (2 elem) 2nd
127 // OBJECT IDENTIFIER 1.2.840.10045.2.1 ecPublicKey (ANSI X9.62 public key type)
128 // OBJECT IDENTIFIER 1.2.840.10045.3.1.7 prime256v1 (ANSI X9.62 named curve)
129 // BIT STRING (520 bit) 000001001010000111010101010111001001101101000100000010...
130 //
131 // If this is a check to see if the key can be encoded, it can.
132 // Need to mark the end sequence
133 if(ctx == NULL)
134 return 1;
135 ASN1StartMarshalContext(ctx); // SEQUENCE (2 elem) 1st
136 {
137 X509PushPoint(ctx, &object->publicArea.unique.ecc); // BIT STRING
138 ASN1StartMarshalContext(ctx); // SEQUENCE (2 elem) 2nd
139 {
140 ASN1PushOID(ctx, curveOid); // curve dependent
141 ASN1PushOID(ctx, OID_ECC_PUBLIC); // (1.2.840.10045.2.1)
142 }
143 ASN1EndEncapsulation(ctx, ASN1_CONSTRUCTED_SEQUENCE); // Ends SEQUENCE 2nd
144 }
145 return ASN1EndEncapsulation(ctx, ASN1_CONSTRUCTED_SEQUENCE); // Ends SEQUENCE 1st
146 }