-
Notifications
You must be signed in to change notification settings - Fork 161
/
TestCategorizer.cs
286 lines (272 loc) · 15.1 KB
/
TestCategorizer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See the LICENSE file in the project root for full license information.
*/
using System.Diagnostics;
using System.Collections.Generic;
using Tpm2Lib;
namespace Tpm2Tester
{
// Validates test categorization (privileges, thread safety, MinTPM-profile,
// and NeedsReboot from observing behavior and tables contained herein
internal class TestCategorizer
{
static bool Initialized = false;
// Indicates whether the command mutates global state. The assumption is that
// if an session, object NV slot, etc. is "owned" by a thread then that is safe.
// Conversely global resources (PCR, the NV-array, owner auth, clock, etc. are
// shared state. Modification of shared state is not thread safe. For the
// purposes of the testCtx, use of shared state IS thread safe.
static CommandThreadSafety[] ThreadInfo =
{
new CommandThreadSafety(TpmCc.EvictControl, false),
new CommandThreadSafety(TpmCc.HierarchyControl, false),
new CommandThreadSafety(TpmCc.NvUndefineSpace, false),
new CommandThreadSafety(TpmCc.ChangeEPS, false),
new CommandThreadSafety(TpmCc.ChangePPS, false),
new CommandThreadSafety(TpmCc.Clear, false),
new CommandThreadSafety(TpmCc.ClearControl, false),
new CommandThreadSafety(TpmCc.ClockSet, false),
new CommandThreadSafety(TpmCc.HierarchyChangeAuth, false),
new CommandThreadSafety(TpmCc.NvDefineSpace, false),
new CommandThreadSafety(TpmCc.PcrAllocate, false),
new CommandThreadSafety(TpmCc.PcrSetAuthPolicy, false),
new CommandThreadSafety(TpmCc.SetPrimaryPolicy, false),
new CommandThreadSafety(TpmCc.FieldUpgradeStart, false),
new CommandThreadSafety(TpmCc.ClockRateAdjust, false),
new CommandThreadSafety(TpmCc.CreatePrimary, true),
new CommandThreadSafety(TpmCc.NvGlobalWriteLock, false),
new CommandThreadSafety(TpmCc.GetCommandAuditDigest, true),
new CommandThreadSafety(TpmCc.NvIncrement, true),
new CommandThreadSafety(TpmCc.NvSetBits, true),
new CommandThreadSafety(TpmCc.NvExtend, true),
new CommandThreadSafety(TpmCc.NvWrite, true),
new CommandThreadSafety(TpmCc.NvWriteLock, true),
new CommandThreadSafety(TpmCc.DictionaryAttackLockReset, false),
new CommandThreadSafety(TpmCc.DictionaryAttackParameters, false),
new CommandThreadSafety(TpmCc.NvChangeAuth, true),
new CommandThreadSafety(TpmCc.PcrEvent, false),
new CommandThreadSafety(TpmCc.PcrReset, false),
new CommandThreadSafety(TpmCc.SequenceComplete, true),
new CommandThreadSafety(TpmCc.SetAlgorithmSet , false),
new CommandThreadSafety(TpmCc.SetCommandCodeAuditStatus, false),
new CommandThreadSafety(TpmCc.FieldUpgradeData, false),
new CommandThreadSafety(TpmCc.IncrementalSelfTest, false),
new CommandThreadSafety(TpmCc.SelfTest, false),
new CommandThreadSafety(TpmCc.Startup, false),
new CommandThreadSafety(TpmCc.Shutdown, false),
new CommandThreadSafety(TpmCc.StirRandom, true),
new CommandThreadSafety(TpmCc.ActivateCredential, true),
new CommandThreadSafety(TpmCc.Certify, true),
new CommandThreadSafety(TpmCc.PolicyNV, true),
new CommandThreadSafety(TpmCc.CertifyCreation, true),
new CommandThreadSafety(TpmCc.Duplicate, true),
new CommandThreadSafety(TpmCc.GetTime, true),
new CommandThreadSafety(TpmCc.GetSessionAuditDigest, true),
new CommandThreadSafety(TpmCc.NvRead, true),
new CommandThreadSafety(TpmCc.NvReadLock, true),
new CommandThreadSafety(TpmCc.ObjectChangeAuth, true),
new CommandThreadSafety(TpmCc.PolicySecret, true),
new CommandThreadSafety(TpmCc.Rewrap, true),
new CommandThreadSafety(TpmCc.Create, true),
new CommandThreadSafety(TpmCc.EcdhZGen, true),
new CommandThreadSafety(TpmCc.Hmac, true),
new CommandThreadSafety(TpmCc.Import, true),
new CommandThreadSafety(TpmCc.Load, true),
new CommandThreadSafety(TpmCc.Quote, true),
new CommandThreadSafety(TpmCc.RsaDecrypt, true),
new CommandThreadSafety(TpmCc.HmacStart, true),
new CommandThreadSafety(TpmCc.SequenceUpdate, true),
new CommandThreadSafety(TpmCc.Sign, true),
new CommandThreadSafety(TpmCc.Unseal, true),
new CommandThreadSafety(TpmCc.PolicySigned, true),
new CommandThreadSafety(TpmCc.ContextLoad, false), // because they won't run on an RM
new CommandThreadSafety(TpmCc.ContextSave, false), // because they won't run on an RM
new CommandThreadSafety(TpmCc.EcdhKeyGen, true),
new CommandThreadSafety(TpmCc.EncryptDecrypt, true),
new CommandThreadSafety(TpmCc.FlushContext, true),
new CommandThreadSafety(TpmCc.LoadExternal, true),
new CommandThreadSafety(TpmCc.MakeCredential, true),
new CommandThreadSafety(TpmCc.NvReadPublic, true),
new CommandThreadSafety(TpmCc.PolicyAuthorize, true),
new CommandThreadSafety(TpmCc.PolicyAuthValue, true),
new CommandThreadSafety(TpmCc.PolicyCommandCode, true),
new CommandThreadSafety(TpmCc.PolicyCounterTimer, true),
new CommandThreadSafety(TpmCc.PolicyCpHash, true),
new CommandThreadSafety(TpmCc.PolicyLocality, true),
new CommandThreadSafety(TpmCc.PolicyNameHash, true),
new CommandThreadSafety(TpmCc.PolicyOR, true),
new CommandThreadSafety(TpmCc.PolicyTicket, true),
new CommandThreadSafety(TpmCc.ReadPublic, true),
new CommandThreadSafety(TpmCc.RsaEncrypt, true),
new CommandThreadSafety(TpmCc.StartAuthSession, true),
new CommandThreadSafety(TpmCc.VerifySignature, true),
new CommandThreadSafety(TpmCc.EccParameters, true),
new CommandThreadSafety(TpmCc.FirmwareRead, false),
new CommandThreadSafety(TpmCc.GetCapability, true),
new CommandThreadSafety(TpmCc.GetRandom, true),
new CommandThreadSafety(TpmCc.GetTestResult, true),
new CommandThreadSafety(TpmCc.Hash, true),
new CommandThreadSafety(TpmCc.PcrRead, true),
new CommandThreadSafety(TpmCc.PolicyPCR, true),
new CommandThreadSafety(TpmCc.PolicyRestart, true),
new CommandThreadSafety(TpmCc.ReadClock, true),
new CommandThreadSafety(TpmCc.PcrExtend, false),
new CommandThreadSafety(TpmCc.PcrSetAuthValue, false),
new CommandThreadSafety(TpmCc.NvCertify, true),
new CommandThreadSafety(TpmCc.EventSequenceComplete, false),
new CommandThreadSafety(TpmCc.HashSequenceStart, true),
new CommandThreadSafety(TpmCc.PolicyPhysicalPresence, true),
new CommandThreadSafety(TpmCc.PolicyDuplicationSelect, true),
new CommandThreadSafety(TpmCc.PolicyGetDigest, true),
new CommandThreadSafety(TpmCc.TestParms, true),
// new CommandThreadSafety(TpmCc.ec.EcdaaCertify, true),
new CommandThreadSafety(TpmCc.PolicyPassword, true)
};
// DB of Windows privileges necessary to execute command
static MinTpmNecessaryPrivileges[] NecessaryPrivileges =
{
// admin, standardUser, okInLockout
new MinTpmNecessaryPrivileges(TpmCc.Startup, false, false, true),
new MinTpmNecessaryPrivileges(TpmCc.Shutdown, false, false, true),
new MinTpmNecessaryPrivileges(TpmCc.SelfTest, true, false, true),
new MinTpmNecessaryPrivileges(TpmCc.GetTestResult, true, false, true),
new MinTpmNecessaryPrivileges(TpmCc.StartAuthSession, true, true, true),
new MinTpmNecessaryPrivileges(TpmCc.PolicyRestart, true, false, true),
new MinTpmNecessaryPrivileges(TpmCc.Create, true, true, false),
new MinTpmNecessaryPrivileges(TpmCc.Load, true, true, false),
new MinTpmNecessaryPrivileges(TpmCc.ReadPublic, true, true, true),
new MinTpmNecessaryPrivileges(TpmCc.ActivateCredential, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.Unseal, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.ObjectChangeAuth, true, true, false),
new MinTpmNecessaryPrivileges(TpmCc.Duplicate, true, true, false),
new MinTpmNecessaryPrivileges(TpmCc.Import, true, true, false),
new MinTpmNecessaryPrivileges(TpmCc.Sign, true, true, false),
new MinTpmNecessaryPrivileges(TpmCc.RsaDecrypt, true, true, false),
new MinTpmNecessaryPrivileges(TpmCc.Hash, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.GetRandom, true, true, true),
new MinTpmNecessaryPrivileges(TpmCc.StirRandom, true, true, true),
new MinTpmNecessaryPrivileges(TpmCc.Certify, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.CertifyCreation, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.Quote, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.PcrEvent, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.PcrRead, true, false, true),
new MinTpmNecessaryPrivileges(TpmCc.PcrReset, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.PcrExtend, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.PolicySecret, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.PolicyOR, true, false, true),
new MinTpmNecessaryPrivileges(TpmCc.PolicyPCR, true, false, true),
new MinTpmNecessaryPrivileges(TpmCc.PolicyCommandCode, true, false, true),
new MinTpmNecessaryPrivileges(TpmCc.PolicyAuthValue, true, true, true),
new MinTpmNecessaryPrivileges(TpmCc.CreatePrimary, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.HierarchyControl,true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.Clear, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.ClearControl, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.HierarchyChangeAuth, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.DictionaryAttackLockReset, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.DictionaryAttackParameters, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.ContextSave, false , false, true),
new MinTpmNecessaryPrivileges(TpmCc.ContextLoad, false , false, true),
new MinTpmNecessaryPrivileges(TpmCc.FlushContext, true, true, true),
new MinTpmNecessaryPrivileges(TpmCc.EvictControl, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.ReadClock, true, false, true),
new MinTpmNecessaryPrivileges(TpmCc.ClockSet, false, false, false),
new MinTpmNecessaryPrivileges(TpmCc.GetCapability, true, true, true),
new MinTpmNecessaryPrivileges(TpmCc.NvDefineSpace, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.NvUndefineSpace, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.NvReadPublic, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.NvWrite, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.NvIncrement, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.NvRead, true, false, false),
new MinTpmNecessaryPrivileges(TpmCc.NvChangeAuth, true, false, false )
};
internal static void Init()
{
//
// Allow Init to be called multiple times for TAEF support
if (TestCategorizer.Initialized == false)
{
Debug.Assert(Privileges.Count == 0);
foreach (MinTpmNecessaryPrivileges priv in NecessaryPrivileges)
{
Privileges.Add(priv.CommandCode, priv);
}
foreach (CommandThreadSafety safe in ThreadInfo)
{
ThreadSafety.Add(safe.CommandCode, safe.ThreadSafe);
}
TestCategorizer.Initialized = true;
}
}
// Determines whether the command is in the minimal (MinTPM) profile
internal static bool InProfile0(TpmCc code)
{
return Privileges.ContainsKey(code);
}
internal static bool GetThreadSafety(TpmCc command)
{
return ThreadSafety[command];
}
// Does the command mutate global state?
internal static bool CommandDefined(TpmCc command)
{
return ThreadSafety.ContainsKey(command);
}
// Return true if the command available for MinTpm
internal static bool CommandDefinedForMinTpm(TpmCc command)
{
foreach (MinTpmNecessaryPrivileges c in NecessaryPrivileges)
{
if (c.CommandCode.Equals(command) == true)
return true;
}
return false;
}
// Get expected privileges to execute command
internal static NecessaryPrivilege GetNecessaryPrivileges(TpmCc code)
{
MinTpmNecessaryPrivileges priv = Privileges[code];
if (priv.StandardUserAccessible) return NecessaryPrivilege.User;
if (priv.AdminAccessible) return NecessaryPrivilege.Admin;
return NecessaryPrivilege.Special;
}
static Dictionary<TpmCc, MinTpmNecessaryPrivileges> Privileges =
new Dictionary<TpmCc, MinTpmNecessaryPrivileges>();
static Dictionary<TpmCc, bool> ThreadSafety = new Dictionary<TpmCc, bool>();
}
// Indicates OS-level privileges necessary to execute the command.
internal enum NecessaryPrivilege
{
User = 1,
Admin = 2,
// Command is not accessible when OS is running, but is needed for Profile0
Special = 3,
// Command is only accessible on instrumented TPMs
Debug = 4
}
internal class CommandThreadSafety
{
internal CommandThreadSafety(TpmCc code, bool safe)
{
CommandCode = code;
ThreadSafe = safe;
}
internal TpmCc CommandCode;
internal bool ThreadSafe;
}
internal class MinTpmNecessaryPrivileges
{
internal MinTpmNecessaryPrivileges(TpmCc code,
bool admin, bool stdUser, bool OkInLockout)
{
CommandCode = code;
AdminAccessible = admin;
StandardUserAccessible = stdUser;
AllowedInLockout = OkInLockout;
}
internal TpmCc CommandCode;
internal bool AdminAccessible;
internal bool StandardUserAccessible;
internal bool AllowedInLockout;
}
}