Skip to content

Commit 68b0feb

Browse files
committed
dealt with explicit naming for OEAP for cross-provider support - relates to github #953
1 parent d858403 commit 68b0feb

File tree

6 files changed

+72
-5
lines changed

6 files changed

+72
-5
lines changed

pkix/src/main/java/org/bouncycastle/operator/jcajce/JceAsymmetricKeyUnwrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public GenericKey generateUnwrappedKey(AlgorithmIdentifier encryptedKeyAlgorithm
9797
{
9898
Key sKey = null;
9999

100-
Cipher keyCipher = helper.createAsymmetricWrapper(this.getAlgorithmIdentifier().getAlgorithm(), extraMappings);
100+
Cipher keyCipher = helper.createAsymmetricWrapper(this.getAlgorithmIdentifier(), extraMappings);
101101
AlgorithmParameters algParams = helper.createAlgorithmParameters(this.getAlgorithmIdentifier());
102102

103103
try

pkix/src/main/java/org/bouncycastle/operator/jcajce/JceAsymmetricKeyWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public byte[] generateWrappedKey(GenericKey encryptionKey)
235235
}
236236
else
237237
{
238-
Cipher keyEncryptionCipher = helper.createAsymmetricWrapper(getAlgorithmIdentifier().getAlgorithm(), extraMappings);
238+
Cipher keyEncryptionCipher = helper.createAsymmetricWrapper(getAlgorithmIdentifier(), extraMappings);
239239
AlgorithmParameters algParams = null;
240240

241241
try

pkix/src/main/java/org/bouncycastle/operator/jcajce/JceKTSKeyUnwrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public GenericKey generateUnwrappedKey(AlgorithmIdentifier encryptedKeyAlgorithm
5757
throws OperatorException
5858
{
5959
GenericHybridParameters params = GenericHybridParameters.getInstance(this.getAlgorithmIdentifier().getParameters());
60-
Cipher keyCipher = helper.createAsymmetricWrapper(this.getAlgorithmIdentifier().getAlgorithm(), extraMappings);
60+
Cipher keyCipher = helper.createAsymmetricWrapper(this.getAlgorithmIdentifier(), extraMappings);
6161
String symmetricWrappingAlg = helper.getWrappingAlgorithmName(params.getDem().getAlgorithm());
6262
RsaKemParameters kemParameters = RsaKemParameters.getInstance(params.getKem().getParameters());
6363
int keySizeInBits = kemParameters.getKeyLength().intValue() * 8;

pkix/src/main/java/org/bouncycastle/operator/jcajce/JceKTSKeyWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public JceKTSKeyWrapper setSecureRandom(SecureRandom random)
7777
public byte[] generateWrappedKey(GenericKey encryptionKey)
7878
throws OperatorException
7979
{
80-
Cipher keyEncryptionCipher = helper.createAsymmetricWrapper(getAlgorithmIdentifier().getAlgorithm(), new HashMap());
80+
Cipher keyEncryptionCipher = helper.createAsymmetricWrapper(getAlgorithmIdentifier(), new HashMap());
8181

8282
try
8383
{

pkix/src/main/java/org/bouncycastle/operator/jcajce/OperatorHelper.java

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@
2323
import javax.crypto.Cipher;
2424
import javax.crypto.KeyAgreement;
2525

26+
import org.bouncycastle.asn1.ASN1Encodable;
2627
import org.bouncycastle.asn1.ASN1Integer;
2728
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
2829
import org.bouncycastle.asn1.ASN1Sequence;
30+
import org.bouncycastle.asn1.DERNull;
31+
import org.bouncycastle.asn1.DERSequence;
2932
import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
3033
import org.bouncycastle.asn1.kisa.KISAObjectIdentifiers;
3134
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
3235
import org.bouncycastle.asn1.ntt.NTTObjectIdentifiers;
3336
import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
3437
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
38+
import org.bouncycastle.asn1.pkcs.RSAESOAEPparams;
3539
import org.bouncycastle.asn1.pkcs.RSASSAPSSparams;
3640
import org.bouncycastle.asn1.teletrust.TeleTrusTObjectIdentifiers;
3741
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
@@ -55,6 +59,10 @@ class OperatorHelper
5559

5660
private static DefaultSignatureNameFinder sigFinder = new DefaultSignatureNameFinder();
5761

62+
private static final RSAESOAEPparams oaepParams_sha256 = calculateDefForDigest(NISTObjectIdentifiers.id_sha256);
63+
private static final RSAESOAEPparams oaepParams_sha384 = calculateDefForDigest(NISTObjectIdentifiers.id_sha384);
64+
private static final RSAESOAEPparams oaepParams_sha512 = calculateDefForDigest(NISTObjectIdentifiers.id_sha512);
65+
5866
static
5967
{
6068
oids.put(OIWObjectIdentifiers.idSHA1, "SHA1");
@@ -101,6 +109,17 @@ class OperatorHelper
101109
symmetricKeyAlgNames.put(PKCSObjectIdentifiers.RC2_CBC, "RC2");
102110
}
103111

112+
private static RSAESOAEPparams calculateDefForDigest(ASN1ObjectIdentifier digest)
113+
{
114+
AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier(
115+
digest,
116+
DERNull.INSTANCE);
117+
AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier(
118+
PKCSObjectIdentifiers.id_mgf1,
119+
new AlgorithmIdentifier(digest, DERNull.INSTANCE));
120+
return new RSAESOAEPparams(hashAlgorithm, maskGenAlgorithm, RSAESOAEPparams.DEFAULT_P_SOURCE_ALGORITHM);
121+
}
122+
104123
private JcaJceHelper helper;
105124

106125
OperatorHelper(JcaJceHelper helper)
@@ -185,9 +204,10 @@ KeyAgreement createKeyAgreement(ASN1ObjectIdentifier algorithm)
185204
}
186205
}
187206

188-
Cipher createAsymmetricWrapper(ASN1ObjectIdentifier algorithm, Map extraAlgNames)
207+
Cipher createAsymmetricWrapper(AlgorithmIdentifier algorithmID, Map extraAlgNames)
189208
throws OperatorCreationException
190209
{
210+
ASN1ObjectIdentifier algorithm = algorithmID.getAlgorithm();
191211
try
192212
{
193213
String cipherName = null;
@@ -200,6 +220,35 @@ Cipher createAsymmetricWrapper(ASN1ObjectIdentifier algorithm, Map extraAlgNames
200220
if (cipherName == null)
201221
{
202222
cipherName = (String)asymmetricWrapperAlgNames.get(algorithm);
223+
if (cipherName.indexOf("OAEPPadding") > 0)
224+
{
225+
ASN1Encodable params = algorithmID.getParameters().toASN1Primitive();
226+
if ((params instanceof ASN1Sequence))
227+
{
228+
ASN1Sequence paramSeq = ASN1Sequence.getInstance(params);
229+
if (paramSeq.size() == 0)
230+
{
231+
cipherName = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding";
232+
}
233+
else if (paramSeq.size() >= 2)
234+
{
235+
// we only check the first 2 as pSource may be different
236+
paramSeq = new DERSequence(new ASN1Encodable[]{ paramSeq.getObjectAt(0), paramSeq.getObjectAt(1) });
237+
if (oaepParams_sha256.equals(paramSeq))
238+
{
239+
cipherName = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding";
240+
}
241+
else if (oaepParams_sha512.equals(paramSeq))
242+
{
243+
cipherName = "RSA/ECB/OAEPWithSHA-512AndMGF1Padding";
244+
}
245+
else if (oaepParams_sha384.equals(paramSeq))
246+
{
247+
cipherName = "RSA/ECB/OAEPWithSHA-384AndMGF1Padding";
248+
}
249+
}
250+
}
251+
}
203252
}
204253

205254
if (cipherName != null)
@@ -223,6 +272,18 @@ Cipher createAsymmetricWrapper(ASN1ObjectIdentifier algorithm, Map extraAlgNames
223272
// Ignore
224273
}
225274
}
275+
else if (cipherName.indexOf("ECB/OAEPWith") > 0)
276+
{
277+
int start = cipherName.indexOf("ECB");
278+
try
279+
{
280+
return helper.createCipher(cipherName.substring(0, start) + "NONE" + cipherName.substring(start + 3));
281+
}
282+
catch (NoSuchAlgorithmException ex)
283+
{
284+
// Ignore
285+
}
286+
}
226287
// Ignore
227288
}
228289
}

pkix/src/test/java/org/bouncycastle/cms/test/NewEnvelopedDataTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,12 @@ public void testKeyTransOAEPSHA256()
11131113
doTestKeyTransOAEPDefaultNamed("SHA-256");
11141114
}
11151115

1116+
public void testKeyTransOAEPSHA384()
1117+
throws Exception
1118+
{
1119+
doTestKeyTransOAEPDefaultNamed("SHA-384");
1120+
}
1121+
11161122
public void testKeyTransOAEPSHA1AndSHA256()
11171123
throws Exception
11181124
{

0 commit comments

Comments
 (0)