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