Imported Upstream version 3.15.4
[platform/upstream/nss.git] / nss / lib / softoken / softoken.h
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 #ifdef NSS_ENABLE_ECC
39 /*
40 ** pepare an ECParam structure from DEREncoded params
41  */
42 extern SECStatus EC_FillParams(PLArenaPool *arena,
43                                const SECItem *encodedParams, ECParams *params);
44 extern SECStatus EC_DecodeParams(const SECItem *encodedParams, 
45                                 ECParams **ecparams);
46 extern SECStatus EC_CopyParams(PLArenaPool *arena, ECParams *dstParams,
47                                 const ECParams *srcParams);
48 #endif
49
50
51 /*
52 ** Prepare a buffer for padded CBC encryption, growing to the appropriate 
53 ** boundary, filling with the appropriate padding.
54 **
55 ** blockSize must be a power of 2.
56 **
57 ** We add from 1 to blockSize bytes -- we *always* grow.
58 ** The extra bytes contain the value of the length of the padding:
59 ** if we have 2 bytes of padding, then the padding is "0x02, 0x02".
60 **
61 ** NOTE: If arena is non-NULL, we re-allocate from there, otherwise
62 ** we assume (and use) PR memory (re)allocation.
63 */
64 extern unsigned char * CBC_PadBuffer(PLArenaPool *arena, unsigned char *inbuf,
65                                      unsigned int inlen, unsigned int *outlen,
66                                      int blockSize);
67
68
69 /****************************************/
70 /*
71 ** Power-Up selftests required for FIPS and invoked only
72 ** under PKCS #11 FIPS mode.
73 */
74 extern CK_RV sftk_fipsPowerUpSelfTest( void ); 
75
76 /*
77 ** make known fixed PKCS #11 key types to their sizes in bytes
78 */      
79 unsigned long sftk_MapKeySize(CK_KEY_TYPE keyType);
80
81 /*
82 ** FIPS 140-2 auditing
83 */
84 extern PRBool sftk_audit_enabled;
85
86 extern void sftk_LogAuditMessage(NSSAuditSeverity severity, 
87                                  NSSAuditType, const char *msg);
88
89 extern void sftk_AuditCreateObject(CK_SESSION_HANDLE hSession,
90                         CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
91                         CK_OBJECT_HANDLE_PTR phObject, CK_RV rv);
92
93 extern void sftk_AuditCopyObject(CK_SESSION_HANDLE hSession,
94                         CK_OBJECT_HANDLE hObject,
95                         CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
96                         CK_OBJECT_HANDLE_PTR phNewObject, CK_RV rv);
97
98 extern void sftk_AuditDestroyObject(CK_SESSION_HANDLE hSession,
99                         CK_OBJECT_HANDLE hObject, CK_RV rv);
100
101 extern void sftk_AuditGetObjectSize(CK_SESSION_HANDLE hSession,
102                         CK_OBJECT_HANDLE hObject, CK_ULONG_PTR pulSize,
103                         CK_RV rv);
104
105 extern void sftk_AuditGetAttributeValue(CK_SESSION_HANDLE hSession,
106                         CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate,
107                         CK_ULONG ulCount, CK_RV rv);
108
109 extern void sftk_AuditSetAttributeValue(CK_SESSION_HANDLE hSession,
110                         CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate,
111                         CK_ULONG ulCount, CK_RV rv);
112
113 extern void sftk_AuditCryptInit(const char *opName,
114                         CK_SESSION_HANDLE hSession,
115                         CK_MECHANISM_PTR pMechanism,
116                         CK_OBJECT_HANDLE hKey, CK_RV rv);
117
118 extern void sftk_AuditGenerateKey(CK_SESSION_HANDLE hSession,
119                         CK_MECHANISM_PTR pMechanism,
120                         CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
121                         CK_OBJECT_HANDLE_PTR phKey, CK_RV rv);
122
123 extern void sftk_AuditGenerateKeyPair(CK_SESSION_HANDLE hSession,
124                         CK_MECHANISM_PTR pMechanism,
125                         CK_ATTRIBUTE_PTR pPublicKeyTemplate,
126                         CK_ULONG ulPublicKeyAttributeCount,
127                         CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
128                         CK_ULONG ulPrivateKeyAttributeCount,
129                         CK_OBJECT_HANDLE_PTR phPublicKey,
130                         CK_OBJECT_HANDLE_PTR phPrivateKey, CK_RV rv);
131
132 extern void sftk_AuditWrapKey(CK_SESSION_HANDLE hSession,
133                         CK_MECHANISM_PTR pMechanism,
134                         CK_OBJECT_HANDLE hWrappingKey, CK_OBJECT_HANDLE hKey,
135                         CK_BYTE_PTR pWrappedKey,
136                         CK_ULONG_PTR pulWrappedKeyLen, CK_RV rv);
137
138 extern void sftk_AuditUnwrapKey(CK_SESSION_HANDLE hSession,
139                         CK_MECHANISM_PTR pMechanism,
140                         CK_OBJECT_HANDLE hUnwrappingKey,
141                         CK_BYTE_PTR pWrappedKey, CK_ULONG ulWrappedKeyLen,
142                         CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount,
143                         CK_OBJECT_HANDLE_PTR phKey, CK_RV rv);
144
145 extern void sftk_AuditDeriveKey(CK_SESSION_HANDLE hSession,
146                         CK_MECHANISM_PTR pMechanism,
147                         CK_OBJECT_HANDLE hBaseKey,
148                         CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount,
149                         CK_OBJECT_HANDLE_PTR phKey, CK_RV rv);
150
151 extern void sftk_AuditDigestKey(CK_SESSION_HANDLE hSession,
152                         CK_OBJECT_HANDLE hKey, CK_RV rv);
153
154 /*
155 ** FIPS 140-2 Error state
156 */
157 extern PRBool sftk_fatalError;
158
159 /*
160 ** macros to check for forked child process after C_Initialize
161 */
162 #if defined(XP_UNIX) && !defined(NO_FORK_CHECK)
163
164 #ifdef DEBUG
165
166 #define FORK_ASSERT() \
167     { \
168         char* forkAssert = getenv("NSS_STRICT_NOFORK"); \
169         if ( (!forkAssert) || (0 == strcmp(forkAssert, "1")) ) { \
170             PORT_Assert(0); \
171         } \
172     }
173
174 #else
175
176 #define FORK_ASSERT()
177
178 #endif
179
180 /* we have 3 methods of implementing the fork checks :
181  * - Solaris "mixed" method
182  * - pthread_atfork method
183  * - getpid method
184  */
185
186 #if !defined (CHECK_FORK_MIXED) && !defined(CHECK_FORK_PTHREAD) && \
187     !defined (CHECK_FORK_GETPID)
188
189 /* Choose fork check method automatically unless specified
190  * This section should be updated as more platforms get pthread fixes
191  * to unregister fork handlers in dlclose.
192  */
193
194 #ifdef SOLARIS
195
196 /* Solaris 8, s9 use PID checks, s10 uses pthread_atfork */
197
198 #define CHECK_FORK_MIXED
199
200 #elif defined(LINUX)
201
202 #define CHECK_FORK_PTHREAD
203
204 #else
205
206 /* Other Unix platforms use only PID checks. Even if pthread_atfork is
207  * available, the behavior of dlclose isn't guaranteed by POSIX to
208  * unregister the fork handler. */
209
210 #define CHECK_FORK_GETPID
211
212 #endif
213
214 #endif
215
216 #if defined(CHECK_FORK_MIXED)
217
218 extern PRBool usePthread_atfork;
219 #include <unistd.h>
220 extern pid_t myPid;
221 extern PRBool forked;
222
223 #define PARENT_FORKED() (usePthread_atfork ? forked : (myPid && myPid != getpid()))
224
225 #elif defined(CHECK_FORK_PTHREAD)
226
227 extern PRBool forked;
228
229 #define PARENT_FORKED() forked
230
231 #elif defined(CHECK_FORK_GETPID)
232
233 #include <unistd.h>
234 extern pid_t myPid;
235
236 #define PARENT_FORKED() (myPid && myPid != getpid())
237     
238 #endif
239
240 extern PRBool parentForkedAfterC_Initialize;
241 extern PRBool sftkForkCheckDisabled;
242
243 #define CHECK_FORK() \
244     do { \
245         if (!sftkForkCheckDisabled && PARENT_FORKED()) { \
246             FORK_ASSERT(); \
247             return CKR_DEVICE_ERROR; \
248         } \
249     } while (0)
250
251 #define SKIP_AFTER_FORK(x) if (!parentForkedAfterC_Initialize) x
252
253 #define ENABLE_FORK_CHECK() \
254     { \
255         char* doForkCheck = getenv("NSS_STRICT_NOFORK"); \
256         if ( doForkCheck && !strcmp(doForkCheck, "DISABLED") ) { \
257             sftkForkCheckDisabled = PR_TRUE; \
258         } \
259     }
260
261
262 #else
263
264 /* non-Unix platforms, or fork check disabled */
265
266 #define CHECK_FORK()
267 #define SKIP_AFTER_FORK(x) x
268 #define ENABLE_FORK_CHECK()
269
270 #ifndef NO_FORK_CHECK
271 #define NO_FORK_CHECK
272 #endif
273
274 #endif
275
276
277 SEC_END_PROTOS
278
279 #endif /* _SOFTOKEN_H_ */