Skip to content

Commit 7526030

Browse files
committed
Refactoring in CMS
1 parent ca7c312 commit 7526030

3 files changed

Lines changed: 45 additions & 52 deletions

File tree

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -253,52 +253,59 @@ protected Key extractSecretKey(AlgorithmIdentifier keyEncryptionAlgorithm, Algor
253253
{
254254
try
255255
{
256-
AlgorithmIdentifier wrapAlg =
257-
AlgorithmIdentifier.getInstance(keyEncryptionAlgorithm.getParameters());
256+
AlgorithmIdentifier wrapAlgID = AlgorithmIdentifier.getInstance(keyEncryptionAlgorithm.getParameters());
257+
ASN1ObjectIdentifier wrapAlgOID = wrapAlgID.getAlgorithm();
258258

259259
X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(senderKey.getEncoded());
260260
KeyFactory fact = helper.createKeyFactory(senderKey.getAlgorithm().getAlgorithm());
261261
PublicKey senderPublicKey = fact.generatePublic(pubSpec);
262262

263263
try
264264
{
265-
SecretKey agreedWrapKey = calculateAgreedWrapKey(keyEncryptionAlgorithm, wrapAlg,
266-
senderPublicKey, userKeyingMaterial, recipientKey, ecc_cms_Generator);
265+
SecretKey agreedWrapKey = calculateAgreedWrapKey(keyEncryptionAlgorithm, wrapAlgID, senderPublicKey,
266+
userKeyingMaterial, recipientKey, ecc_cms_Generator);
267267

268-
if (wrapAlg.getAlgorithm().equals(CryptoProObjectIdentifiers.id_Gost28147_89_None_KeyWrap)
269-
|| wrapAlg.getAlgorithm().equals(CryptoProObjectIdentifiers.id_Gost28147_89_CryptoPro_KeyWrap))
268+
if (CryptoProObjectIdentifiers.id_Gost28147_89_None_KeyWrap.equals(wrapAlgOID) ||
269+
CryptoProObjectIdentifiers.id_Gost28147_89_CryptoPro_KeyWrap.equals(wrapAlgOID))
270270
{
271271
Gost2814789EncryptedKey encKey = Gost2814789EncryptedKey.getInstance(encryptedContentEncryptionKey);
272-
Gost2814789KeyWrapParameters wrapParams = Gost2814789KeyWrapParameters.getInstance(wrapAlg.getParameters());
272+
Gost2814789KeyWrapParameters wrapParams = Gost2814789KeyWrapParameters.getInstance(
273+
wrapAlgID.getParameters());
273274

274-
Cipher keyCipher = helper.createCipher(wrapAlg.getAlgorithm());
275+
Cipher keyCipher = helper.createCipher(wrapAlgOID);
275276

276-
keyCipher.init(Cipher.UNWRAP_MODE, agreedWrapKey, new GOST28147WrapParameterSpec(wrapParams.getEncryptionParamSet(), userKeyingMaterial.getOctets()));
277+
keyCipher.init(Cipher.UNWRAP_MODE, agreedWrapKey,
278+
new GOST28147WrapParameterSpec(wrapParams.getEncryptionParamSet(), userKeyingMaterial.getOctets()));
277279

278-
return keyCipher.unwrap(Arrays.concatenate(encKey.getEncryptedKey(), encKey.getMacKey()), helper.getBaseCipherName(contentEncryptionAlgorithm.getAlgorithm()), Cipher.SECRET_KEY);
280+
byte[] wrappedKey = Arrays.concatenate(encKey.getEncryptedKey(), encKey.getMacKey());
281+
return keyCipher.unwrap(wrappedKey, helper.getBaseCipherName(contentEncryptionAlgorithm.getAlgorithm()),
282+
Cipher.SECRET_KEY);
279283
}
280284

281-
return unwrapSessionKey(wrapAlg.getAlgorithm(), agreedWrapKey, contentEncryptionAlgorithm.getAlgorithm(), encryptedContentEncryptionKey);
285+
return unwrapSessionKey(wrapAlgOID, agreedWrapKey, contentEncryptionAlgorithm.getAlgorithm(),
286+
encryptedContentEncryptionKey);
282287
}
283288
catch (InvalidKeyException e)
284289
{
285290
// might be a pre-RFC 5753 message
286291
if (possibleOldMessages.contains(keyEncryptionAlgorithm.getAlgorithm()))
287292
{
288-
SecretKey agreedWrapKey = calculateAgreedWrapKey(keyEncryptionAlgorithm, wrapAlg,
293+
SecretKey agreedWrapKey = calculateAgreedWrapKey(keyEncryptionAlgorithm, wrapAlgID,
289294
senderPublicKey, userKeyingMaterial, recipientKey, old_ecc_cms_Generator);
290295

291-
return unwrapSessionKey(wrapAlg.getAlgorithm(), agreedWrapKey, contentEncryptionAlgorithm.getAlgorithm(), encryptedContentEncryptionKey);
296+
return unwrapSessionKey(wrapAlgOID, agreedWrapKey, contentEncryptionAlgorithm.getAlgorithm(),
297+
encryptedContentEncryptionKey);
292298
}
293299
// one last try - people do actually do this it turns out
294300
if (userKeyingMaterial != null)
295301
{
296302
try
297303
{
298-
SecretKey agreedWrapKey = calculateAgreedWrapKey(keyEncryptionAlgorithm, wrapAlg,
304+
SecretKey agreedWrapKey = calculateAgreedWrapKey(keyEncryptionAlgorithm, wrapAlgID,
299305
senderPublicKey, userKeyingMaterial, recipientKey, simple_ecc_cmsGenerator);
300306

301-
return unwrapSessionKey(wrapAlg.getAlgorithm(), agreedWrapKey, contentEncryptionAlgorithm.getAlgorithm(), encryptedContentEncryptionKey);
307+
return unwrapSessionKey(wrapAlgOID, agreedWrapKey, contentEncryptionAlgorithm.getAlgorithm(),
308+
encryptedContentEncryptionKey);
302309
}
303310
catch (InvalidKeyException ex)
304311
{

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,19 @@ public JceKeyAgreeRecipientInfoGenerator addRecipient(byte[] subjectKeyID, Publi
146146
return this;
147147
}
148148

149-
public ASN1Sequence generateRecipientEncryptedKeys(AlgorithmIdentifier keyAgreeAlgorithm, AlgorithmIdentifier keyEncryptionAlgorithm, GenericKey contentEncryptionKey)
150-
throws CMSException
149+
public ASN1Sequence generateRecipientEncryptedKeys(AlgorithmIdentifier keyAgreeAlgorithm,
150+
AlgorithmIdentifier keyEncryptionAlgorithm, GenericKey contentEncryptionKey) throws CMSException
151151
{
152152
if (recipientIDs.isEmpty())
153153
{
154154
throw new CMSException("No recipients associated with generator - use addRecipient()");
155155
}
156156

157-
init(keyAgreeAlgorithm.getAlgorithm());
157+
ASN1ObjectIdentifier keyAgreementOID = keyAgreeAlgorithm.getAlgorithm();
158158

159-
PrivateKey senderPrivateKey = this.senderPrivateKey;
159+
init(keyAgreementOID);
160160

161-
ASN1ObjectIdentifier keyAgreementOID = keyAgreeAlgorithm.getAlgorithm();
161+
PrivateKey senderPrivateKey = this.senderPrivateKey;
162162

163163
ASN1EncodableVector recipientEncryptedKeys = new ASN1EncodableVector();
164164
for (int i = 0; i != recipientIDs.size(); i++)
@@ -169,15 +169,16 @@ public ASN1Sequence generateRecipientEncryptedKeys(AlgorithmIdentifier keyAgreeA
169169
try
170170
{
171171
AlgorithmParameterSpec agreementParamSpec;
172-
ASN1ObjectIdentifier keyEncAlg = keyEncryptionAlgorithm.getAlgorithm();
172+
ASN1ObjectIdentifier keyEncryptionOID = keyEncryptionAlgorithm.getAlgorithm();
173173

174174
if (CMSUtils.isMQV(keyAgreementOID))
175175
{
176176
agreementParamSpec = new MQVParameterSpec(ephemeralKP, recipientPublicKey, userKeyingMaterial);
177177
}
178178
else if (CMSUtils.isEC(keyAgreementOID))
179179
{
180-
byte[] ukmKeyingMaterial = ecc_cms_Generator.generateKDFMaterial(keyEncryptionAlgorithm, keySizeProvider.getKeySize(keyEncAlg), userKeyingMaterial);
180+
byte[] ukmKeyingMaterial = ecc_cms_Generator.generateKDFMaterial(keyEncryptionAlgorithm,
181+
keySizeProvider.getKeySize(keyEncryptionOID), userKeyingMaterial);
181182

182183
agreementParamSpec = new UserKeyingMaterialSpec(ukmKeyingMaterial);
183184
}
@@ -217,37 +218,36 @@ else if (CMSUtils.isGOST(keyAgreementOID))
217218
keyAgreement.init(senderPrivateKey, agreementParamSpec, random);
218219
keyAgreement.doPhase(recipientPublicKey, true);
219220

220-
SecretKey keyEncryptionKey = keyAgreement.generateSecret(keyEncAlg.getId());
221+
SecretKey keyEncryptionKey = keyAgreement.generateSecret(keyEncryptionOID.getId());
221222

222223
EnvelopedDataHelper keyWrapHelper = (wrappingHelper != null) ? wrappingHelper : helper;
223224

224225
// Wrap the content encryption key with the agreement key
225-
Cipher keyEncryptionCipher = keyWrapHelper.createCipher(keyEncAlg);
226-
ASN1OctetString encryptedKey;
226+
Cipher keyEncryptionCipher = keyWrapHelper.createCipher(keyEncryptionOID);
227227

228-
if (keyEncAlg.equals(CryptoProObjectIdentifiers.id_Gost28147_89_None_KeyWrap)
229-
|| keyEncAlg.equals(CryptoProObjectIdentifiers.id_Gost28147_89_CryptoPro_KeyWrap))
228+
byte[] encryptedKeyOctets;
229+
if (CryptoProObjectIdentifiers.id_Gost28147_89_None_KeyWrap.equals(keyEncryptionOID) ||
230+
CryptoProObjectIdentifiers.id_Gost28147_89_CryptoPro_KeyWrap.equals(keyEncryptionOID))
230231
{
231-
keyEncryptionCipher.init(Cipher.WRAP_MODE, keyEncryptionKey, new GOST28147WrapParameterSpec(CryptoProObjectIdentifiers.id_Gost28147_89_CryptoPro_A_ParamSet, userKeyingMaterial));
232+
keyEncryptionCipher.init(Cipher.WRAP_MODE, keyEncryptionKey,
233+
new GOST28147WrapParameterSpec(CryptoProObjectIdentifiers.id_Gost28147_89_CryptoPro_A_ParamSet, userKeyingMaterial));
232234

233235
byte[] encKeyBytes = keyEncryptionCipher.wrap(keyWrapHelper.getJceKey(contentEncryptionKey));
234236

235237
Gost2814789EncryptedKey encKey = new Gost2814789EncryptedKey(
236238
Arrays.copyOfRange(encKeyBytes, 0, encKeyBytes.length - 4),
237239
Arrays.copyOfRange(encKeyBytes, encKeyBytes.length - 4, encKeyBytes.length));
238240

239-
encryptedKey = new DEROctetString(encKey.getEncoded(ASN1Encoding.DER));
241+
encryptedKeyOctets = encKey.getEncoded(ASN1Encoding.DER);
240242
}
241243
else
242244
{
243245
keyEncryptionCipher.init(Cipher.WRAP_MODE, keyEncryptionKey, random);
244246

245-
byte[] encryptedKeyBytes = keyEncryptionCipher.wrap(keyWrapHelper.getJceKey(contentEncryptionKey));
246-
247-
encryptedKey = new DEROctetString(encryptedKeyBytes);
247+
encryptedKeyOctets = keyEncryptionCipher.wrap(keyWrapHelper.getJceKey(contentEncryptionKey));
248248
}
249249

250-
recipientEncryptedKeys.add(new RecipientEncryptedKey(karId, encryptedKey));
250+
recipientEncryptedKeys.add(new RecipientEncryptedKey(karId, new DEROctetString(encryptedKeyOctets)));
251251
}
252252
catch (GeneralSecurityException e)
253253
{
@@ -274,7 +274,7 @@ protected byte[] getUserKeyingMaterial(AlgorithmIdentifier keyAgreeAlg)
274274

275275
try
276276
{
277-
ASN1OctetString addedukm = DEROctetString.withContentsOptional(userKeyingMaterial);
277+
ASN1OctetString addedukm = DEROctetString.fromContentsOptional(userKeyingMaterial);
278278

279279
return new MQVuserKeyingMaterial(originatorPublicKey, addedukm).getEncoded();
280280
}

util/src/main/java/org/bouncycastle/asn1/cms/OriginatorIdentifierOrKey.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -154,30 +154,16 @@ public IssuerAndSerialNumber getIssuerAndSerialNumber()
154154

155155
public SubjectKeyIdentifier getSubjectKeyIdentifier()
156156
{
157-
if (id instanceof ASN1TaggedObject)
158-
{
159-
ASN1TaggedObject taggedObject = (ASN1TaggedObject)id;
160-
if (taggedObject.hasContextTag(0))
161-
{
162-
return SubjectKeyIdentifier.getInstance(taggedObject, false);
163-
}
164-
}
157+
ASN1TaggedObject tag0 = ASN1TaggedObject.getContextOptional(id, 0);
165158

166-
return null;
159+
return tag0 == null ? null : SubjectKeyIdentifier.getInstance(tag0, false);
167160
}
168161

169162
public OriginatorPublicKey getOriginatorKey()
170163
{
171-
if (id instanceof ASN1TaggedObject)
172-
{
173-
ASN1TaggedObject taggedObject = (ASN1TaggedObject)id;
174-
if (taggedObject.hasContextTag(1))
175-
{
176-
return OriginatorPublicKey.getInstance(taggedObject, false);
177-
}
178-
}
164+
ASN1TaggedObject tag1 = ASN1TaggedObject.getContextOptional(id, 1);
179165

180-
return null;
166+
return tag1 == null ? null : OriginatorPublicKey.getInstance(tag1, false);
181167
}
182168

183169
/**

0 commit comments

Comments
 (0)