xref: /illumos-gate/usr/src/lib/pkcs11/pkcs11_tpm/common/object.c (revision 47e946e784719ae402ace34695f67b0e6e76ae5c)
1*47e946e7SWyllys Ingersoll /*
2*47e946e7SWyllys Ingersoll  *		Common Public License Version 0.5
3*47e946e7SWyllys Ingersoll  *
4*47e946e7SWyllys Ingersoll  *		THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF
5*47e946e7SWyllys Ingersoll  *		THIS COMMON PUBLIC LICENSE ("AGREEMENT"). ANY USE,
6*47e946e7SWyllys Ingersoll  *		REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
7*47e946e7SWyllys Ingersoll  *		RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
8*47e946e7SWyllys Ingersoll  *
9*47e946e7SWyllys Ingersoll  *		1. DEFINITIONS
10*47e946e7SWyllys Ingersoll  *
11*47e946e7SWyllys Ingersoll  *		"Contribution" means:
12*47e946e7SWyllys Ingersoll  *		      a) in the case of the initial Contributor, the
13*47e946e7SWyllys Ingersoll  *		      initial code and documentation distributed under
14*47e946e7SWyllys Ingersoll  *		      this Agreement, and
15*47e946e7SWyllys Ingersoll  *
16*47e946e7SWyllys Ingersoll  *		      b) in the case of each subsequent Contributor:
17*47e946e7SWyllys Ingersoll  *		      i) changes to the Program, and
18*47e946e7SWyllys Ingersoll  *		      ii) additions to the Program;
19*47e946e7SWyllys Ingersoll  *
20*47e946e7SWyllys Ingersoll  *		      where such changes and/or additions to the Program
21*47e946e7SWyllys Ingersoll  *		      originate from and are distributed by that
22*47e946e7SWyllys Ingersoll  *		      particular Contributor. A Contribution 'originates'
23*47e946e7SWyllys Ingersoll  *		      from a Contributor if it was added to the Program
24*47e946e7SWyllys Ingersoll  *		      by such Contributor itself or anyone acting on such
25*47e946e7SWyllys Ingersoll  *		      Contributor's behalf. Contributions do not include
26*47e946e7SWyllys Ingersoll  *		      additions to the Program which: (i) are separate
27*47e946e7SWyllys Ingersoll  *		      modules of software distributed in conjunction with
28*47e946e7SWyllys Ingersoll  *		      the Program under their own license agreement, and
29*47e946e7SWyllys Ingersoll  *		      (ii) are not derivative works of the Program.
30*47e946e7SWyllys Ingersoll  *
31*47e946e7SWyllys Ingersoll  *
32*47e946e7SWyllys Ingersoll  *		"Contributor" means any person or entity that distributes
33*47e946e7SWyllys Ingersoll  *		the Program.
34*47e946e7SWyllys Ingersoll  *
35*47e946e7SWyllys Ingersoll  *		"Licensed Patents " mean patent claims licensable by a
36*47e946e7SWyllys Ingersoll  *		Contributor which are necessarily infringed by the use or
37*47e946e7SWyllys Ingersoll  *		sale of its Contribution alone or when combined with the
38*47e946e7SWyllys Ingersoll  *		Program.
39*47e946e7SWyllys Ingersoll  *
40*47e946e7SWyllys Ingersoll  *		"Program" means the Contributions distributed in
41*47e946e7SWyllys Ingersoll  *		accordance with this Agreement.
42*47e946e7SWyllys Ingersoll  *
43*47e946e7SWyllys Ingersoll  *		"Recipient" means anyone who receives the Program under
44*47e946e7SWyllys Ingersoll  *		this Agreement, including all Contributors.
45*47e946e7SWyllys Ingersoll  *
46*47e946e7SWyllys Ingersoll  *		2. GRANT OF RIGHTS
47*47e946e7SWyllys Ingersoll  *
48*47e946e7SWyllys Ingersoll  *		      a) Subject to the terms of this Agreement, each
49*47e946e7SWyllys Ingersoll  *		      Contributor hereby grants Recipient a
50*47e946e7SWyllys Ingersoll  *		      no - exclusive, worldwide, royalt - free copyright
51*47e946e7SWyllys Ingersoll  *		      license to reproduce, prepare derivative works of,
52*47e946e7SWyllys Ingersoll  *		      publicly display, publicly perform, distribute and
53*47e946e7SWyllys Ingersoll  *		      sublicense the Contribution of such Contributor, if
54*47e946e7SWyllys Ingersoll  *		      any, and such derivative works, in source code and
55*47e946e7SWyllys Ingersoll  *		      object code form.
56*47e946e7SWyllys Ingersoll  *
57*47e946e7SWyllys Ingersoll  *		      b) Subject to the terms of this Agreement, each
58*47e946e7SWyllys Ingersoll  *		      Contributor hereby grants Recipient a
59*47e946e7SWyllys Ingersoll  *		      no - exclusive, worldwide, royalt - free patent
60*47e946e7SWyllys Ingersoll  *		      license under Licensed Patents to make, use, sell,
61*47e946e7SWyllys Ingersoll  *		      offer to sell, import and otherwise transfer the
62*47e946e7SWyllys Ingersoll  *		      Contribution of such Contributor, if any, in source
63*47e946e7SWyllys Ingersoll  *		      code and object code form. This patent license
64*47e946e7SWyllys Ingersoll  *		      shall apply to the combination of the Contribution
65*47e946e7SWyllys Ingersoll  *		      and the Program if, at the time the Contribution is
66*47e946e7SWyllys Ingersoll  *		      added by the Contributor, such addition of the
67*47e946e7SWyllys Ingersoll  *		      Contribution causes such combination to be covered
68*47e946e7SWyllys Ingersoll  *		      by the Licensed Patents. The patent license shall
69*47e946e7SWyllys Ingersoll  *		      not apply to any other combinations which include
70*47e946e7SWyllys Ingersoll  *		      the Contribution. No hardware per se is licensed
71*47e946e7SWyllys Ingersoll  *		      hereunder.
72*47e946e7SWyllys Ingersoll  *
73*47e946e7SWyllys Ingersoll  *		      c) Recipient understands that although each
74*47e946e7SWyllys Ingersoll  *		      Contributor grants the licenses to its
75*47e946e7SWyllys Ingersoll  *		      Contributions set forth herein, no assurances are
76*47e946e7SWyllys Ingersoll  *		      provided by any Contributor that the Program does
77*47e946e7SWyllys Ingersoll  *		      not infringe the patent or other intellectual
78*47e946e7SWyllys Ingersoll  *		      property rights of any other entity. Each
79*47e946e7SWyllys Ingersoll  *		      Contributor disclaims any liability to Recipient
80*47e946e7SWyllys Ingersoll  *		      for claims brought by any other entity based on
81*47e946e7SWyllys Ingersoll  *		      infringement of intellectual property rights or
82*47e946e7SWyllys Ingersoll  *		      otherwise. As a condition to exercising the rights
83*47e946e7SWyllys Ingersoll  *		      and licenses granted hereunder, each Recipient
84*47e946e7SWyllys Ingersoll  *		      hereby assumes sole responsibility to secure any
85*47e946e7SWyllys Ingersoll  *		      other intellectual property rights needed, if any.
86*47e946e7SWyllys Ingersoll  *
87*47e946e7SWyllys Ingersoll  *		      For example, if a third party patent license is
88*47e946e7SWyllys Ingersoll  *		      required to allow Recipient to distribute the
89*47e946e7SWyllys Ingersoll  *		      Program, it is Recipient's responsibility to
90*47e946e7SWyllys Ingersoll  *		      acquire that license before distributing the
91*47e946e7SWyllys Ingersoll  *		      Program.
92*47e946e7SWyllys Ingersoll  *
93*47e946e7SWyllys Ingersoll  *		      d) Each Contributor represents that to its
94*47e946e7SWyllys Ingersoll  *		      knowledge it has sufficient copyright rights in its
95*47e946e7SWyllys Ingersoll  *		      Contribution, if any, to grant the copyright
96*47e946e7SWyllys Ingersoll  *		      license set forth in this Agreement.
97*47e946e7SWyllys Ingersoll  *
98*47e946e7SWyllys Ingersoll  *		3. REQUIREMENTS
99*47e946e7SWyllys Ingersoll  *
100*47e946e7SWyllys Ingersoll  *		A Contributor may choose to distribute the Program in
101*47e946e7SWyllys Ingersoll  *		object code form under its own license agreement, provided
102*47e946e7SWyllys Ingersoll  *		that:
103*47e946e7SWyllys Ingersoll  *		      a) it complies with the terms and conditions of
104*47e946e7SWyllys Ingersoll  *		      this Agreement; and
105*47e946e7SWyllys Ingersoll  *
106*47e946e7SWyllys Ingersoll  *		      b) its license agreement:
107*47e946e7SWyllys Ingersoll  *		      i) effectively disclaims on behalf of all
108*47e946e7SWyllys Ingersoll  *		      Contributors all warranties and conditions, express
109*47e946e7SWyllys Ingersoll  *		      and implied, including warranties or conditions of
110*47e946e7SWyllys Ingersoll  *		      title and no - infringement, and implied warranties
111*47e946e7SWyllys Ingersoll  *		      or conditions of merchantability and fitness for a
112*47e946e7SWyllys Ingersoll  *		      particular purpose;
113*47e946e7SWyllys Ingersoll  *
114*47e946e7SWyllys Ingersoll  *		      ii) effectively excludes on behalf of all
115*47e946e7SWyllys Ingersoll  *		      Contributors all liability for damages, including
116*47e946e7SWyllys Ingersoll  *		      direct, indirect, special, incidental and
117*47e946e7SWyllys Ingersoll  *		      consequential damages, such as lost profits;
118*47e946e7SWyllys Ingersoll  *
119*47e946e7SWyllys Ingersoll  *		      iii) states that any provisions which differ from
120*47e946e7SWyllys Ingersoll  *		      this Agreement are offered by that Contributor
121*47e946e7SWyllys Ingersoll  *		      alone and not by any other party; and
122*47e946e7SWyllys Ingersoll  *
123*47e946e7SWyllys Ingersoll  *		      iv) states that source code for the Program is
124*47e946e7SWyllys Ingersoll  *		      available from such Contributor, and informs
125*47e946e7SWyllys Ingersoll  *		      licensees how to obtain it in a reasonable manner
126*47e946e7SWyllys Ingersoll  *		      on or through a medium customarily used for
127*47e946e7SWyllys Ingersoll  *		      software exchange.
128*47e946e7SWyllys Ingersoll  *
129*47e946e7SWyllys Ingersoll  *		When the Program is made available in source code form:
130*47e946e7SWyllys Ingersoll  *		      a) it must be made available under this Agreement;
131*47e946e7SWyllys Ingersoll  *		      and
132*47e946e7SWyllys Ingersoll  *		      b) a copy of this Agreement must be included with
133*47e946e7SWyllys Ingersoll  *		      each copy of the Program.
134*47e946e7SWyllys Ingersoll  *
135*47e946e7SWyllys Ingersoll  *		Contributors may not remove or alter any copyright notices
136*47e946e7SWyllys Ingersoll  *		contained within the Program.
137*47e946e7SWyllys Ingersoll  *
138*47e946e7SWyllys Ingersoll  *		Each Contributor must identify itself as the originator of
139*47e946e7SWyllys Ingersoll  *		its Contribution, if any, in a manner that reasonably
140*47e946e7SWyllys Ingersoll  *		allows subsequent Recipients to identify the originator of
141*47e946e7SWyllys Ingersoll  *		the Contribution.
142*47e946e7SWyllys Ingersoll  *
143*47e946e7SWyllys Ingersoll  *
144*47e946e7SWyllys Ingersoll  *		4. COMMERCIAL DISTRIBUTION
145*47e946e7SWyllys Ingersoll  *
146*47e946e7SWyllys Ingersoll  *		Commercial distributors of software may accept certain
147*47e946e7SWyllys Ingersoll  *		responsibilities with respect to end users, business
148*47e946e7SWyllys Ingersoll  *		partners and the like. While this license is intended to
149*47e946e7SWyllys Ingersoll  *		facilitate the commercial use of the Program, the
150*47e946e7SWyllys Ingersoll  *		Contributor who includes the Program in a commercial
151*47e946e7SWyllys Ingersoll  *		product offering should do so in a manner which does not
152*47e946e7SWyllys Ingersoll  *		create potential liability for other Contributors.
153*47e946e7SWyllys Ingersoll  *		Therefore, if a Contributor includes the Program in a
154*47e946e7SWyllys Ingersoll  *		commercial product offering, such Contributor ("Commercial
155*47e946e7SWyllys Ingersoll  *		Contributor") hereby agrees to defend and indemnify every
156*47e946e7SWyllys Ingersoll  *		other Contributor ("Indemnified Contributor") against any
157*47e946e7SWyllys Ingersoll  *		losses, damages and costs (collectively "Losses") arising
158*47e946e7SWyllys Ingersoll  *		from claims, lawsuits and other legal actions brought by a
159*47e946e7SWyllys Ingersoll  *		third party against the Indemnified Contributor to the
160*47e946e7SWyllys Ingersoll  *		extent caused by the acts or omissions of such Commercial
161*47e946e7SWyllys Ingersoll  *		Contributor in connection with its distribution of the
162*47e946e7SWyllys Ingersoll  *		Program in a commercial product offering. The obligations
163*47e946e7SWyllys Ingersoll  *		in this section do not apply to any claims or Losses
164*47e946e7SWyllys Ingersoll  *		relating to any actual or alleged intellectual property
165*47e946e7SWyllys Ingersoll  *		infringement. In order to qualify, an Indemnified
166*47e946e7SWyllys Ingersoll  *		Contributor must: a) promptly notify the Commercial
167*47e946e7SWyllys Ingersoll  *		Contributor in writing of such claim, and b) allow the
168*47e946e7SWyllys Ingersoll  *		Commercial Contributor to control, and cooperate with the
169*47e946e7SWyllys Ingersoll  *		Commercial Contributor in, the defense and any related
170*47e946e7SWyllys Ingersoll  *		settlement negotiations. The Indemnified Contributor may
171*47e946e7SWyllys Ingersoll  *		participate in any such claim at its own expense.
172*47e946e7SWyllys Ingersoll  *
173*47e946e7SWyllys Ingersoll  *
174*47e946e7SWyllys Ingersoll  *		For example, a Contributor might include the Program in a
175*47e946e7SWyllys Ingersoll  *		commercial product offering, Product X. That Contributor
176*47e946e7SWyllys Ingersoll  *		is then a Commercial Contributor. If that Commercial
177*47e946e7SWyllys Ingersoll  *		Contributor then makes performance claims, or offers
178*47e946e7SWyllys Ingersoll  *		warranties related to Product X, those performance claims
179*47e946e7SWyllys Ingersoll  *		and warranties are such Commercial Contributor's
180*47e946e7SWyllys Ingersoll  *		responsibility alone. Under this section, the Commercial
181*47e946e7SWyllys Ingersoll  *		Contributor would have to defend claims against the other
182*47e946e7SWyllys Ingersoll  *		Contributors related to those performance claims and
183*47e946e7SWyllys Ingersoll  *		warranties, and if a court requires any other Contributor
184*47e946e7SWyllys Ingersoll  *		to pay any damages as a result, the Commercial Contributor
185*47e946e7SWyllys Ingersoll  *		must pay those damages.
186*47e946e7SWyllys Ingersoll  *
187*47e946e7SWyllys Ingersoll  *
188*47e946e7SWyllys Ingersoll  *		5. NO WARRANTY
189*47e946e7SWyllys Ingersoll  *
190*47e946e7SWyllys Ingersoll  *		EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE
191*47e946e7SWyllys Ingersoll  *		PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT
192*47e946e7SWyllys Ingersoll  *		WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
193*47e946e7SWyllys Ingersoll  *		IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR
194*47e946e7SWyllys Ingersoll  *		CONDITIONS OF TITLE, NO - INFRINGEMENT, MERCHANTABILITY OR
195*47e946e7SWyllys Ingersoll  *		FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely
196*47e946e7SWyllys Ingersoll  *		responsible for determining the appropriateness of using
197*47e946e7SWyllys Ingersoll  *		and distributing the Program and assumes all risks
198*47e946e7SWyllys Ingersoll  *		associated with its exercise of rights under this
199*47e946e7SWyllys Ingersoll  *		Agreement, including but not limited to the risks and
200*47e946e7SWyllys Ingersoll  *		costs of program errors, compliance with applicable laws,
201*47e946e7SWyllys Ingersoll  *		damage to or loss of data, programs or equipment, and
202*47e946e7SWyllys Ingersoll  *		unavailability or interruption of operations.
203*47e946e7SWyllys Ingersoll  *
204*47e946e7SWyllys Ingersoll  *		6. DISCLAIMER OF LIABILITY
205*47e946e7SWyllys Ingersoll  *		EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER
206*47e946e7SWyllys Ingersoll  *		RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY
207*47e946e7SWyllys Ingersoll  *		FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
208*47e946e7SWyllys Ingersoll  *		OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION
209*47e946e7SWyllys Ingersoll  *		LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF
210*47e946e7SWyllys Ingersoll  *		LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
211*47e946e7SWyllys Ingersoll  *		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
212*47e946e7SWyllys Ingersoll  *		OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE
213*47e946e7SWyllys Ingersoll  *		OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE
214*47e946e7SWyllys Ingersoll  *		POSSIBILITY OF SUCH DAMAGES.
215*47e946e7SWyllys Ingersoll  *
216*47e946e7SWyllys Ingersoll  *		7. GENERAL
217*47e946e7SWyllys Ingersoll  *
218*47e946e7SWyllys Ingersoll  *		If any provision of this Agreement is invalid or
219*47e946e7SWyllys Ingersoll  *		unenforceable under applicable law, it shall not affect
220*47e946e7SWyllys Ingersoll  *		the validity or enforceability of the remainder of the
221*47e946e7SWyllys Ingersoll  *		terms of this Agreement, and without further action by the
222*47e946e7SWyllys Ingersoll  *		parties hereto, such provision shall be reformed to the
223*47e946e7SWyllys Ingersoll  *		minimum extent necessary to make such provision valid and
224*47e946e7SWyllys Ingersoll  *		enforceable.
225*47e946e7SWyllys Ingersoll  *
226*47e946e7SWyllys Ingersoll  *
227*47e946e7SWyllys Ingersoll  *		If Recipient institutes patent litigation against a
228*47e946e7SWyllys Ingersoll  *		Contributor with respect to a patent applicable to
229*47e946e7SWyllys Ingersoll  *		software (including a cros - claim or counterclaim in a
230*47e946e7SWyllys Ingersoll  *		lawsuit), then any patent licenses granted by that
231*47e946e7SWyllys Ingersoll  *		Contributor to such Recipient under this Agreement shall
232*47e946e7SWyllys Ingersoll  *		terminate as of the date such litigation is filed. In
233*47e946e7SWyllys Ingersoll  *		addition, If Recipient institutes patent litigation
234*47e946e7SWyllys Ingersoll  *		against any entity (including a cros - claim or
235*47e946e7SWyllys Ingersoll  *		counterclaim in a lawsuit) alleging that the Program
236*47e946e7SWyllys Ingersoll  *		itself (excluding combinations of the Program with other
237*47e946e7SWyllys Ingersoll  *		software or hardware) infringes such Recipient's
238*47e946e7SWyllys Ingersoll  *		patent(s), then such Recipient's rights granted under
239*47e946e7SWyllys Ingersoll  *		Section 2(b) shall terminate as of the date such
240*47e946e7SWyllys Ingersoll  *		litigation is filed.
241*47e946e7SWyllys Ingersoll  *
242*47e946e7SWyllys Ingersoll  *		All Recipient's rights under this Agreement shall
243*47e946e7SWyllys Ingersoll  *		terminate if it fails to comply with any of the material
244*47e946e7SWyllys Ingersoll  *		terms or conditions of this Agreement and does not cure
245*47e946e7SWyllys Ingersoll  *		such failure in a reasonable period of time after becoming
246*47e946e7SWyllys Ingersoll  *		aware of such noncompliance. If all Recipient's rights
247*47e946e7SWyllys Ingersoll  *		under this Agreement terminate, Recipient agrees to cease
248*47e946e7SWyllys Ingersoll  *		use and distribution of the Program as soon as reasonably
249*47e946e7SWyllys Ingersoll  *		practicable. However, Recipient's obligations under this
250*47e946e7SWyllys Ingersoll  *		Agreement and any licenses granted by Recipient relating
251*47e946e7SWyllys Ingersoll  *		to the Program shall continue and survive.
252*47e946e7SWyllys Ingersoll  *
253*47e946e7SWyllys Ingersoll  *		Everyone is permitted to copy and distribute copies of
254*47e946e7SWyllys Ingersoll  *		this Agreement, but in order to avoid inconsistency the
255*47e946e7SWyllys Ingersoll  *		Agreement is copyrighted and may only be modified in the
256*47e946e7SWyllys Ingersoll  *		following manner. The Agreement Steward reserves the right
257*47e946e7SWyllys Ingersoll  *		to publish new versions (including revisions) of this
258*47e946e7SWyllys Ingersoll  *		Agreement from time to time. No one other than the
259*47e946e7SWyllys Ingersoll  *		Agreement Steward has the right to modify this Agreement.
260*47e946e7SWyllys Ingersoll  *
261*47e946e7SWyllys Ingersoll  *		IBM is the initial Agreement Steward. IBM may assign the
262*47e946e7SWyllys Ingersoll  *		responsibility to serve as the Agreement Steward to a
263*47e946e7SWyllys Ingersoll  *		suitable separate entity. Each new version of the
264*47e946e7SWyllys Ingersoll  *		Agreement will be given a distinguishing version number.
265*47e946e7SWyllys Ingersoll  *		The Program (including Contributions) may always be
266*47e946e7SWyllys Ingersoll  *		distributed subject to the version of the Agreement under
267*47e946e7SWyllys Ingersoll  *		which it was received. In addition, after a new version of
268*47e946e7SWyllys Ingersoll  *		the Agreement is published, Contributor may elect to
269*47e946e7SWyllys Ingersoll  *		distribute the Program (including its Contributions) under
270*47e946e7SWyllys Ingersoll  *		the new version. Except as expressly stated in Sections
271*47e946e7SWyllys Ingersoll  *		2(a) and 2(b) above, Recipient receives no rights or
272*47e946e7SWyllys Ingersoll  *		licenses to the intellectual property of any Contributor
273*47e946e7SWyllys Ingersoll  *		under this Agreement, whether expressly, by implication,
274*47e946e7SWyllys Ingersoll  *		estoppel or otherwise. All rights in the Program not
275*47e946e7SWyllys Ingersoll  *		expressly granted under this Agreement are reserved.
276*47e946e7SWyllys Ingersoll  *
277*47e946e7SWyllys Ingersoll  *
278*47e946e7SWyllys Ingersoll  *		This Agreement is governed by the laws of the State of New
279*47e946e7SWyllys Ingersoll  *		York and the intellectual property laws of the United
280*47e946e7SWyllys Ingersoll  *		States of America. No party to this Agreement will bring a
281*47e946e7SWyllys Ingersoll  *		legal action under this Agreement more than one year after
282*47e946e7SWyllys Ingersoll  *		the cause of action arose. Each party waives its rights to
283*47e946e7SWyllys Ingersoll  *		a jury trial in any resulting litigation.
284*47e946e7SWyllys Ingersoll  *
285*47e946e7SWyllys Ingersoll  *
286*47e946e7SWyllys Ingersoll  *
287*47e946e7SWyllys Ingersoll  * (C) COPYRIGHT International Business Machines Corp. 2001, 2002
288*47e946e7SWyllys Ingersoll  */
289*47e946e7SWyllys Ingersoll /*
290*47e946e7SWyllys Ingersoll  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
291*47e946e7SWyllys Ingersoll  * Use is subject to license terms.
292*47e946e7SWyllys Ingersoll  */
293*47e946e7SWyllys Ingersoll 
294*47e946e7SWyllys Ingersoll #include "tpmtok_int.h"
295*47e946e7SWyllys Ingersoll 
296*47e946e7SWyllys Ingersoll // object_create()
297*47e946e7SWyllys Ingersoll //
298*47e946e7SWyllys Ingersoll // Creates an object with the specified attributes.  Verifies that all required
299*47e946e7SWyllys Ingersoll // attributes are present and adds any missing attributes that have Cryptoki
300*47e946e7SWyllys Ingersoll // defined default values.  This routine does not check whether the session is
301*47e946e7SWyllys Ingersoll // authorized to create the object.  That is done elsewhere
302*47e946e7SWyllys Ingersoll // (see object_mgr_create())
303*47e946e7SWyllys Ingersoll CK_RV
304*47e946e7SWyllys Ingersoll object_create(CK_ATTRIBUTE  * pTemplate,
305*47e946e7SWyllys Ingersoll 	CK_ULONG	ulCount,
306*47e946e7SWyllys Ingersoll 	OBJECT	** obj)
307*47e946e7SWyllys Ingersoll {
308*47e946e7SWyllys Ingersoll 	OBJECT	* o	   = NULL;
309*47e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * attr	= NULL;
310*47e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * sensitive   = NULL;
311*47e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * extractable = NULL;
312*47e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * local	= NULL;
313*47e946e7SWyllys Ingersoll 	CK_BBOOL	class_given = FALSE;
314*47e946e7SWyllys Ingersoll 	CK_BBOOL	subclass_given = FALSE;
315*47e946e7SWyllys Ingersoll 	CK_BBOOL	flag;
316*47e946e7SWyllys Ingersoll 	CK_ULONG	class = 0xFFFFFFFF, subclass = 0xFFFFFFFF;
317*47e946e7SWyllys Ingersoll 	CK_RV	   rc;
318*47e946e7SWyllys Ingersoll 	unsigned int    i;
319*47e946e7SWyllys Ingersoll 
320*47e946e7SWyllys Ingersoll 	if (! pTemplate) {
321*47e946e7SWyllys Ingersoll 		return (CKR_FUNCTION_FAILED);
322*47e946e7SWyllys Ingersoll 	}
323*47e946e7SWyllys Ingersoll 	// extract the object class and subclass
324*47e946e7SWyllys Ingersoll 	//
325*47e946e7SWyllys Ingersoll 	attr = pTemplate;
326*47e946e7SWyllys Ingersoll 	for (i = 0; i < ulCount; i++, attr++) {
327*47e946e7SWyllys Ingersoll 		if (attr->type == CKA_CLASS) {
328*47e946e7SWyllys Ingersoll 			class = *(CK_OBJECT_CLASS *)attr->pValue;
329*47e946e7SWyllys Ingersoll 			class_given = TRUE;
330*47e946e7SWyllys Ingersoll 		}
331*47e946e7SWyllys Ingersoll 
332*47e946e7SWyllys Ingersoll 		if (attr->type == CKA_CERTIFICATE_TYPE) {
333*47e946e7SWyllys Ingersoll 			subclass = *(CK_CERTIFICATE_TYPE *)attr->pValue;
334*47e946e7SWyllys Ingersoll 			subclass_given = TRUE;
335*47e946e7SWyllys Ingersoll 		}
336*47e946e7SWyllys Ingersoll 
337*47e946e7SWyllys Ingersoll 		if (attr->type == CKA_KEY_TYPE) {
338*47e946e7SWyllys Ingersoll 			subclass = *(CK_KEY_TYPE *)attr->pValue;
339*47e946e7SWyllys Ingersoll 			subclass_given = TRUE;
340*47e946e7SWyllys Ingersoll 		}
341*47e946e7SWyllys Ingersoll 
342*47e946e7SWyllys Ingersoll 		if (attr->type == CKA_HW_FEATURE_TYPE) {
343*47e946e7SWyllys Ingersoll 			subclass = *(CK_HW_FEATURE_TYPE *)attr->pValue;
344*47e946e7SWyllys Ingersoll 			subclass_given = TRUE;
345*47e946e7SWyllys Ingersoll 		}
346*47e946e7SWyllys Ingersoll 	}
347*47e946e7SWyllys Ingersoll 
348*47e946e7SWyllys Ingersoll 	if (class_given == FALSE) {
349*47e946e7SWyllys Ingersoll 		return (CKR_TEMPLATE_INCOMPLETE);
350*47e946e7SWyllys Ingersoll 	}
351*47e946e7SWyllys Ingersoll 	if (class != CKO_DATA && subclass_given != TRUE) {
352*47e946e7SWyllys Ingersoll 		return (CKR_TEMPLATE_INCOMPLETE);
353*47e946e7SWyllys Ingersoll 	}
354*47e946e7SWyllys Ingersoll 
355*47e946e7SWyllys Ingersoll 	rc = object_create_skel(pTemplate, ulCount,
356*47e946e7SWyllys Ingersoll 	    MODE_CREATE, class, subclass, &o);
357*47e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
358*47e946e7SWyllys Ingersoll 		return (rc);
359*47e946e7SWyllys Ingersoll 	}
360*47e946e7SWyllys Ingersoll 	// for key objects, we need be careful...
361*47e946e7SWyllys Ingersoll 	//
362*47e946e7SWyllys Ingersoll 	// note:  I would think that keys loaded with C_CreateObject should
363*47e946e7SWyllys Ingersoll 	//	have their CKA_NEVER_EXTRACTABLE == FALSE and
364*47e946e7SWyllys Ingersoll 	//	CKA_ALWAYS_SENSITIVE == FALSE since the key data was presumably
365*47e946e7SWyllys Ingersoll 	//	stored in the clear prior to the call to C_CreateObject.  The
366*47e946e7SWyllys Ingersoll 	//	PKCS #11 spec doesn't impose this restriction however.
367*47e946e7SWyllys Ingersoll 	//
368*47e946e7SWyllys Ingersoll 	if (class == CKO_PRIVATE_KEY || class == CKO_SECRET_KEY) {
369*47e946e7SWyllys Ingersoll 		rc = template_attribute_find(o->template, CKA_SENSITIVE, &attr);
370*47e946e7SWyllys Ingersoll 		if (rc == FALSE) {
371*47e946e7SWyllys Ingersoll 			rc = CKR_FUNCTION_FAILED;
372*47e946e7SWyllys Ingersoll 			goto error;
373*47e946e7SWyllys Ingersoll 		}
374*47e946e7SWyllys Ingersoll 
375*47e946e7SWyllys Ingersoll 		flag = *(CK_BBOOL *)attr->pValue;
376*47e946e7SWyllys Ingersoll 
377*47e946e7SWyllys Ingersoll 		rc = build_attribute(CKA_ALWAYS_SENSITIVE, &flag,
378*47e946e7SWyllys Ingersoll 		    sizeof (CK_BYTE), &sensitive);
379*47e946e7SWyllys Ingersoll 		if (rc != CKR_OK) {
380*47e946e7SWyllys Ingersoll 			goto error;
381*47e946e7SWyllys Ingersoll 		}
382*47e946e7SWyllys Ingersoll 
383*47e946e7SWyllys Ingersoll 		rc = template_attribute_find(o->template, CKA_EXTRACTABLE,
384*47e946e7SWyllys Ingersoll 		    &attr);
385*47e946e7SWyllys Ingersoll 		if (rc == FALSE) {
386*47e946e7SWyllys Ingersoll 			rc = CKR_FUNCTION_FAILED;
387*47e946e7SWyllys Ingersoll 			goto error;
388*47e946e7SWyllys Ingersoll 		}
389*47e946e7SWyllys Ingersoll 
390*47e946e7SWyllys Ingersoll 		flag = *(CK_BBOOL *)attr->pValue;
391*47e946e7SWyllys Ingersoll 		flag = (~flag) & 0x1;
392*47e946e7SWyllys Ingersoll 
393*47e946e7SWyllys Ingersoll 		rc = build_attribute(CKA_NEVER_EXTRACTABLE, &flag,
394*47e946e7SWyllys Ingersoll 		    sizeof (CK_BYTE), &extractable);
395*47e946e7SWyllys Ingersoll 		if (rc != CKR_OK) {
396*47e946e7SWyllys Ingersoll 			goto error;
397*47e946e7SWyllys Ingersoll 		}
398*47e946e7SWyllys Ingersoll 		(void) template_update_attribute(o->template, sensitive);
399*47e946e7SWyllys Ingersoll 		(void) template_update_attribute(o->template, extractable);
400*47e946e7SWyllys Ingersoll 	}
401*47e946e7SWyllys Ingersoll 
402*47e946e7SWyllys Ingersoll 	*obj = o;
403*47e946e7SWyllys Ingersoll 
404*47e946e7SWyllys Ingersoll 	return (CKR_OK);
405*47e946e7SWyllys Ingersoll error:
406*47e946e7SWyllys Ingersoll 	if (sensitive)    free(sensitive);
407*47e946e7SWyllys Ingersoll 	if (extractable)  free(extractable);
408*47e946e7SWyllys Ingersoll 	if (local)	free(local);
409*47e946e7SWyllys Ingersoll 
410*47e946e7SWyllys Ingersoll 	(void) object_free(o);
411*47e946e7SWyllys Ingersoll 	return (rc);
412*47e946e7SWyllys Ingersoll }
413*47e946e7SWyllys Ingersoll 
414*47e946e7SWyllys Ingersoll // object_copy()
415*47e946e7SWyllys Ingersoll //
416*47e946e7SWyllys Ingersoll // Builds a copy of the specified object.  The new object gets the original
417*47e946e7SWyllys Ingersoll // object's attribute template plus any additional attributes that are
418*47e946e7SWyllys Ingersoll // specified.   Verifies that all required attributes are present.  This
419*47e946e7SWyllys Ingersoll // routine does not check whether the session is authorized to copy the
420*47e946e7SWyllys Ingersoll // object -- routines at the individual object level don't have the
421*47e946e7SWyllys Ingersoll // concept of "session".  These checks are done by the object manager.
422*47e946e7SWyllys Ingersoll //
423*47e946e7SWyllys Ingersoll CK_RV
424*47e946e7SWyllys Ingersoll object_copy(CK_ATTRIBUTE  * pTemplate,
425*47e946e7SWyllys Ingersoll 	CK_ULONG	ulCount,
426*47e946e7SWyllys Ingersoll 	OBJECT	* old_obj,
427*47e946e7SWyllys Ingersoll 	OBJECT	** new_obj)
428*47e946e7SWyllys Ingersoll {
429*47e946e7SWyllys Ingersoll 	TEMPLATE  * tmpl	= NULL;
430*47e946e7SWyllys Ingersoll 	TEMPLATE  * new_tmpl = NULL;
431*47e946e7SWyllys Ingersoll 	OBJECT    * o	= NULL;
432*47e946e7SWyllys Ingersoll 	CK_BBOOL    found;
433*47e946e7SWyllys Ingersoll 	CK_ULONG    class, subclass;
434*47e946e7SWyllys Ingersoll 	CK_RV	rc;
435*47e946e7SWyllys Ingersoll 
436*47e946e7SWyllys Ingersoll 
437*47e946e7SWyllys Ingersoll 	if (! old_obj || ! pTemplate || ! new_obj) {
438*47e946e7SWyllys Ingersoll 		return (CKR_FUNCTION_FAILED);
439*47e946e7SWyllys Ingersoll 	}
440*47e946e7SWyllys Ingersoll 	o	= (OBJECT   *)malloc(sizeof (OBJECT));
441*47e946e7SWyllys Ingersoll 	tmpl	= (TEMPLATE *)malloc(sizeof (TEMPLATE));
442*47e946e7SWyllys Ingersoll 	new_tmpl = (TEMPLATE *)malloc(sizeof (TEMPLATE));
443*47e946e7SWyllys Ingersoll 
444*47e946e7SWyllys Ingersoll 	if (! o || ! tmpl || ! new_tmpl) {
445*47e946e7SWyllys Ingersoll 		rc = CKR_HOST_MEMORY;
446*47e946e7SWyllys Ingersoll 		goto error;
447*47e946e7SWyllys Ingersoll 	}
448*47e946e7SWyllys Ingersoll 
449*47e946e7SWyllys Ingersoll 	(void) memset(o, 0x0, sizeof (OBJECT));
450*47e946e7SWyllys Ingersoll 	(void) memset(tmpl,	0x0, sizeof (TEMPLATE));
451*47e946e7SWyllys Ingersoll 	(void) memset(new_tmpl, 0x0, sizeof (TEMPLATE));
452*47e946e7SWyllys Ingersoll 
453*47e946e7SWyllys Ingersoll 	// copy the original object's attribute template
454*47e946e7SWyllys Ingersoll 	//
455*47e946e7SWyllys Ingersoll 	rc = template_copy(tmpl, old_obj->template);
456*47e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
457*47e946e7SWyllys Ingersoll 		goto error;
458*47e946e7SWyllys Ingersoll 	}
459*47e946e7SWyllys Ingersoll 	rc = template_add_attributes(new_tmpl, pTemplate, ulCount);
460*47e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
461*47e946e7SWyllys Ingersoll 		goto error;
462*47e946e7SWyllys Ingersoll 	}
463*47e946e7SWyllys Ingersoll 	// at this point, the new object has the list of attributes.  we need
464*47e946e7SWyllys Ingersoll 	// to do some more checking now:
465*47e946e7SWyllys Ingersoll 	//    1) invalid attribute values
466*47e946e7SWyllys Ingersoll 	//    2) missing required attributes
467*47e946e7SWyllys Ingersoll 	//    3) attributes inappropriate for the object class
468*47e946e7SWyllys Ingersoll 	//    4) conflicting attributes/values
469*47e946e7SWyllys Ingersoll 	//
470*47e946e7SWyllys Ingersoll 
471*47e946e7SWyllys Ingersoll 	found = template_get_class(tmpl, &class, &subclass);
472*47e946e7SWyllys Ingersoll 	if (found == FALSE) {
473*47e946e7SWyllys Ingersoll 		rc = CKR_TEMPLATE_INCONSISTENT;
474*47e946e7SWyllys Ingersoll 		goto error;
475*47e946e7SWyllys Ingersoll 	}
476*47e946e7SWyllys Ingersoll 
477*47e946e7SWyllys Ingersoll 	// The user cannot change object classes so we assume the existing
478*47e946e7SWyllys Ingersoll 	// object attributes are valid.  we still need to check the new
479*47e946e7SWyllys Ingersoll 	// attributes.   We cannot merge the new attributes in with the
480*47e946e7SWyllys Ingersoll 	// old ones and then check for validity because some attributes
481*47e946e7SWyllys Ingersoll 	// are added internally and are not allowed to be specified by
482*47e946e7SWyllys Ingersoll 	// the user (ie. CKA_LOCAL for key types) but may still be part
483*47e946e7SWyllys Ingersoll 	// of the old template.
484*47e946e7SWyllys Ingersoll 	rc = template_validate_attributes(new_tmpl, class, subclass,
485*47e946e7SWyllys Ingersoll 	    MODE_COPY);
486*47e946e7SWyllys Ingersoll 	if (rc != CKR_OK)
487*47e946e7SWyllys Ingersoll 		goto error;
488*47e946e7SWyllys Ingersoll 
489*47e946e7SWyllys Ingersoll 	rc = template_merge(tmpl, &new_tmpl);
490*47e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
491*47e946e7SWyllys Ingersoll 		goto error;
492*47e946e7SWyllys Ingersoll 	}
493*47e946e7SWyllys Ingersoll 
494*47e946e7SWyllys Ingersoll 	rc = template_check_required_attributes(tmpl, class,
495*47e946e7SWyllys Ingersoll 	    subclass, MODE_COPY);
496*47e946e7SWyllys Ingersoll 	if (rc != CKR_OK)
497*47e946e7SWyllys Ingersoll 		goto error;
498*47e946e7SWyllys Ingersoll 
499*47e946e7SWyllys Ingersoll 	o->template = tmpl;
500*47e946e7SWyllys Ingersoll 	*new_obj = o;
501*47e946e7SWyllys Ingersoll 
502*47e946e7SWyllys Ingersoll 	return (CKR_OK);
503*47e946e7SWyllys Ingersoll error:
504*47e946e7SWyllys Ingersoll 	if (tmpl)
505*47e946e7SWyllys Ingersoll 		(void) template_free(tmpl);
506*47e946e7SWyllys Ingersoll 	if (new_tmpl)
507*47e946e7SWyllys Ingersoll 		(void) template_free(new_tmpl);
508*47e946e7SWyllys Ingersoll 	if (o)
509*47e946e7SWyllys Ingersoll 		(void) object_free(o);
510*47e946e7SWyllys Ingersoll 
511*47e946e7SWyllys Ingersoll 	return (rc);
512*47e946e7SWyllys Ingersoll }
513*47e946e7SWyllys Ingersoll 
514*47e946e7SWyllys Ingersoll CK_RV
515*47e946e7SWyllys Ingersoll object_flatten(OBJECT    * obj,
516*47e946e7SWyllys Ingersoll 	CK_BYTE  ** data,
517*47e946e7SWyllys Ingersoll 	CK_ULONG  * len)
518*47e946e7SWyllys Ingersoll {
519*47e946e7SWyllys Ingersoll 	CK_BYTE    * buf = NULL;
520*47e946e7SWyllys Ingersoll 	CK_ULONG	tmpl_len, total_len;
521*47e946e7SWyllys Ingersoll 	CK_ULONG	offset;
522*47e946e7SWyllys Ingersoll 	CK_ULONG_32	count;
523*47e946e7SWyllys Ingersoll 	long	 rc;
524*47e946e7SWyllys Ingersoll 
525*47e946e7SWyllys Ingersoll 	if (! obj) {
526*47e946e7SWyllys Ingersoll 		return (CKR_FUNCTION_FAILED);
527*47e946e7SWyllys Ingersoll 	}
528*47e946e7SWyllys Ingersoll 	count    = template_get_count(obj->template);
529*47e946e7SWyllys Ingersoll 	tmpl_len = template_get_compressed_size(obj->template);
530*47e946e7SWyllys Ingersoll 
531*47e946e7SWyllys Ingersoll 	total_len = tmpl_len + sizeof (CK_OBJECT_CLASS_32) +
532*47e946e7SWyllys Ingersoll 	    sizeof (CK_ULONG_32) + 8;
533*47e946e7SWyllys Ingersoll 
534*47e946e7SWyllys Ingersoll 	buf = (CK_BYTE *)malloc(total_len);
535*47e946e7SWyllys Ingersoll 	if (!buf) {
536*47e946e7SWyllys Ingersoll 		return (CKR_HOST_MEMORY);
537*47e946e7SWyllys Ingersoll 	}
538*47e946e7SWyllys Ingersoll 
539*47e946e7SWyllys Ingersoll 	(void) memset((CK_BYTE *)buf, 0x0, total_len);
540*47e946e7SWyllys Ingersoll 
541*47e946e7SWyllys Ingersoll 	offset = 0;
542*47e946e7SWyllys Ingersoll 
543*47e946e7SWyllys Ingersoll 	(void) memcpy(buf + offset, &obj->class, sizeof (CK_OBJECT_CLASS_32));
544*47e946e7SWyllys Ingersoll 	offset += sizeof (CK_OBJECT_CLASS_32);
545*47e946e7SWyllys Ingersoll 
546*47e946e7SWyllys Ingersoll 	(void) memcpy(buf + offset, &count, sizeof (CK_ULONG_32));
547*47e946e7SWyllys Ingersoll 	offset += sizeof (CK_ULONG_32);
548*47e946e7SWyllys Ingersoll 
549*47e946e7SWyllys Ingersoll 	(void) memcpy(buf + offset, &obj->name,  sizeof (CK_BYTE) * 8);
550*47e946e7SWyllys Ingersoll 	offset += 8;
551*47e946e7SWyllys Ingersoll 	rc = template_flatten(obj->template, buf + offset);
552*47e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
553*47e946e7SWyllys Ingersoll 		free(buf);
554*47e946e7SWyllys Ingersoll 		return (rc);
555*47e946e7SWyllys Ingersoll 	}
556*47e946e7SWyllys Ingersoll 
557*47e946e7SWyllys Ingersoll 	*data = buf;
558*47e946e7SWyllys Ingersoll 	*len  = total_len;
559*47e946e7SWyllys Ingersoll 
560*47e946e7SWyllys Ingersoll 	return (CKR_OK);
561*47e946e7SWyllys Ingersoll }
562*47e946e7SWyllys Ingersoll 
563*47e946e7SWyllys Ingersoll 
564*47e946e7SWyllys Ingersoll 
565*47e946e7SWyllys Ingersoll // object_free()
566*47e946e7SWyllys Ingersoll //
567*47e946e7SWyllys Ingersoll // does what it says...
568*47e946e7SWyllys Ingersoll //
569*47e946e7SWyllys Ingersoll CK_BBOOL
570*47e946e7SWyllys Ingersoll object_free(OBJECT *obj)
571*47e946e7SWyllys Ingersoll {
572*47e946e7SWyllys Ingersoll 	(void) template_free(obj->template);
573*47e946e7SWyllys Ingersoll 	free(obj);
574*47e946e7SWyllys Ingersoll 
575*47e946e7SWyllys Ingersoll 	return (TRUE);
576*47e946e7SWyllys Ingersoll }
577*47e946e7SWyllys Ingersoll 
578*47e946e7SWyllys Ingersoll 
579*47e946e7SWyllys Ingersoll // object_is_modifiable()
580*47e946e7SWyllys Ingersoll //
581*47e946e7SWyllys Ingersoll CK_BBOOL
582*47e946e7SWyllys Ingersoll object_is_modifiable(OBJECT *obj)
583*47e946e7SWyllys Ingersoll {
584*47e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * attr = NULL;
585*47e946e7SWyllys Ingersoll 	CK_BBOOL	modifiable;
586*47e946e7SWyllys Ingersoll 	CK_BBOOL	found;
587*47e946e7SWyllys Ingersoll 
588*47e946e7SWyllys Ingersoll 	found = template_attribute_find(obj->template, CKA_MODIFIABLE, &attr);
589*47e946e7SWyllys Ingersoll 	if (found == FALSE)
590*47e946e7SWyllys Ingersoll 		return (TRUE);
591*47e946e7SWyllys Ingersoll 
592*47e946e7SWyllys Ingersoll 	modifiable = *(CK_BBOOL *)attr->pValue;
593*47e946e7SWyllys Ingersoll 
594*47e946e7SWyllys Ingersoll 	return (modifiable);
595*47e946e7SWyllys Ingersoll }
596*47e946e7SWyllys Ingersoll 
597*47e946e7SWyllys Ingersoll 
598*47e946e7SWyllys Ingersoll // object_is_private()
599*47e946e7SWyllys Ingersoll //
600*47e946e7SWyllys Ingersoll // an is_private member should probably be added to OBJECT
601*47e946e7SWyllys Ingersoll //
602*47e946e7SWyllys Ingersoll CK_BBOOL
603*47e946e7SWyllys Ingersoll object_is_private(OBJECT *obj) {
604*47e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * attr = NULL;
605*47e946e7SWyllys Ingersoll 	CK_BBOOL	priv;
606*47e946e7SWyllys Ingersoll 	CK_BBOOL	found;
607*47e946e7SWyllys Ingersoll 
608*47e946e7SWyllys Ingersoll 	found = template_attribute_find(obj->template, CKA_PRIVATE, &attr);
609*47e946e7SWyllys Ingersoll 	if (found == FALSE)
610*47e946e7SWyllys Ingersoll 		return (TRUE);
611*47e946e7SWyllys Ingersoll 
612*47e946e7SWyllys Ingersoll 	if (attr == NULL)
613*47e946e7SWyllys Ingersoll 		return (TRUE);
614*47e946e7SWyllys Ingersoll 
615*47e946e7SWyllys Ingersoll 	priv = *((CK_BBOOL *)attr->pValue);
616*47e946e7SWyllys Ingersoll 
617*47e946e7SWyllys Ingersoll 	return (priv);
618*47e946e7SWyllys Ingersoll }
619*47e946e7SWyllys Ingersoll 
620*47e946e7SWyllys Ingersoll CK_BBOOL
621*47e946e7SWyllys Ingersoll object_is_public(OBJECT *obj) {
622*47e946e7SWyllys Ingersoll 	CK_BBOOL rc;
623*47e946e7SWyllys Ingersoll 
624*47e946e7SWyllys Ingersoll 	rc = object_is_private(obj);
625*47e946e7SWyllys Ingersoll 
626*47e946e7SWyllys Ingersoll 	if (rc)
627*47e946e7SWyllys Ingersoll 		return (FALSE);
628*47e946e7SWyllys Ingersoll 	return (TRUE);
629*47e946e7SWyllys Ingersoll }
630*47e946e7SWyllys Ingersoll 
631*47e946e7SWyllys Ingersoll CK_BBOOL
632*47e946e7SWyllys Ingersoll object_is_token_object(OBJECT *obj) {
633*47e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * attr = NULL;
634*47e946e7SWyllys Ingersoll 	CK_BBOOL	is_token;
635*47e946e7SWyllys Ingersoll 	CK_BBOOL	found;
636*47e946e7SWyllys Ingersoll 
637*47e946e7SWyllys Ingersoll 	found = template_attribute_find(obj->template,
638*47e946e7SWyllys Ingersoll 	    CKA_TOKEN, &attr);
639*47e946e7SWyllys Ingersoll 	if (found == FALSE)
640*47e946e7SWyllys Ingersoll 		return (FALSE);
641*47e946e7SWyllys Ingersoll 	is_token = *(CK_BBOOL *)attr->pValue;
642*47e946e7SWyllys Ingersoll 	return (is_token);
643*47e946e7SWyllys Ingersoll }
644*47e946e7SWyllys Ingersoll 
645*47e946e7SWyllys Ingersoll CK_BBOOL
646*47e946e7SWyllys Ingersoll object_is_session_object(OBJECT *obj) {
647*47e946e7SWyllys Ingersoll 	CK_BBOOL rc;
648*47e946e7SWyllys Ingersoll 
649*47e946e7SWyllys Ingersoll 	rc = object_is_token_object(obj);
650*47e946e7SWyllys Ingersoll 
651*47e946e7SWyllys Ingersoll 	if (rc)
652*47e946e7SWyllys Ingersoll 		return (FALSE);
653*47e946e7SWyllys Ingersoll 	else
654*47e946e7SWyllys Ingersoll 		return (TRUE);
655*47e946e7SWyllys Ingersoll }
656*47e946e7SWyllys Ingersoll 
657*47e946e7SWyllys Ingersoll CK_ULONG
658*47e946e7SWyllys Ingersoll object_get_size(OBJECT *obj) {
659*47e946e7SWyllys Ingersoll 	CK_ULONG  size;
660*47e946e7SWyllys Ingersoll 
661*47e946e7SWyllys Ingersoll 	size = sizeof (OBJECT) + template_get_size(obj->template);
662*47e946e7SWyllys Ingersoll 
663*47e946e7SWyllys Ingersoll 	return (size);
664*47e946e7SWyllys Ingersoll }
665*47e946e7SWyllys Ingersoll 
666*47e946e7SWyllys Ingersoll CK_RV
667*47e946e7SWyllys Ingersoll object_get_attribute_values(OBJECT	* obj,
668*47e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * pTemplate,
669*47e946e7SWyllys Ingersoll 	CK_ULONG	ulCount)
670*47e946e7SWyllys Ingersoll {
671*47e946e7SWyllys Ingersoll 	TEMPLATE	  *obj_tmpl = NULL;
672*47e946e7SWyllys Ingersoll 	CK_ATTRIBUTE	*attr	= NULL;
673*47e946e7SWyllys Ingersoll 	CK_ULONG	   i;
674*47e946e7SWyllys Ingersoll 	CK_BBOOL	   flag;
675*47e946e7SWyllys Ingersoll 	CK_RV		rc;
676*47e946e7SWyllys Ingersoll 
677*47e946e7SWyllys Ingersoll 	rc = CKR_OK;
678*47e946e7SWyllys Ingersoll 
679*47e946e7SWyllys Ingersoll 	obj_tmpl = obj->template;
680*47e946e7SWyllys Ingersoll 
681*47e946e7SWyllys Ingersoll 	for (i = 0; i < ulCount; i++) {
682*47e946e7SWyllys Ingersoll 		flag = template_check_exportability(obj_tmpl,
683*47e946e7SWyllys Ingersoll 		    pTemplate[i].type);
684*47e946e7SWyllys Ingersoll 		if (flag == FALSE) {
685*47e946e7SWyllys Ingersoll 			rc = CKR_ATTRIBUTE_SENSITIVE;
686*47e946e7SWyllys Ingersoll 			pTemplate[i].ulValueLen = (CK_ULONG) - 1;
687*47e946e7SWyllys Ingersoll 			continue;
688*47e946e7SWyllys Ingersoll 		}
689*47e946e7SWyllys Ingersoll 
690*47e946e7SWyllys Ingersoll 		flag = template_attribute_find(obj_tmpl,
691*47e946e7SWyllys Ingersoll 		    pTemplate[i].type, &attr);
692*47e946e7SWyllys Ingersoll 		if (flag == FALSE) {
693*47e946e7SWyllys Ingersoll 			rc = CKR_ATTRIBUTE_TYPE_INVALID;
694*47e946e7SWyllys Ingersoll 			pTemplate[i].ulValueLen = (CK_ULONG) - 1;
695*47e946e7SWyllys Ingersoll 			continue;
696*47e946e7SWyllys Ingersoll 		}
697*47e946e7SWyllys Ingersoll 
698*47e946e7SWyllys Ingersoll 		if (pTemplate[i].pValue == NULL) {
699*47e946e7SWyllys Ingersoll 			pTemplate[i].ulValueLen = attr->ulValueLen;
700*47e946e7SWyllys Ingersoll 		} else if (pTemplate[i].ulValueLen >= attr->ulValueLen) {
701*47e946e7SWyllys Ingersoll 			(void) memcpy(pTemplate[i].pValue, attr->pValue,
702*47e946e7SWyllys Ingersoll 			    attr->ulValueLen);
703*47e946e7SWyllys Ingersoll 			pTemplate[i].ulValueLen = attr->ulValueLen;
704*47e946e7SWyllys Ingersoll 		} else {
705*47e946e7SWyllys Ingersoll 			rc = CKR_BUFFER_TOO_SMALL;
706*47e946e7SWyllys Ingersoll 			pTemplate[i].ulValueLen = (CK_ULONG) - 1;
707*47e946e7SWyllys Ingersoll 		}
708*47e946e7SWyllys Ingersoll 	}
709*47e946e7SWyllys Ingersoll 
710*47e946e7SWyllys Ingersoll 	return (rc);
711*47e946e7SWyllys Ingersoll }
712*47e946e7SWyllys Ingersoll 
713*47e946e7SWyllys Ingersoll CK_RV
714*47e946e7SWyllys Ingersoll object_set_attribute_values(OBJECT	* obj,
715*47e946e7SWyllys Ingersoll 	CK_ATTRIBUTE  * pTemplate,
716*47e946e7SWyllys Ingersoll 	CK_ULONG	ulCount)
717*47e946e7SWyllys Ingersoll {
718*47e946e7SWyllys Ingersoll 	TEMPLATE * new_tmpl;
719*47e946e7SWyllys Ingersoll 	CK_BBOOL   found;
720*47e946e7SWyllys Ingersoll 	CK_ULONG   class, subclass;
721*47e946e7SWyllys Ingersoll 	CK_RV	rc;
722*47e946e7SWyllys Ingersoll 
723*47e946e7SWyllys Ingersoll 	if (! obj || ! pTemplate) {
724*47e946e7SWyllys Ingersoll 		return (CKR_FUNCTION_FAILED);
725*47e946e7SWyllys Ingersoll 	}
726*47e946e7SWyllys Ingersoll 
727*47e946e7SWyllys Ingersoll 	found = template_get_class(obj->template, &class, &subclass);
728*47e946e7SWyllys Ingersoll 	if (found == FALSE) {
729*47e946e7SWyllys Ingersoll 		rc = CKR_FUNCTION_FAILED;
730*47e946e7SWyllys Ingersoll 		goto error;
731*47e946e7SWyllys Ingersoll 	}
732*47e946e7SWyllys Ingersoll 
733*47e946e7SWyllys Ingersoll 	new_tmpl = (TEMPLATE *)malloc(sizeof (TEMPLATE));
734*47e946e7SWyllys Ingersoll 	if (! new_tmpl) {
735*47e946e7SWyllys Ingersoll 		return (CKR_HOST_MEMORY);
736*47e946e7SWyllys Ingersoll 	}
737*47e946e7SWyllys Ingersoll 	(void) memset(new_tmpl, 0x0, sizeof (TEMPLATE));
738*47e946e7SWyllys Ingersoll 
739*47e946e7SWyllys Ingersoll 	rc = template_add_attributes(new_tmpl, pTemplate, ulCount);
740*47e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
741*47e946e7SWyllys Ingersoll 		goto error;
742*47e946e7SWyllys Ingersoll 	}
743*47e946e7SWyllys Ingersoll 
744*47e946e7SWyllys Ingersoll 	rc = template_validate_attributes(new_tmpl, class,
745*47e946e7SWyllys Ingersoll 	    subclass, MODE_MODIFY);
746*47e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
747*47e946e7SWyllys Ingersoll 		goto error;
748*47e946e7SWyllys Ingersoll 	}
749*47e946e7SWyllys Ingersoll 
750*47e946e7SWyllys Ingersoll 	rc = template_merge(obj->template, &new_tmpl);
751*47e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
752*47e946e7SWyllys Ingersoll 		return (rc);
753*47e946e7SWyllys Ingersoll 	}
754*47e946e7SWyllys Ingersoll 	return (CKR_OK);
755*47e946e7SWyllys Ingersoll error:
756*47e946e7SWyllys Ingersoll 	if (new_tmpl)
757*47e946e7SWyllys Ingersoll 		(void) template_free(new_tmpl);
758*47e946e7SWyllys Ingersoll 	return (rc);
759*47e946e7SWyllys Ingersoll }
760*47e946e7SWyllys Ingersoll 
761*47e946e7SWyllys Ingersoll CK_RV
762*47e946e7SWyllys Ingersoll object_restore(CK_BYTE *data, OBJECT **new_obj, CK_BBOOL replace) {
763*47e946e7SWyllys Ingersoll 	TEMPLATE  * tmpl = NULL;
764*47e946e7SWyllys Ingersoll 	OBJECT    * obj  = NULL;
765*47e946e7SWyllys Ingersoll 	CK_ULONG    offset = 0;
766*47e946e7SWyllys Ingersoll 	CK_ULONG_32    count = 0;
767*47e946e7SWyllys Ingersoll 	CK_RV	rc;
768*47e946e7SWyllys Ingersoll 
769*47e946e7SWyllys Ingersoll 	if (! data || ! new_obj) {
770*47e946e7SWyllys Ingersoll 		return (CKR_FUNCTION_FAILED);
771*47e946e7SWyllys Ingersoll 	}
772*47e946e7SWyllys Ingersoll 	obj = (OBJECT *)malloc(sizeof (OBJECT));
773*47e946e7SWyllys Ingersoll 	if (! obj) {
774*47e946e7SWyllys Ingersoll 		rc = CKR_HOST_MEMORY;
775*47e946e7SWyllys Ingersoll 		goto error;
776*47e946e7SWyllys Ingersoll 	}
777*47e946e7SWyllys Ingersoll 
778*47e946e7SWyllys Ingersoll 
779*47e946e7SWyllys Ingersoll 	(void) memset(obj, 0x0, sizeof (OBJECT));
780*47e946e7SWyllys Ingersoll 
781*47e946e7SWyllys Ingersoll 	(void) memcpy(&obj->class, data + offset, sizeof (CK_OBJECT_CLASS_32));
782*47e946e7SWyllys Ingersoll 	offset += sizeof (CK_OBJECT_CLASS_32);
783*47e946e7SWyllys Ingersoll 
784*47e946e7SWyllys Ingersoll 	(void) memcpy(&count, data + offset, sizeof (CK_ULONG_32));
785*47e946e7SWyllys Ingersoll 	offset += sizeof (CK_ULONG_32);
786*47e946e7SWyllys Ingersoll 
787*47e946e7SWyllys Ingersoll 
788*47e946e7SWyllys Ingersoll 	(void) memcpy(&obj->name, data + offset, 8);
789*47e946e7SWyllys Ingersoll 	offset += 8;
790*47e946e7SWyllys Ingersoll 
791*47e946e7SWyllys Ingersoll 	rc = template_unflatten(&tmpl, data + offset, count);
792*47e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
793*47e946e7SWyllys Ingersoll 		goto error;
794*47e946e7SWyllys Ingersoll 	}
795*47e946e7SWyllys Ingersoll 	obj->template = tmpl;
796*47e946e7SWyllys Ingersoll 
797*47e946e7SWyllys Ingersoll 	if (replace == FALSE) {
798*47e946e7SWyllys Ingersoll 		 *new_obj = obj;
799*47e946e7SWyllys Ingersoll 	} else {
800*47e946e7SWyllys Ingersoll 		(void) template_free((*new_obj)->template);
801*47e946e7SWyllys Ingersoll 		(void) memcpy(*new_obj, obj, sizeof (OBJECT));
802*47e946e7SWyllys Ingersoll 
803*47e946e7SWyllys Ingersoll 		free(obj);
804*47e946e7SWyllys Ingersoll 	}
805*47e946e7SWyllys Ingersoll 
806*47e946e7SWyllys Ingersoll 	return (CKR_OK);
807*47e946e7SWyllys Ingersoll error:
808*47e946e7SWyllys Ingersoll 	if (obj)
809*47e946e7SWyllys Ingersoll 		(void) object_free(obj);
810*47e946e7SWyllys Ingersoll 	if (tmpl)
811*47e946e7SWyllys Ingersoll 		(void) template_free(tmpl);
812*47e946e7SWyllys Ingersoll 
813*47e946e7SWyllys Ingersoll 	return (rc);
814*47e946e7SWyllys Ingersoll }
815*47e946e7SWyllys Ingersoll 
816*47e946e7SWyllys Ingersoll 
817*47e946e7SWyllys Ingersoll //
818*47e946e7SWyllys Ingersoll //
819*47e946e7SWyllys Ingersoll CK_RV
820*47e946e7SWyllys Ingersoll object_create_skel(CK_ATTRIBUTE  * pTemplate,
821*47e946e7SWyllys Ingersoll 	CK_ULONG	ulCount,
822*47e946e7SWyllys Ingersoll 	CK_ULONG	mode,
823*47e946e7SWyllys Ingersoll 	CK_ULONG	class,
824*47e946e7SWyllys Ingersoll 	CK_ULONG	subclass,
825*47e946e7SWyllys Ingersoll 	OBJECT	** obj) {
826*47e946e7SWyllys Ingersoll 	TEMPLATE  * tmpl  = NULL;
827*47e946e7SWyllys Ingersoll 	TEMPLATE  * tmpl2 = NULL;
828*47e946e7SWyllys Ingersoll 	OBJECT   * o	= NULL;
829*47e946e7SWyllys Ingersoll 	CK_RV	rc;
830*47e946e7SWyllys Ingersoll 
831*47e946e7SWyllys Ingersoll 
832*47e946e7SWyllys Ingersoll 	if (! obj) {
833*47e946e7SWyllys Ingersoll 		return (CKR_FUNCTION_FAILED);
834*47e946e7SWyllys Ingersoll 	}
835*47e946e7SWyllys Ingersoll 	if (! pTemplate && (ulCount != 0)) {
836*47e946e7SWyllys Ingersoll 		return (CKR_FUNCTION_FAILED);
837*47e946e7SWyllys Ingersoll 	}
838*47e946e7SWyllys Ingersoll 	o	= (OBJECT *)malloc(sizeof (OBJECT));
839*47e946e7SWyllys Ingersoll 	tmpl  = (TEMPLATE *)malloc(sizeof (TEMPLATE));
840*47e946e7SWyllys Ingersoll 	tmpl2 = (TEMPLATE *)malloc(sizeof (TEMPLATE));
841*47e946e7SWyllys Ingersoll 
842*47e946e7SWyllys Ingersoll 	if (! o || ! tmpl || ! tmpl2) {
843*47e946e7SWyllys Ingersoll 		rc = CKR_HOST_MEMORY;
844*47e946e7SWyllys Ingersoll 		goto done;
845*47e946e7SWyllys Ingersoll 	}
846*47e946e7SWyllys Ingersoll 	(void) memset(o,	0x0, sizeof (OBJECT));
847*47e946e7SWyllys Ingersoll 	(void) memset(tmpl,  0x0, sizeof (TEMPLATE));
848*47e946e7SWyllys Ingersoll 	(void) memset(tmpl2, 0x0, sizeof (TEMPLATE));
849*47e946e7SWyllys Ingersoll 
850*47e946e7SWyllys Ingersoll 
851*47e946e7SWyllys Ingersoll 	rc = template_add_default_attributes(tmpl, class, subclass, mode);
852*47e946e7SWyllys Ingersoll 	if (rc != CKR_OK)
853*47e946e7SWyllys Ingersoll 		goto done;
854*47e946e7SWyllys Ingersoll 
855*47e946e7SWyllys Ingersoll 	rc = template_add_attributes(tmpl2, pTemplate, ulCount);
856*47e946e7SWyllys Ingersoll 	if (rc != CKR_OK)
857*47e946e7SWyllys Ingersoll 		goto done;
858*47e946e7SWyllys Ingersoll 
859*47e946e7SWyllys Ingersoll 
860*47e946e7SWyllys Ingersoll 	// at this point, the new template has the list of attributes.  we need
861*47e946e7SWyllys Ingersoll 	// to do some more checking now:
862*47e946e7SWyllys Ingersoll 	//    1) invalid attribute values
863*47e946e7SWyllys Ingersoll 	//    2) missing required attributes
864*47e946e7SWyllys Ingersoll 	//    3) attributes inappropriate for the object class
865*47e946e7SWyllys Ingersoll 	//    4) conflicting attributes/values
866*47e946e7SWyllys Ingersoll 	//
867*47e946e7SWyllys Ingersoll 
868*47e946e7SWyllys Ingersoll 	rc = template_validate_attributes(tmpl2, class, subclass, mode);
869*47e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
870*47e946e7SWyllys Ingersoll 		goto done;
871*47e946e7SWyllys Ingersoll 	}
872*47e946e7SWyllys Ingersoll 
873*47e946e7SWyllys Ingersoll 	rc = template_check_required_attributes(tmpl2, class, subclass, mode);
874*47e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
875*47e946e7SWyllys Ingersoll 		goto done;
876*47e946e7SWyllys Ingersoll 	}
877*47e946e7SWyllys Ingersoll 
878*47e946e7SWyllys Ingersoll 	rc = template_merge(tmpl, &tmpl2);
879*47e946e7SWyllys Ingersoll 	if (rc != CKR_OK) {
880*47e946e7SWyllys Ingersoll 		goto done;
881*47e946e7SWyllys Ingersoll 	}
882*47e946e7SWyllys Ingersoll 	// at this point, we should have a valid object with correct attributes
883*47e946e7SWyllys Ingersoll 	//
884*47e946e7SWyllys Ingersoll 	o->template = tmpl;
885*47e946e7SWyllys Ingersoll 	 *obj = o;
886*47e946e7SWyllys Ingersoll 
887*47e946e7SWyllys Ingersoll 	return (CKR_OK);
888*47e946e7SWyllys Ingersoll 	done:
889*47e946e7SWyllys Ingersoll 	if (o)	free(o);
890*47e946e7SWyllys Ingersoll 	if (tmpl)
891*47e946e7SWyllys Ingersoll 		(void) template_free(tmpl);
892*47e946e7SWyllys Ingersoll 	if (tmpl2)
893*47e946e7SWyllys Ingersoll 		(void) template_free(tmpl2);
894*47e946e7SWyllys Ingersoll 
895*47e946e7SWyllys Ingersoll 	return (rc);
896*47e946e7SWyllys Ingersoll }
897