Added a new Attribute to store TPM key handle
[aaf/sshsm.git] / SoftHSMv2 / src / lib / P11Objects.cpp
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  P11Objects.cpp
29
30  This class respresent a PKCS#11 object
31  *****************************************************************************/
32
33 #include "config.h"
34 #include "P11Objects.h"
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 // Constructor
39 P11Object::P11Object()
40 {
41         initialized = false;
42         osobject = NULL;
43 }
44
45 // Destructor
46 P11Object::~P11Object()
47 {
48         std::map<CK_ATTRIBUTE_TYPE, P11Attribute*> cleanUp = attributes;
49         attributes.clear();
50
51         for (std::map<CK_ATTRIBUTE_TYPE, P11Attribute*>::iterator i = cleanUp.begin(); i != cleanUp.end(); i++)
52         {
53                 if (i->second == NULL)
54                 {
55                         continue;
56                 }
57
58                 delete i->second;
59                 i->second = NULL;
60         }
61 }
62
63 // Add attributes
64 bool P11Object::init(OSObject *inobject)
65 {
66         if (initialized) return true;
67         if (inobject == NULL) return false;
68
69         osobject = inobject;
70
71         // Create attributes
72         P11Attribute* attrClass = new P11AttrClass(osobject);
73         P11Attribute* attrToken = new P11AttrToken(osobject);
74         P11Attribute* attrPrivate = new P11AttrPrivate(osobject);
75         P11Attribute* attrModifiable = new P11AttrModifiable(osobject);
76         P11Attribute* attrLabel = new P11AttrLabel(osobject);
77         P11Attribute* attrCopyable = new P11AttrCopyable(osobject);
78         P11Attribute* attrDestroyable = new P11AttrDestroyable(osobject);
79
80         // Initialize the attributes
81         if
82         (
83                 !attrClass->init() ||
84                 !attrToken->init() ||
85                 !attrPrivate->init() ||
86                 !attrModifiable->init() ||
87                 !attrLabel->init() ||
88                 !attrCopyable->init() ||
89                 !attrDestroyable->init()
90         )
91         {
92                 ERROR_MSG("Could not initialize the attribute");
93                 delete attrClass;
94                 delete attrToken;
95                 delete attrPrivate;
96                 delete attrModifiable;
97                 delete attrLabel;
98                 delete attrCopyable;
99                 delete attrDestroyable;
100                 return false;
101         }
102
103         // Add them to the map
104         attributes[attrClass->getType()] = attrClass;
105         attributes[attrToken->getType()] = attrToken;
106         attributes[attrPrivate->getType()] = attrPrivate;
107         attributes[attrModifiable->getType()] = attrModifiable;
108         attributes[attrLabel->getType()] = attrLabel;
109         attributes[attrCopyable->getType()] = attrCopyable;
110         attributes[attrDestroyable->getType()] = attrDestroyable;
111
112         initialized = true;
113         return true;
114 }
115
116 CK_RV P11Object::loadTemplate(Token *token, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount)
117 {
118         bool isPrivate = this->isPrivate();
119
120         // [PKCS#11 v2.40, C_GetAttributeValue]
121         // 1. If the specified attribute (i.e., the attribute specified by the
122         //    type field) for the object cannot be revealed because the object
123         //    is sensitive or unextractable, then the ulValueLen field in that
124         //    triple is modified to hold the value CK_UNAVAILABLE_INFORMATION.
125         //
126         // 2. Otherwise, if the specified value for the object is invalid (the
127         //    object does not possess such an attribute), then the ulValueLen
128         //    field in that triple is modified to hold the value
129         //    CK_UNAVAILABLE_INFORMATION.
130         //
131         // 3. Otherwise, if the pValue field has the value NULL_PTR, then the
132         //    ulValueLen field is modified to hold the exact length of the
133         //    specified attribute for the object.
134         //
135         // 4. Otherwise, if the length specified in ulValueLen is large enough
136         //    to hold the value of the specified attribute for the object,
137         //    then that attribute is copied into the buffer located at pValue,
138         //    and the ulValueLen field is modified to hold the exact length of
139         //    the attribute.
140         //
141         // 5. Otherwise, the ulValueLen field is modified to hold the value
142         //    CK_UNAVAILABLE_INFORMATION.
143
144         bool invalid = false, sensitive = false, buffer_too_small = false;
145
146         // If case 3 or 4 applies to all the requested attributes, then the call will return CKR_OK.
147         for (CK_ULONG i = 0; i < ulAttributeCount; ++i)
148         {
149                 P11Attribute* attr = attributes[pTemplate[i].type];
150
151                 // case 2 of the attribute checks
152                 if (attr == NULL) {
153                         pTemplate[i].ulValueLen = CK_UNAVAILABLE_INFORMATION;
154                         // If case 2 applies to any of the requested attributes, then the call should
155                         // return the value CKR_ATTRIBUTE_TYPE_INVALID.
156                         invalid = true;
157                         continue;
158                 }
159
160                 // case 1,3,4 and 5 of the attribute checks are done while retrieving the attribute itself.
161                 CK_RV retrieve_rv = attr->retrieve(token, isPrivate, pTemplate[i].pValue, &pTemplate[i].ulValueLen);
162                 if (retrieve_rv == CKR_ATTRIBUTE_SENSITIVE) {
163                         // If case 1 applies to any of the requested attributes, then the call should
164                         // return the value CKR_ATTRIBUTE_SENSITIVE.
165                         sensitive = true;
166                 } else if (retrieve_rv == CKR_BUFFER_TOO_SMALL) {
167                         // If case 5 applies to any of the requested attributes, then the call should
168                         // return the value CKR_BUFFER_TOO_SMALL.
169                         buffer_too_small = true;
170                 } else if (retrieve_rv != CKR_OK) {
171                     return CKR_GENERAL_ERROR;
172                 }
173
174         }
175
176         // As usual if more than one of these error codes is applicable, Cryptoki may
177         // return any of them. Only if none of them applies to any of the requested
178         // attributes will CKR_OK be returned. We choose to return the errors in
179         // the following priority, which is probably the most useful order.
180         if (sensitive)
181                 return CKR_ATTRIBUTE_SENSITIVE;
182         if (invalid)
183                 return CKR_ATTRIBUTE_TYPE_INVALID;
184         if (buffer_too_small)
185                 return CKR_BUFFER_TOO_SMALL;
186         return CKR_OK;
187 }
188
189 // Save template
190 CK_RV P11Object::saveTemplate(Token *token, bool isPrivate, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount, int op)
191 {
192         if (osobject == NULL)
193                 return CKR_GENERAL_ERROR;
194         if (osobject->startTransaction() == false)
195                 return CKR_GENERAL_ERROR;
196
197         if (op == OBJECT_OP_SET)
198         {
199                 if (!isModifiable())
200                 {
201                         osobject->abortTransaction();
202                         return CKR_ACTION_PROHIBITED;
203                 }
204         }
205
206         // [PKCS#11 v2.40, 4.1.3 Copying objects] OBJECT_OP_COPY
207         //    If the CKA_COPYABLE attribute of the object to be copied is set
208         //    to CK_FALSE, C_CopyObject returns CKR_ACTION_PROHIBITED.
209         if (op == OBJECT_OP_COPY)
210         {
211                 if (!isCopyable())
212                 {
213                         osobject->abortTransaction();
214                         return CKR_ACTION_PROHIBITED;
215                 }
216         }
217
218         for (CK_ULONG i = 0; i < ulAttributeCount; i++)
219         {
220                 // [PKCS#11 v2.40, 4.1.1 Creating objects] OBJECT_OP_CREATE | OBJECT_OP_SET | OBJECT_OP_COPY
221                 //    1. If the supplied template specifies a value for an invalid attribute, then the attempt
222                 //    should fail with the error code CKR_ATTRIBUTE_TYPE_INVALID. An attribute
223                 //    is valid if it is either one of the attributes described in the Cryptoki specification or an
224                 //    additional vendor-specific attribute supported by the library and token.
225                 P11Attribute* attr = attributes[pTemplate[i].type];
226
227                 if (attr == NULL)
228                 {
229                         osobject->abortTransaction();
230                         return CKR_ATTRIBUTE_TYPE_INVALID;
231                 }
232
233                 // Additonal checks are done while updating the attributes themselves.
234                 CK_RV rv = attr->update(token,isPrivate, pTemplate[i].pValue, pTemplate[i].ulValueLen, op);
235                 if (rv != CKR_OK)
236                 {
237                         osobject->abortTransaction();
238                         return rv;
239                 }
240         }
241
242         // [PKCS#11 v2.40, 4.1.1 Creating objects]
243         //    4. If the attribute values in the supplied template, together with any default attribute
244         //    values and any attribute values contributed to the object by the object-creation
245         //    function itself, are insufficient to fully specify the object to create, then the attempt
246         //    should fail with the error code CKR_TEMPLATE_INCOMPLETE.
247
248         // All attributes that have to be specified are marked as such in the specification.
249         // The following checks are relevant here:
250         for (std::map<CK_ATTRIBUTE_TYPE, P11Attribute*>::iterator i = attributes.begin(); i != attributes.end(); i++)
251         {
252                 CK_ULONG checks = i->second->getChecks();
253
254                 //  ck1  MUST be specified when object is created with C_CreateObject.
255                 //  ck3  MUST be specified when object is generated with C_GenerateKey or C_GenerateKeyPair.
256                 //  ck5  MUST be specified when object is unwrapped with C_UnwrapKey.
257                 if (((checks & P11Attribute::ck1) == P11Attribute::ck1 && op == OBJECT_OP_CREATE) ||
258                     ((checks & P11Attribute::ck3) == P11Attribute::ck3 && op == OBJECT_OP_GENERATE) ||
259                     ((checks & P11Attribute::ck5) == P11Attribute::ck5 && op == OBJECT_OP_UNWRAP))
260                 {
261                         bool isSpecified = false;
262
263                         for (CK_ULONG n = 0; n < ulAttributeCount; n++)
264                         {
265                                 if (i->first == pTemplate[n].type)
266                                 {
267                                         isSpecified = true;
268                                         break;
269                                 }
270                         }
271
272                         if (!isSpecified)
273                         {
274                                 ERROR_MSG("Mandatory attribute (0x%08X) was not specified in template", (unsigned int)i->first);
275
276                                 return CKR_TEMPLATE_INCOMPLETE;
277                         }
278                 }
279         }
280
281         // [PKCS#11 v2.40, 4.1.1 Creating objects]
282         //    5. If the attribute values in the supplied template, together with any default attribute
283         //    values and any attribute values contributed to the object by the object-creation
284         //    function itself, are inconsistent, then the attempt should fail with the error code
285         //    CKR_TEMPLATE_INCONSISTENT. A set of attribute values is inconsistent if not
286         //    all of its members can be satisfied simultaneously by the token, although each value
287         //    individually is valid in Cryptoki. One example of an inconsistent template would be
288         //    using a template which specifies two different values for the same attribute. Another
289         //    example would be trying to create a secret key object with an attribute which is
290         //    appropriate for various types of public keys or private keys, but not for secret keys.
291         //    A final example would be a template with an attribute that violates some token
292         //    specific requirement. Note that this final example of an inconsistent template is
293         //    token-dependent—on a different token, such a template might not be inconsistent.
294
295         if (osobject->commitTransaction() == false)
296         {
297                 return CKR_GENERAL_ERROR;
298         }
299
300         return CKR_OK;
301 }
302
303 bool P11Object::isPrivate()
304 {
305         // Get the CKA_PRIVATE attribute, when the attribute is
306         // not present return the default value which we have
307         // chosen to be CK_FALSE.
308         if (!osobject->attributeExists(CKA_PRIVATE)) return false;
309
310         return osobject->getBooleanValue(CKA_PRIVATE, false);
311 }
312
313 bool P11Object::isCopyable()
314 {
315         // Get the CKA_COPYABLE attribute, when the attribute is not
316         // present return the default value which is CK_TRUE.
317         if (!osobject->attributeExists(CKA_COPYABLE)) return true;
318
319         return osobject->getBooleanValue(CKA_COPYABLE, true);
320 }
321
322 bool P11Object::isModifiable()
323 {
324         // Get the CKA_MODIFIABLE attribute, when the attribute is
325         // not present return the default value which is CK_TRUE.
326         if (!osobject->attributeExists(CKA_MODIFIABLE)) return true;
327
328         return osobject->getBooleanValue(CKA_MODIFIABLE, true);
329 }
330
331 // Constructor
332 P11DataObj::P11DataObj()
333 {
334         initialized = false;
335 }
336
337 // Add attributes
338 bool P11DataObj::init(OSObject *inobject)
339 {
340         if (initialized) return true;
341         if (inobject == NULL) return false;
342
343         // Set default values for attributes that will be introduced in the parent
344         if (!inobject->attributeExists(CKA_CLASS) || inobject->getUnsignedLongValue(CKA_CLASS, CKO_VENDOR_DEFINED) != CKO_DATA) {
345                 OSAttribute setClass((unsigned long)CKO_DATA);
346                 inobject->setAttribute(CKA_CLASS, setClass);
347         }
348
349         // Create parent
350         if (!P11Object::init(inobject)) return false;
351
352         // Create attributes
353         P11Attribute* attrApplication = new P11AttrApplication(osobject);
354         P11Attribute* attrObjectID = new P11AttrObjectID(osobject);
355         // NOTE: There is no mention in the PKCS#11 v2.40 spec that for a Data
356         //  Object the CKA_VALUE attribute may be modified after creation!
357         //  Therefore we assume it is not allowed to change the CKA_VALUE
358         //  attribute of a Data Object.
359         P11Attribute* attrValue = new P11AttrValue(osobject,0);
360
361         // Initialize the attributes
362         if
363         (
364                 !attrApplication->init() ||
365                 !attrObjectID->init() ||
366                 !attrValue->init()
367         )
368         {
369                 ERROR_MSG("Could not initialize the attribute");
370                 delete attrApplication;
371                 delete attrObjectID;
372                 delete attrValue;
373                 return false;
374         }
375
376         // Add them to the map
377         attributes[attrApplication->getType()] = attrApplication;
378         attributes[attrObjectID->getType()] = attrObjectID;
379         attributes[attrValue->getType()] = attrValue;
380
381         initialized = true;
382         return true;
383 }
384
385 // Constructor
386 P11CertificateObj::P11CertificateObj()
387 {
388         initialized = false;
389 }
390
391 // Add attributes
392 bool P11CertificateObj::init(OSObject *inobject)
393 {
394         if (initialized) return true;
395         if (inobject == NULL) return false;
396
397         // Set default values for attributes that will be introduced in the parent
398         if (!inobject->attributeExists(CKA_CLASS) || inobject->getUnsignedLongValue(CKA_CLASS, CKO_VENDOR_DEFINED) != CKO_CERTIFICATE) {
399                 OSAttribute setClass((unsigned long)CKO_CERTIFICATE);
400                 inobject->setAttribute(CKA_CLASS, setClass);
401         }
402         // Make certificates public
403         if (!inobject->attributeExists(CKA_PRIVATE)) {
404                 OSAttribute setPrivate(false);
405                 inobject->setAttribute(CKA_PRIVATE, setPrivate);
406         }
407
408         // Create parent
409         if (!P11Object::init(inobject)) return false;
410
411         // Create attributes
412         P11Attribute* attrCertificateType = new P11AttrCertificateType(osobject);
413         P11Attribute* attrTrusted = new P11AttrTrusted(osobject);
414         P11Attribute* attrCertificateCategory = new P11AttrCertificateCategory(osobject);
415         // NOTE: Because these attributes are used in a certificate object
416         //  where the CKA_VALUE containing the certificate data is not
417         //  modifiable, we assume that this attribute is also not modifiable.
418         //  There is also no explicit mention of these attributes being modifiable.
419         P11Attribute* attrCheckValue = new P11AttrCheckValue(osobject, 0);
420         P11Attribute* attrStartDate = new P11AttrStartDate(osobject,0);
421         P11Attribute* attrEndDate = new P11AttrEndDate(osobject,0);
422         // TODO: CKA_PUBLIC_KEY_INFO is accepted, but we do not calculate it.
423         P11Attribute* attrPublicKeyInfo = new P11AttrPublicKeyInfo(osobject,0);
424
425         // Initialize the attributes
426         if
427         (
428                 !attrCertificateType->init() ||
429                 !attrTrusted->init() ||
430                 !attrCertificateCategory->init() ||
431                 !attrCheckValue->init() ||
432                 !attrStartDate->init() ||
433                 !attrEndDate->init() ||
434                 !attrPublicKeyInfo->init()
435         )
436         {
437                 ERROR_MSG("Could not initialize the attribute");
438                 delete attrCertificateType;
439                 delete attrTrusted;
440                 delete attrCertificateCategory;
441                 delete attrCheckValue;
442                 delete attrStartDate;
443                 delete attrEndDate;
444                 delete attrPublicKeyInfo;
445                 return false;
446         }
447
448         // Add them to the map
449         attributes[attrCertificateType->getType()] = attrCertificateType;
450         attributes[attrTrusted->getType()] = attrTrusted;
451         attributes[attrCertificateCategory->getType()] = attrCertificateCategory;
452         attributes[attrCheckValue->getType()] = attrCheckValue;
453         attributes[attrStartDate->getType()] = attrStartDate;
454         attributes[attrEndDate->getType()] = attrEndDate;
455         attributes[attrPublicKeyInfo->getType()] = attrPublicKeyInfo;
456
457         initialized = true;
458         return true;
459 }
460
461 // Constructor
462 P11X509CertificateObj::P11X509CertificateObj()
463 {
464         initialized = false;
465 }
466
467 // Add attributes
468 bool P11X509CertificateObj::init(OSObject *inobject)
469 {
470         if (initialized) return true;
471         if (inobject == NULL) return false;
472
473         // Set default values for attributes that will be introduced in the parent
474         if (!inobject->attributeExists(CKA_CERTIFICATE_TYPE) || inobject->getUnsignedLongValue(CKA_CERTIFICATE_TYPE, CKC_VENDOR_DEFINED) != CKC_X_509) {
475                 OSAttribute setCertType((unsigned long)CKC_X_509);
476                 inobject->setAttribute(CKA_CERTIFICATE_TYPE, setCertType);
477         }
478
479         // Create parent
480         if (!P11CertificateObj::init(inobject)) return false;
481
482         // Create attributes
483         P11Attribute* attrSubject = new P11AttrSubject(osobject,P11Attribute::ck1);
484         P11Attribute* attrID = new P11AttrID(osobject);
485         P11Attribute* attrIssuer = new P11AttrIssuer(osobject);
486         P11Attribute* attrSerialNumber = new P11AttrSerialNumber(osobject);
487         P11Attribute* attrValue = new P11AttrValue(osobject,P11Attribute::ck1|P11Attribute::ck14);
488         P11Attribute* attrURL = new P11AttrURL(osobject);
489         P11Attribute* attrHashOfSubjectPublicKey = new P11AttrHashOfSubjectPublicKey(osobject);
490         P11Attribute* attrHashOfIssuerPublicKey = new P11AttrHashOfIssuerPublicKey(osobject);
491         P11Attribute* attrJavaMidpSecurityDomain = new P11AttrJavaMidpSecurityDomain(osobject);
492         P11Attribute* attrNameHashAlgorithm = new P11AttrNameHashAlgorithm(osobject);
493
494         // Initialize the attributes
495         if
496         (
497                 !attrSubject->init() ||
498                 !attrID->init() ||
499                 !attrIssuer->init() ||
500                 !attrSerialNumber->init() ||
501                 !attrValue->init() ||
502                 !attrURL->init() ||
503                 !attrHashOfSubjectPublicKey->init() ||
504                 !attrHashOfIssuerPublicKey->init() ||
505                 !attrJavaMidpSecurityDomain->init() ||
506                 !attrNameHashAlgorithm->init()
507         )
508         {
509                 ERROR_MSG("Could not initialize the attribute");
510                 delete attrSubject;
511                 delete attrID;
512                 delete attrIssuer;
513                 delete attrSerialNumber;
514                 delete attrValue;
515                 delete attrURL;
516                 delete attrHashOfSubjectPublicKey;
517                 delete attrHashOfIssuerPublicKey;
518                 delete attrJavaMidpSecurityDomain;
519                 delete attrNameHashAlgorithm;
520                 return false;
521         }
522
523         // Add them to the map
524         attributes[attrSubject->getType()] = attrSubject;
525         attributes[attrID->getType()] = attrID;
526         attributes[attrIssuer->getType()] = attrIssuer;
527         attributes[attrSerialNumber->getType()] = attrSerialNumber;
528         attributes[attrValue->getType()] = attrValue;
529         attributes[attrURL->getType()] = attrURL;
530         attributes[attrHashOfSubjectPublicKey->getType()] = attrHashOfSubjectPublicKey;
531         attributes[attrHashOfIssuerPublicKey->getType()] = attrHashOfIssuerPublicKey;
532         attributes[attrJavaMidpSecurityDomain->getType()] = attrJavaMidpSecurityDomain;
533         attributes[attrNameHashAlgorithm->getType()] = attrNameHashAlgorithm;
534
535         return true;
536 }
537
538 // Constructor
539 P11OpenPGPPublicKeyObj::P11OpenPGPPublicKeyObj()
540 {
541         initialized = false;
542 }
543
544 // Add attributes
545 bool P11OpenPGPPublicKeyObj::init(OSObject *inobject)
546 {
547         if (initialized) return true;
548         if (inobject == NULL) return false;
549
550         // Set default values for attributes that will be introduced in the parent
551         if (!inobject->attributeExists(CKA_CERTIFICATE_TYPE) || inobject->getUnsignedLongValue(CKA_CERTIFICATE_TYPE, CKC_VENDOR_DEFINED) != CKC_OPENPGP) {
552                 OSAttribute setCertType((unsigned long)CKC_OPENPGP);
553                 inobject->setAttribute(CKA_CERTIFICATE_TYPE, setCertType);
554         }
555
556         // Create parent
557         if (!P11CertificateObj::init(inobject)) return false;
558
559         // Create attributes
560         P11Attribute* attrSubject = new P11AttrSubject(osobject,P11Attribute::ck1);
561         P11Attribute* attrID = new P11AttrID(osobject);
562         P11Attribute* attrIssuer = new P11AttrIssuer(osobject);
563         P11Attribute* attrSerialNumber = new P11AttrSerialNumber(osobject);
564         P11Attribute* attrValue = new P11AttrValue(osobject,P11Attribute::ck1|P11Attribute::ck14);
565         P11Attribute* attrURL = new P11AttrURL(osobject);
566
567         // Initialize the attributes
568         if
569         (
570                 !attrSubject->init() ||
571                 !attrID->init() ||
572                 !attrIssuer->init() ||
573                 !attrSerialNumber->init() ||
574                 !attrValue->init() ||
575                 !attrURL->init()
576         )
577         {
578                 ERROR_MSG("Could not initialize the attribute");
579                 delete attrSubject;
580                 delete attrID;
581                 delete attrIssuer;
582                 delete attrSerialNumber;
583                 delete attrValue;
584                 delete attrURL;
585                 return false;
586         }
587
588         // Add them to the map
589         attributes[attrSubject->getType()] = attrSubject;
590         attributes[attrID->getType()] = attrID;
591         attributes[attrIssuer->getType()] = attrIssuer;
592         attributes[attrSerialNumber->getType()] = attrSerialNumber;
593         attributes[attrValue->getType()] = attrValue;
594         attributes[attrURL->getType()] = attrURL;
595
596         return true;
597 }
598
599 // Constructor
600 P11KeyObj::P11KeyObj()
601 {
602         initialized = false;
603 }
604
605 // Add attributes
606 bool P11KeyObj::init(OSObject *inobject)
607 {
608         if (initialized) return true;
609         if (inobject == NULL) return false;
610
611         // Create parent
612         if (!P11Object::init(inobject)) return false;
613
614         // Create attributes
615         P11Attribute* attrKeyType = new P11AttrKeyType(osobject,P11Attribute::ck5);
616         P11Attribute* attrID = new P11AttrID(osobject);
617         P11Attribute* attrStartDate = new P11AttrStartDate(osobject,P11Attribute::ck8);
618         P11Attribute* attrEndDate = new P11AttrEndDate(osobject,P11Attribute::ck8);
619         P11Attribute* attrDerive = new P11AttrDerive(osobject);
620         P11Attribute* attrLocal = new P11AttrLocal(osobject,P11Attribute::ck6);
621         P11Attribute* attrKeyGenMechanism = new P11AttrKeyGenMechanism(osobject);
622         P11Attribute* attrAllowedMechanisms = new P11AttrAllowedMechanisms(osobject);
623
624         // Initialize the attributes
625         if
626         (
627                 !attrKeyType->init() ||
628                 !attrID->init() ||
629                 !attrStartDate->init() ||
630                 !attrEndDate->init() ||
631                 !attrDerive->init() ||
632                 !attrLocal->init() ||
633                 !attrKeyGenMechanism->init() ||
634                 !attrAllowedMechanisms->init()
635         )
636         {
637                 ERROR_MSG("Could not initialize the attribute");
638                 delete attrKeyType;
639                 delete attrID;
640                 delete attrStartDate;
641                 delete attrEndDate;
642                 delete attrDerive;
643                 delete attrLocal;
644                 delete attrKeyGenMechanism;
645                 delete attrAllowedMechanisms;
646                 return false;
647         }
648
649         // Add them to the map
650         attributes[attrKeyType->getType()] = attrKeyType;
651         attributes[attrID->getType()] = attrID;
652         attributes[attrStartDate->getType()] = attrStartDate;
653         attributes[attrEndDate->getType()] = attrEndDate;
654         attributes[attrDerive->getType()] = attrDerive;
655         attributes[attrLocal->getType()] = attrLocal;
656         attributes[attrKeyGenMechanism->getType()] = attrKeyGenMechanism;
657         attributes[attrAllowedMechanisms->getType()] = attrAllowedMechanisms;
658
659         initialized = true;
660         return true;
661 }
662
663 // Constructor
664 P11PublicKeyObj::P11PublicKeyObj()
665 {
666         initialized = false;
667 }
668
669 // Add attributes
670 bool P11PublicKeyObj::init(OSObject *inobject)
671 {
672         if (initialized) return true;
673         if (inobject == NULL) return false;
674
675         // Set default values for attributes that will be introduced in the parent
676         if (!inobject->attributeExists(CKA_CLASS) || inobject->getUnsignedLongValue(CKA_CLASS, CKO_VENDOR_DEFINED) != CKO_PUBLIC_KEY) {
677                 OSAttribute setClass((unsigned long)CKO_PUBLIC_KEY);
678                 inobject->setAttribute(CKA_CLASS, setClass);
679         }
680         // Make public keys public
681         if (!inobject->attributeExists(CKA_PRIVATE)) {
682                 OSAttribute setPrivate(false);
683                 inobject->setAttribute(CKA_PRIVATE, setPrivate);
684         }
685
686         // Create parent
687         if (!P11KeyObj::init(inobject)) return false;
688
689         if (initialized) return true;
690
691         // Create attributes
692         P11Attribute* attrSubject = new P11AttrSubject(osobject,P11Attribute::ck8);
693         P11Attribute* attrEncrypt = new P11AttrEncrypt(osobject);
694         P11Attribute* attrVerify = new P11AttrVerify(osobject);
695         P11Attribute* attrVerifyRecover = new P11AttrVerifyRecover(osobject);
696         P11Attribute* attrWrap = new P11AttrWrap(osobject);
697         P11Attribute* attrTrusted = new P11AttrTrusted(osobject);
698         P11Attribute* attrWrapTemplate = new P11AttrWrapTemplate(osobject);
699         // TODO: CKA_PUBLIC_KEY_INFO is accepted, but we do not calculate it
700         P11Attribute* attrPublicKeyInfo = new P11AttrPublicKeyInfo(osobject,0);
701
702         // Initialize the attributes
703         if
704         (
705                 !attrSubject->init() ||
706                 !attrEncrypt->init() ||
707                 !attrVerify->init() ||
708                 !attrVerifyRecover->init() ||
709                 !attrWrap->init() ||
710                 !attrTrusted->init() ||
711                 !attrWrapTemplate->init() ||
712                 !attrPublicKeyInfo->init()
713         )
714         {
715                 ERROR_MSG("Could not initialize the attribute");
716                 delete attrSubject;
717                 delete attrEncrypt;
718                 delete attrVerify;
719                 delete attrVerifyRecover;
720                 delete attrWrap;
721                 delete attrTrusted;
722                 delete attrWrapTemplate;
723                 delete attrPublicKeyInfo;
724                 return false;
725         }
726
727         // Add them to the map
728         attributes[attrSubject->getType()] = attrSubject;
729         attributes[attrEncrypt->getType()] = attrEncrypt;
730         attributes[attrVerify->getType()] = attrVerify;
731         attributes[attrVerifyRecover->getType()] = attrVerifyRecover;
732         attributes[attrWrap->getType()] = attrWrap;
733         attributes[attrTrusted->getType()] = attrTrusted;
734         attributes[attrWrapTemplate->getType()] = attrWrapTemplate;
735         attributes[attrPublicKeyInfo->getType()] = attrPublicKeyInfo;
736
737         initialized = true;
738         return true;
739 }
740
741 // Constructor
742 P11RSAPublicKeyObj::P11RSAPublicKeyObj()
743 {
744         initialized = false;
745 }
746
747 // Add attributes
748 bool P11RSAPublicKeyObj::init(OSObject *inobject)
749 {
750         if (initialized) return true;
751         if (inobject == NULL) return false;
752
753         if (!inobject->attributeExists(CKA_KEY_TYPE) || inobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != CKK_RSA) {
754                 OSAttribute setKeyType((unsigned long)CKK_RSA);
755                 inobject->setAttribute(CKA_KEY_TYPE, setKeyType);
756         }
757
758         // Create parent
759         if (!P11PublicKeyObj::init(inobject)) return false;
760
761         // Create attributes
762         P11Attribute* attrModulus = new P11AttrModulus(osobject);
763         P11Attribute* attrModulusBits = new P11AttrModulusBits(osobject);
764         P11Attribute* attrPublicExponent = new P11AttrPublicExponent(osobject,P11Attribute::ck1);
765
766         // Initialize the attributes
767         if
768         (
769                 !attrModulus->init() ||
770                 !attrModulusBits->init() ||
771                 !attrPublicExponent->init()
772         )
773         {
774                 ERROR_MSG("Could not initialize the attribute");
775                 delete attrModulus;
776                 delete attrModulusBits;
777                 delete attrPublicExponent;
778                 return false;
779         }
780
781         // Add them to the map
782         attributes[attrModulus->getType()] = attrModulus;
783         attributes[attrModulusBits->getType()] = attrModulusBits;
784         attributes[attrPublicExponent->getType()] = attrPublicExponent;
785
786         initialized = true;
787         return true;
788 }
789
790 // Constructor
791 P11DSAPublicKeyObj::P11DSAPublicKeyObj()
792 {
793         initialized = false;
794 }
795
796 // Add attributes
797 bool P11DSAPublicKeyObj::init(OSObject *inobject)
798 {
799         if (initialized) return true;
800         if (inobject == NULL) return false;
801
802         if (!inobject->attributeExists(CKA_KEY_TYPE) || inobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != CKK_DSA) {
803                 OSAttribute setKeyType((unsigned long)CKK_DSA);
804                 inobject->setAttribute(CKA_KEY_TYPE, setKeyType);
805         }
806
807         // Create parent
808         if (!P11PublicKeyObj::init(inobject)) return false;
809
810         // Create attributes
811         P11Attribute* attrPrime = new P11AttrPrime(osobject,P11Attribute::ck3);
812         P11Attribute* attrSubPrime = new P11AttrSubPrime(osobject,P11Attribute::ck3);
813         P11Attribute* attrBase = new P11AttrBase(osobject,P11Attribute::ck3);
814         P11Attribute* attrValue = new P11AttrValue(osobject,P11Attribute::ck1|P11Attribute::ck4);
815
816         // Initialize the attributes
817         if
818         (
819                 !attrPrime->init() ||
820                 !attrSubPrime->init() ||
821                 !attrBase->init() ||
822                 !attrValue->init()
823         )
824         {
825                 ERROR_MSG("Could not initialize the attribute");
826                 delete attrPrime;
827                 delete attrSubPrime;
828                 delete attrBase;
829                 delete attrValue;
830                 return false;
831         }
832
833         // Add them to the map
834         attributes[attrPrime->getType()] = attrPrime;
835         attributes[attrSubPrime->getType()] = attrSubPrime;
836         attributes[attrBase->getType()] = attrBase;
837         attributes[attrValue->getType()] = attrValue;
838
839         initialized = true;
840         return true;
841 }
842
843 // Constructor
844 P11ECPublicKeyObj::P11ECPublicKeyObj()
845 {
846         initialized = false;
847 }
848
849 // Add attributes
850 bool P11ECPublicKeyObj::init(OSObject *inobject)
851 {
852         if (initialized) return true;
853         if (inobject == NULL) return false;
854
855         if (!inobject->attributeExists(CKA_KEY_TYPE) || inobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != CKK_EC) {
856                 OSAttribute setKeyType((unsigned long)CKK_EC);
857                 inobject->setAttribute(CKA_KEY_TYPE, setKeyType);
858         }
859
860         // Create parent
861         if (!P11PublicKeyObj::init(inobject)) return false;
862
863         // Create attributes
864         P11Attribute* attrEcParams = new P11AttrEcParams(osobject,P11Attribute::ck3);
865         P11Attribute* attrEcPoint = new P11AttrEcPoint(osobject);
866
867         // Initialize the attributes
868         if
869         (
870                 !attrEcParams->init() ||
871                 !attrEcPoint->init()
872         )
873         {
874                 ERROR_MSG("Could not initialize the attribute");
875                 delete attrEcParams;
876                 delete attrEcPoint;
877                 return false;
878         }
879
880         // Add them to the map
881         attributes[attrEcParams->getType()] = attrEcParams;
882         attributes[attrEcPoint->getType()] = attrEcPoint;
883
884         initialized = true;
885         return true;
886 }
887
888 // Constructor
889 P11DHPublicKeyObj::P11DHPublicKeyObj()
890 {
891         initialized = false;
892 }
893
894 // Add attributes
895 bool P11DHPublicKeyObj::init(OSObject *inobject)
896 {
897         if (initialized) return true;
898         if (inobject == NULL) return false;
899
900         if (!inobject->attributeExists(CKA_KEY_TYPE) || inobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != CKK_DH) {
901                 OSAttribute setKeyType((unsigned long)CKK_DH);
902                 inobject->setAttribute(CKA_KEY_TYPE, setKeyType);
903         }
904
905         // Create parent
906         if (!P11PublicKeyObj::init(inobject)) return false;
907
908         // Create attributes
909         P11Attribute* attrPrime = new P11AttrPrime(osobject,P11Attribute::ck3);
910         P11Attribute* attrBase = new P11AttrBase(osobject,P11Attribute::ck3);
911         P11Attribute* attrValue = new P11AttrValue(osobject,P11Attribute::ck1|P11Attribute::ck4);
912
913         // Initialize the attributes
914         if
915         (
916                 !attrPrime->init() ||
917                 !attrBase->init() ||
918                 !attrValue->init()
919         )
920         {
921                 ERROR_MSG("Could not initialize the attribute");
922                 delete attrPrime;
923                 delete attrBase;
924                 delete attrValue;
925                 return false;
926         }
927
928         // Add them to the map
929         attributes[attrPrime->getType()] = attrPrime;
930         attributes[attrBase->getType()] = attrBase;
931         attributes[attrValue->getType()] = attrValue;
932
933         initialized = true;
934         return true;
935 }
936
937 // Constructor
938 P11GOSTPublicKeyObj::P11GOSTPublicKeyObj()
939 {
940         initialized = false;
941 }
942
943 // Add attributes
944 bool P11GOSTPublicKeyObj::init(OSObject *inobject)
945 {
946         if (initialized) return true;
947         if (inobject == NULL) return false;
948
949         if (!inobject->attributeExists(CKA_KEY_TYPE) || inobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != CKK_GOSTR3410) {
950                 OSAttribute setKeyType((unsigned long)CKK_GOSTR3410);
951                 inobject->setAttribute(CKA_KEY_TYPE, setKeyType);
952         }
953
954         // Create parent
955         if (!P11PublicKeyObj::init(inobject)) return false;
956
957         // Create attributes
958         P11Attribute* attrValue = new P11AttrValue(osobject,P11Attribute::ck1|P11Attribute::ck4);
959         P11Attribute* attrGostR3410Params = new P11AttrGostR3410Params(osobject,P11Attribute::ck3);
960         P11Attribute* attrGostR3411Params = new P11AttrGostR3411Params(osobject,P11Attribute::ck3);
961         P11Attribute* attrGost28147Params = new P11AttrGost28147Params(osobject,P11Attribute::ck8);
962
963         // Initialize the attributes
964         if
965         (
966                 !attrValue->init() ||
967                 !attrGostR3410Params->init() ||
968                 !attrGostR3411Params->init() ||
969                 !attrGost28147Params->init()
970         )
971         {
972                 ERROR_MSG("Could not initialize the attribute");
973                 delete attrValue;
974                 delete attrGostR3410Params;
975                 delete attrGostR3411Params;
976                 delete attrGost28147Params;
977                 return false;
978         }
979
980         // Add them to the map
981         attributes[attrValue->getType()] = attrValue;
982         attributes[attrGostR3410Params->getType()] = attrGostR3410Params;
983         attributes[attrGostR3411Params->getType()] = attrGostR3411Params;
984         attributes[attrGost28147Params->getType()] = attrGost28147Params;
985
986         initialized = true;
987         return true;
988 }
989
990 //constructor
991 P11PrivateKeyObj::P11PrivateKeyObj()
992 {
993         initialized = false;
994 }
995
996 // Add attributes
997 bool P11PrivateKeyObj::init(OSObject *inobject)
998 {
999         if (initialized) return true;
1000         if (inobject == NULL) return false;
1001
1002         if (!inobject->attributeExists(CKA_CLASS) || inobject->getUnsignedLongValue(CKA_CLASS, CKO_VENDOR_DEFINED) != CKO_PRIVATE_KEY) {
1003                 OSAttribute setClass((unsigned long)CKO_PRIVATE_KEY);
1004                 inobject->setAttribute(CKA_CLASS, setClass);
1005         }
1006
1007         // Create parent
1008         if (!P11KeyObj::init(inobject)) return false;
1009
1010         // Create attributes
1011         P11Attribute* attrSubject = new P11AttrSubject(osobject,P11Attribute::ck8);
1012         P11Attribute* attrSensitive = new P11AttrSensitive(osobject);
1013         P11Attribute* attrDecrypt = new P11AttrDecrypt(osobject);
1014         P11Attribute* attrSign = new P11AttrSign(osobject);
1015         P11Attribute* attrSignRecover = new P11AttrSignRecover(osobject);
1016         P11Attribute* attrUnwrap = new P11AttrUnwrap(osobject);
1017         P11Attribute* attrExtractable = new P11AttrExtractable(osobject);
1018         P11Attribute* attrAlwaysSensitive = new P11AttrAlwaysSensitive(osobject);
1019         P11Attribute* attrNeverExtractable = new P11AttrNeverExtractable(osobject);
1020         P11Attribute* attrWrapWithTrusted = new P11AttrWrapWithTrusted(osobject);
1021         P11Attribute* attrUnwrapTemplate = new P11AttrUnwrapTemplate(osobject);
1022         // TODO: CKA_ALWAYS_AUTHENTICATE is accepted, but we do not use it
1023         P11Attribute* attrAlwaysAuthenticate = new P11AttrAlwaysAuthenticate(osobject);
1024         // TODO: CKA_PUBLIC_KEY_INFO is accepted, but we do not calculate it
1025         P11Attribute* attrPublicKeyInfo = new P11AttrPublicKeyInfo(osobject,P11Attribute::ck8);
1026
1027         // Initialize the attributes
1028         if
1029         (
1030                 !attrSubject->init() ||
1031                 !attrSensitive->init() ||
1032                 !attrDecrypt->init() ||
1033                 !attrSign->init() ||
1034                 !attrSignRecover->init() ||
1035                 !attrUnwrap->init() ||
1036                 !attrExtractable->init() ||
1037                 !attrAlwaysSensitive->init() ||
1038                 !attrNeverExtractable->init() ||
1039                 !attrWrapWithTrusted->init() ||
1040                 !attrUnwrapTemplate->init() ||
1041                 !attrAlwaysAuthenticate->init() ||
1042                 !attrPublicKeyInfo->init()
1043         )
1044         {
1045                 ERROR_MSG("Could not initialize the attribute");
1046                 delete attrSubject;
1047                 delete attrSensitive;
1048                 delete attrDecrypt;
1049                 delete attrSign;
1050                 delete attrSignRecover;
1051                 delete attrUnwrap;
1052                 delete attrExtractable;
1053                 delete attrAlwaysSensitive;
1054                 delete attrNeverExtractable;
1055                 delete attrWrapWithTrusted;
1056                 delete attrUnwrapTemplate;
1057                 delete attrAlwaysAuthenticate;
1058                 delete attrPublicKeyInfo;
1059                 return false;
1060         }
1061
1062         // Add them to the map
1063         attributes[attrSubject->getType()] = attrSubject;
1064         attributes[attrSensitive->getType()] = attrSensitive;
1065         attributes[attrDecrypt->getType()] = attrDecrypt;
1066         attributes[attrSign->getType()] = attrSign;
1067         attributes[attrSignRecover->getType()] = attrSignRecover;
1068         attributes[attrUnwrap->getType()] = attrUnwrap;
1069         attributes[attrExtractable->getType()] = attrExtractable;
1070         attributes[attrAlwaysSensitive->getType()] = attrAlwaysSensitive;
1071         attributes[attrNeverExtractable->getType()] = attrNeverExtractable;
1072         attributes[attrWrapWithTrusted->getType()] = attrWrapWithTrusted;
1073         attributes[attrUnwrapTemplate->getType()] = attrUnwrapTemplate;
1074         attributes[attrAlwaysAuthenticate->getType()] = attrAlwaysAuthenticate;
1075         attributes[attrPublicKeyInfo->getType()] = attrPublicKeyInfo;
1076
1077         initialized = true;
1078         return true;
1079 }
1080
1081 // Constructor
1082 P11RSAPrivateKeyObj::P11RSAPrivateKeyObj()
1083 {
1084         initialized = false;
1085 }
1086
1087 // Add attributes
1088 bool P11RSAPrivateKeyObj::init(OSObject *inobject)
1089 {
1090         if (initialized) return true;
1091         if (inobject == NULL) return false;
1092
1093         if (!inobject->attributeExists(CKA_KEY_TYPE) || inobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != CKK_RSA) {
1094                 OSAttribute setKeyType((unsigned long)CKK_RSA);
1095                 inobject->setAttribute(CKA_KEY_TYPE, setKeyType);
1096         }
1097
1098         // Create parent
1099         if (!P11PrivateKeyObj::init(inobject)) return false;
1100
1101         // Create attributes
1102         P11Attribute* attrModulus = new P11AttrModulus(osobject,P11Attribute::ck6);
1103         P11Attribute* attrPublicExponent = new P11AttrPublicExponent(osobject,P11Attribute::ck4|P11Attribute::ck6);
1104         P11Attribute* attrPrivateExponent = new P11AttrPrivateExponent(osobject);
1105         P11Attribute* attrPrime1 = new P11AttrPrime1(osobject);
1106         P11Attribute* attrPrime2 = new P11AttrPrime2(osobject);
1107         P11Attribute* attrExponent1 = new P11AttrExponent1(osobject);
1108         P11Attribute* attrExponent2 = new P11AttrExponent2(osobject);
1109         P11Attribute* attrCoefficient = new P11AttrCoefficient(osobject);
1110         P11Attribute* attrPrivateHandle = new P11AttrPrivateHandle(osobject);
1111
1112         // Initialize the attributes
1113         if
1114         (
1115                 !attrModulus->init() ||
1116                 !attrPublicExponent->init() ||
1117                 !attrPrivateExponent->init() ||
1118                 !attrPrime1->init() ||
1119                 !attrPrime2->init() ||
1120                 !attrExponent1->init() ||
1121                 !attrExponent2->init() ||
1122                 !attrPrivateHandle->init() ||
1123                 !attrCoefficient->init()
1124         )
1125         {
1126                 ERROR_MSG("Could not initialize the attribute");
1127                 delete attrModulus;
1128                 delete attrPublicExponent;
1129                 delete attrPrivateExponent;
1130                 delete attrPrime1;
1131                 delete attrPrime2;
1132                 delete attrExponent1;
1133                 delete attrExponent2;
1134                 delete attrCoefficient;
1135                 delete attrPrivateHandle;
1136                 return false;
1137         }
1138
1139         // Add them to the map
1140         attributes[attrModulus->getType()] = attrModulus;
1141         attributes[attrPublicExponent->getType()] = attrPublicExponent;
1142         attributes[attrPrivateExponent->getType()] = attrPrivateExponent;
1143         attributes[attrPrime1->getType()] = attrPrime1;
1144         attributes[attrPrime2->getType()] = attrPrime2;
1145         attributes[attrExponent1->getType()] = attrExponent1;
1146         attributes[attrExponent2->getType()] = attrExponent2;
1147         attributes[attrCoefficient->getType()] = attrCoefficient;
1148         attributes[attrPrivateHandle->getType()] = attrPrivateHandle;
1149
1150         initialized = true;
1151         return true;
1152 }
1153
1154 // Constructor
1155 P11DSAPrivateKeyObj::P11DSAPrivateKeyObj()
1156 {
1157         initialized = false;
1158 }
1159
1160 // Add attributes
1161 bool P11DSAPrivateKeyObj::init(OSObject *inobject)
1162 {
1163         if (initialized) return true;
1164         if (inobject == NULL) return false;
1165
1166         if (!inobject->attributeExists(CKA_KEY_TYPE) || inobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != CKK_DSA) {
1167                 OSAttribute setKeyType((unsigned long)CKK_DSA);
1168                 inobject->setAttribute(CKA_KEY_TYPE, setKeyType);
1169         }
1170
1171         // Create parent
1172         if (!P11PrivateKeyObj::init(inobject)) return false;
1173
1174         // Create attributes
1175         P11Attribute* attrPrime = new P11AttrPrime(osobject,P11Attribute::ck4|P11Attribute::ck6);
1176         P11Attribute* attrSubPrime = new P11AttrSubPrime(osobject,P11Attribute::ck4|P11Attribute::ck6);
1177         P11Attribute* attrBase = new P11AttrBase(osobject,P11Attribute::ck4|P11Attribute::ck6);
1178         P11Attribute* attrValue = new P11AttrValue(osobject,P11Attribute::ck1|P11Attribute::ck4|P11Attribute::ck6|P11Attribute::ck7);
1179
1180         // Initialize the attributes
1181         if
1182         (
1183                 !attrPrime->init() ||
1184                 !attrSubPrime->init() ||
1185                 !attrBase->init() ||
1186                 !attrValue->init()
1187         )
1188         {
1189                 ERROR_MSG("Could not initialize the attribute");
1190                 delete attrPrime;
1191                 delete attrSubPrime;
1192                 delete attrBase;
1193                 delete attrValue;
1194                 return false;
1195         }
1196
1197         // Add them to the map
1198         attributes[attrPrime->getType()] = attrPrime;
1199         attributes[attrSubPrime->getType()] = attrSubPrime;
1200         attributes[attrBase->getType()] = attrBase;
1201         attributes[attrValue->getType()] = attrValue;
1202
1203         initialized = true;
1204         return true;
1205 }
1206
1207 // Constructor
1208 P11ECPrivateKeyObj::P11ECPrivateKeyObj()
1209 {
1210         initialized = false;
1211 }
1212
1213 // Add attributes
1214 bool P11ECPrivateKeyObj::init(OSObject *inobject)
1215 {
1216         if (initialized) return true;
1217         if (inobject == NULL) return false;
1218
1219         if (!inobject->attributeExists(CKA_KEY_TYPE) || inobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != CKK_EC) {
1220                 OSAttribute setKeyType((unsigned long)CKK_EC);
1221                 inobject->setAttribute(CKA_KEY_TYPE, setKeyType);
1222         }
1223
1224         // Create parent
1225         if (!P11PrivateKeyObj::init(inobject)) return false;
1226
1227         // Create attributes
1228         P11Attribute* attrEcParams = new P11AttrEcParams(osobject,P11Attribute::ck4|P11Attribute::ck6);
1229         P11Attribute* attrValue = new P11AttrValue(osobject,P11Attribute::ck1|P11Attribute::ck4|P11Attribute::ck6|P11Attribute::ck7);
1230         P11Attribute* attrPrivateHandle = new P11AttrPrivateHandle(osobject);
1231
1232         // Initialize the attributes
1233         if
1234         (
1235                 !attrEcParams->init() ||
1236                 !attrPrivateHandle->init() ||
1237                 !attrValue->init()
1238         )
1239         {
1240                 ERROR_MSG("Could not initialize the attribute");
1241                 delete attrEcParams;
1242                 delete attrValue;
1243                 delete attrPrivateHandle;
1244                 return false;
1245         }
1246
1247         // Add them to the map
1248         attributes[attrEcParams->getType()] = attrEcParams;
1249         attributes[attrValue->getType()] = attrValue;
1250         attributes[attrPrivateHandle->getType()] = attrPrivateHandle;
1251
1252         initialized = true;
1253         return true;
1254 }
1255
1256 // Constructor
1257 P11DHPrivateKeyObj::P11DHPrivateKeyObj()
1258 {
1259         initialized = false;
1260 }
1261
1262 // Add attributes
1263 bool P11DHPrivateKeyObj::init(OSObject *inobject)
1264 {
1265         if (initialized) return true;
1266         if (inobject == NULL) return false;
1267
1268         if (!inobject->attributeExists(CKA_KEY_TYPE) || inobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != CKK_DH) {
1269                 OSAttribute setKeyType((unsigned long)CKK_DH);
1270                 inobject->setAttribute(CKA_KEY_TYPE, setKeyType);
1271         }
1272
1273         // Create parent
1274         if (!P11PrivateKeyObj::init(inobject)) return false;
1275
1276         // Create attributes
1277         P11Attribute* attrPrime = new P11AttrPrime(osobject,P11Attribute::ck4|P11Attribute::ck6);
1278         P11Attribute* attrBase = new P11AttrBase(osobject,P11Attribute::ck4|P11Attribute::ck6);
1279         P11Attribute* attrValue = new P11AttrValue(osobject,P11Attribute::ck1|P11Attribute::ck4|P11Attribute::ck6|P11Attribute::ck7);
1280         P11Attribute* attrValueBits = new P11AttrValueBits(osobject);
1281
1282         // Initialize the attributes
1283         if
1284         (
1285                 !attrPrime->init() ||
1286                 !attrBase->init() ||
1287                 !attrValue->init() ||
1288                 !attrValueBits->init()
1289         )
1290         {
1291                 ERROR_MSG("Could not initialize the attribute");
1292                 delete attrPrime;
1293                 delete attrBase;
1294                 delete attrValue;
1295                 delete attrValueBits;
1296                 return false;
1297         }
1298
1299         // Add them to the map
1300         attributes[attrPrime->getType()] = attrPrime;
1301         attributes[attrBase->getType()] = attrBase;
1302         attributes[attrValue->getType()] = attrValue;
1303         attributes[attrValueBits->getType()] = attrValueBits;
1304
1305         initialized = true;
1306         return true;
1307 }
1308
1309 // Constructor
1310 P11GOSTPrivateKeyObj::P11GOSTPrivateKeyObj()
1311 {
1312         initialized = false;
1313 }
1314
1315 // Add attributes
1316 bool P11GOSTPrivateKeyObj::init(OSObject *inobject)
1317 {
1318         if (initialized) return true;
1319         if (inobject == NULL) return false;
1320
1321         if (!inobject->attributeExists(CKA_KEY_TYPE) || inobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != CKK_GOSTR3410) {
1322                 OSAttribute setKeyType((unsigned long)CKK_GOSTR3410);
1323                 inobject->setAttribute(CKA_KEY_TYPE, setKeyType);
1324         }
1325
1326         // Create parent
1327         if (!P11PrivateKeyObj::init(inobject)) return false;
1328
1329         // Create attributes
1330         P11Attribute* attrValue = new P11AttrValue(osobject,P11Attribute::ck1|P11Attribute::ck4|P11Attribute::ck6|P11Attribute::ck7);
1331         P11Attribute* attrGostR3410Params = new P11AttrGostR3410Params(osobject,P11Attribute::ck4|P11Attribute::ck6);
1332         P11Attribute* attrGostR3411Params = new P11AttrGostR3411Params(osobject,P11Attribute::ck4|P11Attribute::ck6);
1333         P11Attribute* attrGost28147Params = new P11AttrGost28147Params(osobject,P11Attribute::ck4|P11Attribute::ck6|P11Attribute::ck8);
1334
1335         // Initialize the attributes
1336         if
1337         (
1338                 !attrValue->init() ||
1339                 !attrGostR3410Params->init() ||
1340                 !attrGostR3411Params->init() ||
1341                 !attrGost28147Params->init()
1342         )
1343         {
1344                 ERROR_MSG("Could not initialize the attribute");
1345                 delete attrValue;
1346                 delete attrGostR3410Params;
1347                 delete attrGostR3411Params;
1348                 delete attrGost28147Params;
1349                 return false;
1350         }
1351
1352         // Add them to the map
1353         attributes[attrValue->getType()] = attrValue;
1354         attributes[attrGostR3410Params->getType()] = attrGostR3410Params;
1355         attributes[attrGostR3411Params->getType()] = attrGostR3411Params;
1356         attributes[attrGost28147Params->getType()] = attrGost28147Params;
1357
1358         initialized = true;
1359         return true;
1360 }
1361
1362 // Constructor
1363 P11SecretKeyObj::P11SecretKeyObj()
1364 {
1365         initialized = false;
1366 }
1367
1368 // Add attributes
1369 bool P11SecretKeyObj::init(OSObject *inobject)
1370 {
1371         if (initialized) return true;
1372         if (inobject == NULL) return false;
1373
1374         if (!inobject->attributeExists(CKA_CLASS) || inobject->getUnsignedLongValue(CKA_CLASS, CKO_VENDOR_DEFINED) != CKO_SECRET_KEY) {
1375                 OSAttribute setClass((unsigned long)CKO_SECRET_KEY);
1376                 inobject->setAttribute(CKA_CLASS, setClass);
1377         }
1378
1379         // Create parent
1380         if (!P11KeyObj::init(inobject)) return false;
1381
1382         // Create attributes
1383         P11Attribute* attrSensitive = new P11AttrSensitive(osobject);
1384         P11Attribute* attrEncrypt = new P11AttrEncrypt(osobject);
1385         P11Attribute* attrDecrypt = new P11AttrDecrypt(osobject);
1386         P11Attribute* attrSign = new P11AttrSign(osobject);
1387         P11Attribute* attrVerify = new P11AttrVerify(osobject);
1388         P11Attribute* attrWrap = new P11AttrWrap(osobject);
1389         P11Attribute* attrUnwrap = new P11AttrUnwrap(osobject);
1390         P11Attribute* attrExtractable = new P11AttrExtractable(osobject);
1391         P11Attribute* attrAlwaysSensitive = new P11AttrAlwaysSensitive(osobject);
1392         P11Attribute* attrNeverExtractable = new P11AttrNeverExtractable(osobject);
1393         P11Attribute* attrCheckValue = new P11AttrCheckValue(osobject, P11Attribute::ck8);
1394         P11Attribute* attrWrapWithTrusted = new P11AttrWrapWithTrusted(osobject);
1395         P11Attribute* attrTrusted = new P11AttrTrusted(osobject);
1396         P11Attribute* attrWrapTemplate = new P11AttrWrapTemplate(osobject);
1397         P11Attribute* attrUnwrapTemplate = new P11AttrUnwrapTemplate(osobject);
1398
1399         // Initialize the attributes
1400         if
1401         (
1402                 !attrSensitive->init() ||
1403                 !attrEncrypt->init() ||
1404                 !attrDecrypt->init() ||
1405                 !attrSign->init() ||
1406                 !attrVerify->init() ||
1407                 !attrWrap->init() ||
1408                 !attrUnwrap->init() ||
1409                 !attrExtractable->init() ||
1410                 !attrAlwaysSensitive->init() ||
1411                 !attrNeverExtractable->init() ||
1412                 !attrCheckValue->init() ||
1413                 !attrWrapWithTrusted->init() ||
1414                 !attrTrusted->init() ||
1415                 !attrWrapTemplate->init() ||
1416                 !attrUnwrapTemplate->init()
1417         )
1418         {
1419                 ERROR_MSG("Could not initialize the attribute");
1420                 delete attrSensitive;
1421                 delete attrEncrypt;
1422                 delete attrDecrypt;
1423                 delete attrSign;
1424                 delete attrVerify;
1425                 delete attrWrap;
1426                 delete attrUnwrap;
1427                 delete attrExtractable;
1428                 delete attrAlwaysSensitive;
1429                 delete attrNeverExtractable;
1430                 delete attrCheckValue;
1431                 delete attrWrapWithTrusted;
1432                 delete attrTrusted;
1433                 delete attrWrapTemplate;
1434                 delete attrUnwrapTemplate;
1435                 return false;
1436         }
1437
1438         // Add them to the map
1439         attributes[attrSensitive->getType()] = attrSensitive;
1440         attributes[attrEncrypt->getType()] = attrEncrypt;
1441         attributes[attrDecrypt->getType()] = attrDecrypt;
1442         attributes[attrSign->getType()] = attrSign;
1443         attributes[attrVerify->getType()] = attrVerify;
1444         attributes[attrWrap->getType()] = attrWrap;
1445         attributes[attrUnwrap->getType()] = attrUnwrap;
1446         attributes[attrExtractable->getType()] = attrExtractable;
1447         attributes[attrAlwaysSensitive->getType()] = attrAlwaysSensitive;
1448         attributes[attrNeverExtractable->getType()] = attrNeverExtractable;
1449         attributes[attrCheckValue->getType()] = attrCheckValue;
1450         attributes[attrWrapWithTrusted->getType()] = attrWrapWithTrusted;
1451         attributes[attrTrusted->getType()] = attrTrusted;
1452         attributes[attrWrapTemplate->getType()] = attrWrapTemplate;
1453         attributes[attrUnwrapTemplate->getType()] = attrUnwrapTemplate;
1454
1455         initialized = true;
1456         return true;
1457 }
1458
1459 // Constructor
1460 P11GenericSecretKeyObj::P11GenericSecretKeyObj()
1461 {
1462         initialized = false;
1463         keytype = CKK_VENDOR_DEFINED;
1464 }
1465
1466 // Add attributes
1467 bool P11GenericSecretKeyObj::init(OSObject *inobject)
1468 {
1469         if (initialized) return true;
1470         if (inobject == NULL) return false;
1471
1472         if (!inobject->attributeExists(CKA_KEY_TYPE) || inobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != keytype) {
1473                 OSAttribute setKeyType(keytype);
1474                 inobject->setAttribute(CKA_KEY_TYPE, setKeyType);
1475         }
1476
1477         // Create parent
1478         if (!P11SecretKeyObj::init(inobject)) return false;
1479
1480         // Create attributes
1481         P11Attribute* attrValue = new P11AttrValue(osobject,P11Attribute::ck1|P11Attribute::ck4|P11Attribute::ck6|P11Attribute::ck7);
1482         P11Attribute* attrValueLen = new P11AttrValueLen(osobject);
1483
1484         // Initialize the attributes
1485         if
1486         (
1487                 !attrValue->init() ||
1488                 !attrValueLen->init()
1489         )
1490         {
1491                 ERROR_MSG("Could not initialize the attribute");
1492                 delete attrValue;
1493                 delete attrValueLen;
1494                 return false;
1495         }
1496
1497         // Add them to the map
1498         attributes[attrValue->getType()] = attrValue;
1499         attributes[attrValueLen->getType()] = attrValueLen;
1500
1501         initialized = true;
1502         return true;
1503 }
1504
1505 // Set Key Type
1506 bool P11GenericSecretKeyObj::setKeyType(CK_KEY_TYPE inKeytype)
1507 {
1508         if (!initialized)
1509         {
1510                 keytype = inKeytype;
1511                 return true;
1512         }
1513         else
1514                 return false;
1515 }
1516
1517 // Get Key Type
1518 CK_KEY_TYPE P11GenericSecretKeyObj::getKeyType()
1519 {
1520         return keytype;
1521 }
1522
1523 // Constructor
1524 P11AESSecretKeyObj::P11AESSecretKeyObj()
1525 {
1526         initialized = false;
1527 }
1528
1529 // Add attributes
1530 bool P11AESSecretKeyObj::init(OSObject *inobject)
1531 {
1532         if (initialized) return true;
1533         if (inobject == NULL) return false;
1534
1535         if (!inobject->attributeExists(CKA_KEY_TYPE) || inobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != CKK_AES) {
1536                 OSAttribute setKeyType((unsigned long)CKK_AES);
1537                 inobject->setAttribute(CKA_KEY_TYPE, setKeyType);
1538         }
1539
1540         // Create parent
1541         if (!P11SecretKeyObj::init(inobject)) return false;
1542
1543         // Create attributes
1544         P11Attribute* attrValue = new P11AttrValue(osobject,P11Attribute::ck1|P11Attribute::ck4|P11Attribute::ck6|P11Attribute::ck7);
1545         P11Attribute* attrValueLen = new P11AttrValueLen(osobject,P11Attribute::ck6);
1546
1547         // Initialize the attributes
1548         if
1549         (
1550                 !attrValue->init() ||
1551                 !attrValueLen->init()
1552         )
1553         {
1554                 ERROR_MSG("Could not initialize the attribute");
1555                 delete attrValue;
1556                 delete attrValueLen;
1557                 return false;
1558         }
1559
1560         // Add them to the map
1561         attributes[attrValue->getType()] = attrValue;
1562         attributes[attrValueLen->getType()] = attrValueLen;
1563
1564         initialized = true;
1565         return true;
1566 }
1567
1568 // Constructor
1569 P11DESSecretKeyObj::P11DESSecretKeyObj()
1570 {
1571         initialized = false;
1572         keytype = CKK_VENDOR_DEFINED;
1573 }
1574
1575 // Add attributes
1576 bool P11DESSecretKeyObj::init(OSObject *inobject)
1577 {
1578         if (initialized) return true;
1579         if (inobject == NULL) return false;
1580
1581         if (!inobject->attributeExists(CKA_KEY_TYPE) || inobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != keytype) {
1582                 OSAttribute setKeyType(keytype);
1583                 inobject->setAttribute(CKA_KEY_TYPE, setKeyType);
1584         }
1585
1586         // Create parent
1587         if (!P11SecretKeyObj::init(inobject)) return false;
1588
1589         // Create attributes
1590         P11Attribute* attrValue = new P11AttrValue(osobject,P11Attribute::ck1|P11Attribute::ck4|P11Attribute::ck6|P11Attribute::ck7);
1591
1592         // Initialize the attributes
1593         if (!attrValue->init())
1594         {
1595                 ERROR_MSG("Could not initialize the attribute");
1596                 delete attrValue;
1597                 return false;
1598         }
1599
1600         // Add them to the map
1601         attributes[attrValue->getType()] = attrValue;
1602
1603         initialized = true;
1604         return true;
1605 }
1606
1607 // Set Key Type
1608 bool P11DESSecretKeyObj::setKeyType(CK_KEY_TYPE inKeytype)
1609 {
1610         if (!initialized)
1611         {
1612                 keytype = inKeytype;
1613                 return true;
1614         }
1615         else
1616                 return false;
1617 }
1618
1619 // Get Key Type
1620 CK_KEY_TYPE P11DESSecretKeyObj::getKeyType()
1621 {
1622         return keytype;
1623 }
1624
1625 // Constructor
1626 P11GOSTSecretKeyObj::P11GOSTSecretKeyObj()
1627 {
1628         initialized = false;
1629 }
1630
1631 // Add attributes
1632 bool P11GOSTSecretKeyObj::init(OSObject *inobject)
1633 {
1634         if (initialized) return true;
1635         if (inobject == NULL) return false;
1636
1637         if (!inobject->attributeExists(CKA_KEY_TYPE) || inobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != CKK_GOST28147) {
1638                 OSAttribute setKeyType((unsigned long)CKK_GOST28147);
1639                 inobject->setAttribute(CKA_KEY_TYPE, setKeyType);
1640         }
1641
1642         // Create parent
1643         if (!P11SecretKeyObj::init(inobject)) return false;
1644
1645         // Create attributes
1646         P11Attribute* attrValue = new P11AttrValue(osobject,P11Attribute::ck1|P11Attribute::ck4|P11Attribute::ck6|P11Attribute::ck7);
1647         P11Attribute* attrGost28147Params = new P11AttrGost28147Params(osobject,P11Attribute::ck1|P11Attribute::ck3|P11Attribute::ck5);
1648
1649         // Initialize the attributes
1650         if
1651         (
1652                 !attrValue->init() ||
1653                 !attrGost28147Params->init()
1654         )
1655         {
1656                 ERROR_MSG("Could not initialize the attribute");
1657                 delete attrValue;
1658                 delete attrGost28147Params;
1659                 return false;
1660         }
1661
1662         // Add them to the map
1663         attributes[attrValue->getType()] = attrValue;
1664         attributes[attrGost28147Params->getType()] = attrGost28147Params;
1665
1666         initialized = true;
1667         return true;
1668 }
1669
1670 // Constructor
1671 P11DomainObj::P11DomainObj()
1672 {
1673         initialized = false;
1674 }
1675
1676 // Add attributes
1677 bool P11DomainObj::init(OSObject *inobject)
1678 {
1679         if (initialized) return true;
1680         if (inobject == NULL) return false;
1681
1682         if (!inobject->attributeExists(CKA_CLASS) || inobject->getUnsignedLongValue(CKA_CLASS, CKO_VENDOR_DEFINED) != CKO_DOMAIN_PARAMETERS) {
1683                 OSAttribute setClass((unsigned long)CKO_DOMAIN_PARAMETERS);
1684                 inobject->setAttribute(CKA_CLASS, setClass);
1685         }
1686
1687         // Create parent
1688         if (!P11Object::init(inobject)) return false;
1689
1690         // Create attributes
1691         P11Attribute* attrKeyType = new P11AttrKeyType(osobject);
1692         P11Attribute* attrLocal = new P11AttrLocal(osobject);
1693
1694         // Initialize the attributes
1695         if
1696         (
1697                 !attrKeyType->init() ||
1698                 !attrLocal->init()
1699         )
1700         {
1701                 ERROR_MSG("Could not initialize the attribute");
1702                 delete attrKeyType;
1703                 delete attrLocal;
1704                 return false;
1705         }
1706
1707         // Add them to the map
1708         attributes[attrKeyType->getType()] = attrKeyType;
1709         attributes[attrLocal->getType()] = attrLocal;
1710
1711         initialized = true;
1712         return true;
1713 }
1714
1715 // Constructor
1716 P11DSADomainObj::P11DSADomainObj()
1717 {
1718         initialized = false;
1719 }
1720
1721 // Add attributes
1722 bool P11DSADomainObj::init(OSObject *inobject)
1723 {
1724         if (initialized) return true;
1725         if (inobject == NULL) return false;
1726
1727         if (!inobject->attributeExists(CKA_KEY_TYPE) || inobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != CKK_DSA) {
1728                 OSAttribute setKeyType((unsigned long)CKK_DSA);
1729                 inobject->setAttribute(CKA_KEY_TYPE, setKeyType);
1730         }
1731
1732         // Create parent
1733         if (!P11DomainObj::init(inobject)) return false;
1734
1735         // Create attributes
1736         P11Attribute* attrPrime = new P11AttrPrime(osobject,P11Attribute::ck4);
1737         P11Attribute* attrSubPrime = new P11AttrSubPrime(osobject,P11Attribute::ck4);
1738         P11Attribute* attrBase = new P11AttrBase(osobject,P11Attribute::ck4);
1739         P11Attribute* attrPrimeBits = new P11AttrPrimeBits(osobject);
1740
1741         // Initialize the attributes
1742         if
1743         (
1744                 !attrPrime->init() ||
1745                 !attrSubPrime->init() ||
1746                 !attrBase->init() ||
1747                 !attrPrimeBits->init()
1748         )
1749         {
1750                 ERROR_MSG("Could not initialize the attribute");
1751                 delete attrPrime;
1752                 delete attrSubPrime;
1753                 delete attrBase;
1754                 delete attrPrimeBits;
1755                 return false;
1756         }
1757
1758         // Add them to the map
1759         attributes[attrPrime->getType()] = attrPrime;
1760         attributes[attrSubPrime->getType()] = attrSubPrime;
1761         attributes[attrBase->getType()] = attrBase;
1762         attributes[attrPrimeBits->getType()] = attrPrimeBits;
1763
1764         initialized = true;
1765         return true;
1766 }
1767
1768 // Constructor
1769 P11DHDomainObj::P11DHDomainObj()
1770 {
1771         initialized = false;
1772 }
1773
1774 // Add attributes
1775 bool P11DHDomainObj::init(OSObject *inobject)
1776 {
1777         if (initialized) return true;
1778         if (inobject == NULL) return false;
1779
1780         if (!inobject->attributeExists(CKA_KEY_TYPE) || inobject->getUnsignedLongValue(CKA_KEY_TYPE, CKK_VENDOR_DEFINED) != CKK_DH) {
1781                 OSAttribute setKeyType((unsigned long)CKK_DH);
1782                 inobject->setAttribute(CKA_KEY_TYPE, setKeyType);
1783         }
1784
1785         // Create parent
1786         if (!P11DomainObj::init(inobject)) return false;
1787
1788         // Create attributes
1789         P11Attribute* attrPrime = new P11AttrPrime(osobject,P11Attribute::ck4);
1790         P11Attribute* attrBase = new P11AttrBase(osobject,P11Attribute::ck4);
1791         P11Attribute* attrPrimeBits = new P11AttrPrimeBits(osobject);
1792
1793         // Initialize the attributes
1794         if
1795         (
1796                 !attrPrime->init() ||
1797                 !attrBase->init() ||
1798                 !attrPrimeBits->init()
1799         )
1800         {
1801                 ERROR_MSG("Could not initialize the attribute");
1802                 delete attrPrime;
1803                 delete attrBase;
1804                 delete attrPrimeBits;
1805                 return false;
1806         }
1807
1808         // Add them to the map
1809         attributes[attrPrime->getType()] = attrPrime;
1810         attributes[attrBase->getType()] = attrBase;
1811         attributes[attrPrimeBits->getType()] = attrPrimeBits;
1812
1813         initialized = true;
1814         return true;
1815 }