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