-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCertificate.cs
219 lines (183 loc) · 7.01 KB
/
Certificate.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
using System;
using System.Collections.Generic;
using System.Text;
using Net.Pkcs11Interop.HighLevelAPI;
using Net.Pkcs11Interop.Common;
using SCCrypto.Configuration;
using Org.BouncyCastle.X509;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Digests;
namespace SCCrypto
{
public class Certificate
{
public ObjectHandle ObjectHandle
{
get;
internal set;
}
public List<ObjectAttribute> ObjectAttributes
{
get;
internal set;
}
public ulong? StorageSize
{
get;
internal set;
}
public bool CkaPrivate
{
get;
internal set;
}
public ulong CkaCertificateType
{
get;
internal set;
}
public string CkaLabel
{
get;
internal set;
}
public byte[] CkaId
{
get;
internal set;
}
public byte[] CkaValue
{
get;
internal set;
}
public byte[] CkaSubject
{
get;
internal set;
}
public byte[] CkaSubPubKeyHash
{
get;
internal set;
}
public PubKey PubKey
{
get;
internal set;
}
private void calcPubKeyHash()
{
X509Certificate x509 = this.Get509Certificate();
RsaKeyParameters key = x509.GetPublicKey() as RsaKeyParameters;
byte[] exp = key.Exponent.ToByteArrayUnsigned();
byte[] mod = key.Modulus.ToByteArrayUnsigned();
IDigest hash = new Sha256Digest();
CkaSubPubKeyHash = new byte[hash.GetDigestSize()];
hash.BlockUpdate(exp, 0, exp.Length);
hash.BlockUpdate(mod, 0, mod.Length);
hash.DoFinal(CkaSubPubKeyHash, 0);
}
internal Certificate(ObjectHandle objectHandle, List<ObjectAttribute> objectAttributes, ulong? storageSize)
{
ObjectHandle = objectHandle;
ObjectAttributes = objectAttributes;
StorageSize = storageSize;
}
public static List<Certificate> GetCerts(Slot slot)
{
List<Certificate> certs = new List<Certificate>();
using (Session session = slot.OpenSession(SessionType.ReadOnly))
{
List<ObjectAttribute> searchTemplate = new List<ObjectAttribute>();
searchTemplate.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_CERTIFICATE));
searchTemplate.Add(new ObjectAttribute(CKA.CKA_CERTIFICATE_TYPE, CKC.CKC_X_509));
List<ObjectHandle> foundObjects = session.FindAllObjects(searchTemplate);
foreach (ObjectHandle foundObject in foundObjects)
{
// Read attributes required for sane object presentation
List<ulong> attributes = new List<ulong>();
attributes.Add((ulong)CKA.CKA_PRIVATE);
attributes.Add((ulong)CKA.CKA_CERTIFICATE_TYPE);
attributes.Add((ulong)CKA.CKA_LABEL);
attributes.Add((ulong)CKA.CKA_ID);
attributes.Add((ulong)CKA.CKA_VALUE);
attributes.Add((ulong)CKA.CKA_SUBJECT);
List<ObjectAttribute> requiredAttributes = session.GetAttributeValue(foundObject, attributes);
// Read attributes configured for specific object class and type
Config config = Config.GetDefault();
attributes = new List<ulong>();
foreach (ClassAttribute classAttribute in config.CertificateAttributes.CommonAttributes)
attributes.Add(classAttribute.Value);
ulong certType = requiredAttributes[1].GetValueAsUlong();
if (config.CertificateAttributes.TypeSpecificAttributes.ContainsKey(certType))
foreach (ClassAttribute classAttribute in config.CertificateAttributes.TypeSpecificAttributes[certType])
attributes.Add(classAttribute.Value);
List<ObjectAttribute> configuredAttributes = session.GetAttributeValue(foundObject, attributes);
// Read object storage size
ulong? storageSize = null;
try
{
storageSize = session.GetObjectSize(foundObject);
}
catch
{
}
// Construct info object
Certificate cert = new Certificate(foundObject, configuredAttributes, storageSize)
{
CkaPrivate = requiredAttributes[0].GetValueAsBool(),
CkaCertificateType = requiredAttributes[1].GetValueAsUlong(),
CkaLabel = requiredAttributes[2].GetValueAsString(),
CkaId = requiredAttributes[3].GetValueAsByteArray(),
CkaValue = requiredAttributes[4].GetValueAsByteArray(),
CkaSubject = requiredAttributes[5].GetValueAsByteArray(),
};
cert.calcPubKeyHash();
certs.Add(cert);
}
}
return certs;
}
public X509Certificate Get509Certificate()
{
return new X509CertificateParser().ReadCertificate(this.CkaValue);
}
public ObjectAttribute GetAttribute(CKA Attribute)
{
ObjectAttribute res = null;
for (int i = 0; i < ObjectAttributes.Count; i++)
{
if (ObjectAttributes[i].Type == (ulong)Attribute)
{
res = ObjectAttributes[i];
}
}
return res;
}
// Checks if KeyType of Cert is of the given type
public bool CheckKeyType(Slot slot, CKK keyType)
{
if (this.CkaId.Length == 0)
return false;
bool result;
using (Session session = slot.OpenSession(SessionType.ReadOnly))
{
List<ObjectAttribute> searchTemplate = new List<ObjectAttribute>();
searchTemplate.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PUBLIC_KEY));
searchTemplate.Add(new ObjectAttribute(CKA.CKA_ID,this.CkaId));
searchTemplate.Add(new ObjectAttribute(CKA.CKA_KEY_TYPE,keyType));
List<ObjectHandle> foundObjects = session.FindAllObjects(searchTemplate);
session.FindObjectsInit(searchTemplate);
result = session.FindObjects(1).Count != 0;
session.FindObjectsFinal();
return result;
}
}
public void addKey(PubKey key)
{
this.PubKey = key;
}
}
}