forked from microsoft/TSS.MSR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TpmTranslations.cs
573 lines (524 loc) · 24.2 KB
/
TpmTranslations.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
* 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;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
namespace CodeGen
{
public static class TpmTypeTranslations
{
// Just so that the resulting API looks nicer.
static string[] DontFlatten = new string[] { "TPM2B_PRIVATE" };
// The spec uses these two sorts of tagged structure commonly
// { len, array[len] } and
// { selector, [select]union }
// This routine changes references to these sorts of structure to an embedded form.
public static void FlattenTaggedStructures()
{
// Build the list of tagged structures
List<TpmStruct> taggedStructs = new List<TpmStruct>();
foreach(TpmType tp in TpmTypes.TheTypes)
{
var s = tp as TpmStruct;
if (s == null)
continue;
var t = s;
while (t != null && t.Fields.Count != 2)
{
t = t.DerivedFrom;
}
if (t == null || s.SpecName.IsOneOf(DontFlatten))
{
continue;
}
if((t.Fields[0].MarshalType == MarshalType.ArrayCount) ||
(t.Fields[0].MarshalType == MarshalType.UnionSelector) ||
(t.Fields[0].MarshalType == MarshalType.LengthOfStruct)
)
{
taggedStructs.Add(s);
}
}
// find references to the tagged structures and replace them
foreach (TpmType tp in TpmTypes.TheTypes)
{
if (!(tp is TpmStruct))
continue;
TpmStruct t = (TpmStruct)tp;
for (int j = 0; j < t.Fields.Count; j++)
{
StructField origField = t.Fields[j];
if (origField.IsArray())
{
continue; // Don't flatten arrays
}
var toEmbed = origField.Type as TpmStruct;
if (taggedStructs.Contains(toEmbed))
{
// If a structure to flatten is one without fields of its own,
// but is derived from a flattenable one, unwind the inheritance chain.
while (toEmbed != null && toEmbed.Fields.Count != 2)
{
toEmbed = toEmbed.DerivedFrom;
}
StructField tagToEmbed = toEmbed.Fields[0];
Debug.Assert(origField.MinVal == null || origField.MinVal == tagToEmbed.MinVal);
Debug.Assert(origField.MaxVal == null || origField.MaxVal == tagToEmbed.MaxVal);
var bufToEmbed = toEmbed.Fields[1];
string newTagName = origField.Name + Helpers.Capitalize(tagToEmbed.Name);
string newBufTypeName = bufToEmbed.Type.SpecName;
var newTagField = new StructField(tagToEmbed, newTagName);
t.Fields[j] = newTagField;
switch (tagToEmbed.MarshalType)
{
case MarshalType.UnionSelector:
{
var newField = new UnionField(newBufTypeName, origField.Name,
origField.Comment, newTagName, t);
t.Fields.Insert(j + 1, newField);
break;
}
case MarshalType.ArrayCount:
{
var newField = new VariableLengthArray(newBufTypeName, origField.Name,
origField.Comment, newTagName, t);
t.Fields.Insert(j + 1, newField);
break;
}
case MarshalType.LengthOfStruct:
{
var newField = new StructField(newBufTypeName, origField.Name,
origField.Comment);
t.Fields.Insert(j + 1, newField);
newTagField.MarshalType = MarshalType.LengthOfStruct;
newTagField.SizedField = newField;
newField.MarshalType = MarshalType.SizedStruct;
newField.SizeTagField = newTagField;
break;
}
default:
throw new Exception("");
}
} // j-loop
}
}
}
static string[] StructsWithEncryptedBuffer = new string[] { "TPMS_ID_OBJECT", "TPMS_CONTEXT_DATA" };
static void FixStructsWithEncryptedBuffer()
{
foreach (var typeName in StructsWithEncryptedBuffer)
{
var s = (TpmStruct)TpmTypes.Lookup(typeName);
var f = s.Fields[3];
Debug.Assert(f.Name.StartsWith("enc"));
Debug.Assert(f.MarshalType == MarshalType.VariableLengthArray);
f.MarshalType = MarshalType.EncryptedVariableLengthArray;
Debug.Assert(f.SizeTagField != null);
s.Fields[2] = f;
s.Fields.RemoveAt(3);
}
}
static void FixTpm2bStructs()
{
foreach (var s in TpmTypes.Get<TpmStruct>().Where(s => s.Fields.Count == 2 && s.StripTypedefs().SpecName.StartsWith("TPM2B_")))
{
var tagField = s.Fields[0];
var dataField = s.Fields[1];
if (tagField.MarshalType == MarshalType.ArrayCount)
{
// A TPM2B struct has a byte count as the first member that contains the size of the second member.
// The second member can be either a byte buffer or a data structure. In the latter case the type
// of the data structure can be obtained from the name of the TPM2B struct.
string structName = s.SpecName.Replace("TPM2B_", "TPMS_");
if (!TpmTypes.Contains(structName))
continue;
dataField = s.Fields[1] =
new StructField(structName, dataField.Name, dataField.Comment);
}
tagField.MarshalType = MarshalType.LengthOfStruct;
dataField.MarshalType = MarshalType.SizedStruct;
tagField.SizedField = dataField;
dataField.SizeTagField = tagField;
dataField.Domain = tagField.Domain;
}
}
public static string GetEnumPrefix(string memberName, string enumName, bool oldStyle = false)
{
if (oldStyle)
{
if (TargetLang.DotNet)
return enumName == "TPM_ALG_ID" ? "TPM_ALG_" : enumName + "_";
}
else if (memberName == "TPM_RS_PW")
return "TPM_RS_";
else if (enumName == "TPMA_LOCALITY")
return "TPM_";
int curPos = 0,
prefixLen = 0;
int maxPrefixLen = Math.Min(memberName.Length, enumName.Length);
while (curPos < maxPrefixLen && memberName[curPos] == enumName[curPos])
{
if (memberName[curPos++] == '_')
prefixLen = curPos;
}
if (curPos == maxPrefixLen && memberName.Length > curPos && memberName[curPos] == '_')
prefixLen = curPos + 1;
return memberName.Substring(0, prefixLen);
}
public static string RemoveEnumPrefix(string specName, string enclosingTypeName, bool oldStyle = false)
{
string prefix = GetEnumPrefix(specName, enclosingTypeName, oldStyle);
return specName.StartsWith(prefix) ? specName.Substring(prefix.Length) : specName;
}
public static void AddDerivedStruct(TpmStruct baseStruct, UnionMember curMember,
TpmUnion curUnion, string comment = null)
{
string baseTypeName = baseStruct.SpecName;
string newTypeName = baseTypeName + "_" + RemoveEnumPrefix(curMember.SelectorValue.SpecName,
curMember.SelectorValue.EnclosingEnum.SpecName);
if (!TpmTypes.Contains(newTypeName))
{
var newStruct = new TpmStruct(newTypeName,
comment ?? "Auto-derived from " + baseTypeName,
baseStruct);
TpmTypes.Add(newStruct);
}
var s = (TpmStruct)TpmTypes.Lookup(newTypeName);
s.RegisterContainingUnion(curUnion);
// Fix up the union field
curMember.Type = s;
}
static void FixEnumTypeCollisions()
{
List<TpmStruct> toAdd = new List<TpmStruct>();
for (int j = 0; j < TpmTypes.TheTypes.Count; j++)
{
TpmType tp = TpmTypes.TheTypes[j];
if (!(tp is TpmUnion &&
tp.SpecName.IsOneOf(new string[] { "TPMU_PUBLIC_ID", "TPMU_SIGNATURE" })))
{
continue;
}
// See if we have collisions.
// Collided member types are converted into derived types by adding selector name to the base
// type name. Struct that became a base one inherits from all the union interfaces, while
// a derived struct only inherits from the base one and implements interfaces of the unions,
// of which it is a member.
// Base class B provides a union interface implementation only if this union contains a member
// of type B. If a union U contains members of types derived from B, then B's implementation
// of U's interface methods just throws NotImplementedException exception.
TpmUnion u = (TpmUnion)tp;
var dict = new Dictionary<string, UnionMember>();
foreach (UnionMember m in u.Members)
{
string typeName = m.Type.SpecName;
string selectorName = m.SelectorValue.Name;
if (dict.ContainsKey(typeName))
{
// Collision detected.
Debug.WriteLine("Collision in {0} [{1}] -- {2}", u.Name, selectorName, typeName);
TpmStruct baseStruct = (TpmStruct)TpmTypes.Lookup(typeName);
AddDerivedStruct(baseStruct, m, u,
"Auto-derived from " + baseStruct.SpecName +
" to provide unique GetUnionSelector() implementation");
if (dict[typeName] != null)
{
// Create the derived structure for the first occurrence.
AddDerivedStruct(baseStruct, dict[typeName], u);
// But do it only once...
dict[typeName] = null;
}
}
else
{
dict.Add(typeName, m);
}
}
}
} // FixEnumTypeCollisions()
static void FlattenLists()
{
var structs = TpmTypes.TheTypes.Where(x => x is TpmStruct).Cast<TpmStruct>();
foreach (TpmStruct s in structs)
{
List<StructField> toMod = new List<StructField>();
foreach (StructField f in s.Fields)
{
var typeName = f.Type.SpecName;
if (typeName.StartsWith("TPML_") || typeName.StartsWith("TPM2B")) toMod.Add(f);
}
if (toMod.Count() == 0)
continue;
}
}
public static void DoFixups()
{
// Many TPM structs represent a length-prefixed array or structure.
// When such struct is a fields of another (enclosing) structure, get rid of the struct
// wrapper and place the payload array/struct directly as the member of the enclosing struct.
FixTpm2bStructs();
FlattenTaggedStructures();
FixStructsWithEncryptedBuffer();
FlattenLists();
// This command allows session based encryption.
foreach (var s in TpmTypes.Get<TpmStruct>().Where(s => s.IsCmdStruct()))
{
if (s.Fields.Count > s.NumHandles &&
s.Fields[s.NumHandles].MarshalType.IsOneOf(MarshalType.ArrayCount, MarshalType.LengthOfStruct))
{
// This command allows session based encryption.
Debug.Assert(s.Fields.Count > s.NumHandles + 1);
var sizeField = s.Fields[s.NumHandles];
var sizedField = s.Fields[s.NumHandles + 1];
var cmdInfo = s.Info as CmdStructInfo;
cmdInfo.SessEncSizeLen = sizeField.Type.GetSize();
cmdInfo.SessEncValLen = sizeField.MarshalType == MarshalType.LengthOfStruct
? 1 : sizedField.Type.GetSize();
}
}
TpmStruct[] symDefs = { (TpmStruct)TpmTypes.Lookup("TPMT_SYM_DEF"),
(TpmStruct)TpmTypes.Lookup("TPMT_SYM_DEF_OBJECT") };
var fieldTypes = new string[] { "TPM_ALG_ID", "UINT16", "TPM_ALG_ID" };
Debug.Assert(symDefs[0].Fields.Count == symDefs[1].Fields.Count &&
symDefs[0].Fields.Count == fieldTypes.Length);
for (int i = 0; i < fieldTypes.Length; ++i)
{
foreach (var sd in symDefs)
{
sd.Fields[i].MarshalType = MarshalType.Normal;
sd.Fields[i].Type = TpmTypes.Lookup(fieldTypes[i]);
}
}
symDefs[0].Fields[0].Attrs = symDefs[1].Fields[0].Attrs |= StructFieldAttr.TermOnNull;
FixEnumTypeCollisions();
}
} // class TpmTypeTranslations
// This part of the TargetLang class handles names, types and expressions translation
// from their original form in the TPM 2.0 spec to the current target language.
public static partial class TargetLang
{
public static string[,] TypeNameOverrides =
{
{"TPM_ALG_ID", "TpmAlgId"},
{"TPM_CC", "TpmCc"},
{"TPM_RC", "TpmRc"},
{"TPM_ST", "TpmSt"},
{"TPM_RH", "TpmRh"},
{"TPM_RS", "TpmRs"},
{"TPM_HC", "TpmHc"},
{"TPM_SE", "TpmSe"},
{"TPM_HANDLE", "TpmHandle"},
{"TPM2B_PRIVATE", "TpmPrivate"},
{"TPMT_PUBLIC", "TpmPublic"},
{"TPMS_DERIVE", "TpmDerive"},
{"TPMT_HA", "TpmHash"}
};
public static string[,] TypeNamePrefixes =
{
{"TPMU_", "Union"},
{"TPML_", "Array"},
{"TPMA_", "Attr"},
{"TPMS_", ""},
{"TPMT_", ""},
{"TPM_" , ""}
};
internal static string ToCamelStyle(string s)
{
string result = s[0] == '_' ? "_" : "";
string[] words = s.Split(new[] { '_' });
for (int j = 0; j < words.Length; j++)
{
result += Helpers.Capitalize(words[j]);
}
return result;
}
public static string NameToDotNet(string name)
{
if (name.Contains("__"))
return name;
Debug.Assert(!Char.IsDigit(name[0]));
return Helpers.ToCamelStyle(name);
}
public static string TypeToDotNet(string typeName)
{
if (typeName.EndsWith("Response"))
typeName = "TPM2_" + typeName.Substring(0, typeName.Length - 8) + "_RESPONSE";
if (typeName == null || typeName.Contains("__") ||
!typeName.Contains('_') && typeName.ToUpper() != typeName )
{
return typeName;
}
for (int j = 0; j < TypeNameOverrides.GetLength(0); ++j)
{
if (typeName == TypeNameOverrides[j, 0])
return TypeNameOverrides[j, 1];
}
// Drop type name prefix and add the corresponding suffix if necessary.
string qualifier = "";
for (int j = 0; j < TypeNamePrefixes.GetLength(0); ++j)
{
string prefix = TypeNamePrefixes[j, 0];
if (typeName.StartsWith(prefix))
{
typeName = typeName.Substring(prefix.Length);
qualifier = TypeNamePrefixes[j, 1];
break;
}
}
return Helpers.ToCamelStyle(typeName) + qualifier;
}
static string[] ConflictingStdCppMacros = { "NULL", "ERROR", "TRUE", "FALSE" };
static string FixupInvalidName(string name)
{
if (char.IsNumber(name[0]) ||
TargetLang.Cpp && name.IsOneOf(ConflictingStdCppMacros))
{
name = "_" + name;
}
return name;
}
/// <remarks> Old style - pre 2020 (TpmRh.TpmRsPw, EccCurve.TpmEccXxx instead of TpmRh.Pw, EccCurve.Xxx) </remarks>
public static string TransConstantName(string specName, TpmType enclosingType, bool oldStyle = false)
{
string name = TpmTypeTranslations.RemoveEnumPrefix(specName, enclosingType.SpecName, oldStyle);
name = FixupInvalidName(name);
return TargetLang.DotNet ? NameToDotNet(name) : name;
}
public static string TranslateTypeName(TpmType t)
{
var underType = t.StripTypedefs();
if (underType is TpmValueType)
return TargetLang.NameFor(underType.SpecName);
return TargetLang.DotNet ? (underType is TpmUnion ? "I" : "") + TypeToDotNet(underType.SpecName)
: underType.SpecName;
}
/// <summary> Makes constant values specified as arithmetic expressions comply with the current
/// langauge syntax (adds enum type name qualifiers and type casts when necessary, eliminates
/// sizeof() operators when unsupported, etc.) </summary>
public static string TranslateConstExpr(TpmConstExpr ce, TpmEnum enclosingEnum = null)
{
var nameDelimiters = new char[] { ' ', ',', '{', '}',
'(', ')', '+', '-', '<', '*', '/', '`' };
string suffix = TargetLang.Java ? ".toInt()" : "";
string expr = ce.Expr;
string[] tokens = expr.Split(nameDelimiters);
bool sizeofOccurred = false,
commentNeeded = false;
foreach (string token in tokens)
{
if (token.Length == 0)
continue;
if (token == "sizeof")
{
Debug.Assert(!sizeofOccurred);
sizeofOccurred = true;
continue;
}
if (Expression.IsNumber(token))
{
if (token == "00")
{
Debug.Assert(expr == token);
expr = "0";
}
Debug.Assert(!sizeofOccurred);
continue;
}
TpmNamedConstant nc = TpmTypes.LookupConstant(token);
if (enclosingEnum != null && nc?.EnclosingEnum == enclosingEnum)
{
// Members of the enum being processed do not need to be qualified
Debug.Assert(token == nc.SpecName);
expr = expr.Replace(token, nc.Name + suffix);
continue;
}
if (!TpmTypes.ContainsConstant(token))
{
// This must be a type name operand of sizeof()
Debug.Assert(sizeofOccurred);
sizeofOccurred = false;
TpmType t = TpmTypes.Lookup(token);
if (t is TpmStruct || TargetLang.IsOneOf(Lang.Java, Lang.JS, Lang.Py))
{
string sizeofExpr = "sizeof(" + token + ")";
Debug.Assert(expr.Contains(sizeofExpr));
commentNeeded = TargetLang.Py;
string origExpr = TargetLang.Py ? "" : $"/*{sizeofExpr}*/";
expr = expr.Replace(sizeofExpr, $"0x{t.GetSize():X}{origExpr}");
}
else if (TargetLang.DotNet)
{
expr = expr.Replace(token, t.StripTypedefs().Name);
}
}
else
{
nc = TpmTypes.LookupConstant(token);
var translated = nc.QualifiedName + suffix;
if (!TargetLang.IsGenerated(nc.EnclosingEnum))
{
translated = nc.NumericValue.ToString();
if (!TargetLang.Py)
translated += "/*" + token + "*/";
else
commentNeeded = true;
}
expr = expr.Replace(token, translated);
}
} // foreach token
if (TargetLang.DotNet && expr.Contains("<<"))
{
// Shift operator in .Net requires right operand of type 'int' and unsigned left one.
int curPos = 0;
expr = expr.Replace(" << ", "<<");
do
{
int pos = expr.IndexOf("<<", curPos);
if (pos == -1)
break;
curPos = pos + 2 + 6; // Add sizes of "<<" and "(uint)"
while (char.IsLetterOrDigit(expr[--pos]) || expr[pos] == '.')
continue;
expr = expr.Insert(pos + 1, "(uint)");
} while (true);
expr = expr.Replace("<<", " << (int)");
}
if (commentNeeded)
expr += $" # {ce.Expr}";
return expr;
} // TranslateConstExpr()
/// <summary> Constructs language specific representation of the given TPM structure field type
/// for the currently active language. In particular, applies array and byte buffer conventions.
/// </summary>
/// <param name="f"> Structure field metadata extracted from the TPM spec </param>
/// <returns> Language specific representation of the given TPM structure field type </returns>
static string TranslateFieldType(StructField f)
{
string typeName = f.Type.Name;
if (f.IsByteBuffer())
{
if (TargetLang.Cpp)
typeName = "ByteVec";
else
{
if (TargetLang.Node)
return "Buffer";
else if (TargetLang.Py)
return "bytes";
return typeName + "[]";
}
}
else if (f.IsArray())
{
if (TargetLang.Cpp)
typeName = $"vector<{typeName}>";
else
return typeName + "[]";
}
return typeName;
}
} // partial class TargetLang
} // namespace CodeGen