001/**
002 *
003 * Copyright 2016 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.igniterealtime.smack.smackrepl;
018
019import java.io.IOException;
020import java.security.KeyManagementException;
021import java.security.NoSuchAlgorithmException;
022
023import javax.net.ssl.SSLContext;
024
025import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
026import org.jivesoftware.smack.SmackException;
027import org.jivesoftware.smack.SmackException.SecurityRequiredByClientException;
028import org.jivesoftware.smack.XMPPException;
029import org.jivesoftware.smack.tcp.XMPPTCPConnection;
030import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
031import org.jivesoftware.smack.util.StringUtils;
032
033import eu.geekplace.javapinning.java7.Java7Pinning;
034import org.jxmpp.jid.EntityBareJid;
035import org.jxmpp.jid.impl.JidCreate;
036import org.jxmpp.stringprep.XmppStringprepException;
037
038public class TlsTest {
039
040//    private static final Logger LOGGER = Logger.getLogger(TlsTest.class.getName());
041    public static boolean DEBUG = false;
042
043    public static void main(String[] args) throws XmppStringprepException, KeyManagementException, NoSuchAlgorithmException {
044        DEBUG = true;
045        tlsTest(args[0], args[1], args[2], args[3], args[4], args[5], true);
046    }
047
048    public static void tlsTest(String runsString, CharSequence jidCs, String password, String host, String portString,
049                    String tlsPin, boolean shouldThrow) throws XmppStringprepException, KeyManagementException, NoSuchAlgorithmException {
050        int runs = Integer.parseInt(runsString);
051        int port = Integer.parseInt(portString);
052        tlsTest(runs, jidCs, password, host, port, tlsPin, shouldThrow);
053    }
054
055    public static void tlsTest(int runs, CharSequence jidCs, String password, String host, int port,
056                    String tlsPin, boolean shouldThrow) throws XmppStringprepException, KeyManagementException, NoSuchAlgorithmException {
057        EntityBareJid jid = JidCreate.entityBareFrom(jidCs);
058        for (int i = 0; i < runs; i++) {
059            boolean res = tlsTest(jid, password, host, port, tlsPin, shouldThrow);
060            if (!res) {
061                throw new IllegalStateException();
062            }
063        }
064    }
065
066    public static boolean tlsTest(CharSequence jidCs, String password, String host, int port,
067                    String tlsPin, boolean shouldThrow) throws XmppStringprepException, KeyManagementException, NoSuchAlgorithmException {
068        EntityBareJid jid = JidCreate.entityBareFrom(jidCs);
069        return tlsTest(jid, password, host, port, tlsPin, shouldThrow);
070    }
071
072    public static boolean tlsTest(EntityBareJid jid, String password, String host, int port,
073                    String tlsPin, boolean shouldThrow) throws KeyManagementException, NoSuchAlgorithmException {
074        XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
075        // @formatter:off
076        builder.setUsernameAndPassword(jid.getLocalpart(), password)
077               .setXmppDomain(JidCreate.domainBareFrom(jid.getDomain()))
078               .setHost(host)
079               .setPort(port)
080               .setSecurityMode(SecurityMode.required);
081        // @formatter:on
082        if (DEBUG) {
083            builder.enableDefaultDebugger();
084        }
085
086        if (StringUtils.isNotEmpty(tlsPin)) {
087            SSLContext sslContext = Java7Pinning.forPin(tlsPin);
088            builder.setCustomSSLContext(sslContext);
089        }
090
091
092        XMPPTCPConnection connection = new XMPPTCPConnection(builder.build());
093
094        connection.setReplyTimeout(20000);
095
096        try {
097            connection.connect().login();
098            if (shouldThrow) {
099                // Test not success, should have thrown on login().
100                return false;
101            }
102        }
103        catch (SecurityRequiredByClientException e) {
104            if (!shouldThrow) {
105                return false;
106            }
107        }
108        catch (XMPPException | SmackException | IOException | InterruptedException e) {
109            throw new IllegalStateException(e);
110        }
111        finally {
112            connection.disconnect();
113        }
114
115        return true;
116    }
117}