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.
41 #include "SecAsn1Item.h"
44 #include <security_asn1/secasn1.h>
45 #include <security_asn1/secerr.h>
46 #include <security_asn1/secport.h>
48 #include <Security/SecKeyPriv.h>
49 #include <Security/SecCertificatePriv.h>
50 #include <Security/SecCertificateInternal.h>
52 #include "SecCmsRecipientInfo.h"
55 nss_cmsrecipientinfo_usessubjectkeyid(SecCmsRecipientInfoRef ri
)
57 if (ri
->recipientInfoType
== SecCmsRecipientInfoIDKeyTrans
) {
58 SecCmsRecipientIdentifier
*rid
;
59 rid
= &ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
;
60 if (rid
->identifierType
== SecCmsRecipientIDSubjectKeyID
) {
68 static SecCmsRecipientInfoRef
69 nss_cmsrecipientinfo_create(SecCmsEnvelopedDataRef envd
, SecCmsRecipientIDSelector type
,
70 SecCertificateRef cert
, SecPublicKeyRef pubKey
,
71 const SecAsn1Item
*subjKeyID
)
73 SecCmsRecipientInfoRef ri
;
76 OSStatus rv
= SECSuccess
;
77 SecCmsRecipientEncryptedKey
*rek
;
78 SecCmsOriginatorIdentifierOrKey
*oiok
;
79 unsigned long version
;
82 const SECAlgorithmID
*algid
;
83 SECAlgorithmID freeAlgID
;
85 SecCmsRecipientIdentifier
*rid
;
87 poolp
= envd
->contentInfo
.cmsg
->poolp
;
89 mark
= PORT_ArenaMark(poolp
);
91 ri
= (SecCmsRecipientInfoRef
)PORT_ArenaZAlloc(poolp
, sizeof(SecCmsRecipientInfo
));
95 ri
->envelopedData
= envd
;
98 if (type
== SecCmsRecipientIDIssuerSN
)
100 rv
= SecCertificateGetAlgorithmID(cert
,&algid
);
103 rv
= SecKeyGetAlgorithmID(pubKey
,&algid
);
106 ri
->cert
= CERT_DupCertificate(cert
);
107 if (ri
->cert
== NULL
)
110 const SecAsn1AlgId
*length_data_swapped
= (const SecAsn1AlgId
*)SecCertificateGetPublicKeyAlgorithm(cert
);
111 freeAlgID
.algorithm
.Length
= (size_t)length_data_swapped
->algorithm
.Data
;
112 freeAlgID
.algorithm
.Data
= (uint8_t *)length_data_swapped
->algorithm
.Length
;
113 freeAlgID
.parameters
.Length
= (size_t)length_data_swapped
->parameters
.Data
;
114 freeAlgID
.parameters
.Data
= (uint8_t *)length_data_swapped
->parameters
.Length
;
118 certalgtag
= SECOID_GetAlgorithmTag(algid
);
120 rid
= &ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
;
121 switch (certalgtag
) {
122 case SEC_OID_PKCS1_RSA_ENCRYPTION
:
123 ri
->recipientInfoType
= SecCmsRecipientInfoIDKeyTrans
;
124 rid
->identifierType
= type
;
125 if (type
== SecCmsRecipientIDIssuerSN
) {
126 rid
->id
.issuerAndSN
= CERT_GetCertIssuerAndSN(poolp
, cert
);
127 if (rid
->id
.issuerAndSN
== NULL
) {
130 } else if (type
== SecCmsRecipientIDSubjectKeyID
){
132 rid
->id
.subjectKeyID
= PORT_ArenaNew(poolp
, SecAsn1Item
);
133 if (rid
->id
.subjectKeyID
== NULL
) {
135 PORT_SetError(SEC_ERROR_NO_MEMORY
);
138 if (SECITEM_CopyItem(poolp
, rid
->id
.subjectKeyID
, subjKeyID
)) {
140 PORT_SetError(SEC_ERROR_UNKNOWN_CERT
);
143 if (rid
->id
.subjectKeyID
->Data
== NULL
) {
145 PORT_SetError(SEC_ERROR_NO_MEMORY
);
149 PORT_SetError(SEC_ERROR_INVALID_ARGS
);
153 case SEC_OID_MISSI_KEA_DSS_OLD
:
154 case SEC_OID_MISSI_KEA_DSS
:
155 case SEC_OID_MISSI_KEA
:
156 PORT_Assert(type
!= SecCmsRecipientIDSubjectKeyID
);
157 if (type
== SecCmsRecipientIDSubjectKeyID
) {
161 /* backward compatibility - this is not really a keytrans operation */
162 ri
->recipientInfoType
= SecCmsRecipientInfoIDKeyTrans
;
163 /* hardcoded issuerSN choice for now */
164 ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
.identifierType
= SecCmsRecipientIDIssuerSN
;
165 ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
.id
.issuerAndSN
= CERT_GetCertIssuerAndSN(poolp
, cert
);
166 if (ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
.id
.issuerAndSN
== NULL
) {
171 case SEC_OID_X942_DIFFIE_HELMAN_KEY
: /* dh-public-number */
172 PORT_Assert(type
!= SecCmsRecipientIDSubjectKeyID
);
173 if (type
== SecCmsRecipientIDSubjectKeyID
) {
177 /* a key agreement op */
178 ri
->recipientInfoType
= SecCmsRecipientInfoIDKeyAgree
;
180 if (ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
.id
.issuerAndSN
== NULL
) {
184 /* we do not support the case where multiple recipients
185 * share the same KeyAgreeRecipientInfo and have multiple RecipientEncryptedKeys
186 * in this case, we would need to walk all the recipientInfos, take the
187 * ones that do KeyAgreement algorithms and join them, algorithm by algorithm
188 * Then, we'd generate ONE ukm and OriginatorIdentifierOrKey */
190 /* only epheremal-static Diffie-Hellman is supported for now
191 * this is the only form of key agreement that provides potential anonymity
192 * of the sender, plus we do not have to include certs in the message */
194 /* force single recipientEncryptedKey for now */
195 if ((rek
= SecCmsRecipientEncryptedKeyCreate(poolp
)) == NULL
) {
200 /* hardcoded IssuerSN choice for now */
201 rek
->recipientIdentifier
.identifierType
= SecCmsKeyAgreeRecipientIDIssuerSN
;
202 if ((rek
->recipientIdentifier
.id
.issuerAndSN
= CERT_GetCertIssuerAndSN(poolp
, cert
)) == NULL
) {
207 oiok
= &(ri
->ri
.keyAgreeRecipientInfo
.originatorIdentifierOrKey
);
209 /* see RFC2630 12.3.1.1 */
210 oiok
->identifierType
= SecCmsOriginatorIDOrKeyOriginatorPublicKey
;
212 rv
= SecCmsArrayAdd(poolp
, (void ***)&ri
->ri
.keyAgreeRecipientInfo
.recipientEncryptedKeys
,
216 case SEC_OID_EC_PUBLIC_KEY
:
217 /* ephemeral-static ECDH - issuerAndSN, OriginatorPublicKey only */
218 PORT_Assert(type
!= SecCmsRecipientIDSubjectKeyID
);
219 if (type
== SecCmsRecipientIDSubjectKeyID
) {
223 /* a key agreement op */
224 ri
->recipientInfoType
= SecCmsRecipientInfoIDKeyAgree
;
225 ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
.id
.issuerAndSN
= CERT_GetCertIssuerAndSN(poolp
, cert
);
226 if (ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
.id
.issuerAndSN
== NULL
) {
230 /* we do not support the case where multiple recipients
231 * share the same KeyAgreeRecipientInfo and have multiple RecipientEncryptedKeys
232 * in this case, we would need to walk all the recipientInfos, take the
233 * ones that do KeyAgreement algorithms and join them, algorithm by algorithm
234 * Then, we'd generate ONE ukm and OriginatorIdentifierOrKey */
236 /* force single recipientEncryptedKey for now */
237 if ((rek
= SecCmsRecipientEncryptedKeyCreate(poolp
)) == NULL
) {
242 /* hardcoded IssuerSN choice for now */
243 rek
->recipientIdentifier
.identifierType
= SecCmsKeyAgreeRecipientIDIssuerSN
;
244 if ((rek
->recipientIdentifier
.id
.issuerAndSN
= CERT_GetCertIssuerAndSN(poolp
, cert
)) == NULL
) {
249 oiok
= &(ri
->ri
.keyAgreeRecipientInfo
.originatorIdentifierOrKey
);
251 /* see RFC 3278 3.1.1 */
252 oiok
->identifierType
= SecCmsOriginatorIDOrKeyOriginatorPublicKey
;
254 rv
= SecCmsArrayAdd(poolp
, (void ***)&ri
->ri
.keyAgreeRecipientInfo
.recipientEncryptedKeys
,
259 /* other algorithms not supported yet */
260 /* NOTE that we do not support any KEK algorithm */
261 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM
);
266 if (rv
== SECFailure
)
270 switch (ri
->recipientInfoType
) {
271 case SecCmsRecipientInfoIDKeyTrans
:
272 if (ri
->ri
.keyTransRecipientInfo
.recipientIdentifier
.identifierType
== SecCmsRecipientIDIssuerSN
)
273 version
= SEC_CMS_KEYTRANS_RECIPIENT_INFO_VERSION_ISSUERSN
;
275 version
= SEC_CMS_KEYTRANS_RECIPIENT_INFO_VERSION_SUBJKEY
;
276 dummy
= SEC_ASN1EncodeInteger(poolp
, &(ri
->ri
.keyTransRecipientInfo
.version
), version
);
280 case SecCmsRecipientInfoIDKeyAgree
:
281 dummy
= SEC_ASN1EncodeInteger(poolp
, &(ri
->ri
.keyAgreeRecipientInfo
.version
),
282 SEC_CMS_KEYAGREE_RECIPIENT_INFO_VERSION
);
286 case SecCmsRecipientInfoIDKEK
:
287 /* NOTE: this cannot happen as long as we do not support any KEK algorithm */
288 dummy
= SEC_ASN1EncodeInteger(poolp
, &(ri
->ri
.kekRecipientInfo
.version
),
289 SEC_CMS_KEK_RECIPIENT_INFO_VERSION
);
296 if (SecCmsEnvelopedDataAddRecipient(envd
, ri
))
299 PORT_ArenaUnmark (poolp
, mark
);
302 SECKEY_DestroySubjectPublicKeyInfo(freeSpki
);
309 SECKEY_DestroySubjectPublicKeyInfo(freeSpki
);
311 PORT_ArenaRelease (poolp
, mark
);
316 * SecCmsRecipientInfoCreate - create a recipientinfo
318 * we currently do not create KeyAgreement recipientinfos with multiple
319 * recipientEncryptedKeys the certificate is supposed to have been
320 * verified by the caller
322 SecCmsRecipientInfoRef
323 SecCmsRecipientInfoCreate(SecCmsEnvelopedDataRef envd
, SecCertificateRef cert
)
325 /* TODO: We might want to prefer subjkeyid */
327 SecCmsRecipientInfoRef info
= SecCmsRecipientInfoCreateWithSubjKeyIDFromCert(envd
, cert
);
333 return nss_cmsrecipientinfo_create(envd
, SecCmsRecipientIDIssuerSN
, cert
,
337 SecCmsRecipientInfoRef
338 SecCmsRecipientInfoCreateWithSubjKeyID(SecCmsEnvelopedDataRef envd
,
339 const SecAsn1Item
* subjKeyID
,
340 SecPublicKeyRef pubKey
)
342 return nss_cmsrecipientinfo_create(envd
, SecCmsRecipientIDSubjectKeyID
,
343 NULL
, pubKey
, subjKeyID
);
346 SecCmsRecipientInfoRef
347 SecCmsRecipientInfoCreateWithSubjKeyIDFromCert(SecCmsEnvelopedDataRef envd
,
348 SecCertificateRef cert
)
350 SecPublicKeyRef pubKey
= NULL
;
351 SecAsn1Item subjKeyID
= {0, NULL
};
352 SecCmsRecipientInfoRef retVal
= NULL
;
353 CFDataRef subjectKeyIDData
= NULL
;
355 if (!envd
|| !cert
) {
358 subjectKeyIDData
= SecCertificateGetSubjectKeyID(cert
);
359 if (!subjectKeyIDData
)
362 CFDataGetLength(subjectKeyIDData
);
363 subjKeyID
.Data
= (uint8_t *)CFDataGetBytePtr(subjectKeyIDData
);
364 retVal
= nss_cmsrecipientinfo_create(envd
, SecCmsRecipientIDSubjectKeyID
,
365 cert
, pubKey
, &subjKeyID
);
373 SecCmsRecipientInfoDestroy(SecCmsRecipientInfoRef ri
)
375 /* version was allocated on the pool, so no need to destroy it */
376 /* issuerAndSN was allocated on the pool, so no need to destroy it */
377 if (ri
->cert
!= NULL
)
378 CERT_DestroyCertificate(ri
->cert
);
380 if (nss_cmsrecipientinfo_usessubjectkeyid(ri
)) {
381 SecCmsKeyTransRecipientInfoEx
*extra
;
382 extra
= &ri
->ri
.keyTransRecipientInfoEx
;
384 SECKEY_DestroyPublicKey(extra
->pubKey
);
387 /* recipientInfo structure itself was allocated on the pool, so no need to destroy it */
392 SecCmsRecipientInfoGetVersion(SecCmsRecipientInfoRef ri
)
394 unsigned long version
;
395 SecAsn1Item
* versionitem
= NULL
;
397 switch (ri
->recipientInfoType
) {
398 case SecCmsRecipientInfoIDKeyTrans
:
399 /* ignore subIndex */
400 versionitem
= &(ri
->ri
.keyTransRecipientInfo
.version
);
402 case SecCmsRecipientInfoIDKEK
:
403 /* ignore subIndex */
404 versionitem
= &(ri
->ri
.kekRecipientInfo
.version
);
406 case SecCmsRecipientInfoIDKeyAgree
:
407 versionitem
= &(ri
->ri
.keyAgreeRecipientInfo
.version
);
411 PORT_Assert(versionitem
);
412 if (versionitem
== NULL
)
415 /* always take apart the SecAsn1Item */
416 if (SEC_ASN1DecodeInteger(versionitem
, &version
) != SECSuccess
)
423 SecCmsRecipientInfoGetEncryptedKey(SecCmsRecipientInfoRef ri
, int subIndex
)
425 SecAsn1Item
* enckey
= NULL
;
427 switch (ri
->recipientInfoType
) {
428 case SecCmsRecipientInfoIDKeyTrans
:
429 /* ignore subIndex */
430 enckey
= &(ri
->ri
.keyTransRecipientInfo
.encKey
);
432 case SecCmsRecipientInfoIDKEK
:
433 /* ignore subIndex */
434 enckey
= &(ri
->ri
.kekRecipientInfo
.encKey
);
436 case SecCmsRecipientInfoIDKeyAgree
:
437 enckey
= &(ri
->ri
.keyAgreeRecipientInfo
.recipientEncryptedKeys
[subIndex
]->encKey
);
445 SecCmsRecipientInfoGetKeyEncryptionAlgorithmTag(SecCmsRecipientInfoRef ri
)
447 SECOidTag encalgtag
= SEC_OID_UNKNOWN
; /* an invalid encryption alg */
449 switch (ri
->recipientInfoType
) {
450 case SecCmsRecipientInfoIDKeyTrans
:
451 encalgtag
= SECOID_GetAlgorithmTag(&(ri
->ri
.keyTransRecipientInfo
.keyEncAlg
));
453 case SecCmsRecipientInfoIDKeyAgree
:
454 encalgtag
= SECOID_GetAlgorithmTag(&(ri
->ri
.keyAgreeRecipientInfo
.keyEncAlg
));
456 case SecCmsRecipientInfoIDKEK
:
457 encalgtag
= SECOID_GetAlgorithmTag(&(ri
->ri
.kekRecipientInfo
.keyEncAlg
));
464 SecCmsRecipientInfoWrapBulkKey(SecCmsRecipientInfoRef ri
, SecSymmetricKeyRef bulkkey
,
465 SECOidTag bulkalgtag
)
467 SecCertificateRef cert
;
468 SECOidTag certalgtag
;
469 OSStatus rv
= SECSuccess
;
471 SecAsn1Item
* params
= NULL
;
473 SecCmsRecipientEncryptedKey
*rek
;
474 SecCmsOriginatorIdentifierOrKey
*oiok
;
475 const SECAlgorithmID
*algid
;
476 SECAlgorithmID freeAlgID
;
478 Boolean usesSubjKeyID
;
479 uint8_t nullData
[2] = {SEC_ASN1_NULL
, 0};
481 SecCmsKeyAgreeRecipientInfo
*kari
;
483 SecCmsKeyTransRecipientInfoEx
*extra
= NULL
;
486 poolp
= ri
->envelopedData
->contentInfo
.cmsg
->poolp
;
488 usesSubjKeyID
= nss_cmsrecipientinfo_usessubjectkeyid(ri
);
491 rv
= SecCertificateGetAlgorithmID(cert
,&algid
);
495 const SecAsn1AlgId
*length_data_swapped
= (const SecAsn1AlgId
*)SecCertificateGetPublicKeyAlgorithm(cert
);
496 freeAlgID
.algorithm
.Length
= (size_t)length_data_swapped
->algorithm
.Data
;
497 freeAlgID
.algorithm
.Data
= (uint8_t *)length_data_swapped
->algorithm
.Length
;
498 freeAlgID
.parameters
.Length
= (size_t)length_data_swapped
->parameters
.Data
;
499 freeAlgID
.parameters
.Data
= (uint8_t *)length_data_swapped
->parameters
.Length
;
504 else if (usesSubjKeyID
) {
505 extra
= &ri
->ri
.keyTransRecipientInfoEx
;
507 PORT_Assert(extra
->pubKey
);
508 if (!extra
->pubKey
) {
509 PORT_SetError(SEC_ERROR_INVALID_ARGS
);
512 rv
= SecKeyGetAlgorithmID(extra
->pubKey
,&algid
);
516 certalgtag
= SECOID_GetAlgorithmTag(algid
);
520 PORT_SetError(SEC_ERROR_INVALID_ARGS
);
524 /* XXX set ri->recipientInfoType to the proper value here */
525 /* or should we look if it's been set already ? */
527 certalgtag
= SECOID_GetAlgorithmTag(algid
);
528 switch (certalgtag
) {
529 case SEC_OID_PKCS1_RSA_ENCRYPTION
:
530 /* wrap the symkey */
532 rv
= SecCmsUtilEncryptSymKeyRSA(poolp
, cert
, bulkkey
,
533 &ri
->ri
.keyTransRecipientInfo
.encKey
);
534 if (rv
!= SECSuccess
)
537 } else if (usesSubjKeyID
) {
538 PORT_Assert(extra
!= NULL
);
539 rv
= SecCmsUtilEncryptSymKeyRSAPubKey(poolp
, extra
->pubKey
,
540 bulkkey
, &ri
->ri
.keyTransRecipientInfo
.encKey
);
541 if (rv
!= SECSuccess
)
546 rv
= SECOID_SetAlgorithmID(poolp
, &(ri
->ri
.keyTransRecipientInfo
.keyEncAlg
), certalgtag
, NULL
);
549 case SEC_OID_MISSI_KEA_DSS_OLD
:
550 case SEC_OID_MISSI_KEA_DSS
:
551 case SEC_OID_MISSI_KEA
:
552 rv
= SecCmsUtilEncryptSymKeyMISSI(poolp
, cert
, bulkkey
,
554 &ri
->ri
.keyTransRecipientInfo
.encKey
,
555 ¶ms
, ri
->cmsg
->pwfn_arg
);
556 if (rv
!= SECSuccess
)
559 /* here, we DO need to pass the params to the wrap function because, with
560 * RSA, there is no funny stuff going on with generation of IV vectors or so */
561 rv
= SECOID_SetAlgorithmID(poolp
, &(ri
->ri
.keyTransRecipientInfo
.keyEncAlg
), certalgtag
, params
);
563 case SEC_OID_X942_DIFFIE_HELMAN_KEY
: /* dh-public-number */
564 rek
= ri
->ri
.keyAgreeRecipientInfo
.recipientEncryptedKeys
[0];
570 oiok
= &(ri
->ri
.keyAgreeRecipientInfo
.originatorIdentifierOrKey
);
571 PORT_Assert(oiok
->identifierType
== SecCmsOriginatorIDOrKeyOriginatorPublicKey
);
573 /* see RFC2630 12.3.1.1 */
574 if (SECOID_SetAlgorithmID(poolp
, &oiok
->id
.originatorPublicKey
.algorithmIdentifier
,
575 SEC_OID_X942_DIFFIE_HELMAN_KEY
, NULL
) != SECSuccess
) {
580 /* this will generate a key pair, compute the shared secret, */
581 /* derive a key and ukm for the keyEncAlg out of it, encrypt the bulk key with */
582 /* the keyEncAlg, set encKey, keyEncAlg, publicKey etc. */
583 rv
= SecCmsUtilEncryptSymKeyESDH(poolp
, cert
, bulkkey
,
585 &ri
->ri
.keyAgreeRecipientInfo
.ukm
,
586 &ri
->ri
.keyAgreeRecipientInfo
.keyEncAlg
,
587 &oiok
->id
.originatorPublicKey
.publicKey
);
591 case SEC_OID_EC_PUBLIC_KEY
:
592 /* These were set up in nss_cmsrecipientinfo_create() */
593 kari
= &ri
->ri
.keyAgreeRecipientInfo
;
594 rek
= kari
->recipientEncryptedKeys
[0];
600 oiok
= &(kari
->originatorIdentifierOrKey
);
601 PORT_Assert(oiok
->identifierType
== SecCmsOriginatorIDOrKeyOriginatorPublicKey
);
604 * RFC 3278 3.1.1 says this AlgId must contain NULL params which is contrary to
605 * any other use of the SEC_OID_EC_PUBLIC_KEY OID. So we provide one
606 * explicitly instead of mucking up the login in SECOID_SetAlgorithmID().
608 nullItem
.Data
= nullData
;
610 if (SECOID_SetAlgorithmID(poolp
, &oiok
->id
.originatorPublicKey
.algorithmIdentifier
,
611 SEC_OID_EC_PUBLIC_KEY
, &nullItem
) != SECSuccess
) {
615 /* this will generate a key pair, compute the shared secret, */
616 /* derive a key and ukm for the keyEncAlg out of it, encrypt the bulk key with */
617 /* the keyEncAlg, set encKey, keyEncAlg, publicKey etc. */
618 rv
= SecCmsUtilEncryptSymKeyECDH(poolp
, cert
, bulkkey
,
622 &oiok
->id
.originatorPublicKey
.publicKey
);
625 /* other algorithms not supported yet */
626 /* NOTE that we do not support any KEK algorithm */
627 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM
);
633 SECKEY_DestroySubjectPublicKeyInfo(freeSpki
);
640 #define dprintf(args...)
642 #define dprintf(args...) fprintf(stderr, args)
646 SecCmsRecipientInfoUnwrapBulkKey(SecCmsRecipientInfoRef ri
, int subIndex
,
647 SecCertificateRef cert
, SecPrivateKeyRef privkey
, SECOidTag bulkalgtag
)
649 SecSymmetricKeyRef bulkkey
= NULL
;
650 SECAlgorithmID
*encalg
;
652 SecAsn1Item
* enckey
;
655 ri
->cert
= CERT_DupCertificate(cert
);
656 /* mark the recipientInfo so we can find it later */
658 switch (ri
->recipientInfoType
) {
659 case SecCmsRecipientInfoIDKeyTrans
:
660 encalgtag
= SECOID_GetAlgorithmTag(&(ri
->ri
.keyTransRecipientInfo
.keyEncAlg
));
661 enckey
= &(ri
->ri
.keyTransRecipientInfo
.encKey
); /* ignore subIndex */
663 case SEC_OID_PKCS1_RSA_ENCRYPTION
:
664 /* RSA encryption algorithm: */
665 /* get the symmetric (bulk) key by unwrapping it using our private key */
666 bulkkey
= SecCmsUtilDecryptSymKeyRSA(privkey
, enckey
, bulkalgtag
);
669 case SEC_OID_NETSCAPE_SMIME_KEA
:
670 /* FORTEZZA key exchange algorithm */
671 /* the supplemental data is in the parameters of encalg */
672 encalg
= &(ri
->ri
.keyTransRecipientInfo
.keyEncAlg
);
673 bulkkey
= SecCmsUtilDecryptSymKeyMISSI(privkey
, enckey
, encalg
, bulkalgtag
, ri
->cmsg
->pwfn_arg
);
677 error
= SEC_ERROR_UNSUPPORTED_KEYALG
;
681 case SecCmsRecipientInfoIDKeyAgree
:
682 encalgtag
= SECOID_GetAlgorithmTag(&(ri
->ri
.keyAgreeRecipientInfo
.keyEncAlg
));
684 case SEC_OID_X942_DIFFIE_HELMAN_KEY
:
685 /* Diffie-Helman key exchange */
686 /* XXX not yet implemented */
687 /* XXX problem: SEC_OID_X942_DIFFIE_HELMAN_KEY points to a PKCS3 mechanism! */
688 /* we support ephemeral-static DH only, so if the recipientinfo */
689 /* has originator stuff in it, we punt (or do we? shouldn't be that hard...) */
690 /* first, we derive the KEK (a symkey!) using a Derive operation, then we get the */
691 /* content encryption key using a Unwrap op */
692 /* the derive operation has to generate the key using the algorithm in RFC2631 */
693 error
= SEC_ERROR_UNSUPPORTED_KEYALG
;
695 case SEC_OID_DH_SINGLE_STD_SHA1KDF
:
697 /* ephemeral-static ECDH */
698 enckey
= &(ri
->ri
.keyAgreeRecipientInfo
.recipientEncryptedKeys
[subIndex
]->encKey
);
699 encalg
= &(ri
->ri
.keyAgreeRecipientInfo
.keyEncAlg
);
700 SecCmsKeyAgreeRecipientInfo
*kari
= &ri
->ri
.keyAgreeRecipientInfo
;
701 SecCmsOriginatorIdentifierOrKey
*oiok
= &kari
->originatorIdentifierOrKey
;
702 if(oiok
->identifierType
!= SecCmsOriginatorIDOrKeyOriginatorPublicKey
) {
703 dprintf("SEC_OID_EC_PUBLIC_KEY unwrap key: bad oiok.id\n");
704 error
= SEC_ERROR_LIBRARY_FAILURE
;
707 SecCmsOriginatorPublicKey
*opk
= &oiok
->id
.originatorPublicKey
;
708 /* FIXME - verify opk->algorithmIdentifier here? */
709 SecAsn1Item senderPubKey
= opk
->publicKey
;
710 SecAsn1Item
*ukm
= &kari
->ukm
;
711 bulkkey
= SecCmsUtilDecryptSymKeyECDH(privkey
, enckey
, ukm
, encalg
, bulkalgtag
, &senderPubKey
);
715 error
= SEC_ERROR_UNSUPPORTED_KEYALG
;
719 case SecCmsRecipientInfoIDKEK
:
720 /* not supported yet */
721 error
= SEC_ERROR_UNSUPPORTED_KEYALG
;
725 /* XXXX continue here */
729 PORT_SetError(error
);