Search in sources :

Example 1 with DhKeyGenerator

use of org.teiid.core.crypto.DhKeyGenerator in project teiid by teiid.

the class SocketServerInstanceImpl method doHandshake.

private void doHandshake() throws IOException, CommunicationException {
    Handshake handshake = null;
    boolean sentInit = false;
    long handShakeRetries = 1;
    if (this.soTimeout > 0) {
        handShakeRetries = Math.max(1, synchTimeout / this.soTimeout);
    }
    for (int i = 0; i < handShakeRetries; i++) {
        try {
            Object obj = this.socketChannel.read();
            if (!(obj instanceof Handshake)) {
                throw new SingleInstanceCommunicationException(JDBCPlugin.Event.TEIID20009, null, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20009));
            }
            handshake = (Handshake) obj;
            break;
        } catch (ClassNotFoundException e1) {
            throw new SingleInstanceCommunicationException(JDBCPlugin.Event.TEIID20010, e1, e1.getMessage());
        } catch (SocketTimeoutException e) {
            if (!sentInit && !this.info.isSsl()) {
                // write a dummy initialization value - if the server is actually ssl, this can cause the server side handshake to fail, otherwise it's ignored
                // TODO: could always do this initialization in the non-ssl case and not wait for a timeout
                this.socketChannel.write(null);
                sentInit = true;
            }
            if (i == handShakeRetries - 1) {
                throw e;
            }
        } catch (IOException e) {
            if (sentInit && !this.info.isSsl()) {
                throw new SingleInstanceCommunicationException(JDBCPlugin.Event.TEIID20032, e, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20032));
            }
            throw e;
        }
    }
    try {
        /*if (!getVersionInfo().equals(handshake.getVersion())) {
                 throw new CommunicationException(JDBCPlugin.Event.TEIID20011, NetPlugin.Util.getString(JDBCPlugin.Event.TEIID20011, getVersionInfo(), handshake.getVersion()));
            }*/
        serverVersion = handshake.getVersion();
        handshake.setVersion();
        byte[] serverPublicKey = handshake.getPublicKey();
        byte[] serverPublicKeyLarge = handshake.getPublicKeyLarge();
        if (serverPublicKey != null) {
            DhKeyGenerator keyGen = new DhKeyGenerator();
            boolean large = false;
            if (serverPublicKeyLarge != null) {
                try {
                    byte[] publicKey = keyGen.createPublicKey(true);
                    handshake.setPublicKey(null);
                    handshake.setPublicKeyLarge(publicKey);
                    serverPublicKey = serverPublicKeyLarge;
                    large = true;
                } catch (CryptoException e) {
                // not supported on this platform
                }
            }
            if (!large) {
                byte[] publicKey = keyGen.createPublicKey(false);
                handshake.setPublicKey(publicKey);
                handshake.setPublicKeyLarge(null);
            }
            boolean useCbc = handshake.isCbc();
            // $NON-NLS-1$
            this.cryptor = keyGen.getSymmetricCryptor(serverPublicKey, "08.03".compareTo(serverVersion) > 0, this.getClass().getClassLoader(), large, useCbc);
        } else {
            this.cryptor = new NullCryptor();
        }
        this.socketChannel.write(handshake);
    } catch (CryptoException e) {
        throw new CommunicationException(JDBCPlugin.Event.TEIID20012, e, e.getMessage());
    }
}
Also used : CommunicationException(org.teiid.net.CommunicationException) IOException(java.io.IOException) NullCryptor(org.teiid.core.crypto.NullCryptor) SocketTimeoutException(java.net.SocketTimeoutException) DhKeyGenerator(org.teiid.core.crypto.DhKeyGenerator) CryptoException(org.teiid.core.crypto.CryptoException)

Example 2 with DhKeyGenerator

use of org.teiid.core.crypto.DhKeyGenerator in project teiid by teiid.

the class SocketClientInstance method onConnection.

public void onConnection() throws CommunicationException {
    Handshake handshake = new Handshake();
    handshake.setAuthType(csr.getAuthenticationType());
    if (usingEncryption) {
        keyGen = new DhKeyGenerator();
        byte[] publicKey;
        try {
            handshake.setPublicKeyLarge(keyGen.createPublicKey(true));
        } catch (CryptoException e) {
        // not supported on this platform
        }
        try {
            publicKey = keyGen.createPublicKey(false);
        } catch (CryptoException e) {
            throw new CommunicationException(RuntimePlugin.Event.TEIID40051, e);
        }
        handshake.setPublicKey(publicKey);
    }
    this.objectSocket.write(handshake);
}
Also used : CommunicationException(org.teiid.net.CommunicationException) DhKeyGenerator(org.teiid.core.crypto.DhKeyGenerator) CryptoException(org.teiid.core.crypto.CryptoException) Handshake(org.teiid.net.socket.Handshake)

Aggregations

CryptoException (org.teiid.core.crypto.CryptoException)2 DhKeyGenerator (org.teiid.core.crypto.DhKeyGenerator)2 CommunicationException (org.teiid.net.CommunicationException)2 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 NullCryptor (org.teiid.core.crypto.NullCryptor)1 Handshake (org.teiid.net.socket.Handshake)1