001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.internal.test.util.http; 020 021import javax.net.ssl.KeyManagerFactory; 022import javax.net.ssl.SSLContext; 023import javax.net.ssl.TrustManagerFactory; 024 025import java.io.IOException; 026import java.io.InputStream; 027import java.io.UncheckedIOException; 028import java.nio.file.Files; 029import java.nio.file.Path; 030import java.nio.file.Paths; 031import java.nio.file.StandardCopyOption; 032import java.security.KeyStore; 033import java.util.Enumeration; 034 035/** 036 * Key and trust material shared by {@link HttpTransporterTest} and {@link HttpServer}: the stores are copied from the 037 * test resources into the build directory, where the server reads them, and the client side gets an 038 * {@link SSLContext} that trusts them. 039 */ 040public final class SslTestSupport { 041 public static final Path SERVER_STORE_PATH = Paths.get("target/server-store"); 042 public static final Path CLIENT_STORE_PATH = Paths.get("target/client-store"); 043 public static final Path PEM_QUICHE_SERVER_PATH = Paths.get("target/pems"); 044 045 private SslTestSupport() {} 046 047 /** 048 * Copies the server and client stores from the test resources to {@link #SERVER_STORE_PATH} and 049 * {@link #CLIENT_STORE_PATH}, and creates {@link #PEM_QUICHE_SERVER_PATH}. 050 */ 051 public static void installKeyStores() { 052 try { 053 try (InputStream keyStoreStream = 054 SslTestSupport.class.getClassLoader().getResourceAsStream("ssl/server-store"); 055 InputStream trustStoreStream = 056 SslTestSupport.class.getClassLoader().getResourceAsStream("ssl/client-store"); ) { 057 Files.copy(keyStoreStream, SERVER_STORE_PATH, StandardCopyOption.REPLACE_EXISTING); 058 Files.copy(trustStoreStream, CLIENT_STORE_PATH, StandardCopyOption.REPLACE_EXISTING); 059 Files.createDirectories(PEM_QUICHE_SERVER_PATH); 060 } 061 } catch (IOException e) { 062 throw new UncheckedIOException(e); 063 } 064 } 065 066 /** 067 * Creates an {@link SSLContext} for the client that extends the default keystore and truststore with the entries 068 * from {@link #SERVER_STORE_PATH} (password {@code "server-pwd"}) and {@link #CLIENT_STORE_PATH} 069 * (password {@code "client-pwd"}). 070 * 071 * @return an {@link SSLContext} combining default and custom key/trust material 072 */ 073 public static SSLContext createClientSSLContext() { 074 try { 075 // Load custom key store (KEY_STORE_PATH acts as truststore in "cross connected" setup) 076 KeyStore customTrustStore = KeyStore.getInstance("pkcs12"); 077 try (InputStream is = Files.newInputStream(SERVER_STORE_PATH)) { 078 customTrustStore.load(is, "server-pwd".toCharArray()); 079 } 080 081 // Load custom trust store (TRUST_STORE_PATH acts as keystore in "cross connected" setup) 082 KeyStore customKeyStore = KeyStore.getInstance("pkcs12"); 083 try (InputStream is = Files.newInputStream(CLIENT_STORE_PATH)) { 084 customKeyStore.load(is, "client-pwd".toCharArray()); 085 } 086 087 // Load default truststore and merge custom entries 088 KeyStore defaultTrustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 089 Path defaultTrustStorePath = Path.of(System.getProperty("java.home"), "lib", "security", "cacerts"); 090 if (Files.exists(defaultTrustStorePath)) { 091 try (InputStream is = Files.newInputStream(defaultTrustStorePath)) { 092 defaultTrustStore.load(is, "changeit".toCharArray()); 093 } 094 } else { 095 defaultTrustStore.load(null, null); 096 } 097 Enumeration<String> aliases = customTrustStore.aliases(); 098 while (aliases.hasMoreElements()) { 099 String alias = aliases.nextElement(); 100 defaultTrustStore.setCertificateEntry("custom-trust-" + alias, customTrustStore.getCertificate(alias)); 101 } 102 103 // Load default keystore and merge custom entries 104 KeyStore mergedKeyStore = KeyStore.getInstance(KeyStore.getDefaultType()); 105 mergedKeyStore.load(null, null); 106 Enumeration<String> keyAliases = customKeyStore.aliases(); 107 while (keyAliases.hasMoreElements()) { 108 String alias = keyAliases.nextElement(); 109 if (customKeyStore.isKeyEntry(alias)) { 110 mergedKeyStore.setKeyEntry( 111 alias, 112 customKeyStore.getKey(alias, "client-pwd".toCharArray()), 113 "client-pwd".toCharArray(), 114 customKeyStore.getCertificateChain(alias)); 115 } else { 116 mergedKeyStore.setCertificateEntry(alias, customKeyStore.getCertificate(alias)); 117 } 118 } 119 120 TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); 121 tmf.init(defaultTrustStore); 122 123 KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 124 kmf.init(mergedKeyStore, "client-pwd".toCharArray()); 125 126 SSLContext sslContext = SSLContext.getInstance("TLS"); 127 sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); 128 return sslContext; 129 } catch (Exception e) { 130 throw new RuntimeException("Failed to create SSLContext", e); 131 } 132 } 133}