-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateSecretKey.cs
More file actions
200 lines (183 loc) · 7.18 KB
/
Copy pathCreateSecretKey.cs
File metadata and controls
200 lines (183 loc) · 7.18 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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Net.Pkcs11Interop.Common;
using Net.Pkcs11Interop.HighLevelAPI;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Utilities.Encoders;
namespace CryptokiTokenBrowser
{
public partial class CreateSecretKey : Form
{
private string keyType = string.Empty;
private string label = string.Empty;
private string keyValue = string.Empty;
private byte[] keyValueByte = [];
private bool persistantFlag = false;
private bool privateFlag = false;
private bool sensitiveFlag = false;
private bool modifiableFlag = false;
private bool wrapFlag = false;
private bool unwrapFlag = false;
private bool extractableFlag = false;
private bool encryptFlag = false;
private bool decryptFlag = false;
private bool signFlag = false;
private bool verifyFlag = false;
private bool deriveFlag = false;
private bool keyNumHex = false;
private bool keyNumAsc = false;
public IMechanism? selectedMechanism = null;
public ISession? session = null;
public List<IObjectAttribute> flags = new List<IObjectAttribute>();
public CreateSecretKey()
{
InitializeComponent();
}
public static string ComputeKcv(string keyHex, bool useZeroBlock = true)
{
byte[] key = Hex.Decode(keyHex);
byte fillByte = useZeroBlock ? (byte)0x00 : (byte)0x01;
byte[] block = new byte[16];
for (int i = 0; i < block.Length; i++)
block[i] = fillByte;
IBlockCipher engine = new AesEngine();
engine.Init(true, new KeyParameter(key));
byte[] cipher = new byte[16];
engine.ProcessBlock(block, 0, cipher, 0);
return BitConverter.ToString(cipher, 0, 3).Replace("-", string.Empty);
}
private void BtnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void BtnOK_Click(object sender, EventArgs e)
{
if (CbKeyType.SelectedItem == null)
{
MessageBox.Show(
"Please select a key type.",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
return;
}
if (string.IsNullOrWhiteSpace(TxtLabel.Text))
{
MessageBox.Show(
"Please enter a label.",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
return;
}
if (string.IsNullOrWhiteSpace(TxtKeyValue.Text))
{
MessageBox.Show(
"Please enter a key value.",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
return;
}
if (!RadioKValueHex.Checked && !RadioKValueAsc.Checked)
{
MessageBox.Show(
"Please select a key value format (Hex or ASCII).",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
return;
}
keyType = CbKeyType.SelectedItem?.ToString() ?? string.Empty;
label = TxtLabel.Text;
keyValue = TxtKeyValue.Text;
keyValueByte = ConvertUtils.HexStringToBytes(keyValue);
keyNumHex = RadioKValueHex.Checked;
keyNumAsc = RadioKValueAsc.Checked;
persistantFlag = CheckPersistant.Checked;
privateFlag = CheckPrivate.Checked;
sensitiveFlag = CheckSensitive.Checked;
modifiableFlag = CheckModif.Checked;
wrapFlag = CheckWrap.Checked;
unwrapFlag = CheckUnwrap.Checked;
extractableFlag = CheckExtractable.Checked;
encryptFlag = CheckEnc.Checked;
decryptFlag = CheckDec.Checked;
signFlag = CheckSign.Checked;
verifyFlag = CheckVerify.Checked;
deriveFlag = CheckDerive.Checked;
if (session == null)
{
return;
}
string kcv = ComputeKcv(keyValue, true);
byte[] kcvBytes = ConvertUtils.HexStringToBytes(kcv);
flags.Add(
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY)
);
flags.Add(
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_TOKEN, persistantFlag)
);
flags.Add(
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_PRIVATE, privateFlag)
);
flags.Add(
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_SENSITIVE, sensitiveFlag)
);
flags.Add(
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_MODIFIABLE, modifiableFlag)
);
flags.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_WRAP, wrapFlag));
flags.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_UNWRAP, unwrapFlag));
flags.Add(
session.Factories.ObjectAttributeFactory.Create(
CKA.CKA_EXTRACTABLE,
extractableFlag
)
);
flags.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_DERIVE, deriveFlag));
flags.Add(
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_ENCRYPT, encryptFlag)
);
flags.Add(
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_DECRYPT, decryptFlag)
);
flags.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_SIGN, signFlag));
flags.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_VERIFY, verifyFlag));
flags.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_LABEL, label));
flags.Add(
session.Factories.ObjectAttributeFactory.Create(CKA.CKA_KEY_TYPE, CKK.CKK_AES)
);
flags.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_VALUE, keyValueByte));
session.CreateObject(flags);
flags.Clear();
this.DialogResult = DialogResult.OK;
this.Close();
}
private void CreateSecretKey_Load(object sender, EventArgs e)
{
CheckPersistant.Checked = true;
CheckSensitive.Checked = true;
CheckModif.Checked = true;
CheckUnwrap.Checked = true;
CheckExtractable.Checked = true;
CheckEnc.Checked = true;
CheckDec.Checked = true;
CheckSign.Checked = true;
CheckVerify.Checked = true;
}
}
}