xref: /illumos-gate/usr/src/lib/pkcs11/libpkcs11/common/pkcs11Digest.c (revision 543a8da84d26ec70222ecd1638b72c1e6c763275)
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 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <security/cryptoki.h>
28 #include "pkcs11Global.h"
29 #include "pkcs11Conf.h"
30 #include "pkcs11Session.h"
31 #include "pkcs11Slot.h"
32 
33 /*
34  * C_DigestInit will verify that the session handle is valid within
35  * the framework, that the mechanism is not disabled for the slot
36  * associated with this session, and then redirect to the underlying
37  * provider.  Policy is only checked for C_DigestInit, since it is
38  * required to be called before C_Digest and C_DigestUpdate.
39  */
40 CK_RV
41 C_DigestInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism)
42 {
43 
44 	CK_RV rv;
45 	pkcs11_session_t *sessp;
46 	CK_SLOT_ID slotid;
47 
48 	/* Check for a fastpath */
49 	if (purefastpath || policyfastpath) {
50 		if (policyfastpath &&
51 		    pkcs11_is_dismech(fast_slot, pMechanism->mechanism)) {
52 			return (CKR_MECHANISM_INVALID);
53 		}
54 		return (fast_funcs->C_DigestInit(hSession, pMechanism));
55 	}
56 
57 	if (!pkcs11_initialized) {
58 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
59 	}
60 
61 	/* Obtain the session pointer */
62 	HANDLE2SESSION(hSession, sessp, rv);
63 
64 	if (rv != CKR_OK) {
65 		return (rv);
66 	}
67 
68 	slotid = sessp->se_slotid;
69 
70 	/* Make sure this is not a disabled mechanism */
71 	if (pkcs11_is_dismech(slotid, pMechanism->mechanism)) {
72 		return (CKR_MECHANISM_INVALID);
73 	}
74 
75 	/* Initialize the digest with the underlying provider */
76 	rv = FUNCLIST(slotid)->C_DigestInit(sessp->se_handle,
77 	    pMechanism);
78 
79 	/* Present consistent interface to the application */
80 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
81 		return (CKR_FUNCTION_FAILED);
82 	}
83 
84 	return (rv);
85 }
86 
87 /*
88  * C_Digest is a pure wrapper to the underlying provider.
89  * The only argument checked is whether or not hSession is valid.
90  */
91 CK_RV
92 C_Digest(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
93     CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen)
94 {
95 
96 	CK_RV rv;
97 	pkcs11_session_t *sessp;
98 
99 	/* Check for a fastpath */
100 	if (purefastpath || policyfastpath) {
101 		return (fast_funcs->C_Digest(hSession, pData, ulDataLen,
102 			    pDigest, pulDigestLen));
103 	}
104 
105 	if (!pkcs11_initialized) {
106 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
107 	}
108 
109 	/* Obtain the session pointer */
110 	HANDLE2SESSION(hSession, sessp, rv);
111 
112 	if (rv != CKR_OK) {
113 		return (rv);
114 	}
115 
116 	/* Pass data to the provider */
117 	rv = FUNCLIST(sessp->se_slotid)->C_Digest(sessp->se_handle, pData,
118 	    ulDataLen, pDigest, pulDigestLen);
119 
120 	/* Present consistent interface to the application */
121 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
122 		return (CKR_FUNCTION_FAILED);
123 	}
124 
125 	return (rv);
126 
127 }
128 
129 /*
130  * C_DigestUpdate is a pure wrapper to the underlying provider.
131  * The only argument checked is whether or not hSession is valid.
132  */
133 CK_RV
134 C_DigestUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
135     CK_ULONG ulPartLen)
136 {
137 	CK_RV rv;
138 	pkcs11_session_t *sessp;
139 
140 	/* Check for a fastpath */
141 	if (purefastpath || policyfastpath) {
142 		return (fast_funcs->C_DigestUpdate(hSession, pPart,
143 			    ulPartLen));
144 	}
145 
146 	if (!pkcs11_initialized) {
147 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
148 	}
149 
150 	/* Obtain the session pointer */
151 	HANDLE2SESSION(hSession, sessp, rv);
152 
153 	if (rv != CKR_OK) {
154 		return (rv);
155 	}
156 
157 	/* Pass data to the provider */
158 	rv = FUNCLIST(sessp->se_slotid)->C_DigestUpdate(sessp->se_handle,
159 	    pPart, ulPartLen);
160 
161 	/* Present consistent interface to the application */
162 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
163 		return (CKR_FUNCTION_FAILED);
164 	}
165 
166 	return (rv);
167 }
168 
169 /*
170  * C_DigestKey is a pure wrapper to the underlying provider.
171  * The only argument checked is whether or not hSession is valid.
172  */
173 CK_RV
174 C_DigestKey(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey)
175 {
176 	CK_RV rv;
177 	pkcs11_session_t *sessp;
178 
179 	/* Check for a fastpath */
180 	if (purefastpath || policyfastpath) {
181 		return (fast_funcs->C_DigestKey(hSession, hKey));
182 	}
183 
184 	if (!pkcs11_initialized) {
185 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
186 	}
187 
188 	/* Obtain the session pointer */
189 	HANDLE2SESSION(hSession, sessp, rv);
190 
191 	if (rv != CKR_OK) {
192 		return (rv);
193 	}
194 
195 	/* Pass data to the provider */
196 	rv = FUNCLIST(sessp->se_slotid)->C_DigestKey(sessp->se_handle, hKey);
197 
198 	/* Present consistent interface to the application */
199 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
200 		return (CKR_FUNCTION_FAILED);
201 	}
202 
203 	return (rv);
204 }
205 
206 /*
207  * C_DigestFinal is a pure wrapper to the underlying provider.
208  * The only argument checked is whether or not hSession is valid.
209  */
210 CK_RV
211 C_DigestFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest,
212     CK_ULONG_PTR pulDigestLen)
213 {
214 	CK_RV rv;
215 	pkcs11_session_t *sessp;
216 
217 	/* Check for a fastpath */
218 	if (purefastpath || policyfastpath) {
219 		return (fast_funcs->C_DigestFinal(hSession, pDigest,
220 			    pulDigestLen));
221 	}
222 
223 	if (!pkcs11_initialized) {
224 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
225 	}
226 
227 	/* Obtain the session pointer */
228 	HANDLE2SESSION(hSession, sessp, rv);
229 
230 	if (rv != CKR_OK) {
231 		return (rv);
232 	}
233 
234 	/* Pass data to the provider */
235 	rv = FUNCLIST(sessp->se_slotid)->C_DigestFinal(sessp->se_handle,
236 	    pDigest, pulDigestLen);
237 
238 	/* Present consistent interface to the application */
239 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
240 		return (CKR_FUNCTION_FAILED);
241 	}
242 
243 	return (rv);
244 }
245