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: * It supports Public Key ops, and all they bulk ciphers and hashes. andre@0: * It can also support Private Key ops for imported Private keys. It does andre@0: * 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 "secitem.h" andre@0: #include "pkcs11.h" andre@0: #include "pkcs11i.h" andre@0: #include "softoken.h" andre@0: #include "lowkeyi.h" andre@0: #include "blapi.h" andre@0: #include "secder.h" andre@0: #include "secport.h" andre@0: #include "secrng.h" andre@0: #include "prtypes.h" andre@0: #include "nspr.h" andre@0: #include "softkver.h" andre@0: #include "secoid.h" andre@0: #include "sftkdb.h" andre@0: #include "utilpars.h" andre@0: #include "ec.h" andre@0: #include "secasn1.h" andre@0: #include "secerr.h" andre@0: #include "lgglue.h" andre@0: andre@0: PRBool parentForkedAfterC_Initialize; andre@0: andre@0: #ifndef NO_FORK_CHECK andre@0: andre@0: PRBool sftkForkCheckDisabled; andre@0: andre@0: #if defined(CHECK_FORK_PTHREAD) || defined(CHECK_FORK_MIXED) andre@0: PRBool forked = PR_FALSE; andre@0: #endif andre@0: andre@0: #if defined(CHECK_FORK_GETPID) || defined(CHECK_FORK_MIXED) andre@0: #include andre@0: pid_t myPid; andre@0: #endif andre@0: andre@0: #ifdef CHECK_FORK_MIXED andre@0: #include andre@0: PRBool usePthread_atfork; andre@0: #endif andre@0: andre@0: #endif andre@0: andre@0: /* andre@0: * ******************** Static data ******************************* andre@0: */ andre@0: andre@0: /* The next three strings must be exactly 32 characters long */ andre@0: static char *manufacturerID = "Mozilla Foundation "; andre@0: static char manufacturerID_space[33]; andre@0: static char *libraryDescription = "NSS Internal Crypto Services "; andre@0: static char libraryDescription_space[33]; andre@0: andre@0: /* andre@0: * In FIPS mode, we disallow login attempts for 1 second after a login andre@0: * failure so that there are at most 60 login attempts per minute. andre@0: */ andre@0: static PRIntervalTime loginWaitTime; andre@0: static PRUint32 minSessionObjectHandle = 1U; andre@0: andre@0: #define __PASTE(x,y) x##y andre@0: andre@0: /* andre@0: * we renamed all our internal functions, get the correct andre@0: * definitions for them... andre@0: */ andre@0: #undef CK_PKCS11_FUNCTION_INFO andre@0: #undef CK_NEED_ARG_LIST andre@0: andre@0: #define CK_EXTERN extern andre@0: #define CK_PKCS11_FUNCTION_INFO(func) \ andre@0: CK_RV __PASTE(NS,func) andre@0: #define CK_NEED_ARG_LIST 1 andre@0: andre@0: #include "pkcs11f.h" andre@0: andre@0: andre@0: andre@0: /* build the crypto module table */ andre@0: static const CK_FUNCTION_LIST sftk_funcList = { andre@0: { 1, 10 }, andre@0: andre@0: #undef CK_PKCS11_FUNCTION_INFO andre@0: #undef CK_NEED_ARG_LIST andre@0: andre@0: #define CK_PKCS11_FUNCTION_INFO(func) \ andre@0: __PASTE(NS,func), andre@0: #include "pkcs11f.h" andre@0: andre@0: }; andre@0: andre@0: #undef CK_PKCS11_FUNCTION_INFO andre@0: #undef CK_NEED_ARG_LIST andre@0: andre@0: andre@0: #undef __PASTE andre@0: andre@0: /* List of DES Weak Keys */ andre@0: typedef unsigned char desKey[8]; andre@0: static const desKey sftk_desWeakTable[] = { andre@0: #ifdef noParity andre@0: /* weak */ andre@0: { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, andre@0: { 0x1e, 0x1e, 0x1e, 0x1e, 0x0e, 0x0e, 0x0e, 0x0e }, andre@0: { 0xe0, 0xe0, 0xe0, 0xe0, 0xf0, 0xf0, 0xf0, 0xf0 }, andre@0: { 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe }, andre@0: /* semi-weak */ andre@0: { 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe }, andre@0: { 0xfe, 0x00, 0xfe, 0x00, 0x00, 0xfe, 0x00, 0xfe }, andre@0: andre@0: { 0x1e, 0xe0, 0x1e, 0xe0, 0x0e, 0xf0, 0x0e, 0xf0 }, andre@0: { 0xe0, 0x1e, 0xe0, 0x1e, 0xf0, 0x0e, 0xf0, 0x0e }, andre@0: andre@0: { 0x00, 0xe0, 0x00, 0xe0, 0x00, 0x0f, 0x00, 0x0f }, andre@0: { 0xe0, 0x00, 0xe0, 0x00, 0xf0, 0x00, 0xf0, 0x00 }, andre@0: andre@0: { 0x1e, 0xfe, 0x1e, 0xfe, 0x0e, 0xfe, 0x0e, 0xfe }, andre@0: { 0xfe, 0x1e, 0xfe, 0x1e, 0xfe, 0x0e, 0xfe, 0x0e }, andre@0: andre@0: { 0x00, 0x1e, 0x00, 0x1e, 0x00, 0x0e, 0x00, 0x0e }, andre@0: { 0x1e, 0x00, 0x1e, 0x00, 0x0e, 0x00, 0x0e, 0x00 }, andre@0: andre@0: { 0xe0, 0xfe, 0xe0, 0xfe, 0xf0, 0xfe, 0xf0, 0xfe }, andre@0: { 0xfe, 0xe0, 0xfe, 0xe0, 0xfe, 0xf0, 0xfe, 0xf0 }, andre@0: #else andre@0: /* weak */ andre@0: { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }, andre@0: { 0x1f, 0x1f, 0x1f, 0x1f, 0x0e, 0x0e, 0x0e, 0x0e }, andre@0: { 0xe0, 0xe0, 0xe0, 0xe0, 0xf1, 0xf1, 0xf1, 0xf1 }, andre@0: { 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe }, andre@0: andre@0: /* semi-weak */ andre@0: { 0x01, 0xfe, 0x01, 0xfe, 0x01, 0xfe, 0x01, 0xfe }, andre@0: { 0xfe, 0x01, 0xfe, 0x01, 0xfe, 0x01, 0xfe, 0x01 }, andre@0: andre@0: { 0x1f, 0xe0, 0x1f, 0xe0, 0x0e, 0xf1, 0x0e, 0xf1 }, andre@0: { 0xe0, 0x1f, 0xe0, 0x1f, 0xf1, 0x0e, 0xf1, 0x0e }, andre@0: andre@0: { 0x01, 0xe0, 0x01, 0xe0, 0x01, 0xf1, 0x01, 0xf1 }, andre@0: { 0xe0, 0x01, 0xe0, 0x01, 0xf1, 0x01, 0xf1, 0x01 }, andre@0: andre@0: { 0x1f, 0xfe, 0x1f, 0xfe, 0x0e, 0xfe, 0x0e, 0xfe }, andre@0: { 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x0e, 0xfe, 0x0e }, andre@0: andre@0: { 0x01, 0x1f, 0x01, 0x1f, 0x01, 0x0e, 0x01, 0x0e }, andre@0: { 0x1f, 0x01, 0x1f, 0x01, 0x0e, 0x01, 0x0e, 0x01 }, andre@0: andre@0: { 0xe0, 0xfe, 0xe0, 0xfe, 0xf1, 0xfe, 0xf1, 0xfe }, andre@0: { 0xfe, 0xe0, 0xfe, 0xe0, 0xfe, 0xf1, 0xfe, 0xf1 } andre@0: #endif andre@0: }; andre@0: andre@0: andre@0: static const int sftk_desWeakTableSize = sizeof(sftk_desWeakTable)/ andre@0: sizeof(sftk_desWeakTable[0]); andre@0: andre@0: /* DES KEY Parity conversion table. Takes each byte/2 as an index, returns andre@0: * that byte with the proper parity bit set */ andre@0: static const unsigned char parityTable[256] = { andre@0: /* Even...0x00,0x02,0x04,0x06,0x08,0x0a,0x0c,0x0e */ andre@0: /* E */ 0x01,0x02,0x04,0x07,0x08,0x0b,0x0d,0x0e, andre@0: /* Odd....0x10,0x12,0x14,0x16,0x18,0x1a,0x1c,0x1e */ andre@0: /* O */ 0x10,0x13,0x15,0x16,0x19,0x1a,0x1c,0x1f, andre@0: /* Odd....0x20,0x22,0x24,0x26,0x28,0x2a,0x2c,0x2e */ andre@0: /* O */ 0x20,0x23,0x25,0x26,0x29,0x2a,0x2c,0x2f, andre@0: /* Even...0x30,0x32,0x34,0x36,0x38,0x3a,0x3c,0x3e */ andre@0: /* E */ 0x31,0x32,0x34,0x37,0x38,0x3b,0x3d,0x3e, andre@0: /* Odd....0x40,0x42,0x44,0x46,0x48,0x4a,0x4c,0x4e */ andre@0: /* O */ 0x40,0x43,0x45,0x46,0x49,0x4a,0x4c,0x4f, andre@0: /* Even...0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,0x5e */ andre@0: /* E */ 0x51,0x52,0x54,0x57,0x58,0x5b,0x5d,0x5e, andre@0: /* Even...0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6e */ andre@0: /* E */ 0x61,0x62,0x64,0x67,0x68,0x6b,0x6d,0x6e, andre@0: /* Odd....0x70,0x72,0x74,0x76,0x78,0x7a,0x7c,0x7e */ andre@0: /* O */ 0x70,0x73,0x75,0x76,0x79,0x7a,0x7c,0x7f, andre@0: /* Odd....0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e */ andre@0: /* O */ 0x80,0x83,0x85,0x86,0x89,0x8a,0x8c,0x8f, andre@0: /* Even...0x90,0x92,0x94,0x96,0x98,0x9a,0x9c,0x9e */ andre@0: /* E */ 0x91,0x92,0x94,0x97,0x98,0x9b,0x9d,0x9e, andre@0: /* Even...0xa0,0xa2,0xa4,0xa6,0xa8,0xaa,0xac,0xae */ andre@0: /* E */ 0xa1,0xa2,0xa4,0xa7,0xa8,0xab,0xad,0xae, andre@0: /* Odd....0xb0,0xb2,0xb4,0xb6,0xb8,0xba,0xbc,0xbe */ andre@0: /* O */ 0xb0,0xb3,0xb5,0xb6,0xb9,0xba,0xbc,0xbf, andre@0: /* Even...0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcc,0xce */ andre@0: /* E */ 0xc1,0xc2,0xc4,0xc7,0xc8,0xcb,0xcd,0xce, andre@0: /* Odd....0xd0,0xd2,0xd4,0xd6,0xd8,0xda,0xdc,0xde */ andre@0: /* O */ 0xd0,0xd3,0xd5,0xd6,0xd9,0xda,0xdc,0xdf, andre@0: /* Odd....0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xee */ andre@0: /* O */ 0xe0,0xe3,0xe5,0xe6,0xe9,0xea,0xec,0xef, andre@0: /* Even...0xf0,0xf2,0xf4,0xf6,0xf8,0xfa,0xfc,0xfe */ andre@0: /* E */ 0xf1,0xf2,0xf4,0xf7,0xf8,0xfb,0xfd,0xfe, andre@0: }; andre@0: andre@0: /* Mechanisms */ andre@0: struct mechanismList { andre@0: CK_MECHANISM_TYPE type; andre@0: CK_MECHANISM_INFO info; andre@0: PRBool privkey; andre@0: }; andre@0: andre@0: /* andre@0: * the following table includes a complete list of mechanism defined by andre@0: * PKCS #11 version 2.01. Those Mechanisms not supported by this PKCS #11 andre@0: * module are ifdef'ed out. andre@0: */ andre@0: #define CKF_EN_DE CKF_ENCRYPT | CKF_DECRYPT andre@0: #define CKF_WR_UN CKF_WRAP | CKF_UNWRAP andre@0: #define CKF_SN_VR CKF_SIGN | CKF_VERIFY andre@0: #define CKF_SN_RE CKF_SIGN_RECOVER | CKF_VERIFY_RECOVER andre@0: andre@0: #define CKF_EN_DE_WR_UN CKF_EN_DE | CKF_WR_UN andre@0: #define CKF_SN_VR_RE CKF_SN_VR | CKF_SN_RE andre@0: #define CKF_DUZ_IT_ALL CKF_EN_DE_WR_UN | CKF_SN_VR_RE andre@0: andre@0: #define CKF_EC_PNU CKF_EC_FP | CKF_EC_NAMEDCURVE | CKF_EC_UNCOMPRESS andre@0: andre@0: #define CKF_EC_BPNU CKF_EC_F_2M | CKF_EC_PNU andre@0: andre@0: #define CK_MAX 0xffffffff andre@0: andre@0: static const struct mechanismList mechanisms[] = { andre@0: andre@0: /* andre@0: * PKCS #11 Mechanism List. andre@0: * andre@0: * The first argument is the PKCS #11 Mechanism we support. andre@0: * The second argument is Mechanism info structure. It includes: andre@0: * The minimum key size, andre@0: * in bits for RSA, DSA, DH, EC*, KEA, RC2 and RC4 * algs. andre@0: * in bytes for RC5, AES, Camellia, and CAST* andre@0: * ignored for DES*, IDEA and FORTEZZA based andre@0: * The maximum key size, andre@0: * in bits for RSA, DSA, DH, EC*, KEA, RC2 and RC4 * algs. andre@0: * in bytes for RC5, AES, Camellia, and CAST* andre@0: * ignored for DES*, IDEA and FORTEZZA based andre@0: * Flags andre@0: * What operations are supported by this mechanism. andre@0: * The third argument is a bool which tells if this mechanism is andre@0: * supported in the database token. andre@0: * andre@0: */ andre@0: andre@0: /* ------------------------- RSA Operations ---------------------------*/ andre@0: {CKM_RSA_PKCS_KEY_PAIR_GEN,{RSA_MIN_MODULUS_BITS,CK_MAX, andre@0: CKF_GENERATE_KEY_PAIR},PR_TRUE}, andre@0: {CKM_RSA_PKCS, {RSA_MIN_MODULUS_BITS,CK_MAX, andre@0: CKF_DUZ_IT_ALL}, PR_TRUE}, andre@0: {CKM_RSA_PKCS_PSS, {RSA_MIN_MODULUS_BITS,CK_MAX, andre@0: CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_RSA_PKCS_OAEP, {RSA_MIN_MODULUS_BITS,CK_MAX, andre@0: CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: #ifdef SFTK_RSA9796_SUPPORTED andre@0: {CKM_RSA_9796, {RSA_MIN_MODULUS_BITS,CK_MAX, andre@0: CKF_DUZ_IT_ALL}, PR_TRUE}, andre@0: #endif andre@0: {CKM_RSA_X_509, {RSA_MIN_MODULUS_BITS,CK_MAX, andre@0: CKF_DUZ_IT_ALL}, PR_TRUE}, andre@0: /* -------------- RSA Multipart Signing Operations -------------------- */ andre@0: {CKM_MD2_RSA_PKCS, {RSA_MIN_MODULUS_BITS,CK_MAX, andre@0: CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_MD5_RSA_PKCS, {RSA_MIN_MODULUS_BITS,CK_MAX, andre@0: CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SHA1_RSA_PKCS, {RSA_MIN_MODULUS_BITS,CK_MAX, andre@0: CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SHA224_RSA_PKCS, {RSA_MIN_MODULUS_BITS,CK_MAX, andre@0: CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SHA256_RSA_PKCS, {RSA_MIN_MODULUS_BITS,CK_MAX, andre@0: CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SHA384_RSA_PKCS, {RSA_MIN_MODULUS_BITS,CK_MAX, andre@0: CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SHA512_RSA_PKCS, {RSA_MIN_MODULUS_BITS,CK_MAX, andre@0: CKF_SN_VR}, PR_TRUE}, andre@0: /* ------------------------- DSA Operations --------------------------- */ andre@0: {CKM_DSA_KEY_PAIR_GEN, {DSA_MIN_P_BITS, DSA_MAX_P_BITS, andre@0: CKF_GENERATE_KEY_PAIR}, PR_TRUE}, andre@0: {CKM_DSA, {DSA_MIN_P_BITS, DSA_MAX_P_BITS, andre@0: CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_DSA_PARAMETER_GEN, {DSA_MIN_P_BITS, DSA_MAX_P_BITS, andre@0: CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_DSA_SHA1, {DSA_MIN_P_BITS, DSA_MAX_P_BITS, andre@0: CKF_SN_VR}, PR_TRUE}, andre@0: /* -------------------- Diffie Hellman Operations --------------------- */ andre@0: /* no diffie hellman yet */ andre@0: {CKM_DH_PKCS_KEY_PAIR_GEN, {DH_MIN_P_BITS, DH_MAX_P_BITS, andre@0: CKF_GENERATE_KEY_PAIR}, PR_TRUE}, andre@0: {CKM_DH_PKCS_DERIVE, {DH_MIN_P_BITS, DH_MAX_P_BITS, andre@0: CKF_DERIVE}, PR_TRUE}, andre@0: #ifndef NSS_DISABLE_ECC andre@0: /* -------------------- Elliptic Curve Operations --------------------- */ andre@0: {CKM_EC_KEY_PAIR_GEN, {EC_MIN_KEY_BITS, EC_MAX_KEY_BITS, andre@0: CKF_GENERATE_KEY_PAIR|CKF_EC_BPNU}, PR_TRUE}, andre@0: {CKM_ECDH1_DERIVE, {EC_MIN_KEY_BITS, EC_MAX_KEY_BITS, andre@0: CKF_DERIVE|CKF_EC_BPNU}, PR_TRUE}, andre@0: {CKM_ECDSA, {EC_MIN_KEY_BITS, EC_MAX_KEY_BITS, andre@0: CKF_SN_VR|CKF_EC_BPNU}, PR_TRUE}, andre@0: {CKM_ECDSA_SHA1, {EC_MIN_KEY_BITS, EC_MAX_KEY_BITS, andre@0: CKF_SN_VR|CKF_EC_BPNU}, PR_TRUE}, andre@0: #endif /* NSS_DISABLE_ECC */ andre@0: /* ------------------------- RC2 Operations --------------------------- */ andre@0: {CKM_RC2_KEY_GEN, {1, 128, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_RC2_ECB, {1, 128, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_RC2_CBC, {1, 128, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_RC2_MAC, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_RC2_MAC_GENERAL, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_RC2_CBC_PAD, {1, 128, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: /* ------------------------- RC4 Operations --------------------------- */ andre@0: {CKM_RC4_KEY_GEN, {1, 256, CKF_GENERATE}, PR_FALSE}, andre@0: {CKM_RC4, {1, 256, CKF_EN_DE_WR_UN}, PR_FALSE}, andre@0: /* ------------------------- DES Operations --------------------------- */ andre@0: {CKM_DES_KEY_GEN, { 8, 8, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_DES_ECB, { 8, 8, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_DES_CBC, { 8, 8, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_DES_MAC, { 8, 8, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_DES_MAC_GENERAL, { 8, 8, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_DES_CBC_PAD, { 8, 8, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_DES2_KEY_GEN, {24, 24, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_DES3_KEY_GEN, {24, 24, CKF_GENERATE}, PR_TRUE }, andre@0: {CKM_DES3_ECB, {24, 24, CKF_EN_DE_WR_UN}, PR_TRUE }, andre@0: {CKM_DES3_CBC, {24, 24, CKF_EN_DE_WR_UN}, PR_TRUE }, andre@0: {CKM_DES3_MAC, {24, 24, CKF_SN_VR}, PR_TRUE }, andre@0: {CKM_DES3_MAC_GENERAL, {24, 24, CKF_SN_VR}, PR_TRUE }, andre@0: {CKM_DES3_CBC_PAD, {24, 24, CKF_EN_DE_WR_UN}, PR_TRUE }, andre@0: /* ------------------------- CDMF Operations --------------------------- */ andre@0: {CKM_CDMF_KEY_GEN, {8, 8, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_CDMF_ECB, {8, 8, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_CDMF_CBC, {8, 8, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_CDMF_MAC, {8, 8, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_CDMF_MAC_GENERAL, {8, 8, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_CDMF_CBC_PAD, {8, 8, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: /* ------------------------- AES Operations --------------------------- */ andre@0: {CKM_AES_KEY_GEN, {16, 32, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_AES_ECB, {16, 32, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_AES_CBC, {16, 32, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_AES_MAC, {16, 32, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_AES_MAC_GENERAL, {16, 32, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_AES_CBC_PAD, {16, 32, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_AES_CTS, {16, 32, CKF_EN_DE}, PR_TRUE}, andre@0: {CKM_AES_CTR, {16, 32, CKF_EN_DE}, PR_TRUE}, andre@0: {CKM_AES_GCM, {16, 32, CKF_EN_DE}, PR_TRUE}, andre@0: /* ------------------------- Camellia Operations --------------------- */ andre@0: {CKM_CAMELLIA_KEY_GEN, {16, 32, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_CAMELLIA_ECB, {16, 32, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_CAMELLIA_CBC, {16, 32, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_CAMELLIA_MAC, {16, 32, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_CAMELLIA_MAC_GENERAL, {16, 32, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_CAMELLIA_CBC_PAD, {16, 32, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: /* ------------------------- SEED Operations --------------------------- */ andre@0: {CKM_SEED_KEY_GEN, {16, 16, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_SEED_ECB, {16, 16, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_SEED_CBC, {16, 16, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_SEED_MAC, {16, 16, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SEED_MAC_GENERAL, {16, 16, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SEED_CBC_PAD, {16, 16, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: /* ------------------------- ChaCha20 Operations ---------------------- */ andre@0: {CKM_NSS_CHACHA20_KEY_GEN, {32, 32, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_NSS_CHACHA20_POLY1305,{32, 32, CKF_EN_DE}, PR_TRUE}, andre@0: /* ------------------------- Hashing Operations ----------------------- */ andre@0: {CKM_MD2, {0, 0, CKF_DIGEST}, PR_FALSE}, andre@0: {CKM_MD2_HMAC, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_MD2_HMAC_GENERAL, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_MD5, {0, 0, CKF_DIGEST}, PR_FALSE}, andre@0: {CKM_MD5_HMAC, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_MD5_HMAC_GENERAL, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SHA_1, {0, 0, CKF_DIGEST}, PR_FALSE}, andre@0: {CKM_SHA_1_HMAC, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SHA_1_HMAC_GENERAL, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SHA224, {0, 0, CKF_DIGEST}, PR_FALSE}, andre@0: {CKM_SHA224_HMAC, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SHA224_HMAC_GENERAL, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SHA256, {0, 0, CKF_DIGEST}, PR_FALSE}, andre@0: {CKM_SHA256_HMAC, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SHA256_HMAC_GENERAL, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SHA384, {0, 0, CKF_DIGEST}, PR_FALSE}, andre@0: {CKM_SHA384_HMAC, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SHA384_HMAC_GENERAL, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SHA512, {0, 0, CKF_DIGEST}, PR_FALSE}, andre@0: {CKM_SHA512_HMAC, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_SHA512_HMAC_GENERAL, {1, 128, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_TLS_PRF_GENERAL, {0, 512, CKF_SN_VR}, PR_FALSE}, andre@0: {CKM_NSS_TLS_PRF_GENERAL_SHA256, andre@0: {0, 512, CKF_SN_VR}, PR_FALSE}, andre@0: /* ------------------------- HKDF Operations -------------------------- */ andre@0: {CKM_NSS_HKDF_SHA1, {1, 128, CKF_DERIVE}, PR_TRUE}, andre@0: {CKM_NSS_HKDF_SHA256, {1, 128, CKF_DERIVE}, PR_TRUE}, andre@0: {CKM_NSS_HKDF_SHA384, {1, 128, CKF_DERIVE}, PR_TRUE}, andre@0: {CKM_NSS_HKDF_SHA512, {1, 128, CKF_DERIVE}, PR_TRUE}, andre@0: /* ------------------------- CAST Operations --------------------------- */ andre@0: #ifdef NSS_SOFTOKEN_DOES_CAST andre@0: /* Cast operations are not supported ( yet? ) */ andre@0: {CKM_CAST_KEY_GEN, {1, 8, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_CAST_ECB, {1, 8, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_CAST_CBC, {1, 8, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_CAST_MAC, {1, 8, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_CAST_MAC_GENERAL, {1, 8, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_CAST_CBC_PAD, {1, 8, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_CAST3_KEY_GEN, {1, 16, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_CAST3_ECB, {1, 16, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_CAST3_CBC, {1, 16, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_CAST3_MAC, {1, 16, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_CAST3_MAC_GENERAL, {1, 16, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_CAST3_CBC_PAD, {1, 16, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_CAST5_KEY_GEN, {1, 16, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_CAST5_ECB, {1, 16, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_CAST5_CBC, {1, 16, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_CAST5_MAC, {1, 16, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_CAST5_MAC_GENERAL, {1, 16, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_CAST5_CBC_PAD, {1, 16, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: #endif andre@0: #if NSS_SOFTOKEN_DOES_RC5 andre@0: /* ------------------------- RC5 Operations --------------------------- */ andre@0: {CKM_RC5_KEY_GEN, {1, 32, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_RC5_ECB, {1, 32, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_RC5_CBC, {1, 32, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_RC5_MAC, {1, 32, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_RC5_MAC_GENERAL, {1, 32, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_RC5_CBC_PAD, {1, 32, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: #endif andre@0: #ifdef NSS_SOFTOKEN_DOES_IDEA andre@0: /* ------------------------- IDEA Operations -------------------------- */ andre@0: {CKM_IDEA_KEY_GEN, {16, 16, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_IDEA_ECB, {16, 16, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_IDEA_CBC, {16, 16, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_IDEA_MAC, {16, 16, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_IDEA_MAC_GENERAL, {16, 16, CKF_SN_VR}, PR_TRUE}, andre@0: {CKM_IDEA_CBC_PAD, {16, 16, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: #endif andre@0: /* --------------------- Secret Key Operations ------------------------ */ andre@0: {CKM_GENERIC_SECRET_KEY_GEN, {1, 32, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_CONCATENATE_BASE_AND_KEY, {1, 32, CKF_GENERATE}, PR_FALSE}, andre@0: {CKM_CONCATENATE_BASE_AND_DATA, {1, 32, CKF_GENERATE}, PR_FALSE}, andre@0: {CKM_CONCATENATE_DATA_AND_BASE, {1, 32, CKF_GENERATE}, PR_FALSE}, andre@0: {CKM_XOR_BASE_AND_DATA, {1, 32, CKF_GENERATE}, PR_FALSE}, andre@0: {CKM_EXTRACT_KEY_FROM_KEY, {1, 32, CKF_DERIVE}, PR_FALSE}, andre@0: /* ---------------------- SSL Key Derivations ------------------------- */ andre@0: {CKM_SSL3_PRE_MASTER_KEY_GEN, {48, 48, CKF_GENERATE}, PR_FALSE}, andre@0: {CKM_SSL3_MASTER_KEY_DERIVE, {48, 48, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_SSL3_MASTER_KEY_DERIVE_DH, {8, 128, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_SSL3_KEY_AND_MAC_DERIVE, {48, 48, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_SSL3_MD5_MAC, { 0, 16, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_SSL3_SHA1_MAC, { 0, 20, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_MD5_KEY_DERIVATION, { 0, 16, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_MD2_KEY_DERIVATION, { 0, 16, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_SHA1_KEY_DERIVATION, { 0, 20, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_SHA224_KEY_DERIVATION, { 0, 28, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_SHA256_KEY_DERIVATION, { 0, 32, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_SHA384_KEY_DERIVATION, { 0, 48, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_SHA512_KEY_DERIVATION, { 0, 64, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_TLS_MASTER_KEY_DERIVE, {48, 48, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_NSS_TLS_MASTER_KEY_DERIVE_SHA256, andre@0: {48, 48, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_TLS_MASTER_KEY_DERIVE_DH, {8, 128, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_NSS_TLS_MASTER_KEY_DERIVE_DH_SHA256, andre@0: {8, 128, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_TLS_KEY_AND_MAC_DERIVE, {48, 48, CKF_DERIVE}, PR_FALSE}, andre@0: {CKM_NSS_TLS_KEY_AND_MAC_DERIVE_SHA256, andre@0: {48, 48, CKF_DERIVE}, PR_FALSE}, andre@0: /* ---------------------- PBE Key Derivations ------------------------ */ andre@0: {CKM_PBE_MD2_DES_CBC, {8, 8, CKF_DERIVE}, PR_TRUE}, andre@0: {CKM_PBE_MD5_DES_CBC, {8, 8, CKF_DERIVE}, PR_TRUE}, andre@0: /* ------------------ NETSCAPE PBE Key Derivations ------------------- */ andre@0: {CKM_NETSCAPE_PBE_SHA1_DES_CBC, { 8, 8, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_NETSCAPE_PBE_SHA1_FAULTY_3DES_CBC, {24,24, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_PBE_SHA1_DES3_EDE_CBC, {24,24, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_PBE_SHA1_DES2_EDE_CBC, {24,24, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_PBE_SHA1_RC2_40_CBC, {40,40, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_PBE_SHA1_RC2_128_CBC, {128,128, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_PBE_SHA1_RC4_40, {40,40, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_PBE_SHA1_RC4_128, {128,128, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_PBA_SHA1_WITH_SHA1_HMAC, {20,20, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_PKCS5_PBKD2, {1,256, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_NETSCAPE_PBE_SHA1_HMAC_KEY_GEN, {20,20, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_NETSCAPE_PBE_MD5_HMAC_KEY_GEN, {16,16, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_NETSCAPE_PBE_MD2_HMAC_KEY_GEN, {16,16, CKF_GENERATE}, PR_TRUE}, andre@0: /* ------------------ AES Key Wrap (also encrypt) ------------------- */ andre@0: {CKM_NETSCAPE_AES_KEY_WRAP, {16, 32, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: {CKM_NETSCAPE_AES_KEY_WRAP_PAD, {16, 32, CKF_EN_DE_WR_UN}, PR_TRUE}, andre@0: /* --------------------------- J-PAKE -------------------------------- */ andre@0: {CKM_NSS_JPAKE_ROUND1_SHA1, {0, 0, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_NSS_JPAKE_ROUND1_SHA256, {0, 0, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_NSS_JPAKE_ROUND1_SHA384, {0, 0, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_NSS_JPAKE_ROUND1_SHA512, {0, 0, CKF_GENERATE}, PR_TRUE}, andre@0: {CKM_NSS_JPAKE_ROUND2_SHA1, {0, 0, CKF_DERIVE}, PR_TRUE}, andre@0: {CKM_NSS_JPAKE_ROUND2_SHA256, {0, 0, CKF_DERIVE}, PR_TRUE}, andre@0: {CKM_NSS_JPAKE_ROUND2_SHA384, {0, 0, CKF_DERIVE}, PR_TRUE}, andre@0: {CKM_NSS_JPAKE_ROUND2_SHA512, {0, 0, CKF_DERIVE}, PR_TRUE}, andre@0: {CKM_NSS_JPAKE_FINAL_SHA1, {0, 0, CKF_DERIVE}, PR_TRUE}, andre@0: {CKM_NSS_JPAKE_FINAL_SHA256, {0, 0, CKF_DERIVE}, PR_TRUE}, andre@0: {CKM_NSS_JPAKE_FINAL_SHA384, {0, 0, CKF_DERIVE}, PR_TRUE}, andre@0: {CKM_NSS_JPAKE_FINAL_SHA512, {0, 0, CKF_DERIVE}, PR_TRUE}, andre@0: /* -------------------- Constant Time TLS MACs ----------------------- */ andre@0: {CKM_NSS_HMAC_CONSTANT_TIME, {0, 0, CKF_DIGEST}, PR_TRUE}, andre@0: {CKM_NSS_SSL3_MAC_CONSTANT_TIME, {0, 0, CKF_DIGEST}, PR_TRUE} andre@0: }; andre@0: static const CK_ULONG mechanismCount = sizeof(mechanisms)/sizeof(mechanisms[0]); andre@0: andre@0: /* sigh global so fipstokn can read it */ andre@0: PRBool nsc_init = PR_FALSE; andre@0: andre@0: #if defined(CHECK_FORK_PTHREAD) || defined(CHECK_FORK_MIXED) andre@0: andre@0: #include andre@0: andre@0: static void ForkedChild(void) andre@0: { andre@0: if (nsc_init || nsf_init) { andre@0: forked = PR_TRUE; andre@0: } andre@0: } andre@0: andre@0: #endif andre@0: andre@0: static char * andre@0: sftk_setStringName(const char *inString, char *buffer, int buffer_length, PRBool nullTerminate) andre@0: { andre@0: int full_length, string_length; andre@0: andre@0: full_length = nullTerminate ? buffer_length -1 : buffer_length; andre@0: string_length = PORT_Strlen(inString); andre@0: /* andre@0: * shorten the string, respecting utf8 encoding andre@0: * to do so, we work backward from the end andre@0: * bytes looking from the end are either: andre@0: * - ascii [0x00,0x7f] andre@0: * - the [2-n]th byte of a multibyte sequence andre@0: * [0x3F,0xBF], i.e, most significant 2 bits are '10' andre@0: * - the first byte of a multibyte sequence [0xC0,0xFD], andre@0: * i.e, most significant 2 bits are '11' andre@0: * andre@0: * When the string is too long, we lop off any trailing '10' bytes, andre@0: * if any. When these are all eliminated we lop off andre@0: * one additional byte. Thus if we lopped any '10' andre@0: * we'll be lopping a '11' byte (the first byte of the multibyte sequence), andre@0: * otherwise we're lopping off an ascii character. andre@0: * andre@0: * To test for '10' bytes, we first AND it with andre@0: * 11000000 (0xc0) so that we get 10000000 (0x80) if and only if andre@0: * the byte starts with 10. We test for equality. andre@0: */ andre@0: while ( string_length > full_length ) { andre@0: /* need to shorten */ andre@0: while ( string_length > 0 && andre@0: ((inString[string_length-1]&(char)0xc0) == (char)0x80)) { andre@0: /* lop off '10' byte */ andre@0: string_length--; andre@0: } andre@0: /* andre@0: * test string_length in case bad data is received andre@0: * and string consisted of all '10' bytes, andre@0: * avoiding any infinite loop andre@0: */ andre@0: if ( string_length ) { andre@0: /* remove either '11' byte or an asci byte */ andre@0: string_length--; andre@0: } andre@0: } andre@0: PORT_Memset(buffer,' ',full_length); andre@0: if (nullTerminate) { andre@0: buffer[full_length] = 0; andre@0: } andre@0: PORT_Memcpy(buffer,inString,string_length); andre@0: return buffer; andre@0: } andre@0: /* andre@0: * Configuration utils andre@0: */ andre@0: static CK_RV andre@0: sftk_configure(const char *man, const char *libdes) andre@0: { andre@0: andre@0: /* make sure the internationalization was done correctly... */ andre@0: if (man) { andre@0: manufacturerID = sftk_setStringName(man,manufacturerID_space, andre@0: sizeof(manufacturerID_space), PR_TRUE); andre@0: } andre@0: if (libdes) { andre@0: libraryDescription = sftk_setStringName(libdes, andre@0: libraryDescription_space, sizeof(libraryDescription_space), andre@0: PR_TRUE); andre@0: } andre@0: andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* andre@0: * ******************** Password Utilities ******************************* andre@0: */ andre@0: andre@0: /* andre@0: * see if the key DB password is enabled andre@0: */ andre@0: static PRBool andre@0: sftk_hasNullPassword(SFTKSlot *slot, SFTKDBHandle *keydb) andre@0: { andre@0: PRBool pwenabled; andre@0: andre@0: pwenabled = PR_FALSE; andre@0: if (sftkdb_HasPasswordSet(keydb) == SECSuccess) { andre@0: PRBool tokenRemoved = PR_FALSE; andre@0: SECStatus rv = sftkdb_CheckPassword(keydb, "", &tokenRemoved); andre@0: if (tokenRemoved) { andre@0: sftk_CloseAllSessions(slot, PR_FALSE); andre@0: } andre@0: return (rv == SECSuccess); andre@0: } andre@0: andre@0: return pwenabled; andre@0: } andre@0: andre@0: /* andre@0: * ******************** Object Creation Utilities *************************** andre@0: */ andre@0: andre@0: andre@0: /* Make sure a given attribute exists. If it doesn't, initialize it to andre@0: * value and len andre@0: */ andre@0: CK_RV andre@0: sftk_defaultAttribute(SFTKObject *object,CK_ATTRIBUTE_TYPE type, andre@0: const void *value, unsigned int len) andre@0: { andre@0: if ( !sftk_hasAttribute(object, type)) { andre@0: return sftk_AddAttributeType(object,type,value,len); andre@0: } andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* andre@0: * check the consistancy and initialize a Data Object andre@0: */ andre@0: static CK_RV andre@0: sftk_handleDataObject(SFTKSession *session,SFTKObject *object) andre@0: { andre@0: CK_RV crv; andre@0: andre@0: /* first reject private and token data objects */ andre@0: if (sftk_isTrue(object,CKA_PRIVATE) || sftk_isTrue(object,CKA_TOKEN)) { andre@0: return CKR_ATTRIBUTE_VALUE_INVALID; andre@0: } andre@0: andre@0: /* now just verify the required date fields */ andre@0: crv = sftk_defaultAttribute(object,CKA_APPLICATION,NULL,0); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_VALUE,NULL,0); andre@0: if (crv != CKR_OK) return crv; andre@0: andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* andre@0: * check the consistancy and initialize a Certificate Object andre@0: */ andre@0: static CK_RV andre@0: sftk_handleCertObject(SFTKSession *session,SFTKObject *object) andre@0: { andre@0: CK_CERTIFICATE_TYPE type; andre@0: SFTKAttribute *attribute; andre@0: CK_RV crv; andre@0: andre@0: /* certificates must have a type */ andre@0: if ( !sftk_hasAttribute(object,CKA_CERTIFICATE_TYPE) ) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: andre@0: /* we can't store any certs private */ andre@0: if (sftk_isTrue(object,CKA_PRIVATE)) { andre@0: return CKR_ATTRIBUTE_VALUE_INVALID; andre@0: } andre@0: andre@0: /* We only support X.509 Certs for now */ andre@0: attribute = sftk_FindAttribute(object,CKA_CERTIFICATE_TYPE); andre@0: if (attribute == NULL) return CKR_TEMPLATE_INCOMPLETE; andre@0: type = *(CK_CERTIFICATE_TYPE *)attribute->attrib.pValue; andre@0: sftk_FreeAttribute(attribute); andre@0: andre@0: if (type != CKC_X_509) { andre@0: return CKR_ATTRIBUTE_VALUE_INVALID; andre@0: } andre@0: andre@0: /* X.509 Certificate */ andre@0: andre@0: /* make sure we have a cert */ andre@0: if ( !sftk_hasAttribute(object,CKA_VALUE) ) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: andre@0: /* in PKCS #11, Subject is a required field */ andre@0: if ( !sftk_hasAttribute(object,CKA_SUBJECT) ) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: andre@0: /* in PKCS #11, Issuer is a required field */ andre@0: if ( !sftk_hasAttribute(object,CKA_ISSUER) ) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: andre@0: /* in PKCS #11, Serial is a required field */ andre@0: if ( !sftk_hasAttribute(object,CKA_SERIAL_NUMBER) ) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: andre@0: /* add it to the object */ andre@0: object->objectInfo = NULL; andre@0: object->infoFree = (SFTKFree) NULL; andre@0: andre@0: /* now just verify the required date fields */ andre@0: crv = sftk_defaultAttribute(object, CKA_ID, NULL, 0); andre@0: if (crv != CKR_OK) { return crv; } andre@0: andre@0: if (sftk_isTrue(object,CKA_TOKEN)) { andre@0: SFTKSlot *slot = session->slot; andre@0: SFTKDBHandle *certHandle = sftk_getCertDB(slot); andre@0: andre@0: if (certHandle == NULL) { andre@0: return CKR_TOKEN_WRITE_PROTECTED; andre@0: } andre@0: andre@0: crv = sftkdb_write(certHandle, object, &object->handle); andre@0: sftk_freeDB(certHandle); andre@0: return crv; andre@0: } andre@0: andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* andre@0: * check the consistancy and initialize a Trust Object andre@0: */ andre@0: static CK_RV andre@0: sftk_handleTrustObject(SFTKSession *session,SFTKObject *object) andre@0: { andre@0: /* we can't store any certs private */ andre@0: if (sftk_isTrue(object,CKA_PRIVATE)) { andre@0: return CKR_ATTRIBUTE_VALUE_INVALID; andre@0: } andre@0: andre@0: /* certificates must have a type */ andre@0: if ( !sftk_hasAttribute(object,CKA_ISSUER) ) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: if ( !sftk_hasAttribute(object,CKA_SERIAL_NUMBER) ) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: if ( !sftk_hasAttribute(object,CKA_CERT_SHA1_HASH) ) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: if ( !sftk_hasAttribute(object,CKA_CERT_MD5_HASH) ) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: andre@0: if (sftk_isTrue(object,CKA_TOKEN)) { andre@0: SFTKSlot *slot = session->slot; andre@0: SFTKDBHandle *certHandle = sftk_getCertDB(slot); andre@0: CK_RV crv; andre@0: andre@0: if (certHandle == NULL) { andre@0: return CKR_TOKEN_WRITE_PROTECTED; andre@0: } andre@0: andre@0: crv = sftkdb_write(certHandle, object, &object->handle); andre@0: sftk_freeDB(certHandle); andre@0: return crv; andre@0: } andre@0: andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* andre@0: * check the consistancy and initialize a Trust Object andre@0: */ andre@0: static CK_RV andre@0: sftk_handleSMimeObject(SFTKSession *session,SFTKObject *object) andre@0: { andre@0: andre@0: /* we can't store any certs private */ andre@0: if (sftk_isTrue(object,CKA_PRIVATE)) { andre@0: return CKR_ATTRIBUTE_VALUE_INVALID; andre@0: } andre@0: andre@0: /* certificates must have a type */ andre@0: if ( !sftk_hasAttribute(object,CKA_SUBJECT) ) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: if ( !sftk_hasAttribute(object,CKA_NETSCAPE_EMAIL) ) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: andre@0: if (sftk_isTrue(object,CKA_TOKEN)) { andre@0: SFTKSlot *slot = session->slot; andre@0: SFTKDBHandle *certHandle; andre@0: CK_RV crv; andre@0: andre@0: PORT_Assert(slot); andre@0: if (slot == NULL) { andre@0: return CKR_SESSION_HANDLE_INVALID; andre@0: } andre@0: andre@0: certHandle = sftk_getCertDB(slot); andre@0: if (certHandle == NULL) { andre@0: return CKR_TOKEN_WRITE_PROTECTED; andre@0: } andre@0: andre@0: crv = sftkdb_write(certHandle, object, &object->handle); andre@0: sftk_freeDB(certHandle); andre@0: return crv; andre@0: } andre@0: andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* andre@0: * check the consistancy and initialize a Trust Object andre@0: */ andre@0: static CK_RV andre@0: sftk_handleCrlObject(SFTKSession *session,SFTKObject *object) andre@0: { andre@0: andre@0: /* we can't store any certs private */ andre@0: if (sftk_isTrue(object,CKA_PRIVATE)) { andre@0: return CKR_ATTRIBUTE_VALUE_INVALID; andre@0: } andre@0: andre@0: /* certificates must have a type */ andre@0: if ( !sftk_hasAttribute(object,CKA_SUBJECT) ) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: if ( !sftk_hasAttribute(object,CKA_VALUE) ) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: andre@0: if (sftk_isTrue(object,CKA_TOKEN)) { andre@0: SFTKSlot *slot = session->slot; andre@0: SFTKDBHandle *certHandle = sftk_getCertDB(slot); andre@0: CK_RV crv; andre@0: andre@0: if (certHandle == NULL) { andre@0: return CKR_TOKEN_WRITE_PROTECTED; andre@0: } andre@0: andre@0: crv = sftkdb_write(certHandle, object, &object->handle); andre@0: sftk_freeDB(certHandle); andre@0: return crv; andre@0: } andre@0: andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* andre@0: * check the consistancy and initialize a Public Key Object andre@0: */ andre@0: static CK_RV andre@0: sftk_handlePublicKeyObject(SFTKSession *session, SFTKObject *object, andre@0: CK_KEY_TYPE key_type) andre@0: { andre@0: CK_BBOOL encrypt = CK_TRUE; andre@0: CK_BBOOL recover = CK_TRUE; andre@0: CK_BBOOL wrap = CK_TRUE; andre@0: CK_BBOOL derive = CK_FALSE; andre@0: CK_BBOOL verify = CK_TRUE; andre@0: CK_RV crv; andre@0: andre@0: switch (key_type) { andre@0: case CKK_RSA: andre@0: crv = sftk_ConstrainAttribute(object, CKA_MODULUS, andre@0: RSA_MIN_MODULUS_BITS, 0, 0); andre@0: if (crv != CKR_OK) { andre@0: return crv; andre@0: } andre@0: crv = sftk_ConstrainAttribute(object, CKA_PUBLIC_EXPONENT, 2, 0, 0); andre@0: if (crv != CKR_OK) { andre@0: return crv; andre@0: } andre@0: break; andre@0: case CKK_DSA: andre@0: crv = sftk_ConstrainAttribute(object, CKA_SUBPRIME, andre@0: DSA_MIN_Q_BITS, DSA_MAX_Q_BITS, 0); andre@0: if (crv != CKR_OK) { andre@0: return crv; andre@0: } andre@0: crv = sftk_ConstrainAttribute(object, CKA_PRIME, andre@0: DSA_MIN_P_BITS, DSA_MAX_P_BITS, 64); andre@0: if (crv != CKR_OK) { andre@0: return crv; andre@0: } andre@0: crv = sftk_ConstrainAttribute(object, CKA_BASE, 2, DSA_MAX_P_BITS, 0); andre@0: if (crv != CKR_OK) { andre@0: return crv; andre@0: } andre@0: crv = sftk_ConstrainAttribute(object, CKA_VALUE, 2, DSA_MAX_P_BITS, 0); andre@0: if (crv != CKR_OK) { andre@0: return crv; andre@0: } andre@0: encrypt = CK_FALSE; andre@0: recover = CK_FALSE; andre@0: wrap = CK_FALSE; andre@0: break; andre@0: case CKK_DH: andre@0: crv = sftk_ConstrainAttribute(object, CKA_PRIME, andre@0: DH_MIN_P_BITS, DH_MAX_P_BITS, 0); andre@0: if (crv != CKR_OK) { andre@0: return crv; andre@0: } andre@0: crv = sftk_ConstrainAttribute(object, CKA_BASE, 2, DH_MAX_P_BITS, 0); andre@0: if (crv != CKR_OK) { andre@0: return crv; andre@0: } andre@0: crv = sftk_ConstrainAttribute(object, CKA_VALUE, 2, DH_MAX_P_BITS, 0); andre@0: if (crv != CKR_OK) { andre@0: return crv; andre@0: } andre@0: verify = CK_FALSE; andre@0: derive = CK_TRUE; andre@0: encrypt = CK_FALSE; andre@0: recover = CK_FALSE; andre@0: wrap = CK_FALSE; andre@0: break; andre@0: #ifndef NSS_DISABLE_ECC andre@0: case CKK_EC: andre@0: if ( !sftk_hasAttribute(object, CKA_EC_PARAMS)) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: if ( !sftk_hasAttribute(object, CKA_EC_POINT)) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: derive = CK_TRUE; /* for ECDH */ andre@0: verify = CK_TRUE; /* for ECDSA */ andre@0: encrypt = CK_FALSE; andre@0: recover = CK_FALSE; andre@0: wrap = CK_FALSE; andre@0: break; andre@0: #endif /* NSS_DISABLE_ECC */ andre@0: default: andre@0: return CKR_ATTRIBUTE_VALUE_INVALID; andre@0: } andre@0: andre@0: /* make sure the required fields exist */ andre@0: crv = sftk_defaultAttribute(object,CKA_SUBJECT,NULL,0); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_ENCRYPT,&encrypt,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_VERIFY,&verify,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_VERIFY_RECOVER, andre@0: &recover,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_WRAP,&wrap,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_DERIVE,&derive,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: andre@0: object->objectInfo = sftk_GetPubKey(object,key_type, &crv); andre@0: if (object->objectInfo == NULL) { andre@0: return crv; andre@0: } andre@0: object->infoFree = (SFTKFree) nsslowkey_DestroyPublicKey; andre@0: andre@0: if (sftk_isTrue(object,CKA_TOKEN)) { andre@0: SFTKSlot *slot = session->slot; andre@0: SFTKDBHandle *certHandle = sftk_getCertDB(slot); andre@0: andre@0: if (certHandle == NULL) { andre@0: return CKR_TOKEN_WRITE_PROTECTED; andre@0: } andre@0: andre@0: crv = sftkdb_write(certHandle, object, &object->handle); andre@0: sftk_freeDB(certHandle); andre@0: return crv; andre@0: } andre@0: andre@0: return CKR_OK; andre@0: } andre@0: andre@0: static NSSLOWKEYPrivateKey * andre@0: sftk_mkPrivKey(SFTKObject *object,CK_KEY_TYPE key, CK_RV *rvp); andre@0: andre@0: static SECStatus andre@0: sftk_verifyRSAPrivateKey(SFTKObject *object, PRBool fillIfNeeded); andre@0: andre@0: /* andre@0: * check the consistancy and initialize a Private Key Object andre@0: */ andre@0: static CK_RV andre@0: sftk_handlePrivateKeyObject(SFTKSession *session,SFTKObject *object,CK_KEY_TYPE key_type) andre@0: { andre@0: CK_BBOOL cktrue = CK_TRUE; andre@0: CK_BBOOL encrypt = CK_TRUE; andre@0: CK_BBOOL sign = CK_FALSE; andre@0: CK_BBOOL recover = CK_TRUE; andre@0: CK_BBOOL wrap = CK_TRUE; andre@0: CK_BBOOL derive = CK_TRUE; andre@0: CK_BBOOL ckfalse = CK_FALSE; andre@0: PRBool createObjectInfo = PR_TRUE; andre@0: PRBool fillPrivateKey = PR_FALSE; andre@0: int missing_rsa_mod_component = 0; andre@0: int missing_rsa_exp_component = 0; andre@0: int missing_rsa_crt_component = 0; andre@0: andre@0: SECItem mod; andre@0: CK_RV crv; andre@0: SECStatus rv; andre@0: andre@0: switch (key_type) { andre@0: case CKK_RSA: andre@0: if ( !sftk_hasAttribute(object, CKA_MODULUS)) { andre@0: missing_rsa_mod_component++; andre@0: } andre@0: if ( !sftk_hasAttribute(object, CKA_PUBLIC_EXPONENT)) { andre@0: missing_rsa_exp_component++; andre@0: } andre@0: if ( !sftk_hasAttribute(object, CKA_PRIVATE_EXPONENT)) { andre@0: missing_rsa_exp_component++; andre@0: } andre@0: if ( !sftk_hasAttribute(object, CKA_PRIME_1)) { andre@0: missing_rsa_mod_component++; andre@0: } andre@0: if ( !sftk_hasAttribute(object, CKA_PRIME_2)) { andre@0: missing_rsa_mod_component++; andre@0: } andre@0: if ( !sftk_hasAttribute(object, CKA_EXPONENT_1)) { andre@0: missing_rsa_crt_component++; andre@0: } andre@0: if ( !sftk_hasAttribute(object, CKA_EXPONENT_2)) { andre@0: missing_rsa_crt_component++; andre@0: } andre@0: if ( !sftk_hasAttribute(object, CKA_COEFFICIENT)) { andre@0: missing_rsa_crt_component++; andre@0: } andre@0: if (missing_rsa_mod_component || missing_rsa_exp_component || andre@0: missing_rsa_crt_component) { andre@0: /* we are missing a component, see if we have enough to rebuild andre@0: * the rest */ andre@0: int have_exp = 2- missing_rsa_exp_component; andre@0: int have_component = 5- andre@0: (missing_rsa_exp_component+missing_rsa_mod_component); andre@0: andre@0: if ((have_exp == 0) || (have_component < 3)) { andre@0: /* nope, not enough to reconstruct the private key */ andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: fillPrivateKey = PR_TRUE; andre@0: } andre@0: /*verify the parameters for consistency*/ andre@0: rv = sftk_verifyRSAPrivateKey(object, fillPrivateKey); andre@0: if (rv != SECSuccess) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: andre@0: /* make sure Netscape DB attribute is set correctly */ andre@0: crv = sftk_Attribute2SSecItem(NULL, &mod, object, CKA_MODULUS); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_forceAttribute(object, CKA_NETSCAPE_DB, andre@0: sftk_item_expand(&mod)); andre@0: if (mod.data) PORT_Free(mod.data); andre@0: if (crv != CKR_OK) return crv; andre@0: andre@0: sign = CK_TRUE; andre@0: derive = CK_FALSE; andre@0: break; andre@0: case CKK_DSA: andre@0: if ( !sftk_hasAttribute(object, CKA_SUBPRIME)) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: sign = CK_TRUE; andre@0: derive = CK_FALSE; andre@0: /* fall through */ andre@0: case CKK_DH: andre@0: if ( !sftk_hasAttribute(object, CKA_PRIME)) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: if ( !sftk_hasAttribute(object, CKA_BASE)) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: if ( !sftk_hasAttribute(object, CKA_VALUE)) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: encrypt = CK_FALSE; andre@0: recover = CK_FALSE; andre@0: wrap = CK_FALSE; andre@0: break; andre@0: #ifndef NSS_DISABLE_ECC andre@0: case CKK_EC: andre@0: if ( !sftk_hasAttribute(object, CKA_EC_PARAMS)) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: if ( !sftk_hasAttribute(object, CKA_VALUE)) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: encrypt = CK_FALSE; andre@0: sign = CK_TRUE; andre@0: recover = CK_FALSE; andre@0: wrap = CK_FALSE; andre@0: break; andre@0: #endif /* NSS_DISABLE_ECC */ andre@0: case CKK_NSS_JPAKE_ROUND1: andre@0: if (!sftk_hasAttribute(object, CKA_PRIME) || andre@0: !sftk_hasAttribute(object, CKA_SUBPRIME) || andre@0: !sftk_hasAttribute(object, CKA_BASE)) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: /* fall through */ andre@0: case CKK_NSS_JPAKE_ROUND2: andre@0: /* CKA_NSS_JPAKE_SIGNERID and CKA_NSS_JPAKE_PEERID are checked in andre@0: the J-PAKE code. */ andre@0: encrypt = sign = recover = wrap = CK_FALSE; andre@0: derive = CK_TRUE; andre@0: createObjectInfo = PR_FALSE; andre@0: break; andre@0: default: andre@0: return CKR_ATTRIBUTE_VALUE_INVALID; andre@0: } andre@0: crv = sftk_defaultAttribute(object,CKA_SUBJECT,NULL,0); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_SENSITIVE,&cktrue,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_EXTRACTABLE,&cktrue,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_DECRYPT,&encrypt,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_SIGN,&sign,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_SIGN_RECOVER,&recover, andre@0: sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_UNWRAP,&wrap,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_DERIVE,&derive,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: /* the next two bits get modified only in the key gen and token cases */ andre@0: crv = sftk_forceAttribute(object,CKA_ALWAYS_SENSITIVE, andre@0: &ckfalse,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_forceAttribute(object,CKA_NEVER_EXTRACTABLE, andre@0: &ckfalse,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: andre@0: /* should we check the non-token RSA private keys? */ andre@0: andre@0: if (sftk_isTrue(object,CKA_TOKEN)) { andre@0: SFTKSlot *slot = session->slot; andre@0: SFTKDBHandle *keyHandle = sftk_getKeyDB(slot); andre@0: andre@0: if (keyHandle == NULL) { andre@0: return CKR_TOKEN_WRITE_PROTECTED; andre@0: } andre@0: andre@0: crv = sftkdb_write(keyHandle, object, &object->handle); andre@0: sftk_freeDB(keyHandle); andre@0: return crv; andre@0: } else if (createObjectInfo) { andre@0: object->objectInfo = sftk_mkPrivKey(object,key_type,&crv); andre@0: if (object->objectInfo == NULL) return crv; andre@0: object->infoFree = (SFTKFree) nsslowkey_DestroyPrivateKey; andre@0: } andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* forward declare the DES formating function for handleSecretKey */ andre@0: void sftk_FormatDESKey(unsigned char *key, int length); andre@0: andre@0: /* Validate secret key data, and set defaults */ andre@0: static CK_RV andre@0: validateSecretKey(SFTKSession *session, SFTKObject *object, andre@0: CK_KEY_TYPE key_type, PRBool isFIPS) andre@0: { andre@0: CK_RV crv; andre@0: CK_BBOOL cktrue = CK_TRUE; andre@0: CK_BBOOL ckfalse = CK_FALSE; andre@0: SFTKAttribute *attribute = NULL; andre@0: unsigned long requiredLen; andre@0: andre@0: crv = sftk_defaultAttribute(object,CKA_SENSITIVE, andre@0: isFIPS?&cktrue:&ckfalse,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_EXTRACTABLE, andre@0: &cktrue,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_ENCRYPT,&cktrue,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_DECRYPT,&cktrue,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_SIGN,&ckfalse,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_VERIFY,&ckfalse,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_WRAP,&cktrue,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_UNWRAP,&cktrue,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: andre@0: if ( !sftk_hasAttribute(object, CKA_VALUE)) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: /* the next two bits get modified only in the key gen and token cases */ andre@0: crv = sftk_forceAttribute(object,CKA_ALWAYS_SENSITIVE, andre@0: &ckfalse,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_forceAttribute(object,CKA_NEVER_EXTRACTABLE, andre@0: &ckfalse,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: andre@0: /* some types of keys have a value length */ andre@0: crv = CKR_OK; andre@0: switch (key_type) { andre@0: /* force CKA_VALUE_LEN to be set */ andre@0: case CKK_GENERIC_SECRET: andre@0: case CKK_RC2: andre@0: case CKK_RC4: andre@0: #if NSS_SOFTOKEN_DOES_RC5 andre@0: case CKK_RC5: andre@0: #endif andre@0: #ifdef NSS_SOFTOKEN_DOES_CAST andre@0: case CKK_CAST: andre@0: case CKK_CAST3: andre@0: case CKK_CAST5: andre@0: #endif andre@0: #if NSS_SOFTOKEN_DOES_IDEA andre@0: case CKK_IDEA: andre@0: #endif andre@0: attribute = sftk_FindAttribute(object,CKA_VALUE); andre@0: /* shouldn't happen */ andre@0: if (attribute == NULL) return CKR_TEMPLATE_INCOMPLETE; andre@0: crv = sftk_forceAttribute(object, CKA_VALUE_LEN, andre@0: &attribute->attrib.ulValueLen, sizeof(CK_ULONG)); andre@0: sftk_FreeAttribute(attribute); andre@0: break; andre@0: /* force the value to have the correct parity */ andre@0: case CKK_DES: andre@0: case CKK_DES2: andre@0: case CKK_DES3: andre@0: case CKK_CDMF: andre@0: attribute = sftk_FindAttribute(object,CKA_VALUE); andre@0: /* shouldn't happen */ andre@0: if (attribute == NULL) andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: requiredLen = sftk_MapKeySize(key_type); andre@0: if (attribute->attrib.ulValueLen != requiredLen) { andre@0: sftk_FreeAttribute(attribute); andre@0: return CKR_KEY_SIZE_RANGE; andre@0: } andre@0: sftk_FormatDESKey((unsigned char*)attribute->attrib.pValue, andre@0: attribute->attrib.ulValueLen); andre@0: sftk_FreeAttribute(attribute); andre@0: break; andre@0: case CKK_AES: andre@0: attribute = sftk_FindAttribute(object,CKA_VALUE); andre@0: /* shouldn't happen */ andre@0: if (attribute == NULL) andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: if (attribute->attrib.ulValueLen != 16 && andre@0: attribute->attrib.ulValueLen != 24 && andre@0: attribute->attrib.ulValueLen != 32) { andre@0: sftk_FreeAttribute(attribute); andre@0: return CKR_KEY_SIZE_RANGE; andre@0: } andre@0: crv = sftk_forceAttribute(object, CKA_VALUE_LEN, andre@0: &attribute->attrib.ulValueLen, sizeof(CK_ULONG)); andre@0: sftk_FreeAttribute(attribute); andre@0: break; andre@0: default: andre@0: break; andre@0: } andre@0: andre@0: return crv; andre@0: } andre@0: andre@0: /* andre@0: * check the consistancy and initialize a Secret Key Object andre@0: */ andre@0: static CK_RV andre@0: sftk_handleSecretKeyObject(SFTKSession *session,SFTKObject *object, andre@0: CK_KEY_TYPE key_type, PRBool isFIPS) andre@0: { andre@0: CK_RV crv; andre@0: andre@0: /* First validate and set defaults */ andre@0: crv = validateSecretKey(session, object, key_type, isFIPS); andre@0: if (crv != CKR_OK) goto loser; andre@0: andre@0: /* If the object is a TOKEN object, store in the database */ andre@0: if (sftk_isTrue(object,CKA_TOKEN)) { andre@0: SFTKSlot *slot = session->slot; andre@0: SFTKDBHandle *keyHandle = sftk_getKeyDB(slot); andre@0: CK_RV crv; andre@0: andre@0: if (keyHandle == NULL) { andre@0: return CKR_TOKEN_WRITE_PROTECTED; andre@0: } andre@0: andre@0: crv = sftkdb_write(keyHandle, object, &object->handle); andre@0: sftk_freeDB(keyHandle); andre@0: return crv; andre@0: } andre@0: andre@0: loser: andre@0: andre@0: return crv; andre@0: } andre@0: andre@0: /* andre@0: * check the consistancy and initialize a Key Object andre@0: */ andre@0: static CK_RV andre@0: sftk_handleKeyObject(SFTKSession *session, SFTKObject *object) andre@0: { andre@0: SFTKAttribute *attribute; andre@0: CK_KEY_TYPE key_type; andre@0: CK_BBOOL ckfalse = CK_FALSE; andre@0: CK_RV crv; andre@0: andre@0: /* verify the required fields */ andre@0: if ( !sftk_hasAttribute(object,CKA_KEY_TYPE) ) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: andre@0: /* now verify the common fields */ andre@0: crv = sftk_defaultAttribute(object,CKA_ID,NULL,0); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_START_DATE,NULL,0); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_END_DATE,NULL,0); andre@0: if (crv != CKR_OK) return crv; andre@0: /* CKA_DERIVE is common to all keys, but it's default value is andre@0: * key dependent */ andre@0: crv = sftk_defaultAttribute(object,CKA_LOCAL,&ckfalse,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: andre@0: /* get the key type */ andre@0: attribute = sftk_FindAttribute(object,CKA_KEY_TYPE); andre@0: if (!attribute) { andre@0: return CKR_ATTRIBUTE_VALUE_INVALID; andre@0: } andre@0: key_type = *(CK_KEY_TYPE *)attribute->attrib.pValue; andre@0: sftk_FreeAttribute(attribute); andre@0: andre@0: switch (object->objclass) { andre@0: case CKO_PUBLIC_KEY: andre@0: return sftk_handlePublicKeyObject(session,object,key_type); andre@0: case CKO_PRIVATE_KEY: andre@0: return sftk_handlePrivateKeyObject(session,object,key_type); andre@0: case CKO_SECRET_KEY: andre@0: /* make sure the required fields exist */ andre@0: return sftk_handleSecretKeyObject(session,object,key_type, andre@0: (PRBool)(session->slot->slotID == FIPS_SLOT_ID)); andre@0: default: andre@0: break; andre@0: } andre@0: return CKR_ATTRIBUTE_VALUE_INVALID; andre@0: } andre@0: andre@0: /* andre@0: * check the consistancy and Verify a DSA Parameter Object andre@0: */ andre@0: static CK_RV andre@0: sftk_handleDSAParameterObject(SFTKSession *session, SFTKObject *object) andre@0: { andre@0: SFTKAttribute *primeAttr = NULL; andre@0: SFTKAttribute *subPrimeAttr = NULL; andre@0: SFTKAttribute *baseAttr = NULL; andre@0: SFTKAttribute *seedAttr = NULL; andre@0: SFTKAttribute *hAttr = NULL; andre@0: SFTKAttribute *attribute; andre@0: CK_RV crv = CKR_TEMPLATE_INCOMPLETE; andre@0: PQGParams params; andre@0: PQGVerify vfy, *verify = NULL; andre@0: SECStatus result,rv; andre@0: /* This bool keeps track of whether or not we need verify parameters. andre@0: * If a P, Q and G or supplied, we dont' need verify parameters, as we andre@0: * have PQ and G. andre@0: * - If G is not supplied, the presumption is that we want to andre@0: * verify P and Q only. andre@0: * - If counter is supplied, it is presumed we want to verify PQ because andre@0: * the counter is only used in verification. andre@0: * - If H is supplied, is is presumed we want to verify G because H is andre@0: * only used to verify G. andre@0: * - Any verification step must have the SEED (counter or H could be andre@0: * missing depending on exactly what we want to verify). If SEED is supplied, andre@0: * the code just goes ahead and runs verify (other errors are parameter andre@0: * errors are detected by the PQG_VerifyParams function). If SEED is not andre@0: * supplied, but we determined that we are trying to verify (because needVfy andre@0: * is set, go ahead and return CKR_TEMPLATE_INCOMPLETE. andre@0: */ andre@0: PRBool needVfy = PR_FALSE; andre@0: andre@0: primeAttr = sftk_FindAttribute(object,CKA_PRIME); andre@0: if (primeAttr == NULL) goto loser; andre@0: params.prime.data = primeAttr->attrib.pValue; andre@0: params.prime.len = primeAttr->attrib.ulValueLen; andre@0: andre@0: subPrimeAttr = sftk_FindAttribute(object,CKA_SUBPRIME); andre@0: if (subPrimeAttr == NULL) goto loser; andre@0: params.subPrime.data = subPrimeAttr->attrib.pValue; andre@0: params.subPrime.len = subPrimeAttr->attrib.ulValueLen; andre@0: andre@0: baseAttr = sftk_FindAttribute(object,CKA_BASE); andre@0: if (baseAttr != NULL) { andre@0: params.base.data = baseAttr->attrib.pValue; andre@0: params.base.len = baseAttr->attrib.ulValueLen; andre@0: } else { andre@0: params.base.data = NULL; andre@0: params.base.len = 0; andre@0: needVfy = PR_TRUE; /* presumably only including PQ so we can verify andre@0: * them. */ andre@0: } andre@0: andre@0: attribute = sftk_FindAttribute(object, CKA_NETSCAPE_PQG_COUNTER); andre@0: if (attribute != NULL) { andre@0: vfy.counter = *(CK_ULONG *) attribute->attrib.pValue; andre@0: sftk_FreeAttribute(attribute); andre@0: needVfy = PR_TRUE; /* included a count so we can verify PQ */ andre@0: } else { andre@0: vfy.counter = -1; andre@0: } andre@0: andre@0: hAttr = sftk_FindAttribute(object, CKA_NETSCAPE_PQG_H); andre@0: if (hAttr != NULL) { andre@0: vfy.h.data = hAttr->attrib.pValue; andre@0: vfy.h.len = hAttr->attrib.ulValueLen; andre@0: needVfy = PR_TRUE; /* included H so we can verify G */ andre@0: } else { andre@0: vfy.h.data = NULL; andre@0: vfy.h.len = 0; andre@0: } andre@0: seedAttr = sftk_FindAttribute(object, CKA_NETSCAPE_PQG_SEED); andre@0: if (seedAttr != NULL) { andre@0: vfy.seed.data = seedAttr->attrib.pValue; andre@0: vfy.seed.len = seedAttr->attrib.ulValueLen; andre@0: andre@0: verify = &vfy; andre@0: } else if (needVfy) { andre@0: goto loser; /* Verify always needs seed, if we need verify and not seed andre@0: * then fail */ andre@0: } andre@0: andre@0: crv = CKR_FUNCTION_FAILED; andre@0: rv = PQG_VerifyParams(¶ms,verify,&result); andre@0: if (rv == SECSuccess) { andre@0: crv = (result== SECSuccess) ? CKR_OK : CKR_ATTRIBUTE_VALUE_INVALID; andre@0: } andre@0: andre@0: loser: andre@0: if (hAttr) sftk_FreeAttribute(hAttr); andre@0: if (seedAttr) sftk_FreeAttribute(seedAttr); andre@0: if (baseAttr) sftk_FreeAttribute(baseAttr); andre@0: if (subPrimeAttr) sftk_FreeAttribute(subPrimeAttr); andre@0: if (primeAttr) sftk_FreeAttribute(primeAttr); andre@0: andre@0: return crv; andre@0: } andre@0: andre@0: /* andre@0: * check the consistancy and initialize a Key Parameter Object andre@0: */ andre@0: static CK_RV andre@0: sftk_handleKeyParameterObject(SFTKSession *session, SFTKObject *object) andre@0: { andre@0: SFTKAttribute *attribute; andre@0: CK_KEY_TYPE key_type; andre@0: CK_BBOOL ckfalse = CK_FALSE; andre@0: CK_RV crv; andre@0: andre@0: /* verify the required fields */ andre@0: if ( !sftk_hasAttribute(object,CKA_KEY_TYPE) ) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: andre@0: /* now verify the common fields */ andre@0: crv = sftk_defaultAttribute(object,CKA_LOCAL,&ckfalse,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: andre@0: /* get the key type */ andre@0: attribute = sftk_FindAttribute(object,CKA_KEY_TYPE); andre@0: if (!attribute) { andre@0: return CKR_ATTRIBUTE_VALUE_INVALID; andre@0: } andre@0: key_type = *(CK_KEY_TYPE *)attribute->attrib.pValue; andre@0: sftk_FreeAttribute(attribute); andre@0: andre@0: switch (key_type) { andre@0: case CKK_DSA: andre@0: return sftk_handleDSAParameterObject(session,object); andre@0: andre@0: default: andre@0: break; andre@0: } andre@0: return CKR_KEY_TYPE_INCONSISTENT; andre@0: } andre@0: andre@0: /* andre@0: * Handle Object does all the object consistancy checks, automatic attribute andre@0: * generation, attribute defaulting, etc. If handleObject succeeds, the object andre@0: * will be assigned an object handle, and the object installed in the session andre@0: * or stored in the DB. andre@0: */ andre@0: CK_RV andre@0: sftk_handleObject(SFTKObject *object, SFTKSession *session) andre@0: { andre@0: SFTKSlot *slot = session->slot; andre@0: SFTKAttribute *attribute; andre@0: SFTKObject *duplicateObject = NULL; andre@0: CK_OBJECT_HANDLE handle; andre@0: CK_BBOOL ckfalse = CK_FALSE; andre@0: CK_BBOOL cktrue = CK_TRUE; andre@0: CK_RV crv; andre@0: andre@0: /* make sure all the base object types are defined. If not set the andre@0: * defaults */ andre@0: crv = sftk_defaultAttribute(object,CKA_TOKEN,&ckfalse,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_PRIVATE,&ckfalse,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_LABEL,NULL,0); andre@0: if (crv != CKR_OK) return crv; andre@0: crv = sftk_defaultAttribute(object,CKA_MODIFIABLE,&cktrue,sizeof(CK_BBOOL)); andre@0: if (crv != CKR_OK) return crv; andre@0: andre@0: /* don't create a private object if we aren't logged in */ andre@0: if ((!slot->isLoggedIn) && (slot->needLogin) && andre@0: (sftk_isTrue(object,CKA_PRIVATE))) { andre@0: return CKR_USER_NOT_LOGGED_IN; andre@0: } andre@0: andre@0: andre@0: if (((session->info.flags & CKF_RW_SESSION) == 0) && andre@0: (sftk_isTrue(object,CKA_TOKEN))) { andre@0: return CKR_SESSION_READ_ONLY; andre@0: } andre@0: andre@0: /* Assign a unique SESSION object handle to every new object, andre@0: * whether it is a session object or a token object. andre@0: * At this point, all new objects are structured as session objects. andre@0: * Objects with the CKA_TOKEN attribute true will be turned into andre@0: * token objects and will have a token object handle assigned to andre@0: * them by a call to sftk_mkHandle in the handler for each object andre@0: * class, invoked below. andre@0: * andre@0: * It may be helpful to note/remember that andre@0: * sftk_narrowToXxxObject uses sftk_isToken, andre@0: * sftk_isToken examines the sign bit of the object's handle, but andre@0: * sftk_isTrue(...,CKA_TOKEN) examines the CKA_TOKEN attribute. andre@0: */ andre@0: do { andre@0: PRUint32 wrappedAround; andre@0: andre@0: duplicateObject = NULL; andre@0: PZ_Lock(slot->objectLock); andre@0: wrappedAround = slot->sessionObjectHandleCount & SFTK_TOKEN_MASK; andre@0: handle = slot->sessionObjectHandleCount & ~SFTK_TOKEN_MASK; andre@0: if (!handle) /* don't allow zero handle */ andre@0: handle = minSessionObjectHandle; andre@0: slot->sessionObjectHandleCount = (handle + 1U) | wrappedAround; andre@0: /* Is there already a session object with this handle? */ andre@0: if (wrappedAround) { andre@0: sftkqueue_find(duplicateObject, handle, slot->sessObjHashTable, \ andre@0: slot->sessObjHashSize); andre@0: } andre@0: PZ_Unlock(slot->objectLock); andre@0: } while (duplicateObject != NULL); andre@0: object->handle = handle; andre@0: andre@0: /* get the object class */ andre@0: attribute = sftk_FindAttribute(object,CKA_CLASS); andre@0: if (attribute == NULL) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: object->objclass = *(CK_OBJECT_CLASS *)attribute->attrib.pValue; andre@0: sftk_FreeAttribute(attribute); andre@0: andre@0: /* Now handle the specific object class. andre@0: * At this point, all objects are session objects, and the session andre@0: * number must be passed to the object class handlers. andre@0: */ andre@0: switch (object->objclass) { andre@0: case CKO_DATA: andre@0: crv = sftk_handleDataObject(session,object); andre@0: break; andre@0: case CKO_CERTIFICATE: andre@0: crv = sftk_handleCertObject(session,object); andre@0: break; andre@0: case CKO_NETSCAPE_TRUST: andre@0: crv = sftk_handleTrustObject(session,object); andre@0: break; andre@0: case CKO_NETSCAPE_CRL: andre@0: crv = sftk_handleCrlObject(session,object); andre@0: break; andre@0: case CKO_NETSCAPE_SMIME: andre@0: crv = sftk_handleSMimeObject(session,object); andre@0: break; andre@0: case CKO_PRIVATE_KEY: andre@0: case CKO_PUBLIC_KEY: andre@0: case CKO_SECRET_KEY: andre@0: crv = sftk_handleKeyObject(session,object); andre@0: break; andre@0: case CKO_KG_PARAMETERS: andre@0: crv = sftk_handleKeyParameterObject(session,object); andre@0: break; andre@0: default: andre@0: crv = CKR_ATTRIBUTE_VALUE_INVALID; andre@0: break; andre@0: } andre@0: andre@0: /* can't fail from here on out unless the pk_handlXXX functions have andre@0: * failed the request */ andre@0: if (crv != CKR_OK) { andre@0: return crv; andre@0: } andre@0: andre@0: /* Now link the object into the slot and session structures. andre@0: * If the object has a true CKA_TOKEN attribute, the above object andre@0: * class handlers will have set the sign bit in the object handle, andre@0: * causing the following test to be true. andre@0: */ andre@0: if (sftk_isToken(object->handle)) { andre@0: sftk_convertSessionToToken(object); andre@0: } else { andre@0: object->slot = slot; andre@0: sftk_AddObject(session,object); andre@0: } andre@0: andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* andre@0: * ******************** Public Key Utilities *************************** andre@0: */ andre@0: /* Generate a low public key structure from an object */ andre@0: NSSLOWKEYPublicKey *sftk_GetPubKey(SFTKObject *object,CK_KEY_TYPE key_type, andre@0: CK_RV *crvp) andre@0: { andre@0: NSSLOWKEYPublicKey *pubKey; andre@0: PLArenaPool *arena; andre@0: CK_RV crv; andre@0: andre@0: if (object->objclass != CKO_PUBLIC_KEY) { andre@0: *crvp = CKR_KEY_TYPE_INCONSISTENT; andre@0: return NULL; andre@0: } andre@0: andre@0: if (sftk_isToken(object->handle)) { andre@0: /* ferret out the token object handle */ andre@0: } andre@0: andre@0: /* If we already have a key, use it */ andre@0: if (object->objectInfo) { andre@0: *crvp = CKR_OK; andre@0: return (NSSLOWKEYPublicKey *)object->objectInfo; andre@0: } andre@0: andre@0: /* allocate the structure */ andre@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); andre@0: if (arena == NULL) { andre@0: *crvp = CKR_HOST_MEMORY; andre@0: return NULL; andre@0: } andre@0: andre@0: pubKey = (NSSLOWKEYPublicKey *) andre@0: PORT_ArenaAlloc(arena,sizeof(NSSLOWKEYPublicKey)); andre@0: if (pubKey == NULL) { andre@0: PORT_FreeArena(arena,PR_FALSE); andre@0: *crvp = CKR_HOST_MEMORY; andre@0: return NULL; andre@0: } andre@0: andre@0: /* fill in the structure */ andre@0: pubKey->arena = arena; andre@0: switch (key_type) { andre@0: case CKK_RSA: andre@0: pubKey->keyType = NSSLOWKEYRSAKey; andre@0: crv = sftk_Attribute2SSecItem(arena,&pubKey->u.rsa.modulus, andre@0: object,CKA_MODULUS); andre@0: if (crv != CKR_OK) break; andre@0: crv = sftk_Attribute2SSecItem(arena,&pubKey->u.rsa.publicExponent, andre@0: object,CKA_PUBLIC_EXPONENT); andre@0: break; andre@0: case CKK_DSA: andre@0: pubKey->keyType = NSSLOWKEYDSAKey; andre@0: crv = sftk_Attribute2SSecItem(arena,&pubKey->u.dsa.params.prime, andre@0: object,CKA_PRIME); andre@0: if (crv != CKR_OK) break; andre@0: crv = sftk_Attribute2SSecItem(arena,&pubKey->u.dsa.params.subPrime, andre@0: object,CKA_SUBPRIME); andre@0: if (crv != CKR_OK) break; andre@0: crv = sftk_Attribute2SSecItem(arena,&pubKey->u.dsa.params.base, andre@0: object,CKA_BASE); andre@0: if (crv != CKR_OK) break; andre@0: crv = sftk_Attribute2SSecItem(arena,&pubKey->u.dsa.publicValue, andre@0: object,CKA_VALUE); andre@0: break; andre@0: case CKK_DH: andre@0: pubKey->keyType = NSSLOWKEYDHKey; andre@0: crv = sftk_Attribute2SSecItem(arena,&pubKey->u.dh.prime, andre@0: object,CKA_PRIME); andre@0: if (crv != CKR_OK) break; andre@0: crv = sftk_Attribute2SSecItem(arena,&pubKey->u.dh.base, andre@0: object,CKA_BASE); andre@0: if (crv != CKR_OK) break; andre@0: crv = sftk_Attribute2SSecItem(arena,&pubKey->u.dh.publicValue, andre@0: object,CKA_VALUE); andre@0: break; andre@0: #ifndef NSS_DISABLE_ECC andre@0: case CKK_EC: andre@0: pubKey->keyType = NSSLOWKEYECKey; andre@0: crv = sftk_Attribute2SSecItem(arena, andre@0: &pubKey->u.ec.ecParams.DEREncoding, andre@0: object,CKA_EC_PARAMS); andre@0: if (crv != CKR_OK) break; andre@0: andre@0: /* Fill out the rest of the ecParams structure andre@0: * based on the encoded params andre@0: */ andre@0: if (EC_FillParams(arena, &pubKey->u.ec.ecParams.DEREncoding, andre@0: &pubKey->u.ec.ecParams) != SECSuccess) { andre@0: crv = CKR_DOMAIN_PARAMS_INVALID; andre@0: break; andre@0: } andre@0: andre@0: crv = sftk_Attribute2SSecItem(arena,&pubKey->u.ec.publicValue, andre@0: object,CKA_EC_POINT); andre@0: if (crv == CKR_OK) { andre@0: int keyLen,curveLen; andre@0: andre@0: curveLen = (pubKey->u.ec.ecParams.fieldID.size +7)/8; andre@0: keyLen = (2*curveLen)+1; andre@0: andre@0: /* special note: We can't just use the first byte to determine andre@0: * between these 2 cases because both EC_POINT_FORM_UNCOMPRESSED andre@0: * and SEC_ASN1_OCTET_STRING are 0x04 */ andre@0: andre@0: /* handle the non-DER encoded case (UNCOMPRESSED only) */ andre@0: if (pubKey->u.ec.publicValue.data[0] == EC_POINT_FORM_UNCOMPRESSED andre@0: && pubKey->u.ec.publicValue.len == keyLen) { andre@0: break; /* key was not DER encoded, no need to unwrap */ andre@0: } andre@0: andre@0: /* if we ever support compressed, handle it here */ andre@0: andre@0: /* handle the encoded case */ andre@0: if ((pubKey->u.ec.publicValue.data[0] == SEC_ASN1_OCTET_STRING) andre@0: && pubKey->u.ec.publicValue.len > keyLen) { andre@0: SECItem publicValue; andre@0: SECStatus rv; andre@0: andre@0: rv = SEC_QuickDERDecodeItem(arena, &publicValue, andre@0: SEC_ASN1_GET(SEC_OctetStringTemplate), andre@0: &pubKey->u.ec.publicValue); andre@0: /* nope, didn't decode correctly */ andre@0: if ((rv != SECSuccess) andre@0: || (publicValue.data[0] != EC_POINT_FORM_UNCOMPRESSED) andre@0: || (publicValue.len != keyLen)) { andre@0: crv = CKR_ATTRIBUTE_VALUE_INVALID; andre@0: break; andre@0: } andre@0: /* replace our previous with the decoded key */ andre@0: pubKey->u.ec.publicValue = publicValue; andre@0: break; andre@0: } andre@0: crv = CKR_ATTRIBUTE_VALUE_INVALID; andre@0: } andre@0: break; andre@0: #endif /* NSS_DISABLE_ECC */ andre@0: default: andre@0: crv = CKR_KEY_TYPE_INCONSISTENT; andre@0: break; andre@0: } andre@0: *crvp = crv; andre@0: if (crv != CKR_OK) { andre@0: PORT_FreeArena(arena,PR_FALSE); andre@0: return NULL; andre@0: } andre@0: andre@0: object->objectInfo = pubKey; andre@0: object->infoFree = (SFTKFree) nsslowkey_DestroyPublicKey; andre@0: return pubKey; andre@0: } andre@0: andre@0: /* make a private key from a verified object */ andre@0: static NSSLOWKEYPrivateKey * andre@0: sftk_mkPrivKey(SFTKObject *object, CK_KEY_TYPE key_type, CK_RV *crvp) andre@0: { andre@0: NSSLOWKEYPrivateKey *privKey; andre@0: SFTKItemTemplate itemTemplate[SFTK_MAX_ITEM_TEMPLATE]; andre@0: int itemTemplateCount = 0; andre@0: PLArenaPool *arena; andre@0: CK_RV crv = CKR_OK; andre@0: SECStatus rv; andre@0: andre@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); andre@0: if (arena == NULL) { andre@0: *crvp = CKR_HOST_MEMORY; andre@0: return NULL; andre@0: } andre@0: andre@0: privKey = (NSSLOWKEYPrivateKey *) andre@0: PORT_ArenaZAlloc(arena,sizeof(NSSLOWKEYPrivateKey)); andre@0: if (privKey == NULL) { andre@0: PORT_FreeArena(arena,PR_FALSE); andre@0: *crvp = CKR_HOST_MEMORY; andre@0: return NULL; andre@0: } andre@0: andre@0: /* in future this would be a switch on key_type */ andre@0: privKey->arena = arena; andre@0: switch (key_type) { andre@0: case CKK_RSA: andre@0: privKey->keyType = NSSLOWKEYRSAKey; andre@0: andre@0: SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount, andre@0: &privKey->u.rsa.modulus,CKA_MODULUS); andre@0: itemTemplateCount++; andre@0: SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount, andre@0: &privKey->u.rsa.publicExponent, CKA_PUBLIC_EXPONENT); andre@0: itemTemplateCount++; andre@0: SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount, andre@0: &privKey->u.rsa.privateExponent, CKA_PRIVATE_EXPONENT); andre@0: itemTemplateCount++; andre@0: SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount, andre@0: &privKey->u.rsa.prime1, CKA_PRIME_1); andre@0: itemTemplateCount++; andre@0: SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount, andre@0: &privKey->u.rsa.prime2, CKA_PRIME_2); andre@0: itemTemplateCount++; andre@0: SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount, andre@0: &privKey->u.rsa.exponent1, CKA_EXPONENT_1); andre@0: itemTemplateCount++; andre@0: SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount, andre@0: &privKey->u.rsa.exponent2, CKA_EXPONENT_2); andre@0: itemTemplateCount++; andre@0: SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount, andre@0: &privKey->u.rsa.coefficient, CKA_COEFFICIENT); andre@0: itemTemplateCount++; andre@0: rv = DER_SetUInteger(privKey->arena, &privKey->u.rsa.version, andre@0: NSSLOWKEY_PRIVATE_KEY_INFO_VERSION); andre@0: if (rv != SECSuccess) crv = CKR_HOST_MEMORY; andre@0: break; andre@0: andre@0: case CKK_DSA: andre@0: privKey->keyType = NSSLOWKEYDSAKey; andre@0: SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount, andre@0: &privKey->u.dsa.params.prime, CKA_PRIME); andre@0: itemTemplateCount++; andre@0: SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount, andre@0: &privKey->u.dsa.params.subPrime, CKA_SUBPRIME); andre@0: itemTemplateCount++; andre@0: SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount, andre@0: &privKey->u.dsa.params.base, CKA_BASE); andre@0: itemTemplateCount++; andre@0: SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount, andre@0: &privKey->u.dsa.privateValue, CKA_VALUE); andre@0: itemTemplateCount++; andre@0: /* privKey was zero'd so public value is already set to NULL, 0 andre@0: * if we don't set it explicitly */ andre@0: break; andre@0: andre@0: case CKK_DH: andre@0: privKey->keyType = NSSLOWKEYDHKey; andre@0: SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount, andre@0: &privKey->u.dh.prime, CKA_PRIME); andre@0: itemTemplateCount++; andre@0: SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount, andre@0: &privKey->u.dh.base, CKA_BASE); andre@0: itemTemplateCount++; andre@0: SFTK_SET_ITEM_TEMPLATE(itemTemplate, itemTemplateCount, andre@0: &privKey->u.dh.privateValue, CKA_VALUE); andre@0: itemTemplateCount++; andre@0: /* privKey was zero'd so public value is already set to NULL, 0 andre@0: * if we don't set it explicitly */ andre@0: break; andre@0: andre@0: #ifndef NSS_DISABLE_ECC andre@0: case CKK_EC: andre@0: privKey->keyType = NSSLOWKEYECKey; andre@0: crv = sftk_Attribute2SSecItem(arena, andre@0: &privKey->u.ec.ecParams.DEREncoding, andre@0: object,CKA_EC_PARAMS); andre@0: if (crv != CKR_OK) break; andre@0: andre@0: /* Fill out the rest of the ecParams structure andre@0: * based on the encoded params andre@0: */ andre@0: if (EC_FillParams(arena, &privKey->u.ec.ecParams.DEREncoding, andre@0: &privKey->u.ec.ecParams) != SECSuccess) { andre@0: crv = CKR_DOMAIN_PARAMS_INVALID; andre@0: break; andre@0: } andre@0: crv = sftk_Attribute2SSecItem(arena,&privKey->u.ec.privateValue, andre@0: object,CKA_VALUE); andre@0: if (crv != CKR_OK) break; andre@0: andre@0: if (sftk_hasAttribute(object, CKA_NETSCAPE_DB)) { andre@0: crv = sftk_Attribute2SSecItem(arena, &privKey->u.ec.publicValue, andre@0: object, CKA_NETSCAPE_DB); andre@0: if (crv != CKR_OK) break; andre@0: /* privKey was zero'd so public value is already set to NULL, 0 andre@0: * if we don't set it explicitly */ andre@0: } andre@0: rv = DER_SetUInteger(privKey->arena, &privKey->u.ec.version, andre@0: NSSLOWKEY_EC_PRIVATE_KEY_VERSION); andre@0: if (rv != SECSuccess) { andre@0: crv = CKR_HOST_MEMORY; andre@0: /* The following ifdef is needed for Linux arm distros and andre@0: * Android as gcc 4.6 has a bug when targeting arm (but not andre@0: * thumb). The bug has been fixed in gcc 4.7. andre@0: * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56561 andre@0: */ andre@0: #if defined (__arm__) && !defined(__thumb__) && defined (__GNUC__) andre@0: *crvp = CKR_HOST_MEMORY; andre@0: break; andre@0: #endif andre@0: } andre@0: break; andre@0: #endif /* NSS_DISABLE_ECC */ andre@0: andre@0: default: andre@0: crv = CKR_KEY_TYPE_INCONSISTENT; andre@0: break; andre@0: } andre@0: if (crv == CKR_OK && itemTemplateCount != 0) { andre@0: PORT_Assert(itemTemplateCount > 0); andre@0: PORT_Assert(itemTemplateCount <= SFTK_MAX_ITEM_TEMPLATE); andre@0: crv = sftk_MultipleAttribute2SecItem(arena, object, itemTemplate, andre@0: itemTemplateCount); andre@0: } andre@0: *crvp = crv; andre@0: if (crv != CKR_OK) { andre@0: PORT_FreeArena(arena,PR_FALSE); andre@0: return NULL; andre@0: } andre@0: return privKey; andre@0: } andre@0: andre@0: /* andre@0: * If a partial RSA private key is present, fill in the rest if necessary, andre@0: * and then verify the parameters are well-formed andre@0: */ andre@0: static SECStatus andre@0: sftk_verifyRSAPrivateKey(SFTKObject *object, PRBool fillIfNeeded) andre@0: { andre@0: RSAPrivateKey tmpKey = { 0 }; andre@0: SFTKAttribute *modulus = NULL; andre@0: SFTKAttribute *prime1 = NULL; andre@0: SFTKAttribute *prime2 = NULL; andre@0: SFTKAttribute *privateExponent = NULL; andre@0: SFTKAttribute *publicExponent = NULL; andre@0: SFTKAttribute *exponent1 = NULL; andre@0: SFTKAttribute *exponent2 = NULL; andre@0: SFTKAttribute *coefficient = NULL; andre@0: SECStatus rv; andre@0: CK_RV crv; andre@0: andre@0: /* first fill in the components that we have. Populate only uses andre@0: * the non-crt components, so only fill those in */ andre@0: tmpKey.arena = NULL; andre@0: modulus = sftk_FindAttribute(object, CKA_MODULUS); andre@0: if (modulus) { andre@0: tmpKey.modulus.data = modulus->attrib.pValue; andre@0: tmpKey.modulus.len = modulus->attrib.ulValueLen; andre@0: } andre@0: prime1 = sftk_FindAttribute(object, CKA_PRIME_1); andre@0: if (prime1) { andre@0: tmpKey.prime1.data = prime1->attrib.pValue; andre@0: tmpKey.prime1.len = prime1->attrib.ulValueLen; andre@0: } andre@0: prime2 = sftk_FindAttribute(object, CKA_PRIME_2); andre@0: if (prime2) { andre@0: tmpKey.prime2.data = prime2->attrib.pValue; andre@0: tmpKey.prime2.len = prime2->attrib.ulValueLen; andre@0: } andre@0: privateExponent = sftk_FindAttribute(object, CKA_PRIVATE_EXPONENT); andre@0: if (privateExponent) { andre@0: tmpKey.privateExponent.data = privateExponent->attrib.pValue; andre@0: tmpKey.privateExponent.len = privateExponent->attrib.ulValueLen; andre@0: } andre@0: publicExponent = sftk_FindAttribute(object, CKA_PUBLIC_EXPONENT); andre@0: if (publicExponent) { andre@0: tmpKey.publicExponent.data = publicExponent->attrib.pValue; andre@0: tmpKey.publicExponent.len = publicExponent->attrib.ulValueLen; andre@0: } andre@0: exponent1 = sftk_FindAttribute(object, CKA_EXPONENT_1); andre@0: if (exponent1) { andre@0: tmpKey.exponent1.data = exponent1->attrib.pValue; andre@0: tmpKey.exponent1.len = exponent1->attrib.ulValueLen; andre@0: } andre@0: exponent2 = sftk_FindAttribute(object, CKA_EXPONENT_2); andre@0: if (exponent2) { andre@0: tmpKey.exponent2.data = exponent2->attrib.pValue; andre@0: tmpKey.exponent2.len = exponent2->attrib.ulValueLen; andre@0: } andre@0: coefficient = sftk_FindAttribute(object, CKA_COEFFICIENT); andre@0: if (coefficient) { andre@0: tmpKey.coefficient.data = coefficient->attrib.pValue; andre@0: tmpKey.coefficient.len = coefficient->attrib.ulValueLen; andre@0: } andre@0: andre@0: if (fillIfNeeded) { andre@0: /* andre@0: * populate requires one exponent plus 2 other components to work. andre@0: * we expected our caller to check that first. If that didn't happen, andre@0: * populate will simply return an error here. andre@0: */ andre@0: rv = RSA_PopulatePrivateKey(&tmpKey); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: } andre@0: rv = RSA_PrivateKeyCheck(&tmpKey); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: /* now that we have a fully populated key, set all our attribute values */ andre@0: rv = SECFailure; andre@0: if (!modulus || modulus->attrib.pValue != tmpKey.modulus.data) { andre@0: crv = sftk_forceAttribute(object,CKA_MODULUS, andre@0: sftk_item_expand(&tmpKey.modulus)); andre@0: if (crv != CKR_OK) goto loser; andre@0: } andre@0: if (!publicExponent || andre@0: publicExponent->attrib.pValue != tmpKey.publicExponent.data) { andre@0: crv = sftk_forceAttribute(object, CKA_PUBLIC_EXPONENT, andre@0: sftk_item_expand(&tmpKey.publicExponent)); andre@0: if (crv != CKR_OK) goto loser; andre@0: } andre@0: if (!privateExponent || andre@0: privateExponent->attrib.pValue != tmpKey.privateExponent.data) { andre@0: crv = sftk_forceAttribute(object, CKA_PRIVATE_EXPONENT, andre@0: sftk_item_expand(&tmpKey.privateExponent)); andre@0: if (crv != CKR_OK) goto loser; andre@0: } andre@0: if (!prime1 || prime1->attrib.pValue != tmpKey.prime1.data) { andre@0: crv = sftk_forceAttribute(object, CKA_PRIME_1, andre@0: sftk_item_expand(&tmpKey.prime1)); andre@0: if (crv != CKR_OK) goto loser; andre@0: } andre@0: if (!prime2 || prime2->attrib.pValue != tmpKey.prime2.data) { andre@0: crv = sftk_forceAttribute(object, CKA_PRIME_2, andre@0: sftk_item_expand(&tmpKey.prime2)); andre@0: if (crv != CKR_OK) goto loser; andre@0: } andre@0: if (!exponent1 || exponent1->attrib.pValue != tmpKey.exponent1.data) { andre@0: crv = sftk_forceAttribute(object, CKA_EXPONENT_1, andre@0: sftk_item_expand(&tmpKey.exponent1)); andre@0: if (crv != CKR_OK) goto loser; andre@0: } andre@0: if (!exponent2 || exponent2->attrib.pValue != tmpKey.exponent2.data) { andre@0: crv = sftk_forceAttribute(object, CKA_EXPONENT_2, andre@0: sftk_item_expand(&tmpKey.exponent2)); andre@0: if (crv != CKR_OK) goto loser; andre@0: } andre@0: if (!coefficient || coefficient->attrib.pValue != tmpKey.coefficient.data) { andre@0: crv = sftk_forceAttribute(object, CKA_COEFFICIENT, andre@0: sftk_item_expand(&tmpKey.coefficient)); andre@0: if (crv != CKR_OK) goto loser; andre@0: } andre@0: rv = SECSuccess; andre@0: andre@0: /* we're done (one way or the other), clean up all our stuff */ andre@0: loser: andre@0: if (tmpKey.arena) { andre@0: PORT_FreeArena(tmpKey.arena,PR_TRUE); andre@0: } andre@0: if (modulus) { andre@0: sftk_FreeAttribute(modulus); andre@0: } andre@0: if (prime1) { andre@0: sftk_FreeAttribute(prime1); andre@0: } andre@0: if (prime2) { andre@0: sftk_FreeAttribute(prime2); andre@0: } andre@0: if (privateExponent) { andre@0: sftk_FreeAttribute(privateExponent); andre@0: } andre@0: if (publicExponent) { andre@0: sftk_FreeAttribute(publicExponent); andre@0: } andre@0: if (exponent1) { andre@0: sftk_FreeAttribute(exponent1); andre@0: } andre@0: if (exponent2) { andre@0: sftk_FreeAttribute(exponent2); andre@0: } andre@0: if (coefficient) { andre@0: sftk_FreeAttribute(coefficient); andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: /* Generate a low private key structure from an object */ andre@0: NSSLOWKEYPrivateKey * andre@0: sftk_GetPrivKey(SFTKObject *object,CK_KEY_TYPE key_type, CK_RV *crvp) andre@0: { andre@0: NSSLOWKEYPrivateKey *priv = NULL; andre@0: andre@0: if (object->objclass != CKO_PRIVATE_KEY) { andre@0: *crvp = CKR_KEY_TYPE_INCONSISTENT; andre@0: return NULL; andre@0: } andre@0: if (object->objectInfo) { andre@0: *crvp = CKR_OK; andre@0: return (NSSLOWKEYPrivateKey *)object->objectInfo; andre@0: } andre@0: andre@0: priv = sftk_mkPrivKey(object, key_type, crvp); andre@0: object->objectInfo = priv; andre@0: object->infoFree = (SFTKFree) nsslowkey_DestroyPrivateKey; andre@0: return priv; andre@0: } andre@0: andre@0: /* andre@0: **************************** Symetric Key utils ************************ andre@0: */ andre@0: /* andre@0: * set the DES key with parity bits correctly andre@0: */ andre@0: void andre@0: sftk_FormatDESKey(unsigned char *key, int length) andre@0: { andre@0: int i; andre@0: andre@0: /* format the des key */ andre@0: for (i=0; i < length; i++) { andre@0: key[i] = parityTable[key[i]>>1]; andre@0: } andre@0: } andre@0: andre@0: /* andre@0: * check a des key (des2 or des3 subkey) for weak keys. andre@0: */ andre@0: PRBool andre@0: sftk_CheckDESKey(unsigned char *key) andre@0: { andre@0: int i; andre@0: andre@0: /* format the des key with parity */ andre@0: sftk_FormatDESKey(key, 8); andre@0: andre@0: for (i=0; i < sftk_desWeakTableSize; i++) { andre@0: if (PORT_Memcmp(key,sftk_desWeakTable[i],8) == 0) { andre@0: return PR_TRUE; andre@0: } andre@0: } andre@0: return PR_FALSE; andre@0: } andre@0: andre@0: /* andre@0: * check if a des or triple des key is weak. andre@0: */ andre@0: PRBool andre@0: sftk_IsWeakKey(unsigned char *key,CK_KEY_TYPE key_type) andre@0: { andre@0: andre@0: switch(key_type) { andre@0: case CKK_DES: andre@0: return sftk_CheckDESKey(key); andre@0: case CKM_DES2_KEY_GEN: andre@0: if (sftk_CheckDESKey(key)) return PR_TRUE; andre@0: return sftk_CheckDESKey(&key[8]); andre@0: case CKM_DES3_KEY_GEN: andre@0: if (sftk_CheckDESKey(key)) return PR_TRUE; andre@0: if (sftk_CheckDESKey(&key[8])) return PR_TRUE; andre@0: return sftk_CheckDESKey(&key[16]); andre@0: default: andre@0: break; andre@0: } andre@0: return PR_FALSE; andre@0: } andre@0: andre@0: andre@0: /********************************************************************** andre@0: * andre@0: * Start of PKCS 11 functions andre@0: * andre@0: **********************************************************************/ andre@0: andre@0: andre@0: /* return the function list */ andre@0: CK_RV NSC_GetFunctionList(CK_FUNCTION_LIST_PTR *pFunctionList) andre@0: { andre@0: CHECK_FORK(); andre@0: andre@0: *pFunctionList = (CK_FUNCTION_LIST_PTR) &sftk_funcList; andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* return the function list */ andre@0: CK_RV C_GetFunctionList(CK_FUNCTION_LIST_PTR *pFunctionList) andre@0: { andre@0: CHECK_FORK(); andre@0: andre@0: return NSC_GetFunctionList(pFunctionList); andre@0: } andre@0: andre@0: static PLHashNumber andre@0: sftk_HashNumber(const void *key) andre@0: { andre@0: return (PLHashNumber) key; andre@0: } andre@0: andre@0: /* andre@0: * eventually I'd like to expunge all occurances of XXX_SLOT_ID and andre@0: * just go with the info in the slot. This is one place, however, andre@0: * where it might be a little difficult. andre@0: */ andre@0: const char * andre@0: sftk_getDefTokName(CK_SLOT_ID slotID) andre@0: { andre@0: static char buf[33]; andre@0: andre@0: switch (slotID) { andre@0: case NETSCAPE_SLOT_ID: andre@0: return "NSS Generic Crypto Services "; andre@0: case PRIVATE_KEY_SLOT_ID: andre@0: return "NSS Certificate DB "; andre@0: case FIPS_SLOT_ID: andre@0: return "NSS FIPS 140-2 Certificate DB "; andre@0: default: andre@0: break; andre@0: } andre@0: sprintf(buf,"NSS Application Token %08x ",(unsigned int) slotID); andre@0: return buf; andre@0: } andre@0: andre@0: const char * andre@0: sftk_getDefSlotName(CK_SLOT_ID slotID) andre@0: { andre@0: static char buf[65]; andre@0: andre@0: switch (slotID) { andre@0: case NETSCAPE_SLOT_ID: andre@0: return andre@0: "NSS Internal Cryptographic Services "; andre@0: case PRIVATE_KEY_SLOT_ID: andre@0: return andre@0: "NSS User Private Key and Certificate Services "; andre@0: case FIPS_SLOT_ID: andre@0: return andre@0: "NSS FIPS 140-2 User Private Key Services "; andre@0: default: andre@0: break; andre@0: } andre@0: sprintf(buf, andre@0: "NSS Application Slot %08x ", andre@0: (unsigned int) slotID); andre@0: return buf; andre@0: } andre@0: andre@0: static CK_ULONG nscSlotCount[2] = {0 , 0}; andre@0: static CK_SLOT_ID_PTR nscSlotList[2] = {NULL, NULL}; andre@0: static CK_ULONG nscSlotListSize[2] = {0, 0}; andre@0: static PLHashTable *nscSlotHashTable[2] = {NULL, NULL}; andre@0: andre@0: static int andre@0: sftk_GetModuleIndex(CK_SLOT_ID slotID) andre@0: { andre@0: if ((slotID == FIPS_SLOT_ID) || (slotID >= SFTK_MIN_FIPS_USER_SLOT_ID)) { andre@0: return NSC_FIPS_MODULE; andre@0: } andre@0: return NSC_NON_FIPS_MODULE; andre@0: } andre@0: andre@0: /* look up a slot structure from the ID (used to be a macro when we only andre@0: * had two slots) */ andre@0: /* if all is true, return the slot even if it has been 'unloaded' */ andre@0: /* if all is false, only return the slots which are present */ andre@0: SFTKSlot * andre@0: sftk_SlotFromID(CK_SLOT_ID slotID, PRBool all) andre@0: { andre@0: SFTKSlot *slot; andre@0: int index = sftk_GetModuleIndex(slotID); andre@0: andre@0: if (nscSlotHashTable[index] == NULL) return NULL; andre@0: slot = (SFTKSlot *)PL_HashTableLookupConst(nscSlotHashTable[index], andre@0: (void *)slotID); andre@0: /* cleared slots shouldn't 'show up' */ andre@0: if (slot && !all && !slot->present) slot = NULL; andre@0: return slot; andre@0: } andre@0: andre@0: SFTKSlot * andre@0: sftk_SlotFromSessionHandle(CK_SESSION_HANDLE handle) andre@0: { andre@0: CK_ULONG slotIDIndex = (handle >> 24) & 0x7f; andre@0: CK_ULONG moduleIndex = (handle >> 31) & 1; andre@0: andre@0: if (slotIDIndex >= nscSlotCount[moduleIndex]) { andre@0: return NULL; andre@0: } andre@0: andre@0: return sftk_SlotFromID(nscSlotList[moduleIndex][slotIDIndex], PR_FALSE); andre@0: } andre@0: andre@0: static CK_RV andre@0: sftk_RegisterSlot(SFTKSlot *slot, int moduleIndex) andre@0: { andre@0: PLHashEntry *entry; andre@0: int index; andre@0: andre@0: index = sftk_GetModuleIndex(slot->slotID); andre@0: andre@0: /* make sure the slotID for this module is valid */ andre@0: if (moduleIndex != index) { andre@0: return CKR_SLOT_ID_INVALID; andre@0: } andre@0: andre@0: if (nscSlotList[index] == NULL) { andre@0: nscSlotListSize[index] = NSC_SLOT_LIST_BLOCK_SIZE; andre@0: nscSlotList[index] = (CK_SLOT_ID *) andre@0: PORT_ZAlloc(nscSlotListSize[index]*sizeof(CK_SLOT_ID)); andre@0: if (nscSlotList[index] == NULL) { andre@0: return CKR_HOST_MEMORY; andre@0: } andre@0: } andre@0: if (nscSlotCount[index] >= nscSlotListSize[index]) { andre@0: CK_SLOT_ID* oldNscSlotList = nscSlotList[index]; andre@0: CK_ULONG oldNscSlotListSize = nscSlotListSize[index]; andre@0: nscSlotListSize[index] += NSC_SLOT_LIST_BLOCK_SIZE; andre@0: nscSlotList[index] = (CK_SLOT_ID *) PORT_Realloc(oldNscSlotList, andre@0: nscSlotListSize[index]*sizeof(CK_SLOT_ID)); andre@0: if (nscSlotList[index] == NULL) { andre@0: nscSlotList[index] = oldNscSlotList; andre@0: nscSlotListSize[index] = oldNscSlotListSize; andre@0: return CKR_HOST_MEMORY; andre@0: } andre@0: } andre@0: andre@0: if (nscSlotHashTable[index] == NULL) { andre@0: nscSlotHashTable[index] = PL_NewHashTable(64,sftk_HashNumber, andre@0: PL_CompareValues, PL_CompareValues, NULL, 0); andre@0: if (nscSlotHashTable[index] == NULL) { andre@0: return CKR_HOST_MEMORY; andre@0: } andre@0: } andre@0: andre@0: entry = PL_HashTableAdd(nscSlotHashTable[index],(void *)slot->slotID,slot); andre@0: if (entry == NULL) { andre@0: return CKR_HOST_MEMORY; andre@0: } andre@0: slot->index = (nscSlotCount[index] & 0x7f) | ((index << 7) & 0x80); andre@0: nscSlotList[index][nscSlotCount[index]++] = slot->slotID; andre@0: andre@0: return CKR_OK; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * ths function has all the common initialization that happens whenever we andre@0: * create a new slot or repurpose an old slot (only valid for slotID's 4 andre@0: * and greater). andre@0: * andre@0: * things that are not reinitialized are: andre@0: * slotID (can't change) andre@0: * slotDescription (can't change once defined) andre@0: * the locks and hash tables (difficult to change in running code, and andre@0: * unnecessary. hash tables and list are cleared on shutdown, but they andre@0: * are cleared in a 'friendly' way). andre@0: * session and object ID counters -- so any old sessions and objects in the andre@0: * application will get properly notified that the world has changed. andre@0: * andre@0: * things that are reinitialized: andre@0: * database (otherwise what would the point be;). andre@0: * state variables related to databases. andre@0: * session count stat info. andre@0: * tokenDescription. andre@0: * andre@0: * NOTE: slotID's 4 and greater show up as removable devices. andre@0: * andre@0: */ andre@0: CK_RV andre@0: SFTK_SlotReInit(SFTKSlot *slot, char *configdir, char *updatedir, andre@0: char *updateID, sftk_token_parameters *params, int moduleIndex) andre@0: { andre@0: PRBool needLogin = !params->noKeyDB; andre@0: CK_RV crv; andre@0: andre@0: slot->hasTokens = PR_FALSE; andre@0: slot->sessionIDConflict = 0; andre@0: slot->sessionCount = 0; andre@0: slot->rwSessionCount = 0; andre@0: slot->needLogin = PR_FALSE; andre@0: slot->isLoggedIn = PR_FALSE; andre@0: slot->ssoLoggedIn = PR_FALSE; andre@0: slot->DB_loaded = PR_FALSE; andre@0: slot->certDB = NULL; andre@0: slot->keyDB = NULL; andre@0: slot->minimumPinLen = 0; andre@0: slot->readOnly = params->readOnly; andre@0: sftk_setStringName(params->tokdes ? params->tokdes : andre@0: sftk_getDefTokName(slot->slotID), slot->tokDescription, andre@0: sizeof(slot->tokDescription),PR_TRUE); andre@0: sftk_setStringName(params->updtokdes ? params->updtokdes : " ", andre@0: slot->updateTokDescription, andre@0: sizeof(slot->updateTokDescription),PR_TRUE); andre@0: andre@0: if ((!params->noCertDB) || (!params->noKeyDB)) { andre@0: SFTKDBHandle * certHandle = NULL; andre@0: SFTKDBHandle *keyHandle = NULL; andre@0: crv = sftk_DBInit(params->configdir ? params->configdir : configdir, andre@0: params->certPrefix, params->keyPrefix, andre@0: params->updatedir ? params->updatedir : updatedir, andre@0: params->updCertPrefix, params->updKeyPrefix, andre@0: params->updateID ? params->updateID : updateID, andre@0: params->readOnly, params->noCertDB, params->noKeyDB, andre@0: params->forceOpen, andre@0: moduleIndex == NSC_FIPS_MODULE, andre@0: &certHandle, &keyHandle); andre@0: if (crv != CKR_OK) { andre@0: goto loser; andre@0: } andre@0: andre@0: slot->certDB = certHandle; andre@0: slot->keyDB = keyHandle; andre@0: } andre@0: if (needLogin) { andre@0: /* if the data base is initialized with a null password,remember that */ andre@0: slot->needLogin = andre@0: (PRBool)!sftk_hasNullPassword(slot, slot->keyDB); andre@0: if ((params->minPW >= 0) && (params->minPW <= SFTK_MAX_PIN)) { andre@0: slot->minimumPinLen = params->minPW; andre@0: } andre@0: if ((slot->minimumPinLen == 0) && (params->pwRequired)) { andre@0: slot->minimumPinLen = 1; andre@0: } andre@0: if ((moduleIndex == NSC_FIPS_MODULE) && andre@0: (slot->minimumPinLen < FIPS_MIN_PIN)) { andre@0: slot->minimumPinLen = FIPS_MIN_PIN; andre@0: } andre@0: } andre@0: andre@0: slot->present = PR_TRUE; andre@0: return CKR_OK; andre@0: andre@0: loser: andre@0: SFTK_ShutdownSlot(slot); andre@0: return crv; andre@0: } andre@0: andre@0: /* andre@0: * initialize one of the slot structures. figure out which by the ID andre@0: */ andre@0: CK_RV andre@0: SFTK_SlotInit(char *configdir, char *updatedir, char *updateID, andre@0: sftk_token_parameters *params, int moduleIndex) andre@0: { andre@0: unsigned int i; andre@0: CK_SLOT_ID slotID = params->slotID; andre@0: SFTKSlot *slot; andre@0: CK_RV crv = CKR_HOST_MEMORY; andre@0: andre@0: /* andre@0: * first we initialize everything that is 'permanent' with this slot. andre@0: * that is everything we aren't going to shutdown if we close this slot andre@0: * and open it up again with different databases */ andre@0: andre@0: slot = PORT_ZNew(SFTKSlot); andre@0: andre@0: if (slot == NULL) { andre@0: return CKR_HOST_MEMORY; andre@0: } andre@0: andre@0: slot->optimizeSpace = params->optimizeSpace; andre@0: if (slot->optimizeSpace) { andre@0: slot->sessObjHashSize = SPACE_SESSION_OBJECT_HASH_SIZE; andre@0: slot->sessHashSize = SPACE_SESSION_HASH_SIZE; andre@0: slot->numSessionLocks = 1; andre@0: } else { andre@0: slot->sessObjHashSize = TIME_SESSION_OBJECT_HASH_SIZE; andre@0: slot->sessHashSize = TIME_SESSION_HASH_SIZE; andre@0: slot->numSessionLocks = slot->sessHashSize/BUCKETS_PER_SESSION_LOCK; andre@0: } andre@0: slot->sessionLockMask = slot->numSessionLocks-1; andre@0: andre@0: slot->slotLock = PZ_NewLock(nssILockSession); andre@0: if (slot->slotLock == NULL) andre@0: goto mem_loser; andre@0: slot->sessionLock = PORT_ZNewArray(PZLock *, slot->numSessionLocks); andre@0: if (slot->sessionLock == NULL) andre@0: goto mem_loser; andre@0: for (i=0; i < slot->numSessionLocks; i++) { andre@0: slot->sessionLock[i] = PZ_NewLock(nssILockSession); andre@0: if (slot->sessionLock[i] == NULL) andre@0: goto mem_loser; andre@0: } andre@0: slot->objectLock = PZ_NewLock(nssILockObject); andre@0: if (slot->objectLock == NULL) andre@0: goto mem_loser; andre@0: slot->pwCheckLock = PR_NewLock(); andre@0: if (slot->pwCheckLock == NULL) andre@0: goto mem_loser; andre@0: slot->head = PORT_ZNewArray(SFTKSession *, slot->sessHashSize); andre@0: if (slot->head == NULL) andre@0: goto mem_loser; andre@0: slot->sessObjHashTable = PORT_ZNewArray(SFTKObject *, slot->sessObjHashSize); andre@0: if (slot->sessObjHashTable == NULL) andre@0: goto mem_loser; andre@0: slot->tokObjHashTable = PL_NewHashTable(64,sftk_HashNumber,PL_CompareValues, andre@0: SECITEM_HashCompare, NULL, 0); andre@0: if (slot->tokObjHashTable == NULL) andre@0: goto mem_loser; andre@0: andre@0: slot->sessionIDCount = 0; andre@0: slot->sessionObjectHandleCount = minSessionObjectHandle; andre@0: slot->slotID = slotID; andre@0: sftk_setStringName(params->slotdes ? params->slotdes : andre@0: sftk_getDefSlotName(slotID), slot->slotDescription, andre@0: sizeof(slot->slotDescription), PR_TRUE); andre@0: andre@0: /* call the reinit code to set everything that changes between token andre@0: * init calls */ andre@0: crv = SFTK_SlotReInit(slot, configdir, updatedir, updateID, andre@0: params, moduleIndex); andre@0: if (crv != CKR_OK) { andre@0: goto loser; andre@0: } andre@0: crv = sftk_RegisterSlot(slot, moduleIndex); andre@0: if (crv != CKR_OK) { andre@0: goto loser; andre@0: } andre@0: return CKR_OK; andre@0: andre@0: mem_loser: andre@0: crv = CKR_HOST_MEMORY; andre@0: loser: andre@0: SFTK_DestroySlotData(slot); andre@0: return crv; andre@0: } andre@0: andre@0: andre@0: CK_RV sftk_CloseAllSessions(SFTKSlot *slot, PRBool logout) andre@0: { andre@0: SFTKSession *session; andre@0: unsigned int i; andre@0: SFTKDBHandle *handle; andre@0: andre@0: /* first log out the card */ andre@0: /* special case - if we are in a middle of upgrade, we want to close the andre@0: * sessions to fake a token removal to tell the upper level code we have andre@0: * switched from one database to another, but we don't want to andre@0: * explicity logout in case we can continue the upgrade with the andre@0: * existing password if possible. andre@0: */ andre@0: if (logout) { andre@0: handle = sftk_getKeyDB(slot); andre@0: SKIP_AFTER_FORK(PZ_Lock(slot->slotLock)); andre@0: slot->isLoggedIn = PR_FALSE; andre@0: if (slot->needLogin && handle) { andre@0: sftkdb_ClearPassword(handle); andre@0: } andre@0: SKIP_AFTER_FORK(PZ_Unlock(slot->slotLock)); andre@0: if (handle) { andre@0: sftk_freeDB(handle); andre@0: } andre@0: } andre@0: andre@0: /* now close all the current sessions */ andre@0: /* NOTE: If you try to open new sessions before NSC_CloseAllSessions andre@0: * completes, some of those new sessions may or may not be closed by andre@0: * NSC_CloseAllSessions... but any session running when this code starts andre@0: * will guarrenteed be close, and no session will be partially closed */ andre@0: for (i=0; i < slot->sessHashSize; i++) { andre@0: PZLock *lock = SFTK_SESSION_LOCK(slot,i); andre@0: do { andre@0: SKIP_AFTER_FORK(PZ_Lock(lock)); andre@0: session = slot->head[i]; andre@0: /* hand deque */ andre@0: /* this duplicates function of NSC_close session functions, but andre@0: * because we know that we are freeing all the sessions, we can andre@0: * do more efficient processing */ andre@0: if (session) { andre@0: slot->head[i] = session->next; andre@0: if (session->next) session->next->prev = NULL; andre@0: session->next = session->prev = NULL; andre@0: SKIP_AFTER_FORK(PZ_Unlock(lock)); andre@0: SKIP_AFTER_FORK(PZ_Lock(slot->slotLock)); andre@0: --slot->sessionCount; andre@0: SKIP_AFTER_FORK(PZ_Unlock(slot->slotLock)); andre@0: if (session->info.flags & CKF_RW_SESSION) { andre@0: PR_ATOMIC_DECREMENT(&slot->rwSessionCount); andre@0: } andre@0: } else { andre@0: SKIP_AFTER_FORK(PZ_Unlock(lock)); andre@0: } andre@0: if (session) sftk_FreeSession(session); andre@0: } while (session != NULL); andre@0: } andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* andre@0: * shut down the databases. andre@0: * we get the slot lock (which also protects slot->certDB and slot->keyDB) andre@0: * and clear the values so the new users will not find the databases. andre@0: * once things are clear, we can release our references to the databases. andre@0: * The databases will close when the last reference is released. andre@0: * andre@0: * We use reference counts so that we don't crash if someone shuts down andre@0: * a token that another thread is actively using. andre@0: */ andre@0: static void andre@0: sftk_DBShutdown(SFTKSlot *slot) andre@0: { andre@0: SFTKDBHandle *certHandle; andre@0: SFTKDBHandle *keyHandle; andre@0: SKIP_AFTER_FORK(PZ_Lock(slot->slotLock)); andre@0: certHandle = slot->certDB; andre@0: slot->certDB = NULL; andre@0: keyHandle = slot->keyDB; andre@0: slot->keyDB = NULL; andre@0: SKIP_AFTER_FORK(PZ_Unlock(slot->slotLock)); andre@0: if (certHandle) { andre@0: sftk_freeDB(certHandle); andre@0: } andre@0: if (keyHandle) { andre@0: sftk_freeDB(keyHandle); andre@0: } andre@0: } andre@0: andre@0: CK_RV andre@0: SFTK_ShutdownSlot(SFTKSlot *slot) andre@0: { andre@0: /* make sure no new PK11 calls work except C_GetSlotInfo */ andre@0: slot->present = PR_FALSE; andre@0: andre@0: /* close all outstanding sessions andre@0: * the sessHashSize variable guarentees we have all the session andre@0: * mechanism set up */ andre@0: if (slot->head) { andre@0: sftk_CloseAllSessions(slot, PR_TRUE); andre@0: } andre@0: andre@0: /* clear all objects.. session objects are cleared as a result of andre@0: * closing all the sessions. We just need to clear the token object andre@0: * cache. slot->tokObjHashTable guarentees we have the token andre@0: * infrastructure set up. */ andre@0: if (slot->tokObjHashTable) { andre@0: SFTK_ClearTokenKeyHashTable(slot); andre@0: } andre@0: andre@0: /* clear the slot description for the next guy */ andre@0: PORT_Memset(slot->tokDescription, 0, sizeof(slot->tokDescription)); andre@0: andre@0: /* now shut down the databases. */ andre@0: sftk_DBShutdown(slot); andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* andre@0: * initialize one of the slot structures. figure out which by the ID andre@0: */ andre@0: CK_RV andre@0: SFTK_DestroySlotData(SFTKSlot *slot) andre@0: { andre@0: unsigned int i; andre@0: andre@0: SFTK_ShutdownSlot(slot); andre@0: andre@0: if (slot->tokObjHashTable) { andre@0: PL_HashTableDestroy(slot->tokObjHashTable); andre@0: slot->tokObjHashTable = NULL; andre@0: } andre@0: andre@0: if (slot->sessObjHashTable) { andre@0: PORT_Free(slot->sessObjHashTable); andre@0: slot->sessObjHashTable = NULL; andre@0: } andre@0: slot->sessObjHashSize = 0; andre@0: andre@0: if (slot->head) { andre@0: PORT_Free(slot->head); andre@0: slot->head = NULL; andre@0: } andre@0: slot->sessHashSize = 0; andre@0: andre@0: /* OK everything has been disassembled, now we can finally get rid andre@0: * of the locks */ andre@0: SKIP_AFTER_FORK(PZ_DestroyLock(slot->slotLock)); andre@0: slot->slotLock = NULL; andre@0: if (slot->sessionLock) { andre@0: for (i=0; i < slot->numSessionLocks; i++) { andre@0: if (slot->sessionLock[i]) { andre@0: SKIP_AFTER_FORK(PZ_DestroyLock(slot->sessionLock[i])); andre@0: slot->sessionLock[i] = NULL; andre@0: } andre@0: } andre@0: PORT_Free(slot->sessionLock); andre@0: slot->sessionLock = NULL; andre@0: } andre@0: if (slot->objectLock) { andre@0: SKIP_AFTER_FORK(PZ_DestroyLock(slot->objectLock)); andre@0: slot->objectLock = NULL; andre@0: } andre@0: if (slot->pwCheckLock) { andre@0: SKIP_AFTER_FORK(PR_DestroyLock(slot->pwCheckLock)); andre@0: slot->pwCheckLock = NULL; andre@0: } andre@0: PORT_Free(slot); andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* andre@0: * handle the SECMOD.db andre@0: */ andre@0: char ** andre@0: NSC_ModuleDBFunc(unsigned long function,char *parameters, void *args) andre@0: { andre@0: char *secmod = NULL; andre@0: char *appName = NULL; andre@0: char *filename = NULL; andre@0: NSSDBType dbType = NSS_DB_TYPE_NONE; andre@0: PRBool rw; andre@0: static char *success="Success"; andre@0: char **rvstr = NULL; andre@0: andre@0: rvstr = NSSUTIL_DoModuleDBFunction(function, parameters, args); andre@0: if (rvstr != NULL) { andre@0: return rvstr; andre@0: } andre@0: andre@0: if (PORT_GetError() != SEC_ERROR_LEGACY_DATABASE) { andre@0: return NULL; andre@0: } andre@0: andre@0: /* The legacy database uses the old dbm, which is only linked with the andre@0: * legacy DB handler, which is only callable from softoken */ andre@0: andre@0: secmod = _NSSUTIL_GetSecmodName(parameters, &dbType, &appName, andre@0: &filename, &rw); andre@0: andre@0: switch (function) { andre@0: case SECMOD_MODULE_DB_FUNCTION_FIND: andre@0: if (secmod == NULL) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return NULL; andre@0: } andre@0: if (rw && (dbType != NSS_DB_TYPE_LEGACY) && andre@0: (dbType != NSS_DB_TYPE_MULTIACCESS)) { andre@0: /* if we get here, we are trying to update the local database */ andre@0: /* force data from the legacy DB */ andre@0: char *oldSecmod = NULL; andre@0: char *oldAppName = NULL; andre@0: char *oldFilename = NULL; andre@0: PRBool oldrw; andre@0: char **strings = NULL; andre@0: int i; andre@0: andre@0: dbType = NSS_DB_TYPE_LEGACY; andre@0: oldSecmod = _NSSUTIL_GetSecmodName(parameters,&dbType, &oldAppName, andre@0: &oldFilename, &oldrw); andre@0: strings = sftkdbCall_ReadSecmodDB(appName, oldFilename, oldSecmod, andre@0: (char *)parameters, oldrw); andre@0: if (strings) { andre@0: /* write out the strings */ andre@0: for (i=0; strings[i]; i++) { andre@0: NSSUTIL_DoModuleDBFunction(SECMOD_MODULE_DB_FUNCTION_ADD, andre@0: parameters, strings[i]); andre@0: } andre@0: sftkdbCall_ReleaseSecmodDBData(oldAppName,oldFilename,oldSecmod, andre@0: (char **)strings,oldrw); andre@0: } else { andre@0: /* write out a dummy record */ andre@0: NSSUTIL_DoModuleDBFunction(SECMOD_MODULE_DB_FUNCTION_ADD, andre@0: parameters, " "); andre@0: } andre@0: if (oldSecmod) { PR_smprintf_free(oldSecmod); } andre@0: if (oldAppName) { PORT_Free(oldAppName); } andre@0: if (oldFilename) { PORT_Free(oldFilename); } andre@0: rvstr = NSSUTIL_DoModuleDBFunction(function, parameters, args); andre@0: break; andre@0: } andre@0: rvstr = sftkdbCall_ReadSecmodDB(appName,filename,secmod, andre@0: (char *)parameters,rw); andre@0: break; andre@0: case SECMOD_MODULE_DB_FUNCTION_ADD: andre@0: if (secmod == NULL) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return NULL; andre@0: } andre@0: rvstr = (sftkdbCall_AddSecmodDB(appName,filename,secmod, andre@0: (char *)args,rw) == SECSuccess) ? &success: NULL; andre@0: break; andre@0: case SECMOD_MODULE_DB_FUNCTION_DEL: andre@0: if (secmod == NULL) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return NULL; andre@0: } andre@0: rvstr = (sftkdbCall_DeleteSecmodDB(appName,filename,secmod, andre@0: (char *)args,rw) == SECSuccess) ? &success: NULL; andre@0: break; andre@0: case SECMOD_MODULE_DB_FUNCTION_RELEASE: andre@0: rvstr = (sftkdbCall_ReleaseSecmodDBData(appName,filename,secmod, andre@0: (char **)args,rw) == SECSuccess) ? &success: NULL; andre@0: break; andre@0: } andre@0: if (secmod) PR_smprintf_free(secmod); andre@0: if (appName) PORT_Free(appName); andre@0: if (filename) PORT_Free(filename); andre@0: return rvstr; andre@0: } andre@0: andre@0: static void nscFreeAllSlots(int moduleIndex) andre@0: { andre@0: /* free all the slots */ andre@0: SFTKSlot *slot = NULL; andre@0: CK_SLOT_ID slotID; andre@0: int i; andre@0: andre@0: if (nscSlotList[moduleIndex]) { andre@0: CK_ULONG tmpSlotCount = nscSlotCount[moduleIndex]; andre@0: CK_SLOT_ID_PTR tmpSlotList = nscSlotList[moduleIndex]; andre@0: PLHashTable *tmpSlotHashTable = nscSlotHashTable[moduleIndex]; andre@0: andre@0: /* first close all the session */ andre@0: for (i=0; i < (int) tmpSlotCount; i++) { andre@0: slotID = tmpSlotList[i]; andre@0: (void) NSC_CloseAllSessions(slotID); andre@0: } andre@0: andre@0: /* now clear out the statics */ andre@0: nscSlotList[moduleIndex] = NULL; andre@0: nscSlotCount[moduleIndex] = 0; andre@0: nscSlotHashTable[moduleIndex] = NULL; andre@0: nscSlotListSize[moduleIndex] = 0; andre@0: andre@0: for (i=0; i < (int) tmpSlotCount; i++) { andre@0: slotID = tmpSlotList[i]; andre@0: slot = (SFTKSlot *) andre@0: PL_HashTableLookup(tmpSlotHashTable, (void *)slotID); andre@0: PORT_Assert(slot); andre@0: if (!slot) continue; andre@0: SFTK_DestroySlotData(slot); andre@0: PL_HashTableRemove(tmpSlotHashTable, (void *)slotID); andre@0: } andre@0: PORT_Free(tmpSlotList); andre@0: PL_HashTableDestroy(tmpSlotHashTable); andre@0: } andre@0: } andre@0: andre@0: static void andre@0: sftk_closePeer(PRBool isFIPS) andre@0: { andre@0: CK_SLOT_ID slotID = isFIPS ? PRIVATE_KEY_SLOT_ID: FIPS_SLOT_ID; andre@0: SFTKSlot *slot; andre@0: int moduleIndex = isFIPS? NSC_NON_FIPS_MODULE : NSC_FIPS_MODULE; andre@0: PLHashTable *tmpSlotHashTable = nscSlotHashTable[moduleIndex]; andre@0: andre@0: slot = (SFTKSlot *) PL_HashTableLookup(tmpSlotHashTable, (void *)slotID); andre@0: if (slot == NULL) { andre@0: return; andre@0: } andre@0: sftk_DBShutdown(slot); andre@0: return; andre@0: } andre@0: andre@0: /* NSC_Initialize initializes the Cryptoki library. */ andre@0: CK_RV nsc_CommonInitialize(CK_VOID_PTR pReserved, PRBool isFIPS) andre@0: { andre@0: CK_RV crv = CKR_OK; andre@0: SECStatus rv; andre@0: CK_C_INITIALIZE_ARGS *init_args = (CK_C_INITIALIZE_ARGS *) pReserved; andre@0: int i; andre@0: int moduleIndex = isFIPS? NSC_FIPS_MODULE : NSC_NON_FIPS_MODULE; andre@0: andre@0: if (isFIPS) { andre@0: loginWaitTime = PR_SecondsToInterval(1); andre@0: } andre@0: andre@0: ENABLE_FORK_CHECK(); andre@0: andre@0: rv = SECOID_Init(); andre@0: if (rv != SECSuccess) { andre@0: crv = CKR_DEVICE_ERROR; andre@0: return crv; andre@0: } andre@0: andre@0: rv = RNG_RNGInit(); /* initialize random number generator */ andre@0: if (rv != SECSuccess) { andre@0: crv = CKR_DEVICE_ERROR; andre@0: return crv; andre@0: } andre@0: rv = BL_Init(); /* initialize freebl engine */ andre@0: if (rv != SECSuccess) { andre@0: crv = CKR_DEVICE_ERROR; andre@0: return crv; andre@0: } andre@0: andre@0: /* NOTE: andre@0: * we should be getting out mutexes from this list, not statically binding andre@0: * them from NSPR. This should happen before we allow the internal to split andre@0: * off from the rest on NSS. andre@0: */ andre@0: andre@0: /* initialize the key and cert db's */ andre@0: if (init_args && (!(init_args->flags & CKF_OS_LOCKING_OK))) { andre@0: if (init_args->CreateMutex && init_args->DestroyMutex && andre@0: init_args->LockMutex && init_args->UnlockMutex) { andre@0: /* softoken always uses NSPR (ie. OS locking), and doesn't know how andre@0: * to use the lock functions provided by the application. andre@0: */ andre@0: crv = CKR_CANT_LOCK; andre@0: return crv; andre@0: } andre@0: if (init_args->CreateMutex || init_args->DestroyMutex || andre@0: init_args->LockMutex || init_args->UnlockMutex) { andre@0: /* only some of the lock functions were provided by the andre@0: * application. This is invalid per PKCS#11 spec. andre@0: */ andre@0: crv = CKR_ARGUMENTS_BAD; andre@0: return crv; andre@0: } andre@0: } andre@0: crv = CKR_ARGUMENTS_BAD; andre@0: if ((init_args && init_args->LibraryParameters)) { andre@0: sftk_parameters paramStrings; andre@0: andre@0: crv = sftk_parseParameters andre@0: ((char *)init_args->LibraryParameters, ¶mStrings, isFIPS); andre@0: if (crv != CKR_OK) { andre@0: return crv; andre@0: } andre@0: crv = sftk_configure(paramStrings.man, paramStrings.libdes); andre@0: if (crv != CKR_OK) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* if we have a peer already open, have him close his DB's so we andre@0: * don't clobber each other. */ andre@0: if ((isFIPS && nsc_init) || (!isFIPS && nsf_init)) { andre@0: sftk_closePeer(isFIPS); andre@0: if (sftk_audit_enabled) { andre@0: if (isFIPS && nsc_init) { andre@0: sftk_LogAuditMessage(NSS_AUDIT_INFO, NSS_AUDIT_FIPS_STATE, andre@0: "enabled FIPS mode"); andre@0: } else { andre@0: sftk_LogAuditMessage(NSS_AUDIT_INFO, NSS_AUDIT_FIPS_STATE, andre@0: "disabled FIPS mode"); andre@0: } andre@0: } andre@0: } andre@0: andre@0: for (i=0; i < paramStrings.token_count; i++) { andre@0: crv = SFTK_SlotInit(paramStrings.configdir, andre@0: paramStrings.updatedir, paramStrings.updateID, andre@0: ¶mStrings.tokens[i], moduleIndex); andre@0: if (crv != CKR_OK) { andre@0: nscFreeAllSlots(moduleIndex); andre@0: break; andre@0: } andre@0: } andre@0: loser: andre@0: sftk_freeParams(¶mStrings); andre@0: } andre@0: if (CKR_OK == crv) { andre@0: sftk_InitFreeLists(); andre@0: } andre@0: andre@0: #ifndef NO_FORK_CHECK andre@0: if (CKR_OK == crv) { andre@0: #if defined(CHECK_FORK_MIXED) andre@0: /* Before Solaris 10, fork handlers are not unregistered at dlclose() andre@0: * time. So, we only use pthread_atfork on Solaris 10 and later. For andre@0: * earlier versions, we use PID checks. andre@0: */ andre@0: char buf[200]; andre@0: int major = 0, minor = 0; andre@0: andre@0: long rv = sysinfo(SI_RELEASE, buf, sizeof(buf)); andre@0: if (rv > 0 && rv < sizeof(buf)) { andre@0: if (2 == sscanf(buf, "%d.%d", &major, &minor)) { andre@0: /* Are we on Solaris 10 or greater ? */ andre@0: if (major >5 || (5 == major && minor >= 10)) { andre@0: /* we are safe to use pthread_atfork */ andre@0: usePthread_atfork = PR_TRUE; andre@0: } andre@0: } andre@0: } andre@0: if (usePthread_atfork) { andre@0: pthread_atfork(NULL, NULL, ForkedChild); andre@0: } else { andre@0: myPid = getpid(); andre@0: } andre@0: andre@0: #elif defined(CHECK_FORK_PTHREAD) andre@0: pthread_atfork(NULL, NULL, ForkedChild); andre@0: #elif defined(CHECK_FORK_GETPID) andre@0: myPid = getpid(); andre@0: #else andre@0: #error Incorrect fork check method. andre@0: #endif andre@0: } andre@0: #endif andre@0: return crv; andre@0: } andre@0: andre@0: CK_RV NSC_Initialize(CK_VOID_PTR pReserved) andre@0: { andre@0: CK_RV crv; andre@0: andre@0: sftk_ForkReset(pReserved, &crv); andre@0: andre@0: if (nsc_init) { andre@0: return CKR_CRYPTOKI_ALREADY_INITIALIZED; andre@0: } andre@0: crv = nsc_CommonInitialize(pReserved,PR_FALSE); andre@0: nsc_init = (PRBool) (crv == CKR_OK); andre@0: return crv; andre@0: } andre@0: andre@0: andre@0: /* NSC_Finalize indicates that an application is done with the andre@0: * Cryptoki library.*/ andre@0: CK_RV nsc_CommonFinalize (CK_VOID_PTR pReserved, PRBool isFIPS) andre@0: { andre@0: /* propagate the fork status to freebl and util */ andre@0: BL_SetForkState(parentForkedAfterC_Initialize); andre@0: UTIL_SetForkState(parentForkedAfterC_Initialize); andre@0: andre@0: nscFreeAllSlots(isFIPS ? NSC_FIPS_MODULE : NSC_NON_FIPS_MODULE); andre@0: andre@0: /* don't muck with the globals if our peer is still initialized */ andre@0: if (isFIPS && nsc_init) { andre@0: return CKR_OK; andre@0: } andre@0: if (!isFIPS && nsf_init) { andre@0: return CKR_OK; andre@0: } andre@0: andre@0: sftk_CleanupFreeLists(); andre@0: sftkdb_Shutdown(); andre@0: andre@0: /* This function does not discard all our previously aquired entropy. */ andre@0: RNG_RNGShutdown(); andre@0: andre@0: /* tell freeBL to clean up after itself */ andre@0: BL_Cleanup(); andre@0: andre@0: /* reset fork status in freebl. We must do this before BL_Unload so that andre@0: * this call doesn't force freebl to be reloaded. */ andre@0: BL_SetForkState(PR_FALSE); andre@0: andre@0: /* unload freeBL shared library from memory. This may only decrement the andre@0: * OS refcount if it's been loaded multiple times, eg. by libssl */ andre@0: BL_Unload(); andre@0: andre@0: /* clean up the default OID table */ andre@0: SECOID_Shutdown(); andre@0: andre@0: /* reset fork status in util */ andre@0: UTIL_SetForkState(PR_FALSE); andre@0: andre@0: nsc_init = PR_FALSE; andre@0: andre@0: #ifdef CHECK_FORK_MIXED andre@0: if (!usePthread_atfork) { andre@0: myPid = 0; /* allow CHECK_FORK in the next softoken initialization to andre@0: * succeed */ andre@0: } else { andre@0: forked = PR_FALSE; /* allow reinitialization */ andre@0: } andre@0: #elif defined(CHECK_FORK_GETPID) andre@0: myPid = 0; /* allow reinitialization */ andre@0: #elif defined (CHECK_FORK_PTHREAD) andre@0: forked = PR_FALSE; /* allow reinitialization */ andre@0: #endif andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* Hard-reset the entire softoken PKCS#11 module if the parent process forked andre@0: * while it was initialized. */ andre@0: PRBool sftk_ForkReset(CK_VOID_PTR pReserved, CK_RV* crv) andre@0: { andre@0: #ifndef NO_FORK_CHECK andre@0: if (PARENT_FORKED()) { andre@0: parentForkedAfterC_Initialize = PR_TRUE; andre@0: if (nsc_init) { andre@0: /* finalize non-FIPS token */ andre@0: *crv = nsc_CommonFinalize(pReserved, PR_FALSE); andre@0: PORT_Assert(CKR_OK == *crv); andre@0: nsc_init = (PRBool) !(*crv == CKR_OK); andre@0: } andre@0: if (nsf_init) { andre@0: /* finalize FIPS token */ andre@0: *crv = nsc_CommonFinalize(pReserved, PR_TRUE); andre@0: PORT_Assert(CKR_OK == *crv); andre@0: nsf_init = (PRBool) !(*crv == CKR_OK); andre@0: } andre@0: parentForkedAfterC_Initialize = PR_FALSE; andre@0: return PR_TRUE; andre@0: } andre@0: #endif andre@0: return PR_FALSE; andre@0: } andre@0: andre@0: /* NSC_Finalize indicates that an application is done with the andre@0: * Cryptoki library.*/ andre@0: CK_RV NSC_Finalize (CK_VOID_PTR pReserved) andre@0: { andre@0: CK_RV crv; andre@0: andre@0: /* reset entire PKCS#11 module upon fork */ andre@0: if (sftk_ForkReset(pReserved, &crv)) { andre@0: return crv; andre@0: } andre@0: andre@0: if (!nsc_init) { andre@0: return CKR_OK; andre@0: } andre@0: andre@0: crv = nsc_CommonFinalize (pReserved, PR_FALSE); andre@0: andre@0: nsc_init = (PRBool) !(crv == CKR_OK); andre@0: andre@0: return crv; andre@0: } andre@0: andre@0: extern const char __nss_softokn_rcsid[]; andre@0: extern const char __nss_softokn_sccsid[]; andre@0: andre@0: /* NSC_GetInfo returns general information about Cryptoki. */ andre@0: CK_RV NSC_GetInfo(CK_INFO_PTR pInfo) andre@0: { andre@0: volatile char c; /* force a reference that won't get optimized away */ andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: c = __nss_softokn_rcsid[0] + __nss_softokn_sccsid[0]; andre@0: pInfo->cryptokiVersion.major = 2; andre@0: pInfo->cryptokiVersion.minor = 20; andre@0: PORT_Memcpy(pInfo->manufacturerID,manufacturerID,32); andre@0: pInfo->libraryVersion.major = SOFTOKEN_VMAJOR; andre@0: pInfo->libraryVersion.minor = SOFTOKEN_VMINOR; andre@0: PORT_Memcpy(pInfo->libraryDescription,libraryDescription,32); andre@0: pInfo->flags = 0; andre@0: return CKR_OK; andre@0: } andre@0: andre@0: andre@0: /* NSC_GetSlotList obtains a list of slots in the system. */ andre@0: CK_RV nsc_CommonGetSlotList(CK_BBOOL tokenPresent, andre@0: CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount, int moduleIndex) andre@0: { andre@0: *pulCount = nscSlotCount[moduleIndex]; andre@0: if (pSlotList != NULL) { andre@0: PORT_Memcpy(pSlotList,nscSlotList[moduleIndex], andre@0: nscSlotCount[moduleIndex]*sizeof(CK_SLOT_ID)); andre@0: } andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* NSC_GetSlotList obtains a list of slots in the system. */ andre@0: CK_RV NSC_GetSlotList(CK_BBOOL tokenPresent, andre@0: CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount) andre@0: { andre@0: CHECK_FORK(); andre@0: return nsc_CommonGetSlotList(tokenPresent, pSlotList, pulCount, andre@0: NSC_NON_FIPS_MODULE); andre@0: } andre@0: andre@0: /* NSC_GetSlotInfo obtains information about a particular slot in the system. */ andre@0: CK_RV NSC_GetSlotInfo(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo) andre@0: { andre@0: SFTKSlot *slot = sftk_SlotFromID(slotID, PR_TRUE); andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: if (slot == NULL) return CKR_SLOT_ID_INVALID; andre@0: andre@0: PORT_Memcpy(pInfo->manufacturerID,manufacturerID, andre@0: sizeof(pInfo->manufacturerID)); andre@0: PORT_Memcpy(pInfo->slotDescription,slot->slotDescription, andre@0: sizeof(pInfo->slotDescription)); andre@0: pInfo->flags = (slot->present) ? CKF_TOKEN_PRESENT : 0; andre@0: andre@0: /* all user defined slots are defined as removable */ andre@0: if (slotID >= SFTK_MIN_USER_SLOT_ID) { andre@0: pInfo->flags |= CKF_REMOVABLE_DEVICE; andre@0: } else { andre@0: /* In the case where we are doing a merge update, we need andre@0: * the DB slot to be removable so the token name can change andre@0: * appropriately. */ andre@0: SFTKDBHandle *handle = sftk_getKeyDB(slot); andre@0: if (handle) { andre@0: if (sftkdb_InUpdateMerge(handle)) { andre@0: pInfo->flags |= CKF_REMOVABLE_DEVICE; andre@0: } andre@0: sftk_freeDB(handle); andre@0: } andre@0: } andre@0: andre@0: /* ok we really should read it out of the keydb file. */ andre@0: /* pInfo->hardwareVersion.major = NSSLOWKEY_DB_FILE_VERSION; */ andre@0: pInfo->hardwareVersion.major = SOFTOKEN_VMAJOR; andre@0: pInfo->hardwareVersion.minor = SOFTOKEN_VMINOR; andre@0: pInfo->firmwareVersion.major = SOFTOKEN_VPATCH; andre@0: pInfo->firmwareVersion.minor = SOFTOKEN_VBUILD; andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* andre@0: * check the current state of the 'needLogin' flag in case the database has andre@0: * been changed underneath us. andre@0: */ andre@0: static PRBool andre@0: sftk_checkNeedLogin(SFTKSlot *slot, SFTKDBHandle *keyHandle) andre@0: { andre@0: if (sftkdb_PWCached(keyHandle) == SECSuccess) { andre@0: return slot->needLogin; andre@0: } andre@0: slot->needLogin = (PRBool)!sftk_hasNullPassword(slot, keyHandle); andre@0: return (slot->needLogin); andre@0: } andre@0: andre@0: static PRBool andre@0: sftk_isBlank(const char *s, int len) andre@0: { andre@0: int i; andre@0: for (i=0; i < len; i++) { andre@0: if (s[i] != ' ') { andre@0: return PR_FALSE; andre@0: } andre@0: } andre@0: return PR_TRUE; andre@0: } andre@0: andre@0: /* NSC_GetTokenInfo obtains information about a particular token in andre@0: * the system. */ andre@0: CK_RV NSC_GetTokenInfo(CK_SLOT_ID slotID,CK_TOKEN_INFO_PTR pInfo) andre@0: { andre@0: SFTKSlot *slot; andre@0: SFTKDBHandle *handle; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: if (!nsc_init && !nsf_init) return CKR_CRYPTOKI_NOT_INITIALIZED; andre@0: slot = sftk_SlotFromID(slotID, PR_FALSE); andre@0: if (slot == NULL) return CKR_SLOT_ID_INVALID; andre@0: andre@0: PORT_Memcpy(pInfo->manufacturerID,manufacturerID,32); andre@0: PORT_Memcpy(pInfo->model,"NSS 3 ",16); andre@0: PORT_Memcpy(pInfo->serialNumber,"0000000000000000",16); andre@0: PORT_Memcpy(pInfo->utcTime,"0000000000000000",16); andre@0: pInfo->ulMaxSessionCount = 0; /* arbitrarily large */ andre@0: pInfo->ulSessionCount = slot->sessionCount; andre@0: pInfo->ulMaxRwSessionCount = 0; /* arbitarily large */ andre@0: pInfo->ulRwSessionCount = slot->rwSessionCount; andre@0: pInfo->firmwareVersion.major = 0; andre@0: pInfo->firmwareVersion.minor = 0; andre@0: PORT_Memcpy(pInfo->label,slot->tokDescription,sizeof(pInfo->label)); andre@0: handle = sftk_getKeyDB(slot); andre@0: pInfo->flags = CKF_RNG | CKF_DUAL_CRYPTO_OPERATIONS; andre@0: if (handle == NULL) { andre@0: pInfo->flags |= CKF_WRITE_PROTECTED; andre@0: pInfo->ulMaxPinLen = 0; andre@0: pInfo->ulMinPinLen = 0; andre@0: pInfo->ulTotalPublicMemory = 0; andre@0: pInfo->ulFreePublicMemory = 0; andre@0: pInfo->ulTotalPrivateMemory = 0; andre@0: pInfo->ulFreePrivateMemory = 0; andre@0: pInfo->hardwareVersion.major = 4; andre@0: pInfo->hardwareVersion.minor = 0; andre@0: } else { andre@0: /* andre@0: * we have three possible states which we may be in: andre@0: * (1) No DB password has been initialized. This also means we andre@0: * have no keys in the key db. andre@0: * (2) Password initialized to NULL. This means we have keys, but andre@0: * the user has chosen not use a password. andre@0: * (3) Finally we have an initialized password whicn is not NULL, and andre@0: * we will need to prompt for it. andre@0: */ andre@0: if (sftkdb_HasPasswordSet(handle) == SECFailure) { andre@0: pInfo->flags |= CKF_LOGIN_REQUIRED; andre@0: } else if (!sftk_checkNeedLogin(slot,handle)) { andre@0: pInfo->flags |= CKF_USER_PIN_INITIALIZED; andre@0: } else { andre@0: pInfo->flags |= CKF_LOGIN_REQUIRED | CKF_USER_PIN_INITIALIZED; andre@0: /* andre@0: * if we are doing a merge style update, and we need to get the password andre@0: * of our source database (the database we are updating from), make sure we andre@0: * return a token name that will match the database we are prompting for. andre@0: */ andre@0: if (sftkdb_NeedUpdateDBPassword(handle)) { andre@0: /* if we have an update tok description, use it. otherwise andre@0: * use the updateID for this database */ andre@0: if (!sftk_isBlank(slot->updateTokDescription, andre@0: sizeof(pInfo->label))) { andre@0: PORT_Memcpy(pInfo->label,slot->updateTokDescription, andre@0: sizeof(pInfo->label)); andre@0: } else { andre@0: /* build from updateID */ andre@0: const char *updateID = sftkdb_GetUpdateID(handle); andre@0: if (updateID) { andre@0: sftk_setStringName(updateID, (char *)pInfo->label, andre@0: sizeof(pInfo->label), PR_FALSE); andre@0: } andre@0: } andre@0: } andre@0: } andre@0: pInfo->ulMaxPinLen = SFTK_MAX_PIN; andre@0: pInfo->ulMinPinLen = (CK_ULONG)slot->minimumPinLen; andre@0: pInfo->ulTotalPublicMemory = 1; andre@0: pInfo->ulFreePublicMemory = 1; andre@0: pInfo->ulTotalPrivateMemory = 1; andre@0: pInfo->ulFreePrivateMemory = 1; andre@0: #ifdef SHDB_FIXME andre@0: pInfo->hardwareVersion.major = CERT_DB_FILE_VERSION; andre@0: pInfo->hardwareVersion.minor = handle->version; andre@0: #else andre@0: pInfo->hardwareVersion.major = 0; andre@0: pInfo->hardwareVersion.minor = 0; andre@0: #endif andre@0: sftk_freeDB(handle); andre@0: } andre@0: /* andre@0: * CKF_LOGIN_REQUIRED CKF_USER_PIN_INITIALIZED how CKF_TOKEN_INITIALIZED andre@0: * should be set andre@0: * 0 0 1 andre@0: * 1 0 0 andre@0: * 0 1 1 andre@0: * 1 1 1 andre@0: */ andre@0: if (!(pInfo->flags & CKF_LOGIN_REQUIRED) || andre@0: (pInfo->flags & CKF_USER_PIN_INITIALIZED)) { andre@0: pInfo->flags |= CKF_TOKEN_INITIALIZED; andre@0: } andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* NSC_GetMechanismList obtains a list of mechanism types andre@0: * supported by a token. */ andre@0: CK_RV NSC_GetMechanismList(CK_SLOT_ID slotID, andre@0: CK_MECHANISM_TYPE_PTR pMechanismList, CK_ULONG_PTR pulCount) andre@0: { andre@0: CK_ULONG i; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: switch (slotID) { andre@0: /* default: */ andre@0: case NETSCAPE_SLOT_ID: andre@0: *pulCount = mechanismCount; andre@0: if (pMechanismList != NULL) { andre@0: for (i=0; i < mechanismCount; i++) { andre@0: pMechanismList[i] = mechanisms[i].type; andre@0: } andre@0: } andre@0: break; andre@0: default: andre@0: *pulCount = 0; andre@0: for (i=0; i < mechanismCount; i++) { andre@0: if (mechanisms[i].privkey) { andre@0: (*pulCount)++; andre@0: if (pMechanismList != NULL) { andre@0: *pMechanismList++ = mechanisms[i].type; andre@0: } andre@0: } andre@0: } andre@0: break; andre@0: } andre@0: return CKR_OK; andre@0: } andre@0: andre@0: andre@0: /* NSC_GetMechanismInfo obtains information about a particular mechanism andre@0: * possibly supported by a token. */ andre@0: CK_RV NSC_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type, andre@0: CK_MECHANISM_INFO_PTR pInfo) andre@0: { andre@0: PRBool isPrivateKey; andre@0: CK_ULONG i; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: switch (slotID) { andre@0: case NETSCAPE_SLOT_ID: andre@0: isPrivateKey = PR_FALSE; andre@0: break; andre@0: default: andre@0: isPrivateKey = PR_TRUE; andre@0: break; andre@0: } andre@0: for (i=0; i < mechanismCount; i++) { andre@0: if (type == mechanisms[i].type) { andre@0: if (isPrivateKey && !mechanisms[i].privkey) { andre@0: return CKR_MECHANISM_INVALID; andre@0: } andre@0: PORT_Memcpy(pInfo,&mechanisms[i].info, sizeof(CK_MECHANISM_INFO)); andre@0: return CKR_OK; andre@0: } andre@0: } andre@0: return CKR_MECHANISM_INVALID; andre@0: } andre@0: andre@0: CK_RV sftk_MechAllowsOperation(CK_MECHANISM_TYPE type, CK_ATTRIBUTE_TYPE op) andre@0: { andre@0: CK_ULONG i; andre@0: CK_FLAGS flags; andre@0: andre@0: switch (op) { andre@0: case CKA_ENCRYPT: flags = CKF_ENCRYPT; break; andre@0: case CKA_DECRYPT: flags = CKF_DECRYPT; break; andre@0: case CKA_WRAP: flags = CKF_WRAP; break; andre@0: case CKA_UNWRAP: flags = CKF_UNWRAP; break; andre@0: case CKA_SIGN: flags = CKF_SIGN; break; andre@0: case CKA_SIGN_RECOVER: flags = CKF_SIGN_RECOVER; break; andre@0: case CKA_VERIFY: flags = CKF_VERIFY; break; andre@0: case CKA_VERIFY_RECOVER: flags = CKF_VERIFY_RECOVER; break; andre@0: case CKA_DERIVE: flags = CKF_DERIVE; break; andre@0: default: andre@0: return CKR_ARGUMENTS_BAD; andre@0: } andre@0: for (i=0; i < mechanismCount; i++) { andre@0: if (type == mechanisms[i].type) { andre@0: return (flags & mechanisms[i].info.flags) ? CKR_OK andre@0: : CKR_MECHANISM_INVALID; andre@0: } andre@0: } andre@0: return CKR_MECHANISM_INVALID; andre@0: } andre@0: andre@0: /* NSC_InitToken initializes a token. */ andre@0: CK_RV NSC_InitToken(CK_SLOT_ID slotID,CK_CHAR_PTR pPin, andre@0: CK_ULONG ulPinLen,CK_CHAR_PTR pLabel) { andre@0: SFTKSlot *slot = sftk_SlotFromID(slotID, PR_FALSE); andre@0: SFTKDBHandle *handle; andre@0: SFTKDBHandle *certHandle; andre@0: SECStatus rv; andre@0: unsigned int i; andre@0: SFTKObject *object; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: if (slot == NULL) return CKR_SLOT_ID_INVALID; andre@0: andre@0: /* don't initialize the database if we aren't talking to a token andre@0: * that uses the key database. andre@0: */ andre@0: if (slotID == NETSCAPE_SLOT_ID) { andre@0: return CKR_TOKEN_WRITE_PROTECTED; andre@0: } andre@0: andre@0: /* first, delete all our loaded key and cert objects from our andre@0: * internal list. */ andre@0: PZ_Lock(slot->objectLock); andre@0: for (i=0; i < slot->sessObjHashSize; i++) { andre@0: do { andre@0: object = slot->sessObjHashTable[i]; andre@0: /* hand deque */ andre@0: /* this duplicates function of NSC_close session functions, but andre@0: * because we know that we are freeing all the sessions, we can andre@0: * do more efficient processing */ andre@0: if (object) { andre@0: slot->sessObjHashTable[i] = object->next; andre@0: andre@0: if (object->next) object->next->prev = NULL; andre@0: object->next = object->prev = NULL; andre@0: } andre@0: if (object) sftk_FreeObject(object); andre@0: } while (object != NULL); andre@0: } andre@0: slot->DB_loaded = PR_FALSE; andre@0: PZ_Unlock(slot->objectLock); andre@0: andre@0: /* then clear out the key database */ andre@0: handle = sftk_getKeyDB(slot); andre@0: if (handle == NULL) { andre@0: return CKR_TOKEN_WRITE_PROTECTED; andre@0: } andre@0: andre@0: rv = sftkdb_ResetKeyDB(handle); andre@0: sftk_freeDB(handle); andre@0: if (rv != SECSuccess) { andre@0: return CKR_DEVICE_ERROR; andre@0: } andre@0: andre@0: /* finally mark all the user certs as non-user certs */ andre@0: certHandle = sftk_getCertDB(slot); andre@0: if (certHandle == NULL) return CKR_OK; andre@0: andre@0: sftk_freeDB(certHandle); andre@0: andre@0: return CKR_OK; /*is this the right function for not implemented*/ andre@0: } andre@0: andre@0: andre@0: /* NSC_InitPIN initializes the normal user's PIN. */ andre@0: CK_RV NSC_InitPIN(CK_SESSION_HANDLE hSession, andre@0: CK_CHAR_PTR pPin, CK_ULONG ulPinLen) andre@0: { andre@0: SFTKSession *sp = NULL; andre@0: SFTKSlot *slot; andre@0: SFTKDBHandle *handle = NULL; andre@0: char newPinStr[SFTK_MAX_PIN+1]; andre@0: SECStatus rv; andre@0: CK_RV crv = CKR_SESSION_HANDLE_INVALID; andre@0: PRBool tokenRemoved = PR_FALSE; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: sp = sftk_SessionFromHandle(hSession); andre@0: if (sp == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: slot = sftk_SlotFromSession(sp); andre@0: if (slot == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: handle = sftk_getKeyDB(slot); andre@0: if (handle == NULL) { andre@0: crv = CKR_PIN_LEN_RANGE; andre@0: goto loser; andre@0: } andre@0: andre@0: andre@0: if (sp->info.state != CKS_RW_SO_FUNCTIONS) { andre@0: crv = CKR_USER_NOT_LOGGED_IN; andre@0: goto loser; andre@0: } andre@0: andre@0: sftk_FreeSession(sp); andre@0: sp = NULL; andre@0: andre@0: /* make sure the pins aren't too long */ andre@0: if (ulPinLen > SFTK_MAX_PIN) { andre@0: crv = CKR_PIN_LEN_RANGE; andre@0: goto loser; andre@0: } andre@0: if (ulPinLen < (CK_ULONG)slot->minimumPinLen) { andre@0: crv = CKR_PIN_LEN_RANGE; andre@0: goto loser; andre@0: } andre@0: andre@0: if (sftkdb_HasPasswordSet(handle) != SECFailure) { andre@0: crv = CKR_DEVICE_ERROR; andre@0: goto loser; andre@0: } andre@0: andre@0: /* convert to null terminated string */ andre@0: PORT_Memcpy(newPinStr, pPin, ulPinLen); andre@0: newPinStr[ulPinLen] = 0; andre@0: andre@0: /* build the hashed pins which we pass around */ andre@0: andre@0: /* change the data base */ andre@0: rv = sftkdb_ChangePassword(handle, NULL, newPinStr, &tokenRemoved); andre@0: if (tokenRemoved) { andre@0: sftk_CloseAllSessions(slot, PR_FALSE); andre@0: } andre@0: sftk_freeDB(handle); andre@0: handle = NULL; andre@0: andre@0: /* Now update our local copy of the pin */ andre@0: if (rv == SECSuccess) { andre@0: if (ulPinLen == 0) slot->needLogin = PR_FALSE; andre@0: return CKR_OK; andre@0: } andre@0: crv = CKR_PIN_INCORRECT; andre@0: andre@0: loser: andre@0: if (sp) { andre@0: sftk_FreeSession(sp); andre@0: } andre@0: if (handle) { andre@0: sftk_freeDB(handle); andre@0: } andre@0: return crv; andre@0: } andre@0: andre@0: andre@0: /* NSC_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 NSC_SetPIN(CK_SESSION_HANDLE hSession, CK_CHAR_PTR pOldPin, andre@0: CK_ULONG ulOldLen, CK_CHAR_PTR pNewPin, CK_ULONG ulNewLen) andre@0: { andre@0: SFTKSession *sp = NULL; andre@0: SFTKSlot *slot; andre@0: SFTKDBHandle *handle = NULL; andre@0: char newPinStr[SFTK_MAX_PIN+1],oldPinStr[SFTK_MAX_PIN+1]; andre@0: SECStatus rv; andre@0: CK_RV crv = CKR_SESSION_HANDLE_INVALID; andre@0: PRBool tokenRemoved = PR_FALSE; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: sp = sftk_SessionFromHandle(hSession); andre@0: if (sp == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: slot = sftk_SlotFromSession(sp); andre@0: if (!slot) { andre@0: goto loser; andre@0: } andre@0: andre@0: handle = sftk_getKeyDB(slot); andre@0: if (handle == NULL) { andre@0: sftk_FreeSession(sp); andre@0: return CKR_PIN_LEN_RANGE; /* XXX FIXME wrong return value */ andre@0: } andre@0: andre@0: if (slot->needLogin && sp->info.state != CKS_RW_USER_FUNCTIONS) { andre@0: crv = CKR_USER_NOT_LOGGED_IN; andre@0: goto loser; andre@0: } andre@0: andre@0: sftk_FreeSession(sp); andre@0: sp = NULL; andre@0: andre@0: /* make sure the pins aren't too long */ andre@0: if ((ulNewLen > SFTK_MAX_PIN) || (ulOldLen > SFTK_MAX_PIN)) { andre@0: crv = CKR_PIN_LEN_RANGE; andre@0: goto loser; andre@0: } andre@0: if (ulNewLen < (CK_ULONG)slot->minimumPinLen) { andre@0: crv = CKR_PIN_LEN_RANGE; andre@0: goto loser; andre@0: } andre@0: andre@0: andre@0: /* convert to null terminated string */ andre@0: PORT_Memcpy(newPinStr,pNewPin,ulNewLen); andre@0: newPinStr[ulNewLen] = 0; andre@0: PORT_Memcpy(oldPinStr,pOldPin,ulOldLen); andre@0: oldPinStr[ulOldLen] = 0; andre@0: andre@0: /* change the data base password */ andre@0: PR_Lock(slot->pwCheckLock); andre@0: rv = sftkdb_ChangePassword(handle, oldPinStr, newPinStr, &tokenRemoved); andre@0: if (tokenRemoved) { andre@0: sftk_CloseAllSessions(slot, PR_FALSE); andre@0: } andre@0: if ((rv != SECSuccess) && (slot->slotID == FIPS_SLOT_ID)) { andre@0: PR_Sleep(loginWaitTime); andre@0: } andre@0: PR_Unlock(slot->pwCheckLock); andre@0: andre@0: /* Now update our local copy of the pin */ andre@0: if (rv == SECSuccess) { andre@0: slot->needLogin = (PRBool)(ulNewLen != 0); andre@0: /* Reset login flags. */ andre@0: if (ulNewLen == 0) { andre@0: PRBool tokenRemoved = PR_FALSE; andre@0: PZ_Lock(slot->slotLock); andre@0: slot->isLoggedIn = PR_FALSE; andre@0: slot->ssoLoggedIn = PR_FALSE; andre@0: PZ_Unlock(slot->slotLock); andre@0: andre@0: rv = sftkdb_CheckPassword(handle, "", &tokenRemoved); andre@0: if (tokenRemoved) { andre@0: sftk_CloseAllSessions(slot, PR_FALSE); andre@0: } andre@0: } andre@0: sftk_update_all_states(slot); andre@0: sftk_freeDB(handle); andre@0: return CKR_OK; andre@0: } andre@0: crv = CKR_PIN_INCORRECT; andre@0: loser: andre@0: if (sp) { andre@0: sftk_FreeSession(sp); andre@0: } andre@0: if (handle) { andre@0: sftk_freeDB(handle); andre@0: } andre@0: return crv; andre@0: } andre@0: andre@0: /* NSC_OpenSession opens a session between an application and a token. */ andre@0: CK_RV NSC_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags, andre@0: CK_VOID_PTR pApplication,CK_NOTIFY Notify,CK_SESSION_HANDLE_PTR phSession) andre@0: { andre@0: SFTKSlot *slot; andre@0: CK_SESSION_HANDLE sessionID; andre@0: SFTKSession *session; andre@0: SFTKSession *sameID; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: slot = sftk_SlotFromID(slotID, PR_FALSE); andre@0: if (slot == NULL) return CKR_SLOT_ID_INVALID; andre@0: andre@0: /* new session (we only have serial sessions) */ andre@0: session = sftk_NewSession(slotID, Notify, pApplication, andre@0: flags | CKF_SERIAL_SESSION); andre@0: if (session == NULL) return CKR_HOST_MEMORY; andre@0: andre@0: if (slot->readOnly && (flags & CKF_RW_SESSION)) { andre@0: /* NETSCAPE_SLOT_ID is Read ONLY */ andre@0: session->info.flags &= ~CKF_RW_SESSION; andre@0: } andre@0: PZ_Lock(slot->slotLock); andre@0: ++slot->sessionCount; andre@0: PZ_Unlock(slot->slotLock); andre@0: if (session->info.flags & CKF_RW_SESSION) { andre@0: PR_ATOMIC_INCREMENT(&slot->rwSessionCount); andre@0: } andre@0: andre@0: do { andre@0: PZLock *lock; andre@0: do { andre@0: sessionID = (PR_ATOMIC_INCREMENT(&slot->sessionIDCount) & 0xffffff) andre@0: | (slot->index << 24); andre@0: } while (sessionID == CK_INVALID_HANDLE); andre@0: lock = SFTK_SESSION_LOCK(slot,sessionID); andre@0: PZ_Lock(lock); andre@0: sftkqueue_find(sameID, sessionID, slot->head, slot->sessHashSize); andre@0: if (sameID == NULL) { andre@0: session->handle = sessionID; andre@0: sftk_update_state(slot, session); andre@0: sftkqueue_add(session, sessionID, slot->head,slot->sessHashSize); andre@0: } else { andre@0: slot->sessionIDConflict++; /* for debugging */ andre@0: } andre@0: PZ_Unlock(lock); andre@0: } while (sameID != NULL); andre@0: andre@0: *phSession = sessionID; andre@0: return CKR_OK; andre@0: } andre@0: andre@0: andre@0: /* NSC_CloseSession closes a session between an application and a token. */ andre@0: CK_RV NSC_CloseSession(CK_SESSION_HANDLE hSession) andre@0: { andre@0: SFTKSlot *slot; andre@0: SFTKSession *session; andre@0: PRBool sessionFound; andre@0: PZLock *lock; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: session = sftk_SessionFromHandle(hSession); andre@0: if (session == NULL) return CKR_SESSION_HANDLE_INVALID; andre@0: slot = sftk_SlotFromSession(session); andre@0: sessionFound = PR_FALSE; andre@0: andre@0: /* lock */ andre@0: lock = SFTK_SESSION_LOCK(slot,hSession); andre@0: PZ_Lock(lock); andre@0: if (sftkqueue_is_queued(session,hSession,slot->head,slot->sessHashSize)) { andre@0: sessionFound = PR_TRUE; andre@0: sftkqueue_delete(session,hSession,slot->head,slot->sessHashSize); andre@0: session->refCount--; /* can't go to zero while we hold the reference */ andre@0: PORT_Assert(session->refCount > 0); andre@0: } andre@0: PZ_Unlock(lock); andre@0: andre@0: if (sessionFound) { andre@0: SFTKDBHandle *handle; andre@0: handle = sftk_getKeyDB(slot); andre@0: PZ_Lock(slot->slotLock); andre@0: if (--slot->sessionCount == 0) { andre@0: slot->isLoggedIn = PR_FALSE; andre@0: if (slot->needLogin && handle) { andre@0: sftkdb_ClearPassword(handle); andre@0: } andre@0: } andre@0: PZ_Unlock(slot->slotLock); andre@0: if (handle) { andre@0: sftk_freeDB(handle); andre@0: } andre@0: if (session->info.flags & CKF_RW_SESSION) { andre@0: PR_ATOMIC_DECREMENT(&slot->rwSessionCount); andre@0: } andre@0: } andre@0: andre@0: sftk_FreeSession(session); andre@0: return CKR_OK; andre@0: } andre@0: andre@0: andre@0: /* NSC_CloseAllSessions closes all sessions with a token. */ andre@0: CK_RV NSC_CloseAllSessions (CK_SLOT_ID slotID) andre@0: { andre@0: SFTKSlot *slot; andre@0: andre@0: #ifndef NO_FORK_CHECK andre@0: /* skip fork check if we are being called from C_Initialize or C_Finalize */ andre@0: if (!parentForkedAfterC_Initialize) { andre@0: CHECK_FORK(); andre@0: } andre@0: #endif andre@0: andre@0: slot = sftk_SlotFromID(slotID, PR_FALSE); andre@0: if (slot == NULL) return CKR_SLOT_ID_INVALID; andre@0: andre@0: return sftk_CloseAllSessions(slot, PR_TRUE); andre@0: } andre@0: andre@0: andre@0: andre@0: /* NSC_GetSessionInfo obtains information about the session. */ andre@0: CK_RV NSC_GetSessionInfo(CK_SESSION_HANDLE hSession, andre@0: CK_SESSION_INFO_PTR pInfo) andre@0: { andre@0: SFTKSession *session; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: session = sftk_SessionFromHandle(hSession); andre@0: if (session == NULL) return CKR_SESSION_HANDLE_INVALID; andre@0: andre@0: PORT_Memcpy(pInfo,&session->info,sizeof(CK_SESSION_INFO)); andre@0: sftk_FreeSession(session); andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* NSC_Login logs a user into a token. */ andre@0: CK_RV NSC_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType, andre@0: CK_CHAR_PTR pPin, CK_ULONG ulPinLen) andre@0: { andre@0: SFTKSlot *slot; andre@0: SFTKSession *session; andre@0: SFTKDBHandle *handle; andre@0: CK_FLAGS sessionFlags; andre@0: SECStatus rv; andre@0: CK_RV crv; andre@0: char pinStr[SFTK_MAX_PIN+1]; andre@0: PRBool tokenRemoved = PR_FALSE; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: /* get the slot */ andre@0: slot = sftk_SlotFromSessionHandle(hSession); andre@0: if (slot == NULL) { andre@0: return CKR_SESSION_HANDLE_INVALID; andre@0: } andre@0: andre@0: /* make sure the session is valid */ andre@0: session = sftk_SessionFromHandle(hSession); andre@0: if (session == NULL) { andre@0: return CKR_SESSION_HANDLE_INVALID; andre@0: } andre@0: sessionFlags = session->info.flags; andre@0: sftk_FreeSession(session); andre@0: session = NULL; andre@0: andre@0: /* can't log into the Netscape Slot */ andre@0: if (slot->slotID == NETSCAPE_SLOT_ID) { andre@0: return CKR_USER_TYPE_INVALID; andre@0: } andre@0: andre@0: if (slot->isLoggedIn) return CKR_USER_ALREADY_LOGGED_IN; andre@0: if (!slot->needLogin) { andre@0: return ulPinLen ? CKR_PIN_INCORRECT : CKR_OK; andre@0: } andre@0: slot->ssoLoggedIn = PR_FALSE; andre@0: andre@0: if (ulPinLen > SFTK_MAX_PIN) return CKR_PIN_LEN_RANGE; andre@0: andre@0: /* convert to null terminated string */ andre@0: PORT_Memcpy(pinStr,pPin,ulPinLen); andre@0: pinStr[ulPinLen] = 0; andre@0: andre@0: handle = sftk_getKeyDB(slot); andre@0: if (handle == NULL) { andre@0: return CKR_USER_TYPE_INVALID; andre@0: } andre@0: andre@0: /* andre@0: * Deal with bootstrap. We allow the SSO to login in with a NULL andre@0: * password if and only if we haven't initialized the KEY DB yet. andre@0: * We only allow this on a RW session. andre@0: */ andre@0: rv = sftkdb_HasPasswordSet(handle); andre@0: if (rv == SECFailure) { andre@0: /* allow SSO's to log in only if there is not password on the andre@0: * key database */ andre@0: if (((userType == CKU_SO) && (sessionFlags & CKF_RW_SESSION)) andre@0: /* fips always needs to authenticate, even if there isn't a db */ andre@0: || (slot->slotID == FIPS_SLOT_ID)) { andre@0: /* should this be a fixed password? */ andre@0: if (ulPinLen == 0) { andre@0: sftkdb_ClearPassword(handle); andre@0: PZ_Lock(slot->slotLock); andre@0: slot->isLoggedIn = PR_TRUE; andre@0: slot->ssoLoggedIn = (PRBool)(userType == CKU_SO); andre@0: PZ_Unlock(slot->slotLock); andre@0: sftk_update_all_states(slot); andre@0: crv = CKR_OK; andre@0: goto done; andre@0: } andre@0: crv = CKR_PIN_INCORRECT; andre@0: goto done; andre@0: } andre@0: crv = CKR_USER_TYPE_INVALID; andre@0: goto done; andre@0: } andre@0: andre@0: /* don't allow the SSO to log in if the user is already initialized */ andre@0: if (userType != CKU_USER) { andre@0: crv = CKR_USER_TYPE_INVALID; andre@0: goto done; andre@0: } andre@0: andre@0: andre@0: /* build the hashed pins which we pass around */ andre@0: PR_Lock(slot->pwCheckLock); andre@0: rv = sftkdb_CheckPassword(handle,pinStr, &tokenRemoved); andre@0: if (tokenRemoved) { andre@0: sftk_CloseAllSessions(slot, PR_FALSE); andre@0: } andre@0: if ((rv != SECSuccess) && (slot->slotID == FIPS_SLOT_ID)) { andre@0: PR_Sleep(loginWaitTime); andre@0: } andre@0: PR_Unlock(slot->pwCheckLock); andre@0: if (rv == SECSuccess) { andre@0: PZ_Lock(slot->slotLock); andre@0: /* make sure the login state matches the underlying andre@0: * database state */ andre@0: slot->isLoggedIn = sftkdb_PWCached(handle) == SECSuccess ? andre@0: PR_TRUE : PR_FALSE; andre@0: PZ_Unlock(slot->slotLock); andre@0: andre@0: sftk_freeDB(handle); andre@0: handle = NULL; andre@0: andre@0: /* update all sessions */ andre@0: sftk_update_all_states(slot); andre@0: return CKR_OK; andre@0: } andre@0: andre@0: crv = CKR_PIN_INCORRECT; andre@0: done: andre@0: if (handle) { andre@0: sftk_freeDB(handle); andre@0: } andre@0: return crv; andre@0: } andre@0: andre@0: /* NSC_Logout logs a user out from a token. */ andre@0: CK_RV NSC_Logout(CK_SESSION_HANDLE hSession) andre@0: { andre@0: SFTKSlot *slot = sftk_SlotFromSessionHandle(hSession); andre@0: SFTKSession *session; andre@0: SFTKDBHandle *handle; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: if (slot == NULL) { andre@0: return CKR_SESSION_HANDLE_INVALID; andre@0: } andre@0: session = sftk_SessionFromHandle(hSession); andre@0: if (session == NULL) return CKR_SESSION_HANDLE_INVALID; andre@0: sftk_FreeSession(session); andre@0: session = NULL; andre@0: andre@0: if (!slot->isLoggedIn) return CKR_USER_NOT_LOGGED_IN; andre@0: andre@0: handle = sftk_getKeyDB(slot); andre@0: PZ_Lock(slot->slotLock); andre@0: slot->isLoggedIn = PR_FALSE; andre@0: slot->ssoLoggedIn = PR_FALSE; andre@0: if (slot->needLogin && handle) { andre@0: sftkdb_ClearPassword(handle); andre@0: } andre@0: PZ_Unlock(slot->slotLock); andre@0: if (handle) { andre@0: sftk_freeDB(handle); andre@0: } andre@0: andre@0: sftk_update_all_states(slot); andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* andre@0: * Create or remove a new slot on the fly. andre@0: * When creating a slot, "slot" is the slot that the request came from. The andre@0: * resulting slot will live in the same module as "slot". andre@0: * When removing a slot, "slot" is the slot to be removed. andre@0: * "object" is the creation object that specifies the module spec for the slot andre@0: * to add or remove. andre@0: */ andre@0: static CK_RV sftk_CreateNewSlot(SFTKSlot *slot, CK_OBJECT_CLASS class, andre@0: SFTKObject *object) andre@0: { andre@0: PRBool isValidUserSlot = PR_FALSE; andre@0: PRBool isValidFIPSUserSlot = PR_FALSE; andre@0: PRBool isValidSlot = PR_FALSE; andre@0: PRBool isFIPS = PR_FALSE; andre@0: unsigned long moduleIndex; andre@0: SFTKAttribute *attribute; andre@0: sftk_parameters paramStrings; andre@0: char *paramString; andre@0: CK_SLOT_ID slotID = 0; andre@0: SFTKSlot *newSlot = NULL; andre@0: CK_RV crv = CKR_OK; andre@0: andre@0: if (class != CKO_NETSCAPE_DELSLOT && class != CKO_NETSCAPE_NEWSLOT) { andre@0: return CKR_ATTRIBUTE_VALUE_INVALID; andre@0: } andre@0: if (class == CKO_NETSCAPE_NEWSLOT && slot->slotID == FIPS_SLOT_ID) { andre@0: isFIPS = PR_TRUE; andre@0: } andre@0: attribute = sftk_FindAttribute(object, CKA_NETSCAPE_MODULE_SPEC); andre@0: if (attribute == NULL) { andre@0: return CKR_TEMPLATE_INCOMPLETE; andre@0: } andre@0: paramString = (char *)attribute->attrib.pValue; andre@0: crv = sftk_parseParameters(paramString, ¶mStrings, isFIPS); andre@0: if (crv != CKR_OK) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* enforce only one at a time */ andre@0: if (paramStrings.token_count != 1) { andre@0: crv = CKR_ATTRIBUTE_VALUE_INVALID; andre@0: goto loser; andre@0: } andre@0: andre@0: slotID = paramStrings.tokens[0].slotID; andre@0: andre@0: /* stay within the valid ID space */ andre@0: isValidUserSlot = (slotID >= SFTK_MIN_USER_SLOT_ID && andre@0: slotID <= SFTK_MAX_USER_SLOT_ID); andre@0: isValidFIPSUserSlot = (slotID >= SFTK_MIN_FIPS_USER_SLOT_ID && andre@0: slotID <= SFTK_MAX_FIPS_USER_SLOT_ID); andre@0: andre@0: if (class == CKO_NETSCAPE_DELSLOT) { andre@0: if (slot->slotID == slotID) { andre@0: isValidSlot = isValidUserSlot || isValidFIPSUserSlot; andre@0: } andre@0: } else { andre@0: /* only the crypto or FIPS slots can create new slot objects */ andre@0: if (slot->slotID == NETSCAPE_SLOT_ID) { andre@0: isValidSlot = isValidUserSlot; andre@0: moduleIndex = NSC_NON_FIPS_MODULE; andre@0: } else if (slot->slotID == FIPS_SLOT_ID) { andre@0: isValidSlot = isValidFIPSUserSlot; andre@0: moduleIndex = NSC_FIPS_MODULE; andre@0: } andre@0: } andre@0: andre@0: if (!isValidSlot) { andre@0: crv = CKR_ATTRIBUTE_VALUE_INVALID; andre@0: goto loser; andre@0: } andre@0: andre@0: /* unload any existing slot at this id */ andre@0: newSlot = sftk_SlotFromID(slotID, PR_TRUE); andre@0: if (newSlot && newSlot->present) { andre@0: crv = SFTK_ShutdownSlot(newSlot); andre@0: if (crv != CKR_OK) { andre@0: goto loser; andre@0: } andre@0: } andre@0: andre@0: /* if we were just planning on deleting the slot, then do so now */ andre@0: if (class == CKO_NETSCAPE_DELSLOT) { andre@0: /* sort of a unconventional use of this error code, be we are andre@0: * overusing CKR_ATTRIBUTE_VALUE_INVALID, and it does apply */ andre@0: crv = newSlot ? CKR_OK : CKR_SLOT_ID_INVALID; andre@0: goto loser; /* really exit */ andre@0: } andre@0: andre@0: if (newSlot) { andre@0: crv = SFTK_SlotReInit(newSlot, paramStrings.configdir, andre@0: paramStrings.updatedir, paramStrings.updateID, andre@0: ¶mStrings.tokens[0], moduleIndex); andre@0: } else { andre@0: crv = SFTK_SlotInit(paramStrings.configdir, andre@0: paramStrings.updatedir, paramStrings.updateID, andre@0: ¶mStrings.tokens[0], moduleIndex); andre@0: } andre@0: andre@0: loser: andre@0: sftk_freeParams(¶mStrings); andre@0: sftk_FreeAttribute(attribute); andre@0: andre@0: return crv; andre@0: } andre@0: andre@0: andre@0: /* NSC_CreateObject creates a new object. */ andre@0: CK_RV NSC_CreateObject(CK_SESSION_HANDLE hSession, andre@0: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, andre@0: CK_OBJECT_HANDLE_PTR phObject) andre@0: { andre@0: SFTKSlot *slot = sftk_SlotFromSessionHandle(hSession); andre@0: SFTKSession *session; andre@0: SFTKObject *object; andre@0: /* make sure class isn't randomly CKO_NETSCAPE_NEWSLOT or andre@0: * CKO_NETSCPE_DELSLOT. */ andre@0: CK_OBJECT_CLASS class = CKO_VENDOR_DEFINED; andre@0: CK_RV crv; andre@0: int i; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: *phObject = CK_INVALID_HANDLE; andre@0: andre@0: if (slot == NULL) { andre@0: return CKR_SESSION_HANDLE_INVALID; andre@0: } andre@0: /* andre@0: * now lets create an object to hang the attributes off of andre@0: */ andre@0: object = sftk_NewObject(slot); /* fill in the handle later */ andre@0: if (object == NULL) { andre@0: return CKR_HOST_MEMORY; andre@0: } andre@0: andre@0: /* andre@0: * load the template values into the object andre@0: */ andre@0: for (i=0; i < (int) ulCount; i++) { andre@0: crv = sftk_AddAttributeType(object,sftk_attr_expand(&pTemplate[i])); andre@0: if (crv != CKR_OK) { andre@0: sftk_FreeObject(object); andre@0: return crv; andre@0: } andre@0: if ((pTemplate[i].type == CKA_CLASS) && pTemplate[i].pValue) { andre@0: class = *(CK_OBJECT_CLASS *)pTemplate[i].pValue; andre@0: } andre@0: } andre@0: andre@0: /* get the session */ andre@0: session = sftk_SessionFromHandle(hSession); andre@0: if (session == NULL) { andre@0: sftk_FreeObject(object); andre@0: return CKR_SESSION_HANDLE_INVALID; andre@0: } andre@0: andre@0: /* andre@0: * handle pseudo objects (CKO_NEWSLOT) andre@0: */ andre@0: if ((class == CKO_NETSCAPE_NEWSLOT) || (class == CKO_NETSCAPE_DELSLOT)) { andre@0: crv = sftk_CreateNewSlot(slot, class, object); andre@0: goto done; andre@0: } andre@0: andre@0: /* andre@0: * handle the base object stuff andre@0: */ andre@0: crv = sftk_handleObject(object,session); andre@0: *phObject = object->handle; andre@0: done: andre@0: sftk_FreeSession(session); andre@0: sftk_FreeObject(object); andre@0: andre@0: return crv; andre@0: } andre@0: andre@0: andre@0: andre@0: /* NSC_CopyObject copies an object, creating a new object for the copy. */ andre@0: CK_RV NSC_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: { andre@0: SFTKObject *destObject,*srcObject; andre@0: SFTKSession *session; andre@0: CK_RV crv = CKR_OK; andre@0: SFTKSlot *slot = sftk_SlotFromSessionHandle(hSession); andre@0: int i; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: if (slot == NULL) { andre@0: return CKR_SESSION_HANDLE_INVALID; andre@0: } andre@0: /* Get srcObject so we can find the class */ andre@0: session = sftk_SessionFromHandle(hSession); andre@0: if (session == NULL) { andre@0: return CKR_SESSION_HANDLE_INVALID; andre@0: } andre@0: srcObject = sftk_ObjectFromHandle(hObject,session); andre@0: if (srcObject == NULL) { andre@0: sftk_FreeSession(session); andre@0: return CKR_OBJECT_HANDLE_INVALID; andre@0: } andre@0: /* andre@0: * create an object to hang the attributes off of andre@0: */ andre@0: destObject = sftk_NewObject(slot); /* fill in the handle later */ andre@0: if (destObject == NULL) { andre@0: sftk_FreeSession(session); andre@0: sftk_FreeObject(srcObject); andre@0: return CKR_HOST_MEMORY; andre@0: } andre@0: andre@0: /* andre@0: * load the template values into the object andre@0: */ andre@0: for (i=0; i < (int) ulCount; i++) { andre@0: if (sftk_modifyType(pTemplate[i].type,srcObject->objclass) == SFTK_NEVER) { andre@0: crv = CKR_ATTRIBUTE_READ_ONLY; andre@0: break; andre@0: } andre@0: crv = sftk_AddAttributeType(destObject,sftk_attr_expand(&pTemplate[i])); andre@0: if (crv != CKR_OK) { break; } andre@0: } andre@0: if (crv != CKR_OK) { andre@0: sftk_FreeSession(session); andre@0: sftk_FreeObject(srcObject); andre@0: sftk_FreeObject(destObject); andre@0: return crv; andre@0: } andre@0: andre@0: /* sensitive can only be changed to CK_TRUE */ andre@0: if (sftk_hasAttribute(destObject,CKA_SENSITIVE)) { andre@0: if (!sftk_isTrue(destObject,CKA_SENSITIVE)) { andre@0: sftk_FreeSession(session); andre@0: sftk_FreeObject(srcObject); andre@0: sftk_FreeObject(destObject); andre@0: return CKR_ATTRIBUTE_READ_ONLY; andre@0: } andre@0: } andre@0: andre@0: /* andre@0: * now copy the old attributes from the new attributes andre@0: */ andre@0: /* don't create a token object if we aren't in a rw session */ andre@0: /* we need to hold the lock to copy a consistant version of andre@0: * the object. */ andre@0: crv = sftk_CopyObject(destObject,srcObject); andre@0: andre@0: destObject->objclass = srcObject->objclass; andre@0: sftk_FreeObject(srcObject); andre@0: if (crv != CKR_OK) { andre@0: sftk_FreeObject(destObject); andre@0: sftk_FreeSession(session); andre@0: return crv; andre@0: } andre@0: andre@0: crv = sftk_handleObject(destObject,session); andre@0: *phNewObject = destObject->handle; andre@0: sftk_FreeSession(session); andre@0: sftk_FreeObject(destObject); andre@0: andre@0: return crv; andre@0: } andre@0: andre@0: andre@0: /* NSC_GetObjectSize gets the size of an object in bytes. */ andre@0: CK_RV NSC_GetObjectSize(CK_SESSION_HANDLE hSession, andre@0: CK_OBJECT_HANDLE hObject, CK_ULONG_PTR pulSize) andre@0: { andre@0: CHECK_FORK(); andre@0: andre@0: *pulSize = 0; andre@0: return CKR_OK; andre@0: } andre@0: andre@0: andre@0: /* NSC_GetAttributeValue obtains the value of one or more object attributes. */ andre@0: CK_RV NSC_GetAttributeValue(CK_SESSION_HANDLE hSession, andre@0: CK_OBJECT_HANDLE hObject,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulCount) andre@0: { andre@0: SFTKSlot *slot = sftk_SlotFromSessionHandle(hSession); andre@0: SFTKSession *session; andre@0: SFTKObject *object; andre@0: SFTKAttribute *attribute; andre@0: PRBool sensitive; andre@0: CK_RV crv; andre@0: int i; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: if (slot == NULL) { andre@0: return CKR_SESSION_HANDLE_INVALID; andre@0: } andre@0: /* andre@0: * make sure we're allowed andre@0: */ andre@0: session = sftk_SessionFromHandle(hSession); andre@0: if (session == NULL) { andre@0: return CKR_SESSION_HANDLE_INVALID; andre@0: } andre@0: andre@0: /* short circuit everything for token objects */ andre@0: if (sftk_isToken(hObject)) { andre@0: SFTKSlot *slot = sftk_SlotFromSession(session); andre@0: SFTKDBHandle *dbHandle = sftk_getDBForTokenObject(slot, hObject); andre@0: SFTKDBHandle *keydb = NULL; andre@0: andre@0: if (dbHandle == NULL) { andre@0: sftk_FreeSession(session); andre@0: return CKR_OBJECT_HANDLE_INVALID; andre@0: } andre@0: andre@0: crv = sftkdb_GetAttributeValue(dbHandle, hObject, pTemplate, ulCount); andre@0: andre@0: /* make sure we don't export any sensitive information */ andre@0: keydb = sftk_getKeyDB(slot); andre@0: if (dbHandle == keydb) { andre@0: for (i=0; i < (int) ulCount; i++) { andre@0: if (sftk_isSensitive(pTemplate[i].type,CKO_PRIVATE_KEY)) { andre@0: crv = CKR_ATTRIBUTE_SENSITIVE; andre@0: if (pTemplate[i].pValue && (pTemplate[i].ulValueLen!= -1)){ andre@0: PORT_Memset(pTemplate[i].pValue, 0, andre@0: pTemplate[i].ulValueLen); andre@0: } andre@0: pTemplate[i].ulValueLen = -1; andre@0: } andre@0: } andre@0: } andre@0: andre@0: sftk_FreeSession(session); andre@0: sftk_freeDB(dbHandle); andre@0: if (keydb) { andre@0: sftk_freeDB(keydb); andre@0: } andre@0: return crv; andre@0: } andre@0: andre@0: /* handle the session object */ andre@0: object = sftk_ObjectFromHandle(hObject,session); andre@0: sftk_FreeSession(session); andre@0: if (object == NULL) { andre@0: return CKR_OBJECT_HANDLE_INVALID; andre@0: } andre@0: andre@0: /* don't read a private object if we aren't logged in */ andre@0: if ((!slot->isLoggedIn) && (slot->needLogin) && andre@0: (sftk_isTrue(object,CKA_PRIVATE))) { andre@0: sftk_FreeObject(object); andre@0: return CKR_USER_NOT_LOGGED_IN; andre@0: } andre@0: andre@0: crv = CKR_OK; andre@0: sensitive = sftk_isTrue(object,CKA_SENSITIVE); andre@0: for (i=0; i < (int) ulCount; i++) { andre@0: /* Make sure that this attribute is retrievable */ andre@0: if (sensitive && sftk_isSensitive(pTemplate[i].type,object->objclass)) { andre@0: crv = CKR_ATTRIBUTE_SENSITIVE; andre@0: pTemplate[i].ulValueLen = -1; andre@0: continue; andre@0: } andre@0: attribute = sftk_FindAttribute(object,pTemplate[i].type); andre@0: if (attribute == NULL) { andre@0: crv = CKR_ATTRIBUTE_TYPE_INVALID; andre@0: pTemplate[i].ulValueLen = -1; andre@0: continue; andre@0: } andre@0: if (pTemplate[i].pValue != NULL) { andre@0: PORT_Memcpy(pTemplate[i].pValue,attribute->attrib.pValue, andre@0: attribute->attrib.ulValueLen); andre@0: } andre@0: pTemplate[i].ulValueLen = attribute->attrib.ulValueLen; andre@0: sftk_FreeAttribute(attribute); andre@0: } andre@0: andre@0: sftk_FreeObject(object); andre@0: return crv; andre@0: } andre@0: andre@0: /* NSC_SetAttributeValue modifies the value of one or more object attributes */ andre@0: CK_RV NSC_SetAttributeValue (CK_SESSION_HANDLE hSession, andre@0: CK_OBJECT_HANDLE hObject,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulCount) andre@0: { andre@0: SFTKSlot *slot = sftk_SlotFromSessionHandle(hSession); andre@0: SFTKSession *session; andre@0: SFTKAttribute *attribute; andre@0: SFTKObject *object; andre@0: PRBool isToken; andre@0: CK_RV crv = CKR_OK; andre@0: CK_BBOOL legal; andre@0: int i; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: if (slot == NULL) { andre@0: return CKR_SESSION_HANDLE_INVALID; andre@0: } andre@0: /* andre@0: * make sure we're allowed andre@0: */ andre@0: session = sftk_SessionFromHandle(hSession); andre@0: if (session == NULL) { andre@0: return CKR_SESSION_HANDLE_INVALID; andre@0: } andre@0: andre@0: object = sftk_ObjectFromHandle(hObject,session); andre@0: if (object == NULL) { andre@0: sftk_FreeSession(session); andre@0: return CKR_OBJECT_HANDLE_INVALID; andre@0: } andre@0: andre@0: /* don't modify a private object if we aren't logged in */ andre@0: if ((!slot->isLoggedIn) && (slot->needLogin) && andre@0: (sftk_isTrue(object,CKA_PRIVATE))) { andre@0: sftk_FreeSession(session); andre@0: sftk_FreeObject(object); andre@0: return CKR_USER_NOT_LOGGED_IN; andre@0: } andre@0: andre@0: /* don't modify a token object if we aren't in a rw session */ andre@0: isToken = sftk_isTrue(object,CKA_TOKEN); andre@0: if (((session->info.flags & CKF_RW_SESSION) == 0) && isToken) { andre@0: sftk_FreeSession(session); andre@0: sftk_FreeObject(object); andre@0: return CKR_SESSION_READ_ONLY; andre@0: } andre@0: sftk_FreeSession(session); andre@0: andre@0: /* only change modifiable objects */ andre@0: if (!sftk_isTrue(object,CKA_MODIFIABLE)) { andre@0: sftk_FreeObject(object); andre@0: return CKR_ATTRIBUTE_READ_ONLY; andre@0: } andre@0: andre@0: for (i=0; i < (int) ulCount; i++) { andre@0: /* Make sure that this attribute is changeable */ andre@0: switch (sftk_modifyType(pTemplate[i].type,object->objclass)) { andre@0: case SFTK_NEVER: andre@0: case SFTK_ONCOPY: andre@0: default: andre@0: crv = CKR_ATTRIBUTE_READ_ONLY; andre@0: break; andre@0: andre@0: case SFTK_SENSITIVE: andre@0: legal = (pTemplate[i].type == CKA_EXTRACTABLE) ? CK_FALSE : CK_TRUE; andre@0: if ((*(CK_BBOOL *)pTemplate[i].pValue) != legal) { andre@0: crv = CKR_ATTRIBUTE_READ_ONLY; andre@0: } andre@0: break; andre@0: case SFTK_ALWAYS: andre@0: break; andre@0: } andre@0: if (crv != CKR_OK) break; andre@0: andre@0: /* find the old attribute */ andre@0: attribute = sftk_FindAttribute(object,pTemplate[i].type); andre@0: if (attribute == NULL) { andre@0: crv =CKR_ATTRIBUTE_TYPE_INVALID; andre@0: break; andre@0: } andre@0: sftk_FreeAttribute(attribute); andre@0: crv = sftk_forceAttribute(object,sftk_attr_expand(&pTemplate[i])); andre@0: if (crv != CKR_OK) break; andre@0: andre@0: } andre@0: andre@0: sftk_FreeObject(object); andre@0: return crv; andre@0: } andre@0: andre@0: static CK_RV andre@0: sftk_expandSearchList(SFTKSearchResults *search, int count) andre@0: { andre@0: search->array_size += count; andre@0: search->handles = (CK_OBJECT_HANDLE *)PORT_Realloc(search->handles, andre@0: sizeof(CK_OBJECT_HANDLE)*search->array_size); andre@0: return search->handles ? CKR_OK : CKR_HOST_MEMORY; andre@0: } andre@0: andre@0: andre@0: andre@0: static CK_RV andre@0: sftk_searchDatabase(SFTKDBHandle *handle, SFTKSearchResults *search, andre@0: const CK_ATTRIBUTE *pTemplate, CK_ULONG ulCount) andre@0: { andre@0: CK_RV crv; andre@0: int objectListSize = search->array_size-search->size; andre@0: CK_OBJECT_HANDLE *array = &search->handles[search->size]; andre@0: SDBFind *find; andre@0: CK_ULONG count; andre@0: andre@0: crv = sftkdb_FindObjectsInit(handle, pTemplate, ulCount, &find); andre@0: if (crv != CKR_OK) andre@0: return crv; andre@0: do { andre@0: crv = sftkdb_FindObjects(handle, find, array, objectListSize, &count); andre@0: if ((crv != CKR_OK) || (count == 0)) andre@0: break; andre@0: search->size += count; andre@0: objectListSize -= count; andre@0: if (objectListSize > 0) andre@0: break; andre@0: crv = sftk_expandSearchList(search,NSC_SEARCH_BLOCK_SIZE); andre@0: objectListSize = NSC_SEARCH_BLOCK_SIZE; andre@0: array = &search->handles[search->size]; andre@0: } while (crv == CKR_OK); andre@0: sftkdb_FindObjectsFinal(handle, find); andre@0: andre@0: return crv; andre@0: } andre@0: andre@0: /* softoken used to search the SMimeEntries automatically instead of andre@0: * doing this in pk11wrap. This code should really be up in andre@0: * pk11wrap so that it will work with other tokens other than softoken. andre@0: */ andre@0: CK_RV andre@0: sftk_emailhack(SFTKSlot *slot, SFTKDBHandle *handle, andre@0: SFTKSearchResults *search, CK_ATTRIBUTE *pTemplate, CK_ULONG ulCount) andre@0: { andre@0: PRBool isCert = PR_FALSE; andre@0: int emailIndex = -1; andre@0: int i; andre@0: SFTKSearchResults smime_search; andre@0: CK_ATTRIBUTE smime_template[2]; andre@0: CK_OBJECT_CLASS smime_class = CKO_NETSCAPE_SMIME; andre@0: SFTKAttribute *attribute = NULL; andre@0: SFTKObject *object = NULL; andre@0: CK_RV crv = CKR_OK; andre@0: andre@0: andre@0: smime_search.handles = NULL; /* paranoia, some one is bound to add a goto andre@0: * loser before this gets initialized */ andre@0: andre@0: /* see if we are looking for email certs */ andre@0: for (i=0; i < ulCount; i++) { andre@0: if (pTemplate[i].type == CKA_CLASS) { andre@0: if ((pTemplate[i].ulValueLen != sizeof(CK_OBJECT_CLASS) || andre@0: (*(CK_OBJECT_CLASS *)pTemplate[i].pValue) != CKO_CERTIFICATE)) { andre@0: /* not a cert, skip out */ andre@0: break; andre@0: } andre@0: isCert = PR_TRUE; andre@0: } else if (pTemplate[i].type == CKA_NETSCAPE_EMAIL) { andre@0: emailIndex = i; andre@0: andre@0: } andre@0: if (isCert && (emailIndex != -1)) break; andre@0: } andre@0: andre@0: if (!isCert || (emailIndex == -1)) { andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* we are doing a cert and email search, find the SMimeEntry */ andre@0: smime_template[0].type = CKA_CLASS; andre@0: smime_template[0].pValue = &smime_class; andre@0: smime_template[0].ulValueLen = sizeof(smime_class); andre@0: smime_template[1] = pTemplate[emailIndex]; andre@0: andre@0: smime_search.handles = (CK_OBJECT_HANDLE *) andre@0: PORT_Alloc(sizeof(CK_OBJECT_HANDLE) * NSC_SEARCH_BLOCK_SIZE); andre@0: if (smime_search.handles == NULL) { andre@0: crv = CKR_HOST_MEMORY; andre@0: goto loser; andre@0: } andre@0: smime_search.index = 0; andre@0: smime_search.size = 0; andre@0: smime_search.array_size = NSC_SEARCH_BLOCK_SIZE; andre@0: andre@0: crv = sftk_searchDatabase(handle, &smime_search, smime_template, 2); andre@0: if (crv != CKR_OK || smime_search.size == 0) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* get the SMime subject */ andre@0: object = sftk_NewTokenObject(slot, NULL, smime_search.handles[0]); andre@0: if (object == NULL) { andre@0: crv = CKR_HOST_MEMORY; /* is there any other reason for this failure? */ andre@0: goto loser; andre@0: } andre@0: attribute = sftk_FindAttribute(object,CKA_SUBJECT); andre@0: if (attribute == NULL) { andre@0: crv = CKR_ATTRIBUTE_TYPE_INVALID; andre@0: goto loser; andre@0: } andre@0: andre@0: /* now find the certs with that subject */ andre@0: pTemplate[emailIndex] = attribute->attrib; andre@0: /* now add the appropriate certs to the search list */ andre@0: crv = sftk_searchDatabase(handle, search, pTemplate, ulCount); andre@0: pTemplate[emailIndex] = smime_template[1]; /* restore the user's template*/ andre@0: andre@0: loser: andre@0: if (attribute) { andre@0: sftk_FreeAttribute(attribute); andre@0: } andre@0: if (object) { andre@0: sftk_FreeObject(object); andre@0: } andre@0: if (smime_search.handles) { andre@0: PORT_Free(smime_search.handles); andre@0: } andre@0: andre@0: return crv; andre@0: } andre@0: andre@0: static void andre@0: sftk_pruneSearch(CK_ATTRIBUTE *pTemplate, CK_ULONG ulCount, andre@0: PRBool *searchCertDB, PRBool *searchKeyDB) { andre@0: CK_ULONG i; andre@0: andre@0: *searchCertDB = PR_TRUE; andre@0: *searchKeyDB = PR_TRUE; andre@0: for (i = 0; i < ulCount; i++) { andre@0: if (pTemplate[i].type == CKA_CLASS && pTemplate[i].pValue != NULL) { andre@0: CK_OBJECT_CLASS class = *((CK_OBJECT_CLASS*)pTemplate[i].pValue); andre@0: if (class == CKO_PRIVATE_KEY || class == CKO_SECRET_KEY) { andre@0: *searchCertDB = PR_FALSE; andre@0: } else { andre@0: *searchKeyDB = PR_FALSE; andre@0: } andre@0: break; andre@0: } andre@0: } andre@0: } andre@0: andre@0: static CK_RV andre@0: sftk_searchTokenList(SFTKSlot *slot, SFTKSearchResults *search, andre@0: CK_ATTRIBUTE *pTemplate, CK_ULONG ulCount, andre@0: PRBool *tokenOnly, PRBool isLoggedIn) andre@0: { andre@0: CK_RV crv = CKR_OK; andre@0: CK_RV crv2; andre@0: PRBool searchCertDB; andre@0: PRBool searchKeyDB; andre@0: andre@0: sftk_pruneSearch(pTemplate, ulCount, &searchCertDB, &searchKeyDB); andre@0: andre@0: if (searchCertDB) { andre@0: SFTKDBHandle *certHandle = sftk_getCertDB(slot); andre@0: crv = sftk_searchDatabase(certHandle, search, pTemplate, ulCount); andre@0: crv2 = sftk_emailhack(slot, certHandle, search, pTemplate, ulCount); andre@0: if (crv == CKR_OK) crv = crv2; andre@0: sftk_freeDB(certHandle); andre@0: } andre@0: andre@0: if (crv == CKR_OK && isLoggedIn && searchKeyDB) { andre@0: SFTKDBHandle *keyHandle = sftk_getKeyDB(slot); andre@0: crv = sftk_searchDatabase(keyHandle, search, pTemplate, ulCount); andre@0: sftk_freeDB(keyHandle); andre@0: } andre@0: return crv; andre@0: } andre@0: andre@0: /* NSC_FindObjectsInit initializes a search for token and session objects andre@0: * that match a template. */ andre@0: CK_RV NSC_FindObjectsInit(CK_SESSION_HANDLE hSession, andre@0: CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulCount) andre@0: { andre@0: SFTKSearchResults *search = NULL, *freeSearch = NULL; andre@0: SFTKSession *session = NULL; andre@0: SFTKSlot *slot = sftk_SlotFromSessionHandle(hSession); andre@0: PRBool tokenOnly = PR_FALSE; andre@0: CK_RV crv = CKR_OK; andre@0: PRBool isLoggedIn; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: if (slot == NULL) { andre@0: return CKR_SESSION_HANDLE_INVALID; andre@0: } andre@0: session = sftk_SessionFromHandle(hSession); andre@0: if (session == NULL) { andre@0: crv = CKR_SESSION_HANDLE_INVALID; andre@0: goto loser; andre@0: } andre@0: andre@0: search = (SFTKSearchResults *)PORT_Alloc(sizeof(SFTKSearchResults)); andre@0: if (search == NULL) { andre@0: crv = CKR_HOST_MEMORY; andre@0: goto loser; andre@0: } andre@0: search->handles = (CK_OBJECT_HANDLE *) andre@0: PORT_Alloc(sizeof(CK_OBJECT_HANDLE) * NSC_SEARCH_BLOCK_SIZE); andre@0: if (search->handles == NULL) { andre@0: crv = CKR_HOST_MEMORY; andre@0: goto loser; andre@0: } andre@0: search->index = 0; andre@0: search->size = 0; andre@0: search->array_size = NSC_SEARCH_BLOCK_SIZE; andre@0: isLoggedIn = (PRBool)((!slot->needLogin) || slot->isLoggedIn); andre@0: andre@0: crv = sftk_searchTokenList(slot, search, pTemplate, ulCount, &tokenOnly, andre@0: isLoggedIn); andre@0: if (crv != CKR_OK) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* build list of found objects in the session */ andre@0: if (!tokenOnly) { andre@0: crv = sftk_searchObjectList(search, slot->sessObjHashTable, andre@0: slot->sessObjHashSize, slot->objectLock, andre@0: pTemplate, ulCount, isLoggedIn); andre@0: } andre@0: if (crv != CKR_OK) { andre@0: goto loser; andre@0: } andre@0: andre@0: if ((freeSearch = session->search) != NULL) { andre@0: session->search = NULL; andre@0: sftk_FreeSearch(freeSearch); andre@0: } andre@0: session->search = search; andre@0: sftk_FreeSession(session); andre@0: return CKR_OK; andre@0: andre@0: loser: andre@0: if (search) { andre@0: sftk_FreeSearch(search); andre@0: } andre@0: if (session) { andre@0: sftk_FreeSession(session); andre@0: } andre@0: return crv; andre@0: } andre@0: andre@0: andre@0: /* NSC_FindObjects continues a search for token and session objects andre@0: * that match a template, obtaining additional object handles. */ andre@0: CK_RV NSC_FindObjects(CK_SESSION_HANDLE hSession, andre@0: CK_OBJECT_HANDLE_PTR phObject,CK_ULONG ulMaxObjectCount, andre@0: CK_ULONG_PTR pulObjectCount) andre@0: { andre@0: SFTKSession *session; andre@0: SFTKSearchResults *search; andre@0: int transfer; andre@0: int left; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: *pulObjectCount = 0; andre@0: session = sftk_SessionFromHandle(hSession); andre@0: if (session == NULL) return CKR_SESSION_HANDLE_INVALID; andre@0: if (session->search == NULL) { andre@0: sftk_FreeSession(session); andre@0: return CKR_OK; andre@0: } andre@0: search = session->search; andre@0: left = session->search->size - session->search->index; andre@0: transfer = ((int)ulMaxObjectCount > left) ? left : ulMaxObjectCount; andre@0: if (transfer > 0) { andre@0: PORT_Memcpy(phObject,&search->handles[search->index], andre@0: transfer*sizeof(CK_OBJECT_HANDLE)); andre@0: } else { andre@0: *phObject = CK_INVALID_HANDLE; andre@0: } andre@0: andre@0: search->index += transfer; andre@0: if (search->index == search->size) { andre@0: session->search = NULL; andre@0: sftk_FreeSearch(search); andre@0: } andre@0: *pulObjectCount = transfer; andre@0: sftk_FreeSession(session); andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* NSC_FindObjectsFinal finishes a search for token and session objects. */ andre@0: CK_RV NSC_FindObjectsFinal(CK_SESSION_HANDLE hSession) andre@0: { andre@0: SFTKSession *session; andre@0: SFTKSearchResults *search; andre@0: andre@0: CHECK_FORK(); andre@0: andre@0: session = sftk_SessionFromHandle(hSession); andre@0: if (session == NULL) return CKR_SESSION_HANDLE_INVALID; andre@0: search = session->search; andre@0: session->search = NULL; andre@0: sftk_FreeSession(session); andre@0: if (search != NULL) { andre@0: sftk_FreeSearch(search); andre@0: } andre@0: return CKR_OK; andre@0: } andre@0: andre@0: andre@0: andre@0: CK_RV NSC_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 CKR_FUNCTION_NOT_SUPPORTED; andre@0: } andre@0: