147e946e7SWyllys Ingersoll /*
247e946e7SWyllys Ingersoll  *		Common Public License Version 0.5
347e946e7SWyllys Ingersoll  *
447e946e7SWyllys Ingersoll  *		THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF
547e946e7SWyllys Ingersoll  *		THIS COMMON PUBLIC LICENSE ("AGREEMENT"). ANY USE,
647e946e7SWyllys Ingersoll  *		REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
747e946e7SWyllys Ingersoll  *		RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
847e946e7SWyllys Ingersoll  *
947e946e7SWyllys Ingersoll  *		1. DEFINITIONS
1047e946e7SWyllys Ingersoll  *
1147e946e7SWyllys Ingersoll  *		"Contribution" means:
1247e946e7SWyllys Ingersoll  *		      a) in the case of the initial Contributor, the
1347e946e7SWyllys Ingersoll  *		      initial code and documentation distributed under
1447e946e7SWyllys Ingersoll  *		      this Agreement, and
1547e946e7SWyllys Ingersoll  *
1647e946e7SWyllys Ingersoll  *		      b) in the case of each subsequent Contributor:
1747e946e7SWyllys Ingersoll  *		      i) changes to the Program, and
1847e946e7SWyllys Ingersoll  *		      ii) additions to the Program;
1947e946e7SWyllys Ingersoll  *
2047e946e7SWyllys Ingersoll  *		      where such changes and/or additions to the Program
2147e946e7SWyllys Ingersoll  *		      originate from and are distributed by that
2247e946e7SWyllys Ingersoll  *		      particular Contributor. A Contribution 'originates'
2347e946e7SWyllys Ingersoll  *		      from a Contributor if it was added to the Program
2447e946e7SWyllys Ingersoll  *		      by such Contributor itself or anyone acting on such
2547e946e7SWyllys Ingersoll  *		      Contributor's behalf. Contributions do not include
2647e946e7SWyllys Ingersoll  *		      additions to the Program which: (i) are separate
2747e946e7SWyllys Ingersoll  *		      modules of software distributed in conjunction with
2847e946e7SWyllys Ingersoll  *		      the Program under their own license agreement, and
2947e946e7SWyllys Ingersoll  *		      (ii) are not derivative works of the Program.
3047e946e7SWyllys Ingersoll  *
3147e946e7SWyllys Ingersoll  *
3247e946e7SWyllys Ingersoll  *		"Contributor" means any person or entity that distributes
3347e946e7SWyllys Ingersoll  *		the Program.
3447e946e7SWyllys Ingersoll  *
3547e946e7SWyllys Ingersoll  *		"Licensed Patents " mean patent claims licensable by a
3647e946e7SWyllys Ingersoll  *		Contributor which are necessarily infringed by the use or
3747e946e7SWyllys Ingersoll  *		sale of its Contribution alone or when combined with the
3847e946e7SWyllys Ingersoll  *		Program.
3947e946e7SWyllys Ingersoll  *
4047e946e7SWyllys Ingersoll  *		"Program" means the Contributions distributed in
4147e946e7SWyllys Ingersoll  *		accordance with this Agreement.
4247e946e7SWyllys Ingersoll  *
4347e946e7SWyllys Ingersoll  *		"Recipient" means anyone who receives the Program under
4447e946e7SWyllys Ingersoll  *		this Agreement, including all Contributors.
4547e946e7SWyllys Ingersoll  *
4647e946e7SWyllys Ingersoll  *		2. GRANT OF RIGHTS
4747e946e7SWyllys Ingersoll  *
4847e946e7SWyllys Ingersoll  *		      a) Subject to the terms of this Agreement, each
4947e946e7SWyllys Ingersoll  *		      Contributor hereby grants Recipient a
5047e946e7SWyllys Ingersoll  *		      no - exclusive, worldwide, royalt - free copyright
5147e946e7SWyllys Ingersoll  *		      license to reproduce, prepare derivative works of,
5247e946e7SWyllys Ingersoll  *		      publicly display, publicly perform, distribute and
5347e946e7SWyllys Ingersoll  *		      sublicense the Contribution of such Contributor, if
5447e946e7SWyllys Ingersoll  *		      any, and such derivative works, in source code and
5547e946e7SWyllys Ingersoll  *		      object code form.
5647e946e7SWyllys Ingersoll  *
5747e946e7SWyllys Ingersoll  *		      b) Subject to the terms of this Agreement, each
5847e946e7SWyllys Ingersoll  *		      Contributor hereby grants Recipient a
5947e946e7SWyllys Ingersoll  *		      no - exclusive, worldwide, royalt - free patent
6047e946e7SWyllys Ingersoll  *		      license under Licensed Patents to make, use, sell,
6147e946e7SWyllys Ingersoll  *		      offer to sell, import and otherwise transfer the
6247e946e7SWyllys Ingersoll  *		      Contribution of such Contributor, if any, in source
6347e946e7SWyllys Ingersoll  *		      code and object code form. This patent license
6447e946e7SWyllys Ingersoll  *		      shall apply to the combination of the Contribution
6547e946e7SWyllys Ingersoll  *		      and the Program if, at the time the Contribution is
6647e946e7SWyllys Ingersoll  *		      added by the Contributor, such addition of the
6747e946e7SWyllys Ingersoll  *		      Contribution causes such combination to be covered
6847e946e7SWyllys Ingersoll  *		      by the Licensed Patents. The patent license shall
6947e946e7SWyllys Ingersoll  *		      not apply to any other combinations which include
7047e946e7SWyllys Ingersoll  *		      the Contribution. No hardware per se is licensed
7147e946e7SWyllys Ingersoll  *		      hereunder.
7247e946e7SWyllys Ingersoll  *
7347e946e7SWyllys Ingersoll  *		      c) Recipient understands that although each
7447e946e7SWyllys Ingersoll  *		      Contributor grants the licenses to its
7547e946e7SWyllys Ingersoll  *		      Contributions set forth herein, no assurances are
7647e946e7SWyllys Ingersoll  *		      provided by any Contributor that the Program does
7747e946e7SWyllys Ingersoll  *		      not infringe the patent or other intellectual
7847e946e7SWyllys Ingersoll  *		      property rights of any other entity. Each
7947e946e7SWyllys Ingersoll  *		      Contributor disclaims any liability to Recipient
8047e946e7SWyllys Ingersoll  *		      for claims brought by any other entity based on
8147e946e7SWyllys Ingersoll  *		      infringement of intellectual property rights or
8247e946e7SWyllys Ingersoll  *		      otherwise. As a condition to exercising the rights
8347e946e7SWyllys Ingersoll  *		      and licenses granted hereunder, each Recipient
8447e946e7SWyllys Ingersoll  *		      hereby assumes sole responsibility to secure any
8547e946e7SWyllys Ingersoll  *		      other intellectual property rights needed, if any.
8647e946e7SWyllys Ingersoll  *
8747e946e7SWyllys Ingersoll  *		      For example, if a third party patent license is
8847e946e7SWyllys Ingersoll  *		      required to allow Recipient to distribute the
8947e946e7SWyllys Ingersoll  *		      Program, it is Recipient's responsibility to
9047e946e7SWyllys Ingersoll  *		      acquire that license before distributing the
9147e946e7SWyllys Ingersoll  *		      Program.
9247e946e7SWyllys Ingersoll  *
9347e946e7SWyllys Ingersoll  *		      d) Each Contributor represents that to its
9447e946e7SWyllys Ingersoll  *		      knowledge it has sufficient copyright rights in its
9547e946e7SWyllys Ingersoll  *		      Contribution, if any, to grant the copyright
9647e946e7SWyllys Ingersoll  *		      license set forth in this Agreement.
9747e946e7SWyllys Ingersoll  *
9847e946e7SWyllys Ingersoll  *		3. REQUIREMENTS
9947e946e7SWyllys Ingersoll  *
10047e946e7SWyllys Ingersoll  *		A Contributor may choose to distribute the Program in
10147e946e7SWyllys Ingersoll  *		object code form under its own license agreement, provided
10247e946e7SWyllys Ingersoll  *		that:
10347e946e7SWyllys Ingersoll  *		      a) it complies with the terms and conditions of
10447e946e7SWyllys Ingersoll  *		      this Agreement; and
10547e946e7SWyllys Ingersoll  *
10647e946e7SWyllys Ingersoll  *		      b) its license agreement:
10747e946e7SWyllys Ingersoll  *		      i) effectively disclaims on behalf of all
10847e946e7SWyllys Ingersoll  *		      Contributors all warranties and conditions, express
10947e946e7SWyllys Ingersoll  *		      and implied, including warranties or conditions of
11047e946e7SWyllys Ingersoll  *		      title and no - infringement, and implied warranties
11147e946e7SWyllys Ingersoll  *		      or conditions of merchantability and fitness for a
11247e946e7SWyllys Ingersoll  *		      particular purpose;
11347e946e7SWyllys Ingersoll  *
11447e946e7SWyllys Ingersoll  *		      ii) effectively excludes on behalf of all
11547e946e7SWyllys Ingersoll  *		      Contributors all liability for damages, including
11647e946e7SWyllys Ingersoll  *		      direct, indirect, special, incidental and
11747e946e7SWyllys Ingersoll  *		      consequential damages, such as lost profits;
11847e946e7SWyllys Ingersoll  *
11947e946e7SWyllys Ingersoll  *		      iii) states that any provisions which differ from
12047e946e7SWyllys Ingersoll  *		      this Agreement are offered by that Contributor
12147e946e7SWyllys Ingersoll  *		      alone and not by any other party; and
12247e946e7SWyllys Ingersoll  *
12347e946e7SWyllys Ingersoll  *		      iv) states that source code for the Program is
12447e946e7SWyllys Ingersoll  *		      available from such Contributor, and informs
12547e946e7SWyllys Ingersoll  *		      licensees how to obtain it in a reasonable manner
12647e946e7SWyllys Ingersoll  *		      on or through a medium customarily used for
12747e946e7SWyllys Ingersoll  *		      software exchange.
12847e946e7SWyllys Ingersoll  *
12947e946e7SWyllys Ingersoll  *		When the Program is made available in source code form:
13047e946e7SWyllys Ingersoll  *		      a) it must be made available under this Agreement;
13147e946e7SWyllys Ingersoll  *		      and
13247e946e7SWyllys Ingersoll  *		      b) a copy of this Agreement must be included with
13347e946e7SWyllys Ingersoll  *		      each copy of the Program.
13447e946e7SWyllys Ingersoll  *
13547e946e7SWyllys Ingersoll  *		Contributors may not remove or alter any copyright notices
13647e946e7SWyllys Ingersoll  *		contained within the Program.
13747e946e7SWyllys Ingersoll  *
13847e946e7SWyllys Ingersoll  *		Each Contributor must identify itself as the originator of
13947e946e7SWyllys Ingersoll  *		its Contribution, if any, in a manner that reasonably
14047e946e7SWyllys Ingersoll  *		allows subsequent Recipients to identify the originator of
14147e946e7SWyllys Ingersoll  *		the Contribution.
14247e946e7SWyllys Ingersoll  *
14347e946e7SWyllys Ingersoll  *
14447e946e7SWyllys Ingersoll  *		4. COMMERCIAL DISTRIBUTION
14547e946e7SWyllys Ingersoll  *
14647e946e7SWyllys Ingersoll  *		Commercial distributors of software may accept certain
14747e946e7SWyllys Ingersoll  *		responsibilities with respect to end users, business
14847e946e7SWyllys Ingersoll  *		partners and the like. While this license is intended to
14947e946e7SWyllys Ingersoll  *		facilitate the commercial use of the Program, the
15047e946e7SWyllys Ingersoll  *		Contributor who includes the Program in a commercial
15147e946e7SWyllys Ingersoll  *		product offering should do so in a manner which does not
15247e946e7SWyllys Ingersoll  *		create potential liability for other Contributors.
15347e946e7SWyllys Ingersoll  *		Therefore, if a Contributor includes the Program in a
15447e946e7SWyllys Ingersoll  *		commercial product offering, such Contributor ("Commercial
15547e946e7SWyllys Ingersoll  *		Contributor") hereby agrees to defend and indemnify every
15647e946e7SWyllys Ingersoll  *		other Contributor ("Indemnified Contributor") against any
15747e946e7SWyllys Ingersoll  *		losses, damages and costs (collectively "Losses") arising
15847e946e7SWyllys Ingersoll  *		from claims, lawsuits and other legal actions brought by a
15947e946e7SWyllys Ingersoll  *		third party against the Indemnified Contributor to the
16047e946e7SWyllys Ingersoll  *		extent caused by the acts or omissions of such Commercial
16147e946e7SWyllys Ingersoll  *		Contributor in connection with its distribution of the
16247e946e7SWyllys Ingersoll  *		Program in a commercial product offering. The obligations
16347e946e7SWyllys Ingersoll  *		in this section do not apply to any claims or Losses
16447e946e7SWyllys Ingersoll  *		relating to any actual or alleged intellectual property
16547e946e7SWyllys Ingersoll  *		infringement. In order to qualify, an Indemnified
16647e946e7SWyllys Ingersoll  *		Contributor must: a) promptly notify the Commercial
16747e946e7SWyllys Ingersoll  *		Contributor in writing of such claim, and b) allow the
16847e946e7SWyllys Ingersoll  *		Commercial Contributor to control, and cooperate with the
16947e946e7SWyllys Ingersoll  *		Commercial Contributor in, the defense and any related
17047e946e7SWyllys Ingersoll  *		settlement negotiations. The Indemnified Contributor may
17147e946e7SWyllys Ingersoll  *		participate in any such claim at its own expense.
17247e946e7SWyllys Ingersoll  *
17347e946e7SWyllys Ingersoll  *
17447e946e7SWyllys Ingersoll  *		For example, a Contributor might include the Program in a
17547e946e7SWyllys Ingersoll  *		commercial product offering, Product X. That Contributor
17647e946e7SWyllys Ingersoll  *		is then a Commercial Contributor. If that Commercial
17747e946e7SWyllys Ingersoll  *		Contributor then makes performance claims, or offers
17847e946e7SWyllys Ingersoll  *		warranties related to Product X, those performance claims
17947e946e7SWyllys Ingersoll  *		and warranties are such Commercial Contributor's
18047e946e7SWyllys Ingersoll  *		responsibility alone. Under this section, the Commercial
18147e946e7SWyllys Ingersoll  *		Contributor would have to defend claims against the other
18247e946e7SWyllys Ingersoll  *		Contributors related to those performance claims and
18347e946e7SWyllys Ingersoll  *		warranties, and if a court requires any other Contributor
18447e946e7SWyllys Ingersoll  *		to pay any damages as a result, the Commercial Contributor
18547e946e7SWyllys Ingersoll  *		must pay those damages.
18647e946e7SWyllys Ingersoll  *
18747e946e7SWyllys Ingersoll  *
18847e946e7SWyllys Ingersoll  *		5. NO WARRANTY
18947e946e7SWyllys Ingersoll  *
19047e946e7SWyllys Ingersoll  *		EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE
19147e946e7SWyllys Ingersoll  *		PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT
19247e946e7SWyllys Ingersoll  *		WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
19347e946e7SWyllys Ingersoll  *		IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR
19447e946e7SWyllys Ingersoll  *		CONDITIONS OF TITLE, NO - INFRINGEMENT, MERCHANTABILITY OR
19547e946e7SWyllys Ingersoll  *		FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely
19647e946e7SWyllys Ingersoll  *		responsible for determining the appropriateness of using
19747e946e7SWyllys Ingersoll  *		and distributing the Program and assumes all risks
19847e946e7SWyllys Ingersoll  *		associated with its exercise of rights under this
19947e946e7SWyllys Ingersoll  *		Agreement, including but not limited to the risks and
20047e946e7SWyllys Ingersoll  *		costs of program errors, compliance with applicable laws,
20147e946e7SWyllys Ingersoll  *		damage to or loss of data, programs or equipment, and
20247e946e7SWyllys Ingersoll  *		unavailability or interruption of operations.
20347e946e7SWyllys Ingersoll  *
20447e946e7SWyllys Ingersoll  *		6. DISCLAIMER OF LIABILITY
20547e946e7SWyllys Ingersoll  *		EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER
20647e946e7SWyllys Ingersoll  *		RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY
20747e946e7SWyllys Ingersoll  *		FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20847e946e7SWyllys Ingersoll  *		OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION
20947e946e7SWyllys Ingersoll  *		LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF
21047e946e7SWyllys Ingersoll  *		LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21147e946e7SWyllys Ingersoll  *		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
21247e946e7SWyllys Ingersoll  *		OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE
21347e946e7SWyllys Ingersoll  *		OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE
21447e946e7SWyllys Ingersoll  *		POSSIBILITY OF SUCH DAMAGES.
21547e946e7SWyllys Ingersoll  *
21647e946e7SWyllys Ingersoll  *		7. GENERAL
21747e946e7SWyllys Ingersoll  *
21847e946e7SWyllys Ingersoll  *		If any provision of this Agreement is invalid or
21947e946e7SWyllys Ingersoll  *		unenforceable under applicable law, it shall not affect
22047e946e7SWyllys Ingersoll  *		the validity or enforceability of the remainder of the
22147e946e7SWyllys Ingersoll  *		terms of this Agreement, and without further action by the
22247e946e7SWyllys Ingersoll  *		parties hereto, such provision shall be reformed to the
22347e946e7SWyllys Ingersoll  *		minimum extent necessary to make such provision valid and
22447e946e7SWyllys Ingersoll  *		enforceable.
22547e946e7SWyllys Ingersoll  *
22647e946e7SWyllys Ingersoll  *
22747e946e7SWyllys Ingersoll  *		If Recipient institutes patent litigation against a
22847e946e7SWyllys Ingersoll  *		Contributor with respect to a patent applicable to
22947e946e7SWyllys Ingersoll  *		software (including a cros - claim or counterclaim in a
23047e946e7SWyllys Ingersoll  *		lawsuit), then any patent licenses granted by that
23147e946e7SWyllys Ingersoll  *		Contributor to such Recipient under this Agreement shall
23247e946e7SWyllys Ingersoll  *		terminate as of the date such litigation is filed. In
23347e946e7SWyllys Ingersoll  *		addition, If Recipient institutes patent litigation
23447e946e7SWyllys Ingersoll  *		against any entity (including a cros - claim or
23547e946e7SWyllys Ingersoll  *		counterclaim in a lawsuit) alleging that the Program
23647e946e7SWyllys Ingersoll  *		itself (excluding combinations of the Program with other
23747e946e7SWyllys Ingersoll  *		software or hardware) infringes such Recipient's
23847e946e7SWyllys Ingersoll  *		patent(s), then such Recipient's rights granted under
23947e946e7SWyllys Ingersoll  *		Section 2(b) shall terminate as of the date such
24047e946e7SWyllys Ingersoll  *		litigation is filed.
24147e946e7SWyllys Ingersoll  *
24247e946e7SWyllys Ingersoll  *		All Recipient's rights under this Agreement shall
24347e946e7SWyllys Ingersoll  *		terminate if it fails to comply with any of the material
24447e946e7SWyllys Ingersoll  *		terms or conditions of this Agreement and does not cure
24547e946e7SWyllys Ingersoll  *		such failure in a reasonable period of time after becoming
24647e946e7SWyllys Ingersoll  *		aware of such noncompliance. If all Recipient's rights
24747e946e7SWyllys Ingersoll  *		under this Agreement terminate, Recipient agrees to cease
24847e946e7SWyllys Ingersoll  *		use and distribution of the Program as soon as reasonably
24947e946e7SWyllys Ingersoll  *		practicable. However, Recipient's obligations under this
25047e946e7SWyllys Ingersoll  *		Agreement and any licenses granted by Recipient relating
25147e946e7SWyllys Ingersoll  *		to the Program shall continue and survive.
25247e946e7SWyllys Ingersoll  *
25347e946e7SWyllys Ingersoll  *		Everyone is permitted to copy and distribute copies of
25447e946e7SWyllys Ingersoll  *		this Agreement, but in order to avoid inconsistency the
25547e946e7SWyllys Ingersoll  *		Agreement is copyrighted and may only be modified in the
25647e946e7SWyllys Ingersoll  *		following manner. The Agreement Steward reserves the right
25747e946e7SWyllys Ingersoll  *		to publish new versions (including revisions) of this
25847e946e7SWyllys Ingersoll  *		Agreement from time to time. No one other than the
25947e946e7SWyllys Ingersoll  *		Agreement Steward has the right to modify this Agreement.
26047e946e7SWyllys Ingersoll  *
26147e946e7SWyllys Ingersoll  *		IBM is the initial Agreement Steward. IBM may assign the
26247e946e7SWyllys Ingersoll  *		responsibility to serve as the Agreement Steward to a
26347e946e7SWyllys Ingersoll  *		suitable separate entity. Each new version of the
26447e946e7SWyllys Ingersoll  *		Agreement will be given a distinguishing version number.
26547e946e7SWyllys Ingersoll  *		The Program (including Contributions) may always be
26647e946e7SWyllys Ingersoll  *		distributed subject to the version of the Agreement under
26747e946e7SWyllys Ingersoll  *		which it was received. In addition, after a new version of
26847e946e7SWyllys Ingersoll  *		the Agreement is published, Contributor may elect to
26947e946e7SWyllys Ingersoll  *		distribute the Program (including its Contributions) under
27047e946e7SWyllys Ingersoll  *		the new version. Except as expressly stated in Sections
27147e946e7SWyllys Ingersoll  *		2(a) and 2(b) above, Recipient receives no rights or
27247e946e7SWyllys Ingersoll  *		licenses to the intellectual property of any Contributor
27347e946e7SWyllys Ingersoll  *		under this Agreement, whether expressly, by implication,
27447e946e7SWyllys Ingersoll  *		estoppel or otherwise. All rights in the Program not
27547e946e7SWyllys Ingersoll  *		expressly granted under this Agreement are reserved.
27647e946e7SWyllys Ingersoll  *
27747e946e7SWyllys Ingersoll  *
27847e946e7SWyllys Ingersoll  *		This Agreement is governed by the laws of the State of New
27947e946e7SWyllys Ingersoll  *		York and the intellectual property laws of the United
28047e946e7SWyllys Ingersoll  *		States of America. No party to this Agreement will bring a
28147e946e7SWyllys Ingersoll  *		legal action under this Agreement more than one year after
28247e946e7SWyllys Ingersoll  *		the cause of action arose. Each party waives its rights to
28347e946e7SWyllys Ingersoll  *		a jury trial in any resulting litigation.
28447e946e7SWyllys Ingersoll  *
28547e946e7SWyllys Ingersoll  *
28647e946e7SWyllys Ingersoll  *
28747e946e7SWyllys Ingersoll  * (C) COPYRIGHT International Business Machines Corp. 2001, 2002
28847e946e7SWyllys Ingersoll  */
28947e946e7SWyllys Ingersoll /*
29047e946e7SWyllys Ingersoll  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
29147e946e7SWyllys Ingersoll  * Use is subject to license terms.
29247e946e7SWyllys Ingersoll  */
29347e946e7SWyllys Ingersoll 
29447e946e7SWyllys Ingersoll #include "tpmtok_int.h"
29547e946e7SWyllys Ingersoll 
29647e946e7SWyllys Ingersoll // object_create()
29747e946e7SWyllys Ingersoll //
29847e946e7SWyllys Ingersoll // Creates an object with the specified attributes.  Verifies that all required
29947e946e7SWyllys Ingersoll // attributes are present and adds any missing attributes that have Cryptoki
30047e946e7SWyllys Ingersoll // defined default values.  This routine does not check whether the session is
30147e946e7SWyllys Ingersoll // authorized to create the object.  That is done elsewhere
30247e946e7SWyllys Ingersoll // (see object_mgr_create())
30347e946e7SWyllys Ingersoll CK_RV
object_create(CK_ATTRIBUTE * pTemplate,CK_ULONG ulCount,OBJECT ** obj)30447e946e7SWyllys Ingersoll object_create(CK_ATTRIBUTE  * pTemplate,
30547e946e7SWyllys Ingersoll 	CK_ULONG	ulCount,
30647e946e7SWyllys Ingersoll 	OBJECT	** obj)
30747e946e7SWyllys Ingersoll {
30847e946e7SWyllys Ingersoll 	OBJECT	* o	   = NULL;
30947e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * attr	= NULL;
31047e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * sensitive   = NULL;
31147e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * extractable = NULL;
31247e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * local	= NULL;
31347e946e7SWyllys Ingersoll 	CK_BBOOL	class_given = FALSE;
31447e946e7SWyllys Ingersoll 	CK_BBOOL	subclass_given = FALSE;
31547e946e7SWyllys Ingersoll 	CK_BBOOL	flag;
31647e946e7SWyllys Ingersoll 	CK_ULONG	class = 0xFFFFFFFF, subclass = 0xFFFFFFFF;
31747e946e7SWyllys Ingersoll 	CK_RV	   rc;
31847e946e7SWyllys Ingersoll 	unsigned int    i;
31947e946e7SWyllys Ingersoll 
32047e946e7SWyllys Ingersoll 	if (! pTemplate) {
32147e946e7SWyllys Ingersoll 		return (CKR_FUNCTION_FAILED);
32247e946e7SWyllys Ingersoll 	}
32347e946e7SWyllys Ingersoll 	// extract the object class and subclass
32447e946e7SWyllys Ingersoll 	//
32547e946e7SWyllys Ingersoll 	attr = pTemplate;
32647e946e7SWyllys Ingersoll 	for (i = 0; i < ulCount; i++, attr++) {
32747e946e7SWyllys Ingersoll 		if (attr->type == CKA_CLASS) {
32847e946e7SWyllys Ingersoll 			class = *(CK_OBJECT_CLASS *)attr->pValue;
32947e946e7SWyllys Ingersoll 			class_given = TRUE;
33047e946e7SWyllys Ingersoll 		}
33147e946e7SWyllys Ingersoll 
33247e946e7SWyllys Ingersoll 		if (attr->type == CKA_CERTIFICATE_TYPE) {
33347e946e7SWyllys Ingersoll 			subclass = *(CK_CERTIFICATE_TYPE *)attr->pValue;
33447e946e7SWyllys Ingersoll 			subclass_given = TRUE;
33547e946e7SWyllys Ingersoll 		}
33647e946e7SWyllys Ingersoll 
33747e946e7SWyllys Ingersoll 		if (attr->type == CKA_KEY_TYPE) {
33847e946e7SWyllys Ingersoll 			subclass = *(CK_KEY_TYPE *)attr->pValue;
33947e946e7SWyllys Ingersoll 			subclass_given = TRUE;
34047e946e7SWyllys Ingersoll 		}
34147e946e7SWyllys Ingersoll 
34247e946e7SWyllys Ingersoll 		if (attr->type == CKA_HW_FEATURE_TYPE) {
34347e946e7SWyllys Ingersoll 			subclass = *(CK_HW_FEATURE_TYPE *)attr->pValue;
34447e946e7SWyllys Ingersoll 			subclass_given = TRUE;
34547e946e7SWyllys Ingersoll 		}
34647e946e7SWyllys Ingersoll 	}
34747e946e7SWyllys Ingersoll 
34847e946e7SWyllys Ingersoll 	if (class_given == FALSE) {
34947e946e7SWyllys Ingersoll 		return (CKR_TEMPLATE_INCOMPLETE);
35047e946e7SWyllys Ingersoll 	}
35147e946e7SWyllys Ingersoll 	if (class != CKO_DATA && subclass_given != TRUE) {
35247e946e7SWyllys Ingersoll 		return (CKR_TEMPLATE_INCOMPLETE);
35347e946e7SWyllys Ingersoll 	}
35447e946e7SWyllys Ingersoll 
35547e946e7SWyllys Ingersoll 	rc = object_create_skel(pTemplate, ulCount,
35647e946e7SWyllys Ingersoll 	    MODE_CREATE, class, subclass, &o);
35747e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
35847e946e7SWyllys Ingersoll 		return (rc);
35947e946e7SWyllys Ingersoll 	}
36047e946e7SWyllys Ingersoll 	// for key objects, we need be careful...
36147e946e7SWyllys Ingersoll 	//
36247e946e7SWyllys Ingersoll 	// note:  I would think that keys loaded with C_CreateObject should
36347e946e7SWyllys Ingersoll 	//	have their CKA_NEVER_EXTRACTABLE == FALSE and
36447e946e7SWyllys Ingersoll 	//	CKA_ALWAYS_SENSITIVE == FALSE since the key data was presumably
36547e946e7SWyllys Ingersoll 	//	stored in the clear prior to the call to C_CreateObject.  The
36647e946e7SWyllys Ingersoll 	//	PKCS #11 spec doesn't impose this restriction however.
36747e946e7SWyllys Ingersoll 	//
36847e946e7SWyllys Ingersoll 	if (class == CKO_PRIVATE_KEY || class == CKO_SECRET_KEY) {
36947e946e7SWyllys Ingersoll 		rc = template_attribute_find(o->template, CKA_SENSITIVE, &attr);
37047e946e7SWyllys Ingersoll 		if (rc == FALSE) {
37147e946e7SWyllys Ingersoll 			rc = CKR_FUNCTION_FAILED;
37247e946e7SWyllys Ingersoll 			goto error;
37347e946e7SWyllys Ingersoll 		}
37447e946e7SWyllys Ingersoll 
37547e946e7SWyllys Ingersoll 		flag = *(CK_BBOOL *)attr->pValue;
37647e946e7SWyllys Ingersoll 
37747e946e7SWyllys Ingersoll 		rc = build_attribute(CKA_ALWAYS_SENSITIVE, &flag,
37847e946e7SWyllys Ingersoll 		    sizeof (CK_BYTE), &sensitive);
37947e946e7SWyllys Ingersoll 		if (rc != CKR_OK) {
38047e946e7SWyllys Ingersoll 			goto error;
38147e946e7SWyllys Ingersoll 		}
38247e946e7SWyllys Ingersoll 
38347e946e7SWyllys Ingersoll 		rc = template_attribute_find(o->template, CKA_EXTRACTABLE,
38447e946e7SWyllys Ingersoll 		    &attr);
38547e946e7SWyllys Ingersoll 		if (rc == FALSE) {
38647e946e7SWyllys Ingersoll 			rc = CKR_FUNCTION_FAILED;
38747e946e7SWyllys Ingersoll 			goto error;
38847e946e7SWyllys Ingersoll 		}
38947e946e7SWyllys Ingersoll 
39047e946e7SWyllys Ingersoll 		flag = *(CK_BBOOL *)attr->pValue;
39147e946e7SWyllys Ingersoll 		flag = (~flag) & 0x1;
39247e946e7SWyllys Ingersoll 
39347e946e7SWyllys Ingersoll 		rc = build_attribute(CKA_NEVER_EXTRACTABLE, &flag,
39447e946e7SWyllys Ingersoll 		    sizeof (CK_BYTE), &extractable);
39547e946e7SWyllys Ingersoll 		if (rc != CKR_OK) {
39647e946e7SWyllys Ingersoll 			goto error;
39747e946e7SWyllys Ingersoll 		}
39847e946e7SWyllys Ingersoll 		(void) template_update_attribute(o->template, sensitive);
39947e946e7SWyllys Ingersoll 		(void) template_update_attribute(o->template, extractable);
40047e946e7SWyllys Ingersoll 	}
40147e946e7SWyllys Ingersoll 
40247e946e7SWyllys Ingersoll 	*obj = o;
40347e946e7SWyllys Ingersoll 
40447e946e7SWyllys Ingersoll 	return (CKR_OK);
40547e946e7SWyllys Ingersoll error:
40647e946e7SWyllys Ingersoll 	if (sensitive)    free(sensitive);
40747e946e7SWyllys Ingersoll 	if (extractable)  free(extractable);
40847e946e7SWyllys Ingersoll 	if (local)	free(local);
40947e946e7SWyllys Ingersoll 
41047e946e7SWyllys Ingersoll 	(void) object_free(o);
41147e946e7SWyllys Ingersoll 	return (rc);
41247e946e7SWyllys Ingersoll }
41347e946e7SWyllys Ingersoll 
41447e946e7SWyllys Ingersoll // object_copy()
41547e946e7SWyllys Ingersoll //
41647e946e7SWyllys Ingersoll // Builds a copy of the specified object.  The new object gets the original
41747e946e7SWyllys Ingersoll // object's attribute template plus any additional attributes that are
41847e946e7SWyllys Ingersoll // specified.   Verifies that all required attributes are present.  This
41947e946e7SWyllys Ingersoll // routine does not check whether the session is authorized to copy the
42047e946e7SWyllys Ingersoll // object -- routines at the individual object level don't have the
42147e946e7SWyllys Ingersoll // concept of "session".  These checks are done by the object manager.
42247e946e7SWyllys Ingersoll //
42347e946e7SWyllys Ingersoll CK_RV
object_copy(CK_ATTRIBUTE * pTemplate,CK_ULONG ulCount,OBJECT * old_obj,OBJECT ** new_obj)42447e946e7SWyllys Ingersoll object_copy(CK_ATTRIBUTE  * pTemplate,
42547e946e7SWyllys Ingersoll 	CK_ULONG	ulCount,
42647e946e7SWyllys Ingersoll 	OBJECT	* old_obj,
42747e946e7SWyllys Ingersoll 	OBJECT	** new_obj)
42847e946e7SWyllys Ingersoll {
42947e946e7SWyllys Ingersoll 	TEMPLATE  * tmpl	= NULL;
43047e946e7SWyllys Ingersoll 	TEMPLATE  * new_tmpl = NULL;
43147e946e7SWyllys Ingersoll 	OBJECT    * o	= NULL;
43247e946e7SWyllys Ingersoll 	CK_BBOOL    found;
43347e946e7SWyllys Ingersoll 	CK_ULONG    class, subclass;
43447e946e7SWyllys Ingersoll 	CK_RV	rc;
43547e946e7SWyllys Ingersoll 
43647e946e7SWyllys Ingersoll 
43747e946e7SWyllys Ingersoll 	if (! old_obj || ! pTemplate || ! new_obj) {
43847e946e7SWyllys Ingersoll 		return (CKR_FUNCTION_FAILED);
43947e946e7SWyllys Ingersoll 	}
44047e946e7SWyllys Ingersoll 	o	= (OBJECT   *)malloc(sizeof (OBJECT));
44147e946e7SWyllys Ingersoll 	tmpl	= (TEMPLATE *)malloc(sizeof (TEMPLATE));
44247e946e7SWyllys Ingersoll 	new_tmpl = (TEMPLATE *)malloc(sizeof (TEMPLATE));
44347e946e7SWyllys Ingersoll 
44447e946e7SWyllys Ingersoll 	if (! o || ! tmpl || ! new_tmpl) {
44547e946e7SWyllys Ingersoll 		rc = CKR_HOST_MEMORY;
44647e946e7SWyllys Ingersoll 		goto error;
44747e946e7SWyllys Ingersoll 	}
44847e946e7SWyllys Ingersoll 
44947e946e7SWyllys Ingersoll 	(void) memset(o, 0x0, sizeof (OBJECT));
45047e946e7SWyllys Ingersoll 	(void) memset(tmpl,	0x0, sizeof (TEMPLATE));
45147e946e7SWyllys Ingersoll 	(void) memset(new_tmpl, 0x0, sizeof (TEMPLATE));
45247e946e7SWyllys Ingersoll 
45347e946e7SWyllys Ingersoll 	// copy the original object's attribute template
45447e946e7SWyllys Ingersoll 	//
45547e946e7SWyllys Ingersoll 	rc = template_copy(tmpl, old_obj->template);
45647e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
45747e946e7SWyllys Ingersoll 		goto error;
45847e946e7SWyllys Ingersoll 	}
45947e946e7SWyllys Ingersoll 	rc = template_add_attributes(new_tmpl, pTemplate, ulCount);
46047e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
46147e946e7SWyllys Ingersoll 		goto error;
46247e946e7SWyllys Ingersoll 	}
46347e946e7SWyllys Ingersoll 	// at this point, the new object has the list of attributes.  we need
46447e946e7SWyllys Ingersoll 	// to do some more checking now:
46547e946e7SWyllys Ingersoll 	//    1) invalid attribute values
46647e946e7SWyllys Ingersoll 	//    2) missing required attributes
46747e946e7SWyllys Ingersoll 	//    3) attributes inappropriate for the object class
46847e946e7SWyllys Ingersoll 	//    4) conflicting attributes/values
46947e946e7SWyllys Ingersoll 	//
47047e946e7SWyllys Ingersoll 
47147e946e7SWyllys Ingersoll 	found = template_get_class(tmpl, &class, &subclass);
47247e946e7SWyllys Ingersoll 	if (found == FALSE) {
47347e946e7SWyllys Ingersoll 		rc = CKR_TEMPLATE_INCONSISTENT;
47447e946e7SWyllys Ingersoll 		goto error;
47547e946e7SWyllys Ingersoll 	}
47647e946e7SWyllys Ingersoll 
47747e946e7SWyllys Ingersoll 	// The user cannot change object classes so we assume the existing
47847e946e7SWyllys Ingersoll 	// object attributes are valid.  we still need to check the new
47947e946e7SWyllys Ingersoll 	// attributes.   We cannot merge the new attributes in with the
48047e946e7SWyllys Ingersoll 	// old ones and then check for validity because some attributes
48147e946e7SWyllys Ingersoll 	// are added internally and are not allowed to be specified by
48247e946e7SWyllys Ingersoll 	// the user (ie. CKA_LOCAL for key types) but may still be part
48347e946e7SWyllys Ingersoll 	// of the old template.
48447e946e7SWyllys Ingersoll 	rc = template_validate_attributes(new_tmpl, class, subclass,
48547e946e7SWyllys Ingersoll 	    MODE_COPY);
48647e946e7SWyllys Ingersoll 	if (rc != CKR_OK)
48747e946e7SWyllys Ingersoll 		goto error;
48847e946e7SWyllys Ingersoll 
48947e946e7SWyllys Ingersoll 	rc = template_merge(tmpl, &new_tmpl);
49047e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
49147e946e7SWyllys Ingersoll 		goto error;
49247e946e7SWyllys Ingersoll 	}
49347e946e7SWyllys Ingersoll 
49447e946e7SWyllys Ingersoll 	rc = template_check_required_attributes(tmpl, class,
49547e946e7SWyllys Ingersoll 	    subclass, MODE_COPY);
49647e946e7SWyllys Ingersoll 	if (rc != CKR_OK)
49747e946e7SWyllys Ingersoll 		goto error;
49847e946e7SWyllys Ingersoll 
49947e946e7SWyllys Ingersoll 	o->template = tmpl;
50047e946e7SWyllys Ingersoll 	*new_obj = o;
50147e946e7SWyllys Ingersoll 
50247e946e7SWyllys Ingersoll 	return (CKR_OK);
50347e946e7SWyllys Ingersoll error:
50447e946e7SWyllys Ingersoll 	if (tmpl)
50547e946e7SWyllys Ingersoll 		(void) template_free(tmpl);
50647e946e7SWyllys Ingersoll 	if (new_tmpl)
50747e946e7SWyllys Ingersoll 		(void) template_free(new_tmpl);
50847e946e7SWyllys Ingersoll 	if (o)
50947e946e7SWyllys Ingersoll 		(void) object_free(o);
51047e946e7SWyllys Ingersoll 
51147e946e7SWyllys Ingersoll 	return (rc);
51247e946e7SWyllys Ingersoll }
51347e946e7SWyllys Ingersoll 
51447e946e7SWyllys Ingersoll CK_RV
object_flatten(OBJECT * obj,CK_BYTE ** data,UINT32 * len)51547e946e7SWyllys Ingersoll object_flatten(OBJECT    * obj,
51647e946e7SWyllys Ingersoll 	CK_BYTE  ** data,
517*33c15889SWyllys Ingersoll 	UINT32  *len)
51847e946e7SWyllys Ingersoll {
51947e946e7SWyllys Ingersoll 	CK_BYTE    * buf = NULL;
520*33c15889SWyllys Ingersoll 	CK_ULONG_32	tmpl_len, total_len;
521*33c15889SWyllys Ingersoll 	CK_ULONG_32	offset;
52247e946e7SWyllys Ingersoll 	CK_ULONG_32	count;
52347e946e7SWyllys Ingersoll 	long	 rc;
52447e946e7SWyllys Ingersoll 
52547e946e7SWyllys Ingersoll 	if (! obj) {
52647e946e7SWyllys Ingersoll 		return (CKR_FUNCTION_FAILED);
52747e946e7SWyllys Ingersoll 	}
52847e946e7SWyllys Ingersoll 	count    = template_get_count(obj->template);
52947e946e7SWyllys Ingersoll 	tmpl_len = template_get_compressed_size(obj->template);
53047e946e7SWyllys Ingersoll 
53147e946e7SWyllys Ingersoll 	total_len = tmpl_len + sizeof (CK_OBJECT_CLASS_32) +
53247e946e7SWyllys Ingersoll 	    sizeof (CK_ULONG_32) + 8;
53347e946e7SWyllys Ingersoll 
53447e946e7SWyllys Ingersoll 	buf = (CK_BYTE *)malloc(total_len);
53547e946e7SWyllys Ingersoll 	if (!buf) {
53647e946e7SWyllys Ingersoll 		return (CKR_HOST_MEMORY);
53747e946e7SWyllys Ingersoll 	}
53847e946e7SWyllys Ingersoll 
53947e946e7SWyllys Ingersoll 	(void) memset((CK_BYTE *)buf, 0x0, total_len);
54047e946e7SWyllys Ingersoll 
54147e946e7SWyllys Ingersoll 	offset = 0;
54247e946e7SWyllys Ingersoll 
54347e946e7SWyllys Ingersoll 	(void) memcpy(buf + offset, &obj->class, sizeof (CK_OBJECT_CLASS_32));
54447e946e7SWyllys Ingersoll 	offset += sizeof (CK_OBJECT_CLASS_32);
54547e946e7SWyllys Ingersoll 
54647e946e7SWyllys Ingersoll 	(void) memcpy(buf + offset, &count, sizeof (CK_ULONG_32));
54747e946e7SWyllys Ingersoll 	offset += sizeof (CK_ULONG_32);
54847e946e7SWyllys Ingersoll 
54947e946e7SWyllys Ingersoll 	(void) memcpy(buf + offset, &obj->name,  sizeof (CK_BYTE) * 8);
55047e946e7SWyllys Ingersoll 	offset += 8;
55147e946e7SWyllys Ingersoll 	rc = template_flatten(obj->template, buf + offset);
55247e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
55347e946e7SWyllys Ingersoll 		free(buf);
55447e946e7SWyllys Ingersoll 		return (rc);
55547e946e7SWyllys Ingersoll 	}
55647e946e7SWyllys Ingersoll 
55747e946e7SWyllys Ingersoll 	*data = buf;
55847e946e7SWyllys Ingersoll 	*len  = total_len;
55947e946e7SWyllys Ingersoll 
56047e946e7SWyllys Ingersoll 	return (CKR_OK);
56147e946e7SWyllys Ingersoll }
56247e946e7SWyllys Ingersoll 
56347e946e7SWyllys Ingersoll CK_BBOOL
object_free(OBJECT * obj)56447e946e7SWyllys Ingersoll object_free(OBJECT *obj)
56547e946e7SWyllys Ingersoll {
56647e946e7SWyllys Ingersoll 	(void) template_free(obj->template);
56747e946e7SWyllys Ingersoll 	free(obj);
56847e946e7SWyllys Ingersoll 
56947e946e7SWyllys Ingersoll 	return (TRUE);
57047e946e7SWyllys Ingersoll }
57147e946e7SWyllys Ingersoll 
57247e946e7SWyllys Ingersoll 
57347e946e7SWyllys Ingersoll // object_is_modifiable()
57447e946e7SWyllys Ingersoll //
57547e946e7SWyllys Ingersoll CK_BBOOL
object_is_modifiable(OBJECT * obj)57647e946e7SWyllys Ingersoll object_is_modifiable(OBJECT *obj)
57747e946e7SWyllys Ingersoll {
57847e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * attr = NULL;
57947e946e7SWyllys Ingersoll 	CK_BBOOL	modifiable;
58047e946e7SWyllys Ingersoll 	CK_BBOOL	found;
58147e946e7SWyllys Ingersoll 
58247e946e7SWyllys Ingersoll 	found = template_attribute_find(obj->template, CKA_MODIFIABLE, &attr);
58347e946e7SWyllys Ingersoll 	if (found == FALSE)
58447e946e7SWyllys Ingersoll 		return (TRUE);
58547e946e7SWyllys Ingersoll 
58647e946e7SWyllys Ingersoll 	modifiable = *(CK_BBOOL *)attr->pValue;
58747e946e7SWyllys Ingersoll 
58847e946e7SWyllys Ingersoll 	return (modifiable);
58947e946e7SWyllys Ingersoll }
59047e946e7SWyllys Ingersoll 
59147e946e7SWyllys Ingersoll 
59247e946e7SWyllys Ingersoll // object_is_private()
59347e946e7SWyllys Ingersoll //
59447e946e7SWyllys Ingersoll // an is_private member should probably be added to OBJECT
59547e946e7SWyllys Ingersoll //
59647e946e7SWyllys Ingersoll CK_BBOOL
object_is_private(OBJECT * obj)59747e946e7SWyllys Ingersoll object_is_private(OBJECT *obj) {
59847e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * attr = NULL;
59947e946e7SWyllys Ingersoll 	CK_BBOOL	priv;
60047e946e7SWyllys Ingersoll 	CK_BBOOL	found;
60147e946e7SWyllys Ingersoll 
60247e946e7SWyllys Ingersoll 	found = template_attribute_find(obj->template, CKA_PRIVATE, &attr);
60347e946e7SWyllys Ingersoll 	if (found == FALSE)
60447e946e7SWyllys Ingersoll 		return (TRUE);
60547e946e7SWyllys Ingersoll 
60647e946e7SWyllys Ingersoll 	if (attr == NULL)
60747e946e7SWyllys Ingersoll 		return (TRUE);
60847e946e7SWyllys Ingersoll 
60947e946e7SWyllys Ingersoll 	priv = *((CK_BBOOL *)attr->pValue);
61047e946e7SWyllys Ingersoll 
61147e946e7SWyllys Ingersoll 	return (priv);
61247e946e7SWyllys Ingersoll }
61347e946e7SWyllys Ingersoll 
61447e946e7SWyllys Ingersoll CK_BBOOL
object_is_public(OBJECT * obj)61547e946e7SWyllys Ingersoll object_is_public(OBJECT *obj) {
61647e946e7SWyllys Ingersoll 	CK_BBOOL rc;
61747e946e7SWyllys Ingersoll 
61847e946e7SWyllys Ingersoll 	rc = object_is_private(obj);
61947e946e7SWyllys Ingersoll 
62047e946e7SWyllys Ingersoll 	if (rc)
62147e946e7SWyllys Ingersoll 		return (FALSE);
62247e946e7SWyllys Ingersoll 	return (TRUE);
62347e946e7SWyllys Ingersoll }
62447e946e7SWyllys Ingersoll 
62547e946e7SWyllys Ingersoll CK_BBOOL
object_is_token_object(OBJECT * obj)62647e946e7SWyllys Ingersoll object_is_token_object(OBJECT *obj) {
62747e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * attr = NULL;
62847e946e7SWyllys Ingersoll 	CK_BBOOL	is_token;
62947e946e7SWyllys Ingersoll 	CK_BBOOL	found;
63047e946e7SWyllys Ingersoll 
63147e946e7SWyllys Ingersoll 	found = template_attribute_find(obj->template,
63247e946e7SWyllys Ingersoll 	    CKA_TOKEN, &attr);
63347e946e7SWyllys Ingersoll 	if (found == FALSE)
63447e946e7SWyllys Ingersoll 		return (FALSE);
63547e946e7SWyllys Ingersoll 	is_token = *(CK_BBOOL *)attr->pValue;
63647e946e7SWyllys Ingersoll 	return (is_token);
63747e946e7SWyllys Ingersoll }
63847e946e7SWyllys Ingersoll 
63947e946e7SWyllys Ingersoll CK_BBOOL
object_is_session_object(OBJECT * obj)64047e946e7SWyllys Ingersoll object_is_session_object(OBJECT *obj) {
64147e946e7SWyllys Ingersoll 	CK_BBOOL rc;
64247e946e7SWyllys Ingersoll 
64347e946e7SWyllys Ingersoll 	rc = object_is_token_object(obj);
64447e946e7SWyllys Ingersoll 
64547e946e7SWyllys Ingersoll 	if (rc)
64647e946e7SWyllys Ingersoll 		return (FALSE);
64747e946e7SWyllys Ingersoll 	else
64847e946e7SWyllys Ingersoll 		return (TRUE);
64947e946e7SWyllys Ingersoll }
65047e946e7SWyllys Ingersoll 
65147e946e7SWyllys Ingersoll CK_ULONG
object_get_size(OBJECT * obj)65247e946e7SWyllys Ingersoll object_get_size(OBJECT *obj) {
65347e946e7SWyllys Ingersoll 	CK_ULONG  size;
65447e946e7SWyllys Ingersoll 
65547e946e7SWyllys Ingersoll 	size = sizeof (OBJECT) + template_get_size(obj->template);
65647e946e7SWyllys Ingersoll 
65747e946e7SWyllys Ingersoll 	return (size);
65847e946e7SWyllys Ingersoll }
65947e946e7SWyllys Ingersoll 
66047e946e7SWyllys Ingersoll CK_RV
object_get_attribute_values(OBJECT * obj,CK_ATTRIBUTE * pTemplate,CK_ULONG ulCount)66147e946e7SWyllys Ingersoll object_get_attribute_values(OBJECT	* obj,
66247e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * pTemplate,
66347e946e7SWyllys Ingersoll 	CK_ULONG	ulCount)
66447e946e7SWyllys Ingersoll {
66547e946e7SWyllys Ingersoll 	TEMPLATE	  *obj_tmpl = NULL;
66647e946e7SWyllys Ingersoll 	CK_ATTRIBUTE	*attr	= NULL;
66747e946e7SWyllys Ingersoll 	CK_ULONG	   i;
66847e946e7SWyllys Ingersoll 	CK_BBOOL	   flag;
66947e946e7SWyllys Ingersoll 	CK_RV		rc;
67047e946e7SWyllys Ingersoll 
67147e946e7SWyllys Ingersoll 	rc = CKR_OK;
67247e946e7SWyllys Ingersoll 
67347e946e7SWyllys Ingersoll 	obj_tmpl = obj->template;
67447e946e7SWyllys Ingersoll 
67547e946e7SWyllys Ingersoll 	for (i = 0; i < ulCount; i++) {
67647e946e7SWyllys Ingersoll 		flag = template_check_exportability(obj_tmpl,
67747e946e7SWyllys Ingersoll 		    pTemplate[i].type);
67847e946e7SWyllys Ingersoll 		if (flag == FALSE) {
67947e946e7SWyllys Ingersoll 			rc = CKR_ATTRIBUTE_SENSITIVE;
68047e946e7SWyllys Ingersoll 			pTemplate[i].ulValueLen = (CK_ULONG) - 1;
68147e946e7SWyllys Ingersoll 			continue;
68247e946e7SWyllys Ingersoll 		}
68347e946e7SWyllys Ingersoll 
68447e946e7SWyllys Ingersoll 		flag = template_attribute_find(obj_tmpl,
68547e946e7SWyllys Ingersoll 		    pTemplate[i].type, &attr);
68647e946e7SWyllys Ingersoll 		if (flag == FALSE) {
68747e946e7SWyllys Ingersoll 			rc = CKR_ATTRIBUTE_TYPE_INVALID;
68847e946e7SWyllys Ingersoll 			pTemplate[i].ulValueLen = (CK_ULONG) - 1;
68947e946e7SWyllys Ingersoll 			continue;
69047e946e7SWyllys Ingersoll 		}
69147e946e7SWyllys Ingersoll 
69247e946e7SWyllys Ingersoll 		if (pTemplate[i].pValue == NULL) {
69347e946e7SWyllys Ingersoll 			pTemplate[i].ulValueLen = attr->ulValueLen;
69447e946e7SWyllys Ingersoll 		} else if (pTemplate[i].ulValueLen >= attr->ulValueLen) {
69547e946e7SWyllys Ingersoll 			(void) memcpy(pTemplate[i].pValue, attr->pValue,
69647e946e7SWyllys Ingersoll 			    attr->ulValueLen);
69747e946e7SWyllys Ingersoll 			pTemplate[i].ulValueLen = attr->ulValueLen;
69847e946e7SWyllys Ingersoll 		} else {
69947e946e7SWyllys Ingersoll 			rc = CKR_BUFFER_TOO_SMALL;
70047e946e7SWyllys Ingersoll 			pTemplate[i].ulValueLen = (CK_ULONG) - 1;
70147e946e7SWyllys Ingersoll 		}
70247e946e7SWyllys Ingersoll 	}
70347e946e7SWyllys Ingersoll 
70447e946e7SWyllys Ingersoll 	return (rc);
70547e946e7SWyllys Ingersoll }
70647e946e7SWyllys Ingersoll 
70747e946e7SWyllys Ingersoll CK_RV
object_set_attribute_values(OBJECT * obj,CK_ATTRIBUTE * pTemplate,CK_ULONG ulCount)70847e946e7SWyllys Ingersoll object_set_attribute_values(OBJECT	* obj,
70947e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * pTemplate,
71047e946e7SWyllys Ingersoll 	CK_ULONG	ulCount)
71147e946e7SWyllys Ingersoll {
71247e946e7SWyllys Ingersoll 	TEMPLATE * new_tmpl;
71347e946e7SWyllys Ingersoll 	CK_BBOOL   found;
71447e946e7SWyllys Ingersoll 	CK_ULONG   class, subclass;
71547e946e7SWyllys Ingersoll 	CK_RV	rc;
71647e946e7SWyllys Ingersoll 
71747e946e7SWyllys Ingersoll 	if (! obj || ! pTemplate) {
71847e946e7SWyllys Ingersoll 		return (CKR_FUNCTION_FAILED);
71947e946e7SWyllys Ingersoll 	}
72047e946e7SWyllys Ingersoll 
72147e946e7SWyllys Ingersoll 	found = template_get_class(obj->template, &class, &subclass);
72247e946e7SWyllys Ingersoll 	if (found == FALSE) {
72347e946e7SWyllys Ingersoll 		rc = CKR_FUNCTION_FAILED;
72447e946e7SWyllys Ingersoll 		goto error;
72547e946e7SWyllys Ingersoll 	}
72647e946e7SWyllys Ingersoll 
72747e946e7SWyllys Ingersoll 	new_tmpl = (TEMPLATE *)malloc(sizeof (TEMPLATE));
72847e946e7SWyllys Ingersoll 	if (! new_tmpl) {
72947e946e7SWyllys Ingersoll 		return (CKR_HOST_MEMORY);
73047e946e7SWyllys Ingersoll 	}
73147e946e7SWyllys Ingersoll 	(void) memset(new_tmpl, 0x0, sizeof (TEMPLATE));
73247e946e7SWyllys Ingersoll 
73347e946e7SWyllys Ingersoll 	rc = template_add_attributes(new_tmpl, pTemplate, ulCount);
73447e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
73547e946e7SWyllys Ingersoll 		goto error;
73647e946e7SWyllys Ingersoll 	}
73747e946e7SWyllys Ingersoll 
73847e946e7SWyllys Ingersoll 	rc = template_validate_attributes(new_tmpl, class,
73947e946e7SWyllys Ingersoll 	    subclass, MODE_MODIFY);
74047e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
74147e946e7SWyllys Ingersoll 		goto error;
74247e946e7SWyllys Ingersoll 	}
74347e946e7SWyllys Ingersoll 
74447e946e7SWyllys Ingersoll 	rc = template_merge(obj->template, &new_tmpl);
74547e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
74647e946e7SWyllys Ingersoll 		return (rc);
74747e946e7SWyllys Ingersoll 	}
74847e946e7SWyllys Ingersoll 	return (CKR_OK);
74947e946e7SWyllys Ingersoll error:
75047e946e7SWyllys Ingersoll 	if (new_tmpl)
75147e946e7SWyllys Ingersoll 		(void) template_free(new_tmpl);
75247e946e7SWyllys Ingersoll 	return (rc);
75347e946e7SWyllys Ingersoll }
75447e946e7SWyllys Ingersoll 
75547e946e7SWyllys Ingersoll CK_RV
object_restore(CK_BYTE * data,OBJECT ** new_obj,CK_BBOOL replace)75647e946e7SWyllys Ingersoll object_restore(CK_BYTE *data, OBJECT **new_obj, CK_BBOOL replace) {
75747e946e7SWyllys Ingersoll 	TEMPLATE  * tmpl = NULL;
75847e946e7SWyllys Ingersoll 	OBJECT    * obj  = NULL;
75947e946e7SWyllys Ingersoll 	CK_ULONG    offset = 0;
76047e946e7SWyllys Ingersoll 	CK_ULONG_32    count = 0;
76147e946e7SWyllys Ingersoll 	CK_RV	rc;
76247e946e7SWyllys Ingersoll 
76347e946e7SWyllys Ingersoll 	if (! data || ! new_obj) {
76447e946e7SWyllys Ingersoll 		return (CKR_FUNCTION_FAILED);
76547e946e7SWyllys Ingersoll 	}
76647e946e7SWyllys Ingersoll 	obj = (OBJECT *)malloc(sizeof (OBJECT));
76747e946e7SWyllys Ingersoll 	if (! obj) {
76847e946e7SWyllys Ingersoll 		rc = CKR_HOST_MEMORY;
76947e946e7SWyllys Ingersoll 		goto error;
77047e946e7SWyllys Ingersoll 	}
77147e946e7SWyllys Ingersoll 
77247e946e7SWyllys Ingersoll 
77347e946e7SWyllys Ingersoll 	(void) memset(obj, 0x0, sizeof (OBJECT));
77447e946e7SWyllys Ingersoll 
77547e946e7SWyllys Ingersoll 	(void) memcpy(&obj->class, data + offset, sizeof (CK_OBJECT_CLASS_32));
77647e946e7SWyllys Ingersoll 	offset += sizeof (CK_OBJECT_CLASS_32);
77747e946e7SWyllys Ingersoll 
77847e946e7SWyllys Ingersoll 	(void) memcpy(&count, data + offset, sizeof (CK_ULONG_32));
77947e946e7SWyllys Ingersoll 	offset += sizeof (CK_ULONG_32);
78047e946e7SWyllys Ingersoll 
78147e946e7SWyllys Ingersoll 
78247e946e7SWyllys Ingersoll 	(void) memcpy(&obj->name, data + offset, 8);
78347e946e7SWyllys Ingersoll 	offset += 8;
78447e946e7SWyllys Ingersoll 
78547e946e7SWyllys Ingersoll 	rc = template_unflatten(&tmpl, data + offset, count);
78647e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
78747e946e7SWyllys Ingersoll 		goto error;
78847e946e7SWyllys Ingersoll 	}
78947e946e7SWyllys Ingersoll 	obj->template = tmpl;
79047e946e7SWyllys Ingersoll 
79147e946e7SWyllys Ingersoll 	if (replace == FALSE) {
79247e946e7SWyllys Ingersoll 		 *new_obj = obj;
79347e946e7SWyllys Ingersoll 	} else {
79447e946e7SWyllys Ingersoll 		(void) template_free((*new_obj)->template);
79547e946e7SWyllys Ingersoll 		(void) memcpy(*new_obj, obj, sizeof (OBJECT));
79647e946e7SWyllys Ingersoll 
79747e946e7SWyllys Ingersoll 		free(obj);
79847e946e7SWyllys Ingersoll 	}
79947e946e7SWyllys Ingersoll 
80047e946e7SWyllys Ingersoll 	return (CKR_OK);
80147e946e7SWyllys Ingersoll error:
80247e946e7SWyllys Ingersoll 	if (obj)
80347e946e7SWyllys Ingersoll 		(void) object_free(obj);
80447e946e7SWyllys Ingersoll 	if (tmpl)
80547e946e7SWyllys Ingersoll 		(void) template_free(tmpl);
80647e946e7SWyllys Ingersoll 
80747e946e7SWyllys Ingersoll 	return (rc);
80847e946e7SWyllys Ingersoll }
80947e946e7SWyllys Ingersoll 
81047e946e7SWyllys Ingersoll 
81147e946e7SWyllys Ingersoll //
81247e946e7SWyllys Ingersoll //
81347e946e7SWyllys Ingersoll CK_RV
object_create_skel(CK_ATTRIBUTE * pTemplate,CK_ULONG ulCount,CK_ULONG mode,CK_ULONG class,CK_ULONG subclass,OBJECT ** obj)81447e946e7SWyllys Ingersoll object_create_skel(CK_ATTRIBUTE  * pTemplate,
81547e946e7SWyllys Ingersoll 	CK_ULONG	ulCount,
81647e946e7SWyllys Ingersoll 	CK_ULONG	mode,
81747e946e7SWyllys Ingersoll 	CK_ULONG	class,
81847e946e7SWyllys Ingersoll 	CK_ULONG	subclass,
81947e946e7SWyllys Ingersoll 	OBJECT	** obj) {
82047e946e7SWyllys Ingersoll 	TEMPLATE  * tmpl  = NULL;
82147e946e7SWyllys Ingersoll 	TEMPLATE  * tmpl2 = NULL;
82247e946e7SWyllys Ingersoll 	OBJECT   * o	= NULL;
82347e946e7SWyllys Ingersoll 	CK_RV	rc;
82447e946e7SWyllys Ingersoll 
82547e946e7SWyllys Ingersoll 
82647e946e7SWyllys Ingersoll 	if (! obj) {
82747e946e7SWyllys Ingersoll 		return (CKR_FUNCTION_FAILED);
82847e946e7SWyllys Ingersoll 	}
82947e946e7SWyllys Ingersoll 	if (! pTemplate && (ulCount != 0)) {
83047e946e7SWyllys Ingersoll 		return (CKR_FUNCTION_FAILED);
83147e946e7SWyllys Ingersoll 	}
83247e946e7SWyllys Ingersoll 	o	= (OBJECT *)malloc(sizeof (OBJECT));
83347e946e7SWyllys Ingersoll 	tmpl  = (TEMPLATE *)malloc(sizeof (TEMPLATE));
83447e946e7SWyllys Ingersoll 	tmpl2 = (TEMPLATE *)malloc(sizeof (TEMPLATE));
83547e946e7SWyllys Ingersoll 
83647e946e7SWyllys Ingersoll 	if (! o || ! tmpl || ! tmpl2) {
83747e946e7SWyllys Ingersoll 		rc = CKR_HOST_MEMORY;
83847e946e7SWyllys Ingersoll 		goto done;
83947e946e7SWyllys Ingersoll 	}
84047e946e7SWyllys Ingersoll 	(void) memset(o,	0x0, sizeof (OBJECT));
84147e946e7SWyllys Ingersoll 	(void) memset(tmpl,  0x0, sizeof (TEMPLATE));
84247e946e7SWyllys Ingersoll 	(void) memset(tmpl2, 0x0, sizeof (TEMPLATE));
84347e946e7SWyllys Ingersoll 
84447e946e7SWyllys Ingersoll 
84547e946e7SWyllys Ingersoll 	rc = template_add_default_attributes(tmpl, class, subclass, mode);
84647e946e7SWyllys Ingersoll 	if (rc != CKR_OK)
84747e946e7SWyllys Ingersoll 		goto done;
84847e946e7SWyllys Ingersoll 
84947e946e7SWyllys Ingersoll 	rc = template_add_attributes(tmpl2, pTemplate, ulCount);
85047e946e7SWyllys Ingersoll 	if (rc != CKR_OK)
85147e946e7SWyllys Ingersoll 		goto done;
85247e946e7SWyllys Ingersoll 
85347e946e7SWyllys Ingersoll 
85447e946e7SWyllys Ingersoll 	// at this point, the new template has the list of attributes.  we need
85547e946e7SWyllys Ingersoll 	// to do some more checking now:
85647e946e7SWyllys Ingersoll 	//    1) invalid attribute values
85747e946e7SWyllys Ingersoll 	//    2) missing required attributes
85847e946e7SWyllys Ingersoll 	//    3) attributes inappropriate for the object class
85947e946e7SWyllys Ingersoll 	//    4) conflicting attributes/values
86047e946e7SWyllys Ingersoll 	//
86147e946e7SWyllys Ingersoll 
86247e946e7SWyllys Ingersoll 	rc = template_validate_attributes(tmpl2, class, subclass, mode);
86347e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
86447e946e7SWyllys Ingersoll 		goto done;
86547e946e7SWyllys Ingersoll 	}
86647e946e7SWyllys Ingersoll 
86747e946e7SWyllys Ingersoll 	rc = template_check_required_attributes(tmpl2, class, subclass, mode);
86847e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
86947e946e7SWyllys Ingersoll 		goto done;
87047e946e7SWyllys Ingersoll 	}
87147e946e7SWyllys Ingersoll 
87247e946e7SWyllys Ingersoll 	rc = template_merge(tmpl, &tmpl2);
87347e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
87447e946e7SWyllys Ingersoll 		goto done;
87547e946e7SWyllys Ingersoll 	}
87647e946e7SWyllys Ingersoll 	// at this point, we should have a valid object with correct attributes
87747e946e7SWyllys Ingersoll 	//
87847e946e7SWyllys Ingersoll 	o->template = tmpl;
87947e946e7SWyllys Ingersoll 	 *obj = o;
88047e946e7SWyllys Ingersoll 
88147e946e7SWyllys Ingersoll 	return (CKR_OK);
88247e946e7SWyllys Ingersoll 	done:
88347e946e7SWyllys Ingersoll 	if (o)	free(o);
88447e946e7SWyllys Ingersoll 	if (tmpl)
88547e946e7SWyllys Ingersoll 		(void) template_free(tmpl);
88647e946e7SWyllys Ingersoll 	if (tmpl2)
88747e946e7SWyllys Ingersoll 		(void) template_free(tmpl2);
88847e946e7SWyllys Ingersoll 
88947e946e7SWyllys Ingersoll 	return (rc);
89047e946e7SWyllys Ingersoll }
891