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 #include "tpmtok_int.h"
29*9126SWyllys.Ingersoll@Sun.COM
30*9126SWyllys.Ingersoll@Sun.COM CK_RV
encr_mgr_init(SESSION * sess,ENCR_DECR_CONTEXT * ctx,CK_ULONG operation,CK_MECHANISM * mech,CK_OBJECT_HANDLE key_handle)31*9126SWyllys.Ingersoll@Sun.COM encr_mgr_init(SESSION * sess,
32*9126SWyllys.Ingersoll@Sun.COM ENCR_DECR_CONTEXT * ctx,
33*9126SWyllys.Ingersoll@Sun.COM CK_ULONG operation,
34*9126SWyllys.Ingersoll@Sun.COM CK_MECHANISM * mech,
35*9126SWyllys.Ingersoll@Sun.COM CK_OBJECT_HANDLE key_handle)
36*9126SWyllys.Ingersoll@Sun.COM {
37*9126SWyllys.Ingersoll@Sun.COM OBJECT * key_obj = NULL;
38*9126SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE * attr = NULL;
39*9126SWyllys.Ingersoll@Sun.COM CK_BYTE * ptr = NULL;
40*9126SWyllys.Ingersoll@Sun.COM CK_KEY_TYPE keytype;
41*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL flag;
42*9126SWyllys.Ingersoll@Sun.COM CK_RV rc;
43*9126SWyllys.Ingersoll@Sun.COM
44*9126SWyllys.Ingersoll@Sun.COM
45*9126SWyllys.Ingersoll@Sun.COM if (! sess || ! ctx || ! mech) {
46*9126SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_FAILED);
47*9126SWyllys.Ingersoll@Sun.COM }
48*9126SWyllys.Ingersoll@Sun.COM if (ctx->active != FALSE) {
49*9126SWyllys.Ingersoll@Sun.COM return (CKR_OPERATION_ACTIVE);
50*9126SWyllys.Ingersoll@Sun.COM }
51*9126SWyllys.Ingersoll@Sun.COM
52*9126SWyllys.Ingersoll@Sun.COM if (operation == OP_ENCRYPT_INIT) {
53*9126SWyllys.Ingersoll@Sun.COM rc = object_mgr_find_in_map1(sess->hContext, key_handle,
54*9126SWyllys.Ingersoll@Sun.COM &key_obj);
55*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
56*9126SWyllys.Ingersoll@Sun.COM return (CKR_KEY_HANDLE_INVALID);
57*9126SWyllys.Ingersoll@Sun.COM }
58*9126SWyllys.Ingersoll@Sun.COM rc = template_attribute_find(key_obj->template,
59*9126SWyllys.Ingersoll@Sun.COM CKA_ENCRYPT, &attr);
60*9126SWyllys.Ingersoll@Sun.COM if (rc == FALSE) {
61*9126SWyllys.Ingersoll@Sun.COM return (CKR_KEY_FUNCTION_NOT_PERMITTED);
62*9126SWyllys.Ingersoll@Sun.COM } else {
63*9126SWyllys.Ingersoll@Sun.COM flag = *(CK_BBOOL *)attr->pValue;
64*9126SWyllys.Ingersoll@Sun.COM if (flag != TRUE) {
65*9126SWyllys.Ingersoll@Sun.COM return (CKR_KEY_FUNCTION_NOT_PERMITTED);
66*9126SWyllys.Ingersoll@Sun.COM }
67*9126SWyllys.Ingersoll@Sun.COM }
68*9126SWyllys.Ingersoll@Sun.COM } else if (operation == OP_WRAP) {
69*9126SWyllys.Ingersoll@Sun.COM rc = object_mgr_find_in_map1(sess->hContext, key_handle,
70*9126SWyllys.Ingersoll@Sun.COM &key_obj);
71*9126SWyllys.Ingersoll@Sun.COM if (rc != CKR_OK) {
72*9126SWyllys.Ingersoll@Sun.COM return (CKR_WRAPPING_KEY_HANDLE_INVALID);
73*9126SWyllys.Ingersoll@Sun.COM }
74*9126SWyllys.Ingersoll@Sun.COM rc = template_attribute_find(key_obj->template,
75*9126SWyllys.Ingersoll@Sun.COM CKA_WRAP, &attr);
76*9126SWyllys.Ingersoll@Sun.COM if (rc == FALSE) {
77*9126SWyllys.Ingersoll@Sun.COM return (CKR_KEY_NOT_WRAPPABLE);
78*9126SWyllys.Ingersoll@Sun.COM } else {
79*9126SWyllys.Ingersoll@Sun.COM flag = *(CK_BBOOL *)attr->pValue;
80*9126SWyllys.Ingersoll@Sun.COM if (flag == FALSE) {
81*9126SWyllys.Ingersoll@Sun.COM return (CKR_KEY_NOT_WRAPPABLE);
82*9126SWyllys.Ingersoll@Sun.COM }
83*9126SWyllys.Ingersoll@Sun.COM }
84*9126SWyllys.Ingersoll@Sun.COM } else {
85*9126SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_FAILED);
86*9126SWyllys.Ingersoll@Sun.COM }
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 break;
109*9126SWyllys.Ingersoll@Sun.COM default:
110*9126SWyllys.Ingersoll@Sun.COM return (CKR_MECHANISM_INVALID);
111*9126SWyllys.Ingersoll@Sun.COM }
112*9126SWyllys.Ingersoll@Sun.COM
113*9126SWyllys.Ingersoll@Sun.COM
114*9126SWyllys.Ingersoll@Sun.COM if (mech->ulParameterLen > 0) {
115*9126SWyllys.Ingersoll@Sun.COM ptr = (CK_BYTE *)malloc(mech->ulParameterLen);
116*9126SWyllys.Ingersoll@Sun.COM if (! ptr) {
117*9126SWyllys.Ingersoll@Sun.COM return (CKR_HOST_MEMORY);
118*9126SWyllys.Ingersoll@Sun.COM }
119*9126SWyllys.Ingersoll@Sun.COM (void) memcpy(ptr, mech->pParameter, mech->ulParameterLen);
120*9126SWyllys.Ingersoll@Sun.COM }
121*9126SWyllys.Ingersoll@Sun.COM
122*9126SWyllys.Ingersoll@Sun.COM ctx->key = key_handle;
123*9126SWyllys.Ingersoll@Sun.COM ctx->mech.ulParameterLen = mech->ulParameterLen;
124*9126SWyllys.Ingersoll@Sun.COM ctx->mech.mechanism = mech->mechanism;
125*9126SWyllys.Ingersoll@Sun.COM ctx->mech.pParameter = ptr;
126*9126SWyllys.Ingersoll@Sun.COM ctx->multi = FALSE;
127*9126SWyllys.Ingersoll@Sun.COM ctx->active = TRUE;
128*9126SWyllys.Ingersoll@Sun.COM
129*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
130*9126SWyllys.Ingersoll@Sun.COM }
131*9126SWyllys.Ingersoll@Sun.COM
132*9126SWyllys.Ingersoll@Sun.COM CK_RV
encr_mgr_cleanup(ENCR_DECR_CONTEXT * ctx)133*9126SWyllys.Ingersoll@Sun.COM encr_mgr_cleanup(ENCR_DECR_CONTEXT *ctx)
134*9126SWyllys.Ingersoll@Sun.COM {
135*9126SWyllys.Ingersoll@Sun.COM if (! ctx) {
136*9126SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_FAILED);
137*9126SWyllys.Ingersoll@Sun.COM }
138*9126SWyllys.Ingersoll@Sun.COM ctx->key = 0;
139*9126SWyllys.Ingersoll@Sun.COM ctx->mech.ulParameterLen = 0;
140*9126SWyllys.Ingersoll@Sun.COM ctx->mech.mechanism = 0;
141*9126SWyllys.Ingersoll@Sun.COM ctx->multi = FALSE;
142*9126SWyllys.Ingersoll@Sun.COM ctx->active = FALSE;
143*9126SWyllys.Ingersoll@Sun.COM ctx->context_len = 0;
144*9126SWyllys.Ingersoll@Sun.COM
145*9126SWyllys.Ingersoll@Sun.COM if (ctx->mech.pParameter) {
146*9126SWyllys.Ingersoll@Sun.COM free(ctx->mech.pParameter);
147*9126SWyllys.Ingersoll@Sun.COM ctx->mech.pParameter = NULL;
148*9126SWyllys.Ingersoll@Sun.COM }
149*9126SWyllys.Ingersoll@Sun.COM
150*9126SWyllys.Ingersoll@Sun.COM if (ctx->context) {
151*9126SWyllys.Ingersoll@Sun.COM free(ctx->context);
152*9126SWyllys.Ingersoll@Sun.COM ctx->context = NULL;
153*9126SWyllys.Ingersoll@Sun.COM }
154*9126SWyllys.Ingersoll@Sun.COM
155*9126SWyllys.Ingersoll@Sun.COM return (CKR_OK);
156*9126SWyllys.Ingersoll@Sun.COM }
157*9126SWyllys.Ingersoll@Sun.COM
158*9126SWyllys.Ingersoll@Sun.COM CK_RV
encr_mgr_encrypt(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)159*9126SWyllys.Ingersoll@Sun.COM encr_mgr_encrypt(SESSION *sess,
160*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL length_only,
161*9126SWyllys.Ingersoll@Sun.COM ENCR_DECR_CONTEXT *ctx,
162*9126SWyllys.Ingersoll@Sun.COM CK_BYTE *in_data,
163*9126SWyllys.Ingersoll@Sun.COM CK_ULONG in_data_len,
164*9126SWyllys.Ingersoll@Sun.COM CK_BYTE *out_data,
165*9126SWyllys.Ingersoll@Sun.COM CK_ULONG *out_data_len)
166*9126SWyllys.Ingersoll@Sun.COM {
167*9126SWyllys.Ingersoll@Sun.COM if (! sess || ! ctx) {
168*9126SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_FAILED);
169*9126SWyllys.Ingersoll@Sun.COM }
170*9126SWyllys.Ingersoll@Sun.COM if (ctx->active == FALSE) {
171*9126SWyllys.Ingersoll@Sun.COM return (CKR_OPERATION_NOT_INITIALIZED);
172*9126SWyllys.Ingersoll@Sun.COM }
173*9126SWyllys.Ingersoll@Sun.COM if ((length_only == FALSE) && (! in_data || ! out_data)) {
174*9126SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_FAILED);
175*9126SWyllys.Ingersoll@Sun.COM }
176*9126SWyllys.Ingersoll@Sun.COM if (ctx->multi == TRUE) {
177*9126SWyllys.Ingersoll@Sun.COM return (CKR_OPERATION_ACTIVE);
178*9126SWyllys.Ingersoll@Sun.COM }
179*9126SWyllys.Ingersoll@Sun.COM switch (ctx->mech.mechanism) {
180*9126SWyllys.Ingersoll@Sun.COM case CKM_RSA_PKCS:
181*9126SWyllys.Ingersoll@Sun.COM return (rsa_pkcs_encrypt(sess, length_only,
182*9126SWyllys.Ingersoll@Sun.COM ctx, in_data, in_data_len, out_data,
183*9126SWyllys.Ingersoll@Sun.COM out_data_len));
184*9126SWyllys.Ingersoll@Sun.COM
185*9126SWyllys.Ingersoll@Sun.COM default:
186*9126SWyllys.Ingersoll@Sun.COM return (CKR_MECHANISM_INVALID);
187*9126SWyllys.Ingersoll@Sun.COM }
188*9126SWyllys.Ingersoll@Sun.COM }
189