forked from microsoft/TSS.MSR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCertifyX509Support.cs
473 lines (418 loc) · 20.5 KB
/
CertifyX509Support.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
/*
* 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 Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Operators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
using System;
using System.Diagnostics;
using Tpm2Lib;
using Tpm2Tester;
namespace Tpm2TestSuite
{
/// <summary>
/// Contains static methods supporting the TPM2_CertifyX509() command
/// </summary>
static class X509Helpers
{
/// <summary>
/// Makes a minimal partial certificate for a signing key
/// </summary>
/// <returns></returns>
internal static PartialCertificate
MakePartialCert(TpmPublic pub, string issuer = null, string subject = null,
DateTime? notBefore = null, DateTime? notAfter = null)
{
return MakePartialCert(pub.objectAttributes, pub.type, TpmHelper.GetSchemeHash(pub), issuer, subject, notBefore, notAfter);
}
/// <summary>
/// Makes a minimal partial certificate for a signing key
/// </summary>
/// <returns></returns>
internal static PartialCertificate
MakePartialCert(ObjectAttr keyAttrs,
TpmAlgId keyType = TpmAlgId.None, TpmAlgId schemeHash = TpmAlgId.None,
string issuer = null, string subject = null,
DateTime? notBefore = null, DateTime? notAfter = null)
{
AlgorithmIdentifier algID = null;
if (keyType != TpmAlgId.None && schemeHash != TpmAlgId.None)
algID = GetAlgId(keyType, schemeHash);
return MakePartialCert(GetKeyUsage(keyAttrs), algID, issuer, subject, notBefore, notAfter);
}
/// <summary>
/// Makes a minimal partial certificate for a signing key
/// </summary>
/// <returns></returns>
internal static PartialCertificate
MakePartialCert(X509KeyUsage keyUsage, AlgorithmIdentifier algID = null,
string issuer = null, string subject = null,
DateTime? notBefore = null, DateTime? notAfter = null)
{
X509ExtensionsGenerator g = new X509ExtensionsGenerator();
g.AddExtension(X509Extensions.KeyUsage, true, keyUsage);
return new PartialCertificate()
{
Issuer = new X509Name(issuer ?? "CN=TPM Test Issuer,O=TPM Test Suite"),
NotBefore = notBefore ?? new DateTime(2000, 1, 1),
NotAfter = notAfter ?? new DateTime(2999, 12, 31),
Subject = new X509Name(subject ?? "CN=TPM X509 CA,O=MSFT"),
SigAlgID = algID,
Extensions = g.Generate()
};
}
internal static X509KeyUsage GetKeyUsage(ObjectAttr keyAttrs)
{
int usage = 0;
if (keyAttrs.HasFlag(ObjectAttr.Sign))
usage |= X509KeyUsage.DigitalSignature;
if (keyAttrs.HasFlag(ObjectAttr.Decrypt))
if (keyAttrs.HasFlag(ObjectAttr.Restricted))
usage |= X509KeyUsage.KeyEncipherment;
else
usage |= X509KeyUsage.DataEncipherment;
if (keyAttrs.HasFlag(ObjectAttr.FixedTPM))
usage |= X509KeyUsage.NonRepudiation;
return new X509KeyUsage(usage);
}
internal static AlgorithmIdentifier GetAlgId(TpmAlgId keyType, TpmAlgId schemeHash)
{
DerObjectIdentifier doid = null;
if (keyType == TpmAlgId.Rsa)
{
switch (schemeHash)
{
case TpmAlgId.Sha1: doid = PkcsObjectIdentifiers.Sha1WithRsaEncryption; break;
case TpmAlgId.Sha256: doid = PkcsObjectIdentifiers.Sha256WithRsaEncryption; break;
case TpmAlgId.Sha384: doid = PkcsObjectIdentifiers.Sha384WithRsaEncryption; break;
case TpmAlgId.Sha512: doid = PkcsObjectIdentifiers.Sha512WithRsaEncryption; break;
default: return null;
}
}
else if (keyType == TpmAlgId.Ecc)
{
switch (schemeHash)
{
case TpmAlgId.Sha1: doid = X9ObjectIdentifiers.ECDsaWithSha1; break;
case TpmAlgId.Sha256: doid = X9ObjectIdentifiers.ECDsaWithSha256; break;
case TpmAlgId.Sha384: doid = X9ObjectIdentifiers.ECDsaWithSha384; break;
case TpmAlgId.Sha512: doid = X9ObjectIdentifiers.ECDsaWithSha512; break;
default: return null;
}
}
else
return null;
return new AlgorithmIdentifier(doid);
}
/// <summary>
/// To be called after the TPM has returned the addedTo part of the certificate and the signature. Assembles
/// partialCert, addedTo, and the signature to form an actual X.509 certificate encapsulated in a Bouncy Castle
/// X509Certificate
/// </summary>
/// <param name="partialCert"></param>
/// <param name="addedToCertificate"></param>
/// <param name="signature"></param>
/// <returns></returns>
internal static X509Certificate AssembleCertificate(
PartialCertificate partialCert, AddedToCertificate addedToCertificate,
byte[] signatureBytes)
{
Debug.Assert(addedToCertificate.Version.ToString() == "2");
var signature = new DerBitString(signatureBytes);
var sigAlgID = partialCert.SigAlgID ?? addedToCertificate.SigAlgID;
// Assemble TBS. Start with a vector which we will later convert to a sequence
Asn1EncodableVector tbsContents = new Asn1EncodableVector();
tbsContents.Add(new DerTaggedObject(0, new DerInteger(BigInteger.ValueOf(2))));
tbsContents.Add(addedToCertificate.SerialNumber);
tbsContents.Add(sigAlgID);
tbsContents.Add(partialCert.Issuer);
tbsContents.Add(new CertificateValidity(partialCert.NotBefore, partialCert.NotAfter));
tbsContents.Add(partialCert.Subject);
tbsContents.Add(addedToCertificate.SubjectPublicKeyInfo);
// Add optional components
if (partialCert.IssuerUniqueId != null) tbsContents.Add(new DerTaggedObject(false, 1, new DerBitString(partialCert.IssuerUniqueId)));
if (partialCert.SubjectUniqueId != null) tbsContents.Add(new DerTaggedObject(false, 2, new DerBitString(partialCert.SubjectUniqueId)));
if (partialCert.Extensions != null) tbsContents.Add(new DerTaggedObject(3, partialCert.Extensions.ToAsn1Object()));
// Convert the vector to an ASN SEQUENCE
var tbsCertificate = new DerSequence(tbsContents);
// assemble the certificate. Start with the components as a vector
Asn1EncodableVector certContents = new Asn1EncodableVector();
certContents.Add(tbsCertificate);
certContents.Add(sigAlgID);
certContents.Add(signature);
// Convert to a SEQUENCE. certSequence will contain the DER encoded certificate
var certSequence = new DerSequence(certContents);
// Convert to a first-class BC X509Certificate
var certBytes = certSequence.GetEncoded();
var cert = new X509CertificateParser().ReadCertificate(certSequence.GetEncoded());
// and return it
return cert;
}
/// <summary>
/// Simulates the crytographic operations that a TPM would perform in X509Certify()
/// (Mostly to test the tester)
/// </summary>
/// <param name="partialCert"></param>
/// <param name="publicKeyToCertify"></param>
/// <param name="signingKey"></param>
/// <returns>Tuple (X509Certificate CompleteCertificate, AddedToCertificate AddedTo)</returns>
internal static (X509Certificate, AddedToCertificate)
SimulateX509Certify(PartialCertificate partialCert, AsymmetricKeyParameter publicKeyToCertify,
AsymmetricKeyParameter signingKey, string signingAlgorithm)
{
// We can simulate ECDSA/SHA256 and RSA/SHA256
ISignatureFactory signatureFactory;
DerObjectIdentifier sigAlgOid;
if (signingKey is ECPrivateKeyParameters)
{
sigAlgOid = X9ObjectIdentifiers.ECDsaWithSha256;
}
else
{
sigAlgOid = PkcsObjectIdentifiers.Sha256WithRsaEncryption;
}
//signatureFactory = new Asn1SignatureFactory(sigAlgOid.ToString(), signingKey);
signatureFactory = new Asn1SignatureFactory(sigAlgOid.ToString(), signingKey);
// Make the certificate
var certGenerator = new X509V3CertificateGenerator();
certGenerator.SetIssuerDN(partialCert.Issuer);
certGenerator.SetSubjectDN(partialCert.Subject);
certGenerator.SetSerialNumber(BigInteger.ValueOf(1));
certGenerator.SetNotBefore(partialCert.NotBefore);
certGenerator.SetNotAfter(partialCert.NotAfter);
certGenerator.SetPublicKey(publicKeyToCertify);
if (partialCert.SubjectUniqueId != null) certGenerator.SetSubjectUniqueID(ByteArrayToBoolArray(partialCert.SubjectUniqueId));
if (partialCert.IssuerUniqueId != null) certGenerator.SetIssuerUniqueID(ByteArrayToBoolArray(partialCert.IssuerUniqueId));
// process the extensions. Note that this will not preserve the order
var extensions = partialCert.Extensions;
if (extensions != null)
{
foreach (var critExtOid in extensions.GetCriticalExtensionOids())
{
certGenerator.AddExtension(critExtOid.ToString(), true, extensions.GetExtension(critExtOid).GetParsedValue());
}
foreach (var nonCritExtOid in extensions.GetNonCriticalExtensionOids())
{
certGenerator.AddExtension(nonCritExtOid.ToString(), false, extensions.GetExtension(nonCritExtOid).GetParsedValue());
}
}
// and sign to make the cert
var cert = certGenerator.Generate(signatureFactory);
// take things apart again for addedToCert
var subjectPubKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKeyToCertify);
// get the exact bytes for the signature algorithm
var tbsSequence = ((DerSequence)DerSequence.FromByteArray(cert.GetTbsCertificate()));
var sigAlgBytes = tbsSequence[2];
AddedToCertificate addedTo = new AddedToCertificate()
{
Version = new DerInteger(BigInteger.ValueOf(cert.Version)),
SerialNumber = new DerInteger(cert.SerialNumber),
SigAlgID = AlgorithmIdentifier.GetInstance(sigAlgBytes),
SubjectPublicKeyInfo = subjectPubKeyInfo
};
return (cert, addedTo);
}
/// <summary>
/// Validate the tester by emulating the actions of the TPM
/// </summary>
internal static void TestTester()
{
var random = new SecureRandom();
// Algorithms to test
var algInfo = new[] {
new { genner = (IAsymmetricCipherKeyPairGenerator) new ECKeyPairGenerator(), strength = 256, signingAlgorithm = "SHA256WITHECDSA" },
new { genner = (IAsymmetricCipherKeyPairGenerator) new RsaKeyPairGenerator(), strength = 2048, signingAlgorithm = "SHA256WITHRSAENCRYPTION"}
};
foreach (var keyType in algInfo)
{
// Make a two keys of the specified algorithm
var keyGenerationParameters = new KeyGenerationParameters(random, keyType.strength);
keyType.genner.Init(keyGenerationParameters);
var signingKey = keyType.genner.GenerateKeyPair();
var toBeCertifiedKeyPair = keyType.genner.GenerateKeyPair();
// Make the partial certificate that is input to the TPM
PartialCertificate partialCert = MakePartialCert(ObjectAttr.Sign,
keyType.genner is ECKeyPairGenerator ? TpmAlgId.Ecc : TpmAlgId.Rsa, TpmAlgId.Sha256);
// Simulate the actions of the TPM. This returns both the full and partial (AddedTo) certificate
// The full certitificate is just for debugging
var certTuple /*(CompleteCertificate, AddedTo)*/ =
SimulateX509Certify(partialCert, toBeCertifiedKeyPair.Public, signingKey.Private, keyType.signingAlgorithm);
X509Certificate CompleteCertificate = certTuple.Item1;
AddedToCertificate AddedTo = certTuple.Item2;
// Is the full certificate OK (it certainly should be)
CompleteCertificate.Verify(signingKey.Public);
// Debugging help...
DebugPrintHex(CompleteCertificate.GetEncoded(), "ActualCert");
// When using TPM2_CertifyX509() the TPM does not return the whole certificate: it returns the
// parts of the certificate that were not originally provided in partialCertificate in a data
// structure called AddedTo, as well as the signature. The caller/TSS has to recosntruct
// the full certificate.
var signature = CompleteCertificate.GetSignature();
var finishedCert = AssembleCertificate(partialCert, AddedTo, signature);
DebugPrintHex(finishedCert.GetEncoded(), "AssembledCert");
// sanity check that we can parse a DER encoded AddedTo (this is wha the TPM will return.)
var addedToBytes = AddedTo.GetDerEncoded();
var reconstructedAddedTo = AddedToCertificate.FromDerEncoding(addedToBytes);
AssertByteArraysTheSame(addedToBytes, reconstructedAddedTo.ToAsn1Object().GetDerEncoded());
// Sanity
AssertByteArraysTheSame(CompleteCertificate.GetEncoded(), finishedCert.GetEncoded());
// and more sanity
finishedCert.Verify(signingKey.Public);
}
}
/// <summary>
/// Debugging support
/// </summary>
/// <param name="obj"></param>
/// <param name="tag"></param>
public static void DebugPrintHex(Asn1Encodable obj, string tag = "")
{
DebugPrintHex(obj.GetDerEncoded(), tag);
}
/// <summary>
/// Debugging support
/// </summary>
/// <param name="bin"></param>
/// <param name="tag"></param>
public static void DebugPrintHex(byte[] bin, string tag = "")
{
Debug.WriteLine(tag);
foreach (var b in bin)
{
Debug.Write(b.ToString("X2"));
}
Debug.WriteLine("");
}
/// <summary>
/// Deugging support
/// </summary>
/// <param name="x0"></param>
/// <param name="x1"></param>
public static void AssertByteArraysTheSame(byte[] x0, byte[] x1)
{
var min = Math.Min(x0.Length, x1.Length);
if (x0.Length != x1.Length)
{
Debug.WriteLine($"Arrays not the same length {x0.Length}, {x1.Length}");
}
Debug.Assert(x0.Length == x1.Length);
for (int j = 0; j < min; j++)
{
Debug.Assert(x0[j] == x1[j]);
}
}
/// <summary>
/// The Bouncy Castle certificate generator needs a bool[] for Subject and Issuer UniqueIdentifier
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
static bool[] ByteArrayToBoolArray(byte[] x)
{
var ret = new bool[x.Length * 8];
for (int j = 0; j < x.Length; j++)
{
for (int bit = 0; bit < 7; bit++)
{
int bitPos = 7 - bit;
byte bitVal = (byte)((byte)1 << bitPos);
if ((x[j] & bitVal) == 0) continue;
ret[j] = true;
}
}
return ret;
}
} // static class X509Helpers
/// <summary>
/// A DER-encoded PartialCertificate must be input to TPM2_X509Certify()
/// </summary>
public class PartialCertificate : Asn1Encodable
{
public X509Name Issuer;
public DateTime NotBefore;
public DateTime NotAfter;
public X509Name Subject;
public AlgorithmIdentifier SigAlgID;
public byte[] IssuerUniqueId; // todo: this won't work for non-octets (i.e. padding!=0)
public byte[] SubjectUniqueId;
public X509Extensions Extensions;
public override Asn1Object ToAsn1Object()
{
// Construct a Asn1Vector with the mandadory and optional contents
Asn1EncodableVector partialCertContents = new Asn1EncodableVector();
if (SigAlgID != null)
partialCertContents.Add(SigAlgID);
partialCertContents.Add(Issuer);
partialCertContents.Add(new CertificateValidity(NotBefore, NotAfter).ToAsn1Object());
partialCertContents.Add(Subject);
if (IssuerUniqueId != null) partialCertContents.Add(new DerTaggedObject(1, new DerBitString(IssuerUniqueId)));
if (SubjectUniqueId != null) partialCertContents.Add(new DerTaggedObject(2, new DerBitString(SubjectUniqueId)));
partialCertContents.Add(new DerTaggedObject(3, Extensions));
// Encode it as a DER sequence
var partialCert = new DerSequence(partialCertContents);
return partialCert;
}
}
/// <summary>
/// TPM2_CertifyX509() returns an AddedToCertificate and the actual signature. The completed certificate must
/// be assembled by the caller.
/// </summary>
public class AddedToCertificate : Asn1Encodable
{
public DerInteger Version;
public DerInteger SerialNumber;
public AlgorithmIdentifier SigAlgID;
public SubjectPublicKeyInfo SubjectPublicKeyInfo;
public override Asn1Object ToAsn1Object()
{
Asn1EncodableVector addeddToContents = new Asn1EncodableVector();
var addedElts = SigAlgID == null
? new Asn1Encodable[] { new DerTaggedObject(0, Version), SerialNumber, SubjectPublicKeyInfo }
: new Asn1Encodable[] { new DerTaggedObject(0, Version), SerialNumber, SigAlgID, SubjectPublicKeyInfo };
addeddToContents.Add(addedElts);
var addedToSequence = new DerSequence(addeddToContents);
return addedToSequence;
}
/// <summary>
/// Parses the AddedTo data that is returned from TPM2_CertifyX509()
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static AddedToCertificate FromDerEncoding(byte[] data)
{
var ret = new AddedToCertificate();
var sequence = (DerSequence)DerSequence.FromByteArray(data);
var taggedVersion = (DerTaggedObject)(sequence[0]);
Debug.Assert(taggedVersion.TagNo == 0);
ret.Version = (DerInteger)taggedVersion.GetObject();
ret.SerialNumber = (DerInteger)sequence[1];
int i = 2;
if (sequence.Count > 3)
ret.SigAlgID = AlgorithmIdentifier.GetInstance(sequence[i++]);
ret.SubjectPublicKeyInfo = SubjectPublicKeyInfo.GetInstance(sequence[i++]);
return ret;
}
} // class AddedToCertificate
public class CertificateValidity : Asn1Encodable
{
public DateTime NotBefore;
public DateTime NotAfter;
public CertificateValidity(DateTime notBefore, DateTime notAfter)
{
NotBefore = notBefore;
NotAfter = notAfter;
}
public override Asn1Object ToAsn1Object()
{
return new DerSequence(new Time(NotBefore), new Time(NotAfter));
}
}
}