package org.bouncycastle.crypto.tls;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.SecureRandom;
import java.util.Vector;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.util.PublicKeyFactory;
import org.bouncycastle.util.Arrays;
public class TlsServerProtocol
extends TlsProtocol
{
protected TlsServer tlsServer = null;
TlsServerContextImpl tlsServerContext = null;
protected TlsKeyExchange keyExchange = null;
protected TlsCredentials serverCredentials = null;
protected CertificateRequest certificateRequest = null;
protected short clientCertificateType = -1;
protected TlsHandshakeHash prepareFinishHash = null;
public TlsServerProtocol(InputStream input, OutputStream output, SecureRandom secureRandom)
{
super(input, output, secureRandom);
}
public TlsServerProtocol(SecureRandom secureRandom)
{
super(secureRandom);
}
public void accept(TlsServer tlsServer)
throws IOException
{
if (tlsServer == null)
{
throw new IllegalArgumentException("'tlsServer' cannot be null");
}
if (this.tlsServer != null)
{
throw new IllegalStateException("'accept' can only be called once");
}
this.tlsServer = tlsServer;
this.securityParameters = new SecurityParameters();
this.securityParameters.entity = ConnectionEnd.server;
this.tlsServerContext = new TlsServerContextImpl(secureRandom, securityParameters);
this.securityParameters.serverRandom = createRandomBlock(tlsServer.shouldUseGMTUnixTime(),
tlsServerContext.getNonceRandomGenerator());
this.tlsServer.init(tlsServerContext);
this.recordStream.init(tlsServerContext);
this.recordStream.setRestrictReadVersion(false);
blockForHandshake();
}
protected void cleanupHandshake()
{
super.cleanupHandshake();
this.keyExchange = null;
this.serverCredentials = null;
this.certificateRequest = null;
this.prepareFinishHash = null;
}
protected TlsContext getContext()
{
return tlsServerContext;
}
AbstractTlsContext getContextAdmin()
{
return tlsServerContext;
}
protected TlsPeer getPeer()
{
return tlsServer;
}
protected void handleHandshakeMessage(short type, ByteArrayInputStream buf)
throws IOException
{
switch (type)
{
case HandshakeType.client_hello:
{
switch (this.connection_state)
{
case CS_START:
{
receiveClientHelloMessage(buf);
this.connection_state = CS_CLIENT_HELLO;
sendServerHelloMessage();
this.connection_state = CS_SERVER_HELLO;
recordStream.notifyHelloComplete();
Vector serverSupplementalData = tlsServer.getServerSupplementalData();
if (serverSupplementalData != null)
{
sendSupplementalDataMessage(serverSupplementalData);
}
this.connection_state = CS_SERVER_SUPPLEMENTAL_DATA;
this.keyExchange = tlsServer.getKeyExchange();
this.keyExchange.init(getContext());
this.serverCredentials = tlsServer.getCredentials();
Certificate serverCertificate = null;
if (this.serverCredentials == null)
{
this.keyExchange.skipServerCredentials();
}
else
{
this.keyExchange.processServerCredentials(this.serverCredentials);
serverCertificate = this.serverCredentials.getCertificate();
sendCertificateMessage(serverCertificate);
}
this.connection_state = CS_SERVER_CERTIFICATE;
if (serverCertificate == null || serverCertificate.isEmpty())
{
this.allowCertificateStatus = false;
}
if (this.allowCertificateStatus)
{
CertificateStatus certificateStatus = tlsServer.getCertificateStatus();
if (certificateStatus != null)
{
sendCertificateStatusMessage(certificateStatus);
}
}
this.connection_state = CS_CERTIFICATE_STATUS;
byte[] serverKeyExchange = this.keyExchange.generateServerKeyExchange();
if (serverKeyExchange != null)
{
sendServerKeyExchangeMessage(serverKeyExchange);
}
this.connection_state = CS_SERVER_KEY_EXCHANGE;
if (this.serverCredentials != null)
{
this.certificateRequest = tlsServer.getCertificateRequest();
if (this.certificateRequest != null)
{
if (TlsUtils.isTLSv12(getContext()) != (certificateRequest.getSupportedSignatureAlgorithms() != null))
{
throw new TlsFatalAlert(AlertDescription.internal_error);
}
this.keyExchange.validateCertificateRequest(certificateRequest);
sendCertificateRequestMessage(certificateRequest);
TlsUtils.trackHashAlgorithms(this.recordStream.getHandshakeHash(),
this.certificateRequest.getSupportedSignatureAlgorithms());
}
}
this.connection_state = CS_CERTIFICATE_REQUEST;
sendServerHelloDoneMessage();
this.connection_state = CS_SERVER_HELLO_DONE;
this.recordStream.getHandshakeHash().sealHashAlgorithms();
break;
}
case CS_END:
{
refuseRenegotiation();
break;
}
default:
throw new TlsFatalAlert(AlertDescription.unexpected_message);
}
break;
}
case HandshakeType.supplemental_data:
{
switch (this.connection_state)
{
case CS_SERVER_HELLO_DONE:
{
tlsServer.processClientSupplementalData(readSupplementalDataMessage(buf));
this.connection_state = CS_CLIENT_SUPPLEMENTAL_DATA;
break;
}
default:
throw new TlsFatalAlert(AlertDescription.unexpected_message);
}
break;
}
case HandshakeType.certificate:
{
switch (this.connection_state)
{
case CS_SERVER_HELLO_DONE:
{
tlsServer.processClientSupplementalData(null);
}
case CS_CLIENT_SUPPLEMENTAL_DATA:
{
if (this.certificateRequest == null)
{
throw new TlsFatalAlert(AlertDescription.unexpected_message);
}
receiveCertificateMessage(buf);
this.connection_state = CS_CLIENT_CERTIFICATE;
break;
}
default:
throw new TlsFatalAlert(AlertDescription.unexpected_message);
}
break;
}
case HandshakeType.client_key_exchange:
{
switch (this.connection_state)
{
case CS_SERVER_HELLO_DONE:
{
tlsServer.processClientSupplementalData(null);
}
case CS_CLIENT_SUPPLEMENTAL_DATA:
{
if (this.certificateRequest == null)
{
this.keyExchange.skipClientCredentials();
}
else
{
if (TlsUtils.isTLSv12(getContext()))
{
throw new TlsFatalAlert(AlertDescription.unexpected_message);
}
else if (TlsUtils.isSSL(getContext()))
{
if (this.peerCertificate == null)
{
throw new TlsFatalAlert(AlertDescription.unexpected_message);
}
}
else
{
notifyClientCertificate(Certificate.EMPTY_CHAIN);
}
}
}
case CS_CLIENT_CERTIFICATE:
{
receiveClientKeyExchangeMessage(buf);
this.connection_state = CS_CLIENT_KEY_EXCHANGE;
break;
}
default:
throw new TlsFatalAlert(AlertDescription.unexpected_message);
}
break;
}
case HandshakeType.certificate_verify:
{
switch (this.connection_state)
{
case CS_CLIENT_KEY_EXCHANGE:
{
if (!expectCertificateVerifyMessage())
{
throw new TlsFatalAlert(AlertDescription.unexpected_message);
}
receiveCertificateVerifyMessage(buf);
this.connection_state = CS_CERTIFICATE_VERIFY;
break;
}
default:
throw new TlsFatalAlert(AlertDescription.unexpected_message);
}
break;
}
case HandshakeType.finished:
{
switch (this.connection_state)
{
case CS_CLIENT_KEY_EXCHANGE:
{
if (expectCertificateVerifyMessage())
{
throw new TlsFatalAlert(AlertDescription.unexpected_message);
}
}
case CS_CERTIFICATE_VERIFY:
{
processFinishedMessage(buf);
this.connection_state = CS_CLIENT_FINISHED;
if (this.expectSessionTicket)
{
sendNewSessionTicketMessage(tlsServer.getNewSessionTicket());
}
this.connection_state = CS_SERVER_SESSION_TICKET;
sendChangeCipherSpecMessage();
sendFinishedMessage();
this.connection_state = CS_SERVER_FINISHED;
completeHandshake();
break;
}
default:
throw new TlsFatalAlert(AlertDescription.unexpected_message);
}
break;
}
case HandshakeType.hello_request:
case HandshakeType.hello_verify_request:
case HandshakeType.server_hello:
case HandshakeType.server_key_exchange:
case HandshakeType.certificate_request:
case HandshakeType.server_hello_done:
case HandshakeType.session_ticket:
default:
throw new TlsFatalAlert(AlertDescription.unexpected_message);
}
}
protected void handleAlertWarningMessage(short alertDescription)
throws IOException
{
super.handleAlertWarningMessage(alertDescription);
switch (alertDescription)
{
case AlertDescription.no_certificate:
{
if (TlsUtils.isSSL(getContext()) && this.certificateRequest != null)
{
switch (this.connection_state)
{
case CS_SERVER_HELLO_DONE:
{
tlsServer.processClientSupplementalData(null);
}
case CS_CLIENT_SUPPLEMENTAL_DATA:
{
notifyClientCertificate(Certificate.EMPTY_CHAIN);
this.connection_state = CS_CLIENT_CERTIFICATE;
return;
}
}
}
throw new TlsFatalAlert(AlertDescription.unexpected_message);
}
}
}
protected void notifyClientCertificate(Certificate clientCertificate)
throws IOException
{
if (certificateRequest == null)
{
throw new IllegalStateException();
}
if (peerCertificate != null)
{
throw new TlsFatalAlert(AlertDescription.unexpected_message);
}
this.peerCertificate = clientCertificate;
if (clientCertificate.isEmpty())
{
this.keyExchange.skipClientCredentials();
}
else
{
this.clientCertificateType = TlsUtils.getClientCertificateType(clientCertificate,
this.serverCredentials.getCertificate());
this.keyExchange.processClientCertificate(clientCertificate);
}
this.tlsServer.notifyClientCertificate(clientCertificate);
}
protected void receiveCertificateMessage(ByteArrayInputStream buf)
throws IOException
{
Certificate clientCertificate = Certificate.parse(buf);
assertEmpty(buf);
notifyClientCertificate(clientCertificate);
}
protected void receiveCertificateVerifyMessage(ByteArrayInputStream buf)
throws IOException
{
if (certificateRequest == null)
{
throw new IllegalStateException();
}
DigitallySigned clientCertificateVerify = DigitallySigned.parse(getContext(), buf);
assertEmpty(buf);
try
{
SignatureAndHashAlgorithm signatureAlgorithm = clientCertificateVerify.getAlgorithm();
byte[] hash;
if (TlsUtils.isTLSv12(getContext()))
{
TlsUtils.verifySupportedSignatureAlgorithm(certificateRequest.getSupportedSignatureAlgorithms(), signatureAlgorithm);
hash = prepareFinishHash.getFinalHash(signatureAlgorithm.getHash());
}
else
{
hash = securityParameters.getSessionHash();
}
org.bouncycastle.asn1.x509.Certificate x509Cert = peerCertificate.getCertificateAt(0);
SubjectPublicKeyInfo keyInfo = x509Cert.getSubjectPublicKeyInfo();
AsymmetricKeyParameter publicKey = PublicKeyFactory.createKey(keyInfo);
TlsSigner tlsSigner = TlsUtils.createTlsSigner(clientCertificateType);
tlsSigner.init(getContext());
if (!tlsSigner.verifyRawSignature(signatureAlgorithm, clientCertificateVerify.getSignature(), publicKey, hash))
{
throw new TlsFatalAlert(AlertDescription.decrypt_error);
}
}
catch (TlsFatalAlert e)
{
throw e;
}
catch (Exception e)
{
throw new TlsFatalAlert(AlertDescription.decrypt_error, e);
}
}
protected void receiveClientHelloMessage(ByteArrayInputStream buf)
throws IOException
{
ProtocolVersion client_version = TlsUtils.readVersion(buf);
recordStream.setWriteVersion(client_version);
if (client_version.isDTLS())
{
throw new TlsFatalAlert(AlertDescription.illegal_parameter);
}
byte[] client_random = TlsUtils.readFully(32, buf);
byte[] sessionID = TlsUtils.readOpaque8(buf);
if (sessionID.length > 32)
{
throw new TlsFatalAlert(AlertDescription.illegal_parameter);
}
int cipher_suites_length = TlsUtils.readUint16(buf);
if (cipher_suites_length < 2 || (cipher_suites_length & 1) != 0)
{
throw new TlsFatalAlert(AlertDescription.decode_error);
}
this.offeredCipherSuites = TlsUtils.readUint16Array(cipher_suites_length / 2, buf);
int compression_methods_length = TlsUtils.readUint8(buf);
if (compression_methods_length < 1)
{
throw new TlsFatalAlert(AlertDescription.illegal_parameter);
}
this.offeredCompressionMethods = TlsUtils.readUint8Array(compression_methods_length, buf);
this.clientExtensions = readExtensions(buf);
this.securityParameters.extendedMasterSecret = TlsExtensionsUtils.hasExtendedMasterSecretExtension(clientExtensions);
if (!securityParameters.isExtendedMasterSecret() && tlsServer.requiresExtendedMasterSecret())
{
throw new TlsFatalAlert(AlertDescription.handshake_failure);
}
getContextAdmin().setClientVersion(client_version);
tlsServer.notifyClientVersion(client_version);
tlsServer.notifyFallback(Arrays.contains(offeredCipherSuites, CipherSuite.TLS_FALLBACK_SCSV));
securityParameters.clientRandom = client_random;
tlsServer.notifyOfferedCipherSuites(offeredCipherSuites);
tlsServer.notifyOfferedCompressionMethods(offeredCompressionMethods);
{
if (Arrays.contains(offeredCipherSuites, CipherSuite.TLS_EMPTY_RENEGOTIATION_INFO_SCSV))
{
this.secure_renegotiation = true;
}
byte[] renegExtData = TlsUtils.getExtensionData(clientExtensions, EXT_RenegotiationInfo);
if (renegExtData != null)
{
this.secure_renegotiation = true;
if (!Arrays.constantTimeAreEqual(renegExtData, createRenegotiationInfo(TlsUtils.EMPTY_BYTES)))
{
throw new TlsFatalAlert(AlertDescription.handshake_failure);
}
}
}
tlsServer.notifySecureRenegotiation(this.secure_renegotiation);
if (clientExtensions != null)
{
TlsExtensionsUtils.getPaddingExtension(clientExtensions);
tlsServer.processClientExtensions(clientExtensions);
}
}
protected void receiveClientKeyExchangeMessage(ByteArrayInputStream buf)
throws IOException
{
keyExchange.processClientKeyExchange(buf);
assertEmpty(buf);
if (TlsUtils.isSSL(getContext()))
{
establishMasterSecret(getContext(), keyExchange);
}
this.prepareFinishHash = recordStream.prepareToFinish();
this.securityParameters.sessionHash = getCurrentPRFHash(getContext(), prepareFinishHash, null);
if (!TlsUtils.isSSL(getContext()))
{
establishMasterSecret(getContext(), keyExchange);
}
recordStream.setPendingConnectionState(getPeer().getCompression(), getPeer().getCipher());
}
protected void sendCertificateRequestMessage(CertificateRequest certificateRequest)
throws IOException
{
HandshakeMessage message = new HandshakeMessage(HandshakeType.certificate_request);
certificateRequest.encode(message);
message.writeToRecordStream();
}
protected void sendCertificateStatusMessage(CertificateStatus certificateStatus)
throws IOException
{
HandshakeMessage message = new HandshakeMessage(HandshakeType.certificate_status);
certificateStatus.encode(message);
message.writeToRecordStream();
}
protected void sendNewSessionTicketMessage(NewSessionTicket newSessionTicket)
throws IOException
{
if (newSessionTicket == null)
{
throw new TlsFatalAlert(AlertDescription.internal_error);
}
HandshakeMessage message = new HandshakeMessage(HandshakeType.session_ticket);
newSessionTicket.encode(message);
message.writeToRecordStream();
}
protected void sendServerHelloMessage()
throws IOException
{
HandshakeMessage message = new HandshakeMessage(HandshakeType.server_hello);
{
ProtocolVersion server_version = tlsServer.getServerVersion();
if (!server_version.isEqualOrEarlierVersionOf(getContext().getClientVersion()))
{
throw new TlsFatalAlert(AlertDescription.internal_error);
}
recordStream.setReadVersion(server_version);
recordStream.setWriteVersion(server_version);
recordStream.setRestrictReadVersion(true);
getContextAdmin().setServerVersion(server_version);
TlsUtils.writeVersion(server_version, message);
}
message.write(this.securityParameters.serverRandom);
TlsUtils.writeOpaque8(TlsUtils.EMPTY_BYTES, message);
int selectedCipherSuite = tlsServer.getSelectedCipherSuite();
if (!Arrays.contains(offeredCipherSuites, selectedCipherSuite)
|| selectedCipherSuite == CipherSuite.TLS_NULL_WITH_NULL_NULL
|| CipherSuite.isSCSV(selectedCipherSuite)
|| !TlsUtils.isValidCipherSuiteForVersion(selectedCipherSuite, getContext().getServerVersion()))
{
throw new TlsFatalAlert(AlertDescription.internal_error);
}
securityParameters.cipherSuite = selectedCipherSuite;
short selectedCompressionMethod = tlsServer.getSelectedCompressionMethod();
if (!Arrays.contains(offeredCompressionMethods, selectedCompressionMethod))
{
throw new TlsFatalAlert(AlertDescription.internal_error);
}
securityParameters.compressionAlgorithm = selectedCompressionMethod;
TlsUtils.writeUint16(selectedCipherSuite, message);
TlsUtils.writeUint8(selectedCompressionMethod, message);
this.serverExtensions = TlsExtensionsUtils.ensureExtensionsInitialised(tlsServer.getServerExtensions());
if (this.secure_renegotiation)
{
byte[] renegExtData = TlsUtils.getExtensionData(this.serverExtensions, EXT_RenegotiationInfo);
boolean noRenegExt = (null == renegExtData);
if (noRenegExt)
{
this.serverExtensions.put(EXT_RenegotiationInfo, createRenegotiationInfo(TlsUtils.EMPTY_BYTES));
}
}
if (TlsUtils.isSSL(tlsServerContext))
{
securityParameters.extendedMasterSecret = false;
}
else if (securityParameters.isExtendedMasterSecret())
{
TlsExtensionsUtils.addExtendedMasterSecretExtension(serverExtensions);
}
if (!this.serverExtensions.isEmpty())
{
this.securityParameters.encryptThenMAC = TlsExtensionsUtils.hasEncryptThenMACExtension(serverExtensions);
this.securityParameters.maxFragmentLength = processMaxFragmentLengthExtension(clientExtensions,
serverExtensions, AlertDescription.internal_error);
this.securityParameters.truncatedHMac = TlsExtensionsUtils.hasTruncatedHMacExtension(serverExtensions);
this.allowCertificateStatus = !resumedSession
&& TlsUtils.hasExpectedEmptyExtensionData(serverExtensions, TlsExtensionsUtils.EXT_status_request,
AlertDescription.internal_error);
this.expectSessionTicket = !resumedSession
&& TlsUtils.hasExpectedEmptyExtensionData(serverExtensions, TlsProtocol.EXT_SessionTicket,
AlertDescription.internal_error);
writeExtensions(message, serverExtensions);
}
securityParameters.prfAlgorithm = getPRFAlgorithm(getContext(), securityParameters.getCipherSuite());
securityParameters.verifyDataLength = 12;
applyMaxFragmentLengthExtension();
message.writeToRecordStream();
}
protected void sendServerHelloDoneMessage()
throws IOException
{
byte[] message = new byte[4];
TlsUtils.writeUint8(HandshakeType.server_hello_done, message, 0);
TlsUtils.writeUint24(0, message, 1);
writeHandshakeMessage(message, 0, message.length);
}
protected void sendServerKeyExchangeMessage(byte[] serverKeyExchange)
throws IOException
{
HandshakeMessage message = new HandshakeMessage(HandshakeType.server_key_exchange, serverKeyExchange.length);
message.write(serverKeyExchange);
message.writeToRecordStream();
}
protected boolean expectCertificateVerifyMessage()
{
return clientCertificateType >= 0 && TlsUtils.hasSigningCapability(clientCertificateType);
}
}