ClientKeyExchangeHandler extends
        HandshakeMessageHandler<Message> { protected byte[] calculateMasterSecret(ClientKeyExchangeMessage message) throws CryptoException { Chooser chooser = tlsContext.getChooser(); if (chooser.getSelectedProtocolVersion() == ProtocolVersion.SSL3) { LOGGER.debug("Calculate SSL MasterSecret with Client and Server Nonces, which are: " + ArrayConverter.bytesToHexString(message.getComputations().getClientServerRandom().getValue())); return SSLUtils.calculateMasterSecretSSL3(chooser.getPreMasterSecret(), message.getComputations() .getClientServerRandom().getValue()); } else { PRFAlgorithm prfAlgorithm = AlgorithmResolver.getPRFAlgorithm(chooser.getSelectedProtocolVersion(), chooser.getSelectedCipherSuite()); if (chooser.isUseExtendedMasterSecret()) { LOGGER.debug("Calculating ExtendedMasterSecret"); byte[] sessionHash = tlsContext.getDigest().digest(chooser.getSelectedProtocolVersion(), chooser.getSelectedCipherSuite()); LOGGER.debug("Premastersecret: " + ArrayConverter.bytesToHexString(chooser.getPreMasterSecret())); LOGGER.debug("SessionHash: " + ArrayConverter.bytesToHexString(sessionHash)); byte[] extendedMasterSecret = PseudoRandomFunction.compute(prfAlgorithm, chooser.getPreMasterSecret(), PseudoRandomFunction.EXTENDED_MASTER_SECRET_LABEL, sessionHash, HandshakeByteLength.MASTER_SECRET); return extendedMasterSecret; } else { LOGGER.debug("Calculating MasterSecret"); byte[] masterSecret = PseudoRandomFunction.compute(prfAlgorithm, chooser.getPreMasterSecret(), PseudoRandomFunction.MASTER_SECRET_LABEL, message.getComputations().getClientServerRandom() .getValue(), HandshakeByteLength.MASTER_SECRET); return masterSecret; } } } ClientKeyExchangeHandler(TlsContext tlsContext); void adjustPremasterSecret(ClientKeyExchangeMessage message); void adjustMasterSecret(ClientKeyExchangeMessage message);  }
@Test public void testMasterSecretCalculationSSL3() throws NoSuchAlgorithmException, CryptoException { byte[] preMasterSecret = ArrayConverter.hexStringToByteArray(StringUtils.repeat("01", 48)); byte[] serverRdm = ArrayConverter.hexStringToByteArray(StringUtils.repeat("02", 32)); byte[] clientRdm = ArrayConverter.hexStringToByteArray(StringUtils.repeat("03", 32)); DHClientKeyExchangeMessage message = new DHClientKeyExchangeMessage(); message.prepareComputations(); message.getComputations().setPremasterSecret(preMasterSecret); message.getComputations().setClientServerRandom(ArrayConverter.concatenate(clientRdm, serverRdm)); context.setServerRandom(serverRdm); context.setSelectedCipherSuite(CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA); context.setSelectedProtocolVersion(ProtocolVersion.SSL3); context.initRecordLayer(); context.setPreMasterSecret(preMasterSecret); final MessageDigest md5 = java.security.MessageDigest.getInstance("MD5"); final MessageDigest sha = java.security.MessageDigest.getInstance("SHA-1"); final byte[] shaDigest1 = sha.digest(ArrayConverter.concatenate(ArrayConverter.hexStringToByteArray("41"), preMasterSecret, clientRdm, serverRdm)); final byte[] shaDigest2 = sha.digest(ArrayConverter.concatenate(ArrayConverter.hexStringToByteArray("4242"), preMasterSecret, clientRdm, serverRdm)); final byte[] shaDigest3 = sha.digest(ArrayConverter.concatenate(ArrayConverter.hexStringToByteArray("434343"), preMasterSecret, clientRdm, serverRdm)); final byte[] md5Digest1 = md5.digest(ArrayConverter.concatenate(preMasterSecret, shaDigest1)); final byte[] md5Digest2 = md5.digest(ArrayConverter.concatenate(preMasterSecret, shaDigest2)); final byte[] md5Digest3 = md5.digest(ArrayConverter.concatenate(preMasterSecret, shaDigest3)); byte[] expectedMasterSecret = ArrayConverter.concatenate(md5Digest1, md5Digest2, md5Digest3); byte[] calculatedMasterSecret = handler.calculateMasterSecret(message); Assert.assertArrayEquals(expectedMasterSecret, calculatedMasterSecret); }