/**
   * Processes ServerHelloDone: makes verification of the server messages; sends client messages,
   * computers masterSecret, sends ChangeCipherSpec
   */
  @DSComment("Package priviledge")
  @DSBan(DSCat.DEFAULT_MODIFIER)
  @DSGenerator(
      tool_name = "Doppelganger",
      tool_version = "2.0",
      generated_on = "2013-12-30 13:01:10.434 -0500",
      hash_original_method = "2DDF37E22088D1FE8BC73EB3CA83F3A0",
      hash_generated_method = "D2F899ECF0C8F0AF7307958FBF03F0A2")
  void processServerHelloDone() {
    PrivateKey clientKey = null;

    if (serverCert != null) {
      if (session.cipherSuite.isAnonymous()) {
        unexpectedMessage();
        return;
      }
      verifyServerCert();
    } else {
      if (!session.cipherSuite.isAnonymous()) {
        unexpectedMessage();
        return;
      }
    }

    // Client certificate
    if (certificateRequest != null) {
      X509Certificate[] certs = null;
      // obtain certificates from key manager
      String alias = null;
      String[] certTypes = certificateRequest.getTypesAsString();
      X500Principal[] issuers = certificateRequest.certificate_authorities;
      X509KeyManager km = parameters.getKeyManager();
      if (km instanceof X509ExtendedKeyManager) {
        X509ExtendedKeyManager ekm = (X509ExtendedKeyManager) km;
        if (this.socketOwner != null) {
          alias = ekm.chooseClientAlias(certTypes, issuers, this.socketOwner);
        } else {
          alias = ekm.chooseEngineClientAlias(certTypes, issuers, this.engineOwner);
        }
        if (alias != null) {
          certs = ekm.getCertificateChain(alias);
        }
      } else {
        alias = km.chooseClientAlias(certTypes, issuers, this.socketOwner);
        if (alias != null) {
          certs = km.getCertificateChain(alias);
        }
      }

      session.localCertificates = certs;
      clientCert = new CertificateMessage(certs);
      clientKey = km.getPrivateKey(alias);
      send(clientCert);
    }
    // Client key exchange
    if (session.cipherSuite.keyExchange == CipherSuite.KEY_EXCHANGE_RSA
        || session.cipherSuite.keyExchange == CipherSuite.KEY_EXCHANGE_RSA_EXPORT) {
      // RSA encrypted premaster secret message
      Cipher c;
      try {
        c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        if (serverKeyExchange != null) {
          c.init(Cipher.ENCRYPT_MODE, serverKeyExchange.getRSAPublicKey());
        } else {
          c.init(Cipher.ENCRYPT_MODE, serverCert.certs[0]);
        }
      } catch (Exception e) {
        fatalAlert(AlertProtocol.INTERNAL_ERROR, "Unexpected exception", e);
        return;
      }
      preMasterSecret = new byte[48];
      parameters.getSecureRandom().nextBytes(preMasterSecret);
      System.arraycopy(clientHello.client_version, 0, preMasterSecret, 0, 2);
      try {
        clientKeyExchange =
            new ClientKeyExchange(c.doFinal(preMasterSecret), serverHello.server_version[1] == 1);
      } catch (Exception e) {
        fatalAlert(AlertProtocol.INTERNAL_ERROR, "Unexpected exception", e);
        return;
      }
    } else {
      try {
        KeyFactory kf = KeyFactory.getInstance("DH");
        KeyAgreement agreement = KeyAgreement.getInstance("DH");
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH");
        PublicKey serverPublic;
        DHParameterSpec spec;
        if (serverKeyExchange != null) {
          serverPublic =
              kf.generatePublic(
                  new DHPublicKeySpec(
                      serverKeyExchange.par3, serverKeyExchange.par1, serverKeyExchange.par2));
          spec = new DHParameterSpec(serverKeyExchange.par1, serverKeyExchange.par2);
        } else {
          serverPublic = serverCert.certs[0].getPublicKey();
          spec = ((DHPublicKey) serverPublic).getParams();
        }
        kpg.initialize(spec);

        KeyPair kp = kpg.generateKeyPair();
        Key key = kp.getPublic();
        if (clientCert != null
            && serverCert != null
            && (session.cipherSuite.keyExchange == CipherSuite.KEY_EXCHANGE_DHE_RSA
                || session.cipherSuite.keyExchange == CipherSuite.KEY_EXCHANGE_DHE_DSS)) {
          PublicKey client_pk = clientCert.certs[0].getPublicKey();
          PublicKey server_pk = serverCert.certs[0].getPublicKey();
          if (client_pk instanceof DHKey && server_pk instanceof DHKey) {
            if (((DHKey) client_pk)
                    .getParams()
                    .getG()
                    .equals(((DHKey) server_pk).getParams().getG())
                && ((DHKey) client_pk)
                    .getParams()
                    .getP()
                    .equals(((DHKey) server_pk).getParams().getG())) {
              // client cert message DH public key parameters
              // matched those specified by the
              //   server in its certificate,
              clientKeyExchange = new ClientKeyExchange(); // empty
            }
          }
        } else {
          clientKeyExchange = new ClientKeyExchange(((DHPublicKey) key).getY());
        }
        key = kp.getPrivate();
        agreement.init(key);
        agreement.doPhase(serverPublic, true);
        preMasterSecret = agreement.generateSecret();
      } catch (Exception e) {
        fatalAlert(AlertProtocol.INTERNAL_ERROR, "Unexpected exception", e);
        return;
      }
    }
    if (clientKeyExchange != null) {
      send(clientKeyExchange);
    }

    computerMasterSecret();

    // send certificate verify for all certificates except those containing
    // fixed DH parameters
    if (clientCert != null && !clientKeyExchange.isEmpty()) {
      // Certificate verify
      String authType = clientKey.getAlgorithm();
      DigitalSignature ds = new DigitalSignature(authType);
      ds.init(clientKey);

      if ("RSA".equals(authType)) {
        ds.setMD5(io_stream.getDigestMD5());
        ds.setSHA(io_stream.getDigestSHA());
      } else if ("DSA".equals(authType)) {
        ds.setSHA(io_stream.getDigestSHA());
        // The Signature should be empty in case of anonymous signature algorithm:
        // } else if ("DH".equals(authType)) {
      }
      certificateVerify = new CertificateVerify(ds.sign());
      send(certificateVerify);
    }

    sendChangeCipherSpec();
  }