⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 extendedpkcs10certificationrequest.java

📁 JAVA做的J2EE下CA认证系统 基于EJB开发
💻 JAVA
字号:
package se.anatom.ejbca.common;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.security.InvalidKeyException;import java.security.KeyFactory;import java.security.NoSuchAlgorithmException;import java.security.NoSuchProviderException;import java.security.PrivateKey;import java.security.PublicKey;import java.security.Signature;import java.security.SignatureException;import java.security.spec.InvalidKeySpecException;import java.security.spec.X509EncodedKeySpec;import java.util.Hashtable;import javax.security.auth.x500.X500Principal;import org.bouncycastle.asn1.ASN1InputStream;import org.bouncycastle.asn1.ASN1Sequence;import org.bouncycastle.asn1.ASN1Set;import org.bouncycastle.asn1.DERBitString;import org.bouncycastle.asn1.DERObjectIdentifier;import org.bouncycastle.asn1.DEROutputStream;import org.bouncycastle.asn1.pkcs.CertificationRequest;import org.bouncycastle.asn1.pkcs.CertificationRequestInfo;import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;import org.bouncycastle.asn1.x509.AlgorithmIdentifier;import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;import org.bouncycastle.asn1.x509.X509Name;import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;import org.bouncycastle.jce.X509Principal;/** * Class copied from BC where RSASSA-PSS support is added. *  * @version $Id: ExtendedPKCS10CertificationRequest.java,v 1.1.2.2 2005/11/25 08:38:07 anatom Exp $ */public class ExtendedPKCS10CertificationRequest    extends CertificationRequest{    private static Hashtable            algorithms = new Hashtable();    private static Hashtable            oids = new Hashtable();        private static final String OID_RSASSA_PSS = "1.2.840.113549.1.1.10";    static    {        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("RSAWITHMD5", 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("SHA224WITHRSAENCRYPTION", PKCSObjectIdentifiers.sha224WithRSAEncryption);        algorithms.put("SHA224WITHRSA", PKCSObjectIdentifiers.sha224WithRSAEncryption);        algorithms.put("SHA256WITHRSAENCRYPTION", PKCSObjectIdentifiers.sha256WithRSAEncryption);        algorithms.put("SHA256WITHRSA", PKCSObjectIdentifiers.sha256WithRSAEncryption);        algorithms.put("SHA384WITHRSAENCRYPTION", PKCSObjectIdentifiers.sha384WithRSAEncryption);        algorithms.put("SHA384WITHRSA", PKCSObjectIdentifiers.sha384WithRSAEncryption);        algorithms.put("SHA512WITHRSAENCRYPTION", PKCSObjectIdentifiers.sha512WithRSAEncryption);        algorithms.put("SHA512WITHRSA", PKCSObjectIdentifiers.sha512WithRSAEncryption);        algorithms.put("RSAWITHSHA1", 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", X9ObjectIdentifiers.ecdsa_with_SHA1);        algorithms.put("ECDSAWITHSHA1", X9ObjectIdentifiers.ecdsa_with_SHA1);        //        // reverse mappings        //        oids.put(new DERObjectIdentifier("1.2.840.113549.1.1.5"), "SHA1WITHRSA");        oids.put(PKCSObjectIdentifiers.sha224WithRSAEncryption, "SHA224WITHRSA");        oids.put(PKCSObjectIdentifiers.sha256WithRSAEncryption, "SHA256WITHRSA");        oids.put(PKCSObjectIdentifiers.sha384WithRSAEncryption, "SHA384WITHRSA");        oids.put(PKCSObjectIdentifiers.sha512WithRSAEncryption, "SHA512WITHRSA");        oids.put(new DERObjectIdentifier("1.2.840.113549.1.1.4"), "MD5WITHRSA");        oids.put(new DERObjectIdentifier("1.2.840.113549.1.1.2"), "MD2WITHRSA");        oids.put(new DERObjectIdentifier("1.2.840.10040.4.3"), "DSAWITHSHA1");        oids.put(X9ObjectIdentifiers.ecdsa_with_SHA1, "DSAWITHSHA1");            }    private static ASN1Sequence toDERSequence(        byte[]  bytes)    {        try        {            ByteArrayInputStream    bIn = new ByteArrayInputStream(bytes);            ASN1InputStream         dIn = new ASN1InputStream(bIn);            return (ASN1Sequence)dIn.readObject();        }        catch (Exception e)        {            throw new IllegalArgumentException("badly encoded request");        }    }    /**     * construct a PKCS10 certification request from a DER encoded     * byte stream.     */    public ExtendedPKCS10CertificationRequest(        byte[]  bytes)    {        super(toDERSequence(bytes));    }    public ExtendedPKCS10CertificationRequest(        ASN1Sequence  sequence)    {        super(sequence);    }    /**     * create a PKCS10 certfication request using the BC provider.     */    public ExtendedPKCS10CertificationRequest(        String              signatureAlgorithm,        X509Name            subject,        PublicKey           key,        ASN1Set             attributes,        PrivateKey          signingKey)        throws NoSuchAlgorithmException, NoSuchProviderException,                InvalidKeyException, SignatureException    {        this(signatureAlgorithm, subject, key, attributes, signingKey, "BC");    }    private static X509Name convertName(        X500Principal	name)    {        try        {            return new X509Principal(name.getEncoded());        }        catch (IOException e)        {            throw new IllegalArgumentException("can't convert name");        }    }        /**     * create a PKCS10 certfication request using the BC provider.     */    public ExtendedPKCS10CertificationRequest(        String              signatureAlgorithm,        X500Principal       subject,        PublicKey           key,        ASN1Set             attributes,        PrivateKey          signingKey)        throws NoSuchAlgorithmException, NoSuchProviderException,                InvalidKeyException, SignatureException    {        this(signatureAlgorithm, convertName(subject), key, attributes, signingKey, "BC");    }        /**     * create a PKCS10 certfication request using the named provider.     */    public ExtendedPKCS10CertificationRequest(        String              signatureAlgorithm,        X500Principal       subject,        PublicKey           key,        ASN1Set             attributes,        PrivateKey          signingKey,        String              provider)        throws NoSuchAlgorithmException, NoSuchProviderException,                InvalidKeyException, SignatureException    {        this(signatureAlgorithm, convertName(subject), key, attributes, signingKey, provider);    }        /**     * create a PKCS10 certfication request using the named provider.     */    public ExtendedPKCS10CertificationRequest(        String              signatureAlgorithm,        X509Name            subject,        PublicKey           key,        ASN1Set             attributes,        PrivateKey          signingKey,        String              provider)        throws NoSuchAlgorithmException, NoSuchProviderException,                InvalidKeyException, SignatureException    {        DERObjectIdentifier sigOID = (DERObjectIdentifier)algorithms.get(signatureAlgorithm.toUpperCase());        if (sigOID == null)        {            throw new IllegalArgumentException("Unknown signature type requested");        }        if (subject == null)        {            throw new IllegalArgumentException("subject must not be null");        }        if (key == null)        {            throw new IllegalArgumentException("public key must not be null");        }        this.sigAlgId = new AlgorithmIdentifier(sigOID, null);        byte[]                  bytes = key.getEncoded();        ByteArrayInputStream    bIn = new ByteArrayInputStream(bytes);        ASN1InputStream         dIn = new ASN1InputStream(bIn);        try        {            this.reqInfo = new CertificationRequestInfo(subject, new SubjectPublicKeyInfo((ASN1Sequence)dIn.readObject()), attributes);        }        catch (IOException e)        {            throw new IllegalArgumentException("can't encode public key");        }        Signature sig = null;                try        {        	sig = Signature.getInstance(sigAlgId.getObjectId().getId(), provider);        }        catch (NoSuchAlgorithmException e)        {            sig = Signature.getInstance(signatureAlgorithm, provider);        }        sig.initSign(signingKey);        try        {            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();            DEROutputStream         dOut = new DEROutputStream(bOut);            dOut.writeObject(reqInfo);            sig.update(bOut.toByteArray());        }        catch (Exception e)        {            throw new SecurityException("exception encoding TBS cert request - " + e);        }        this.sigBits = new DERBitString(sig.sign());    }    /**     * return the public key associated with the certification request -     * the public key is created using the BC provider.     */    public PublicKey getPublicKey()        throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException    {        return getPublicKey("BC");    }    public PublicKey getPublicKey(        String  provider)        throws NoSuchAlgorithmException, NoSuchProviderException,                InvalidKeyException    {        SubjectPublicKeyInfo    subjectPKInfo = reqInfo.getSubjectPublicKeyInfo();        try        {            X509EncodedKeySpec      xspec = new X509EncodedKeySpec(new DERBitString(subjectPKInfo).getBytes());            AlgorithmIdentifier     keyAlg = subjectPKInfo.getAlgorithmId ();            return KeyFactory.getInstance(keyAlg.getObjectId().getId (), provider).generatePublic(xspec);        }        catch (InvalidKeySpecException e)        {            throw new InvalidKeyException("error decoding public key");        }    }    /**     * verify the request using the BC provider.     */    public boolean verify()        throws NoSuchAlgorithmException, NoSuchProviderException,                InvalidKeyException, SignatureException    {        return verify("BC");    }    public boolean verify(        String provider)        throws NoSuchAlgorithmException, NoSuchProviderException,                InvalidKeyException, SignatureException    {        Signature   sig = null;        try        {        	System.out.println("First signature test");         	if(sigAlgId.getObjectId().getId().equals(OID_RSASSA_PSS)){        		        			sig = Signature.getInstance("SHA256withRSAandMGF1",provider);        		        	}else{              sig = Signature.getInstance(sigAlgId.getObjectId().getId(), provider);        	}                      	System.out.println("Result " + sig.getAlgorithm());         }        catch (NoSuchAlgorithmException e)        {            //            // try an alternate            //            if (oids.get(sigAlgId.getObjectId().getId()) != null)            {            	System.out.println("No such algorithm next try");                 String  signatureAlgorithm = (String)oids.get(sigAlgId.getObjectId().getId());            	System.out.println("Foung algorithm" +signatureAlgorithm);                sig = Signature.getInstance(signatureAlgorithm, provider);            	System.out.println("Found following " + sig.getAlgorithm());            }        }                sig.initVerify(this.getPublicKey(provider));        try        {            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();            DEROutputStream         dOut = new DEROutputStream(bOut);            dOut.writeObject(reqInfo);            sig.update(bOut.toByteArray());        }        catch (Exception e)        {            throw new SecurityException("exception encoding TBS cert request - " + e);        }                 		        return sig.verify(sigBits.getBytes());    }    /**     * return a DER encoded byte array representing this object     */    public byte[] getEncoded()    {        ByteArrayOutputStream   bOut = new ByteArrayOutputStream();        DEROutputStream         dOut = new DEROutputStream(bOut);        try        {            dOut.writeObject(this);        }        catch (IOException e)        {            throw new RuntimeException(e.toString());        }        return bOut.toByteArray();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -