1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
16 #include "cmsreclist.h"
19 * Exported PK11 wrap functions.
24 /************************************************************
25 * Generic Slot Lists Management
26 ************************************************************/
27 void PK11_FreeSlotList(PK11SlotList *list);
28 SECStatus PK11_FreeSlotListElement(PK11SlotList *list, PK11SlotListElement *le);
29 PK11SlotListElement * PK11_GetFirstSafe(PK11SlotList *list);
30 PK11SlotListElement *PK11_GetNextSafe(PK11SlotList *list,
31 PK11SlotListElement *le, PRBool restart);
33 /************************************************************
34 * Generic Slot Management
35 ************************************************************/
36 PK11SlotInfo *PK11_ReferenceSlot(PK11SlotInfo *slot);
37 void PK11_FreeSlot(PK11SlotInfo *slot);
38 SECStatus PK11_DestroyObject(PK11SlotInfo *slot,CK_OBJECT_HANDLE object);
39 SECStatus PK11_DestroyTokenObject(PK11SlotInfo *slot,CK_OBJECT_HANDLE object);
40 PK11SlotInfo *PK11_GetInternalKeySlot(void);
41 PK11SlotInfo *PK11_GetInternalSlot(void);
42 SECStatus PK11_Logout(PK11SlotInfo *slot);
43 void PK11_LogoutAll(void);
46 /************************************************************
47 * Slot Password Management
48 ************************************************************/
49 void PK11_SetSlotPWValues(PK11SlotInfo *slot,int askpw, int timeout);
50 void PK11_GetSlotPWValues(PK11SlotInfo *slot,int *askpw, int *timeout);
51 SECStatus PK11_CheckSSOPassword(PK11SlotInfo *slot, char *ssopw);
52 SECStatus PK11_CheckUserPassword(PK11SlotInfo *slot, const char *pw);
53 PRBool PK11_IsLoggedIn(PK11SlotInfo *slot, void *wincx);
54 SECStatus PK11_InitPin(PK11SlotInfo *slot,const char *ssopw,
55 const char *pk11_userpwd);
56 SECStatus PK11_ChangePW(PK11SlotInfo *slot, const char *oldpw,
58 void PK11_SetPasswordFunc(PK11PasswordFunc func);
59 int PK11_GetMinimumPwdLength(PK11SlotInfo *slot);
60 SECStatus PK11_ResetToken(PK11SlotInfo *slot, char *sso_pwd);
61 SECStatus PK11_Authenticate(PK11SlotInfo *slot, PRBool loadCerts, void *wincx);
62 SECStatus PK11_TokenRefresh(PK11SlotInfo *slot);
65 /******************************************************************
67 ******************************************************************/
68 PK11SlotInfo *PK11_FindSlotByName(const char *name);
69 /******************************************************************
70 * PK11_FindSlotsByNames searches for a PK11SlotInfo using one or
71 * more criteria : dllName, slotName and tokenName . In addition, if
72 * presentOnly is set , only slots with a token inserted will be
74 ******************************************************************/
75 PK11SlotList *PK11_FindSlotsByNames(const char *dllName,
76 const char* slotName, const char* tokenName, PRBool presentOnly);
77 PRBool PK11_IsReadOnly(PK11SlotInfo *slot);
78 PRBool PK11_IsInternal(PK11SlotInfo *slot);
79 PRBool PK11_IsInternalKeySlot(PK11SlotInfo *slot);
80 char * PK11_GetTokenName(PK11SlotInfo *slot);
81 char * PK11_GetSlotName(PK11SlotInfo *slot);
82 PRBool PK11_NeedLogin(PK11SlotInfo *slot);
83 PRBool PK11_IsFriendly(PK11SlotInfo *slot);
84 PRBool PK11_IsHW(PK11SlotInfo *slot);
85 PRBool PK11_IsRemovable(PK11SlotInfo *slot);
86 PRBool PK11_NeedUserInit(PK11SlotInfo *slot);
87 PRBool PK11_ProtectedAuthenticationPath(PK11SlotInfo *slot);
88 int PK11_GetSlotSeries(PK11SlotInfo *slot);
89 int PK11_GetCurrentWrapIndex(PK11SlotInfo *slot);
90 unsigned long PK11_GetDefaultFlags(PK11SlotInfo *slot);
91 CK_SLOT_ID PK11_GetSlotID(PK11SlotInfo *slot);
92 SECMODModuleID PK11_GetModuleID(PK11SlotInfo *slot);
93 SECStatus PK11_GetSlotInfo(PK11SlotInfo *slot, CK_SLOT_INFO *info);
94 SECStatus PK11_GetTokenInfo(PK11SlotInfo *slot, CK_TOKEN_INFO *info);
95 PRBool PK11_IsDisabled(PK11SlotInfo *slot);
96 PRBool PK11_HasRootCerts(PK11SlotInfo *slot);
97 PK11DisableReasons PK11_GetDisabledReason(PK11SlotInfo *slot);
98 /* Prevents the slot from being used, and set disable reason to user-disable */
99 /* NOTE: Mechanisms that were ON continue to stay ON */
100 /* Therefore, when the slot is enabled, it will remember */
101 /* what mechanisms needs to be turned on */
102 PRBool PK11_UserDisableSlot(PK11SlotInfo *slot);
103 /* Allow all mechanisms that are ON before UserDisableSlot() */
104 /* was called to be available again */
105 PRBool PK11_UserEnableSlot(PK11SlotInfo *slot);
107 * wait for a specific slot event.
108 * event is a specific event to wait for. Currently only
109 * PK11TokenChangeOrRemovalEvent and PK11TokenPresentEvents are defined.
110 * timeout can be an interval time to wait, PR_INTERVAL_NO_WAIT (meaning only
111 * poll once), or PR_INTERVAL_NO_TIMEOUT (meaning block until a change).
112 * pollInterval is a suggested pulling interval value. '0' means use the
113 * default. Future implementations that don't poll may ignore this value.
114 * series is the current series for the last slot. This should be the series
115 * value for the slot the last time you read persistant information from the
116 * slot. For instance, if you publish a cert from the slot, you should obtain
117 * the slot series at that time. Then PK11_WaitForTokenEvent can detect a
118 * a change in the slot between the time you publish and the time
119 * PK11_WaitForTokenEvent is called, elliminating potential race conditions.
121 * The current status that is returned is:
122 * PK11TokenNotRemovable - always returned for any non-removable token.
123 * PK11TokenPresent - returned when the token is present and we are waiting
124 * on a PK11TokenPresentEvent. Then next event to look for is a
125 * PK11TokenChangeOrRemovalEvent.
126 * PK11TokenChanged - returned when the old token has been removed and a new
127 * token ad been inserted, and we are waiting for a
128 * PK11TokenChangeOrRemovalEvent. The next event to look for is another
129 * PK11TokenChangeOrRemovalEvent.
130 * PK11TokenRemoved - returned when the token is not present and we are
131 * waiting for a PK11TokenChangeOrRemovalEvent. The next event to look for
132 * is a PK11TokenPresentEvent.
134 PK11TokenStatus PK11_WaitForTokenEvent(PK11SlotInfo *slot, PK11TokenEvent event,
135 PRIntervalTime timeout, PRIntervalTime pollInterval, int series);
137 PRBool PK11_NeedPWInit(void);
138 PRBool PK11_TokenExists(CK_MECHANISM_TYPE);
139 SECStatus PK11_GetModInfo(SECMODModule *mod, CK_INFO *info);
140 PRBool PK11_IsFIPS(void);
141 SECMODModule *PK11_GetModule(PK11SlotInfo *slot);
143 /*********************************************************************
144 * Slot mapping utility functions.
145 *********************************************************************/
146 PRBool PK11_IsPresent(PK11SlotInfo *slot);
147 PRBool PK11_DoesMechanism(PK11SlotInfo *slot, CK_MECHANISM_TYPE type);
148 PK11SlotList * PK11_GetAllTokens(CK_MECHANISM_TYPE type,PRBool needRW,
149 PRBool loadCerts, void *wincx);
150 PK11SlotInfo *PK11_GetBestSlotMultipleWithAttributes(CK_MECHANISM_TYPE *type,
151 CK_FLAGS *mechFlag, unsigned int *keySize,
152 unsigned int count, void *wincx);
153 PK11SlotInfo *PK11_GetBestSlotMultiple(CK_MECHANISM_TYPE *type,
154 unsigned int count, void *wincx);
155 PK11SlotInfo *PK11_GetBestSlot(CK_MECHANISM_TYPE type, void *wincx);
156 PK11SlotInfo *PK11_GetBestSlotWithAttributes(CK_MECHANISM_TYPE type,
157 CK_FLAGS mechFlag, unsigned int keySize, void *wincx);
158 CK_MECHANISM_TYPE PK11_GetBestWrapMechanism(PK11SlotInfo *slot);
159 int PK11_GetBestKeyLength(PK11SlotInfo *slot, CK_MECHANISM_TYPE type);
162 * Open a new database using the softoken. The caller is responsible for making
163 * sure the module spec is correct and usable. The caller should ask for one
164 * new database per call if the caller wants to get meaningful information
165 * about the new database.
167 * moduleSpec is the same data that you would pass to softoken at
168 * initialization time under the 'tokens' options. For example, if you were
169 * to specify tokens=<0x4=[configdir='./mybackup' tokenDescription='Backup']>
170 * You would specify "configdir='./mybackup' tokenDescription='Backup'" as your
171 * module spec here. The slot ID will be calculated for you by
172 * SECMOD_OpenUserDB().
174 * Typical parameters here are configdir, tokenDescription and flags.
176 * a Full list is below:
179 * configDir - The location of the databases for this token. If configDir is
180 * not specified, and noCertDB and noKeyDB is not specified, the load
182 * certPrefix - Cert prefix for this token.
183 * keyPrefix - Prefix for the key database for this token. (if not specified,
184 * certPrefix will be used).
185 * tokenDescription - The label value for this token returned in the
186 * CK_TOKEN_INFO structure with an internationalize string (UTF8).
187 * This value will be truncated at 32 bytes (no NULL, partial UTF8
188 * characters dropped). You should specify a user friendly name here
189 * as this is the value the token will be referred to in most
190 * application UI's. You should make sure tokenDescription is unique.
191 * slotDescription - The slotDescription value for this token returned
192 * in the CK_SLOT_INFO structure with an internationalize string
193 * (UTF8). This value will be truncated at 64 bytes (no NULL, partial
194 * UTF8 characters dropped). This name will not change after the
195 * database is closed. It should have some number to make this unique.
196 * minPWLen - minimum password length for this token.
197 * flags - comma separated list of flag values, parsed case-insensitive.
199 * readOnly - Databases should be opened read only.
200 * noCertDB - Don't try to open a certificate database.
201 * noKeyDB - Don't try to open a key database.
202 * forceOpen - Don't fail to initialize the token if the
203 * databases could not be opened.
204 * passwordRequired - zero length passwords are not acceptable
205 * (valid only if there is a keyDB).
206 * optimizeSpace - allocate smaller hash tables and lock tables.
207 * When this flag is not specified, Softoken will allocate
208 * large tables to prevent lock contention.
210 PK11SlotInfo *SECMOD_OpenUserDB(const char *moduleSpec);
211 SECStatus SECMOD_CloseUserDB(PK11SlotInfo *slot);
214 * This is exactly the same as OpenUserDB except it can be called on any
215 * module that understands softoken style new slot entries. The resulting
216 * slot can be closed using SECMOD_CloseUserDB above. Value of moduleSpec
219 PK11SlotInfo *SECMOD_OpenNewSlot(SECMODModule *mod, const char *moduleSpec);
223 * merge the permanent objects from on token to another
225 SECStatus PK11_MergeTokens(PK11SlotInfo *targetSlot, PK11SlotInfo *sourceSlot,
226 PK11MergeLog *log, void *targetPwArg, void *sourcePwArg);
229 * create and destroy merge logs needed by PK11_MergeTokens
231 PK11MergeLog * PK11_CreateMergeLog(void);
232 void PK11_DestroyMergeLog(PK11MergeLog *log);
236 /*********************************************************************
237 * Mechanism Mapping functions
238 *********************************************************************/
239 CK_KEY_TYPE PK11_GetKeyType(CK_MECHANISM_TYPE type,unsigned long len);
240 CK_MECHANISM_TYPE PK11_GetKeyGen(CK_MECHANISM_TYPE type);
241 int PK11_GetBlockSize(CK_MECHANISM_TYPE type,SECItem *params);
242 int PK11_GetIVLength(CK_MECHANISM_TYPE type);
243 SECItem *PK11_ParamFromIV(CK_MECHANISM_TYPE type,SECItem *iv);
244 unsigned char *PK11_IVFromParam(CK_MECHANISM_TYPE type,SECItem *param,int *len);
245 SECItem * PK11_BlockData(SECItem *data,unsigned long size);
247 /* PKCS #11 to DER mapping functions */
248 SECItem *PK11_ParamFromAlgid(SECAlgorithmID *algid);
249 SECItem *PK11_GenerateNewParam(CK_MECHANISM_TYPE, PK11SymKey *);
250 CK_MECHANISM_TYPE PK11_AlgtagToMechanism(SECOidTag algTag);
251 SECOidTag PK11_MechanismToAlgtag(CK_MECHANISM_TYPE type);
252 SECOidTag PK11_FortezzaMapSig(SECOidTag algTag);
253 SECStatus PK11_ParamToAlgid(SECOidTag algtag, SECItem *param,
254 PLArenaPool *arena, SECAlgorithmID *algid);
255 SECStatus PK11_SeedRandom(PK11SlotInfo *,unsigned char *data,int len);
256 SECStatus PK11_GenerateRandomOnSlot(PK11SlotInfo *,unsigned char *data,int len);
257 SECStatus PK11_RandomUpdate(void *data, size_t bytes);
258 SECStatus PK11_GenerateRandom(unsigned char *data,int len);
260 /* warning: cannot work with pkcs 5 v2
261 * use algorithm ID s instead of pkcs #11 mechanism pointers */
262 CK_RV PK11_MapPBEMechanismToCryptoMechanism(CK_MECHANISM_PTR pPBEMechanism,
263 CK_MECHANISM_PTR pCryptoMechanism,
264 SECItem *pbe_pwd, PRBool bad3DES);
265 CK_MECHANISM_TYPE PK11_GetPadMechanism(CK_MECHANISM_TYPE);
266 CK_MECHANISM_TYPE PK11_MapSignKeyType(KeyType keyType);
268 /**********************************************************************
269 * Symmetric, Public, and Private Keys
270 **********************************************************************/
271 void PK11_FreeSymKey(PK11SymKey *key);
272 PK11SymKey *PK11_ReferenceSymKey(PK11SymKey *symKey);
273 PK11SymKey *PK11_ImportSymKey(PK11SlotInfo *slot, CK_MECHANISM_TYPE type,
274 PK11Origin origin, CK_ATTRIBUTE_TYPE operation, SECItem *key, void *wincx);
275 PK11SymKey *PK11_ImportSymKeyWithFlags(PK11SlotInfo *slot,
276 CK_MECHANISM_TYPE type, PK11Origin origin, CK_ATTRIBUTE_TYPE operation,
277 SECItem *key, CK_FLAGS flags, PRBool isPerm, void *wincx);
278 PK11SymKey *PK11_SymKeyFromHandle(PK11SlotInfo *slot, PK11SymKey *parent,
279 PK11Origin origin, CK_MECHANISM_TYPE type, CK_OBJECT_HANDLE keyID,
280 PRBool owner, void *wincx);
281 PK11SymKey *PK11_GetWrapKey(PK11SlotInfo *slot, int wrap,
282 CK_MECHANISM_TYPE type,int series, void *wincx);
284 * This function is not thread-safe. It can only be called when only
285 * one thread has a reference to wrapKey.
287 void PK11_SetWrapKey(PK11SlotInfo *slot, int wrap, PK11SymKey *wrapKey);
288 CK_MECHANISM_TYPE PK11_GetMechanism(PK11SymKey *symKey);
290 * import a public key into the desired slot
292 * This function takes a public key structure and creates a public key in a
293 * given slot. If isToken is set, then a persistant public key is created.
295 * Note: it is possible for this function to return a handle for a key which
296 * is persistant, even if isToken is not set.
298 CK_OBJECT_HANDLE PK11_ImportPublicKey(PK11SlotInfo *slot,
299 SECKEYPublicKey *pubKey, PRBool isToken);
300 PK11SymKey *PK11_KeyGen(PK11SlotInfo *slot,CK_MECHANISM_TYPE type,
301 SECItem *param, int keySize,void *wincx);
302 PK11SymKey *PK11_TokenKeyGen(PK11SlotInfo *slot, CK_MECHANISM_TYPE type,
303 SECItem *param, int keySize, SECItem *keyid,
304 PRBool isToken, void *wincx);
305 PK11SymKey *PK11_TokenKeyGenWithFlags(PK11SlotInfo *slot,
306 CK_MECHANISM_TYPE type, SECItem *param,
307 int keySize, SECItem *keyid, CK_FLAGS opFlags,
308 PK11AttrFlags attrFlags, void *wincx);
309 /* Generates a key using the exact template supplied by the caller. The other
310 * PK11_[Token]KeyGen mechanisms should be used instead of this one whenever
311 * they work because they include/exclude the CKA_VALUE_LEN template value
312 * based on the mechanism type as required by many tokens.
314 * keyGenType should be PK11_GetKeyGenWithSize(type, <key size>) or it should
315 * be equal to type if PK11_GetKeyGenWithSize cannot be used (e.g. because
316 * pk11wrap does not know about the mechanisms).
318 PK11SymKey *PK11_KeyGenWithTemplate(PK11SlotInfo *slot, CK_MECHANISM_TYPE type,
319 CK_MECHANISM_TYPE keyGenType,
320 SECItem *param, CK_ATTRIBUTE * attrs,
321 unsigned int attrsCount, void *wincx);
322 PK11SymKey * PK11_ListFixedKeysInSlot(PK11SlotInfo *slot, char *nickname,
324 PK11SymKey *PK11_GetNextSymKey(PK11SymKey *symKey);
325 CK_KEY_TYPE PK11_GetSymKeyType(PK11SymKey *key);
326 CK_OBJECT_HANDLE PK11_GetSymKeyHandle(PK11SymKey *symKey);
330 * PK11_SetSymKeyUserData
331 * sets generic user data on keys (usually a pointer to a data structure)
332 * that can later be retrieved by PK11_GetSymKeyUserData().
333 * symKey - key where data will be set.
334 * data - data to be set.
335 * freefunc - function used to free the data.
336 * Setting user data on symKeys with existing user data already set will cause
337 * the existing user data to be freed before the new user data is set.
338 * Freeing user data is done by calling the user specified freefunc.
339 * If freefunc is NULL, the user data is assumed to be global or static an
340 * not freed. Passing NULL for user data to PK11_SetSymKeyUserData has the
341 * effect of freeing any existing user data, and clearing the user data
342 * pointer. If user data exists when the symKey is finally freed, that
343 * data will be freed with freefunc.
345 * Applications should only use this function on keys which the application
346 * has created directly, as there is only one user data value per key.
348 void PK11_SetSymKeyUserData(PK11SymKey *symKey, void *data,
349 PK11FreeDataFunc freefunc);
350 /* PK11_GetSymKeyUserData
351 * retrieves generic user data which was set on a key by
352 * PK11_SetSymKeyUserData.
353 * symKey - key with data to be fetched
355 * If no data exists, or the data has been cleared, PK11_GetSymKeyUserData
356 * will return NULL. Returned data is still owned and managed by the SymKey,
357 * the caller should not free the data.
360 void *PK11_GetSymKeyUserData(PK11SymKey *symKey);
362 SECStatus PK11_PubWrapSymKey(CK_MECHANISM_TYPE type, SECKEYPublicKey *pubKey,
363 PK11SymKey *symKey, SECItem *wrappedKey);
364 SECStatus PK11_WrapSymKey(CK_MECHANISM_TYPE type, SECItem *params,
365 PK11SymKey *wrappingKey, PK11SymKey *symKey, SECItem *wrappedKey);
366 /* move a key to 'slot' optionally set the key attributes according to either
367 * operation or the flags and making the key permanent at the same time.
368 * If the key is moved to the same slot, operation and flags values are
369 * currently ignored */
370 PK11SymKey *PK11_MoveSymKey(PK11SlotInfo *slot, CK_ATTRIBUTE_TYPE operation,
371 CK_FLAGS flags, PRBool perm, PK11SymKey *symKey);
373 * derive a new key from the base key.
374 * PK11_Derive returns a key which can do exactly one operation, and is
375 * ephemeral (session key).
376 * PK11_DeriveWithFlags is the same as PK11_Derive, except you can use
377 * CKF_ flags to enable more than one operation.
378 * PK11_DeriveWithFlagsPerm is the same as PK11_DeriveWithFlags except you can
379 * (optionally) make the key permanent (token key).
381 PK11SymKey *PK11_Derive(PK11SymKey *baseKey, CK_MECHANISM_TYPE mechanism,
382 SECItem *param, CK_MECHANISM_TYPE target,
383 CK_ATTRIBUTE_TYPE operation, int keySize);
384 PK11SymKey *PK11_DeriveWithFlags( PK11SymKey *baseKey,
385 CK_MECHANISM_TYPE derive, SECItem *param, CK_MECHANISM_TYPE target,
386 CK_ATTRIBUTE_TYPE operation, int keySize, CK_FLAGS flags);
387 PK11SymKey * PK11_DeriveWithFlagsPerm( PK11SymKey *baseKey,
388 CK_MECHANISM_TYPE derive,
389 SECItem *param, CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation,
390 int keySize, CK_FLAGS flags, PRBool isPerm);
392 PK11_DeriveWithTemplate( PK11SymKey *baseKey, CK_MECHANISM_TYPE derive,
393 SECItem *param, CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation,
394 int keySize, CK_ATTRIBUTE *userAttr, unsigned int numAttrs,
398 PK11SymKey *PK11_PubDerive( SECKEYPrivateKey *privKey,
399 SECKEYPublicKey *pubKey, PRBool isSender, SECItem *randomA, SECItem *randomB,
400 CK_MECHANISM_TYPE derive, CK_MECHANISM_TYPE target,
401 CK_ATTRIBUTE_TYPE operation, int keySize,void *wincx) ;
402 PK11SymKey *PK11_PubDeriveWithKDF( SECKEYPrivateKey *privKey,
403 SECKEYPublicKey *pubKey, PRBool isSender, SECItem *randomA, SECItem *randomB,
404 CK_MECHANISM_TYPE derive, CK_MECHANISM_TYPE target,
405 CK_ATTRIBUTE_TYPE operation, int keySize,
406 CK_ULONG kdf, SECItem *sharedData, void *wincx);
409 * unwrap a new key with a symetric key.
410 * PK11_Unwrap returns a key which can do exactly one operation, and is
411 * ephemeral (session key).
412 * PK11_UnwrapWithFlags is the same as PK11_Unwrap, except you can use
413 * CKF_ flags to enable more than one operation.
414 * PK11_UnwrapWithFlagsPerm is the same as PK11_UnwrapWithFlags except you can
415 * (optionally) make the key permanent (token key).
417 PK11SymKey *PK11_UnwrapSymKey(PK11SymKey *key,
418 CK_MECHANISM_TYPE wraptype, SECItem *param, SECItem *wrapppedKey,
419 CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation, int keySize);
420 PK11SymKey *PK11_UnwrapSymKeyWithFlags(PK11SymKey *wrappingKey,
421 CK_MECHANISM_TYPE wrapType, SECItem *param, SECItem *wrappedKey,
422 CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation, int keySize,
424 PK11SymKey * PK11_UnwrapSymKeyWithFlagsPerm(PK11SymKey *wrappingKey,
425 CK_MECHANISM_TYPE wrapType,
426 SECItem *param, SECItem *wrappedKey,
427 CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation,
428 int keySize, CK_FLAGS flags, PRBool isPerm);
431 * unwrap a new key with a private key.
432 * PK11_PubUnwrap returns a key which can do exactly one operation, and is
433 * ephemeral (session key).
434 * PK11_PubUnwrapWithFlagsPerm is the same as PK11_PubUnwrap except you can
435 * use * CKF_ flags to enable more than one operation, and optionally make
436 * the key permanent (token key).
438 PK11SymKey *PK11_PubUnwrapSymKey(SECKEYPrivateKey *key, SECItem *wrapppedKey,
439 CK_MECHANISM_TYPE target, CK_ATTRIBUTE_TYPE operation, int keySize);
440 PK11SymKey * PK11_PubUnwrapSymKeyWithFlagsPerm(SECKEYPrivateKey *wrappingKey,
441 SECItem *wrappedKey, CK_MECHANISM_TYPE target,
442 CK_ATTRIBUTE_TYPE operation, int keySize,
443 CK_FLAGS flags, PRBool isPerm);
444 PK11SymKey *PK11_FindFixedKey(PK11SlotInfo *slot, CK_MECHANISM_TYPE type,
445 SECItem *keyID, void *wincx);
446 SECStatus PK11_DeleteTokenPrivateKey(SECKEYPrivateKey *privKey,PRBool force);
447 SECStatus PK11_DeleteTokenPublicKey(SECKEYPublicKey *pubKey);
448 SECStatus PK11_DeleteTokenSymKey(PK11SymKey *symKey);
449 SECStatus PK11_DeleteTokenCertAndKey(CERTCertificate *cert,void *wincx);
450 SECKEYPrivateKey * PK11_LoadPrivKey(PK11SlotInfo *slot,
451 SECKEYPrivateKey *privKey, SECKEYPublicKey *pubKey,
452 PRBool token, PRBool sensitive);
453 char * PK11_GetSymKeyNickname(PK11SymKey *symKey);
454 char * PK11_GetPrivateKeyNickname(SECKEYPrivateKey *privKey);
455 char * PK11_GetPublicKeyNickname(SECKEYPublicKey *pubKey);
456 SECStatus PK11_SetSymKeyNickname(PK11SymKey *symKey, const char *nickname);
457 SECStatus PK11_SetPrivateKeyNickname(SECKEYPrivateKey *privKey,
458 const char *nickname);
459 SECStatus PK11_SetPublicKeyNickname(SECKEYPublicKey *pubKey,
460 const char *nickname);
462 /* size to hold key in bytes */
463 unsigned int PK11_GetKeyLength(PK11SymKey *key);
464 /* size of actual secret parts of key in bits */
465 /* algid is because RC4 strength is determined by the effective bits as well
467 unsigned int PK11_GetKeyStrength(PK11SymKey *key,SECAlgorithmID *algid);
468 SECStatus PK11_ExtractKeyValue(PK11SymKey *symKey);
469 SECItem * PK11_GetKeyData(PK11SymKey *symKey);
470 PK11SlotInfo * PK11_GetSlotFromKey(PK11SymKey *symKey);
471 void *PK11_GetWindow(PK11SymKey *symKey);
474 * Explicitly set the key usage for the generated private key.
476 * This allows us to specify single use EC and RSA keys whose usage
477 * can be regulated by the underlying token.
479 * The underlying key usage is set using opFlags. opFlagsMask specifies
480 * which operations are specified by opFlags. For instance to turn encrypt
481 * on and signing off, opFlags would be CKF_ENCRYPT|CKF_DECRYPT and
482 * opFlagsMask would be CKF_ENCRYPT|CKF_DECRYPT|CKF_SIGN|CKF_VERIFY. You
483 * need to specify both the public and private key flags,
484 * PK11_GenerateKeyPairWithOpFlags will sort out the correct flag to the
485 * correct key type. Flags not specified in opFlagMask will be defaulted
486 * according to mechanism type and token capabilities.
488 SECKEYPrivateKey *PK11_GenerateKeyPairWithOpFlags(PK11SlotInfo *slot,
489 CK_MECHANISM_TYPE type, void *param, SECKEYPublicKey **pubk,
490 PK11AttrFlags attrFlags, CK_FLAGS opFlags, CK_FLAGS opFlagsMask,
493 * The attrFlags is the logical OR of the PK11_ATTR_XXX bitflags.
494 * These flags apply to the private key. The PK11_ATTR_TOKEN,
495 * PK11_ATTR_SESSION, PK11_ATTR_MODIFIABLE, and PK11_ATTR_UNMODIFIABLE
496 * flags also apply to the public key.
498 SECKEYPrivateKey *PK11_GenerateKeyPairWithFlags(PK11SlotInfo *slot,
499 CK_MECHANISM_TYPE type, void *param, SECKEYPublicKey **pubk,
500 PK11AttrFlags attrFlags, void *wincx);
501 SECKEYPrivateKey *PK11_GenerateKeyPair(PK11SlotInfo *slot,
502 CK_MECHANISM_TYPE type, void *param, SECKEYPublicKey **pubk,
503 PRBool isPerm, PRBool isSensitive, void *wincx);
504 SECKEYPrivateKey * PK11_FindPrivateKeyFromCert(PK11SlotInfo *slot,
505 CERTCertificate *cert, void *wincx);
506 SECKEYPrivateKey * PK11_FindKeyByAnyCert(CERTCertificate *cert, void *wincx);
507 SECKEYPrivateKey * PK11_FindKeyByKeyID(PK11SlotInfo *slot, SECItem *keyID,
509 int PK11_GetPrivateModulusLen(SECKEYPrivateKey *key);
511 SECStatus PK11_Decrypt(PK11SymKey *symkey,
512 CK_MECHANISM_TYPE mechanism, SECItem *param,
513 unsigned char *out, unsigned int *outLen,
515 const unsigned char *enc, unsigned int encLen);
516 SECStatus PK11_Encrypt(PK11SymKey *symKey,
517 CK_MECHANISM_TYPE mechanism, SECItem *param,
518 unsigned char *out, unsigned int *outLen,
520 const unsigned char *data, unsigned int dataLen);
522 /* note: despite the name, this function takes a private key. */
523 SECStatus PK11_PubDecryptRaw(SECKEYPrivateKey *key, unsigned char *data,
524 unsigned *outLen, unsigned int maxLen, unsigned char *enc, unsigned encLen);
525 #define PK11_PrivDecryptRaw PK11_PubDecryptRaw
526 /* The encrypt function that complements the above decrypt function. */
527 SECStatus PK11_PubEncryptRaw(SECKEYPublicKey *key, unsigned char *enc,
528 unsigned char *data, unsigned dataLen, void *wincx);
530 SECStatus PK11_PrivDecryptPKCS1(SECKEYPrivateKey *key, unsigned char *data,
531 unsigned *outLen, unsigned int maxLen, unsigned char *enc, unsigned encLen);
532 /* The encrypt function that complements the above decrypt function. */
533 SECStatus PK11_PubEncryptPKCS1(SECKEYPublicKey *key, unsigned char *enc,
534 unsigned char *data, unsigned dataLen, void *wincx);
536 SECStatus PK11_ImportPrivateKeyInfo(PK11SlotInfo *slot,
537 SECKEYPrivateKeyInfo *pki, SECItem *nickname,
538 SECItem *publicValue, PRBool isPerm, PRBool isPrivate,
539 unsigned int usage, void *wincx);
540 SECStatus PK11_ImportPrivateKeyInfoAndReturnKey(PK11SlotInfo *slot,
541 SECKEYPrivateKeyInfo *pki, SECItem *nickname,
542 SECItem *publicValue, PRBool isPerm, PRBool isPrivate,
543 unsigned int usage, SECKEYPrivateKey** privk, void *wincx);
544 SECStatus PK11_ImportDERPrivateKeyInfo(PK11SlotInfo *slot,
545 SECItem *derPKI, SECItem *nickname,
546 SECItem *publicValue, PRBool isPerm, PRBool isPrivate,
547 unsigned int usage, void *wincx);
548 SECStatus PK11_ImportDERPrivateKeyInfoAndReturnKey(PK11SlotInfo *slot,
549 SECItem *derPKI, SECItem *nickname,
550 SECItem *publicValue, PRBool isPerm, PRBool isPrivate,
551 unsigned int usage, SECKEYPrivateKey** privk, void *wincx);
552 SECStatus PK11_ImportEncryptedPrivateKeyInfo(PK11SlotInfo *slot,
553 SECKEYEncryptedPrivateKeyInfo *epki, SECItem *pwitem,
554 SECItem *nickname, SECItem *publicValue, PRBool isPerm,
555 PRBool isPrivate, KeyType type,
556 unsigned int usage, void *wincx);
557 SECStatus PK11_ImportEncryptedPrivateKeyInfoAndReturnKey(PK11SlotInfo *slot,
558 SECKEYEncryptedPrivateKeyInfo *epki, SECItem *pwitem,
559 SECItem *nickname, SECItem *publicValue, PRBool isPerm,
560 PRBool isPrivate, KeyType type,
561 unsigned int usage, SECKEYPrivateKey** privk, void *wincx);
562 SECKEYPrivateKeyInfo *PK11_ExportPrivateKeyInfo(
563 CERTCertificate *cert, void *wincx);
564 SECKEYEncryptedPrivateKeyInfo *PK11_ExportEncryptedPrivKeyInfo(
565 PK11SlotInfo *slot, SECOidTag algTag, SECItem *pwitem,
566 SECKEYPrivateKey *pk, int iteration, void *wincx);
567 SECKEYEncryptedPrivateKeyInfo *PK11_ExportEncryptedPrivateKeyInfo(
568 PK11SlotInfo *slot, SECOidTag algTag, SECItem *pwitem,
569 CERTCertificate *cert, int iteration, void *wincx);
570 SECKEYPrivateKey *PK11_FindKeyByDERCert(PK11SlotInfo *slot,
571 CERTCertificate *cert, void *wincx);
572 SECKEYPublicKey *PK11_MakeKEAPubKey(unsigned char *data, int length);
573 SECStatus PK11_DigestKey(PK11Context *context, PK11SymKey *key);
574 PRBool PK11_VerifyKeyOK(PK11SymKey *key);
575 SECKEYPrivateKey *PK11_UnwrapPrivKey(PK11SlotInfo *slot,
576 PK11SymKey *wrappingKey, CK_MECHANISM_TYPE wrapType,
577 SECItem *param, SECItem *wrappedKey, SECItem *label,
578 SECItem *publicValue, PRBool token, PRBool sensitive,
579 CK_KEY_TYPE keyType, CK_ATTRIBUTE_TYPE *usage, int usageCount,
581 SECStatus PK11_WrapPrivKey(PK11SlotInfo *slot, PK11SymKey *wrappingKey,
582 SECKEYPrivateKey *privKey, CK_MECHANISM_TYPE wrapType,
583 SECItem *param, SECItem *wrappedKey, void *wincx);
585 * The caller of PK11_DEREncodePublicKey should free the returned SECItem with
586 * a SECITEM_FreeItem(..., PR_TRUE) call.
588 SECItem* PK11_DEREncodePublicKey(SECKEYPublicKey *pubk);
589 PK11SymKey* PK11_CopySymKeyForSigning(PK11SymKey *originalKey,
590 CK_MECHANISM_TYPE mech);
591 SECKEYPrivateKeyList* PK11_ListPrivKeysInSlot(PK11SlotInfo *slot,
592 char *nickname, void *wincx);
593 SECKEYPublicKeyList* PK11_ListPublicKeysInSlot(PK11SlotInfo *slot,
595 SECKEYPQGParams *PK11_GetPQGParamsFromPrivateKey(SECKEYPrivateKey *privKey);
597 SECKEYPrivateKeyList* PK11_ListPrivateKeysInSlot(PK11SlotInfo *slot);
599 PK11SymKey *PK11_ConvertSessionSymKeyToTokenSymKey(PK11SymKey *symk,
601 SECKEYPrivateKey *PK11_ConvertSessionPrivKeyToTokenPrivKey(
602 SECKEYPrivateKey *privk, void* wincx);
603 SECKEYPrivateKey * PK11_CopyTokenPrivKeyToSessionPrivKey(PK11SlotInfo *destSlot,
604 SECKEYPrivateKey *privKey);
606 /**********************************************************************
608 **********************************************************************/
609 SECItem *PK11_MakeIDFromPubKey(SECItem *pubKeyData);
610 SECStatus PK11_TraverseSlotCerts(
611 SECStatus(* callback)(CERTCertificate*,SECItem *,void *),
612 void *arg, void *wincx);
613 CERTCertificate * PK11_FindCertFromNickname(const char *nickname, void *wincx);
614 CERTCertList * PK11_FindCertsFromEmailAddress(const char *email, void *wincx);
615 CERTCertList * PK11_FindCertsFromNickname(const char *nickname, void *wincx);
616 CERTCertificate *PK11_GetCertFromPrivateKey(SECKEYPrivateKey *privKey);
617 SECStatus PK11_ImportCert(PK11SlotInfo *slot, CERTCertificate *cert,
618 CK_OBJECT_HANDLE key, const char *nickname,
619 PRBool includeTrust);
620 SECStatus PK11_ImportDERCert(PK11SlotInfo *slot, SECItem *derCert,
621 CK_OBJECT_HANDLE key, char *nickname, PRBool includeTrust);
622 PK11SlotInfo *PK11_ImportCertForKey(CERTCertificate *cert,
623 const char *nickname, void *wincx);
624 PK11SlotInfo *PK11_ImportDERCertForKey(SECItem *derCert, char *nickname,
626 PK11SlotInfo *PK11_KeyForCertExists(CERTCertificate *cert,
627 CK_OBJECT_HANDLE *keyPtr, void *wincx);
628 PK11SlotInfo *PK11_KeyForDERCertExists(SECItem *derCert,
629 CK_OBJECT_HANDLE *keyPtr, void *wincx);
630 CERTCertificate * PK11_FindCertByIssuerAndSN(PK11SlotInfo **slot,
631 CERTIssuerAndSN *sn, void *wincx);
632 CERTCertificate * PK11_FindCertAndKeyByRecipientList(PK11SlotInfo **slot,
633 SEC_PKCS7RecipientInfo **array, SEC_PKCS7RecipientInfo **rip,
634 SECKEYPrivateKey**privKey, void *wincx);
635 int PK11_FindCertAndKeyByRecipientListNew(NSSCMSRecipient **recipientlist,
637 SECStatus PK11_TraverseCertsForSubjectInSlot(CERTCertificate *cert,
638 PK11SlotInfo *slot, SECStatus(*callback)(CERTCertificate *, void *),
640 CERTCertificate *PK11_FindCertFromDERCert(PK11SlotInfo *slot,
641 CERTCertificate *cert, void *wincx);
642 CERTCertificate *PK11_FindCertFromDERCertItem(PK11SlotInfo *slot,
643 const SECItem *derCert, void *wincx);
644 SECStatus PK11_ImportCertForKeyToSlot(PK11SlotInfo *slot, CERTCertificate *cert,
645 char *nickname, PRBool addUsage,
647 CERTCertificate *PK11_FindBestKEAMatch(CERTCertificate *serverCert,void *wincx);
648 PRBool PK11_FortezzaHasKEA(CERTCertificate *cert);
649 CK_OBJECT_HANDLE PK11_FindCertInSlot(PK11SlotInfo *slot, CERTCertificate *cert,
651 SECStatus PK11_TraverseCertsForNicknameInSlot(SECItem *nickname,
652 PK11SlotInfo *slot, SECStatus(*callback)(CERTCertificate *, void *),
654 CERTCertList * PK11_ListCerts(PK11CertListType type, void *pwarg);
655 CERTCertList * PK11_ListCertsInSlot(PK11SlotInfo *slot);
656 CERTSignedCrl* PK11_ImportCRL(PK11SlotInfo * slot, SECItem *derCRL, char *url,
657 int type, void *wincx, PRInt32 importOptions, PLArenaPool* arena, PRInt32 decodeOptions);
659 /**********************************************************************
661 **********************************************************************/
664 * Return the length in bytes of a signature generated with the
667 * Return 0 or -1 on failure. (XXX Should we fix it to always return
670 int PK11_SignatureLen(SECKEYPrivateKey *key);
671 PK11SlotInfo * PK11_GetSlotFromPrivateKey(SECKEYPrivateKey *key);
672 SECStatus PK11_Sign(SECKEYPrivateKey *key, SECItem *sig,
673 const SECItem *hash);
674 SECStatus PK11_SignWithSymKey(PK11SymKey *symKey, CK_MECHANISM_TYPE mechanism,
675 SECItem *param, SECItem *sig, const SECItem *data);
676 SECStatus PK11_VerifyRecover(SECKEYPublicKey *key, const SECItem *sig,
677 SECItem *dsig, void * wincx);
678 SECStatus PK11_Verify(SECKEYPublicKey *key, const SECItem *sig,
679 const SECItem *hash, void *wincx);
683 /**********************************************************************
685 **********************************************************************/
686 void PK11_DestroyContext(PK11Context *context, PRBool freeit);
687 PK11Context *PK11_CreateContextBySymKey(CK_MECHANISM_TYPE type,
688 CK_ATTRIBUTE_TYPE operation, PK11SymKey *symKey, SECItem *param);
689 PK11Context *PK11_CreateDigestContext(SECOidTag hashAlg);
690 PK11Context *PK11_CloneContext(PK11Context *old);
691 SECStatus PK11_DigestBegin(PK11Context *cx);
693 * The output buffer 'out' must be big enough to hold the output of
694 * the hash algorithm 'hashAlg'.
696 SECStatus PK11_HashBuf(SECOidTag hashAlg, unsigned char *out,
697 const unsigned char *in, PRInt32 len);
698 SECStatus PK11_DigestOp(PK11Context *context, const unsigned char *in,
700 SECStatus PK11_CipherOp(PK11Context *context, unsigned char * out, int *outlen,
701 int maxout, const unsigned char *in, int inlen);
702 SECStatus PK11_Finalize(PK11Context *context);
703 SECStatus PK11_DigestFinal(PK11Context *context, unsigned char *data,
704 unsigned int *outLen, unsigned int length);
705 #define PK11_CipherFinal PK11_DigestFinal
706 SECStatus PK11_SaveContext(PK11Context *cx,unsigned char *save,
707 int *len, int saveLength);
709 /* Save the context's state, with possible allocation.
710 * The caller may supply an already allocated buffer in preAllocBuf,
711 * with length pabLen. If the buffer is large enough for the context's
712 * state, it will receive the state.
713 * If the buffer is not large enough (or NULL), then a new buffer will
714 * be allocated with PORT_Alloc.
715 * In either case, the state will be returned as a buffer, and the length
716 * of the state will be given in *stateLen.
719 PK11_SaveContextAlloc(PK11Context *cx,
720 unsigned char *preAllocBuf, unsigned int pabLen,
721 unsigned int *stateLen);
723 SECStatus PK11_RestoreContext(PK11Context *cx,unsigned char *save,int len);
724 SECStatus PK11_GenerateFortezzaIV(PK11SymKey *symKey,unsigned char *iv,int len);
725 void PK11_SetFortezzaHack(PK11SymKey *symKey) ;
728 /**********************************************************************
730 **********************************************************************/
732 /* This function creates PBE parameters from the given inputs. The result
733 * can be used to create a password integrity key for PKCS#12, by sending
734 * the return value to PK11_KeyGen along with the appropriate mechanism.
737 PK11_CreatePBEParams(SECItem *salt, SECItem *pwd, unsigned int iterations);
739 /* free params created above (can be called after keygen is done */
740 void PK11_DestroyPBEParams(SECItem *params);
743 PK11_CreatePBEAlgorithmID(SECOidTag algorithm, int iteration, SECItem *salt);
745 /* use to create PKCS5 V2 algorithms with finder control than that provided
746 * by PK11_CreatePBEAlgorithmID. */
748 PK11_CreatePBEV2AlgorithmID(SECOidTag pbeAlgTag, SECOidTag cipherAlgTag,
749 SECOidTag prfAlgTag, int keyLength, int iteration,
752 PK11_PBEKeyGen(PK11SlotInfo *slot, SECAlgorithmID *algid, SECItem *pwitem,
753 PRBool faulty3DES, void *wincx);
755 /* warning: cannot work with PKCS 5 v2 use PK11_PBEKeyGen instead */
757 PK11_RawPBEKeyGen(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, SECItem *params,
758 SECItem *pwitem, PRBool faulty3DES, void *wincx);
760 PK11_GetPBEIV(SECAlgorithmID *algid, SECItem *pwitem);
762 * Get the Mechanism and parameter of the base encryption or mac scheme from
763 * a PBE algorithm ID.
764 * Caller is responsible for freeing the return parameter (param).
767 PK11_GetPBECryptoMechanism(SECAlgorithmID *algid,
768 SECItem **param, SECItem *pwd);
770 /**********************************************************************
771 * Functions to manage secmod flags
772 **********************************************************************/
773 PK11DefaultArrayEntry *PK11_GetDefaultArray(int *size);
774 SECStatus PK11_UpdateSlotAttribute(PK11SlotInfo *slot,
775 PK11DefaultArrayEntry *entry, PRBool add);
777 /**********************************************************************
778 * Functions to look at PKCS #11 dependent data
779 **********************************************************************/
780 PK11GenericObject *PK11_FindGenericObjects(PK11SlotInfo *slot,
781 CK_OBJECT_CLASS objClass);
782 PK11GenericObject *PK11_GetNextGenericObject(PK11GenericObject *object);
783 PK11GenericObject *PK11_GetPrevGenericObject(PK11GenericObject *object);
784 SECStatus PK11_UnlinkGenericObject(PK11GenericObject *object);
785 SECStatus PK11_LinkGenericObject(PK11GenericObject *list,
786 PK11GenericObject *object);
787 SECStatus PK11_DestroyGenericObjects(PK11GenericObject *object);
788 SECStatus PK11_DestroyGenericObject(PK11GenericObject *object);
789 PK11GenericObject *PK11_CreateGenericObject(PK11SlotInfo *slot,
790 const CK_ATTRIBUTE *pTemplate,
791 int count, PRBool token);
794 * PK11_ReadRawAttribute and PK11_WriteRawAttribute are generic
795 * functions to read and modify the actual PKCS #11 attributes of
796 * the underlying pkcs #11 object.
798 * object is a pointer to an NSS object that represents the underlying
799 * PKCS #11 object. It's type must match the type of PK11ObjectType
803 * PK11_TypeGeneric PK11GenericObject *
804 * PK11_TypePrivKey SECKEYPrivateKey *
805 * PK11_TypePubKey SECKEYPublicKey *
806 * PK11_TypeSymKey PK11SymKey *
808 * All other types are considered invalid. If type does not match the object
809 * passed, unpredictable results will occur.
811 * PK11_ReadRawAttribute allocates the buffer for returning the attribute
812 * value. The caller of PK11_ReadRawAttribute should free the data buffer
813 * pointed to by item using a SECITEM_FreeItem(item, PR_FALSE) or
814 * PORT_Free(item->data) call.
816 SECStatus PK11_ReadRawAttribute(PK11ObjectType type, void *object,
817 CK_ATTRIBUTE_TYPE attr, SECItem *item);
818 SECStatus PK11_WriteRawAttribute(PK11ObjectType type, void *object,
819 CK_ATTRIBUTE_TYPE attr, SECItem *item);
822 * PK11_GetAllSlotsForCert returns all the slots that a given certificate
823 * exists on, since it's possible for a cert to exist on more than one
827 PK11_GetAllSlotsForCert(CERTCertificate *cert, void *arg);
829 /**********************************************************************
830 * New functions which are already deprecated....
831 **********************************************************************/
833 PK11_GetLowLevelKeyIDForCert(PK11SlotInfo *slot,
834 CERTCertificate *cert, void *pwarg);
836 PK11_GetLowLevelKeyIDForPrivateKey(SECKEYPrivateKey *key);
838 PRBool SECMOD_HasRootCerts(void);