Search in sources :

Example 1 with CodeSigner

use of java.security.CodeSigner in project XobotOS by xamarin.

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 2 with CodeSigner

use of java.security.CodeSigner in project jdk8u_jdk by JetBrains.

the class Serialize method main.

public static void main(String[] args) throws Exception {
    // Create a certpath consisting of one certificate
    File f = new File(System.getProperty("test.src", "."), "cert_file");
    FileInputStream fis = new FileInputStream(f);
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    Certificate c = cf.generateCertificate(fis);
    fis.close();
    CertPath cp = cf.generateCertPath(Collections.singletonList(c));
    // Create a code signer
    CodeSigner cs = new CodeSigner(cp, new Timestamp(new Date(), cp));
    // Serialize the code signer
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(byteOut);
    out.writeObject(cs);
    out.close();
    // Deserialize the code signer
    byte[] data = byteOut.toByteArray();
    CodeSigner cs2 = (CodeSigner) new ObjectInputStream(new ByteArrayInputStream(data)).readObject();
    // Test for equality
    if (!cs.equals(cs2) || cs.hashCode() != cs2.hashCode()) {
        throw new Exception("CodeSigner serialization test FAILED");
    }
}
Also used : Timestamp(java.security.Timestamp) Date(java.util.Date) CodeSigner(java.security.CodeSigner)

Example 3 with CodeSigner

use of java.security.CodeSigner in project jdk8u_jdk by JetBrains.

the class GetMethodsReturnClones method main.

public static void main(String[] args) throws Exception {
    List<JarEntry> entries = new ArrayList<>();
    try (JarFile jf = new JarFile(BASE + "test.jar", true)) {
        byte[] buffer = new byte[8192];
        Enumeration<JarEntry> e = jf.entries();
        while (e.hasMoreElements()) {
            JarEntry je = e.nextElement();
            entries.add(je);
            try (InputStream is = jf.getInputStream(je)) {
                while (is.read(buffer, 0, buffer.length) != -1) {
                // we just read. this will throw a SecurityException
                // if  a signature/digest check fails.
                }
            }
        }
    }
    for (JarEntry je : entries) {
        Certificate[] certs = je.getCertificates();
        CodeSigner[] signers = je.getCodeSigners();
        if (certs != null) {
            certs[0] = null;
            certs = je.getCertificates();
            if (certs[0] == null) {
                throw new Exception("Modified internal certs array");
            }
        }
        if (signers != null) {
            signers[0] = null;
            signers = je.getCodeSigners();
            if (signers[0] == null) {
                throw new Exception("Modified internal codesigners array");
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) CodeSigner(java.security.CodeSigner) Certificate(java.security.cert.Certificate)

Example 4 with CodeSigner

use of java.security.CodeSigner 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 5 with CodeSigner

use of java.security.CodeSigner in project robovm by robovm.

the class CodeSignerTest method testCodeSigner_02.

/**
     * Not null parameters
     */
public final void testCodeSigner_02() {
    try {
        CodeSigner cs = new CodeSigner(cpath, ts);
        assertNotNull(cs);
    } catch (Exception e) {
        fail("Unexpected exception");
    }
}
Also used : CodeSigner(java.security.CodeSigner)

Aggregations

CodeSigner (java.security.CodeSigner)31 Certificate (java.security.cert.Certificate)8 CodeSource (java.security.CodeSource)7 X509Certificate (java.security.cert.X509Certificate)6 Manifest (java.util.jar.Manifest)5 IOException (java.io.IOException)4 URL (java.net.URL)4 ByteBuffer (java.nio.ByteBuffer)4 CertPath (java.security.cert.CertPath)4 JarEntry (java.util.jar.JarEntry)4 JarFile (java.util.jar.JarFile)4 SignerInfo (sun.security.pkcs.SignerInfo)4 Timestamp (java.security.Timestamp)3 ArrayList (java.util.ArrayList)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URI (java.net.URI)2 GeneralSecurityException (java.security.GeneralSecurityException)2 KeyStoreException (java.security.KeyStoreException)2