Search in sources :

Example 71 with Certificate

use of java.security.cert.Certificate in project robovm by robovm.

the class CipherTest method test_initWithCertificate.

public void test_initWithCertificate() throws Exception {
    /* Certificate creation notes: certificate should be valid 37273 starting
     * from 13 Nov 2008
     * If it brcomes invalidated regenerate it using following commands:
     * 1. openssl genrsa -des3 -out test.key 1024
     * 2. openssl req -new -key test.key -out test.csr
     * 3. cp test.key test.key.org
     * 4. openssl rsa -in test.key.org -out test.key
     * 5. openssl x509 -req -days 37273 -in test.csr -signkey test.key -out test.cert
     * */
    String certName = Support_Resources.getURL("test.cert");
    InputStream is = new URL(certName).openConnection().getInputStream();
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    Certificate cert = cf.generateCertificate(is);
    is.close();
    Cipher c = Cipher.getInstance("RSA");
    c.init(Cipher.ENCRYPT_MODE, cert);
    c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    try {
        c.init(Cipher.ENCRYPT_MODE, cert);
        fail();
    } catch (InvalidKeyException expected) {
    }
}
Also used : InputStream(java.io.InputStream) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) InvalidKeyException(java.security.InvalidKeyException) CertificateFactory(java.security.cert.CertificateFactory) URL(java.net.URL) Certificate(java.security.cert.Certificate)

Example 72 with Certificate

use of java.security.cert.Certificate in project robovm by robovm.

the class SecureClassLoaderTest method testGetPermissions.

public void testGetPermissions() throws Exception {
    URL url = new URL("http://localhost");
    CodeSource cs = new CodeSource(url, (Certificate[]) null);
    MyClassLoader ldr = new MyClassLoader();
    ldr.getPerms(null);
    ldr.getPerms(cs);
}
Also used : CodeSource(java.security.CodeSource) URL(java.net.URL) Certificate(java.security.cert.Certificate)

Example 73 with Certificate

use of java.security.cert.Certificate in project robovm by robovm.

the class JarEntry method getCodeSigners.

private CodeSigner[] getCodeSigners(Certificate[] certs) {
    if (certs == null) {
        return null;
    }
    X500Principal prevIssuer = null;
    ArrayList<Certificate> list = new ArrayList<Certificate>(certs.length);
    ArrayList<CodeSigner> asigners = new ArrayList<CodeSigner>();
    for (Certificate element : certs) {
        if (!(element instanceof X509Certificate)) {
            // Only X509Certificate-s are taken into account - see API spec.
            continue;
        }
        X509Certificate x509 = (X509Certificate) element;
        if (prevIssuer != null) {
            X500Principal subj = x509.getSubjectX500Principal();
            if (!prevIssuer.equals(subj)) {
                // Ok, this ends the previous chain,
                // so transform this one into CertPath ...
                addCodeSigner(asigners, list);
                // ... and start a new one
                list.clear();
            }
        // else { it's still the same chain }
        }
        prevIssuer = x509.getIssuerX500Principal();
        list.add(x509);
    }
    if (!list.isEmpty()) {
        addCodeSigner(asigners, list);
    }
    if (asigners.isEmpty()) {
        // 'signers' is 'null' already
        return null;
    }
    CodeSigner[] tmp = new CodeSigner[asigners.size()];
    asigners.toArray(tmp);
    return tmp;
}
Also used : ArrayList(java.util.ArrayList) X500Principal(javax.security.auth.x500.X500Principal) CodeSigner(java.security.CodeSigner) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 74 with Certificate

use of java.security.cert.Certificate in project robovm by robovm.

the class OldJarURLConnectionTest method test_getCertificates.

public void test_getCertificates() throws Exception {
    URL u = createContent("TestCodeSigners.jar", "Test.class");
    juc = (JarURLConnection) u.openConnection();
    assertNull(juc.getCertificates());
    JarEntry je = juc.getJarEntry();
    JarFile jf = juc.getJarFile();
    InputStream is = jf.getInputStream(je);
    is.skip(je.getSize());
    Certificate[] certs = juc.getCertificates();
    assertEquals(3, certs.length);
    URL invURL = createContent("InvalidJar.jar", "Test.class");
    JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
    try {
        juConn.getCertificates();
        fail("IOException was not thrown.");
    } catch (java.io.IOException io) {
    //expected
    }
}
Also used : InputStream(java.io.InputStream) JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) URL(java.net.URL) Certificate(java.security.cert.Certificate)

Example 75 with Certificate

use of java.security.cert.Certificate in project robovm by robovm.

the class OldAndroidZipStressTest method checkJarCertificates.

/**
     * JarEntry.getCertificates() is really slow. http://b/1046174
     */
public void checkJarCertificates(File file) throws Exception {
    JarFile jarFile = new JarFile(file);
    JarEntry je = jarFile.getJarEntry("AndroidManifest.xml");
    byte[] readBuffer = new byte[1024];
    long t0 = System.currentTimeMillis();
    // We must read the stream for the JarEntry to retrieve its certificates.
    InputStream is = jarFile.getInputStream(je);
    while (is.read(readBuffer, 0, readBuffer.length) != -1) {
    }
    is.close();
    Certificate[] certs = je != null ? je.getCertificates() : null;
    long t1 = System.currentTimeMillis();
    System.out.println("loadCertificates() took " + (t1 - t0) + " ms");
    if (certs == null) {
        System.out.println("We have no certificates");
    } else {
        System.out.println("We have " + certs.length + " certificates");
    }
}
Also used : InputStream(java.io.InputStream) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Certificate(java.security.cert.Certificate)

Aggregations

Certificate (java.security.cert.Certificate)723 X509Certificate (java.security.cert.X509Certificate)469 CertificateFactory (java.security.cert.CertificateFactory)272 ByteArrayInputStream (java.io.ByteArrayInputStream)237 KeyStore (java.security.KeyStore)133 PrivateKey (java.security.PrivateKey)132 IOException (java.io.IOException)106 CertificateException (java.security.cert.CertificateException)102 KeyFactory (java.security.KeyFactory)89 KeyStoreException (java.security.KeyStoreException)88 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)72 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)69 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)63 ArrayList (java.util.ArrayList)63 TrustedCertificateEntry (java.security.KeyStore.TrustedCertificateEntry)56 Entry (java.security.KeyStore.Entry)53 PublicKey (java.security.PublicKey)48 InputStream (java.io.InputStream)40 FileInputStream (java.io.FileInputStream)39 Key (java.security.Key)36