Debian Codegraph @

src/lib/P11Attributes.cpp

raw
1 /*
2 * Copyright (c) 2011 .SE (The Internet Infrastructure Foundation)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 P11Attributes.h
29
30 This file contains classes for controlling attributes
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "P11Attributes.h"
35 #include "ByteString.h"
36 #include "CryptoFactory.h"
37 #include "DESKey.h"
38 #include "AESKey.h"
39 #include <stdio.h>
40 #include <stdlib.h>
41
42 // Constructor
43 P11Attribute::P11Attribute(OSObject* inobject)
44 {
45 osobject = inobject;
46 type = CKA_VENDOR_DEFINED;
47 size = (CK_ULONG)-1;
48 checks = 0;
49 }
50
51 // Destructor
52 P11Attribute::~P11Attribute()
53 {
54 }
55
56 CK_RV P11Attribute::updateAttr(Token *token, bool isPrivate, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
57 {
58 ByteString value;
59 if (isPrivate)
60 {
61 if (!token->encrypt(ByteString((unsigned char*)pValue, ulValueLen),value))
62 return CKR_GENERAL_ERROR;
63 }
64 else
65 value = ByteString((unsigned char*)pValue, ulValueLen);
66 if (value.size() < ulValueLen)
67 return CKR_GENERAL_ERROR;
68 osobject->setAttribute(type, value);
69 return CKR_OK;
70 }
71
72 bool P11Attribute::isModifiable()
73 {
74 // Get the CKA_MODIFIABLE attribute, when the attribute is
75 // not present return the default value which is CK_TRUE.
76 if (!osobject->attributeExists(CKA_MODIFIABLE)) return true;
77
78 return osobject->getBooleanValue(CKA_MODIFIABLE, true);
79 }
80
81 bool P11Attribute::isSensitive()
82 {
83 // Get the CKA_SENSITIVE attribute, when the attribute is not present
84 // assume the object is not sensitive.
85 if (!osobject->attributeExists(CKA_SENSITIVE)) return false;
86
87 return osobject->getBooleanValue(CKA_SENSITIVE, false);
88 }
89
90 bool P11Attribute::isExtractable()
91 {
92 // Get the CKA_EXTRACTABLE attribute, when the attribute is
93 // not present assume the object allows extraction.
94 if (!osobject->attributeExists(CKA_EXTRACTABLE)) return true;
95
96 return osobject->getBooleanValue(CKA_EXTRACTABLE, true);
97 }
98
99 bool P11Attribute::isTrusted()
100 {
101 // Get the CKA_TRUSTED attribute, when the attribute is
102 // not present assume the object is not trusted.
103 if (!osobject->attributeExists(CKA_TRUSTED)) return false;
104
105 return osobject->getBooleanValue(CKA_TRUSTED, false);
106 }
107
108 // Initialize the attribute
109 bool P11Attribute::init()
110 {
111 if (osobject == NULL) return false;
112
113 // Create a default value if the attribute does not exist
114 if (osobject->attributeExists(type) == false)
115 {
116 return setDefault();
117 }
118
119 return true;
120 }
121
122 // Return the attribute type
123 CK_ATTRIBUTE_TYPE P11Attribute::getType()
124 {
125 return type;
126 }
127
128 // Return the attribute checks
129 CK_ATTRIBUTE_TYPE P11Attribute::getChecks()
130 {
131 return checks;
132 }
133
134 // Retrieve a template map
135 static CK_RV retrieveAttributeMap(CK_ATTRIBUTE_PTR pTemplate, const std::map<CK_ATTRIBUTE_TYPE,OSAttribute>& map)
136 {
137 size_t nullcnt = 0;
138
139 for (size_t i = 0; i < map.size(); ++i)
140 {
141 if (pTemplate[i].pValue == NULL_PTR)
142 ++nullcnt;
143 }
144
145 // Caller wants type & size
146 if (nullcnt == map.size())
147 {
148 std::map<CK_ATTRIBUTE_TYPE,OSAttribute>::const_iterator a = map.begin();
149 for (size_t i = 0; i < map.size(); ++i, ++a)
150 {
151 pTemplate[i].type = a->first;
152 const OSAttribute& attr = a->second;
153 if (attr.isBooleanAttribute())
154 {
155 pTemplate[i].ulValueLen = sizeof(CK_BBOOL);
156 }
157 else if (attr.isUnsignedLongAttribute())
158 {
159 pTemplate[i].ulValueLen = sizeof(CK_ULONG);
160 }
161 else if (attr.isByteStringAttribute())
162 {
163 pTemplate[i].ulValueLen = attr.getByteStringValue().size();
164 }
165 else
166 {
167 // Impossible
168 ERROR_MSG("Internal error: bad attribute in attribute map");
169
170 return CKR_GENERAL_ERROR;
171 }
172 }
173
174 return CKR_OK;
175 }
176
177 // Callers wants to get values
178 for (size_t i = 0; i < map.size(); ++i)
179 {
180 std::map<CK_ATTRIBUTE_TYPE,OSAttribute>::const_iterator a = map.find(pTemplate[i].type);
181 if (a == map.end())
182 {
183 pTemplate[i].ulValueLen = CK_UNAVAILABLE_INFORMATION;
184 return CKR_ATTRIBUTE_TYPE_INVALID;
185 }
186 const OSAttribute& attr = a->second;
187 if (attr.isBooleanAttribute())
188 {
189 if (pTemplate[i].ulValueLen < sizeof(CK_BBOOL))
190 {
191 pTemplate[i].ulValueLen = CK_UNAVAILABLE_INFORMATION;
192 return CKR_BUFFER_TOO_SMALL;
193 }
194 pTemplate[i].ulValueLen = sizeof(CK_BBOOL);
195 *(CK_BBOOL*)pTemplate[i].pValue = attr.getBooleanValue() ? CK_TRUE : CK_FALSE;
196 }
197 else if (attr.isUnsignedLongAttribute())
198 {
199 if (pTemplate[i].ulValueLen < sizeof(CK_ULONG))
200 {
201 pTemplate[i].ulValueLen= CK_UNAVAILABLE_INFORMATION;
202 return CKR_BUFFER_TOO_SMALL;
203 }
204 pTemplate[i].ulValueLen = sizeof(CK_ULONG);
205 *(CK_ULONG_PTR)pTemplate[i].pValue= attr.getUnsignedLongValue();
206 }
207 else if (attr.isByteStringAttribute())
208 {
209 ByteString value = attr.getByteStringValue();
210 if (pTemplate[i].ulValueLen < value.size())
211 {
212 pTemplate[i].ulValueLen= CK_UNAVAILABLE_INFORMATION;
213 return CKR_BUFFER_TOO_SMALL;
214 }
215 pTemplate[i].ulValueLen = value.size();
216 memcpy(pTemplate[i].pValue, value.const_byte_str(), value.size());
217 }
218 else
219 {
220 // Impossible
221 ERROR_MSG("Internal error: bad attribute in attribute map");
222
223 return CKR_GENERAL_ERROR;
224 }
225 }
226
227 return CKR_OK;
228 }
229
230 // Retrieve the value if allowed
231 CK_RV P11Attribute::retrieve(Token *token, bool isPrivate, CK_VOID_PTR pValue, CK_ULONG_PTR pulValueLen)
232 {
233
234 if (osobject == NULL) {
235 ERROR_MSG("Internal error: osobject field contains NULL_PTR");
236 return CKR_GENERAL_ERROR;
237 }
238
239 if (pulValueLen == NULL) {
240 ERROR_MSG("Internal error: pulValueLen contains NULL_PTR");
241 return CKR_GENERAL_ERROR;
242 }
243
244 // [PKCS#11 v2.40, C_GetAttributeValue]
245 // 1. If the specified attribute (i.e., the attribute specified by the
246 // type field) for the object cannot be revealed because the object
247 // is sensitive or unextractable, then the ulValueLen field in that
248 // triple is modified to hold the value CK_UNAVAILABLE_INFORMATION.
249 //
250 // [PKCS#11 v2.40, 4.2 Common attributes, table 10]
251 // 7 Cannot be revealed if object has its CKA_SENSITIVE attribute
252 // set to CK_TRUE or its CKA_EXTRACTABLE attribute set to CK_FALSE.
253 if ((checks & ck7) == ck7 && (isSensitive() || !isExtractable())) {
254 *pulValueLen = CK_UNAVAILABLE_INFORMATION;
255 return CKR_ATTRIBUTE_SENSITIVE;
256 }
257
258 // Retrieve the lower level attribute.
259 if (!osobject->attributeExists(type)) {
260 // Should be impossible.
261 ERROR_MSG("Internal error: attribute not present");
262 return CKR_GENERAL_ERROR;
263 }
264 OSAttribute attr = osobject->getAttribute(type);
265
266 // Get the actual attribute size.
267 CK_ULONG attrSize = size;
268 if (size == (CK_ULONG)-1)
269 {
270 // We don't have a fixed size attribute so we need to consult
271 // the lower level attribute for the exact size.
272
273 // Lower level attribute has to be variable sized.
274 if (attr.isByteStringAttribute())
275 {
276 if (isPrivate && attr.getByteStringValue().size() != 0)
277 {
278 ByteString value;
279 if (!token->decrypt(attr.getByteStringValue(),value))
280 {
281 ERROR_MSG("Internal error: failed to decrypt private attribute value");
282 return CKR_GENERAL_ERROR;
283 }
284 attrSize = value.size();
285 }
286 else
287 attrSize = attr.getByteStringValue().size();
288 }
289 else if (attr.isMechanismTypeSetAttribute())
290 {
291 attrSize = attr.getMechanismTypeSetValue().size() * sizeof(CK_MECHANISM_TYPE);
292 }
293 else if (attr.isAttributeMapAttribute())
294 {
295 attrSize = attr.getAttributeMapValue().size() * sizeof(CK_ATTRIBUTE);
296 }
297 else
298 {
299 // Should be impossible.
300 ERROR_MSG("Internal error: attribute has fixed size");
301 return CKR_GENERAL_ERROR;
302 }
303 }
304
305 // [PKCS#11 v2.40, C_GetAttributeValue]
306 // 3. Otherwise, if the pValue field has the value NULL_PTR, then the
307 // ulValueLen field is modified to hold the exact length of the
308 // specified attribute for the object.
309 if (pValue == NULL_PTR) {
310 // Return the size of the attribute.
311 *pulValueLen = attrSize;
312 return CKR_OK;
313 }
314
315 // [PKCS#11 v2.40, C_GetAttributeValue]
316 // 4. Otherwise, if the length specified in ulValueLen is large enough
317 // to hold the value of the specified attribute for the object, then
318 // that attribute is copied into the buffer located at pValue, and
319 // the ulValueLen field is modified to hold the exact length of the
320 // attribute.
321 if (*pulValueLen >= attrSize)
322 {
323 // Only copy when there is actually something to copy
324 CK_RV rv = CKR_OK;
325
326 if (attr.isUnsignedLongAttribute()) {
327 *(CK_ULONG_PTR)pValue = attr.getUnsignedLongValue();
328 }
329 else if (attr.isBooleanAttribute())
330 {
331 *(CK_BBOOL*)pValue = attr.getBooleanValue() ? CK_TRUE : CK_FALSE;
332 }
333 else if (attr.isByteStringAttribute())
334 {
335 if (isPrivate && attr.getByteStringValue().size() != 0)
336 {
337 ByteString value;
338 if (!token->decrypt(attr.getByteStringValue(),value))
339 {
340 ERROR_MSG("Internal error: failed to decrypt private attribute value");
341 return CKR_GENERAL_ERROR;
342 }
343 if (value.size() != 0) {
344 const unsigned char* attrPtr = value.const_byte_str();
345 memcpy(pValue,attrPtr,attrSize);
346 }
347 }
348 else if (attr.getByteStringValue().size() != 0)
349 {
350 const unsigned char* attrPtr = attr.getByteStringValue().const_byte_str();
351 memcpy(pValue,attrPtr,attrSize);
352 }
353 }
354 else if (attr.isMechanismTypeSetAttribute())
355 {
356 CK_MECHANISM_TYPE_PTR pTemplate = (CK_MECHANISM_TYPE_PTR) pValue;
357 size_t i = 0;
358
359 std::set<CK_MECHANISM_TYPE> set = attr.getMechanismTypeSetValue();
360 for (std::set<CK_MECHANISM_TYPE>::const_iterator it = set.begin(); it != set.end(); ++it)
361 pTemplate[i++] = *it;
362 }
363 else
364 {
365 // attr is already retrieved and verified to be an Attribute Map
366 rv = retrieveAttributeMap((CK_ATTRIBUTE_PTR)pValue, attr.getAttributeMapValue());
367 }
368 *pulValueLen = attrSize;
369 return rv;
370 }
371
372 // [PKCS#11 v2.40, C_GetAttributeValue]
373 // 5. Otherwise, the ulValueLen field is modified to hold the value CK_UNAVAILABLE_INFORMATION.
374 *pulValueLen = CK_UNAVAILABLE_INFORMATION;
375 return CKR_BUFFER_TOO_SMALL;
376 }
377
378 // Update the value if allowed
379 CK_RV P11Attribute::update(Token* token, bool isPrivate, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int op)
380 {
381 if (osobject == NULL) {
382 ERROR_MSG("Internal error: osobject field contains NULL_PTR");
383 return CKR_GENERAL_ERROR;
384 }
385
386 // [PKCS#11 v2.40, 4.1.1 Creating objects]
387 // 2. If the supplied template specifies an invalid value for a valid attribute, then the
388 // attempt should fail with the error code CKR_ATTRIBUTE_VALUE_INVALID.
389 // The valid values for Cryptoki attributes are described in the Cryptoki specification.
390
391 // Check for null pointers in values.
392 if (pValue == NULL_PTR && ulValueLen != 0) {
393 ERROR_MSG("The attribute is a NULL_PTR but has a non-zero length")
394 return CKR_ATTRIBUTE_VALUE_INVALID;
395 }
396
397 // For fixed sized attributes check that the size matches.
398 if (size != ((CK_ULONG)-1) && size != ulValueLen) {
399 ERROR_MSG("The attribute size is different from the expected size")
400 return CKR_ATTRIBUTE_VALUE_INVALID;
401 }
402
403 // [PKCS#11 v2.40, 4.1.1 Creating objects] OBJECT_OP_CREATE | OBJECT_OP_SET | OBJECT_OP_COPY
404 // 3. If the supplied template specifies a value for a read-only attribute, then the attempt
405 // should fail with the error code CKR_ATTRIBUTE_READ_ONLY.
406 // Whether or not a given Cryptoki attribute is read-only is explicitly stated in the Cryptoki
407 // specification; however, a particular library and token may be even more restrictive than
408 // Cryptoki specifies. In other words, an attribute which Cryptoki says is not read-only may
409 // nonetheless be read-only under certain circumstances (i.e., in conjunction with some
410 // combinations of other attributes) for a particular library and token. Whether or not a
411 // given non-Cryptoki attribute is read-only is obviously outside the scope of Cryptoki.
412
413 // Attributes cannot be changed if CKA_MODIFIABLE is set to false
414 if (!isModifiable() && op != OBJECT_OP_GENERATE && op != OBJECT_OP_CREATE) {
415 ERROR_MSG("An object is with CKA_MODIFIABLE set to false is not modifiable");
416 return CKR_ATTRIBUTE_READ_ONLY;
417 }
418
419 // Attributes cannot be modified if CKA_TRUSTED is true on a certificate object.
420 if (isTrusted() && op != OBJECT_OP_GENERATE && op != OBJECT_OP_CREATE) {
421 if (osobject->getUnsignedLongValue(CKA_CLASS, CKO_VENDOR_DEFINED) == CKO_CERTIFICATE)
422 {
423 ERROR_MSG("A trusted certificate cannot be modified");
424 return CKR_ATTRIBUTE_READ_ONLY;
425 }
426 }
427
428 // ck2 MUST not be specified when object is created with C_CreateObject.
429 if ((checks & ck2) == ck2)
430 {
431 if (OBJECT_OP_CREATE==op)
432 {
433 ERROR_MSG("Prohibited attribute was passed to object creation function");
434 return CKR_ATTRIBUTE_READ_ONLY;
435 }
436 }
437
438 // ck4 MUST not be specified when object is generated with C_GenerateKey or C_GenerateKeyPair.
439 if ((checks & ck4) == ck4)
440 {
441 if (OBJECT_OP_GENERATE==op)
442 {
443 ERROR_MSG("Prohibited attribute was passed to key generation function");
444 return CKR_ATTRIBUTE_READ_ONLY;
445 }
446 }
447
448 // ck6 MUST not be specified when object is unwrapped with C_UnwrapKey.
449 if ((checks & ck6) == ck6)
450 {
451 if (OBJECT_OP_UNWRAP==op)
452 {
453 ERROR_MSG("Prohibited attribute was passed to key unwrapping function");
454 return CKR_ATTRIBUTE_READ_ONLY;
455 }
456 }
457
458 // ck8 May be modified after object is created with a C_SetAttributeValue call
459 // or in the process of copying an object with a C_CopyObject call.
460 // However, it is possible that a particular token may not permit modification of
461 // the attribute during the course of a C_CopyObject call.
462 if ((checks & ck8) == ck8)
463 {
464 if (OBJECT_OP_SET==op || OBJECT_OP_COPY==op)
465 {
466 return updateAttr(token, isPrivate, pValue, ulValueLen, op);
467 }
468 }
469
470 // ck17 Can be changed in the process of copying the object using C_CopyObject.
471 if ((checks & ck17) == ck17)
472 {
473 if (OBJECT_OP_COPY==op)
474 {
475 return updateAttr(token, isPrivate, pValue, ulValueLen, op);
476 }
477 }
478
479 // For attributes that have not been explicitly excluded from modification
480 // during create/derive/generate/unwrap, we allow them to be modified.
481 if (OBJECT_OP_CREATE==op || OBJECT_OP_DERIVE==op || OBJECT_OP_GENERATE==op || OBJECT_OP_UNWRAP==op)
482 {
483 return updateAttr(token, isPrivate, pValue, ulValueLen, op);
484 }
485
486 return CKR_ATTRIBUTE_READ_ONLY;
487 }
488
489 /*****************************************
490 * CKA_CLASS
491 *****************************************/
492
493 // Set default value
494 bool P11AttrClass::setDefault()
495 {
496 OSAttribute attrClass((unsigned long)CKO_VENDOR_DEFINED);
497 return osobject->setAttribute(type, attrClass);
498 }
499
500 // Update the value if allowed
501 CK_RV P11AttrClass::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
502 {
503 // Attribute specific checks
504
505 if (ulValueLen !=sizeof(CK_ULONG))
506 {
507 return CKR_ATTRIBUTE_VALUE_INVALID;
508 }
509
510 if (osobject->getUnsignedLongValue(CKA_CLASS, CKO_VENDOR_DEFINED) != *(CK_ULONG*)pValue)
511 {
512 return CKR_TEMPLATE_INCONSISTENT;
513 }
514
515 return CKR_OK;
516 }
517
518 /*****************************************
519 * CKA_KEY_TYPE
520 *****************************************/
521
522 // Set default value
523 bool P11AttrKeyType::setDefault()
524 {
525 OSAttribute attr((unsigned long)CKK_VENDOR_DEFINED);
526 return osobject->setAttribute(type, attr);
527 }
528
529 // Update the value if allowed
530 CK_RV P11AttrKeyType::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
531 {
532 // Attribute specific checks
533
534 if (ulValueLen !=sizeof(CK_ULONG))
535 {
536 return CKR_ATTRIBUTE_VALUE_INVALID;
537 }
538
539 if (osobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != *(CK_ULONG*)pValue)
540 {
541 return CKR_TEMPLATE_INCONSISTENT;
542 }
543
544 return CKR_OK;
545 }
546
547 /*****************************************
548 * CKA_CERTIFICATE_TYPE
549 * footnote 1
550 * 1 MUST be specified when object is created with C_CreateObject.
551 *****************************************/
552
553 // Set default value
554 bool P11AttrCertificateType::setDefault()
555 {
556 OSAttribute attr((unsigned long)CKC_VENDOR_DEFINED);
557 return osobject->setAttribute(type, attr);
558 }
559
560 // Update the value if allowed
561 CK_RV P11AttrCertificateType::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
562 {
563 // Attribute specific checks
564
565 if (ulValueLen !=sizeof(CK_ULONG))
566 {
567 return CKR_ATTRIBUTE_VALUE_INVALID;
568 }
569
570 if (osobject->getUnsignedLongValue(CKA_CERTIFICATE_TYPE, CKC_VENDOR_DEFINED) != *(CK_ULONG*)pValue)
571 {
572 return CKR_TEMPLATE_INCONSISTENT;
573 }
574
575 return CKR_OK;
576 }
577
578 /*****************************************
579 * CKA_TOKEN
580 *****************************************/
581
582 // Set default value
583 bool P11AttrToken::setDefault()
584 {
585 OSAttribute attr(false);
586 return osobject->setAttribute(type, attr);
587 }
588
589 // Update the value if allowed
590 CK_RV P11AttrToken::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
591 {
592 OSAttribute attrTrue(true);
593 OSAttribute attrFalse(false);
594
595 // Attribute specific checks
596
597 if (ulValueLen !=sizeof(CK_BBOOL))
598 {
599 return CKR_ATTRIBUTE_VALUE_INVALID;
600 }
601
602 // Store data
603
604 if (*(CK_BBOOL*)pValue == CK_FALSE)
605 {
606 osobject->setAttribute(type, attrFalse);
607 }
608 else
609 {
610 osobject->setAttribute(type, attrTrue);
611 }
612
613 return CKR_OK;
614 }
615
616 /*****************************************
617 * CKA_PRIVATE
618 *****************************************/
619
620 // Set default value
621 bool P11AttrPrivate::setDefault()
622 {
623 OSAttribute attr(true);
624 return osobject->setAttribute(type, attr);
625 }
626
627 // Update the value if allowed
628 CK_RV P11AttrPrivate::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
629 {
630 OSAttribute attrTrue(true);
631 OSAttribute attrFalse(false);
632
633 // Attribute specific checks
634
635 if (ulValueLen !=sizeof(CK_BBOOL))
636 {
637 return CKR_ATTRIBUTE_VALUE_INVALID;
638 }
639
640 // Store data
641
642 if (*(CK_BBOOL*)pValue == CK_FALSE)
643 {
644 osobject->setAttribute(type, attrFalse);
645 }
646 else
647 {
648 osobject->setAttribute(type, attrTrue);
649 }
650
651 return CKR_OK;
652 }
653
654 /*****************************************
655 * CKA_MODIFIABLE
656 *****************************************/
657
658 // Set default value
659 bool P11AttrModifiable::setDefault()
660 {
661 OSAttribute attr(true);
662 return osobject->setAttribute(type, attr);
663 }
664
665 // Update the value if allowed
666 CK_RV P11AttrModifiable::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
667 {
668 OSAttribute attrTrue(true);
669 OSAttribute attrFalse(false);
670
671 // Attribute specific checks
672
673 if (ulValueLen !=sizeof(CK_BBOOL))
674 {
675 return CKR_ATTRIBUTE_VALUE_INVALID;
676 }
677
678 // Store data
679
680 if (*(CK_BBOOL*)pValue == CK_FALSE)
681 {
682 osobject->setAttribute(type, attrFalse);
683 }
684 else
685 {
686 osobject->setAttribute(type, attrTrue);
687 }
688
689 return CKR_OK;
690 }
691
692 /*****************************************
693 * CKA_LABEL
694 *****************************************/
695
696 // Set default value
697 bool P11AttrLabel::setDefault()
698 {
699 OSAttribute attr(ByteString(""));
700 return osobject->setAttribute(type, attr);
701 }
702
703 /*****************************************
704 * CKA_COPYABLE
705 *****************************************/
706
707 // Set default value
708 bool P11AttrCopyable::setDefault()
709 {
710 OSAttribute attr(true);
711 return osobject->setAttribute(type, attr);
712 }
713
714 // Update the value if allowed
715 CK_RV P11AttrCopyable::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
716 {
717 OSAttribute attrTrue(true);
718 OSAttribute attrFalse(false);
719
720 // Attribute specific checks
721
722 if (ulValueLen !=sizeof(CK_BBOOL))
723 {
724 return CKR_ATTRIBUTE_VALUE_INVALID;
725 }
726
727 // Store data
728
729 if (*(CK_BBOOL*)pValue == CK_FALSE)
730 {
731 osobject->setAttribute(type, attrFalse);
732 }
733 else
734 {
735 if (osobject->getBooleanValue(CKA_COPYABLE, true) == false)
736 {
737 return CKR_ATTRIBUTE_READ_ONLY;
738 }
739 }
740
741 return CKR_OK;
742 }
743
744 /*****************************************
745 * CKA_DESTROYABLE
746 *****************************************/
747
748 // Set default value
749 bool P11AttrDestroyable::setDefault()
750 {
751 OSAttribute attr(true);
752 return osobject->setAttribute(type, attr);
753 }
754
755 // Update the value if allowed
756 CK_RV P11AttrDestroyable::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
757 {
758 OSAttribute attrTrue(true);
759 OSAttribute attrFalse(false);
760
761 // Attribute specific checks
762
763 if (ulValueLen !=sizeof(CK_BBOOL))
764 {
765 return CKR_ATTRIBUTE_VALUE_INVALID;
766 }
767
768 // Store data
769
770 if (*(CK_BBOOL*)pValue == CK_FALSE)
771 {
772 osobject->setAttribute(type, attrFalse);
773 }
774 else
775 {
776 osobject->setAttribute(type, attrTrue);
777 }
778
779 return CKR_OK;
780 }
781
782 /*****************************************
783 * CKA_APPLICATION
784 *****************************************/
785
786 // Set default value
787 bool P11AttrApplication::setDefault()
788 {
789 OSAttribute attr(ByteString(""));
790 return osobject->setAttribute(type, attr);
791 }
792
793 /*****************************************
794 * CKA_OBJECT_ID
795 *****************************************/
796
797 // Set default value
798 bool P11AttrObjectID::setDefault()
799 {
800 OSAttribute attr(ByteString(""));
801 return osobject->setAttribute(type, attr);
802 }
803
804 /*****************************************
805 * CKA_CHECK_VALUE
806 *****************************************/
807
808 // Set default value
809 bool P11AttrCheckValue::setDefault()
810 {
811 OSAttribute attr(ByteString(""));
812 return osobject->setAttribute(type, attr);
813 }
814
815 // Update the value if allowed
816 CK_RV P11AttrCheckValue::updateAttr(Token *token, bool isPrivate, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
817 {
818 ByteString plaintext((unsigned char*)pValue, ulValueLen);
819 ByteString value;
820
821 // Encrypt
822
823 if (isPrivate)
824 {
825 if (!token->encrypt(plaintext, value))
826 return CKR_GENERAL_ERROR;
827 }
828 else
829 value = plaintext;
830
831 // Attribute specific checks
832
833 if (value.size() < ulValueLen)
834 return CKR_GENERAL_ERROR;
835
836 // Store data
837 if (ulValueLen == 0)
838 {
839 osobject->setAttribute(type, value);
840 }
841 else
842 {
843 ByteString checkValue;
844 ByteString keybits;
845 if (isPrivate)
846 {
847 if (!token->decrypt(osobject->getByteStringValue(CKA_VALUE), keybits))
848 return CKR_GENERAL_ERROR;
849 }
850 else
851 {
852 keybits = osobject->getByteStringValue(CKA_VALUE);
853 }
854
855 SymmetricKey key;
856 AESKey aes;
857 DESKey des;
858 switch (osobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED))
859 {
860 case CKK_GENERIC_SECRET:
861 case CKK_MD5_HMAC:
862 case CKK_SHA_1_HMAC:
863 case CKK_SHA224_HMAC:
864 case CKK_SHA256_HMAC:
865 case CKK_SHA384_HMAC:
866 case CKK_SHA512_HMAC:
867 key.setKeyBits(keybits);
868 key.setBitLen(keybits.size() * 8);
869 checkValue = key.getKeyCheckValue();
870 break;
871 case CKK_AES:
872 aes.setKeyBits(keybits);
873 aes.setBitLen(keybits.size() * 8);
874 checkValue = aes.getKeyCheckValue();
875 break;
876 case CKK_DES:
877 case CKK_DES2:
878 case CKK_DES3:
879 des.setKeyBits(keybits);
880 des.setBitLen(keybits.size() * 7);
881 checkValue = des.getKeyCheckValue();
882 break;
883 case CKK_GOST28147:
884 // TODO: Encryption support for CKK_GOST28147
885 // We do not calculate the KCV
886 checkValue = plaintext;
887 break;
888 default:
889 return CKR_GENERAL_ERROR;
890 }
891
892 if (plaintext != checkValue)
893 return CKR_ATTRIBUTE_VALUE_INVALID;
894
895 osobject->setAttribute(type, value);
896 }
897
898 return CKR_OK;
899 }
900
901 /*****************************************
902 * CKA_PUBLIC_KEY_INFO
903 *****************************************/
904
905 // Set default value
906 bool P11AttrPublicKeyInfo::setDefault()
907 {
908 OSAttribute attr(ByteString(""));
909 return osobject->setAttribute(type, attr);
910 }
911
912 /*****************************************
913 * CKA_ID
914 *****************************************/
915
916 // Set default value
917 bool P11AttrID::setDefault()
918 {
919 OSAttribute attr(ByteString(""));
920 return osobject->setAttribute(type, attr);
921 }
922
923 /*****************************************
924 * CKA_VALUE
925 *****************************************/
926
927 // Set default value
928 bool P11AttrValue::setDefault()
929 {
930 OSAttribute attr(ByteString(""));
931 return osobject->setAttribute(type, attr);
932 }
933
934 // Update the value if allowed
935 CK_RV P11AttrValue::updateAttr(Token *token, bool isPrivate, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int op)
936 {
937 ByteString plaintext((unsigned char*)pValue, ulValueLen);
938 ByteString value;
939
940 // Encrypt
941
942 if (isPrivate)
943 {
944 if (!token->encrypt(plaintext, value))
945 return CKR_GENERAL_ERROR;
946 }
947 else
948 value = plaintext;
949
950 // Attribute specific checks
951
952 if (value.size() < ulValueLen)
953 return CKR_GENERAL_ERROR;
954
955 // Store data
956
957 osobject->setAttribute(type, value);
958
959 // Set the size during C_CreateObject and C_UnwrapKey.
960
961 if (op == OBJECT_OP_CREATE || op == OBJECT_OP_UNWRAP)
962 {
963 // Set the CKA_VALUE_LEN
964 if (osobject->attributeExists(CKA_VALUE_LEN))
965 {
966 OSAttribute bytes((unsigned long)plaintext.size());
967 osobject->setAttribute(CKA_VALUE_LEN, bytes);
968 }
969
970 // Set the CKA_VALUE_BITS
971 if (osobject->attributeExists(CKA_VALUE_BITS))
972 {
973 OSAttribute bits((unsigned long)plaintext.bits());
974 osobject->setAttribute(CKA_VALUE_BITS, bits);
975 }
976 }
977
978 // Calculate the CKA_CHECK_VALUE for certificates
979 if (osobject->getUnsignedLongValue(CKA_CLASS, CKO_VENDOR_DEFINED) == CKO_CERTIFICATE)
980 {
981 HashAlgorithm* hash = CryptoFactory::i()->getHashAlgorithm(HashAlgo::SHA1);
982 if (hash == NULL) return CKR_GENERAL_ERROR;
983
984 ByteString digest;
985 if (hash->hashInit() == false ||
986 hash->hashUpdate(plaintext) == false ||
987 hash->hashFinal(digest) == false)
988 {
989 CryptoFactory::i()->recycleHashAlgorithm(hash);
990 return CKR_GENERAL_ERROR;
991 }
992 CryptoFactory::i()->recycleHashAlgorithm(hash);
993
994 // First three bytes of the SHA-1 hash
995 digest.resize(3);
996
997 if (isPrivate)
998 {
999 ByteString encrypted;
1000 if (!token->encrypt(digest, encrypted))
1001 return CKR_GENERAL_ERROR;
1002 osobject->setAttribute(CKA_CHECK_VALUE, encrypted);
1003 }
1004 else
1005 osobject->setAttribute(CKA_CHECK_VALUE, digest);
1006 }
1007
1008 // Calculate the CKA_CHECK_VALUE for secret keys
1009 if (op == OBJECT_OP_CREATE &&
1010 osobject->getUnsignedLongValue(CKA_CLASS, CKO_VENDOR_DEFINED) == CKO_SECRET_KEY)
1011 {
1012 SymmetricKey key;
1013 AESKey aes;
1014 DESKey des;
1015 ByteString checkValue;
1016 switch (osobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED))
1017 {
1018 case CKK_GENERIC_SECRET:
1019 case CKK_MD5_HMAC:
1020 case CKK_SHA_1_HMAC:
1021 case CKK_SHA224_HMAC:
1022 case CKK_SHA256_HMAC:
1023 case CKK_SHA384_HMAC:
1024 case CKK_SHA512_HMAC:
1025 key.setKeyBits(plaintext);
1026 key.setBitLen(plaintext.size() * 8);
1027 checkValue = key.getKeyCheckValue();
1028 break;
1029 case CKK_AES:
1030 aes.setKeyBits(plaintext);
1031 aes.setBitLen(plaintext.size() * 8);
1032 checkValue = aes.getKeyCheckValue();
1033 break;
1034 case CKK_DES:
1035 case CKK_DES2:
1036 case CKK_DES3:
1037 des.setKeyBits(plaintext);
1038 des.setBitLen(plaintext.size() * 7);
1039 checkValue = des.getKeyCheckValue();
1040 break;
1041 case CKK_GOST28147:
1042 // TODO: Encryption support for CKK_GOST28147
1043 // We do not calculate the KCV
1044 break;
1045 default:
1046 return CKR_GENERAL_ERROR;
1047 }
1048
1049 if (isPrivate)
1050 {
1051 ByteString encrypted;
1052 if (!token->encrypt(checkValue, encrypted))
1053 return CKR_GENERAL_ERROR;
1054 osobject->setAttribute(CKA_CHECK_VALUE, encrypted);
1055 }
1056 else
1057 osobject->setAttribute(CKA_CHECK_VALUE, checkValue);
1058 }
1059
1060 return CKR_OK;
1061 }
1062
1063 /*****************************************
1064 * CKA_SUBJECT
1065 *****************************************/
1066
1067 // Set default value
1068 bool P11AttrSubject::setDefault()
1069 {
1070 OSAttribute attr(ByteString(""));
1071 return osobject->setAttribute(type, attr);
1072 }
1073
1074 /*****************************************
1075 * CKA_ISSUER
1076 *****************************************/
1077
1078 // Set default value
1079 bool P11AttrIssuer::setDefault()
1080 {
1081 OSAttribute attr(ByteString(""));
1082 return osobject->setAttribute(type, attr);
1083 }
1084
1085 /*****************************************
1086 * CKA_TRUSTED
1087 *****************************************/
1088
1089 // Set default value
1090 bool P11AttrTrusted::setDefault()
1091 {
1092 OSAttribute attr(false);
1093 return osobject->setAttribute(type, attr);
1094 }
1095
1096 // Update the value if allowed
1097 CK_RV P11AttrTrusted::updateAttr(Token *token, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
1098 {
1099 OSAttribute attrTrue(true);
1100 OSAttribute attrFalse(false);
1101
1102 // Attribute specific checks
1103
1104 if (ulValueLen !=sizeof(CK_BBOOL))
1105 {
1106 return CKR_ATTRIBUTE_VALUE_INVALID;
1107 }
1108
1109 // Store data
1110
1111 if (*(CK_BBOOL*)pValue == CK_FALSE)
1112 {
1113 osobject->setAttribute(type, attrFalse);
1114 }
1115 else
1116 {
1117 if (!token->isSOLoggedIn())
1118 {
1119 ERROR_MSG("CKA_TRUSTED can only be set to true by the SO");
1120 return CKR_ATTRIBUTE_READ_ONLY;
1121 }
1122 osobject->setAttribute(type, attrTrue);
1123 }
1124
1125 return CKR_OK;
1126 }
1127
1128 /*****************************************
1129 * CKA_CERTIFICATE_CATEGORY
1130 *****************************************/
1131
1132 // Set default value
1133 bool P11AttrCertificateCategory::setDefault()
1134 {
1135 OSAttribute attr((unsigned long)0);
1136 return osobject->setAttribute(type, attr);
1137 }
1138
1139 // Update the value if allowed
1140 CK_RV P11AttrCertificateCategory::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
1141 {
1142 // Attribute specific checks
1143
1144 if (ulValueLen !=sizeof(CK_ULONG))
1145 {
1146 return CKR_ATTRIBUTE_VALUE_INVALID;
1147 }
1148
1149 // Store data
1150 osobject->setAttribute(type, *(CK_ULONG*)pValue);
1151
1152 return CKR_OK;
1153 }
1154
1155 /*****************************************
1156 * CKA_START_DATE
1157 *****************************************/
1158
1159 // Set default value
1160 bool P11AttrStartDate::setDefault()
1161 {
1162 OSAttribute attr(ByteString(""));
1163 return osobject->setAttribute(type, attr);
1164 }
1165
1166 // Update the value if allowed
1167 CK_RV P11AttrStartDate::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
1168 {
1169 // Attribute specific checks
1170
1171 if (ulValueLen !=sizeof(CK_DATE) && ulValueLen !=0)
1172 {
1173 return CKR_ATTRIBUTE_VALUE_INVALID;
1174 }
1175
1176 // Store data
1177 osobject->setAttribute(type, ByteString((unsigned char*)pValue, ulValueLen));
1178
1179 return CKR_OK;
1180 }
1181
1182 /*****************************************
1183 * CKA_END_DATE
1184 *****************************************/
1185
1186 // Set default value
1187 bool P11AttrEndDate::setDefault()
1188 {
1189 OSAttribute attr(ByteString(""));
1190 return osobject->setAttribute(type, attr);
1191 }
1192
1193 // Update the value if allowed
1194 CK_RV P11AttrEndDate::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
1195 {
1196 // Attribute specific checks
1197
1198 if (ulValueLen !=sizeof(CK_DATE) && ulValueLen !=0)
1199 {
1200 return CKR_ATTRIBUTE_VALUE_INVALID;
1201 }
1202
1203 // Store data
1204 osobject->setAttribute(type, ByteString((unsigned char*)pValue, ulValueLen));
1205
1206 return CKR_OK;
1207 }
1208
1209 /*****************************************
1210 * CKA_SERIAL_NUMBER
1211 *****************************************/
1212
1213 // Set default value
1214 bool P11AttrSerialNumber::setDefault()
1215 {
1216 OSAttribute attr(ByteString(""));
1217 return osobject->setAttribute(type, attr);
1218 }
1219
1220 /*****************************************
1221 * CKA_URL
1222 *****************************************/
1223
1224 // Set default value
1225 bool P11AttrURL::setDefault()
1226 {
1227 OSAttribute attr(ByteString(""));
1228 return osobject->setAttribute(type, attr);
1229 }
1230
1231 /*****************************************
1232 * CKA_HASH_OF_SUBJECT_PUBLIC_KEY
1233 *****************************************/
1234
1235 // Set default value
1236 bool P11AttrHashOfSubjectPublicKey::setDefault()
1237 {
1238 OSAttribute attr(ByteString(""));
1239 return osobject->setAttribute(type, attr);
1240 }
1241
1242 /*****************************************
1243 * CKA_HASH_OF_ISSUER_PUBLIC_KEY
1244 *****************************************/
1245
1246 // Set default value
1247 bool P11AttrHashOfIssuerPublicKey::setDefault()
1248 {
1249 OSAttribute attr(ByteString(""));
1250 return osobject->setAttribute(type, attr);
1251 }
1252
1253 /*****************************************
1254 * CKA_JAVA_MIDP_SECURITY_DOMAIN
1255 *****************************************/
1256
1257 // Set default value
1258 bool P11AttrJavaMidpSecurityDomain::setDefault()
1259 {
1260 OSAttribute attr((unsigned long)0);
1261 return osobject->setAttribute(type, attr);
1262 }
1263
1264 // Update the value if allowed
1265 CK_RV P11AttrJavaMidpSecurityDomain::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
1266 {
1267 // Attribute specific checks
1268
1269 if (ulValueLen !=sizeof(CK_ULONG))
1270 {
1271 return CKR_ATTRIBUTE_VALUE_INVALID;
1272 }
1273
1274 // Store data
1275 osobject->setAttribute(type, *(CK_ULONG*)pValue);
1276
1277 return CKR_OK;
1278 }
1279
1280 /*****************************************
1281 * CKA_NAME_HASH_ALGORITHM
1282 *****************************************/
1283
1284 // Set default value
1285 bool P11AttrNameHashAlgorithm::setDefault()
1286 {
1287 OSAttribute attr((unsigned long)CKM_SHA_1);
1288 return osobject->setAttribute(type, attr);
1289 }
1290
1291 // Update the value if allowed
1292 CK_RV P11AttrNameHashAlgorithm::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
1293 {
1294 // Attribute specific checks
1295
1296 if (ulValueLen !=sizeof(CK_ULONG))
1297 {
1298 return CKR_ATTRIBUTE_VALUE_INVALID;
1299 }
1300
1301 // Store data
1302 osobject->setAttribute(type, *(CK_ULONG*)pValue);
1303
1304 return CKR_OK;
1305 }
1306
1307 /*****************************************
1308 * CKA_DERIVE
1309 *****************************************/
1310
1311 // Set default value
1312 bool P11AttrDerive::setDefault()
1313 {
1314 OSAttribute attr(false);
1315 return osobject->setAttribute(type, attr);
1316 }
1317
1318 // Update the value if allowed
1319 CK_RV P11AttrDerive::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
1320 {
1321 OSAttribute attrTrue(true);
1322 OSAttribute attrFalse(false);
1323
1324 // Attribute specific checks
1325
1326 if (ulValueLen !=sizeof(CK_BBOOL))
1327 {
1328 return CKR_ATTRIBUTE_VALUE_INVALID;
1329 }
1330
1331 // Store data
1332
1333 if (*(CK_BBOOL*)pValue == CK_FALSE)
1334 {
1335 osobject->setAttribute(type, attrFalse);
1336 }
1337 else
1338 {
1339 osobject->setAttribute(type, attrTrue);
1340 }
1341
1342 return CKR_OK;
1343 }
1344
1345 /*****************************************
1346 * CKA_ENCRYPT
1347 *****************************************/
1348
1349 // Set default value
1350 bool P11AttrEncrypt::setDefault()
1351 {
1352 OSAttribute attr(true);
1353 return osobject->setAttribute(type, attr);
1354 }
1355
1356 // Update the value if allowed
1357 CK_RV P11AttrEncrypt::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
1358 {
1359 OSAttribute attrTrue(true);
1360 OSAttribute attrFalse(false);
1361
1362 // Attribute specific checks
1363
1364 if (ulValueLen !=sizeof(CK_BBOOL))
1365 {
1366 return CKR_ATTRIBUTE_VALUE_INVALID;
1367 }
1368
1369 // Store data
1370
1371 if (*(CK_BBOOL*)pValue == CK_FALSE)
1372 {
1373 osobject->setAttribute(type, attrFalse);
1374 }
1375 else
1376 {
1377 osobject->setAttribute(type, attrTrue);
1378 }
1379
1380 return CKR_OK;
1381 }
1382
1383 /*****************************************
1384 * CKA_VERIFY
1385 *****************************************/
1386
1387 // Set default value
1388 bool P11AttrVerify::setDefault()
1389 {
1390 OSAttribute attr(true);
1391 return osobject->setAttribute(type, attr);
1392 }
1393
1394 // Update the value if allowed
1395 CK_RV P11AttrVerify::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
1396 {
1397 OSAttribute attrTrue(true);
1398 OSAttribute attrFalse(false);
1399
1400 // Attribute specific checks
1401
1402 if (ulValueLen !=sizeof(CK_BBOOL))
1403 {
1404 return CKR_ATTRIBUTE_VALUE_INVALID;
1405 }
1406
1407 // Store data
1408
1409 if (*(CK_BBOOL*)pValue == CK_FALSE)
1410 {
1411 osobject->setAttribute(type, attrFalse);
1412 }
1413 else
1414 {
1415 osobject->setAttribute(type, attrTrue);
1416 }
1417
1418 return CKR_OK;
1419 }
1420
1421 /*****************************************
1422 * CKA_VERIFY_RECOVER
1423 *****************************************/
1424
1425 // Set default value
1426 bool P11AttrVerifyRecover::setDefault()
1427 {
1428 OSAttribute attr(true);
1429 return osobject->setAttribute(type, attr);
1430 }
1431
1432 // Update the value if allowed
1433 CK_RV P11AttrVerifyRecover::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
1434 {
1435 OSAttribute attrTrue(true);
1436 OSAttribute attrFalse(false);
1437
1438 // Attribute specific checks
1439
1440 if (ulValueLen !=sizeof(CK_BBOOL))
1441 {
1442 return CKR_ATTRIBUTE_VALUE_INVALID;
1443 }
1444
1445 // Store data
1446
1447 if (*(CK_BBOOL*)pValue == CK_FALSE)
1448 {
1449 osobject->setAttribute(type, attrFalse);
1450 }
1451 else
1452 {
1453 osobject->setAttribute(type, attrTrue);
1454 }
1455
1456 return CKR_OK;
1457 }
1458
1459 /*****************************************
1460 * CKA_WRAP
1461 *****************************************/
1462
1463 // Set default value
1464 bool P11AttrWrap::setDefault()
1465 {
1466 OSAttribute attr(true);
1467 return osobject->setAttribute(type, attr);
1468 }
1469
1470 // Update the value if allowed
1471 CK_RV P11AttrWrap::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
1472 {
1473 OSAttribute attrTrue(true);
1474 OSAttribute attrFalse(false);
1475
1476 // Attribute specific checks
1477
1478 if (ulValueLen !=sizeof(CK_BBOOL))
1479 {
1480 return CKR_ATTRIBUTE_VALUE_INVALID;
1481 }
1482
1483 // Store data
1484
1485 if (*(CK_BBOOL*)pValue == CK_FALSE)
1486 {
1487 osobject->setAttribute(type, attrFalse);
1488 }
1489 else
1490 {
1491 osobject->setAttribute(type, attrTrue);
1492 }
1493
1494 return CKR_OK;
1495 }
1496
1497 /*****************************************
1498 * CKA_DECRYPT
1499 *****************************************/
1500
1501 // Set default value
1502 bool P11AttrDecrypt::setDefault()
1503 {
1504 OSAttribute attr(true);
1505 return osobject->setAttribute(type, attr);
1506 }
1507
1508 // Update the value if allowed
1509 CK_RV P11AttrDecrypt::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
1510 {
1511 OSAttribute attrTrue(true);
1512 OSAttribute attrFalse(false);
1513
1514 // Attribute specific checks
1515
1516 if (ulValueLen !=sizeof(CK_BBOOL))
1517 {
1518 return CKR_ATTRIBUTE_VALUE_INVALID;
1519 }
1520
1521 // Store data
1522
1523 if (*(CK_BBOOL*)pValue == CK_FALSE)
1524 {
1525 osobject->setAttribute(type, attrFalse);
1526 }
1527 else
1528 {
1529 osobject->setAttribute(type, attrTrue);
1530 }
1531
1532 return CKR_OK;
1533 }
1534
1535 /*****************************************
1536 * CKA_SIGN
1537 *****************************************/
1538
1539 // Set default value
1540 bool P11AttrSign::setDefault()
1541 {
1542 OSAttribute attr(true);
1543 return osobject->setAttribute(type, attr);
1544 }
1545
1546 // Update the value if allowed
1547 CK_RV P11AttrSign::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
1548 {
1549 OSAttribute attrTrue(true);
1550 OSAttribute attrFalse(false);
1551
1552 // Attribute specific checks
1553
1554 if (ulValueLen !=sizeof(CK_BBOOL))
1555 {
1556 return CKR_ATTRIBUTE_VALUE_INVALID;
1557 }
1558
1559 // Store data
1560
1561 if (*(CK_BBOOL*)pValue == CK_FALSE)
1562 {
1563 osobject->setAttribute(type, attrFalse);
1564 }
1565 else
1566 {
1567 osobject->setAttribute(type, attrTrue);
1568 }
1569
1570 return CKR_OK;
1571 }
1572
1573 /*****************************************
1574 * CKA_SIGN_RECOVER
1575 *****************************************/
1576
1577 // Set default value
1578 bool P11AttrSignRecover::setDefault()
1579 {
1580 OSAttribute attr(true);
1581 return osobject->setAttribute(type, attr);
1582 }
1583
1584 // Update the value if allowed
1585 CK_RV P11AttrSignRecover::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
1586 {
1587 OSAttribute attrTrue(true);
1588 OSAttribute attrFalse(false);
1589
1590 // Attribute specific checks
1591
1592 if (ulValueLen !=sizeof(CK_BBOOL))
1593 {
1594 return CKR_ATTRIBUTE_VALUE_INVALID;
1595 }
1596
1597 // Store data
1598
1599 if (*(CK_BBOOL*)pValue == CK_FALSE)
1600 {
1601 osobject->setAttribute(type, attrFalse);
1602 }
1603 else
1604 {
1605 osobject->setAttribute(type, attrTrue);
1606 }
1607
1608 return CKR_OK;
1609 }
1610
1611 /*****************************************
1612 * CKA_UNWRAP
1613 *****************************************/
1614
1615 // Set default value
1616 bool P11AttrUnwrap::setDefault()
1617 {
1618 OSAttribute attr(true);
1619 return osobject->setAttribute(type, attr);
1620 }
1621
1622 // Update the value if allowed
1623 CK_RV P11AttrUnwrap::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
1624 {
1625 OSAttribute attrTrue(true);
1626 OSAttribute attrFalse(false);
1627
1628 // Attribute specific checks
1629
1630 if (ulValueLen !=sizeof(CK_BBOOL))
1631 {
1632 return CKR_ATTRIBUTE_VALUE_INVALID;
1633 }
1634
1635 // Store data
1636
1637 if (*(CK_BBOOL*)pValue == CK_FALSE)
1638 {
1639 osobject->setAttribute(type, attrFalse);
1640 }
1641 else
1642 {
1643 osobject->setAttribute(type, attrTrue);
1644 }
1645
1646 return CKR_OK;
1647 }
1648
1649 /*****************************************
1650 * CKA_LOCAL
1651 *****************************************/
1652
1653 // Set default value
1654 bool P11AttrLocal::setDefault()
1655 {
1656 OSAttribute attr(false);
1657 return osobject->setAttribute(type, attr);
1658 }
1659
1660 // Update the value if allowed
1661 CK_RV P11AttrLocal::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR /*pValue*/, CK_ULONG /*ulValueLen*/, int /*op*/)
1662 {
1663 return CKR_ATTRIBUTE_READ_ONLY;
1664 }
1665
1666 /*****************************************
1667 * CKA_KEY_GEN_MECHANISM
1668 *****************************************/
1669
1670 // Set default value
1671 bool P11AttrKeyGenMechanism::setDefault()
1672 {
1673 OSAttribute attr((unsigned long)CK_UNAVAILABLE_INFORMATION);
1674 return osobject->setAttribute(type, attr);
1675 }
1676
1677 // Update the value if allowed
1678 CK_RV P11AttrKeyGenMechanism::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR /*pValue*/, CK_ULONG /*ulValueLen*/, int /*op*/)
1679 {
1680 return CKR_ATTRIBUTE_READ_ONLY;
1681 }
1682
1683 /*****************************************
1684 * CKA_ALWAYS_SENSITIVE
1685 *****************************************/
1686
1687 // Set default value
1688 bool P11AttrAlwaysSensitive::setDefault()
1689 {
1690 OSAttribute attr(false);
1691 return osobject->setAttribute(type, attr);
1692 }
1693
1694 // Update the value if allowed
1695 CK_RV P11AttrAlwaysSensitive::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR /*pValue*/, CK_ULONG /*ulValueLen*/, int /*op*/)
1696 {
1697 return CKR_ATTRIBUTE_READ_ONLY;
1698 }
1699
1700 /*****************************************
1701 * CKA_NEVER_EXTRACTABLE
1702 *****************************************/
1703
1704 // Set default value
1705 bool P11AttrNeverExtractable::setDefault()
1706 {
1707 OSAttribute attr(true);
1708 return osobject->setAttribute(type, attr);
1709 }
1710
1711 // Update the value if allowed
1712 CK_RV P11AttrNeverExtractable::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR /*pValue*/, CK_ULONG /*ulValueLen*/, int /*op*/)
1713 {
1714 return CKR_ATTRIBUTE_READ_ONLY;
1715 }
1716
1717 /*****************************************
1718 * CKA_SENSITIVE
1719 *****************************************/
1720
1721 // Set default value
1722 bool P11AttrSensitive::setDefault()
1723 {
1724 // We default to false because we want to handle the secret keys in a correct way
1725 OSAttribute attr(false);
1726 return osobject->setAttribute(type, attr);
1727 }
1728
1729 // Update the value if allowed
1730 CK_RV P11AttrSensitive::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int op)
1731 {
1732 OSAttribute attrTrue(true);
1733 OSAttribute attrFalse(false);
1734
1735 // Attribute specific checks
1736
1737 if (op == OBJECT_OP_SET || op == OBJECT_OP_COPY)
1738 {
1739 if (osobject->getBooleanValue(CKA_SENSITIVE, false))
1740 {
1741 return CKR_ATTRIBUTE_READ_ONLY;
1742 }
1743 }
1744
1745 if (ulValueLen !=sizeof(CK_BBOOL))
1746 {
1747 return CKR_ATTRIBUTE_VALUE_INVALID;
1748 }
1749
1750 // Store data
1751
1752 if (*(CK_BBOOL*)pValue == CK_FALSE)
1753 {
1754 osobject->setAttribute(type, attrFalse);
1755 osobject->setAttribute(CKA_ALWAYS_SENSITIVE, attrFalse);
1756 }
1757 else
1758 {
1759 osobject->setAttribute(type, attrTrue);
1760
1761 // This is so that generated keys get the correct value
1762 if (op == OBJECT_OP_GENERATE || op == OBJECT_OP_DERIVE)
1763 {
1764 osobject->setAttribute(CKA_ALWAYS_SENSITIVE, attrTrue);
1765 }
1766 }
1767
1768 return CKR_OK;
1769 }
1770
1771 /*****************************************
1772 * CKA_EXTRACTABLE
1773 *****************************************/
1774
1775 // Set default value
1776 bool P11AttrExtractable::setDefault()
1777 {
1778 OSAttribute attr(false);
1779 return osobject->setAttribute(type, attr);
1780 }
1781
1782 // Update the value if allowed
1783 CK_RV P11AttrExtractable::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int op)
1784 {
1785 OSAttribute attrTrue(true);
1786 OSAttribute attrFalse(false);
1787
1788 // Attribute specific checks
1789
1790 if (op == OBJECT_OP_SET || op == OBJECT_OP_COPY)
1791 {
1792 if (osobject->getBooleanValue(CKA_EXTRACTABLE, false) == false)
1793 {
1794 return CKR_ATTRIBUTE_READ_ONLY;
1795 }
1796 }
1797
1798 if (ulValueLen !=sizeof(CK_BBOOL))
1799 {
1800 return CKR_ATTRIBUTE_VALUE_INVALID;
1801 }
1802
1803 // Store data
1804
1805 if (*(CK_BBOOL*)pValue == CK_FALSE)
1806 {
1807 osobject->setAttribute(type, attrFalse);
1808 }
1809 else
1810 {
1811 osobject->setAttribute(type, attrTrue);
1812 osobject->setAttribute(CKA_NEVER_EXTRACTABLE, attrFalse);
1813 }
1814
1815 return CKR_OK;
1816 }
1817
1818 /*****************************************
1819 * CKA_WRAP_WITH_TRUSTED
1820 *****************************************/
1821
1822 // Set default value
1823 bool P11AttrWrapWithTrusted::setDefault()
1824 {
1825 OSAttribute attr(false);
1826 return osobject->setAttribute(type, attr);
1827 }
1828
1829 // Update the value if allowed
1830 CK_RV P11AttrWrapWithTrusted::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int op)
1831 {
1832 OSAttribute attrTrue(true);
1833 OSAttribute attrFalse(false);
1834
1835 // Attribute specific checks
1836
1837 if (op == OBJECT_OP_SET || op == OBJECT_OP_COPY)
1838 {
1839 if (osobject->getBooleanValue(CKA_WRAP_WITH_TRUSTED, false))
1840 {
1841 return CKR_ATTRIBUTE_READ_ONLY;
1842 }
1843 }
1844
1845 if (ulValueLen !=sizeof(CK_BBOOL))
1846 {
1847 return CKR_ATTRIBUTE_VALUE_INVALID;
1848 }
1849
1850 // Store data
1851
1852 if (*(CK_BBOOL*)pValue == CK_FALSE)
1853 {
1854 osobject->setAttribute(type, attrFalse);
1855 }
1856 else
1857 {
1858 osobject->setAttribute(type, attrTrue);
1859 }
1860
1861 return CKR_OK;
1862 }
1863
1864 /*****************************************
1865 * CKA_ALWAYS_AUTHENTICATE
1866 *****************************************/
1867
1868 // Set default value
1869 bool P11AttrAlwaysAuthenticate::setDefault()
1870 {
1871 OSAttribute attr(false);
1872 return osobject->setAttribute(type, attr);
1873 }
1874
1875 // Update the value if allowed
1876 CK_RV P11AttrAlwaysAuthenticate::updateAttr(Token* /*token*/, bool isPrivate, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
1877 {
1878 OSAttribute attrTrue(true);
1879 OSAttribute attrFalse(false);
1880
1881 // Attribute specific checks
1882
1883 if (ulValueLen !=sizeof(CK_BBOOL))
1884 {
1885 return CKR_ATTRIBUTE_VALUE_INVALID;
1886 }
1887
1888 // Store data
1889
1890 if (*(CK_BBOOL*)pValue == CK_FALSE)
1891 {
1892 osobject->setAttribute(type, attrFalse);
1893 }
1894 else
1895 {
1896 if (!isPrivate)
1897 {
1898 return CKR_TEMPLATE_INCONSISTENT;
1899 }
1900
1901 osobject->setAttribute(type, attrTrue);
1902 }
1903
1904 return CKR_OK;
1905 }
1906
1907 /*****************************************
1908 * CKA_MODULUS
1909 *****************************************/
1910
1911 // Set default value
1912 bool P11AttrModulus::setDefault()
1913 {
1914 OSAttribute attr(ByteString(""));
1915 return osobject->setAttribute(type, attr);
1916 }
1917
1918 // Update the value if allowed
1919 CK_RV P11AttrModulus::updateAttr(Token *token, bool isPrivate, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int op)
1920 {
1921 ByteString plaintext((unsigned char*)pValue, ulValueLen);
1922 ByteString value;
1923
1924 // Encrypt
1925
1926 if (isPrivate)
1927 {
1928 if (!token->encrypt(plaintext, value))
1929 return CKR_GENERAL_ERROR;
1930 }
1931 else
1932 value = plaintext;
1933
1934 // Attribute specific checks
1935
1936 if (value.size() < ulValueLen)
1937 return CKR_GENERAL_ERROR;
1938
1939 // Store data
1940
1941 osobject->setAttribute(type, value);
1942
1943 // Set the CKA_MODULUS_BITS during C_CreateObject
1944
1945 if (op == OBJECT_OP_CREATE && osobject->attributeExists(CKA_MODULUS_BITS))
1946 {
1947 OSAttribute bits((unsigned long)plaintext.bits());
1948 osobject->setAttribute(CKA_MODULUS_BITS, bits);
1949 }
1950
1951 return CKR_OK;
1952 }
1953
1954 /*****************************************
1955 * CKA_PUBLIC_EXPONENT
1956 *****************************************/
1957
1958 // Set default value
1959 bool P11AttrPublicExponent::setDefault()
1960 {
1961 OSAttribute attr(ByteString(""));
1962 return osobject->setAttribute(type, attr);
1963 }
1964
1965 /*****************************************
1966 * CKA_PRIVATE_EXPONENT
1967 *****************************************/
1968
1969 // Set default value
1970 bool P11AttrPrivateExponent::setDefault()
1971 {
1972 OSAttribute attr(ByteString(""));
1973 return osobject->setAttribute(type, attr);
1974 }
1975
1976 /*****************************************
1977 * CKA_PRIME_1
1978 *****************************************/
1979
1980 // Set default value
1981 bool P11AttrPrime1::setDefault()
1982 {
1983 OSAttribute attr(ByteString(""));
1984 return osobject->setAttribute(type, attr);
1985 }
1986
1987 /*****************************************
1988 * CKA_PRIME_2
1989 *****************************************/
1990
1991 // Set default value
1992 bool P11AttrPrime2::setDefault()
1993 {
1994 OSAttribute attr(ByteString(""));
1995 return osobject->setAttribute(type, attr);
1996 }
1997
1998 /*****************************************
1999 * CKA_EXPONENT_1
2000 *****************************************/
2001
2002 // Set default value
2003 bool P11AttrExponent1::setDefault()
2004 {
2005 OSAttribute attr(ByteString(""));
2006 return osobject->setAttribute(type, attr);
2007 }
2008
2009 /*****************************************
2010 * CKA_EXPONENT_2
2011 *****************************************/
2012
2013 // Set default value
2014 bool P11AttrExponent2::setDefault()
2015 {
2016 OSAttribute attr(ByteString(""));
2017 return osobject->setAttribute(type, attr);
2018 }
2019
2020 /*****************************************
2021 * CKA_COEFFICIENT
2022 *****************************************/
2023
2024 // Set default value
2025 bool P11AttrCoefficient::setDefault()
2026 {
2027 OSAttribute attr(ByteString(""));
2028 return osobject->setAttribute(type, attr);
2029 }
2030
2031 /*****************************************
2032 * CKA_MODULUS_BITS
2033 *****************************************/
2034
2035 // Set default value
2036 bool P11AttrModulusBits::setDefault()
2037 {
2038 OSAttribute attr((unsigned long)0);
2039 return osobject->setAttribute(type, attr);
2040 }
2041
2042 // Update the value if allowed
2043 CK_RV P11AttrModulusBits::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int op)
2044 {
2045 // Attribute specific checks
2046
2047 if (op != OBJECT_OP_GENERATE)
2048 {
2049 return CKR_ATTRIBUTE_READ_ONLY;
2050 }
2051
2052 if (ulValueLen !=sizeof(CK_ULONG))
2053 {
2054 return CKR_ATTRIBUTE_VALUE_INVALID;
2055 }
2056
2057 // Store data
2058
2059 osobject->setAttribute(type, *(CK_ULONG*)pValue);
2060
2061 return CKR_OK;
2062 }
2063
2064 /*****************************************
2065 * CKA_PRIME
2066 *****************************************/
2067
2068 // Set default value
2069 bool P11AttrPrime::setDefault()
2070 {
2071 OSAttribute attr(ByteString(""));
2072 return osobject->setAttribute(type, attr);
2073 }
2074
2075 // Update the value if allowed
2076 CK_RV P11AttrPrime::updateAttr(Token *token, bool isPrivate, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int op)
2077 {
2078 ByteString plaintext((unsigned char*)pValue, ulValueLen);
2079 ByteString value;
2080
2081 // Encrypt
2082
2083 if (isPrivate)
2084 {
2085 if (!token->encrypt(plaintext, value))
2086 return CKR_GENERAL_ERROR;
2087 }
2088 else
2089 value = plaintext;
2090
2091 // Attribute specific checks
2092
2093 if (value.size() < ulValueLen)
2094 return CKR_GENERAL_ERROR;
2095
2096 // Store data
2097
2098 osobject->setAttribute(type, value);
2099
2100 // Set the CKA_PRIME_BITS during C_CreateObject
2101
2102 if (op == OBJECT_OP_CREATE && osobject->attributeExists(CKA_PRIME_BITS))
2103 {
2104 OSAttribute bits((unsigned long)plaintext.bits());
2105 osobject->setAttribute(CKA_PRIME_BITS, bits);
2106 }
2107
2108 return CKR_OK;
2109 }
2110
2111 /*****************************************
2112 * CKA_SUBPRIME
2113 *****************************************/
2114
2115 // Set default value
2116 bool P11AttrSubPrime::setDefault()
2117 {
2118 OSAttribute attr(ByteString(""));
2119 return osobject->setAttribute(type, attr);
2120 }
2121
2122 /*****************************************
2123 * CKA_BASE
2124 *****************************************/
2125
2126 // Set default value
2127 bool P11AttrBase::setDefault()
2128 {
2129 OSAttribute attr(ByteString(""));
2130 return osobject->setAttribute(type, attr);
2131 }
2132
2133 /*****************************************
2134 * CKA_PRIME_BITS
2135 *****************************************/
2136
2137 // Set default value
2138 bool P11AttrPrimeBits::setDefault()
2139 {
2140 OSAttribute attr((unsigned long)0);
2141 return osobject->setAttribute(type, attr);
2142 }
2143
2144 // Update the value if allowed
2145 CK_RV P11AttrPrimeBits::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int op)
2146 {
2147 // Attribute specific checks
2148
2149 if (op != OBJECT_OP_GENERATE)
2150 {
2151 return CKR_ATTRIBUTE_READ_ONLY;
2152 }
2153
2154 if (ulValueLen != sizeof(CK_ULONG))
2155 {
2156 return CKR_ATTRIBUTE_VALUE_INVALID;
2157 }
2158
2159 // Store data
2160
2161 osobject->setAttribute(type, *(CK_ULONG*)pValue);
2162
2163 return CKR_OK;
2164 }
2165
2166 /*****************************************
2167 * CKA_VALUE_BITS
2168 *****************************************/
2169
2170 // Set default value
2171 bool P11AttrValueBits::setDefault()
2172 {
2173 OSAttribute attr((unsigned long)0);
2174 return osobject->setAttribute(type, attr);
2175 }
2176
2177 // Update the value if allowed
2178 CK_RV P11AttrValueBits::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int op)
2179 {
2180 // Attribute specific checks
2181
2182 if (op != OBJECT_OP_GENERATE)
2183 {
2184 return CKR_ATTRIBUTE_READ_ONLY;
2185 }
2186
2187 if (ulValueLen != sizeof(CK_ULONG))
2188 {
2189 return CKR_ATTRIBUTE_VALUE_INVALID;
2190 }
2191
2192 // Store data
2193
2194 osobject->setAttribute(type, *(CK_ULONG*)pValue);
2195
2196 return CKR_OK;
2197 }
2198
2199 /*****************************************
2200 * CKA_EC_PARAMS
2201 *****************************************/
2202
2203 // Set default value
2204 bool P11AttrEcParams::setDefault()
2205 {
2206 OSAttribute attr(ByteString(""));
2207 return osobject->setAttribute(type, attr);
2208 }
2209
2210 /*****************************************
2211 * CKA_EC_POINT
2212 *****************************************/
2213
2214 // Set default value
2215 bool P11AttrEcPoint::setDefault()
2216 {
2217 OSAttribute attr(ByteString(""));
2218 return osobject->setAttribute(type, attr);
2219 }
2220
2221 /*****************************************
2222 * CKA_GOSTR3410_PARAMS
2223 *****************************************/
2224
2225 // Set default value
2226 bool P11AttrGostR3410Params::setDefault()
2227 {
2228 OSAttribute attr(ByteString(""));
2229 return osobject->setAttribute(type, attr);
2230 }
2231
2232 /*****************************************
2233 * CKA_GOSTR3411_PARAMS
2234 *****************************************/
2235
2236 // Set default value
2237 bool P11AttrGostR3411Params::setDefault()
2238 {
2239 OSAttribute attr(ByteString(""));
2240 return osobject->setAttribute(type, attr);
2241 }
2242
2243 /*****************************************
2244 * CKA_GOST28147_PARAMS
2245 *****************************************/
2246
2247 // Set default value
2248 bool P11AttrGost28147Params::setDefault()
2249 {
2250 OSAttribute attr(ByteString(""));
2251 return osobject->setAttribute(type, attr);
2252 }
2253
2254 /*****************************************
2255 * CKA_VALUE_LEN
2256 *****************************************/
2257
2258 // Set default value
2259 bool P11AttrValueLen::setDefault()
2260 {
2261 OSAttribute attr((unsigned long)0);
2262 return osobject->setAttribute(type, attr);
2263 }
2264
2265 // Update the value if allowed
2266 CK_RV P11AttrValueLen::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int op)
2267 {
2268 // Attribute specific checks
2269
2270 if (op != OBJECT_OP_GENERATE && op != OBJECT_OP_DERIVE)
2271 {
2272 return CKR_ATTRIBUTE_READ_ONLY;
2273 }
2274
2275 if (ulValueLen != sizeof(CK_ULONG))
2276 {
2277 return CKR_ATTRIBUTE_VALUE_INVALID;
2278 }
2279
2280 // Store data
2281
2282 osobject->setAttribute(type, *(CK_ULONG*)pValue);
2283
2284 return CKR_OK;
2285 }
2286
2287 /*****************************************
2288 * CKA_WRAP_TEMPLATE
2289 *****************************************/
2290
2291 // Set default value
2292 bool P11AttrWrapTemplate::setDefault()
2293 {
2294 std::map<CK_ATTRIBUTE_TYPE,OSAttribute> empty;
2295 OSAttribute attr(empty);
2296 return osobject->setAttribute(type, attr);
2297 }
2298
2299 // Update the value
2300 CK_RV P11AttrWrapTemplate::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
2301 {
2302 // Attribute specific checks
2303 if ((ulValueLen % sizeof(CK_ATTRIBUTE)) != 0)
2304 {
2305 return CKR_ATTRIBUTE_VALUE_INVALID;
2306 }
2307
2308 // Fill the template vector with elements
2309 CK_ATTRIBUTE_PTR attr = (CK_ATTRIBUTE_PTR) pValue;
2310 std::map<CK_ATTRIBUTE_TYPE,OSAttribute> data;
2311 for (size_t i = 0; i < ulValueLen / sizeof(CK_ATTRIBUTE); ++i, ++attr)
2312 // Specialization for known attributes
2313 switch (attr->type)
2314 {
2315 case CKA_TOKEN:
2316 case CKA_PRIVATE:
2317 case CKA_MODIFIABLE:
2318 case CKA_COPYABLE:
2319 case CKA_TRUSTED:
2320 case CKA_ENCRYPT:
2321 case CKA_DECRYPT:
2322 case CKA_SIGN:
2323 case CKA_SIGN_RECOVER:
2324 case CKA_VERIFY:
2325 case CKA_VERIFY_RECOVER:
2326 case CKA_WRAP:
2327 case CKA_UNWRAP:
2328 case CKA_DERIVE:
2329 case CKA_LOCAL:
2330 case CKA_ALWAYS_SENSITIVE:
2331 case CKA_SENSITIVE:
2332 case CKA_NEVER_EXTRACTABLE:
2333 case CKA_EXTRACTABLE:
2334 case CKA_WRAP_WITH_TRUSTED:
2335 case CKA_SECONDARY_AUTH:
2336 case CKA_ALWAYS_AUTHENTICATE:
2337 {
2338 // CK_BBOOL
2339 if (attr->ulValueLen != sizeof(CK_BBOOL))
2340 return CKR_ATTRIBUTE_VALUE_INVALID;
2341 bool elem = (*(CK_BBOOL*)attr->pValue != CK_FALSE);
2342 data.insert(std::pair<CK_ATTRIBUTE_TYPE,OSAttribute> (attr->type, elem));
2343 }
2344 break;
2345
2346 case CKA_CLASS:
2347 case CKA_KEY_TYPE:
2348 case CKA_CERTIFICATE_TYPE:
2349 case CKA_CERTIFICATE_CATEGORY:
2350 case CKA_JAVA_MIDP_SECURITY_DOMAIN:
2351 case CKA_NAME_HASH_ALGORITHM:
2352 case CKA_KEY_GEN_MECHANISM:
2353 case CKA_MODULUS_BITS:
2354 case CKA_PRIME_BITS:
2355 case CKA_SUB_PRIME_BITS:
2356 case CKA_VALUE_BITS:
2357 case CKA_VALUE_LEN:
2358 case CKA_AUTH_PIN_FLAGS:
2359 {
2360 // CK_ULONG
2361 if (attr->ulValueLen != sizeof(CK_ULONG))
2362 return CKR_ATTRIBUTE_VALUE_INVALID;
2363 unsigned long elem = *(CK_ULONG*)attr->pValue;
2364 data.insert(std::pair<CK_ATTRIBUTE_TYPE,OSAttribute> (attr->type, elem));
2365 }
2366 break;
2367
2368 case CKA_WRAP_TEMPLATE:
2369 case CKA_UNWRAP_TEMPLATE:
2370 return CKR_ATTRIBUTE_VALUE_INVALID;
2371
2372 default:
2373 {
2374 // CK_BYTE
2375 ByteString elem = ByteString((unsigned char*)attr->pValue, attr->ulValueLen);
2376 data.insert(std::pair<CK_ATTRIBUTE_TYPE,OSAttribute> (attr->type, elem));
2377 }
2378 }
2379
2380 // Store data
2381 osobject->setAttribute(type, data);
2382
2383 return CKR_OK;
2384 }
2385
2386 /*****************************************
2387 * CKA_UNWRAP_TEMPLATE
2388 *****************************************/
2389
2390 // Set default value
2391 bool P11AttrUnwrapTemplate::setDefault()
2392 {
2393 std::map<CK_ATTRIBUTE_TYPE,OSAttribute> empty;
2394 OSAttribute attr(empty);
2395 return osobject->setAttribute(type, attr);
2396 }
2397
2398 // Update the value
2399 CK_RV P11AttrUnwrapTemplate::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
2400 {
2401 // Attribute specific checks
2402 if ((ulValueLen % sizeof(CK_ATTRIBUTE)) != 0)
2403 {
2404 return CKR_ATTRIBUTE_VALUE_INVALID;
2405 }
2406
2407 // Fill the template vector with elements
2408 CK_ATTRIBUTE_PTR attr = (CK_ATTRIBUTE_PTR) pValue;
2409 std::map<CK_ATTRIBUTE_TYPE,OSAttribute> data;
2410 for (size_t i = 0; i < ulValueLen / sizeof(CK_ATTRIBUTE); ++i, ++attr)
2411 // Specialization for known attributes
2412 switch (attr->type)
2413 {
2414 case CKA_TOKEN:
2415 case CKA_PRIVATE:
2416 case CKA_MODIFIABLE:
2417 case CKA_COPYABLE:
2418 case CKA_TRUSTED:
2419 case CKA_ENCRYPT:
2420 case CKA_DECRYPT:
2421 case CKA_SIGN:
2422 case CKA_SIGN_RECOVER:
2423 case CKA_VERIFY:
2424 case CKA_VERIFY_RECOVER:
2425 case CKA_WRAP:
2426 case CKA_UNWRAP:
2427 case CKA_DERIVE:
2428 case CKA_LOCAL:
2429 case CKA_ALWAYS_SENSITIVE:
2430 case CKA_SENSITIVE:
2431 case CKA_NEVER_EXTRACTABLE:
2432 case CKA_EXTRACTABLE:
2433 case CKA_WRAP_WITH_TRUSTED:
2434 case CKA_SECONDARY_AUTH:
2435 case CKA_ALWAYS_AUTHENTICATE:
2436 {
2437 // CK_BBOOL
2438 if (attr->ulValueLen != sizeof(CK_BBOOL))
2439 return CKR_ATTRIBUTE_VALUE_INVALID;
2440 bool elem = (*(CK_BBOOL*)attr->pValue != CK_FALSE);
2441 data.insert(std::pair<CK_ATTRIBUTE_TYPE,OSAttribute> (attr->type, elem));
2442 }
2443 break;
2444
2445 case CKA_CLASS:
2446 case CKA_KEY_TYPE:
2447 case CKA_CERTIFICATE_TYPE:
2448 case CKA_CERTIFICATE_CATEGORY:
2449 case CKA_JAVA_MIDP_SECURITY_DOMAIN:
2450 case CKA_NAME_HASH_ALGORITHM:
2451 case CKA_KEY_GEN_MECHANISM:
2452 case CKA_MODULUS_BITS:
2453 case CKA_PRIME_BITS:
2454 case CKA_SUB_PRIME_BITS:
2455 case CKA_VALUE_BITS:
2456 case CKA_VALUE_LEN:
2457 case CKA_AUTH_PIN_FLAGS:
2458 {
2459 // CK_ULONG
2460 if (attr->ulValueLen != sizeof(CK_ULONG))
2461 return CKR_ATTRIBUTE_VALUE_INVALID;
2462 unsigned long elem = *(CK_ULONG*)attr->pValue;
2463 data.insert(std::pair<CK_ATTRIBUTE_TYPE,OSAttribute> (attr->type, elem));
2464 }
2465 break;
2466
2467 case CKA_WRAP_TEMPLATE:
2468 case CKA_UNWRAP_TEMPLATE:
2469 return CKR_ATTRIBUTE_VALUE_INVALID;
2470
2471 default:
2472 {
2473 // CK_BYTE
2474 ByteString elem = ByteString((unsigned char*)attr->pValue, attr->ulValueLen);
2475 data.insert(std::pair<CK_ATTRIBUTE_TYPE,OSAttribute> (attr->type, elem));
2476 }
2477 }
2478
2479 // Store data
2480 osobject->setAttribute(type, data);
2481
2482 return CKR_OK;
2483 }
2484
2485 /*****************************************
2486 * CKA_ALLOWED_MECHANISMS
2487 *****************************************/
2488
2489 // Set default value
2490 bool P11AttrAllowedMechanisms::setDefault()
2491 {
2492 std::set<CK_MECHANISM_TYPE> emptyMap;
2493 return osobject->setAttribute(type, OSAttribute(emptyMap));
2494 }
2495
2496 // Update the value if allowed
2497 CK_RV P11AttrAllowedMechanisms::updateAttr(Token* /*token*/, bool /*isPrivate*/, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
2498 {
2499 if (ulValueLen == 0 || (ulValueLen % sizeof(CK_MECHANISM_TYPE)) != 0)
2500 {
2501 return CKR_ATTRIBUTE_VALUE_INVALID;
2502 }
2503
2504 CK_MECHANISM_TYPE_PTR mechType = (CK_MECHANISM_TYPE_PTR) pValue;
2505
2506 // Fill the set with values
2507 std::set<CK_MECHANISM_TYPE> data;
2508 for (size_t i = 0; i < ulValueLen / sizeof(CK_MECHANISM_TYPE); ++i, ++mechType)
2509 {
2510 data.insert(*mechType);
2511 }
2512
2513 // Store data
2514 osobject->setAttribute(type, OSAttribute(data));
2515 return CKR_OK;
2516 }