Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/net/ssl/compatibility/Utils.java
38853 views
/*1* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayInputStream;24import java.io.IOException;25import java.net.SocketTimeoutException;26import java.security.KeyFactory;27import java.security.KeyStore;28import java.security.NoSuchAlgorithmException;29import java.security.PrivateKey;30import java.security.cert.Certificate;31import java.security.cert.CertificateFactory;32import java.security.spec.InvalidKeySpecException;33import java.security.spec.PKCS8EncodedKeySpec;3435import javax.net.ssl.KeyManagerFactory;36import javax.net.ssl.SSLContext;37import javax.net.ssl.SSLHandshakeException;38import javax.net.ssl.TrustManagerFactory;3940/*41* Utilities for testing.42*/43public class Utils {4445/* ***** Properties ***** */46public static final String PROP_PORT = "test.port";47public static final String PROP_PROTOCOL = "test.protocol";48public static final String PROP_CIPHER_SUITE = "test.cipher.suite";49public static final String PROP_CLIENT_AUTH = "test.client.auth";50public static final String PROP_SERVER_JDK = "test.server.jdk";51public static final String PROP_CLIENT_JDK = "test.client.jdk";52public static final String PROP_SERVER_NAME = "test.server.name";53public static final String PROP_APP_PROTOCOLS54= "test.app.protocols";55public static final String PROP_NEGO_APP_PROTOCOL56= "test.negotiated.app.protocol";57public static final String PROP_SUPPORTS_SNI_ON_SERVER58= "test.supports.sni.on.server";59public static final String PROP_SUPPORTS_SNI_ON_CLIENT60= "test.supports.sni.on.client";61public static final String PROP_SUPPORTS_ALPN_ON_SERVER62= "test.supports.alpn.on.server";63public static final String PROP_SUPPORTS_ALPN_ON_CLIENT64= "test.supports.alpn.on.client";65public static final String PROP_NEGATIVE_CASE_ON_SERVER66= "test.negative.case.on.server";67public static final String PROP_NEGATIVE_CASE_ON_CLIENT68= "test.negative.case.on.client";6970public static final int TIMEOUT = 10000;71public static final char[] PASSWORD = "testpass".toCharArray();7273public static final String TEST_LOG = "test.html";74public static final String PORT_LOG = "port";7576public static final String HTTP_2 = "h2";77public static final String HTTP_1_1 = "http/1.1";7879public static final String PARAM_DELIMITER = ";";80public static final String VALUE_DELIMITER = ",";8182/*83* Creates SSL context with the specified certificate.84*/85public static SSLContext createSSLContext(Cert... certs) throws Exception {86KeyStore trustStore = KeyStore.getInstance("JKS");87trustStore.load(null, null);88for (int i = 0; i < certs.length; i++) {89trustStore.setCertificateEntry("trust-" + certs[i].name(),90createCert(certs[i]));91}92TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX");93tmf.init(trustStore);9495KeyStore keyStore = KeyStore.getInstance("JKS");96keyStore.load(null, null);97for (int i = 0; i < certs.length; i++) {98PrivateKey privKey = createKey(certs[i]);99keyStore.setKeyEntry("cert-" + certs[i].name(), privKey, PASSWORD,100new Certificate[] { createCert(certs[i]) });101}102KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509");103kmf.init(keyStore, PASSWORD);104105SSLContext context = SSLContext.getInstance("TLS");106context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);107return context;108}109110private static Certificate createCert(Cert cert) throws IOException {111try {112CertificateFactory certFactory113= CertificateFactory.getInstance("X.509");114return certFactory.generateCertificate(115new ByteArrayInputStream(cert.certMaterials.getBytes()));116} catch (Exception e) {117throw new RuntimeException("Create key failed: " + cert, e);118}119}120121private static PrivateKey createKey(Cert cert)122throws NoSuchAlgorithmException, InvalidKeySpecException {123PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(124hexToBytes(cert.privKeyMaterials));125KeyFactory keyFactory = KeyFactory.getInstance(126cert.keyAlgorithm.name);127PrivateKey privKey = keyFactory.generatePrivate(privKeySpec);128return privKey;129}130131public static byte[] hexToBytes(String hex) {132if (hex == null) {133return null;134}135136int length = hex.length();137if (length % 2 != 0) {138throw new IllegalArgumentException("Hex format is wrong.");139}140141byte[] bytes = new byte[length / 2];142for (int i = 0; i < length; i += 2) {143bytes[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)144+ Character.digit(hex.charAt(i + 1), 16));145}146return bytes;147}148149public static String join(String delimiter, String... values) {150StringBuilder result = new StringBuilder();151if (values != null && values.length > 0) {152for (int i = 0; i < values.length - 1; i++) {153result.append(values[i]).append(delimiter);154}155result.append(values[values.length - 1]);156}157return result.toString();158}159160public static String[] split(String str, String delimiter) {161return str == null ? new String[0] : str.split(delimiter);162}163164public static String boolToStr(boolean bool) {165return bool ? "Y" : "N";166}167168public static Status handleException(Exception exception,169boolean negativeCase) {170Status status;171if ((exception instanceof SSLHandshakeException172|| exception instanceof IllegalArgumentException)173&& negativeCase) {174System.out.println("Expected exception: " + exception);175status = Status.EXPECTED_FAIL;176} else if (exception instanceof SocketTimeoutException) {177status = Status.TIMEOUT;178} else {179exception.printStackTrace(System.out);180status = Status.FAIL;181}182return status;183}184185/* The HTML-related constants and methods. */186187private static final String STYLE188= "style=\"font-family: Courier New; "189+ "font-size: 12px; "190+ "white-space: pre-wrap\"";191192private static final String TABLE_STYLE193= "#test { font-family: \"Courier New\"; font-size: 12px; border-collapse: collapse; }\n"194+ "#test td { border: 1px solid #ddd; padding: 4px; }\n"195+ "#test tr:nth-child(odd) { background-color: #f2f2f2; }";196197public static String row(Object... values) {198StringBuilder row = new StringBuilder();199row.append(startTr());200for (Object value : values) {201row.append(startTd());202row.append(value);203row.append(endTd());204}205row.append(endTr());206return row.toString();207}208209public static String startHtml() {210return startTag("html");211}212213public static String endHtml() {214return endTag("html");215}216217public static String startPre() {218return startTag("pre " + STYLE);219}220221public static String endPre() {222return endTag("pre");223}224225public static String anchorName(String name, String text) {226return "<a name=" + name + ">" + text + "</a>";227}228229public static String anchorLink(String file, String anchorName,230String text) {231return "<a href=" + file + "#" + anchorName + ">" + text + "</a>";232}233234public static String tableStyle() {235return startTag("style") + TABLE_STYLE +endTag("style");236}237238public static String startTable() {239return startTag("table id=\"test\"");240}241242public static String endTable() {243return endTag("table");244}245246private static String startTr() {247return startTag("tr");248}249250private static String endTr() {251return endTag("tr");252}253254private static String startTd() {255return startTag("td");256}257258private static String endTd() {259return endTag("td");260}261262private static String startTag(String tag) {263return "<" + tag + ">";264}265266private static String endTag(String tag) {267return "</" + tag + ">";268}269}270271272