BCJSSE: Handle default SNI peerHost via BCSNIHostName instance
Use new ServerName constructor taking SNI encoding directly
package org.bouncycastle.jsse.provider;
import java.io.IOException;
-import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
+import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import javax.security.auth.x500.X500Principal;
import org.bouncycastle.asn1.x500.X500Name;
+import org.bouncycastle.jsse.BCSNIHostName;
import org.bouncycastle.jsse.BCSNIServerName;
import org.bouncycastle.tls.AlertDescription;
import org.bouncycastle.tls.AlertLevel;
import org.bouncycastle.tls.CertificateStatusRequest;
import org.bouncycastle.tls.DefaultTlsClient;
import org.bouncycastle.tls.KeyExchangeAlgorithm;
-import org.bouncycastle.tls.NameType;
import org.bouncycastle.tls.ProtocolVersion;
import org.bouncycastle.tls.ServerName;
import org.bouncycastle.tls.SignatureAndHashAlgorithm;
@@ -88,44 +88,31 @@ protected Vector getSNIServerNames()
if (provEnableSNIExtension)
{
List<BCSNIServerName> sniServerNames = sslParameters.getServerNames();
- if (sniServerNames == null)
+ if (null == sniServerNames)
{
String peerHost = manager.getPeerHost();
if (peerHost != null && peerHost.indexOf('.') > 0 && !IPAddress.isValid(peerHost))
- {
- Vector serverNames = new Vector(1);
- serverNames.addElement(new ServerName(NameType.host_name, peerHost));
- return serverNames;
- }
- }
- else
- {
- Vector serverNames = new Vector(sniServerNames.size());
- for (BCSNIServerName sniServerName : sniServerNames)
- {
- /*
- * TODO[jsse] Add support for constructing ServerName using
- * BCSNIServerName.getEncoded() directly, then remove the 'host_name' limitation
- * (although it's currently the only defined type).
- */
- if (sniServerName.getType() == NameType.host_name)
{
try
{
- serverNames.addElement(new ServerName((short)sniServerName.getType(), new String(sniServerName.getEncoded(), "ASCII")));
+ sniServerNames = Collections.<BCSNIServerName>singletonList(new BCSNIHostName(peerHost));
}
- catch (UnsupportedEncodingException e)
+ catch (RuntimeException e)
{
- LOG.log(Level.WARNING, "Unable to include SNI server name", e);
+ LOG.fine("Failed to add peer host as default SNI host_name: " + peerHost);
}
}
}
// NOTE: We follow SunJSSE behaviour and disable SNI if there are no server names to send
- if (!serverNames.isEmpty())
+ if (null != sniServerNames && !sniServerNames.isEmpty())
{
- return serverNames;
+ Vector serverNames = new Vector(sniServerNames.size());
+ for (BCSNIServerName sniServerName : sniServerNames)
+ {
+ serverNames.addElement(new ServerName((short)sniServerName.getType(), sniServerName.getEncoded()));
}
+ return serverNames;
}
}
return null;
