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
|
/*
* Copyright (C) 2007 Stef Walter
*
* 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 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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "ckcapi.h"
#include "ckcapi-object.h"
#include "pkcs11/pkcs11n.h"
#include <memory.h>
enum
{
DATA_UNKNOWN = 0,
DATA_BOOL,
DATA_ULONG,
DATA_DATE,
DATA_BYTES
};
int
attribute_data_type(CK_ATTRIBUTE_TYPE type)
{
switch(type)
{
// CK_ULONG attribute types
case CKA_CLASS:
case CKA_CERTIFICATE_TYPE:
case CKA_CERTIFICATE_CATEGORY:
case CKA_KEY_TYPE:
case CKA_MODULUS_BITS:
case CKA_PRIME_BITS:
/* case CKA_SUBPRIME_BITS: */
case CKA_SUB_PRIME_BITS:
case CKA_VALUE_BITS:
case CKA_VALUE_LEN:
case CKA_KEY_GEN_MECHANISM:
case CKA_HW_FEATURE_TYPE:
case CKA_PIXEL_X:
case CKA_PIXEL_Y:
case CKA_RESOLUTION:
case CKA_CHAR_ROWS:
case CKA_CHAR_COLUMNS:
case CKA_BITS_PER_PIXEL:
case CKA_MECHANISM_TYPE:
case CKA_JAVA_MIDP_SECURITY_DOMAIN:
case CKA_TRUST_SERVER_AUTH:
case CKA_TRUST_CLIENT_AUTH:
case CKA_TRUST_CODE_SIGNING:
case CKA_TRUST_EMAIL_PROTECTION:
case CKA_TRUST_IPSEC_END_SYSTEM:
case CKA_TRUST_IPSEC_TUNNEL:
case CKA_TRUST_IPSEC_USER:
case CKA_TRUST_TIME_STAMPING:
return DATA_ULONG;
// CK_BBOOL attribute types
case CKA_TOKEN:
case CKA_PRIVATE:
case CKA_MODIFIABLE:
case CKA_TRUSTED:
case CKA_SENSITIVE:
case CKA_DECRYPT:
case CKA_SIGN:
case CKA_SIGN_RECOVER:
case CKA_UNWRAP:
case CKA_EXTRACTABLE:
case CKA_NEVER_EXTRACTABLE:
case CKA_ALWAYS_SENSITIVE:
case CKA_WRAP_WITH_TRUSTED:
case CKA_ALWAYS_AUTHENTICATE:
case CKA_ENCRYPT:
case CKA_WRAP:
case CKA_VERIFY:
case CKA_VERIFY_RECOVER:
case CKA_DERIVE:
case CKA_LOCAL:
case CKA_RESET_ON_INIT:
case CKA_HAS_RESET:
case CKA_COLOR:
case CKA_TRUST_STEP_UP_APPROVED:
return DATA_BOOL;
// Raw or string data
case CKA_LABEL:
case CKA_APPLICATION:
case CKA_VALUE:
case CKA_OBJECT_ID:
case CKA_CHECK_VALUE:
case CKA_ISSUER:
case CKA_SERIAL_NUMBER:
case CKA_SUBJECT:
case CKA_ID:
case CKA_URL:
case CKA_HASH_OF_SUBJECT_PUBLIC_KEY:
case CKA_HASH_OF_ISSUER_PUBLIC_KEY:
case CKA_AC_ISSUER:
case CKA_OWNER:
case CKA_ATTR_TYPES:
case CKA_MODULUS:
case CKA_PUBLIC_EXPONENT:
case CKA_PRIVATE_EXPONENT:
case CKA_PRIME_1:
case CKA_PRIME_2:
case CKA_EXPONENT_1:
case CKA_EXPONENT_2:
case CKA_COEFFICIENT:
case CKA_PRIME:
case CKA_SUBPRIME:
case CKA_BASE:
case CKA_ECDSA_PARAMS:
/* case CKA_EC_PARAMS: */
case CKA_EC_POINT:
case CKA_CHAR_SETS:
case CKA_ENCODING_METHODS:
case CKA_MIME_TYPES:
case CKA_REQUIRED_CMS_ATTRIBUTES:
case CKA_DEFAULT_CMS_ATTRIBUTES:
case CKA_SUPPORTED_CMS_ATTRIBUTES:
case CKA_CERT_SHA1_HASH:
case CKA_CERT_MD5_HASH:
return DATA_BYTES;
// CK_DATE data
case CKA_START_DATE:
case CKA_END_DATE:
return DATA_DATE;
// Arrays are nasty
case CKA_WRAP_TEMPLATE:
case CKA_ALLOWED_MECHANISMS:
case CKA_UNWRAP_TEMPLATE:
default:
DBG(("unknown attribute type: %x", type));
return DATA_UNKNOWN;
};
}
CK_BBOOL
ckcapi_object_data_match_attr(CkCapiObjectData* objdata, CK_ATTRIBUTE_PTR match)
{
CK_ATTRIBUTE attr;
CK_RV rv;
int dtype;
ASSERT(match);
ASSERT(objdata);
ASSERT(objdata->data_funcs);
/* Get the data type of the attribute */
dtype = attribute_data_type(match->type);
if(dtype == DATA_UNKNOWN)
return CK_FALSE;
/* We only do byte matching */
if(match->pValue == NULL)
return CK_FALSE;
/* Only load as much data as is needed */
attr.type = match->type;
attr.pValue = _alloca(match->ulValueLen > 4 ? match->ulValueLen : 4);
attr.ulValueLen = match->ulValueLen;
switch(dtype)
{
case DATA_BOOL:
rv = (objdata->data_funcs->get_bool)(objdata, &attr);
break;
case DATA_ULONG:
rv = (objdata->data_funcs->get_ulong)(objdata, &attr);
break;
case DATA_BYTES:
rv = (objdata->data_funcs->get_bytes)(objdata, &attr);
break;
case DATA_DATE:
rv = (objdata->data_funcs->get_date)(objdata, &attr);
break;
default:
ASSERT(0 && "unrecognized type");
break;
};
/* Unrecognized attribute */
if(rv == CKR_ATTRIBUTE_TYPE_INVALID)
return CK_FALSE;
/* Value is longer than this one */
if(rv == CKR_BUFFER_TOO_SMALL)
return CK_FALSE;
/* All other errors */
if(rv != CKR_OK)
return CK_FALSE;
return (match->ulValueLen == attr.ulValueLen &&
memcmp(match->pValue, attr.pValue, attr.ulValueLen) == 0);
}
CK_BBOOL
ckcapi_object_data_match(CkCapiObjectData* objdata, CK_ATTRIBUTE_PTR matches,
CK_ULONG count)
{
CK_ULONG i;
for(i = 0; i < count; ++i)
{
if(!ckcapi_object_data_match_attr(objdata, &matches[i]))
return CK_FALSE;
}
return CK_TRUE;
}
CK_RV
ckcapi_object_data_get_attrs(CkCapiObjectData* objdata, CK_ATTRIBUTE_PTR attrs,
CK_ULONG count)
{
CK_ULONG i;
CK_RV rv, ret = CKR_OK;
ASSERT(objdata);
ASSERT(!count || attrs);
for(i = 0; i < count; ++i)
{
/* Get the data type of the attribute */
switch(attribute_data_type(attrs[i].type))
{
case DATA_BOOL:
rv = (objdata->data_funcs->get_bool)(objdata, &attrs[i]);
break;
case DATA_ULONG:
rv = (objdata->data_funcs->get_ulong)(objdata, &attrs[i]);
break;
case DATA_BYTES:
rv = (objdata->data_funcs->get_bytes)(objdata, &attrs[i]);
break;
case DATA_DATE:
rv = (objdata->data_funcs->get_date)(objdata, &attrs[i]);
break;
case DATA_UNKNOWN:
rv = CKR_ATTRIBUTE_TYPE_INVALID;
break;
default:
ASSERT(0 && "unrecognized type");
break;
};
/* Not an error if they were just requesting the size */
if(rv != CKR_OK)
{
if(rv == CKR_BUFFER_TOO_SMALL)
{
if(!attrs[i].pValue)
rv = CKR_OK;
}
/* Attribute is sensitive */
else if(rv == CKR_ATTRIBUTE_SENSITIVE)
{
attrs[i].ulValueLen = (CK_ULONG)-1;
}
/* Attribute doesn't exist */
else if(rv == CKR_ATTRIBUTE_TYPE_INVALID)
{
DBG(("O%d: attribute not found: 0x%08x", objdata->object, attrs[i].type));
attrs[i].ulValueLen = (CK_ULONG)-1;
}
/* A fatal error? */
else
{
ret = rv;
break;
}
/* Transfer any non-fatal errors outward */
if(rv != CKR_OK && ret == CKR_OK)
ret = rv;
}
}
return ret;
}
|