xref: /titanic_51/usr/src/lib/pkcs11/libpkcs11/common/metaSign.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Signing and MACing Functions
30  * (as defined in PKCs#11 spec section 11.11)
31  */
32 
33 #include "metaGlobal.h"
34 
35 
36 /*
37  * meta_SignInit
38  *
39  */
40 CK_RV
41 meta_SignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
42     CK_OBJECT_HANDLE hKey)
43 {
44 	CK_RV rv;
45 	meta_session_t *session;
46 	meta_object_t *key;
47 
48 	if (pMechanism == NULL)
49 		return (CKR_ARGUMENTS_BAD);
50 
51 	rv = meta_handle2session(hSession, &session);
52 	if (rv != CKR_OK)
53 		return (rv);
54 
55 	rv = meta_handle2object(hKey, &key);
56 	if (rv != CKR_OK) {
57 		REFRELEASE(session);
58 		return (rv);
59 	}
60 
61 	rv = meta_operation_init(CKF_SIGN, session, pMechanism, key);
62 
63 	OBJRELEASE(key);
64 	REFRELEASE(session);
65 
66 	return (rv);
67 }
68 
69 
70 /*
71  * meta_Sign
72  *
73  */
74 CK_RV
75 meta_Sign(CK_SESSION_HANDLE hSession,
76     CK_BYTE_PTR pData, CK_ULONG ulDataLen,
77     CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen)
78 {
79 	CK_RV rv;
80 	meta_session_t *session;
81 
82 	rv = meta_handle2session(hSession, &session);
83 	if (rv != CKR_OK)
84 		return (rv);
85 
86 	if (pData == NULL || pulSignatureLen == NULL) {
87 		meta_operation_cleanup(session, CKF_SIGN, FALSE);
88 		REFRELEASE(session);
89 		return (CKR_ARGUMENTS_BAD);
90 	}
91 
92 	rv = meta_do_operation(CKF_SIGN, MODE_SINGLE, session, NULL,
93 	    pData, ulDataLen, pSignature, pulSignatureLen);
94 
95 	REFRELEASE(session);
96 
97 	return (rv);
98 }
99 
100 
101 /*
102  * meta_SignUpdate
103  *
104  */
105 CK_RV
106 meta_SignUpdate(CK_SESSION_HANDLE hSession,
107     CK_BYTE_PTR pPart, CK_ULONG ulPartLen)
108 {
109 	CK_RV rv;
110 	meta_session_t *session;
111 
112 	rv = meta_handle2session(hSession, &session);
113 	if (rv != CKR_OK)
114 		return (rv);
115 
116 	if (pPart == NULL) {
117 		meta_operation_cleanup(session, CKF_SIGN, FALSE);
118 		REFRELEASE(session);
119 		return (CKR_ARGUMENTS_BAD);
120 	}
121 
122 	rv = meta_do_operation(CKF_SIGN, MODE_UPDATE, session, NULL,
123 	    pPart, ulPartLen, NULL, NULL);
124 
125 	REFRELEASE(session);
126 
127 	return (rv);
128 }
129 
130 
131 /*
132  * meta_SignFinal
133  *
134  */
135 CK_RV
136 meta_SignFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
137     CK_ULONG_PTR pulSignatureLen)
138 {
139 	CK_RV rv;
140 	meta_session_t *session;
141 
142 	rv = meta_handle2session(hSession, &session);
143 	if (rv != CKR_OK)
144 		return (rv);
145 
146 	if (pulSignatureLen == NULL) {
147 		meta_operation_cleanup(session, CKF_SIGN, FALSE);
148 		REFRELEASE(session);
149 		return (CKR_ARGUMENTS_BAD);
150 	}
151 
152 	rv = meta_do_operation(CKF_SIGN, MODE_FINAL, session, NULL,
153 	    NULL, 0, pSignature, pulSignatureLen);
154 
155 	REFRELEASE(session);
156 
157 	return (rv);
158 }
159 
160 /*
161  * meta_SignRecoverInit
162  *
163  */
164 CK_RV
165 meta_SignRecoverInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
166     CK_OBJECT_HANDLE hKey)
167 {
168 	CK_RV rv;
169 	meta_session_t *session;
170 	meta_object_t *key;
171 
172 	if (pMechanism == NULL)
173 		return (CKR_ARGUMENTS_BAD);
174 
175 	rv = meta_handle2session(hSession, &session);
176 	if (rv != CKR_OK)
177 		return (rv);
178 
179 	rv = meta_handle2object(hKey, &key);
180 	if (rv != CKR_OK) {
181 		REFRELEASE(session);
182 		return (rv);
183 	}
184 
185 	rv = meta_operation_init(CKF_SIGN_RECOVER, session, pMechanism, key);
186 
187 	OBJRELEASE(key);
188 	REFRELEASE(session);
189 
190 	return (rv);
191 }
192 
193 
194 /*
195  * meta_SignRecover
196  *
197  */
198 CK_RV
199 meta_SignRecover(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
200     CK_ULONG ulDataLen, CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen)
201 {
202 	CK_RV rv;
203 	meta_session_t *session;
204 
205 	rv = meta_handle2session(hSession, &session);
206 	if (rv != CKR_OK)
207 		return (rv);
208 
209 	if (pData == NULL || pulSignatureLen == NULL) {
210 		meta_operation_cleanup(session, CKF_SIGN_RECOVER, FALSE);
211 		REFRELEASE(session);
212 		return (CKR_ARGUMENTS_BAD);
213 	}
214 
215 	rv = meta_do_operation(CKF_SIGN_RECOVER, MODE_SINGLE, session, NULL,
216 	    pData, ulDataLen, pSignature, pulSignatureLen);
217 
218 	REFRELEASE(session);
219 
220 	return (rv);
221 }
222