4abd2c0db4280927d19880699d5ca2db0f37197b
[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 ** RSA encryption/decryption. When encrypting/decrypting the output
22 ** buffer must be at least the size of the public key modulus.
23 */
24
25 /*
26 ** Format some data into a PKCS#1 encryption block, preparing the
27 ** data for RSA encryption.
28 **      "result" where the formatted block is stored (memory is allocated)
29 **      "modulusLen" the size of the formatted block
30 **      "blockType" what block type to use (SEC_RSABlock*)
31 **      "data" the data to format
32 */
33 extern SECStatus RSA_FormatBlock(SECItem *result,
34                                  unsigned int modulusLen,
35                                  RSA_BlockType blockType,
36                                  SECItem *data);
37 /*
38 ** Similar, but just returns a pointer to the allocated memory, *and*
39 ** will *only* format one block, even if we (in the future) modify
40 ** RSA_FormatBlock() to loop over multiples of modulusLen.
41 */
42 extern unsigned char *RSA_FormatOneBlock(unsigned int modulusLen,
43                                          RSA_BlockType blockType,
44                                          SECItem *data);
45
46
47
48 /*
49  * convenience wrappers for doing single RSA operations. They create the
50  * RSA context internally and take care of the formatting
51  * requirements. Blinding happens automagically within RSA_Sign and
52  * RSA_DecryptBlock.
53  */
54 extern
55 SECStatus RSA_Sign(NSSLOWKEYPrivateKey *key, unsigned char *output,
56                        unsigned int *outputLen, unsigned int maxOutputLen,
57                        unsigned char *input, unsigned int inputLen);
58 extern
59 SECStatus RSA_HashSign(SECOidTag hashOid,
60                         NSSLOWKEYPrivateKey *key, unsigned char *sig,
61                         unsigned int *sigLen, unsigned int maxLen,
62                         unsigned char *hash, unsigned int hashLen);
63 extern
64 SECStatus RSA_SignPSS(CK_RSA_PKCS_PSS_PARAMS *pss_params,
65                       NSSLOWKEYPrivateKey *key, 
66                       unsigned char *output, unsigned int *output_len, 
67                       unsigned int max_output_len, const unsigned char *input,
68                       unsigned int input_len);
69 extern
70 SECStatus RSA_CheckSign(NSSLOWKEYPublicKey *key, unsigned char *sign,
71                             unsigned int signLength, unsigned char *hash,
72                             unsigned int hashLength);
73 extern
74 SECStatus RSA_HashCheckSign(SECOidTag hashOid,
75                             NSSLOWKEYPublicKey *key, unsigned char *sig,
76                             unsigned int sigLen, unsigned char *digest,
77                             unsigned int digestLen);
78 extern
79 SECStatus RSA_CheckSignPSS(CK_RSA_PKCS_PSS_PARAMS *pss_params,
80                            NSSLOWKEYPublicKey *key,
81                            const unsigned char *sign, unsigned int sign_len,
82                            const unsigned char *hash, unsigned int hash_len);
83 extern
84 SECStatus RSA_CheckSignRecover(NSSLOWKEYPublicKey *key, unsigned char *data,
85                             unsigned int *data_len,unsigned int max_output_len, 
86                             unsigned char *sign, unsigned int sign_len);
87 extern
88 SECStatus RSA_EncryptBlock(NSSLOWKEYPublicKey *key, unsigned char *output,
89                            unsigned int *outputLen, unsigned int maxOutputLen,
90                            unsigned char *input, unsigned int inputLen);
91 extern
92 SECStatus RSA_DecryptBlock(NSSLOWKEYPrivateKey *key, unsigned char *output,
93                            unsigned int *outputLen, unsigned int maxOutputLen,
94                            unsigned char *input, unsigned int inputLen);
95
96 extern
97 SECStatus RSA_EncryptOAEP(CK_RSA_PKCS_OAEP_PARAMS *oaepParams,
98                           NSSLOWKEYPublicKey *key,
99                           unsigned char *output, unsigned int *outputLen,
100                           unsigned int maxOutputLen,
101                           const unsigned char *input, unsigned int inputLen);
102
103 extern
104 SECStatus RSA_DecryptOAEP(CK_RSA_PKCS_OAEP_PARAMS *oaepParams,
105                           NSSLOWKEYPrivateKey *key,
106                           unsigned char *output, unsigned int *outputLen,
107                           unsigned int maxOutputLen,
108                           const unsigned char *input, unsigned int inputLen);
109
110 /*
111  * added to make pkcs #11 happy
112  *   RAW is RSA_X_509
113  */
114 extern
115 SECStatus RSA_SignRaw( NSSLOWKEYPrivateKey *key, unsigned char *output,
116                          unsigned int *output_len, unsigned int maxOutputLen,
117                          unsigned char *input, unsigned int input_len);
118 extern
119 SECStatus RSA_CheckSignRaw( NSSLOWKEYPublicKey *key, unsigned char *sign, 
120                             unsigned int sign_len, unsigned char *hash, 
121                             unsigned int hash_len);
122 extern
123 SECStatus RSA_CheckSignRecoverRaw( NSSLOWKEYPublicKey *key, unsigned char *data,
124                             unsigned int *data_len, unsigned int max_output_len,
125                             unsigned char *sign, unsigned int sign_len);
126 extern
127 SECStatus RSA_EncryptRaw( NSSLOWKEYPublicKey *key, unsigned char *output,
128                             unsigned int *output_len,
129                             unsigned int max_output_len, 
130                             unsigned char *input, unsigned int input_len);
131 extern
132 SECStatus RSA_DecryptRaw(NSSLOWKEYPrivateKey *key, unsigned char *output,
133                              unsigned int *output_len,
134                              unsigned int max_output_len,
135                              unsigned char *input, unsigned int input_len);
136 #ifdef NSS_ENABLE_ECC
137 /*
138 ** pepare an ECParam structure from DEREncoded params
139  */
140 extern SECStatus EC_FillParams(PLArenaPool *arena,
141                                const SECItem *encodedParams, ECParams *params);
142 extern SECStatus EC_DecodeParams(const SECItem *encodedParams, 
143                                 ECParams **ecparams);
144 extern SECStatus EC_CopyParams(PLArenaPool *arena, ECParams *dstParams,
145                                 const ECParams *srcParams);
146 #endif
147
148
149 /*
150 ** Prepare a buffer for padded CBC encryption, growing to the appropriate 
151 ** boundary, filling with the appropriate padding.
152 **
153 ** blockSize must be a power of 2.
154 **
155 ** We add from 1 to blockSize bytes -- we *always* grow.
156 ** The extra bytes contain the value of the length of the padding:
157 ** if we have 2 bytes of padding, then the padding is "0x02, 0x02".
158 **
159 ** NOTE: If arena is non-NULL, we re-allocate from there, otherwise
160 ** we assume (and use) PR memory (re)allocation.
161 */
162 extern unsigned char * CBC_PadBuffer(PLArenaPool *arena, unsigned char *inbuf,
163                                      unsigned int inlen, unsigned int *outlen,
164                                      int blockSize);
165
166
167 /****************************************/
168 /*
169 ** Power-Up selftests required for FIPS and invoked only
170 ** under PKCS #11 FIPS mode.
171 */
172 extern CK_RV sftk_fipsPowerUpSelfTest( void ); 
173
174 /*
175 ** make known fixed PKCS #11 key types to their sizes in bytes
176 */      
177 unsigned long sftk_MapKeySize(CK_KEY_TYPE keyType);
178
179 /*
180 ** FIPS 140-2 auditing
181 */
182 extern PRBool sftk_audit_enabled;
183
184 extern void sftk_LogAuditMessage(NSSAuditSeverity severity, 
185                                  NSSAuditType, const char *msg);
186
187 extern void sftk_AuditCreateObject(CK_SESSION_HANDLE hSession,
188                         CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
189                         CK_OBJECT_HANDLE_PTR phObject, CK_RV rv);
190
191 extern void sftk_AuditCopyObject(CK_SESSION_HANDLE hSession,
192                         CK_OBJECT_HANDLE hObject,
193                         CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
194                         CK_OBJECT_HANDLE_PTR phNewObject, CK_RV rv);
195
196 extern void sftk_AuditDestroyObject(CK_SESSION_HANDLE hSession,
197                         CK_OBJECT_HANDLE hObject, CK_RV rv);
198
199 extern void sftk_AuditGetObjectSize(CK_SESSION_HANDLE hSession,
200                         CK_OBJECT_HANDLE hObject, CK_ULONG_PTR pulSize,
201                         CK_RV rv);
202
203 extern void sftk_AuditGetAttributeValue(CK_SESSION_HANDLE hSession,
204                         CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate,
205                         CK_ULONG ulCount, CK_RV rv);
206
207 extern void sftk_AuditSetAttributeValue(CK_SESSION_HANDLE hSession,
208                         CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate,
209                         CK_ULONG ulCount, CK_RV rv);
210
211 extern void sftk_AuditCryptInit(const char *opName,
212                         CK_SESSION_HANDLE hSession,
213                         CK_MECHANISM_PTR pMechanism,
214                         CK_OBJECT_HANDLE hKey, CK_RV rv);
215
216 extern void sftk_AuditGenerateKey(CK_SESSION_HANDLE hSession,
217                         CK_MECHANISM_PTR pMechanism,
218                         CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
219                         CK_OBJECT_HANDLE_PTR phKey, CK_RV rv);
220
221 extern void sftk_AuditGenerateKeyPair(CK_SESSION_HANDLE hSession,
222                         CK_MECHANISM_PTR pMechanism,
223                         CK_ATTRIBUTE_PTR pPublicKeyTemplate,
224                         CK_ULONG ulPublicKeyAttributeCount,
225                         CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
226                         CK_ULONG ulPrivateKeyAttributeCount,
227                         CK_OBJECT_HANDLE_PTR phPublicKey,
228                         CK_OBJECT_HANDLE_PTR phPrivateKey, CK_RV rv);
229
230 extern void sftk_AuditWrapKey(CK_SESSION_HANDLE hSession,
231                         CK_MECHANISM_PTR pMechanism,
232                         CK_OBJECT_HANDLE hWrappingKey, CK_OBJECT_HANDLE hKey,
233                         CK_BYTE_PTR pWrappedKey,
234                         CK_ULONG_PTR pulWrappedKeyLen, CK_RV rv);
235
236 extern void sftk_AuditUnwrapKey(CK_SESSION_HANDLE hSession,
237                         CK_MECHANISM_PTR pMechanism,
238                         CK_OBJECT_HANDLE hUnwrappingKey,
239                         CK_BYTE_PTR pWrappedKey, CK_ULONG ulWrappedKeyLen,
240                         CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount,
241                         CK_OBJECT_HANDLE_PTR phKey, CK_RV rv);
242
243 extern void sftk_AuditDeriveKey(CK_SESSION_HANDLE hSession,
244                         CK_MECHANISM_PTR pMechanism,
245                         CK_OBJECT_HANDLE hBaseKey,
246                         CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount,
247                         CK_OBJECT_HANDLE_PTR phKey, CK_RV rv);
248
249 extern void sftk_AuditDigestKey(CK_SESSION_HANDLE hSession,
250                         CK_OBJECT_HANDLE hKey, CK_RV rv);
251
252 /*
253 ** FIPS 140-2 Error state
254 */
255 extern PRBool sftk_fatalError;
256
257 /*
258 ** macros to check for forked child process after C_Initialize
259 */
260 #if defined(XP_UNIX) && !defined(NO_FORK_CHECK)
261
262 #ifdef DEBUG
263
264 #define FORK_ASSERT() \
265     { \
266         char* forkAssert = getenv("NSS_STRICT_NOFORK"); \
267         if ( (!forkAssert) || (0 == strcmp(forkAssert, "1")) ) { \
268             PORT_Assert(0); \
269         } \
270     }
271
272 #else
273
274 #define FORK_ASSERT()
275
276 #endif
277
278 /* we have 3 methods of implementing the fork checks :
279  * - Solaris "mixed" method
280  * - pthread_atfork method
281  * - getpid method
282  */
283
284 #if !defined (CHECK_FORK_MIXED) && !defined(CHECK_FORK_PTHREAD) && \
285     !defined (CHECK_FORK_GETPID)
286
287 /* Choose fork check method automatically unless specified
288  * This section should be updated as more platforms get pthread fixes
289  * to unregister fork handlers in dlclose.
290  */
291
292 #ifdef SOLARIS
293
294 /* Solaris 8, s9 use PID checks, s10 uses pthread_atfork */
295
296 #define CHECK_FORK_MIXED
297
298 #elif defined(LINUX)
299
300 #define CHECK_FORK_PTHREAD
301
302 #else
303
304 /* Other Unix platforms use only PID checks. Even if pthread_atfork is
305  * available, the behavior of dlclose isn't guaranteed by POSIX to
306  * unregister the fork handler. */
307
308 #define CHECK_FORK_GETPID
309
310 #endif
311
312 #endif
313
314 #if defined(CHECK_FORK_MIXED)
315
316 extern PRBool usePthread_atfork;
317 #include <unistd.h>
318 extern pid_t myPid;
319 extern PRBool forked;
320
321 #define PARENT_FORKED() (usePthread_atfork ? forked : (myPid && myPid != getpid()))
322
323 #elif defined(CHECK_FORK_PTHREAD)
324
325 extern PRBool forked;
326
327 #define PARENT_FORKED() forked
328
329 #elif defined(CHECK_FORK_GETPID)
330
331 #include <unistd.h>
332 extern pid_t myPid;
333
334 #define PARENT_FORKED() (myPid && myPid != getpid())
335     
336 #endif
337
338 extern PRBool parentForkedAfterC_Initialize;
339 extern PRBool sftkForkCheckDisabled;
340
341 #define CHECK_FORK() \
342     do { \
343         if (!sftkForkCheckDisabled && PARENT_FORKED()) { \
344             FORK_ASSERT(); \
345             return CKR_DEVICE_ERROR; \
346         } \
347     } while (0)
348
349 #define SKIP_AFTER_FORK(x) if (!parentForkedAfterC_Initialize) x
350
351 #define ENABLE_FORK_CHECK() \
352     { \
353         char* doForkCheck = getenv("NSS_STRICT_NOFORK"); \
354         if ( doForkCheck && !strcmp(doForkCheck, "DISABLED") ) { \
355             sftkForkCheckDisabled = PR_TRUE; \
356         } \
357     }
358
359
360 #else
361
362 /* non-Unix platforms, or fork check disabled */
363
364 #define CHECK_FORK()
365 #define SKIP_AFTER_FORK(x) x
366 #define ENABLE_FORK_CHECK()
367
368 #ifndef NO_FORK_CHECK
369 #define NO_FORK_CHECK
370 #endif
371
372 #endif
373
374
375 SEC_END_PROTOS
376
377 #endif /* _SOFTOKEN_H_ */