xref: /onnv-gate/usr/src/lib/pkcs11/pkcs11_tpm/common/decr_mgr.c (revision 9126:6acea8ac53c8)
1*9126SWyllys.Ingersoll@Sun.COM /*
2*9126SWyllys.Ingersoll@Sun.COM  * The Initial Developer of the Original Code is International
3*9126SWyllys.Ingersoll@Sun.COM  * Business Machines Corporation. Portions created by IBM
4*9126SWyllys.Ingersoll@Sun.COM  * Corporation are Copyright (C) 2005 International Business
5*9126SWyllys.Ingersoll@Sun.COM  * Machines Corporation. All Rights Reserved.
6*9126SWyllys.Ingersoll@Sun.COM  *
7*9126SWyllys.Ingersoll@Sun.COM  * This program is free software; you can redistribute it and/or modify
8*9126SWyllys.Ingersoll@Sun.COM  * it under the terms of the Common Public License as published by
9*9126SWyllys.Ingersoll@Sun.COM  * IBM Corporation; either version 1 of the License, or (at your option)
10*9126SWyllys.Ingersoll@Sun.COM  * any later version.
11*9126SWyllys.Ingersoll@Sun.COM  *
12*9126SWyllys.Ingersoll@Sun.COM  * This program is distributed in the hope that it will be useful,
13*9126SWyllys.Ingersoll@Sun.COM  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14*9126SWyllys.Ingersoll@Sun.COM  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*9126SWyllys.Ingersoll@Sun.COM  * Common Public License for more details.
16*9126SWyllys.Ingersoll@Sun.COM  *
17*9126SWyllys.Ingersoll@Sun.COM  * You should have received a copy of the Common Public License
18*9126SWyllys.Ingersoll@Sun.COM  * along with this program; if not, a copy can be viewed at
19*9126SWyllys.Ingersoll@Sun.COM  * http://www.opensource.org/licenses/cpl1.0.php.
20*9126SWyllys.Ingersoll@Sun.COM  */
21*9126SWyllys.Ingersoll@Sun.COM 
22*9126SWyllys.Ingersoll@Sun.COM /* (C) COPYRIGHT International Business Machines Corp. 2001, 2002, 2005 */
23*9126SWyllys.Ingersoll@Sun.COM /*
24*9126SWyllys.Ingersoll@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25*9126SWyllys.Ingersoll@Sun.COM  * Use is subject to license terms.
26*9126SWyllys.Ingersoll@Sun.COM  */
27*9126SWyllys.Ingersoll@Sun.COM 
28*9126SWyllys.Ingersoll@Sun.COM 
29*9126SWyllys.Ingersoll@Sun.COM #include "tpmtok_int.h"
30*9126SWyllys.Ingersoll@Sun.COM 
31*9126SWyllys.Ingersoll@Sun.COM CK_RV
decr_mgr_init(SESSION * sess,ENCR_DECR_CONTEXT * ctx,CK_ULONG operation,CK_MECHANISM * mech,CK_OBJECT_HANDLE key_handle)32*9126SWyllys.Ingersoll@Sun.COM decr_mgr_init(
33*9126SWyllys.Ingersoll@Sun.COM 	SESSION		*sess,
34*9126SWyllys.Ingersoll@Sun.COM 	ENCR_DECR_CONTEXT *ctx,
35*9126SWyllys.Ingersoll@Sun.COM 	CK_ULONG	operation,
36*9126SWyllys.Ingersoll@Sun.COM 	CK_MECHANISM	*mech,
37*9126SWyllys.Ingersoll@Sun.COM 	CK_OBJECT_HANDLE	key_handle)
38*9126SWyllys.Ingersoll@Sun.COM {
39*9126SWyllys.Ingersoll@Sun.COM 	OBJECT	* key_obj = NULL;
40*9126SWyllys.Ingersoll@Sun.COM 	CK_ATTRIBUTE  * attr    = NULL;
41*9126SWyllys.Ingersoll@Sun.COM 	CK_BYTE	*ptr = NULL;
42*9126SWyllys.Ingersoll@Sun.COM 	CK_KEY_TYPE   keytype;
43*9126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL flag;
44*9126SWyllys.Ingersoll@Sun.COM 	CK_RV rc;
45*9126SWyllys.Ingersoll@Sun.COM 
46*9126SWyllys.Ingersoll@Sun.COM 	if (! sess) {
47*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
48*9126SWyllys.Ingersoll@Sun.COM 	}
49*9126SWyllys.Ingersoll@Sun.COM 	if (ctx->active != FALSE) {
50*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_OPERATION_ACTIVE);
51*9126SWyllys.Ingersoll@Sun.COM 	}
52*9126SWyllys.Ingersoll@Sun.COM 
53*9126SWyllys.Ingersoll@Sun.COM 	if (operation == OP_DECRYPT_INIT) {
54*9126SWyllys.Ingersoll@Sun.COM 		rc = object_mgr_find_in_map1(sess->hContext, key_handle,
55*9126SWyllys.Ingersoll@Sun.COM 		    &key_obj);
56*9126SWyllys.Ingersoll@Sun.COM 		if (rc != CKR_OK) {
57*9126SWyllys.Ingersoll@Sun.COM 			return (CKR_KEY_HANDLE_INVALID);
58*9126SWyllys.Ingersoll@Sun.COM 		}
59*9126SWyllys.Ingersoll@Sun.COM 		rc = template_attribute_find(key_obj->template,
60*9126SWyllys.Ingersoll@Sun.COM 		    CKA_DECRYPT, &attr);
61*9126SWyllys.Ingersoll@Sun.COM 		if (rc == FALSE) {
62*9126SWyllys.Ingersoll@Sun.COM 			return (CKR_KEY_FUNCTION_NOT_PERMITTED);
63*9126SWyllys.Ingersoll@Sun.COM 		} else {
64*9126SWyllys.Ingersoll@Sun.COM 			flag = *(CK_BBOOL *)attr->pValue;
65*9126SWyllys.Ingersoll@Sun.COM 			if (flag != TRUE) {
66*9126SWyllys.Ingersoll@Sun.COM 				return (CKR_KEY_FUNCTION_NOT_PERMITTED);
67*9126SWyllys.Ingersoll@Sun.COM 			}
68*9126SWyllys.Ingersoll@Sun.COM 		}
69*9126SWyllys.Ingersoll@Sun.COM 	} else if (operation == OP_UNWRAP) {
70*9126SWyllys.Ingersoll@Sun.COM 		rc = object_mgr_find_in_map1(sess->hContext, key_handle,
71*9126SWyllys.Ingersoll@Sun.COM 		    &key_obj);
72*9126SWyllys.Ingersoll@Sun.COM 		if (rc != CKR_OK) {
73*9126SWyllys.Ingersoll@Sun.COM 			return (CKR_WRAPPING_KEY_HANDLE_INVALID);
74*9126SWyllys.Ingersoll@Sun.COM 		}
75*9126SWyllys.Ingersoll@Sun.COM 		rc = template_attribute_find(key_obj->template,
76*9126SWyllys.Ingersoll@Sun.COM 		    CKA_UNWRAP, &attr);
77*9126SWyllys.Ingersoll@Sun.COM 		if (rc == FALSE) {
78*9126SWyllys.Ingersoll@Sun.COM 			return (CKR_FUNCTION_FAILED);
79*9126SWyllys.Ingersoll@Sun.COM 		} else {
80*9126SWyllys.Ingersoll@Sun.COM 			flag = *(CK_BBOOL *)(attr->pValue);
81*9126SWyllys.Ingersoll@Sun.COM 			if (flag == FALSE) {
82*9126SWyllys.Ingersoll@Sun.COM 				return (CKR_FUNCTION_FAILED);
83*9126SWyllys.Ingersoll@Sun.COM 			}
84*9126SWyllys.Ingersoll@Sun.COM 		}
85*9126SWyllys.Ingersoll@Sun.COM 	} else {
86*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
87*9126SWyllys.Ingersoll@Sun.COM 	}
88*9126SWyllys.Ingersoll@Sun.COM 	switch (mech->mechanism) {
89*9126SWyllys.Ingersoll@Sun.COM 		case CKM_RSA_PKCS:
90*9126SWyllys.Ingersoll@Sun.COM 		{
91*9126SWyllys.Ingersoll@Sun.COM 			if (mech->ulParameterLen != 0)
92*9126SWyllys.Ingersoll@Sun.COM 				return (CKR_MECHANISM_PARAM_INVALID);
93*9126SWyllys.Ingersoll@Sun.COM 
94*9126SWyllys.Ingersoll@Sun.COM 			rc = template_attribute_find(key_obj->template,
95*9126SWyllys.Ingersoll@Sun.COM 			    CKA_KEY_TYPE, &attr);
96*9126SWyllys.Ingersoll@Sun.COM 			if (rc == FALSE) {
97*9126SWyllys.Ingersoll@Sun.COM 				return (CKR_KEY_TYPE_INCONSISTENT);
98*9126SWyllys.Ingersoll@Sun.COM 			} else {
99*9126SWyllys.Ingersoll@Sun.COM 				keytype = *(CK_KEY_TYPE *)attr->pValue;
100*9126SWyllys.Ingersoll@Sun.COM 				if (keytype != CKK_RSA) {
101*9126SWyllys.Ingersoll@Sun.COM 					return (CKR_KEY_TYPE_INCONSISTENT);
102*9126SWyllys.Ingersoll@Sun.COM 				}
103*9126SWyllys.Ingersoll@Sun.COM 			}
104*9126SWyllys.Ingersoll@Sun.COM 
105*9126SWyllys.Ingersoll@Sun.COM 			ctx->context_len = 0;
106*9126SWyllys.Ingersoll@Sun.COM 			ctx->context = NULL;
107*9126SWyllys.Ingersoll@Sun.COM 
108*9126SWyllys.Ingersoll@Sun.COM 		}
109*9126SWyllys.Ingersoll@Sun.COM 		break;
110*9126SWyllys.Ingersoll@Sun.COM 		default:
111*9126SWyllys.Ingersoll@Sun.COM 			return (CKR_MECHANISM_INVALID);
112*9126SWyllys.Ingersoll@Sun.COM 	}
113*9126SWyllys.Ingersoll@Sun.COM 
114*9126SWyllys.Ingersoll@Sun.COM 
115*9126SWyllys.Ingersoll@Sun.COM 	if (mech->ulParameterLen > 0) {
116*9126SWyllys.Ingersoll@Sun.COM 		ptr = (CK_BYTE *)malloc(mech->ulParameterLen);
117*9126SWyllys.Ingersoll@Sun.COM 		if (! ptr) {
118*9126SWyllys.Ingersoll@Sun.COM 			return (CKR_HOST_MEMORY);
119*9126SWyllys.Ingersoll@Sun.COM 		}
120*9126SWyllys.Ingersoll@Sun.COM 		(void) memcpy(ptr, mech->pParameter, mech->ulParameterLen);
121*9126SWyllys.Ingersoll@Sun.COM 	}
122*9126SWyllys.Ingersoll@Sun.COM 
123*9126SWyllys.Ingersoll@Sun.COM 	ctx->key		 = key_handle;
124*9126SWyllys.Ingersoll@Sun.COM 	ctx->mech.ulParameterLen = mech->ulParameterLen;
125*9126SWyllys.Ingersoll@Sun.COM 	ctx->mech.mechanism	= mech->mechanism;
126*9126SWyllys.Ingersoll@Sun.COM 	ctx->mech.pParameter	= ptr;
127*9126SWyllys.Ingersoll@Sun.COM 	ctx->multi		= FALSE;
128*9126SWyllys.Ingersoll@Sun.COM 	ctx->active		= TRUE;
129*9126SWyllys.Ingersoll@Sun.COM 
130*9126SWyllys.Ingersoll@Sun.COM 	return (CKR_OK);
131*9126SWyllys.Ingersoll@Sun.COM }
132*9126SWyllys.Ingersoll@Sun.COM 
133*9126SWyllys.Ingersoll@Sun.COM CK_RV
decr_mgr_cleanup(ENCR_DECR_CONTEXT * ctx)134*9126SWyllys.Ingersoll@Sun.COM decr_mgr_cleanup(ENCR_DECR_CONTEXT *ctx)
135*9126SWyllys.Ingersoll@Sun.COM {
136*9126SWyllys.Ingersoll@Sun.COM 	if (! ctx) {
137*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
138*9126SWyllys.Ingersoll@Sun.COM 	}
139*9126SWyllys.Ingersoll@Sun.COM 	ctx->key		= 0;
140*9126SWyllys.Ingersoll@Sun.COM 	ctx->mech.ulParameterLen = 0;
141*9126SWyllys.Ingersoll@Sun.COM 	ctx->mech.mechanism	= 0;
142*9126SWyllys.Ingersoll@Sun.COM 	ctx->multi		= FALSE;
143*9126SWyllys.Ingersoll@Sun.COM 	ctx->active		= FALSE;
144*9126SWyllys.Ingersoll@Sun.COM 	ctx->context_len	= 0;
145*9126SWyllys.Ingersoll@Sun.COM 
146*9126SWyllys.Ingersoll@Sun.COM 	if (ctx->mech.pParameter) {
147*9126SWyllys.Ingersoll@Sun.COM 		free(ctx->mech.pParameter);
148*9126SWyllys.Ingersoll@Sun.COM 		ctx->mech.pParameter = NULL;
149*9126SWyllys.Ingersoll@Sun.COM 	}
150*9126SWyllys.Ingersoll@Sun.COM 
151*9126SWyllys.Ingersoll@Sun.COM 	if (ctx->context) {
152*9126SWyllys.Ingersoll@Sun.COM 		free(ctx->context);
153*9126SWyllys.Ingersoll@Sun.COM 		ctx->context = NULL;
154*9126SWyllys.Ingersoll@Sun.COM 	}
155*9126SWyllys.Ingersoll@Sun.COM 
156*9126SWyllys.Ingersoll@Sun.COM 	return (CKR_OK);
157*9126SWyllys.Ingersoll@Sun.COM }
158*9126SWyllys.Ingersoll@Sun.COM 
159*9126SWyllys.Ingersoll@Sun.COM CK_RV
decr_mgr_decrypt(SESSION * sess,CK_BBOOL length_only,ENCR_DECR_CONTEXT * ctx,CK_BYTE * in_data,CK_ULONG in_data_len,CK_BYTE * out_data,CK_ULONG * out_data_len)160*9126SWyllys.Ingersoll@Sun.COM decr_mgr_decrypt(SESSION	   *sess,
161*9126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL	length_only,
162*9126SWyllys.Ingersoll@Sun.COM 	ENCR_DECR_CONTEXT *ctx,
163*9126SWyllys.Ingersoll@Sun.COM 	CK_BYTE		*in_data,
164*9126SWyllys.Ingersoll@Sun.COM 	CK_ULONG	in_data_len,
165*9126SWyllys.Ingersoll@Sun.COM 	CK_BYTE		*out_data,
166*9126SWyllys.Ingersoll@Sun.COM 	CK_ULONG	*out_data_len)
167*9126SWyllys.Ingersoll@Sun.COM {
168*9126SWyllys.Ingersoll@Sun.COM 	if (! sess || ! ctx) {
169*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
170*9126SWyllys.Ingersoll@Sun.COM 	}
171*9126SWyllys.Ingersoll@Sun.COM 	if (ctx->active == FALSE) {
172*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_OPERATION_NOT_INITIALIZED);
173*9126SWyllys.Ingersoll@Sun.COM 	}
174*9126SWyllys.Ingersoll@Sun.COM 	if ((length_only == FALSE) && (! in_data || ! out_data)) {
175*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
176*9126SWyllys.Ingersoll@Sun.COM 	}
177*9126SWyllys.Ingersoll@Sun.COM 	if (ctx->multi == TRUE) {
178*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_OPERATION_ACTIVE);
179*9126SWyllys.Ingersoll@Sun.COM 	}
180*9126SWyllys.Ingersoll@Sun.COM 	switch (ctx->mech.mechanism) {
181*9126SWyllys.Ingersoll@Sun.COM 		case CKM_RSA_PKCS:
182*9126SWyllys.Ingersoll@Sun.COM 			return (rsa_pkcs_decrypt(sess, length_only,
183*9126SWyllys.Ingersoll@Sun.COM 			    ctx, in_data,  in_data_len, out_data,
184*9126SWyllys.Ingersoll@Sun.COM 			    out_data_len));
185*9126SWyllys.Ingersoll@Sun.COM 
186*9126SWyllys.Ingersoll@Sun.COM 		default:
187*9126SWyllys.Ingersoll@Sun.COM 			return (CKR_MECHANISM_INVALID);
188*9126SWyllys.Ingersoll@Sun.COM 	}
189*9126SWyllys.Ingersoll@Sun.COM }
190