FileDocCategorySizeDatePackage
CMSTestUtil.javaAPI DocBouncy Castle Crypto API 1.41 (Java 1.5)15415Wed Oct 01 10:56:10 BST 2008org.bouncycastle.cms.test

CMSTestUtil

public class CMSTestUtil extends Object

Fields Summary
public static SecureRandom
rand
public static KeyPairGenerator
kpg
public static KeyPairGenerator
gostKpg
public static KeyPairGenerator
dsaKpg
public static KeyPairGenerator
ecGostKpg
public static KeyPairGenerator
ecDsaKpg
public static KeyGenerator
aes192kg
public static KeyGenerator
desede128kg
public static KeyGenerator
desede192kg
public static KeyGenerator
rc240kg
public static KeyGenerator
rc264kg
public static KeyGenerator
rc2128kg
public static KeyGenerator
aesKg
public static KeyGenerator
seedKg
public static KeyGenerator
camelliaKg
public static BigInteger
serialNumber
public static final boolean
DEBUG
private static byte[]
attrCert
Constructors Summary
Methods Summary
private static java.math.BigIntegerallocateSerialNumber()

        BigInteger _tmp = serialNumber;
        serialNumber = serialNumber.add(BigInteger.ONE);
        return _tmp;
    
private static org.bouncycastle.asn1.x509.AuthorityKeyIdentifiercreateAuthorityKeyId(java.security.PublicKey _pubKey)


        ByteArrayInputStream _bais = new ByteArrayInputStream(_pubKey
                .getEncoded());
        SubjectPublicKeyInfo _info = new SubjectPublicKeyInfo(
                (ASN1Sequence)new ASN1InputStream(_bais).readObject());

        return new AuthorityKeyIdentifier(_info);
    
static org.bouncycastle.asn1.x509.SubjectKeyIdentifiercreateSubjectKeyId(java.security.PublicKey _pubKey)


        ByteArrayInputStream _bais = new ByteArrayInputStream(_pubKey
                .getEncoded());
        SubjectPublicKeyInfo _info = new SubjectPublicKeyInfo(
                (ASN1Sequence)new ASN1InputStream(_bais).readObject());
        return new SubjectKeyIdentifier(_info);
    
