📄 pemreader.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: PEMReader.java
package org.bouncycastle.openssl;
import java.io.*;
import java.security.*;
import java.security.cert.*;
import java.security.spec.*;
import java.util.StringTokenizer;
import org.bouncycastle.asn1.*;
import org.bouncycastle.asn1.cms.ContentInfo;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.sec.ECPrivateKeyStructure;
import org.bouncycastle.asn1.x509.*;
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.PKCS10CertificationRequest;
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.util.encoders.Hex;
import org.bouncycastle.x509.X509AttributeCertificate;
import org.bouncycastle.x509.X509V2AttributeCertificate;
// Referenced classes of package org.bouncycastle.openssl:
// PEMUtilities, PasswordFinder
public class PEMReader extends BufferedReader
{
private final PasswordFinder pFinder;
private final String provider;
public PEMReader(Reader reader)
{
this(reader, null, "BC");
}
public PEMReader(Reader reader, PasswordFinder pFinder)
{
this(reader, pFinder, "BC");
}
public PEMReader(Reader reader, PasswordFinder pFinder, String provider)
{
super(reader);
this.pFinder = pFinder;
this.provider = provider;
}
public Object readObject()
throws IOException
{
_L2:
String line;
if ((line = readLine()) == null)
break; /* Loop/switch isn't completed */
if (line.indexOf("-----BEGIN PUBLIC KEY") != -1)
return readPublicKey("-----END PUBLIC KEY");
if (line.indexOf("-----BEGIN RSA PUBLIC KEY") != -1)
return readRSAPublicKey("-----END RSA PUBLIC KEY");
if (line.indexOf("-----BEGIN CERTIFICATE REQUEST") != -1)
return readCertificateRequest("-----END CERTIFICATE REQUEST");
if (line.indexOf("-----BEGIN NEW CERTIFICATE REQUEST") != -1)
return readCertificateRequest("-----END NEW CERTIFICATE REQUEST");
if (line.indexOf("-----BEGIN CERTIFICATE") != -1)
return readCertificate("-----END CERTIFICATE");
if (line.indexOf("-----BEGIN PKCS7") != -1)
return readPKCS7("-----END PKCS7");
if (line.indexOf("-----BEGIN X509 CERTIFICATE") != -1)
return readCertificate("-----END X509 CERTIFICATE");
if (line.indexOf("-----BEGIN X509 CRL") != -1)
return readCRL("-----END X509 CRL");
if (line.indexOf("-----BEGIN ATTRIBUTE CERTIFICATE") != -1)
return readAttributeCertificate("-----END ATTRIBUTE CERTIFICATE");
if (line.indexOf("-----BEGIN RSA PRIVATE KEY") == -1)
break MISSING_BLOCK_LABEL_212;
return readKeyPair("RSA", "-----END RSA PRIVATE KEY");
Exception e;
e;
throw new IOException((new StringBuilder()).append("problem creating RSA private key: ").append(e.toString()).toString());
if (line.indexOf("-----BEGIN DSA PRIVATE KEY") == -1)
break MISSING_BLOCK_LABEL_262;
return readKeyPair("DSA", "-----END DSA PRIVATE KEY");
e;
throw new IOException((new StringBuilder()).append("problem creating DSA private key: ").append(e.toString()).toString());
if (line.indexOf("-----BEGIN EC PARAMETERS-----") != -1)
return readECParameters("-----END EC PARAMETERS-----");
if (line.indexOf("-----BEGIN EC PRIVATE KEY-----") != -1)
return readECPrivateKey("-----END EC PRIVATE KEY-----");
if (true) goto _L2; else goto _L1
_L1:
return null;
}
private byte[] readBytes(String endMarker)
throws IOException
{
StringBuffer buf = new StringBuffer();
String line;
for (; (line = readLine()) != null && line.indexOf(endMarker) == -1; buf.append(line.trim()));
if (line == null)
throw new IOException((new StringBuilder()).append(endMarker).append(" not found").toString());
else
return Base64.decode(buf.toString());
}
private PublicKey readRSAPublicKey(String endMarker)
throws IOException
{
RSAPublicKeySpec keySpec;
ByteArrayInputStream bAIS = new ByteArrayInputStream(readBytes(endMarker));
ASN1InputStream ais = new ASN1InputStream(bAIS);
Object asnObject = ais.readObject();
ASN1Sequence sequence = (ASN1Sequence)asnObject;
RSAPublicKeyStructure rsaPubStructure = new RSAPublicKeyStructure(sequence);
keySpec = new RSAPublicKeySpec(rsaPubStructure.getModulus(), rsaPubStructure.getPublicExponent());
KeyFactory keyFact = KeyFactory.getInstance("RSA", provider);
return keyFact.generatePublic(keySpec);
NoSuchProviderException e;
e;
throw new IOException((new StringBuilder()).append("can't find provider ").append(provider).toString());
e;
throw new IOException((new StringBuilder()).append("problem extracting key: ").append(e.toString()).toString());
}
private PublicKey readPublicKey(String endMarker)
throws IOException
{
java.security.spec.KeySpec keySpec;
String algorithms[];
int i;
keySpec = new X509EncodedKeySpec(readBytes(endMarker));
algorithms = (new String[] {
"DSA", "RSA"
});
i = 0;
_L3:
if (i >= algorithms.length) goto _L2; else goto _L1
_L1:
PublicKey pubKey;
KeyFactory keyFact = KeyFactory.getInstance(algorithms[i], provider);
pubKey = keyFact.generatePublic(keySpec);
return pubKey;
NoSuchAlgorithmException e;
e;
continue; /* Loop/switch isn't completed */
e;
continue; /* Loop/switch isn't completed */
e;
throw new RuntimeException((new StringBuilder()).append("can't find provider ").append(provider).toString());
i++;
goto _L3
_L2:
return null;
}
private X509Certificate readCertificate(String endMarker)
throws IOException
{
ByteArrayInputStream bIn = new ByteArrayInputStream(readBytes(endMarker));
CertificateFactory certFact = CertificateFactory.getInstance("X.509", provider);
return (X509Certificate)certFact.generateCertificate(bIn);
Exception e;
e;
throw new IOException((new StringBuilder()).append("problem parsing cert: ").append(e.toString()).toString());
}
private X509CRL readCRL(String endMarker)
throws IOException
{
ByteArrayInputStream bIn = new ByteArrayInputStream(readBytes(endMarker));
CertificateFactory certFact = CertificateFactory.getInstance("X.509", provider);
return (X509CRL)certFact.generateCRL(bIn);
Exception e;
e;
throw new IOException((new StringBuilder()).append("problem parsing cert: ").append(e.toString()).toString());
}
private PKCS10CertificationRequest readCertificateRequest(String endMarker)
throws IOException
{
return new PKCS10CertificationRequest(readBytes(endMarker));
Exception e;
e;
throw new IOException((new StringBuilder()).append("problem parsing cert: ").append(e.toString()).toString());
}
private X509AttributeCertificate readAttributeCertificate(String endMarker)
throws IOException
{
return new X509V2AttributeCertificate(readBytes(endMarker));
}
private ContentInfo readPKCS7(String endMarker)
throws IOException
{
ByteArrayInputStream bIn;
StringBuffer buf = new StringBuffer();
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
String line;
for (; (line = readLine()) != null && line.indexOf(endMarker) == -1; buf.delete(0, (buf.length() / 4) * 4))
{
line = line.trim();
buf.append(line.trim());
Base64.decode(buf.substring(0, (buf.length() / 4) * 4), bOut);
}
if (buf.length() != 0)
throw new RuntimeException("base64 data appears to be truncated");
if (line == null)
throw new IOException((new StringBuilder()).append(endMarker).append(" not found").toString());
bIn = new ByteArrayInputStream(bOut.toByteArray());
ASN1InputStream aIn = new ASN1InputStream(bIn);
return ContentInfo.getInstance(aIn.readObject());
Exception e;
e;
throw new IOException((new StringBuilder()).append("problem parsing PKCS7 object: ").append(e.toString()).toString());
}
private KeyPair readKeyPair(String type, String endMarker)
throws Exception
{
boolean isEncrypted = false;
String line = null;
String dekInfo = null;
StringBuffer buf = new StringBuffer();
do
{
if ((line = readLine()) == null)
break;
if (line.startsWith("Proc-Type: 4,ENCRYPTED"))
{
isEncrypted = true;
continue;
}
if (line.startsWith("DEK-Info:"))
{
dekInfo = line.substring(10);
continue;
}
if (line.indexOf(endMarker) != -1)
break;
buf.append(line.trim());
} while (true);
byte keyBytes[] = Base64.decode(buf.toString());
if (isEncrypted)
{
if (pFinder == null)
throw new IOException("No password finder specified, but a password is required");
char password[] = pFinder.getPassword();
if (password == null)
throw new IOException("Password is null, but a password is required");
StringTokenizer tknz = new StringTokenizer(dekInfo, ",");
String dekAlgName = tknz.nextToken();
byte iv[] = Hex.decode(tknz.nextToken());
keyBytes = PEMUtilities.crypt(false, provider, keyBytes, password, dekAlgName, iv);
}
ByteArrayInputStream bIn = new ByteArrayInputStream(keyBytes);
ASN1InputStream aIn = new ASN1InputStream(bIn);
ASN1Sequence seq = (ASN1Sequence)aIn.readObject();
java.security.spec.KeySpec pubSpec;
java.security.spec.KeySpec privSpec;
if (type.equals("RSA"))
{
DERInteger mod = (DERInteger)seq.getObjectAt(1);
DERInteger pubExp = (DERInteger)seq.getObjectAt(2);
DERInteger privExp = (DERInteger)seq.getObjectAt(3);
DERInteger p1 = (DERInteger)seq.getObjectAt(4);
DERInteger p2 = (DERInteger)seq.getObjectAt(5);
DERInteger exp1 = (DERInteger)seq.getObjectAt(6);
DERInteger exp2 = (DERInteger)seq.getObjectAt(7);
DERInteger crtCoef = (DERInteger)seq.getObjectAt(8);
pubSpec = new RSAPublicKeySpec(mod.getValue(), pubExp.getValue());
privSpec = new RSAPrivateCrtKeySpec(mod.getValue(), pubExp.getValue(), privExp.getValue(), p1.getValue(), p2.getValue(), exp1.getValue(), exp2.getValue(), crtCoef.getValue());
} else
{
DERInteger p = (DERInteger)seq.getObjectAt(1);
DERInteger q = (DERInteger)seq.getObjectAt(2);
DERInteger g = (DERInteger)seq.getObjectAt(3);
DERInteger y = (DERInteger)seq.getObjectAt(4);
DERInteger x = (DERInteger)seq.getObjectAt(5);
privSpec = new DSAPrivateKeySpec(x.getValue(), p.getValue(), q.getValue(), g.getValue());
pubSpec = new DSAPublicKeySpec(y.getValue(), p.getValue(), q.getValue(), g.getValue());
}
KeyFactory fact = KeyFactory.getInstance(type, provider);
return new KeyPair(fact.generatePublic(pubSpec), fact.generatePrivate(privSpec));
}
private ECNamedCurveParameterSpec readECParameters(String endMarker)
throws IOException
{
DERObjectIdentifier oid = (DERObjectIdentifier)ASN1Object.fromByteArray(readBytes(endMarker));
return ECNamedCurveTable.getParameterSpec(oid.getId());
}
private KeyPair readECPrivateKey(String endMarker)
throws IOException
{
PKCS8EncodedKeySpec privSpec;
X509EncodedKeySpec pubSpec;
KeyFactory fact;
ECPrivateKeyStructure pKey = new ECPrivateKeyStructure((ASN1Sequence)ASN1Object.fromByteArray(readBytes(endMarker)));
AlgorithmIdentifier algId = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, pKey.getParameters());
PrivateKeyInfo privInfo = new PrivateKeyInfo(algId, pKey.getDERObject());
SubjectPublicKeyInfo pubInfo = new SubjectPublicKeyInfo(algId, pKey.getPublicKey().getBytes());
privSpec = new PKCS8EncodedKeySpec(privInfo.getEncoded());
pubSpec = new X509EncodedKeySpec(pubInfo.getEncoded());
fact = KeyFactory.getInstance("ECDSA", provider);
return new KeyPair(fact.generatePublic(pubSpec), fact.generatePrivate(privSpec));
ClassCastException e;
e;
throw new IOException("wrong ASN.1 object found in stream");
e;
throw new IOException((new StringBuilder()).append("problem parsing EC private key: ").append(e).toString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -