11package org .bouncycastle .cms ;
22
3+ import org .bouncycastle .asn1 .ASN1Encodable ;
34import org .bouncycastle .asn1 .ASN1ObjectIdentifier ;
5+ import org .bouncycastle .asn1 .ASN1OctetString ;
46import org .bouncycastle .asn1 .ASN1Sequence ;
57import org .bouncycastle .asn1 .DERNull ;
68import org .bouncycastle .asn1 .DEROctetString ;
1820public abstract class KeyAgreeRecipientInfoGenerator
1921 implements RecipientInfoGenerator
2022{
21- private ASN1ObjectIdentifier keyAgreementOID ;
22- private ASN1ObjectIdentifier keyEncryptionOID ;
23- private SubjectPublicKeyInfo originatorKeyInfo ;
23+ private final ASN1ObjectIdentifier keyAgreementOID ;
24+ private final ASN1ObjectIdentifier keyEncryptionOID ;
25+ private final SubjectPublicKeyInfo originatorKeyInfo ;
2426
25- protected KeyAgreeRecipientInfoGenerator (ASN1ObjectIdentifier keyAgreementOID , SubjectPublicKeyInfo originatorKeyInfo , ASN1ObjectIdentifier keyEncryptionOID )
27+ protected KeyAgreeRecipientInfoGenerator (ASN1ObjectIdentifier keyAgreementOID ,
28+ SubjectPublicKeyInfo originatorKeyInfo , ASN1ObjectIdentifier keyEncryptionOID )
2629 {
2730 this .originatorKeyInfo = originatorKeyInfo ;
2831 this .keyAgreementOID = keyAgreementOID ;
2932 this .keyEncryptionOID = keyEncryptionOID ;
3033 }
3134
32- public RecipientInfo generate (GenericKey contentEncryptionKey )
33- throws CMSException
35+ public RecipientInfo generate (GenericKey contentEncryptionKey ) throws CMSException
3436 {
35- OriginatorIdentifierOrKey originator = new OriginatorIdentifierOrKey (
36- createOriginatorPublicKey ( originatorKeyInfo ) );
37+ OriginatorPublicKey originatorPublicKey = createOriginatorPublicKey ( originatorKeyInfo );
38+ OriginatorIdentifierOrKey originator = new OriginatorIdentifierOrKey ( originatorPublicKey );
3739
38- AlgorithmIdentifier keyEncAlg ;
39- if (CMSUtils .isDES (keyEncryptionOID . getId ()) || keyEncryptionOID . equals ( PKCSObjectIdentifiers .id_alg_CMSRC2wrap ))
40+ ASN1Encodable keyEncAlgParams = null ;
41+ if (CMSUtils .isDES (keyEncryptionOID ) || PKCSObjectIdentifiers .id_alg_CMSRC2wrap . equals ( keyEncryptionOID ))
4042 {
41- keyEncAlg = new AlgorithmIdentifier ( keyEncryptionOID , DERNull .INSTANCE ) ;
43+ keyEncAlgParams = DERNull .INSTANCE ;
4244 }
4345 else if (CMSUtils .isGOST (keyAgreementOID ))
4446 {
45- keyEncAlg = new AlgorithmIdentifier (keyEncryptionOID , new Gost2814789KeyWrapParameters (CryptoProObjectIdentifiers .id_Gost28147_89_CryptoPro_A_ParamSet ));
46- }
47- else
48- {
49- keyEncAlg = new AlgorithmIdentifier (keyEncryptionOID );
47+ keyEncAlgParams = new Gost2814789KeyWrapParameters (CryptoProObjectIdentifiers .id_Gost28147_89_CryptoPro_A_ParamSet );
5048 }
5149
52- AlgorithmIdentifier keyAgreeAlg = new AlgorithmIdentifier (keyAgreementOID , keyEncAlg );
50+ AlgorithmIdentifier keyEncAlgorithm = new AlgorithmIdentifier (keyEncryptionOID , keyEncAlgParams );
51+ AlgorithmIdentifier keyAgreeAlgorithm = new AlgorithmIdentifier (keyAgreementOID , keyEncAlgorithm );
5352
54- ASN1Sequence recipients = generateRecipientEncryptedKeys (keyAgreeAlg , keyEncAlg , contentEncryptionKey );
55- byte [] userKeyingMaterial = getUserKeyingMaterial (keyAgreeAlg );
53+ ASN1Sequence recipients = generateRecipientEncryptedKeys (keyAgreeAlgorithm , keyEncAlgorithm , contentEncryptionKey );
5654
57- if (userKeyingMaterial != null )
58- {
59- return new RecipientInfo (new KeyAgreeRecipientInfo (originator , new DEROctetString (userKeyingMaterial ),
60- keyAgreeAlg , recipients ));
61- }
62- else
63- {
64- return new RecipientInfo (new KeyAgreeRecipientInfo (originator , null , keyAgreeAlg , recipients ));
65- }
55+ ASN1OctetString ukm = DEROctetString .fromContentsOptional (getUserKeyingMaterial (keyAgreeAlgorithm ));
56+
57+ return new RecipientInfo (new KeyAgreeRecipientInfo (originator , ukm , keyAgreeAlgorithm , recipients ));
6658 }
6759
6860 protected OriginatorPublicKey createOriginatorPublicKey (SubjectPublicKeyInfo originatorKeyInfo )
6961 {
70- return new OriginatorPublicKey (
71- originatorKeyInfo .getAlgorithm (),
72- originatorKeyInfo .getPublicKeyData ().getBytes ());
62+ return new OriginatorPublicKey (originatorKeyInfo .getAlgorithm (), originatorKeyInfo .getPublicKeyData ());
7363 }
7464
75- protected abstract ASN1Sequence generateRecipientEncryptedKeys (AlgorithmIdentifier keyAgreeAlgorithm , AlgorithmIdentifier keyEncAlgorithm , GenericKey contentEncryptionKey )
76- throws CMSException ;
77-
78- protected abstract byte [] getUserKeyingMaterial (AlgorithmIdentifier keyAgreeAlgorithm )
79- throws CMSException ;
65+ protected abstract ASN1Sequence generateRecipientEncryptedKeys (AlgorithmIdentifier keyAgreeAlgorithm ,
66+ AlgorithmIdentifier keyEncAlgorithm , GenericKey contentEncryptionKey ) throws CMSException ;
8067
81- }
68+ protected abstract byte [] getUserKeyingMaterial (AlgorithmIdentifier keyAgreeAlgorithm ) throws CMSException ;
69+ }
0 commit comments