Skip to content

Commit

Permalink
Refactor PEMParser
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdettman committed May 7, 2024
1 parent 1f8265a commit fbfff8e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
46 changes: 20 additions & 26 deletions pkix/src/main/java/org/bouncycastle/openssl/PEMParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Set;
import java.util.StringTokenizer;

import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1BitString;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Primitive;
Expand Down Expand Up @@ -110,22 +110,19 @@ public Object readObject()
throws IOException
{
PemObject obj = readPemObject();
if (obj == null)
{
return null;
}

if (obj != null)
String type = obj.getType();
Object pemObjectParser = parsers.get(type);
if (pemObjectParser == null)
{
String type = obj.getType();
Object pemObjectParser = parsers.get(type);
if (pemObjectParser != null)
{
return ((PemObjectParser)pemObjectParser).parseObject(obj);
}
else
{
throw new IOException("unrecognised object: " + type);
}
throw new IOException("unrecognised object: " + type);
}

return null;
return ((PemObjectParser)pemObjectParser).parseObject(obj);
}

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

if (pKey.getPublicKey() != null)
ASN1BitString publicKey = pKey.getPublicKey();
SubjectPublicKeyInfo pubInfo = null;
if (publicKey != null)
{
SubjectPublicKeyInfo pubInfo = new SubjectPublicKeyInfo(algId, pKey.getPublicKey().getBytes());

return new PEMKeyPair(pubInfo, privInfo);
}
else
{
return new PEMKeyPair(null, privInfo);
pubInfo = new SubjectPublicKeyInfo(algId, publicKey.getBytes());
}

return new PEMKeyPair(pubInfo, privInfo);
}
catch (IOException e)
{
Expand Down Expand Up @@ -353,9 +348,10 @@ public Object parseObject(PemObject obj)
{
try
{
AlgorithmIdentifier algId = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE);
RSAPublicKey rsaPubStructure = RSAPublicKey.getInstance(obj.getContent());

return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), rsaPubStructure);
return new SubjectPublicKeyInfo(algId, rsaPubStructure);
}
catch (IOException e)
{
Expand Down Expand Up @@ -475,9 +471,7 @@ public Object parseObject(PemObject obj)
{
try
{
ASN1InputStream aIn = new ASN1InputStream(obj.getContent());

return ContentInfo.getInstance(aIn.readObject());
return ContentInfo.getInstance(obj.getContent());
}
catch (Exception e)
{
Expand Down Expand Up @@ -508,7 +502,7 @@ public Object parseObject(PemObject obj)

if (param instanceof ASN1ObjectIdentifier)
{
return ASN1Primitive.fromByteArray(obj.getContent());
return param;
}
else if (param instanceof ASN1Sequence)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public void performTest()
{
if (privInfo instanceof PrivateKeyInfo)
{
privKey = (RSAPrivateCrtKey)converter.getPrivateKey(PrivateKeyInfo.getInstance(privInfo));
privKey = (RSAPrivateCrtKey)converter.getPrivateKey((PrivateKeyInfo)privInfo);
}
else
{
Expand Down

0 comments on commit fbfff8e

Please sign in to comment.