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