softoken.h (10867B)
1 /* 2 * softoken.h - private data structures and prototypes for the softoken lib 3 * 4 * This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 7 8 #ifndef _SOFTOKEN_H_ 9 #define _SOFTOKEN_H_ 10 11 #include "blapi.h" 12 #include "lowkeyti.h" 13 #include "softoknt.h" 14 #include "secoidt.h" 15 16 #include "pkcs11t.h" 17 18 SEC_BEGIN_PROTOS 19 20 /* 21 * Convenience wrapper for doing a single PKCS#1 v1.5 RSA operations where the 22 * encoded digest info is computed internally, rather than by the caller. 23 * 24 * The HashSign variants expect as input the value of H, the computed hash 25 * from RFC 3447, Section 9.2, Step 1, and will compute the DER-encoded 26 * DigestInfo structure internally prior to signing/verifying. 27 */ 28 extern SECStatus 29 RSA_HashSign(SECOidTag hashOid, NSSLOWKEYPrivateKey *key, 30 unsigned char *sig, unsigned int *sigLen, unsigned int maxLen, 31 const unsigned char *hash, unsigned int hashLen); 32 33 extern SECStatus 34 RSA_HashCheckSign(SECOidTag hashOid, NSSLOWKEYPublicKey *key, 35 const unsigned char *sig, unsigned int sigLen, 36 const unsigned char *hash, unsigned int hashLen); 37 38 /* 39 ** Prepare a buffer for padded CBC encryption, growing to the appropriate 40 ** boundary, filling with the appropriate padding. 41 ** 42 ** blockSize must be a power of 2. 43 ** 44 ** We add from 1 to blockSize bytes -- we *always* grow. 45 ** The extra bytes contain the value of the length of the padding: 46 ** if we have 2 bytes of padding, then the padding is "0x02, 0x02". 47 ** 48 ** NOTE: If arena is non-NULL, we re-allocate from there, otherwise 49 ** we assume (and use) PR memory (re)allocation. 50 */ 51 extern unsigned char *CBC_PadBuffer(PLArenaPool *arena, unsigned char *inbuf, 52 unsigned int inlen, unsigned int *outlen, 53 int blockSize); 54 55 /****************************************/ 56 /* 57 ** Power-Up selftests are required for FIPS. 58 */ 59 /* make sure Power-up selftests have been run. */ 60 extern CK_RV sftk_FIPSEntryOK(PRBool rerun); 61 62 /* 63 ** make known fixed PKCS #11 key types to their sizes in bytes 64 */ 65 unsigned long sftk_MapKeySize(CK_KEY_TYPE keyType); 66 67 /* 68 ** FIPS 140-2 auditing 69 */ 70 extern PRBool sftk_audit_enabled; 71 72 extern void sftk_LogAuditMessage(NSSAuditSeverity severity, 73 NSSAuditType, const char *msg); 74 75 extern void sftk_AuditCreateObject(CK_SESSION_HANDLE hSession, 76 CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, 77 CK_OBJECT_HANDLE_PTR phObject, CK_RV rv); 78 79 extern void sftk_AuditCopyObject(CK_SESSION_HANDLE hSession, 80 CK_OBJECT_HANDLE hObject, 81 CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, 82 CK_OBJECT_HANDLE_PTR phNewObject, CK_RV rv); 83 84 extern void sftk_AuditDestroyObject(CK_SESSION_HANDLE hSession, 85 CK_OBJECT_HANDLE hObject, CK_RV rv); 86 87 extern void sftk_AuditGetObjectSize(CK_SESSION_HANDLE hSession, 88 CK_OBJECT_HANDLE hObject, CK_ULONG_PTR pulSize, 89 CK_RV rv); 90 91 extern void sftk_AuditGetAttributeValue(CK_SESSION_HANDLE hSession, 92 CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, 93 CK_ULONG ulCount, CK_RV rv); 94 95 extern void sftk_AuditSetAttributeValue(CK_SESSION_HANDLE hSession, 96 CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, 97 CK_ULONG ulCount, CK_RV rv); 98 99 extern void sftk_AuditCryptInit(const char *opName, 100 CK_SESSION_HANDLE hSession, 101 CK_MECHANISM_PTR pMechanism, 102 CK_OBJECT_HANDLE hKey, CK_RV rv); 103 104 extern void sftk_AuditGenerateKey(CK_SESSION_HANDLE hSession, 105 CK_MECHANISM_PTR pMechanism, 106 CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, 107 CK_OBJECT_HANDLE_PTR phKey, CK_RV rv); 108 109 extern void sftk_AuditGenerateKeyPair(CK_SESSION_HANDLE hSession, 110 CK_MECHANISM_PTR pMechanism, 111 CK_ATTRIBUTE_PTR pPublicKeyTemplate, 112 CK_ULONG ulPublicKeyAttributeCount, 113 CK_ATTRIBUTE_PTR pPrivateKeyTemplate, 114 CK_ULONG ulPrivateKeyAttributeCount, 115 CK_OBJECT_HANDLE_PTR phPublicKey, 116 CK_OBJECT_HANDLE_PTR phPrivateKey, CK_RV rv); 117 118 extern void sftk_AuditWrapKey(CK_SESSION_HANDLE hSession, 119 CK_MECHANISM_PTR pMechanism, 120 CK_OBJECT_HANDLE hWrappingKey, CK_OBJECT_HANDLE hKey, 121 CK_BYTE_PTR pWrappedKey, 122 CK_ULONG_PTR pulWrappedKeyLen, CK_RV rv); 123 124 extern void sftk_AuditUnwrapKey(CK_SESSION_HANDLE hSession, 125 CK_MECHANISM_PTR pMechanism, 126 CK_OBJECT_HANDLE hUnwrappingKey, 127 CK_BYTE_PTR pWrappedKey, CK_ULONG ulWrappedKeyLen, 128 CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount, 129 CK_OBJECT_HANDLE_PTR phKey, CK_RV rv); 130 131 extern void sftk_AuditDeriveKey(CK_SESSION_HANDLE hSession, 132 CK_MECHANISM_PTR pMechanism, 133 CK_OBJECT_HANDLE hBaseKey, 134 CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount, 135 CK_OBJECT_HANDLE_PTR phKey, CK_RV rv); 136 137 extern void sftk_AuditDigestKey(CK_SESSION_HANDLE hSession, 138 CK_OBJECT_HANDLE hKey, CK_RV rv); 139 140 extern void sftk_AuditEncapsulateKey(CK_SESSION_HANDLE hSession, 141 CK_MECHANISM_PTR pMechanism, 142 CK_OBJECT_HANDLE hPublicKey, 143 CK_ATTRIBUTE_PTR pTemplate, 144 CK_ULONG ulAttributeCount, 145 CK_BYTE_PTR pCiphertext, 146 CK_ULONG_PTR pulCiphertextLen, 147 CK_OBJECT_HANDLE_PTR phKey, CK_RV rv); 148 149 extern void sftk_AuditDecapsulateKey(CK_SESSION_HANDLE hSession, 150 CK_MECHANISM_PTR pMechanism, 151 CK_OBJECT_HANDLE hPrivateKey, 152 CK_ATTRIBUTE_PTR pTemplate, 153 CK_ULONG ulAttributeCount, 154 CK_BYTE_PTR pCiphertext, 155 CK_ULONG ulCiphertextLen, 156 CK_OBJECT_HANDLE_PTR phKey, CK_RV rv); 157 158 /* 159 ** FIPS 140-2 Error state 160 */ 161 extern PRBool sftk_fatalError; 162 163 /* 164 ** macros to check for forked child process after C_Initialize 165 */ 166 /* for PKCS #11 3.0, default is NO_FORK_CHECK, if you want it, now you 167 * need to define DO_FORK_CHECK */ 168 #if defined(XP_UNIX) && defined(DO_FORK_CHECK) 169 170 #ifdef DEBUG 171 172 #define FORK_ASSERT() \ 173 { \ 174 char *forkAssert = PR_GetEnvSecure("NSS_STRICT_NOFORK"); \ 175 if ((!forkAssert) || (0 == strcmp(forkAssert, "1"))) { \ 176 PORT_Assert(0); \ 177 } \ 178 } 179 180 #else 181 182 #define FORK_ASSERT() 183 184 #endif 185 186 /* we have 3 methods of implementing the fork checks : 187 * - Solaris "mixed" method 188 * - pthread_atfork method 189 * - getpid method 190 */ 191 192 #if !defined(CHECK_FORK_MIXED) && !defined(CHECK_FORK_PTHREAD) && \ 193 !defined(CHECK_FORK_GETPID) 194 195 /* Choose fork check method automatically unless specified 196 * This section should be updated as more platforms get pthread fixes 197 * to unregister fork handlers in dlclose. 198 */ 199 200 #ifdef SOLARIS 201 202 /* Solaris 8, s9 use PID checks, s10 uses pthread_atfork */ 203 204 #define CHECK_FORK_MIXED 205 206 #elif defined(LINUX) || defined(__GLIBC__) || defined(FREEBSD) || defined(OPENBSD) 207 208 #define CHECK_FORK_PTHREAD 209 210 #else 211 212 /* Other Unix platforms use only PID checks. Even if pthread_atfork is 213 * available, the behavior of dlclose isn't guaranteed by POSIX to 214 * unregister the fork handler. */ 215 216 #define CHECK_FORK_GETPID 217 218 #endif 219 220 #endif 221 222 #if defined(CHECK_FORK_MIXED) 223 224 extern PRBool usePthread_atfork; 225 #include <unistd.h> 226 extern pid_t myPid; 227 extern PRBool forked; 228 229 #define PARENT_FORKED() (usePthread_atfork ? forked : (myPid && myPid != getpid())) 230 231 #elif defined(CHECK_FORK_PTHREAD) 232 233 extern PRBool forked; 234 235 #define PARENT_FORKED() forked 236 237 #elif defined(CHECK_FORK_GETPID) 238 239 #include <unistd.h> 240 extern pid_t myPid; 241 242 #define PARENT_FORKED() (myPid && myPid != getpid()) 243 244 #endif 245 246 extern PRBool parentForkedAfterC_Initialize; 247 extern PRBool sftkForkCheckDisabled; 248 249 #define CHECK_FORK() \ 250 do { \ 251 if (!sftkForkCheckDisabled && PARENT_FORKED()) { \ 252 FORK_ASSERT(); \ 253 return CKR_DEVICE_ERROR; \ 254 } \ 255 } while (0) 256 257 #define SKIP_AFTER_FORK(x) \ 258 if (!parentForkedAfterC_Initialize) \ 259 x 260 261 #define ENABLE_FORK_CHECK() \ 262 { \ 263 char *doForkCheck = PR_GetEnvSecure("NSS_STRICT_NOFORK"); \ 264 if (doForkCheck && !strcmp(doForkCheck, "DISABLED")) { \ 265 sftkForkCheckDisabled = PR_TRUE; \ 266 } \ 267 } 268 269 #else 270 271 /* non-Unix platforms, or fork check disabled */ 272 273 #define CHECK_FORK() 274 #define SKIP_AFTER_FORK(x) x 275 #define ENABLE_FORK_CHECK() 276 277 #ifndef NO_FORK_CHECK 278 #define NO_FORK_CHECK 279 #endif 280 281 #endif 282 283 /* 284 * If we were trying to be complete, we would have both FORK_SAFE 285 * and non-Fork safe interfaces here. That would require doubling 286 * the functions in our function list for both this and the FIPS 287 * interface. Since NSS now always asks for a FORK_SAFE interface, 288 * and can fall back to a non-FORK_SAFE interface, we set only 289 * export one set of interfaces here */ 290 #ifdef NO_FORK_CHECK 291 #define NSS_INTERFACE_FLAGS CKF_INTERFACE_FORK_SAFE 292 #else 293 #define NSS_INTERFACE_FLAGS 0 294 #endif 295 296 SEC_END_PROTOS 297 298 #endif /* _SOFTOKEN_H_ */