|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 Benjamin W. ([email protected]) |
| 3 | + * |
| 4 | + * This file is part of TLSServerScanner. |
| 5 | + * |
| 6 | + * TLSServerScanner is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * TLSServerScanner is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with TLSServerScanner. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.bitbatzen.tlsserverscanner; |
| 21 | + |
| 22 | +import java.awt.Color; |
| 23 | +import java.awt.Font; |
| 24 | +import java.io.BufferedReader; |
| 25 | +import java.io.Closeable; |
| 26 | +import java.io.File; |
| 27 | +import java.io.FileOutputStream; |
| 28 | +import java.io.FileReader; |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.OutputStreamWriter; |
| 31 | +import java.net.URI; |
| 32 | +import java.net.URISyntaxException; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +import org.bitbatzen.tlsserverscanner.gui.HostListItem; |
| 36 | +import org.bitbatzen.tlsserverscanner.scantask.Cert; |
| 37 | + |
| 38 | + |
| 39 | +public class Util { |
| 40 | + |
| 41 | + public static final String T_APP_NAME = "TLSServerScanner"; |
| 42 | + public static final String T_APP_VERSION = "1.0.1"; |
| 43 | + public static final String T_APP_LICENSE = "GPLv3"; |
| 44 | + public static final String T_AUTOR = "Benjamin W."; |
| 45 | + public static final String T_CONTACT_EMAIL = "[email protected]"; |
| 46 | + public static final String T_CODE_URL = "https://github.com/bewue/tlsserverscanner"; |
| 47 | + |
| 48 | + public static final String LF = System.lineSeparator(); |
| 49 | + public static final String BR = "<br>"; |
| 50 | + |
| 51 | + public static final String COLOR_END_TAG = "</font>"; |
| 52 | + public static final String FONT_END_TAG = "</font>"; |
| 53 | + |
| 54 | + |
| 55 | + public static String getJavaVersionString() { |
| 56 | + return System.getProperty("java.version"); |
| 57 | + } |
| 58 | + |
| 59 | + public static int getMajorJavaVersion() { |
| 60 | + String[] javaVersionElements = System.getProperty("java.version").split("\\."); |
| 61 | + return Integer.parseInt(javaVersionElements[1]); |
| 62 | + } |
| 63 | + |
| 64 | + public static int extractPort(String hostAndPort) { |
| 65 | + int port = -1; |
| 66 | + try { |
| 67 | + URI uri = new URI("test://" + hostAndPort); |
| 68 | + port = uri.getPort(); |
| 69 | + } |
| 70 | + catch (URISyntaxException ex) { |
| 71 | + return -1; |
| 72 | + } |
| 73 | + |
| 74 | + return isPortValid(port) ? port : -1; |
| 75 | + } |
| 76 | + |
| 77 | + public static String extractHost(String hostAndPort) { |
| 78 | + String host = null; |
| 79 | + try { |
| 80 | + URI uri = new URI("test://" + hostAndPort); |
| 81 | + host = uri.getHost(); |
| 82 | + } |
| 83 | + catch (URISyntaxException ex) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + return host; |
| 88 | + } |
| 89 | + |
| 90 | + public static boolean isPortValid(int port) { |
| 91 | + return (port >= 0 && port <= 65535); |
| 92 | + } |
| 93 | + |
| 94 | + public static boolean checkHostString(String hostAndPort) { |
| 95 | + return (extractHost(hostAndPort) != null && extractPort(hostAndPort) != -1); |
| 96 | + } |
| 97 | + |
| 98 | + public static String getBGColorTag(Color c) { |
| 99 | + String hex = Integer.toHexString(c.getRGB()).substring(2).toUpperCase(); |
| 100 | + return "<font style=\"background-color: " + hex + "\">"; |
| 101 | + } |
| 102 | + |
| 103 | + public static String getFGColorTag(Color c) { |
| 104 | + String hex = Integer.toHexString(c.getRGB()).substring(2).toUpperCase(); |
| 105 | + return "<font style=\"color: " + hex + "\">"; |
| 106 | + } |
| 107 | + |
| 108 | + public static String getFontFamilyTag(Font font) { |
| 109 | + String tag = "<font style=\"font-family: " + font.getFamily() + "\">"; |
| 110 | + return tag; |
| 111 | + } |
| 112 | + |
| 113 | + public static void saveHostlistToFile(String hostlist, File file) throws Exception { |
| 114 | + OutputStreamWriter osw = null; |
| 115 | + try { |
| 116 | + FileOutputStream fos = new FileOutputStream(file); |
| 117 | + osw = new OutputStreamWriter(fos, "UTF-8"); |
| 118 | + osw.write(hostlist); |
| 119 | + osw.flush(); |
| 120 | + } |
| 121 | + catch (Exception e) { |
| 122 | + throw e; |
| 123 | + } |
| 124 | + finally { |
| 125 | + close(osw); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @param hostlistToFill |
| 131 | + * @param file |
| 132 | + * @return 0 on success or the line number with the syntax error |
| 133 | + * @throws Exception |
| 134 | + */ |
| 135 | + public static int loadHostlistFromFile(List<String> hostlistToFill, File file) throws Exception { |
| 136 | +// List<String> hostlist = new ArrayList<String>(); |
| 137 | + BufferedReader br = null; |
| 138 | + try { |
| 139 | + FileReader fileReader = new FileReader(file); |
| 140 | + br = new BufferedReader(fileReader); |
| 141 | + String line; |
| 142 | + int lineCounter = 1; |
| 143 | + while ((line = br.readLine()) != null) { |
| 144 | + if (checkHostString(line)) { |
| 145 | + hostlistToFill.add(line); |
| 146 | + } |
| 147 | + else { |
| 148 | + return lineCounter; |
| 149 | + } |
| 150 | + lineCounter++; |
| 151 | + } |
| 152 | + } |
| 153 | + catch (Exception e) { |
| 154 | + throw e; |
| 155 | + } |
| 156 | + finally { |
| 157 | + close(br); |
| 158 | + } |
| 159 | + |
| 160 | + return 0; |
| 161 | + } |
| 162 | + |
| 163 | + public static void saveCertificatesToDirectory(List<HostListItem> hostlist, File directory) throws Exception { |
| 164 | + FileOutputStream fos = null; |
| 165 | + try { |
| 166 | + for (HostListItem host : hostlist) { |
| 167 | + if (host.getScanTask() == null || host.getScanTask().getScanData().getCertAvailable() == false) { |
| 168 | + continue; |
| 169 | + } |
| 170 | + |
| 171 | + String id = host.getHost() + "_" + host.getPort(); |
| 172 | + File subDirectory = new File(directory.getPath() + File.separator + id); |
| 173 | + subDirectory.mkdir(); |
| 174 | + |
| 175 | + Cert[] certs = host.getScanTask().getScanData().certs; |
| 176 | + for (int i = 0; i < certs.length; i++) { |
| 177 | + Cert cert = certs[i]; |
| 178 | + String filename = ""; |
| 179 | + if (i == 0) { |
| 180 | + filename = id + ".cert"; |
| 181 | + } |
| 182 | + else { |
| 183 | + filename = "issuer_" + i + ".cert"; |
| 184 | + } |
| 185 | + |
| 186 | + File file = new File(subDirectory.getPath() + File.separator + filename); |
| 187 | + byte[] buffer = cert.getCert().getEncoded(); |
| 188 | + fos = new FileOutputStream(file); |
| 189 | + fos.write(buffer); |
| 190 | + fos.close(); |
| 191 | + fos.flush(); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + catch (Exception e) { |
| 196 | + throw e; |
| 197 | + } |
| 198 | + finally { |
| 199 | + close(fos); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + public static void close(Closeable closeable) { |
| 204 | + if (closeable != null) { |
| 205 | + try { |
| 206 | + closeable.close(); |
| 207 | + } |
| 208 | + catch (IOException e) { |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + public static String bytesToHex(byte[] bytes, boolean addWhitespaces) { |
| 214 | + final char[] hexArray = "0123456789ABCDEF".toCharArray(); |
| 215 | + char[] hexChars = new char[bytes.length * 2]; |
| 216 | + for (int i = 0; i < bytes.length; i++) { |
| 217 | + int v = bytes[i] & 0xFF; |
| 218 | + hexChars[i * 2] = hexArray[v >>> 4]; |
| 219 | + hexChars[i * 2 + 1] = hexArray[v & 0x0F]; |
| 220 | + } |
| 221 | + |
| 222 | + if (addWhitespaces) { |
| 223 | + StringBuilder sb = new StringBuilder(128); |
| 224 | + for (int i = 0; i < hexChars.length; i++) { |
| 225 | + if (i % 2 == 0) { |
| 226 | + sb.append(hexChars[i]); |
| 227 | + } |
| 228 | + else { |
| 229 | + sb.append(hexChars[i] + " "); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + return sb.toString(); |
| 234 | + } |
| 235 | + else { |
| 236 | + return new String(hexChars); |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments