-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmartCard.cs
74 lines (62 loc) · 2.36 KB
/
SmartCard.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
using System;
using System.Collections.Generic;
using System.Text;
using Net.Pkcs11Interop.Common;
using Net.Pkcs11Interop.HighLevelAPI;
using SCCrypto.Configuration;
namespace SCCrypto
{
public class SmartCard
{
public Settings settings
{
get;
internal set;
}
public SmartCard(Settings settings)
{
this.settings = settings;
}
// get available Keys and their slots
public Tuple<List<Certificate>,List<Slot>> getAvailableCertsAndSlots()
{
Pkcs11 pkcs11 = settings.GetLibrary();
List<Slot> slots = pkcs11.GetSlotList(SlotsType.WithTokenPresent);
List<Certificate> certs = new List<Certificate>();
List<Slot> retSlots = new List<Slot>();
List<Certificate> tempCerts;
foreach (Slot slot in slots)
{
tempCerts = Certificate.GetCerts(slot);
foreach (Certificate cert in tempCerts)
{
// Take just Keymanagement Cert
PubKey key = PubKey.GetKey(slot.OpenSession(SessionType.ReadOnly),cert.CkaId);
if (key.CkaWrap && key.CkaEncrypt)
{
// only accept RSA for now
if(key.GetAttribute(CKA.CKA_KEY_TYPE).GetValueAsUlong() == (uint)CKK.CKK_RSA)
{
cert.addKey(key);
certs.Add(cert);
retSlots.Add(slot);
}
}
}
}
return new Tuple<List<Certificate>, List<Slot>>(certs, retSlots);
}
// get privateKeyHandle via KeyID
public static ObjectHandle getKeyID(Session session,byte[] keyID)
{
List<ObjectAttribute> searchTemplate = new List<ObjectAttribute>();
searchTemplate.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY));
searchTemplate.Add(new ObjectAttribute(CKA.CKA_ID, keyID));
session.FindObjectsInit(searchTemplate);
List<ObjectHandle> foundObjects = session.FindObjects(1);
if (foundObjects.Count == 0) return null;
session.FindObjectsFinal();
return foundObjects[0];
}
}
}