📄 x509v1certificategenerator.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: X509V1CertificateGenerator.java
package org.bouncycastle.jce;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.security.*;
import java.security.cert.X509Certificate;
import java.util.Date;
import java.util.Hashtable;
import org.bouncycastle.asn1.*;
import org.bouncycastle.asn1.x509.*;
import org.bouncycastle.jce.provider.X509CertificateObject;
public class X509V1CertificateGenerator
{
private V1TBSCertificateGenerator tbsGen;
private DERObjectIdentifier sigOID;
private AlgorithmIdentifier sigAlgId;
private String signatureAlgorithm;
private static Hashtable algorithms;
public X509V1CertificateGenerator()
{
tbsGen = new V1TBSCertificateGenerator();
}
public void reset()
{
tbsGen = new V1TBSCertificateGenerator();
}
public void setSerialNumber(BigInteger serialNumber)
{
tbsGen.setSerialNumber(new DERInteger(serialNumber));
}
public void setIssuerDN(X509Name issuer)
{
tbsGen.setIssuer(issuer);
}
public void setNotBefore(Date date)
{
tbsGen.setStartDate(new Time(date));
}
public void setNotAfter(Date date)
{
tbsGen.setEndDate(new Time(date));
}
public void setSubjectDN(X509Name subject)
{
tbsGen.setSubject(subject);
}
public void setPublicKey(PublicKey key)
{
try
{
tbsGen.setSubjectPublicKeyInfo(new SubjectPublicKeyInfo((ASN1Sequence)(new DERInputStream(new ByteArrayInputStream(key.getEncoded()))).readObject()));
}
catch (Exception e)
{
throw new IllegalArgumentException((new StringBuilder()).append("unable to process key - ").append(e.toString()).toString());
}
}
public void setSignatureAlgorithm(String signatureAlgorithm)
{
this.signatureAlgorithm = signatureAlgorithm;
sigOID = (DERObjectIdentifier)algorithms.get(signatureAlgorithm.toUpperCase());
if (sigOID == null)
{
throw new IllegalArgumentException("Unknown signature type requested");
} else
{
sigAlgId = new AlgorithmIdentifier(sigOID, new DERNull());
tbsGen.setSignature(sigAlgId);
return;
}
}
public X509Certificate generateX509Certificate(PrivateKey key)
throws SecurityException, SignatureException, InvalidKeyException
{
return generateX509Certificate(key, "BC", null);
NoSuchProviderException e;
e;
throw new SecurityException("BC provider not installed!");
}
public X509Certificate generateX509Certificate(PrivateKey key, SecureRandom random)
throws SecurityException, SignatureException, InvalidKeyException
{
return generateX509Certificate(key, "BC", random);
NoSuchProviderException e;
e;
throw new SecurityException("BC provider not installed!");
}
public X509Certificate generateX509Certificate(PrivateKey key, String provider)
throws NoSuchProviderException, SecurityException, SignatureException, InvalidKeyException
{
return generateX509Certificate(key, provider, null);
}
public X509Certificate generateX509Certificate(PrivateKey key, String provider, SecureRandom random)
throws NoSuchProviderException, SecurityException, SignatureException, InvalidKeyException
{
Signature sig = null;
try
{
sig = Signature.getInstance(sigOID.getId(), provider);
}
catch (NoSuchAlgorithmException ex)
{
try
{
sig = Signature.getInstance(signatureAlgorithm, provider);
}
catch (NoSuchAlgorithmException e)
{
throw new SecurityException((new StringBuilder()).append("exception creating signature: ").append(e.toString()).toString());
}
}
if (random != null)
sig.initSign(key, random);
else
sig.initSign(key);
org.bouncycastle.asn1.x509.TBSCertificateStructure tbsCert = tbsGen.generateTBSCertificate();
try
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
DEROutputStream dOut = new DEROutputStream(bOut);
dOut.writeObject(tbsCert);
sig.update(bOut.toByteArray());
}
catch (Exception e)
{
throw new SecurityException((new StringBuilder()).append("exception encoding TBS cert - ").append(e).toString());
}
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(tbsCert);
v.add(sigAlgId);
v.add(new DERBitString(sig.sign()));
return new X509CertificateObject(new X509CertificateStructure(new DERSequence(v)));
}
static
{
algorithms = new Hashtable();
algorithms.put("MD2WITHRSAENCRYPTION", new DERObjectIdentifier("1.2.840.113549.1.1.2"));
algorithms.put("MD2WITHRSA", new DERObjectIdentifier("1.2.840.113549.1.1.2"));
algorithms.put("MD5WITHRSAENCRYPTION", new DERObjectIdentifier("1.2.840.113549.1.1.4"));
algorithms.put("MD5WITHRSA", new DERObjectIdentifier("1.2.840.113549.1.1.4"));
algorithms.put("SHA1WITHRSAENCRYPTION", new DERObjectIdentifier("1.2.840.113549.1.1.5"));
algorithms.put("SHA1WITHRSA", new DERObjectIdentifier("1.2.840.113549.1.1.5"));
algorithms.put("RIPEMD160WITHRSAENCRYPTION", new DERObjectIdentifier("1.3.36.3.3.1.2"));
algorithms.put("RIPEMD160WITHRSA", new DERObjectIdentifier("1.3.36.3.3.1.2"));
algorithms.put("SHA1WITHDSA", new DERObjectIdentifier("1.2.840.10040.4.3"));
algorithms.put("DSAWITHSHA1", new DERObjectIdentifier("1.2.840.10040.4.3"));
algorithms.put("SHA1WITHECDSA", new DERObjectIdentifier("1.2.840.10045.4.1"));
algorithms.put("ECDSAWITHSHA1", new DERObjectIdentifier("1.2.840.10045.4.1"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -