001/** 002 * 003 * Copyright 2016 Florian Schmaus 004 * 005 * This file is part of smack-repl. 006 * 007 * smack-repl is free software; you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License as published by 009 * the Free Software Foundation; either version 3 of the License, or 010 * (at your option) any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with this program; if not, write to the Free Software Foundation, 019 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 020 */ 021package org.igniterealtime.smack.smackrepl; 022 023import java.io.IOException; 024import java.security.KeyManagementException; 025import java.security.NoSuchAlgorithmException; 026 027import javax.net.ssl.SSLContext; 028 029import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode; 030import org.jivesoftware.smack.SmackException; 031import org.jivesoftware.smack.SmackException.SecurityRequiredByClientException; 032import org.jivesoftware.smack.XMPPException; 033import org.jivesoftware.smack.tcp.XMPPTCPConnection; 034import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; 035import org.jivesoftware.smack.util.StringUtils; 036 037import eu.geekplace.javapinning.java7.Java7Pinning; 038import org.jxmpp.jid.EntityBareJid; 039import org.jxmpp.jid.impl.JidCreate; 040import org.jxmpp.stringprep.XmppStringprepException; 041 042public class TlsTest { 043 044// private static final Logger LOGGER = Logger.getLogger(TlsTest.class.getName()); 045 public static boolean DEBUG = false; 046 047 public static void main(String[] args) throws XmppStringprepException, KeyManagementException, NoSuchAlgorithmException { 048 DEBUG = true; 049 tlsTest(args[0], args[1], args[2], args[3], args[4], args[5], true); 050 } 051 052 public static void tlsTest(String runsString, CharSequence jidCs, String password, String host, String portString, 053 String tlsPin, boolean shouldThrow) throws XmppStringprepException, KeyManagementException, NoSuchAlgorithmException { 054 int runs = Integer.parseInt(runsString); 055 int port = Integer.parseInt(portString); 056 tlsTest(runs, jidCs, password, host, port, tlsPin, shouldThrow); 057 } 058 059 public static void tlsTest(int runs, CharSequence jidCs, String password, String host, int port, 060 String tlsPin, boolean shouldThrow) throws XmppStringprepException, KeyManagementException, NoSuchAlgorithmException { 061 EntityBareJid jid = JidCreate.entityBareFrom(jidCs); 062 for (int i = 0; i < runs; i++) { 063 boolean res = tlsTest(jid, password, host, port, tlsPin, shouldThrow); 064 if (!res) { 065 throw new IllegalStateException(); 066 } 067 } 068 } 069 070 public static boolean tlsTest(CharSequence jidCs, String password, String host, int port, 071 String tlsPin, boolean shouldThrow) throws XmppStringprepException, KeyManagementException, NoSuchAlgorithmException { 072 EntityBareJid jid = JidCreate.entityBareFrom(jidCs); 073 return tlsTest(jid, password, host, port, tlsPin, shouldThrow); 074 } 075 076 public static boolean tlsTest(EntityBareJid jid, String password, String host, int port, 077 String tlsPin, boolean shouldThrow) throws KeyManagementException, NoSuchAlgorithmException { 078 XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder(); 079 // @formatter:off 080 builder.setUsernameAndPassword(jid.getLocalpart(), password) 081 .setXmppDomain(JidCreate.domainBareFrom(jid.getDomain())) 082 .setHost(host) 083 .setPort(port) 084 .setSecurityMode(SecurityMode.required); 085 // @formatter:on 086 if (DEBUG) { 087 builder.enableDefaultDebugger(); 088 } 089 090 if (StringUtils.isNotEmpty(tlsPin)) { 091 SSLContext sslContext = Java7Pinning.forPin(tlsPin); 092 builder.setSslContextFactory(() -> sslContext); 093 } 094 095 096 XMPPTCPConnection connection = new XMPPTCPConnection(builder.build()); 097 098 connection.setReplyTimeout(20000); 099 100 try { 101 connection.connect().login(); 102 if (shouldThrow) { 103 // Test not success, should have thrown on login(). 104 return false; 105 } 106 } 107 catch (SecurityRequiredByClientException e) { 108 if (!shouldThrow) { 109 return false; 110 } 111 } 112 catch (XMPPException | SmackException | IOException | InterruptedException e) { 113 throw new IllegalStateException(e); 114 } 115 finally { 116 connection.disconnect(); 117 } 118 119 return true; 120 } 121}