forked from Nitrokey/nitrokey-keepass-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pkcs11.cs
234 lines (218 loc) · 8.28 KB
/
Pkcs11.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
using System;
using System.Collections.Generic;
using Net.Pkcs11Interop.HighLevelAPI;
using Net.Pkcs11Interop.Common;
using CryptokiKeyProvider.Forms;
using System.Windows.Forms;
using KeePass.UI;
namespace CryptokiKeyProvider
{
public class Pkcs11
{
private static Net.Pkcs11Interop.HighLevelAPI.Pkcs11 pk;
public struct keyfile
{
public ObjectHandle handle;
public System.UInt32 slotid;
public string label;
public string token_name;
}
private static List<keyfile> keyfiles;
public static Session session;
public static void init (String lib)
{
pk = new Net.Pkcs11Interop.HighLevelAPI.Pkcs11(lib,false);
//Console.WriteLine("OK");
}
public static Slot slots(){
if (pk.GetSlotList(true).Count == 1)
return pk.GetSlotList(true)[0];
else return pk.GetSlotList(true)[1];
}
public static void Login(String password){
Slot slot = slots();
try{
//try logout any session open
Logout();}catch(Exception ex){
}
session = slot.OpenSession(false);
//Login as normal user
session.Login(Net.Pkcs11Interop.Common.CKU.CKU_USER, password);
}
public static bool loginRequired(System.UInt32 slotId)
{
try
{
TokenInfo token_info = slots().GetTokenInfo();
if ((token_info.TokenFlags.Flags & CKF.CKF_LOGIN_REQUIRED) == CKF.CKF_LOGIN_REQUIRED)
return true;
else
return false;
}
catch (Exception)
{
throw;
}
}
public static void LoginIfRequired(UInt32 slotId)
{
try
{
//SessionInfo session_info = session.GetSessionInfo();
TokenInfo token_info = slots().GetTokenInfo();
if ( (token_info.TokenFlags.Flags & CKF.CKF_LOGIN_REQUIRED) == CKF.CKF_LOGIN_REQUIRED)
{
if ((token_info.TokenFlags.Flags & CKF.CKF_PROTECTED_AUTHENTICATION_PATH) == CKF.CKF_PROTECTED_AUTHENTICATION_PATH)
{
Login("");
}
else
{
CkpPromtForm dialog = new CkpPromtForm();
if (UIUtil.ShowDialogAndDestroy(dialog) != DialogResult.OK)
return;
Login(dialog.pin);
}
}
}
catch (Exception)
{
throw;
}
}
public static void createKeyfile(string label, byte[] data)
{
//Slot slot = slots();
TokenInfo token_info = slots().GetTokenInfo();
if ( (token_info.TokenFlags.Flags & CKF.CKF_LOGIN_REQUIRED) == CKF.CKF_LOGIN_REQUIRED)
{
if ((token_info.TokenFlags.Flags & CKF.CKF_PROTECTED_AUTHENTICATION_PATH) == CKF.CKF_PROTECTED_AUTHENTICATION_PATH)
{
Login("");
}
else
{
CkpPromtForm dialog = new CkpPromtForm();
if (UIUtil.ShowDialogAndDestroy(dialog) != DialogResult.OK)
return;
Login(dialog.pin);
}
}
List<ObjectAttribute> objectAttributes = new List<ObjectAttribute>();
objectAttributes.Add(new ObjectAttribute(Net.Pkcs11Interop.Common.CKA.CKA_CLASS, (uint)Net.Pkcs11Interop.Common.CKO.CKO_DATA));
objectAttributes.Add(new ObjectAttribute(Net.Pkcs11Interop.Common.CKA.CKA_TOKEN, new byte[] { 0x01 }));
objectAttributes.Add(new ObjectAttribute(Net.Pkcs11Interop.Common.CKA.CKA_APPLICATION, "CryptokiKeyProvider"));
objectAttributes.Add(new ObjectAttribute(Net.Pkcs11Interop.Common.CKA.CKA_LABEL, "CryptokiKeyProvider"));
objectAttributes.Add(new ObjectAttribute(Net.Pkcs11Interop.Common.CKA.CKA_VALUE, data));
// Create object
session.CreateObject(objectAttributes);
//session.Logout();
}
public static List<keyfile> read_allkeyfiles(String path) {
init(path);
List<keyfile> _keyfiles = new List<keyfile>();
Slot slot = slots();
//Console.WriteLine("Get Slots");
//SessionInfo session_info = session.GetSessionInfo();
TokenInfo token_info = slots().GetTokenInfo();
if ( (token_info.TokenFlags.Flags & CKF.CKF_LOGIN_REQUIRED) == CKF.CKF_LOGIN_REQUIRED)
{
if ((token_info.TokenFlags.Flags & CKF.CKF_PROTECTED_AUTHENTICATION_PATH) == CKF.CKF_PROTECTED_AUTHENTICATION_PATH)
{
Login("");
}
else
{
CkpPromtForm dialog = new CkpPromtForm();
if (UIUtil.ShowDialogAndDestroy(dialog) != DialogResult.OK)
return null;
Login(dialog.pin);
}
}
//Console.WriteLine("Login");
//using (Session session = slot.OpenSession(false)){
// session.Login(Net.Pkcs11Interop.Common.CKU.CKU_USER,password);
List<ObjectAttribute> objectAttributes = new List<ObjectAttribute>();
objectAttributes.Add(new ObjectAttribute(Net.Pkcs11Interop.Common.CKA.CKA_CLASS, (uint)Net.Pkcs11Interop.Common.CKO.CKO_DATA));
objectAttributes.Add(new ObjectAttribute(Net.Pkcs11Interop.Common.CKA.CKA_TOKEN, true));
List<ObjectHandle> found = session.FindAllObjects(objectAttributes);
foreach(ObjectHandle hanlder in found){
List<Net.Pkcs11Interop.Common.CKA> attr = new List<Net.Pkcs11Interop.Common.CKA>();
attr.Add(Net.Pkcs11Interop.Common.CKA.CKA_LABEL);
string label = session.GetAttributeValue(hanlder, attr)[0].GetValueAsString();
_keyfiles.Add(new keyfile{
handle = hanlder,
label = label,
slotid = slot.GetTokenInfo().SlotId,
token_name = slot.GetTokenInfo().Label,
});
}
//}
return _keyfiles;
}
public static byte[] GetAttributeValue(ObjectHandle hObject, Net.Pkcs11Interop.Common.CKA type)
{
//byte[] value_array = new byte[template.ulValueLen];
List<Net.Pkcs11Interop.Common.CKA> attr = new List<Net.Pkcs11Interop.Common.CKA>();
attr.Add(type);
return session.GetAttributeValue(hObject,attr)[0].GetValueAsByteArray();
//return null;
}
public static void Logout(){
session.Logout();
session.CloseSession();
}
public static void deleteDataObject(keyfile key){
try{
//LoginIfRequired(key.slotid);
session.DestroyObject(key.handle);
Logout();
}catch(Exception ex){
MessageBox.Show(ex.Message);
}
}
public static byte[] pkcs11_read_key(string path, string label)
{
try
{
keyfiles = read_allkeyfiles(path);
foreach (keyfile keyfile in keyfiles) {
if (keyfile.label == label)
{
byte[] key = GetAttributeValue(keyfile.handle, CKA.CKA_VALUE);
Logout();
//CloseSession();
return key;
}
}
}
catch (Exception)
{
Logout();
//CloseSession();
throw;
}
Logout();
//CloseSession();
return null;
}
/*public static byte[] pkcs11_read_key(string password)
{
try
{
keyfiles keyfile = read_allkeyfiles(password);
Slot slot = slots();
using(Session session = slot.OpenSession(false)){
foreach (keyfiles keyfile in keyfiles) {
byte[] key = GetAttributeValue(keyfile.handle, CKA_VALUE);
return key;
}
}
}
catch (Exception)
{
}
return null;
}*/
}
}