Skip to content

Commit 90cee3c

Browse files
committed
Refactoring in CMS
1 parent 1bda45c commit 90cee3c

3 files changed

Lines changed: 38 additions & 57 deletions

File tree

pkix/src/main/java/org/bouncycastle/cms/CMSUtils.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,16 @@
6161

6262
class CMSUtils
6363
{
64-
private static final Set<String> des = new HashSet<String>();
64+
private static final Set desAlgs = new HashSet();
6565
private static final Set mqvAlgs = new HashSet();
6666
private static final Set ecAlgs = new HashSet();
6767
private static final Set gostAlgs = new HashSet();
6868

6969
static
7070
{
71-
des.add("DES");
72-
des.add("DESEDE");
73-
des.add(OIWObjectIdentifiers.desCBC.getId());
74-
des.add(PKCSObjectIdentifiers.des_EDE3_CBC.getId());
75-
des.add(PKCSObjectIdentifiers.id_alg_CMS3DESwrap.getId());
71+
desAlgs.add(OIWObjectIdentifiers.desCBC);
72+
desAlgs.add(PKCSObjectIdentifiers.des_EDE3_CBC);
73+
desAlgs.add(PKCSObjectIdentifiers.id_alg_CMS3DESwrap);
7674

7775
mqvAlgs.add(X9ObjectIdentifiers.mqvSinglePass_sha1kdf_scheme);
7876
mqvAlgs.add(SECObjectIdentifiers.mqvSinglePass_sha224kdf_scheme);
@@ -113,14 +111,13 @@ static boolean isGOST(ASN1ObjectIdentifier algorithm)
113111

114112
static boolean isRFC2631(ASN1ObjectIdentifier algorithm)
115113
{
116-
return algorithm.equals(PKCSObjectIdentifiers.id_alg_ESDH) || algorithm.equals(PKCSObjectIdentifiers.id_alg_SSDH);
114+
return PKCSObjectIdentifiers.id_alg_ESDH.equals(algorithm)
115+
|| PKCSObjectIdentifiers.id_alg_SSDH.equals(algorithm);
117116
}
118117

119-
static boolean isDES(String algorithmID)
118+
static boolean isDES(ASN1ObjectIdentifier algorithm)
120119
{
121-
String name = Strings.toUpperCase(algorithmID);
122-
123-
return des.contains(name);
120+
return desAlgs.contains(algorithm);
124121
}
125122

126123
static boolean isEquivalent(AlgorithmIdentifier algId1, AlgorithmIdentifier algId2)
Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.bouncycastle.cms;
22

3+
import org.bouncycastle.asn1.ASN1Encodable;
34
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
5+
import org.bouncycastle.asn1.ASN1OctetString;
46
import org.bouncycastle.asn1.ASN1Sequence;
57
import org.bouncycastle.asn1.DERNull;
68
import org.bouncycastle.asn1.DEROctetString;
@@ -18,64 +20,50 @@
1820
public 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+
}

pkix/src/main/java/org/bouncycastle/cms/jcajce/JceKeyAgreeRecipientInfoGenerator.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,18 +269,14 @@ protected byte[] getUserKeyingMaterial(AlgorithmIdentifier keyAgreeAlg)
269269

270270
if (ephemeralKP != null)
271271
{
272-
OriginatorPublicKey originatorPublicKey = createOriginatorPublicKey(SubjectPublicKeyInfo.getInstance(ephemeralKP.getPublic().getEncoded()));
272+
OriginatorPublicKey originatorPublicKey = createOriginatorPublicKey(
273+
SubjectPublicKeyInfo.getInstance(ephemeralKP.getPublic().getEncoded()));
273274

274275
try
275276
{
276-
if (userKeyingMaterial != null)
277-
{
278-
return new MQVuserKeyingMaterial(originatorPublicKey, new DEROctetString(userKeyingMaterial)).getEncoded();
279-
}
280-
else
281-
{
282-
return new MQVuserKeyingMaterial(originatorPublicKey, null).getEncoded();
283-
}
277+
ASN1OctetString addedukm = DEROctetString.withContentsOptional(userKeyingMaterial);
278+
279+
return new MQVuserKeyingMaterial(originatorPublicKey, addedukm).getEncoded();
284280
}
285281
catch (IOException e)
286282
{

0 commit comments

Comments
 (0)