Imported Upstream version 3.13.6
[platform/upstream/nss.git] / mozilla / security / nss / lib / cryptohi / keythi.h
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is the Netscape security libraries.
15  *
16  * The Initial Developer of the Original Code is
17  * Netscape Communications Corporation.
18  * Portions created by the Initial Developer are Copyright (C) 1994-2000
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  *   Dr Vipul Gupta <vipul.gupta@sun.com>, Sun Microsystems Laboratories
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 #ifndef _KEYTHI_H_
38 #define _KEYTHI_H_ 1
39
40 #include "plarena.h"
41 #include "pkcs11t.h"
42 #include "secmodt.h"
43 #include "prclist.h"
44
45 /*
46 ** RFC 4055 Section 1.2 specifies three different RSA key types.
47 **
48 ** rsaKey maps to keys with SEC_OID_PKCS1_RSA_ENCRYPTION and can be used for
49 ** both encryption and signatures with old (PKCS #1 v1.5) and new (PKCS #1
50 ** v2.1) padding schemes.
51 **
52 ** rsaPssKey maps to keys with SEC_OID_PKCS1_RSA_PSS_SIGNATURE and may only
53 ** be used for signatures with PSS padding (PKCS #1 v2.1).
54 **
55 ** rsaOaepKey maps to keys with SEC_OID_PKCS1_RSA_OAEP_ENCRYPTION and may only
56 ** be used for encryption with OAEP padding (PKCS #1 v2.1).
57 */ 
58
59 typedef enum { 
60     nullKey = 0, 
61     rsaKey = 1, 
62     dsaKey = 2, 
63     fortezzaKey = 3, /* deprecated */
64     dhKey = 4, 
65     keaKey = 5, /* deprecated */
66     ecKey = 6,
67     rsaPssKey = 7,
68     rsaOaepKey = 8
69 } KeyType;
70
71 /*
72 ** Template Definitions
73 **/
74
75 SEC_BEGIN_PROTOS
76 extern const SEC_ASN1Template SECKEY_RSAPublicKeyTemplate[];
77 extern const SEC_ASN1Template SECKEY_RSAPSSParamsTemplate[];
78 extern const SEC_ASN1Template SECKEY_DSAPublicKeyTemplate[];
79 extern const SEC_ASN1Template SECKEY_DHPublicKeyTemplate[];
80 extern const SEC_ASN1Template SECKEY_DHParamKeyTemplate[];
81 extern const SEC_ASN1Template SECKEY_PQGParamsTemplate[];
82 extern const SEC_ASN1Template SECKEY_DSAPrivateKeyExportTemplate[];
83
84 /* Windows DLL accessor functions */
85 SEC_ASN1_CHOOSER_DECLARE(SECKEY_DSAPublicKeyTemplate)
86 SEC_ASN1_CHOOSER_DECLARE(SECKEY_RSAPublicKeyTemplate)
87 SEC_ASN1_CHOOSER_DECLARE(SECKEY_RSAPSSParamsTemplate)
88 SEC_END_PROTOS
89
90
91 /*
92 ** RSA Public Key structures
93 ** member names from PKCS#1, section 7.1 
94 */
95
96 struct SECKEYRSAPublicKeyStr {
97     PLArenaPool * arena;
98     SECItem modulus;
99     SECItem publicExponent;
100 };
101 typedef struct SECKEYRSAPublicKeyStr SECKEYRSAPublicKey;
102
103 /* 
104 ** RSA-PSS parameters
105 */
106 struct SECKEYRSAPSSParamsStr {
107     SECAlgorithmID *hashAlg;
108     SECAlgorithmID *maskAlg;
109     SECItem saltLength;
110     SECItem trailerField;
111 };
112 typedef struct SECKEYRSAPSSParamsStr SECKEYRSAPSSParams;
113
114 /*
115 ** DSA Public Key and related structures
116 */
117
118 struct SECKEYPQGParamsStr {
119     PLArenaPool *arena;
120     SECItem prime;    /* p */
121     SECItem subPrime; /* q */
122     SECItem base;     /* g */
123     /* XXX chrisk: this needs to be expanded to hold j and validationParms (RFC2459 7.3.2) */
124 };
125 typedef struct SECKEYPQGParamsStr SECKEYPQGParams;
126
127 struct SECKEYDSAPublicKeyStr {
128     SECKEYPQGParams params;
129     SECItem publicValue;
130 };
131 typedef struct SECKEYDSAPublicKeyStr SECKEYDSAPublicKey;
132
133
134 /*
135 ** Diffie-Hellman Public Key structure
136 ** Structure member names suggested by PKCS#3.
137 */
138 struct SECKEYDHParamsStr {
139     PLArenaPool * arena;
140     SECItem prime; /* p */
141     SECItem base; /* g */
142 };
143 typedef struct SECKEYDHParamsStr SECKEYDHParams;
144
145 struct SECKEYDHPublicKeyStr {
146     PLArenaPool * arena;
147     SECItem prime;
148     SECItem base;
149     SECItem publicValue;
150 };
151 typedef struct SECKEYDHPublicKeyStr SECKEYDHPublicKey;
152
153 /*
154 ** Elliptic curve Public Key structure
155 ** The PKCS#11 layer needs DER encoding of ANSI X9.62
156 ** parameters value
157 */
158 typedef SECItem SECKEYECParams;
159
160 struct SECKEYECPublicKeyStr {
161     SECKEYECParams DEREncodedParams;
162     int     size;             /* size in bits */
163     SECItem publicValue;      /* encoded point */
164     /* XXX Even though the PKCS#11 interface takes encoded parameters,
165      * we may still wish to decode them above PKCS#11 for things like
166      * printing key information. For named curves, which is what
167      * we initially support, we ought to have the curve name at the
168      * very least.
169      */
170 };
171 typedef struct SECKEYECPublicKeyStr SECKEYECPublicKey;
172
173 /*
174 ** FORTEZZA Public Key structures
175 */
176 struct SECKEYFortezzaPublicKeyStr {
177     int      KEAversion;
178     int      DSSversion;
179     unsigned char    KMID[8];
180     SECItem clearance;
181     SECItem KEApriviledge;
182     SECItem DSSpriviledge;
183     SECItem KEAKey;
184     SECItem DSSKey;
185     SECKEYPQGParams params;
186     SECKEYPQGParams keaParams;
187 };
188 typedef struct SECKEYFortezzaPublicKeyStr SECKEYFortezzaPublicKey;
189 #define KEAprivilege KEApriviledge /* corrected spelling */
190 #define DSSprivilege DSSpriviledge /* corrected spelling */
191
192 struct SECKEYDiffPQGParamsStr {
193     SECKEYPQGParams DiffKEAParams;
194     SECKEYPQGParams DiffDSAParams;
195 };
196 typedef struct SECKEYDiffPQGParamsStr SECKEYDiffPQGParams;
197
198 struct SECKEYPQGDualParamsStr {
199     SECKEYPQGParams CommParams;
200     SECKEYDiffPQGParams DiffParams;
201 };
202 typedef struct SECKEYPQGDualParamsStr SECKEYPQGDualParams;
203
204 struct SECKEYKEAParamsStr {
205     PLArenaPool *arena;
206     SECItem hash;
207 };
208 typedef struct SECKEYKEAParamsStr SECKEYKEAParams;
209  
210 struct SECKEYKEAPublicKeyStr {
211     SECKEYKEAParams params;
212     SECItem publicValue;
213 };
214 typedef struct SECKEYKEAPublicKeyStr SECKEYKEAPublicKey;
215
216 /*
217 ** A Generic  public key object.
218 */
219 struct SECKEYPublicKeyStr {
220     PLArenaPool *arena;
221     KeyType keyType;
222     PK11SlotInfo *pkcs11Slot;
223     CK_OBJECT_HANDLE pkcs11ID;
224     union {
225         SECKEYRSAPublicKey rsa;
226         SECKEYDSAPublicKey dsa;
227         SECKEYDHPublicKey  dh;
228         SECKEYKEAPublicKey kea;
229         SECKEYFortezzaPublicKey fortezza;
230         SECKEYECPublicKey  ec;
231     } u;
232 };
233 typedef struct SECKEYPublicKeyStr SECKEYPublicKey;
234
235 /* bit flag definitions for staticflags */
236 #define SECKEY_Attributes_Cached 0x1    /* bit 0 states
237                                            whether attributes are cached */
238 #define SECKEY_CKA_PRIVATE (1U << 1)    /* bit 1 is the value of CKA_PRIVATE */
239
240 #define SECKEY_ATTRIBUTES_CACHED(key) \
241      (0 != (key->staticflags & SECKEY_Attributes_Cached))
242
243 #define SECKEY_ATTRIBUTE_VALUE(key,attribute) \
244      (0 != (key->staticflags & SECKEY_##attribute))
245
246 #define SECKEY_HAS_ATTRIBUTE_SET(key,attribute) \
247     (0 != (key->staticflags & SECKEY_Attributes_Cached)) ? \
248     (0 != (key->staticflags & SECKEY_##attribute)) : \
249     PK11_HasAttributeSet(key->pkcs11Slot,key->pkcs11ID,attribute)
250
251 /*
252 ** A generic key structure
253 */ 
254 struct SECKEYPrivateKeyStr {
255     PLArenaPool *arena;
256     KeyType keyType;
257     PK11SlotInfo *pkcs11Slot;   /* pkcs11 slot this key lives in */
258     CK_OBJECT_HANDLE pkcs11ID;  /* ID of pkcs11 object */
259     PRBool pkcs11IsTemp;        /* temp pkcs11 object, delete it when done */
260     void *wincx;                /* context for errors and pw prompts */
261     PRUint32 staticflags;       /* bit flag of cached PKCS#11 attributes */
262 };
263 typedef struct SECKEYPrivateKeyStr SECKEYPrivateKey;
264
265 typedef struct {
266     PRCList links;
267     SECKEYPrivateKey *key;
268 } SECKEYPrivateKeyListNode;
269
270 typedef struct {
271     PRCList list;
272     PLArenaPool *arena;
273 } SECKEYPrivateKeyList;
274
275 typedef struct {
276     PRCList links;
277     SECKEYPublicKey *key;
278 } SECKEYPublicKeyListNode;
279
280 typedef struct {
281     PRCList list;
282     PLArenaPool *arena;
283 } SECKEYPublicKeyList;
284 #endif /* _KEYTHI_H_ */
285