-
Notifications
You must be signed in to change notification settings - Fork 15
/
object.cpp
320 lines (241 loc) · 10.9 KB
/
object.cpp
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
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <openssl/x509.h>
#include <openssl/md5.h>
#include <boost/lexical_cast.hpp>
#include "tools.h"
#include "storage.h"
#include "object.h"
const CK_BBOOL bool_true = CK_TRUE;
const CK_BBOOL bool_false = CK_FALSE;
Attributes data_object_t::operator()(descriptor_p desc, const Attributes& attributes) const
{
const CK_OBJECT_CLASS klass = CKO_DATA;
const CK_FLAGS flags = 0;
Attributes attrs = {
create_object(CKA_CLASS, klass),
//Common Storage Object Attributes
create_object(CKA_TOKEN, bool_true),
create_object(CKA_PRIVATE, bool_true),
create_object(CKA_MODIFIABLE,bool_false),
create_object(CKA_LABEL, desc->item.filename),
//Data Object Attributes
//create_object(CKA_APPLICATION, desc->id),
create_object(CKA_OBJECT_ID, boost::lexical_cast<std::string>(desc->id)),
//create_object(CKA_VALUE, desc->id), //read when needed
};
//keys in attrs takes precedence with attributes
attrs.insert(attributes.begin(), attributes.end());
return attrs;
}
Attributes public_key_t::operator()(descriptor_p desc, const Attributes& attributes) const
{
const Attributes base_attrs = data_object_t::operator()(desc, attributes);
const CK_OBJECT_CLASS klass = CKO_PUBLIC_KEY;
const CK_MECHANISM_TYPE mech_type = CKM_RSA_X_509;
CK_ULONG id = desc->id;
Attributes attrs = {
create_object(CKA_CLASS, klass),
//Common Storage Object Attributes
create_object(CKA_TOKEN, bool_true),
create_object(CKA_PRIVATE, bool_false),
create_object(CKA_MODIFIABLE,bool_false),
create_object(CKA_LABEL, desc->item.filename),
//Common Key Attributes
//create_object(CKA_KEY_TYPE, type),
create_object(CKA_ID, id),
//create_object(CKA_START_DATE, id),
//create_object(CKA_END_DATE, id),
create_object(CKA_DERIVE, bool_false),
create_object(CKA_LOCAL, bool_false),
create_object(CKA_KEY_GEN_MECHANISM, mech_type),
//Common Public Key Attributes
//create_object(CKA_SUBJECT, bool_true),
create_object(CKA_ENCRYPT, bool_true),
create_object(CKA_VERIFY, bool_true),
//create_object(CKA_VERIFY_RECOVER, bool_false),
//create_object(CKA_TRUSTED10, bool_true),
//create_object(CKA_WRAP_TEMPLATE , bool_true),
/////////////
};
//keys in attrs takes precedence with attributes
attrs.insert(base_attrs.begin(), base_attrs.end());
return attrs;
}
Attributes rsa_public_key_t::operator()(descriptor_p desc, const Attributes& attributes) const
{
const Attributes base_attrs = public_key_t::operator()(desc, attributes);
const CK_KEY_TYPE type = CKK_RSA;
Attributes attrs = {
create_object(CKA_KEY_TYPE, type),
};
if (EVP_PKEY *pkey = PEM_read_PUBKEY(desc->file.get(), NULL, NULL, NULL)) {
int size = 0;
std::shared_ptr<unsigned char> buf;
std::tie(size, buf) = read_bignum(pkey->pkey.rsa->n);
attrs.insert(std::make_pair(CKA_MODULUS, attribute_t(CKA_MODULUS, buf.get(), size)));
attrs.insert(create_object(CKA_MODULUS_BITS, size * 8));
std::tie(size, buf) = read_bignum(pkey->pkey.rsa->e);
attrs.insert(std::make_pair(CKA_PUBLIC_EXPONENT, attribute_t(CKA_PUBLIC_EXPONENT, buf.get(), size)));
EVP_PKEY_free(pkey);
}
//keys in attrs takes precedence with attributes
attrs.insert(base_attrs.begin(), base_attrs.end());
return attrs;
}
Attributes ssh_public_key_t::operator()(descriptor_p desc, const Attributes& attributes) const
{
Attributes attrs;
const auto data = piped("cat > /tmp/.soft-pkcs.tmp && ssh-keygen -e -m PKCS8 -f /tmp/.soft-pkcs.tmp && rm /tmp/.soft-pkcs.tmp", desc->item.data);
assert(data.size());
if (!data.empty()) {
std::shared_ptr<FILE> reserve = desc->file;
desc->file =read_mem(data);
attrs = rsa_public_key_t::operator()(desc, attributes);
desc->file = reserve;
attrs.insert(create_object(CKA_VALUE, data));
}
return attrs;
}
Attributes private_key_t::operator()(descriptor_p desc, const Attributes& attributes) const
{
const Attributes base_attrs = data_object_t::operator()(desc, attributes);
const CK_OBJECT_CLASS klass = CKO_PRIVATE_KEY;
const CK_MECHANISM_TYPE mech_type = CKM_RSA_X_509;
const CK_KEY_TYPE type = CKK_GENERIC_SECRET;
CK_ULONG id = desc->id;
Attributes attrs = {
create_object(CKA_CLASS, klass),
// std::make_pair(CKA_VALUE, attribute_t(CKA_VALUE, data.size())), // SPECIAL CASE FOR VALUE
//Common Storage Object Attributes
create_object(CKA_TOKEN, bool_true),
create_object(CKA_PRIVATE, bool_true),
create_object(CKA_MODIFIABLE,bool_false),
create_object(CKA_LABEL, desc->item.filename),
//Common Key Attributes
create_object(CKA_KEY_TYPE, type),
create_object(CKA_ID, id),
//create_object(CKA_START_DATE, id),
//create_object(CKA_END_DATE, id),
create_object(CKA_DERIVE, bool_false),
create_object(CKA_LOCAL, bool_false),
create_object(CKA_KEY_GEN_MECHANISM, mech_type),
//Common Private Key Attributes
//create_object(CKA_SUBJECT, bool_true),
create_object(CKA_SENSITIVE, bool_true),
create_object(CKA_DECRYPT, bool_true),
create_object(CKA_SIGN, bool_true),
create_object(CKA_SIGN_RECOVER, bool_false),
create_object(CKA_UNWRAP, bool_true),
create_object(CKA_EXTRACTABLE, bool_true),
//create_object(CKA_ALWAYS_SENSITIVE, bool_true),
create_object(CKA_NEVER_EXTRACTABLE, bool_false),
//create_object(CKA_WRAP_WITH_TRUSTED1, bool_false),
//create_object(CKA_UNWRAP_TEMPLATE, bool_false),
create_object(CKA_ALWAYS_AUTHENTICATE, bool_true),
/////////////
};
if (EVP_PKEY *pkey = PEM_read_PrivateKey(desc->file.get(), NULL, NULL, const_cast<char*>(""))) {
int size = 0;
std::shared_ptr<unsigned char> buf;
std::tie(size, buf) = read_bignum(pkey->pkey.rsa->n);
attrs.insert(std::make_pair(CKA_MODULUS, attribute_t(CKA_MODULUS, buf.get(), size)));
std::tie(size, buf) = read_bignum(pkey->pkey.rsa->e);
attrs.insert(std::make_pair(CKA_PUBLIC_EXPONENT, attribute_t(CKA_PUBLIC_EXPONENT, buf.get(), size)));
switch (EVP_PKEY_type(pkey->type)) {
case EVP_PKEY_RSA:
attrs[CKA_KEY_TYPE] = attribute_t(CKA_KEY_TYPE, (CK_KEY_TYPE)CKK_RSA);
break;
case EVP_PKEY_DSA:
attrs[CKA_KEY_TYPE] = attribute_t(CKA_KEY_TYPE, (CK_KEY_TYPE)CKK_DSA);
break;
case EVP_PKEY_DH:
attrs[CKA_KEY_TYPE] = attribute_t(CKA_KEY_TYPE, (CK_KEY_TYPE)CKK_DH);
break;
case EVP_PKEY_EC:
attrs[CKA_KEY_TYPE] = attribute_t(CKA_KEY_TYPE, (CK_KEY_TYPE)CKK_EC);
break;
};
EVP_PKEY_free(pkey);
}
//keys in attrs takes precedence with attributes
attrs.insert(base_attrs.begin(), base_attrs.end());
return attrs;
}
// Attributes rsa_private_key_t::operator()(descriptor_p desc, const Attributes& attributes) const
// {
// const Attributes base_attrs = private_key_t::operator()(desc, attributes);
//
// const CK_KEY_TYPE type = CKK_RSA;
//
// Attributes attrs = {
// create_object(CKA_KEY_TYPE, type),
// };
//
// if (EVP_PKEY *pkey = PEM_read_PrivateKey(desc->file.get(), NULL, NULL, const_cast<char*>(""))) {
// int size = 0;
// std::shared_ptr<unsigned char> buf;
//
// std::tie(size, buf) = read_bignum(pkey->pkey.rsa->n);
// attrs.insert(std::make_pair(CKA_MODULUS, attribute_t(CKA_MODULUS, buf.get(), size)));
//
// st_logf(" ..... CKA_MODULUS: %lu\n", attrs[CKA_MODULUS].to_handle());
//
// std::tie(size, buf) = read_bignum(pkey->pkey.rsa->e);
// attrs.insert(std::make_pair(CKA_PUBLIC_EXPONENT, attribute_t(CKA_PUBLIC_EXPONENT, buf.get(), size)));
//
// EVP_PKEY_free(pkey);
// }
//
// //keys in attrs takes precedence with attributes
// attrs.insert(base_attrs.begin(), base_attrs.end());
//
// return attrs;
// }
Attributes secrete_key_t::operator()(descriptor_p desc, const Attributes& attributes) const
{
const Attributes base_attrs = data_object_t::operator()(desc, attributes);
const CK_OBJECT_CLASS klass = CKO_SECRET_KEY;
const CK_MECHANISM_TYPE mech_type = CKM_RSA_X_509;
//ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-11/v2-20/pkcs-11v2-20.pdf
Attributes attrs = {
create_object(CKA_CLASS, klass),
// std::make_pair(CKA_VALUE, attribute_t(CKA_VALUE, data.size())), // SPECIAL CASE FOR VALUE
//Common Storage Object Attributes
create_object(CKA_TOKEN, bool_true),
create_object(CKA_PRIVATE, bool_true),
create_object(CKA_MODIFIABLE,bool_false),
create_object(CKA_LABEL, desc->item.filename),
//Common Key Attributes
//create_object(CKA_KEY_TYPE, id),
create_object(CKA_ID, desc->id),
//create_object(CKA_START_DATE, id),
//create_object(CKA_END_DATE, id),
create_object(CKA_DERIVE, bool_false),
create_object(CKA_LOCAL, bool_false),
create_object(CKA_KEY_GEN_MECHANISM, mech_type),
//Common Secret Key Attributes
create_object(CKA_SENSITIVE, bool_true), //bool_false
create_object(CKA_ENCRYPT, bool_true),
create_object(CKA_DECRYPT, bool_true),
create_object(CKA_SIGN, bool_true),
create_object(CKA_VERIFY, bool_false),
create_object(CKA_WRAP, bool_false),
create_object(CKA_UNWRAP, bool_false),
create_object(CKA_EXTRACTABLE, bool_true),
//create_object(CKA_ALWAYS_SENSITIVE, bool_true),
create_object(CKA_NEVER_EXTRACTABLE, bool_false),
//create_object(CKA_CHECK_VALUE, bool_false),
//create_object(CKA_WRAP_WITH_TRUSTED, bool_false),
//create_object(CKA_TRUSTED, bool_false),
//create_object(CKA_WRAP_TEMPLATE, bool_false),
//create_object(CKA_UNWRAP_TEMPLATE, bool_false),
create_object(CKA_ALWAYS_AUTHENTICATE, bool_true),
/////////////
};
//keys in attrs takes precedence with attributes
attrs.insert(attributes.begin(), attributes.end());
return attrs;
}