我正在使用带有HTTPS和有效证书的Jetty,我不确定是否正确,因为密码套件在服务器日志中似乎是SSL_NULL_WITH_NULL_NULL
。但是,客户端日志看起来不错。
长篇大论:我正在附加一个期望Jetty-7.6.10的Java样本和两个用于创建密钥库和信任库的脚本。
JettyHttpsForStackOverflow
一起运行客户端和服务器,或单独运行以解除日志。
create-chains.sh
脚本创建密钥库和信任库。密钥库包含以从临时密钥库生成的根证书颁发机构结束的链。它使用证书颁发机构和中间证书复制了一个真实案例。
create-single-autosigned.sh
脚本也会创建密钥库和信任库,但使用自签名证书。
请注意,SSL_NULL_WITH_NULL_NULL
显示为服务器的密码套件,同时包含两个证书链。
我认为服务器域名没有问题。对于在正确签名的证书中具有与专有名称匹配的域名的计算机上运行的服务器,我遇到了同样的问题。 SSLLab确认我的服务器上的SSL工作正常(B级),谷歌Chrome连接愉快。
我认为Jetty客户端没有问题。当我使用它时,它只是调用我设置的SSLContextFactory
来创建SSLSocket
。令人惊讶的是,在Jetty客户端日志中,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
似乎是正在使用的密码套件。
在Jetty服务器日志中获取SSL_NULL_WITH_NULL_NULL
是否正常?如果没有,如何正确地做到这一点?
create-single-autosigned.sh
#!/bin/bash
rm their-keystore.jks 2> /dev/null
rm my-keystore.jks 2> /dev/null
rm my-truststore.jks 2> /dev/null
echo "===================================================="
echo "Creating fake third-party chain ca2 -> ca1 -> ca ..."
echo "===================================================="
keytool -genkeypair -alias ca -dname cn=ca \
-validity 10000 -keyalg RSA -keysize 2048 \
-ext BasicConstraints:critical=ca:true,pathlen:10000 \
-keystore their-keystore.jks -keypass Keypass -storepass Storepass
keytool -genkeypair -alias ca1 -dname cn=ca1 \
-validity 10000 -keyalg RSA -keysize 2048 \
-keystore their-keystore.jks -keypass Keypass -storepass Storepass
keytool -genkeypair -alias ca2 -dname cn=ca2 \
-validity 10000 -keyalg RSA -keysize 2048 \
-keystore their-keystore.jks -keypass Keypass -storepass Storepass
keytool -certreq -alias ca1 \
-keystore their-keystore.jks -keypass Keypass -storepass Storepass \
| keytool -gencert -alias ca \
-ext KeyUsage:critical=keyCertSign \
-ext SubjectAlternativeName=dns:ca1 \
-keystore their-keystore.jks -keypass Keypass -storepass Storepass \
| keytool -importcert -alias ca1 \
-keystore their-keystore.jks -keypass Keypass -storepass Storepass
#echo "Debug exit" ; exit 0
keytool -certreq -alias ca2 \
-keystore their-keystore.jks -keypass Keypass -storepass Storepass \
| keytool -gencert -alias ca1 \
-ext KeyUsage:critical=keyCertSign \
-ext SubjectAlternativeName=dns:ca2 \
-keystore their-keystore.jks -keypass Keypass -storepass Storepass \
| keytool -importcert -alias ca2 \
-keystore their-keystore.jks -keypass Keypass -storepass Storepass
keytool -list -v -storepass Storepass -keystore their-keystore.jks
echo "===================================================================="
echo "Fake third-party chain generated. Now generating my-keystore.jks ..."
echo "===================================================================="
read -p "Press a key to continue."
# Import authority's certificate chain
keytool -exportcert -alias ca \
-keystore their-keystore.jks -keypass Keypass -storepass Storepass \
| keytool -importcert -trustcacerts -noprompt -alias ca \
-keystore my-keystore.jks -keypass Keypass -storepass Storepass
keytool -exportcert -alias ca1 \
-keystore their-keystore.jks -keypass Keypass -storepass Storepass \
| keytool -importcert -noprompt -alias ca1 \
-keystore my-keystore.jks -keypass Keypass -storepass Storepass
keytool -exportcert -alias ca2 \
-keystore their-keystore.jks -keypass Keypass -storepass Storepass \
| keytool -importcert -noprompt -alias ca2 \
-keystore my-keystore.jks -keypass Keypass -storepass Storepass
# Create our own certificate, the authority signs it.
keytool -genkeypair -alias e1 -dname cn=e1 \
-validity 10000 -keyalg RSA -keysize 2048 \
-keystore my-keystore.jks -keypass Keypass -storepass Storepass
keytool -certreq -alias e1 \
-keystore my-keystore.jks -keypass Keypass -storepass Storepass \
| keytool -gencert -alias ca2 \
-ext SubjectAlternativeName=dns:localhost,ip:127.0.0.1 \
-ext KeyUsage:critical=keyEncipherment,digitalSignature \
-ext ExtendedKeyUsage=serverAuth,clientAuth \
-keystore their-keystore.jks -keypass Keypass -storepass Storepass \
| keytool -importcert -alias e1 \
-keystore my-keystore.jks -keypass Keypass -storepass Storepass
keytool -list -v -storepass Storepass -keystore my-keystore.jks
echo "================================================="
echo "Keystore generated. Now generating truststore ..."
echo "================================================="
read -p "Press a key to continue."
keytool -exportcert -alias ca \
-keystore my-keystore.jks -keypass Keypass -storepass Storepass \
| keytool -importcert -trustcacerts -noprompt -alias ca \
-keystore my-truststore.jks -keypass Keypass -storepass Storepass
keytool -exportcert -alias ca1 \
-keystore my-keystore.jks -keypass Keypass -storepass Storepass \
| keytool -importcert -noprompt -alias ca1 \
-keystore my-truststore.jks -keypass Keypass -storepass Storepass
keytool -exportcert -alias ca2 \
-keystore my-keystore.jks -keypass Keypass -storepass Storepass \
| keytool -importcert -noprompt -alias ca2 \
-keystore my-truststore.jks -keypass Keypass -storepass Storepass
keytool -exportcert -alias e1 \
-keystore my-keystore.jks -keypass Keypass -storepass Storepass \
| keytool -importcert -noprompt -alias e1 \
-keystore my-truststore.jks -keypass Keypass -storepass Storepass
keytool -list -v -storepass Storepass -keystore my-truststore.jks
rm their-keystore.jks 2> /dev/null
create-single-autosigned.sh
#!/bin/bash
rm my-keystore.jks 2> /dev/null
rm my-truststore.jks 2> /dev/null
keytool -genkeypair -alias e1 -dname cn=e1 \
-validity 10000 -keyalg RSA -keysize 2048 \
-keystore my-keystore.jks -keypass Keypass -storepass Storepass
keytool -list -v -storepass Storepass -keystore my-keystore.jks
echo "================================================="
echo "Keystore generated. Now generating truststore ..."
echo "================================================="
read -p "Press a key to continue."
keytool -exportcert -alias e1 \
-keystore my-keystore.jks -keypass Keypass -storepass Storepass \
| keytool -importcert -noprompt -alias e1 \
-keystore my-truststore.jks -keypass Keypass -storepass Storepass
keytool -list -v -storepass Storepass -keystore my-truststore.jks
JettyHttpsForStackOverflow.java
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import org.eclipse.jetty.client.ContentExchange;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
import org.eclipse.jetty.util.ssl.SslContextFactory;
/**
* Code sample for Jetty {@link HttpClient} with HTTPS, in a completely standalone fashion.
* Use create-chains.sh and create-empty.sh to generate completely standalone certificates.
*/
public class JettyHttpsForStackOverflow {
public static void main( final String... arguments ) throws Exception {
System.setProperty( "javax.net.debug", "all" ) ;
try {
if( arguments.length == 0 || "server".equals( arguments[ 0 ] ) ) {
runServer() ;
}
if( arguments.length == 0 || "client".equals( arguments[ 0 ] ) ) {
runClient() ;
}
} catch( Exception e ) {
e.printStackTrace() ;
System.exit( 1 ) ; // Avoids keeping the port open.
}
}
private static void runServer() throws Exception {
final KeyStore keyStore = loadKeystore() ;
final SSLContext sslContext = createSslContext(
keyStore,
KEYPASS,
newTrustManagers( keyStore, CERTIFICATE_ALIAS )
) ;
final SslContextFactory sslContextFactory = new SslContextFactory() {
@Override
public SSLEngine newSslEngine() {
return sslContext.createSSLEngine() ;
}
@Override
public SSLEngine newSslEngine( final String host, final int port ) {
return sslContext.createSSLEngine( host, port ) ;
}
} ;
sslContextFactory.setAllowRenegotiate( true ) ;
sslContextFactory.setNeedClientAuth( false ) ;
sslContextFactory.setWantClientAuth( false ) ;
sslContextFactory.setKeyStorePath( keyStore.toString() ) ; // Better logging.
sslContextFactory.setKeyStore( keyStore ) ;
sslContextFactory.setCertAlias( CERTIFICATE_ALIAS ) ;
sslContextFactory.setKeyManagerPassword( KEYPASS ) ;
final SslSelectChannelConnector sslConnector =
new SslSelectChannelConnector( sslContextFactory ) ;
sslConnector.setPort( PORT ) ;
sslConnector.open() ;
final Server jettyServer = new Server() ;
jettyServer.addConnector( sslConnector ) ;
jettyServer.start() ;
}
public static void runClient() throws Exception {
final KeyStore keyStore = loadTruststore() ;
final HttpClient httpClient = new HttpClient() ;
httpClient.getSslContextFactory().setKeyStore( keyStore ) ; // Better logging.
httpClient.getSslContextFactory().setKeyStorePassword( "storepwd" ) ;
httpClient.getSslContextFactory().setKeyManagerPassword( KEYPASS ) ;
httpClient.setConnectorType( HttpClient.CONNECTOR_SELECT_CHANNEL ) ;
httpClient.setConnectorType(HttpClient.CONNECTOR_SOCKET);
// Don't need that because shipping our own certificate in the truststore.
// Anyways, it blows when set to true.
// httpClient.getSslContextFactory().setValidateCerts( false ) ;
httpClient.start() ;
final ContentExchange contentExchange = new ContentExchange() ;
contentExchange.setURI( new URL( "https://localhost:" + PORT ).toURI() ) ;
contentExchange.setTimeout( 36_000_000 ) ; // Leave time for debugging.
httpClient.send( contentExchange ) ;
contentExchange.waitForDone() ;
assert( contentExchange.getStatus() == ContentExchange.STATUS_COMPLETED ) ;
}
private static SSLContext createSslContext(
final KeyStore keyStore,
final String keypass,
final TrustManager[] trustManagers
) {
try {
final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance( "SunX509" ) ;
keyManagerFactory.init( keyStore, keypass == null ? null : keypass.toCharArray() ) ;
final KeyManager[] keyManagers = keyManagerFactory.getKeyManagers() ;
final SecureRandom secureRandom = new SecureRandom() ;
final SSLContext sslContext = SSLContext.getInstance( "TLS" ) ;
sslContext.init(
keyManagers,
trustManagers,
secureRandom
) ;
return sslContext ;
} catch( NoSuchAlgorithmException | UnrecoverableKeyException | KeyStoreException
| KeyManagementException e
) {
throw new RuntimeException( e ) ;
}
}
private static TrustManager[] newTrustManagers(
final KeyStore keyStore,
final String certificateAlias
) {
try {
final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance( "SunX509" ) ;
trustManagerFactory.init( keyStore ) ;
final TrustManager[] trustManagers ;
if( certificateAlias == null ) {
trustManagers = trustManagerFactory.getTrustManagers() ;
} else {
final Certificate certificate = keyStore.getCertificate( certificateAlias ) ;
final X509Certificate[] x509Certificates ;
if( certificate == null ) {
x509Certificates = new X509Certificate[ 0 ] ;
} else {
x509Certificates = new X509Certificate[] { ( X509Certificate ) certificate } ;
}
trustManagers = new TrustManager[] { newX509TrustManager( x509Certificates ) } ;
}
return trustManagers ;
} catch( KeyStoreException | NoSuchAlgorithmException e ) {
throw new RuntimeException( e );
}
}
private static final TrustManager newX509TrustManager( final X509Certificate[] certificates ) {
return new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return certificates ;
}
public void checkClientTrusted(
final X509Certificate[] certs,
final String authType
) { ; }
public void checkServerTrusted(
final X509Certificate[] certs,
final String authType
) { ; }
} ;
}
public static KeyStore loadKeystore()
throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException
{
return loadKeystore( KEYSTORE_RESOURCE_URL ) ;
}
public static KeyStore loadTruststore()
throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException
{
return loadKeystore( TRUSTSTORE_RESOURCE_URL ) ;
}
public static KeyStore loadKeystore( final URL keystoreResourceUrl )
throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException
{
try( final InputStream inputStream = keystoreResourceUrl.openStream() ) {
final KeyStore keyStore = KeyStore.getInstance( "JKS" ) ;
// We don't need the storepass for just reading one password-protected certificate
// of our own, or a trusted entry.
keyStore.load( inputStream, null ) ;
return keyStore ;
}
}
private static final int PORT = 8443 ;
private static final String CERTIFICATE_ALIAS = "e1";
private static final String KEYPASS = "Keypass";
private static final URL KEYSTORE_RESOURCE_URL
= JettyHttpsForStackOverflow.class.getResource( "my-keystore.jks" ) ;
private static final URL TRUSTSTORE_RESOURCE_URL
= JettyHttpsForStackOverflow.class.getResource( "my-truststore.jks" ) ;
}
答案 0 :(得分:15)
事实证明Jetty-7.6.10.v20130312中的SslConnection
记录不正确,而加密应该发生。
长篇故事:SslConnection
在创建时,会从SSLSession
中提取初始SSLEngine
对象并继续使用它进行记录。初始SSLSession
有一个SSL_NULL_WITH_NULL_NULL
密码,这是正常的,因为SSL握手尚未发生。激活-Djavax.net.debug=all
表明握手确实发生,并且交互式调试显示SSLEngine
使用真实密码升级到SSLSession
。问题只是Jetty的SslConnection
仍然使用初始SSLSession
对象进行记录。 (它还使用初始SSLSession
的值来分配缓冲区,但这是另一个问题。)
使用SslConnection
修补_engine.getSession()
以获取预期结果。
Epilogue:Jetty 9完全重写了它的SslConnection
。
答案 1 :(得分:1)
至少在以下情况下你会看到这一点:
您已修改enabledCipherSuites
以包含所有受支持的密码套件。 (不!)
SSL握手尚未完成。