certtest.java

来自「内容:基于jdk1.4的加密算法的具体实现」· Java 代码 · 共 1,483 行 · 第 1/5 页

JAVA
1,483
字号
            extOids.addElement(X509Extensions.ReasonCode);            extValues.addElement(new X509Extension(false, new DEROctetString(crlReason.getEncoded())));        }        catch (IOException e)        {            throw new IllegalArgumentException("error encoding reason: " + e);        }                X509Extensions entryExtensions = new X509Extensions(extOids, extValues);                crlGen.addCRLEntry(BigInteger.ONE, now, entryExtensions);                crlGen.addExtension(X509Extensions.AuthorityKeyIdentifier, false, new AuthorityKeyIdentifierStructure(pair.getPublic()));                X509CRL    crl = crlGen.generateX509CRL(pair.getPrivate(), "BC");                if (!crl.getIssuerX500Principal().equals(new X500Principal("CN=Test CA")))        {            fail("failed CRL issuer test");        }                byte[] authExt = crl.getExtensionValue(X509Extensions.AuthorityKeyIdentifier.getId());                if (authExt == null)        {            fail("failed to find CRL extension");        }                AuthorityKeyIdentifier authId = new AuthorityKeyIdentifierStructure(authExt);                X509CRLEntry entry = crl.getRevokedCertificate(BigInteger.ONE);                if (entry == null)        {            fail("failed to find CRL entry");        }                if (!entry.getSerialNumber().equals(BigInteger.ONE))        {            fail("CRL cert serial number does not match");        }                if (!entry.hasExtensions())        {            fail("CRL entry extension not found");        }        byte[]  ext = entry.getExtensionValue(X509Extensions.ReasonCode.getId());        if (ext != null)        {            DEREnumerated   reasonCode = (DEREnumerated)X509ExtensionUtil.fromExtensionValue(ext);                                                                                   if (reasonCode.getValue().intValue() != CRLReason.privilegeWithdrawn)            {                fail("CRL entry reasonCode wrong");            }        }        else        {            fail("CRL entry reasonCode not found");        }    }        /**     * we generate a self signed certificate for the sake of testing - GOST3410     */    public void checkCreation4()        throws Exception    {        //        // set up the keys        //        PrivateKey          privKey;        PublicKey           pubKey;        KeyPairGenerator    g = KeyPairGenerator.getInstance("GOST3410", "BC");        GOST3410ParameterSpec gost3410P = new GOST3410ParameterSpec("GostR3410-94-CryptoPro-A");        g.initialize(gost3410P, new SecureRandom());        KeyPair p = g.generateKeyPair();        privKey = p.getPrivate();        pubKey = p.getPublic();        //        // distinguished name table.        //        Hashtable                   attrs = new Hashtable();        attrs.put(X509Principal.C, "AU");        attrs.put(X509Principal.O, "The Legion of the Bouncy Castle");        attrs.put(X509Principal.L, "Melbourne");        attrs.put(X509Principal.ST, "Victoria");        attrs.put(X509Principal.E, "feedback-crypto@bouncycastle.org");        //        // extensions        //        //        // create the certificate - version 3        //        X509V3CertificateGenerator  certGen = new X509V3CertificateGenerator();        certGen.setSerialNumber(BigInteger.valueOf(1));        certGen.setIssuerDN(new X509Principal(attrs));        certGen.setNotBefore(new Date(System.currentTimeMillis() - 50000));        certGen.setNotAfter(new Date(System.currentTimeMillis() + 50000));        certGen.setSubjectDN(new X509Principal(attrs));        certGen.setPublicKey(pubKey);        certGen.setSignatureAlgorithm("GOST3411withGOST3410");        X509Certificate cert = certGen.generateX509Certificate(privKey);        cert.checkValidity(new Date());        //        // check verifies in general        //        cert.verify(pubKey);        //        // check verifies with contained key        //        cert.verify(cert.getPublicKey());                ByteArrayInputStream    bIn = new ByteArrayInputStream(cert.getEncoded());        CertificateFactory      fact = CertificateFactory.getInstance("X.509", "BC");        cert = (X509Certificate)fact.generateCertificate(bIn);        //System.out.println(cert);        //check getEncoded()        byte[]  bytesch = cert.getEncoded();    }        public void checkCreation5()        throws Exception    {        //        // a sample key pair.        //        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(            new BigInteger("b4a7e46170574f16a97082b22be58b6a2a629798419be12872a4bdba626cfae9900f76abfb12139dce5de56564fab2b6543165a040c606887420e33d91ed7ed7", 16),            new BigInteger("11", 16));            RSAPrivateCrtKeySpec privKeySpec = new RSAPrivateCrtKeySpec(            new BigInteger("b4a7e46170574f16a97082b22be58b6a2a629798419be12872a4bdba626cfae9900f76abfb12139dce5de56564fab2b6543165a040c606887420e33d91ed7ed7", 16),            new BigInteger("11", 16),            new BigInteger("9f66f6b05410cd503b2709e88115d55daced94d1a34d4e32bf824d0dde6028ae79c5f07b580f5dce240d7111f7ddb130a7945cd7d957d1920994da389f490c89", 16),            new BigInteger("c0a0758cdf14256f78d4708c86becdead1b50ad4ad6c5c703e2168fbf37884cb", 16),            new BigInteger("f01734d7960ea60070f1b06f2bb81bfac48ff192ae18451d5e56c734a5aab8a5", 16),            new BigInteger("b54bb9edff22051d9ee60f9351a48591b6500a319429c069a3e335a1d6171391", 16),            new BigInteger("d3d83daf2a0cecd3367ae6f8ae1aeb82e9ac2f816c6fc483533d8297dd7884cd", 16),            new BigInteger("b8f52fc6f38593dabb661d3f50f8897f8106eee68b1bce78a95b132b4e5b5d19", 16));            //        // set up the keys        //        SecureRandom        rand = new SecureRandom();        PrivateKey          privKey;        PublicKey           pubKey;            KeyFactory  fact = KeyFactory.getInstance("RSA", "BC");            privKey = fact.generatePrivate(privKeySpec);        pubKey = fact.generatePublic(pubKeySpec);            //        // distinguished name table.        //        Hashtable                   attrs = new Hashtable();            attrs.put(X509Principal.C, "AU");        attrs.put(X509Principal.O, "The Legion of the Bouncy Castle");        attrs.put(X509Principal.L, "Melbourne");        attrs.put(X509Principal.ST, "Victoria");        attrs.put(X509Principal.E, "feedback-crypto@bouncycastle.org");            Vector                      ord = new Vector();        Vector                      values = new Vector();            ord.addElement(X509Principal.C);        ord.addElement(X509Principal.O);        ord.addElement(X509Principal.L);        ord.addElement(X509Principal.ST);        ord.addElement(X509Principal.E);            values.addElement("AU");        values.addElement("The Legion of the Bouncy Castle");        values.addElement("Melbourne");        values.addElement("Victoria");        values.addElement("feedback-crypto@bouncycastle.org");            //        // create base certificate - version 3        //        X509V3CertificateGenerator  certGen = new X509V3CertificateGenerator();            certGen.setSerialNumber(BigInteger.valueOf(1));        certGen.setIssuerDN(new X509Principal(attrs));        certGen.setNotBefore(new Date(System.currentTimeMillis() - 50000));        certGen.setNotAfter(new Date(System.currentTimeMillis() + 50000));        certGen.setSubjectDN(new X509Principal(attrs));        certGen.setPublicKey(pubKey);        certGen.setSignatureAlgorithm("MD5WithRSAEncryption");        certGen.addExtension("2.5.29.15", true,            new X509KeyUsage(X509KeyUsage.encipherOnly));        certGen.addExtension("2.5.29.37", true,            new DERSequence(KeyPurposeId.anyExtendedKeyUsage));        certGen.addExtension("2.5.29.17", true,            new GeneralNames(new GeneralName(GeneralName.rfc822Name, "test@test.test")));            X509Certificate baseCert = certGen.generateX509Certificate(privKey);                //        // copy certificate        //        certGen = new X509V3CertificateGenerator();                certGen.setSerialNumber(BigInteger.valueOf(1));        certGen.setIssuerDN(new X509Principal(attrs));        certGen.setNotBefore(new Date(System.currentTimeMillis() - 50000));        certGen.setNotAfter(new Date(System.currentTimeMillis() + 50000));        certGen.setSubjectDN(new X509Principal(attrs));        certGen.setPublicKey(pubKey);        certGen.setSignatureAlgorithm("MD5WithRSAEncryption");        certGen.copyAndAddExtension(new DERObjectIdentifier("2.5.29.15"), true, baseCert);        certGen.copyAndAddExtension("2.5.29.37", false, baseCert);                X509Certificate cert = certGen.generateX509Certificate(privKey);                cert.checkValidity(new Date());            cert.verify(pubKey);            if (!areEqual(baseCert.getExtensionValue("2.5.29.15"), cert.getExtensionValue("2.5.29.15")))        {            fail("2.5.29.15 differs");        }                if (!areEqual(baseCert.getExtensionValue("2.5.29.37"), cert.getExtensionValue("2.5.29.37")))        {            fail("2.5.29.37 differs");        }                //        // exception test        //        try        {            certGen.copyAndAddExtension("2.5.99.99", true, baseCert);                        fail("exception not thrown on dud extension copy");        }        catch (CertificateParsingException e)        {            // expected        }                try        {            certGen.setPublicKey(dudPublicKey);                        certGen.generateX509Certificate(privKey);                        fail("key without encoding not detected in v3");        }        catch (IllegalArgumentException e)        {            // expected        }    }        public void performTest()        throws Exception    {        checkCertificate(1, cert1);        checkCertificate(2, cert2);        checkCertificate(4, cert4);        checkCertificate(5, cert5);        checkCertificate(6, oldEcdsa);        checkCertificate(7, cert7);                checkKeyUsage(8, keyUsage);        checkSelfSignedCertificate(9, uncompressedPtEC);        checkNameCertificate(10, nameCert);                checkSelfSignedCertificate(11, probSelfSignedCert);        checkSelfSignedCertificate(12, gostCA1);    

⌨️ 快捷键说明

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