michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: /* michael@0: * This file implements PKCS 11 on top of our existing security modules michael@0: * michael@0: * For more information about PKCS 11 See PKCS 11 Token Inteface Standard. michael@0: * This implementation has two slots: michael@0: * slot 1 is our generic crypto support. It does not require login michael@0: * (unless you've enabled FIPS). It supports Public Key ops, and all they michael@0: * bulk ciphers and hashes. It can also support Private Key ops for imported michael@0: * Private keys. It does not have any token storage. michael@0: * slot 2 is our private key support. It requires a login before use. It michael@0: * can store Private Keys and Certs as token objects. Currently only private michael@0: * keys and their associated Certificates are saved on the token. michael@0: * michael@0: * In this implementation, session objects are only visible to the session michael@0: * that created or generated them. michael@0: */ michael@0: #include "seccomon.h" michael@0: #include "softoken.h" michael@0: #include "lowkeyi.h" michael@0: #include "pkcs11.h" michael@0: #include "pkcs11i.h" michael@0: #include "prenv.h" michael@0: #include "prprf.h" michael@0: michael@0: #include michael@0: michael@0: #ifdef XP_UNIX michael@0: #define NSS_AUDIT_WITH_SYSLOG 1 michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #ifdef LINUX michael@0: #include michael@0: #include michael@0: #define LIBAUDIT_NAME "libaudit.so.0" michael@0: #ifndef AUDIT_CRYPTO_TEST_USER michael@0: #define AUDIT_CRYPTO_TEST_USER 2400 /* Crypto test results */ michael@0: #define AUDIT_CRYPTO_PARAM_CHANGE_USER 2401 /* Crypto attribute change */ michael@0: #define AUDIT_CRYPTO_LOGIN 2402 /* Logged in as crypto officer */ michael@0: #define AUDIT_CRYPTO_LOGOUT 2403 /* Logged out from crypto */ michael@0: #define AUDIT_CRYPTO_KEY_USER 2404 /* Create,delete,negotiate */ michael@0: #define AUDIT_CRYPTO_FAILURE_USER 2405 /* Fail decrypt,encrypt,randomize */ michael@0: #endif michael@0: static void *libaudit_handle; michael@0: static int (*audit_open_func)(void); michael@0: static void (*audit_close_func)(int fd); michael@0: static int (*audit_log_user_message_func)(int audit_fd, int type, michael@0: const char *message, const char *hostname, const char *addr, michael@0: const char *tty, int result); michael@0: static int (*audit_send_user_message_func)(int fd, int type, michael@0: const char *message); michael@0: michael@0: static pthread_once_t libaudit_once_control = PTHREAD_ONCE_INIT; michael@0: michael@0: static void michael@0: libaudit_init(void) michael@0: { michael@0: libaudit_handle = dlopen(LIBAUDIT_NAME, RTLD_LAZY); michael@0: if (!libaudit_handle) { michael@0: return; michael@0: } michael@0: audit_open_func = dlsym(libaudit_handle, "audit_open"); michael@0: audit_close_func = dlsym(libaudit_handle, "audit_close"); michael@0: /* michael@0: * audit_send_user_message is the older function. michael@0: * audit_log_user_message, if available, is preferred. michael@0: */ michael@0: audit_log_user_message_func = dlsym(libaudit_handle, michael@0: "audit_log_user_message"); michael@0: if (!audit_log_user_message_func) { michael@0: audit_send_user_message_func = dlsym(libaudit_handle, michael@0: "audit_send_user_message"); michael@0: } michael@0: if (!audit_open_func || !audit_close_func || michael@0: (!audit_log_user_message_func && !audit_send_user_message_func)) { michael@0: dlclose(libaudit_handle); michael@0: libaudit_handle = NULL; michael@0: audit_open_func = NULL; michael@0: audit_close_func = NULL; michael@0: audit_log_user_message_func = NULL; michael@0: audit_send_user_message_func = NULL; michael@0: } michael@0: } michael@0: #endif /* LINUX */ michael@0: michael@0: michael@0: /* michael@0: * ******************** Password Utilities ******************************* michael@0: */ michael@0: static PRBool isLoggedIn = PR_FALSE; michael@0: PRBool sftk_fatalError = PR_FALSE; michael@0: michael@0: /* michael@0: * This function returns michael@0: * - CKR_PIN_INVALID if the password/PIN is not a legal UTF8 string michael@0: * - CKR_PIN_LEN_RANGE if the password/PIN is too short or does not michael@0: * consist of characters from three or more character classes. michael@0: * - CKR_OK otherwise michael@0: * michael@0: * The minimum password/PIN length is FIPS_MIN_PIN Unicode characters. michael@0: * We define five character classes: digits (0-9), ASCII lowercase letters, michael@0: * ASCII uppercase letters, ASCII non-alphanumeric characters (such as michael@0: * space and punctuation marks), and non-ASCII characters. If an ASCII michael@0: * uppercase letter is the first character of the password/PIN, the michael@0: * uppercase letter is not counted toward its character class. Similarly, michael@0: * if a digit is the last character of the password/PIN, the digit is not michael@0: * counted toward its character class. michael@0: * michael@0: * Although NSC_SetPIN and NSC_InitPIN already do the maximum and minimum michael@0: * password/PIN length checks, they check the length in bytes as opposed michael@0: * to characters. To meet the minimum password/PIN guessing probability michael@0: * requirements in FIPS 140-2, we need to check the length in characters. michael@0: */ michael@0: static CK_RV sftk_newPinCheck(CK_CHAR_PTR pPin, CK_ULONG ulPinLen) { michael@0: unsigned int i; michael@0: int nchar = 0; /* number of characters */ michael@0: int ntrail = 0; /* number of trailing bytes to follow */ michael@0: int ndigit = 0; /* number of decimal digits */ michael@0: int nlower = 0; /* number of ASCII lowercase letters */ michael@0: int nupper = 0; /* number of ASCII uppercase letters */ michael@0: int nnonalnum = 0; /* number of ASCII non-alphanumeric characters */ michael@0: int nnonascii = 0; /* number of non-ASCII characters */ michael@0: int nclass; /* number of character classes */ michael@0: michael@0: for (i = 0; i < ulPinLen; i++) { michael@0: unsigned int byte = pPin[i]; michael@0: michael@0: if (ntrail) { michael@0: if ((byte & 0xc0) != 0x80) { michael@0: /* illegal */ michael@0: nchar = -1; michael@0: break; michael@0: } michael@0: if (--ntrail == 0) { michael@0: nchar++; michael@0: nnonascii++; michael@0: } michael@0: continue; michael@0: } michael@0: if ((byte & 0x80) == 0x00) { michael@0: /* single-byte (ASCII) character */ michael@0: nchar++; michael@0: if (isdigit(byte)) { michael@0: if (i < ulPinLen - 1) { michael@0: ndigit++; michael@0: } michael@0: } else if (islower(byte)) { michael@0: nlower++; michael@0: } else if (isupper(byte)) { michael@0: if (i > 0) { michael@0: nupper++; michael@0: } michael@0: } else { michael@0: nnonalnum++; michael@0: } michael@0: } else if ((byte & 0xe0) == 0xc0) { michael@0: /* leading byte of two-byte character */ michael@0: ntrail = 1; michael@0: } else if ((byte & 0xf0) == 0xe0) { michael@0: /* leading byte of three-byte character */ michael@0: ntrail = 2; michael@0: } else if ((byte & 0xf8) == 0xf0) { michael@0: /* leading byte of four-byte character */ michael@0: ntrail = 3; michael@0: } else { michael@0: /* illegal */ michael@0: nchar = -1; michael@0: break; michael@0: } michael@0: } michael@0: if (nchar == -1) { michael@0: /* illegal UTF8 string */ michael@0: return CKR_PIN_INVALID; michael@0: } michael@0: if (nchar < FIPS_MIN_PIN) { michael@0: return CKR_PIN_LEN_RANGE; michael@0: } michael@0: nclass = (ndigit != 0) + (nlower != 0) + (nupper != 0) + michael@0: (nnonalnum != 0) + (nnonascii != 0); michael@0: if (nclass < 3) { michael@0: return CKR_PIN_LEN_RANGE; michael@0: } michael@0: return CKR_OK; michael@0: } michael@0: michael@0: michael@0: /* FIPS required checks before any useful cryptographic services */ michael@0: static CK_RV sftk_fipsCheck(void) { michael@0: if (sftk_fatalError) michael@0: return CKR_DEVICE_ERROR; michael@0: if (!isLoggedIn) michael@0: return CKR_USER_NOT_LOGGED_IN; michael@0: return CKR_OK; michael@0: } michael@0: michael@0: michael@0: #define SFTK_FIPSCHECK() \ michael@0: CK_RV rv; \ michael@0: if ((rv = sftk_fipsCheck()) != CKR_OK) return rv; michael@0: michael@0: #define SFTK_FIPSFATALCHECK() \ michael@0: if (sftk_fatalError) return CKR_DEVICE_ERROR; michael@0: michael@0: michael@0: /* grab an attribute out of a raw template */ michael@0: void * michael@0: fc_getAttribute(CK_ATTRIBUTE_PTR pTemplate, michael@0: CK_ULONG ulCount, CK_ATTRIBUTE_TYPE type) michael@0: { michael@0: int i; michael@0: michael@0: for (i=0; i < (int) ulCount; i++) { michael@0: if (pTemplate[i].type == type) { michael@0: return pTemplate[i].pValue; michael@0: } michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: michael@0: #define __PASTE(x,y) x##y michael@0: michael@0: /* ------------- forward declare all the NSC_ functions ------------- */ michael@0: #undef CK_NEED_ARG_LIST michael@0: #undef CK_PKCS11_FUNCTION_INFO michael@0: michael@0: #define CK_PKCS11_FUNCTION_INFO(name) CK_RV __PASTE(NS,name) michael@0: #define CK_NEED_ARG_LIST 1 michael@0: michael@0: #include "pkcs11f.h" michael@0: michael@0: /* ------------- forward declare all the FIPS functions ------------- */ michael@0: #undef CK_NEED_ARG_LIST michael@0: #undef CK_PKCS11_FUNCTION_INFO michael@0: michael@0: #define CK_PKCS11_FUNCTION_INFO(name) CK_RV __PASTE(F,name) michael@0: #define CK_NEED_ARG_LIST 1 michael@0: michael@0: #include "pkcs11f.h" michael@0: michael@0: /* ------------- build the CK_CRYPTO_TABLE ------------------------- */ michael@0: static CK_FUNCTION_LIST sftk_fipsTable = { michael@0: { 1, 10 }, michael@0: michael@0: #undef CK_NEED_ARG_LIST michael@0: #undef CK_PKCS11_FUNCTION_INFO michael@0: michael@0: #define CK_PKCS11_FUNCTION_INFO(name) __PASTE(F,name), michael@0: michael@0: michael@0: #include "pkcs11f.h" michael@0: michael@0: }; michael@0: michael@0: #undef CK_NEED_ARG_LIST michael@0: #undef CK_PKCS11_FUNCTION_INFO michael@0: michael@0: michael@0: #undef __PASTE michael@0: michael@0: /* CKO_NOT_A_KEY can be any object class that's not a key object. */ michael@0: #define CKO_NOT_A_KEY CKO_DATA michael@0: michael@0: #define SFTK_IS_KEY_OBJECT(objClass) \ michael@0: (((objClass) == CKO_PUBLIC_KEY) || \ michael@0: ((objClass) == CKO_PRIVATE_KEY) || \ michael@0: ((objClass) == CKO_SECRET_KEY)) michael@0: michael@0: #define SFTK_IS_NONPUBLIC_KEY_OBJECT(objClass) \ michael@0: (((objClass) == CKO_PRIVATE_KEY) || ((objClass) == CKO_SECRET_KEY)) michael@0: michael@0: static CK_RV michael@0: sftk_get_object_class_and_fipsCheck(CK_SESSION_HANDLE hSession, michael@0: CK_OBJECT_HANDLE hObject, CK_OBJECT_CLASS *pObjClass) michael@0: { michael@0: CK_RV rv; michael@0: CK_ATTRIBUTE class; michael@0: class.type = CKA_CLASS; michael@0: class.pValue = pObjClass; michael@0: class.ulValueLen = sizeof(*pObjClass); michael@0: rv = NSC_GetAttributeValue(hSession, hObject, &class, 1); michael@0: if ((rv == CKR_OK) && SFTK_IS_NONPUBLIC_KEY_OBJECT(*pObjClass)) { michael@0: rv = sftk_fipsCheck(); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: #ifdef LINUX michael@0: michael@0: int michael@0: sftk_mapLinuxAuditType(NSSAuditSeverity severity, NSSAuditType auditType) michael@0: { michael@0: switch (auditType) { michael@0: case NSS_AUDIT_ACCESS_KEY: michael@0: case NSS_AUDIT_CHANGE_KEY: michael@0: case NSS_AUDIT_COPY_KEY: michael@0: case NSS_AUDIT_DERIVE_KEY: michael@0: case NSS_AUDIT_DESTROY_KEY: michael@0: case NSS_AUDIT_DIGEST_KEY: michael@0: case NSS_AUDIT_GENERATE_KEY: michael@0: case NSS_AUDIT_LOAD_KEY: michael@0: case NSS_AUDIT_UNWRAP_KEY: michael@0: case NSS_AUDIT_WRAP_KEY: michael@0: return AUDIT_CRYPTO_KEY_USER; michael@0: case NSS_AUDIT_CRYPT: michael@0: return (severity == NSS_AUDIT_ERROR) ? AUDIT_CRYPTO_FAILURE_USER : michael@0: AUDIT_CRYPTO_KEY_USER; michael@0: case NSS_AUDIT_FIPS_STATE: michael@0: case NSS_AUDIT_INIT_PIN: michael@0: case NSS_AUDIT_INIT_TOKEN: michael@0: case NSS_AUDIT_SET_PIN: michael@0: return AUDIT_CRYPTO_PARAM_CHANGE_USER; michael@0: case NSS_AUDIT_SELF_TEST: michael@0: return AUDIT_CRYPTO_TEST_USER; michael@0: case NSS_AUDIT_LOGIN: michael@0: return AUDIT_CRYPTO_LOGIN; michael@0: case NSS_AUDIT_LOGOUT: michael@0: return AUDIT_CRYPTO_LOGOUT; michael@0: /* we skip the fault case here so we can get compiler michael@0: * warnings if new 'NSSAuditType's are added without michael@0: * added them to this list, defaults fall through */ michael@0: } michael@0: /* default */ michael@0: return AUDIT_CRYPTO_PARAM_CHANGE_USER; michael@0: } michael@0: #endif michael@0: michael@0: michael@0: /********************************************************************** michael@0: * michael@0: * FIPS 140 auditable event logging michael@0: * michael@0: **********************************************************************/ michael@0: michael@0: PRBool sftk_audit_enabled = PR_FALSE; michael@0: michael@0: /* michael@0: * Each audit record must have the following information: michael@0: * - Date and time of the event michael@0: * - Type of event michael@0: * - user (subject) identity michael@0: * - outcome (success or failure) of the event michael@0: * - process ID michael@0: * - name (ID) of the object michael@0: * - for changes to data (except for authentication data and CSPs), the new michael@0: * and old values of the data michael@0: * - for authentication attempts, the origin of the attempt (e.g., terminal michael@0: * identifier) michael@0: * - for assuming a role, the type of role, and the location of the request michael@0: */ michael@0: void michael@0: sftk_LogAuditMessage(NSSAuditSeverity severity, NSSAuditType auditType, michael@0: const char *msg) michael@0: { michael@0: #ifdef NSS_AUDIT_WITH_SYSLOG michael@0: int level; michael@0: michael@0: switch (severity) { michael@0: case NSS_AUDIT_ERROR: michael@0: level = LOG_ERR; michael@0: break; michael@0: case NSS_AUDIT_WARNING: michael@0: level = LOG_WARNING; michael@0: break; michael@0: default: michael@0: level = LOG_INFO; michael@0: break; michael@0: } michael@0: /* timestamp is provided by syslog in the message header */ michael@0: syslog(level | LOG_USER /* facility */, michael@0: "NSS " SOFTOKEN_LIB_NAME "[pid=%d uid=%d]: %s", michael@0: (int)getpid(), (int)getuid(), msg); michael@0: #ifdef LINUX michael@0: if (pthread_once(&libaudit_once_control, libaudit_init) != 0) { michael@0: return; michael@0: } michael@0: if (libaudit_handle) { michael@0: int audit_fd; michael@0: int linuxAuditType; michael@0: int result = (severity != NSS_AUDIT_ERROR); /* 1=success; 0=failed */ michael@0: char *message = PR_smprintf("NSS " SOFTOKEN_LIB_NAME ": %s", msg); michael@0: if (!message) { michael@0: return; michael@0: } michael@0: audit_fd = audit_open_func(); michael@0: if (audit_fd < 0) { michael@0: PR_smprintf_free(message); michael@0: return; michael@0: } michael@0: linuxAuditType = sftk_mapLinuxAuditType(severity, auditType); michael@0: if (audit_log_user_message_func) { michael@0: audit_log_user_message_func(audit_fd, linuxAuditType, message, michael@0: NULL, NULL, NULL, result); michael@0: } else { michael@0: audit_send_user_message_func(audit_fd, linuxAuditType, message); michael@0: } michael@0: audit_close_func(audit_fd); michael@0: PR_smprintf_free(message); michael@0: } michael@0: #endif /* LINUX */ michael@0: #else michael@0: /* do nothing */ michael@0: #endif michael@0: } michael@0: michael@0: michael@0: /********************************************************************** michael@0: * michael@0: * Start of PKCS 11 functions michael@0: * michael@0: **********************************************************************/ michael@0: /* return the function list */ michael@0: CK_RV FC_GetFunctionList(CK_FUNCTION_LIST_PTR *pFunctionList) { michael@0: michael@0: CHECK_FORK(); michael@0: michael@0: *pFunctionList = &sftk_fipsTable; michael@0: return CKR_OK; michael@0: } michael@0: michael@0: /* sigh global so pkcs11 can read it */ michael@0: PRBool nsf_init = PR_FALSE; michael@0: michael@0: /* FC_Initialize initializes the PKCS #11 library. */ michael@0: CK_RV FC_Initialize(CK_VOID_PTR pReserved) { michael@0: const char *envp; michael@0: CK_RV crv; michael@0: michael@0: sftk_ForkReset(pReserved, &crv); michael@0: michael@0: if (nsf_init) { michael@0: return CKR_CRYPTOKI_ALREADY_INITIALIZED; michael@0: } michael@0: michael@0: if ((envp = PR_GetEnv("NSS_ENABLE_AUDIT")) != NULL) { michael@0: sftk_audit_enabled = (atoi(envp) == 1); michael@0: } michael@0: michael@0: crv = nsc_CommonInitialize(pReserved, PR_TRUE); michael@0: michael@0: /* not an 'else' rv can be set by either SFTK_LowInit or SFTK_SlotInit*/ michael@0: if (crv != CKR_OK) { michael@0: sftk_fatalError = PR_TRUE; michael@0: return crv; michael@0: } michael@0: michael@0: sftk_fatalError = PR_FALSE; /* any error has been reset */ michael@0: michael@0: crv = sftk_fipsPowerUpSelfTest(); michael@0: if (crv != CKR_OK) { michael@0: nsc_CommonFinalize(NULL, PR_TRUE); michael@0: sftk_fatalError = PR_TRUE; michael@0: if (sftk_audit_enabled) { michael@0: char msg[128]; michael@0: PR_snprintf(msg,sizeof msg, michael@0: "C_Initialize()=0x%08lX " michael@0: "power-up self-tests failed", michael@0: (PRUint32)crv); michael@0: sftk_LogAuditMessage(NSS_AUDIT_ERROR, NSS_AUDIT_SELF_TEST, msg); michael@0: } michael@0: return crv; michael@0: } michael@0: nsf_init = PR_TRUE; michael@0: michael@0: return CKR_OK; michael@0: } michael@0: michael@0: /*FC_Finalize indicates that an application is done with the PKCS #11 library.*/ michael@0: CK_RV FC_Finalize (CK_VOID_PTR pReserved) { michael@0: CK_RV crv; michael@0: michael@0: if (sftk_ForkReset(pReserved, &crv)) { michael@0: return crv; michael@0: } michael@0: michael@0: if (!nsf_init) { michael@0: return CKR_OK; michael@0: } michael@0: michael@0: crv = nsc_CommonFinalize (pReserved, PR_TRUE); michael@0: michael@0: nsf_init = (PRBool) !(crv == CKR_OK); michael@0: return crv; michael@0: } michael@0: michael@0: michael@0: /* FC_GetInfo returns general information about PKCS #11. */ michael@0: CK_RV FC_GetInfo(CK_INFO_PTR pInfo) { michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_GetInfo(pInfo); michael@0: } michael@0: michael@0: /* FC_GetSlotList obtains a list of slots in the system. */ michael@0: CK_RV FC_GetSlotList(CK_BBOOL tokenPresent, michael@0: CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount) { michael@0: CHECK_FORK(); michael@0: michael@0: return nsc_CommonGetSlotList(tokenPresent,pSlotList,pulCount, michael@0: NSC_FIPS_MODULE); michael@0: } michael@0: michael@0: /* FC_GetSlotInfo obtains information about a particular slot in the system. */ michael@0: CK_RV FC_GetSlotInfo(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo) { michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_GetSlotInfo(slotID,pInfo); michael@0: } michael@0: michael@0: michael@0: /*FC_GetTokenInfo obtains information about a particular token in the system.*/ michael@0: CK_RV FC_GetTokenInfo(CK_SLOT_ID slotID,CK_TOKEN_INFO_PTR pInfo) { michael@0: CK_RV crv; michael@0: michael@0: CHECK_FORK(); michael@0: michael@0: crv = NSC_GetTokenInfo(slotID,pInfo); michael@0: if (crv == CKR_OK) michael@0: pInfo->flags |= CKF_LOGIN_REQUIRED; michael@0: return crv; michael@0: michael@0: } michael@0: michael@0: michael@0: michael@0: /*FC_GetMechanismList obtains a list of mechanism types supported by a token.*/ michael@0: CK_RV FC_GetMechanismList(CK_SLOT_ID slotID, michael@0: CK_MECHANISM_TYPE_PTR pMechanismList, CK_ULONG_PTR pusCount) { michael@0: CHECK_FORK(); michael@0: michael@0: SFTK_FIPSFATALCHECK(); michael@0: if (slotID == FIPS_SLOT_ID) slotID = NETSCAPE_SLOT_ID; michael@0: /* FIPS Slot supports all functions */ michael@0: return NSC_GetMechanismList(slotID,pMechanismList,pusCount); michael@0: } michael@0: michael@0: michael@0: /* FC_GetMechanismInfo obtains information about a particular mechanism michael@0: * possibly supported by a token. */ michael@0: CK_RV FC_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type, michael@0: CK_MECHANISM_INFO_PTR pInfo) { michael@0: CHECK_FORK(); michael@0: michael@0: SFTK_FIPSFATALCHECK(); michael@0: if (slotID == FIPS_SLOT_ID) slotID = NETSCAPE_SLOT_ID; michael@0: /* FIPS Slot supports all functions */ michael@0: return NSC_GetMechanismInfo(slotID,type,pInfo); michael@0: } michael@0: michael@0: michael@0: /* FC_InitToken initializes a token. */ michael@0: CK_RV FC_InitToken(CK_SLOT_ID slotID,CK_CHAR_PTR pPin, michael@0: CK_ULONG usPinLen,CK_CHAR_PTR pLabel) { michael@0: CK_RV crv; michael@0: michael@0: CHECK_FORK(); michael@0: michael@0: crv = NSC_InitToken(slotID,pPin,usPinLen,pLabel); michael@0: if (sftk_audit_enabled) { michael@0: char msg[128]; michael@0: NSSAuditSeverity severity = (crv == CKR_OK) ? michael@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; michael@0: /* pLabel points to a 32-byte label, which is not null-terminated */ michael@0: PR_snprintf(msg,sizeof msg, michael@0: "C_InitToken(slotID=%lu, pLabel=\"%.32s\")=0x%08lX", michael@0: (PRUint32)slotID,pLabel,(PRUint32)crv); michael@0: sftk_LogAuditMessage(severity, NSS_AUDIT_INIT_TOKEN, msg); michael@0: } michael@0: return crv; michael@0: } michael@0: michael@0: michael@0: /* FC_InitPIN initializes the normal user's PIN. */ michael@0: CK_RV FC_InitPIN(CK_SESSION_HANDLE hSession, michael@0: CK_CHAR_PTR pPin, CK_ULONG ulPinLen) { michael@0: CK_RV rv; michael@0: michael@0: CHECK_FORK(); michael@0: michael@0: if (sftk_fatalError) return CKR_DEVICE_ERROR; michael@0: if ((rv = sftk_newPinCheck(pPin,ulPinLen)) == CKR_OK) { michael@0: rv = NSC_InitPIN(hSession,pPin,ulPinLen); michael@0: } michael@0: if (sftk_audit_enabled) { michael@0: char msg[128]; michael@0: NSSAuditSeverity severity = (rv == CKR_OK) ? michael@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; michael@0: PR_snprintf(msg,sizeof msg, michael@0: "C_InitPIN(hSession=0x%08lX)=0x%08lX", michael@0: (PRUint32)hSession,(PRUint32)rv); michael@0: sftk_LogAuditMessage(severity, NSS_AUDIT_INIT_PIN, msg); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* FC_SetPIN modifies the PIN of user that is currently logged in. */ michael@0: /* NOTE: This is only valid for the PRIVATE_KEY_SLOT */ michael@0: CK_RV FC_SetPIN(CK_SESSION_HANDLE hSession, CK_CHAR_PTR pOldPin, michael@0: CK_ULONG usOldLen, CK_CHAR_PTR pNewPin, CK_ULONG usNewLen) { michael@0: CK_RV rv; michael@0: michael@0: CHECK_FORK(); michael@0: michael@0: if ((rv = sftk_fipsCheck()) == CKR_OK && michael@0: (rv = sftk_newPinCheck(pNewPin,usNewLen)) == CKR_OK) { michael@0: rv = NSC_SetPIN(hSession,pOldPin,usOldLen,pNewPin,usNewLen); michael@0: } michael@0: if (sftk_audit_enabled) { michael@0: char msg[128]; michael@0: NSSAuditSeverity severity = (rv == CKR_OK) ? michael@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; michael@0: PR_snprintf(msg,sizeof msg, michael@0: "C_SetPIN(hSession=0x%08lX)=0x%08lX", michael@0: (PRUint32)hSession,(PRUint32)rv); michael@0: sftk_LogAuditMessage(severity, NSS_AUDIT_SET_PIN, msg); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: /* FC_OpenSession opens a session between an application and a token. */ michael@0: CK_RV FC_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags, michael@0: CK_VOID_PTR pApplication,CK_NOTIFY Notify,CK_SESSION_HANDLE_PTR phSession) { michael@0: SFTK_FIPSFATALCHECK(); michael@0: michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_OpenSession(slotID,flags,pApplication,Notify,phSession); michael@0: } michael@0: michael@0: michael@0: /* FC_CloseSession closes a session between an application and a token. */ michael@0: CK_RV FC_CloseSession(CK_SESSION_HANDLE hSession) { michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_CloseSession(hSession); michael@0: } michael@0: michael@0: michael@0: /* FC_CloseAllSessions closes all sessions with a token. */ michael@0: CK_RV FC_CloseAllSessions (CK_SLOT_ID slotID) { michael@0: michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_CloseAllSessions (slotID); michael@0: } michael@0: michael@0: michael@0: /* FC_GetSessionInfo obtains information about the session. */ michael@0: CK_RV FC_GetSessionInfo(CK_SESSION_HANDLE hSession, michael@0: CK_SESSION_INFO_PTR pInfo) { michael@0: CK_RV rv; michael@0: SFTK_FIPSFATALCHECK(); michael@0: michael@0: CHECK_FORK(); michael@0: michael@0: rv = NSC_GetSessionInfo(hSession,pInfo); michael@0: if (rv == CKR_OK) { michael@0: if ((isLoggedIn) && (pInfo->state == CKS_RO_PUBLIC_SESSION)) { michael@0: pInfo->state = CKS_RO_USER_FUNCTIONS; michael@0: } michael@0: if ((isLoggedIn) && (pInfo->state == CKS_RW_PUBLIC_SESSION)) { michael@0: pInfo->state = CKS_RW_USER_FUNCTIONS; michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: /* FC_Login logs a user into a token. */ michael@0: CK_RV FC_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType, michael@0: CK_CHAR_PTR pPin, CK_ULONG usPinLen) { michael@0: CK_RV rv; michael@0: PRBool successful; michael@0: if (sftk_fatalError) return CKR_DEVICE_ERROR; michael@0: rv = NSC_Login(hSession,userType,pPin,usPinLen); michael@0: successful = (rv == CKR_OK) || (rv == CKR_USER_ALREADY_LOGGED_IN); michael@0: if (successful) michael@0: isLoggedIn = PR_TRUE; michael@0: if (sftk_audit_enabled) { michael@0: char msg[128]; michael@0: NSSAuditSeverity severity; michael@0: severity = successful ? NSS_AUDIT_INFO : NSS_AUDIT_ERROR; michael@0: PR_snprintf(msg,sizeof msg, michael@0: "C_Login(hSession=0x%08lX, userType=%lu)=0x%08lX", michael@0: (PRUint32)hSession,(PRUint32)userType,(PRUint32)rv); michael@0: sftk_LogAuditMessage(severity, NSS_AUDIT_LOGIN, msg); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: /* FC_Logout logs a user out from a token. */ michael@0: CK_RV FC_Logout(CK_SESSION_HANDLE hSession) { michael@0: CK_RV rv; michael@0: michael@0: CHECK_FORK(); michael@0: michael@0: if ((rv = sftk_fipsCheck()) == CKR_OK) { michael@0: rv = NSC_Logout(hSession); michael@0: isLoggedIn = PR_FALSE; michael@0: } michael@0: if (sftk_audit_enabled) { michael@0: char msg[128]; michael@0: NSSAuditSeverity severity = (rv == CKR_OK) ? michael@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; michael@0: PR_snprintf(msg,sizeof msg, michael@0: "C_Logout(hSession=0x%08lX)=0x%08lX", michael@0: (PRUint32)hSession,(PRUint32)rv); michael@0: sftk_LogAuditMessage(severity, NSS_AUDIT_LOGOUT, msg); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* FC_CreateObject creates a new object. */ michael@0: CK_RV FC_CreateObject(CK_SESSION_HANDLE hSession, michael@0: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, michael@0: CK_OBJECT_HANDLE_PTR phObject) { michael@0: CK_OBJECT_CLASS * classptr; michael@0: michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: classptr = (CK_OBJECT_CLASS *)fc_getAttribute(pTemplate,ulCount,CKA_CLASS); michael@0: if (classptr == NULL) return CKR_TEMPLATE_INCOMPLETE; michael@0: michael@0: /* FIPS can't create keys from raw key material */ michael@0: if (SFTK_IS_NONPUBLIC_KEY_OBJECT(*classptr)) { michael@0: rv = CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } else { michael@0: rv = NSC_CreateObject(hSession,pTemplate,ulCount,phObject); michael@0: } michael@0: if (sftk_audit_enabled && SFTK_IS_KEY_OBJECT(*classptr)) { michael@0: sftk_AuditCreateObject(hSession,pTemplate,ulCount,phObject,rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: michael@0: /* FC_CopyObject copies an object, creating a new object for the copy. */ michael@0: CK_RV FC_CopyObject(CK_SESSION_HANDLE hSession, michael@0: CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, michael@0: CK_OBJECT_HANDLE_PTR phNewObject) { michael@0: CK_RV rv; michael@0: CK_OBJECT_CLASS objClass = CKO_NOT_A_KEY; michael@0: michael@0: CHECK_FORK(); michael@0: michael@0: SFTK_FIPSFATALCHECK(); michael@0: rv = sftk_get_object_class_and_fipsCheck(hSession, hObject, &objClass); michael@0: if (rv == CKR_OK) { michael@0: rv = NSC_CopyObject(hSession,hObject,pTemplate,ulCount,phNewObject); michael@0: } michael@0: if (sftk_audit_enabled && SFTK_IS_KEY_OBJECT(objClass)) { michael@0: sftk_AuditCopyObject(hSession, michael@0: hObject,pTemplate,ulCount,phNewObject,rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* FC_DestroyObject destroys an object. */ michael@0: CK_RV FC_DestroyObject(CK_SESSION_HANDLE hSession, michael@0: CK_OBJECT_HANDLE hObject) { michael@0: CK_RV rv; michael@0: CK_OBJECT_CLASS objClass = CKO_NOT_A_KEY; michael@0: michael@0: CHECK_FORK(); michael@0: michael@0: SFTK_FIPSFATALCHECK(); michael@0: rv = sftk_get_object_class_and_fipsCheck(hSession, hObject, &objClass); michael@0: if (rv == CKR_OK) { michael@0: rv = NSC_DestroyObject(hSession,hObject); michael@0: } michael@0: if (sftk_audit_enabled && SFTK_IS_KEY_OBJECT(objClass)) { michael@0: sftk_AuditDestroyObject(hSession,hObject,rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* FC_GetObjectSize gets the size of an object in bytes. */ michael@0: CK_RV FC_GetObjectSize(CK_SESSION_HANDLE hSession, michael@0: CK_OBJECT_HANDLE hObject, CK_ULONG_PTR pulSize) { michael@0: CK_RV rv; michael@0: CK_OBJECT_CLASS objClass = CKO_NOT_A_KEY; michael@0: michael@0: CHECK_FORK(); michael@0: michael@0: SFTK_FIPSFATALCHECK(); michael@0: rv = sftk_get_object_class_and_fipsCheck(hSession, hObject, &objClass); michael@0: if (rv == CKR_OK) { michael@0: rv = NSC_GetObjectSize(hSession, hObject, pulSize); michael@0: } michael@0: if (sftk_audit_enabled && SFTK_IS_KEY_OBJECT(objClass)) { michael@0: sftk_AuditGetObjectSize(hSession, hObject, pulSize, rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* FC_GetAttributeValue obtains the value of one or more object attributes. */ michael@0: CK_RV FC_GetAttributeValue(CK_SESSION_HANDLE hSession, michael@0: CK_OBJECT_HANDLE hObject,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulCount) { michael@0: CK_RV rv; michael@0: CK_OBJECT_CLASS objClass = CKO_NOT_A_KEY; michael@0: michael@0: CHECK_FORK(); michael@0: michael@0: SFTK_FIPSFATALCHECK(); michael@0: rv = sftk_get_object_class_and_fipsCheck(hSession, hObject, &objClass); michael@0: if (rv == CKR_OK) { michael@0: rv = NSC_GetAttributeValue(hSession,hObject,pTemplate,ulCount); michael@0: } michael@0: if (sftk_audit_enabled && SFTK_IS_KEY_OBJECT(objClass)) { michael@0: sftk_AuditGetAttributeValue(hSession,hObject,pTemplate,ulCount,rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* FC_SetAttributeValue modifies the value of one or more object attributes */ michael@0: CK_RV FC_SetAttributeValue (CK_SESSION_HANDLE hSession, michael@0: CK_OBJECT_HANDLE hObject,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulCount) { michael@0: CK_RV rv; michael@0: CK_OBJECT_CLASS objClass = CKO_NOT_A_KEY; michael@0: michael@0: CHECK_FORK(); michael@0: michael@0: SFTK_FIPSFATALCHECK(); michael@0: rv = sftk_get_object_class_and_fipsCheck(hSession, hObject, &objClass); michael@0: if (rv == CKR_OK) { michael@0: rv = NSC_SetAttributeValue(hSession,hObject,pTemplate,ulCount); michael@0: } michael@0: if (sftk_audit_enabled && SFTK_IS_KEY_OBJECT(objClass)) { michael@0: sftk_AuditSetAttributeValue(hSession,hObject,pTemplate,ulCount,rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: michael@0: /* FC_FindObjectsInit initializes a search for token and session objects michael@0: * that match a template. */ michael@0: CK_RV FC_FindObjectsInit(CK_SESSION_HANDLE hSession, michael@0: CK_ATTRIBUTE_PTR pTemplate,CK_ULONG usCount) { michael@0: /* let publically readable object be found */ michael@0: unsigned int i; michael@0: CK_RV rv; michael@0: PRBool needLogin = PR_FALSE; michael@0: michael@0: michael@0: CHECK_FORK(); michael@0: michael@0: SFTK_FIPSFATALCHECK(); michael@0: michael@0: for (i=0; i < usCount; i++) { michael@0: CK_OBJECT_CLASS class; michael@0: if (pTemplate[i].type != CKA_CLASS) { michael@0: continue; michael@0: } michael@0: if (pTemplate[i].ulValueLen != sizeof(CK_OBJECT_CLASS)) { michael@0: continue; michael@0: } michael@0: if (pTemplate[i].pValue == NULL) { michael@0: continue; michael@0: } michael@0: class = *(CK_OBJECT_CLASS *)pTemplate[i].pValue; michael@0: if ((class == CKO_PRIVATE_KEY) || (class == CKO_SECRET_KEY)) { michael@0: needLogin = PR_TRUE; michael@0: break; michael@0: } michael@0: } michael@0: if (needLogin) { michael@0: if ((rv = sftk_fipsCheck()) != CKR_OK) return rv; michael@0: } michael@0: return NSC_FindObjectsInit(hSession,pTemplate,usCount); michael@0: } michael@0: michael@0: michael@0: /* FC_FindObjects continues a search for token and session objects michael@0: * that match a template, obtaining additional object handles. */ michael@0: CK_RV FC_FindObjects(CK_SESSION_HANDLE hSession, michael@0: CK_OBJECT_HANDLE_PTR phObject,CK_ULONG usMaxObjectCount, michael@0: CK_ULONG_PTR pusObjectCount) { michael@0: CHECK_FORK(); michael@0: michael@0: /* let publically readable object be found */ michael@0: SFTK_FIPSFATALCHECK(); michael@0: return NSC_FindObjects(hSession,phObject,usMaxObjectCount, michael@0: pusObjectCount); michael@0: } michael@0: michael@0: michael@0: /* michael@0: ************** Crypto Functions: Encrypt ************************ michael@0: */ michael@0: michael@0: /* FC_EncryptInit initializes an encryption operation. */ michael@0: CK_RV FC_EncryptInit(CK_SESSION_HANDLE hSession, michael@0: CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: rv = NSC_EncryptInit(hSession,pMechanism,hKey); michael@0: if (sftk_audit_enabled) { michael@0: sftk_AuditCryptInit("Encrypt",hSession,pMechanism,hKey,rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: /* FC_Encrypt encrypts single-part data. */ michael@0: CK_RV FC_Encrypt (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, michael@0: CK_ULONG usDataLen, CK_BYTE_PTR pEncryptedData, michael@0: CK_ULONG_PTR pusEncryptedDataLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_Encrypt(hSession,pData,usDataLen,pEncryptedData, michael@0: pusEncryptedDataLen); michael@0: } michael@0: michael@0: michael@0: /* FC_EncryptUpdate continues a multiple-part encryption operation. */ michael@0: CK_RV FC_EncryptUpdate(CK_SESSION_HANDLE hSession, michael@0: CK_BYTE_PTR pPart, CK_ULONG usPartLen, CK_BYTE_PTR pEncryptedPart, michael@0: CK_ULONG_PTR pusEncryptedPartLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_EncryptUpdate(hSession,pPart,usPartLen,pEncryptedPart, michael@0: pusEncryptedPartLen); michael@0: } michael@0: michael@0: michael@0: /* FC_EncryptFinal finishes a multiple-part encryption operation. */ michael@0: CK_RV FC_EncryptFinal(CK_SESSION_HANDLE hSession, michael@0: CK_BYTE_PTR pLastEncryptedPart, CK_ULONG_PTR pusLastEncryptedPartLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_EncryptFinal(hSession,pLastEncryptedPart, michael@0: pusLastEncryptedPartLen); michael@0: } michael@0: michael@0: /* michael@0: ************** Crypto Functions: Decrypt ************************ michael@0: */ michael@0: michael@0: michael@0: /* FC_DecryptInit initializes a decryption operation. */ michael@0: CK_RV FC_DecryptInit( CK_SESSION_HANDLE hSession, michael@0: CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: rv = NSC_DecryptInit(hSession,pMechanism,hKey); michael@0: if (sftk_audit_enabled) { michael@0: sftk_AuditCryptInit("Decrypt",hSession,pMechanism,hKey,rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: /* FC_Decrypt decrypts encrypted data in a single part. */ michael@0: CK_RV FC_Decrypt(CK_SESSION_HANDLE hSession, michael@0: CK_BYTE_PTR pEncryptedData,CK_ULONG usEncryptedDataLen,CK_BYTE_PTR pData, michael@0: CK_ULONG_PTR pusDataLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_Decrypt(hSession,pEncryptedData,usEncryptedDataLen,pData, michael@0: pusDataLen); michael@0: } michael@0: michael@0: michael@0: /* FC_DecryptUpdate continues a multiple-part decryption operation. */ michael@0: CK_RV FC_DecryptUpdate(CK_SESSION_HANDLE hSession, michael@0: CK_BYTE_PTR pEncryptedPart, CK_ULONG usEncryptedPartLen, michael@0: CK_BYTE_PTR pPart, CK_ULONG_PTR pusPartLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_DecryptUpdate(hSession,pEncryptedPart,usEncryptedPartLen, michael@0: pPart,pusPartLen); michael@0: } michael@0: michael@0: michael@0: /* FC_DecryptFinal finishes a multiple-part decryption operation. */ michael@0: CK_RV FC_DecryptFinal(CK_SESSION_HANDLE hSession, michael@0: CK_BYTE_PTR pLastPart, CK_ULONG_PTR pusLastPartLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_DecryptFinal(hSession,pLastPart,pusLastPartLen); michael@0: } michael@0: michael@0: michael@0: /* michael@0: ************** Crypto Functions: Digest (HASH) ************************ michael@0: */ michael@0: michael@0: /* FC_DigestInit initializes a message-digesting operation. */ michael@0: CK_RV FC_DigestInit(CK_SESSION_HANDLE hSession, michael@0: CK_MECHANISM_PTR pMechanism) { michael@0: SFTK_FIPSFATALCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_DigestInit(hSession, pMechanism); michael@0: } michael@0: michael@0: michael@0: /* FC_Digest digests data in a single part. */ michael@0: CK_RV FC_Digest(CK_SESSION_HANDLE hSession, michael@0: CK_BYTE_PTR pData, CK_ULONG usDataLen, CK_BYTE_PTR pDigest, michael@0: CK_ULONG_PTR pusDigestLen) { michael@0: SFTK_FIPSFATALCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_Digest(hSession,pData,usDataLen,pDigest,pusDigestLen); michael@0: } michael@0: michael@0: michael@0: /* FC_DigestUpdate continues a multiple-part message-digesting operation. */ michael@0: CK_RV FC_DigestUpdate(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pPart, michael@0: CK_ULONG usPartLen) { michael@0: SFTK_FIPSFATALCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_DigestUpdate(hSession,pPart,usPartLen); michael@0: } michael@0: michael@0: michael@0: /* FC_DigestFinal finishes a multiple-part message-digesting operation. */ michael@0: CK_RV FC_DigestFinal(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pDigest, michael@0: CK_ULONG_PTR pusDigestLen) { michael@0: SFTK_FIPSFATALCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_DigestFinal(hSession,pDigest,pusDigestLen); michael@0: } michael@0: michael@0: michael@0: /* michael@0: ************** Crypto Functions: Sign ************************ michael@0: */ michael@0: michael@0: /* FC_SignInit initializes a signature (private key encryption) operation, michael@0: * where the signature is (will be) an appendix to the data, michael@0: * and plaintext cannot be recovered from the signature */ michael@0: CK_RV FC_SignInit(CK_SESSION_HANDLE hSession, michael@0: CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: rv = NSC_SignInit(hSession,pMechanism,hKey); michael@0: if (sftk_audit_enabled) { michael@0: sftk_AuditCryptInit("Sign",hSession,pMechanism,hKey,rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* FC_Sign signs (encrypts with private key) data in a single part, michael@0: * where the signature is (will be) an appendix to the data, michael@0: * and plaintext cannot be recovered from the signature */ michael@0: CK_RV FC_Sign(CK_SESSION_HANDLE hSession, michael@0: CK_BYTE_PTR pData,CK_ULONG usDataLen,CK_BYTE_PTR pSignature, michael@0: CK_ULONG_PTR pusSignatureLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_Sign(hSession,pData,usDataLen,pSignature,pusSignatureLen); michael@0: } michael@0: michael@0: michael@0: /* FC_SignUpdate continues a multiple-part signature operation, michael@0: * where the signature is (will be) an appendix to the data, michael@0: * and plaintext cannot be recovered from the signature */ michael@0: CK_RV FC_SignUpdate(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pPart, michael@0: CK_ULONG usPartLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_SignUpdate(hSession,pPart,usPartLen); michael@0: } michael@0: michael@0: michael@0: /* FC_SignFinal finishes a multiple-part signature operation, michael@0: * returning the signature. */ michael@0: CK_RV FC_SignFinal(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pSignature, michael@0: CK_ULONG_PTR pusSignatureLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_SignFinal(hSession,pSignature,pusSignatureLen); michael@0: } michael@0: michael@0: /* michael@0: ************** Crypto Functions: Sign Recover ************************ michael@0: */ michael@0: /* FC_SignRecoverInit initializes a signature operation, michael@0: * where the (digest) data can be recovered from the signature. michael@0: * E.g. encryption with the user's private key */ michael@0: CK_RV FC_SignRecoverInit(CK_SESSION_HANDLE hSession, michael@0: CK_MECHANISM_PTR pMechanism,CK_OBJECT_HANDLE hKey) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: rv = NSC_SignRecoverInit(hSession,pMechanism,hKey); michael@0: if (sftk_audit_enabled) { michael@0: sftk_AuditCryptInit("SignRecover",hSession,pMechanism,hKey,rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* FC_SignRecover signs data in a single operation michael@0: * where the (digest) data can be recovered from the signature. michael@0: * E.g. encryption with the user's private key */ michael@0: CK_RV FC_SignRecover(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, michael@0: CK_ULONG usDataLen, CK_BYTE_PTR pSignature, CK_ULONG_PTR pusSignatureLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_SignRecover(hSession,pData,usDataLen,pSignature,pusSignatureLen); michael@0: } michael@0: michael@0: /* michael@0: ************** Crypto Functions: verify ************************ michael@0: */ michael@0: michael@0: /* FC_VerifyInit initializes a verification operation, michael@0: * where the signature is an appendix to the data, michael@0: * and plaintext cannot be recovered from the signature (e.g. DSA) */ michael@0: CK_RV FC_VerifyInit(CK_SESSION_HANDLE hSession, michael@0: CK_MECHANISM_PTR pMechanism,CK_OBJECT_HANDLE hKey) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: rv = NSC_VerifyInit(hSession,pMechanism,hKey); michael@0: if (sftk_audit_enabled) { michael@0: sftk_AuditCryptInit("Verify",hSession,pMechanism,hKey,rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* FC_Verify verifies a signature in a single-part operation, michael@0: * where the signature is an appendix to the data, michael@0: * and plaintext cannot be recovered from the signature */ michael@0: CK_RV FC_Verify(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, michael@0: CK_ULONG usDataLen, CK_BYTE_PTR pSignature, CK_ULONG usSignatureLen) { michael@0: /* make sure we're legal */ michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_Verify(hSession,pData,usDataLen,pSignature,usSignatureLen); michael@0: } michael@0: michael@0: michael@0: /* FC_VerifyUpdate continues a multiple-part verification operation, michael@0: * where the signature is an appendix to the data, michael@0: * and plaintext cannot be recovered from the signature */ michael@0: CK_RV FC_VerifyUpdate( CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, michael@0: CK_ULONG usPartLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_VerifyUpdate(hSession,pPart,usPartLen); michael@0: } michael@0: michael@0: michael@0: /* FC_VerifyFinal finishes a multiple-part verification operation, michael@0: * checking the signature. */ michael@0: CK_RV FC_VerifyFinal(CK_SESSION_HANDLE hSession, michael@0: CK_BYTE_PTR pSignature,CK_ULONG usSignatureLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_VerifyFinal(hSession,pSignature,usSignatureLen); michael@0: } michael@0: michael@0: /* michael@0: ************** Crypto Functions: Verify Recover ************************ michael@0: */ michael@0: michael@0: /* FC_VerifyRecoverInit initializes a signature verification operation, michael@0: * where the data is recovered from the signature. michael@0: * E.g. Decryption with the user's public key */ michael@0: CK_RV FC_VerifyRecoverInit(CK_SESSION_HANDLE hSession, michael@0: CK_MECHANISM_PTR pMechanism,CK_OBJECT_HANDLE hKey) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: rv = NSC_VerifyRecoverInit(hSession,pMechanism,hKey); michael@0: if (sftk_audit_enabled) { michael@0: sftk_AuditCryptInit("VerifyRecover",hSession,pMechanism,hKey,rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* FC_VerifyRecover verifies a signature in a single-part operation, michael@0: * where the data is recovered from the signature. michael@0: * E.g. Decryption with the user's public key */ michael@0: CK_RV FC_VerifyRecover(CK_SESSION_HANDLE hSession, michael@0: CK_BYTE_PTR pSignature,CK_ULONG usSignatureLen, michael@0: CK_BYTE_PTR pData,CK_ULONG_PTR pusDataLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_VerifyRecover(hSession,pSignature,usSignatureLen,pData, michael@0: pusDataLen); michael@0: } michael@0: michael@0: /* michael@0: **************************** Key Functions: ************************ michael@0: */ michael@0: michael@0: /* FC_GenerateKey generates a secret key, creating a new key object. */ michael@0: CK_RV FC_GenerateKey(CK_SESSION_HANDLE hSession, michael@0: CK_MECHANISM_PTR pMechanism,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulCount, michael@0: CK_OBJECT_HANDLE_PTR phKey) { michael@0: CK_BBOOL *boolptr; michael@0: michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: /* all secret keys must be sensitive, if the upper level code tries to say michael@0: * otherwise, reject it. */ michael@0: boolptr = (CK_BBOOL *) fc_getAttribute(pTemplate, ulCount, CKA_SENSITIVE); michael@0: if (boolptr != NULL) { michael@0: if (!(*boolptr)) { michael@0: return CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } michael@0: } michael@0: michael@0: rv = NSC_GenerateKey(hSession,pMechanism,pTemplate,ulCount,phKey); michael@0: if (sftk_audit_enabled) { michael@0: sftk_AuditGenerateKey(hSession,pMechanism,pTemplate,ulCount,phKey,rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* FC_GenerateKeyPair generates a public-key/private-key pair, michael@0: * creating new key objects. */ michael@0: CK_RV FC_GenerateKeyPair (CK_SESSION_HANDLE hSession, michael@0: CK_MECHANISM_PTR pMechanism, CK_ATTRIBUTE_PTR pPublicKeyTemplate, michael@0: CK_ULONG usPublicKeyAttributeCount, CK_ATTRIBUTE_PTR pPrivateKeyTemplate, michael@0: CK_ULONG usPrivateKeyAttributeCount, CK_OBJECT_HANDLE_PTR phPublicKey, michael@0: CK_OBJECT_HANDLE_PTR phPrivateKey) { michael@0: CK_BBOOL *boolptr; michael@0: CK_RV crv; michael@0: michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: michael@0: /* all private keys must be sensitive, if the upper level code tries to say michael@0: * otherwise, reject it. */ michael@0: boolptr = (CK_BBOOL *) fc_getAttribute(pPrivateKeyTemplate, michael@0: usPrivateKeyAttributeCount, CKA_SENSITIVE); michael@0: if (boolptr != NULL) { michael@0: if (!(*boolptr)) { michael@0: return CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } michael@0: } michael@0: crv = NSC_GenerateKeyPair (hSession,pMechanism,pPublicKeyTemplate, michael@0: usPublicKeyAttributeCount,pPrivateKeyTemplate, michael@0: usPrivateKeyAttributeCount,phPublicKey,phPrivateKey); michael@0: if (crv == CKR_GENERAL_ERROR) { michael@0: /* pairwise consistency check failed. */ michael@0: sftk_fatalError = PR_TRUE; michael@0: } michael@0: if (sftk_audit_enabled) { michael@0: sftk_AuditGenerateKeyPair(hSession,pMechanism,pPublicKeyTemplate, michael@0: usPublicKeyAttributeCount,pPrivateKeyTemplate, michael@0: usPrivateKeyAttributeCount,phPublicKey,phPrivateKey,crv); michael@0: } michael@0: return crv; michael@0: } michael@0: michael@0: michael@0: /* FC_WrapKey wraps (i.e., encrypts) a key. */ michael@0: CK_RV FC_WrapKey(CK_SESSION_HANDLE hSession, michael@0: CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hWrappingKey, michael@0: CK_OBJECT_HANDLE hKey, CK_BYTE_PTR pWrappedKey, michael@0: CK_ULONG_PTR pulWrappedKeyLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: rv = NSC_WrapKey(hSession,pMechanism,hWrappingKey,hKey,pWrappedKey, michael@0: pulWrappedKeyLen); michael@0: if (sftk_audit_enabled) { michael@0: sftk_AuditWrapKey(hSession,pMechanism,hWrappingKey,hKey,pWrappedKey, michael@0: pulWrappedKeyLen,rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* FC_UnwrapKey unwraps (decrypts) a wrapped key, creating a new key object. */ michael@0: CK_RV FC_UnwrapKey(CK_SESSION_HANDLE hSession, michael@0: CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hUnwrappingKey, michael@0: CK_BYTE_PTR pWrappedKey, CK_ULONG ulWrappedKeyLen, michael@0: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount, michael@0: CK_OBJECT_HANDLE_PTR phKey) { michael@0: CK_BBOOL *boolptr; michael@0: michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: /* all secret keys must be sensitive, if the upper level code tries to say michael@0: * otherwise, reject it. */ michael@0: boolptr = (CK_BBOOL *) fc_getAttribute(pTemplate, michael@0: ulAttributeCount, CKA_SENSITIVE); michael@0: if (boolptr != NULL) { michael@0: if (!(*boolptr)) { michael@0: return CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } michael@0: } michael@0: rv = NSC_UnwrapKey(hSession,pMechanism,hUnwrappingKey,pWrappedKey, michael@0: ulWrappedKeyLen,pTemplate,ulAttributeCount,phKey); michael@0: if (sftk_audit_enabled) { michael@0: sftk_AuditUnwrapKey(hSession,pMechanism,hUnwrappingKey,pWrappedKey, michael@0: ulWrappedKeyLen,pTemplate,ulAttributeCount,phKey,rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: /* FC_DeriveKey derives a key from a base key, creating a new key object. */ michael@0: CK_RV FC_DeriveKey( CK_SESSION_HANDLE hSession, michael@0: CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hBaseKey, michael@0: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount, michael@0: CK_OBJECT_HANDLE_PTR phKey) { michael@0: CK_BBOOL *boolptr; michael@0: michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: /* all secret keys must be sensitive, if the upper level code tries to say michael@0: * otherwise, reject it. */ michael@0: boolptr = (CK_BBOOL *) fc_getAttribute(pTemplate, michael@0: ulAttributeCount, CKA_SENSITIVE); michael@0: if (boolptr != NULL) { michael@0: if (!(*boolptr)) { michael@0: return CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } michael@0: } michael@0: rv = NSC_DeriveKey(hSession,pMechanism,hBaseKey,pTemplate, michael@0: ulAttributeCount, phKey); michael@0: if (sftk_audit_enabled) { michael@0: sftk_AuditDeriveKey(hSession,pMechanism,hBaseKey,pTemplate, michael@0: ulAttributeCount,phKey,rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: /* michael@0: **************************** Radom Functions: ************************ michael@0: */ michael@0: michael@0: /* FC_SeedRandom mixes additional seed material into the token's random number michael@0: * generator. */ michael@0: CK_RV FC_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed, michael@0: CK_ULONG usSeedLen) { michael@0: CK_RV crv; michael@0: michael@0: SFTK_FIPSFATALCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: crv = NSC_SeedRandom(hSession,pSeed,usSeedLen); michael@0: if (crv != CKR_OK) { michael@0: sftk_fatalError = PR_TRUE; michael@0: } michael@0: return crv; michael@0: } michael@0: michael@0: michael@0: /* FC_GenerateRandom generates random data. */ michael@0: CK_RV FC_GenerateRandom(CK_SESSION_HANDLE hSession, michael@0: CK_BYTE_PTR pRandomData, CK_ULONG ulRandomLen) { michael@0: CK_RV crv; michael@0: michael@0: CHECK_FORK(); michael@0: michael@0: SFTK_FIPSFATALCHECK(); michael@0: crv = NSC_GenerateRandom(hSession,pRandomData,ulRandomLen); michael@0: if (crv != CKR_OK) { michael@0: sftk_fatalError = PR_TRUE; michael@0: if (sftk_audit_enabled) { michael@0: char msg[128]; michael@0: PR_snprintf(msg,sizeof msg, michael@0: "C_GenerateRandom(hSession=0x%08lX, pRandomData=%p, " michael@0: "ulRandomLen=%lu)=0x%08lX " michael@0: "self-test: continuous RNG test failed", michael@0: (PRUint32)hSession,pRandomData, michael@0: (PRUint32)ulRandomLen,(PRUint32)crv); michael@0: sftk_LogAuditMessage(NSS_AUDIT_ERROR, NSS_AUDIT_SELF_TEST, msg); michael@0: } michael@0: } michael@0: return crv; michael@0: } michael@0: michael@0: michael@0: /* FC_GetFunctionStatus obtains an updated status of a function running michael@0: * in parallel with an application. */ michael@0: CK_RV FC_GetFunctionStatus(CK_SESSION_HANDLE hSession) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_GetFunctionStatus(hSession); michael@0: } michael@0: michael@0: michael@0: /* FC_CancelFunction cancels a function running in parallel */ michael@0: CK_RV FC_CancelFunction(CK_SESSION_HANDLE hSession) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_CancelFunction(hSession); michael@0: } michael@0: michael@0: /* michael@0: **************************** Version 1.1 Functions: ************************ michael@0: */ michael@0: michael@0: /* FC_GetOperationState saves the state of the cryptographic michael@0: *operation in a session. */ michael@0: CK_RV FC_GetOperationState(CK_SESSION_HANDLE hSession, michael@0: CK_BYTE_PTR pOperationState, CK_ULONG_PTR pulOperationStateLen) { michael@0: SFTK_FIPSFATALCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_GetOperationState(hSession,pOperationState,pulOperationStateLen); michael@0: } michael@0: michael@0: michael@0: /* FC_SetOperationState restores the state of the cryptographic operation michael@0: * in a session. */ michael@0: CK_RV FC_SetOperationState(CK_SESSION_HANDLE hSession, michael@0: CK_BYTE_PTR pOperationState, CK_ULONG ulOperationStateLen, michael@0: CK_OBJECT_HANDLE hEncryptionKey, CK_OBJECT_HANDLE hAuthenticationKey) { michael@0: SFTK_FIPSFATALCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_SetOperationState(hSession,pOperationState,ulOperationStateLen, michael@0: hEncryptionKey,hAuthenticationKey); michael@0: } michael@0: michael@0: /* FC_FindObjectsFinal finishes a search for token and session objects. */ michael@0: CK_RV FC_FindObjectsFinal(CK_SESSION_HANDLE hSession) { michael@0: /* let publically readable object be found */ michael@0: SFTK_FIPSFATALCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_FindObjectsFinal(hSession); michael@0: } michael@0: michael@0: michael@0: /* Dual-function cryptographic operations */ michael@0: michael@0: /* FC_DigestEncryptUpdate continues a multiple-part digesting and encryption michael@0: * operation. */ michael@0: CK_RV FC_DigestEncryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, michael@0: CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart, michael@0: CK_ULONG_PTR pulEncryptedPartLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_DigestEncryptUpdate(hSession,pPart,ulPartLen,pEncryptedPart, michael@0: pulEncryptedPartLen); michael@0: } michael@0: michael@0: michael@0: /* FC_DecryptDigestUpdate continues a multiple-part decryption and digesting michael@0: * operation. */ michael@0: CK_RV FC_DecryptDigestUpdate(CK_SESSION_HANDLE hSession, michael@0: CK_BYTE_PTR pEncryptedPart, CK_ULONG ulEncryptedPartLen, michael@0: CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_DecryptDigestUpdate(hSession, pEncryptedPart,ulEncryptedPartLen, michael@0: pPart,pulPartLen); michael@0: } michael@0: michael@0: /* FC_SignEncryptUpdate continues a multiple-part signing and encryption michael@0: * operation. */ michael@0: CK_RV FC_SignEncryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, michael@0: CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart, michael@0: CK_ULONG_PTR pulEncryptedPartLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_SignEncryptUpdate(hSession,pPart,ulPartLen,pEncryptedPart, michael@0: pulEncryptedPartLen); michael@0: } michael@0: michael@0: /* FC_DecryptVerifyUpdate continues a multiple-part decryption and verify michael@0: * operation. */ michael@0: CK_RV FC_DecryptVerifyUpdate(CK_SESSION_HANDLE hSession, michael@0: CK_BYTE_PTR pEncryptedData, CK_ULONG ulEncryptedDataLen, michael@0: CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_DecryptVerifyUpdate(hSession,pEncryptedData,ulEncryptedDataLen, michael@0: pData,pulDataLen); michael@0: } michael@0: michael@0: michael@0: /* FC_DigestKey continues a multi-part message-digesting operation, michael@0: * by digesting the value of a secret key as part of the data already digested. michael@0: */ michael@0: CK_RV FC_DigestKey(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey) { michael@0: SFTK_FIPSCHECK(); michael@0: CHECK_FORK(); michael@0: michael@0: rv = NSC_DigestKey(hSession,hKey); michael@0: if (sftk_audit_enabled) { michael@0: sftk_AuditDigestKey(hSession,hKey,rv); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: michael@0: CK_RV FC_WaitForSlotEvent(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot, michael@0: CK_VOID_PTR pReserved) michael@0: { michael@0: CHECK_FORK(); michael@0: michael@0: return NSC_WaitForSlotEvent(flags, pSlot, pReserved); michael@0: }