Search in sources :

Example 1 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project camel by apache.

the class RSAKeyPairIdentity method getSignature.

@Override
public byte[] getSignature(byte[] data) {
    PrivateKey prvKey = keyPair.getPrivate();
    Signature sig;
    try {
        sig = Signature.getInstance("SHA1withRSA");
        sig.initSign(prvKey);
        sig.update(data);
        byte[] sshRsa = ALGORITHM_TYPE.getBytes();
        byte[] signature = sig.sign();
        byte[] result = new byte[sshRsa.length + 4 + signature.length + 4];
        int index = 0;
        byte[] intAsByteArray = ByteBuffer.allocate(4).putInt(sshRsa.length).array();
        System.arraycopy(intAsByteArray, 0, result, index, 4);
        index += 4;
        System.arraycopy(sshRsa, 0, result, index, sshRsa.length);
        index += sshRsa.length;
        intAsByteArray = ByteBuffer.allocate(4).putInt(signature.length).array();
        System.arraycopy(intAsByteArray, 0, result, index, 4);
        index += 4;
        System.arraycopy(signature, 0, result, index, signature.length);
        return result;
    } catch (NoSuchAlgorithmException e) {
        log.error("Cannot sign", e);
    } catch (InvalidKeyException e) {
        log.error("Cannot sign", e);
    } catch (SignatureException e) {
        log.error("Cannot sign", e);
    }
    return null;
}
Also used : PrivateKey(java.security.PrivateKey) Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Example 2 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project camel by apache.

the class SftpServerTestSupport method setUpServer.

protected void setUpServer() throws Exception {
    canTest = true;
    try {
        sshd = SshServer.setUpDefaultServer();
        sshd.setPort(getPort());
        sshd.setKeyPairProvider(new FileKeyPairProvider(new String[] { "src/test/resources/hostkey.pem" }));
        sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
        sshd.setCommandFactory(new ScpCommandFactory());
        sshd.setPasswordAuthenticator(new MyPasswordAuthenticator());
        PublickeyAuthenticator publickeyAuthenticator = new PublickeyAuthenticator() {

            // consider all keys as authorized for all users
            @Override
            public boolean authenticate(String username, PublicKey key, ServerSession session) {
                return true;
            }
        };
        sshd.setPublickeyAuthenticator(publickeyAuthenticator);
        sshd.start();
    } catch (Exception e) {
        // ignore if algorithm is not on the OS
        NoSuchAlgorithmException nsae = ObjectHelper.getException(NoSuchAlgorithmException.class, e);
        if (nsae != null) {
            canTest = false;
            String name = System.getProperty("os.name");
            String message = nsae.getMessage();
            log.warn("SunX509 is not avail on this platform [{}] Testing is skipped! Real cause: {}", name, message);
        } else {
            // some other error then throw it so the test can fail
            throw e;
        }
    }
}
Also used : ScpCommandFactory(org.apache.sshd.server.command.ScpCommandFactory) FileKeyPairProvider(org.apache.sshd.common.keyprovider.FileKeyPairProvider) ServerSession(org.apache.sshd.server.session.ServerSession) PublickeyAuthenticator(org.apache.sshd.server.PublickeyAuthenticator) Command(org.apache.sshd.server.Command) PublicKey(java.security.PublicKey) ScpCommandFactory(org.apache.sshd.server.command.ScpCommandFactory) NamedFactory(org.apache.sshd.common.NamedFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 3 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project camel by apache.

the class FtpServerTestSupport method setUp.

@Override
@Before
public void setUp() throws Exception {
    deleteDirectory(FTP_ROOT_DIR);
    canTest = false;
    FtpServerFactory factory = createFtpServerFactory();
    if (factory != null) {
        ftpServer = factory.createServer();
        if (ftpServer != null) {
            ftpServer.start();
            canTest = true;
        }
    }
    try {
        super.setUp();
    } catch (Exception e) {
        // ignore if algorithm is not on the OS
        NoSuchAlgorithmException nsae = ObjectHelper.getException(NoSuchAlgorithmException.class, e);
        if (nsae != null) {
            canTest = false;
            String name = System.getProperty("os.name");
            String message = nsae.getMessage();
            log.warn("SunX509 is not avail on this platform [{}] Testing is skipped! Real cause: {}", name, message);
        } else {
            // some other error then throw it so the test can fail
            throw e;
        }
    }
}
Also used : FtpServerFactory(org.apache.ftpserver.FtpServerFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Before(org.junit.Before)

Example 4 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project camel by apache.

the class AbstractJsseParametersTest method createPropertiesPlaceholderAwareContext.

protected CamelContext createPropertiesPlaceholderAwareContext() throws Exception {
    Properties supplementalProperties = new Properties();
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    SecureRandom sr = null;
    try {
        sr = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
    // Ignore
    }
    SSLContext sslc = SSLContext.getInstance("TLS");
    sslc.init(null, null, null);
    SSLSocket socket = (SSLSocket) sslc.getSocketFactory().createSocket();
    supplementalProperties.setProperty("keyStoreParameters.type", KeyStore.getDefaultType());
    supplementalProperties.setProperty("keyStoreParameters.provider", ks.getProvider().getName());
    supplementalProperties.setProperty("keyManagersParameters.algorithm", KeyManagerFactory.getDefaultAlgorithm());
    supplementalProperties.setProperty("keyManagersParameters.provider", kmf.getProvider().getName());
    supplementalProperties.setProperty("trustManagersParameters.algorithm", TrustManagerFactory.getDefaultAlgorithm());
    supplementalProperties.setProperty("trustManagersParameters.provider", tmf.getProvider().getName());
    if (sr != null) {
        supplementalProperties.setProperty("secureRandomParameters.algorithm", "SHA1PRNG");
        supplementalProperties.setProperty("secureRandomParameters.provider", sr.getProvider().getName());
    }
    supplementalProperties.setProperty("sslContextParameters.provider", sslc.getProvider().getName());
    supplementalProperties.setProperty("cipherSuite.0", socket.getSupportedCipherSuites()[0]);
    // Have to skip this guy because he doesn't work with TLS as the SSLContext protocol
    String ssp = "";
    for (String protocol : socket.getSupportedProtocols()) {
        if (!"SSLv2Hello".equals(protocol)) {
            ssp = protocol;
            break;
        }
    }
    supplementalProperties.setProperty("secureSocketProtocol.0", ssp);
    return this.createPropertiesPlaceholderAwareContext(supplementalProperties);
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLSocket(javax.net.ssl.SSLSocket) SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SSLContext(javax.net.ssl.SSLContext) Properties(java.util.Properties) KeyStore(java.security.KeyStore) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 5 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project hadoop by apache.

the class AbstractJavaKeyStoreProvider method flush.

@Override
public void flush() throws IOException {
    writeLock.lock();
    try {
        if (!changed) {
            LOG.debug("Keystore hasn't changed, returning.");
            return;
        }
        LOG.debug("Writing out keystore.");
        try (OutputStream out = getOutputStreamForKeystore()) {
            keyStore.store(out, password);
        } catch (KeyStoreException e) {
            throw new IOException("Can't store keystore " + this, e);
        } catch (NoSuchAlgorithmException e) {
            throw new IOException("No such algorithm storing keystore " + this, e);
        } catch (CertificateException e) {
            throw new IOException("Certificate exception storing keystore " + this, e);
        }
        changed = false;
    } finally {
        writeLock.unlock();
    }
}
Also used : OutputStream(java.io.OutputStream) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

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