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