andre@0: /* andre@0: * softoken.h - private data structures and prototypes for the softoken lib andre@0: * 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: #ifndef _SOFTOKEN_H_ andre@0: #define _SOFTOKEN_H_ andre@0: andre@0: #include "blapi.h" andre@0: #include "lowkeyti.h" andre@0: #include "softoknt.h" andre@0: #include "secoidt.h" andre@0: andre@0: #include "pkcs11t.h" andre@0: andre@0: SEC_BEGIN_PROTOS andre@0: andre@0: /* andre@0: * Convenience wrapper for doing a single PKCS#1 v1.5 RSA operations where the andre@0: * encoded digest info is computed internally, rather than by the caller. andre@0: * andre@0: * The HashSign variants expect as input the value of H, the computed hash andre@0: * from RFC 3447, Section 9.2, Step 1, and will compute the DER-encoded andre@0: * DigestInfo structure internally prior to signing/verifying. andre@0: */ andre@0: extern SECStatus andre@0: RSA_HashSign(SECOidTag hashOid, NSSLOWKEYPrivateKey *key, andre@0: unsigned char *sig, unsigned int *sigLen, unsigned int maxLen, andre@0: const unsigned char *hash, unsigned int hashLen); andre@0: andre@0: extern SECStatus andre@0: RSA_HashCheckSign(SECOidTag hashOid, NSSLOWKEYPublicKey *key, andre@0: const unsigned char *sig, unsigned int sigLen, andre@0: const unsigned char *hash, unsigned int hashLen); andre@0: andre@0: /* andre@0: ** Prepare a buffer for padded CBC encryption, growing to the appropriate andre@0: ** boundary, filling with the appropriate padding. andre@0: ** andre@0: ** blockSize must be a power of 2. andre@0: ** andre@0: ** We add from 1 to blockSize bytes -- we *always* grow. andre@0: ** The extra bytes contain the value of the length of the padding: andre@0: ** if we have 2 bytes of padding, then the padding is "0x02, 0x02". andre@0: ** andre@0: ** NOTE: If arena is non-NULL, we re-allocate from there, otherwise andre@0: ** we assume (and use) PR memory (re)allocation. andre@0: */ andre@0: extern unsigned char * CBC_PadBuffer(PLArenaPool *arena, unsigned char *inbuf, andre@0: unsigned int inlen, unsigned int *outlen, andre@0: int blockSize); andre@0: andre@0: andre@0: /****************************************/ andre@0: /* andre@0: ** Power-Up selftests required for FIPS and invoked only andre@0: ** under PKCS #11 FIPS mode. andre@0: */ andre@0: extern CK_RV sftk_fipsPowerUpSelfTest( void ); andre@0: andre@0: /* andre@0: ** make known fixed PKCS #11 key types to their sizes in bytes andre@0: */ andre@0: unsigned long sftk_MapKeySize(CK_KEY_TYPE keyType); andre@0: andre@0: /* andre@0: ** FIPS 140-2 auditing andre@0: */ andre@0: extern PRBool sftk_audit_enabled; andre@0: andre@0: extern void sftk_LogAuditMessage(NSSAuditSeverity severity, andre@0: NSSAuditType, const char *msg); andre@0: andre@0: extern void sftk_AuditCreateObject(CK_SESSION_HANDLE hSession, andre@0: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, andre@0: CK_OBJECT_HANDLE_PTR phObject, CK_RV rv); andre@0: andre@0: extern void sftk_AuditCopyObject(CK_SESSION_HANDLE hSession, andre@0: CK_OBJECT_HANDLE hObject, andre@0: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, andre@0: CK_OBJECT_HANDLE_PTR phNewObject, CK_RV rv); andre@0: andre@0: extern void sftk_AuditDestroyObject(CK_SESSION_HANDLE hSession, andre@0: CK_OBJECT_HANDLE hObject, CK_RV rv); andre@0: andre@0: extern void sftk_AuditGetObjectSize(CK_SESSION_HANDLE hSession, andre@0: CK_OBJECT_HANDLE hObject, CK_ULONG_PTR pulSize, andre@0: CK_RV rv); andre@0: andre@0: extern void sftk_AuditGetAttributeValue(CK_SESSION_HANDLE hSession, andre@0: CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, andre@0: CK_ULONG ulCount, CK_RV rv); andre@0: andre@0: extern void sftk_AuditSetAttributeValue(CK_SESSION_HANDLE hSession, andre@0: CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, andre@0: CK_ULONG ulCount, CK_RV rv); andre@0: andre@0: extern void sftk_AuditCryptInit(const char *opName, andre@0: CK_SESSION_HANDLE hSession, andre@0: CK_MECHANISM_PTR pMechanism, andre@0: CK_OBJECT_HANDLE hKey, CK_RV rv); andre@0: andre@0: extern void sftk_AuditGenerateKey(CK_SESSION_HANDLE hSession, andre@0: CK_MECHANISM_PTR pMechanism, andre@0: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, andre@0: CK_OBJECT_HANDLE_PTR phKey, CK_RV rv); andre@0: andre@0: extern void sftk_AuditGenerateKeyPair(CK_SESSION_HANDLE hSession, andre@0: CK_MECHANISM_PTR pMechanism, andre@0: CK_ATTRIBUTE_PTR pPublicKeyTemplate, andre@0: CK_ULONG ulPublicKeyAttributeCount, andre@0: CK_ATTRIBUTE_PTR pPrivateKeyTemplate, andre@0: CK_ULONG ulPrivateKeyAttributeCount, andre@0: CK_OBJECT_HANDLE_PTR phPublicKey, andre@0: CK_OBJECT_HANDLE_PTR phPrivateKey, CK_RV rv); andre@0: andre@0: extern void sftk_AuditWrapKey(CK_SESSION_HANDLE hSession, andre@0: CK_MECHANISM_PTR pMechanism, andre@0: CK_OBJECT_HANDLE hWrappingKey, CK_OBJECT_HANDLE hKey, andre@0: CK_BYTE_PTR pWrappedKey, andre@0: CK_ULONG_PTR pulWrappedKeyLen, CK_RV rv); andre@0: andre@0: extern void sftk_AuditUnwrapKey(CK_SESSION_HANDLE hSession, andre@0: CK_MECHANISM_PTR pMechanism, andre@0: 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, CK_RV rv); andre@0: andre@0: extern void sftk_AuditDeriveKey(CK_SESSION_HANDLE hSession, andre@0: CK_MECHANISM_PTR pMechanism, andre@0: CK_OBJECT_HANDLE hBaseKey, andre@0: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount, andre@0: CK_OBJECT_HANDLE_PTR phKey, CK_RV rv); andre@0: andre@0: extern void sftk_AuditDigestKey(CK_SESSION_HANDLE hSession, andre@0: CK_OBJECT_HANDLE hKey, CK_RV rv); andre@0: andre@0: /* andre@0: ** FIPS 140-2 Error state andre@0: */ andre@0: extern PRBool sftk_fatalError; andre@0: andre@0: /* andre@0: ** macros to check for forked child process after C_Initialize andre@0: */ andre@0: #if defined(XP_UNIX) && !defined(NO_FORK_CHECK) andre@0: andre@0: #ifdef DEBUG andre@0: andre@0: #define FORK_ASSERT() \ andre@0: { \ andre@0: char* forkAssert = getenv("NSS_STRICT_NOFORK"); \ andre@0: if ( (!forkAssert) || (0 == strcmp(forkAssert, "1")) ) { \ andre@0: PORT_Assert(0); \ andre@0: } \ andre@0: } andre@0: andre@0: #else andre@0: andre@0: #define FORK_ASSERT() andre@0: andre@0: #endif andre@0: andre@0: /* we have 3 methods of implementing the fork checks : andre@0: * - Solaris "mixed" method andre@0: * - pthread_atfork method andre@0: * - getpid method andre@0: */ andre@0: andre@0: #if !defined (CHECK_FORK_MIXED) && !defined(CHECK_FORK_PTHREAD) && \ andre@0: !defined (CHECK_FORK_GETPID) andre@0: andre@0: /* Choose fork check method automatically unless specified andre@0: * This section should be updated as more platforms get pthread fixes andre@0: * to unregister fork handlers in dlclose. andre@0: */ andre@0: andre@0: #ifdef SOLARIS andre@0: andre@0: /* Solaris 8, s9 use PID checks, s10 uses pthread_atfork */ andre@0: andre@0: #define CHECK_FORK_MIXED andre@0: andre@0: #elif defined(LINUX) andre@0: andre@0: #define CHECK_FORK_PTHREAD andre@0: andre@0: #else andre@0: andre@0: /* Other Unix platforms use only PID checks. Even if pthread_atfork is andre@0: * available, the behavior of dlclose isn't guaranteed by POSIX to andre@0: * unregister the fork handler. */ andre@0: andre@0: #define CHECK_FORK_GETPID andre@0: andre@0: #endif andre@0: andre@0: #endif andre@0: andre@0: #if defined(CHECK_FORK_MIXED) andre@0: andre@0: extern PRBool usePthread_atfork; andre@0: #include andre@0: extern pid_t myPid; andre@0: extern PRBool forked; andre@0: andre@0: #define PARENT_FORKED() (usePthread_atfork ? forked : (myPid && myPid != getpid())) andre@0: andre@0: #elif defined(CHECK_FORK_PTHREAD) andre@0: andre@0: extern PRBool forked; andre@0: andre@0: #define PARENT_FORKED() forked andre@0: andre@0: #elif defined(CHECK_FORK_GETPID) andre@0: andre@0: #include andre@0: extern pid_t myPid; andre@0: andre@0: #define PARENT_FORKED() (myPid && myPid != getpid()) andre@0: andre@0: #endif andre@0: andre@0: extern PRBool parentForkedAfterC_Initialize; andre@0: extern PRBool sftkForkCheckDisabled; andre@0: andre@0: #define CHECK_FORK() \ andre@0: do { \ andre@0: if (!sftkForkCheckDisabled && PARENT_FORKED()) { \ andre@0: FORK_ASSERT(); \ andre@0: return CKR_DEVICE_ERROR; \ andre@0: } \ andre@0: } while (0) andre@0: andre@0: #define SKIP_AFTER_FORK(x) if (!parentForkedAfterC_Initialize) x andre@0: andre@0: #define ENABLE_FORK_CHECK() \ andre@0: { \ andre@0: char* doForkCheck = getenv("NSS_STRICT_NOFORK"); \ andre@0: if ( doForkCheck && !strcmp(doForkCheck, "DISABLED") ) { \ andre@0: sftkForkCheckDisabled = PR_TRUE; \ andre@0: } \ andre@0: } andre@0: andre@0: andre@0: #else andre@0: andre@0: /* non-Unix platforms, or fork check disabled */ andre@0: andre@0: #define CHECK_FORK() andre@0: #define SKIP_AFTER_FORK(x) x andre@0: #define ENABLE_FORK_CHECK() andre@0: andre@0: #ifndef NO_FORK_CHECK andre@0: #define NO_FORK_CHECK andre@0: #endif andre@0: andre@0: #endif andre@0: andre@0: andre@0: SEC_END_PROTOS andre@0: andre@0: #endif /* _SOFTOKEN_H_ */