]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_smime/lib/smimeutil.c
Security-57740.31.2.tar.gz
[apple/security.git] / OSX / libsecurity_smime / lib / smimeutil.c
1 /*
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is the Netscape security libraries.
13 *
14 * The Initial Developer of the Original Code is Netscape
15 * Communications Corporation. Portions created by Netscape are
16 * Copyright (C) 1994-2000 Netscape Communications Corporation. All
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * Alternatively, the contents of this file may be used under the
22 * terms of the GNU General Public License Version 2 or later (the
23 * "GPL"), in which case the provisions of the GPL are applicable
24 * instead of those above. If you wish to allow use of your
25 * version of this file only under the terms of the GPL and not to
26 * allow others to use your version of this file under the MPL,
27 * indicate your decision by deleting the provisions above and
28 * replace them with the notice and other provisions required by
29 * the GPL. If you do not delete the provisions above, a recipient
30 * may use your version of this file under either the MPL or the
31 * GPL.
32 */
33
34 /*
35 * Stuff specific to S/MIME policy and interoperability.
36 */
37
38 #include "cmslocal.h"
39
40 #include "secoid.h"
41 #include "secitem.h"
42 #include "cert.h"
43 #include "SecSMIMEPriv.h"
44
45 #include <security_asn1/secasn1.h>
46 #include <security_asn1/secerr.h>
47 #include <Security/SecSMIME.h>
48 #include <Security/SecKeyPriv.h>
49
50 SEC_ASN1_MKSUB(CERT_IssuerAndSNTemplate)
51 SEC_ASN1_MKSUB(SEC_OctetStringTemplate)
52 SEC_ASN1_CHOOSER_DECLARE(CERT_IssuerAndSNTemplate)
53
54 /* various integer's ASN.1 encoding */
55 static unsigned char asn1_int40[] = { SEC_ASN1_INTEGER, 0x01, 0x28 };
56 static unsigned char asn1_int64[] = { SEC_ASN1_INTEGER, 0x01, 0x40 };
57 static unsigned char asn1_int128[] = { SEC_ASN1_INTEGER, 0x02, 0x00, 0x80 };
58
59 /* RC2 algorithm parameters (used in smime_cipher_map) */
60 static CSSM_DATA param_int40 = { sizeof(asn1_int40), asn1_int40 };
61 static CSSM_DATA param_int64 = { sizeof(asn1_int64), asn1_int64 };
62 static CSSM_DATA param_int128 = { sizeof(asn1_int128), asn1_int128 };
63
64 /*
65 * XXX Would like the "parameters" field to be a CSSM_DATA_PTR , but the
66 * encoder is having trouble with optional pointers to an ANY. Maybe
67 * once that is fixed, can change this back...
68 */
69 typedef struct {
70 CSSM_DATA capabilityID;
71 CSSM_DATA parameters;
72 long cipher; /* optimization */
73 } NSSSMIMECapability;
74
75 static const SecAsn1Template NSSSMIMECapabilityTemplate[] = {
76 { SEC_ASN1_SEQUENCE,
77 0, NULL, sizeof(NSSSMIMECapability) },
78 { SEC_ASN1_OBJECT_ID,
79 offsetof(NSSSMIMECapability,capabilityID), },
80 { SEC_ASN1_OPTIONAL | SEC_ASN1_ANY,
81 offsetof(NSSSMIMECapability,parameters), },
82 { 0, }
83 };
84
85 static const SecAsn1Template NSSSMIMECapabilitiesTemplate[] = {
86 { SEC_ASN1_SEQUENCE_OF, 0, NSSSMIMECapabilityTemplate }
87 };
88
89 /*
90 * NSSSMIMEEncryptionKeyPreference - if we find one of these, it needs to prompt us
91 * to store this and only this certificate permanently for the sender email address.
92 */
93 typedef enum {
94 NSSSMIMEEncryptionKeyPref_IssuerSN,
95 NSSSMIMEEncryptionKeyPref_RKeyID,
96 NSSSMIMEEncryptionKeyPref_SubjectKeyID
97 } NSSSMIMEEncryptionKeyPrefSelector;
98
99 typedef struct {
100 NSSSMIMEEncryptionKeyPrefSelector selector;
101 union {
102 SecCmsIssuerAndSN *issuerAndSN;
103 SecCmsRecipientKeyIdentifier *recipientKeyID;
104 CSSM_DATA_PTR subjectKeyID;
105 } id;
106 } NSSSMIMEEncryptionKeyPreference;
107
108 extern const SecAsn1Template SecCmsRecipientKeyIdentifierTemplate[];
109
110 static const SecAsn1Template smime_encryptionkeypref_template[] = {
111 { SEC_ASN1_CHOICE,
112 offsetof(NSSSMIMEEncryptionKeyPreference,selector), NULL,
113 sizeof(NSSSMIMEEncryptionKeyPreference) },
114 { SEC_ASN1_POINTER | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
115 offsetof(NSSSMIMEEncryptionKeyPreference,id.issuerAndSN),
116 SEC_ASN1_SUB(SecCmsIssuerAndSNTemplate),
117 NSSSMIMEEncryptionKeyPref_IssuerSN },
118 { SEC_ASN1_POINTER | SEC_ASN1_CONTEXT_SPECIFIC | 1,
119 offsetof(NSSSMIMEEncryptionKeyPreference,id.recipientKeyID),
120 SecCmsRecipientKeyIdentifierTemplate,
121 NSSSMIMEEncryptionKeyPref_IssuerSN },
122 { SEC_ASN1_POINTER | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 2,
123 offsetof(NSSSMIMEEncryptionKeyPreference,id.subjectKeyID),
124 SEC_ASN1_SUB(kSecAsn1OctetStringTemplate),
125 NSSSMIMEEncryptionKeyPref_SubjectKeyID },
126 { 0, }
127 };
128
129 /* smime_cipher_map - map of SMIME symmetric "ciphers" to algtag & parameters */
130 typedef struct {
131 unsigned long cipher;
132 SECOidTag algtag;
133 CSSM_DATA_PTR parms;
134 Boolean enabled; /* in the user's preferences */
135 Boolean allowed; /* per export policy */
136 } smime_cipher_map_entry;
137
138 /* global: list of supported SMIME symmetric ciphers, ordered roughly by increasing strength */
139 static smime_cipher_map_entry smime_cipher_map[] = {
140 /* cipher algtag parms enabled allowed */
141 /* ---------------------------------------------------------------------------------- */
142 { SMIME_RC2_CBC_40, SEC_OID_RC2_CBC, &param_int40, PR_TRUE, PR_TRUE },
143 { SMIME_DES_CBC_56, SEC_OID_DES_CBC, NULL, PR_TRUE, PR_TRUE },
144 { SMIME_RC2_CBC_64, SEC_OID_RC2_CBC, &param_int64, PR_TRUE, PR_TRUE },
145 { SMIME_RC2_CBC_128, SEC_OID_RC2_CBC, &param_int128, PR_TRUE, PR_TRUE },
146 { SMIME_DES_EDE3_168, SEC_OID_DES_EDE3_CBC, NULL, PR_TRUE, PR_TRUE },
147 { SMIME_AES_CBC_128, SEC_OID_AES_128_CBC, NULL, PR_TRUE, PR_TRUE },
148 { SMIME_FORTEZZA, SEC_OID_FORTEZZA_SKIPJACK, NULL, PR_TRUE, PR_TRUE }
149 };
150 static const int smime_cipher_map_count = sizeof(smime_cipher_map) / sizeof(smime_cipher_map_entry);
151
152 /*
153 * smime_mapi_by_cipher - find index into smime_cipher_map by cipher
154 */
155 static int
156 smime_mapi_by_cipher(unsigned long cipher)
157 {
158 int i;
159
160 for (i = 0; i < smime_cipher_map_count; i++) {
161 if (smime_cipher_map[i].cipher == cipher)
162 return i; /* bingo */
163 }
164 return -1; /* should not happen if we're consistent, right? */
165 }
166
167 /*
168 * NSS_SMIME_EnableCipher - this function locally records the user's preference
169 */
170 OSStatus
171 SecSMIMEEnableCipher(uint32 which, Boolean on)
172 {
173 unsigned long mask;
174 int mapi;
175
176 mask = which & CIPHER_FAMILYID_MASK;
177
178 PORT_Assert (mask == CIPHER_FAMILYID_SMIME);
179 if (mask != CIPHER_FAMILYID_SMIME)
180 /* XXX set an error! */
181 return SECFailure;
182
183 mapi = smime_mapi_by_cipher(which);
184 if (mapi < 0)
185 /* XXX set an error */
186 return SECFailure;
187
188 /* do we try to turn on a forbidden cipher? */
189 if (!smime_cipher_map[mapi].allowed && on) {
190 PORT_SetError (SEC_ERROR_BAD_EXPORT_ALGORITHM);
191 return SECFailure;
192 }
193
194 if (smime_cipher_map[mapi].enabled != on)
195 smime_cipher_map[mapi].enabled = on;
196
197 return SECSuccess;
198 }
199
200
201 /*
202 * this function locally records the export policy
203 */
204 OSStatus
205 SecSMIMEAllowCipher(uint32 which, Boolean on)
206 {
207 unsigned long mask;
208 int mapi;
209
210 mask = which & CIPHER_FAMILYID_MASK;
211
212 PORT_Assert (mask == CIPHER_FAMILYID_SMIME);
213 if (mask != CIPHER_FAMILYID_SMIME)
214 /* XXX set an error! */
215 return SECFailure;
216
217 mapi = smime_mapi_by_cipher(which);
218 if (mapi < 0)
219 /* XXX set an error */
220 return SECFailure;
221
222 if (smime_cipher_map[mapi].allowed != on)
223 smime_cipher_map[mapi].allowed = on;
224
225 return SECSuccess;
226 }
227
228 /*
229 * Based on the given algorithm (including its parameters, in some cases!)
230 * and the given key (may or may not be inspected, depending on the
231 * algorithm), find the appropriate policy algorithm specification
232 * and return it. If no match can be made, -1 is returned.
233 */
234 static OSStatus
235 nss_smime_get_cipher_for_alg_and_key(SECAlgorithmID *algid, SecSymmetricKeyRef key, unsigned long *cipher)
236 {
237 SECOidTag algtag;
238 unsigned int keylen_bits;
239 unsigned long c;
240
241 algtag = SECOID_GetAlgorithmTag(algid);
242 switch (algtag) {
243 case SEC_OID_RC2_CBC:
244 if (SecKeyGetStrengthInBits(key, algid, &keylen_bits))
245 return SECFailure;
246 switch (keylen_bits) {
247 case 40:
248 c = SMIME_RC2_CBC_40;
249 break;
250 case 64:
251 c = SMIME_RC2_CBC_64;
252 break;
253 case 128:
254 c = SMIME_RC2_CBC_128;
255 break;
256 default:
257 return SECFailure;
258 }
259 break;
260 case SEC_OID_DES_CBC:
261 c = SMIME_DES_CBC_56;
262 break;
263 case SEC_OID_DES_EDE3_CBC:
264 c = SMIME_DES_EDE3_168;
265 break;
266 case SEC_OID_AES_128_CBC:
267 c = SMIME_AES_CBC_128;
268 break;
269 case SEC_OID_FORTEZZA_SKIPJACK:
270 c = SMIME_FORTEZZA;
271 break;
272 default:
273 return SECFailure;
274 }
275 *cipher = c;
276 return SECSuccess;
277 }
278
279 static Boolean
280 nss_smime_cipher_allowed(unsigned long which)
281 {
282 int mapi;
283
284 mapi = smime_mapi_by_cipher(which);
285 if (mapi < 0)
286 return PR_FALSE;
287 return smime_cipher_map[mapi].allowed;
288 }
289
290 Boolean
291 SecSMIMEDecryptionAllowed(SECAlgorithmID *algid, SecSymmetricKeyRef key)
292 {
293 unsigned long which;
294
295 if (nss_smime_get_cipher_for_alg_and_key(algid, key, &which) != SECSuccess)
296 return PR_FALSE;
297
298 return nss_smime_cipher_allowed(which);
299 }
300
301
302 /*
303 * NSS_SMIME_EncryptionPossible - check if any encryption is allowed
304 *
305 * This tells whether or not *any* S/MIME encryption can be done,
306 * according to policy. Callers may use this to do nicer user interface
307 * (say, greying out a checkbox so a user does not even try to encrypt
308 * a message when they are not allowed to) or for any reason they want
309 * to check whether S/MIME encryption (or decryption, for that matter)
310 * may be done.
311 *
312 * It takes no arguments. The return value is a simple boolean:
313 * PR_TRUE means encryption (or decryption) is *possible*
314 * (but may still fail due to other reasons, like because we cannot
315 * find all the necessary certs, etc.; PR_TRUE is *not* a guarantee)
316 * PR_FALSE means encryption (or decryption) is not permitted
317 *
318 * There are no errors from this routine.
319 */
320 Boolean
321 SecSMIMEEncryptionPossible(void)
322 {
323 int i;
324
325 for (i = 0; i < smime_cipher_map_count; i++) {
326 if (smime_cipher_map[i].allowed)
327 return PR_TRUE;
328 }
329 return PR_FALSE;
330 }
331
332
333 static unsigned long
334 nss_SMIME_FindCipherForSMIMECap(NSSSMIMECapability *cap)
335 {
336 int i;
337 SECOidTag capIDTag;
338
339 /* we need the OIDTag here */
340 capIDTag = SECOID_FindOIDTag(&(cap->capabilityID));
341
342 /* go over all the SMIME ciphers we know and see if we find a match */
343 for (i = 0; i < smime_cipher_map_count; i++) {
344 if (smime_cipher_map[i].algtag != capIDTag)
345 continue;
346 /*
347 * XXX If SECITEM_CompareItem allowed NULLs as arguments (comparing
348 * 2 NULLs as equal and NULL and non-NULL as not equal), we could
349 * use that here instead of all of the following comparison code.
350 */
351 if (cap->parameters.Data == NULL && smime_cipher_map[i].parms == NULL)
352 break; /* both empty: bingo */
353
354 if (cap->parameters.Data != NULL && smime_cipher_map[i].parms != NULL &&
355 cap->parameters.Length == smime_cipher_map[i].parms->Length &&
356 PORT_Memcmp (cap->parameters.Data, smime_cipher_map[i].parms->Data,
357 cap->parameters.Length) == 0)
358 {
359 break; /* both not empty, same length & equal content: bingo */
360 }
361 }
362
363 if (i == smime_cipher_map_count)
364 return 0; /* no match found */
365 else
366 return smime_cipher_map[i].cipher; /* match found, point to cipher */
367 }
368
369 static int smime_keysize_by_cipher (unsigned long which);
370
371 /*
372 * smime_choose_cipher - choose a cipher that works for all the recipients
373 *
374 * "scert" - sender's certificate
375 * "rcerts" - recipient's certificates
376 */
377 static long
378 smime_choose_cipher(SecCertificateRef scert, SecCertificateRef *rcerts)
379 {
380 PRArenaPool *poolp;
381 long cipher;
382 long chosen_cipher;
383 int *cipher_abilities;
384 int *cipher_votes;
385 int weak_mapi;
386 int strong_mapi;
387 int rcount, mapi, max, i;
388 #if 1
389 // @@@ We Don't support Fortezza yet.
390 Boolean scert_is_fortezza = PR_FALSE;
391 #else
392 Boolean scert_is_fortezza = (scert == NULL) ? PR_FALSE : PK11_FortezzaHasKEA(scert);
393 #endif
394
395 chosen_cipher = SMIME_RC2_CBC_40; /* the default, LCD */
396 weak_mapi = smime_mapi_by_cipher(chosen_cipher);
397
398 poolp = PORT_NewArena (1024); /* XXX what is right value? */
399 if (poolp == NULL)
400 goto done;
401
402 cipher_abilities = (int *)PORT_ArenaZAlloc(poolp, smime_cipher_map_count * sizeof(int));
403 cipher_votes = (int *)PORT_ArenaZAlloc(poolp, smime_cipher_map_count * sizeof(int));
404 if (cipher_votes == NULL || cipher_abilities == NULL)
405 goto done;
406
407 /* If the user has the Fortezza preference turned on, make
408 * that the strong cipher. Otherwise, use triple-DES. */
409 strong_mapi = smime_mapi_by_cipher (SMIME_DES_EDE3_168);
410 if (scert_is_fortezza) {
411 mapi = smime_mapi_by_cipher(SMIME_FORTEZZA);
412 if (mapi >= 0 && smime_cipher_map[mapi].enabled)
413 strong_mapi = mapi;
414 }
415
416 /* walk all the recipient's certs */
417 for (rcount = 0; rcerts[rcount] != NULL; rcount++) {
418 CSSM_DATA_PTR profile;
419 NSSSMIMECapability **caps;
420 int pref;
421
422 /* the first cipher that matches in the user's SMIME profile gets
423 * "smime_cipher_map_count" votes; the next one gets "smime_cipher_map_count" - 1
424 * and so on. If every cipher matches, the last one gets 1 (one) vote */
425 pref = smime_cipher_map_count;
426
427 /* find recipient's SMIME profile */
428 profile = CERT_FindSMimeProfile(rcerts[rcount]);
429
430 if (profile != NULL && profile->Data != NULL && profile->Length > 0) {
431 /* we have a profile (still DER-encoded) */
432 caps = NULL;
433 /* decode it */
434 if (SEC_ASN1DecodeItem(poolp, &caps, NSSSMIMECapabilitiesTemplate, profile) == SECSuccess &&
435 caps != NULL)
436 {
437 /* walk the SMIME capabilities for this recipient */
438 for (i = 0; caps[i] != NULL; i++) {
439 cipher = nss_SMIME_FindCipherForSMIMECap(caps[i]);
440 mapi = smime_mapi_by_cipher(cipher);
441 if (mapi >= 0) {
442 /* found the cipher */
443 cipher_abilities[mapi]++;
444 cipher_votes[mapi] += pref;
445 --pref;
446 }
447 }
448 }
449 } else {
450 /* no profile found - so we can only assume that the user can do
451 * the mandatory algorithms which is RC2-40 (weak crypto) and 3DES (strong crypto) */
452 SecPublicKeyRef key;
453 unsigned int pklen_bits;
454
455 /*
456 * if recipient's public key length is > 512, vote for a strong cipher
457 * please not that the side effect of this is that if only one recipient
458 * has an export-level public key, the strong cipher is disabled.
459 *
460 * XXX This is probably only good for RSA keys. What I would
461 * really like is a function to just say; Is the public key in
462 * this cert an export-length key? Then I would not have to
463 * know things like the value 512, or the kind of key, or what
464 * a subjectPublicKeyInfo is, etc.
465 */
466 key = CERT_ExtractPublicKey(rcerts[rcount]);
467 pklen_bits = 0;
468 if (key != NULL) {
469 SecKeyGetStrengthInBits(key, NULL, &pklen_bits);
470 SECKEY_DestroyPublicKey (key);
471 }
472
473 if (pklen_bits > 512) {
474 /* cast votes for the strong algorithm */
475 cipher_abilities[strong_mapi]++;
476 cipher_votes[strong_mapi] += pref;
477 pref--;
478 }
479
480 /* always cast (possibly less) votes for the weak algorithm */
481 cipher_abilities[weak_mapi]++;
482 cipher_votes[weak_mapi] += pref;
483 }
484 if (profile != NULL)
485 SECITEM_FreeItem(profile, PR_TRUE);
486 }
487
488 /* find cipher that is agreeable by all recipients and that has the most votes */
489 max = 0;
490 for (mapi = 0; mapi < smime_cipher_map_count; mapi++) {
491 /* if not all of the recipients can do this, forget it */
492 if (cipher_abilities[mapi] != rcount)
493 continue;
494 /* if cipher is not enabled or not allowed by policy, forget it */
495 if (!smime_cipher_map[mapi].enabled || !smime_cipher_map[mapi].allowed)
496 continue;
497 /* if we're not doing fortezza, but the cipher is fortezza, forget it */
498 if (!scert_is_fortezza && (smime_cipher_map[mapi].cipher == SMIME_FORTEZZA))
499 continue;
500 /* now see if this one has more votes than the last best one */
501 if (cipher_votes[mapi] >= max) {
502 /* if equal number of votes, prefer the ones further down in the list */
503 /* with the expectation that these are higher rated ciphers */
504 chosen_cipher = smime_cipher_map[mapi].cipher;
505 max = cipher_votes[mapi];
506 }
507 }
508 /* if no common cipher was found, chosen_cipher stays at the default */
509
510 done:
511 if (poolp != NULL)
512 PORT_FreeArena (poolp, PR_FALSE);
513
514 if (smime_keysize_by_cipher(chosen_cipher) < 128) {
515 /* you're going to use strong(er) crypto whether you like it or not */
516 chosen_cipher = SMIME_DES_EDE3_168;
517 }
518 return chosen_cipher;
519 }
520
521 /*
522 * XXX This is a hack for now to satisfy our current interface.
523 * Eventually, with more parameters needing to be specified, just
524 * looking up the keysize is not going to be sufficient.
525 */
526 static int
527 smime_keysize_by_cipher (unsigned long which)
528 {
529 int keysize;
530
531 switch (which) {
532 case SMIME_RC2_CBC_40:
533 keysize = 40;
534 break;
535 case SMIME_RC2_CBC_64:
536 keysize = 64;
537 break;
538 case SMIME_RC2_CBC_128:
539 case SMIME_AES_CBC_128:
540 keysize = 128;
541 break;
542 case SMIME_DES_CBC_56:
543 keysize = 64;
544 break;
545 case SMIME_DES_EDE3_168:
546 keysize = 192;
547 break;
548 case SMIME_FORTEZZA:
549 /*
550 * This is special; since the key size is fixed, we actually
551 * want to *avoid* specifying a key size.
552 */
553 keysize = 0;
554 break;
555 default:
556 keysize = -1;
557 break;
558 }
559
560 return keysize;
561 }
562
563 /*
564 * SecSMIMEFindBulkAlgForRecipients - find bulk algorithm suitable for all recipients
565 *
566 * it would be great for UI purposes if there would be a way to find out which recipients
567 * prevented a strong cipher from being used...
568 */
569 OSStatus
570 SecSMIMEFindBulkAlgForRecipients(SecCertificateRef *rcerts, SECOidTag *bulkalgtag, int *keysize)
571 {
572 unsigned long cipher;
573 int mapi;
574
575 cipher = smime_choose_cipher(NULL, rcerts);
576 mapi = smime_mapi_by_cipher(cipher);
577
578 *bulkalgtag = smime_cipher_map[mapi].algtag;
579 *keysize = smime_keysize_by_cipher(smime_cipher_map[mapi].cipher);
580
581 return SECSuccess;
582 }
583
584 /*
585 * SecSMIMECreateSMIMECapabilities - get S/MIME capabilities for this instance of NSS
586 *
587 * scans the list of allowed and enabled ciphers and construct a PKCS9-compliant
588 * S/MIME capabilities attribute value.
589 *
590 * XXX Please note that, in contradiction to RFC2633 2.5.2, the capabilities only include
591 * symmetric ciphers, NO signature algorithms or key encipherment algorithms.
592 *
593 * "poolp" - arena pool to create the S/MIME capabilities data on
594 * "dest" - CSSM_DATA to put the data in
595 * "includeFortezzaCiphers" - PR_TRUE if fortezza ciphers should be included
596 */
597 OSStatus
598 SecSMIMECreateSMIMECapabilities(SecArenaPoolRef pool, CSSM_DATA_PTR dest, Boolean includeFortezzaCiphers)
599 {
600 PLArenaPool *poolp = (PLArenaPool *)pool;
601 NSSSMIMECapability *cap;
602 NSSSMIMECapability **smime_capabilities;
603 smime_cipher_map_entry *map;
604 SECOidData *oiddata;
605 CSSM_DATA_PTR dummy;
606 int i, capIndex;
607
608 /* if we have an old NSSSMIMECapability array, we'll reuse it (has the right size) */
609 /* smime_cipher_map_count + 1 is an upper bound - we might end up with less */
610 smime_capabilities = (NSSSMIMECapability **)PORT_ZAlloc((smime_cipher_map_count + 1)
611 * sizeof(NSSSMIMECapability *));
612 if (smime_capabilities == NULL)
613 return SECFailure;
614
615 capIndex = 0;
616
617 /* Add all the symmetric ciphers
618 * We walk the cipher list backwards, as it is ordered by increasing strength,
619 * we prefer the stronger cipher over a weaker one, and we have to list the
620 * preferred algorithm first */
621 for (i = smime_cipher_map_count - 1; i >= 0; i--) {
622 /* Find the corresponding entry in the cipher map. */
623 map = &(smime_cipher_map[i]);
624 if (!map->enabled)
625 continue;
626
627 /* If we're using a non-Fortezza cert, only advertise non-Fortezza
628 capabilities. (We advertise all capabilities if we have a
629 Fortezza cert.) */
630 if ((!includeFortezzaCiphers) && (map->cipher == SMIME_FORTEZZA))
631 continue;
632
633 /* get next SMIME capability */
634 cap = (NSSSMIMECapability *)PORT_ZAlloc(sizeof(NSSSMIMECapability));
635 if (cap == NULL)
636 break;
637 smime_capabilities[capIndex++] = cap;
638
639 oiddata = SECOID_FindOIDByTag(map->algtag);
640 if (oiddata == NULL)
641 break;
642
643 cap->capabilityID.Data = oiddata->oid.Data;
644 cap->capabilityID.Length = oiddata->oid.Length;
645 cap->parameters.Data = map->parms ? map->parms->Data : NULL;
646 cap->parameters.Length = map->parms ? map->parms->Length : 0;
647 cap->cipher = smime_cipher_map[i].cipher;
648 }
649
650 /* XXX add signature algorithms */
651 /* XXX add key encipherment algorithms */
652
653 smime_capabilities[capIndex] = NULL; /* last one - now encode */
654 dummy = SEC_ASN1EncodeItem(poolp, dest, &smime_capabilities, NSSSMIMECapabilitiesTemplate);
655
656 /* now that we have the proper encoded SMIMECapabilities (or not),
657 * free the work data */
658 for (i = 0; smime_capabilities[i] != NULL; i++)
659 PORT_Free(smime_capabilities[i]);
660 PORT_Free(smime_capabilities);
661
662 return (dummy == NULL) ? SECFailure : SECSuccess;
663 }
664
665 /*
666 * SecSMIMECreateSMIMEEncKeyPrefs - create S/MIME encryption key preferences attr value
667 *
668 * "poolp" - arena pool to create the attr value on
669 * "dest" - CSSM_DATA to put the data in
670 * "cert" - certificate that should be marked as preferred encryption key
671 * cert is expected to have been verified for EmailRecipient usage.
672 */
673 OSStatus
674 SecSMIMECreateSMIMEEncKeyPrefs(SecArenaPoolRef pool, CSSM_DATA_PTR dest, SecCertificateRef cert)
675 {
676 PLArenaPool *poolp = (PLArenaPool *)pool;
677 NSSSMIMEEncryptionKeyPreference ekp;
678 CSSM_DATA_PTR dummy = NULL;
679 PLArenaPool *tmppoolp = NULL;
680
681 if (cert == NULL)
682 goto loser;
683
684 tmppoolp = PORT_NewArena(1024);
685 if (tmppoolp == NULL)
686 goto loser;
687
688 /* XXX hardcoded IssuerSN choice for now */
689 ekp.selector = NSSSMIMEEncryptionKeyPref_IssuerSN;
690 ekp.id.issuerAndSN = CERT_GetCertIssuerAndSN(tmppoolp, cert);
691 if (ekp.id.issuerAndSN == NULL)
692 goto loser;
693
694 dummy = SEC_ASN1EncodeItem(poolp, dest, &ekp, smime_encryptionkeypref_template);
695
696 loser:
697 if (tmppoolp) PORT_FreeArena(tmppoolp, PR_FALSE);
698
699 return (dummy == NULL) ? SECFailure : SECSuccess;
700 }
701
702 /*
703 * SecSMIMECreateSMIMEEncKeyPrefs - create S/MIME encryption key preferences attr value using MS oid
704 *
705 * "poolp" - arena pool to create the attr value on
706 * "dest" - CSSM_DATA to put the data in
707 * "cert" - certificate that should be marked as preferred encryption key
708 * cert is expected to have been verified for EmailRecipient usage.
709 */
710 OSStatus
711 SecSMIMECreateMSSMIMEEncKeyPrefs(SecArenaPoolRef pool, CSSM_DATA_PTR dest, SecCertificateRef cert)
712 {
713 PLArenaPool *poolp = (PLArenaPool *)pool;
714 CSSM_DATA_PTR dummy = NULL;
715 PLArenaPool *tmppoolp = NULL;
716 SecCmsIssuerAndSN *isn;
717
718 if (cert == NULL)
719 goto loser;
720
721 tmppoolp = PORT_NewArena(1024);
722 if (tmppoolp == NULL)
723 goto loser;
724
725 isn = CERT_GetCertIssuerAndSN(tmppoolp, cert);
726 if (isn == NULL)
727 goto loser;
728
729 dummy = SEC_ASN1EncodeItem(poolp, dest, isn, SEC_ASN1_GET(SecCmsIssuerAndSNTemplate));
730
731 loser:
732 if (tmppoolp) PORT_FreeArena(tmppoolp, PR_FALSE);
733
734 return (dummy == NULL) ? SECFailure : SECSuccess;
735 }
736
737 /*
738 * SecSMIMEGetCertFromEncryptionKeyPreference -
739 * find cert marked by EncryptionKeyPreference attribute
740 *
741 * "keychainOrArray" - handle for the cert database to look in
742 * "DERekp" - DER-encoded value of S/MIME Encryption Key Preference attribute
743 *
744 * if certificate is supposed to be found among the message's included certificates,
745 * they are assumed to have been imported already.
746 */
747 SecCertificateRef
748 SecSMIMEGetCertFromEncryptionKeyPreference(SecKeychainRef keychainOrArray, CSSM_DATA_PTR DERekp)
749 {
750 PLArenaPool *tmppoolp = NULL;
751 SecCertificateRef cert = NULL;
752 NSSSMIMEEncryptionKeyPreference ekp;
753
754 tmppoolp = PORT_NewArena(1024);
755 if (tmppoolp == NULL)
756 return NULL;
757
758 /* decode DERekp */
759 if (SEC_ASN1DecodeItem(tmppoolp, &ekp, smime_encryptionkeypref_template, DERekp) != SECSuccess)
760 goto loser;
761
762 /* find cert */
763 switch (ekp.selector) {
764 case NSSSMIMEEncryptionKeyPref_IssuerSN:
765 cert = CERT_FindCertByIssuerAndSN(keychainOrArray, NULL, NULL, ekp.id.issuerAndSN);
766 break;
767 case NSSSMIMEEncryptionKeyPref_RKeyID:
768 case NSSSMIMEEncryptionKeyPref_SubjectKeyID:
769 /* XXX not supported yet - we need to be able to look up certs by SubjectKeyID */
770 break;
771 default:
772 PORT_Assert(0);
773 }
774 loser:
775 if (tmppoolp) PORT_FreeArena(tmppoolp, PR_FALSE);
776
777 return cert;
778 }
779
780 #if 0
781 extern const char __nss_smime_rcsid[];
782 extern const char __nss_smime_sccsid[];
783 #endif
784
785 #if 0 /* -- unused */
786 Boolean
787 NSSSMIME_VersionCheck(const char *importedVersion)
788 {
789 #if 1
790 return PR_TRUE;
791 #else
792 /*
793 * This is the secret handshake algorithm.
794 *
795 * This release has a simple version compatibility
796 * check algorithm. This release is not backward
797 * compatible with previous major releases. It is
798 * not compatible with future major, minor, or
799 * patch releases.
800 */
801 volatile char c; /* force a reference that won't get optimized away */
802
803 c = __nss_smime_rcsid[0] + __nss_smime_sccsid[0];
804
805 return NSS_VersionCheck(importedVersion);
806 #endif
807 }
808 #endif