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/
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.
12 * The Original Code is the Netscape security libraries.
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
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
35 * CMS recipientInfo methods.
44 #include <security_asn1/secasn1.h>
45 #include <security_asn1/secerr.h>
46 #include <Security/SecKeyPriv.h>
47 #include <Security/SecCertificatePriv.h>
48 #include <Security/SecCmsRecipientInfo.h>
51 nss_cmsrecipientinfo_usessubjectkeyid(SecCmsRecipientInfoRef ri
)
53 if (ri
->recipientInfoType
== SecCmsRecipientInfoIDKeyTrans
) {
54 SecCmsRecipientIdentifier
*rid
;
55 rid
= &ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
;
56 if (rid
->identifierType
== SecCmsRecipientIDSubjectKeyID
) {
64 static SecCmsRecipientInfoRef
65 nss_cmsrecipientinfo_create(SecCmsMessageRef cmsg
, SecCmsRecipientIDSelector type
,
66 SecCertificateRef cert
, SecPublicKeyRef pubKey
,
67 CSSM_DATA_PTR subjKeyID
)
69 SecCmsRecipientInfoRef ri
;
72 OSStatus rv
= SECSuccess
;
73 SecCmsRecipientEncryptedKey
*rek
;
74 SecCmsOriginatorIdentifierOrKey
*oiok
;
75 unsigned long version
;
78 const SECAlgorithmID
*algid
;
79 SecCmsRecipientIdentifier
*rid
;
83 mark
= PORT_ArenaMark(poolp
);
85 ri
= (SecCmsRecipientInfoRef
)PORT_ArenaZAlloc(poolp
, sizeof(SecCmsRecipientInfo
));
90 if (type
== SecCmsRecipientIDIssuerSN
)
92 ri
->cert
= CERT_DupCertificate(cert
);
95 rv
= SecCertificateGetAlgorithmID(cert
,&algid
);
98 #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR))
99 rv
= SecKeyGetAlgorithmID(pubKey
,&algid
);
101 /* TBD: Unify this code. Currently, iOS has an incompatible
102 * SecKeyGetAlgorithmID implementation. */
107 certalgtag
= SECOID_GetAlgorithmTag(algid
);
109 rid
= &ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
;
110 switch (certalgtag
) {
111 case SEC_OID_PKCS1_RSA_ENCRYPTION
:
112 ri
->recipientInfoType
= SecCmsRecipientInfoIDKeyTrans
;
113 rid
->identifierType
= type
;
114 if (type
== SecCmsRecipientIDIssuerSN
) {
115 rid
->id
.issuerAndSN
= CERT_GetCertIssuerAndSN(poolp
, cert
);
116 if (rid
->id
.issuerAndSN
== NULL
) {
119 } else if (type
== SecCmsRecipientIDSubjectKeyID
){
120 SecCmsKeyTransRecipientInfoEx
*riExtra
;
122 rid
->id
.subjectKeyID
= PORT_ArenaNew(poolp
, CSSM_DATA
);
123 if (rid
->id
.subjectKeyID
== NULL
) {
125 PORT_SetError(SEC_ERROR_NO_MEMORY
);
128 SECITEM_CopyItem(poolp
, rid
->id
.subjectKeyID
, subjKeyID
);
129 if (rid
->id
.subjectKeyID
->Data
== NULL
) {
131 PORT_SetError(SEC_ERROR_NO_MEMORY
);
134 riExtra
= &ri
->ri
.keyTransRecipientInfoEx
;
135 riExtra
->version
= 0;
136 riExtra
->pubKey
= SECKEY_CopyPublicKey(pubKey
);
137 if (riExtra
->pubKey
== NULL
) {
139 PORT_SetError(SEC_ERROR_NO_MEMORY
);
143 PORT_SetError(SEC_ERROR_INVALID_ARGS
);
147 case SEC_OID_MISSI_KEA_DSS_OLD
:
148 case SEC_OID_MISSI_KEA_DSS
:
149 case SEC_OID_MISSI_KEA
:
150 PORT_Assert(type
!= SecCmsRecipientIDSubjectKeyID
);
151 if (type
== SecCmsRecipientIDSubjectKeyID
) {
155 /* backward compatibility - this is not really a keytrans operation */
156 ri
->recipientInfoType
= SecCmsRecipientInfoIDKeyTrans
;
157 /* hardcoded issuerSN choice for now */
158 ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
.identifierType
= SecCmsRecipientIDIssuerSN
;
159 ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
.id
.issuerAndSN
= CERT_GetCertIssuerAndSN(poolp
, cert
);
160 if (ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
.id
.issuerAndSN
== NULL
) {
165 case SEC_OID_X942_DIFFIE_HELMAN_KEY
: /* dh-public-number */
166 PORT_Assert(type
!= SecCmsRecipientIDSubjectKeyID
);
167 if (type
== SecCmsRecipientIDSubjectKeyID
) {
171 /* a key agreement op */
172 ri
->recipientInfoType
= SecCmsRecipientInfoIDKeyAgree
;
174 if (ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
.id
.issuerAndSN
== NULL
) {
178 /* we do not support the case where multiple recipients
179 * share the same KeyAgreeRecipientInfo and have multiple RecipientEncryptedKeys
180 * in this case, we would need to walk all the recipientInfos, take the
181 * ones that do KeyAgreement algorithms and join them, algorithm by algorithm
182 * Then, we'd generate ONE ukm and OriginatorIdentifierOrKey */
184 /* only epheremal-static Diffie-Hellman is supported for now
185 * this is the only form of key agreement that provides potential anonymity
186 * of the sender, plus we do not have to include certs in the message */
188 /* force single recipientEncryptedKey for now */
189 if ((rek
= SecCmsRecipientEncryptedKeyCreate(poolp
)) == NULL
) {
194 /* hardcoded IssuerSN choice for now */
195 rek
->recipientIdentifier
.identifierType
= SecCmsKeyAgreeRecipientIDIssuerSN
;
196 if ((rek
->recipientIdentifier
.id
.issuerAndSN
= CERT_GetCertIssuerAndSN(poolp
, cert
)) == NULL
) {
201 oiok
= &(ri
->ri
.keyAgreeRecipientInfo
.originatorIdentifierOrKey
);
203 /* see RFC2630 12.3.1.1 */
204 oiok
->identifierType
= SecCmsOriginatorIDOrKeyOriginatorPublicKey
;
206 rv
= SecCmsArrayAdd(poolp
, (void ***)&ri
->ri
.keyAgreeRecipientInfo
.recipientEncryptedKeys
,
211 case SEC_OID_EC_PUBLIC_KEY
:
212 /* ephemeral-static ECDH - issuerAndSN, OriginatorPublicKey only */
213 PORT_Assert(type
!= SecCmsRecipientIDSubjectKeyID
);
214 if (type
== SecCmsRecipientIDSubjectKeyID
) {
218 /* a key agreement op */
219 ri
->recipientInfoType
= SecCmsRecipientInfoIDKeyAgree
;
220 ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
.id
.issuerAndSN
= CERT_GetCertIssuerAndSN(poolp
, cert
);
221 if (ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
.id
.issuerAndSN
== NULL
) {
225 /* we do not support the case where multiple recipients
226 * share the same KeyAgreeRecipientInfo and have multiple RecipientEncryptedKeys
227 * in this case, we would need to walk all the recipientInfos, take the
228 * ones that do KeyAgreement algorithms and join them, algorithm by algorithm
229 * Then, we'd generate ONE ukm and OriginatorIdentifierOrKey */
231 /* force single recipientEncryptedKey for now */
232 if ((rek
= SecCmsRecipientEncryptedKeyCreate(poolp
)) == NULL
) {
237 /* hardcoded IssuerSN choice for now */
238 rek
->recipientIdentifier
.identifierType
= SecCmsKeyAgreeRecipientIDIssuerSN
;
239 if ((rek
->recipientIdentifier
.id
.issuerAndSN
= CERT_GetCertIssuerAndSN(poolp
, cert
)) == NULL
) {
244 oiok
= &(ri
->ri
.keyAgreeRecipientInfo
.originatorIdentifierOrKey
);
246 /* see RFC 3278 3.1.1 */
247 oiok
->identifierType
= SecCmsOriginatorIDOrKeyOriginatorPublicKey
;
249 rv
= SecCmsArrayAdd(poolp
, (void ***)&ri
->ri
.keyAgreeRecipientInfo
.recipientEncryptedKeys
,
255 /* other algorithms not supported yet */
256 /* NOTE that we do not support any KEK algorithm */
257 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM
);
262 if (rv
== SECFailure
)
266 switch (ri
->recipientInfoType
) {
267 case SecCmsRecipientInfoIDKeyTrans
:
268 if (ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
.identifierType
== SecCmsRecipientIDIssuerSN
)
269 version
= SEC_CMS_KEYTRANS_RECIPIENT_INFO_VERSION_ISSUERSN
;
271 version
= SEC_CMS_KEYTRANS_RECIPIENT_INFO_VERSION_SUBJKEY
;
272 dummy
= SEC_ASN1EncodeInteger(poolp
, &(ri
->ri
.keyTransRecipientInfo
.version
), version
);
276 case SecCmsRecipientInfoIDKeyAgree
:
277 dummy
= SEC_ASN1EncodeInteger(poolp
, &(ri
->ri
.keyAgreeRecipientInfo
.version
),
278 SEC_CMS_KEYAGREE_RECIPIENT_INFO_VERSION
);
282 case SecCmsRecipientInfoIDKEK
:
283 /* NOTE: this cannot happen as long as we do not support any KEK algorithm */
284 dummy
= SEC_ASN1EncodeInteger(poolp
, &(ri
->ri
.kekRecipientInfo
.version
),
285 SEC_CMS_KEK_RECIPIENT_INFO_VERSION
);
292 PORT_ArenaUnmark (poolp
, mark
);
295 SECKEY_DestroySubjectPublicKeyInfo(freeSpki
);
302 SECKEY_DestroySubjectPublicKeyInfo(freeSpki
);
304 PORT_ArenaRelease (poolp
, mark
);
309 * SecCmsRecipientInfoCreate - create a recipientinfo
311 * we currently do not create KeyAgreement recipientinfos with multiple
312 * recipientEncryptedKeys the certificate is supposed to have been
313 * verified by the caller
315 SecCmsRecipientInfoRef
316 SecCmsRecipientInfoCreate(SecCmsMessageRef cmsg
, SecCertificateRef cert
)
318 return nss_cmsrecipientinfo_create(cmsg
, SecCmsRecipientIDIssuerSN
, cert
,
322 SecCmsRecipientInfoRef
323 SecCmsRecipientInfoCreateWithSubjKeyID(SecCmsMessageRef cmsg
,
324 CSSM_DATA_PTR subjKeyID
,
325 SecPublicKeyRef pubKey
)
327 return nss_cmsrecipientinfo_create(cmsg
, SecCmsRecipientIDSubjectKeyID
,
328 NULL
, pubKey
, subjKeyID
);
331 SecCmsRecipientInfoRef
332 SecCmsRecipientInfoCreateWithSubjKeyIDFromCert(SecCmsMessageRef cmsg
,
333 SecCertificateRef cert
)
335 SecPublicKeyRef pubKey
= NULL
;
336 CSSM_DATA subjKeyID
= {0, NULL
};
337 SecCmsRecipientInfoRef retVal
= NULL
;
339 if (!cmsg
|| !cert
) {
342 pubKey
= CERT_ExtractPublicKey(cert
);
346 if (CERT_FindSubjectKeyIDExtension(cert
, &subjKeyID
) != SECSuccess
||
347 subjKeyID
.Data
== NULL
) {
350 retVal
= SecCmsRecipientInfoCreateWithSubjKeyID(cmsg
, &subjKeyID
, pubKey
);
353 SECKEY_DestroyPublicKey(pubKey
);
356 SECITEM_FreeItem(&subjKeyID
, PR_FALSE
);
362 SecCmsRecipientInfoDestroy(SecCmsRecipientInfoRef ri
)
364 /* version was allocated on the pool, so no need to destroy it */
365 /* issuerAndSN was allocated on the pool, so no need to destroy it */
366 if (ri
->cert
!= NULL
)
367 CERT_DestroyCertificate(ri
->cert
);
369 if (nss_cmsrecipientinfo_usessubjectkeyid(ri
)) {
370 SecCmsKeyTransRecipientInfoEx
*extra
;
371 extra
= &ri
->ri
.keyTransRecipientInfoEx
;
373 SECKEY_DestroyPublicKey(extra
->pubKey
);
376 /* recipientInfo structure itself was allocated on the pool, so no need to destroy it */
381 SecCmsRecipientInfoGetVersion(SecCmsRecipientInfoRef ri
)
383 unsigned long version
;
384 CSSM_DATA_PTR versionitem
= NULL
;
386 switch (ri
->recipientInfoType
) {
387 case SecCmsRecipientInfoIDKeyTrans
:
388 /* ignore subIndex */
389 versionitem
= &(ri
->ri
.keyTransRecipientInfo
.version
);
391 case SecCmsRecipientInfoIDKEK
:
392 /* ignore subIndex */
393 versionitem
= &(ri
->ri
.kekRecipientInfo
.version
);
395 case SecCmsRecipientInfoIDKeyAgree
:
396 versionitem
= &(ri
->ri
.keyAgreeRecipientInfo
.version
);
400 PORT_Assert(versionitem
);
401 if (versionitem
== NULL
)
404 /* always take apart the CSSM_DATA */
405 if (SEC_ASN1DecodeInteger(versionitem
, &version
) != SECSuccess
)
412 SecCmsRecipientInfoGetEncryptedKey(SecCmsRecipientInfoRef ri
, int subIndex
)
414 CSSM_DATA_PTR enckey
= NULL
;
416 switch (ri
->recipientInfoType
) {
417 case SecCmsRecipientInfoIDKeyTrans
:
418 /* ignore subIndex */
419 enckey
= &(ri
->ri
.keyTransRecipientInfo
.encKey
);
421 case SecCmsRecipientInfoIDKEK
:
422 /* ignore subIndex */
423 enckey
= &(ri
->ri
.kekRecipientInfo
.encKey
);
425 case SecCmsRecipientInfoIDKeyAgree
:
426 enckey
= &(ri
->ri
.keyAgreeRecipientInfo
.recipientEncryptedKeys
[subIndex
]->encKey
);
434 SecCmsRecipientInfoGetKeyEncryptionAlgorithmTag(SecCmsRecipientInfoRef ri
)
436 SECOidTag encalgtag
= SEC_OID_UNKNOWN
; /* an invalid encryption alg */
438 switch (ri
->recipientInfoType
) {
439 case SecCmsRecipientInfoIDKeyTrans
:
440 encalgtag
= SECOID_GetAlgorithmTag(&(ri
->ri
.keyTransRecipientInfo
.keyEncAlg
));
442 case SecCmsRecipientInfoIDKeyAgree
:
443 encalgtag
= SECOID_GetAlgorithmTag(&(ri
->ri
.keyAgreeRecipientInfo
.keyEncAlg
));
445 case SecCmsRecipientInfoIDKEK
:
446 encalgtag
= SECOID_GetAlgorithmTag(&(ri
->ri
.kekRecipientInfo
.keyEncAlg
));
453 SecCmsRecipientInfoWrapBulkKey(SecCmsRecipientInfoRef ri
, SecSymmetricKeyRef bulkkey
,
454 SECOidTag bulkalgtag
)
456 SecCertificateRef cert
;
457 SECOidTag certalgtag
;
458 OSStatus rv
= SECSuccess
;
460 CSSM_DATA_PTR params
= NULL
;
462 SecCmsRecipientEncryptedKey
*rek
;
463 SecCmsOriginatorIdentifierOrKey
*oiok
;
464 const SECAlgorithmID
*algid
;
466 SecCmsKeyTransRecipientInfoEx
*extra
= NULL
;
467 Boolean usesSubjKeyID
;
468 uint8 nullData
[2] = {SEC_ASN1_NULL
, 0};
470 SecCmsKeyAgreeRecipientInfo
*kari
;
472 poolp
= ri
->cmsg
->poolp
;
474 usesSubjKeyID
= nss_cmsrecipientinfo_usessubjectkeyid(ri
);
476 rv
= SecCertificateGetAlgorithmID(cert
,&algid
);
479 certalgtag
= SECOID_GetAlgorithmTag(algid
);
480 } else if (usesSubjKeyID
) {
481 extra
= &ri
->ri
.keyTransRecipientInfoEx
;
483 PORT_Assert(extra
->pubKey
);
484 if (!extra
->pubKey
) {
485 PORT_SetError(SEC_ERROR_INVALID_ARGS
);
488 #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR))
489 rv
= SecKeyGetAlgorithmID(extra
->pubKey
,&algid
);
491 /* TBD: Unify this code. Currently, iOS has an incompatible
492 * SecKeyGetAlgorithmID implementation. */
497 certalgtag
= SECOID_GetAlgorithmTag(algid
);
499 PORT_SetError(SEC_ERROR_INVALID_ARGS
);
503 /* XXX set ri->recipientInfoType to the proper value here */
504 /* or should we look if it's been set already ? */
506 certalgtag
= SECOID_GetAlgorithmTag(algid
);
507 switch (certalgtag
) {
508 case SEC_OID_PKCS1_RSA_ENCRYPTION
:
509 /* wrap the symkey */
511 rv
= SecCmsUtilEncryptSymKeyRSA(poolp
, cert
, bulkkey
,
512 &ri
->ri
.keyTransRecipientInfo
.encKey
);
513 if (rv
!= SECSuccess
)
515 } else if (usesSubjKeyID
) {
516 PORT_Assert(extra
!= NULL
);
517 rv
= SecCmsUtilEncryptSymKeyRSAPubKey(poolp
, extra
->pubKey
,
518 bulkkey
, &ri
->ri
.keyTransRecipientInfo
.encKey
);
519 if (rv
!= SECSuccess
)
523 rv
= SECOID_SetAlgorithmID(poolp
, &(ri
->ri
.keyTransRecipientInfo
.keyEncAlg
), certalgtag
, NULL
);
526 case SEC_OID_MISSI_KEA_DSS_OLD
:
527 case SEC_OID_MISSI_KEA_DSS
:
528 case SEC_OID_MISSI_KEA
:
529 rv
= SecCmsUtilEncryptSymKeyMISSI(poolp
, cert
, bulkkey
,
531 &ri
->ri
.keyTransRecipientInfo
.encKey
,
532 ¶ms
, ri
->cmsg
->pwfn_arg
);
533 if (rv
!= SECSuccess
)
536 /* here, we DO need to pass the params to the wrap function because, with
537 * RSA, there is no funny stuff going on with generation of IV vectors or so */
538 rv
= SECOID_SetAlgorithmID(poolp
, &(ri
->ri
.keyTransRecipientInfo
.keyEncAlg
), certalgtag
, params
);
540 case SEC_OID_X942_DIFFIE_HELMAN_KEY
: /* dh-public-number */
541 rek
= ri
->ri
.keyAgreeRecipientInfo
.recipientEncryptedKeys
[0];
547 oiok
= &(ri
->ri
.keyAgreeRecipientInfo
.originatorIdentifierOrKey
);
548 PORT_Assert(oiok
->identifierType
== SecCmsOriginatorIDOrKeyOriginatorPublicKey
);
550 /* see RFC2630 12.3.1.1 */
551 if (SECOID_SetAlgorithmID(poolp
, &oiok
->id
.originatorPublicKey
.algorithmIdentifier
,
552 SEC_OID_X942_DIFFIE_HELMAN_KEY
, NULL
) != SECSuccess
) {
557 /* this will generate a key pair, compute the shared secret, */
558 /* derive a key and ukm for the keyEncAlg out of it, encrypt the bulk key with */
559 /* the keyEncAlg, set encKey, keyEncAlg, publicKey etc. */
560 rv
= SecCmsUtilEncryptSymKeyESDH(poolp
, cert
, bulkkey
,
562 &ri
->ri
.keyAgreeRecipientInfo
.ukm
,
563 &ri
->ri
.keyAgreeRecipientInfo
.keyEncAlg
,
564 &oiok
->id
.originatorPublicKey
.publicKey
);
569 case SEC_OID_EC_PUBLIC_KEY
:
570 /* These were set up in nss_cmsrecipientinfo_create() */
571 kari
= &ri
->ri
.keyAgreeRecipientInfo
;
572 rek
= kari
->recipientEncryptedKeys
[0];
578 oiok
= &(kari
->originatorIdentifierOrKey
);
579 PORT_Assert(oiok
->identifierType
== SecCmsOriginatorIDOrKeyOriginatorPublicKey
);
582 * RFC 3278 3.1.1 says this AlgId must contain NULL params which is contrary to
583 * any other use of the SEC_OID_EC_PUBLIC_KEY OID. So we provide one
584 * explicitly instead of mucking up the login in SECOID_SetAlgorithmID().
586 nullItem
.Data
= nullData
;
588 if (SECOID_SetAlgorithmID(poolp
, &oiok
->id
.originatorPublicKey
.algorithmIdentifier
,
589 SEC_OID_EC_PUBLIC_KEY
, &nullItem
) != SECSuccess
) {
594 /* this will generate a key pair, compute the shared secret, */
595 /* derive a key and ukm for the keyEncAlg out of it, encrypt the bulk key with */
596 /* the keyEncAlg, set encKey, keyEncAlg, publicKey etc. */
597 rv
= SecCmsUtilEncryptSymKeyECDH(poolp
, cert
, bulkkey
,
601 &oiok
->id
.originatorPublicKey
.publicKey
);
602 /* this is a BIT STRING */
603 oiok
->id
.originatorPublicKey
.publicKey
.Length
<<= 3;
607 /* other algorithms not supported yet */
608 /* NOTE that we do not support any KEK algorithm */
609 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM
);
615 SECKEY_DestroySubjectPublicKeyInfo(freeSpki
);
622 #define dprintf(args...)
624 #define dprintf(args...) fprintf(stderr, args)
628 SecCmsRecipientInfoUnwrapBulkKey(SecCmsRecipientInfoRef ri
, int subIndex
,
629 SecCertificateRef cert
, SecPrivateKeyRef privkey
, SECOidTag bulkalgtag
)
631 SecSymmetricKeyRef bulkkey
= NULL
;
632 SECAlgorithmID
*encalg
;
634 CSSM_DATA_PTR enckey
;
637 ri
->cert
= CERT_DupCertificate(cert
);
638 /* mark the recipientInfo so we can find it later */
640 switch (ri
->recipientInfoType
) {
641 case SecCmsRecipientInfoIDKeyTrans
:
642 encalg
= &(ri
->ri
.keyTransRecipientInfo
.keyEncAlg
);
643 encalgtag
= SECOID_GetAlgorithmTag(&(ri
->ri
.keyTransRecipientInfo
.keyEncAlg
));
644 enckey
= &(ri
->ri
.keyTransRecipientInfo
.encKey
); /* ignore subIndex */
646 case SEC_OID_PKCS1_RSA_ENCRYPTION
:
647 /* RSA encryption algorithm: */
648 /* get the symmetric (bulk) key by unwrapping it using our private key */
649 bulkkey
= SecCmsUtilDecryptSymKeyRSA(privkey
, enckey
, bulkalgtag
);
652 case SEC_OID_NETSCAPE_SMIME_KEA
:
653 /* FORTEZZA key exchange algorithm */
654 /* the supplemental data is in the parameters of encalg */
655 bulkkey
= SecCmsUtilDecryptSymKeyMISSI(privkey
, enckey
, encalg
, bulkalgtag
, ri
->cmsg
->pwfn_arg
);
659 error
= SEC_ERROR_UNSUPPORTED_KEYALG
;
663 case SecCmsRecipientInfoIDKeyAgree
:
664 encalg
= &(ri
->ri
.keyAgreeRecipientInfo
.keyEncAlg
);
665 encalgtag
= SECOID_GetAlgorithmTag(&(ri
->ri
.keyAgreeRecipientInfo
.keyEncAlg
));
666 enckey
= &(ri
->ri
.keyAgreeRecipientInfo
.recipientEncryptedKeys
[subIndex
]->encKey
);
668 case SEC_OID_X942_DIFFIE_HELMAN_KEY
:
669 /* Diffie-Helman key exchange */
670 /* XXX not yet implemented */
671 /* XXX problem: SEC_OID_X942_DIFFIE_HELMAN_KEY points to a PKCS3 mechanism! */
672 /* we support ephemeral-static DH only, so if the recipientinfo */
673 /* has originator stuff in it, we punt (or do we? shouldn't be that hard...) */
674 /* first, we derive the KEK (a symkey!) using a Derive operation, then we get the */
675 /* content encryption key using a Unwrap op */
676 /* the derive operation has to generate the key using the algorithm in RFC2631 */
677 error
= SEC_ERROR_UNSUPPORTED_KEYALG
;
679 case SEC_OID_DH_SINGLE_STD_SHA1KDF
:
681 /* ephemeral-static ECDH */
682 SecCmsKeyAgreeRecipientInfo
*kari
= &ri
->ri
.keyAgreeRecipientInfo
;
683 SecCmsOriginatorIdentifierOrKey
*oiok
= &kari
->originatorIdentifierOrKey
;
684 if(oiok
->identifierType
!= SecCmsOriginatorIDOrKeyOriginatorPublicKey
) {
685 dprintf("SEC_OID_EC_PUBLIC_KEY unwrap key: bad oiok.id\n");
688 SecCmsOriginatorPublicKey
*opk
= &oiok
->id
.originatorPublicKey
;
689 /* FIXME - verify opk->algorithmIdentifier here? */
690 CSSM_DATA senderPubKey
= opk
->publicKey
;
691 /* Bit string, convert here */
692 senderPubKey
.Length
= (senderPubKey
.Length
+ 7) >> 3;
693 CSSM_DATA_PTR ukm
= &kari
->ukm
;
694 bulkkey
= SecCmsUtilDecryptSymKeyECDH(privkey
, enckey
, ukm
, encalg
, bulkalgtag
, &senderPubKey
);
698 error
= SEC_ERROR_UNSUPPORTED_KEYALG
;
702 case SecCmsRecipientInfoIDKEK
:
703 encalg
= &(ri
->ri
.kekRecipientInfo
.keyEncAlg
);
704 encalgtag
= SECOID_GetAlgorithmTag(&(ri
->ri
.kekRecipientInfo
.keyEncAlg
));
705 enckey
= &(ri
->ri
.kekRecipientInfo
.encKey
);
706 /* not supported yet */
707 error
= SEC_ERROR_UNSUPPORTED_KEYALG
;
711 /* XXXX continue here */