mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 602
Expand file tree
/
Copy pathTSPUtil.cs
More file actions
148 lines (131 loc) · 6.31 KB
/
Copy pathTSPUtil.cs
File metadata and controls
148 lines (131 loc) · 6.31 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.GM;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.Rosstandart;
using Org.BouncyCastle.Asn1.TeleTrust;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Cms;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.X509;
using Org.BouncyCastle.X509.Extension;
namespace Org.BouncyCastle.Tsp
{
public class TspUtil
{
// TODO Migrate this information to DigestUtilities
private static readonly Dictionary<DerObjectIdentifier, int> DigestLengths =
new Dictionary<DerObjectIdentifier, int>();
static TspUtil()
{
DigestLengths.Add(PkcsObjectIdentifiers.MD5, 16);
DigestLengths.Add(OiwObjectIdentifiers.IdSha1, 20);
DigestLengths.Add(NistObjectIdentifiers.IdSha224, 28);
DigestLengths.Add(NistObjectIdentifiers.IdSha256, 32);
DigestLengths.Add(NistObjectIdentifiers.IdSha384, 48);
DigestLengths.Add(NistObjectIdentifiers.IdSha512, 64);
DigestLengths.Add(NistObjectIdentifiers.IdSha3_224, 28);
DigestLengths.Add(NistObjectIdentifiers.IdSha3_256, 32);
DigestLengths.Add(NistObjectIdentifiers.IdSha3_384, 48);
DigestLengths.Add(NistObjectIdentifiers.IdSha3_512, 64);
DigestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD128, 16);
DigestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD160, 20);
DigestLengths.Add(TeleTrusTObjectIdentifiers.RipeMD256, 32);
DigestLengths.Add(CryptoProObjectIdentifiers.GostR3411, 32);
DigestLengths.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256, 32);
DigestLengths.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512, 64);
DigestLengths.Add(GMObjectIdentifiers.sm3, 32);
}
/**
* Fetches the signature time-stamp attributes from a SignerInformation object.
* Checks that the MessageImprint for each time-stamp matches the signature field.
* (see RFC 3161 Appendix A).
*
* @param signerInfo a SignerInformation to search for time-stamps
* @return a collection of TimeStampToken objects
* @throws TSPValidationException
*/
public static IList<TimeStampToken> GetSignatureTimestamps(SignerInformation signerInfo)
{
var timestamps = new List<TimeStampToken>();
Asn1.Cms.AttributeTable unsignedAttributes = signerInfo.UnsignedAttributes;
if (unsignedAttributes != null)
{
foreach (Asn1.Cms.Attribute tsAttr in unsignedAttributes.GetAll(
PkcsObjectIdentifiers.IdAASignatureTimeStampToken))
{
foreach (Asn1Encodable asn1 in tsAttr.AttrValues)
{
try
{
Asn1.Cms.ContentInfo contentInfo = Asn1.Cms.ContentInfo.GetInstance(
asn1.ToAsn1Object());
TimeStampToken timeStampToken = new TimeStampToken(contentInfo);
TimeStampTokenInfo tstInfo = timeStampToken.TimeStampInfo;
byte[] expectedDigest = DigestUtilities.CalculateDigest(tstInfo.MessageImprintAlgOid,
signerInfo.GetSignature());
if (!Arrays.FixedTimeEquals(expectedDigest, tstInfo.GetMessageImprintDigest()))
throw new TspValidationException("Incorrect digest in message imprint");
timestamps.Add(timeStampToken);
}
catch (SecurityUtilityException)
{
throw new TspValidationException("Unknown hash algorithm specified in timestamp");
}
catch (Exception)
{
throw new TspValidationException("Timestamp could not be parsed");
}
}
}
}
return timestamps;
}
/**
* Validate the passed in certificate as being of the correct type to be used
* for time stamping. To be valid it must have an ExtendedKeyUsage extension
* which has a key purpose identifier of id-kp-timeStamping.
*
* @param cert the certificate of interest.
* @throws TspValidationException if the certicate fails on one of the check points.
*/
public static void ValidateCertificate(X509Certificate cert)
{
if (cert.Version != 3)
throw new ArgumentException("Certificate must have an ExtendedKeyUsage extension.");
ExtendedKeyUsage eku;
try
{
eku = cert.GetExtension(X509Extensions.ExtendedKeyUsage, ExtendedKeyUsage.GetInstance);
}
catch (IOException)
{
throw new TspValidationException("cannot process ExtendedKeyUsage extension");
}
if (eku == null)
throw new TspValidationException("Certificate must have an ExtendedKeyUsage extension.");
if (!cert.GetCriticalExtensionOids().Contains(X509Extensions.ExtendedKeyUsage.Id))
throw new TspValidationException("Certificate must have an ExtendedKeyUsage extension marked as critical.");
if (!eku.HasKeyPurposeId(KeyPurposeID.id_kp_timeStamping) || eku.Count != 1)
throw new TspValidationException("ExtendedKeyUsage not solely time stamping.");
}
internal static int GetDigestLength(DerObjectIdentifier digestAlgOid)
{
if (!DigestLengths.TryGetValue(digestAlgOid, out int length))
throw new TspException("digest algorithm cannot be found.");
return length;
}
internal static IList<DerObjectIdentifier> GetExtensionOids(X509Extensions extensions)
{
return extensions == null
? new List<DerObjectIdentifier>()
: new List<DerObjectIdentifier>(extensions.GetExtensionOids());
}
}
}