-
Notifications
You must be signed in to change notification settings - Fork 1
/
Template.cpp
320 lines (239 loc) · 9.4 KB
/
Template.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
312
313
314
315
316
317
318
319
320
/*
* PKCS#11 library for .Net smart cards
* Copyright (C) 2007-2009 Gemalto <support@gemalto.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "Template.hpp"
#include <boost/foreach.hpp>
#include "PKCS11Exception.hpp"
#include "Log.hpp"
extern bool IS_LITTLE_ENDIAN;
/*
*/
Template::Template( CK_ATTRIBUTE_PTR a_Template, const CK_ULONG& a_ulCount ) {
for( CK_ULONG i = 0 ; i < a_ulCount ; ++i ) {
CK_ATTRIBUTE a;
a.type = a_Template[ i ].type;
a.ulValueLen = a_Template[ i ].ulValueLen;
a.pValue = NULL_PTR;
if( a.ulValueLen > 0 ) {
a.pValue = new CK_BYTE[ a.ulValueLen ];
memcpy( a.pValue, a_Template[ i ].pValue, a.ulValueLen );
}
m_Attributes.push_back( a );
}
}
/*
*/
Template::~Template( ) {
BOOST_FOREACH( CK_ATTRIBUTE& a, m_Attributes ) {
if( 1 == a.ulValueLen ) {
delete ( ( CK_BYTE* ) a.pValue );
} else if( a.ulValueLen > 1 ) {
delete[ ] ( ( CK_BYTE* ) a.pValue );
}
}
}
/*
*/
void Template::fixEndianness( CK_ATTRIBUTE& a_attribute ) {
// Only for Little Endian processors
if( IS_LITTLE_ENDIAN ) {
// we need to fix the endianness if we are dealing with data on 2 or 4 or 8 bytes
switch( a_attribute.ulValueLen ) {
case 2:
case 4:
case 8:
{
// fix up needs to be done for specific attributes. Byte arrays may have sizes of 2,4 or 8
switch( a_attribute.type ) {
// CK_ULONG data types
case CKA_CLASS:
case CKA_CERTIFICATE_TYPE:
case CKA_JAVA_MIDP_SECURITY_DOMAIN:
case CKA_KEY_TYPE:
case CKA_KEY_GEN_MECHANISM:
case CKA_MODULUS_BITS:
{
//PKCS11_ASSERT(attrTemplate.ulValueLen == sizeof(CK_ULONG));
CK_BYTE b1 = ((CK_BYTE_PTR)a_attribute.pValue)[0];
CK_BYTE b2 = ((CK_BYTE_PTR)a_attribute.pValue)[1];
CK_BYTE b3 = ((CK_BYTE_PTR)a_attribute.pValue)[2];
CK_BYTE b4 = ((CK_BYTE_PTR)a_attribute.pValue)[3];
((CK_BYTE_PTR)a_attribute.pValue)[3] = b1;
((CK_BYTE_PTR)a_attribute.pValue)[2] = b2;
((CK_BYTE_PTR)a_attribute.pValue)[1] = b3;
((CK_BYTE_PTR)a_attribute.pValue)[0] = b4;
}
break;
}
}
break;
default:
break;
}
}
}
/*
*/
CK_OBJECT_CLASS Template::getClass( CK_ATTRIBUTE_PTR a_pTemplate, const CK_ULONG& a_ulCount ) {
for( CK_ULONG idx = 0; idx < a_ulCount ; ++idx ) {
if( CKA_CLASS == a_pTemplate[ idx ].type ) {
return (*(CK_ULONG*)a_pTemplate[ idx ].pValue);
}
}
return CK_UNAVAILABLE_INFORMATION;
}
/*
*/
CK_CERTIFICATE_TYPE Template::getCertificateType( CK_ATTRIBUTE_PTR a_pTemplate, const CK_ULONG& a_ulCount ) {
for( CK_ULONG idx = 0; idx < a_ulCount ; ++idx ) {
if( CKA_CERTIFICATE_TYPE == a_pTemplate[ idx ].type ) {
return (*(CK_ULONG*)a_pTemplate[ idx ].pValue);
}
}
return CK_UNAVAILABLE_INFORMATION;
}
/*
*/
bool Template::isToken( CK_ATTRIBUTE_PTR a_pTemplate, const CK_ULONG& a_ulCount ) {
for( CK_ULONG idx = 0; idx < a_ulCount ; ++idx ) {
if( CKA_TOKEN == a_pTemplate[ idx ].type ) {
return (*(bool*)a_pTemplate[ idx ].pValue);
}
}
return false;
}
/*
*/
bool Template::isPrivate( CK_ATTRIBUTE_PTR a_pTemplate, const CK_ULONG& a_ulCount ) {
for( CK_ULONG idx = 0; idx < a_ulCount ; ++idx ) {
if( CKA_PRIVATE == a_pTemplate[ idx ].type ) {
return (*(bool*)a_pTemplate[ idx ].pValue);
}
}
return false;
}
/*
*/
bool Template::isPresent( CK_ATTRIBUTE_PTR a_pTemplate, const CK_ULONG& a_ulCount, const CK_ATTRIBUTE_TYPE& a_ulType ) {
for( CK_ULONG idx = 0; idx < a_ulCount ; ++idx ) {
if( a_ulType == a_pTemplate[ idx ].type ) {
return true;
}
}
return false;
}
/*
*/
void Template::checkTemplate( CK_ATTRIBUTE_PTR a_pTemplate, const CK_ULONG& a_ulCount, const CK_BYTE& a_bMode ) {
// Get Object Class
CK_OBJECT_CLASS c = getClass( a_pTemplate, a_ulCount );
// Get Cert Type
CK_CERTIFICATE_TYPE t = CK_UNAVAILABLE_INFORMATION;
if( CKO_CERTIFICATE == c ) {
t = getCertificateType( a_pTemplate, a_ulCount );
}
// Check Creation Template
if( MODE_CREATE == a_bMode ) {
switch( c ) {
case CKO_DATA:
{
if( isPresent( a_pTemplate, a_ulCount, CKA_CLASS ) ) {
return;
}
}
break;
case CKO_CERTIFICATE:
{
if( ( CKC_X_509 == t ) && isPresent( a_pTemplate, a_ulCount, CKA_CLASS ) && isPresent( a_pTemplate, a_ulCount, CKA_SUBJECT ) && isPresent( a_pTemplate, a_ulCount, CKA_VALUE ) ) {
return;
} else if( ( CKC_X_509_ATTR_CERT == t ) && isPresent( a_pTemplate, a_ulCount, CKA_CLASS ) && isPresent( a_pTemplate, a_ulCount, CKA_OWNER ) && isPresent( a_pTemplate, a_ulCount, CKA_VALUE ) ) {
return;
} else {
throw PKCS11Exception( CKR_TEMPLATE_INCONSISTENT );
}
}
break;
case CKO_PUBLIC_KEY:
{
if( isPresent( a_pTemplate, a_ulCount, CKA_CLASS ) && isPresent( a_pTemplate, a_ulCount, CKA_KEY_TYPE ) && !isPresent( a_pTemplate, a_ulCount, CKA_LOCAL ) && !isPresent( a_pTemplate, a_ulCount, CKA_KEY_GEN_MECHANISM )
&& isPresent( a_pTemplate, a_ulCount, CKA_MODULUS ) && !isPresent( a_pTemplate, a_ulCount, CKA_MODULUS_BITS ) && isPresent(a_pTemplate, a_ulCount, CKA_PUBLIC_EXPONENT ) ) {
return;
}
}
break;
case CKO_PRIVATE_KEY:
{
if ( ( isPresent(a_pTemplate, a_ulCount, CKA_CLASS))
&&( isPresent(a_pTemplate, a_ulCount, CKA_KEY_TYPE))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_LOCAL))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_KEY_GEN_MECHANISM))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_ALWAYS_SENSITIVE))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_NEVER_EXTRACTABLE))
&&( isPresent(a_pTemplate, a_ulCount, CKA_MODULUS))
&&( isPresent(a_pTemplate, a_ulCount, CKA_PRIVATE_EXPONENT))
&&( isPresent(a_pTemplate, a_ulCount, CKA_PRIME_1))
&&( isPresent(a_pTemplate, a_ulCount, CKA_PRIME_2))
&&( isPresent(a_pTemplate, a_ulCount, CKA_EXPONENT_1))
&&( isPresent(a_pTemplate, a_ulCount, CKA_EXPONENT_2))
&&( isPresent(a_pTemplate, a_ulCount, CKA_COEFFICIENT))
)
{
return;
}
}
break;
default:
throw PKCS11Exception( CKR_TEMPLATE_INCONSISTENT );
}
}
// Check Public Key Generation Template
else if (a_bMode == MODE_GENERATE_PUB)
{
if ( (!isPresent(a_pTemplate, a_ulCount, CKA_LOCAL))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_KEY_GEN_MECHANISM))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_MODULUS))
&&( isPresent(a_pTemplate, a_ulCount, CKA_MODULUS_BITS))
&&( isPresent(a_pTemplate, a_ulCount, CKA_PUBLIC_EXPONENT))
)
{
return;
}
}
// Check Private Key Generation Template
else if (a_bMode == MODE_GENERATE_PRIV)
{
if ( (!isPresent(a_pTemplate, a_ulCount, CKA_LOCAL))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_KEY_GEN_MECHANISM))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_ALWAYS_SENSITIVE))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_NEVER_EXTRACTABLE))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_MODULUS))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_PUBLIC_EXPONENT))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_PRIVATE_EXPONENT))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_PRIME_1))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_PRIME_2))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_EXPONENT_1))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_EXPONENT_2))
&&(!isPresent(a_pTemplate, a_ulCount, CKA_COEFFICIENT))
)
{
return;
}
}
throw PKCS11Exception( CKR_TEMPLATE_INCONSISTENT );
}