Search in sources :

Example 36 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project buck by facebook.

the class ApkBuilderStep method createKeystoreProperties.

private PrivateKeyAndCertificate createKeystoreProperties() throws IOException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
    KeyStore keystore = KeyStore.getInstance(JARSIGNER_KEY_STORE_TYPE);
    KeystoreProperties keystoreProperties = keystorePropertiesSupplier.get();
    InputStream inputStream = filesystem.getInputStreamForRelativePath(pathToKeystore);
    char[] keystorePassword = keystoreProperties.getStorepass().toCharArray();
    try {
        keystore.load(inputStream, keystorePassword);
    } catch (IOException | NoSuchAlgorithmException | CertificateException e) {
        throw new HumanReadableException(e, "%s is an invalid keystore.", pathToKeystore);
    }
    String alias = keystoreProperties.getAlias();
    char[] keyPassword = keystoreProperties.getKeypass().toCharArray();
    Key key = keystore.getKey(alias, keyPassword);
    // key can be null if alias/password is incorrect.
    if (key == null) {
        throw new HumanReadableException("The keystore [%s] key.alias [%s] does not exist or does not identify a key-related " + "entry", pathToKeystore, alias);
    }
    Certificate certificate = keystore.getCertificate(alias);
    return new PrivateKeyAndCertificate((PrivateKey) key, (X509Certificate) certificate);
}
Also used : InputStream(java.io.InputStream) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStore(java.security.KeyStore) HumanReadableException(com.facebook.buck.util.HumanReadableException) Key(java.security.Key) PrivateKey(java.security.PrivateKey) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 37 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project buck by facebook.

the class MoreProjectFilesystems method fileContentsDiffer.

public static boolean fileContentsDiffer(InputStream contents, Path path, ProjectFilesystem projectFilesystem) throws IOException {
    try {
        // Hash the contents of the file at path so we don't have to pull the whole thing into memory.
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
        byte[] pathDigest;
        try (InputStream is = projectFilesystem.newFileInputStream(path)) {
            pathDigest = inputStreamDigest(is, sha1);
        }
        // Hash 'contents' and see if the two differ.
        sha1.reset();
        byte[] contentsDigest = inputStreamDigest(contents, sha1);
        return !Arrays.equals(pathDigest, contentsDigest);
    } catch (NoSuchFileException e) {
        // If the file doesn't exist, we need to create it.
        return true;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) NoSuchFileException(java.nio.file.NoSuchFileException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 38 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project elasticsearch by elastic.

the class Bootstrap method setup.

private void setup(boolean addShutdownHook, Environment environment) throws BootstrapException {
    Settings settings = environment.settings();
    try {
        spawner.spawnNativePluginControllers(environment);
        Runtime.getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                try {
                    spawner.close();
                } catch (IOException e) {
                    throw new ElasticsearchException("Failed to destroy spawned controllers", e);
                }
            }
        });
    } catch (IOException e) {
        throw new BootstrapException(e);
    }
    initializeNatives(environment.tmpFile(), BootstrapSettings.MEMORY_LOCK_SETTING.get(settings), BootstrapSettings.SYSTEM_CALL_FILTER_SETTING.get(settings), BootstrapSettings.CTRLHANDLER_SETTING.get(settings));
    // initialize probes before the security manager is installed
    initializeProbes();
    if (addShutdownHook) {
        Runtime.getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                try {
                    IOUtils.close(node);
                    LoggerContext context = (LoggerContext) LogManager.getContext(false);
                    Configurator.shutdown(context);
                } catch (IOException ex) {
                    throw new ElasticsearchException("failed to stop node", ex);
                }
            }
        });
    }
    try {
        // look for jar hell
        JarHell.checkJarHell();
    } catch (IOException | URISyntaxException e) {
        throw new BootstrapException(e);
    }
    // Log ifconfig output before SecurityManager is installed
    IfConfig.logIfNecessary();
    // install SM after natives, shutdown hooks, etc.
    try {
        Security.configure(environment, BootstrapSettings.SECURITY_FILTER_BAD_DEFAULTS_SETTING.get(settings));
    } catch (IOException | NoSuchAlgorithmException e) {
        throw new BootstrapException(e);
    }
    node = new Node(environment) {

        @Override
        protected void validateNodeBeforeAcceptingRequests(final Settings settings, final BoundTransportAddress boundTransportAddress, List<BootstrapCheck> checks) throws NodeValidationException {
            BootstrapChecks.check(settings, boundTransportAddress, checks);
        }
    };
}
Also used : Node(org.elasticsearch.node.Node) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) LoggerContext(org.apache.logging.log4j.core.LoggerContext) NodeValidationException(org.elasticsearch.node.NodeValidationException) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) Settings(org.elasticsearch.common.settings.Settings) SecureSettings(org.elasticsearch.common.settings.SecureSettings)

