xref: /onnv-gate/usr/src/lib/pkcs11/pkcs11_tpm/common/object.c (revision 9651:ca22f7d7086c)
19126SWyllys.Ingersoll@Sun.COM /*
29126SWyllys.Ingersoll@Sun.COM  *		Common Public License Version 0.5
39126SWyllys.Ingersoll@Sun.COM  *
49126SWyllys.Ingersoll@Sun.COM  *		THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF
59126SWyllys.Ingersoll@Sun.COM  *		THIS COMMON PUBLIC LICENSE ("AGREEMENT"). ANY USE,
69126SWyllys.Ingersoll@Sun.COM  *		REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
79126SWyllys.Ingersoll@Sun.COM  *		RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
89126SWyllys.Ingersoll@Sun.COM  *
99126SWyllys.Ingersoll@Sun.COM  *		1. DEFINITIONS
109126SWyllys.Ingersoll@Sun.COM  *
119126SWyllys.Ingersoll@Sun.COM  *		"Contribution" means:
129126SWyllys.Ingersoll@Sun.COM  *		      a) in the case of the initial Contributor, the
139126SWyllys.Ingersoll@Sun.COM  *		      initial code and documentation distributed under
149126SWyllys.Ingersoll@Sun.COM  *		      this Agreement, and
159126SWyllys.Ingersoll@Sun.COM  *
169126SWyllys.Ingersoll@Sun.COM  *		      b) in the case of each subsequent Contributor:
179126SWyllys.Ingersoll@Sun.COM  *		      i) changes to the Program, and
189126SWyllys.Ingersoll@Sun.COM  *		      ii) additions to the Program;
199126SWyllys.Ingersoll@Sun.COM  *
209126SWyllys.Ingersoll@Sun.COM  *		      where such changes and/or additions to the Program
219126SWyllys.Ingersoll@Sun.COM  *		      originate from and are distributed by that
229126SWyllys.Ingersoll@Sun.COM  *		      particular Contributor. A Contribution 'originates'
239126SWyllys.Ingersoll@Sun.COM  *		      from a Contributor if it was added to the Program
249126SWyllys.Ingersoll@Sun.COM  *		      by such Contributor itself or anyone acting on such
259126SWyllys.Ingersoll@Sun.COM  *		      Contributor's behalf. Contributions do not include
269126SWyllys.Ingersoll@Sun.COM  *		      additions to the Program which: (i) are separate
279126SWyllys.Ingersoll@Sun.COM  *		      modules of software distributed in conjunction with
289126SWyllys.Ingersoll@Sun.COM  *		      the Program under their own license agreement, and
299126SWyllys.Ingersoll@Sun.COM  *		      (ii) are not derivative works of the Program.
309126SWyllys.Ingersoll@Sun.COM  *
319126SWyllys.Ingersoll@Sun.COM  *
329126SWyllys.Ingersoll@Sun.COM  *		"Contributor" means any person or entity that distributes
339126SWyllys.Ingersoll@Sun.COM  *		the Program.
349126SWyllys.Ingersoll@Sun.COM  *
359126SWyllys.Ingersoll@Sun.COM  *		"Licensed Patents " mean patent claims licensable by a
369126SWyllys.Ingersoll@Sun.COM  *		Contributor which are necessarily infringed by the use or
379126SWyllys.Ingersoll@Sun.COM  *		sale of its Contribution alone or when combined with the
389126SWyllys.Ingersoll@Sun.COM  *		Program.
399126SWyllys.Ingersoll@Sun.COM  *
409126SWyllys.Ingersoll@Sun.COM  *		"Program" means the Contributions distributed in
419126SWyllys.Ingersoll@Sun.COM  *		accordance with this Agreement.
429126SWyllys.Ingersoll@Sun.COM  *
439126SWyllys.Ingersoll@Sun.COM  *		"Recipient" means anyone who receives the Program under
449126SWyllys.Ingersoll@Sun.COM  *		this Agreement, including all Contributors.
459126SWyllys.Ingersoll@Sun.COM  *
469126SWyllys.Ingersoll@Sun.COM  *		2. GRANT OF RIGHTS
479126SWyllys.Ingersoll@Sun.COM  *
489126SWyllys.Ingersoll@Sun.COM  *		      a) Subject to the terms of this Agreement, each
499126SWyllys.Ingersoll@Sun.COM  *		      Contributor hereby grants Recipient a
509126SWyllys.Ingersoll@Sun.COM  *		      no - exclusive, worldwide, royalt - free copyright
519126SWyllys.Ingersoll@Sun.COM  *		      license to reproduce, prepare derivative works of,
529126SWyllys.Ingersoll@Sun.COM  *		      publicly display, publicly perform, distribute and
539126SWyllys.Ingersoll@Sun.COM  *		      sublicense the Contribution of such Contributor, if
549126SWyllys.Ingersoll@Sun.COM  *		      any, and such derivative works, in source code and
559126SWyllys.Ingersoll@Sun.COM  *		      object code form.
569126SWyllys.Ingersoll@Sun.COM  *
579126SWyllys.Ingersoll@Sun.COM  *		      b) Subject to the terms of this Agreement, each
589126SWyllys.Ingersoll@Sun.COM  *		      Contributor hereby grants Recipient a
599126SWyllys.Ingersoll@Sun.COM  *		      no - exclusive, worldwide, royalt - free patent
609126SWyllys.Ingersoll@Sun.COM  *		      license under Licensed Patents to make, use, sell,
619126SWyllys.Ingersoll@Sun.COM  *		      offer to sell, import and otherwise transfer the
629126SWyllys.Ingersoll@Sun.COM  *		      Contribution of such Contributor, if any, in source
639126SWyllys.Ingersoll@Sun.COM  *		      code and object code form. This patent license
649126SWyllys.Ingersoll@Sun.COM  *		      shall apply to the combination of the Contribution
659126SWyllys.Ingersoll@Sun.COM  *		      and the Program if, at the time the Contribution is
669126SWyllys.Ingersoll@Sun.COM  *		      added by the Contributor, such addition of the
679126SWyllys.Ingersoll@Sun.COM  *		      Contribution causes such combination to be covered
689126SWyllys.Ingersoll@Sun.COM  *		      by the Licensed Patents. The patent license shall
699126SWyllys.Ingersoll@Sun.COM  *		      not apply to any other combinations which include
709126SWyllys.Ingersoll@Sun.COM  *		      the Contribution. No hardware per se is licensed
719126SWyllys.Ingersoll@Sun.COM  *		      hereunder.
729126SWyllys.Ingersoll@Sun.COM  *
739126SWyllys.Ingersoll@Sun.COM  *		      c) Recipient understands that although each
749126SWyllys.Ingersoll@Sun.COM  *		      Contributor grants the licenses to its
759126SWyllys.Ingersoll@Sun.COM  *		      Contributions set forth herein, no assurances are
769126SWyllys.Ingersoll@Sun.COM  *		      provided by any Contributor that the Program does
779126SWyllys.Ingersoll@Sun.COM  *		      not infringe the patent or other intellectual
789126SWyllys.Ingersoll@Sun.COM  *		      property rights of any other entity. Each
799126SWyllys.Ingersoll@Sun.COM  *		      Contributor disclaims any liability to Recipient
809126SWyllys.Ingersoll@Sun.COM  *		      for claims brought by any other entity based on
819126SWyllys.Ingersoll@Sun.COM  *		      infringement of intellectual property rights or
829126SWyllys.Ingersoll@Sun.COM  *		      otherwise. As a condition to exercising the rights
839126SWyllys.Ingersoll@Sun.COM  *		      and licenses granted hereunder, each Recipient
849126SWyllys.Ingersoll@Sun.COM  *		      hereby assumes sole responsibility to secure any
859126SWyllys.Ingersoll@Sun.COM  *		      other intellectual property rights needed, if any.
869126SWyllys.Ingersoll@Sun.COM  *
879126SWyllys.Ingersoll@Sun.COM  *		      For example, if a third party patent license is
889126SWyllys.Ingersoll@Sun.COM  *		      required to allow Recipient to distribute the
899126SWyllys.Ingersoll@Sun.COM  *		      Program, it is Recipient's responsibility to
909126SWyllys.Ingersoll@Sun.COM  *		      acquire that license before distributing the
919126SWyllys.Ingersoll@Sun.COM  *		      Program.
929126SWyllys.Ingersoll@Sun.COM  *
939126SWyllys.Ingersoll@Sun.COM  *		      d) Each Contributor represents that to its
949126SWyllys.Ingersoll@Sun.COM  *		      knowledge it has sufficient copyright rights in its
959126SWyllys.Ingersoll@Sun.COM  *		      Contribution, if any, to grant the copyright
969126SWyllys.Ingersoll@Sun.COM  *		      license set forth in this Agreement.
979126SWyllys.Ingersoll@Sun.COM  *
989126SWyllys.Ingersoll@Sun.COM  *		3. REQUIREMENTS
999126SWyllys.Ingersoll@Sun.COM  *
1009126SWyllys.Ingersoll@Sun.COM  *		A Contributor may choose to distribute the Program in
1019126SWyllys.Ingersoll@Sun.COM  *		object code form under its own license agreement, provided
1029126SWyllys.Ingersoll@Sun.COM  *		that:
1039126SWyllys.Ingersoll@Sun.COM  *		      a) it complies with the terms and conditions of
1049126SWyllys.Ingersoll@Sun.COM  *		      this Agreement; and
1059126SWyllys.Ingersoll@Sun.COM  *
1069126SWyllys.Ingersoll@Sun.COM  *		      b) its license agreement:
1079126SWyllys.Ingersoll@Sun.COM  *		      i) effectively disclaims on behalf of all
1089126SWyllys.Ingersoll@Sun.COM  *		      Contributors all warranties and conditions, express
1099126SWyllys.Ingersoll@Sun.COM  *		      and implied, including warranties or conditions of
1109126SWyllys.Ingersoll@Sun.COM  *		      title and no - infringement, and implied warranties
1119126SWyllys.Ingersoll@Sun.COM  *		      or conditions of merchantability and fitness for a
1129126SWyllys.Ingersoll@Sun.COM  *		      particular purpose;
1139126SWyllys.Ingersoll@Sun.COM  *
1149126SWyllys.Ingersoll@Sun.COM  *		      ii) effectively excludes on behalf of all
1159126SWyllys.Ingersoll@Sun.COM  *		      Contributors all liability for damages, including
1169126SWyllys.Ingersoll@Sun.COM  *		      direct, indirect, special, incidental and
1179126SWyllys.Ingersoll@Sun.COM  *		      consequential damages, such as lost profits;
1189126SWyllys.Ingersoll@Sun.COM  *
1199126SWyllys.Ingersoll@Sun.COM  *		      iii) states that any provisions which differ from
1209126SWyllys.Ingersoll@Sun.COM  *		      this Agreement are offered by that Contributor
1219126SWyllys.Ingersoll@Sun.COM  *		      alone and not by any other party; and
1229126SWyllys.Ingersoll@Sun.COM  *
1239126SWyllys.Ingersoll@Sun.COM  *		      iv) states that source code for the Program is
1249126SWyllys.Ingersoll@Sun.COM  *		      available from such Contributor, and informs
1259126SWyllys.Ingersoll@Sun.COM  *		      licensees how to obtain it in a reasonable manner
1269126SWyllys.Ingersoll@Sun.COM  *		      on or through a medium customarily used for
1279126SWyllys.Ingersoll@Sun.COM  *		      software exchange.
1289126SWyllys.Ingersoll@Sun.COM  *
1299126SWyllys.Ingersoll@Sun.COM  *		When the Program is made available in source code form:
1309126SWyllys.Ingersoll@Sun.COM  *		      a) it must be made available under this Agreement;
1319126SWyllys.Ingersoll@Sun.COM  *		      and
1329126SWyllys.Ingersoll@Sun.COM  *		      b) a copy of this Agreement must be included with
1339126SWyllys.Ingersoll@Sun.COM  *		      each copy of the Program.
1349126SWyllys.Ingersoll@Sun.COM  *
1359126SWyllys.Ingersoll@Sun.COM  *		Contributors may not remove or alter any copyright notices
1369126SWyllys.Ingersoll@Sun.COM  *		contained within the Program.
1379126SWyllys.Ingersoll@Sun.COM  *
1389126SWyllys.Ingersoll@Sun.COM  *		Each Contributor must identify itself as the originator of
1399126SWyllys.Ingersoll@Sun.COM  *		its Contribution, if any, in a manner that reasonably
1409126SWyllys.Ingersoll@Sun.COM  *		allows subsequent Recipients to identify the originator of
1419126SWyllys.Ingersoll@Sun.COM  *		the Contribution.
1429126SWyllys.Ingersoll@Sun.COM  *
1439126SWyllys.Ingersoll@Sun.COM  *
1449126SWyllys.Ingersoll@Sun.COM  *		4. COMMERCIAL DISTRIBUTION
1459126SWyllys.Ingersoll@Sun.COM  *
1469126SWyllys.Ingersoll@Sun.COM  *		Commercial distributors of software may accept certain
1479126SWyllys.Ingersoll@Sun.COM  *		responsibilities with respect to end users, business
1489126SWyllys.Ingersoll@Sun.COM  *		partners and the like. While this license is intended to
1499126SWyllys.Ingersoll@Sun.COM  *		facilitate the commercial use of the Program, the
1509126SWyllys.Ingersoll@Sun.COM  *		Contributor who includes the Program in a commercial
1519126SWyllys.Ingersoll@Sun.COM  *		product offering should do so in a manner which does not
1529126SWyllys.Ingersoll@Sun.COM  *		create potential liability for other Contributors.
1539126SWyllys.Ingersoll@Sun.COM  *		Therefore, if a Contributor includes the Program in a
1549126SWyllys.Ingersoll@Sun.COM  *		commercial product offering, such Contributor ("Commercial
1559126SWyllys.Ingersoll@Sun.COM  *		Contributor") hereby agrees to defend and indemnify every
1569126SWyllys.Ingersoll@Sun.COM  *		other Contributor ("Indemnified Contributor") against any
1579126SWyllys.Ingersoll@Sun.COM  *		losses, damages and costs (collectively "Losses") arising
1589126SWyllys.Ingersoll@Sun.COM  *		from claims, lawsuits and other legal actions brought by a
1599126SWyllys.Ingersoll@Sun.COM  *		third party against the Indemnified Contributor to the
1609126SWyllys.Ingersoll@Sun.COM  *		extent caused by the acts or omissions of such Commercial
1619126SWyllys.Ingersoll@Sun.COM  *		Contributor in connection with its distribution of the
1629126SWyllys.Ingersoll@Sun.COM  *		Program in a commercial product offering. The obligations
1639126SWyllys.Ingersoll@Sun.COM  *		in this section do not apply to any claims or Losses
1649126SWyllys.Ingersoll@Sun.COM  *		relating to any actual or alleged intellectual property
1659126SWyllys.Ingersoll@Sun.COM  *		infringement. In order to qualify, an Indemnified
1669126SWyllys.Ingersoll@Sun.COM  *		Contributor must: a) promptly notify the Commercial
1679126SWyllys.Ingersoll@Sun.COM  *		Contributor in writing of such claim, and b) allow the
1689126SWyllys.Ingersoll@Sun.COM  *		Commercial Contributor to control, and cooperate with the
1699126SWyllys.Ingersoll@Sun.COM  *		Commercial Contributor in, the defense and any related
1709126SWyllys.Ingersoll@Sun.COM  *		settlement negotiations. The Indemnified Contributor may
1719126SWyllys.Ingersoll@Sun.COM  *		participate in any such claim at its own expense.
1729126SWyllys.Ingersoll@Sun.COM  *
1739126SWyllys.Ingersoll@Sun.COM  *
1749126SWyllys.Ingersoll@Sun.COM  *		For example, a Contributor might include the Program in a
1759126SWyllys.Ingersoll@Sun.COM  *		commercial product offering, Product X. That Contributor
1769126SWyllys.Ingersoll@Sun.COM  *		is then a Commercial Contributor. If that Commercial
1779126SWyllys.Ingersoll@Sun.COM  *		Contributor then makes performance claims, or offers
1789126SWyllys.Ingersoll@Sun.COM  *		warranties related to Product X, those performance claims
1799126SWyllys.Ingersoll@Sun.COM  *		and warranties are such Commercial Contributor's
1809126SWyllys.Ingersoll@Sun.COM  *		responsibility alone. Under this section, the Commercial
1819126SWyllys.Ingersoll@Sun.COM  *		Contributor would have to defend claims against the other
1829126SWyllys.Ingersoll@Sun.COM  *		Contributors related to those performance claims and
1839126SWyllys.Ingersoll@Sun.COM  *		warranties, and if a court requires any other Contributor
1849126SWyllys.Ingersoll@Sun.COM  *		to pay any damages as a result, the Commercial Contributor
1859126SWyllys.Ingersoll@Sun.COM  *		must pay those damages.
1869126SWyllys.Ingersoll@Sun.COM  *
1879126SWyllys.Ingersoll@Sun.COM  *
1889126SWyllys.Ingersoll@Sun.COM  *		5. NO WARRANTY
1899126SWyllys.Ingersoll@Sun.COM  *
1909126SWyllys.Ingersoll@Sun.COM  *		EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE
1919126SWyllys.Ingersoll@Sun.COM  *		PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT
1929126SWyllys.Ingersoll@Sun.COM  *		WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
1939126SWyllys.Ingersoll@Sun.COM  *		IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR
1949126SWyllys.Ingersoll@Sun.COM  *		CONDITIONS OF TITLE, NO - INFRINGEMENT, MERCHANTABILITY OR
1959126SWyllys.Ingersoll@Sun.COM  *		FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely
1969126SWyllys.Ingersoll@Sun.COM  *		responsible for determining the appropriateness of using
1979126SWyllys.Ingersoll@Sun.COM  *		and distributing the Program and assumes all risks
1989126SWyllys.Ingersoll@Sun.COM  *		associated with its exercise of rights under this
1999126SWyllys.Ingersoll@Sun.COM  *		Agreement, including but not limited to the risks and
2009126SWyllys.Ingersoll@Sun.COM  *		costs of program errors, compliance with applicable laws,
2019126SWyllys.Ingersoll@Sun.COM  *		damage to or loss of data, programs or equipment, and
2029126SWyllys.Ingersoll@Sun.COM  *		unavailability or interruption of operations.
2039126SWyllys.Ingersoll@Sun.COM  *
2049126SWyllys.Ingersoll@Sun.COM  *		6. DISCLAIMER OF LIABILITY
2059126SWyllys.Ingersoll@Sun.COM  *		EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER
2069126SWyllys.Ingersoll@Sun.COM  *		RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY
2079126SWyllys.Ingersoll@Sun.COM  *		FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
2089126SWyllys.Ingersoll@Sun.COM  *		OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION
2099126SWyllys.Ingersoll@Sun.COM  *		LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF
2109126SWyllys.Ingersoll@Sun.COM  *		LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2119126SWyllys.Ingersoll@Sun.COM  *		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2129126SWyllys.Ingersoll@Sun.COM  *		OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE
2139126SWyllys.Ingersoll@Sun.COM  *		OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE
2149126SWyllys.Ingersoll@Sun.COM  *		POSSIBILITY OF SUCH DAMAGES.
2159126SWyllys.Ingersoll@Sun.COM  *
2169126SWyllys.Ingersoll@Sun.COM  *		7. GENERAL
2179126SWyllys.Ingersoll@Sun.COM  *
2189126SWyllys.Ingersoll@Sun.COM  *		If any provision of this Agreement is invalid or
2199126SWyllys.Ingersoll@Sun.COM  *		unenforceable under applicable law, it shall not affect
2209126SWyllys.Ingersoll@Sun.COM  *		the validity or enforceability of the remainder of the
2219126SWyllys.Ingersoll@Sun.COM  *		terms of this Agreement, and without further action by the
2229126SWyllys.Ingersoll@Sun.COM  *		parties hereto, such provision shall be reformed to the
2239126SWyllys.Ingersoll@Sun.COM  *		minimum extent necessary to make such provision valid and
2249126SWyllys.Ingersoll@Sun.COM  *		enforceable.
2259126SWyllys.Ingersoll@Sun.COM  *
2269126SWyllys.Ingersoll@Sun.COM  *
2279126SWyllys.Ingersoll@Sun.COM  *		If Recipient institutes patent litigation against a
2289126SWyllys.Ingersoll@Sun.COM  *		Contributor with respect to a patent applicable to
2299126SWyllys.Ingersoll@Sun.COM  *		software (including a cros - claim or counterclaim in a
2309126SWyllys.Ingersoll@Sun.COM  *		lawsuit), then any patent licenses granted by that
2319126SWyllys.Ingersoll@Sun.COM  *		Contributor to such Recipient under this Agreement shall
2329126SWyllys.Ingersoll@Sun.COM  *		terminate as of the date such litigation is filed. In
2339126SWyllys.Ingersoll@Sun.COM  *		addition, If Recipient institutes patent litigation
2349126SWyllys.Ingersoll@Sun.COM  *		against any entity (including a cros - claim or
2359126SWyllys.Ingersoll@Sun.COM  *		counterclaim in a lawsuit) alleging that the Program
2369126SWyllys.Ingersoll@Sun.COM  *		itself (excluding combinations of the Program with other
2379126SWyllys.Ingersoll@Sun.COM  *		software or hardware) infringes such Recipient's
2389126SWyllys.Ingersoll@Sun.COM  *		patent(s), then such Recipient's rights granted under
2399126SWyllys.Ingersoll@Sun.COM  *		Section 2(b) shall terminate as of the date such
2409126SWyllys.Ingersoll@Sun.COM  *		litigation is filed.
2419126SWyllys.Ingersoll@Sun.COM  *
2429126SWyllys.Ingersoll@Sun.COM  *		All Recipient's rights under this Agreement shall
2439126SWyllys.Ingersoll@Sun.COM  *		terminate if it fails to comply with any of the material
2449126SWyllys.Ingersoll@Sun.COM  *		terms or conditions of this Agreement and does not cure
2459126SWyllys.Ingersoll@Sun.COM  *		such failure in a reasonable period of time after becoming
2469126SWyllys.Ingersoll@Sun.COM  *		aware of such noncompliance. If all Recipient's rights
2479126SWyllys.Ingersoll@Sun.COM  *		under this Agreement terminate, Recipient agrees to cease
2489126SWyllys.Ingersoll@Sun.COM  *		use and distribution of the Program as soon as reasonably
2499126SWyllys.Ingersoll@Sun.COM  *		practicable. However, Recipient's obligations under this
2509126SWyllys.Ingersoll@Sun.COM  *		Agreement and any licenses granted by Recipient relating
2519126SWyllys.Ingersoll@Sun.COM  *		to the Program shall continue and survive.
2529126SWyllys.Ingersoll@Sun.COM  *
2539126SWyllys.Ingersoll@Sun.COM  *		Everyone is permitted to copy and distribute copies of
2549126SWyllys.Ingersoll@Sun.COM  *		this Agreement, but in order to avoid inconsistency the
2559126SWyllys.Ingersoll@Sun.COM  *		Agreement is copyrighted and may only be modified in the
2569126SWyllys.Ingersoll@Sun.COM  *		following manner. The Agreement Steward reserves the right
2579126SWyllys.Ingersoll@Sun.COM  *		to publish new versions (including revisions) of this
2589126SWyllys.Ingersoll@Sun.COM  *		Agreement from time to time. No one other than the
2599126SWyllys.Ingersoll@Sun.COM  *		Agreement Steward has the right to modify this Agreement.
2609126SWyllys.Ingersoll@Sun.COM  *
2619126SWyllys.Ingersoll@Sun.COM  *		IBM is the initial Agreement Steward. IBM may assign the
2629126SWyllys.Ingersoll@Sun.COM  *		responsibility to serve as the Agreement Steward to a
2639126SWyllys.Ingersoll@Sun.COM  *		suitable separate entity. Each new version of the
2649126SWyllys.Ingersoll@Sun.COM  *		Agreement will be given a distinguishing version number.
2659126SWyllys.Ingersoll@Sun.COM  *		The Program (including Contributions) may always be
2669126SWyllys.Ingersoll@Sun.COM  *		distributed subject to the version of the Agreement under
2679126SWyllys.Ingersoll@Sun.COM  *		which it was received. In addition, after a new version of
2689126SWyllys.Ingersoll@Sun.COM  *		the Agreement is published, Contributor may elect to
2699126SWyllys.Ingersoll@Sun.COM  *		distribute the Program (including its Contributions) under
2709126SWyllys.Ingersoll@Sun.COM  *		the new version. Except as expressly stated in Sections
2719126SWyllys.Ingersoll@Sun.COM  *		2(a) and 2(b) above, Recipient receives no rights or
2729126SWyllys.Ingersoll@Sun.COM  *		licenses to the intellectual property of any Contributor
2739126SWyllys.Ingersoll@Sun.COM  *		under this Agreement, whether expressly, by implication,
2749126SWyllys.Ingersoll@Sun.COM  *		estoppel or otherwise. All rights in the Program not
2759126SWyllys.Ingersoll@Sun.COM  *		expressly granted under this Agreement are reserved.
2769126SWyllys.Ingersoll@Sun.COM  *
2779126SWyllys.Ingersoll@Sun.COM  *
2789126SWyllys.Ingersoll@Sun.COM  *		This Agreement is governed by the laws of the State of New
2799126SWyllys.Ingersoll@Sun.COM  *		York and the intellectual property laws of the United
2809126SWyllys.Ingersoll@Sun.COM  *		States of America. No party to this Agreement will bring a
2819126SWyllys.Ingersoll@Sun.COM  *		legal action under this Agreement more than one year after
2829126SWyllys.Ingersoll@Sun.COM  *		the cause of action arose. Each party waives its rights to
2839126SWyllys.Ingersoll@Sun.COM  *		a jury trial in any resulting litigation.
2849126SWyllys.Ingersoll@Sun.COM  *
2859126SWyllys.Ingersoll@Sun.COM  *
2869126SWyllys.Ingersoll@Sun.COM  *
2879126SWyllys.Ingersoll@Sun.COM  * (C) COPYRIGHT International Business Machines Corp. 2001, 2002
2889126SWyllys.Ingersoll@Sun.COM  */
2899126SWyllys.Ingersoll@Sun.COM /*
2909126SWyllys.Ingersoll@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2919126SWyllys.Ingersoll@Sun.COM  * Use is subject to license terms.
2929126SWyllys.Ingersoll@Sun.COM  */
2939126SWyllys.Ingersoll@Sun.COM 
2949126SWyllys.Ingersoll@Sun.COM #include "tpmtok_int.h"
2959126SWyllys.Ingersoll@Sun.COM 
2969126SWyllys.Ingersoll@Sun.COM // object_create()
2979126SWyllys.Ingersoll@Sun.COM //
2989126SWyllys.Ingersoll@Sun.COM // Creates an object with the specified attributes.  Verifies that all required
2999126SWyllys.Ingersoll@Sun.COM // attributes are present and adds any missing attributes that have Cryptoki
3009126SWyllys.Ingersoll@Sun.COM // defined default values.  This routine does not check whether the session is
3019126SWyllys.Ingersoll@Sun.COM // authorized to create the object.  That is done elsewhere
3029126SWyllys.Ingersoll@Sun.COM // (see object_mgr_create())
3039126SWyllys.Ingersoll@Sun.COM CK_RV
object_create(CK_ATTRIBUTE * pTemplate,CK_ULONG ulCount,OBJECT ** obj)3049126SWyllys.Ingersoll@Sun.COM object_create(CK_ATTRIBUTE  * pTemplate,
3059126SWyllys.Ingersoll@Sun.COM 	CK_ULONG	ulCount,
3069126SWyllys.Ingersoll@Sun.COM 	OBJECT	** obj)
3079126SWyllys.Ingersoll@Sun.COM {
3089126SWyllys.Ingersoll@Sun.COM 	OBJECT	* o	   = NULL;
3099126SWyllys.Ingersoll@Sun.COM 	CK_ATTRIBUTE  * attr	= NULL;
3109126SWyllys.Ingersoll@Sun.COM 	CK_ATTRIBUTE  * sensitive   = NULL;
3119126SWyllys.Ingersoll@Sun.COM 	CK_ATTRIBUTE  * extractable = NULL;
3129126SWyllys.Ingersoll@Sun.COM 	CK_ATTRIBUTE  * local	= NULL;
3139126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL	class_given = FALSE;
3149126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL	subclass_given = FALSE;
3159126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL	flag;
3169126SWyllys.Ingersoll@Sun.COM 	CK_ULONG	class = 0xFFFFFFFF, subclass = 0xFFFFFFFF;
3179126SWyllys.Ingersoll@Sun.COM 	CK_RV	   rc;
3189126SWyllys.Ingersoll@Sun.COM 	unsigned int    i;
3199126SWyllys.Ingersoll@Sun.COM 
3209126SWyllys.Ingersoll@Sun.COM 	if (! pTemplate) {
3219126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
3229126SWyllys.Ingersoll@Sun.COM 	}
3239126SWyllys.Ingersoll@Sun.COM 	// extract the object class and subclass
3249126SWyllys.Ingersoll@Sun.COM 	//
3259126SWyllys.Ingersoll@Sun.COM 	attr = pTemplate;
3269126SWyllys.Ingersoll@Sun.COM 	for (i = 0; i < ulCount; i++, attr++) {
3279126SWyllys.Ingersoll@Sun.COM 		if (attr->type == CKA_CLASS) {
3289126SWyllys.Ingersoll@Sun.COM 			class = *(CK_OBJECT_CLASS *)attr->pValue;
3299126SWyllys.Ingersoll@Sun.COM 			class_given = TRUE;
3309126SWyllys.Ingersoll@Sun.COM 		}
3319126SWyllys.Ingersoll@Sun.COM 
3329126SWyllys.Ingersoll@Sun.COM 		if (attr->type == CKA_CERTIFICATE_TYPE) {
3339126SWyllys.Ingersoll@Sun.COM 			subclass = *(CK_CERTIFICATE_TYPE *)attr->pValue;
3349126SWyllys.Ingersoll@Sun.COM 			subclass_given = TRUE;
3359126SWyllys.Ingersoll@Sun.COM 		}
3369126SWyllys.Ingersoll@Sun.COM 
3379126SWyllys.Ingersoll@Sun.COM 		if (attr->type == CKA_KEY_TYPE) {
3389126SWyllys.Ingersoll@Sun.COM 			subclass = *(CK_KEY_TYPE *)attr->pValue;
3399126SWyllys.Ingersoll@Sun.COM 			subclass_given = TRUE;
3409126SWyllys.Ingersoll@Sun.COM 		}
3419126SWyllys.Ingersoll@Sun.COM 
3429126SWyllys.Ingersoll@Sun.COM 		if (attr->type == CKA_HW_FEATURE_TYPE) {
3439126SWyllys.Ingersoll@Sun.COM 			subclass = *(CK_HW_FEATURE_TYPE *)attr->pValue;
3449126SWyllys.Ingersoll@Sun.COM 			subclass_given = TRUE;
3459126SWyllys.Ingersoll@Sun.COM 		}
3469126SWyllys.Ingersoll@Sun.COM 	}
3479126SWyllys.Ingersoll@Sun.COM 
3489126SWyllys.Ingersoll@Sun.COM 	if (class_given == FALSE) {
3499126SWyllys.Ingersoll@Sun.COM 		return (CKR_TEMPLATE_INCOMPLETE);
3509126SWyllys.Ingersoll@Sun.COM 	}
3519126SWyllys.Ingersoll@Sun.COM 	if (class != CKO_DATA && subclass_given != TRUE) {
3529126SWyllys.Ingersoll@Sun.COM 		return (CKR_TEMPLATE_INCOMPLETE);
3539126SWyllys.Ingersoll@Sun.COM 	}
3549126SWyllys.Ingersoll@Sun.COM 
3559126SWyllys.Ingersoll@Sun.COM 	rc = object_create_skel(pTemplate, ulCount,
3569126SWyllys.Ingersoll@Sun.COM 	    MODE_CREATE, class, subclass, &o);
3579126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
3589126SWyllys.Ingersoll@Sun.COM 		return (rc);
3599126SWyllys.Ingersoll@Sun.COM 	}
3609126SWyllys.Ingersoll@Sun.COM 	// for key objects, we need be careful...
3619126SWyllys.Ingersoll@Sun.COM 	//
3629126SWyllys.Ingersoll@Sun.COM 	// note:  I would think that keys loaded with C_CreateObject should
3639126SWyllys.Ingersoll@Sun.COM 	//	have their CKA_NEVER_EXTRACTABLE == FALSE and
3649126SWyllys.Ingersoll@Sun.COM 	//	CKA_ALWAYS_SENSITIVE == FALSE since the key data was presumably
3659126SWyllys.Ingersoll@Sun.COM 	//	stored in the clear prior to the call to C_CreateObject.  The
3669126SWyllys.Ingersoll@Sun.COM 	//	PKCS #11 spec doesn't impose this restriction however.
3679126SWyllys.Ingersoll@Sun.COM 	//
3689126SWyllys.Ingersoll@Sun.COM 	if (class == CKO_PRIVATE_KEY || class == CKO_SECRET_KEY) {
3699126SWyllys.Ingersoll@Sun.COM 		rc = template_attribute_find(o->template, CKA_SENSITIVE, &attr);
3709126SWyllys.Ingersoll@Sun.COM 		if (rc == FALSE) {
3719126SWyllys.Ingersoll@Sun.COM 			rc = CKR_FUNCTION_FAILED;
3729126SWyllys.Ingersoll@Sun.COM 			goto error;
3739126SWyllys.Ingersoll@Sun.COM 		}
3749126SWyllys.Ingersoll@Sun.COM 
3759126SWyllys.Ingersoll@Sun.COM 		flag = *(CK_BBOOL *)attr->pValue;
3769126SWyllys.Ingersoll@Sun.COM 
3779126SWyllys.Ingersoll@Sun.COM 		rc = build_attribute(CKA_ALWAYS_SENSITIVE, &flag,
3789126SWyllys.Ingersoll@Sun.COM 		    sizeof (CK_BYTE), &sensitive);
3799126SWyllys.Ingersoll@Sun.COM 		if (rc != CKR_OK) {
3809126SWyllys.Ingersoll@Sun.COM 			goto error;
3819126SWyllys.Ingersoll@Sun.COM 		}
3829126SWyllys.Ingersoll@Sun.COM 
3839126SWyllys.Ingersoll@Sun.COM 		rc = template_attribute_find(o->template, CKA_EXTRACTABLE,
3849126SWyllys.Ingersoll@Sun.COM 		    &attr);
3859126SWyllys.Ingersoll@Sun.COM 		if (rc == FALSE) {
3869126SWyllys.Ingersoll@Sun.COM 			rc = CKR_FUNCTION_FAILED;
3879126SWyllys.Ingersoll@Sun.COM 			goto error;
3889126SWyllys.Ingersoll@Sun.COM 		}
3899126SWyllys.Ingersoll@Sun.COM 
3909126SWyllys.Ingersoll@Sun.COM 		flag = *(CK_BBOOL *)attr->pValue;
3919126SWyllys.Ingersoll@Sun.COM 		flag = (~flag) & 0x1;
3929126SWyllys.Ingersoll@Sun.COM 
3939126SWyllys.Ingersoll@Sun.COM 		rc = build_attribute(CKA_NEVER_EXTRACTABLE, &flag,
3949126SWyllys.Ingersoll@Sun.COM 		    sizeof (CK_BYTE), &extractable);
3959126SWyllys.Ingersoll@Sun.COM 		if (rc != CKR_OK) {
3969126SWyllys.Ingersoll@Sun.COM 			goto error;
3979126SWyllys.Ingersoll@Sun.COM 		}
3989126SWyllys.Ingersoll@Sun.COM 		(void) template_update_attribute(o->template, sensitive);
3999126SWyllys.Ingersoll@Sun.COM 		(void) template_update_attribute(o->template, extractable);
4009126SWyllys.Ingersoll@Sun.COM 	}
4019126SWyllys.Ingersoll@Sun.COM 
4029126SWyllys.Ingersoll@Sun.COM 	*obj = o;
4039126SWyllys.Ingersoll@Sun.COM 
4049126SWyllys.Ingersoll@Sun.COM 	return (CKR_OK);
4059126SWyllys.Ingersoll@Sun.COM error:
4069126SWyllys.Ingersoll@Sun.COM 	if (sensitive)    free(sensitive);
4079126SWyllys.Ingersoll@Sun.COM 	if (extractable)  free(extractable);
4089126SWyllys.Ingersoll@Sun.COM 	if (local)	free(local);
4099126SWyllys.Ingersoll@Sun.COM 
4109126SWyllys.Ingersoll@Sun.COM 	(void) object_free(o);
4119126SWyllys.Ingersoll@Sun.COM 	return (rc);
4129126SWyllys.Ingersoll@Sun.COM }
4139126SWyllys.Ingersoll@Sun.COM 
4149126SWyllys.Ingersoll@Sun.COM // object_copy()
4159126SWyllys.Ingersoll@Sun.COM //
4169126SWyllys.Ingersoll@Sun.COM // Builds a copy of the specified object.  The new object gets the original
4179126SWyllys.Ingersoll@Sun.COM // object's attribute template plus any additional attributes that are
4189126SWyllys.Ingersoll@Sun.COM // specified.   Verifies that all required attributes are present.  This
4199126SWyllys.Ingersoll@Sun.COM // routine does not check whether the session is authorized to copy the
4209126SWyllys.Ingersoll@Sun.COM // object -- routines at the individual object level don't have the
4219126SWyllys.Ingersoll@Sun.COM // concept of "session".  These checks are done by the object manager.
4229126SWyllys.Ingersoll@Sun.COM //
4239126SWyllys.Ingersoll@Sun.COM CK_RV
object_copy(CK_ATTRIBUTE * pTemplate,CK_ULONG ulCount,OBJECT * old_obj,OBJECT ** new_obj)4249126SWyllys.Ingersoll@Sun.COM object_copy(CK_ATTRIBUTE  * pTemplate,
4259126SWyllys.Ingersoll@Sun.COM 	CK_ULONG	ulCount,
4269126SWyllys.Ingersoll@Sun.COM 	OBJECT	* old_obj,
4279126SWyllys.Ingersoll@Sun.COM 	OBJECT	** new_obj)
4289126SWyllys.Ingersoll@Sun.COM {
4299126SWyllys.Ingersoll@Sun.COM 	TEMPLATE  * tmpl	= NULL;
4309126SWyllys.Ingersoll@Sun.COM 	TEMPLATE  * new_tmpl = NULL;
4319126SWyllys.Ingersoll@Sun.COM 	OBJECT    * o	= NULL;
4329126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL    found;
4339126SWyllys.Ingersoll@Sun.COM 	CK_ULONG    class, subclass;
4349126SWyllys.Ingersoll@Sun.COM 	CK_RV	rc;
4359126SWyllys.Ingersoll@Sun.COM 
4369126SWyllys.Ingersoll@Sun.COM 
4379126SWyllys.Ingersoll@Sun.COM 	if (! old_obj || ! pTemplate || ! new_obj) {
4389126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
4399126SWyllys.Ingersoll@Sun.COM 	}
4409126SWyllys.Ingersoll@Sun.COM 	o	= (OBJECT   *)malloc(sizeof (OBJECT));
4419126SWyllys.Ingersoll@Sun.COM 	tmpl	= (TEMPLATE *)malloc(sizeof (TEMPLATE));
4429126SWyllys.Ingersoll@Sun.COM 	new_tmpl = (TEMPLATE *)malloc(sizeof (TEMPLATE));
4439126SWyllys.Ingersoll@Sun.COM 
4449126SWyllys.Ingersoll@Sun.COM 	if (! o || ! tmpl || ! new_tmpl) {
4459126SWyllys.Ingersoll@Sun.COM 		rc = CKR_HOST_MEMORY;
4469126SWyllys.Ingersoll@Sun.COM 		goto error;
4479126SWyllys.Ingersoll@Sun.COM 	}
4489126SWyllys.Ingersoll@Sun.COM 
4499126SWyllys.Ingersoll@Sun.COM 	(void) memset(o, 0x0, sizeof (OBJECT));
4509126SWyllys.Ingersoll@Sun.COM 	(void) memset(tmpl,	0x0, sizeof (TEMPLATE));
4519126SWyllys.Ingersoll@Sun.COM 	(void) memset(new_tmpl, 0x0, sizeof (TEMPLATE));
4529126SWyllys.Ingersoll@Sun.COM 
4539126SWyllys.Ingersoll@Sun.COM 	// copy the original object's attribute template
4549126SWyllys.Ingersoll@Sun.COM 	//
4559126SWyllys.Ingersoll@Sun.COM 	rc = template_copy(tmpl, old_obj->template);
4569126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
4579126SWyllys.Ingersoll@Sun.COM 		goto error;
4589126SWyllys.Ingersoll@Sun.COM 	}
4599126SWyllys.Ingersoll@Sun.COM 	rc = template_add_attributes(new_tmpl, pTemplate, ulCount);
4609126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
4619126SWyllys.Ingersoll@Sun.COM 		goto error;
4629126SWyllys.Ingersoll@Sun.COM 	}
4639126SWyllys.Ingersoll@Sun.COM 	// at this point, the new object has the list of attributes.  we need
4649126SWyllys.Ingersoll@Sun.COM 	// to do some more checking now:
4659126SWyllys.Ingersoll@Sun.COM 	//    1) invalid attribute values
4669126SWyllys.Ingersoll@Sun.COM 	//    2) missing required attributes
4679126SWyllys.Ingersoll@Sun.COM 	//    3) attributes inappropriate for the object class
4689126SWyllys.Ingersoll@Sun.COM 	//    4) conflicting attributes/values
4699126SWyllys.Ingersoll@Sun.COM 	//
4709126SWyllys.Ingersoll@Sun.COM 
4719126SWyllys.Ingersoll@Sun.COM 	found = template_get_class(tmpl, &class, &subclass);
4729126SWyllys.Ingersoll@Sun.COM 	if (found == FALSE) {
4739126SWyllys.Ingersoll@Sun.COM 		rc = CKR_TEMPLATE_INCONSISTENT;
4749126SWyllys.Ingersoll@Sun.COM 		goto error;
4759126SWyllys.Ingersoll@Sun.COM 	}
4769126SWyllys.Ingersoll@Sun.COM 
4779126SWyllys.Ingersoll@Sun.COM 	// The user cannot change object classes so we assume the existing
4789126SWyllys.Ingersoll@Sun.COM 	// object attributes are valid.  we still need to check the new
4799126SWyllys.Ingersoll@Sun.COM 	// attributes.   We cannot merge the new attributes in with the
4809126SWyllys.Ingersoll@Sun.COM 	// old ones and then check for validity because some attributes
4819126SWyllys.Ingersoll@Sun.COM 	// are added internally and are not allowed to be specified by
4829126SWyllys.Ingersoll@Sun.COM 	// the user (ie. CKA_LOCAL for key types) but may still be part
4839126SWyllys.Ingersoll@Sun.COM 	// of the old template.
4849126SWyllys.Ingersoll@Sun.COM 	rc = template_validate_attributes(new_tmpl, class, subclass,
4859126SWyllys.Ingersoll@Sun.COM 	    MODE_COPY);
4869126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK)
4879126SWyllys.Ingersoll@Sun.COM 		goto error;
4889126SWyllys.Ingersoll@Sun.COM 
4899126SWyllys.Ingersoll@Sun.COM 	rc = template_merge(tmpl, &new_tmpl);
4909126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
4919126SWyllys.Ingersoll@Sun.COM 		goto error;
4929126SWyllys.Ingersoll@Sun.COM 	}
4939126SWyllys.Ingersoll@Sun.COM 
4949126SWyllys.Ingersoll@Sun.COM 	rc = template_check_required_attributes(tmpl, class,
4959126SWyllys.Ingersoll@Sun.COM 	    subclass, MODE_COPY);
4969126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK)
4979126SWyllys.Ingersoll@Sun.COM 		goto error;
4989126SWyllys.Ingersoll@Sun.COM 
4999126SWyllys.Ingersoll@Sun.COM 	o->template = tmpl;
5009126SWyllys.Ingersoll@Sun.COM 	*new_obj = o;
5019126SWyllys.Ingersoll@Sun.COM 
5029126SWyllys.Ingersoll@Sun.COM 	return (CKR_OK);
5039126SWyllys.Ingersoll@Sun.COM error:
5049126SWyllys.Ingersoll@Sun.COM 	if (tmpl)
5059126SWyllys.Ingersoll@Sun.COM 		(void) template_free(tmpl);
5069126SWyllys.Ingersoll@Sun.COM 	if (new_tmpl)
5079126SWyllys.Ingersoll@Sun.COM 		(void) template_free(new_tmpl);
5089126SWyllys.Ingersoll@Sun.COM 	if (o)
5099126SWyllys.Ingersoll@Sun.COM 		(void) object_free(o);
5109126SWyllys.Ingersoll@Sun.COM 
5119126SWyllys.Ingersoll@Sun.COM 	return (rc);
5129126SWyllys.Ingersoll@Sun.COM }
5139126SWyllys.Ingersoll@Sun.COM 
5149126SWyllys.Ingersoll@Sun.COM CK_RV
object_flatten(OBJECT * obj,CK_BYTE ** data,UINT32 * len)5159126SWyllys.Ingersoll@Sun.COM object_flatten(OBJECT    * obj,
5169126SWyllys.Ingersoll@Sun.COM 	CK_BYTE  ** data,
517*9651SWyllys.Ingersoll@Sun.COM 	UINT32  *len)
5189126SWyllys.Ingersoll@Sun.COM {
5199126SWyllys.Ingersoll@Sun.COM 	CK_BYTE    * buf = NULL;
520*9651SWyllys.Ingersoll@Sun.COM 	CK_ULONG_32	tmpl_len, total_len;
521*9651SWyllys.Ingersoll@Sun.COM 	CK_ULONG_32	offset;
5229126SWyllys.Ingersoll@Sun.COM 	CK_ULONG_32	count;
5239126SWyllys.Ingersoll@Sun.COM 	long	 rc;
5249126SWyllys.Ingersoll@Sun.COM 
5259126SWyllys.Ingersoll@Sun.COM 	if (! obj) {
5269126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
5279126SWyllys.Ingersoll@Sun.COM 	}
5289126SWyllys.Ingersoll@Sun.COM 	count    = template_get_count(obj->template);
5299126SWyllys.Ingersoll@Sun.COM 	tmpl_len = template_get_compressed_size(obj->template);
5309126SWyllys.Ingersoll@Sun.COM 
5319126SWyllys.Ingersoll@Sun.COM 	total_len = tmpl_len + sizeof (CK_OBJECT_CLASS_32) +
5329126SWyllys.Ingersoll@Sun.COM 	    sizeof (CK_ULONG_32) + 8;
5339126SWyllys.Ingersoll@Sun.COM 
5349126SWyllys.Ingersoll@Sun.COM 	buf = (CK_BYTE *)malloc(total_len);
5359126SWyllys.Ingersoll@Sun.COM 	if (!buf) {
5369126SWyllys.Ingersoll@Sun.COM 		return (CKR_HOST_MEMORY);
5379126SWyllys.Ingersoll@Sun.COM 	}
5389126SWyllys.Ingersoll@Sun.COM 
5399126SWyllys.Ingersoll@Sun.COM 	(void) memset((CK_BYTE *)buf, 0x0, total_len);
5409126SWyllys.Ingersoll@Sun.COM 
5419126SWyllys.Ingersoll@Sun.COM 	offset = 0;
5429126SWyllys.Ingersoll@Sun.COM 
5439126SWyllys.Ingersoll@Sun.COM 	(void) memcpy(buf + offset, &obj->class, sizeof (CK_OBJECT_CLASS_32));
5449126SWyllys.Ingersoll@Sun.COM 	offset += sizeof (CK_OBJECT_CLASS_32);
5459126SWyllys.Ingersoll@Sun.COM 
5469126SWyllys.Ingersoll@Sun.COM 	(void) memcpy(buf + offset, &count, sizeof (CK_ULONG_32));
5479126SWyllys.Ingersoll@Sun.COM 	offset += sizeof (CK_ULONG_32);
5489126SWyllys.Ingersoll@Sun.COM 
5499126SWyllys.Ingersoll@Sun.COM 	(void) memcpy(buf + offset, &obj->name,  sizeof (CK_BYTE) * 8);
5509126SWyllys.Ingersoll@Sun.COM 	offset += 8;
5519126SWyllys.Ingersoll@Sun.COM 	rc = template_flatten(obj->template, buf + offset);
5529126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
5539126SWyllys.Ingersoll@Sun.COM 		free(buf);
5549126SWyllys.Ingersoll@Sun.COM 		return (rc);
5559126SWyllys.Ingersoll@Sun.COM 	}
5569126SWyllys.Ingersoll@Sun.COM 
5579126SWyllys.Ingersoll@Sun.COM 	*data = buf;
5589126SWyllys.Ingersoll@Sun.COM 	*len  = total_len;
5599126SWyllys.Ingersoll@Sun.COM 
5609126SWyllys.Ingersoll@Sun.COM 	return (CKR_OK);
5619126SWyllys.Ingersoll@Sun.COM }
5629126SWyllys.Ingersoll@Sun.COM 
5639126SWyllys.Ingersoll@Sun.COM CK_BBOOL
object_free(OBJECT * obj)5649126SWyllys.Ingersoll@Sun.COM object_free(OBJECT *obj)
5659126SWyllys.Ingersoll@Sun.COM {
5669126SWyllys.Ingersoll@Sun.COM 	(void) template_free(obj->template);
5679126SWyllys.Ingersoll@Sun.COM 	free(obj);
5689126SWyllys.Ingersoll@Sun.COM 
5699126SWyllys.Ingersoll@Sun.COM 	return (TRUE);
5709126SWyllys.Ingersoll@Sun.COM }
5719126SWyllys.Ingersoll@Sun.COM 
5729126SWyllys.Ingersoll@Sun.COM 
5739126SWyllys.Ingersoll@Sun.COM // object_is_modifiable()
5749126SWyllys.Ingersoll@Sun.COM //
5759126SWyllys.Ingersoll@Sun.COM CK_BBOOL
object_is_modifiable(OBJECT * obj)5769126SWyllys.Ingersoll@Sun.COM object_is_modifiable(OBJECT *obj)
5779126SWyllys.Ingersoll@Sun.COM {
5789126SWyllys.Ingersoll@Sun.COM 	CK_ATTRIBUTE  * attr = NULL;
5799126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL	modifiable;
5809126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL	found;
5819126SWyllys.Ingersoll@Sun.COM 
5829126SWyllys.Ingersoll@Sun.COM 	found = template_attribute_find(obj->template, CKA_MODIFIABLE, &attr);
5839126SWyllys.Ingersoll@Sun.COM 	if (found == FALSE)
5849126SWyllys.Ingersoll@Sun.COM 		return (TRUE);
5859126SWyllys.Ingersoll@Sun.COM 
5869126SWyllys.Ingersoll@Sun.COM 	modifiable = *(CK_BBOOL *)attr->pValue;
5879126SWyllys.Ingersoll@Sun.COM 
5889126SWyllys.Ingersoll@Sun.COM 	return (modifiable);
5899126SWyllys.Ingersoll@Sun.COM }
5909126SWyllys.Ingersoll@Sun.COM 
5919126SWyllys.Ingersoll@Sun.COM 
5929126SWyllys.Ingersoll@Sun.COM // object_is_private()
5939126SWyllys.Ingersoll@Sun.COM //
5949126SWyllys.Ingersoll@Sun.COM // an is_private member should probably be added to OBJECT
5959126SWyllys.Ingersoll@Sun.COM //
5969126SWyllys.Ingersoll@Sun.COM CK_BBOOL
object_is_private(OBJECT * obj)5979126SWyllys.Ingersoll@Sun.COM object_is_private(OBJECT *obj) {
5989126SWyllys.Ingersoll@Sun.COM 	CK_ATTRIBUTE  * attr = NULL;
5999126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL	priv;
6009126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL	found;
6019126SWyllys.Ingersoll@Sun.COM 
6029126SWyllys.Ingersoll@Sun.COM 	found = template_attribute_find(obj->template, CKA_PRIVATE, &attr);
6039126SWyllys.Ingersoll@Sun.COM 	if (found == FALSE)
6049126SWyllys.Ingersoll@Sun.COM 		return (TRUE);
6059126SWyllys.Ingersoll@Sun.COM 
6069126SWyllys.Ingersoll@Sun.COM 	if (attr == NULL)
6079126SWyllys.Ingersoll@Sun.COM 		return (TRUE);
6089126SWyllys.Ingersoll@Sun.COM 
6099126SWyllys.Ingersoll@Sun.COM 	priv = *((CK_BBOOL *)attr->pValue);
6109126SWyllys.Ingersoll@Sun.COM 
6119126SWyllys.Ingersoll@Sun.COM 	return (priv);
6129126SWyllys.Ingersoll@Sun.COM }
6139126SWyllys.Ingersoll@Sun.COM 
6149126SWyllys.Ingersoll@Sun.COM CK_BBOOL
object_is_public(OBJECT * obj)6159126SWyllys.Ingersoll@Sun.COM object_is_public(OBJECT *obj) {
6169126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL rc;
6179126SWyllys.Ingersoll@Sun.COM 
6189126SWyllys.Ingersoll@Sun.COM 	rc = object_is_private(obj);
6199126SWyllys.Ingersoll@Sun.COM 
6209126SWyllys.Ingersoll@Sun.COM 	if (rc)
6219126SWyllys.Ingersoll@Sun.COM 		return (FALSE);
6229126SWyllys.Ingersoll@Sun.COM 	return (TRUE);
6239126SWyllys.Ingersoll@Sun.COM }
6249126SWyllys.Ingersoll@Sun.COM 
6259126SWyllys.Ingersoll@Sun.COM CK_BBOOL
object_is_token_object(OBJECT * obj)6269126SWyllys.Ingersoll@Sun.COM object_is_token_object(OBJECT *obj) {
6279126SWyllys.Ingersoll@Sun.COM 	CK_ATTRIBUTE  * attr = NULL;
6289126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL	is_token;
6299126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL	found;
6309126SWyllys.Ingersoll@Sun.COM 
6319126SWyllys.Ingersoll@Sun.COM 	found = template_attribute_find(obj->template,
6329126SWyllys.Ingersoll@Sun.COM 	    CKA_TOKEN, &attr);
6339126SWyllys.Ingersoll@Sun.COM 	if (found == FALSE)
6349126SWyllys.Ingersoll@Sun.COM 		return (FALSE);
6359126SWyllys.Ingersoll@Sun.COM 	is_token = *(CK_BBOOL *)attr->pValue;
6369126SWyllys.Ingersoll@Sun.COM 	return (is_token);
6379126SWyllys.Ingersoll@Sun.COM }
6389126SWyllys.Ingersoll@Sun.COM 
6399126SWyllys.Ingersoll@Sun.COM CK_BBOOL
object_is_session_object(OBJECT * obj)6409126SWyllys.Ingersoll@Sun.COM object_is_session_object(OBJECT *obj) {
6419126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL rc;
6429126SWyllys.Ingersoll@Sun.COM 
6439126SWyllys.Ingersoll@Sun.COM 	rc = object_is_token_object(obj);
6449126SWyllys.Ingersoll@Sun.COM 
6459126SWyllys.Ingersoll@Sun.COM 	if (rc)
6469126SWyllys.Ingersoll@Sun.COM 		return (FALSE);
6479126SWyllys.Ingersoll@Sun.COM 	else
6489126SWyllys.Ingersoll@Sun.COM 		return (TRUE);
6499126SWyllys.Ingersoll@Sun.COM }
6509126SWyllys.Ingersoll@Sun.COM 
6519126SWyllys.Ingersoll@Sun.COM CK_ULONG
object_get_size(OBJECT * obj)6529126SWyllys.Ingersoll@Sun.COM object_get_size(OBJECT *obj) {
6539126SWyllys.Ingersoll@Sun.COM 	CK_ULONG  size;
6549126SWyllys.Ingersoll@Sun.COM 
6559126SWyllys.Ingersoll@Sun.COM 	size = sizeof (OBJECT) + template_get_size(obj->template);
6569126SWyllys.Ingersoll@Sun.COM 
6579126SWyllys.Ingersoll@Sun.COM 	return (size);
6589126SWyllys.Ingersoll@Sun.COM }
6599126SWyllys.Ingersoll@Sun.COM 
6609126SWyllys.Ingersoll@Sun.COM CK_RV
object_get_attribute_values(OBJECT * obj,CK_ATTRIBUTE * pTemplate,CK_ULONG ulCount)6619126SWyllys.Ingersoll@Sun.COM object_get_attribute_values(OBJECT	* obj,
6629126SWyllys.Ingersoll@Sun.COM 	CK_ATTRIBUTE  * pTemplate,
6639126SWyllys.Ingersoll@Sun.COM 	CK_ULONG	ulCount)
6649126SWyllys.Ingersoll@Sun.COM {
6659126SWyllys.Ingersoll@Sun.COM 	TEMPLATE	  *obj_tmpl = NULL;
6669126SWyllys.Ingersoll@Sun.COM 	CK_ATTRIBUTE	*attr	= NULL;
6679126SWyllys.Ingersoll@Sun.COM 	CK_ULONG	   i;
6689126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL	   flag;
6699126SWyllys.Ingersoll@Sun.COM 	CK_RV		rc;
6709126SWyllys.Ingersoll@Sun.COM 
6719126SWyllys.Ingersoll@Sun.COM 	rc = CKR_OK;
6729126SWyllys.Ingersoll@Sun.COM 
6739126SWyllys.Ingersoll@Sun.COM 	obj_tmpl = obj->template;
6749126SWyllys.Ingersoll@Sun.COM 
6759126SWyllys.Ingersoll@Sun.COM 	for (i = 0; i < ulCount; i++) {
6769126SWyllys.Ingersoll@Sun.COM 		flag = template_check_exportability(obj_tmpl,
6779126SWyllys.Ingersoll@Sun.COM 		    pTemplate[i].type);
6789126SWyllys.Ingersoll@Sun.COM 		if (flag == FALSE) {
6799126SWyllys.Ingersoll@Sun.COM 			rc = CKR_ATTRIBUTE_SENSITIVE;
6809126SWyllys.Ingersoll@Sun.COM 			pTemplate[i].ulValueLen = (CK_ULONG) - 1;
6819126SWyllys.Ingersoll@Sun.COM 			continue;
6829126SWyllys.Ingersoll@Sun.COM 		}
6839126SWyllys.Ingersoll@Sun.COM 
6849126SWyllys.Ingersoll@Sun.COM 		flag = template_attribute_find(obj_tmpl,
6859126SWyllys.Ingersoll@Sun.COM 		    pTemplate[i].type, &attr);
6869126SWyllys.Ingersoll@Sun.COM 		if (flag == FALSE) {
6879126SWyllys.Ingersoll@Sun.COM 			rc = CKR_ATTRIBUTE_TYPE_INVALID;
6889126SWyllys.Ingersoll@Sun.COM 			pTemplate[i].ulValueLen = (CK_ULONG) - 1;
6899126SWyllys.Ingersoll@Sun.COM 			continue;
6909126SWyllys.Ingersoll@Sun.COM 		}
6919126SWyllys.Ingersoll@Sun.COM 
6929126SWyllys.Ingersoll@Sun.COM 		if (pTemplate[i].pValue == NULL) {
6939126SWyllys.Ingersoll@Sun.COM 			pTemplate[i].ulValueLen = attr->ulValueLen;
6949126SWyllys.Ingersoll@Sun.COM 		} else if (pTemplate[i].ulValueLen >= attr->ulValueLen) {
6959126SWyllys.Ingersoll@Sun.COM 			(void) memcpy(pTemplate[i].pValue, attr->pValue,
6969126SWyllys.Ingersoll@Sun.COM 			    attr->ulValueLen);
6979126SWyllys.Ingersoll@Sun.COM 			pTemplate[i].ulValueLen = attr->ulValueLen;
6989126SWyllys.Ingersoll@Sun.COM 		} else {
6999126SWyllys.Ingersoll@Sun.COM 			rc = CKR_BUFFER_TOO_SMALL;
7009126SWyllys.Ingersoll@Sun.COM 			pTemplate[i].ulValueLen = (CK_ULONG) - 1;
7019126SWyllys.Ingersoll@Sun.COM 		}
7029126SWyllys.Ingersoll@Sun.COM 	}
7039126SWyllys.Ingersoll@Sun.COM 
7049126SWyllys.Ingersoll@Sun.COM 	return (rc);
7059126SWyllys.Ingersoll@Sun.COM }
7069126SWyllys.Ingersoll@Sun.COM 
7079126SWyllys.Ingersoll@Sun.COM CK_RV
object_set_attribute_values(OBJECT * obj,CK_ATTRIBUTE * pTemplate,CK_ULONG ulCount)7089126SWyllys.Ingersoll@Sun.COM object_set_attribute_values(OBJECT	* obj,
7099126SWyllys.Ingersoll@Sun.COM 	CK_ATTRIBUTE  * pTemplate,
7109126SWyllys.Ingersoll@Sun.COM 	CK_ULONG	ulCount)
7119126SWyllys.Ingersoll@Sun.COM {
7129126SWyllys.Ingersoll@Sun.COM 	TEMPLATE * new_tmpl;
7139126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL   found;
7149126SWyllys.Ingersoll@Sun.COM 	CK_ULONG   class, subclass;
7159126SWyllys.Ingersoll@Sun.COM 	CK_RV	rc;
7169126SWyllys.Ingersoll@Sun.COM 
7179126SWyllys.Ingersoll@Sun.COM 	if (! obj || ! pTemplate) {
7189126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
7199126SWyllys.Ingersoll@Sun.COM 	}
7209126SWyllys.Ingersoll@Sun.COM 
7219126SWyllys.Ingersoll@Sun.COM 	found = template_get_class(obj->template, &class, &subclass);
7229126SWyllys.Ingersoll@Sun.COM 	if (found == FALSE) {
7239126SWyllys.Ingersoll@Sun.COM 		rc = CKR_FUNCTION_FAILED;
7249126SWyllys.Ingersoll@Sun.COM 		goto error;
7259126SWyllys.Ingersoll@Sun.COM 	}
7269126SWyllys.Ingersoll@Sun.COM 
7279126SWyllys.Ingersoll@Sun.COM 	new_tmpl = (TEMPLATE *)malloc(sizeof (TEMPLATE));
7289126SWyllys.Ingersoll@Sun.COM 	if (! new_tmpl) {
7299126SWyllys.Ingersoll@Sun.COM 		return (CKR_HOST_MEMORY);
7309126SWyllys.Ingersoll@Sun.COM 	}
7319126SWyllys.Ingersoll@Sun.COM 	(void) memset(new_tmpl, 0x0, sizeof (TEMPLATE));
7329126SWyllys.Ingersoll@Sun.COM 
7339126SWyllys.Ingersoll@Sun.COM 	rc = template_add_attributes(new_tmpl, pTemplate, ulCount);
7349126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
7359126SWyllys.Ingersoll@Sun.COM 		goto error;
7369126SWyllys.Ingersoll@Sun.COM 	}
7379126SWyllys.Ingersoll@Sun.COM 
7389126SWyllys.Ingersoll@Sun.COM 	rc = template_validate_attributes(new_tmpl, class,
7399126SWyllys.Ingersoll@Sun.COM 	    subclass, MODE_MODIFY);
7409126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
7419126SWyllys.Ingersoll@Sun.COM 		goto error;
7429126SWyllys.Ingersoll@Sun.COM 	}
7439126SWyllys.Ingersoll@Sun.COM 
7449126SWyllys.Ingersoll@Sun.COM 	rc = template_merge(obj->template, &new_tmpl);
7459126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
7469126SWyllys.Ingersoll@Sun.COM 		return (rc);
7479126SWyllys.Ingersoll@Sun.COM 	}
7489126SWyllys.Ingersoll@Sun.COM 	return (CKR_OK);
7499126SWyllys.Ingersoll@Sun.COM error:
7509126SWyllys.Ingersoll@Sun.COM 	if (new_tmpl)
7519126SWyllys.Ingersoll@Sun.COM 		(void) template_free(new_tmpl);
7529126SWyllys.Ingersoll@Sun.COM 	return (rc);
7539126SWyllys.Ingersoll@Sun.COM }
7549126SWyllys.Ingersoll@Sun.COM 
7559126SWyllys.Ingersoll@Sun.COM CK_RV
object_restore(CK_BYTE * data,OBJECT ** new_obj,CK_BBOOL replace)7569126SWyllys.Ingersoll@Sun.COM object_restore(CK_BYTE *data, OBJECT **new_obj, CK_BBOOL replace) {
7579126SWyllys.Ingersoll@Sun.COM 	TEMPLATE  * tmpl = NULL;
7589126SWyllys.Ingersoll@Sun.COM 	OBJECT    * obj  = NULL;
7599126SWyllys.Ingersoll@Sun.COM 	CK_ULONG    offset = 0;
7609126SWyllys.Ingersoll@Sun.COM 	CK_ULONG_32    count = 0;
7619126SWyllys.Ingersoll@Sun.COM 	CK_RV	rc;
7629126SWyllys.Ingersoll@Sun.COM 
7639126SWyllys.Ingersoll@Sun.COM 	if (! data || ! new_obj) {
7649126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
7659126SWyllys.Ingersoll@Sun.COM 	}
7669126SWyllys.Ingersoll@Sun.COM 	obj = (OBJECT *)malloc(sizeof (OBJECT));
7679126SWyllys.Ingersoll@Sun.COM 	if (! obj) {
7689126SWyllys.Ingersoll@Sun.COM 		rc = CKR_HOST_MEMORY;
7699126SWyllys.Ingersoll@Sun.COM 		goto error;
7709126SWyllys.Ingersoll@Sun.COM 	}
7719126SWyllys.Ingersoll@Sun.COM 
7729126SWyllys.Ingersoll@Sun.COM 
7739126SWyllys.Ingersoll@Sun.COM 	(void) memset(obj, 0x0, sizeof (OBJECT));
7749126SWyllys.Ingersoll@Sun.COM 
7759126SWyllys.Ingersoll@Sun.COM 	(void) memcpy(&obj->class, data + offset, sizeof (CK_OBJECT_CLASS_32));
7769126SWyllys.Ingersoll@Sun.COM 	offset += sizeof (CK_OBJECT_CLASS_32);
7779126SWyllys.Ingersoll@Sun.COM 
7789126SWyllys.Ingersoll@Sun.COM 	(void) memcpy(&count, data + offset, sizeof (CK_ULONG_32));
7799126SWyllys.Ingersoll@Sun.COM 	offset += sizeof (CK_ULONG_32);
7809126SWyllys.Ingersoll@Sun.COM 
7819126SWyllys.Ingersoll@Sun.COM 
7829126SWyllys.Ingersoll@Sun.COM 	(void) memcpy(&obj->name, data + offset, 8);
7839126SWyllys.Ingersoll@Sun.COM 	offset += 8;
7849126SWyllys.Ingersoll@Sun.COM 
7859126SWyllys.Ingersoll@Sun.COM 	rc = template_unflatten(&tmpl, data + offset, count);
7869126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
7879126SWyllys.Ingersoll@Sun.COM 		goto error;
7889126SWyllys.Ingersoll@Sun.COM 	}
7899126SWyllys.Ingersoll@Sun.COM 	obj->template = tmpl;
7909126SWyllys.Ingersoll@Sun.COM 
7919126SWyllys.Ingersoll@Sun.COM 	if (replace == FALSE) {
7929126SWyllys.Ingersoll@Sun.COM 		 *new_obj = obj;
7939126SWyllys.Ingersoll@Sun.COM 	} else {
7949126SWyllys.Ingersoll@Sun.COM 		(void) template_free((*new_obj)->template);
7959126SWyllys.Ingersoll@Sun.COM 		(void) memcpy(*new_obj, obj, sizeof (OBJECT));
7969126SWyllys.Ingersoll@Sun.COM 
7979126SWyllys.Ingersoll@Sun.COM 		free(obj);
7989126SWyllys.Ingersoll@Sun.COM 	}
7999126SWyllys.Ingersoll@Sun.COM 
8009126SWyllys.Ingersoll@Sun.COM 	return (CKR_OK);
8019126SWyllys.Ingersoll@Sun.COM error:
8029126SWyllys.Ingersoll@Sun.COM 	if (obj)
8039126SWyllys.Ingersoll@Sun.COM 		(void) object_free(obj);
8049126SWyllys.Ingersoll@Sun.COM 	if (tmpl)
8059126SWyllys.Ingersoll@Sun.COM 		(void) template_free(tmpl);
8069126SWyllys.Ingersoll@Sun.COM 
8079126SWyllys.Ingersoll@Sun.COM 	return (rc);
8089126SWyllys.Ingersoll@Sun.COM }
8099126SWyllys.Ingersoll@Sun.COM 
8109126SWyllys.Ingersoll@Sun.COM 
8119126SWyllys.Ingersoll@Sun.COM //
8129126SWyllys.Ingersoll@Sun.COM //
8139126SWyllys.Ingersoll@Sun.COM CK_RV
object_create_skel(CK_ATTRIBUTE * pTemplate,CK_ULONG ulCount,CK_ULONG mode,CK_ULONG class,CK_ULONG subclass,OBJECT ** obj)8149126SWyllys.Ingersoll@Sun.COM object_create_skel(CK_ATTRIBUTE  * pTemplate,
8159126SWyllys.Ingersoll@Sun.COM 	CK_ULONG	ulCount,
8169126SWyllys.Ingersoll@Sun.COM 	CK_ULONG	mode,
8179126SWyllys.Ingersoll@Sun.COM 	CK_ULONG	class,
8189126SWyllys.Ingersoll@Sun.COM 	CK_ULONG	subclass,
8199126SWyllys.Ingersoll@Sun.COM 	OBJECT	** obj) {
8209126SWyllys.Ingersoll@Sun.COM 	TEMPLATE  * tmpl  = NULL;
8219126SWyllys.Ingersoll@Sun.COM 	TEMPLATE  * tmpl2 = NULL;
8229126SWyllys.Ingersoll@Sun.COM 	OBJECT   * o	= NULL;
8239126SWyllys.Ingersoll@Sun.COM 	CK_RV	rc;
8249126SWyllys.Ingersoll@Sun.COM 
8259126SWyllys.Ingersoll@Sun.COM 
8269126SWyllys.Ingersoll@Sun.COM 	if (! obj) {
8279126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
8289126SWyllys.Ingersoll@Sun.COM 	}
8299126SWyllys.Ingersoll@Sun.COM 	if (! pTemplate && (ulCount != 0)) {
8309126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
8319126SWyllys.Ingersoll@Sun.COM 	}
8329126SWyllys.Ingersoll@Sun.COM 	o	= (OBJECT *)malloc(sizeof (OBJECT));
8339126SWyllys.Ingersoll@Sun.COM 	tmpl  = (TEMPLATE *)malloc(sizeof (TEMPLATE));
8349126SWyllys.Ingersoll@Sun.COM 	tmpl2 = (TEMPLATE *)malloc(sizeof (TEMPLATE));
8359126SWyllys.Ingersoll@Sun.COM 
8369126SWyllys.Ingersoll@Sun.COM 	if (! o || ! tmpl || ! tmpl2) {
8379126SWyllys.Ingersoll@Sun.COM 		rc = CKR_HOST_MEMORY;
8389126SWyllys.Ingersoll@Sun.COM 		goto done;
8399126SWyllys.Ingersoll@Sun.COM 	}
8409126SWyllys.Ingersoll@Sun.COM 	(void) memset(o,	0x0, sizeof (OBJECT));
8419126SWyllys.Ingersoll@Sun.COM 	(void) memset(tmpl,  0x0, sizeof (TEMPLATE));
8429126SWyllys.Ingersoll@Sun.COM 	(void) memset(tmpl2, 0x0, sizeof (TEMPLATE));
8439126SWyllys.Ingersoll@Sun.COM 
8449126SWyllys.Ingersoll@Sun.COM 
8459126SWyllys.Ingersoll@Sun.COM 	rc = template_add_default_attributes(tmpl, class, subclass, mode);
8469126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK)
8479126SWyllys.Ingersoll@Sun.COM 		goto done;
8489126SWyllys.Ingersoll@Sun.COM 
8499126SWyllys.Ingersoll@Sun.COM 	rc = template_add_attributes(tmpl2, pTemplate, ulCount);
8509126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK)
8519126SWyllys.Ingersoll@Sun.COM 		goto done;
8529126SWyllys.Ingersoll@Sun.COM 
8539126SWyllys.Ingersoll@Sun.COM 
8549126SWyllys.Ingersoll@Sun.COM 	// at this point, the new template has the list of attributes.  we need
8559126SWyllys.Ingersoll@Sun.COM 	// to do some more checking now:
8569126SWyllys.Ingersoll@Sun.COM 	//    1) invalid attribute values
8579126SWyllys.Ingersoll@Sun.COM 	//    2) missing required attributes
8589126SWyllys.Ingersoll@Sun.COM 	//    3) attributes inappropriate for the object class
8599126SWyllys.Ingersoll@Sun.COM 	//    4) conflicting attributes/values
8609126SWyllys.Ingersoll@Sun.COM 	//
8619126SWyllys.Ingersoll@Sun.COM 
8629126SWyllys.Ingersoll@Sun.COM 	rc = template_validate_attributes(tmpl2, class, subclass, mode);
8639126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
8649126SWyllys.Ingersoll@Sun.COM 		goto done;
8659126SWyllys.Ingersoll@Sun.COM 	}
8669126SWyllys.Ingersoll@Sun.COM 
8679126SWyllys.Ingersoll@Sun.COM 	rc = template_check_required_attributes(tmpl2, class, subclass, mode);
8689126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
8699126SWyllys.Ingersoll@Sun.COM 		goto done;
8709126SWyllys.Ingersoll@Sun.COM 	}
8719126SWyllys.Ingersoll@Sun.COM 
8729126SWyllys.Ingersoll@Sun.COM 	rc = template_merge(tmpl, &tmpl2);
8739126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
8749126SWyllys.Ingersoll@Sun.COM 		goto done;
8759126SWyllys.Ingersoll@Sun.COM 	}
8769126SWyllys.Ingersoll@Sun.COM 	// at this point, we should have a valid object with correct attributes
8779126SWyllys.Ingersoll@Sun.COM 	//
8789126SWyllys.Ingersoll@Sun.COM 	o->template = tmpl;
8799126SWyllys.Ingersoll@Sun.COM 	 *obj = o;
8809126SWyllys.Ingersoll@Sun.COM 
8819126SWyllys.Ingersoll@Sun.COM 	return (CKR_OK);
8829126SWyllys.Ingersoll@Sun.COM 	done:
8839126SWyllys.Ingersoll@Sun.COM 	if (o)	free(o);
8849126SWyllys.Ingersoll@Sun.COM 	if (tmpl)
8859126SWyllys.Ingersoll@Sun.COM 		(void) template_free(tmpl);
8869126SWyllys.Ingersoll@Sun.COM 	if (tmpl2)
8879126SWyllys.Ingersoll@Sun.COM 		(void) template_free(tmpl2);
8889126SWyllys.Ingersoll@Sun.COM 
8899126SWyllys.Ingersoll@Sun.COM 	return (rc);
8909126SWyllys.Ingersoll@Sun.COM }
891