Skip to content

Commit fbfff8e

Browse files
committed
Refactor PEMParser
1 parent 1f8265a commit fbfff8e

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

pkix/src/main/java/org/bouncycastle/openssl/PEMParser.java

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.Set;
1111
import java.util.StringTokenizer;
1212

13-
import org.bouncycastle.asn1.ASN1InputStream;
13+
import org.bouncycastle.asn1.ASN1BitString;
1414
import org.bouncycastle.asn1.ASN1Integer;
1515
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
1616
import org.bouncycastle.asn1.ASN1Primitive;
@@ -110,22 +110,19 @@ public Object readObject()
110110
throws IOException
111111
{
112112
PemObject obj = readPemObject();
113+
if (obj == null)
114+
{
115+
return null;
116+
}
113117

114-
if (obj != null)
118+
String type = obj.getType();
119+
Object pemObjectParser = parsers.get(type);
120+
if (pemObjectParser == null)
115121
{
116-
String type = obj.getType();
117-
Object pemObjectParser = parsers.get(type);
118-
if (pemObjectParser != null)
119-
{
120-
return ((PemObjectParser)pemObjectParser).parseObject(obj);
121-
}
122-
else
123-
{
124-
throw new IOException("unrecognised object: " + type);
125-
}
122+
throw new IOException("unrecognised object: " + type);
126123
}
127124

128-
return null;
125+
return ((PemObjectParser)pemObjectParser).parseObject(obj);
129126
}
130127

131128
/**
@@ -268,16 +265,14 @@ public PEMKeyPair parse(byte[] encoding)
268265
pKey.getParametersObject());
269266
PrivateKeyInfo privInfo = new PrivateKeyInfo(algId, pKey);
270267

271-
if (pKey.getPublicKey() != null)
268+
ASN1BitString publicKey = pKey.getPublicKey();
269+
SubjectPublicKeyInfo pubInfo = null;
270+
if (publicKey != null)
272271
{
273-
SubjectPublicKeyInfo pubInfo = new SubjectPublicKeyInfo(algId, pKey.getPublicKey().getBytes());
274-
275-
return new PEMKeyPair(pubInfo, privInfo);
276-
}
277-
else
278-
{
279-
return new PEMKeyPair(null, privInfo);
272+
pubInfo = new SubjectPublicKeyInfo(algId, publicKey.getBytes());
280273
}
274+
275+
return new PEMKeyPair(pubInfo, privInfo);
281276
}
282277
catch (IOException e)
283278
{
@@ -353,9 +348,10 @@ public Object parseObject(PemObject obj)
353348
{
354349
try
355350
{
351+
AlgorithmIdentifier algId = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE);
356352
RSAPublicKey rsaPubStructure = RSAPublicKey.getInstance(obj.getContent());
357353

358-
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), rsaPubStructure);
354+
return new SubjectPublicKeyInfo(algId, rsaPubStructure);
359355
}
360356
catch (IOException e)
361357
{
@@ -475,9 +471,7 @@ public Object parseObject(PemObject obj)
475471
{
476472
try
477473
{
478-
ASN1InputStream aIn = new ASN1InputStream(obj.getContent());
479-
480-
return ContentInfo.getInstance(aIn.readObject());
474+
return ContentInfo.getInstance(obj.getContent());
481475
}
482476
catch (Exception e)
483477
{
@@ -508,7 +502,7 @@ public Object parseObject(PemObject obj)
508502

509503
if (param instanceof ASN1ObjectIdentifier)
510504
{
511-
return ASN1Primitive.fromByteArray(obj.getContent());
505+
return param;
512506
}
513507
else if (param instanceof ASN1Sequence)
514508
{

pkix/src/test/java/org/bouncycastle/openssl/test/ParserTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ public void performTest()
301301
{
302302
if (privInfo instanceof PrivateKeyInfo)
303303
{
304-
privKey = (RSAPrivateCrtKey)converter.getPrivateKey(PrivateKeyInfo.getInstance(privInfo));
304+
privKey = (RSAPrivateCrtKey)converter.getPrivateKey((PrivateKeyInfo)privInfo);
305305
}
306306
else
307307
{

0 commit comments

Comments
 (0)