Program: 52584
Project: 52584
Buggy source:
protected DTLSTransport clientHandshake(ClientHandshakeState state,DTLSRecordLayer recordLayer) throws IOException {
  SecurityParameters securityParameters=state.clientContext.getSecurityParameters();
  DTLSReliableHandshake handshake=new DTLSReliableHandshake(state.clientContext,recordLayer);
  byte[] clientHelloBody=generateClientHello(state,state.client);
  handshake.sendMessage(HandshakeType.client_hello,clientHelloBody);
  DTLSReliableHandshake.Message serverMessage=handshake.receiveMessage();
{
    ProtocolVersion server_version=recordLayer.getDiscoveredPeerVersion();
    ProtocolVersion client_version=state.clientContext.getClientVersion();
    if (!server_version.isEqualOrEarlierVersionOf(client_version)) {
      throw new TlsFatalAlert(AlertDescription.illegal_parameter);
    }
    state.clientContext.setServerVersion(server_version);
    state.client.notifyServerVersion(server_version);
  }
  while (serverMessage.getType() == HandshakeType.hello_verify_request) {
    byte[] cookie=parseHelloVerifyRequest(state.clientContext,serverMessage.getBody());
    byte[] patched=patchClientHelloWithCookie(clientHelloBody,cookie);
    handshake.resetHandshakeMessagesDigest();
    handshake.sendMessage(HandshakeType.client_hello,patched);
    serverMessage=handshake.receiveMessage();
  }
  if (serverMessage.getType() == HandshakeType.server_hello) {
    processServerHello(state,serverMessage.getBody());
    if (state.maxFragmentLength >= 0) {
      int plainTextLimit=1 << (8 + state.maxFragmentLength);
      recordLayer.setPlaintextLimit(plainTextLimit);
    }
    serverMessage=handshake.receiveMessage();
  }
 else {
    throw new TlsFatalAlert(AlertDescription.unexpected_message);
  }
  securityParameters.cipherSuite=state.selectedCipherSuite;
  securityParameters.compressionAlgorithm=state.selectedCompressionMethod;
  securityParameters.prfAlgorithm=TlsProtocol.getPRFAlgorithm(state.clientContext,state.selectedCipherSuite);
  securityParameters.verifyDataLength=12;
  handshake.notifyHelloComplete();
  boolean resumedSession=state.selectedSessionID.length > 0 && state.tlsSession != null && Arrays.areEqual(state.selectedSessionID,state.tlsSession.getSessionID());
  if (resumedSession) {
    if (securityParameters.getCipherSuite() != state.sessionParameters.getCipherSuite() || securityParameters.getCompressionAlgorithm() != state.sessionParameters.getCompressionAlgorithm()) {
      throw new TlsFatalAlert(AlertDescription.illegal_parameter);
    }
    securityParameters.masterSecret=Arrays.clone(state.sessionParameters.getMasterSecret());
    recordLayer.initPendingEpoch(state.client.getCipher());
    byte[] expectedServerVerifyData=TlsUtils.calculateVerifyData(state.clientContext,"String_Node_Str",handshake.getCurrentHash());
    processFinished(handshake.receiveMessageBody(HandshakeType.finished),expectedServerVerifyData);
    byte[] clientVerifyData=TlsUtils.calculateVerifyData(state.clientContext,"String_Node_Str",handshake.getCurrentHash());
    handshake.sendMessage(HandshakeType.finished,clientVerifyData);
    handshake.finish();
    state.clientContext.setResumableSession(state.tlsSession);
    state.client.notifyHandshakeComplete();
    return new DTLSTransport(recordLayer);
  }
  invalidateSession(state);
  if (state.selectedSessionID.length > 0) {
    state.tlsSession=new TlsSessionImpl(state.selectedSessionID,null);
  }
  if (serverMessage.getType() == HandshakeType.supplemental_data) {
    processServerSupplementalData(state,serverMessage.getBody());
    serverMessage=handshake.receiveMessage();
  }
 else {
    state.client.processServerSupplementalData(null);
  }
  state.keyExchange=state.client.getKeyExchange();
  state.keyExchange.init(state.clientContext);
  Certificate serverCertificate=null;
  if (serverMessage.getType() == HandshakeType.certificate) {
    serverCertificate=processServerCertificate(state,serverMessage.getBody());
    serverMessage=handshake.receiveMessage();
  }
 else {
    state.keyExchange.skipServerCredentials();
  }
  if (serverCertificate == null || serverCertificate.isEmpty()) {
    state.allowCertificateStatus=false;
  }
  if (serverMessage.getType() == HandshakeType.certificate_status) {
    processCertificateStatus(state,serverMessage.getBody());
    serverMessage=handshake.receiveMessage();
  }
 else {
  }
  if (serverMessage.getType() == HandshakeType.server_key_exchange) {
    processServerKeyExchange(state,serverMessage.getBody());
    serverMessage=handshake.receiveMessage();
  }
 else {
    state.keyExchange.skipServerKeyExchange();
  }
  if (serverMessage.getType() == HandshakeType.certificate_request) {
    processCertificateRequest(state,serverMessage.getBody());
    serverMessage=handshake.receiveMessage();
  }
 else {
  }
  if (serverMessage.getType() == HandshakeType.server_hello_done) {
    if (serverMessage.getBody().length != 0) {
      throw new TlsFatalAlert(AlertDescription.decode_error);
    }
  }
 else {
    throw new TlsFatalAlert(AlertDescription.unexpected_message);
  }
  Vector clientSupplementalData=state.client.getClientSupplementalData();
  if (clientSupplementalData != null) {
    byte[] supplementalDataBody=generateSupplementalData(clientSupplementalData);
    handshake.sendMessage(HandshakeType.supplemental_data,supplementalDataBody);
  }
  if (state.certificateRequest != null) {
    state.clientCredentials=state.authentication.getClientCredentials(state.certificateRequest);
    Certificate clientCertificate=null;
    if (state.clientCredentials != null) {
      clientCertificate=state.clientCredentials.getCertificate();
    }
    if (clientCertificate == null) {
      clientCertificate=Certificate.EMPTY_CHAIN;
    }
    byte[] certificateBody=generateCertificate(clientCertificate);
    handshake.sendMessage(HandshakeType.certificate,certificateBody);
  }
  if (state.clientCredentials != null) {
    state.keyExchange.processClientCredentials(state.clientCredentials);
  }
 else {
    state.keyExchange.skipClientCredentials();
  }
  byte[] clientKeyExchangeBody=generateClientKeyExchange(state);
  handshake.sendMessage(HandshakeType.client_key_exchange,clientKeyExchangeBody);
  TlsProtocol.establishMasterSecret(state.clientContext,state.keyExchange);
  recordLayer.initPendingEpoch(state.client.getCipher());
  if (state.clientCredentials != null && state.clientCredentials instanceof TlsSignerCredentials) {
    TlsSignerCredentials signerCredentials=(TlsSignerCredentials)state.clientCredentials;
    byte[] md5andsha1=handshake.getCurrentHash();
    byte[] signature=signerCredentials.generateCertificateSignature(md5andsha1);
    DigitallySigned certificateVerify=new DigitallySigned(null,signature);
    byte[] certificateVerifyBody=generateCertificateVerify(state,certificateVerify);
    handshake.sendMessage(HandshakeType.certificate_verify,certificateVerifyBody);
  }
  byte[] clientVerifyData=TlsUtils.calculateVerifyData(state.clientContext,"String_Node_Str",handshake.getCurrentHash());
  handshake.sendMessage(HandshakeType.finished,clientVerifyData);
  if (state.expectSessionTicket) {
    serverMessage=handshake.receiveMessage();
    if (serverMessage.getType() == HandshakeType.session_ticket) {
      processNewSessionTicket(state,serverMessage.getBody());
    }
 else {
      throw new TlsFatalAlert(AlertDescription.unexpected_message);
    }
  }
  byte[] expectedServerVerifyData=TlsUtils.calculateVerifyData(state.clientContext,"String_Node_Str",handshake.getCurrentHash());
  processFinished(handshake.receiveMessageBody(HandshakeType.finished),expectedServerVerifyData);
  handshake.finish();
  if (state.tlsSession != null) {
    state.sessionParameters=new SessionParameters(serverCertificate,securityParameters);
    state.tlsSession=new TlsSessionImpl(state.tlsSession.getSessionID(),state.sessionParameters);
    state.clientContext.setResumableSession(state.tlsSession);
  }
  state.client.notifyHandshakeComplete();
  return new DTLSTransport(recordLayer);
}

