This repository has been archived by the owner on Dec 9, 2023. It is now read-only.
/
Pkcs11RsaSignature.cs
566 lines (474 loc) · 23.5 KB
/
Pkcs11RsaSignature.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
/*
* Pkcs11Interop.PDF - Integration layer for Pkcs11Interop
* and iText (iTextSharp) libraries
* Copyright (c) 2013-2017 JWC s.r.o. <http://www.jwc.sk>
* Author: Jaroslav Imrich <jimrich@jimrich.sk>
*
* Licensing for open source projects:
* Pkcs11Interop.PDF is available under the terms of the GNU Affero General
* Public License version 3 as published by the Free Software Foundation.
* Please see <http://www.gnu.org/licenses/agpl-3.0.html> for more details.
*
* Licensing for other types of projects:
* Pkcs11Interop.PDF is available under the terms of flexible commercial license.
* Please contact JWC s.r.o. at <info@pkcs11interop.net> for more details.
*/
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using iTextSharp.text.pdf.security;
using Net.Pkcs11Interop.Common;
using Net.Pkcs11Interop.HighLevelAPI;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Digests;
namespace Net.Pkcs11Interop.PDF
{
/// <summary>
/// PKCS#1 v1.5 RSA signature creator that uses private key stored on PKCS#11 compatible device.
/// In multithreaded environment one instance of this class should be reused by all the threads.
/// </summary>
public class Pkcs11RsaSignature : IExternalSignature, IDisposable
{
#region Variables
/// <summary>
/// Flag indicating whether instance has been disposed
/// </summary>
private bool _disposed = false;
/// <summary>
/// High level PKCS#11 wrapper
/// </summary>
private Pkcs11 _pkcs11 = null;
/// <summary>
/// Logical reader with token used for signing
/// </summary>
private Slot _slot = null;
/// <summary>
/// Master session where user is logged in
/// </summary>
private Session _session = null;
/// <summary>
/// Handle of private key used for signing
/// </summary>
private ObjectHandle _privateKeyHandle = null;
/// <summary>
/// Label (value of CKA_LABEL attribute) of the private key used for signing
/// </summary>
private string _ckaLabel = null;
/// <summary>
/// Identifier (value of CKA_ID attribute) of the private key used for signing
/// </summary>
private byte[] _ckaId = null;
/// <summary>
/// Hash algorihtm used for the signature creation
/// </summary>
private HashAlgorithm _hashAlgorihtm = HashAlgorithm.SHA512;
/// <summary>
/// Raw data of certificate related to private key used for signing
/// </summary>
private byte[] _signingCertificate = null;
/// <summary>
/// Raw data of all certificates stored in device
/// </summary>
private List<byte[]> _allCertificates = null;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the Pkcs11Signature class
/// </summary>
/// <param name="libraryPath">Path to the unmanaged PCKS#11 library</param>
/// <param name="tokenSerial">Serial number of the token (smartcard) that contains signing key. May be null if tokenLabel is specified.</param>
/// <param name="tokenLabel">Label of of the token (smartcard) that contains signing key. May be null if tokenSerial is specified.</param>
/// <param name="pin">PIN for the token (smartcard)</param>
/// <param name="ckaLabel">Label (value of CKA_LABEL attribute) of the private key used for signing. May be null if ckaId is specified.</param>
/// <param name="ckaId">Hex encoded string with identifier (value of CKA_ID attribute) of the private key used for signing. May be null if ckaLabel is specified.</param>
/// <param name="hashAlgorihtm">Hash algorihtm used for the signature creation</param>
public Pkcs11RsaSignature(string libraryPath, string tokenSerial, string tokenLabel, string pin, string ckaLabel, string ckaId, HashAlgorithm hashAlgorihtm)
{
byte[] pinValue = (pin == null) ? null : ConvertUtils.Utf8StringToBytes(pin);
byte[] ckaIdValue = (ckaId == null) ? null : ConvertUtils.HexStringToBytes(ckaId);
InitializePkcs11Signature(libraryPath, tokenSerial, tokenLabel, pinValue, ckaLabel, ckaIdValue, hashAlgorihtm);
}
/// <summary>
/// Initializes a new instance of the Pkcs11Signature class
/// </summary>
/// <param name="libraryPath">Path to the unmanaged PCKS#11 library</param>
/// <param name="tokenSerial">Serial number of the token (smartcard) that contains signing key. May be null if tokenLabel is specified.</param>
/// <param name="tokenLabel">Label of of the token (smartcard) that contains signing key. May be null if tokenSerial is specified.</param>
/// <param name="pin">PIN for the token (smartcard)</param>
/// <param name="ckaLabel">Label (value of CKA_LABEL attribute) of the private key used for signing. May be null if ckaId is specified.</param>
/// <param name="ckaId">Identifier (value of CKA_ID attribute) of the private key used for signing. May be null if ckaLabel is specified.</param>
/// <param name="hashAlgorihtm">Hash algorihtm used for the signature creation</param>
public Pkcs11RsaSignature(string libraryPath, string tokenSerial, string tokenLabel, byte[] pin, string ckaLabel, byte[] ckaId, HashAlgorithm hashAlgorihtm)
{
InitializePkcs11Signature(libraryPath, tokenSerial, tokenLabel, pin, ckaLabel, ckaId, hashAlgorihtm);
}
/// <summary>
/// Initializes a new instance of the Pkcs11Signature class
/// </summary>
/// <param name="libraryPath">Path to the unmanaged PCKS#11 library</param>
/// <param name="tokenSerial">Serial number of the token (smartcard) that contains signing key. May be null if tokenLabel is specified.</param>
/// <param name="tokenLabel">Label of of the token (smartcard) that contains signing key. May be null if tokenSerial is specified.</param>
/// <param name="pin">PIN for the token (smartcard)</param>
/// <param name="ckaLabel">Label (value of CKA_LABEL attribute) of the private key used for signing. May be null if ckaId is specified.</param>
/// <param name="ckaId">Identifier (value of CKA_ID attribute) of the private key used for signing. May be null if ckaLabel is specified.</param>
/// <param name="hashAlgorihtm">Hash algorihtm used for the signature creation</param>
private void InitializePkcs11Signature(string libraryPath, string tokenSerial, string tokenLabel, byte[] pin, string ckaLabel, byte[] ckaId, HashAlgorithm hashAlgorihtm)
{
try
{
if (string.IsNullOrEmpty(libraryPath))
throw new ArgumentNullException("libraryPath");
try
{
_pkcs11 = new Pkcs11(libraryPath, true);
}
catch (Pkcs11Exception ex)
{
if (ex.RV == CKR.CKR_CANT_LOCK)
_pkcs11 = new Pkcs11(libraryPath, false);
else
throw;
}
_slot = FindSlot(tokenSerial, tokenLabel);
if (_slot == null)
throw new TokenNotFoundException(string.Format("Token with serial \"{0}\" and label \"{1}\" was not found", tokenSerial, tokenLabel));
_session = _slot.OpenSession(true);
_session.Login(CKU.CKU_USER, pin);
_privateKeyHandle = FindPrivateKey(ckaLabel, ckaId);
_ckaLabel = ckaLabel;
_ckaId = ckaId;
if (!Enum.IsDefined(typeof(HashAlgorithm), hashAlgorihtm))
throw new ArgumentException("Invalid hash algorithm specified");
_hashAlgorihtm = hashAlgorihtm;
}
catch
{
if (_session != null)
{
_session.Dispose();
_session = null;
}
if (_pkcs11 != null)
{
_pkcs11.Dispose();
_pkcs11 = null;
}
throw;
}
}
#endregion
#region Certificates
/// <summary>
/// Gets the raw data of certificate related to private key used for signing
/// </summary>
/// <returns>Raw data of certificate related to private key used for signing</returns>
public byte[] GetSigningCertificate()
{
if (this._disposed)
throw new ObjectDisposedException(this.GetType().FullName);
// Don't read certificate from token if it has already been read
if (_signingCertificate == null)
{
using (Session session = _slot.OpenSession(true))
{
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));
if (!string.IsNullOrEmpty(_ckaLabel))
searchTemplate.Add(new ObjectAttribute(CKA.CKA_LABEL, _ckaLabel));
if (_ckaId != null)
searchTemplate.Add(new ObjectAttribute(CKA.CKA_ID, _ckaId));
List<ObjectHandle> foundObjects = session.FindAllObjects(searchTemplate);
if (foundObjects.Count < 1)
throw new ObjectNotFoundException(string.Format("Certificate with label \"{0}\" and id \"{1}\" was not found", _ckaLabel, (_ckaId == null) ? null : ConvertUtils.BytesToHexString(_ckaId)));
else if (foundObjects.Count > 1)
throw new ObjectNotFoundException(string.Format("More than one certificate with label \"{0}\" and id \"{1}\" was found", _ckaLabel, (_ckaId == null) ? null : ConvertUtils.BytesToHexString(_ckaId)));
List<CKA> attributes = new List<CKA>();
attributes.Add(CKA.CKA_VALUE);
List<ObjectAttribute> certificateAttributes = session.GetAttributeValue(foundObjects[0], attributes);
_signingCertificate = certificateAttributes[0].GetValueAsByteArray();
}
}
return _signingCertificate;
}
/// <summary>
/// Gets the raw data of all certificates stored in device
/// </summary>
/// <returns>Raw data of all certificates stored in device</returns>
public List<byte[]> GetAllCertificates()
{
if (this._disposed)
throw new ObjectDisposedException(this.GetType().FullName);
// Don't read certificates from token if they have already been read
if (_allCertificates == null)
{
List<byte[]> certificates = new List<byte[]>();
using (Session session = _slot.OpenSession(true))
{
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<CKA> attributes = new List<CKA>();
attributes.Add(CKA.CKA_VALUE);
List<ObjectHandle> foundObjects = session.FindAllObjects(searchTemplate);
foreach (ObjectHandle foundObject in foundObjects)
{
List<ObjectAttribute> objectAttributes = session.GetAttributeValue(foundObject, attributes);
certificates.Add(objectAttributes[0].GetValueAsByteArray());
}
}
_allCertificates = certificates;
}
return _allCertificates;
}
/// <summary>
/// Gets the raw data of certificate related to private key used for signing
/// </summary>
/// <returns>Raw data of certificate related to private key used for signing</returns>
[Obsolete("Use GetSigningCertificate() method instead")]
public byte[] GetCertificateAsByteArray()
{
if (this._disposed)
throw new ObjectDisposedException(this.GetType().FullName);
return GetSigningCertificate();
}
/// <summary>
/// Gets the certificate related to private key used for signing as Org.BouncyCastle.X509.X509Certificate
/// </summary>
/// <returns>Certificate related to private key used for signing as Org.BouncyCastle.X509.X509Certificate</returns>
[Obsolete("Use GetSigningCertificate() method instead")]
public Org.BouncyCastle.X509.X509Certificate GetCertificateAsX509Certificate()
{
if (this._disposed)
throw new ObjectDisposedException(this.GetType().FullName);
return CertUtils.ToBouncyCastleObject(GetSigningCertificate());
}
/// <summary>
/// Gets the certificate related to private key used for signing as System.Security.Cryptography.X509Certificates.X509Certificate2
/// </summary>
/// <returns>Certificate related to private key used for signing as System.Security.Cryptography.X509Certificates.X509Certificate2</returns>
[Obsolete("Use GetSigningCertificate() method instead")]
public X509Certificate2 GetCertificateAsX509Certificate2()
{
if (this._disposed)
throw new ObjectDisposedException(this.GetType().FullName);
return CertUtils.ToDotNetObject(GetSigningCertificate());
}
#endregion
#region iTextSharp.text.pdf.security.IExternalSignature
/// <summary>
/// Returns the hash algorithm
/// </summary>
/// <returns>The hash algorithm ("SHA1", "SHA256", ...)</returns>
public string GetHashAlgorithm()
{
if (this._disposed)
throw new ObjectDisposedException(this.GetType().FullName);
return _hashAlgorihtm.ToString();
}
/// <summary>
/// Returns the encryption algorithm used for signing
/// </summary>
/// <returns>The encryption algorithm ("RSA")</returns>
public string GetEncryptionAlgorithm()
{
if (this._disposed)
throw new ObjectDisposedException(this.GetType().FullName);
return "RSA";
}
/// <summary>
/// Signs the specified message using the encryption algorithm in combination with the digest algorithm
/// </summary>
/// <param name="message">the message you want to be hashed and signed</param>
/// <returns>A signed message digest</returns>
public byte[] Sign(byte[] message)
{
if (this._disposed)
throw new ObjectDisposedException(this.GetType().FullName);
byte[] digest = null;
byte[] digestInfo = null;
switch (_hashAlgorihtm)
{
case HashAlgorithm.SHA1:
digest = ComputeDigest(new Sha1Digest(), message);
digestInfo = CreateDigestInfo(digest, "1.3.14.3.2.26");
break;
case HashAlgorithm.SHA256:
digest = ComputeDigest(new Sha256Digest(), message);
digestInfo = CreateDigestInfo(digest, "2.16.840.1.101.3.4.2.1");
break;
case HashAlgorithm.SHA384:
digest = ComputeDigest(new Sha384Digest(), message);
digestInfo = CreateDigestInfo(digest, "2.16.840.1.101.3.4.2.2");
break;
case HashAlgorithm.SHA512:
digest = ComputeDigest(new Sha512Digest(), message);
digestInfo = CreateDigestInfo(digest, "2.16.840.1.101.3.4.2.3");
break;
}
using (Session session = _slot.OpenSession(true))
using (Mechanism mechanism = new Mechanism(CKM.CKM_RSA_PKCS))
return session.Sign(mechanism, _privateKeyHandle, digestInfo);
}
#endregion
#region Private methods
/// <summary>
/// Finds slot containing the token that matches specified criteria
/// </summary>
/// <param name="tokenSerial">Serial number of token that should be found</param>
/// <param name="tokenLabel">Label of token that should be found</param>
/// <returns>Slot containing the token that matches specified criteria</returns>
private Slot FindSlot(string tokenSerial, string tokenLabel)
{
if (this._disposed)
throw new ObjectDisposedException(this.GetType().FullName);
if (string.IsNullOrEmpty(tokenSerial) && tokenLabel == null)
throw new ArgumentException("Token serial and/or label has to be specified");
List<Slot> slots = _pkcs11.GetSlotList(true);
foreach (Slot slot in slots)
{
TokenInfo tokenInfo = null;
try
{
tokenInfo = slot.GetTokenInfo();
}
catch (Pkcs11Exception ex)
{
if (ex.RV != CKR.CKR_TOKEN_NOT_RECOGNIZED && ex.RV != CKR.CKR_TOKEN_NOT_PRESENT)
throw;
}
if (tokenInfo == null)
continue;
if (!string.IsNullOrEmpty(tokenSerial))
if (0 != String.Compare(tokenSerial, tokenInfo.SerialNumber, StringComparison.InvariantCultureIgnoreCase))
continue;
if (!string.IsNullOrEmpty(tokenLabel))
if (0 != String.Compare(tokenLabel, tokenInfo.Label, StringComparison.InvariantCultureIgnoreCase))
continue;
return slot;
}
return null;
}
/// <summary>
/// Finds private key that matches specified criteria
/// </summary>
/// <param name="ckaLabel">Label (value of CKA_LABEL attribute) of the private key</param>
/// <param name="ckaId">Identifier (value of CKA_ID attribute) of the private key</param>
/// <returns>Handle of private key that matches specified criteria</returns>
private ObjectHandle FindPrivateKey(string ckaLabel, byte[] ckaId)
{
if (this._disposed)
throw new ObjectDisposedException(this.GetType().FullName);
if (string.IsNullOrEmpty(ckaLabel) && ckaId == null)
throw new ArgumentException("Private key label and/or id has to be specified");
using (Session session = _slot.OpenSession(true))
{
List<ObjectAttribute> searchTemplate = new List<ObjectAttribute>();
searchTemplate.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY));
searchTemplate.Add(new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_RSA));
if (!string.IsNullOrEmpty(ckaLabel))
searchTemplate.Add(new ObjectAttribute(CKA.CKA_LABEL, ckaLabel));
if (ckaId != null)
searchTemplate.Add(new ObjectAttribute(CKA.CKA_ID, ckaId));
List<ObjectHandle> foundObjects = session.FindAllObjects(searchTemplate);
if (foundObjects.Count < 1)
throw new ObjectNotFoundException(string.Format("Private key with label \"{0}\" and id \"{1}\" was not found", ckaLabel, (ckaId == null) ? null : ConvertUtils.BytesToHexString(ckaId)));
else if (foundObjects.Count > 1)
throw new ObjectNotFoundException(string.Format("More than one private key with label \"{0}\" and id \"{1}\" was found", ckaLabel, (ckaId == null) ? null : ConvertUtils.BytesToHexString(ckaId)));
return foundObjects[0];
}
}
/// <summary>
/// Creates PKCS#1 DigestInfo
/// </summary>
/// <param name="hash">Hash value</param>
/// <param name="hashOid">Hash algorithm OID</param>
/// <returns>DER encoded PKCS#1 DigestInfo</returns>
private byte[] CreateDigestInfo(byte[] hash, string hashOid)
{
DerObjectIdentifier derObjectIdentifier = new DerObjectIdentifier(hashOid);
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(derObjectIdentifier, null);
DigestInfo digestInfo = new DigestInfo(algorithmIdentifier, hash);
return digestInfo.GetDerEncoded();
}
/// <summary>
/// Computes hash of the data
/// </summary>
/// <param name="digest">Hash algorithm implementation</param>
/// <param name="data">Data that should be processed</param>
/// <returns>Hash of data</returns>
private byte[] ComputeDigest(IDigest digest, byte[] data)
{
if (digest == null)
throw new ArgumentNullException("digest");
if (data == null)
throw new ArgumentNullException("data");
byte[] hash = new byte[digest.GetDigestSize()];
digest.Reset();
digest.BlockUpdate(data, 0, data.Length);
digest.DoFinal(hash, 0);
return hash;
}
#endregion
#region IDisposable
/// <summary>
/// Disposes object
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes object
/// </summary>
/// <param name="disposing">Flag indicating whether managed resources should be disposed</param>
protected virtual void Dispose(bool disposing)
{
if (!this._disposed)
{
// Dispose managed objects
if (disposing)
{
_allCertificates = null;
_signingCertificate = null;
_hashAlgorihtm = HashAlgorithm.SHA512;
_ckaId = null;
_ckaLabel = null;
_privateKeyHandle = null;
if (_session != null)
{
try
{
_session.Logout();
}
catch
{
// Any exceptions can be safely ignored here
}
_session.Dispose();
_session = null;
}
_slot = null;
if (_pkcs11 != null)
{
_pkcs11.Dispose();
_pkcs11 = null;
}
}
// Dispose unmanaged objects
_disposed = true;
}
}
/// <summary>
/// Class destructor that disposes object if caller forgot to do so
/// </summary>
~Pkcs11RsaSignature()
{
Dispose(false);
}
#endregion
}
}