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 "Duplicate_fp.h"
37
38 #if CC_Duplicate // Conditional expansion of this file
39
40 #include "Object_spt_fp.h"
41
42 /*(See part 3 specification)
43 // Duplicate a loaded object
44 */
45 // Return Type: TPM_RC
46 // TPM_RC_ATTRIBUTES key to duplicate has 'fixedParent' SET
47 // TPM_RC_HASH for an RSA key, the nameAlg digest size for the
48 // newParent is not compatible with the key size
49 // TPM_RC_HIERARCHY 'encryptedDuplication' is SET and 'newParentHandle'
50 // specifies Null Hierarchy
51 // TPM_RC_KEY 'newParentHandle' references invalid ECC key (public
52 // point not on the curve)
53 // TPM_RC_SIZE input encryption key size does not match the
54 // size specified in symmetric algorithm
55 // TPM_RC_SYMMETRIC 'encryptedDuplication' is SET but no symmetric
56 // algorithm is provided
57 // TPM_RC_TYPE 'newParentHandle' is neither a storage key nor
58 // TPM_RH_NULL; or the object has a NULL nameAlg
59 // TPM_RC_VALUE for an RSA newParent, the sizes of the digest and
60 // the encryption key are too large to be OAEP encoded
61 TPM_RC
TPM2_Duplicate(Duplicate_In * in,Duplicate_Out * out)62 TPM2_Duplicate(
63 Duplicate_In *in, // IN: input parameter list
64 Duplicate_Out *out // OUT: output parameter list
65 )
66 {
67 TPM_RC result = TPM_RC_SUCCESS;
68 TPMT_SENSITIVE sensitive;
69
70 UINT16 innerKeySize = 0; // encrypt key size for inner wrap
71
72 OBJECT *object;
73 OBJECT *newParent;
74 TPM2B_DATA data;
75
76 // Input Validation
77
78 // Get duplicate object pointer
79 object = HandleToObject(in->objectHandle);
80 // Get new parent
81 newParent = HandleToObject(in->newParentHandle);
82
83 // duplicate key must have fixParent bit CLEAR.
84 if(IS_ATTRIBUTE(object->publicArea.objectAttributes, TPMA_OBJECT, fixedParent))
85 return TPM_RCS_ATTRIBUTES + RC_Duplicate_objectHandle;
86
87 // Do not duplicate object with NULL nameAlg
88 if(object->publicArea.nameAlg == TPM_ALG_NULL)
89 return TPM_RCS_TYPE + RC_Duplicate_objectHandle;
90
91 // new parent key must be a storage object or TPM_RH_NULL
92 if(in->newParentHandle != TPM_RH_NULL
93 && !ObjectIsStorage(in->newParentHandle))
94 return TPM_RCS_TYPE + RC_Duplicate_newParentHandle;
95
96 // If the duplicated object has encryptedDuplication SET, then there must be
97 // an inner wrapper and the new parent may not be TPM_RH_NULL
98 if(IS_ATTRIBUTE(object->publicArea.objectAttributes, TPMA_OBJECT,
99 encryptedDuplication))
100 {
101 if(in->symmetricAlg.algorithm == TPM_ALG_NULL)
102 return TPM_RCS_SYMMETRIC + RC_Duplicate_symmetricAlg;
103 if(in->newParentHandle == TPM_RH_NULL)
104 return TPM_RCS_HIERARCHY + RC_Duplicate_newParentHandle;
105 }
106
107 if(in->symmetricAlg.algorithm == TPM_ALG_NULL)
108 {
109 // if algorithm is TPM_ALG_NULL, input key size must be 0
110 if(in->encryptionKeyIn.t.size != 0)
111 return TPM_RCS_SIZE + RC_Duplicate_encryptionKeyIn;
112 }
113 else
114 {
115 // Get inner wrap key size
116 innerKeySize = in->symmetricAlg.keyBits.sym;
117
118 // If provided the input symmetric key must match the size of the algorithm
119 if(in->encryptionKeyIn.t.size != 0
120 && in->encryptionKeyIn.t.size != (innerKeySize + 7) / 8)
121 return TPM_RCS_SIZE + RC_Duplicate_encryptionKeyIn;
122 }
123
124 // Command Output
125
126 if(in->newParentHandle != TPM_RH_NULL)
127 {
128 // Make encrypt key and its associated secret structure. A TPM_RC_KEY
129 // error may be returned at this point
130 out->outSymSeed.t.size = sizeof(out->outSymSeed.t.secret);
131 result = CryptSecretEncrypt(newParent, DUPLICATE_STRING, &data,
132 &out->outSymSeed);
133 if(result != TPM_RC_SUCCESS)
134 return result;
135 }
136 else
137 {
138 // Do not apply outer wrapper
139 data.t.size = 0;
140 out->outSymSeed.t.size = 0;
141 }
142
143 // Copy sensitive area
144 sensitive = object->sensitive;
145
146 // Prepare output private data from sensitive.
147 // Note: If there is no encryption key, one will be provided by
148 // SensitiveToDuplicate(). This is why the assignment of encryptionKeyIn to
149 // encryptionKeyOut will work properly and is not conditional.
150 SensitiveToDuplicate(&sensitive, &object->name.b, newParent,
151 object->publicArea.nameAlg, &data.b,
152 &in->symmetricAlg, &in->encryptionKeyIn,
153 &out->duplicate);
154
155 out->encryptionKeyOut = in->encryptionKeyIn;
156
157 return TPM_RC_SUCCESS;
158 }
159
160 #endif // CC_Duplicate