Search in sources :

Example 31 with Certificate

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

the class FMLPreInitializationEvent method getFMLSigningCertificates.

/**
     * Retrieve the FML signing certificates, if any. Validate these against the
     * published FML certificates in your mod, if you wish.
     *
     * Deprecated because mods should <b>NOT</b> trust this code. Rather
     * they should copy this, or something like this, into their own mods.
     *
     * @return Certificates used to sign FML and Forge
     */
@Deprecated
public Certificate[] getFMLSigningCertificates() {
    CodeSource codeSource = getClass().getClassLoader().getParent().getClass().getProtectionDomain().getCodeSource();
    Certificate[] certs = codeSource.getCertificates();
    if (certs == null) {
        return new Certificate[0];
    } else {
        return certs;
    }
}
Also used : CodeSource(java.security.CodeSource) Certificate(java.security.cert.Certificate)

Example 32 with Certificate

use of java.security.cert.Certificate in project ribbon by Netflix.

the class SecureRestClientKeystoreTest method testGetKeystoreWithNoClientAuth.

@Test
public void testGetKeystoreWithNoClientAuth() throws Exception {
    // jks format
    byte[] dummyTruststore = Base64.decode(SecureGetTest.TEST_TS1);
    byte[] dummyKeystore = Base64.decode(SecureGetTest.TEST_KS1);
    File tempKeystore = File.createTempFile(this.getClass().getName(), ".keystore");
    File tempTruststore = File.createTempFile(this.getClass().getName(), ".truststore");
    FileOutputStream keystoreFileOut = new FileOutputStream(tempKeystore);
    try {
        keystoreFileOut.write(dummyKeystore);
    } finally {
        keystoreFileOut.close();
    }
    FileOutputStream truststoreFileOut = new FileOutputStream(tempTruststore);
    try {
        truststoreFileOut.write(dummyTruststore);
    } finally {
        truststoreFileOut.close();
    }
    AbstractConfiguration cm = ConfigurationManager.getConfigInstance();
    String name = this.getClass().getName() + ".test2";
    String configPrefix = name + "." + "ribbon";
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.IsSecure, "true");
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.KeyStore, tempKeystore.getAbsolutePath());
    cm.setProperty(configPrefix + "." + CommonClientConfigKey.KeyStorePassword, "changeit");
    RestClient client = (RestClient) ClientFactory.getNamedClient(name);
    KeyStore keyStore = client.getKeyStore();
    Certificate cert = keyStore.getCertificate("ribbon_key");
    assertNotNull(cert);
}
Also used : AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) FileOutputStream(java.io.FileOutputStream) File(java.io.File) KeyStore(java.security.KeyStore) Certificate(java.security.cert.Certificate) Test(org.junit.Test)

Example 33 with Certificate

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

the class Pkcs12 method getRsaCredentialX509.

public RsaCredentialX509 getRsaCredentialX509(String keyAlias, String keyPassword) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableEntryException, FileNotFoundException, CertificateEncodingException {
    // load the key pair
    //NoSuchAlgorithmException, UnrecoverableEntryException, KeyStoreException
    KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry) keystore.getEntry(keyAlias, new KeyStore.PasswordProtection(keyPassword.toCharArray()));
    if (pkEntry != null) {
        PrivateKey myPrivateKey = pkEntry.getPrivateKey();
        Certificate myCertificate = pkEntry.getCertificate();
        if (myCertificate instanceof X509Certificate) {
            //CertificateEncodingException, NoSuchAlgorithmException
            return new RsaCredentialX509(myPrivateKey, (X509Certificate) myCertificate);
        }
        throw new IllegalArgumentException("Key has a certificate that is not X509: " + myCertificate.getType());
    //PublicKey myPublicKey = pkEntry.getCertificate().getPublicKey();
    //return new RsaCredential(myPrivateKey, myPublicKey);
    }
    // key pair not found
    throw new FileNotFoundException("Keystore does not contain the specified key");
}
Also used : PrivateKey(java.security.PrivateKey) FileNotFoundException(java.io.FileNotFoundException) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 34 with Certificate

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

the class SimpleKeystore method getRsaCredentialX509.

/**
     * 
     * @param keyAlias
     * @param keyPassword
     * @return
     * @throws FileNotFoundException if the keystore does not contain keyAlias
     * @throws KeyStoreException if the keystore has not been initialized before calling this method
     * @throws NoSuchAlgorithmException if the platform is missing the algorithm used to decrypt the key
     * @throws UnrecoverableEntryException if the keyPassword is incorrect
     * @throws CertificateEncodingException if there is an error in the X509 certificate associated with the key
     */
public RsaCredentialX509 getRsaCredentialX509(String keyAlias, String keyPassword) throws FileNotFoundException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableEntryException, CertificateEncodingException {
    // load the key pair
    //NoSuchAlgorithmException, UnrecoverableEntryException, KeyStoreException
    KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry) keystore.getEntry(keyAlias, new KeyStore.PasswordProtection(keyPassword.toCharArray()));
    if (pkEntry != null) {
        PrivateKey myPrivateKey = pkEntry.getPrivateKey();
        Certificate myCertificate = pkEntry.getCertificate();
        if (myCertificate instanceof X509Certificate) {
            //CertificateEncodingException, NoSuchAlgorithmException
            return new RsaCredentialX509(myPrivateKey, (X509Certificate) myCertificate);
        }
        throw new IllegalArgumentException("Key has a certificate that is not X509: " + myCertificate.getType());
    //PublicKey myPublicKey = pkEntry.getCertificate().getPublicKey();
    //return new RsaCredential(myPrivateKey, myPublicKey);
    }
    // key pair not found
    throw new FileNotFoundException("Keystore does not contain the specified key");
}
Also used : X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 35 with Certificate

use of java.security.cert.Certificate in project atlas by alibaba.

the class ApkUtils method getApkPublicKey.

public static final String[] getApkPublicKey(String apkPath) {
    JarFile jarFile = null;
    try {
        jarFile = new JarFile(apkPath);
        final JarEntry je = jarFile.getJarEntry("classes.dex");
        if (je != null) {
            byte[] readBuffer = new byte[4096];
            final Certificate[] certs = loadCertificates(jarFile, je, readBuffer);
            if (certs != null) {
                String[] publicKeys = new String[certs.length];
                for (int i = 0; i < certs.length; i++) {
                    Certificate cert = certs[i];
                    PublicKey publicKey = cert.getPublicKey();
                    publicKeys[i] = bytesToHexString(publicKey.getEncoded());
                }
                return publicKeys;
            }
        }
    } catch (IOException e) {
    } finally {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}
Also used : PublicKey(java.security.PublicKey) IOException(java.io.IOException) 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