• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Rewrap_fp.h"
37 
38 #if CC_Rewrap  // Conditional expansion of this file
39 
40 #include "Object_spt_fp.h"
41 
42 /*(See part 3 specification)
43 // This command allows the TPM to serve in the role as an MA.
44 */
45 //  Return Type: TPM_RC
46 //      TPM_RC_ATTRIBUTES       'newParent' is not a decryption key
47 //      TPM_RC_HANDLE           'oldParent' is not consistent with inSymSeed
48 //      TPM_RC_INTEGRITY        the integrity check of 'inDuplicate' failed
49 //      TPM_RC_KEY              for an ECC key, the public key is not on the curve
50 //                              of the curve ID
51 //      TPM_RC_KEY_SIZE         the decrypted input symmetric key size
52 //                              does not match the symmetric algorithm
53 //                              key size of 'oldParent'
54 //      TPM_RC_TYPE             'oldParent' is not a storage key, or 'newParent'
55 //                              is not a storage key
56 //      TPM_RC_VALUE            for an 'oldParent'; RSA key, the data to be decrypted
57 //                              is greater than the public exponent
58 //      Unmarshal errors        errors during unmarshaling the input
59 //                              encrypted buffer to a ECC public key, or
60 //                              unmarshal the private buffer to 'sensitive'
61 TPM_RC
TPM2_Rewrap(Rewrap_In * in,Rewrap_Out * out)62 TPM2_Rewrap(
63     Rewrap_In       *in,            // IN: input parameter list
64     Rewrap_Out      *out            // OUT: output parameter list
65     )
66 {
67     TPM_RC                  result = TPM_RC_SUCCESS;
68     TPM2B_DATA              data;               // symmetric key
69     UINT16                  hashSize = 0;
70     TPM2B_PRIVATE           privateBlob;        // A temporary private blob
71                                                 // to transit between old
72                                                 // and new wrappers
73 // Input Validation
74     if((in->inSymSeed.t.size == 0 && in->oldParent != TPM_RH_NULL)
75        || (in->inSymSeed.t.size != 0 && in->oldParent == TPM_RH_NULL))
76         return TPM_RCS_HANDLE + RC_Rewrap_oldParent;
77     if(in->oldParent != TPM_RH_NULL)
78     {
79         OBJECT              *oldParent = HandleToObject(in->oldParent);
80 
81         // old parent key must be a storage object
82         if(!ObjectIsStorage(in->oldParent))
83             return TPM_RCS_TYPE + RC_Rewrap_oldParent;
84         // Decrypt input secret data via asymmetric decryption.  A
85         // TPM_RC_VALUE, TPM_RC_KEY or unmarshal errors may be returned at this
86         // point
87         result = CryptSecretDecrypt(oldParent, NULL, DUPLICATE_STRING,
88                                     &in->inSymSeed, &data);
89         if(result != TPM_RC_SUCCESS)
90             return TPM_RCS_VALUE + RC_Rewrap_inSymSeed;
91         // Unwrap Outer
92         result = UnwrapOuter(oldParent, &in->name.b,
93                              oldParent->publicArea.nameAlg, &data.b,
94                              FALSE,
95                              in->inDuplicate.t.size, in->inDuplicate.t.buffer);
96         if(result != TPM_RC_SUCCESS)
97             return RcSafeAddToResult(result, RC_Rewrap_inDuplicate);
98         // Copy unwrapped data to temporary variable, remove the integrity field
99         hashSize = sizeof(UINT16) +
100             CryptHashGetDigestSize(oldParent->publicArea.nameAlg);
101         privateBlob.t.size = in->inDuplicate.t.size - hashSize;
102         pAssert(privateBlob.t.size <= sizeof(privateBlob.t.buffer));
103         MemoryCopy(privateBlob.t.buffer, in->inDuplicate.t.buffer + hashSize,
104                      privateBlob.t.size);
105     }
106     else
107     {
108         // No outer wrap from input blob.  Direct copy.
109         privateBlob = in->inDuplicate;
110     }
111     if(in->newParent != TPM_RH_NULL)
112     {
113         OBJECT          *newParent;
114         newParent = HandleToObject(in->newParent);
115 
116         // New parent must be a storage object
117         if(!ObjectIsStorage(in->newParent))
118             return TPM_RCS_TYPE + RC_Rewrap_newParent;
119         // Make new encrypt key and its associated secret structure.  A
120         // TPM_RC_VALUE error may be returned at this point if RSA algorithm is
121         // enabled in TPM
122         out->outSymSeed.t.size = sizeof(out->outSymSeed.t.secret);
123         result = CryptSecretEncrypt(newParent, DUPLICATE_STRING, &data,
124                                     &out->outSymSeed);
125         if(result != TPM_RC_SUCCESS)
126             return result;
127         // Copy temporary variable to output, reserve the space for integrity
128         hashSize = sizeof(UINT16) +
129             CryptHashGetDigestSize(newParent->publicArea.nameAlg);
130  // Make sure that everything fits into the output buffer
131  // Note: this is mostly only an issue if there was no outer wrapper on
132  // 'inDuplicate'. It could be as large as a TPM2B_PRIVATE buffer. If we add
133  // a digest for an outer wrapper, it won't fit anymore.
134         if((privateBlob.t.size + hashSize) > sizeof(out->outDuplicate.t.buffer))
135             return TPM_RCS_VALUE + RC_Rewrap_inDuplicate;
136 // Command output
137         out->outDuplicate.t.size = privateBlob.t.size;
138         pAssert(privateBlob.t.size
139                 <= sizeof(out->outDuplicate.t.buffer) - hashSize);
140         MemoryCopy(out->outDuplicate.t.buffer + hashSize, privateBlob.t.buffer,
141                    privateBlob.t.size);
142         // Produce outer wrapper for output
143         out->outDuplicate.t.size = ProduceOuterWrap(newParent, &in->name.b,
144                                                     newParent->publicArea.nameAlg,
145                                                     &data.b,
146                                                     FALSE,
147                                                     out->outDuplicate.t.size,
148                                                     out->outDuplicate.t.buffer);
149     }
150     else  // New parent is a null key so there is no seed
151     {
152         out->outSymSeed.t.size = 0;
153 
154         // Copy privateBlob directly
155         out->outDuplicate = privateBlob;
156     }
157     return TPM_RC_SUCCESS;
158 }
159 
160 #endif // CC_Rewrap