Debian Codegraph @

src/pobject.c

raw
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 * Rob Crittenden ([email protected])
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
38 #include "ckpem.h"
39
40 /*
41 * pobject.c
42 *
43 * This file implements the NSSCKMDObject object for the
44 * "PEM objects" cryptoki module.
45 */
46
47 #include <blapi.h>
48 #include <certt.h>
49 #include <nssckfwc.h>
50 #include <pk11pub.h>
51 #include <secasn1.h>
52
53 #ifndef HAVE_GETSLOTID_FN
54 # define NSSCKFWInstance_FindSessionHandle nssCKFWInstance_FindSessionHandle
55 # define NSSCKFWInstance_DestroySessionHandle nssCKFWInstance_DestroySessionHandle
56 CK_SESSION_HANDLE nssCKFWInstance_FindSessionHandle(
57 NSSCKFWInstance *fwInstance,
58 NSSCKFWSession *fwSession);
59
60 void nssCKFWInstance_DestroySessionHandle(
61 NSSCKFWInstance *fwInstance,
62 CK_SESSION_HANDLE hSession);
63 #endif
64
65 #define APPEND_LIST_ITEM(item) do { \
66 item->next = NSS_ZNEW(NULL, pemObjectListItem); \
67 if (NULL == item->next) \
68 goto loser; \
69 item = item->next; \
70 } while (0)
71
72 const CK_ATTRIBUTE_TYPE certAttrs[] = {
73 CKA_CLASS,
74 CKA_TOKEN,
75 CKA_PRIVATE,
76 CKA_MODIFIABLE,
77 CKA_LABEL,
78 CKA_CERTIFICATE_TYPE,
79 CKA_SUBJECT,
80 CKA_ISSUER,
81 CKA_SERIAL_NUMBER,
82 CKA_VALUE
83 };
84 const PRUint32 certAttrsCount = NSS_PEM_ARRAY_SIZE(certAttrs);
85
86 /* private keys, for now only support RSA */
87 const CK_ATTRIBUTE_TYPE privKeyAttrs[] = {
88 CKA_CLASS,
89 CKA_TOKEN,
90 CKA_PRIVATE,
91 CKA_MODIFIABLE,
92 CKA_LABEL,
93 CKA_KEY_TYPE,
94 CKA_DERIVE,
95 CKA_LOCAL,
96 CKA_SUBJECT,
97 CKA_SENSITIVE,
98 CKA_DECRYPT,
99 CKA_SIGN,
100 CKA_SIGN_RECOVER,
101 CKA_UNWRAP,
102 CKA_EXTRACTABLE,
103 CKA_ALWAYS_SENSITIVE,
104 CKA_NEVER_EXTRACTABLE,
105 CKA_MODULUS,
106 CKA_PUBLIC_EXPONENT,
107 };
108 const PRUint32 privKeyAttrsCount = NSS_PEM_ARRAY_SIZE(privKeyAttrs);
109
110 /* public keys, for now only support RSA */
111 const CK_ATTRIBUTE_TYPE pubKeyAttrs[] = {
112 CKA_CLASS,
113 CKA_TOKEN,
114 CKA_PRIVATE,
115 CKA_MODIFIABLE,
116 CKA_LABEL,
117 CKA_KEY_TYPE,
118 CKA_DERIVE,
119 CKA_LOCAL,
120 CKA_SUBJECT,
121 CKA_ENCRYPT,
122 CKA_VERIFY,
123 CKA_VERIFY_RECOVER,
124 CKA_WRAP,
125 CKA_MODULUS,
126 CKA_PUBLIC_EXPONENT,
127 };
128 const PRUint32 pubKeyAttrsCount = NSS_PEM_ARRAY_SIZE(pubKeyAttrs);
129
130 /* Trust */
131 const CK_ATTRIBUTE_TYPE trustAttrs[] = {
132 CKA_CLASS,
133 CKA_TOKEN,
134 CKA_LABEL,
135 CKA_CERT_SHA1_HASH,
136 CKA_CERT_MD5_HASH,
137 CKA_ISSUER,
138 CKA_SUBJECT,
139 CKA_TRUST_SERVER_AUTH,
140 CKA_TRUST_CLIENT_AUTH,
141 CKA_TRUST_EMAIL_PROTECTION,
142 CKA_TRUST_CODE_SIGNING
143 };
144 const PRUint32 trustAttrsCount = NSS_PEM_ARRAY_SIZE(trustAttrs);
145
146 static const CK_BBOOL ck_true = CK_TRUE;
147 static const CK_BBOOL ck_false = CK_FALSE;
148 static const CK_CERTIFICATE_TYPE ckc_x509 = CKC_X_509;
149 static const CK_KEY_TYPE ckk_rsa = CKK_RSA;
150 static const CK_OBJECT_CLASS cko_certificate = CKO_CERTIFICATE;
151 static const CK_OBJECT_CLASS cko_private_key = CKO_PRIVATE_KEY;
152 static const CK_OBJECT_CLASS cko_public_key = CKO_PUBLIC_KEY;
153 static const CK_OBJECT_CLASS cko_trust = CKO_NSS_TRUST;
154 static const CK_TRUST ckt_netscape_trusted = CKT_NSS_TRUSTED_DELEGATOR;
155 static const NSSItem pem_trueItem = {
156 (void *) &ck_true, (PRUint32) sizeof(CK_BBOOL)
157 };
158 static const NSSItem pem_falseItem = {
159 (void *) &ck_false, (PRUint32) sizeof(CK_BBOOL)
160 };
161 static const NSSItem pem_x509Item = {
162 (void *) &ckc_x509, (PRUint32) sizeof(CK_CERTIFICATE_TYPE)
163 };
164 static const NSSItem pem_rsaItem = {
165 (void *) &ckk_rsa, (PRUint32) sizeof(CK_KEY_TYPE)
166 };
167 static const NSSItem pem_certClassItem = {
168 (void *) &cko_certificate, (PRUint32) sizeof(CK_OBJECT_CLASS)
169 };
170 static const NSSItem pem_privKeyClassItem = {
171 (void *) &cko_private_key, (PRUint32) sizeof(CK_OBJECT_CLASS)
172 };
173 static const NSSItem pem_pubKeyClassItem = {
174 (void *) &cko_public_key, (PRUint32) sizeof(CK_OBJECT_CLASS)
175 };
176 static const NSSItem pem_trustClassItem = {
177 (void *) &cko_trust, (PRUint32) sizeof(CK_OBJECT_CLASS)
178 };
179 static const NSSItem pem_emptyItem = {
180 (void *) &ck_true, 0
181 };
182 static const NSSItem pem_trusted = {
183 (void *) &ckt_netscape_trusted, (PRUint32) sizeof(CK_TRUST)
184 };
185
186 /* SEC_SkipTemplate is already defined and exported by libnssutil */
187 #ifdef SEC_SKIP_TEMPLATE
188 /*
189 * Template for skipping a subitem.
190 *
191 * Note that it only makes sense to use this for decoding (when you want
192 * to decode something where you are only interested in one or two of
193 * the fields); you cannot encode a SKIP!
194 */
195 const SEC_ASN1Template SEC_SkipTemplate[] = {
196 {SEC_ASN1_SKIP}
197 };
198 #endif
199
200 /*
201 * Find the subjectName in a DER encoded certificate
202 */
203 const SEC_ASN1Template SEC_CertSubjectTemplate[] = {
204 {SEC_ASN1_SEQUENCE,
205 0, NULL, sizeof(SECItem)} ,
206 {SEC_ASN1_EXPLICIT | SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED |
207 SEC_ASN1_CONTEXT_SPECIFIC | 0,
208 0, SEC_SkipTemplate} , /* version */
209 {SEC_ASN1_SKIP}, /* serial number */
210 {SEC_ASN1_SKIP}, /* signature algorithm */
211 {SEC_ASN1_SKIP}, /* issuer */
212 {SEC_ASN1_SKIP}, /* validity */
213 {SEC_ASN1_ANY, 0, NULL}, /* subject */
214 {SEC_ASN1_SKIP_REST},
215 {0}
216 };
217
218 void
219 pem_FetchLabel
220 (
221 pemInternalObject * io
222 )
223 {
224 pemCertObject *co = &io->u.cert;
225
226 co->label.data = io->nickname;
227 co->label.size = strlen(io->nickname);
228 }
229
230 const NSSItem
231 *pem_FetchCertAttribute
232 (
233 pemInternalObject * io,
234 CK_ATTRIBUTE_TYPE type
235 )
236 {
237 switch (type) {
238 case CKA_CLASS:
239 plog(" fetch cert CKA_CLASS\n");
240 return &pem_certClassItem;
241 case CKA_TOKEN:
242 plog(" fetch cert CKA_TOKEN\n");
243 return &pem_trueItem;
244 case CKA_PRIVATE:
245 return &pem_falseItem;
246 case CKA_CERTIFICATE_TYPE:
247 plog(" fetch cert CKA_CERTIFICATE_TYPE\n");
248 return &pem_x509Item;
249 case CKA_LABEL:
250 if (0 == io->u.cert.label.size) {
251 pem_FetchLabel(io);
252 }
253 plog(" fetch cert CKA_LABEL %s\n", io->u.cert.label.data);
254 return &io->u.cert.label;
255 case CKA_SUBJECT:
256 plog(" fetch cert CKA_SUBJECT size %d\n", io->u.cert.subject.size);
257 return &io->u.cert.subject;
258 case CKA_ISSUER:
259 plog(" fetch cert CKA_ISSUER size %d\n", io->u.cert.issuer.size);
260 return &io->u.cert.issuer;
261 case CKA_SERIAL_NUMBER:
262 plog(" fetch cert CKA_SERIAL_NUMBER size %d value %08x\n", io->u.cert.serial.size, io->u.cert.serial.data);
263 return &io->u.cert.serial;
264 case CKA_VALUE:
265 if (0 == io->u.cert.derCert.size) {
266 io->u.cert.derCert.data = io->derCert->data;
267 io->u.cert.derCert.size = io->derCert->len;
268 }
269 plog(" fetch cert CKA_VALUE\n");
270 return &io->u.cert.derCert;
271 case CKA_ID:
272 plog(" fetch cert CKA_ID val=%s size=%d\n", (char *) io->id.data,
273 io->id.size);
274 return &io->id;
275 case CKA_TRUSTED:
276 plog(" fetch cert CKA_TRUSTED: returning NULL\n");
277 return NULL;
278 default:
279 plog(" fetching cert unknown type %d\n", type);
280 break;
281 }
282 return NULL;
283 }
284
285 const NSSItem *
286 pem_FetchPrivKeyAttribute
287 (
288 pemInternalObject * io,
289 CK_ATTRIBUTE_TYPE type,
290 CK_RV * pError
291 )
292 {
293 PRBool isCertType = (pemCert == io->type);
294 pemKeyParams *kp = isCertType ? &io->u.cert.key : &io->u.key.key;
295
296 switch (type) {
297 case CKA_CLASS:
298 return &pem_privKeyClassItem;
299 case CKA_TOKEN:
300 case CKA_LOCAL:
301 case CKA_SIGN:
302 case CKA_DECRYPT:
303 case CKA_SIGN_RECOVER:
304 return &pem_trueItem;
305 case CKA_SENSITIVE:
306 case CKA_PRIVATE: /* should move in the future */
307 case CKA_MODIFIABLE:
308 case CKA_DERIVE:
309 case CKA_UNWRAP:
310 case CKA_EXTRACTABLE: /* will probably move in the future */
311 case CKA_ALWAYS_SENSITIVE:
312 case CKA_NEVER_EXTRACTABLE:
313 return &pem_falseItem;
314 case CKA_KEY_TYPE:
315 return &pem_rsaItem;
316 case CKA_LABEL:
317 if (!isCertType) {
318 return &pem_emptyItem;
319 }
320 if (0 == io->u.cert.label.size) {
321 pem_FetchLabel(io);
322 }
323 plog(" fetch key CKA_LABEL %s\n", io->u.cert.label.data);
324 return &io->u.cert.label;
325 case CKA_SUBJECT:
326 if (!isCertType) {
327 return &pem_emptyItem;
328 }
329 plog(" fetch key CKA_SUBJECT %s\n", io->u.cert.label.data);
330 return &io->u.cert.subject;
331 case CKA_MODULUS:
332 if (0 == kp->modulus.size) {
333 *pError = pem_PopulateModulusExponent(io);
334 if (CKR_OK != *pError) {
335 return NULL;
336 }
337 }
338 plog(" fetch key CKA_MODULUS\n");
339 return &kp->modulus;
340 case CKA_PUBLIC_EXPONENT:
341 if (0 == kp->modulus.size) {
342 *pError = pem_PopulateModulusExponent(io);
343 if (CKR_OK != *pError) {
344 return NULL;
345 }
346 }
347 plog(" fetch key CKA_PUBLIC_EXPONENT\n");
348 return &kp->exponent;
349 case CKA_PRIVATE_EXPONENT:
350 if (0 == kp->privateExponent.size) {
351 *pError = pem_PopulateModulusExponent(io);
352 if (CKR_OK != *pError) {
353 return NULL;
354 }
355 }
356 plog(" fetch key CKA_PRIVATE_EXPONENT\n");
357 return &kp->privateExponent;
358 case CKA_PRIME_1:
359 if (0 == kp->prime1.size) {
360 *pError = pem_PopulateModulusExponent(io);
361 if (CKR_OK != *pError) {
362 return NULL;
363 }
364 }
365 plog(" fetch key CKA_PRIME_1\n");
366 return &kp->prime1;
367 case CKA_PRIME_2:
368 if (0 == kp->prime2.size) {
369 *pError = pem_PopulateModulusExponent(io);
370 if (CKR_OK != *pError) {
371 return NULL;
372 }
373 }
374 plog(" fetch key CKA_PRIME_2\n");
375 return &kp->prime2;
376 case CKA_EXPONENT_1:
377 if (0 == kp->exponent1.size) {
378 *pError = pem_PopulateModulusExponent(io);
379 if (CKR_OK != *pError) {
380 return NULL;
381 }
382 }
383 plog(" fetch key CKA_EXPONENT_1\n");
384 return &kp->exponent1;
385 case CKA_EXPONENT_2:
386 if (0 == kp->exponent2.size) {
387 *pError = pem_PopulateModulusExponent(io);
388 if (CKR_OK != *pError) {
389 return NULL;
390 }
391 }
392 plog(" fetch key CKA_EXPONENT_2\n");
393 return &kp->exponent2;
394 case CKA_COEFFICIENT:
395 if (0 == kp->coefficient.size) {
396 *pError = pem_PopulateModulusExponent(io);
397 if (CKR_OK != *pError) {
398 return NULL;
399 }
400 }
401 plog(" fetch key CKA_COEFFICIENT_2\n");
402 return &kp->coefficient;
403 case CKA_ID:
404 plog(" fetch key CKA_ID val=%s size=%d\n", (char *) io->id.data,
405 io->id.size);
406 return &io->id;
407 default:
408 return NULL;
409 }
410 }
411
412 const NSSItem *
413 pem_FetchPubKeyAttribute
414 (
415 pemInternalObject * io,
416 CK_ATTRIBUTE_TYPE type
417 )
418 {
419 PRBool isCertType = (pemCert == io->type);
420 pemKeyParams *kp = isCertType ? &io->u.cert.key : &io->u.key.key;
421
422 switch (type) {
423 case CKA_CLASS:
424 return &pem_pubKeyClassItem;
425 case CKA_TOKEN:
426 case CKA_LOCAL:
427 case CKA_ENCRYPT:
428 case CKA_VERIFY:
429 case CKA_VERIFY_RECOVER:
430 return &pem_trueItem;
431 case CKA_PRIVATE:
432 case CKA_MODIFIABLE:
433 case CKA_DERIVE:
434 case CKA_WRAP:
435 return &pem_falseItem;
436 case CKA_KEY_TYPE:
437 return &pem_rsaItem;
438 case CKA_LABEL:
439 if (!isCertType) {
440 return &pem_emptyItem;
441 }
442 if (0 == io->u.cert.label.size) {
443 pem_FetchLabel(io);
444 }
445 return &io->u.cert.label;
446 case CKA_SUBJECT:
447 if (!isCertType) {
448 return &pem_emptyItem;
449 }
450 return &io->u.cert.subject;
451 case CKA_MODULUS:
452 if (0 == kp->modulus.size) {
453 pem_PopulateModulusExponent(io);
454 }
455 return &kp->modulus;
456 case CKA_PUBLIC_EXPONENT:
457 if (0 == kp->modulus.size) {
458 pem_PopulateModulusExponent(io);
459 }
460 return &kp->exponent;
461 case CKA_ID:
462 return &io->id;
463 default:
464 break;
465 }
466 return NULL;
467 }
468
469 const NSSItem *
470 pem_FetchTrustAttribute
471 (
472 pemInternalObject * io,
473 CK_ATTRIBUTE_TYPE type
474 )
475 {
476 static NSSItem hash;
477 SECStatus rv;
478
479 switch (type) {
480 case CKA_CLASS:
481 return &pem_trustClassItem;
482 case CKA_TOKEN:
483 return &pem_trueItem;
484 case CKA_PRIVATE:
485 return &pem_falseItem;
486 case CKA_CERTIFICATE_TYPE:
487 return &pem_x509Item;
488 case CKA_LABEL:
489 if (0 == io->u.cert.label.size) {
490 pem_FetchLabel(io);
491 }
492 plog(" fetch trust CKA_LABEL %s\n", io->u.cert.label.data);
493 return &io->u.cert.label;
494 case CKA_SUBJECT:
495 plog(" fetch trust CKA_SUBJECT\n");
496 return NULL;
497 case CKA_ISSUER:
498 plog(" fetch trust CKA_ISSUER\n");
499 return &io->u.cert.issuer;
500 case CKA_SERIAL_NUMBER:
501 plog(" fetch trust CKA_SERIAL_NUMBER size %d value %08x\n", io->u.cert.serial.size, io->u.cert.serial.data);
502 return &io->u.cert.serial;
503 case CKA_VALUE:
504 return &pem_trueItem;
505 case CKA_ID:
506 plog(" fetch trust CKA_ID val=%s size=%d\n", (char *) io->id.data,
507 io->id.size);
508 return &io->id;
509 case CKA_TRUSTED:
510 return &pem_trusted;
511 case CKA_TRUST_SERVER_AUTH:
512 return &pem_trusted;
513 case CKA_TRUST_CLIENT_AUTH:
514 return &pem_trusted;
515 case CKA_TRUST_CODE_SIGNING:
516 return &pem_trusted;
517 case CKA_TRUST_EMAIL_PROTECTION:
518 return &pem_trusted;
519 case CKA_TRUST_IPSEC_END_SYSTEM:
520 return &pem_trusted;
521 case CKA_TRUST_IPSEC_TUNNEL:
522 return &pem_trusted;
523 case CKA_TRUST_IPSEC_USER:
524 return &pem_trusted;
525 case CKA_TRUST_TIME_STAMPING:
526 return &pem_trusted;
527 case CKA_TRUST_STEP_UP_APPROVED:
528 return &pem_falseItem;
529 case CKA_CERT_SHA1_HASH:
530 hash.size = 0;
531 hash.data = NULL;
532 memset(io->u.cert.sha1_hash, 0, SHA1_LENGTH);
533 rv = SHA1_HashBuf(io->u.cert.sha1_hash, io->derCert->data,
534 io->derCert->len);
535 if (rv == SECSuccess) {
536 hash.data = io->u.cert.sha1_hash;
537 hash.size = sizeof(io->u.cert.sha1_hash);
538 }
539 return &hash;
540 case CKA_CERT_MD5_HASH:
541 hash.size = 0;
542 hash.data = NULL;
543 memset(io->u.cert.sha1_hash, 0, MD5_LENGTH);
544 rv = MD5_HashBuf(io->u.cert.sha1_hash, io->derCert->data,
545 io->derCert->len);
546 if (rv == SECSuccess) {
547 hash.data = io->u.cert.sha1_hash;
548 hash.size = sizeof(io->u.cert.sha1_hash);
549 }
550 return &hash;
551 default:
552 return &pem_trusted;
553 break;
554 }
555 return NULL;
556 }
557
558 const NSSItem *
559 pem_FetchAttribute
560 (
561 pemInternalObject * io,
562 CK_ATTRIBUTE_TYPE type,
563 CK_RV * pError
564 )
565 {
566 CK_ULONG i;
567
568 if (io->type == pemRaw) {
569 for (i = 0; i < io->u.raw.n; i++) {
570 if (type == io->u.raw.types[i]) {
571 return &io->u.raw.items[i];
572 }
573 }
574 return NULL;
575 }
576 /* deal with the common attributes */
577 switch (io->objClass) {
578 case CKO_CERTIFICATE:
579 return pem_FetchCertAttribute(io, type);
580 case CKO_PRIVATE_KEY:
581 return pem_FetchPrivKeyAttribute(io, type, pError);
582 case CKO_NSS_TRUST:
583 return pem_FetchTrustAttribute(io, type);
584 case CKO_PUBLIC_KEY:
585 return pem_FetchPubKeyAttribute(io, type);
586 }
587 return NULL;
588 }
589
590 /*
591 * Destroy internal object or list object if refCount becomes zero (after
592 * decrement). Safe to call with NULL argument.
593 */
594 void
595 pem_DestroyInternalObject
596 (
597 pemInternalObject * io
598 )
599 {
600 if (NULL == io)
601 /* nothing to destroy */
602 return;
603
604 if (NULL != io->list) {
605 /* destroy list object */
606 pemObjectListItem *item = io->list;
607 while (item) {
608 pemObjectListItem *next = item->next;
609
610 /* recursion of maximal depth 1 */
611 pem_DestroyInternalObject(item->io);
612
613 NSS_ZFreeIf(item);
614 item = next;
615 }
616 NSS_ZFreeIf(io);
617 return;
618 }
619
620 io->refCount --;
621 if (0 < io->refCount)
622 return;
623
624 /* destroy internal object */
625 switch (io->type) {
626 case pemRaw:
627 return;
628 case pemCert:
629 NSS_ZFreeIf(io->u.cert.key.privateKey);
630 NSS_ZFreeIf(io->u.cert.key.pubKey);
631 /* go through */
632 case pemTrust:
633 NSS_ZFreeIf(io->id.data);
634 NSS_ZFreeIf(io->nickname);
635 NSS_ZFreeIf(io->derCert->data);
636 NSS_ZFreeIf(io->derCert);
637 if (io->u.cert.subject.size > 0) {
638 NSS_ZFreeIf(io->u.cert.subject.data);
639 }
640 if (io->u.cert.issuer.size > 0) {
641 NSS_ZFreeIf(io->u.cert.issuer.data);
642 }
643 if (io->u.cert.serial.size > 0) {
644 NSS_ZFreeIf(io->u.cert.serial.data);
645 }
646 break;
647 case pemBareKey:
648 SECITEM_FreeItem(io->u.key.key.privateKeyOrig, PR_TRUE);
649 NSS_ZFreeIf(io->u.key.key.coefficient.data);
650 NSS_ZFreeIf(io->u.key.key.exponent2.data);
651 NSS_ZFreeIf(io->u.key.key.exponent1.data);
652 NSS_ZFreeIf(io->u.key.key.prime2.data);
653 NSS_ZFreeIf(io->u.key.key.prime1.data);
654 NSS_ZFreeIf(io->u.key.key.privateExponent.data);
655 NSS_ZFreeIf(io->u.key.key.exponent.data);
656 NSS_ZFreeIf(io->u.key.key.modulus.data);
657 NSS_ZFreeIf(io->u.key.key.privateKey->data);
658 NSS_ZFreeIf(io->u.key.key.privateKey);
659 NSS_ZFreeIf(io->u.key.key.pubKey);
660 NSS_ZFreeIf(io->id.data);
661 NSS_ZFreeIf(io->nickname);
662 NSS_ZFreeIf(io->derCert->data);
663 NSS_ZFreeIf(io->derCert);
664
665 /* PORT_Strdup'd in ReadDERFromFile */
666 if (io->u.key.ivstring)
667 PORT_Free(io->u.key.ivstring);
668 break;
669 case pemAll:
670 /* pemAll is not used, keep the compiler happy
671 * TODO: investigate a proper solution
672 */
673 return;
674 }
675
676 /* remove self from the global list */
677 list_del(&io->gl_list);
678 NSS_ZFreeIf(io);
679 return;
680 }
681
682 /*
683 * Finalize - needed
684 * Destroy - CKR_SESSION_READ_ONLY
685 * IsTokenObject - CK_TRUE
686 * GetAttributeCount
687 * GetAttributeTypes
688 * GetAttributeSize
689 * GetAttribute
690 * SetAttribute - unneeded
691 * GetObjectSize - unneeded
692 */
693
694 static void
695 pem_mdObject_Finalize
696 (
697 NSSCKMDObject * mdObject,
698 NSSCKFWObject * fwObject,
699 NSSCKMDSession * mdSession,
700 NSSCKFWSession * fwSession,
701 NSSCKMDToken * mdToken,
702 NSSCKFWToken * fwToken,
703 NSSCKMDInstance * mdInstance,
704 NSSCKFWInstance * fwInstance
705 )
706 {
707 pem_DestroyInternalObject((pemInternalObject *) mdObject->etc);
708 }
709
710 static CK_RV
711 pem_mdObject_Destroy
712 (
713 NSSCKMDObject * mdObject,
714 NSSCKFWObject * fwObject,
715 NSSCKMDSession * mdSession,
716 NSSCKFWSession * fwSession,
717 NSSCKMDToken * mdToken,
718 NSSCKFWToken * fwToken,
719 NSSCKMDInstance * mdInstance,
720 NSSCKFWInstance * fwInstance
721 )
722 {
723 pemInternalObject *io = (pemInternalObject *) mdObject->etc;
724
725 pem_DestroyInternalObject(io);
726 return CKR_OK;
727 }
728
729 static CK_BBOOL
730 pem_mdObject_IsTokenObject
731 (
732 NSSCKMDObject * mdObject,
733 NSSCKFWObject * fwObject,
734 NSSCKMDSession * mdSession,
735 NSSCKFWSession * fwSession,
736 NSSCKMDToken * mdToken,
737 NSSCKFWToken * fwToken,
738 NSSCKMDInstance * mdInstance,
739 NSSCKFWInstance * fwInstance
740 )
741 {
742 return CK_TRUE;
743 }
744
745 static CK_ULONG
746 pem_mdObject_GetAttributeCount
747 (
748 NSSCKMDObject * mdObject,
749 NSSCKFWObject * fwObject,
750 NSSCKMDSession * mdSession,
751 NSSCKFWSession * fwSession,
752 NSSCKMDToken * mdToken,
753 NSSCKFWToken * fwToken,
754 NSSCKMDInstance * mdInstance,
755 NSSCKFWInstance * fwInstance,
756 CK_RV * pError
757 )
758 {
759 pemInternalObject *io = (pemInternalObject *) mdObject->etc;
760
761 if (NULL != io->list) {
762 /* list object --> use the first item in the list */
763 NSSCKMDObject *md = &(io->list->io->mdObject);
764 return md->GetAttributeCount(md, fwObject, mdSession, fwSession,
765 mdToken, fwToken, mdInstance, fwInstance,
766 pError);
767 }
768
769 if (pemRaw == io->type) {
770 return io->u.raw.n;
771 }
772 switch (io->objClass) {
773 case CKO_CERTIFICATE:
774 return certAttrsCount;
775 case CKO_PUBLIC_KEY:
776 return pubKeyAttrsCount;
777 case CKO_PRIVATE_KEY:
778 return privKeyAttrsCount;
779 case CKO_NSS_TRUST:
780 return trustAttrsCount;
781 default:
782 break;
783 }
784 return 0;
785 }
786
787 static CK_RV
788 pem_mdObject_GetAttributeTypes
789 (
790 NSSCKMDObject * mdObject,
791 NSSCKFWObject * fwObject,
792 NSSCKMDSession * mdSession,
793 NSSCKFWSession * fwSession,
794 NSSCKMDToken * mdToken,
795 NSSCKFWToken * fwToken,
796 NSSCKMDInstance * mdInstance,
797 NSSCKFWInstance * fwInstance,
798 CK_ATTRIBUTE_TYPE_PTR typeArray,
799 CK_ULONG ulCount
800 )
801 {
802 pemInternalObject *io = (pemInternalObject *) mdObject->etc;
803 CK_ULONG i;
804 CK_RV error = CKR_OK;
805 const CK_ATTRIBUTE_TYPE *attrs = NULL;
806 CK_ULONG size;
807
808 if (NULL != io->list) {
809 /* list object --> use the first item in the list */
810 NSSCKMDObject *md = &(io->list->io->mdObject);
811 return md->GetAttributeTypes(md, fwObject, mdSession, fwSession,
812 mdToken, fwToken, mdInstance, fwInstance,
813 typeArray, ulCount);
814 }
815
816 size = pem_mdObject_GetAttributeCount(mdObject, fwObject, mdSession,
817 fwSession, mdToken, fwToken, mdInstance,
818 fwInstance, &error);
819
820 if (size != ulCount) {
821 return CKR_BUFFER_TOO_SMALL;
822 }
823 if (io->type == pemRaw) {
824 attrs = io->u.raw.types;
825 } else
826 switch (io->objClass) {
827 case CKO_CERTIFICATE:
828 attrs = certAttrs;
829 break;
830 case CKO_PUBLIC_KEY:
831 attrs = pubKeyAttrs;
832 break;
833 case CKO_PRIVATE_KEY:
834 attrs = privKeyAttrs;
835 break;
836 default:
837 return CKR_OK;
838 }
839
840 for (i = 0; i < size; i++) {
841 typeArray[i] = attrs[i];
842 }
843
844 return CKR_OK;
845 }
846
847 static CK_ULONG
848 pem_mdObject_GetAttributeSize
849 (
850 NSSCKMDObject * mdObject,
851 NSSCKFWObject * fwObject,
852 NSSCKMDSession * mdSession,
853 NSSCKFWSession * fwSession,
854 NSSCKMDToken * mdToken,
855 NSSCKFWToken * fwToken,
856 NSSCKMDInstance * mdInstance,
857 NSSCKFWInstance * fwInstance,
858 CK_ATTRIBUTE_TYPE attribute,
859 CK_RV * pError
860 )
861 {
862 pemInternalObject *io = (pemInternalObject *) mdObject->etc;
863 const NSSItem *b;
864
865 if (NULL != io->list) {
866 /* list object --> use the first item in the list */
867 NSSCKMDObject *md = &(io->list->io->mdObject);
868 return md->GetAttributeSize(md, fwObject, mdSession, fwSession,
869 mdToken, fwToken, mdInstance, fwInstance,
870 attribute, pError);
871 }
872
873 b = pem_FetchAttribute(io, attribute, pError);
874 plog("pem_FetchAttribute pError = 0x%08x\n", *pError);
875
876 /* Don't assume that the returned item is NULL on error */
877 if (*pError != CKR_OK) {
878 if ((const NSSItem *) NULL != b) {
879 NSS_ZFreeIf(b->data);
880 }
881 return 0;
882 }
883
884 if ((const NSSItem *) NULL == b) {
885 *pError = CKR_ATTRIBUTE_TYPE_INVALID;
886 return 0;
887 }
888 return b->size;
889 }
890
891 static NSSCKFWItem
892 pem_mdObject_GetAttribute
893 (
894 NSSCKMDObject * mdObject,
895 NSSCKFWObject * fwObject,
896 NSSCKMDSession * mdSession,
897 NSSCKFWSession * fwSession,
898 NSSCKMDToken * mdToken,
899 NSSCKFWToken * fwToken,
900 NSSCKMDInstance * mdInstance,
901 NSSCKFWInstance * fwInstance,
902 CK_ATTRIBUTE_TYPE attribute,
903 CK_RV * pError
904 )
905 {
906 NSSCKFWItem mdItem;
907 pemInternalObject *io = (pemInternalObject *) mdObject->etc;
908
909 if (NULL != io->list) {
910 /* list object --> use the first item in the list */
911 NSSCKMDObject *md = &(io->list->io->mdObject);
912 return md->GetAttribute(md, fwObject, mdSession, fwSession,
913 mdToken, fwToken, mdInstance, fwInstance,
914 attribute, pError);
915 }
916
917 mdItem.needsFreeing = PR_FALSE;
918 mdItem.item = (NSSItem *) pem_FetchAttribute(io, attribute, pError);
919
920 if ((NSSItem *) NULL == mdItem.item && !*pError) {
921 *pError = CKR_ATTRIBUTE_TYPE_INVALID;
922 }
923
924 return mdItem;
925 }
926
927 /*
928 * get an attribute from a template. Value is returned in NSS item.
929 * data for the item is owned by the template.
930 */
931 CK_RV
932 pem_GetAttribute
933 (
934 CK_ATTRIBUTE_TYPE type,
935 CK_ATTRIBUTE * template,
936 CK_ULONG templateSize,
937 NSSItem * item
938 )
939 {
940 CK_ULONG i;
941
942 for (i = 0; i < templateSize; i++) {
943 if (template[i].type == type) {
944 item->data = template[i].pValue;
945 item->size = template[i].ulValueLen;
946 return CKR_OK;
947 }
948 }
949 return CKR_TEMPLATE_INCOMPLETE;
950 }
951
952 /*
953 * get an attribute which is type CK_ULONG.
954 */
955 CK_ULONG
956 pem_GetULongAttribute
957 (
958 CK_ATTRIBUTE_TYPE type,
959 CK_ATTRIBUTE * template,
960 CK_ULONG templateSize,
961 CK_RV * pError
962 )
963 {
964 NSSItem item;
965
966 *pError = pem_GetAttribute(type, template, templateSize, &item);
967 if (CKR_OK != *pError) {
968 return (CK_ULONG) 0;
969 }
970 if (item.size != sizeof(CK_ULONG)) {
971 *pError = CKR_ATTRIBUTE_VALUE_INVALID;
972 return (CK_ULONG) 0;
973 }
974 return *(CK_ULONG *) item.data;
975 }
976
977 /*
978 * get an attribute which is type CK_BBOOL.
979 */
980 CK_BBOOL
981 pem_GetBoolAttribute
982 (
983 CK_ATTRIBUTE_TYPE type,
984 CK_ATTRIBUTE * template,
985 CK_ULONG templateSize,
986 CK_RV * pError
987 )
988 {
989 NSSItem item;
990
991 *pError = pem_GetAttribute(type, template, templateSize, &item);
992 if (CKR_OK != *pError) {
993 return (CK_BBOOL) 0;
994 }
995 if (item.size != sizeof(CK_BBOOL)) {
996 *pError = CKR_ATTRIBUTE_VALUE_INVALID;
997 return (CK_BBOOL) 0;
998 }
999 return *(CK_BBOOL *) item.data;
1000 }
1001
1002 /*
1003 * Get a string attribute. Caller needs to free this.
1004 */
1005 char *
1006 pem_GetStringAttribute
1007 (
1008 CK_ATTRIBUTE_TYPE type,
1009 CK_ATTRIBUTE * template,
1010 CK_ULONG templateSize,
1011 CK_RV * pError
1012 )
1013 {
1014 NSSItem item;
1015 char *str;
1016
1017 /* get the attribute */
1018 *pError = pem_GetAttribute(type, template, templateSize, &item);
1019 if (CKR_OK != *pError) {
1020 return (char *) NULL;
1021 }
1022 /* make sure it is null terminated */
1023 str = NSS_ZNEWARRAY(NULL, char, item.size + 1);
1024 if ((char *) NULL == str) {
1025 *pError = CKR_HOST_MEMORY;
1026 return (char *) NULL;
1027 }
1028
1029 memcpy(str, item.data, item.size);
1030 str[item.size] = 0;
1031
1032 return str;
1033 }
1034
1035 static const NSSCKMDObject
1036 pem_prototype_mdObject = {
1037 (void *) NULL, /* etc */
1038 pem_mdObject_Finalize,
1039 pem_mdObject_Destroy,
1040 pem_mdObject_IsTokenObject,
1041 pem_mdObject_GetAttributeCount,
1042 pem_mdObject_GetAttributeTypes,
1043 pem_mdObject_GetAttributeSize,
1044 pem_mdObject_GetAttribute,
1045 NULL, /* FreeAttribute */
1046 NULL, /* SetAttribute */
1047 NULL, /* GetObjectSize */
1048 (void *) NULL /* null terminator */
1049 };
1050
1051 NSS_IMPLEMENT NSSCKMDObject *
1052 pem_CreateMDObject
1053 (
1054 NSSArena * arena,
1055 pemInternalObject * io,
1056 CK_RV * pError
1057 )
1058 {
1059 if ((void *) NULL == io->mdObject.etc) {
1060 memcpy(&io->mdObject, &pem_prototype_mdObject, sizeof io->mdObject);
1061 io->mdObject.etc = (void *) io;
1062 }
1063
1064 return &io->mdObject;
1065 }
1066
1067 /*
1068 * Each object has an identifier. For a certificate and key pair this id
1069 * needs to be the same so we use the right combination. If the target object
1070 * is a key we first look to see if its certificate was already added and if
1071 * so, use that id. The same thing is done when a key is added.
1072 */
1073 NSS_EXTERN NSSCKMDObject *
1074 pem_CreateObject
1075 (
1076 NSSCKFWInstance * fwInstance,
1077 NSSCKFWSession * fwSession,
1078 NSSCKMDToken * mdToken,
1079 CK_ATTRIBUTE_PTR pTemplate,
1080 CK_ULONG ulAttributeCount,
1081 CK_RV * pError
1082 )
1083 {
1084 CK_OBJECT_CLASS objClass;
1085 NSSCKFWSlot *fwSlot;
1086 CK_SLOT_ID slotID;
1087 CK_BBOOL cacert;
1088 char *filename;
1089 SECItem **derlist = NULL;
1090 int nobjs = 0;
1091 int i;
1092 long objid;
1093 int cipher = 0;
1094 char *ivstring = NULL;
1095 pemInternalObject *listObj = NULL;
1096 pemObjectListItem *listItem = NULL;
1097
1098 /* What slot are we adding the object to? */
1099 fwSlot = NSSCKFWSession_GetFWSlot(fwSession);
1100 if ((NSSCKFWSlot *) NULL == fwSlot) {
1101 *pError = CKR_GENERAL_ERROR;
1102 return (NSSCKMDObject *) NULL;
1103
1104 }
1105 slotID = NSSCKFWSlot_GetSlotID(fwSlot);
1106
1107 /*
1108 * only create keys and certs.
1109 */
1110 objClass = pem_GetULongAttribute(CKA_CLASS, pTemplate,
1111 ulAttributeCount, pError);
1112 if (CKR_OK != *pError) {
1113 return (NSSCKMDObject *) NULL;
1114 }
1115
1116 cacert = pem_GetBoolAttribute(CKA_TRUST, pTemplate,
1117 ulAttributeCount, pError);
1118
1119 filename = pem_GetStringAttribute(CKA_LABEL, pTemplate,
1120 ulAttributeCount, pError);
1121 if (CKR_OK != *pError) {
1122 return (NSSCKMDObject *) NULL;
1123 }
1124
1125 #ifdef notdef
1126 if (objClass == CKO_PUBLIC_KEY) {
1127 return CKR_OK; /* fake public key creation, happens as a side effect of
1128 * private key creation */
1129 }
1130 #endif
1131
1132 listObj = NSS_ZNEW(NULL, pemInternalObject);
1133 if (NULL == listObj) {
1134 NSS_ZFreeIf(filename);
1135 return NULL;
1136 }
1137
1138 listItem = listObj->list = NSS_ZNEW(NULL, pemObjectListItem);
1139 if (NULL == listItem) {
1140 NSS_ZFreeIf(listObj);
1141 NSS_ZFreeIf(filename);
1142 return NULL;
1143 }
1144
1145 if (objClass == CKO_CERTIFICATE) {
1146 nobjs = ReadDERFromFile(&derlist, filename, &cipher, &ivstring,
1147 /* certs only */ PR_TRUE);
1148 if (nobjs < 1)
1149 goto loser;
1150
1151 /* We're just adding a cert, we'll assume the key is next */
1152 objid = pem_nobjs + 1;
1153
1154 if (cacert) {
1155 /* Add the certificate. There may be more than one */
1156 int c;
1157 for (c = 0; c < nobjs; c++) {
1158 char nickname[1024];
1159 objid = pem_nobjs + 1;
1160
1161 snprintf(nickname, sizeof nickname, "%s - %d", filename, c);
1162
1163 if (c) {
1164 APPEND_LIST_ITEM(listItem);
1165 }
1166 listItem->io = AddObjectIfNeeded(CKO_CERTIFICATE, pemCert,
1167 derlist[c], NULL, nickname, 0,
1168 slotID, NULL);
1169 if (listItem->io != NULL) {
1170 /* Add the trust object */
1171 APPEND_LIST_ITEM(listItem);
1172 listItem->io = AddObjectIfNeeded(CKO_NSS_TRUST, pemTrust,
1173 derlist[c], NULL, nickname, 0,
1174 slotID, NULL);
1175 }
1176 if (listItem->io == NULL)
1177 goto loser;
1178 }
1179 } else {
1180 listItem->io = AddObjectIfNeeded(CKO_CERTIFICATE, pemCert,
1181 derlist[0], NULL, filename, objid,
1182 slotID, NULL);
1183 if (listItem->io == NULL)
1184 goto loser;
1185 }
1186 } else if (objClass == CKO_PRIVATE_KEY) {
1187 pemInternalObject *curObj;
1188 SECItem certDER;
1189 PRBool added;
1190
1191 nobjs = ReadDERFromFile(&derlist, filename, &cipher, &ivstring,
1192 /* keys only */ PR_FALSE);
1193 if (nobjs < 1)
1194 goto loser;
1195
1196 certDER.len = 0; /* in case there is no equivalent cert */
1197 certDER.data = NULL;
1198
1199 /* Brute force: find the id of the certificate, if any, in this slot */
1200 objid = -1;
1201 list_for_each_entry_reverse(curObj, &pem_objs, gl_list) {
1202 if (slotID != curObj->slotID)
1203 continue;
1204
1205 if (curObj->type != pemCert)
1206 continue;
1207
1208 if (curObj->objid != pem_nobjs)
1209 /* not a certificate that refers to the key being added */
1210 continue;
1211
1212 objid = pem_nobjs;
1213 certDER.data = NSS_ZAlloc(NULL, curObj->derCert->len);
1214
1215 if (certDER.data == NULL)
1216 goto loser;
1217
1218 certDER.len = curObj->derCert->len;
1219 memcpy(certDER.data,
1220 curObj->derCert->data,
1221 curObj->derCert->len);
1222 break;
1223 }
1224
1225 /* We're just adding a key, we'll assume the cert is next */
1226 if (objid == -1)
1227 objid = pem_nobjs + 1;
1228
1229 listItem->io = AddObjectIfNeeded(CKO_PRIVATE_KEY, pemBareKey, &certDER,
1230 derlist[0], filename, objid, slotID,
1231 &added);
1232 if (listItem->io == NULL)
1233 goto loser;
1234
1235 listItem->io->u.key.ivstring = ivstring;
1236 listItem->io->u.key.cipher = cipher;
1237 NSS_ZFreeIf(certDER.data);
1238
1239 /* If the key was encrypted then free the session to make it appear that
1240 * the token was removed so we can force a login.
1241 */
1242 if (cipher && added) {
1243 /* remember the slot for pem_mdInstance_WaitForSlotEvent() */
1244 lastEventSlot = NSSCKFWSlot_GetMDSlot(fwSlot);
1245
1246 token_needsLogin[slotID - 1] = PR_TRUE;
1247 /* FIXME: dirty hack relying on NSS internals */
1248 CK_SESSION_HANDLE hSession =
1249 NSSCKFWInstance_FindSessionHandle(fwInstance, fwSession);
1250 NSSCKFWC_Logout(fwInstance, hSession);
1251 NSSCKFWInstance_DestroySessionHandle(fwInstance, hSession);
1252 } else {
1253 *pError = CKR_KEY_UNEXTRACTABLE;
1254 }
1255 } else {
1256 *pError = CKR_ATTRIBUTE_VALUE_INVALID;
1257 }
1258
1259 loser:
1260
1261 for (i = 0; i < nobjs; i++) {
1262 NSS_ZFreeIf(derlist[i]->data);
1263 NSS_ZFreeIf(derlist[i]);
1264 }
1265 NSS_ZFreeIf(filename);
1266 NSS_ZFreeIf(derlist);
1267 if ((pemInternalObject *) NULL == listItem->io) {
1268 pem_DestroyInternalObject(listObj);
1269 return (NSSCKMDObject *) NULL;
1270 }
1271 return pem_CreateMDObject(NULL, listObj, pError);
1272 }