Fixed version (ground truth):
protected DTLSTransport clientHandshake(ClientHandshakeState state,DTLSRecordLayer recordLayer) throws IOException {
  SecurityParameters securityParameters=state.clientContext.getSecurityParameters();
  DTLSReliableHandshake handshake=new DTLSReliableHandshake(state.clientContext,recordLayer);
  byte[] clientHelloBody=generateClientHello(state,state.client);
  handshake.sendMessage(HandshakeType.client_hello,clientHelloBody);
  DTLSReliableHandshake.Message serverMessage=handshake.receiveMessage();
{
    ProtocolVersion server_version=recordLayer.getDiscoveredPeerVersion();
    ProtocolVersion client_version=state.clientContext.getClientVersion();
    if (!server_version.isEqualOrEarlierVersionOf(client_version)) {
      throw new TlsFatalAlert(AlertDescription.illegal_parameter);
    }
    state.clientContext.setServerVersion(server_version);
    state.client.notifyServerVersion(server_version);
  }
  while (serverMessage.getType() == HandshakeType.hello_verify_request) {
    byte[] cookie=parseHelloVerifyRequest(state.clientContext,serverMessage.getBody());
    byte[] patched=patchClientHelloWithCookie(clientHelloBody,cookie);
    handshake.resetHandshakeMessagesDigest();
    handshake.sendMessage(HandshakeType.client_hello,patched);
    serverMessage=handshake.receiveMessage();
  }
  if (serverMessage.getType() == HandshakeType.server_hello) {
    processServerHello(state,serverMessage.getBody());
    if (state.maxFragmentLength >= 0) {
      int plainTextLimit=1 << (8 + state.maxFragmentLength);
      recordLayer.setPlaintextLimit(plainTextLimit);
    }
  }
 else {
    throw new TlsFatalAlert(AlertDescription.unexpected_message);
  }
  securityParameters.cipherSuite=state.selectedCipherSuite;
  securityParameters.compressionAlgorithm=state.selectedCompressionMethod;
  securityParameters.prfAlgorithm=TlsProtocol.getPRFAlgorithm(state.clientContext,state.selectedCipherSuite);
  securityParameters.verifyDataLength=12;
  handshake.notifyHelloComplete();
  boolean resumedSession=state.selectedSessionID.length > 0 && state.tlsSession != null && Arrays.areEqual(state.selectedSessionID,state.tlsSession.getSessionID());
  if (resumedSession) {
    if (securityParameters.getCipherSuite() != state.sessionParameters.getCipherSuite() || securityParameters.getCompressionAlgorithm() != state.sessionParameters.getCompressionAlgorithm()) {
      throw new TlsFatalAlert(AlertDescription.illegal_parameter);
    }
    securityParameters.masterSecret=Arrays.clone(state.sessionParameters.getMasterSecret());
    recordLayer.initPendingEpoch(state.client.getCipher());
    byte[] expectedServerVerifyData=TlsUtils.calculateVerifyData(state.clientContext,"String_Node_Str",handshake.getCurrentHash());
    processFinished(handshake.receiveMessageBody(HandshakeType.finished),expectedServerVerifyData);
    byte[] clientVerifyData=TlsUtils.calculateVerifyData(state.clientContext,"String_Node_Str",handshake.getCurrentHash());
    handshake.sendMessage(HandshakeType.finished,clientVerifyData);
    handshake.finish();
    state.clientContext.setResumableSession(state.tlsSession);
    state.client.notifyHandshakeComplete();
    return new DTLSTransport(recordLayer);
  }
  invalidateSession(state);
  if (state.selectedSessionID.length > 0) {
    state.tlsSession=new TlsSessionImpl(state.selectedSessionID,null);
  }
  serverMessage=handshake.receiveMessage();
  if (serverMessage.getType() == HandshakeType.supplemental_data) {
    processServerSupplementalData(state,serverMessage.getBody());
    serverMessage=handshake.receiveMessage();
  }
 else {
    state.client.processServerSupplementalData(null);
  }
  state.keyExchange=state.client.getKeyExchange();
  state.keyExchange.init(state.clientContext);
  Certificate serverCertificate=null;
  if (serverMessage.getType() == HandshakeType.certificate) {
    serverCertificate=processServerCertificate(state,serverMessage.getBody());
    serverMessage=handshake.receiveMessage();
  }
 else {
    state.keyExchange.skipServerCredentials();
  }
  if (serverCertificate == null || serverCertificate.isEmpty()) {
    state.allowCertificateStatus=false;
  }
  if (serverMessage.getType() == HandshakeType.certificate_status) {
    processCertificateStatus(state,serverMessage.getBody());
    serverMessage=handshake.receiveMessage();
  }
 else {
  }
  if (serverMessage.getType() == HandshakeType.server_key_exchange) {
    processServerKeyExchange(state,serverMessage.getBody());
    serverMessage=handshake.receiveMessage();
  }
 else {
    state.keyExchange.skipServerKeyExchange();
  }
  if (serverMessage.getType() == HandshakeType.certificate_request) {
    processCertificateRequest(state,serverMessage.getBody());
    serverMessage=handshake.receiveMessage();
  }
 else {
  }
  if (serverMessage.getType() == HandshakeType.server_hello_done) {
    if (serverMessage.getBody().length != 0) {
      throw new TlsFatalAlert(AlertDescription.decode_error);
    }
  }
 else {
    throw new TlsFatalAlert(AlertDescription.unexpected_message);
  }
  Vector clientSupplementalData=state.client.getClientSupplementalData();
  if (clientSupplementalData != null) {
    byte[] supplementalDataBody=generateSupplementalData(clientSupplementalData);
    handshake.sendMessage(HandshakeType.supplemental_data,supplementalDataBody);
  }
  if (state.certificateRequest != null) {
    state.clientCredentials=state.authentication.getClientCredentials(state.certificateRequest);
    Certificate clientCertificate=null;
    if (state.clientCredentials != null) {
      clientCertificate=state.clientCredentials.getCertificate();
    }
    if (clientCertificate == null) {
      clientCertificate=Certificate.EMPTY_CHAIN;
    }
    byte[] certificateBody=generateCertificate(clientCertificate);
    handshake.sendMessage(HandshakeType.certificate,certificateBody);
  }
  if (state.clientCredentials != null) {
    state.keyExchange.processClientCredentials(state.clientCredentials);
  }
 else {
    state.keyExchange.skipClientCredentials();
  }
  byte[] clientKeyExchangeBody=generateClientKeyExchange(state);
  handshake.sendMessage(HandshakeType.client_key_exchange,clientKeyExchangeBody);
  TlsProtocol.establishMasterSecret(state.clientContext,state.keyExchange);
  recordLayer.initPendingEpoch(state.client.getCipher());
  if (state.clientCredentials != null && state.clientCredentials instanceof TlsSignerCredentials) {
    TlsSignerCredentials signerCredentials=(TlsSignerCredentials)state.clientCredentials;
    byte[] md5andsha1=handshake.getCurrentHash();
    byte[] signature=signerCredentials.generateCertificateSignature(md5andsha1);
    DigitallySigned certificateVerify=new DigitallySigned(null,signature);
    byte[] certificateVerifyBody=generateCertificateVerify(state,certificateVerify);
    handshake.sendMessage(HandshakeType.certificate_verify,certificateVerifyBody);
  }
  byte[] clientVerifyData=TlsUtils.calculateVerifyData(state.clientContext,"String_Node_Str",handshake.getCurrentHash());
  handshake.sendMessage(HandshakeType.finished,clientVerifyData);
  if (state.expectSessionTicket) {
    serverMessage=handshake.receiveMessage();
    if (serverMessage.getType() == HandshakeType.session_ticket) {
      processNewSessionTicket(state,serverMessage.getBody());
    }
 else {
      throw new TlsFatalAlert(AlertDescription.unexpected_message);
    }
  }
  byte[] expectedServerVerifyData=TlsUtils.calculateVerifyData(state.clientContext,"String_Node_Str",handshake.getCurrentHash());
  processFinished(handshake.receiveMessageBody(HandshakeType.finished),expectedServerVerifyData);
  handshake.finish();
  if (state.tlsSession != null) {
    state.sessionParameters=new SessionParameters(serverCertificate,securityParameters);
    state.tlsSession=new TlsSessionImpl(state.tlsSession.getSessionID(),state.sessionParameters);
    state.clientContext.setResumableSession(state.tlsSession);
  }
  state.client.notifyHandshakeComplete();
  return new DTLSTransport(recordLayer);
}