Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / softoken / fipstokn.c
blob3dd386b0312972f915c6d3f6f5861757c29dd4b8
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 * This file implements PKCS 11 on top of our existing security modules
39 * For more information about PKCS 11 See PKCS 11 Token Inteface Standard.
40 * This implementation has two slots:
41 * slot 1 is our generic crypto support. It does not require login
42 * (unless you've enabled FIPS). It supports Public Key ops, and all they
43 * bulk ciphers and hashes. It can also support Private Key ops for imported
44 * Private keys. It does not have any token storage.
45 * slot 2 is our private key support. It requires a login before use. It
46 * can store Private Keys and Certs as token objects. Currently only private
47 * keys and their associated Certificates are saved on the token.
49 * In this implementation, session objects are only visible to the session
50 * that created or generated them.
52 #include "seccomon.h"
53 #include "softoken.h"
54 #include "lowkeyi.h"
55 #include "pkcs11.h"
56 #include "pkcs11i.h"
57 #include "prenv.h"
58 #include "prprf.h"
60 #include <ctype.h>
62 #ifdef XP_UNIX
63 #define NSS_AUDIT_WITH_SYSLOG 1
64 #include <syslog.h>
65 #include <unistd.h>
66 #endif
68 #ifdef SOLARIS
69 #include <bsm/libbsm.h>
70 #define AUE_FIPS_AUDIT 34444
71 #endif
73 #ifdef LINUX
74 #include <pthread.h>
75 #include <dlfcn.h>
76 #define LIBAUDIT_NAME "libaudit.so.0"
77 #ifndef AUDIT_USER
78 #define AUDIT_USER 1005 /* message type: message from userspace */
79 #endif
80 static void *libaudit_handle;
81 static int (*audit_open_func)(void);
82 static void (*audit_close_func)(int fd);
83 static int (*audit_log_user_message_func)(int audit_fd, int type,
84 const char *message, const char *hostname, const char *addr,
85 const char *tty, int result);
86 static int (*audit_send_user_message_func)(int fd, int type,
87 const char *message);
89 static pthread_once_t libaudit_once_control = PTHREAD_ONCE_INIT;
91 static void
92 libaudit_init(void)
94 libaudit_handle = dlopen(LIBAUDIT_NAME, RTLD_LAZY);
95 if (!libaudit_handle) {
96 return;
98 audit_open_func = dlsym(libaudit_handle, "audit_open");
99 audit_close_func = dlsym(libaudit_handle, "audit_close");
101 * audit_send_user_message is the older function.
102 * audit_log_user_message, if available, is preferred.
104 audit_log_user_message_func = dlsym(libaudit_handle,
105 "audit_log_user_message");
106 if (!audit_log_user_message_func) {
107 audit_send_user_message_func = dlsym(libaudit_handle,
108 "audit_send_user_message");
110 if (!audit_open_func || !audit_close_func ||
111 (!audit_log_user_message_func && !audit_send_user_message_func)) {
112 dlclose(libaudit_handle);
113 libaudit_handle = NULL;
114 audit_open_func = NULL;
115 audit_close_func = NULL;
116 audit_log_user_message_func = NULL;
117 audit_send_user_message_func = NULL;
120 #endif /* LINUX */
124 * ******************** Password Utilities *******************************
126 static PRBool isLoggedIn = PR_FALSE;
127 PRBool sftk_fatalError = PR_FALSE;
130 * This function returns
131 * - CKR_PIN_INVALID if the password/PIN is not a legal UTF8 string
132 * - CKR_PIN_LEN_RANGE if the password/PIN is too short or does not
133 * consist of characters from three or more character classes.
134 * - CKR_OK otherwise
136 * The minimum password/PIN length is FIPS_MIN_PIN Unicode characters.
137 * We define five character classes: digits (0-9), ASCII lowercase letters,
138 * ASCII uppercase letters, ASCII non-alphanumeric characters (such as
139 * space and punctuation marks), and non-ASCII characters. If an ASCII
140 * uppercase letter is the first character of the password/PIN, the
141 * uppercase letter is not counted toward its character class. Similarly,
142 * if a digit is the last character of the password/PIN, the digit is not
143 * counted toward its character class.
145 * Although NSC_SetPIN and NSC_InitPIN already do the maximum and minimum
146 * password/PIN length checks, they check the length in bytes as opposed
147 * to characters. To meet the minimum password/PIN guessing probability
148 * requirements in FIPS 140-2, we need to check the length in characters.
150 static CK_RV sftk_newPinCheck(CK_CHAR_PTR pPin, CK_ULONG ulPinLen) {
151 unsigned int i;
152 int nchar = 0; /* number of characters */
153 int ntrail = 0; /* number of trailing bytes to follow */
154 int ndigit = 0; /* number of decimal digits */
155 int nlower = 0; /* number of ASCII lowercase letters */
156 int nupper = 0; /* number of ASCII uppercase letters */
157 int nnonalnum = 0; /* number of ASCII non-alphanumeric characters */
158 int nnonascii = 0; /* number of non-ASCII characters */
159 int nclass; /* number of character classes */
161 for (i = 0; i < ulPinLen; i++) {
162 unsigned int byte = pPin[i];
164 if (ntrail) {
165 if ((byte & 0xc0) != 0x80) {
166 /* illegal */
167 nchar = -1;
168 break;
170 if (--ntrail == 0) {
171 nchar++;
172 nnonascii++;
174 continue;
176 if ((byte & 0x80) == 0x00) {
177 /* single-byte (ASCII) character */
178 nchar++;
179 if (isdigit(byte)) {
180 if (i < ulPinLen - 1) {
181 ndigit++;
183 } else if (islower(byte)) {
184 nlower++;
185 } else if (isupper(byte)) {
186 if (i > 0) {
187 nupper++;
189 } else {
190 nnonalnum++;
192 } else if ((byte & 0xe0) == 0xc0) {
193 /* leading byte of two-byte character */
194 ntrail = 1;
195 } else if ((byte & 0xf0) == 0xe0) {
196 /* leading byte of three-byte character */
197 ntrail = 2;
198 } else if ((byte & 0xf8) == 0xf0) {
199 /* leading byte of four-byte character */
200 ntrail = 3;
201 } else {
202 /* illegal */
203 nchar = -1;
204 break;
207 if (nchar == -1) {
208 /* illegal UTF8 string */
209 return CKR_PIN_INVALID;
211 if (nchar < FIPS_MIN_PIN) {
212 return CKR_PIN_LEN_RANGE;
214 nclass = (ndigit != 0) + (nlower != 0) + (nupper != 0) +
215 (nnonalnum != 0) + (nnonascii != 0);
216 if (nclass < 3) {
217 return CKR_PIN_LEN_RANGE;
219 return CKR_OK;
223 /* FIPS required checks before any useful cryptographic services */
224 static CK_RV sftk_fipsCheck(void) {
225 if (sftk_fatalError)
226 return CKR_DEVICE_ERROR;
227 if (!isLoggedIn)
228 return CKR_USER_NOT_LOGGED_IN;
229 return CKR_OK;
233 #define SFTK_FIPSCHECK() \
234 CK_RV rv; \
235 if ((rv = sftk_fipsCheck()) != CKR_OK) return rv;
237 #define SFTK_FIPSFATALCHECK() \
238 if (sftk_fatalError) return CKR_DEVICE_ERROR;
241 /* grab an attribute out of a raw template */
242 void *
243 fc_getAttribute(CK_ATTRIBUTE_PTR pTemplate,
244 CK_ULONG ulCount, CK_ATTRIBUTE_TYPE type)
246 int i;
248 for (i=0; i < (int) ulCount; i++) {
249 if (pTemplate[i].type == type) {
250 return pTemplate[i].pValue;
253 return NULL;
257 #define __PASTE(x,y) x##y
259 /* ------------- forward declare all the NSC_ functions ------------- */
260 #undef CK_NEED_ARG_LIST
261 #undef CK_PKCS11_FUNCTION_INFO
263 #define CK_PKCS11_FUNCTION_INFO(name) CK_RV __PASTE(NS,name)
264 #define CK_NEED_ARG_LIST 1
266 #include "pkcs11f.h"
268 /* ------------- forward declare all the FIPS functions ------------- */
269 #undef CK_NEED_ARG_LIST
270 #undef CK_PKCS11_FUNCTION_INFO
272 #define CK_PKCS11_FUNCTION_INFO(name) CK_RV __PASTE(F,name)
273 #define CK_NEED_ARG_LIST 1
275 #include "pkcs11f.h"
277 /* ------------- build the CK_CRYPTO_TABLE ------------------------- */
278 static CK_FUNCTION_LIST sftk_fipsTable = {
279 { 1, 10 },
281 #undef CK_NEED_ARG_LIST
282 #undef CK_PKCS11_FUNCTION_INFO
284 #define CK_PKCS11_FUNCTION_INFO(name) __PASTE(F,name),
287 #include "pkcs11f.h"
291 #undef CK_NEED_ARG_LIST
292 #undef CK_PKCS11_FUNCTION_INFO
295 #undef __PASTE
297 /* CKO_NOT_A_KEY can be any object class that's not a key object. */
298 #define CKO_NOT_A_KEY CKO_DATA
300 #define SFTK_IS_KEY_OBJECT(objClass) \
301 (((objClass) == CKO_PUBLIC_KEY) || \
302 ((objClass) == CKO_PRIVATE_KEY) || \
303 ((objClass) == CKO_SECRET_KEY))
305 #define SFTK_IS_NONPUBLIC_KEY_OBJECT(objClass) \
306 (((objClass) == CKO_PRIVATE_KEY) || ((objClass) == CKO_SECRET_KEY))
308 static CK_RV
309 sftk_get_object_class_and_fipsCheck(CK_SESSION_HANDLE hSession,
310 CK_OBJECT_HANDLE hObject, CK_OBJECT_CLASS *pObjClass)
312 CK_RV rv;
313 CK_ATTRIBUTE class;
314 class.type = CKA_CLASS;
315 class.pValue = pObjClass;
316 class.ulValueLen = sizeof(*pObjClass);
317 rv = NSC_GetAttributeValue(hSession, hObject, &class, 1);
318 if ((rv == CKR_OK) && SFTK_IS_NONPUBLIC_KEY_OBJECT(*pObjClass)) {
319 rv = sftk_fipsCheck();
321 return rv;
324 /**********************************************************************
326 * FIPS 140 auditable event logging
328 **********************************************************************/
330 PRBool sftk_audit_enabled = PR_FALSE;
333 * Each audit record must have the following information:
334 * - Date and time of the event
335 * - Type of event
336 * - user (subject) identity
337 * - outcome (success or failure) of the event
338 * - process ID
339 * - name (ID) of the object
340 * - for changes to data (except for authentication data and CSPs), the new
341 * and old values of the data
342 * - for authentication attempts, the origin of the attempt (e.g., terminal
343 * identifier)
344 * - for assuming a role, the type of role, and the location of the request
346 void
347 sftk_LogAuditMessage(NSSAuditSeverity severity, const char *msg)
349 #ifdef NSS_AUDIT_WITH_SYSLOG
350 int level;
352 switch (severity) {
353 case NSS_AUDIT_ERROR:
354 level = LOG_ERR;
355 break;
356 case NSS_AUDIT_WARNING:
357 level = LOG_WARNING;
358 break;
359 default:
360 level = LOG_INFO;
361 break;
363 /* timestamp is provided by syslog in the message header */
364 syslog(level | LOG_USER /* facility */,
365 "NSS " SOFTOKEN_LIB_NAME "[pid=%d uid=%d]: %s",
366 (int)getpid(), (int)getuid(), msg);
367 #ifdef LINUX
368 if (pthread_once(&libaudit_once_control, libaudit_init) != 0) {
369 return;
371 if (libaudit_handle) {
372 int audit_fd;
373 int result = (severity != NSS_AUDIT_ERROR); /* 1=success; 0=failed */
374 char *message = PR_smprintf("NSS " SOFTOKEN_LIB_NAME ": %s", msg);
375 if (!message) {
376 return;
378 audit_fd = audit_open_func();
379 if (audit_fd < 0) {
380 PR_smprintf_free(message);
381 return;
383 if (audit_log_user_message_func) {
384 audit_log_user_message_func(audit_fd, AUDIT_USER, message,
385 NULL, NULL, NULL, result);
386 } else {
387 audit_send_user_message_func(audit_fd, AUDIT_USER, message);
389 audit_close_func(audit_fd);
390 PR_smprintf_free(message);
392 #endif /* LINUX */
393 #ifdef SOLARIS
395 int rd;
396 char *message = PR_smprintf("NSS " SOFTOKEN_LIB_NAME ": %s", msg);
398 if (!message) {
399 return;
402 /* open the record descriptor */
403 if ((rd = au_open()) == -1) {
404 PR_smprintf_free(message);
405 return;
408 /* write the audit tokens to the audit record */
409 if (au_write(rd, au_to_text(message))) {
410 (void)au_close(rd, AU_TO_NO_WRITE, AUE_FIPS_AUDIT);
411 PR_smprintf_free(message);
412 return;
415 /* close the record and send it to the audit trail */
416 (void)au_close(rd, AU_TO_WRITE, AUE_FIPS_AUDIT);
418 PR_smprintf_free(message);
420 #endif /* SOLARIS */
421 #else
422 /* do nothing */
423 #endif
427 /**********************************************************************
429 * Start of PKCS 11 functions
431 **********************************************************************/
432 /* return the function list */
433 CK_RV FC_GetFunctionList(CK_FUNCTION_LIST_PTR *pFunctionList) {
434 *pFunctionList = &sftk_fipsTable;
435 return CKR_OK;
438 /* sigh global so pkcs11 can read it */
439 PRBool nsf_init = PR_FALSE;
441 /* FC_Initialize initializes the PKCS #11 library. */
442 CK_RV FC_Initialize(CK_VOID_PTR pReserved) {
443 const char *envp;
444 CK_RV crv;
446 if (nsf_init) {
447 return CKR_CRYPTOKI_ALREADY_INITIALIZED;
450 if ((envp = PR_GetEnv("NSS_ENABLE_AUDIT")) != NULL) {
451 sftk_audit_enabled = (atoi(envp) == 1);
454 crv = nsc_CommonInitialize(pReserved, PR_TRUE);
456 /* not an 'else' rv can be set by either SFTK_LowInit or SFTK_SlotInit*/
457 if (crv != CKR_OK) {
458 sftk_fatalError = PR_TRUE;
459 return crv;
462 sftk_fatalError = PR_FALSE; /* any error has been reset */
464 crv = sftk_fipsPowerUpSelfTest();
465 if (crv != CKR_OK) {
466 nsc_CommonFinalize(NULL, PR_TRUE);
467 sftk_fatalError = PR_TRUE;
468 if (sftk_audit_enabled) {
469 char msg[128];
470 PR_snprintf(msg,sizeof msg,
471 "C_Initialize()=0x%08lX "
472 "power-up self-tests failed",
473 (PRUint32)crv);
474 sftk_LogAuditMessage(NSS_AUDIT_ERROR, msg);
476 return crv;
478 nsf_init = PR_TRUE;
480 return CKR_OK;
483 /*FC_Finalize indicates that an application is done with the PKCS #11 library.*/
484 CK_RV FC_Finalize (CK_VOID_PTR pReserved) {
485 CK_RV crv;
486 if (!nsf_init) {
487 return CKR_OK;
489 crv = nsc_CommonFinalize (pReserved, PR_TRUE);
490 nsf_init = (PRBool) !(crv == CKR_OK);
491 return crv;
495 /* FC_GetInfo returns general information about PKCS #11. */
496 CK_RV FC_GetInfo(CK_INFO_PTR pInfo) {
497 return NSC_GetInfo(pInfo);
500 /* FC_GetSlotList obtains a list of slots in the system. */
501 CK_RV FC_GetSlotList(CK_BBOOL tokenPresent,
502 CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount) {
503 return nsc_CommonGetSlotList(tokenPresent,pSlotList,pulCount,
504 NSC_FIPS_MODULE);
507 /* FC_GetSlotInfo obtains information about a particular slot in the system. */
508 CK_RV FC_GetSlotInfo(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo) {
509 return NSC_GetSlotInfo(slotID,pInfo);
513 /*FC_GetTokenInfo obtains information about a particular token in the system.*/
514 CK_RV FC_GetTokenInfo(CK_SLOT_ID slotID,CK_TOKEN_INFO_PTR pInfo) {
515 CK_RV crv;
517 crv = NSC_GetTokenInfo(slotID,pInfo);
518 if (crv == CKR_OK)
519 pInfo->flags |= CKF_LOGIN_REQUIRED;
520 return crv;
526 /*FC_GetMechanismList obtains a list of mechanism types supported by a token.*/
527 CK_RV FC_GetMechanismList(CK_SLOT_ID slotID,
528 CK_MECHANISM_TYPE_PTR pMechanismList, CK_ULONG_PTR pusCount) {
529 SFTK_FIPSFATALCHECK();
530 if (slotID == FIPS_SLOT_ID) slotID = NETSCAPE_SLOT_ID;
531 /* FIPS Slot supports all functions */
532 return NSC_GetMechanismList(slotID,pMechanismList,pusCount);
536 /* FC_GetMechanismInfo obtains information about a particular mechanism
537 * possibly supported by a token. */
538 CK_RV FC_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type,
539 CK_MECHANISM_INFO_PTR pInfo) {
540 SFTK_FIPSFATALCHECK();
541 if (slotID == FIPS_SLOT_ID) slotID = NETSCAPE_SLOT_ID;
542 /* FIPS Slot supports all functions */
543 return NSC_GetMechanismInfo(slotID,type,pInfo);
547 /* FC_InitToken initializes a token. */
548 CK_RV FC_InitToken(CK_SLOT_ID slotID,CK_CHAR_PTR pPin,
549 CK_ULONG usPinLen,CK_CHAR_PTR pLabel) {
550 CK_RV crv;
552 crv = NSC_InitToken(slotID,pPin,usPinLen,pLabel);
553 if (sftk_audit_enabled) {
554 char msg[128];
555 NSSAuditSeverity severity = (crv == CKR_OK) ?
556 NSS_AUDIT_INFO : NSS_AUDIT_ERROR;
557 /* pLabel points to a 32-byte label, which is not null-terminated */
558 PR_snprintf(msg,sizeof msg,
559 "C_InitToken(slotID=%lu, pLabel=\"%.32s\")=0x%08lX",
560 (PRUint32)slotID,pLabel,(PRUint32)crv);
561 sftk_LogAuditMessage(severity, msg);
563 return crv;
567 /* FC_InitPIN initializes the normal user's PIN. */
568 CK_RV FC_InitPIN(CK_SESSION_HANDLE hSession,
569 CK_CHAR_PTR pPin, CK_ULONG ulPinLen) {
570 CK_RV rv;
571 if (sftk_fatalError) return CKR_DEVICE_ERROR;
572 if ((rv = sftk_newPinCheck(pPin,ulPinLen)) == CKR_OK) {
573 rv = NSC_InitPIN(hSession,pPin,ulPinLen);
575 if (sftk_audit_enabled) {
576 char msg[128];
577 NSSAuditSeverity severity = (rv == CKR_OK) ?
578 NSS_AUDIT_INFO : NSS_AUDIT_ERROR;
579 PR_snprintf(msg,sizeof msg,
580 "C_InitPIN(hSession=0x%08lX)=0x%08lX",
581 (PRUint32)hSession,(PRUint32)rv);
582 sftk_LogAuditMessage(severity, msg);
584 return rv;
588 /* FC_SetPIN modifies the PIN of user that is currently logged in. */
589 /* NOTE: This is only valid for the PRIVATE_KEY_SLOT */
590 CK_RV FC_SetPIN(CK_SESSION_HANDLE hSession, CK_CHAR_PTR pOldPin,
591 CK_ULONG usOldLen, CK_CHAR_PTR pNewPin, CK_ULONG usNewLen) {
592 CK_RV rv;
593 if ((rv = sftk_fipsCheck()) == CKR_OK &&
594 (rv = sftk_newPinCheck(pNewPin,usNewLen)) == CKR_OK) {
595 rv = NSC_SetPIN(hSession,pOldPin,usOldLen,pNewPin,usNewLen);
597 if (sftk_audit_enabled) {
598 char msg[128];
599 NSSAuditSeverity severity = (rv == CKR_OK) ?
600 NSS_AUDIT_INFO : NSS_AUDIT_ERROR;
601 PR_snprintf(msg,sizeof msg,
602 "C_SetPIN(hSession=0x%08lX)=0x%08lX",
603 (PRUint32)hSession,(PRUint32)rv);
604 sftk_LogAuditMessage(severity, msg);
606 return rv;
609 /* FC_OpenSession opens a session between an application and a token. */
610 CK_RV FC_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags,
611 CK_VOID_PTR pApplication,CK_NOTIFY Notify,CK_SESSION_HANDLE_PTR phSession) {
612 SFTK_FIPSFATALCHECK();
613 return NSC_OpenSession(slotID,flags,pApplication,Notify,phSession);
617 /* FC_CloseSession closes a session between an application and a token. */
618 CK_RV FC_CloseSession(CK_SESSION_HANDLE hSession) {
619 return NSC_CloseSession(hSession);
623 /* FC_CloseAllSessions closes all sessions with a token. */
624 CK_RV FC_CloseAllSessions (CK_SLOT_ID slotID) {
625 return NSC_CloseAllSessions (slotID);
629 /* FC_GetSessionInfo obtains information about the session. */
630 CK_RV FC_GetSessionInfo(CK_SESSION_HANDLE hSession,
631 CK_SESSION_INFO_PTR pInfo) {
632 CK_RV rv;
633 SFTK_FIPSFATALCHECK();
635 rv = NSC_GetSessionInfo(hSession,pInfo);
636 if (rv == CKR_OK) {
637 if ((isLoggedIn) && (pInfo->state == CKS_RO_PUBLIC_SESSION)) {
638 pInfo->state = CKS_RO_USER_FUNCTIONS;
640 if ((isLoggedIn) && (pInfo->state == CKS_RW_PUBLIC_SESSION)) {
641 pInfo->state = CKS_RW_USER_FUNCTIONS;
644 return rv;
647 /* FC_Login logs a user into a token. */
648 CK_RV FC_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType,
649 CK_CHAR_PTR pPin, CK_ULONG usPinLen) {
650 CK_RV rv;
651 PRBool successful;
652 if (sftk_fatalError) return CKR_DEVICE_ERROR;
653 rv = NSC_Login(hSession,userType,pPin,usPinLen);
654 successful = (rv == CKR_OK) || (rv == CKR_USER_ALREADY_LOGGED_IN);
655 if (successful)
656 isLoggedIn = PR_TRUE;
657 if (sftk_audit_enabled) {
658 char msg[128];
659 NSSAuditSeverity severity;
660 severity = successful ? NSS_AUDIT_INFO : NSS_AUDIT_ERROR;
661 PR_snprintf(msg,sizeof msg,
662 "C_Login(hSession=0x%08lX, userType=%lu)=0x%08lX",
663 (PRUint32)hSession,(PRUint32)userType,(PRUint32)rv);
664 sftk_LogAuditMessage(severity, msg);
666 return rv;
669 /* FC_Logout logs a user out from a token. */
670 CK_RV FC_Logout(CK_SESSION_HANDLE hSession) {
671 CK_RV rv;
672 if ((rv = sftk_fipsCheck()) == CKR_OK) {
673 rv = NSC_Logout(hSession);
674 isLoggedIn = PR_FALSE;
676 if (sftk_audit_enabled) {
677 char msg[128];
678 NSSAuditSeverity severity = (rv == CKR_OK) ?
679 NSS_AUDIT_INFO : NSS_AUDIT_ERROR;
680 PR_snprintf(msg,sizeof msg,
681 "C_Logout(hSession=0x%08lX)=0x%08lX",
682 (PRUint32)hSession,(PRUint32)rv);
683 sftk_LogAuditMessage(severity, msg);
685 return rv;
689 /* FC_CreateObject creates a new object. */
690 CK_RV FC_CreateObject(CK_SESSION_HANDLE hSession,
691 CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
692 CK_OBJECT_HANDLE_PTR phObject) {
693 CK_OBJECT_CLASS * classptr;
694 SFTK_FIPSCHECK();
695 classptr = (CK_OBJECT_CLASS *)fc_getAttribute(pTemplate,ulCount,CKA_CLASS);
696 if (classptr == NULL) return CKR_TEMPLATE_INCOMPLETE;
698 /* FIPS can't create keys from raw key material */
699 if (SFTK_IS_NONPUBLIC_KEY_OBJECT(*classptr)) {
700 rv = CKR_ATTRIBUTE_VALUE_INVALID;
701 } else {
702 rv = NSC_CreateObject(hSession,pTemplate,ulCount,phObject);
704 if (sftk_audit_enabled && SFTK_IS_KEY_OBJECT(*classptr)) {
705 sftk_AuditCreateObject(hSession,pTemplate,ulCount,phObject,rv);
707 return rv;
714 /* FC_CopyObject copies an object, creating a new object for the copy. */
715 CK_RV FC_CopyObject(CK_SESSION_HANDLE hSession,
716 CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
717 CK_OBJECT_HANDLE_PTR phNewObject) {
718 CK_RV rv;
719 CK_OBJECT_CLASS objClass = CKO_NOT_A_KEY;
720 SFTK_FIPSFATALCHECK();
721 rv = sftk_get_object_class_and_fipsCheck(hSession, hObject, &objClass);
722 if (rv == CKR_OK) {
723 rv = NSC_CopyObject(hSession,hObject,pTemplate,ulCount,phNewObject);
725 if (sftk_audit_enabled && SFTK_IS_KEY_OBJECT(objClass)) {
726 sftk_AuditCopyObject(hSession,
727 hObject,pTemplate,ulCount,phNewObject,rv);
729 return rv;
733 /* FC_DestroyObject destroys an object. */
734 CK_RV FC_DestroyObject(CK_SESSION_HANDLE hSession,
735 CK_OBJECT_HANDLE hObject) {
736 CK_RV rv;
737 CK_OBJECT_CLASS objClass = CKO_NOT_A_KEY;
738 SFTK_FIPSFATALCHECK();
739 rv = sftk_get_object_class_and_fipsCheck(hSession, hObject, &objClass);
740 if (rv == CKR_OK) {
741 rv = NSC_DestroyObject(hSession,hObject);
743 if (sftk_audit_enabled && SFTK_IS_KEY_OBJECT(objClass)) {
744 sftk_AuditDestroyObject(hSession,hObject,rv);
746 return rv;
750 /* FC_GetObjectSize gets the size of an object in bytes. */
751 CK_RV FC_GetObjectSize(CK_SESSION_HANDLE hSession,
752 CK_OBJECT_HANDLE hObject, CK_ULONG_PTR pulSize) {
753 CK_RV rv;
754 CK_OBJECT_CLASS objClass = CKO_NOT_A_KEY;
755 SFTK_FIPSFATALCHECK();
756 rv = sftk_get_object_class_and_fipsCheck(hSession, hObject, &objClass);
757 if (rv == CKR_OK) {
758 rv = NSC_GetObjectSize(hSession, hObject, pulSize);
760 if (sftk_audit_enabled && SFTK_IS_KEY_OBJECT(objClass)) {
761 sftk_AuditGetObjectSize(hSession, hObject, pulSize, rv);
763 return rv;
767 /* FC_GetAttributeValue obtains the value of one or more object attributes. */
768 CK_RV FC_GetAttributeValue(CK_SESSION_HANDLE hSession,
769 CK_OBJECT_HANDLE hObject,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulCount) {
770 CK_RV rv;
771 CK_OBJECT_CLASS objClass = CKO_NOT_A_KEY;
772 SFTK_FIPSFATALCHECK();
773 rv = sftk_get_object_class_and_fipsCheck(hSession, hObject, &objClass);
774 if (rv == CKR_OK) {
775 rv = NSC_GetAttributeValue(hSession,hObject,pTemplate,ulCount);
777 if (sftk_audit_enabled && SFTK_IS_KEY_OBJECT(objClass)) {
778 sftk_AuditGetAttributeValue(hSession,hObject,pTemplate,ulCount,rv);
780 return rv;
784 /* FC_SetAttributeValue modifies the value of one or more object attributes */
785 CK_RV FC_SetAttributeValue (CK_SESSION_HANDLE hSession,
786 CK_OBJECT_HANDLE hObject,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulCount) {
787 CK_RV rv;
788 CK_OBJECT_CLASS objClass = CKO_NOT_A_KEY;
789 SFTK_FIPSFATALCHECK();
790 rv = sftk_get_object_class_and_fipsCheck(hSession, hObject, &objClass);
791 if (rv == CKR_OK) {
792 rv = NSC_SetAttributeValue(hSession,hObject,pTemplate,ulCount);
794 if (sftk_audit_enabled && SFTK_IS_KEY_OBJECT(objClass)) {
795 sftk_AuditSetAttributeValue(hSession,hObject,pTemplate,ulCount,rv);
797 return rv;
802 /* FC_FindObjectsInit initializes a search for token and session objects
803 * that match a template. */
804 CK_RV FC_FindObjectsInit(CK_SESSION_HANDLE hSession,
805 CK_ATTRIBUTE_PTR pTemplate,CK_ULONG usCount) {
806 /* let publically readable object be found */
807 unsigned int i;
808 CK_RV rv;
809 PRBool needLogin = PR_FALSE;
811 SFTK_FIPSFATALCHECK();
813 for (i=0; i < usCount; i++) {
814 CK_OBJECT_CLASS class;
815 if (pTemplate[i].type != CKA_CLASS) {
816 continue;
818 if (pTemplate[i].ulValueLen != sizeof(CK_OBJECT_CLASS)) {
819 continue;
821 if (pTemplate[i].pValue == NULL) {
822 continue;
824 class = *(CK_OBJECT_CLASS *)pTemplate[i].pValue;
825 if ((class == CKO_PRIVATE_KEY) || (class == CKO_SECRET_KEY)) {
826 needLogin = PR_TRUE;
827 break;
830 if (needLogin) {
831 if ((rv = sftk_fipsCheck()) != CKR_OK) return rv;
833 return NSC_FindObjectsInit(hSession,pTemplate,usCount);
837 /* FC_FindObjects continues a search for token and session objects
838 * that match a template, obtaining additional object handles. */
839 CK_RV FC_FindObjects(CK_SESSION_HANDLE hSession,
840 CK_OBJECT_HANDLE_PTR phObject,CK_ULONG usMaxObjectCount,
841 CK_ULONG_PTR pusObjectCount) {
842 /* let publically readable object be found */
843 SFTK_FIPSFATALCHECK();
844 return NSC_FindObjects(hSession,phObject,usMaxObjectCount,
845 pusObjectCount);
850 ************** Crypto Functions: Encrypt ************************
853 /* FC_EncryptInit initializes an encryption operation. */
854 CK_RV FC_EncryptInit(CK_SESSION_HANDLE hSession,
855 CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey) {
856 SFTK_FIPSCHECK();
857 rv = NSC_EncryptInit(hSession,pMechanism,hKey);
858 if (sftk_audit_enabled) {
859 sftk_AuditCryptInit("Encrypt",hSession,pMechanism,hKey,rv);
861 return rv;
864 /* FC_Encrypt encrypts single-part data. */
865 CK_RV FC_Encrypt (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
866 CK_ULONG usDataLen, CK_BYTE_PTR pEncryptedData,
867 CK_ULONG_PTR pusEncryptedDataLen) {
868 SFTK_FIPSCHECK();
869 return NSC_Encrypt(hSession,pData,usDataLen,pEncryptedData,
870 pusEncryptedDataLen);
874 /* FC_EncryptUpdate continues a multiple-part encryption operation. */
875 CK_RV FC_EncryptUpdate(CK_SESSION_HANDLE hSession,
876 CK_BYTE_PTR pPart, CK_ULONG usPartLen, CK_BYTE_PTR pEncryptedPart,
877 CK_ULONG_PTR pusEncryptedPartLen) {
878 SFTK_FIPSCHECK();
879 return NSC_EncryptUpdate(hSession,pPart,usPartLen,pEncryptedPart,
880 pusEncryptedPartLen);
884 /* FC_EncryptFinal finishes a multiple-part encryption operation. */
885 CK_RV FC_EncryptFinal(CK_SESSION_HANDLE hSession,
886 CK_BYTE_PTR pLastEncryptedPart, CK_ULONG_PTR pusLastEncryptedPartLen) {
888 SFTK_FIPSCHECK();
889 return NSC_EncryptFinal(hSession,pLastEncryptedPart,
890 pusLastEncryptedPartLen);
894 ************** Crypto Functions: Decrypt ************************
898 /* FC_DecryptInit initializes a decryption operation. */
899 CK_RV FC_DecryptInit( CK_SESSION_HANDLE hSession,
900 CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey) {
901 SFTK_FIPSCHECK();
902 rv = NSC_DecryptInit(hSession,pMechanism,hKey);
903 if (sftk_audit_enabled) {
904 sftk_AuditCryptInit("Decrypt",hSession,pMechanism,hKey,rv);
906 return rv;
909 /* FC_Decrypt decrypts encrypted data in a single part. */
910 CK_RV FC_Decrypt(CK_SESSION_HANDLE hSession,
911 CK_BYTE_PTR pEncryptedData,CK_ULONG usEncryptedDataLen,CK_BYTE_PTR pData,
912 CK_ULONG_PTR pusDataLen) {
913 SFTK_FIPSCHECK();
914 return NSC_Decrypt(hSession,pEncryptedData,usEncryptedDataLen,pData,
915 pusDataLen);
919 /* FC_DecryptUpdate continues a multiple-part decryption operation. */
920 CK_RV FC_DecryptUpdate(CK_SESSION_HANDLE hSession,
921 CK_BYTE_PTR pEncryptedPart, CK_ULONG usEncryptedPartLen,
922 CK_BYTE_PTR pPart, CK_ULONG_PTR pusPartLen) {
923 SFTK_FIPSCHECK();
924 return NSC_DecryptUpdate(hSession,pEncryptedPart,usEncryptedPartLen,
925 pPart,pusPartLen);
929 /* FC_DecryptFinal finishes a multiple-part decryption operation. */
930 CK_RV FC_DecryptFinal(CK_SESSION_HANDLE hSession,
931 CK_BYTE_PTR pLastPart, CK_ULONG_PTR pusLastPartLen) {
932 SFTK_FIPSCHECK();
933 return NSC_DecryptFinal(hSession,pLastPart,pusLastPartLen);
938 ************** Crypto Functions: Digest (HASH) ************************
941 /* FC_DigestInit initializes a message-digesting operation. */
942 CK_RV FC_DigestInit(CK_SESSION_HANDLE hSession,
943 CK_MECHANISM_PTR pMechanism) {
944 SFTK_FIPSFATALCHECK();
945 return NSC_DigestInit(hSession, pMechanism);
949 /* FC_Digest digests data in a single part. */
950 CK_RV FC_Digest(CK_SESSION_HANDLE hSession,
951 CK_BYTE_PTR pData, CK_ULONG usDataLen, CK_BYTE_PTR pDigest,
952 CK_ULONG_PTR pusDigestLen) {
953 SFTK_FIPSFATALCHECK();
954 return NSC_Digest(hSession,pData,usDataLen,pDigest,pusDigestLen);
958 /* FC_DigestUpdate continues a multiple-part message-digesting operation. */
959 CK_RV FC_DigestUpdate(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pPart,
960 CK_ULONG usPartLen) {
961 SFTK_FIPSFATALCHECK();
962 return NSC_DigestUpdate(hSession,pPart,usPartLen);
966 /* FC_DigestFinal finishes a multiple-part message-digesting operation. */
967 CK_RV FC_DigestFinal(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pDigest,
968 CK_ULONG_PTR pusDigestLen) {
969 SFTK_FIPSFATALCHECK();
970 return NSC_DigestFinal(hSession,pDigest,pusDigestLen);
975 ************** Crypto Functions: Sign ************************
978 /* FC_SignInit initializes a signature (private key encryption) operation,
979 * where the signature is (will be) an appendix to the data,
980 * and plaintext cannot be recovered from the signature */
981 CK_RV FC_SignInit(CK_SESSION_HANDLE hSession,
982 CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey) {
983 SFTK_FIPSCHECK();
984 rv = NSC_SignInit(hSession,pMechanism,hKey);
985 if (sftk_audit_enabled) {
986 sftk_AuditCryptInit("Sign",hSession,pMechanism,hKey,rv);
988 return rv;
992 /* FC_Sign signs (encrypts with private key) data in a single part,
993 * where the signature is (will be) an appendix to the data,
994 * and plaintext cannot be recovered from the signature */
995 CK_RV FC_Sign(CK_SESSION_HANDLE hSession,
996 CK_BYTE_PTR pData,CK_ULONG usDataLen,CK_BYTE_PTR pSignature,
997 CK_ULONG_PTR pusSignatureLen) {
998 SFTK_FIPSCHECK();
999 return NSC_Sign(hSession,pData,usDataLen,pSignature,pusSignatureLen);
1003 /* FC_SignUpdate continues a multiple-part signature operation,
1004 * where the signature is (will be) an appendix to the data,
1005 * and plaintext cannot be recovered from the signature */
1006 CK_RV FC_SignUpdate(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pPart,
1007 CK_ULONG usPartLen) {
1008 SFTK_FIPSCHECK();
1009 return NSC_SignUpdate(hSession,pPart,usPartLen);
1013 /* FC_SignFinal finishes a multiple-part signature operation,
1014 * returning the signature. */
1015 CK_RV FC_SignFinal(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pSignature,
1016 CK_ULONG_PTR pusSignatureLen) {
1017 SFTK_FIPSCHECK();
1018 return NSC_SignFinal(hSession,pSignature,pusSignatureLen);
1022 ************** Crypto Functions: Sign Recover ************************
1024 /* FC_SignRecoverInit initializes a signature operation,
1025 * where the (digest) data can be recovered from the signature.
1026 * E.g. encryption with the user's private key */
1027 CK_RV FC_SignRecoverInit(CK_SESSION_HANDLE hSession,
1028 CK_MECHANISM_PTR pMechanism,CK_OBJECT_HANDLE hKey) {
1029 SFTK_FIPSCHECK();
1030 rv = NSC_SignRecoverInit(hSession,pMechanism,hKey);
1031 if (sftk_audit_enabled) {
1032 sftk_AuditCryptInit("SignRecover",hSession,pMechanism,hKey,rv);
1034 return rv;
1038 /* FC_SignRecover signs data in a single operation
1039 * where the (digest) data can be recovered from the signature.
1040 * E.g. encryption with the user's private key */
1041 CK_RV FC_SignRecover(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
1042 CK_ULONG usDataLen, CK_BYTE_PTR pSignature, CK_ULONG_PTR pusSignatureLen) {
1043 SFTK_FIPSCHECK();
1044 return NSC_SignRecover(hSession,pData,usDataLen,pSignature,pusSignatureLen);
1048 ************** Crypto Functions: verify ************************
1051 /* FC_VerifyInit initializes a verification operation,
1052 * where the signature is an appendix to the data,
1053 * and plaintext cannot be recovered from the signature (e.g. DSA) */
1054 CK_RV FC_VerifyInit(CK_SESSION_HANDLE hSession,
1055 CK_MECHANISM_PTR pMechanism,CK_OBJECT_HANDLE hKey) {
1056 SFTK_FIPSCHECK();
1057 rv = NSC_VerifyInit(hSession,pMechanism,hKey);
1058 if (sftk_audit_enabled) {
1059 sftk_AuditCryptInit("Verify",hSession,pMechanism,hKey,rv);
1061 return rv;
1065 /* FC_Verify verifies a signature in a single-part operation,
1066 * where the signature is an appendix to the data,
1067 * and plaintext cannot be recovered from the signature */
1068 CK_RV FC_Verify(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
1069 CK_ULONG usDataLen, CK_BYTE_PTR pSignature, CK_ULONG usSignatureLen) {
1070 /* make sure we're legal */
1071 SFTK_FIPSCHECK();
1072 return NSC_Verify(hSession,pData,usDataLen,pSignature,usSignatureLen);
1076 /* FC_VerifyUpdate continues a multiple-part verification operation,
1077 * where the signature is an appendix to the data,
1078 * and plaintext cannot be recovered from the signature */
1079 CK_RV FC_VerifyUpdate( CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
1080 CK_ULONG usPartLen) {
1081 SFTK_FIPSCHECK();
1082 return NSC_VerifyUpdate(hSession,pPart,usPartLen);
1086 /* FC_VerifyFinal finishes a multiple-part verification operation,
1087 * checking the signature. */
1088 CK_RV FC_VerifyFinal(CK_SESSION_HANDLE hSession,
1089 CK_BYTE_PTR pSignature,CK_ULONG usSignatureLen) {
1090 SFTK_FIPSCHECK();
1091 return NSC_VerifyFinal(hSession,pSignature,usSignatureLen);
1095 ************** Crypto Functions: Verify Recover ************************
1098 /* FC_VerifyRecoverInit initializes a signature verification operation,
1099 * where the data is recovered from the signature.
1100 * E.g. Decryption with the user's public key */
1101 CK_RV FC_VerifyRecoverInit(CK_SESSION_HANDLE hSession,
1102 CK_MECHANISM_PTR pMechanism,CK_OBJECT_HANDLE hKey) {
1103 SFTK_FIPSCHECK();
1104 rv = NSC_VerifyRecoverInit(hSession,pMechanism,hKey);
1105 if (sftk_audit_enabled) {
1106 sftk_AuditCryptInit("VerifyRecover",hSession,pMechanism,hKey,rv);
1108 return rv;
1112 /* FC_VerifyRecover verifies a signature in a single-part operation,
1113 * where the data is recovered from the signature.
1114 * E.g. Decryption with the user's public key */
1115 CK_RV FC_VerifyRecover(CK_SESSION_HANDLE hSession,
1116 CK_BYTE_PTR pSignature,CK_ULONG usSignatureLen,
1117 CK_BYTE_PTR pData,CK_ULONG_PTR pusDataLen) {
1118 SFTK_FIPSCHECK();
1119 return NSC_VerifyRecover(hSession,pSignature,usSignatureLen,pData,
1120 pusDataLen);
1124 **************************** Key Functions: ************************
1127 /* FC_GenerateKey generates a secret key, creating a new key object. */
1128 CK_RV FC_GenerateKey(CK_SESSION_HANDLE hSession,
1129 CK_MECHANISM_PTR pMechanism,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulCount,
1130 CK_OBJECT_HANDLE_PTR phKey) {
1131 CK_BBOOL *boolptr;
1133 SFTK_FIPSCHECK();
1135 /* all secret keys must be sensitive, if the upper level code tries to say
1136 * otherwise, reject it. */
1137 boolptr = (CK_BBOOL *) fc_getAttribute(pTemplate, ulCount, CKA_SENSITIVE);
1138 if (boolptr != NULL) {
1139 if (!(*boolptr)) {
1140 return CKR_ATTRIBUTE_VALUE_INVALID;
1144 rv = NSC_GenerateKey(hSession,pMechanism,pTemplate,ulCount,phKey);
1145 if (sftk_audit_enabled) {
1146 sftk_AuditGenerateKey(hSession,pMechanism,pTemplate,ulCount,phKey,rv);
1148 return rv;
1152 /* FC_GenerateKeyPair generates a public-key/private-key pair,
1153 * creating new key objects. */
1154 CK_RV FC_GenerateKeyPair (CK_SESSION_HANDLE hSession,
1155 CK_MECHANISM_PTR pMechanism, CK_ATTRIBUTE_PTR pPublicKeyTemplate,
1156 CK_ULONG usPublicKeyAttributeCount, CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
1157 CK_ULONG usPrivateKeyAttributeCount, CK_OBJECT_HANDLE_PTR phPublicKey,
1158 CK_OBJECT_HANDLE_PTR phPrivateKey) {
1159 CK_BBOOL *boolptr;
1160 CK_RV crv;
1162 SFTK_FIPSCHECK();
1164 /* all private keys must be sensitive, if the upper level code tries to say
1165 * otherwise, reject it. */
1166 boolptr = (CK_BBOOL *) fc_getAttribute(pPrivateKeyTemplate,
1167 usPrivateKeyAttributeCount, CKA_SENSITIVE);
1168 if (boolptr != NULL) {
1169 if (!(*boolptr)) {
1170 return CKR_ATTRIBUTE_VALUE_INVALID;
1173 crv = NSC_GenerateKeyPair (hSession,pMechanism,pPublicKeyTemplate,
1174 usPublicKeyAttributeCount,pPrivateKeyTemplate,
1175 usPrivateKeyAttributeCount,phPublicKey,phPrivateKey);
1176 if (crv == CKR_GENERAL_ERROR) {
1177 /* pairwise consistency check failed. */
1178 sftk_fatalError = PR_TRUE;
1180 if (sftk_audit_enabled) {
1181 sftk_AuditGenerateKeyPair(hSession,pMechanism,pPublicKeyTemplate,
1182 usPublicKeyAttributeCount,pPrivateKeyTemplate,
1183 usPrivateKeyAttributeCount,phPublicKey,phPrivateKey,crv);
1185 return crv;
1189 /* FC_WrapKey wraps (i.e., encrypts) a key. */
1190 CK_RV FC_WrapKey(CK_SESSION_HANDLE hSession,
1191 CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hWrappingKey,
1192 CK_OBJECT_HANDLE hKey, CK_BYTE_PTR pWrappedKey,
1193 CK_ULONG_PTR pulWrappedKeyLen) {
1194 SFTK_FIPSCHECK();
1195 rv = NSC_WrapKey(hSession,pMechanism,hWrappingKey,hKey,pWrappedKey,
1196 pulWrappedKeyLen);
1197 if (sftk_audit_enabled) {
1198 sftk_AuditWrapKey(hSession,pMechanism,hWrappingKey,hKey,pWrappedKey,
1199 pulWrappedKeyLen,rv);
1201 return rv;
1205 /* FC_UnwrapKey unwraps (decrypts) a wrapped key, creating a new key object. */
1206 CK_RV FC_UnwrapKey(CK_SESSION_HANDLE hSession,
1207 CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hUnwrappingKey,
1208 CK_BYTE_PTR pWrappedKey, CK_ULONG ulWrappedKeyLen,
1209 CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount,
1210 CK_OBJECT_HANDLE_PTR phKey) {
1211 CK_BBOOL *boolptr;
1213 SFTK_FIPSCHECK();
1215 /* all secret keys must be sensitive, if the upper level code tries to say
1216 * otherwise, reject it. */
1217 boolptr = (CK_BBOOL *) fc_getAttribute(pTemplate,
1218 ulAttributeCount, CKA_SENSITIVE);
1219 if (boolptr != NULL) {
1220 if (!(*boolptr)) {
1221 return CKR_ATTRIBUTE_VALUE_INVALID;
1224 rv = NSC_UnwrapKey(hSession,pMechanism,hUnwrappingKey,pWrappedKey,
1225 ulWrappedKeyLen,pTemplate,ulAttributeCount,phKey);
1226 if (sftk_audit_enabled) {
1227 sftk_AuditUnwrapKey(hSession,pMechanism,hUnwrappingKey,pWrappedKey,
1228 ulWrappedKeyLen,pTemplate,ulAttributeCount,phKey,rv);
1230 return rv;
1234 /* FC_DeriveKey derives a key from a base key, creating a new key object. */
1235 CK_RV FC_DeriveKey( CK_SESSION_HANDLE hSession,
1236 CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hBaseKey,
1237 CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount,
1238 CK_OBJECT_HANDLE_PTR phKey) {
1239 CK_BBOOL *boolptr;
1241 SFTK_FIPSCHECK();
1243 /* all secret keys must be sensitive, if the upper level code tries to say
1244 * otherwise, reject it. */
1245 boolptr = (CK_BBOOL *) fc_getAttribute(pTemplate,
1246 ulAttributeCount, CKA_SENSITIVE);
1247 if (boolptr != NULL) {
1248 if (!(*boolptr)) {
1249 return CKR_ATTRIBUTE_VALUE_INVALID;
1252 rv = NSC_DeriveKey(hSession,pMechanism,hBaseKey,pTemplate,
1253 ulAttributeCount, phKey);
1254 if (sftk_audit_enabled) {
1255 sftk_AuditDeriveKey(hSession,pMechanism,hBaseKey,pTemplate,
1256 ulAttributeCount,phKey,rv);
1258 return rv;
1262 **************************** Radom Functions: ************************
1265 /* FC_SeedRandom mixes additional seed material into the token's random number
1266 * generator. */
1267 CK_RV FC_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed,
1268 CK_ULONG usSeedLen) {
1269 CK_RV crv;
1271 SFTK_FIPSFATALCHECK();
1272 crv = NSC_SeedRandom(hSession,pSeed,usSeedLen);
1273 if (crv != CKR_OK) {
1274 sftk_fatalError = PR_TRUE;
1276 return crv;
1280 /* FC_GenerateRandom generates random data. */
1281 CK_RV FC_GenerateRandom(CK_SESSION_HANDLE hSession,
1282 CK_BYTE_PTR pRandomData, CK_ULONG ulRandomLen) {
1283 CK_RV crv;
1285 SFTK_FIPSFATALCHECK();
1286 crv = NSC_GenerateRandom(hSession,pRandomData,ulRandomLen);
1287 if (crv != CKR_OK) {
1288 sftk_fatalError = PR_TRUE;
1289 if (sftk_audit_enabled) {
1290 char msg[128];
1291 PR_snprintf(msg,sizeof msg,
1292 "C_GenerateRandom(hSession=0x%08lX, pRandomData=%p, "
1293 "ulRandomLen=%lu)=0x%08lX "
1294 "self-test: continuous RNG test failed",
1295 (PRUint32)hSession,pRandomData,
1296 (PRUint32)ulRandomLen,(PRUint32)crv);
1297 sftk_LogAuditMessage(NSS_AUDIT_ERROR, msg);
1300 return crv;
1304 /* FC_GetFunctionStatus obtains an updated status of a function running
1305 * in parallel with an application. */
1306 CK_RV FC_GetFunctionStatus(CK_SESSION_HANDLE hSession) {
1307 SFTK_FIPSCHECK();
1308 return NSC_GetFunctionStatus(hSession);
1312 /* FC_CancelFunction cancels a function running in parallel */
1313 CK_RV FC_CancelFunction(CK_SESSION_HANDLE hSession) {
1314 SFTK_FIPSCHECK();
1315 return NSC_CancelFunction(hSession);
1319 **************************** Version 1.1 Functions: ************************
1322 /* FC_GetOperationState saves the state of the cryptographic
1323 *operation in a session. */
1324 CK_RV FC_GetOperationState(CK_SESSION_HANDLE hSession,
1325 CK_BYTE_PTR pOperationState, CK_ULONG_PTR pulOperationStateLen) {
1326 SFTK_FIPSFATALCHECK();
1327 return NSC_GetOperationState(hSession,pOperationState,pulOperationStateLen);
1331 /* FC_SetOperationState restores the state of the cryptographic operation
1332 * in a session. */
1333 CK_RV FC_SetOperationState(CK_SESSION_HANDLE hSession,
1334 CK_BYTE_PTR pOperationState, CK_ULONG ulOperationStateLen,
1335 CK_OBJECT_HANDLE hEncryptionKey, CK_OBJECT_HANDLE hAuthenticationKey) {
1336 SFTK_FIPSFATALCHECK();
1337 return NSC_SetOperationState(hSession,pOperationState,ulOperationStateLen,
1338 hEncryptionKey,hAuthenticationKey);
1341 /* FC_FindObjectsFinal finishes a search for token and session objects. */
1342 CK_RV FC_FindObjectsFinal(CK_SESSION_HANDLE hSession) {
1343 /* let publically readable object be found */
1344 SFTK_FIPSFATALCHECK();
1345 return NSC_FindObjectsFinal(hSession);
1349 /* Dual-function cryptographic operations */
1351 /* FC_DigestEncryptUpdate continues a multiple-part digesting and encryption
1352 * operation. */
1353 CK_RV FC_DigestEncryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
1354 CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
1355 CK_ULONG_PTR pulEncryptedPartLen) {
1356 SFTK_FIPSCHECK();
1357 return NSC_DigestEncryptUpdate(hSession,pPart,ulPartLen,pEncryptedPart,
1358 pulEncryptedPartLen);
1362 /* FC_DecryptDigestUpdate continues a multiple-part decryption and digesting
1363 * operation. */
1364 CK_RV FC_DecryptDigestUpdate(CK_SESSION_HANDLE hSession,
1365 CK_BYTE_PTR pEncryptedPart, CK_ULONG ulEncryptedPartLen,
1366 CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen) {
1368 SFTK_FIPSCHECK();
1369 return NSC_DecryptDigestUpdate(hSession, pEncryptedPart,ulEncryptedPartLen,
1370 pPart,pulPartLen);
1373 /* FC_SignEncryptUpdate continues a multiple-part signing and encryption
1374 * operation. */
1375 CK_RV FC_SignEncryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
1376 CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
1377 CK_ULONG_PTR pulEncryptedPartLen) {
1379 SFTK_FIPSCHECK();
1380 return NSC_SignEncryptUpdate(hSession,pPart,ulPartLen,pEncryptedPart,
1381 pulEncryptedPartLen);
1384 /* FC_DecryptVerifyUpdate continues a multiple-part decryption and verify
1385 * operation. */
1386 CK_RV FC_DecryptVerifyUpdate(CK_SESSION_HANDLE hSession,
1387 CK_BYTE_PTR pEncryptedData, CK_ULONG ulEncryptedDataLen,
1388 CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen) {
1390 SFTK_FIPSCHECK();
1391 return NSC_DecryptVerifyUpdate(hSession,pEncryptedData,ulEncryptedDataLen,
1392 pData,pulDataLen);
1396 /* FC_DigestKey continues a multi-part message-digesting operation,
1397 * by digesting the value of a secret key as part of the data already digested.
1399 CK_RV FC_DigestKey(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey) {
1400 SFTK_FIPSCHECK();
1401 rv = NSC_DigestKey(hSession,hKey);
1402 if (sftk_audit_enabled) {
1403 sftk_AuditDigestKey(hSession,hKey,rv);
1405 return rv;
1409 CK_RV FC_WaitForSlotEvent(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot,
1410 CK_VOID_PTR pReserved)
1412 return NSC_WaitForSlotEvent(flags, pSlot, pReserved);