1*9126SWyllys.Ingersoll@Sun.COM /*
2*9126SWyllys.Ingersoll@Sun.COM * Common Public License Version 0.5
3*9126SWyllys.Ingersoll@Sun.COM *
4*9126SWyllys.Ingersoll@Sun.COM * THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF
5*9126SWyllys.Ingersoll@Sun.COM * THIS COMMON PUBLIC LICENSE ("AGREEMENT"). ANY USE,
6*9126SWyllys.Ingersoll@Sun.COM * REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
7*9126SWyllys.Ingersoll@Sun.COM * RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
8*9126SWyllys.Ingersoll@Sun.COM *
9*9126SWyllys.Ingersoll@Sun.COM * 1. DEFINITIONS
10*9126SWyllys.Ingersoll@Sun.COM *
11*9126SWyllys.Ingersoll@Sun.COM * "Contribution" means:
12*9126SWyllys.Ingersoll@Sun.COM * a) in the case of the initial Contributor, the
13*9126SWyllys.Ingersoll@Sun.COM * initial code and documentation distributed under
14*9126SWyllys.Ingersoll@Sun.COM * this Agreement, and
15*9126SWyllys.Ingersoll@Sun.COM *
16*9126SWyllys.Ingersoll@Sun.COM * b) in the case of each subsequent Contributor:
17*9126SWyllys.Ingersoll@Sun.COM * i) changes to the Program, and
18*9126SWyllys.Ingersoll@Sun.COM * ii) additions to the Program;
19*9126SWyllys.Ingersoll@Sun.COM *
20*9126SWyllys.Ingersoll@Sun.COM * where such changes and/or additions to the Program
21*9126SWyllys.Ingersoll@Sun.COM * originate from and are distributed by that
22*9126SWyllys.Ingersoll@Sun.COM * particular Contributor. A Contribution 'originates'
23*9126SWyllys.Ingersoll@Sun.COM * from a Contributor if it was added to the Program
24*9126SWyllys.Ingersoll@Sun.COM * by such Contributor itself or anyone acting on such
25*9126SWyllys.Ingersoll@Sun.COM * Contributor's behalf. Contributions do not include
26*9126SWyllys.Ingersoll@Sun.COM * additions to the Program which: (i) are separate
27*9126SWyllys.Ingersoll@Sun.COM * modules of software distributed in conjunction with
28*9126SWyllys.Ingersoll@Sun.COM * the Program under their own license agreement, and
29*9126SWyllys.Ingersoll@Sun.COM * (ii) are not derivative works of the Program.
30*9126SWyllys.Ingersoll@Sun.COM *
31*9126SWyllys.Ingersoll@Sun.COM *
32*9126SWyllys.Ingersoll@Sun.COM * "Contributor" means any person or entity that distributes
33*9126SWyllys.Ingersoll@Sun.COM * the Program.
34*9126SWyllys.Ingersoll@Sun.COM *
35*9126SWyllys.Ingersoll@Sun.COM * "Licensed Patents " mean patent claims licensable by a
36*9126SWyllys.Ingersoll@Sun.COM * Contributor which are necessarily infringed by the use or
37*9126SWyllys.Ingersoll@Sun.COM * sale of its Contribution alone or when combined with the
38*9126SWyllys.Ingersoll@Sun.COM * Program.
39*9126SWyllys.Ingersoll@Sun.COM *
40*9126SWyllys.Ingersoll@Sun.COM * "Program" means the Contributions distributed in
41*9126SWyllys.Ingersoll@Sun.COM * accordance with this Agreement.
42*9126SWyllys.Ingersoll@Sun.COM *
43*9126SWyllys.Ingersoll@Sun.COM * "Recipient" means anyone who receives the Program under
44*9126SWyllys.Ingersoll@Sun.COM * this Agreement, including all Contributors.
45*9126SWyllys.Ingersoll@Sun.COM *
46*9126SWyllys.Ingersoll@Sun.COM * 2. GRANT OF RIGHTS
47*9126SWyllys.Ingersoll@Sun.COM *
48*9126SWyllys.Ingersoll@Sun.COM * a) Subject to the terms of this Agreement, each
49*9126SWyllys.Ingersoll@Sun.COM * Contributor hereby grants Recipient a
50*9126SWyllys.Ingersoll@Sun.COM * no - exclusive, worldwide, royalt - free copyright
51*9126SWyllys.Ingersoll@Sun.COM * license to reproduce, prepare derivative works of,
52*9126SWyllys.Ingersoll@Sun.COM * publicly display, publicly perform, distribute and
53*9126SWyllys.Ingersoll@Sun.COM * sublicense the Contribution of such Contributor, if
54*9126SWyllys.Ingersoll@Sun.COM * any, and such derivative works, in source code and
55*9126SWyllys.Ingersoll@Sun.COM * object code form.
56*9126SWyllys.Ingersoll@Sun.COM *
57*9126SWyllys.Ingersoll@Sun.COM * b) Subject to the terms of this Agreement, each
58*9126SWyllys.Ingersoll@Sun.COM * Contributor hereby grants Recipient a
59*9126SWyllys.Ingersoll@Sun.COM * no - exclusive, worldwide, royalt - free patent
60*9126SWyllys.Ingersoll@Sun.COM * license under Licensed Patents to make, use, sell,
61*9126SWyllys.Ingersoll@Sun.COM * offer to sell, import and otherwise transfer the
62*9126SWyllys.Ingersoll@Sun.COM * Contribution of such Contributor, if any, in source
63*9126SWyllys.Ingersoll@Sun.COM * code and object code form. This patent license
64*9126SWyllys.Ingersoll@Sun.COM * shall apply to the combination of the Contribution
65*9126SWyllys.Ingersoll@Sun.COM * and the Program if, at the time the Contribution is
66*9126SWyllys.Ingersoll@Sun.COM * added by the Contributor, such addition of the
67*9126SWyllys.Ingersoll@Sun.COM * Contribution causes such combination to be covered
68*9126SWyllys.Ingersoll@Sun.COM * by the Licensed Patents. The patent license shall
69*9126SWyllys.Ingersoll@Sun.COM * not apply to any other combinations which include
70*9126SWyllys.Ingersoll@Sun.COM * the Contribution. No hardware per se is licensed
71*9126SWyllys.Ingersoll@Sun.COM * hereunder.
72*9126SWyllys.Ingersoll@Sun.COM *
73*9126SWyllys.Ingersoll@Sun.COM * c) Recipient understands that although each
74*9126SWyllys.Ingersoll@Sun.COM * Contributor grants the licenses to its
75*9126SWyllys.Ingersoll@Sun.COM * Contributions set forth herein, no assurances are
76*9126SWyllys.Ingersoll@Sun.COM * provided by any Contributor that the Program does
77*9126SWyllys.Ingersoll@Sun.COM * not infringe the patent or other intellectual
78*9126SWyllys.Ingersoll@Sun.COM * property rights of any other entity. Each
79*9126SWyllys.Ingersoll@Sun.COM * Contributor disclaims any liability to Recipient
80*9126SWyllys.Ingersoll@Sun.COM * for claims brought by any other entity based on
81*9126SWyllys.Ingersoll@Sun.COM * infringement of intellectual property rights or
82*9126SWyllys.Ingersoll@Sun.COM * otherwise. As a condition to exercising the rights
83*9126SWyllys.Ingersoll@Sun.COM * and licenses granted hereunder, each Recipient
84*9126SWyllys.Ingersoll@Sun.COM * hereby assumes sole responsibility to secure any
85*9126SWyllys.Ingersoll@Sun.COM * other intellectual property rights needed, if any.
86*9126SWyllys.Ingersoll@Sun.COM *
87*9126SWyllys.Ingersoll@Sun.COM * For example, if a third party patent license is
88*9126SWyllys.Ingersoll@Sun.COM * required to allow Recipient to distribute the
89*9126SWyllys.Ingersoll@Sun.COM * Program, it is Recipient's responsibility to
90*9126SWyllys.Ingersoll@Sun.COM * acquire that license before distributing the
91*9126SWyllys.Ingersoll@Sun.COM * Program.
92*9126SWyllys.Ingersoll@Sun.COM *
93*9126SWyllys.Ingersoll@Sun.COM * d) Each Contributor represents that to its
94*9126SWyllys.Ingersoll@Sun.COM * knowledge it has sufficient copyright rights in its
95*9126SWyllys.Ingersoll@Sun.COM * Contribution, if any, to grant the copyright
96*9126SWyllys.Ingersoll@Sun.COM * license set forth in this Agreement.
97*9126SWyllys.Ingersoll@Sun.COM *
98*9126SWyllys.Ingersoll@Sun.COM * 3. REQUIREMENTS
99*9126SWyllys.Ingersoll@Sun.COM *
100*9126SWyllys.Ingersoll@Sun.COM * A Contributor may choose to distribute the Program in
101*9126SWyllys.Ingersoll@Sun.COM * object code form under its own license agreement, provided
102*9126SWyllys.Ingersoll@Sun.COM * that:
103*9126SWyllys.Ingersoll@Sun.COM * a) it complies with the terms and conditions of
104*9126SWyllys.Ingersoll@Sun.COM * this Agreement; and
105*9126SWyllys.Ingersoll@Sun.COM *
106*9126SWyllys.Ingersoll@Sun.COM * b) its license agreement:
107*9126SWyllys.Ingersoll@Sun.COM * i) effectively disclaims on behalf of all
108*9126SWyllys.Ingersoll@Sun.COM * Contributors all warranties and conditions, express
109*9126SWyllys.Ingersoll@Sun.COM * and implied, including warranties or conditions of
110*9126SWyllys.Ingersoll@Sun.COM * title and no - infringement, and implied warranties
111*9126SWyllys.Ingersoll@Sun.COM * or conditions of merchantability and fitness for a
112*9126SWyllys.Ingersoll@Sun.COM * particular purpose;
113*9126SWyllys.Ingersoll@Sun.COM *
114*9126SWyllys.Ingersoll@Sun.COM * ii) effectively excludes on behalf of all
115*9126SWyllys.Ingersoll@Sun.COM * Contributors all liability for damages, including
116*9126SWyllys.Ingersoll@Sun.COM * direct, indirect, special, incidental and
117*9126SWyllys.Ingersoll@Sun.COM * consequential damages, such as lost profits;
118*9126SWyllys.Ingersoll@Sun.COM *
119*9126SWyllys.Ingersoll@Sun.COM * iii) states that any provisions which differ from
120*9126SWyllys.Ingersoll@Sun.COM * this Agreement are offered by that Contributor
121*9126SWyllys.Ingersoll@Sun.COM * alone and not by any other party; and
122*9126SWyllys.Ingersoll@Sun.COM *
123*9126SWyllys.Ingersoll@Sun.COM * iv) states that source code for the Program is
124*9126SWyllys.Ingersoll@Sun.COM * available from such Contributor, and informs
125*9126SWyllys.Ingersoll@Sun.COM * licensees how to obtain it in a reasonable manner
126*9126SWyllys.Ingersoll@Sun.COM * on or through a medium customarily used for
127*9126SWyllys.Ingersoll@Sun.COM * software exchange.
128*9126SWyllys.Ingersoll@Sun.COM *
129*9126SWyllys.Ingersoll@Sun.COM * When the Program is made available in source code form:
130*9126SWyllys.Ingersoll@Sun.COM * a) it must be made available under this Agreement;
131*9126SWyllys.Ingersoll@Sun.COM * and
132*9126SWyllys.Ingersoll@Sun.COM * b) a copy of this Agreement must be included with
133*9126SWyllys.Ingersoll@Sun.COM * each copy of the Program.
134*9126SWyllys.Ingersoll@Sun.COM *
135*9126SWyllys.Ingersoll@Sun.COM * Contributors may not remove or alter any copyright notices
136*9126SWyllys.Ingersoll@Sun.COM * contained within the Program.
137*9126SWyllys.Ingersoll@Sun.COM *
138*9126SWyllys.Ingersoll@Sun.COM * Each Contributor must identify itself as the originator of
139*9126SWyllys.Ingersoll@Sun.COM * its Contribution, if any, in a manner that reasonably
140*9126SWyllys.Ingersoll@Sun.COM * allows subsequent Recipients to identify the originator of
141*9126SWyllys.Ingersoll@Sun.COM * the Contribution.
142*9126SWyllys.Ingersoll@Sun.COM *
143*9126SWyllys.Ingersoll@Sun.COM *
144*9126SWyllys.Ingersoll@Sun.COM * 4. COMMERCIAL DISTRIBUTION
145*9126SWyllys.Ingersoll@Sun.COM *
146*9126SWyllys.Ingersoll@Sun.COM * Commercial distributors of software may accept certain
147*9126SWyllys.Ingersoll@Sun.COM * responsibilities with respect to end users, business
148*9126SWyllys.Ingersoll@Sun.COM * partners and the like. While this license is intended to
149*9126SWyllys.Ingersoll@Sun.COM * facilitate the commercial use of the Program, the
150*9126SWyllys.Ingersoll@Sun.COM * Contributor who includes the Program in a commercial
151*9126SWyllys.Ingersoll@Sun.COM * product offering should do so in a manner which does not
152*9126SWyllys.Ingersoll@Sun.COM * create potential liability for other Contributors.
153*9126SWyllys.Ingersoll@Sun.COM * Therefore, if a Contributor includes the Program in a
154*9126SWyllys.Ingersoll@Sun.COM * commercial product offering, such Contributor ("Commercial
155*9126SWyllys.Ingersoll@Sun.COM * Contributor") hereby agrees to defend and indemnify every
156*9126SWyllys.Ingersoll@Sun.COM * other Contributor ("Indemnified Contributor") against any
157*9126SWyllys.Ingersoll@Sun.COM * losses, damages and costs (collectively "Losses") arising
158*9126SWyllys.Ingersoll@Sun.COM * from claims, lawsuits and other legal actions brought by a
159*9126SWyllys.Ingersoll@Sun.COM * third party against the Indemnified Contributor to the
160*9126SWyllys.Ingersoll@Sun.COM * extent caused by the acts or omissions of such Commercial
161*9126SWyllys.Ingersoll@Sun.COM * Contributor in connection with its distribution of the
162*9126SWyllys.Ingersoll@Sun.COM * Program in a commercial product offering. The obligations
163*9126SWyllys.Ingersoll@Sun.COM * in this section do not apply to any claims or Losses
164*9126SWyllys.Ingersoll@Sun.COM * relating to any actual or alleged intellectual property
165*9126SWyllys.Ingersoll@Sun.COM * infringement. In order to qualify, an Indemnified
166*9126SWyllys.Ingersoll@Sun.COM * Contributor must: a) promptly notify the Commercial
167*9126SWyllys.Ingersoll@Sun.COM * Contributor in writing of such claim, and b) allow the
168*9126SWyllys.Ingersoll@Sun.COM * Commercial Contributor to control, and cooperate with the
169*9126SWyllys.Ingersoll@Sun.COM * Commercial Contributor in, the defense and any related
170*9126SWyllys.Ingersoll@Sun.COM * settlement negotiations. The Indemnified Contributor may
171*9126SWyllys.Ingersoll@Sun.COM * participate in any such claim at its own expense.
172*9126SWyllys.Ingersoll@Sun.COM *
173*9126SWyllys.Ingersoll@Sun.COM *
174*9126SWyllys.Ingersoll@Sun.COM * For example, a Contributor might include the Program in a
175*9126SWyllys.Ingersoll@Sun.COM * commercial product offering, Product X. That Contributor
176*9126SWyllys.Ingersoll@Sun.COM * is then a Commercial Contributor. If that Commercial
177*9126SWyllys.Ingersoll@Sun.COM * Contributor then makes performance claims, or offers
178*9126SWyllys.Ingersoll@Sun.COM * warranties related to Product X, those performance claims
179*9126SWyllys.Ingersoll@Sun.COM * and warranties are such Commercial Contributor's
180*9126SWyllys.Ingersoll@Sun.COM * responsibility alone. Under this section, the Commercial
181*9126SWyllys.Ingersoll@Sun.COM * Contributor would have to defend claims against the other
182*9126SWyllys.Ingersoll@Sun.COM * Contributors related to those performance claims and
183*9126SWyllys.Ingersoll@Sun.COM * warranties, and if a court requires any other Contributor
184*9126SWyllys.Ingersoll@Sun.COM * to pay any damages as a result, the Commercial Contributor
185*9126SWyllys.Ingersoll@Sun.COM * must pay those damages.
186*9126SWyllys.Ingersoll@Sun.COM *
187*9126SWyllys.Ingersoll@Sun.COM *
188*9126SWyllys.Ingersoll@Sun.COM * 5. NO WARRANTY
189*9126SWyllys.Ingersoll@Sun.COM *
190*9126SWyllys.Ingersoll@Sun.COM * EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE
191*9126SWyllys.Ingersoll@Sun.COM * PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT
192*9126SWyllys.Ingersoll@Sun.COM * WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
193*9126SWyllys.Ingersoll@Sun.COM * IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR
194*9126SWyllys.Ingersoll@Sun.COM * CONDITIONS OF TITLE, NO - INFRINGEMENT, MERCHANTABILITY OR
195*9126SWyllys.Ingersoll@Sun.COM * FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely
196*9126SWyllys.Ingersoll@Sun.COM * responsible for determining the appropriateness of using
197*9126SWyllys.Ingersoll@Sun.COM * and distributing the Program and assumes all risks
198*9126SWyllys.Ingersoll@Sun.COM * associated with its exercise of rights under this
199*9126SWyllys.Ingersoll@Sun.COM * Agreement, including but not limited to the risks and
200*9126SWyllys.Ingersoll@Sun.COM * costs of program errors, compliance with applicable laws,
201*9126SWyllys.Ingersoll@Sun.COM * damage to or loss of data, programs or equipment, and
202*9126SWyllys.Ingersoll@Sun.COM * unavailability or interruption of operations.
203*9126SWyllys.Ingersoll@Sun.COM *
204*9126SWyllys.Ingersoll@Sun.COM * 6. DISCLAIMER OF LIABILITY
205*9126SWyllys.Ingersoll@Sun.COM * EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER
206*9126SWyllys.Ingersoll@Sun.COM * RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY
207*9126SWyllys.Ingersoll@Sun.COM * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
208*9126SWyllys.Ingersoll@Sun.COM * OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION
209*9126SWyllys.Ingersoll@Sun.COM * LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF
210*9126SWyllys.Ingersoll@Sun.COM * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
211*9126SWyllys.Ingersoll@Sun.COM * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
212*9126SWyllys.Ingersoll@Sun.COM * OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE
213*9126SWyllys.Ingersoll@Sun.COM * OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE
214*9126SWyllys.Ingersoll@Sun.COM * POSSIBILITY OF SUCH DAMAGES.
215*9126SWyllys.Ingersoll@Sun.COM *
216*9126SWyllys.Ingersoll@Sun.COM * 7. GENERAL
217*9126SWyllys.Ingersoll@Sun.COM *
218*9126SWyllys.Ingersoll@Sun.COM * If any provision of this Agreement is invalid or
219*9126SWyllys.Ingersoll@Sun.COM * unenforceable under applicable law, it shall not affect
220*9126SWyllys.Ingersoll@Sun.COM * the validity or enforceability of the remainder of the
221*9126SWyllys.Ingersoll@Sun.COM * terms of this Agreement, and without further action by the
222*9126SWyllys.Ingersoll@Sun.COM * parties hereto, such provision shall be reformed to the
223*9126SWyllys.Ingersoll@Sun.COM * minimum extent necessary to make such provision valid and
224*9126SWyllys.Ingersoll@Sun.COM * enforceable.
225*9126SWyllys.Ingersoll@Sun.COM *
226*9126SWyllys.Ingersoll@Sun.COM *
227*9126SWyllys.Ingersoll@Sun.COM * If Recipient institutes patent litigation against a
228*9126SWyllys.Ingersoll@Sun.COM * Contributor with respect to a patent applicable to
229*9126SWyllys.Ingersoll@Sun.COM * software (including a cros - claim or counterclaim in a
230*9126SWyllys.Ingersoll@Sun.COM * lawsuit), then any patent licenses granted by that
231*9126SWyllys.Ingersoll@Sun.COM * Contributor to such Recipient under this Agreement shall
232*9126SWyllys.Ingersoll@Sun.COM * terminate as of the date such litigation is filed. In
233*9126SWyllys.Ingersoll@Sun.COM * addition, If Recipient institutes patent litigation
234*9126SWyllys.Ingersoll@Sun.COM * against any entity (including a cros - claim or
235*9126SWyllys.Ingersoll@Sun.COM * counterclaim in a lawsuit) alleging that the Program
236*9126SWyllys.Ingersoll@Sun.COM * itself (excluding combinations of the Program with other
237*9126SWyllys.Ingersoll@Sun.COM * software or hardware) infringes such Recipient's
238*9126SWyllys.Ingersoll@Sun.COM * patent(s), then such Recipient's rights granted under
239*9126SWyllys.Ingersoll@Sun.COM * Section 2(b) shall terminate as of the date such
240*9126SWyllys.Ingersoll@Sun.COM * litigation is filed.
241*9126SWyllys.Ingersoll@Sun.COM *
242*9126SWyllys.Ingersoll@Sun.COM * All Recipient's rights under this Agreement shall
243*9126SWyllys.Ingersoll@Sun.COM * terminate if it fails to comply with any of the material
244*9126SWyllys.Ingersoll@Sun.COM * terms or conditions of this Agreement and does not cure
245*9126SWyllys.Ingersoll@Sun.COM * such failure in a reasonable period of time after becoming
246*9126SWyllys.Ingersoll@Sun.COM * aware of such noncompliance. If all Recipient's rights
247*9126SWyllys.Ingersoll@Sun.COM * under this Agreement terminate, Recipient agrees to cease
248*9126SWyllys.Ingersoll@Sun.COM * use and distribution of the Program as soon as reasonably
249*9126SWyllys.Ingersoll@Sun.COM * practicable. However, Recipient's obligations under this
250*9126SWyllys.Ingersoll@Sun.COM * Agreement and any licenses granted by Recipient relating
251*9126SWyllys.Ingersoll@Sun.COM * to the Program shall continue and survive.
252*9126SWyllys.Ingersoll@Sun.COM *
253*9126SWyllys.Ingersoll@Sun.COM * Everyone is permitted to copy and distribute copies of
254*9126SWyllys.Ingersoll@Sun.COM * this Agreement, but in order to avoid inconsistency the
255*9126SWyllys.Ingersoll@Sun.COM * Agreement is copyrighted and may only be modified in the
256*9126SWyllys.Ingersoll@Sun.COM * following manner. The Agreement Steward reserves the right
257*9126SWyllys.Ingersoll@Sun.COM * to publish new versions (including revisions) of this
258*9126SWyllys.Ingersoll@Sun.COM * Agreement from time to time. No one other than the
259*9126SWyllys.Ingersoll@Sun.COM * Agreement Steward has the right to modify this Agreement.
260*9126SWyllys.Ingersoll@Sun.COM *
261*9126SWyllys.Ingersoll@Sun.COM * IBM is the initial Agreement Steward. IBM may assign the
262*9126SWyllys.Ingersoll@Sun.COM * responsibility to serve as the Agreement Steward to a
263*9126SWyllys.Ingersoll@Sun.COM * suitable separate entity. Each new version of the
264*9126SWyllys.Ingersoll@Sun.COM * Agreement will be given a distinguishing version number.
265*9126SWyllys.Ingersoll@Sun.COM * The Program (including Contributions) may always be
266*9126SWyllys.Ingersoll@Sun.COM * distributed subject to the version of the Agreement under
267*9126SWyllys.Ingersoll@Sun.COM * which it was received. In addition, after a new version of
268*9126SWyllys.Ingersoll@Sun.COM * the Agreement is published, Contributor may elect to
269*9126SWyllys.Ingersoll@Sun.COM * distribute the Program (including its Contributions) under
270*9126SWyllys.Ingersoll@Sun.COM * the new version. Except as expressly stated in Sections
271*9126SWyllys.Ingersoll@Sun.COM * 2(a) and 2(b) above, Recipient receives no rights or
272*9126SWyllys.Ingersoll@Sun.COM * licenses to the intellectual property of any Contributor
273*9126SWyllys.Ingersoll@Sun.COM * under this Agreement, whether expressly, by implication,
274*9126SWyllys.Ingersoll@Sun.COM * estoppel or otherwise. All rights in the Program not
275*9126SWyllys.Ingersoll@Sun.COM * expressly granted under this Agreement are reserved.
276*9126SWyllys.Ingersoll@Sun.COM *
277*9126SWyllys.Ingersoll@Sun.COM *
278*9126SWyllys.Ingersoll@Sun.COM * This Agreement is governed by the laws of the State of New
279*9126SWyllys.Ingersoll@Sun.COM * York and the intellectual property laws of the United
280*9126SWyllys.Ingersoll@Sun.COM * States of America. No party to this Agreement will bring a
281*9126SWyllys.Ingersoll@Sun.COM * legal action under this Agreement more than one year after
282*9126SWyllys.Ingersoll@Sun.COM * the cause of action arose. Each party waives its rights to
283*9126SWyllys.Ingersoll@Sun.COM * a jury trial in any resulting litigation.
284*9126SWyllys.Ingersoll@Sun.COM *
285*9126SWyllys.Ingersoll@Sun.COM *
286*9126SWyllys.Ingersoll@Sun.COM *
287*9126SWyllys.Ingersoll@Sun.COM * (C) COPYRIGHT International Business Machines Corp. 2001, 2002
288*9126SWyllys.Ingersoll@Sun.COM */
289*9126SWyllys.Ingersoll@Sun.COM /*
290*9126SWyllys.Ingersoll@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
291*9126SWyllys.Ingersoll@Sun.COM * Use is subject to license terms.
292*9126SWyllys.Ingersoll@Sun.COM */
293*9126SWyllys.Ingersoll@Sun.COM #include "tpmtok_int.h"
294*9126SWyllys.Ingersoll@Sun.COM
295*9126SWyllys.Ingersoll@Sun.COM CK_RV
key_object_check_required_attributes(TEMPLATE * tmpl,CK_ULONG mode)296*9126SWyllys.Ingersoll@Sun.COM key_object_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode)
297*9126SWyllys.Ingersoll@Sun.COM {
298*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE * attr = NULL;
299*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL found;
300*9126SWyllys.Ingersoll@Sun.COM
301*9126SWyllys.Ingersoll@Sun.COM found = template_attribute_find(tmpl, CKA_KEY_TYPE, &attr);
302*9126SWyllys.Ingersoll@Sun.COM if (! found) {
303*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE) {
304*9126SWyllys.Ingersoll@Sun.COM return (CKR_TEMPLATE_INCOMPLETE);
305*9126SWyllys.Ingersoll@Sun.COM }
306*9126SWyllys.Ingersoll@Sun.COM }
307*9126SWyllys.Ingersoll@Sun.COM
308*9126SWyllys.Ingersoll@Sun.COM return (template_check_required_base_attributes(tmpl, mode));
309*9126SWyllys.Ingersoll@Sun.COM }
310*9126SWyllys.Ingersoll@Sun.COM
311*9126SWyllys.Ingersoll@Sun.COM CK_RV
key_object_set_default_attributes(TEMPLATE * tmpl,CK_ULONG mode)312*9126SWyllys.Ingersoll@Sun.COM key_object_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode)
313*9126SWyllys.Ingersoll@Sun.COM {
314*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE * id_attr = NULL;
315*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE * sdate_attr = NULL;
316*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE * edate_attr = NULL;
317*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE * derive_attr = NULL;
318*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE * local_attr = NULL;
319*9126SWyllys.Ingersoll@Sun.COM
320*9126SWyllys.Ingersoll@Sun.COM // satisfy the compiler
321*9126SWyllys.Ingersoll@Sun.COM //
322*9126SWyllys.Ingersoll@Sun.COM if (mode)
323*9126SWyllys.Ingersoll@Sun.COM id_attr = NULL;
324*9126SWyllys.Ingersoll@Sun.COM
325*9126SWyllys.Ingersoll@Sun.COM id_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE));
326*9126SWyllys.Ingersoll@Sun.COM sdate_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE));
327*9126SWyllys.Ingersoll@Sun.COM edate_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE));
328*9126SWyllys.Ingersoll@Sun.COM derive_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
329*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
330*9126SWyllys.Ingersoll@Sun.COM local_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
331*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
332*9126SWyllys.Ingersoll@Sun.COM
333*9126SWyllys.Ingersoll@Sun.COM if (! id_attr || ! sdate_attr || ! edate_attr ||
334*9126SWyllys.Ingersoll@Sun.COM ! derive_attr || ! local_attr) {
335*9126SWyllys.Ingersoll@Sun.COM if (id_attr) free(id_attr);
336*9126SWyllys.Ingersoll@Sun.COM if (sdate_attr) free(sdate_attr);
337*9126SWyllys.Ingersoll@Sun.COM if (edate_attr) free(edate_attr);
338*9126SWyllys.Ingersoll@Sun.COM if (derive_attr) free(derive_attr);
339*9126SWyllys.Ingersoll@Sun.COM if (local_attr) free(local_attr);
340*9126SWyllys.Ingersoll@Sun.COM return (CKR_HOST_MEMORY);
341*9126SWyllys.Ingersoll@Sun.COM }
342*9126SWyllys.Ingersoll@Sun.COM
343*9126SWyllys.Ingersoll@Sun.COM id_attr->type = CKA_ID;
344*9126SWyllys.Ingersoll@Sun.COM id_attr->ulValueLen = 0;
345*9126SWyllys.Ingersoll@Sun.COM id_attr->pValue = NULL;
346*9126SWyllys.Ingersoll@Sun.COM
347*9126SWyllys.Ingersoll@Sun.COM sdate_attr->type = CKA_START_DATE;
348*9126SWyllys.Ingersoll@Sun.COM sdate_attr->ulValueLen = 0;
349*9126SWyllys.Ingersoll@Sun.COM sdate_attr->pValue = NULL;
350*9126SWyllys.Ingersoll@Sun.COM
351*9126SWyllys.Ingersoll@Sun.COM edate_attr->type = CKA_END_DATE;
352*9126SWyllys.Ingersoll@Sun.COM edate_attr->ulValueLen = 0;
353*9126SWyllys.Ingersoll@Sun.COM edate_attr->pValue = NULL;
354*9126SWyllys.Ingersoll@Sun.COM
355*9126SWyllys.Ingersoll@Sun.COM derive_attr->type = CKA_DERIVE;
356*9126SWyllys.Ingersoll@Sun.COM derive_attr->ulValueLen = sizeof (CK_BBOOL);
357*9126SWyllys.Ingersoll@Sun.COM derive_attr->pValue = (CK_BYTE *)derive_attr +
358*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
359*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)derive_attr->pValue = FALSE;
360*9126SWyllys.Ingersoll@Sun.COM
361*9126SWyllys.Ingersoll@Sun.COM local_attr->type = CKA_LOCAL;
362*9126SWyllys.Ingersoll@Sun.COM local_attr->ulValueLen = sizeof (CK_BBOOL);
363*9126SWyllys.Ingersoll@Sun.COM local_attr->pValue = (CK_BYTE *)local_attr + sizeof (CK_ATTRIBUTE);
364*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)local_attr->pValue = FALSE;
365*9126SWyllys.Ingersoll@Sun.COM
366*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, id_attr);
367*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, sdate_attr);
368*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, edate_attr);
369*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, derive_attr);
370*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, local_attr);
371*9126SWyllys.Ingersoll@Sun.COM
372*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
373*9126SWyllys.Ingersoll@Sun.COM }
374*9126SWyllys.Ingersoll@Sun.COM
375*9126SWyllys.Ingersoll@Sun.COM CK_RV
key_object_validate_attribute(TEMPLATE * tmpl,CK_ATTRIBUTE * attr,CK_ULONG mode)376*9126SWyllys.Ingersoll@Sun.COM key_object_validate_attribute(TEMPLATE *tmpl,
377*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *attr, CK_ULONG mode)
378*9126SWyllys.Ingersoll@Sun.COM {
379*9126SWyllys.Ingersoll@Sun.COM switch (attr->type) {
380*9126SWyllys.Ingersoll@Sun.COM case CKA_KEY_TYPE:
381*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE || mode == MODE_DERIVE ||
382*9126SWyllys.Ingersoll@Sun.COM mode == MODE_KEYGEN || mode == MODE_UNWRAP)
383*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
384*9126SWyllys.Ingersoll@Sun.COM else {
385*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
386*9126SWyllys.Ingersoll@Sun.COM }
387*9126SWyllys.Ingersoll@Sun.COM case CKA_ID:
388*9126SWyllys.Ingersoll@Sun.COM case CKA_START_DATE:
389*9126SWyllys.Ingersoll@Sun.COM case CKA_END_DATE:
390*9126SWyllys.Ingersoll@Sun.COM case CKA_DERIVE:
391*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
392*9126SWyllys.Ingersoll@Sun.COM
393*9126SWyllys.Ingersoll@Sun.COM case CKA_LOCAL:
394*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
395*9126SWyllys.Ingersoll@Sun.COM
396*9126SWyllys.Ingersoll@Sun.COM default:
397*9126SWyllys.Ingersoll@Sun.COM return (template_validate_base_attribute(tmpl, attr, mode));
398*9126SWyllys.Ingersoll@Sun.COM }
399*9126SWyllys.Ingersoll@Sun.COM }
400*9126SWyllys.Ingersoll@Sun.COM
401*9126SWyllys.Ingersoll@Sun.COM CK_RV
publ_key_check_required_attributes(TEMPLATE * tmpl,CK_ULONG mode)402*9126SWyllys.Ingersoll@Sun.COM publ_key_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode) {
403*9126SWyllys.Ingersoll@Sun.COM return (key_object_check_required_attributes(tmpl, mode));
404*9126SWyllys.Ingersoll@Sun.COM }
405*9126SWyllys.Ingersoll@Sun.COM
406*9126SWyllys.Ingersoll@Sun.COM CK_RV
publ_key_set_default_attributes(TEMPLATE * tmpl,CK_ULONG mode)407*9126SWyllys.Ingersoll@Sun.COM publ_key_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode)
408*9126SWyllys.Ingersoll@Sun.COM {
409*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *class_attr = NULL;
410*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *subject_attr = NULL;
411*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *encrypt_attr = NULL;
412*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *verify_attr = NULL;
413*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *verify_recover_attr = NULL;
414*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *wrap_attr = NULL;
415*9126SWyllys.Ingersoll@Sun.COM
416*9126SWyllys.Ingersoll@Sun.COM CK_OBJECT_CLASS class = CKO_PUBLIC_KEY;
417*9126SWyllys.Ingersoll@Sun.COM CK_RV rc;
418*9126SWyllys.Ingersoll@Sun.COM
419*9126SWyllys.Ingersoll@Sun.COM
420*9126SWyllys.Ingersoll@Sun.COM rc = key_object_set_default_attributes(tmpl, mode);
421*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
422*9126SWyllys.Ingersoll@Sun.COM return (rc);
423*9126SWyllys.Ingersoll@Sun.COM }
424*9126SWyllys.Ingersoll@Sun.COM //
425*9126SWyllys.Ingersoll@Sun.COM // add the default CKO_PUBLIC_KEY attributes
426*9126SWyllys.Ingersoll@Sun.COM //
427*9126SWyllys.Ingersoll@Sun.COM class_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
428*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_OBJECT_CLASS));
429*9126SWyllys.Ingersoll@Sun.COM subject_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE));
430*9126SWyllys.Ingersoll@Sun.COM encrypt_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
431*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
432*9126SWyllys.Ingersoll@Sun.COM verify_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
433*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
434*9126SWyllys.Ingersoll@Sun.COM verify_recover_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
435*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
436*9126SWyllys.Ingersoll@Sun.COM wrap_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
437*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
438*9126SWyllys.Ingersoll@Sun.COM
439*9126SWyllys.Ingersoll@Sun.COM if (! class || ! subject_attr || ! encrypt_attr ||
440*9126SWyllys.Ingersoll@Sun.COM ! verify_attr || ! verify_recover_attr || ! wrap_attr) {
441*9126SWyllys.Ingersoll@Sun.COM if (class_attr) free(class_attr);
442*9126SWyllys.Ingersoll@Sun.COM if (subject_attr) free(subject_attr);
443*9126SWyllys.Ingersoll@Sun.COM if (encrypt_attr) free(encrypt_attr);
444*9126SWyllys.Ingersoll@Sun.COM if (verify_attr) free(verify_attr);
445*9126SWyllys.Ingersoll@Sun.COM if (verify_recover_attr) free(verify_recover_attr);
446*9126SWyllys.Ingersoll@Sun.COM if (wrap_attr) free(wrap_attr);
447*9126SWyllys.Ingersoll@Sun.COM
448*9126SWyllys.Ingersoll@Sun.COM return (CKR_HOST_MEMORY);
449*9126SWyllys.Ingersoll@Sun.COM }
450*9126SWyllys.Ingersoll@Sun.COM
451*9126SWyllys.Ingersoll@Sun.COM class_attr->type = CKA_CLASS;
452*9126SWyllys.Ingersoll@Sun.COM class_attr->ulValueLen = sizeof (CK_OBJECT_CLASS);
453*9126SWyllys.Ingersoll@Sun.COM class_attr->pValue = (CK_BYTE *)class_attr +
454*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
455*9126SWyllys.Ingersoll@Sun.COM *(CK_OBJECT_CLASS *)class_attr->pValue = CKO_PUBLIC_KEY;
456*9126SWyllys.Ingersoll@Sun.COM
457*9126SWyllys.Ingersoll@Sun.COM subject_attr->type = CKA_SUBJECT;
458*9126SWyllys.Ingersoll@Sun.COM subject_attr->ulValueLen = 0; // empty string
459*9126SWyllys.Ingersoll@Sun.COM subject_attr->pValue = NULL;
460*9126SWyllys.Ingersoll@Sun.COM
461*9126SWyllys.Ingersoll@Sun.COM encrypt_attr->type = CKA_ENCRYPT;
462*9126SWyllys.Ingersoll@Sun.COM encrypt_attr->ulValueLen = sizeof (CK_BBOOL);
463*9126SWyllys.Ingersoll@Sun.COM encrypt_attr->pValue = (CK_BYTE *)encrypt_attr +
464*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
465*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)encrypt_attr->pValue = TRUE;
466*9126SWyllys.Ingersoll@Sun.COM
467*9126SWyllys.Ingersoll@Sun.COM verify_attr->type = CKA_VERIFY;
468*9126SWyllys.Ingersoll@Sun.COM verify_attr->ulValueLen = sizeof (CK_BBOOL);
469*9126SWyllys.Ingersoll@Sun.COM verify_attr->pValue = (CK_BYTE *)verify_attr +
470*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
471*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)verify_attr->pValue = TRUE;
472*9126SWyllys.Ingersoll@Sun.COM
473*9126SWyllys.Ingersoll@Sun.COM verify_recover_attr->type = CKA_VERIFY_RECOVER;
474*9126SWyllys.Ingersoll@Sun.COM verify_recover_attr->ulValueLen = sizeof (CK_BBOOL);
475*9126SWyllys.Ingersoll@Sun.COM verify_recover_attr->pValue = (CK_BYTE *)verify_recover_attr +
476*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
477*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)verify_recover_attr->pValue = TRUE;
478*9126SWyllys.Ingersoll@Sun.COM
479*9126SWyllys.Ingersoll@Sun.COM wrap_attr->type = CKA_WRAP;
480*9126SWyllys.Ingersoll@Sun.COM wrap_attr->ulValueLen = sizeof (CK_BBOOL);
481*9126SWyllys.Ingersoll@Sun.COM wrap_attr->pValue = (CK_BYTE *)wrap_attr +
482*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
483*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)wrap_attr->pValue = TRUE;
484*9126SWyllys.Ingersoll@Sun.COM
485*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, class_attr);
486*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, subject_attr);
487*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, encrypt_attr);
488*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, verify_attr);
489*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, verify_recover_attr);
490*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, wrap_attr);
491*9126SWyllys.Ingersoll@Sun.COM
492*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
493*9126SWyllys.Ingersoll@Sun.COM }
494*9126SWyllys.Ingersoll@Sun.COM
495*9126SWyllys.Ingersoll@Sun.COM CK_RV
publ_key_validate_attribute(TEMPLATE * tmpl,CK_ATTRIBUTE * attr,CK_ULONG mode)496*9126SWyllys.Ingersoll@Sun.COM publ_key_validate_attribute(TEMPLATE *tmpl,
497*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *attr, CK_ULONG mode) {
498*9126SWyllys.Ingersoll@Sun.COM switch (attr->type) {
499*9126SWyllys.Ingersoll@Sun.COM case CKA_SUBJECT:
500*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
501*9126SWyllys.Ingersoll@Sun.COM
502*9126SWyllys.Ingersoll@Sun.COM case CKA_ENCRYPT:
503*9126SWyllys.Ingersoll@Sun.COM case CKA_VERIFY:
504*9126SWyllys.Ingersoll@Sun.COM case CKA_VERIFY_RECOVER:
505*9126SWyllys.Ingersoll@Sun.COM case CKA_WRAP:
506*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_MODIFY) {
507*9126SWyllys.Ingersoll@Sun.COM if (nv_token_data->tweak_vector.allow_key_mods == TRUE)
508*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
509*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
510*9126SWyllys.Ingersoll@Sun.COM }
511*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
512*9126SWyllys.Ingersoll@Sun.COM
513*9126SWyllys.Ingersoll@Sun.COM default:
514*9126SWyllys.Ingersoll@Sun.COM return (key_object_validate_attribute(tmpl, attr, mode));
515*9126SWyllys.Ingersoll@Sun.COM }
516*9126SWyllys.Ingersoll@Sun.COM }
517*9126SWyllys.Ingersoll@Sun.COM
518*9126SWyllys.Ingersoll@Sun.COM CK_RV
priv_key_check_required_attributes(TEMPLATE * tmpl,CK_ULONG mode)519*9126SWyllys.Ingersoll@Sun.COM priv_key_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode) {
520*9126SWyllys.Ingersoll@Sun.COM return (key_object_check_required_attributes(tmpl, mode));
521*9126SWyllys.Ingersoll@Sun.COM }
522*9126SWyllys.Ingersoll@Sun.COM
523*9126SWyllys.Ingersoll@Sun.COM CK_RV
priv_key_set_default_attributes(TEMPLATE * tmpl,CK_ULONG mode)524*9126SWyllys.Ingersoll@Sun.COM priv_key_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode)
525*9126SWyllys.Ingersoll@Sun.COM {
526*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *class_attr = NULL;
527*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *subject_attr = NULL;
528*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *sensitive_attr = NULL;
529*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *decrypt_attr = NULL;
530*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *sign_attr = NULL;
531*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *sign_recover_attr = NULL;
532*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *unwrap_attr = NULL;
533*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *extractable_attr = NULL;
534*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *never_extr_attr = NULL;
535*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *always_sens_attr = NULL;
536*9126SWyllys.Ingersoll@Sun.COM CK_RV rc;
537*9126SWyllys.Ingersoll@Sun.COM
538*9126SWyllys.Ingersoll@Sun.COM
539*9126SWyllys.Ingersoll@Sun.COM rc = key_object_set_default_attributes(tmpl, mode);
540*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
541*9126SWyllys.Ingersoll@Sun.COM return (rc);
542*9126SWyllys.Ingersoll@Sun.COM }
543*9126SWyllys.Ingersoll@Sun.COM class_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
544*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_OBJECT_CLASS));
545*9126SWyllys.Ingersoll@Sun.COM subject_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE));
546*9126SWyllys.Ingersoll@Sun.COM sensitive_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
547*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
548*9126SWyllys.Ingersoll@Sun.COM decrypt_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
549*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
550*9126SWyllys.Ingersoll@Sun.COM sign_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
551*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
552*9126SWyllys.Ingersoll@Sun.COM sign_recover_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
553*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
554*9126SWyllys.Ingersoll@Sun.COM unwrap_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
555*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
556*9126SWyllys.Ingersoll@Sun.COM extractable_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
557*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
558*9126SWyllys.Ingersoll@Sun.COM never_extr_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
559*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
560*9126SWyllys.Ingersoll@Sun.COM always_sens_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
561*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
562*9126SWyllys.Ingersoll@Sun.COM
563*9126SWyllys.Ingersoll@Sun.COM if (! class_attr || ! subject_attr || ! sensitive_attr ||
564*9126SWyllys.Ingersoll@Sun.COM ! decrypt_attr || ! sign_attr || ! sign_recover_attr ||
565*9126SWyllys.Ingersoll@Sun.COM ! unwrap_attr || ! extractable_attr ||
566*9126SWyllys.Ingersoll@Sun.COM ! never_extr_attr || ! always_sens_attr) {
567*9126SWyllys.Ingersoll@Sun.COM if (class_attr) free(class_attr);
568*9126SWyllys.Ingersoll@Sun.COM if (subject_attr) free(subject_attr);
569*9126SWyllys.Ingersoll@Sun.COM if (sensitive_attr) free(sensitive_attr);
570*9126SWyllys.Ingersoll@Sun.COM if (decrypt_attr) free(decrypt_attr);
571*9126SWyllys.Ingersoll@Sun.COM if (sign_attr) free(sign_attr);
572*9126SWyllys.Ingersoll@Sun.COM if (sign_recover_attr) free(sign_recover_attr);
573*9126SWyllys.Ingersoll@Sun.COM if (unwrap_attr) free(unwrap_attr);
574*9126SWyllys.Ingersoll@Sun.COM if (extractable_attr) free(extractable_attr);
575*9126SWyllys.Ingersoll@Sun.COM if (always_sens_attr) free(always_sens_attr);
576*9126SWyllys.Ingersoll@Sun.COM if (never_extr_attr) free(never_extr_attr);
577*9126SWyllys.Ingersoll@Sun.COM
578*9126SWyllys.Ingersoll@Sun.COM return (CKR_HOST_MEMORY);
579*9126SWyllys.Ingersoll@Sun.COM }
580*9126SWyllys.Ingersoll@Sun.COM
581*9126SWyllys.Ingersoll@Sun.COM class_attr->type = CKA_CLASS;
582*9126SWyllys.Ingersoll@Sun.COM class_attr->ulValueLen = sizeof (CK_OBJECT_CLASS);
583*9126SWyllys.Ingersoll@Sun.COM class_attr->pValue = (CK_BYTE *)class_attr + sizeof (CK_ATTRIBUTE);
584*9126SWyllys.Ingersoll@Sun.COM *(CK_OBJECT_CLASS *)class_attr->pValue = CKO_PRIVATE_KEY;
585*9126SWyllys.Ingersoll@Sun.COM
586*9126SWyllys.Ingersoll@Sun.COM subject_attr->type = CKA_SUBJECT;
587*9126SWyllys.Ingersoll@Sun.COM subject_attr->ulValueLen = 0; // empty string
588*9126SWyllys.Ingersoll@Sun.COM subject_attr->pValue = NULL;
589*9126SWyllys.Ingersoll@Sun.COM
590*9126SWyllys.Ingersoll@Sun.COM sensitive_attr->type = CKA_SENSITIVE;
591*9126SWyllys.Ingersoll@Sun.COM sensitive_attr->ulValueLen = sizeof (CK_BBOOL);
592*9126SWyllys.Ingersoll@Sun.COM sensitive_attr->pValue = (CK_BYTE *)sensitive_attr +
593*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
594*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)sensitive_attr->pValue = FALSE;
595*9126SWyllys.Ingersoll@Sun.COM
596*9126SWyllys.Ingersoll@Sun.COM decrypt_attr->type = CKA_DECRYPT;
597*9126SWyllys.Ingersoll@Sun.COM decrypt_attr->ulValueLen = sizeof (CK_BBOOL);
598*9126SWyllys.Ingersoll@Sun.COM decrypt_attr->pValue = (CK_BYTE *)decrypt_attr +
599*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
600*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)decrypt_attr->pValue = TRUE;
601*9126SWyllys.Ingersoll@Sun.COM
602*9126SWyllys.Ingersoll@Sun.COM sign_attr->type = CKA_SIGN;
603*9126SWyllys.Ingersoll@Sun.COM sign_attr->ulValueLen = sizeof (CK_BBOOL);
604*9126SWyllys.Ingersoll@Sun.COM sign_attr->pValue = (CK_BYTE *)sign_attr +
605*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
606*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)sign_attr->pValue = TRUE;
607*9126SWyllys.Ingersoll@Sun.COM
608*9126SWyllys.Ingersoll@Sun.COM sign_recover_attr->type = CKA_SIGN_RECOVER;
609*9126SWyllys.Ingersoll@Sun.COM sign_recover_attr->ulValueLen = sizeof (CK_BBOOL);
610*9126SWyllys.Ingersoll@Sun.COM sign_recover_attr->pValue = (CK_BYTE *)sign_recover_attr +
611*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
612*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)sign_recover_attr->pValue = TRUE;
613*9126SWyllys.Ingersoll@Sun.COM
614*9126SWyllys.Ingersoll@Sun.COM unwrap_attr->type = CKA_UNWRAP;
615*9126SWyllys.Ingersoll@Sun.COM unwrap_attr->ulValueLen = sizeof (CK_BBOOL);
616*9126SWyllys.Ingersoll@Sun.COM unwrap_attr->pValue = (CK_BYTE *)unwrap_attr +
617*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
618*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)unwrap_attr->pValue = TRUE;
619*9126SWyllys.Ingersoll@Sun.COM
620*9126SWyllys.Ingersoll@Sun.COM extractable_attr->type = CKA_EXTRACTABLE;
621*9126SWyllys.Ingersoll@Sun.COM extractable_attr->ulValueLen = sizeof (CK_BBOOL);
622*9126SWyllys.Ingersoll@Sun.COM extractable_attr->pValue = (CK_BYTE *)extractable_attr +
623*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
624*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)extractable_attr->pValue = TRUE;
625*9126SWyllys.Ingersoll@Sun.COM
626*9126SWyllys.Ingersoll@Sun.COM never_extr_attr->type = CKA_NEVER_EXTRACTABLE;
627*9126SWyllys.Ingersoll@Sun.COM never_extr_attr->ulValueLen = sizeof (CK_BBOOL);
628*9126SWyllys.Ingersoll@Sun.COM never_extr_attr->pValue = (CK_BYTE *)never_extr_attr +
629*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
630*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)never_extr_attr->pValue = FALSE;
631*9126SWyllys.Ingersoll@Sun.COM
632*9126SWyllys.Ingersoll@Sun.COM always_sens_attr->type = CKA_ALWAYS_SENSITIVE;
633*9126SWyllys.Ingersoll@Sun.COM always_sens_attr->ulValueLen = sizeof (CK_BBOOL);
634*9126SWyllys.Ingersoll@Sun.COM always_sens_attr->pValue = (CK_BYTE *)always_sens_attr +
635*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
636*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)always_sens_attr->pValue = FALSE;
637*9126SWyllys.Ingersoll@Sun.COM
638*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, class_attr);
639*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, subject_attr);
640*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, sensitive_attr);
641*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, decrypt_attr);
642*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, sign_attr);
643*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, sign_recover_attr);
644*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, unwrap_attr);
645*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, extractable_attr);
646*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, never_extr_attr);
647*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, always_sens_attr);
648*9126SWyllys.Ingersoll@Sun.COM
649*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
650*9126SWyllys.Ingersoll@Sun.COM }
651*9126SWyllys.Ingersoll@Sun.COM
652*9126SWyllys.Ingersoll@Sun.COM CK_RV
priv_key_unwrap(TEMPLATE * tmpl,CK_ULONG keytype,CK_BYTE * data,CK_ULONG data_len)653*9126SWyllys.Ingersoll@Sun.COM priv_key_unwrap(TEMPLATE *tmpl,
654*9126SWyllys.Ingersoll@Sun.COM CK_ULONG keytype,
655*9126SWyllys.Ingersoll@Sun.COM CK_BYTE *data,
656*9126SWyllys.Ingersoll@Sun.COM CK_ULONG data_len)
657*9126SWyllys.Ingersoll@Sun.COM {
658*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *extractable = NULL;
659*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *always_sens = NULL;
660*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *never_extract = NULL;
661*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *sensitive = NULL;
662*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *local = NULL;
663*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL true = TRUE;
664*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL false = FALSE;
665*9126SWyllys.Ingersoll@Sun.COM CK_RV rc;
666*9126SWyllys.Ingersoll@Sun.COM
667*9126SWyllys.Ingersoll@Sun.COM switch (keytype) {
668*9126SWyllys.Ingersoll@Sun.COM case CKK_RSA:
669*9126SWyllys.Ingersoll@Sun.COM rc = rsa_priv_unwrap(tmpl, data, data_len);
670*9126SWyllys.Ingersoll@Sun.COM break;
671*9126SWyllys.Ingersoll@Sun.COM
672*9126SWyllys.Ingersoll@Sun.COM default:
673*9126SWyllys.Ingersoll@Sun.COM return (CKR_WRAPPED_KEY_INVALID);
674*9126SWyllys.Ingersoll@Sun.COM }
675*9126SWyllys.Ingersoll@Sun.COM
676*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
677*9126SWyllys.Ingersoll@Sun.COM return (rc);
678*9126SWyllys.Ingersoll@Sun.COM }
679*9126SWyllys.Ingersoll@Sun.COM
680*9126SWyllys.Ingersoll@Sun.COM /*
681*9126SWyllys.Ingersoll@Sun.COM * make sure
682*9126SWyllys.Ingersoll@Sun.COM * CKA_LOCAL == FALSE
683*9126SWyllys.Ingersoll@Sun.COM * CKA_ALWAYS_SENSITIVE == FALSE
684*9126SWyllys.Ingersoll@Sun.COM * CKA_EXTRACTABLE == TRUE
685*9126SWyllys.Ingersoll@Sun.COM * CKA_NEVER_EXTRACTABLE == FALSE
686*9126SWyllys.Ingersoll@Sun.COM */
687*9126SWyllys.Ingersoll@Sun.COM rc = build_attribute(CKA_LOCAL, &false, 1, &local);
688*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
689*9126SWyllys.Ingersoll@Sun.COM goto cleanup;
690*9126SWyllys.Ingersoll@Sun.COM }
691*9126SWyllys.Ingersoll@Sun.COM rc = build_attribute(CKA_ALWAYS_SENSITIVE, &false, 1, &always_sens);
692*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
693*9126SWyllys.Ingersoll@Sun.COM goto cleanup;
694*9126SWyllys.Ingersoll@Sun.COM }
695*9126SWyllys.Ingersoll@Sun.COM rc = build_attribute(CKA_SENSITIVE, &false, 1, &sensitive);
696*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
697*9126SWyllys.Ingersoll@Sun.COM goto cleanup;
698*9126SWyllys.Ingersoll@Sun.COM }
699*9126SWyllys.Ingersoll@Sun.COM rc = build_attribute(CKA_EXTRACTABLE, &true, 1, &extractable);
700*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
701*9126SWyllys.Ingersoll@Sun.COM goto cleanup;
702*9126SWyllys.Ingersoll@Sun.COM }
703*9126SWyllys.Ingersoll@Sun.COM rc = build_attribute(CKA_NEVER_EXTRACTABLE, &false, 1, &never_extract);
704*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
705*9126SWyllys.Ingersoll@Sun.COM goto cleanup;
706*9126SWyllys.Ingersoll@Sun.COM }
707*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, local);
708*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, always_sens);
709*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, sensitive);
710*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, extractable);
711*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, never_extract);
712*9126SWyllys.Ingersoll@Sun.COM
713*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
714*9126SWyllys.Ingersoll@Sun.COM
715*9126SWyllys.Ingersoll@Sun.COM cleanup:
716*9126SWyllys.Ingersoll@Sun.COM
717*9126SWyllys.Ingersoll@Sun.COM if (local) free(local);
718*9126SWyllys.Ingersoll@Sun.COM if (always_sens) free(always_sens);
719*9126SWyllys.Ingersoll@Sun.COM if (extractable) free(extractable);
720*9126SWyllys.Ingersoll@Sun.COM if (never_extract) free(never_extract);
721*9126SWyllys.Ingersoll@Sun.COM
722*9126SWyllys.Ingersoll@Sun.COM return (rc);
723*9126SWyllys.Ingersoll@Sun.COM }
724*9126SWyllys.Ingersoll@Sun.COM
725*9126SWyllys.Ingersoll@Sun.COM CK_RV
priv_key_validate_attribute(TEMPLATE * tmpl,CK_ATTRIBUTE * attr,CK_ULONG mode)726*9126SWyllys.Ingersoll@Sun.COM priv_key_validate_attribute(TEMPLATE *tmpl, CK_ATTRIBUTE *attr, CK_ULONG mode) {
727*9126SWyllys.Ingersoll@Sun.COM switch (attr->type) {
728*9126SWyllys.Ingersoll@Sun.COM case CKA_SUBJECT:
729*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
730*9126SWyllys.Ingersoll@Sun.COM
731*9126SWyllys.Ingersoll@Sun.COM case CKA_DECRYPT:
732*9126SWyllys.Ingersoll@Sun.COM case CKA_SIGN:
733*9126SWyllys.Ingersoll@Sun.COM case CKA_SIGN_RECOVER:
734*9126SWyllys.Ingersoll@Sun.COM case CKA_UNWRAP:
735*9126SWyllys.Ingersoll@Sun.COM // we might want to do this for MODE_COPY too
736*9126SWyllys.Ingersoll@Sun.COM //
737*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_MODIFY) {
738*9126SWyllys.Ingersoll@Sun.COM if (nv_token_data->tweak_vector.allow_key_mods == TRUE)
739*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
740*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
741*9126SWyllys.Ingersoll@Sun.COM }
742*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
743*9126SWyllys.Ingersoll@Sun.COM
744*9126SWyllys.Ingersoll@Sun.COM // after key creation, CKA_SENSITIVE may only be set to TRUE
745*9126SWyllys.Ingersoll@Sun.COM //
746*9126SWyllys.Ingersoll@Sun.COM case CKA_SENSITIVE:
747*9126SWyllys.Ingersoll@Sun.COM {
748*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL value;
749*9126SWyllys.Ingersoll@Sun.COM
750*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE || mode == MODE_KEYGEN)
751*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
752*9126SWyllys.Ingersoll@Sun.COM
753*9126SWyllys.Ingersoll@Sun.COM value = *(CK_BBOOL *)attr->pValue;
754*9126SWyllys.Ingersoll@Sun.COM if (value != TRUE) {
755*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
756*9126SWyllys.Ingersoll@Sun.COM }
757*9126SWyllys.Ingersoll@Sun.COM }
758*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
759*9126SWyllys.Ingersoll@Sun.COM
760*9126SWyllys.Ingersoll@Sun.COM case CKA_EXTRACTABLE:
761*9126SWyllys.Ingersoll@Sun.COM {
762*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL value;
763*9126SWyllys.Ingersoll@Sun.COM
764*9126SWyllys.Ingersoll@Sun.COM value = *(CK_BBOOL *)attr->pValue;
765*9126SWyllys.Ingersoll@Sun.COM if ((mode != MODE_CREATE && mode != MODE_KEYGEN) &&
766*9126SWyllys.Ingersoll@Sun.COM value != FALSE) {
767*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
768*9126SWyllys.Ingersoll@Sun.COM }
769*9126SWyllys.Ingersoll@Sun.COM if (value == FALSE) {
770*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *attr;
771*9126SWyllys.Ingersoll@Sun.COM
772*9126SWyllys.Ingersoll@Sun.COM attr = (CK_ATTRIBUTE *)malloc(
773*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE) + sizeof (CK_BBOOL));
774*9126SWyllys.Ingersoll@Sun.COM if (! attr) {
775*9126SWyllys.Ingersoll@Sun.COM return (CKR_HOST_MEMORY);
776*9126SWyllys.Ingersoll@Sun.COM }
777*9126SWyllys.Ingersoll@Sun.COM attr->type = CKA_NEVER_EXTRACTABLE;
778*9126SWyllys.Ingersoll@Sun.COM attr->ulValueLen = sizeof (CK_BBOOL);
779*9126SWyllys.Ingersoll@Sun.COM attr->pValue = (CK_BYTE *)attr +
780*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
781*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)attr->pValue = FALSE;
782*9126SWyllys.Ingersoll@Sun.COM
783*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, attr);
784*9126SWyllys.Ingersoll@Sun.COM }
785*9126SWyllys.Ingersoll@Sun.COM }
786*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
787*9126SWyllys.Ingersoll@Sun.COM
788*9126SWyllys.Ingersoll@Sun.COM case CKA_ALWAYS_SENSITIVE:
789*9126SWyllys.Ingersoll@Sun.COM case CKA_NEVER_EXTRACTABLE:
790*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
791*9126SWyllys.Ingersoll@Sun.COM
792*9126SWyllys.Ingersoll@Sun.COM default:
793*9126SWyllys.Ingersoll@Sun.COM return (key_object_validate_attribute(tmpl, attr, mode));
794*9126SWyllys.Ingersoll@Sun.COM }
795*9126SWyllys.Ingersoll@Sun.COM }
796*9126SWyllys.Ingersoll@Sun.COM
797*9126SWyllys.Ingersoll@Sun.COM CK_RV
secret_key_check_required_attributes(TEMPLATE * tmpl,CK_ULONG mode)798*9126SWyllys.Ingersoll@Sun.COM secret_key_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode)
799*9126SWyllys.Ingersoll@Sun.COM {
800*9126SWyllys.Ingersoll@Sun.COM return (key_object_check_required_attributes(tmpl, mode));
801*9126SWyllys.Ingersoll@Sun.COM }
802*9126SWyllys.Ingersoll@Sun.COM
803*9126SWyllys.Ingersoll@Sun.COM CK_RV
secret_key_set_default_attributes(TEMPLATE * tmpl,CK_ULONG mode)804*9126SWyllys.Ingersoll@Sun.COM secret_key_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode)
805*9126SWyllys.Ingersoll@Sun.COM {
806*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *class_attr = NULL;
807*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *sensitive_attr = NULL;
808*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *encrypt_attr = NULL;
809*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *decrypt_attr = NULL;
810*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *sign_attr = NULL;
811*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *verify_attr = NULL;
812*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *wrap_attr = NULL;
813*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *unwrap_attr = NULL;
814*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *extractable_attr = NULL;
815*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *never_extr_attr = NULL;
816*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *always_sens_attr = NULL;
817*9126SWyllys.Ingersoll@Sun.COM CK_RV rc;
818*9126SWyllys.Ingersoll@Sun.COM
819*9126SWyllys.Ingersoll@Sun.COM
820*9126SWyllys.Ingersoll@Sun.COM rc = key_object_set_default_attributes(tmpl, mode);
821*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK)
822*9126SWyllys.Ingersoll@Sun.COM return (rc);
823*9126SWyllys.Ingersoll@Sun.COM
824*9126SWyllys.Ingersoll@Sun.COM class_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
825*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_OBJECT_CLASS));
826*9126SWyllys.Ingersoll@Sun.COM sensitive_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
827*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
828*9126SWyllys.Ingersoll@Sun.COM encrypt_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
829*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
830*9126SWyllys.Ingersoll@Sun.COM decrypt_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
831*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
832*9126SWyllys.Ingersoll@Sun.COM sign_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
833*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
834*9126SWyllys.Ingersoll@Sun.COM verify_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
835*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
836*9126SWyllys.Ingersoll@Sun.COM wrap_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
837*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
838*9126SWyllys.Ingersoll@Sun.COM unwrap_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
839*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
840*9126SWyllys.Ingersoll@Sun.COM extractable_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
841*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
842*9126SWyllys.Ingersoll@Sun.COM never_extr_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
843*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
844*9126SWyllys.Ingersoll@Sun.COM always_sens_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
845*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_BBOOL));
846*9126SWyllys.Ingersoll@Sun.COM
847*9126SWyllys.Ingersoll@Sun.COM if (! class_attr || ! sensitive_attr || ! encrypt_attr ||
848*9126SWyllys.Ingersoll@Sun.COM ! decrypt_attr || ! sign_attr || ! verify_attr ||
849*9126SWyllys.Ingersoll@Sun.COM ! wrap_attr || ! unwrap_attr || ! extractable_attr ||
850*9126SWyllys.Ingersoll@Sun.COM ! never_extr_attr || ! always_sens_attr) {
851*9126SWyllys.Ingersoll@Sun.COM if (class_attr) free(class_attr);
852*9126SWyllys.Ingersoll@Sun.COM if (sensitive_attr) free(sensitive_attr);
853*9126SWyllys.Ingersoll@Sun.COM if (encrypt_attr) free(encrypt_attr);
854*9126SWyllys.Ingersoll@Sun.COM if (decrypt_attr) free(decrypt_attr);
855*9126SWyllys.Ingersoll@Sun.COM if (sign_attr) free(sign_attr);
856*9126SWyllys.Ingersoll@Sun.COM if (verify_attr) free(verify_attr);
857*9126SWyllys.Ingersoll@Sun.COM if (wrap_attr) free(wrap_attr);
858*9126SWyllys.Ingersoll@Sun.COM if (unwrap_attr) free(unwrap_attr);
859*9126SWyllys.Ingersoll@Sun.COM if (extractable_attr) free(extractable_attr);
860*9126SWyllys.Ingersoll@Sun.COM if (never_extr_attr) free(never_extr_attr);
861*9126SWyllys.Ingersoll@Sun.COM if (always_sens_attr) free(always_sens_attr);
862*9126SWyllys.Ingersoll@Sun.COM
863*9126SWyllys.Ingersoll@Sun.COM return (CKR_HOST_MEMORY);
864*9126SWyllys.Ingersoll@Sun.COM }
865*9126SWyllys.Ingersoll@Sun.COM
866*9126SWyllys.Ingersoll@Sun.COM class_attr->type = CKA_CLASS;
867*9126SWyllys.Ingersoll@Sun.COM class_attr->ulValueLen = sizeof (CK_OBJECT_CLASS);
868*9126SWyllys.Ingersoll@Sun.COM class_attr->pValue = (CK_BYTE *)class_attr +
869*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
870*9126SWyllys.Ingersoll@Sun.COM *(CK_OBJECT_CLASS *)class_attr->pValue = CKO_SECRET_KEY;
871*9126SWyllys.Ingersoll@Sun.COM
872*9126SWyllys.Ingersoll@Sun.COM sensitive_attr->type = CKA_SENSITIVE;
873*9126SWyllys.Ingersoll@Sun.COM sensitive_attr->ulValueLen = sizeof (CK_BBOOL);
874*9126SWyllys.Ingersoll@Sun.COM sensitive_attr->pValue = (CK_BYTE *)sensitive_attr +
875*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
876*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)sensitive_attr->pValue = FALSE;
877*9126SWyllys.Ingersoll@Sun.COM
878*9126SWyllys.Ingersoll@Sun.COM encrypt_attr->type = CKA_ENCRYPT;
879*9126SWyllys.Ingersoll@Sun.COM encrypt_attr->ulValueLen = sizeof (CK_BBOOL);
880*9126SWyllys.Ingersoll@Sun.COM encrypt_attr->pValue = (CK_BYTE *)encrypt_attr +
881*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
882*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)encrypt_attr->pValue = TRUE;
883*9126SWyllys.Ingersoll@Sun.COM
884*9126SWyllys.Ingersoll@Sun.COM decrypt_attr->type = CKA_DECRYPT;
885*9126SWyllys.Ingersoll@Sun.COM decrypt_attr->ulValueLen = sizeof (CK_BBOOL);
886*9126SWyllys.Ingersoll@Sun.COM decrypt_attr->pValue = (CK_BYTE *)decrypt_attr +
887*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
888*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)decrypt_attr->pValue = TRUE;
889*9126SWyllys.Ingersoll@Sun.COM
890*9126SWyllys.Ingersoll@Sun.COM sign_attr->type = CKA_SIGN;
891*9126SWyllys.Ingersoll@Sun.COM sign_attr->ulValueLen = sizeof (CK_BBOOL);
892*9126SWyllys.Ingersoll@Sun.COM sign_attr->pValue = (CK_BYTE *)sign_attr +
893*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
894*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)sign_attr->pValue = TRUE;
895*9126SWyllys.Ingersoll@Sun.COM
896*9126SWyllys.Ingersoll@Sun.COM verify_attr->type = CKA_VERIFY;
897*9126SWyllys.Ingersoll@Sun.COM verify_attr->ulValueLen = sizeof (CK_BBOOL);
898*9126SWyllys.Ingersoll@Sun.COM verify_attr->pValue = (CK_BYTE *)verify_attr +
899*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
900*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)verify_attr->pValue = TRUE;
901*9126SWyllys.Ingersoll@Sun.COM
902*9126SWyllys.Ingersoll@Sun.COM wrap_attr->type = CKA_WRAP;
903*9126SWyllys.Ingersoll@Sun.COM wrap_attr->ulValueLen = sizeof (CK_BBOOL);
904*9126SWyllys.Ingersoll@Sun.COM wrap_attr->pValue = (CK_BYTE *)wrap_attr +
905*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
906*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)wrap_attr->pValue = TRUE;
907*9126SWyllys.Ingersoll@Sun.COM
908*9126SWyllys.Ingersoll@Sun.COM unwrap_attr->type = CKA_UNWRAP;
909*9126SWyllys.Ingersoll@Sun.COM unwrap_attr->ulValueLen = sizeof (CK_BBOOL);
910*9126SWyllys.Ingersoll@Sun.COM unwrap_attr->pValue = (CK_BYTE *)unwrap_attr +
911*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
912*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)unwrap_attr->pValue = TRUE;
913*9126SWyllys.Ingersoll@Sun.COM
914*9126SWyllys.Ingersoll@Sun.COM extractable_attr->type = CKA_EXTRACTABLE;
915*9126SWyllys.Ingersoll@Sun.COM extractable_attr->ulValueLen = sizeof (CK_BBOOL);
916*9126SWyllys.Ingersoll@Sun.COM extractable_attr->pValue = (CK_BYTE *)extractable_attr +
917*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
918*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)extractable_attr->pValue = TRUE;
919*9126SWyllys.Ingersoll@Sun.COM
920*9126SWyllys.Ingersoll@Sun.COM always_sens_attr->type = CKA_ALWAYS_SENSITIVE;
921*9126SWyllys.Ingersoll@Sun.COM always_sens_attr->ulValueLen = sizeof (CK_BBOOL);
922*9126SWyllys.Ingersoll@Sun.COM always_sens_attr->pValue = (CK_BYTE *)always_sens_attr +
923*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
924*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)always_sens_attr->pValue = FALSE;
925*9126SWyllys.Ingersoll@Sun.COM
926*9126SWyllys.Ingersoll@Sun.COM never_extr_attr->type = CKA_NEVER_EXTRACTABLE;
927*9126SWyllys.Ingersoll@Sun.COM never_extr_attr->ulValueLen = sizeof (CK_BBOOL);
928*9126SWyllys.Ingersoll@Sun.COM never_extr_attr->pValue = (CK_BYTE *)never_extr_attr +
929*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
930*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)never_extr_attr->pValue = FALSE;
931*9126SWyllys.Ingersoll@Sun.COM
932*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, class_attr);
933*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, sensitive_attr);
934*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, encrypt_attr);
935*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, decrypt_attr);
936*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, sign_attr);
937*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, verify_attr);
938*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, wrap_attr);
939*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, unwrap_attr);
940*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, extractable_attr);
941*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, never_extr_attr);
942*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, always_sens_attr);
943*9126SWyllys.Ingersoll@Sun.COM
944*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
945*9126SWyllys.Ingersoll@Sun.COM }
946*9126SWyllys.Ingersoll@Sun.COM
947*9126SWyllys.Ingersoll@Sun.COM CK_RV
secret_key_unwrap(TEMPLATE * tmpl,CK_ULONG keytype,CK_BYTE * data,CK_ULONG data_len,CK_BBOOL fromend)948*9126SWyllys.Ingersoll@Sun.COM secret_key_unwrap(TEMPLATE *tmpl,
949*9126SWyllys.Ingersoll@Sun.COM CK_ULONG keytype,
950*9126SWyllys.Ingersoll@Sun.COM CK_BYTE *data,
951*9126SWyllys.Ingersoll@Sun.COM CK_ULONG data_len,
952*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL fromend)
953*9126SWyllys.Ingersoll@Sun.COM {
954*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *local = NULL;
955*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *always_sens = NULL;
956*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *sensitive = NULL;
957*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *extractable = NULL;
958*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *never_extract = NULL;
959*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL true = TRUE;
960*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL false = FALSE;
961*9126SWyllys.Ingersoll@Sun.COM CK_RV rc;
962*9126SWyllys.Ingersoll@Sun.COM
963*9126SWyllys.Ingersoll@Sun.COM switch (keytype) {
964*9126SWyllys.Ingersoll@Sun.COM case CKK_GENERIC_SECRET:
965*9126SWyllys.Ingersoll@Sun.COM rc = generic_secret_unwrap(tmpl, data, data_len, fromend);
966*9126SWyllys.Ingersoll@Sun.COM break;
967*9126SWyllys.Ingersoll@Sun.COM
968*9126SWyllys.Ingersoll@Sun.COM default:
969*9126SWyllys.Ingersoll@Sun.COM return (CKR_WRAPPED_KEY_INVALID);
970*9126SWyllys.Ingersoll@Sun.COM }
971*9126SWyllys.Ingersoll@Sun.COM
972*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK)
973*9126SWyllys.Ingersoll@Sun.COM return (rc);
974*9126SWyllys.Ingersoll@Sun.COM
975*9126SWyllys.Ingersoll@Sun.COM // make sure
976*9126SWyllys.Ingersoll@Sun.COM // CKA_LOCAL == FALSE
977*9126SWyllys.Ingersoll@Sun.COM // CKA_ALWAYS_SENSITIVE == FALSE
978*9126SWyllys.Ingersoll@Sun.COM // CKA_EXTRACTABLE == TRUE
979*9126SWyllys.Ingersoll@Sun.COM // CKA_NEVER_EXTRACTABLE == FALSE
980*9126SWyllys.Ingersoll@Sun.COM //
981*9126SWyllys.Ingersoll@Sun.COM rc = build_attribute(CKA_LOCAL, &false, 1, &local);
982*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
983*9126SWyllys.Ingersoll@Sun.COM goto cleanup;
984*9126SWyllys.Ingersoll@Sun.COM }
985*9126SWyllys.Ingersoll@Sun.COM rc = build_attribute(CKA_ALWAYS_SENSITIVE, &false, 1, &always_sens);
986*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
987*9126SWyllys.Ingersoll@Sun.COM goto cleanup;
988*9126SWyllys.Ingersoll@Sun.COM }
989*9126SWyllys.Ingersoll@Sun.COM rc = build_attribute(CKA_SENSITIVE, &false, 1, &sensitive);
990*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
991*9126SWyllys.Ingersoll@Sun.COM goto cleanup;
992*9126SWyllys.Ingersoll@Sun.COM }
993*9126SWyllys.Ingersoll@Sun.COM rc = build_attribute(CKA_EXTRACTABLE, &true, 1, &extractable);
994*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
995*9126SWyllys.Ingersoll@Sun.COM goto cleanup;
996*9126SWyllys.Ingersoll@Sun.COM }
997*9126SWyllys.Ingersoll@Sun.COM rc = build_attribute(CKA_NEVER_EXTRACTABLE, &false, 1, &never_extract);
998*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
999*9126SWyllys.Ingersoll@Sun.COM goto cleanup;
1000*9126SWyllys.Ingersoll@Sun.COM }
1001*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, local);
1002*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, always_sens);
1003*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, sensitive);
1004*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, extractable);
1005*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, never_extract);
1006*9126SWyllys.Ingersoll@Sun.COM
1007*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
1008*9126SWyllys.Ingersoll@Sun.COM
1009*9126SWyllys.Ingersoll@Sun.COM cleanup:
1010*9126SWyllys.Ingersoll@Sun.COM
1011*9126SWyllys.Ingersoll@Sun.COM if (local) free(local);
1012*9126SWyllys.Ingersoll@Sun.COM if (extractable) free(extractable);
1013*9126SWyllys.Ingersoll@Sun.COM if (always_sens) free(always_sens);
1014*9126SWyllys.Ingersoll@Sun.COM if (never_extract) free(never_extract);
1015*9126SWyllys.Ingersoll@Sun.COM
1016*9126SWyllys.Ingersoll@Sun.COM return (rc);
1017*9126SWyllys.Ingersoll@Sun.COM }
1018*9126SWyllys.Ingersoll@Sun.COM
1019*9126SWyllys.Ingersoll@Sun.COM CK_RV
secret_key_validate_attribute(TEMPLATE * tmpl,CK_ATTRIBUTE * attr,CK_ULONG mode)1020*9126SWyllys.Ingersoll@Sun.COM secret_key_validate_attribute(TEMPLATE *tmpl, CK_ATTRIBUTE *attr,
1021*9126SWyllys.Ingersoll@Sun.COM CK_ULONG mode)
1022*9126SWyllys.Ingersoll@Sun.COM {
1023*9126SWyllys.Ingersoll@Sun.COM switch (attr->type) {
1024*9126SWyllys.Ingersoll@Sun.COM case CKA_ENCRYPT:
1025*9126SWyllys.Ingersoll@Sun.COM case CKA_DECRYPT:
1026*9126SWyllys.Ingersoll@Sun.COM case CKA_SIGN:
1027*9126SWyllys.Ingersoll@Sun.COM case CKA_VERIFY:
1028*9126SWyllys.Ingersoll@Sun.COM case CKA_WRAP:
1029*9126SWyllys.Ingersoll@Sun.COM case CKA_UNWRAP:
1030*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_MODIFY) {
1031*9126SWyllys.Ingersoll@Sun.COM if (nv_token_data->tweak_vector.allow_key_mods == TRUE)
1032*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
1033*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
1034*9126SWyllys.Ingersoll@Sun.COM }
1035*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
1036*9126SWyllys.Ingersoll@Sun.COM
1037*9126SWyllys.Ingersoll@Sun.COM // after key creation, CKA_SENSITIVE may only be set to TRUE
1038*9126SWyllys.Ingersoll@Sun.COM //
1039*9126SWyllys.Ingersoll@Sun.COM case CKA_SENSITIVE:
1040*9126SWyllys.Ingersoll@Sun.COM {
1041*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL value;
1042*9126SWyllys.Ingersoll@Sun.COM
1043*9126SWyllys.Ingersoll@Sun.COM value = *(CK_BBOOL *)attr->pValue;
1044*9126SWyllys.Ingersoll@Sun.COM if ((mode != MODE_CREATE && mode != MODE_DERIVE &&
1045*9126SWyllys.Ingersoll@Sun.COM mode != MODE_KEYGEN) && (value != TRUE)) {
1046*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
1047*9126SWyllys.Ingersoll@Sun.COM }
1048*9126SWyllys.Ingersoll@Sun.COM }
1049*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
1050*9126SWyllys.Ingersoll@Sun.COM
1051*9126SWyllys.Ingersoll@Sun.COM case CKA_EXTRACTABLE:
1052*9126SWyllys.Ingersoll@Sun.COM {
1053*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL value;
1054*9126SWyllys.Ingersoll@Sun.COM
1055*9126SWyllys.Ingersoll@Sun.COM value = *(CK_BBOOL *)attr->pValue;
1056*9126SWyllys.Ingersoll@Sun.COM if ((mode != MODE_CREATE && mode != MODE_DERIVE &&
1057*9126SWyllys.Ingersoll@Sun.COM mode != MODE_KEYGEN) && (value != FALSE)) {
1058*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
1059*9126SWyllys.Ingersoll@Sun.COM }
1060*9126SWyllys.Ingersoll@Sun.COM if (value == FALSE) {
1061*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *attr;
1062*9126SWyllys.Ingersoll@Sun.COM
1063*9126SWyllys.Ingersoll@Sun.COM attr = (CK_ATTRIBUTE *)malloc(
1064*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE) + sizeof (CK_BBOOL));
1065*9126SWyllys.Ingersoll@Sun.COM if (! attr) {
1066*9126SWyllys.Ingersoll@Sun.COM return (CKR_HOST_MEMORY);
1067*9126SWyllys.Ingersoll@Sun.COM }
1068*9126SWyllys.Ingersoll@Sun.COM attr->type = CKA_NEVER_EXTRACTABLE;
1069*9126SWyllys.Ingersoll@Sun.COM attr->ulValueLen = sizeof (CK_BBOOL);
1070*9126SWyllys.Ingersoll@Sun.COM attr->pValue = (CK_BYTE *)attr +
1071*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
1072*9126SWyllys.Ingersoll@Sun.COM *(CK_BBOOL *)attr->pValue = FALSE;
1073*9126SWyllys.Ingersoll@Sun.COM
1074*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, attr);
1075*9126SWyllys.Ingersoll@Sun.COM }
1076*9126SWyllys.Ingersoll@Sun.COM }
1077*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
1078*9126SWyllys.Ingersoll@Sun.COM
1079*9126SWyllys.Ingersoll@Sun.COM case CKA_ALWAYS_SENSITIVE:
1080*9126SWyllys.Ingersoll@Sun.COM case CKA_NEVER_EXTRACTABLE:
1081*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
1082*9126SWyllys.Ingersoll@Sun.COM
1083*9126SWyllys.Ingersoll@Sun.COM default:
1084*9126SWyllys.Ingersoll@Sun.COM return (key_object_validate_attribute(tmpl,
1085*9126SWyllys.Ingersoll@Sun.COM attr, mode));
1086*9126SWyllys.Ingersoll@Sun.COM }
1087*9126SWyllys.Ingersoll@Sun.COM }
1088*9126SWyllys.Ingersoll@Sun.COM
1089*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL
secret_key_check_exportability(CK_ATTRIBUTE_TYPE type)1090*9126SWyllys.Ingersoll@Sun.COM secret_key_check_exportability(CK_ATTRIBUTE_TYPE type)
1091*9126SWyllys.Ingersoll@Sun.COM {
1092*9126SWyllys.Ingersoll@Sun.COM switch (type) {
1093*9126SWyllys.Ingersoll@Sun.COM case CKA_VALUE:
1094*9126SWyllys.Ingersoll@Sun.COM return (FALSE);
1095*9126SWyllys.Ingersoll@Sun.COM }
1096*9126SWyllys.Ingersoll@Sun.COM
1097*9126SWyllys.Ingersoll@Sun.COM return (TRUE);
1098*9126SWyllys.Ingersoll@Sun.COM }
1099*9126SWyllys.Ingersoll@Sun.COM
1100*9126SWyllys.Ingersoll@Sun.COM CK_RV
rsa_publ_check_required_attributes(TEMPLATE * tmpl,CK_ULONG mode)1101*9126SWyllys.Ingersoll@Sun.COM rsa_publ_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode)
1102*9126SWyllys.Ingersoll@Sun.COM {
1103*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *attr = NULL;
1104*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL found;
1105*9126SWyllys.Ingersoll@Sun.COM
1106*9126SWyllys.Ingersoll@Sun.COM found = template_attribute_find(tmpl, CKA_MODULUS, &attr);
1107*9126SWyllys.Ingersoll@Sun.COM if (! found) {
1108*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE) {
1109*9126SWyllys.Ingersoll@Sun.COM return (CKR_TEMPLATE_INCOMPLETE);
1110*9126SWyllys.Ingersoll@Sun.COM }
1111*9126SWyllys.Ingersoll@Sun.COM }
1112*9126SWyllys.Ingersoll@Sun.COM
1113*9126SWyllys.Ingersoll@Sun.COM found = template_attribute_find(tmpl, CKA_MODULUS_BITS, &attr);
1114*9126SWyllys.Ingersoll@Sun.COM if (! found) {
1115*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_KEYGEN) {
1116*9126SWyllys.Ingersoll@Sun.COM return (CKR_TEMPLATE_INCOMPLETE);
1117*9126SWyllys.Ingersoll@Sun.COM }
1118*9126SWyllys.Ingersoll@Sun.COM }
1119*9126SWyllys.Ingersoll@Sun.COM
1120*9126SWyllys.Ingersoll@Sun.COM found = template_attribute_find(tmpl, CKA_PUBLIC_EXPONENT, &attr);
1121*9126SWyllys.Ingersoll@Sun.COM if (! found) {
1122*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE || mode == MODE_KEYGEN) {
1123*9126SWyllys.Ingersoll@Sun.COM return (CKR_TEMPLATE_INCOMPLETE);
1124*9126SWyllys.Ingersoll@Sun.COM }
1125*9126SWyllys.Ingersoll@Sun.COM }
1126*9126SWyllys.Ingersoll@Sun.COM
1127*9126SWyllys.Ingersoll@Sun.COM return (publ_key_check_required_attributes(tmpl, mode));
1128*9126SWyllys.Ingersoll@Sun.COM }
1129*9126SWyllys.Ingersoll@Sun.COM
1130*9126SWyllys.Ingersoll@Sun.COM CK_RV
rsa_publ_set_default_attributes(TEMPLATE * tmpl,CK_ULONG mode)1131*9126SWyllys.Ingersoll@Sun.COM rsa_publ_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode)
1132*9126SWyllys.Ingersoll@Sun.COM {
1133*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *type_attr = NULL;
1134*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *modulus_attr = NULL;
1135*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *modulus_bits_attr = NULL;
1136*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *public_exp_attr = NULL;
1137*9126SWyllys.Ingersoll@Sun.COM CK_ULONG bits = 0L;
1138*9126SWyllys.Ingersoll@Sun.COM
1139*9126SWyllys.Ingersoll@Sun.COM (void) publ_key_set_default_attributes(tmpl, mode);
1140*9126SWyllys.Ingersoll@Sun.COM
1141*9126SWyllys.Ingersoll@Sun.COM type_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
1142*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_KEY_TYPE));
1143*9126SWyllys.Ingersoll@Sun.COM modulus_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE));
1144*9126SWyllys.Ingersoll@Sun.COM modulus_bits_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
1145*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ULONG));
1146*9126SWyllys.Ingersoll@Sun.COM public_exp_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE));
1147*9126SWyllys.Ingersoll@Sun.COM
1148*9126SWyllys.Ingersoll@Sun.COM if (! type_attr || ! modulus_attr ||
1149*9126SWyllys.Ingersoll@Sun.COM ! modulus_bits_attr || ! public_exp_attr) {
1150*9126SWyllys.Ingersoll@Sun.COM if (type_attr) free(type_attr);
1151*9126SWyllys.Ingersoll@Sun.COM if (modulus_attr) free(modulus_attr);
1152*9126SWyllys.Ingersoll@Sun.COM if (modulus_bits_attr) free(modulus_bits_attr);
1153*9126SWyllys.Ingersoll@Sun.COM if (public_exp_attr) free(public_exp_attr);
1154*9126SWyllys.Ingersoll@Sun.COM
1155*9126SWyllys.Ingersoll@Sun.COM return (CKR_HOST_MEMORY);
1156*9126SWyllys.Ingersoll@Sun.COM }
1157*9126SWyllys.Ingersoll@Sun.COM
1158*9126SWyllys.Ingersoll@Sun.COM type_attr->type = CKA_KEY_TYPE;
1159*9126SWyllys.Ingersoll@Sun.COM type_attr->ulValueLen = sizeof (CK_KEY_TYPE);
1160*9126SWyllys.Ingersoll@Sun.COM type_attr->pValue = (CK_BYTE *)type_attr +
1161*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
1162*9126SWyllys.Ingersoll@Sun.COM *(CK_KEY_TYPE *)type_attr->pValue = CKK_RSA;
1163*9126SWyllys.Ingersoll@Sun.COM
1164*9126SWyllys.Ingersoll@Sun.COM modulus_attr->type = CKA_MODULUS;
1165*9126SWyllys.Ingersoll@Sun.COM modulus_attr->ulValueLen = 0;
1166*9126SWyllys.Ingersoll@Sun.COM modulus_attr->pValue = NULL;
1167*9126SWyllys.Ingersoll@Sun.COM
1168*9126SWyllys.Ingersoll@Sun.COM modulus_bits_attr->type = CKA_MODULUS_BITS;
1169*9126SWyllys.Ingersoll@Sun.COM modulus_bits_attr->ulValueLen = sizeof (CK_ULONG);
1170*9126SWyllys.Ingersoll@Sun.COM modulus_bits_attr->pValue = (CK_BYTE *)modulus_bits_attr +
1171*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
1172*9126SWyllys.Ingersoll@Sun.COM *(CK_ULONG *)modulus_bits_attr->pValue = bits;
1173*9126SWyllys.Ingersoll@Sun.COM
1174*9126SWyllys.Ingersoll@Sun.COM public_exp_attr->type = CKA_PUBLIC_EXPONENT;
1175*9126SWyllys.Ingersoll@Sun.COM public_exp_attr->ulValueLen = 0;
1176*9126SWyllys.Ingersoll@Sun.COM public_exp_attr->pValue = NULL;
1177*9126SWyllys.Ingersoll@Sun.COM
1178*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, type_attr);
1179*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, modulus_attr);
1180*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, modulus_bits_attr);
1181*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, public_exp_attr);
1182*9126SWyllys.Ingersoll@Sun.COM
1183*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
1184*9126SWyllys.Ingersoll@Sun.COM }
1185*9126SWyllys.Ingersoll@Sun.COM
1186*9126SWyllys.Ingersoll@Sun.COM CK_RV
rsa_publ_validate_attribute(TEMPLATE * tmpl,CK_ATTRIBUTE * attr,CK_ULONG mode)1187*9126SWyllys.Ingersoll@Sun.COM rsa_publ_validate_attribute(TEMPLATE *tmpl, CK_ATTRIBUTE *attr, CK_ULONG mode) {
1188*9126SWyllys.Ingersoll@Sun.COM switch (attr->type) {
1189*9126SWyllys.Ingersoll@Sun.COM case CKA_MODULUS_BITS:
1190*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_KEYGEN) {
1191*9126SWyllys.Ingersoll@Sun.COM if (attr->ulValueLen != sizeof (CK_ULONG)) {
1192*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_VALUE_INVALID);
1193*9126SWyllys.Ingersoll@Sun.COM } else {
1194*9126SWyllys.Ingersoll@Sun.COM CK_ULONG mod_bits = *(CK_ULONG *)attr->pValue;
1195*9126SWyllys.Ingersoll@Sun.COM
1196*9126SWyllys.Ingersoll@Sun.COM if (mod_bits < 512 || mod_bits > 2048) {
1197*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_VALUE_INVALID);
1198*9126SWyllys.Ingersoll@Sun.COM }
1199*9126SWyllys.Ingersoll@Sun.COM
1200*9126SWyllys.Ingersoll@Sun.COM if (mod_bits % 8 != 0) {
1201*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_VALUE_INVALID);
1202*9126SWyllys.Ingersoll@Sun.COM }
1203*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
1204*9126SWyllys.Ingersoll@Sun.COM }
1205*9126SWyllys.Ingersoll@Sun.COM } else {
1206*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
1207*9126SWyllys.Ingersoll@Sun.COM }
1208*9126SWyllys.Ingersoll@Sun.COM case CKA_MODULUS:
1209*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE)
1210*9126SWyllys.Ingersoll@Sun.COM return (remove_leading_zeros(attr));
1211*9126SWyllys.Ingersoll@Sun.COM else {
1212*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
1213*9126SWyllys.Ingersoll@Sun.COM }
1214*9126SWyllys.Ingersoll@Sun.COM case CKA_PUBLIC_EXPONENT:
1215*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE || mode == MODE_KEYGEN)
1216*9126SWyllys.Ingersoll@Sun.COM return (remove_leading_zeros(attr));
1217*9126SWyllys.Ingersoll@Sun.COM else {
1218*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
1219*9126SWyllys.Ingersoll@Sun.COM }
1220*9126SWyllys.Ingersoll@Sun.COM default:
1221*9126SWyllys.Ingersoll@Sun.COM return (publ_key_validate_attribute(tmpl, attr, mode));
1222*9126SWyllys.Ingersoll@Sun.COM }
1223*9126SWyllys.Ingersoll@Sun.COM }
1224*9126SWyllys.Ingersoll@Sun.COM
1225*9126SWyllys.Ingersoll@Sun.COM CK_RV
rsa_priv_check_required_attributes(TEMPLATE * tmpl,CK_ULONG mode)1226*9126SWyllys.Ingersoll@Sun.COM rsa_priv_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode) {
1227*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *attr = NULL;
1228*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL found;
1229*9126SWyllys.Ingersoll@Sun.COM
1230*9126SWyllys.Ingersoll@Sun.COM found = template_attribute_find(tmpl, CKA_MODULUS, &attr);
1231*9126SWyllys.Ingersoll@Sun.COM if (! found) {
1232*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE) {
1233*9126SWyllys.Ingersoll@Sun.COM return (CKR_TEMPLATE_INCOMPLETE);
1234*9126SWyllys.Ingersoll@Sun.COM }
1235*9126SWyllys.Ingersoll@Sun.COM }
1236*9126SWyllys.Ingersoll@Sun.COM
1237*9126SWyllys.Ingersoll@Sun.COM //
1238*9126SWyllys.Ingersoll@Sun.COM // PKCS #11 is flexible with respect to which attributes must be present
1239*9126SWyllys.Ingersoll@Sun.COM // in an RSA key. Keys can be specified in Chinese Remainder format or
1240*9126SWyllys.Ingersoll@Sun.COM // they can be specified in modula exponent format. Right now, I only
1241*9126SWyllys.Ingersoll@Sun.COM // support keys created in Chinese Remainder format. That is, we return
1242*9126SWyllys.Ingersoll@Sun.COM // CKR_TEMPLATE_INCOMPLETE if a modula exponent key is specified. This
1243*9126SWyllys.Ingersoll@Sun.COM // is allowed by PKCS #11.
1244*9126SWyllys.Ingersoll@Sun.COM //
1245*9126SWyllys.Ingersoll@Sun.COM // In the future, we should allow for creation of keys in modula exp
1246*9126SWyllys.Ingersoll@Sun.COM // format too. This raises some issues. It's easy enough to recognize
1247*9126SWyllys.Ingersoll@Sun.COM // when a key has been specified in modula exponent format. And it's
1248*9126SWyllys.Ingersoll@Sun.COM // easy enough to recognize when all attributes have been specified
1249*9126SWyllys.Ingersoll@Sun.COM // (which is what we require right now). What's trickier to handle is
1250*9126SWyllys.Ingersoll@Sun.COM // the "middle" cases in which more than the minimum yet less than the
1251*9126SWyllys.Ingersoll@Sun.COM // full number of attributes have been specified. Do we revert back to
1252*9126SWyllys.Ingersoll@Sun.COM // modula - exponent representation? Do we compute the missing
1253*9126SWyllys.Ingersoll@Sun.COM // attributes ourselves? Do we simply return CKR_TEMPLATE_INCOMPLETE?
1254*9126SWyllys.Ingersoll@Sun.COM //
1255*9126SWyllys.Ingersoll@Sun.COM
1256*9126SWyllys.Ingersoll@Sun.COM found = template_attribute_find(tmpl, CKA_PUBLIC_EXPONENT, &attr);
1257*9126SWyllys.Ingersoll@Sun.COM if (! found) {
1258*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE) {
1259*9126SWyllys.Ingersoll@Sun.COM return (CKR_TEMPLATE_INCOMPLETE);
1260*9126SWyllys.Ingersoll@Sun.COM }
1261*9126SWyllys.Ingersoll@Sun.COM }
1262*9126SWyllys.Ingersoll@Sun.COM
1263*9126SWyllys.Ingersoll@Sun.COM found = template_attribute_find(tmpl, CKA_PRIVATE_EXPONENT, &attr);
1264*9126SWyllys.Ingersoll@Sun.COM if (! found) {
1265*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE) {
1266*9126SWyllys.Ingersoll@Sun.COM return (CKR_TEMPLATE_INCOMPLETE);
1267*9126SWyllys.Ingersoll@Sun.COM }
1268*9126SWyllys.Ingersoll@Sun.COM }
1269*9126SWyllys.Ingersoll@Sun.COM
1270*9126SWyllys.Ingersoll@Sun.COM found = template_attribute_find(tmpl, CKA_PRIME_1, &attr);
1271*9126SWyllys.Ingersoll@Sun.COM if (! found) {
1272*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE) {
1273*9126SWyllys.Ingersoll@Sun.COM return (CKR_TEMPLATE_INCOMPLETE);
1274*9126SWyllys.Ingersoll@Sun.COM }
1275*9126SWyllys.Ingersoll@Sun.COM }
1276*9126SWyllys.Ingersoll@Sun.COM
1277*9126SWyllys.Ingersoll@Sun.COM found = template_attribute_find(tmpl, CKA_PRIME_2, &attr);
1278*9126SWyllys.Ingersoll@Sun.COM if (! found) {
1279*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE) {
1280*9126SWyllys.Ingersoll@Sun.COM return (CKR_TEMPLATE_INCOMPLETE);
1281*9126SWyllys.Ingersoll@Sun.COM }
1282*9126SWyllys.Ingersoll@Sun.COM }
1283*9126SWyllys.Ingersoll@Sun.COM
1284*9126SWyllys.Ingersoll@Sun.COM found = template_attribute_find(tmpl, CKA_EXPONENT_1, &attr);
1285*9126SWyllys.Ingersoll@Sun.COM if (! found) {
1286*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE) {
1287*9126SWyllys.Ingersoll@Sun.COM return (CKR_TEMPLATE_INCOMPLETE);
1288*9126SWyllys.Ingersoll@Sun.COM }
1289*9126SWyllys.Ingersoll@Sun.COM }
1290*9126SWyllys.Ingersoll@Sun.COM
1291*9126SWyllys.Ingersoll@Sun.COM found = template_attribute_find(tmpl, CKA_EXPONENT_2, &attr);
1292*9126SWyllys.Ingersoll@Sun.COM if (! found) {
1293*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE) {
1294*9126SWyllys.Ingersoll@Sun.COM return (CKR_TEMPLATE_INCOMPLETE);
1295*9126SWyllys.Ingersoll@Sun.COM }
1296*9126SWyllys.Ingersoll@Sun.COM }
1297*9126SWyllys.Ingersoll@Sun.COM
1298*9126SWyllys.Ingersoll@Sun.COM found = template_attribute_find(tmpl, CKA_COEFFICIENT, &attr);
1299*9126SWyllys.Ingersoll@Sun.COM if (! found) {
1300*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE) {
1301*9126SWyllys.Ingersoll@Sun.COM return (CKR_TEMPLATE_INCOMPLETE);
1302*9126SWyllys.Ingersoll@Sun.COM }
1303*9126SWyllys.Ingersoll@Sun.COM }
1304*9126SWyllys.Ingersoll@Sun.COM
1305*9126SWyllys.Ingersoll@Sun.COM return (priv_key_check_required_attributes(tmpl, mode));
1306*9126SWyllys.Ingersoll@Sun.COM }
1307*9126SWyllys.Ingersoll@Sun.COM
1308*9126SWyllys.Ingersoll@Sun.COM CK_RV
rsa_priv_set_default_attributes(TEMPLATE * tmpl,CK_ULONG mode)1309*9126SWyllys.Ingersoll@Sun.COM rsa_priv_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode)
1310*9126SWyllys.Ingersoll@Sun.COM {
1311*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *modulus_attr = NULL;
1312*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *public_exp_attr = NULL;
1313*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *private_exp_attr = NULL;
1314*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *type_attr = NULL;
1315*9126SWyllys.Ingersoll@Sun.COM
1316*9126SWyllys.Ingersoll@Sun.COM (void) priv_key_set_default_attributes(tmpl, mode);
1317*9126SWyllys.Ingersoll@Sun.COM
1318*9126SWyllys.Ingersoll@Sun.COM type_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
1319*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_KEY_TYPE));
1320*9126SWyllys.Ingersoll@Sun.COM modulus_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE));
1321*9126SWyllys.Ingersoll@Sun.COM public_exp_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE));
1322*9126SWyllys.Ingersoll@Sun.COM private_exp_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE));
1323*9126SWyllys.Ingersoll@Sun.COM
1324*9126SWyllys.Ingersoll@Sun.COM if (! type_attr || ! modulus_attr || ! public_exp_attr ||
1325*9126SWyllys.Ingersoll@Sun.COM ! private_exp_attr) {
1326*9126SWyllys.Ingersoll@Sun.COM if (type_attr) free(type_attr);
1327*9126SWyllys.Ingersoll@Sun.COM if (modulus_attr) free(modulus_attr);
1328*9126SWyllys.Ingersoll@Sun.COM if (public_exp_attr) free(public_exp_attr);
1329*9126SWyllys.Ingersoll@Sun.COM if (private_exp_attr) free(private_exp_attr);
1330*9126SWyllys.Ingersoll@Sun.COM
1331*9126SWyllys.Ingersoll@Sun.COM return (CKR_HOST_MEMORY);
1332*9126SWyllys.Ingersoll@Sun.COM }
1333*9126SWyllys.Ingersoll@Sun.COM
1334*9126SWyllys.Ingersoll@Sun.COM modulus_attr->type = CKA_MODULUS;
1335*9126SWyllys.Ingersoll@Sun.COM modulus_attr->ulValueLen = 0;
1336*9126SWyllys.Ingersoll@Sun.COM modulus_attr->pValue = NULL;
1337*9126SWyllys.Ingersoll@Sun.COM
1338*9126SWyllys.Ingersoll@Sun.COM public_exp_attr->type = CKA_PUBLIC_EXPONENT;
1339*9126SWyllys.Ingersoll@Sun.COM public_exp_attr->ulValueLen = 0;
1340*9126SWyllys.Ingersoll@Sun.COM public_exp_attr->pValue = NULL;
1341*9126SWyllys.Ingersoll@Sun.COM
1342*9126SWyllys.Ingersoll@Sun.COM private_exp_attr->type = CKA_PRIVATE_EXPONENT;
1343*9126SWyllys.Ingersoll@Sun.COM private_exp_attr->ulValueLen = 0;
1344*9126SWyllys.Ingersoll@Sun.COM private_exp_attr->pValue = NULL;
1345*9126SWyllys.Ingersoll@Sun.COM
1346*9126SWyllys.Ingersoll@Sun.COM type_attr->type = CKA_KEY_TYPE;
1347*9126SWyllys.Ingersoll@Sun.COM type_attr->ulValueLen = sizeof (CK_KEY_TYPE);
1348*9126SWyllys.Ingersoll@Sun.COM type_attr->pValue = (CK_BYTE *)type_attr + sizeof (CK_ATTRIBUTE);
1349*9126SWyllys.Ingersoll@Sun.COM *(CK_KEY_TYPE *)type_attr->pValue = CKK_RSA;
1350*9126SWyllys.Ingersoll@Sun.COM
1351*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, type_attr);
1352*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, modulus_attr);
1353*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, public_exp_attr);
1354*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, private_exp_attr);
1355*9126SWyllys.Ingersoll@Sun.COM
1356*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
1357*9126SWyllys.Ingersoll@Sun.COM }
1358*9126SWyllys.Ingersoll@Sun.COM
1359*9126SWyllys.Ingersoll@Sun.COM CK_RV
rsa_priv_validate_attribute(TEMPLATE * tmpl,CK_ATTRIBUTE * attr,CK_ULONG mode)1360*9126SWyllys.Ingersoll@Sun.COM rsa_priv_validate_attribute(TEMPLATE *tmpl, CK_ATTRIBUTE *attr, CK_ULONG mode) {
1361*9126SWyllys.Ingersoll@Sun.COM switch (attr->type) {
1362*9126SWyllys.Ingersoll@Sun.COM case CKA_MODULUS:
1363*9126SWyllys.Ingersoll@Sun.COM case CKA_PRIVATE_EXPONENT:
1364*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE)
1365*9126SWyllys.Ingersoll@Sun.COM return (remove_leading_zeros(attr));
1366*9126SWyllys.Ingersoll@Sun.COM else {
1367*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
1368*9126SWyllys.Ingersoll@Sun.COM }
1369*9126SWyllys.Ingersoll@Sun.COM case CKA_PUBLIC_EXPONENT:
1370*9126SWyllys.Ingersoll@Sun.COM case CKA_PRIME_1:
1371*9126SWyllys.Ingersoll@Sun.COM case CKA_PRIME_2:
1372*9126SWyllys.Ingersoll@Sun.COM case CKA_EXPONENT_1:
1373*9126SWyllys.Ingersoll@Sun.COM case CKA_EXPONENT_2:
1374*9126SWyllys.Ingersoll@Sun.COM case CKA_COEFFICIENT:
1375*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE)
1376*9126SWyllys.Ingersoll@Sun.COM return (remove_leading_zeros(attr));
1377*9126SWyllys.Ingersoll@Sun.COM else {
1378*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
1379*9126SWyllys.Ingersoll@Sun.COM }
1380*9126SWyllys.Ingersoll@Sun.COM default:
1381*9126SWyllys.Ingersoll@Sun.COM return (priv_key_validate_attribute(tmpl, attr, mode));
1382*9126SWyllys.Ingersoll@Sun.COM }
1383*9126SWyllys.Ingersoll@Sun.COM }
1384*9126SWyllys.Ingersoll@Sun.COM
1385*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL
rsa_priv_check_exportability(CK_ATTRIBUTE_TYPE type)1386*9126SWyllys.Ingersoll@Sun.COM rsa_priv_check_exportability(CK_ATTRIBUTE_TYPE type) {
1387*9126SWyllys.Ingersoll@Sun.COM switch (type) {
1388*9126SWyllys.Ingersoll@Sun.COM case CKA_PRIVATE_EXPONENT:
1389*9126SWyllys.Ingersoll@Sun.COM case CKA_PRIME_1:
1390*9126SWyllys.Ingersoll@Sun.COM case CKA_PRIME_2:
1391*9126SWyllys.Ingersoll@Sun.COM case CKA_EXPONENT_1:
1392*9126SWyllys.Ingersoll@Sun.COM case CKA_EXPONENT_2:
1393*9126SWyllys.Ingersoll@Sun.COM case CKA_COEFFICIENT:
1394*9126SWyllys.Ingersoll@Sun.COM return (FALSE);
1395*9126SWyllys.Ingersoll@Sun.COM }
1396*9126SWyllys.Ingersoll@Sun.COM
1397*9126SWyllys.Ingersoll@Sun.COM return (TRUE);
1398*9126SWyllys.Ingersoll@Sun.COM }
1399*9126SWyllys.Ingersoll@Sun.COM
1400*9126SWyllys.Ingersoll@Sun.COM
1401*9126SWyllys.Ingersoll@Sun.COM /*
1402*9126SWyllys.Ingersoll@Sun.COM * create the ASN.1 encoding for the private key for wrapping as defined
1403*9126SWyllys.Ingersoll@Sun.COM * in PKCS #8
1404*9126SWyllys.Ingersoll@Sun.COM *
1405*9126SWyllys.Ingersoll@Sun.COM * ASN.1 type PrivateKeyInfo ::= SEQUENCE {
1406*9126SWyllys.Ingersoll@Sun.COM * version Version
1407*9126SWyllys.Ingersoll@Sun.COM * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier
1408*9126SWyllys.Ingersoll@Sun.COM * privateKey PrivateKey
1409*9126SWyllys.Ingersoll@Sun.COM * attributes OPTIONAL
1410*9126SWyllys.Ingersoll@Sun.COM *
1411*9126SWyllys.Ingersoll@Sun.COM * }
1412*9126SWyllys.Ingersoll@Sun.COM *
1413*9126SWyllys.Ingersoll@Sun.COM * Where PrivateKey is defined as follows for RSA:
1414*9126SWyllys.Ingersoll@Sun.COM *
1415*9126SWyllys.Ingersoll@Sun.COM * ASN.1 type RSAPrivateKey
1416*9126SWyllys.Ingersoll@Sun.COM *
1417*9126SWyllys.Ingersoll@Sun.COM * RSAPrivateKey ::= SEQUENCE {
1418*9126SWyllys.Ingersoll@Sun.COM * version Version
1419*9126SWyllys.Ingersoll@Sun.COM * modulus INTEGER
1420*9126SWyllys.Ingersoll@Sun.COM * publicExponent INTEGER
1421*9126SWyllys.Ingersoll@Sun.COM * privateExponent INTEGER
1422*9126SWyllys.Ingersoll@Sun.COM * prime1 INTEGER
1423*9126SWyllys.Ingersoll@Sun.COM * prime2 INTEGER
1424*9126SWyllys.Ingersoll@Sun.COM * exponent1 INTEGER
1425*9126SWyllys.Ingersoll@Sun.COM * exponent2 INTEGER
1426*9126SWyllys.Ingersoll@Sun.COM * coefficient INTEGER
1427*9126SWyllys.Ingersoll@Sun.COM * }
1428*9126SWyllys.Ingersoll@Sun.COM */
1429*9126SWyllys.Ingersoll@Sun.COM CK_RV
rsa_priv_wrap_get_data(TEMPLATE * tmpl,CK_BBOOL length_only,CK_BYTE ** data,CK_ULONG * data_len)1430*9126SWyllys.Ingersoll@Sun.COM rsa_priv_wrap_get_data(TEMPLATE *tmpl,
1431*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL length_only,
1432*9126SWyllys.Ingersoll@Sun.COM CK_BYTE **data,
1433*9126SWyllys.Ingersoll@Sun.COM CK_ULONG *data_len)
1434*9126SWyllys.Ingersoll@Sun.COM {
1435*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *modulus = NULL;
1436*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *publ_exp = NULL, *priv_exp = NULL;
1437*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *prime1 = NULL, *prime2 = NULL;
1438*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *exponent1 = NULL, *exponent2 = NULL;
1439*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *coeff = NULL;
1440*9126SWyllys.Ingersoll@Sun.COM CK_RV rc;
1441*9126SWyllys.Ingersoll@Sun.COM
1442*9126SWyllys.Ingersoll@Sun.COM if (template_attribute_find(tmpl, CKA_MODULUS, &modulus) == FALSE) {
1443*9126SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_FAILED);
1444*9126SWyllys.Ingersoll@Sun.COM }
1445*9126SWyllys.Ingersoll@Sun.COM if (template_attribute_find(tmpl, CKA_PUBLIC_EXPONENT,
1446*9126SWyllys.Ingersoll@Sun.COM &publ_exp) == FALSE) {
1447*9126SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_FAILED);
1448*9126SWyllys.Ingersoll@Sun.COM }
1449*9126SWyllys.Ingersoll@Sun.COM if (template_attribute_find(tmpl, CKA_PRIVATE_EXPONENT,
1450*9126SWyllys.Ingersoll@Sun.COM &priv_exp) == FALSE) {
1451*9126SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_FAILED);
1452*9126SWyllys.Ingersoll@Sun.COM }
1453*9126SWyllys.Ingersoll@Sun.COM if (template_attribute_find(tmpl, CKA_PRIME_1,
1454*9126SWyllys.Ingersoll@Sun.COM &prime1) == FALSE) {
1455*9126SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_FAILED);
1456*9126SWyllys.Ingersoll@Sun.COM }
1457*9126SWyllys.Ingersoll@Sun.COM if (template_attribute_find(tmpl, CKA_PRIME_2,
1458*9126SWyllys.Ingersoll@Sun.COM &prime2) == FALSE) {
1459*9126SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_FAILED);
1460*9126SWyllys.Ingersoll@Sun.COM }
1461*9126SWyllys.Ingersoll@Sun.COM if (template_attribute_find(tmpl, CKA_EXPONENT_1,
1462*9126SWyllys.Ingersoll@Sun.COM &exponent1) == FALSE) {
1463*9126SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_FAILED);
1464*9126SWyllys.Ingersoll@Sun.COM }
1465*9126SWyllys.Ingersoll@Sun.COM if (template_attribute_find(tmpl, CKA_EXPONENT_2,
1466*9126SWyllys.Ingersoll@Sun.COM &exponent2) == FALSE) {
1467*9126SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_FAILED);
1468*9126SWyllys.Ingersoll@Sun.COM }
1469*9126SWyllys.Ingersoll@Sun.COM if (template_attribute_find(tmpl, CKA_COEFFICIENT,
1470*9126SWyllys.Ingersoll@Sun.COM &coeff) == FALSE) {
1471*9126SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_FAILED);
1472*9126SWyllys.Ingersoll@Sun.COM }
1473*9126SWyllys.Ingersoll@Sun.COM rc = ber_encode_RSAPrivateKey(length_only, data, data_len,
1474*9126SWyllys.Ingersoll@Sun.COM modulus, publ_exp, priv_exp, prime1, prime2,
1475*9126SWyllys.Ingersoll@Sun.COM exponent1, exponent2, coeff);
1476*9126SWyllys.Ingersoll@Sun.COM
1477*9126SWyllys.Ingersoll@Sun.COM return (rc);
1478*9126SWyllys.Ingersoll@Sun.COM }
1479*9126SWyllys.Ingersoll@Sun.COM
1480*9126SWyllys.Ingersoll@Sun.COM CK_RV
rsa_priv_unwrap(TEMPLATE * tmpl,CK_BYTE * data,CK_ULONG total_length)1481*9126SWyllys.Ingersoll@Sun.COM rsa_priv_unwrap(TEMPLATE *tmpl,
1482*9126SWyllys.Ingersoll@Sun.COM CK_BYTE *data,
1483*9126SWyllys.Ingersoll@Sun.COM CK_ULONG total_length)
1484*9126SWyllys.Ingersoll@Sun.COM {
1485*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *modulus = NULL;
1486*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *publ_exp = NULL;
1487*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *priv_exp = NULL;
1488*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *prime1 = NULL;
1489*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *prime2 = NULL;
1490*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *exponent1 = NULL;
1491*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *exponent2 = NULL;
1492*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *coeff = NULL;
1493*9126SWyllys.Ingersoll@Sun.COM CK_RV rc;
1494*9126SWyllys.Ingersoll@Sun.COM
1495*9126SWyllys.Ingersoll@Sun.COM rc = ber_decode_RSAPrivateKey(data, total_length,
1496*9126SWyllys.Ingersoll@Sun.COM &modulus, &publ_exp, &priv_exp, &prime1, &prime2,
1497*9126SWyllys.Ingersoll@Sun.COM &exponent1, &exponent2, &coeff);
1498*9126SWyllys.Ingersoll@Sun.COM
1499*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
1500*9126SWyllys.Ingersoll@Sun.COM return (rc);
1501*9126SWyllys.Ingersoll@Sun.COM }
1502*9126SWyllys.Ingersoll@Sun.COM (void) remove_leading_zeros(modulus);
1503*9126SWyllys.Ingersoll@Sun.COM (void) remove_leading_zeros(publ_exp);
1504*9126SWyllys.Ingersoll@Sun.COM (void) remove_leading_zeros(priv_exp);
1505*9126SWyllys.Ingersoll@Sun.COM (void) remove_leading_zeros(prime1);
1506*9126SWyllys.Ingersoll@Sun.COM (void) remove_leading_zeros(prime2);
1507*9126SWyllys.Ingersoll@Sun.COM (void) remove_leading_zeros(exponent1);
1508*9126SWyllys.Ingersoll@Sun.COM (void) remove_leading_zeros(exponent2);
1509*9126SWyllys.Ingersoll@Sun.COM (void) remove_leading_zeros(coeff);
1510*9126SWyllys.Ingersoll@Sun.COM
1511*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, modulus);
1512*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, publ_exp);
1513*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, priv_exp);
1514*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, prime1);
1515*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, prime2);
1516*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, exponent1);
1517*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, exponent2);
1518*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, coeff);
1519*9126SWyllys.Ingersoll@Sun.COM
1520*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
1521*9126SWyllys.Ingersoll@Sun.COM }
1522*9126SWyllys.Ingersoll@Sun.COM
1523*9126SWyllys.Ingersoll@Sun.COM CK_RV
generic_secret_check_required_attributes(TEMPLATE * tmpl,CK_ULONG mode)1524*9126SWyllys.Ingersoll@Sun.COM generic_secret_check_required_attributes(TEMPLATE *tmpl, CK_ULONG mode)
1525*9126SWyllys.Ingersoll@Sun.COM {
1526*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *attr = NULL;
1527*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL found;
1528*9126SWyllys.Ingersoll@Sun.COM
1529*9126SWyllys.Ingersoll@Sun.COM found = template_attribute_find(tmpl, CKA_VALUE, &attr);
1530*9126SWyllys.Ingersoll@Sun.COM if (! found) {
1531*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE) {
1532*9126SWyllys.Ingersoll@Sun.COM return (CKR_TEMPLATE_INCOMPLETE);
1533*9126SWyllys.Ingersoll@Sun.COM }
1534*9126SWyllys.Ingersoll@Sun.COM }
1535*9126SWyllys.Ingersoll@Sun.COM
1536*9126SWyllys.Ingersoll@Sun.COM
1537*9126SWyllys.Ingersoll@Sun.COM found = template_attribute_find(tmpl, CKA_VALUE_LEN, &attr);
1538*9126SWyllys.Ingersoll@Sun.COM if (! found) {
1539*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
1540*9126SWyllys.Ingersoll@Sun.COM } else {
1541*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE) {
1542*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
1543*9126SWyllys.Ingersoll@Sun.COM }
1544*9126SWyllys.Ingersoll@Sun.COM }
1545*9126SWyllys.Ingersoll@Sun.COM
1546*9126SWyllys.Ingersoll@Sun.COM return (secret_key_check_required_attributes(tmpl, mode));
1547*9126SWyllys.Ingersoll@Sun.COM }
1548*9126SWyllys.Ingersoll@Sun.COM
1549*9126SWyllys.Ingersoll@Sun.COM CK_RV
generic_secret_set_default_attributes(TEMPLATE * tmpl,CK_ULONG mode)1550*9126SWyllys.Ingersoll@Sun.COM generic_secret_set_default_attributes(TEMPLATE *tmpl, CK_ULONG mode)
1551*9126SWyllys.Ingersoll@Sun.COM {
1552*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *value_attr = NULL;
1553*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *value_len_attr = NULL;
1554*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *type_attr = NULL;
1555*9126SWyllys.Ingersoll@Sun.COM CK_ULONG len = 0L;
1556*9126SWyllys.Ingersoll@Sun.COM
1557*9126SWyllys.Ingersoll@Sun.COM if (mode)
1558*9126SWyllys.Ingersoll@Sun.COM value_attr = NULL;
1559*9126SWyllys.Ingersoll@Sun.COM
1560*9126SWyllys.Ingersoll@Sun.COM (void) secret_key_set_default_attributes(tmpl, mode);
1561*9126SWyllys.Ingersoll@Sun.COM
1562*9126SWyllys.Ingersoll@Sun.COM type_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
1563*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_KEY_TYPE));
1564*9126SWyllys.Ingersoll@Sun.COM value_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE));
1565*9126SWyllys.Ingersoll@Sun.COM value_len_attr = (CK_ATTRIBUTE *)malloc(sizeof (CK_ATTRIBUTE) +
1566*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ULONG));
1567*9126SWyllys.Ingersoll@Sun.COM
1568*9126SWyllys.Ingersoll@Sun.COM if (! type_attr || ! value_attr || ! value_len_attr) {
1569*9126SWyllys.Ingersoll@Sun.COM if (type_attr) free(type_attr);
1570*9126SWyllys.Ingersoll@Sun.COM if (value_attr) free(value_attr);
1571*9126SWyllys.Ingersoll@Sun.COM if (value_len_attr) free(value_len_attr);
1572*9126SWyllys.Ingersoll@Sun.COM
1573*9126SWyllys.Ingersoll@Sun.COM return (CKR_HOST_MEMORY);
1574*9126SWyllys.Ingersoll@Sun.COM }
1575*9126SWyllys.Ingersoll@Sun.COM
1576*9126SWyllys.Ingersoll@Sun.COM value_attr->type = CKA_VALUE;
1577*9126SWyllys.Ingersoll@Sun.COM value_attr->ulValueLen = 0;
1578*9126SWyllys.Ingersoll@Sun.COM value_attr->pValue = NULL;
1579*9126SWyllys.Ingersoll@Sun.COM
1580*9126SWyllys.Ingersoll@Sun.COM value_len_attr->type = CKA_VALUE_LEN;
1581*9126SWyllys.Ingersoll@Sun.COM value_len_attr->ulValueLen = sizeof (CK_ULONG);
1582*9126SWyllys.Ingersoll@Sun.COM value_len_attr->pValue = (CK_BYTE *)value_len_attr +
1583*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
1584*9126SWyllys.Ingersoll@Sun.COM *(CK_ULONG *)value_len_attr->pValue = len;
1585*9126SWyllys.Ingersoll@Sun.COM
1586*9126SWyllys.Ingersoll@Sun.COM type_attr->type = CKA_KEY_TYPE;
1587*9126SWyllys.Ingersoll@Sun.COM type_attr->ulValueLen = sizeof (CK_KEY_TYPE);
1588*9126SWyllys.Ingersoll@Sun.COM type_attr->pValue = (CK_BYTE *)type_attr +
1589*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ATTRIBUTE);
1590*9126SWyllys.Ingersoll@Sun.COM *(CK_KEY_TYPE *)type_attr->pValue = CKK_GENERIC_SECRET;
1591*9126SWyllys.Ingersoll@Sun.COM
1592*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, type_attr);
1593*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, value_attr);
1594*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, value_len_attr);
1595*9126SWyllys.Ingersoll@Sun.COM
1596*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
1597*9126SWyllys.Ingersoll@Sun.COM }
1598*9126SWyllys.Ingersoll@Sun.COM
1599*9126SWyllys.Ingersoll@Sun.COM CK_RV
generic_secret_validate_attribute(TEMPLATE * tmpl,CK_ATTRIBUTE * attr,CK_ULONG mode)1600*9126SWyllys.Ingersoll@Sun.COM generic_secret_validate_attribute(TEMPLATE *tmpl,
1601*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE *attr, CK_ULONG mode)
1602*9126SWyllys.Ingersoll@Sun.COM {
1603*9126SWyllys.Ingersoll@Sun.COM switch (attr->type) {
1604*9126SWyllys.Ingersoll@Sun.COM case CKA_VALUE:
1605*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_CREATE)
1606*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
1607*9126SWyllys.Ingersoll@Sun.COM else
1608*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
1609*9126SWyllys.Ingersoll@Sun.COM
1610*9126SWyllys.Ingersoll@Sun.COM case CKA_VALUE_LEN:
1611*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_KEYGEN || mode == MODE_DERIVE)
1612*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
1613*9126SWyllys.Ingersoll@Sun.COM else {
1614*9126SWyllys.Ingersoll@Sun.COM if (mode == MODE_UNWRAP) {
1615*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
1616*9126SWyllys.Ingersoll@Sun.COM }
1617*9126SWyllys.Ingersoll@Sun.COM return (CKR_ATTRIBUTE_READ_ONLY);
1618*9126SWyllys.Ingersoll@Sun.COM }
1619*9126SWyllys.Ingersoll@Sun.COM
1620*9126SWyllys.Ingersoll@Sun.COM default:
1621*9126SWyllys.Ingersoll@Sun.COM return (secret_key_validate_attribute(tmpl, attr, mode));
1622*9126SWyllys.Ingersoll@Sun.COM }
1623*9126SWyllys.Ingersoll@Sun.COM }
1624*9126SWyllys.Ingersoll@Sun.COM
1625*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL
generic_secret_check_exportability(CK_ATTRIBUTE_TYPE type)1626*9126SWyllys.Ingersoll@Sun.COM generic_secret_check_exportability(CK_ATTRIBUTE_TYPE type) {
1627*9126SWyllys.Ingersoll@Sun.COM switch (type) {
1628*9126SWyllys.Ingersoll@Sun.COM case CKA_VALUE:
1629*9126SWyllys.Ingersoll@Sun.COM return (FALSE);
1630*9126SWyllys.Ingersoll@Sun.COM }
1631*9126SWyllys.Ingersoll@Sun.COM
1632*9126SWyllys.Ingersoll@Sun.COM return (TRUE);
1633*9126SWyllys.Ingersoll@Sun.COM }
1634*9126SWyllys.Ingersoll@Sun.COM
1635*9126SWyllys.Ingersoll@Sun.COM CK_RV
generic_secret_wrap_get_data(TEMPLATE * tmpl,CK_BBOOL length_only,CK_BYTE ** data,CK_ULONG * data_len)1636*9126SWyllys.Ingersoll@Sun.COM generic_secret_wrap_get_data(TEMPLATE * tmpl,
1637*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL length_only,
1638*9126SWyllys.Ingersoll@Sun.COM CK_BYTE ** data,
1639*9126SWyllys.Ingersoll@Sun.COM CK_ULONG * data_len)
1640*9126SWyllys.Ingersoll@Sun.COM {
1641*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE * attr = NULL;
1642*9126SWyllys.Ingersoll@Sun.COM CK_BYTE * ptr = NULL;
1643*9126SWyllys.Ingersoll@Sun.COM CK_RV rc;
1644*9126SWyllys.Ingersoll@Sun.COM
1645*9126SWyllys.Ingersoll@Sun.COM if (! tmpl || ! data_len) {
1646*9126SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_FAILED);
1647*9126SWyllys.Ingersoll@Sun.COM }
1648*9126SWyllys.Ingersoll@Sun.COM
1649*9126SWyllys.Ingersoll@Sun.COM rc = template_attribute_find(tmpl, CKA_VALUE, &attr);
1650*9126SWyllys.Ingersoll@Sun.COM if (rc == FALSE) {
1651*9126SWyllys.Ingersoll@Sun.COM return (CKR_KEY_NOT_WRAPPABLE);
1652*9126SWyllys.Ingersoll@Sun.COM }
1653*9126SWyllys.Ingersoll@Sun.COM *data_len = attr->ulValueLen;
1654*9126SWyllys.Ingersoll@Sun.COM
1655*9126SWyllys.Ingersoll@Sun.COM if (length_only == FALSE) {
1656*9126SWyllys.Ingersoll@Sun.COM ptr = (CK_BYTE *)malloc(attr->ulValueLen);
1657*9126SWyllys.Ingersoll@Sun.COM if (! ptr) {
1658*9126SWyllys.Ingersoll@Sun.COM return (CKR_HOST_MEMORY);
1659*9126SWyllys.Ingersoll@Sun.COM }
1660*9126SWyllys.Ingersoll@Sun.COM (void) memcpy(ptr, attr->pValue, attr->ulValueLen);
1661*9126SWyllys.Ingersoll@Sun.COM
1662*9126SWyllys.Ingersoll@Sun.COM *data = ptr;
1663*9126SWyllys.Ingersoll@Sun.COM }
1664*9126SWyllys.Ingersoll@Sun.COM
1665*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
1666*9126SWyllys.Ingersoll@Sun.COM }
1667*9126SWyllys.Ingersoll@Sun.COM
1668*9126SWyllys.Ingersoll@Sun.COM CK_RV
generic_secret_unwrap(TEMPLATE * tmpl,CK_BYTE * data,CK_ULONG data_len,CK_BBOOL fromend)1669*9126SWyllys.Ingersoll@Sun.COM generic_secret_unwrap(TEMPLATE *tmpl,
1670*9126SWyllys.Ingersoll@Sun.COM CK_BYTE *data,
1671*9126SWyllys.Ingersoll@Sun.COM CK_ULONG data_len,
1672*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL fromend)
1673*9126SWyllys.Ingersoll@Sun.COM {
1674*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE * attr = NULL;
1675*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE * value_attr = NULL;
1676*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE * value_len_attr = NULL;
1677*9126SWyllys.Ingersoll@Sun.COM CK_BYTE * ptr = NULL;
1678*9126SWyllys.Ingersoll@Sun.COM CK_ULONG rc, len = 0;
1679*9126SWyllys.Ingersoll@Sun.COM
1680*9126SWyllys.Ingersoll@Sun.COM
1681*9126SWyllys.Ingersoll@Sun.COM if (fromend == TRUE)
1682*9126SWyllys.Ingersoll@Sun.COM ptr = data + data_len;
1683*9126SWyllys.Ingersoll@Sun.COM else
1684*9126SWyllys.Ingersoll@Sun.COM ptr = data;
1685*9126SWyllys.Ingersoll@Sun.COM
1686*9126SWyllys.Ingersoll@Sun.COM rc = template_attribute_find(tmpl, CKA_VALUE_LEN, &attr);
1687*9126SWyllys.Ingersoll@Sun.COM if (rc) {
1688*9126SWyllys.Ingersoll@Sun.COM len = *(CK_ULONG *)attr->pValue;
1689*9126SWyllys.Ingersoll@Sun.COM if (len > data_len) {
1690*9126SWyllys.Ingersoll@Sun.COM rc = CKR_ATTRIBUTE_VALUE_INVALID;
1691*9126SWyllys.Ingersoll@Sun.COM goto error;
1692*9126SWyllys.Ingersoll@Sun.COM }
1693*9126SWyllys.Ingersoll@Sun.COM
1694*9126SWyllys.Ingersoll@Sun.COM if (len != 0)
1695*9126SWyllys.Ingersoll@Sun.COM data_len = len;
1696*9126SWyllys.Ingersoll@Sun.COM }
1697*9126SWyllys.Ingersoll@Sun.COM
1698*9126SWyllys.Ingersoll@Sun.COM if (fromend == TRUE)
1699*9126SWyllys.Ingersoll@Sun.COM ptr -= data_len;
1700*9126SWyllys.Ingersoll@Sun.COM
1701*9126SWyllys.Ingersoll@Sun.COM rc = build_attribute(CKA_VALUE, ptr, data_len, &value_attr);
1702*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
1703*9126SWyllys.Ingersoll@Sun.COM goto error;
1704*9126SWyllys.Ingersoll@Sun.COM }
1705*9126SWyllys.Ingersoll@Sun.COM if (data_len != len) {
1706*9126SWyllys.Ingersoll@Sun.COM rc = build_attribute(CKA_VALUE_LEN, (CK_BYTE *)&data_len,
1707*9126SWyllys.Ingersoll@Sun.COM sizeof (CK_ULONG), &value_len_attr);
1708*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK)
1709*9126SWyllys.Ingersoll@Sun.COM goto error;
1710*9126SWyllys.Ingersoll@Sun.COM }
1711*9126SWyllys.Ingersoll@Sun.COM
1712*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, value_attr);
1713*9126SWyllys.Ingersoll@Sun.COM
1714*9126SWyllys.Ingersoll@Sun.COM if (data_len != len)
1715*9126SWyllys.Ingersoll@Sun.COM (void) template_update_attribute(tmpl, value_len_attr);
1716*9126SWyllys.Ingersoll@Sun.COM
1717*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
1718*9126SWyllys.Ingersoll@Sun.COM
1719*9126SWyllys.Ingersoll@Sun.COM error:
1720*9126SWyllys.Ingersoll@Sun.COM if (value_attr) free(value_attr);
1721*9126SWyllys.Ingersoll@Sun.COM if (value_len_attr) free(value_len_attr);
1722*9126SWyllys.Ingersoll@Sun.COM
1723*9126SWyllys.Ingersoll@Sun.COM return (rc);
1724*9126SWyllys.Ingersoll@Sun.COM }
1725