Example 39 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project elasticsearch by elastic.

the class Security method readPolicy.

/**
     * Reads and returns the specified {@code policyFile}.
     * <p>
     * Resources (e.g. jar files and directories) listed in {@code codebases} location
     * will be provided to the policy file via a system property of the short name:
     * e.g. <code>${codebase.joda-convert-1.2.jar}</code> would map to full URL.
     */
@SuppressForbidden(reason = "accesses fully qualified URLs to configure security")
static Policy readPolicy(URL policyFile, URL[] codebases) {
    try {
        try {
            // set codebase properties
            for (URL url : codebases) {
                String shortName = PathUtils.get(url.toURI()).getFileName().toString();
                System.setProperty("codebase." + shortName, url.toString());
            }
            return Policy.getInstance("JavaPolicy", new URIParameter(policyFile.toURI()));
        } finally {
            // clear codebase properties
            for (URL url : codebases) {
                String shortName = PathUtils.get(url.toURI()).getFileName().toString();
                System.clearProperty("codebase." + shortName);
            }
        }
    } catch (NoSuchAlgorithmException | URISyntaxException e) {
        throw new IllegalArgumentException("unable to parse policy file `" + policyFile + "`", e);
    }
}
Also used : URIParameter(java.security.URIParameter) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) SuppressForbidden(org.elasticsearch.common.SuppressForbidden)

Example 40 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project android_frameworks_base by ParanoidAndroid.

the class PackageParser method parseVerifier.

private static VerifierInfo parseVerifier(Resources res, XmlPullParser parser, AttributeSet attrs, int flags, String[] outError) throws XmlPullParserException, IOException {
    final TypedArray sa = res.obtainAttributes(attrs, com.android.internal.R.styleable.AndroidManifestPackageVerifier);
    final String packageName = sa.getNonResourceString(com.android.internal.R.styleable.AndroidManifestPackageVerifier_name);
    final String encodedPublicKey = sa.getNonResourceString(com.android.internal.R.styleable.AndroidManifestPackageVerifier_publicKey);
    sa.recycle();
    if (packageName == null || packageName.length() == 0) {
        Slog.i(TAG, "verifier package name was null; skipping");
        return null;
    } else if (encodedPublicKey == null) {
        Slog.i(TAG, "verifier " + packageName + " public key was null; skipping");
    }
    EncodedKeySpec keySpec;
    try {
        final byte[] encoded = Base64.decode(encodedPublicKey, Base64.DEFAULT);
        keySpec = new X509EncodedKeySpec(encoded);
    } catch (IllegalArgumentException e) {
        Slog.i(TAG, "Could not parse verifier " + packageName + " public key; invalid Base64");
        return null;
    }
    /* First try the key as an RSA key. */
    try {
        final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        final PublicKey publicKey = keyFactory.generatePublic(keySpec);
        return new VerifierInfo(packageName, publicKey);
    } catch (NoSuchAlgorithmException e) {
        Log.wtf(TAG, "Could not parse public key because RSA isn't included in build");
        return null;
    } catch (InvalidKeySpecException e) {
    // Not a RSA public key.
    }
    /* Now try it as a DSA key. */
    try {
        final KeyFactory keyFactory = KeyFactory.getInstance("DSA");
        final PublicKey publicKey = keyFactory.generatePublic(keySpec);
        return new VerifierInfo(packageName, publicKey);
    } catch (NoSuchAlgorithmException e) {
        Log.wtf(TAG, "Could not parse public key because DSA isn't included in build");
        return null;
    } catch (InvalidKeySpecException e) {
    // Not a DSA public key.
    }
    return null;
}
Also used : PublicKey(java.security.PublicKey) TypedArray(android.content.res.TypedArray) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) EncodedKeySpec(java.security.spec.EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Aggregations

NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1403 MessageDigest (java.security.MessageDigest)548 IOException (java.io.IOException)328 InvalidKeyException (java.security.InvalidKeyException)242 KeyStoreException (java.security.KeyStoreException)168 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)145 CertificateException (java.security.cert.CertificateException)138 UnsupportedEncodingException (java.io.UnsupportedEncodingException)131 KeyManagementException (java.security.KeyManagementException)105 KeyFactory (java.security.KeyFactory)96 NoSuchProviderException (java.security.NoSuchProviderException)93 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)89 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)79 UnrecoverableKeyException (java.security.UnrecoverableKeyException)78 KeyStore (java.security.KeyStore)73 SecureRandom (java.security.SecureRandom)72 SSLContext (javax.net.ssl.SSLContext)72 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)69 BadPaddingException (javax.crypto.BadPaddingException)69 Cipher (javax.crypto.Cipher)69