public static java.lang.StringdumpBase64(byte[] data)

    
    
    
        try
        {
            java.security.Security.addProvider(new BouncyCastleProvider());

            rand = new SecureRandom();

            kpg  = KeyPairGenerator.getInstance("RSA", "BC");
            kpg.initialize(1024, rand);
            
            gostKpg  = KeyPairGenerator.getInstance("GOST3410", "BC");
            GOST3410ParameterSpec gost3410P = new GOST3410ParameterSpec(CryptoProObjectIdentifiers.gostR3410_94_CryptoPro_A.getId());
            
            gostKpg.initialize(gost3410P, new SecureRandom());
            
            dsaKpg = KeyPairGenerator.getInstance("DSA", "BC");
            DSAParameterSpec dsaSpec = new DSAParameterSpec(
                        new BigInteger("7434410770759874867539421675728577177024889699586189000788950934679315164676852047058354758883833299702695428196962057871264685291775577130504050839126673"),
                        new BigInteger("1138656671590261728308283492178581223478058193247"),
                        new BigInteger("4182906737723181805517018315469082619513954319976782448649747742951189003482834321192692620856488639629011570381138542789803819092529658402611668375788410"));

            dsaKpg.initialize(dsaSpec, new SecureRandom());

            ecGostKpg = KeyPairGenerator.getInstance("ECGOST3410", "BC");
            ecGostKpg.initialize(ECGOST3410NamedCurveTable.getParameterSpec("GostR3410-2001-CryptoPro-A"), new SecureRandom());

            ecDsaKpg = KeyPairGenerator.getInstance("ECDSA", "BC");
            ecDsaKpg.initialize(239, new SecureRandom());

            aes192kg = KeyGenerator.getInstance("AES", "BC");
            aes192kg.init(192, rand);

            desede128kg = KeyGenerator.getInstance("DESEDE", "BC");
            desede128kg.init(112, rand);

            desede192kg = KeyGenerator.getInstance("DESEDE", "BC");
            desede192kg.init(168, rand);

            rc240kg = KeyGenerator.getInstance("RC2", "BC");
            rc240kg.init(40, rand);
            
            rc264kg = KeyGenerator.getInstance("RC2", "BC");
            rc264kg.init(64, rand);
            
            rc2128kg = KeyGenerator.getInstance("RC2", "BC");
            rc2128kg.init(128, rand);

            aesKg = KeyGenerator.getInstance("AES", "BC");

            seedKg = KeyGenerator.getInstance("SEED", "BC");

            camelliaKg = KeyGenerator.getInstance("Camellia", "BC");
            
            serialNumber = new BigInteger("1");
        }
        catch (Exception ex)
        {
            throw new RuntimeException(ex.toString());
        }
    
        StringBuffer    buf = new StringBuffer();
        
        data = Base64.encode(data);
        
        for (int i = 0; i < data.length; i += 64)
        {
            if (i + 64 < data.length)
            {
                buf.append(new String(data, i, 64));
            }
            else
            {
                buf.append(new String(data, i, data.length - i));
            }
            buf.append('\n");
        }
        
        return buf.toString();
    
public static org.bouncycastle.x509.X509AttributeCertificategetAttributeCertificate()

        X509StreamParser parser = X509StreamParser.getInstance("AttributeCertificate", "BC");

        parser.init(CMSTestUtil.attrCert);

        return (X509AttributeCertificate)parser.read();
    
public static javax.crypto.SecretKeymakeAES192Key()

        return aes192kg.generateKey();
    
public static javax.crypto.SecretKeymakeAESKey(int keySize)

        aesKg.init(keySize);
        return aesKg.generateKey();
    
public static java.security.cert.X509CertificatemakeCACertificate(java.security.KeyPair _subKP, java.lang.String _subDN, java.security.KeyPair _issKP, java.lang.String _issDN)


        return makeCertificate(_subKP, _subDN, _issKP, _issDN, true);
    
public static javax.crypto.SecretKeymakeCamelliaKey(int keySize)

        camelliaKg.init(keySize);
        return camelliaKg.generateKey();
    
public static java.security.cert.X509CertificatemakeCertificate(java.security.KeyPair _subKP, java.lang.String _subDN, java.security.KeyPair _issKP, java.lang.String _issDN)


        return makeCertificate(_subKP, _subDN, _issKP, _issDN, false);
    
public static java.security.cert.X509CertificatemakeCertificate(java.security.KeyPair subKP, java.lang.String _subDN, java.security.KeyPair issKP, java.lang.String _issDN, boolean _ca)


        PublicKey  subPub  = subKP.getPublic();
        PrivateKey issPriv = issKP.getPrivate();
        PublicKey  issPub  = issKP.getPublic();
        
        X509V3CertificateGenerator v3CertGen = new X509V3CertificateGenerator();
        
        v3CertGen.reset();
        v3CertGen.setSerialNumber(allocateSerialNumber());
        v3CertGen.setIssuerDN(new X509Name(_issDN));
        v3CertGen.setNotBefore(new Date(System.currentTimeMillis()));
        v3CertGen.setNotAfter(new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 100)));
        v3CertGen.setSubjectDN(new X509Name(_subDN));
        v3CertGen.setPublicKey(subPub);
        
        if (issPub instanceof RSAPublicKey)
        {
            v3CertGen.setSignatureAlgorithm("SHA1WithRSA");
        }
        else if (issPub.getAlgorithm().equals("DSA"))
        {
            v3CertGen.setSignatureAlgorithm("SHA1withDSA");
        }
        else if (issPub.getAlgorithm().equals("ECDSA"))
        {
            v3CertGen.setSignatureAlgorithm("SHA1withECDSA");
        }
        else if (issPub.getAlgorithm().equals("ECGOST3410"))
        {
            v3CertGen.setSignatureAlgorithm("GOST3411withECGOST3410");
        }
        else
        {
            v3CertGen.setSignatureAlgorithm("GOST3411WithGOST3410");
        }

        v3CertGen.addExtension(
            X509Extensions.SubjectKeyIdentifier,
            false,
            createSubjectKeyId(subPub));

        v3CertGen.addExtension(
            X509Extensions.AuthorityKeyIdentifier,
            false,
            createAuthorityKeyId(issPub));

        v3CertGen.addExtension(
            X509Extensions.BasicConstraints,
            false,
            new BasicConstraints(_ca));

        X509Certificate _cert = v3CertGen.generate(issPriv);

        _cert.checkValidity(new Date());
        _cert.verify(issPub);

        return _cert;
    
public static java.security.cert.X509CRLmakeCrl(java.security.KeyPair pair)

        X509V2CRLGenerator crlGen = new X509V2CRLGenerator();
        Date                 now = new Date();

        crlGen.setIssuerDN(new X509Principal("CN=Test CA"));

        crlGen.setThisUpdate(now);
        crlGen.setNextUpdate(new Date(now.getTime() + 100000));
        crlGen.setSignatureAlgorithm("SHA256WithRSAEncryption");

        crlGen.addCRLEntry(BigInteger.ONE, now, CRLReason.privilegeWithdrawn);

        crlGen.addExtension(X509Extensions.AuthorityKeyIdentifier, false, new AuthorityKeyIdentifierStructure(pair.getPublic()));

        return crlGen.generate(pair.getPrivate(), "BC");
    
public static javax.crypto.SecretKeymakeDesede128Key()

        return desede128kg.generateKey();
    
public static javax.crypto.SecretKeymakeDesede192Key()

        return desede192kg.generateKey();
    
public static java.security.KeyPairmakeDsaKeyPair()

        return dsaKpg.generateKeyPair();
    
public static java.security.KeyPairmakeEcDsaKeyPair()

        return ecDsaKpg.generateKeyPair();
    
public static java.security.KeyPairmakeEcGostKeyPair()

        return ecGostKpg.generateKeyPair();
    
public static java.security.KeyPairmakeGostKeyPair()

        return gostKpg.generateKeyPair();
    
public static java.security.KeyPairmakeKeyPair()

        return kpg.generateKeyPair();
    
public static javax.crypto.SecretKeymakeRC2128Key()

        return rc2128kg.generateKey();
    
public static javax.crypto.SecretKeymakeRC240Key()

        return rc240kg.generateKey();
    
public static javax.crypto.SecretKeymakeRC264Key()

        return rc264kg.generateKey();
    
public static javax.crypto.SecretKeymakeSEEDKey()

        return seedKg.generateKey();
    
public static byte[]streamToByteArray(java.io.InputStream in)

        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        int ch;
        
        while ((ch = in.read()) >= 0)
        {
            bOut.write(ch);
        }
        
        return bOut.toByteArray();