8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / pkcs11 / libpkcs11 / common / metaSlotToken.c
blobdf9b0bcc6f4d61484f5c973327ef1566f0bd5fb7
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Slot and Token Management functions
30 * (as defined in PKCS#11 spec section 11.5)
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "metaGlobal.h"
38 extern CK_ULONG num_meta_sessions;
39 extern CK_ULONG num_rw_meta_sessions;
42 * meta_GetSlotList
44 * For the metaslot, this is a trivial function. The metaslot module,
45 * by defination, provides exactly one slot. The token is always present.
47 * This function is actually not called.
49 /* ARGSUSED */
50 CK_RV
51 meta_GetSlotList(CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList,
52 CK_ULONG_PTR pulCount)
54 CK_RV rv;
56 if (pulCount == NULL)
57 return (CKR_ARGUMENTS_BAD);
59 if (pSlotList == NULL) {
60 *pulCount = 1;
61 return (CKR_OK);
64 if (*pulCount < 1) {
65 rv = CKR_BUFFER_TOO_SMALL;
66 } else {
67 pSlotList[0] = METASLOT_SLOTID;
68 rv = CKR_OK;
70 *pulCount = 1;
72 return (rv);
77 * meta_GetSlotInfo
79 * Returns basic information about the metaslot.
81 * The slotID argument is ignored.
83 /*ARGSUSED*/
84 CK_RV
85 meta_GetSlotInfo(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo)
87 CK_SLOT_INFO slotinfo;
88 CK_SLOT_ID true_id;
89 CK_RV rv;
91 if (!metaslot_enabled) {
92 return (CKR_SLOT_ID_INVALID);
95 if (pInfo == NULL) {
96 return (CKR_ARGUMENTS_BAD);
99 /* Provide information about the slot in the provided buffer */
100 (void) memcpy(pInfo->slotDescription, METASLOT_SLOT_DESCRIPTION, 64);
101 (void) memcpy(pInfo->manufacturerID, METASLOT_MANUFACTURER_ID, 32);
102 pInfo->hardwareVersion.major = METASLOT_HARDWARE_VERSION_MAJOR;
103 pInfo->hardwareVersion.minor = METASLOT_HARDWARE_VERSION_MINOR;
104 pInfo->firmwareVersion.major = METASLOT_FIRMWARE_VERSION_MAJOR;
105 pInfo->firmwareVersion.minor = METASLOT_FIRMWARE_VERSION_MINOR;
107 /* Find out token is present in the underlying keystore */
108 true_id = TRUEID(metaslot_keystore_slotid);
110 rv = FUNCLIST(metaslot_keystore_slotid)->C_GetSlotInfo(true_id,
111 &slotinfo);
112 if ((rv == CKR_OK) && (slotinfo.flags & CKF_TOKEN_PRESENT)) {
114 * store the token present flag if it is successfully
115 * received from the keystore slot.
116 * If not, this flag will not be set.
118 pInfo->flags = CKF_TOKEN_PRESENT;
121 return (CKR_OK);
126 * meta_GetTokenInfo
128 * Returns basic information about the metaslot "token."
130 * The slotID argument is ignored.
133 /*ARGSUSED*/
134 CK_RV
135 meta_GetTokenInfo(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo)
137 CK_RV rv;
138 CK_TOKEN_INFO metainfo;
139 CK_SLOT_ID true_id;
141 if (!metaslot_enabled) {
142 return (CKR_SLOT_ID_INVALID);
145 if (pInfo == NULL)
146 return (CKR_ARGUMENTS_BAD);
148 true_id = TRUEID(metaslot_keystore_slotid);
150 rv = FUNCLIST(metaslot_keystore_slotid)->C_GetTokenInfo(true_id,
151 &metainfo);
154 * If we could not get information about the object token, use
155 * default values. This allows metaslot to be used even if there
156 * are problems with the object token (eg, it's not present).
158 if (rv != CKR_OK) {
159 metainfo.ulTotalPublicMemory = CK_UNAVAILABLE_INFORMATION;
160 metainfo.ulFreePublicMemory = CK_UNAVAILABLE_INFORMATION;
161 metainfo.ulTotalPrivateMemory = CK_UNAVAILABLE_INFORMATION;
162 metainfo.ulFreePrivateMemory = CK_UNAVAILABLE_INFORMATION;
164 metainfo.flags = CKF_WRITE_PROTECTED;
166 metainfo.ulMaxPinLen = 0;
167 metainfo.ulMinPinLen = 0;
168 metainfo.hardwareVersion.major =
169 METASLOT_HARDWARE_VERSION_MAJOR;
170 metainfo.hardwareVersion.minor =
171 METASLOT_HARDWARE_VERSION_MINOR;
172 metainfo.firmwareVersion.major =
173 METASLOT_FIRMWARE_VERSION_MAJOR;
174 metainfo.firmwareVersion.minor =
175 METASLOT_FIRMWARE_VERSION_MINOR;
179 * Override some values that the object token may have set. They
180 * can be inappropriate/misleading when used in the context of
181 * metaslot.
183 (void) memcpy(metainfo.label, METASLOT_TOKEN_LABEL, 32);
184 (void) memcpy(metainfo.manufacturerID,
185 METASLOT_MANUFACTURER_ID, 32);
186 (void) memcpy(metainfo.model, METASLOT_TOKEN_MODEL, 16);
187 (void) memset(metainfo.serialNumber, ' ', 16);
189 metainfo.ulMaxSessionCount = CK_EFFECTIVELY_INFINITE;
190 metainfo.ulSessionCount = num_meta_sessions;
191 metainfo.ulMaxRwSessionCount = CK_EFFECTIVELY_INFINITE;
192 metainfo.ulRwSessionCount = num_rw_meta_sessions;
194 metainfo.flags |= CKF_RNG;
195 metainfo.flags &= ~CKF_RESTORE_KEY_NOT_NEEDED;
196 metainfo.flags |= CKF_TOKEN_INITIALIZED;
197 metainfo.flags &= ~CKF_SECONDARY_AUTHENTICATION;
199 /* Clear the time field if the token does not have a clock. */
200 if (!(metainfo.flags & CKF_CLOCK_ON_TOKEN))
201 (void) memset(metainfo.utcTime, ' ', 16);
203 *pInfo = metainfo;
205 return (CKR_OK);
210 * meta_WaitForSlotEvent
212 * The metaslot never generates events, so this function doesn't do anything
213 * useful. We do not pass on provider events because we want to hide details
214 * of the providers.
216 * If CKF_DONT_BLOCK flag is turned on, CKR_NO_EVENT will be return.
217 * Otherwise, return CKR_FUNCTION_FAILED.
220 /* ARGSUSED */
221 CK_RV
222 meta_WaitForSlotEvent(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot,
223 CK_VOID_PTR pReserved)
225 if (flags & CKF_DONT_BLOCK) {
226 return (CKR_NO_EVENT);
227 } else {
228 return (CKR_FUNCTION_FAILED);
234 * meta_GetMechanismList
236 * The slotID argument is not used.
239 /*ARGSUSED*/
240 CK_RV
241 meta_GetMechanismList(CK_SLOT_ID slotID, CK_MECHANISM_TYPE_PTR pMechanismList,
242 CK_ULONG_PTR pulCount)
244 CK_RV rv;
246 if (!metaslot_enabled) {
247 return (CKR_SLOT_ID_INVALID);
250 if (pulCount == NULL)
251 return (CKR_ARGUMENTS_BAD);
253 rv = meta_mechManager_get_mechs(pMechanismList, pulCount);
255 if ((rv == CKR_BUFFER_TOO_SMALL) && (pMechanismList == NULL)) {
257 * if pMechanismList is not provided, just need to
258 * return count
260 rv = CKR_OK;
262 return (rv);
267 * meta_GetMechanismInfo
269 * The slotID argument is not used.
271 /*ARGSUSED*/
272 CK_RV
273 meta_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type,
274 CK_MECHANISM_INFO_PTR pInfo)
276 CK_RV rv;
277 mechinfo_t **slots = NULL;
278 unsigned long i, slotCount = 0;
279 mech_support_info_t mech_support_info;
281 if (!metaslot_enabled) {
282 return (CKR_SLOT_ID_INVALID);
285 if (pInfo == NULL) {
286 return (CKR_ARGUMENTS_BAD);
289 mech_support_info.supporting_slots =
290 malloc(meta_slotManager_get_slotcount() * sizeof (mechinfo_t *));
291 if (mech_support_info.supporting_slots == NULL) {
292 return (CKR_HOST_MEMORY);
295 mech_support_info.mech = type;
297 rv = meta_mechManager_get_slots(&mech_support_info, TRUE, NULL);
298 if (rv != CKR_OK) {
299 free(mech_support_info.supporting_slots);
300 return (rv);
303 slotCount = mech_support_info.num_supporting_slots;
304 slots = mech_support_info.supporting_slots;
306 /* Merge mechanism info from all slots. */
307 (void) memcpy(pInfo, &(slots[0]->mechanism_info),
308 sizeof (CK_MECHANISM_INFO));
310 /* no need to look at index 0, since that's what we started with */
311 for (i = 1; i < slotCount; i++) {
312 CK_ULONG thisValue;
314 /* MinKeySize should be smallest of all slots. */
315 thisValue = slots[i]->mechanism_info.ulMinKeySize;
316 if (thisValue < pInfo->ulMinKeySize) {
317 pInfo->ulMinKeySize = thisValue;
320 /* MaxKeySize should be largest of all slots. */
321 thisValue = slots[i]->mechanism_info.ulMaxKeySize;
322 if (thisValue > pInfo->ulMaxKeySize) {
323 pInfo->ulMaxKeySize = thisValue;
326 pInfo->flags |= slots[i]->mechanism_info.flags;
329 /* Clear the CKF_HW flag. We might select a software provider later. */
330 pInfo->flags &= ~CKF_HW;
332 /* Clear the extenstion flag. Spec says is should never even be set. */
333 pInfo->flags &= ~CKF_EXTENSION;
335 free(mech_support_info.supporting_slots);
337 return (CKR_OK);
342 * meta_InitToken
344 * Not supported. The metaslot "token" is always initialized. The token object
345 * token must already be initialized. Other vendors don't seem to support
346 * this anyway.
348 /* ARGSUSED */
349 CK_RV
350 meta_InitToken(CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen,
351 CK_UTF8CHAR_PTR pLabel)
353 return (CKR_FUNCTION_NOT_SUPPORTED);
358 * meta_InitPIN
360 * Not supported. Same reason as C_InitToken.
362 /* ARGSUSED */
363 CK_RV
364 meta_InitPIN(CK_SESSION_HANDLE hSession,
365 CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen)
367 return (CKR_FUNCTION_NOT_SUPPORTED);
372 * meta_SetPIN
374 * This is basically just a pass-thru to the object token. No need to
375 * even check the arguments, since we don't use them.
377 CK_RV
378 meta_SetPIN(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pOldPin,
379 CK_ULONG ulOldPinLen, CK_UTF8CHAR_PTR pNewPin, CK_ULONG ulNewPinLen)
381 CK_RV rv;
382 meta_session_t *session;
383 slot_session_t *slot_session;
385 rv = meta_handle2session(hSession, &session);
386 if (rv != CKR_OK)
387 return (rv);
389 if (IS_READ_ONLY_SESSION(session->session_flags)) {
390 REFRELEASE(session);
391 return (CKR_SESSION_READ_ONLY);
394 rv = meta_get_slot_session(get_keystore_slotnum(), &slot_session,
395 session->session_flags);
396 if (rv != CKR_OK) {
397 REFRELEASE(session);
398 return (rv);
401 rv = FUNCLIST(slot_session->fw_st_id)->C_SetPIN(slot_session->hSession,
402 pOldPin, ulOldPinLen, pNewPin, ulNewPinLen);
404 meta_release_slot_session(slot_session);
406 REFRELEASE(session);
407 return (rv);