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/. */
4 /*
5  * Internal header file included only by files in pkcs11 dir, or in
6  * pkcs11 specific client and server files.
7  */
8 
9 #ifndef _SECMODTI_H_
10 #define _SECMODTI_H_ 1
11 #include "prmon.h"
12 #include "prtypes.h"
13 #include "nssilckt.h"
14 #include "secmodt.h"
15 #include "pkcs11t.h"
16 
17 #include "nssdevt.h"
18 
19 /* internal data structures */
20 
21 /* Traverse slots callback */
22 typedef struct pk11TraverseSlotStr {
23     SECStatus (*callback)(PK11SlotInfo *, CK_OBJECT_HANDLE, void *);
24     void *callbackArg;
25     CK_ATTRIBUTE *findTemplate;
26     int templateCount;
27 } pk11TraverseSlot;
28 
29 /* represent a pkcs#11 slot reference counted. */
30 struct PK11SlotInfoStr {
31     /* the PKCS11 function list for this slot */
32     void *functionList;
33     SECMODModule *module; /* our parent module */
34     /* Boolean to indicate the current state of this slot */
35     PRBool needTest;           /* Has this slot been tested for Export complience */
36     PRBool isPerm;             /* is this slot a permanment device */
37     PRBool isHW;               /* is this slot a hardware device */
38     PRBool isInternal;         /* is this slot one of our internal PKCS #11 devices */
39     PRBool disabled;           /* is this slot disabled... */
40     PK11DisableReasons reason; /* Why this slot is disabled */
41     PRBool readOnly;           /* is the token in this slot read-only */
42     PRBool needLogin;          /* does the token of the type that needs
43                                 * authentication (still true even if token is logged
44                                 * in) */
45     PRBool hasRandom;          /* can this token generated random numbers */
46     PRBool defRWSession;       /* is the default session RW (we open our default
47                                 * session rw if the token can only handle one session
48                                 * at a time. */
49     PRBool isThreadSafe;       /* copied from the module */
50     /* The actual flags (many of which are distilled into the above PRBools) */
51     CK_FLAGS flags; /* flags from PKCS #11 token Info */
52     /* a default session handle to do quick and dirty functions */
53     CK_SESSION_HANDLE session;
54     PZLock *sessionLock; /* lock for this session */
55     /* our ID */
56     CK_SLOT_ID slotID;
57     /* persistant flags saved from startup to startup */
58     unsigned long defaultFlags;
59     /* keep track of who is using us so we don't accidently get freed while
60      * still in use */
61     PRInt32 refCount; /* to be in/decremented by atomic calls ONLY! */
62     PZLock *freeListLock;
63     PK11SymKey *freeSymKeysWithSessionHead;
64     PK11SymKey *freeSymKeysHead;
65     int keyCount;
66     int maxKeyCount;
67     /* Password control functions for this slot. many of these are only
68      * active if the appropriate flag is on in defaultFlags */
69     int askpw;           /* what our password options are */
70     int timeout;         /* If we're ask_timeout, what is our timeout time is
71                           * seconds */
72     int authTransact;    /* allow multiple authentications off one password if
73                           * they are all part of the same transaction */
74     PRTime authTime;     /* when were we last authenticated */
75     int minPassword;     /* smallest legal password */
76     int maxPassword;     /* largest legal password */
77     PRUint16 series;     /* break up the slot info into various groups of
78                           * inserted tokens so that keys and certs can be
79                           * invalidated */
80     PRUint16 flagSeries; /* record the last series for the last event
81                           * returned for this slot */
82     PRBool flagState;    /* record the state of the last event returned for this
83                           * slot. */
84     PRUint16 wrapKey;    /* current wrapping key for SSL master secrets */
85     CK_MECHANISM_TYPE wrapMechanism;
86     /* current wrapping mechanism for current wrapKey */
87     CK_OBJECT_HANDLE refKeys[1];      /* array of existing wrapping keys for */
88     CK_MECHANISM_TYPE *mechanismList; /* list of mechanism supported by this
89                                        * token */
90     int mechanismCount;
91     /* cache the certificates stored on the token of this slot */
92     CERTCertificate **cert_array;
93     int array_size;
94     int cert_count;
95     char serial[16];
96     /* since these are odd sizes, keep them last. They are odd sizes to
97      * allow them to become null terminated strings */
98     char slot_name[65];
99     char token_name[33];
100     PRBool hasRootCerts;
101     PRBool hasRootTrust;
102     PRBool hasRSAInfo;
103     CK_FLAGS RSAInfoFlags;
104     PRBool protectedAuthPath;
105     PRBool isActiveCard;
106     PRIntervalTime lastLoginCheck;
107     unsigned int lastState;
108     /* for Stan */
109     NSSToken *nssToken;
110     PZLock *nssTokenLock;
111     /* the tokeninfo struct */
112     CK_TOKEN_INFO tokenInfo;
113     /* fast mechanism lookup */
114     char mechanismBits[256];
115     CK_PROFILE_ID *profileList;
116     int profileCount;
117 };
118 
119 /* Symetric Key structure. Reference Counted */
120 struct PK11SymKeyStr {
121     CK_MECHANISM_TYPE type;    /* type of operation this key was created for*/
122     CK_OBJECT_HANDLE objectID; /* object id of this key in the slot */
123     PK11SlotInfo *slot;        /* Slot this key is loaded into */
124     void *cx;                  /* window context in case we need to loggin */
125     PK11SymKey *next;
126     PRBool owner;
127     SECItem data; /* raw key data if available */
128     CK_SESSION_HANDLE session;
129     PRBool sessionOwner;
130     PRInt32 refCount;          /* number of references to this key */
131     int size;                  /* key size in bytes */
132     PK11Origin origin;         /* where this key came from
133                                 * (see def in secmodt.h) */
134     PK11SymKey *parent;        /* potential owner key of the session */
135     PRUint16 series;           /* break up the slot info into various groups
136                                 * of inserted tokens so that keys and certs
137                                 * can be invalidated */
138     void *userData;            /* random data the application can attach to
139                                 * this key */
140     PK11FreeDataFunc freeFunc; /* function to free the user data */
141 };
142 
143 /*
144  * hold a hash, encryption or signing context for multi-part operations.
145  * hold enough information so that multiple contexts can be interleaved
146  * if necessary. ... Not RefCounted.
147  */
148 struct PK11ContextStr {
149     CK_ATTRIBUTE_TYPE operation;          /* type of operation this context is
150                                            * doing (CKA_ENCRYPT, CKA_SIGN,
151                                            * CKA_HASH, etc.) */
152     PK11SymKey *key;                      /* symetric key for this context */
153     CK_OBJECT_HANDLE objectID;            /* object handle to key */
154     PK11SlotInfo *slot;                   /* slot this context is using */
155     CK_SESSION_HANDLE session;            /* session this context is using */
156     PZLock *sessionLock;                  /* lock before accessing a PKCS #11
157                                            * session */
158     PRBool ownSession;                    /* do we own the session? */
159     void *pwArg;                          /* applicaton specific passwd arg */
160     void *savedData;                      /* save data when we are
161                                            * multiplexing on a single context */
162     unsigned long savedLength;            /* length of the saved context */
163     SECItem *param;                       /* mechanism parameters used to
164                                            * build this context */
165     PRBool init;                          /* this contexted been initialized? */
166     CK_MECHANISM_TYPE type;               /* what is the PKCS #11 this context
167                                            * is representing (usually what
168                                            * algorithm is being used
169                                            * (CKM_RSA_PKCS, CKM_DES, CKM_SHA,
170                                            * etc. */
171     PRBool fortezzaHack;                  /* Fortezza SSL has some special
172                                            * non-standard semantics*/
173     PRBool simulate_message;              /* We are initializing a message
174                                            * interface but the underlying
175                                            * PKCS #11 module does not support
176                                            * it. We simulate the interface with
177                                            * the PCKS #11 v2 interface */
178     CK_MECHANISM_TYPE simulate_mechanism; /* The mechanism we are simulating */
179     PRUint64 ivCounter;                   /* iv counter for simulated message */
180     PRUint64 ivMaxCount;                  /* total number of IVs valid for
181                                            * this key */
182     unsigned long ivLen;                  /* length of the iv in bytes */
183     unsigned int ivFixedBits;             /* number of bits not generated
184                                            * for the iv */
185     CK_GENERATOR_FUNCTION ivGen;          /* PKCS #11 iv generator value */
186 };
187 
188 /*
189  * structure to hold a pointer to a unique PKCS #11 object
190  * (pointer to the slot and the object id).
191  */
192 struct PK11GenericObjectStr {
193     PK11GenericObject *prev;
194     PK11GenericObject *next;
195     PK11SlotInfo *slot;
196     CK_OBJECT_HANDLE objectID;
197     PRBool owner;
198 };
199 
200 #define MAX_TEMPL_ATTRS 16 /* maximum attributes in template */
201 
202 /* This mask includes all CK_FLAGs with an equivalent CKA_ attribute. */
203 #define CKF_KEY_OPERATION_FLAGS 0x000e7b00UL
204 
205 #endif /* _SECMODTI_H_ */
206