Skip to content

Commit 6f53ba1

Browse files
committed
Fix NPE and add extra checks
- see #2166
1 parent 126ac9e commit 6f53ba1

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

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

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.bouncycastle.asn1.ASN1Encodable;
2727
import org.bouncycastle.asn1.ASN1Integer;
2828
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
29+
import org.bouncycastle.asn1.ASN1Primitive;
2930
import org.bouncycastle.asn1.ASN1Sequence;
3031
import org.bouncycastle.asn1.DERNull;
3132
import org.bouncycastle.asn1.DERSequence;
@@ -207,52 +208,62 @@ KeyAgreement createKeyAgreement(ASN1ObjectIdentifier algorithm)
207208
Cipher createAsymmetricWrapper(AlgorithmIdentifier algorithmID, Map extraAlgNames)
208209
throws OperatorCreationException
209210
{
210-
ASN1ObjectIdentifier algorithm = algorithmID.getAlgorithm();
211+
if (algorithmID == null)
212+
{
213+
throw new NullPointerException("'algorithmID' cannot be null");
214+
}
215+
216+
ASN1ObjectIdentifier algOID = algorithmID.getAlgorithm();
211217
try
212218
{
213219
String cipherName = null;
214220

215-
if (!extraAlgNames.isEmpty())
221+
if (extraAlgNames != null && !extraAlgNames.isEmpty())
216222
{
217-
cipherName = (String)extraAlgNames.get(algorithm);
223+
cipherName = (String)extraAlgNames.get(algOID);
218224
}
219225

220226
if (cipherName == null)
221227
{
222-
cipherName = (String)asymmetricWrapperAlgNames.get(algorithm);
228+
cipherName = (String)asymmetricWrapperAlgNames.get(algOID);
229+
}
230+
231+
if (cipherName != null)
232+
{
223233
if (cipherName.indexOf("OAEPPadding") > 0)
224234
{
225-
ASN1Encodable params = algorithmID.getParameters().toASN1Primitive();
226-
if ((params instanceof ASN1Sequence))
235+
ASN1Encodable algParams = algorithmID.getParameters();
236+
if (algParams != null)
227237
{
228-
ASN1Sequence paramSeq = ASN1Sequence.getInstance(params);
229-
if (paramSeq.size() == 0)
238+
ASN1Primitive primitive = algParams.toASN1Primitive();
239+
if ((primitive instanceof ASN1Sequence))
230240
{
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))
241+
ASN1Sequence oaepParams = (ASN1Sequence)primitive;
242+
if (oaepParams.size() == 0)
242243
{
243-
cipherName = "RSA/ECB/OAEPWithSHA-512AndMGF1Padding";
244+
cipherName = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding";
244245
}
245-
else if (oaepParams_sha384.equals(paramSeq))
246+
else if (oaepParams.size() >= 2)
246247
{
247-
cipherName = "RSA/ECB/OAEPWithSHA-384AndMGF1Padding";
248+
// we only check the first 2 as pSource may be different
249+
oaepParams = new DERSequence(new ASN1Encodable[]{ oaepParams.getObjectAt(0), oaepParams.getObjectAt(1) });
250+
if (oaepParams_sha256.equals(oaepParams))
251+
{
252+
cipherName = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding";
253+
}
254+
else if (oaepParams_sha512.equals(oaepParams))
255+
{
256+
cipherName = "RSA/ECB/OAEPWithSHA-512AndMGF1Padding";
257+
}
258+
else if (oaepParams_sha384.equals(oaepParams))
259+
{
260+
cipherName = "RSA/ECB/OAEPWithSHA-384AndMGF1Padding";
261+
}
248262
}
249263
}
250264
}
251265
}
252-
}
253266

254-
if (cipherName != null)
255-
{
256267
try
257268
{
258269
// this is reversed as the Sun policy files now allow unlimited strength RSA
@@ -288,7 +299,7 @@ else if (cipherName.indexOf("ECB/OAEPWith") > 0)
288299
}
289300
}
290301

291-
return helper.createCipher(algorithm.getId());
302+
return helper.createCipher(algOID.getId());
292303
}
293304
catch (GeneralSecurityException e)
294305
{

0 commit comments

Comments
 (0)