147e946eWyllys Ingersoll/*
247e946eWyllys Ingersoll *		Common Public License Version 0.5
347e946eWyllys Ingersoll *
847e946eWyllys Ingersoll *
947e946eWyllys Ingersoll *		1. DEFINITIONS
1047e946eWyllys Ingersoll *
1147e946eWyllys Ingersoll *		"Contribution" means:
1247e946eWyllys Ingersoll *		      a) in the case of the initial Contributor, the
1347e946eWyllys Ingersoll *		      initial code and documentation distributed under
1447e946eWyllys Ingersoll *		      this Agreement, and
1547e946eWyllys Ingersoll *
1647e946eWyllys Ingersoll *		      b) in the case of each subsequent Contributor:
1747e946eWyllys Ingersoll *		      i) changes to the Program, and
1847e946eWyllys Ingersoll *		      ii) additions to the Program;
1947e946eWyllys Ingersoll *
2047e946eWyllys Ingersoll *		      where such changes and/or additions to the Program
2147e946eWyllys Ingersoll *		      originate from and are distributed by that
2247e946eWyllys Ingersoll *		      particular Contributor. A Contribution 'originates'
2347e946eWyllys Ingersoll *		      from a Contributor if it was added to the Program
2447e946eWyllys Ingersoll *		      by such Contributor itself or anyone acting on such
2547e946eWyllys Ingersoll *		      Contributor's behalf. Contributions do not include
2647e946eWyllys Ingersoll *		      additions to the Program which: (i) are separate
2747e946eWyllys Ingersoll *		      modules of software distributed in conjunction with
2847e946eWyllys Ingersoll *		      the Program under their own license agreement, and
2947e946eWyllys Ingersoll *		      (ii) are not derivative works of the Program.
3047e946eWyllys Ingersoll *
3147e946eWyllys Ingersoll *
3247e946eWyllys Ingersoll *		"Contributor" means any person or entity that distributes
3347e946eWyllys Ingersoll *		the Program.
3447e946eWyllys Ingersoll *
3547e946eWyllys Ingersoll *		"Licensed Patents " mean patent claims licensable by a
3647e946eWyllys Ingersoll *		Contributor which are necessarily infringed by the use or
3747e946eWyllys Ingersoll *		sale of its Contribution alone or when combined with the
3847e946eWyllys Ingersoll *		Program.
3947e946eWyllys Ingersoll *
4047e946eWyllys Ingersoll *		"Program" means the Contributions distributed in
4147e946eWyllys Ingersoll *		accordance with this Agreement.
4247e946eWyllys Ingersoll *
4347e946eWyllys Ingersoll *		"Recipient" means anyone who receives the Program under
4447e946eWyllys Ingersoll *		this Agreement, including all Contributors.
4547e946eWyllys Ingersoll *
4647e946eWyllys Ingersoll *		2. GRANT OF RIGHTS
4747e946eWyllys Ingersoll *
4847e946eWyllys Ingersoll *		      a) Subject to the terms of this Agreement, each
4947e946eWyllys Ingersoll *		      Contributor hereby grants Recipient a
5047e946eWyllys Ingersoll *		      no - exclusive, worldwide, royalt - free copyright
5147e946eWyllys Ingersoll *		      license to reproduce, prepare derivative works of,
5247e946eWyllys Ingersoll *		      publicly display, publicly perform, distribute and
5347e946eWyllys Ingersoll *		      sublicense the Contribution of such Contributor, if
5447e946eWyllys Ingersoll *		      any, and such derivative works, in source code and
5547e946eWyllys Ingersoll *		      object code form.
5647e946eWyllys Ingersoll *
5747e946eWyllys Ingersoll *		      b) Subject to the terms of this Agreement, each
5847e946eWyllys Ingersoll *		      Contributor hereby grants Recipient a
5947e946eWyllys Ingersoll *		      no - exclusive, worldwide, royalt - free patent
6047e946eWyllys Ingersoll *		      license under Licensed Patents to make, use, sell,
6147e946eWyllys Ingersoll *		      offer to sell, import and otherwise transfer the
6247e946eWyllys Ingersoll *		      Contribution of such Contributor, if any, in source
6347e946eWyllys Ingersoll *		      code and object code form. This patent license
6447e946eWyllys Ingersoll *		      shall apply to the combination of the Contribution
6547e946eWyllys Ingersoll *		      and the Program if, at the time the Contribution is
6647e946eWyllys Ingersoll *		      added by the Contributor, such addition of the
6747e946eWyllys Ingersoll *		      Contribution causes such combination to be covered
6847e946eWyllys Ingersoll *		      by the Licensed Patents. The patent license shall
6947e946eWyllys Ingersoll *		      not apply to any other combinations which include
7047e946eWyllys Ingersoll *		      the Contribution. No hardware per se is licensed
7147e946eWyllys Ingersoll *		      hereunder.
7247e946eWyllys Ingersoll *
7347e946eWyllys Ingersoll *		      c) Recipient understands that although each
7447e946eWyllys Ingersoll *		      Contributor grants the licenses to its
7547e946eWyllys Ingersoll *		      Contributions set forth herein, no assurances are
7647e946eWyllys Ingersoll *		      provided by any Contributor that the Program does
7747e946eWyllys Ingersoll *		      not infringe the patent or other intellectual
7847e946eWyllys Ingersoll *		      property rights of any other entity. Each
7947e946eWyllys Ingersoll *		      Contributor disclaims any liability to Recipient
8047e946eWyllys Ingersoll *		      for claims brought by any other entity based on
8147e946eWyllys Ingersoll *		      infringement of intellectual property rights or
8247e946eWyllys Ingersoll *		      otherwise. As a condition to exercising the rights
8347e946eWyllys Ingersoll *		      and licenses granted hereunder, each Recipient
8447e946eWyllys Ingersoll *		      hereby assumes sole responsibility to secure any
8547e946eWyllys Ingersoll *		      other intellectual property rights needed, if any.
8647e946eWyllys Ingersoll *
8747e946eWyllys Ingersoll *		      For example, if a third party patent license is
8847e946eWyllys Ingersoll *		      required to allow Recipient to distribute the
8947e946eWyllys Ingersoll *		      Program, it is Recipient's responsibility to
9047e946eWyllys Ingersoll *		      acquire that license before distributing the
9147e946eWyllys Ingersoll *		      Program.
9247e946eWyllys Ingersoll *
9347e946eWyllys Ingersoll *		      d) Each Contributor represents that to its
9447e946eWyllys Ingersoll *		      knowledge it has sufficient copyright rights in its
9547e946eWyllys Ingersoll *		      Contribution, if any, to grant the copyright
9647e946eWyllys Ingersoll *		      license set forth in this Agreement.
9747e946eWyllys Ingersoll *
9847e946eWyllys Ingersoll *		3. REQUIREMENTS
9947e946eWyllys Ingersoll *
10047e946eWyllys Ingersoll *		A Contributor may choose to distribute the Program in
10147e946eWyllys Ingersoll *		object code form under its own license agreement, provided
10247e946eWyllys Ingersoll *		that:
10347e946eWyllys Ingersoll *		      a) it complies with the terms and conditions of
10447e946eWyllys Ingersoll *		      this Agreement; and
10547e946eWyllys Ingersoll *
10647e946eWyllys Ingersoll *		      b) its license agreement:
10747e946eWyllys Ingersoll *		      i) effectively disclaims on behalf of all
10847e946eWyllys Ingersoll *		      Contributors all warranties and conditions, express
10947e946eWyllys Ingersoll *		      and implied, including warranties or conditions of
11047e946eWyllys Ingersoll *		      title and no - infringement, and implied warranties
11147e946eWyllys Ingersoll *		      or conditions of merchantability and fitness for a
11247e946eWyllys Ingersoll *		      particular purpose;
11347e946eWyllys Ingersoll *
11447e946eWyllys Ingersoll *		      ii) effectively excludes on behalf of all
11547e946eWyllys Ingersoll *		      Contributors all liability for damages, including
11647e946eWyllys Ingersoll *		      direct, indirect, special, incidental and
11747e946eWyllys Ingersoll *		      consequential damages, such as lost profits;
11847e946eWyllys Ingersoll *
11947e946eWyllys Ingersoll *		      iii) states that any provisions which differ from
12047e946eWyllys Ingersoll *		      this Agreement are offered by that Contributor
12147e946eWyllys Ingersoll *		      alone and not by any other party; and
12247e946eWyllys Ingersoll *
12347e946eWyllys Ingersoll *		      iv) states that source code for the Program is
12447e946eWyllys Ingersoll *		      available from such Contributor, and informs
12547e946eWyllys Ingersoll *		      licensees how to obtain it in a reasonable manner
12647e946eWyllys Ingersoll *		      on or through a medium customarily used for
12747e946eWyllys Ingersoll *		      software exchange.
12847e946eWyllys Ingersoll *
12947e946eWyllys Ingersoll *		When the Program is made available in source code form:
13047e946eWyllys Ingersoll *		      a) it must be made available under this Agreement;
13147e946eWyllys Ingersoll *		      and
13247e946eWyllys Ingersoll *		      b) a copy of this Agreement must be included with
13347e946eWyllys Ingersoll *		      each copy of the Program.
13447e946eWyllys Ingersoll *
13547e946eWyllys Ingersoll *		Contributors may not remove or alter any copyright notices
13647e946eWyllys Ingersoll *		contained within the Program.
13747e946eWyllys Ingersoll *
13847e946eWyllys Ingersoll *		Each Contributor must identify itself as the originator of
13947e946eWyllys Ingersoll *		its Contribution, if any, in a manner that reasonably
14047e946eWyllys Ingersoll *		allows subsequent Recipients to identify the originator of
14147e946eWyllys Ingersoll *		the Contribution.
14247e946eWyllys Ingersoll *
14347e946eWyllys Ingersoll *
14447e946eWyllys Ingersoll *		4. COMMERCIAL DISTRIBUTION
14547e946eWyllys Ingersoll *
14647e946eWyllys Ingersoll *		Commercial distributors of software may accept certain
14747e946eWyllys Ingersoll *		responsibilities with respect to end users, business
14847e946eWyllys Ingersoll *		partners and the like. While this license is intended to
14947e946eWyllys Ingersoll *		facilitate the commercial use of the Program, the
15047e946eWyllys Ingersoll *		Contributor who includes the Program in a commercial
15147e946eWyllys Ingersoll *		product offering should do so in a manner which does not
15247e946eWyllys Ingersoll *		create potential liability for other Contributors.
15347e946eWyllys Ingersoll *		Therefore, if a Contributor includes the Program in a
15447e946eWyllys Ingersoll *		commercial product offering, such Contributor ("Commercial
15547e946eWyllys Ingersoll *		Contributor") hereby agrees to defend and indemnify every
15647e946eWyllys Ingersoll *		other Contributor ("Indemnified Contributor") against any
15747e946eWyllys Ingersoll *		losses, damages and costs (collectively "Losses") arising
15847e946eWyllys Ingersoll *		from claims, lawsuits and other legal actions brought by a
15947e946eWyllys Ingersoll *		third party against the Indemnified Contributor to the
16047e946eWyllys Ingersoll *		extent caused by the acts or omissions of such Commercial
16147e946eWyllys Ingersoll *		Contributor in connection with its distribution of the
16247e946eWyllys Ingersoll *		Program in a commercial product offering. The obligations
16347e946eWyllys Ingersoll *		in this section do not apply to any claims or Losses
16447e946eWyllys Ingersoll *		relating to any actual or alleged intellectual property
16547e946eWyllys Ingersoll *		infringement. In order to qualify, an Indemnified
16647e946eWyllys Ingersoll *		Contributor must: a) promptly notify the Commercial
16747e946eWyllys Ingersoll *		Contributor in writing of such claim, and b) allow the
16847e946eWyllys Ingersoll *		Commercial Contributor to control, and cooperate with the
16947e946eWyllys Ingersoll *		Commercial Contributor in, the defense and any related
17047e946eWyllys Ingersoll *		settlement negotiations. The Indemnified Contributor may
17147e946eWyllys Ingersoll *		participate in any such claim at its own expense.
17247e946eWyllys Ingersoll *
17347e946eWyllys Ingersoll *
17447e946eWyllys Ingersoll *		For example, a Contributor might include the Program in a
17547e946eWyllys Ingersoll *		commercial product offering, Product X. That Contributor
17647e946eWyllys Ingersoll *		is then a Commercial Contributor. If that Commercial
17747e946eWyllys Ingersoll *		Contributor then makes performance claims, or offers
17847e946eWyllys Ingersoll *		warranties related to Product X, those performance claims
17947e946eWyllys Ingersoll *		and warranties are such Commercial Contributor's
18047e946eWyllys Ingersoll *		responsibility alone. Under this section, the Commercial
18147e946eWyllys Ingersoll *		Contributor would have to defend claims against the other
18247e946eWyllys Ingersoll *		Contributors related to those performance claims and
18347e946eWyllys Ingersoll *		warranties, and if a court requires any other Contributor
18447e946eWyllys Ingersoll *		to pay any damages as a result, the Commercial Contributor
18547e946eWyllys Ingersoll *		must pay those damages.
18647e946eWyllys Ingersoll *
18747e946eWyllys Ingersoll *
18847e946eWyllys Ingersoll *		5. NO WARRANTY
18947e946eWyllys Ingersoll *
19147e946eWyllys Ingersoll *		PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT
19547e946eWyllys Ingersoll *		FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely
19647e946eWyllys Ingersoll *		responsible for determining the appropriateness of using
19747e946eWyllys Ingersoll *		and distributing the Program and assumes all risks
19847e946eWyllys Ingersoll *		associated with its exercise of rights under this
19947e946eWyllys Ingersoll *		Agreement, including but not limited to the risks and
20047e946eWyllys Ingersoll *		costs of program errors, compliance with applicable laws,
20147e946eWyllys Ingersoll *		damage to or loss of data, programs or equipment, and
20247e946eWyllys Ingersoll *		unavailability or interruption of operations.
20347e946eWyllys Ingersoll *
20447e946eWyllys Ingersoll *		6. DISCLAIMER OF LIABILITY
21447e946eWyllys Ingersoll *		POSSIBILITY OF SUCH DAMAGES.
21547e946eWyllys Ingersoll *
21647e946eWyllys Ingersoll *		7. GENERAL
21747e946eWyllys Ingersoll *
21847e946eWyllys Ingersoll *		If any provision of this Agreement is invalid or
21947e946eWyllys Ingersoll *		unenforceable under applicable law, it shall not affect
22047e946eWyllys Ingersoll *		the validity or enforceability of the remainder of the
22147e946eWyllys Ingersoll *		terms of this Agreement, and without further action by the
22247e946eWyllys Ingersoll *		parties hereto, such provision shall be reformed to the
22347e946eWyllys Ingersoll *		minimum extent necessary to make such provision valid and
22447e946eWyllys Ingersoll *		enforceable.
22547e946eWyllys Ingersoll *
22647e946eWyllys Ingersoll *
22747e946eWyllys Ingersoll *		If Recipient institutes patent litigation against a
22847e946eWyllys Ingersoll *		Contributor with respect to a patent applicable to
22947e946eWyllys Ingersoll *		software (including a cros - claim or counterclaim in a
23047e946eWyllys Ingersoll *		lawsuit), then any patent licenses granted by that
23147e946eWyllys Ingersoll *		Contributor to such Recipient under this Agreement shall
23247e946eWyllys Ingersoll *		terminate as of the date such litigation is filed. In
23347e946eWyllys Ingersoll *		addition, If Recipient institutes patent litigation
23447e946eWyllys Ingersoll *		against any entity (including a cros - claim or
23547e946eWyllys Ingersoll *		counterclaim in a lawsuit) alleging that the Program
23647e946eWyllys Ingersoll *		itself (excluding combinations of the Program with other
23747e946eWyllys Ingersoll *		software or hardware) infringes such Recipient's
23847e946eWyllys Ingersoll *		patent(s), then such Recipient's rights granted under
23947e946eWyllys Ingersoll *		Section 2(b) shall terminate as of the date such
24047e946eWyllys Ingersoll *		litigation is filed.
24147e946eWyllys Ingersoll *
24247e946eWyllys Ingersoll *		All Recipient's rights under this Agreement shall
24347e946eWyllys Ingersoll *		terminate if it fails to comply with any of the material
24447e946eWyllys Ingersoll *		terms or conditions of this Agreement and does not cure
24547e946eWyllys Ingersoll *		such failure in a reasonable period of time after becoming
24647e946eWyllys Ingersoll *		aware of such noncompliance. If all Recipient's rights
24747e946eWyllys Ingersoll *		under this Agreement terminate, Recipient agrees to cease
24847e946eWyllys Ingersoll *		use and distribution of the Program as soon as reasonably
24947e946eWyllys Ingersoll *		practicable. However, Recipient's obligations under this
25047e946eWyllys Ingersoll *		Agreement and any licenses granted by Recipient relating
25147e946eWyllys Ingersoll *		to the Program shall continue and survive.
25247e946eWyllys Ingersoll *
25347e946eWyllys Ingersoll *		Everyone is permitted to copy and distribute copies of
25447e946eWyllys Ingersoll *		this Agreement, but in order to avoid inconsistency the
25547e946eWyllys Ingersoll *		Agreement is copyrighted and may only be modified in the
25647e946eWyllys Ingersoll *		following manner. The Agreement Steward reserves the right
25747e946eWyllys Ingersoll *		to publish new versions (including revisions) of this
25847e946eWyllys Ingersoll *		Agreement from time to time. No one other than the
25947e946eWyllys Ingersoll *		Agreement Steward has the right to modify this Agreement.
26047e946eWyllys Ingersoll *
26147e946eWyllys Ingersoll *		IBM is the initial Agreement Steward. IBM may assign the
26247e946eWyllys Ingersoll *		responsibility to serve as the Agreement Steward to a
26347e946eWyllys Ingersoll *		suitable separate entity. Each new version of the
26447e946eWyllys Ingersoll *		Agreement will be given a distinguishing version number.
26547e946eWyllys Ingersoll *		The Program (including Contributions) may always be
26647e946eWyllys Ingersoll *		distributed subject to the version of the Agreement under
26747e946eWyllys Ingersoll *		which it was received. In addition, after a new version of
26847e946eWyllys Ingersoll *		the Agreement is published, Contributor may elect to
26947e946eWyllys Ingersoll *		distribute the Program (including its Contributions) under
27047e946eWyllys Ingersoll *		the new version. Except as expressly stated in Sections
27147e946eWyllys Ingersoll *		2(a) and 2(b) above, Recipient receives no rights or
27247e946eWyllys Ingersoll *		licenses to the intellectual property of any Contributor
27347e946eWyllys Ingersoll *		under this Agreement, whether expressly, by implication,
27447e946eWyllys Ingersoll *		estoppel or otherwise. All rights in the Program not
27547e946eWyllys Ingersoll *		expressly granted under this Agreement are reserved.
27647e946eWyllys Ingersoll *
27747e946eWyllys Ingersoll *
27847e946eWyllys Ingersoll *		This Agreement is governed by the laws of the State of New
27947e946eWyllys Ingersoll *		York and the intellectual property laws of the United
28047e946eWyllys Ingersoll *		States of America. No party to this Agreement will bring a
28147e946eWyllys Ingersoll *		legal action under this Agreement more than one year after
28247e946eWyllys Ingersoll *		the cause of action arose. Each party waives its rights to
28347e946eWyllys Ingersoll *		a jury trial in any resulting litigation.
28447e946eWyllys Ingersoll *
28547e946eWyllys Ingersoll *
28647e946eWyllys Ingersoll *
28747e946eWyllys Ingersoll * (C) COPYRIGHT International Business Machines Corp. 2001, 2002
28847e946eWyllys Ingersoll */
28947e946eWyllys Ingersoll/*
29047e946eWyllys Ingersoll * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
29147e946eWyllys Ingersoll * Use is subject to license terms.
29247e946eWyllys Ingersoll */
29347e946eWyllys Ingersoll
29447e946eWyllys Ingersoll#include "tpmtok_int.h"
29547e946eWyllys Ingersoll
29647e946eWyllys Ingersoll// template_add_attributes()
29747e946eWyllys Ingersoll//
29847e946eWyllys Ingersoll// blindly add the given attributes to the template.  do no sanity checking
29947e946eWyllys Ingersoll// at this point.  sanity checking will occur later.
30047e946eWyllys Ingersoll//
30147e946eWyllys IngersollCK_RV
30247e946eWyllys Ingersolltemplate_add_attributes(TEMPLATE	* tmpl,
30347e946eWyllys Ingersoll	CK_ATTRIBUTE * pTemplate,
30447e946eWyllys Ingersoll	CK_ULONG	ulCount)
30547e946eWyllys Ingersoll{
30647e946eWyllys Ingersoll	CK_ATTRIBUTE  * attr = NULL;
30747e946eWyllys Ingersoll	CK_RV	   rc;
30847e946eWyllys Ingersoll	unsigned int    i;
30947e946eWyllys Ingersoll
31047e946eWyllys Ingersoll	for (i = 0; i < ulCount; i++) {
31147e946eWyllys Ingersoll		if (! is_attribute_defined(pTemplate[i].type)) {
31247e946eWyllys Ingersoll			return (CKR_ATTRIBUTE_TYPE_INVALID);
31347e946eWyllys Ingersoll		}
31447e946eWyllys Ingersoll		attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
31547e946eWyllys Ingersoll		    pTemplate[i].ulValueLen);
31647e946eWyllys Ingersoll		if (! attr) {
31747e946eWyllys Ingersoll			return (CKR_HOST_MEMORY);
31847e946eWyllys Ingersoll		}
31947e946eWyllys Ingersoll		attr->type	= pTemplate[i].type;
32047e946eWyllys Ingersoll		attr->ulValueLen = pTemplate[i].ulValueLen;
32147e946eWyllys Ingersoll
32247e946eWyllys Ingersoll		if (attr->ulValueLen != 0) {
32347e946eWyllys Ingersoll			attr->pValue = (CK_BYTE *)attr + sizeof (CK_ATTRIBUTE);
32447e946eWyllys Ingersoll			(void) memcpy(attr->pValue, pTemplate[i].pValue,
32547e946eWyllys Ingersoll			    attr->ulValueLen);
32647e946eWyllys Ingersoll		} else
32747e946eWyllys Ingersoll			attr->pValue = NULL;
32847e946eWyllys Ingersoll
32947e946eWyllys Ingersoll		rc = template_update_attribute(tmpl, attr);
33047e946eWyllys Ingersoll		if (rc != CKR_OK) {
33147e946eWyllys Ingersoll			free(attr);
33247e946eWyllys Ingersoll			return (rc);
33347e946eWyllys Ingersoll		}
33447e946eWyllys Ingersoll	}
33547e946eWyllys Ingersoll
33647e946eWyllys Ingersoll	return (CKR_OK);
33747e946eWyllys Ingersoll}
33847e946eWyllys Ingersoll
33947e946eWyllys Ingersoll
34047e946eWyllys Ingersoll// template_add_default_attributes()
34147e946eWyllys Ingersoll//
34247e946eWyllys IngersollCK_RV
34347e946eWyllys Ingersolltemplate_add_default_attributes(TEMPLATE * tmpl,
34447e946eWyllys Ingersoll	CK_ULONG   class,
34547e946eWyllys Ingersoll	CK_ULONG   subclass,
34647e946eWyllys Ingersoll	CK_ULONG   mode)
34747e946eWyllys Ingersoll{
34847e946eWyllys Ingersoll	CK_RV rc;
34947e946eWyllys Ingersoll
35047e946eWyllys Ingersoll	// first add the default common attributes
35147e946eWyllys Ingersoll	//
35247e946eWyllys Ingersoll	rc = template_set_default_common_attributes(tmpl);
35347e946eWyllys Ingersoll	if (rc != CKR_OK) {
35447e946eWyllys Ingersoll		return (rc);
35547e946eWyllys Ingersoll	}
35647e946eWyllys Ingersoll	// set the template class - specific default attributes
35747e946eWyllys Ingersoll	//
35847e946eWyllys Ingersoll	switch (class) {
35947e946eWyllys Ingersoll		case CKO_DATA:
36047e946eWyllys Ingersoll		return (data_object_set_default_attributes(tmpl, mode));
36147e946eWyllys Ingersoll		case CKO_CERTIFICATE:
36247e946eWyllys Ingersoll		if (subclass == CKC_X_509)
36347e946eWyllys Ingersoll			return (cert_x509_set_default_attributes(tmpl, mode));
36447e946eWyllys Ingersoll		else
36547e946eWyllys Ingersoll			return (CKR_OK);
36647e946eWyllys Ingersoll		case CKO_PUBLIC_KEY:
36747e946eWyllys Ingersoll		switch (subclass) {
36847e946eWyllys Ingersoll			case CKK_RSA:
36947e946eWyllys Ingersoll				return (rsa_publ_set_default_attributes(
37047e946eWyllys Ingersoll				    tmpl, mode));
37147e946eWyllys Ingersoll			default:
37247e946eWyllys Ingersoll				return (CKR_ATTRIBUTE_VALUE_INVALID);
37347e946eWyllys Ingersoll		}
37447e946eWyllys Ingersoll
37547e946eWyllys Ingersoll		case CKO_PRIVATE_KEY:
37647e946eWyllys Ingersoll		switch (subclass) {
37747e946eWyllys Ingersoll			case CKK_RSA:
37847e946eWyllys Ingersoll				return (rsa_priv_set_default_attributes(
37947e946eWyllys Ingersoll				    tmpl, mode));
38047e946eWyllys Ingersoll			default:
38147e946eWyllys Ingersoll				return (CKR_ATTRIBUTE_VALUE_INVALID);
38247e946eWyllys Ingersoll		}
38347e946eWyllys Ingersoll
38447e946eWyllys Ingersoll		case CKO_SECRET_KEY:
38547e946eWyllys Ingersoll		switch (subclass) {
38647e946eWyllys Ingersoll			case CKK_GENERIC_SECRET:
38747e946eWyllys Ingersoll				return (generic_secret_set_default_attributes(
38847e946eWyllys Ingersoll				    tmpl, mode));
38947e946eWyllys Ingersoll			default:
39047e946eWyllys Ingersoll				return (CKR_ATTRIBUTE_VALUE_INVALID);
39147e946eWyllys Ingersoll		}
39247e946eWyllys Ingersoll
39347e946eWyllys Ingersoll		case CKO_HW_FEATURE:
39447e946eWyllys Ingersoll		switch (subclass) {
39547e946eWyllys Ingersoll			case CKH_CLOCK:
39647e946eWyllys Ingersoll				return (clock_set_default_attributes(
39747e946eWyllys Ingersoll				    tmpl));
39847e946eWyllys Ingersoll			case CKH_MONOTONIC_COUNTER:
39947e946eWyllys Ingersoll				return (counter_set_default_attributes(
40047e946eWyllys Ingersoll				    tmpl));
40147e946eWyllys Ingersoll			default:
40247e946eWyllys Ingersoll				return (CKR_ATTRIBUTE_VALUE_INVALID);
40347e946eWyllys Ingersoll		}
40447e946eWyllys Ingersoll
40547e946eWyllys Ingersoll		case CKO_DOMAIN_PARAMETERS:
40647e946eWyllys Ingersoll		switch (subclass) {
40747e946eWyllys Ingersoll			default:
40847e946eWyllys Ingersoll				return (CKR_ATTRIBUTE_VALUE_INVALID);
40947e946eWyllys Ingersoll		}
41047e946eWyllys Ingersoll
41147e946eWyllys Ingersoll		default:
41247e946eWyllys Ingersoll			return (CKR_ATTRIBUTE_VALUE_INVALID);
41347e946eWyllys Ingersoll	}
41447e946eWyllys Ingersoll}
41547e946eWyllys Ingersoll
41647e946eWyllys Ingersoll// template_attribute_find()
41747e946eWyllys Ingersoll//
41847e946eWyllys Ingersoll// find the attribute in the list and return (its value
41947e946eWyllys Ingersoll//
42047e946eWyllys IngersollCK_BBOOL
42147e946eWyllys Ingersolltemplate_attribute_find(TEMPLATE	   * tmpl,
42247e946eWyllys Ingersoll	CK_ATTRIBUTE_TYPE    type,
42347e946eWyllys Ingersoll	CK_ATTRIBUTE	** attr)
42447e946eWyllys Ingersoll{
42547e946eWyllys Ingersoll	DL_NODE	* node = NULL;
42647e946eWyllys Ingersoll	CK_ATTRIBUTE * a    = NULL;
42747e946eWyllys Ingersoll
42847e946eWyllys Ingersoll	if (! tmpl || ! attr)
42947e946eWyllys Ingersoll		return (FALSE);
43047e946eWyllys Ingersoll	node = tmpl->attribute_list;
43147e946eWyllys Ingersoll
43247e946eWyllys Ingersoll	while (node != NULL) {
43347e946eWyllys Ingersoll		a = (CK_ATTRIBUTE *)node->data;
43447e946eWyllys Ingersoll
43547e946eWyllys Ingersoll		if (type == a->type) {
43647e946eWyllys Ingersoll			*attr = a;
43747e946eWyllys Ingersoll			return (TRUE);
43847e946eWyllys Ingersoll		}
43947e946eWyllys Ingersoll
44047e946eWyllys Ingersoll		node = node->next;
44147e946eWyllys Ingersoll	}
44247e946eWyllys Ingersoll
44347e946eWyllys Ingersoll	*attr = NULL;
44447e946eWyllys Ingersoll	return (FALSE);
44547e946eWyllys Ingersoll}
44647e946eWyllys Ingersoll
44747e946eWyllys Ingersoll// template_attribute_find_multiple()
44847e946eWyllys Ingersoll//
44947e946eWyllys Ingersoll// find the attributes in the list and return (their values
45047e946eWyllys Ingersoll//
45147e946eWyllys Ingersollvoid
45247e946eWyllys Ingersolltemplate_attribute_find_multiple(TEMPLATE		* tmpl,
45347e946eWyllys Ingersoll	ATTRIBUTE_PARSE_LIST * parselist,
45447e946eWyllys Ingersoll	CK_ULONG		plcount)
45547e946eWyllys Ingersoll{
45647e946eWyllys Ingersoll	CK_ATTRIBUTE  * attr = NULL;
45747e946eWyllys Ingersoll	CK_ULONG	i;
45847e946eWyllys Ingersoll
45947e946eWyllys Ingersoll	for (i = 0; i < plcount; i++) {
46047e946eWyllys Ingersoll		parselist[i].found = template_attribute_find(
46147e946eWyllys Ingersoll		    tmpl, parselist[i].type, &attr);
46247e946eWyllys Ingersoll
46347e946eWyllys Ingersoll		if (parselist[i].found && parselist[i].ptr != NULL)
46447e946eWyllys Ingersoll			(void) memcpy(parselist[i].ptr, attr->pValue,
46547e946eWyllys Ingersoll			    parselist[i].len);
46647e946eWyllys Ingersoll	}
46747e946eWyllys Ingersoll}
46847e946eWyllys Ingersoll
46947e946eWyllys IngersollCK_RV
47047e946eWyllys Ingersolltemplate_check_required_attributes(TEMPLATE  * tmpl,
47147e946eWyllys Ingersoll	CK_ULONG    class,
47247e946eWyllys Ingersoll	CK_ULONG    subclass,
47347e946eWyllys Ingersoll	CK_ULONG    mode)
47447e946eWyllys Ingersoll{
47547e946eWyllys Ingersoll	if (class == CKO_DATA)
47647e946eWyllys Ingersoll		return (data_object_check_required_attributes(
47747e946eWyllys Ingersoll		    tmpl, mode));
47847e946eWyllys Ingersoll	else if (class == CKO_CERTIFICATE) {
47947e946eWyllys Ingersoll		if (subclass == CKC_X_509)
48047e946eWyllys Ingersoll			return (cert_x509_check_required_attributes(
48147e946eWyllys Ingersoll			    tmpl, mode));
48247e946eWyllys Ingersoll		else
48347e946eWyllys Ingersoll			return (cert_vendor_check_required_attributes(
48447e946eWyllys Ingersoll			    tmpl, mode));
48547e946eWyllys Ingersoll	} else if (class == CKO_PUBLIC_KEY) {
48647e946eWyllys Ingersoll		switch (subclass) {
48747e946eWyllys Ingersoll			case CKK_RSA:
48847e946eWyllys Ingersoll				return (rsa_publ_check_required_attributes(
48947e946eWyllys Ingersoll				    tmpl, mode));
49047e946eWyllys Ingersoll			default:
49147e946eWyllys Ingersoll				return (CKR_ATTRIBUTE_VALUE_INVALID);
49247e946eWyllys Ingersoll		}
49347e946eWyllys Ingersoll	} else if (class == CKO_PRIVATE_KEY) {
49447e946eWyllys Ingersoll		switch (subclass) {
49547e946eWyllys Ingersoll			case CKK_RSA:
49647e946eWyllys Ingersoll				return (rsa_priv_check_required_attributes(
49747e946eWyllys Ingersoll				    tmpl, mode));
49847e946eWyllys Ingersoll			default:
49947e946eWyllys Ingersoll				return (CKR_ATTRIBUTE_VALUE_INVALID);
50047e946eWyllys Ingersoll		}
50147e946eWyllys Ingersoll	} else if (class == CKO_SECRET_KEY) {
50247e946eWyllys Ingersoll		switch (subclass) {
50347e946eWyllys Ingersoll			case CKK_GENERIC_SECRET:
50447e946eWyllys Ingersoll				return (
50547e946eWyllys Ingersoll				    generic_secret_check_required_attributes(
50647e946eWyllys Ingersoll				    tmpl, mode));
50747e946eWyllys Ingersoll			default:
50847e946eWyllys Ingersoll				return (CKR_ATTRIBUTE_VALUE_INVALID);
50947e946eWyllys Ingersoll		}
51047e946eWyllys Ingersoll	} else if (class == CKO_HW_FEATURE) {
51147e946eWyllys Ingersoll
51247e946eWyllys Ingersoll		switch (subclass) {
51347e946eWyllys Ingersoll			case CKH_CLOCK:
51447e946eWyllys Ingersoll				return (
51547e946eWyllys Ingersoll				    clock_check_required_attributes(
51647e946eWyllys Ingersoll				    tmpl, mode));
51747e946eWyllys Ingersoll			case CKH_MONOTONIC_COUNTER:
51847e946eWyllys Ingersoll				return (
51947e946eWyllys Ingersoll				    counter_check_required_attributes(
52047e946eWyllys Ingersoll				    tmpl, mode));
52147e946eWyllys Ingersoll			default:
52247e946eWyllys Ingersoll				return (CKR_ATTRIBUTE_VALUE_INVALID);
52347e946eWyllys Ingersoll		}
52447e946eWyllys Ingersoll	} else if (class == CKO_DOMAIN_PARAMETERS) {
52547e946eWyllys Ingersoll		switch (subclass) {
52647e946eWyllys Ingersoll			default:
52747e946eWyllys Ingersoll				return (CKR_ATTRIBUTE_VALUE_INVALID);
52847e946eWyllys Ingersoll		}
52947e946eWyllys Ingersoll	}
53047e946eWyllys Ingersoll	return (CKR_ATTRIBUTE_VALUE_INVALID);
53147e946eWyllys Ingersoll}
53247e946eWyllys Ingersoll
53347e946eWyllys Ingersoll
53447e946eWyllys Ingersoll// template_check_required_base_attributes()
53547e946eWyllys Ingersoll//
53647e946eWyllys Ingersoll// check to make sure that attributes required by Cryptoki are
53747e946eWyllys Ingersoll// present.  does not check to see if the attribute makes sense
53847e946eWyllys Ingersoll// for the particular object (that is done in the 'validate' routines)
53947e946eWyllys Ingersoll//
54047e946eWyllys IngersollCK_RV
54147e946eWyllys Ingersolltemplate_check_required_base_attributes(TEMPLATE * tmpl,
54247e946eWyllys IngersollCK_ULONG   mode)
54347e946eWyllys Ingersoll{
54447e946eWyllys Ingersoll	CK_ATTRIBUTE  * attr;
54547e946eWyllys Ingersoll	CK_BBOOL	found;
54647e946eWyllys Ingersoll
54747e946eWyllys Ingersoll	found = template_attribute_find(tmpl, CKA_CLASS, &attr);
54847e946eWyllys Ingersoll	if (mode == MODE_CREATE && found == FALSE)
54947e946eWyllys Ingersoll		return (CKR_TEMPLATE_INCOMPLETE);
55047e946eWyllys Ingersoll	return (CKR_OK);
55147e946eWyllys Ingersoll}
55247e946eWyllys Ingersoll
55347e946eWyllys Ingersoll
55447e946eWyllys Ingersoll// template_compare()
55547e946eWyllys Ingersoll//
55647e946eWyllys IngersollCK_BBOOL
55747e946eWyllys Ingersolltemplate_compare(CK_ATTRIBUTE  * t1,
55847e946eWyllys Ingersoll	CK_ULONG	ulCount,
55947e946eWyllys Ingersoll	TEMPLATE	* t2) {
56047e946eWyllys Ingersoll	CK_ATTRIBUTE  * attr1 = NULL;
56147e946eWyllys Ingersoll	CK_ATTRIBUTE  * attr2 = NULL;
56247e946eWyllys Ingersoll	CK_ULONG	i;
56347e946eWyllys Ingersoll	CK_RV	   rc;
56447e946eWyllys Ingersoll
56547e946eWyllys Ingersoll	if (! t1 || ! t2)
56647e946eWyllys Ingersoll		return (FALSE);
56747e946eWyllys Ingersoll	attr1 = t1;
56847e946eWyllys Ingersoll
56947e946eWyllys Ingersoll	for (i = 0; i < ulCount; i++) {
57047e946eWyllys Ingersoll		rc = template_attribute_find(t2, attr1->type, &attr2);
57147e946eWyllys Ingersoll		if (rc == FALSE)
57247e946eWyllys Ingersoll			return (FALSE);
57347e946eWyllys Ingersoll		if (attr1->ulValueLen != attr2->ulValueLen)
57447e946eWyllys Ingersoll			return (FALSE);
57547e946eWyllys Ingersoll		if (memcmp(attr1->pValue, attr2->pValue, attr1->ulValueLen)
57647e946eWyllys Ingersoll		    != 0)
57747e946eWyllys Ingersoll			return (FALSE);
57847e946eWyllys Ingersoll		attr1++;
57947e946eWyllys Ingersoll	}
58047e946eWyllys Ingersoll
58147e946eWyllys Ingersoll	return (TRUE);
58247e946eWyllys Ingersoll}
58347e946eWyllys Ingersoll
58447e946eWyllys Ingersoll// template_copy()
58547e946eWyllys Ingersoll//
58647e946eWyllys Ingersoll// This doesn't copy the template items verbatim.  The new template is in
58747e946eWyllys Ingersoll// the reverse order of the old one.  This should not have any effect.
58847e946eWyllys Ingersoll//
58947e946eWyllys Ingersoll// This is very similar to template_merge().  template_merge() can also
59047e946eWyllys Ingersoll// be used to copy a list (of unique attributes) but is slower because for
59147e946eWyllys Ingersoll// each attribute, it must search through the list.
59247e946eWyllys Ingersoll//
59347e946eWyllys IngersollCK_RV
59447e946eWyllys Ingersolltemplate_copy(TEMPLATE *dest, TEMPLATE *src) {
59547e946eWyllys Ingersoll	DL_NODE  *node;
59647e946eWyllys Ingersoll
59747e946eWyllys Ingersoll	if (! dest || ! src) {
59847e946eWyllys Ingersoll		return (CKR_FUNCTION_FAILED);
59947e946eWyllys Ingersoll	}
60047e946eWyllys Ingersoll	node = src->attribute_list;
60147e946eWyllys Ingersoll
60247e946eWyllys Ingersoll	while (node) {
60347e946eWyllys Ingersoll		CK_ATTRIBUTE *attr	= (CK_ATTRIBUTE *)node->data;
60447e946eWyllys Ingersoll		CK_ATTRIBUTE *new_attr = NULL;
60547e946eWyllys Ingersoll		CK_ULONG	len;
60647e946eWyllys Ingersoll
60747e946eWyllys Ingersoll		len = sizeof (CK_ATTRIBUTE) + attr->ulValueLen;
60847e946eWyllys Ingersoll
60947e946eWyllys Ingersoll		new_attr = (CK_ATTRIBUTE *)malloc(len);
61047e946eWyllys Ingersoll		if (! new_attr) {
61147e946eWyllys Ingersoll			return (CKR_HOST_MEMORY);
61247e946eWyllys Ingersoll		}
61347e946eWyllys Ingersoll		(void) memcpy(new_attr, attr, len);
61447e946eWyllys Ingersoll
61547e946eWyllys Ingersoll		new_attr->pValue = (CK_BYTE *)new_attr + sizeof (CK_ATTRIBUTE);
61647e946eWyllys Ingersoll
61747e946eWyllys Ingersoll		dest->attribute_list = dlist_add_as_first(dest->attribute_list,
61847e946eWyllys Ingersoll		    new_attr);
61947e946eWyllys Ingersoll
62047e946eWyllys Ingersoll		node = node->next;
62147e946eWyllys Ingersoll	}
62247e946eWyllys Ingersoll
62347e946eWyllys Ingersoll	return (CKR_OK);
62447e946eWyllys Ingersoll}
62547e946eWyllys Ingersoll
62647e946eWyllys Ingersoll
62747e946eWyllys Ingersoll// template_flatten() - this still gets used when saving token objects to disk
62847e946eWyllys Ingersoll//
62947e946eWyllys IngersollCK_RV
63047e946eWyllys Ingersolltemplate_flatten(TEMPLATE  * tmpl,
63147e946eWyllys Ingersoll	CK_BYTE   * dest)
63247e946eWyllys Ingersoll{
63347e946eWyllys Ingersoll	DL_NODE   * node = NULL;
63447e946eWyllys Ingersoll	CK_BYTE   * ptr = NULL;
63547e946eWyllys Ingersoll	CK_ULONG_32 long_len;
636e6fda97Wyllys Ingersoll	CK_ATTRIBUTE_32 *attr_32;
63747e946eWyllys Ingersoll	CK_ULONG    Val;
63847e946eWyllys Ingersoll	CK_ULONG_32 Val_32;
63947e946eWyllys Ingersoll	CK_ULONG  * pVal;
64047e946eWyllys Ingersoll	long_len = sizeof (CK_ULONG);
64147e946eWyllys Ingersoll
64247e946eWyllys Ingersoll	if (! tmpl || ! dest) {
64347e946eWyllys Ingersoll		return (CKR_FUNCTION_FAILED);
64447e946eWyllys Ingersoll	}
64547e946eWyllys Ingersoll	ptr = dest;
64647e946eWyllys Ingersoll	node = tmpl->attribute_list;
64747e946eWyllys Ingersoll	while (node) {
64847e946eWyllys Ingersoll		CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *)node->data;
64947e946eWyllys Ingersoll
65047e946eWyllys Ingersoll		if (long_len == 4) {
65147e946eWyllys Ingersoll			(void) memcpy(ptr, attr, sizeof (CK_ATTRIBUTE) +
65247e946eWyllys Ingersoll			    attr->ulValueLen);
65347e946eWyllys Ingersoll			ptr += sizeof (CK_ATTRIBUTE) + attr->ulValueLen;
65447e946eWyllys Ingersoll		} else {
65547e946eWyllys Ingersoll			attr_32 = malloc(sizeof (CK_ATTRIBUTE_32));
65647e946eWyllys Ingersoll			if (! attr_32) {
65747e946eWyllys Ingersoll				return (CKR_HOST_MEMORY);
65847e946eWyllys Ingersoll			}
65947e946eWyllys Ingersoll			attr_32->type = attr->type;
66047e946eWyllys Ingersoll			attr_32->pValue = 0x00;
66147e946eWyllys Ingersoll			if ((attr->type == CKA_CLASS ||
66247e946eWyllys Ingersoll			    attr->type == CKA_KEY_TYPE ||
66347e946eWyllys Ingersoll			    attr->type == CKA_MODULUS_BITS ||
66447e946eWyllys Ingersoll			    attr->type == CKA_VALUE_BITS ||
66547e946eWyllys Ingersoll			    attr->type == CKA_CERTIFICATE_TYPE ||
66647e946eWyllys Ingersoll			    attr->type == CKA_VALUE_LEN) &&
66747e946eWyllys Ingersoll			    attr->ulValueLen != 0) {
66847e946eWyllys Ingersoll
66947e946eWyllys Ingersoll				attr_32->ulValueLen = sizeof (CK_ULONG_32);
67047e946eWyllys Ingersoll
67147e946eWyllys Ingersoll				(void) memcpy(ptr, attr_32,
67247e946eWyllys Ingersoll				    sizeof (CK_ATTRIBUTE_32));
67347e946eWyllys Ingersoll				ptr += sizeof (CK_ATTRIBUTE_32);
67447e946eWyllys Ingersoll
67547e946eWyllys Ingersoll				pVal = (CK_ULONG *)attr->pValue;
67647e946eWyllys Ingersoll				Val = *pVal;
67747e946eWyllys Ingersoll				Val_32 = (CK_ULONG_32)Val;
67847e946eWyllys Ingersoll				(void) memcpy(ptr, &Val_32,
67947e946eWyllys Ingersoll				    sizeof (CK_ULONG_32));
68047e946eWyllys Ingersoll				ptr += sizeof (CK_ULONG_32);
68147e946eWyllys Ingersoll			} else {
68247e946eWyllys Ingersoll				attr_32->ulValueLen = attr->ulValueLen;
68347e946eWyllys Ingersoll				(void) memcpy(ptr, attr_32,
68447e946eWyllys Ingersoll				    sizeof (CK_ATTRIBUTE_32));
68547e946eWyllys Ingersoll				ptr += sizeof (CK_ATTRIBUTE_32);
68647e946eWyllys Ingersoll				if (attr->ulValueLen != 0) {
68747e946eWyllys Ingersoll					(void) memcpy(ptr, attr->pValue,
68847e946eWyllys Ingersoll					    attr->ulValueLen);
68947e946eWyllys Ingersoll					ptr += attr->ulValueLen;
69047e946eWyllys Ingersoll				}
69147e946eWyllys Ingersoll			}
692e6fda97Wyllys Ingersoll			free(attr_32);
69347e946eWyllys Ingersoll		}
69447e946eWyllys Ingersoll		node = node->next;
69547e946eWyllys Ingersoll	}
69647e946eWyllys Ingersoll
69747e946eWyllys Ingersoll	return (CKR_OK);
69847e946eWyllys Ingersoll}
69947e946eWyllys Ingersoll
70047e946eWyllys IngersollCK_RV
70147e946eWyllys Ingersolltemplate_unflatten(TEMPLATE ** new_tmpl,
70247e946eWyllys Ingersoll	CK_BYTE   * buf,
70347e946eWyllys Ingersoll	CK_ULONG    count)
70447e946eWyllys Ingersoll{
70547e946eWyllys Ingersoll	TEMPLATE	* tmpl = NULL;
70647e946eWyllys Ingersoll	CK_ATTRIBUTE  * a2   = NULL;
70747e946eWyllys Ingersoll	CK_BYTE	*ptr  = NULL;
70847e946eWyllys Ingersoll	CK_ULONG	i, len;
70947e946eWyllys Ingersoll	CK_RV	   rc;
71047e946eWyllys Ingersoll	CK_ULONG_32	long_len = sizeof (CK_ULONG);
71147e946eWyllys Ingersoll	CK_ULONG_32	attr_ulong_32;
71247e946eWyllys Ingersoll	CK_ULONG	attr_ulong;
71347e946eWyllys Ingersoll	CK_ATTRIBUTE * a1_64 = NULL;
71447e946eWyllys Ingersoll	CK_ATTRIBUTE_32 * a1 = NULL;
71547e946eWyllys Ingersoll
71647e946eWyllys Ingersoll	if (! new_tmpl || ! buf) {
71747e946eWyllys Ingersoll		return (CKR_FUNCTION_FAILED);
71847e946eWyllys Ingersoll	}
71947e946eWyllys Ingersoll	tmpl = (TEMPLATE *)malloc(sizeof (TEMPLATE));
72047e946eWyllys Ingersoll	if (! tmpl) {
72147e946eWyllys Ingersoll		return (CKR_HOST_MEMORY);
72247e946eWyllys Ingersoll	}
72347e946eWyllys Ingersoll	(void) memset(tmpl, 0x0, sizeof (TEMPLATE));
72447e946eWyllys Ingersoll
72547e946eWyllys Ingersoll	ptr = buf;
72647e946eWyllys Ingersoll	for (i = 0; i < count; i++) {
72747e946eWyllys Ingersoll		if (long_len == 4) {
72847e946eWyllys Ingersoll			void *aptr = ptr;
72947e946eWyllys Ingersoll			a1_64 = (CK_ATTRIBUTE *)aptr;
73047e946eWyllys Ingersoll
73147e946eWyllys Ingersoll			len = sizeof (CK_ATTRIBUTE) + a1_64->ulValueLen;
73247e946eWyllys Ingersoll			a2 = (CK_ATTRIBUTE *)malloc(len);
73347e946eWyllys Ingersoll			if (! a2) {
73447e946eWyllys Ingersoll				(void) template_free(tmpl);
73547e946eWyllys Ingersoll				return (CKR_HOST_MEMORY);
73647e946eWyllys Ingersoll			}
73747e946eWyllys Ingersoll
73847e946eWyllys Ingersoll			(void) memcpy(a2, a1_64, len);
73947e946eWyllys Ingersoll		} else {
74047e946eWyllys Ingersoll			void *aptr = ptr;
74147e946eWyllys Ingersoll			a1 = (CK_ATTRIBUTE_32 *)aptr;
74247e946eWyllys Ingersoll
74347e946eWyllys Ingersoll			if ((a1->type == CKA_CLASS ||
74447e946eWyllys Ingersoll			    a1->type == CKA_KEY_TYPE ||
74547e946eWyllys Ingersoll			    a1->type == CKA_MODULUS_BITS ||
74647e946eWyllys Ingersoll			    a1->type == CKA_VALUE_BITS ||
74747e946eWyllys Ingersoll			    a1->type == CKA_CERTIFICATE_TYPE ||
74847e946eWyllys Ingersoll			    a1->type == CKA_VALUE_LEN) &&
74947e946eWyllys Ingersoll			    a1->ulValueLen != 0) {
75047e946eWyllys Ingersoll				len = sizeof (CK_ATTRIBUTE) + sizeof (CK_ULONG);
75147e946eWyllys Ingersoll			} else {
75247e946eWyllys Ingersoll				len = sizeof (CK_ATTRIBUTE) + a1->ulValueLen;
75347e946eWyllys Ingersoll			}
75447e946eWyllys Ingersoll
75547e946eWyllys Ingersoll			a2 = (CK_ATTRIBUTE *)malloc(len);
75647e946eWyllys Ingersoll			if (! a2) {
75747e946eWyllys Ingersoll				return (CKR_HOST_MEMORY);
75847e946eWyllys Ingersoll			}
75947e946eWyllys Ingersoll			a2->type = a1->type;
76047e946eWyllys Ingersoll
76147e946eWyllys Ingersoll			if ((a1->type == CKA_CLASS ||
76247e946eWyllys Ingersoll			    a1->type == CKA_KEY_TYPE ||
76347e946eWyllys Ingersoll			    a1->type == CKA_MODULUS_BITS ||
76447e946eWyllys Ingersoll			    a1->type == CKA_VALUE_BITS ||
76547e946eWyllys Ingersoll			    a1->type == CKA_CERTIFICATE_TYPE ||
76647e946eWyllys Ingersoll			    a1->type == CKA_VALUE_LEN) &&
76747e946eWyllys Ingersoll			    a1->ulValueLen != 0) {
76847e946eWyllys Ingersoll				a2->ulValueLen = sizeof (CK_ULONG);
76947e946eWyllys Ingersoll				{
77047e946eWyllys Ingersoll					CK_ULONG_32 *p32;
77147e946eWyllys Ingersoll					CK_BYTE *pb2;
77247e946eWyllys Ingersoll					void *aptr = a1;
77347e946eWyllys Ingersoll					void *bptr;
77447e946eWyllys Ingersoll
77547e946eWyllys Ingersoll					pb2 = (CK_BYTE *)aptr;
77647e946eWyllys Ingersoll					pb2 += sizeof (CK_ATTRIBUTE_32);
77747e946eWyllys Ingersoll					bptr = pb2;
77847e946eWyllys Ingersoll					p32 = (CK_ULONG_32 *)bptr;
77947e946eWyllys Ingersoll					attr_ulong_32 = *p32;
78047e946eWyllys Ingersoll				}
78147e946eWyllys Ingersoll
78247e946eWyllys Ingersoll				attr_ulong = attr_ulong_32;
78347e946eWyllys Ingersoll
78447e946eWyllys Ingersoll				{
78547e946eWyllys Ingersoll					CK_BYTE *pb2;
78647e946eWyllys Ingersoll					pb2 = (CK_BYTE *)a2;
78747e946eWyllys Ingersoll					pb2 += sizeof (CK_ATTRIBUTE);
78847e946eWyllys Ingersoll					(void) memcpy(pb2,
78947e946eWyllys Ingersoll					    (CK_BYTE *)&attr_ulong,
79047e946eWyllys Ingersoll					    sizeof (CK_ULONG));
79147e946eWyllys Ingersoll				}
79247e946eWyllys Ingersoll			} else {
79347e946eWyllys Ingersoll				CK_BYTE *pb2, *pb;
79447e946eWyllys Ingersoll
79547e946eWyllys Ingersoll				a2->ulValueLen = a1->ulValueLen;
79647e946eWyllys Ingersoll				pb2 = (CK_BYTE *)a2;
79747e946eWyllys Ingersoll				pb2 += sizeof (CK_ATTRIBUTE);
79847e946eWyllys Ingersoll				pb = (CK_BYTE *)a1;
79947e946eWyllys Ingersoll				pb += sizeof (CK_ATTRIBUTE_32);
80047e946eWyllys Ingersoll				(void) memcpy(pb2, pb, a1->ulValueLen);
80147e946eWyllys Ingersoll			}
80247e946eWyllys Ingersoll		}
80347e946eWyllys Ingersoll
80447e946eWyllys Ingersoll
80547e946eWyllys Ingersoll		if (a2->ulValueLen != 0)
80647e946eWyllys Ingersoll			a2->pValue = (CK_BYTE *)a2 + sizeof (CK_ATTRIBUTE);
80747e946eWyllys Ingersoll		else
80847e946eWyllys Ingersoll			a2->pValue = NULL;
80947e946eWyllys Ingersoll
81047e946eWyllys Ingersoll		rc = template_update_attribute(tmpl, a2);
81147e946eWyllys Ingersoll		if (rc != CKR_OK) {
81247e946eWyllys Ingersoll			free(a2);
81347e946eWyllys Ingersoll			(void) template_free(tmpl);
81447e946eWyllys Ingersoll			return (rc);
81547e946eWyllys Ingersoll		}
81647e946eWyllys Ingersoll		if (long_len == 4)
81747e946eWyllys Ingersoll			ptr += len;
81847e946eWyllys Ingersoll		else
81947e946eWyllys Ingersoll			ptr += sizeof (CK_ATTRIBUTE_32) + a1->ulValueLen;
82047e946eWyllys Ingersoll
82147e946eWyllys Ingersoll
82247e946eWyllys Ingersoll	}
82347e946eWyllys Ingersoll
82447e946eWyllys Ingersoll		*new_tmpl = tmpl;
82547e946eWyllys Ingersoll	return (CKR_OK);
82647e946eWyllys Ingersoll}
82747e946eWyllys Ingersoll
82847e946eWyllys IngersollCK_RV
82947e946eWyllys Ingersolltemplate_free(TEMPLATE *tmpl) {
83047e946eWyllys Ingersoll	if (! tmpl)
83147e946eWyllys Ingersoll		return (CKR_OK);
83247e946eWyllys Ingersoll	while (tmpl->attribute_list) {
83347e946eWyllys Ingersoll		CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *)tmpl->attribute_list->data;
83447e946eWyllys Ingersoll
83547e946eWyllys Ingersoll		if (attr)
83647e946eWyllys Ingersoll			free(attr);
83747e946eWyllys Ingersoll
83847e946eWyllys Ingersoll		tmpl->attribute_list = dlist_remove_node(tmpl->attribute_list,
83947e946eWyllys Ingersoll		tmpl->attribute_list);
84047e946eWyllys Ingersoll	}
84147e946eWyllys Ingersoll
84247e946eWyllys Ingersoll	free(tmpl);
84347e946eWyllys Ingersoll	return (CKR_OK);
84447e946eWyllys Ingersoll}
84547e946eWyllys Ingersoll
84647e946eWyllys IngersollCK_BBOOL
84747e946eWyllys Ingersolltemplate_get_class(TEMPLATE  * tmpl,
84847e946eWyllys Ingersoll	CK_ULONG  * class,
84947e946eWyllys Ingersoll	CK_ULONG  * subclass)
85047e946eWyllys Ingersoll{
85147e946eWyllys Ingersoll	DL_NODE * node;
85247e946eWyllys Ingersoll	CK_BBOOL  found;
85347e946eWyllys Ingersoll
85447e946eWyllys Ingersoll	if (! tmpl || ! class || ! subclass)
85547e946eWyllys Ingersoll		return (FALSE);
85647e946eWyllys Ingersoll	node = tmpl->attribute_list;
85747e946eWyllys Ingersoll
85847e946eWyllys Ingersoll	while (node) {
85947e946eWyllys Ingersoll		CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *)node->data;
86047e946eWyllys Ingersoll
86147e946eWyllys Ingersoll		if (attr->type == CKA_CLASS) {
86247e946eWyllys Ingersoll			*class = *(CK_OBJECT_CLASS *)attr->pValue;
86347e946eWyllys Ingersoll			found = TRUE;
86447e946eWyllys Ingersoll		}
86547e946eWyllys Ingersoll
86647e946eWyllys Ingersoll		if (attr->type == CKA_CERTIFICATE_TYPE)
86747e946eWyllys Ingersoll			*subclass = *(CK_CERTIFICATE_TYPE *)attr->pValue;
86847e946eWyllys Ingersoll
86947e946eWyllys Ingersoll		if (attr->type == CKA_KEY_TYPE)
87047e946eWyllys Ingersoll			*subclass = *(CK_KEY_TYPE *)attr->pValue;
87147e946eWyllys Ingersoll
87247e946eWyllys Ingersoll		node = node->next;
87347e946eWyllys Ingersoll	}
87447e946eWyllys Ingersoll
87547e946eWyllys Ingersoll	return (found);
87647e946eWyllys Ingersoll}
87747e946eWyllys Ingersoll
87847e946eWyllys IngersollCK_ULONG
87947e946eWyllys Ingersolltemplate_get_count(TEMPLATE *tmpl)
88047e946eWyllys Ingersoll{
88147e946eWyllys Ingersoll	if (tmpl == NULL)
88247e946eWyllys Ingersoll		return (0);
88347e946eWyllys Ingersoll	return (dlist_length(tmpl->attribute_list));
88447e946eWyllys Ingersoll}
88547e946eWyllys Ingersoll
88647e946eWyllys IngersollCK_ULONG
88747e946eWyllys Ingersolltemplate_get_size(TEMPLATE *tmpl)
88847e946eWyllys Ingersoll{
88947e946eWyllys Ingersoll	DL_NODE   * node;
89047e946eWyllys Ingersoll	CK_ULONG    size = 0;
89147e946eWyllys Ingersoll
89247e946eWyllys Ingersoll	if (tmpl == NULL)
89347e946eWyllys Ingersoll		return (0);
89447e946eWyllys Ingersoll	node = tmpl->attribute_list;
89547e946eWyllys Ingersoll	while (node) {
89647e946eWyllys Ingersoll		CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *)node->data;
89747e946eWyllys Ingersoll
89847e946eWyllys Ingersoll		size += sizeof (CK_ATTRIBUTE) + attr->ulValueLen;
89947e946eWyllys Ingersoll
90047e946eWyllys Ingersoll		node = node->next;
90147e946eWyllys Ingersoll	}
90247e946eWyllys Ingersoll
90347e946eWyllys Ingersoll	return (size);
90447e946eWyllys Ingersoll}
90547e946eWyllys Ingersoll
90647e946eWyllys IngersollCK_ULONG
90747e946eWyllys Ingersolltemplate_get_compressed_size(TEMPLATE *tmpl)
90847e946eWyllys Ingersoll{
90947e946eWyllys Ingersoll	DL_NODE   * node;
91047e946eWyllys Ingersoll	CK_ULONG    size = 0;
91147e946eWyllys Ingersoll
91247e946eWyllys Ingersoll	if (tmpl == NULL)
91347e946eWyllys Ingersoll		return (0);
91447e946eWyllys Ingersoll	node = tmpl->attribute_list;
91547e946eWyllys Ingersoll	while (node) {
91647e946eWyllys Ingersoll		CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *)node->data;
91747e946eWyllys Ingersoll
91847e946eWyllys Ingersoll		size += sizeof (CK_ATTRIBUTE_32);
91947e946eWyllys Ingersoll		if ((attr->type == CKA_CLASS ||
92047e946eWyllys Ingersoll		    attr->type == CKA_KEY_TYPE ||
92147e946eWyllys Ingersoll		    attr->type == CKA_MODULUS_BITS ||
92247e946eWyllys Ingersoll		    attr->type == CKA_VALUE_BITS ||
92347e946eWyllys Ingersoll		    attr->type == CKA_CERTIFICATE_TYPE ||
92447e946eWyllys Ingersoll		    attr->type == CKA_VALUE_LEN) &&
92547e946eWyllys Ingersoll		    attr->ulValueLen != 0) {
92647e946eWyllys Ingersoll			size += sizeof (CK_ULONG_32);
92747e946eWyllys Ingersoll		} else {
92847e946eWyllys Ingersoll			size += attr->ulValueLen;
92947e946eWyllys Ingersoll		}
93047e946eWyllys Ingersoll
93147e946eWyllys Ingersoll		node = node->next;
93247e946eWyllys Ingersoll	}
93347e946eWyllys Ingersoll
93447e946eWyllys Ingersoll	return (size);
93547e946eWyllys Ingersoll}
93647e946eWyllys Ingersoll
93747e946eWyllys Ingersoll/*
93847e946eWyllys Ingersoll * template_is_okay_to_reveal_attribute()
93947e946eWyllys Ingersoll *
94047e946eWyllys Ingersoll * determines whether the specified CK_ATTRIBUTE_TYPE is allowed to
94147e946eWyllys Ingersoll * be leave the card in the clear.  note: the specified template doesn't need
94247e946eWyllys Ingersoll * to actually posess an attribute of type 'type'.  The template is
94347e946eWyllys Ingersoll * provided mainly to determine the object class and subclass
94447e946eWyllys Ingersoll *
94547e946eWyllys Ingersoll * this routine is called by C_GetAttributeValue which exports the attributes
94647e946eWyllys Ingersoll * in the clear.  this routine is NOT called when wrapping a key.
94747e946eWyllys Ingersoll */
94847e946eWyllys IngersollCK_BBOOL
94947e946eWyllys Ingersolltemplate_check_exportability(TEMPLATE *tmpl, CK_ATTRIBUTE_TYPE type)
95047e946eWyllys Ingersoll{
95147e946eWyllys Ingersoll	CK_ATTRIBUTE * attr = NULL;
95247e946eWyllys Ingersoll	CK_ULONG	class;
95347e946eWyllys Ingersoll	CK_ULONG	subclass;
95447e946eWyllys Ingersoll	CK_BBOOL	val;
95547e946eWyllys Ingersoll
95647e946eWyllys Ingersoll	if (! tmpl)
95747e946eWyllys Ingersoll		return (FALSE);
95847e946eWyllys Ingersoll	(void) template_get_class(tmpl, &class, &subclass);
95947e946eWyllys Ingersoll
96047e946eWyllys Ingersoll	if (class != CKO_PRIVATE_KEY && class != CKO_SECRET_KEY)
96147e946eWyllys Ingersoll		return (TRUE);
96247e946eWyllys Ingersoll	val = template_attribute_find(tmpl, CKA_SENSITIVE, &attr);
96347e946eWyllys Ingersoll	if (val) {
96447e946eWyllys Ingersoll		val = *(CK_BBOOL *)attr->pValue;
96547e946eWyllys Ingersoll		if (val == FALSE)
96647e946eWyllys Ingersoll			return (TRUE);
96747e946eWyllys Ingersoll	} else {
96847e946eWyllys Ingersoll		return (FALSE);
96947e946eWyllys Ingersoll	}
97047e946eWyllys Ingersoll
97147e946eWyllys Ingersoll	if (class == CKO_PRIVATE_KEY) {
97247e946eWyllys Ingersoll		switch (subclass) {
97347e946eWyllys Ingersoll			case CKK_RSA:
97447e946eWyllys Ingersoll				return (rsa_priv_check_exportability(type));
97547e946eWyllys Ingersoll			default:
97647e946eWyllys Ingersoll				return (CKR_ATTRIBUTE_VALUE_INVALID);
97747e946eWyllys Ingersoll		}
97847e946eWyllys Ingersoll	} else if (class == CKO_SECRET_KEY) {
97947e946eWyllys Ingersoll		return (secret_key_check_exportability(type));
98047e946eWyllys Ingersoll	}
98147e946eWyllys Ingersoll
98247e946eWyllys Ingersoll	return (CKR_ATTRIBUTE_VALUE_INVALID);
98347e946eWyllys Ingersoll}
98447e946eWyllys Ingersoll
98547e946eWyllys IngersollCK_RV
98647e946eWyllys Ingersolltemplate_merge(TEMPLATE *dest, TEMPLATE **src)
98747e946eWyllys Ingersoll{
98847e946eWyllys Ingersoll	DL_NODE  *node;
98947e946eWyllys Ingersoll	CK_RV	rc;
99047e946eWyllys Ingersoll
99147e946eWyllys Ingersoll	if (! dest || ! src) {
99247e946eWyllys Ingersoll		return (CKR_FUNCTION_FAILED);
99347e946eWyllys Ingersoll	}
99447e946eWyllys Ingersoll	node = (*src)->attribute_list;
99547e946eWyllys Ingersoll
99647e946eWyllys Ingersoll	while (node) {
99747e946eWyllys Ingersoll		CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *)node->data;
99847e946eWyllys Ingersoll
99947e946eWyllys Ingersoll		rc = template_update_attribute(dest, attr);
100047e946eWyllys Ingersoll		if (rc != CKR_OK) {
100147e946eWyllys Ingersoll			return (rc);
100247e946eWyllys Ingersoll		}
100347e946eWyllys Ingersoll		node->data = NULL;
100447e946eWyllys Ingersoll		node = node->next;
100547e946eWyllys Ingersoll	}
100647e946eWyllys Ingersoll
100747e946eWyllys Ingersoll	(void) template_free(*src);
100847e946eWyllys Ingersoll	*src = NULL;
100947e946eWyllys Ingersoll
101047e946eWyllys Ingersoll	return (CKR_OK);
101147e946eWyllys Ingersoll}
101247e946eWyllys Ingersoll
101347e946eWyllys Ingersoll/*
101447e946eWyllys Ingersoll * template_set_default_common_attributes()
101547e946eWyllys Ingersoll *
101647e946eWyllys Ingersoll * Set the default attributes common to all objects:
101747e946eWyllys Ingersoll *
101847e946eWyllys Ingersoll *    CKA_TOKEN	:  FALSE
101947e946eWyllys Ingersoll *    CKA_PRIVATE    :  TRUE -- Cryptoki leaves this up to the token to decide
102047e946eWyllys Ingersoll *    CKA_MODIFIABLE :  TRUE
102147e946eWyllys Ingersoll *    CKA_LABEL	:  empty string
102247e946eWyllys Ingersoll */
102347e946eWyllys IngersollCK_RV
102447e946eWyllys Ingersolltemplate_set_default_common_attributes(TEMPLATE *tmpl)
102547e946eWyllys Ingersoll{
102647e946eWyllys Ingersoll	CK_ATTRIBUTE * token_attr;
102747e946eWyllys Ingersoll	CK_ATTRIBUTE * priv_attr;
102847e946eWyllys Ingersoll	CK_ATTRIBUTE * mod_attr;
102947e946eWyllys Ingersoll	CK_ATTRIBUTE * label_attr;
103047e946eWyllys Ingersoll
103147e946eWyllys Ingersoll	token_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
103247e946eWyllys Ingersoll	    sizeof (CK_BBOOL));
103347e946eWyllys Ingersoll	priv_attr  = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
103447e946eWyllys Ingersoll	    sizeof (CK_BBOOL));
103547e946eWyllys Ingersoll	mod_attr   = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
103647e946eWyllys Ingersoll	    sizeof (CK_BBOOL));
103747e946eWyllys Ingersoll	label_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) + 0);
103847e946eWyllys Ingersoll
103947e946eWyllys Ingersoll	if (! token_attr || ! priv_attr || ! mod_attr || ! label_attr) {
104047e946eWyllys Ingersoll		if (token_attr) free(token_attr);
104147e946eWyllys Ingersoll		if (priv_attr)  free(priv_attr);
104247e946eWyllys Ingersoll		if (mod_attr)   free(mod_attr);
104347e946eWyllys Ingersoll		if (label_attr) free(label_attr);
104447e946eWyllys Ingersoll
104547e946eWyllys Ingersoll		return (CKR_HOST_MEMORY);
104647e946eWyllys Ingersoll	}
104747e946eWyllys Ingersoll
104847e946eWyllys Ingersoll	token_attr->type	 = CKA_TOKEN;
104947e946eWyllys Ingersoll	token_attr->ulValueLen   = sizeof (CK_BBOOL);
105047e946eWyllys Ingersoll	token_attr->pValue	= (CK_BYTE *)token_attr + sizeof (CK_ATTRIBUTE);
105147e946eWyllys Ingersoll	*(CK_BBOOL *)token_attr->pValue = FALSE;
105247e946eWyllys Ingersoll
105347e946eWyllys Ingersoll	priv_attr->type	  = CKA_PRIVATE;
105447e946eWyllys Ingersoll	priv_attr->ulValueLen    = sizeof (CK_BBOOL);
105547e946eWyllys Ingersoll	priv_attr->pValue	= (CK_BYTE *)priv_attr + sizeof (CK_ATTRIBUTE);
105647e946eWyllys Ingersoll	*(CK_BBOOL *)priv_attr->pValue = FALSE;
105747e946eWyllys Ingersoll
105847e946eWyllys Ingersoll	mod_attr->type	   = CKA_MODIFIABLE;
105947e946eWyllys Ingersoll	mod_attr->ulValueLen	= sizeof (CK_BBOOL);
106047e946eWyllys Ingersoll	mod_attr->pValue	 = (CK_BYTE *)mod_attr + sizeof (CK_ATTRIBUTE);
106147e946eWyllys Ingersoll	*(CK_BBOOL *)mod_attr->pValue = TRUE;
106247e946eWyllys Ingersoll
106347e946eWyllys Ingersoll	label_attr->type	 = CKA_LABEL;
106447e946eWyllys Ingersoll	label_attr->ulValueLen   = 0;	 // empty string
106547e946eWyllys Ingersoll	label_attr->pValue	= NULL;
106647e946eWyllys Ingersoll
106747e946eWyllys Ingersoll	(void) template_update_attribute(tmpl, token_attr);
106847e946eWyllys Ingersoll	(void) template_update_attribute(tmpl, priv_attr);
106947e946eWyllys Ingersoll	(void) template_update_attribute(tmpl, mod_attr);
107047e946eWyllys Ingersoll	(void) template_update_attribute(tmpl, label_attr);
107147e946eWyllys Ingersoll
107247e946eWyllys Ingersoll	return (CKR_OK);
107347e946eWyllys Ingersoll}
107447e946eWyllys Ingersoll
107547e946eWyllys IngersollCK_RV
107647e946eWyllys Ingersolltemplate_update_attribute(TEMPLATE *tmpl, CK_ATTRIBUTE *new_attr) {
107747e946eWyllys Ingersoll	DL_NODE	* node = NULL;
107847e946eWyllys Ingersoll	CK_ATTRIBUTE * attr = NULL;
107947e946eWyllys Ingersoll
108047e946eWyllys Ingersoll	if (! tmpl || ! new_attr) {
108147e946eWyllys Ingersoll		return (CKR_FUNCTION_FAILED);
108247e946eWyllys Ingersoll	}
108347e946eWyllys Ingersoll	node = tmpl->attribute_list;
108447e946eWyllys Ingersoll
108547e946eWyllys Ingersoll	while (node != NULL) {
108647e946eWyllys Ingersoll		attr = (CK_ATTRIBUTE *)node->data;
108747e946eWyllys Ingersoll
108847e946eWyllys Ingersoll		if (new_attr->type == attr->type) {
108947e946eWyllys Ingersoll			free(attr);
109047e946eWyllys Ingersoll			tmpl->attribute_list = dlist_remove_node(
109147e946eWyllys Ingersoll			    tmpl->attribute_list, node);
109247e946eWyllys Ingersoll			break;
109347e946eWyllys Ingersoll		}
109447e946eWyllys Ingersoll
109547e946eWyllys Ingersoll		node = node->next;
109647e946eWyllys Ingersoll	}
109747e946eWyllys Ingersoll
109847e946eWyllys Ingersoll	tmpl->attribute_list = dlist_add_as_first(
109947e946eWyllys Ingersoll	    tmpl->attribute_list, new_attr);
110047e946eWyllys Ingersoll
110147e946eWyllys Ingersoll	return (CKR_OK);
110247e946eWyllys Ingersoll}
110347e946eWyllys Ingersoll
110447e946eWyllys IngersollCK_RV
110547e946eWyllys Ingersolltemplate_validate_attribute(TEMPLATE	* tmpl,
110647e946eWyllys Ingersoll	CK_ATTRIBUTE * attr,
110747e946eWyllys Ingersoll	CK_ULONG	class,
110847e946eWyllys Ingersoll	CK_ULONG	subclass,
110947e946eWyllys Ingersoll	CK_ULONG	mode)
111047e946eWyllys Ingersoll{
111147e946eWyllys Ingersoll	if (class == CKO_DATA)
111247e946eWyllys Ingersoll		return (data_object_validate_attribute(tmpl, attr, mode));
111347e946eWyllys Ingersoll	else if (class == CKO_CERTIFICATE) {
111447e946eWyllys Ingersoll		if (subclass == CKC_X_509)
111547e946eWyllys Ingersoll			return (cert_x509_validate_attribute(tmpl, attr, mode));
111647e946eWyllys Ingersoll		else
111747e946eWyllys Ingersoll			return (cert_vendor_validate_attribute(tmpl,
111847e946eWyllys Ingersoll			    attr, mode));
111947e946eWyllys Ingersoll	} else if (class == CKO_PUBLIC_KEY) {
112047e946eWyllys Ingersoll
112147e946eWyllys Ingersoll		switch (subclass) {
112247e946eWyllys Ingersoll			case CKK_RSA:
112347e946eWyllys Ingersoll				return (rsa_publ_validate_attribute(tmpl,
112447e946eWyllys Ingersoll				    attr, mode));
112547e946eWyllys Ingersoll			default:
112647e946eWyllys Ingersoll				return (CKR_ATTRIBUTE_VALUE_INVALID);
112747e946eWyllys Ingersoll		}
112847e946eWyllys Ingersoll	} else if (class == CKO_PRIVATE_KEY) {
112947e946eWyllys Ingersoll
113047e946eWyllys Ingersoll		switch (subclass) {
113147e946eWyllys Ingersoll			case CKK_RSA:
113247e946eWyllys Ingersoll				return (rsa_priv_validate_attribute(tmpl,
113347e946eWyllys Ingersoll				    attr, mode));
113447e946eWyllys Ingersoll			default:
113547e946eWyllys Ingersoll				return (CKR_ATTRIBUTE_VALUE_INVALID);
113647e946eWyllys Ingersoll		}
113747e946eWyllys Ingersoll	} else if (class == CKO_SECRET_KEY) {
113847e946eWyllys Ingersoll		switch (subclass) {
113947e946eWyllys Ingersoll			case CKK_GENERIC_SECRET:
114047e946eWyllys Ingersoll				return (generic_secret_validate_attribute(tmpl,
114147e946eWyllys Ingersoll				    attr, mode));
114247e946eWyllys Ingersoll			default:
114347e946eWyllys Ingersoll				return (CKR_ATTRIBUTE_VALUE_INVALID);
114447e946eWyllys Ingersoll			}
114547e946eWyllys Ingersoll		} else if (class == CKO_HW_FEATURE) {
114647e946eWyllys Ingersoll
114747e946eWyllys Ingersoll		switch (subclass) {
114847e946eWyllys Ingersoll			case CKH_CLOCK:
114947e946eWyllys Ingersoll				return (clock_validate_attribute(tmpl,
115047e946eWyllys Ingersoll				    attr, mode));
115147e946eWyllys Ingersoll			case CKH_MONOTONIC_COUNTER:
115247e946eWyllys Ingersoll				return (counter_validate_attribute(tmpl,
115347e946eWyllys Ingersoll				    attr, mode));
115447e946eWyllys Ingersoll			default:
115547e946eWyllys Ingersoll				return (CKR_ATTRIBUTE_VALUE_INVALID);
115647e946eWyllys Ingersoll		}
115747e946eWyllys Ingersoll	} else if (class == CKO_DOMAIN_PARAMETERS) {
115847e946eWyllys Ingersoll		switch (subclass) {
115947e946eWyllys Ingersoll			default:
116047e946eWyllys Ingersoll				return (CKR_ATTRIBUTE_VALUE_INVALID);
116147e946eWyllys Ingersoll		}
116247e946eWyllys Ingersoll	}
116347e946eWyllys Ingersoll	return (CKR_ATTRIBUTE_VALUE_INVALID);
116447e946eWyllys Ingersoll}
116547e946eWyllys Ingersoll
116647e946eWyllys IngersollCK_RV
116747e946eWyllys Ingersolltemplate_validate_attributes(TEMPLATE * tmpl,
116847e946eWyllys Ingersoll	CK_ULONG   class,
116947e946eWyllys Ingersoll	CK_ULONG   subclass,
117047e946eWyllys Ingersoll	CK_ULONG   mode)
117147e946eWyllys Ingersoll{
117247e946eWyllys Ingersoll	DL_NODE	*node;
117347e946eWyllys Ingersoll	CK_RV	rc = CKR_OK;
117447e946eWyllys Ingersoll
117547e946eWyllys Ingersoll	node = tmpl->attribute_list;
117647e946eWyllys Ingersoll
117747e946eWyllys Ingersoll	while (node) {
117847e946eWyllys Ingersoll		CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *)node->data;
117947e946eWyllys Ingersoll
118047e946eWyllys Ingersoll		rc = template_validate_attribute(tmpl, attr, class,
118147e946eWyllys Ingersoll		    subclass, mode);
118247e946eWyllys Ingersoll		if (rc != CKR_OK) {
118347e946eWyllys Ingersoll			return (rc);
118447e946eWyllys Ingersoll		}
118547e946eWyllys Ingersoll		node = node->next;
118647e946eWyllys Ingersoll	}
118747e946eWyllys Ingersoll
118847e946eWyllys Ingersoll	return (CKR_OK);
118947e946eWyllys Ingersoll}
119047e946eWyllys Ingersoll