1 /*
2  * Copyright (c) 2011 .SE (The Internet Infrastructure Foundation)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*****************************************************************************
28  P11Objects.h
29 
30  This class respresent a PKCS#11 object
31  *****************************************************************************/
32 
33 #ifndef _SOFTHSM_V2_P11OBJECTS_H
34 #define _SOFTHSM_V2_P11OBJECTS_H
35 
36 #include "OSObject.h"
37 #include "P11Attributes.h"
38 #include "Token.h"
39 #include "cryptoki.h"
40 #include <map>
41 
42 class P11Object
43 {
44 public:
45 	// Destructor
46 	virtual ~P11Object();
47 
48 protected:
49 	// Constructor
50 	P11Object();
51 
52 	// The object
53 	OSObject* osobject;
54 
55 	// The attributes
56 	std::map<CK_ATTRIBUTE_TYPE, P11Attribute*> attributes;
57 
58 public:
59 	// Add attributes
60 	virtual bool init(OSObject *inobject);
61 
62 protected:
63 	bool initialized;
64 
65 public:
66 	CK_RV loadTemplate(Token *token, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount);
67 
68 	// Save template
69 	CK_RV saveTemplate(Token *token, bool isPrivate, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount, int op);
70 
71 protected:
72 	bool isPrivate();
73 	bool isCopyable();
74 	bool isModifiable();
75 };
76 
77 class P11DataObj : public P11Object
78 {
79 public:
80 	// Constructor
81 	P11DataObj();
82 
83 	// Add attributes
84 	virtual bool init(OSObject *inobject);
85 
86 protected:
87 	bool initialized;
88 };
89 
90 class P11CertificateObj : public P11Object
91 {
92 protected:
93 	// Constructor
94 	P11CertificateObj();
95 
96 	// Add attributes
97 	virtual bool init(OSObject *inobject);
98 	bool initialized;
99 };
100 
101 class P11X509CertificateObj : public P11CertificateObj
102 {
103 public:
104 	// Constructor
105 	P11X509CertificateObj();
106 
107 	// Add attributes
108 	virtual bool init(OSObject *inobject);
109 
110 protected:
111 	bool initialized;
112 };
113 
114 class P11OpenPGPPublicKeyObj : public P11CertificateObj
115 {
116 public:
117 	// Constructor
118 	P11OpenPGPPublicKeyObj();
119 
120 	// Add attributes
121 	virtual bool init(OSObject *inobject);
122 
123 protected:
124 	bool initialized;
125 };
126 
127 class P11KeyObj : public P11Object
128 {
129 protected:
130 	// Constructor
131 	P11KeyObj();
132 
133 	// Add attributes
134 	virtual bool init(OSObject *inobject);
135 	bool initialized;
136 };
137 
138 class P11PublicKeyObj : public P11KeyObj
139 {
140 protected:
141 	// Constructor
142 	P11PublicKeyObj();
143 
144 	// Add attributes
145 	virtual bool init(OSObject *inobject);
146 	bool initialized;
147 };
148 
149 class P11RSAPublicKeyObj : public P11PublicKeyObj
150 {
151 public:
152 	// Constructor
153 	P11RSAPublicKeyObj();
154 
155 	// Add attributes
156 	virtual bool init(OSObject *inobject);
157 
158 protected:
159 	bool initialized;
160 };
161 
162 class P11DSAPublicKeyObj : public P11PublicKeyObj
163 {
164 public:
165 	// Constructor
166 	P11DSAPublicKeyObj();
167 
168 	// Add attributes
169 	virtual bool init(OSObject *inobject);
170 
171 protected:
172 	bool initialized;
173 };
174 
175 class P11ECPublicKeyObj : public P11PublicKeyObj
176 {
177 public:
178 	// Constructor
179 	P11ECPublicKeyObj();
180 
181 	// Add attributes
182 	virtual bool init(OSObject *inobject);
183 
184 protected:
185 	bool initialized;
186 };
187 
188 class P11EDPublicKeyObj : public P11PublicKeyObj
189 {
190 public:
191 	// Constructor
192 	P11EDPublicKeyObj();
193 
194 	// Add attributes
195 	virtual bool init(OSObject *inobject);
196 
197 protected:
198 	bool initialized;
199 };
200 
201 class P11DHPublicKeyObj : public P11PublicKeyObj
202 {
203 public:
204 	// Constructor
205 	P11DHPublicKeyObj();
206 
207 	// Add attributes
208 	virtual bool init(OSObject *inobject);
209 
210 protected:
211 	bool initialized;
212 };
213 
214 class P11GOSTPublicKeyObj : public P11PublicKeyObj
215 {
216 public:
217 	// Constructor
218 	P11GOSTPublicKeyObj();
219 
220 	// Add attributes
221 	virtual bool init(OSObject *inobject);
222 
223 protected:
224 	bool initialized;
225 };
226 
227 class P11PrivateKeyObj : public P11KeyObj
228 {
229 protected:
230 	// Constructor
231 	P11PrivateKeyObj();
232 
233 	// Add attributes
234 	virtual bool init(OSObject *inobject);
235 	bool initialized;
236 };
237 
238 class P11RSAPrivateKeyObj : public P11PrivateKeyObj
239 {
240 public:
241 	// Constructor
242 	P11RSAPrivateKeyObj();
243 
244 	// Add attributes
245 	virtual bool init(OSObject *inobject);
246 
247 protected:
248 	bool initialized;
249 };
250 
251 class P11DSAPrivateKeyObj : public P11PrivateKeyObj
252 {
253 public:
254 	// Constructor
255 	P11DSAPrivateKeyObj();
256 
257 	// Add attributes
258 	virtual bool init(OSObject *inobject);
259 
260 protected:
261 	bool initialized;
262 };
263 
264 class P11ECPrivateKeyObj : public P11PrivateKeyObj
265 {
266 public:
267 	// Constructor
268 	P11ECPrivateKeyObj();
269 
270 	// Add attributes
271 	virtual bool init(OSObject *inobject);
272 
273 protected:
274 	bool initialized;
275 };
276 
277 class P11EDPrivateKeyObj : public P11PrivateKeyObj
278 {
279 public:
280 	// Constructor
281 	P11EDPrivateKeyObj();
282 
283 	// Add attributes
284 	virtual bool init(OSObject *inobject);
285 
286 protected:
287 	bool initialized;
288 };
289 
290 class P11DHPrivateKeyObj : public P11PrivateKeyObj
291 {
292 public:
293 	// Constructor
294 	P11DHPrivateKeyObj();
295 
296 	// Add attributes
297 	virtual bool init(OSObject *inobject);
298 
299 protected:
300 	bool initialized;
301 };
302 
303 class P11GOSTPrivateKeyObj : public P11PrivateKeyObj
304 {
305 public:
306 	// Constructor
307 	P11GOSTPrivateKeyObj();
308 
309 	// Add attributes
310 	virtual bool init(OSObject *inobject);
311 
312 protected:
313 	bool initialized;
314 };
315 
316 class P11SecretKeyObj : public P11KeyObj
317 {
318 protected:
319 	// Constructor
320 	P11SecretKeyObj();
321 
322 	// Add attributes
323 	virtual bool init(OSObject *inobject);
324 	bool initialized;
325 };
326 
327 class P11GenericSecretKeyObj : public P11SecretKeyObj
328 {
329 public:
330 	// Constructor
331 	P11GenericSecretKeyObj();
332 
333 	// Add attributes
334 	virtual bool init(OSObject *inobject);
335 
336 	// Better than multiply subclasses
337 	virtual bool setKeyType(CK_KEY_TYPE inKeytype);
338 	virtual CK_KEY_TYPE getKeyType();
339 
340 protected:
341 	bool initialized;
342 	CK_KEY_TYPE keytype;
343 };
344 
345 class P11AESSecretKeyObj : public P11SecretKeyObj
346 {
347 public:
348 	// Constructor
349 	P11AESSecretKeyObj();
350 
351 	// Add attributes
352 	virtual bool init(OSObject *inobject);
353 
354 protected:
355 	bool initialized;
356 };
357 
358 class P11DESSecretKeyObj : public P11SecretKeyObj
359 {
360 public:
361 	// Constructor
362 	P11DESSecretKeyObj();
363 
364 	// Add attributes
365 	virtual bool init(OSObject *inobject);
366 
367 	// Better than multiply subclasses
368 	virtual bool setKeyType(CK_KEY_TYPE inKeytype);
369 	virtual CK_KEY_TYPE getKeyType();
370 
371 protected:
372 	bool initialized;
373 	CK_KEY_TYPE keytype;
374 };
375 
376 class P11GOSTSecretKeyObj : public P11SecretKeyObj
377 {
378 public:
379 	// Constructor
380 	P11GOSTSecretKeyObj();
381 
382 	// Add attributes
383 	virtual bool init(OSObject *inobject);
384 
385 protected:
386 	bool initialized;
387 };
388 
389 class P11DomainObj : public P11Object
390 {
391 protected:
392 	// Constructor
393 	P11DomainObj();
394 
395 	// Add attributes
396 	virtual bool init(OSObject *inobject);
397 	bool initialized;
398 };
399 
400 class P11DSADomainObj : public P11DomainObj
401 {
402 public:
403 	// Constructor
404 	P11DSADomainObj();
405 
406 	// Add attributes
407 	virtual bool init(OSObject *inobject);
408 protected:
409 	bool initialized;
410 };
411 
412 class P11DHDomainObj : public P11DomainObj
413 {
414 public:
415 	// Constructor
416 	P11DHDomainObj();
417 
418 	// Add attributes
419 	virtual bool init(OSObject *inobject);
420 protected:
421 	bool initialized;
422 };
423 
424 #endif // !_SOFTHSM_V2_P11OBJECTS_H
425