public class SSLClientContext extends SSLContext implements java.lang.Cloneable
Note that there is a separate document about the iSaSiLk authentication framework, see your installation directory.
A SSLClientContext
is used on the client side to tell
iSaSiLk the TLS security parameters like cipher suites, key, certificates
to be used.
After having created
an SSLClientContext,
you may set cipher suites
, client credentials
(if needed for authenticating against the server), trusted certificates
and
any other parameters you may require for your client configuration, e.g.:
// create client context SSLClientContext context = new SSLClientContext(); // key and certificate for client authentication PrivateKey clientKey = ...; X509Certificate[] clientCertChain = ...; context.addClientCredentials(clientCertChain, clientKey); // add some trust anchor(s) X509Certificate trustAnchor = ...; context.addTrustedCertificate(trustAnchor); // enable all default cipher suites CipherSuiteList suites = new CipherSuiteList(CipherSuiteList.L_DEFAULT); context.setEnabledCipherSuiteList(suites); context.updateCipherSuites(); ...If you want to use TLS extensions you can set them as
ExtensionList
, e.g. (for using the server_name
extension):
ServerNameList serverNameList = new ServerNameList(); // add to ExtensionList ExtensionList extensions = new ExtensionList(); extensions.addExtension(serverNameList); // set extensions for the SSLClientContext configuration: context.setExtensions(extensions);Finally, when having finished your configuration, set your
SSLClientContext
for the SSLTransport
implementation you are using. Most
commonly you will use an SSLSocket
to connect to
the server, e.g.:
// the name of the server String serverName = ...; // the port on which the server is listening int serverPort = 443; SSLSocket socket = new SSLSocket(serverName, serverPort, context); // print debug info to System.out socket.setDebugStream(System.out); // send GET-request System.out.println("Sending HTTPS request to " + serverName); PrintWriter writer = Utils.getASCIIWriter(socket.getOutputStream()); BufferedReader reader = Utils.getASCIIReader(socket.getInputStream()); writer.println("GET / HTTP/1.0"); writer.println(); writer.flush(); // read response System.out.println("Reading response..."); while (true) { String line = reader.readLine(); if (line == null) { break; } System.out.print(":"); System.out.println(line); } // close streams and socket writer.close(); reader.close(); socket.close();
SSLContext
,
SSLClientContext
AVAIL_MODE_DECRYPTED, AVAIL_MODE_ENCRYPTED, AVAIL_MODE_ONE_BYTE, CERTTYPE_DSS_FIXED_DH, CERTTYPE_DSS_SIGN, CERTTYPE_ECDSA_EC, CERTTYPE_ECDSA_FIXED_ECDH, CERTTYPE_ECDSA_SIGN, CERTTYPE_RSA_ENCRYPT, CERTTYPE_RSA_FIXED_DH, CERTTYPE_RSA_FIXED_ECDH, CERTTYPE_RSA_SIGN, CERTTYPE_UNKNOWN, LIBRARY_VERSION, LIBRARY_VERSION_STRING, SEND_EMPTY_FRAGMENT, VERSION_NOT_CONNECTED, VERSION_SSL20, VERSION_SSL30, VERSION_TLS10, VERSION_TLS11, VERSION_TLS12, VERSION_TLS13
Constructor and Description |
---|
SSLClientContext()
Create a default SSLContext.
|
SSLClientContext(CipherSuiteList cipherSuiteList)
Create a SSLContext with the given cipher suite list.
|
SSLClientContext(java.security.SecureRandom random)
Create a default SSLContext with a specific random number generator.
|
SSLClientContext(java.security.SecureRandom random,
CipherSuiteList cipherSuiteList)
Create a SSLContext with a specific random number generator and given cipher suite list.
|
SSLClientContext(SSLClientContext other)
Create a SSLClientContext cloning another context.
|
Modifier and Type | Method and Description |
---|---|
void |
addClientCredentials(KeyAndCert keyAndCert)
Add some client credentials.
|
int |
addClientCredentials(java.security.KeyStore keyStore,
char[] password)
Add some client credentials from a KeyStore.
|
int |
addClientCredentials(java.security.KeyStore keyStore,
char[] password,
java.lang.String[] aliases)
Add some client credentials from a KeyStore.
|
int |
addClientCredentials(java.lang.String keyStoreFile,
char[] keyStorePassword,
java.lang.String keyStoreType,
java.lang.String keyStoreProvider)
Add some client credentials from a KeyStore.
|
void |
addClientCredentials(java.security.cert.X509Certificate[] chain,
java.security.PrivateKey key)
Add some client credentials.
|
void |
clearClientCredentials()
Clear the database of client credentials.
|
java.lang.Object |
clone()
Implements the Cloneable interface.
|
protected KeyAndCert[] |
getClientCredentials(java.security.Principal[] issuers,
byte[] certificateTypes)
Deprecated.
use getClientCredentials(issuers, certificateTypes, null) instead
|
protected KeyAndCert[] |
getClientCredentials(java.security.Principal[] issuers,
byte[] certificateTypes,
java.security.PublicKey publicKey)
Gets all matching client credentials.
|
boolean |
getIgnorePSKIdentityHint()
Gets whether a PSK identity hint sent by the server
shall be ignored by the client or not.
|
void |
setCheckExportRestrictions(boolean check)
Set the export restriction check.
|
void |
setIgnorePSKIdentityHint(boolean ignore)
Sets whether a PSK identity hint sent by the server
shall be ignored by the client or not.
|
void |
setReadPostHandshakeMessageAfterFinsished(boolean readMessage)
TLS 1.3: Whether to try to immediately read a post handshake
message after having sent the Finished message.
|
void |
setUseMaxVersionForRSAPremasterSecret(boolean useMaxVersion)
Decides whether to include the max client version into the RSA premaster
secret.
|
java.lang.String |
toString()
Returns a string representation of this SSLClientContext.
|
addPSKCredential, addTrustedCertificate, addTrustedCertificates, addTrustedCertificates, addTrustedCertificates, clearPSKCredentials, convertCertificateChain, getAllowedProtocolVersionNames, getAllowedProtocolVersions, getAllowIdentityChangeDuringRenegotiation, getAllowLegacyRenegotiation, getAllSupportedProtocolVersionNames, getAllSupportedProtocolVersions, getCacheTerminatedSessions, getChainVerifier, getChannelBindings, getDebugStream, getDisableRenegotiation, getDoNotSendServerCloseNotify, getEnabledCipherSuiteList, getEnabledCipherSuites, getEnabledCompressionMethods, getPSKCredential, getPSKManager, getRandomGenerator, getSendEmptyFragment, getSessionManager, getTrustDecider, getUseNoRenegotiationWarnings, setAllowedProtocolVersions, setAllowedProtocolVersions, setAllowIdentityChangeDuringRenegotiation, setAllowLegacyRenegotiation, setCacheTerminatedSessions, setChainVerifier, setChannelBindings, setCheckTypeOfNextHandshakeMessage, setDebugStream, setDebugStream, setDisableRenegotiation, setDoNotPackHandshakeMessages, setDoNotSendServerCloseNotify, setDowngradeMaxVersionToTLS12IfNoTLS13CipherSuitesAvailable, setEnabledCipherSuiteList, setEnabledCipherSuites, setEnabledCompressionMethods, setExtensions, setInputStreamAvailableMode, setPSKCredential, setPSKManager, setRandomGenerator, setRecordOverheadSize, setSendEmptyFragment, setSendRecordOverflowAlert, setSessionManager, setTls13MiddleboxCompatibilityMode, setTLS13WaitOnPeerCloseNotify, setTrustDecider, setUseNoRenegotiationWarnings, setUseRecordSplitting, updateCipherSuites
public SSLClientContext()
public SSLClientContext(java.security.SecureRandom random)
random
- the random number generator to be used or null for the default SecureRandompublic SSLClientContext(CipherSuiteList cipherSuiteList)
cipherSuiteList
- the cipher suite list to be used or null for the default cipher suite listpublic SSLClientContext(java.security.SecureRandom random, CipherSuiteList cipherSuiteList)
random
- the random number generator to be used or null for the default SecureRandomcipherSuiteList
- the cipher suite list to be used or null for the default cipher suite listpublic SSLClientContext(SSLClientContext other)
public java.lang.Object clone()
clone
in class SSLContext
public void setCheckExportRestrictions(boolean check)
public void addClientCredentials(java.security.cert.X509Certificate[] chain, java.security.PrivateKey key)
public void addClientCredentials(KeyAndCert keyAndCert) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
public int addClientCredentials(java.lang.String keyStoreFile, char[] keyStorePassword, java.lang.String keyStoreType, java.lang.String keyStoreProvider) throws java.security.KeyStoreException
Note that this method does not allow the same fine granularity as when
adding
some particular
key and certificate. Any key entry (and its corresponding certificate)
chain) contained in the KeyStore is added to the key/cert database.
If the KeyStore contains SecretKeys, they are added as PSKCredentials
.
All key entries must have the same password.
keyStoreFile
- the name of the KeyStore filekeyStorePassword
- the KeyStore passwordkeyStoreType
- the KeyStore type; default: IAIKKeyStore
keyStoreProvider
- the KeyStore provider; default: IAIK
java.security.KeyStoreException
- if an error occurs while getting keys/certificates
from the KeyStorepublic int addClientCredentials(java.security.KeyStore keyStore, char[] password) throws java.security.KeyStoreException
Note that this method does not allow the same fine granularity as when
adding
some particular
key and certificate. Any key entry (and its corresponding certificate)
chain) contained in the KeyStore is added to the key/cert database.
If the KeyStore contains SecretKeys, they are added as PSKCredentials
.
The KeyStore must have been already loaded when calling this method. All key entries must have the same password.
keyStore
- the KeyStore from which to read the client credentialspassword
- the password for the private/secret key entriesjava.security.KeyStoreException
- if an error occurs while getting keys/certificates
from the KeyStorepublic int addClientCredentials(java.security.KeyStore keyStore, char[] password, java.lang.String[] aliases) throws java.security.KeyStoreException
This method is similar to method addClientCredentials(KeyStore, char[])
except that are only those key entries are added
that are specified by their aliases. If no aliases are specified, all
key entries are added (in this case this method does the same as method
addClientCredentials(KeyStore, char[])
).
If any of the referenced key entries represents a SecretKeys, it is added as PSKCredential
.
The KeyStore must have been already loaded when calling this method. All key entries must have the same password.
keyStore
- the KeyStore from which to read the client credentialspassword
- the password for the private/secret key entriesaliases
- the alias names of the key entries to be addedjava.security.KeyStoreException
- if an error occurs while getting keys/certificates
from the KeyStore, or no key entry is available for
any of the specified alias namespublic void clearClientCredentials()
public void setUseMaxVersionForRSAPremasterSecret(boolean useMaxVersion)
SSL/TLS requires that the max version (sent within the
ClientHello
message) is included in the RSA premaster
secret, however, many implementations send the negotiated version.
By default iSaSiLk uses the max client version as required by SSL/TLS;
you can call this method if you want to use the negotiated version:
... sslClientContext.setUseMaxVersionForRSAPremasterSecret(false);
useMaxVersion
- whether to include the max ClientHello version
into the RSA premaster secretpublic boolean getIgnorePSKIdentityHint()
public void setIgnorePSKIdentityHint(boolean ignore)
ignore
- whether to ignore a psk identity hint sent
by the serverpublic void setReadPostHandshakeMessageAfterFinsished(boolean readMessage)
Usually -- if session resumption is enabled -- a TLS 1.3
server may send a NewSessionTicket post handshake message before exchanging
application data. When setting read_post_handshake_message_after_finsish
to true
the client will try to read a post handshake
message immediately after having sent its Finished message and before
sending application data. Usually this should work without any problems,
however, when the server does not send a NewSessionTicket message
before exchanging application data, the client may block for
the time of the configured read time out before continuing with
sending its application data request. For that reason by default
an iSaSiLk client does not try to read a post handshake message
immediately after having sent the Finished message (and before
sending its application request). However, it might be useful to
enable this feature for debugging purposes (to, for instance, check
if the server has denied the client certificate when having got a
socket write error before being able to exchange any application data.
readMessage
- whether to try to read a post handshake message
after having sent the Finished message (default:
false
) (only meaningful for TLS 1.3)protected KeyAndCert[] getClientCredentials(java.security.Principal[] issuers, byte[] certificateTypes, java.security.PublicKey publicKey)
The publicKey parameter, if not null, is the server's public key for the key exchange. For client authentication with a Diffie-Hellman certificate the parameters from that public key must match the client parameters for the key exchange to work. This method verifies this as well.
This method returns all appropriate credentials or the empty array if no are available. An application may override this method to display a dialog window etc. to the user to allow him select the credentials to use if multiple are available. The library will always use the credentials at index 0 in the returned array. An overridden method may look like this:
protected KeyAndCert[] getClientCredentials(Principal[] issuers, byte[] certificateTypes, PublicKey publicKey) { KeyAndCert[] certs = super.getClientCredentials(issuers, certificateType, publicKey); // allow user to select a certificate or cancel if( selected == -1 ) { return new KeyAndCert[0]; } else { return new KeyAndCert[] { certs[selected] }; } }
issuers
- a list of CA names trusted by the servercertificateTypes
- the certificate types accepted by the serverpublicKey
- the public key of the serverprotected KeyAndCert[] getClientCredentials(java.security.Principal[] issuers, byte[] certificateTypes)
public java.lang.String toString()
toString
in class SSLContext