Search in sources :

Example 16 with CodeSigner

use of java.security.CodeSigner in project Bytecoder by mirkosertic.

the class BuiltinClassLoader method defineClass.

/**
 * Defines the given binary class name to the VM, loading the class
 * bytes from the given module.
 *
 * @return the resulting Class or {@code null} if an I/O error occurs
 */
private Class<?> defineClass(String cn, LoadedModule loadedModule) {
    ModuleReference mref = loadedModule.mref();
    ModuleReader reader = moduleReaderFor(mref);
    try {
        ByteBuffer bb = null;
        URL csURL = null;
        // locate class file, special handling for patched modules to
        // avoid locating the resource twice
        String rn = cn.replace('.', '/').concat(".class");
        if (reader instanceof PatchedModuleReader) {
            Resource r = ((PatchedModuleReader) reader).findResource(rn);
            if (r != null) {
                bb = r.getByteBuffer();
                csURL = r.getCodeSourceURL();
            }
        } else {
            bb = reader.read(rn).orElse(null);
            csURL = loadedModule.codeSourceURL();
        }
        if (bb == null) {
            // class not found
            return null;
        }
        CodeSource cs = new CodeSource(csURL, (CodeSigner[]) null);
        try {
            // define class to VM
            return defineClass(cn, bb, cs);
        } finally {
            reader.release(bb);
        }
    } catch (IOException ioe) {
        // TBD on how I/O errors should be propagated
        return null;
    }
}
Also used : PatchedModuleReader(jdk.internal.module.ModulePatcher.PatchedModuleReader) PatchedModuleReader(jdk.internal.module.ModulePatcher.PatchedModuleReader) ModuleReader(java.lang.module.ModuleReader) ModuleReference(java.lang.module.ModuleReference) IOException(java.io.IOException) CodeSource(java.security.CodeSource) ByteBuffer(java.nio.ByteBuffer) URL(java.net.URL) CodeSigner(java.security.CodeSigner)

Example 17 with CodeSigner

use of java.security.CodeSigner in project LanternServer by LanternPowered.

the class LanternClassLoader method getCodeSource.

private CodeSource getCodeSource(String name, URL url) {
    // Classes without a jar protocol, nope
    if (!url.getProtocol().equalsIgnoreCase("jar")) {
        return null;
    }
    final String u = url.toString();
    final String s = "jar:";
    if (!u.startsWith(s)) {
        return null;
    }
    // The url should end with the following string, this
    // is pointing to a class inside a jar
    final String e = "!/" + name.replace('.', '/') + ".class";
    if (!u.endsWith(e)) {
        return null;
    }
    try {
        url = new URL(u.substring(s.length(), u.length() - e.length()));
    } catch (MalformedURLException ex) {
        // Malformed, just fail
        return null;
    }
    // Create the code source, no code signers since it's not required
    return new CodeSource(url, (CodeSigner[]) null);
}
Also used : MalformedURLException(java.net.MalformedURLException) CodeSource(java.security.CodeSource) URL(java.net.URL) CodeSigner(java.security.CodeSigner)

Example 18 with CodeSigner

use of java.security.CodeSigner in project airavata by apache.

the class JythonClassLoader method findClassFromURL.

private Class findClassFromURL(String name, URL url, JarFile jarFile) throws ClassNotFoundException {
    // logger.entering(new Object[] { name, url, jarFile });
    String classPath = name.replace('.', '/').concat(".class");
    try {
        byte[] classBytes;
        CodeSource codeSource = null;
        if (jarFile == null) {
            // It's a local directory
            String dirPath = URLDecoder.decode(url.getPath(), "UTF-8");
            File classFile = new File(dirPath, classPath);
            classBytes = IOUtil.readToByteArray(classFile);
        } else {
            // A Jar file
            JarEntry jarEntry = jarFile.getJarEntry(classPath);
            CodeSigner[] codeSigners = jarEntry.getCodeSigners();
            // logger.info("codeSigners: " + codeSigners);
            if (codeSigners != null) {
                // Somehow it's null.
                for (CodeSigner signer : codeSigners) {
                    logger.debug("signer: " + signer);
                }
            }
            codeSource = new CodeSource(this.xbayaURL, codeSigners);
            InputStream classInputStream = jarFile.getInputStream(jarEntry);
            classBytes = IOUtil.readToByteArray(classInputStream);
        }
        Class<?> klass = defineClass(name, classBytes, 0, classBytes.length, codeSource);
        this.classes.put(name, klass);
        return klass;
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        throw new ClassNotFoundException();
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) CodeSource(java.security.CodeSource) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File) CodeSigner(java.security.CodeSigner)

Example 19 with CodeSigner

use of java.security.CodeSigner in project j2objc by google.

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)

Example 20 with CodeSigner

use of java.security.CodeSigner in project j2objc by google.

the class CodeSignerTest method testEqualsObject.

/**
 * Test various assertions about equals()
 */
public final void testEqualsObject() {
    CodeSigner one = new CodeSigner(cpath, ts);
    CodeSigner two = new CodeSigner(cpath, ts);
    CodeSigner three = new CodeSigner(cpath, null);
    CertPath cpath2 = TestCertUtils.genCertPath(5, 3);
    CodeSigner four = new CodeSigner(cpath2, null);
    assertTrue(one.equals(one));
    assertTrue(one.equals(two));
    assertTrue(two.equals(one));
    assertFalse(one.equals(three));
    assertFalse(three.equals(one));
    assertTrue(three.equals(three));
    // different CertPaths
    assertFalse(three.equals(four));
    // special cases
    assertFalse(one.equals(null));
    assertFalse(one.equals(new Object()));
}
Also used : CertPath(java.security.cert.CertPath) 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