|
| 1 | +/** |
| 2 | + * TLS-Attacker - A Modular Penetration Testing Framework for TLS |
| 3 | + * |
| 4 | + * Copyright 2014-2020 Ruhr University Bochum, Paderborn University, |
| 5 | + * and Hackmanit GmbH |
| 6 | + * |
| 7 | + * Licensed under Apache License 2.0 |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + */ |
| 10 | + |
| 11 | +package de.rub.nds.tlsattacker.core.protocol.handler; |
| 12 | + |
| 13 | +import de.rub.nds.modifiablevariable.util.ArrayConverter; |
| 14 | +import de.rub.nds.tlsattacker.core.constants.AlgorithmResolver; |
| 15 | +import de.rub.nds.tlsattacker.core.constants.HKDFAlgorithm; |
| 16 | +import de.rub.nds.tlsattacker.core.constants.Tls13KeySetType; |
| 17 | +import de.rub.nds.tlsattacker.core.crypto.HKDFunction; |
| 18 | +import de.rub.nds.tlsattacker.core.exceptions.AdjustmentException; |
| 19 | +import de.rub.nds.tlsattacker.core.exceptions.CryptoException; |
| 20 | +import de.rub.nds.tlsattacker.core.protocol.message.KeyUpdateMessage; |
| 21 | +import de.rub.nds.tlsattacker.core.protocol.parser.KeyUpdateParser; |
| 22 | +import de.rub.nds.tlsattacker.core.protocol.parser.ProtocolMessageParser; |
| 23 | +import de.rub.nds.tlsattacker.core.protocol.preparator.KeyUpdatePreparator; |
| 24 | +import de.rub.nds.tlsattacker.core.protocol.preparator.ProtocolMessagePreparator; |
| 25 | +import de.rub.nds.tlsattacker.core.protocol.serializer.KeyUpdateSerializer; |
| 26 | +import de.rub.nds.tlsattacker.core.protocol.serializer.ProtocolMessageSerializer; |
| 27 | +import de.rub.nds.tlsattacker.core.record.cipher.RecordCipher; |
| 28 | +import de.rub.nds.tlsattacker.core.record.cipher.RecordCipherFactory; |
| 29 | +import de.rub.nds.tlsattacker.core.record.cipher.cryptohelper.KeySet; |
| 30 | +import de.rub.nds.tlsattacker.core.record.cipher.cryptohelper.KeySetGenerator; |
| 31 | +import de.rub.nds.tlsattacker.core.state.TlsContext; |
| 32 | +import de.rub.nds.tlsattacker.transport.ConnectionEndType; |
| 33 | + |
| 34 | +import java.security.NoSuchAlgorithmException; |
| 35 | + |
| 36 | +import javax.crypto.Mac; |
| 37 | +import org.apache.logging.log4j.LogManager; |
| 38 | +import org.apache.logging.log4j.Logger; |
| 39 | + |
| 40 | +public class KeyUpdateHandler extends HandshakeMessageHandler<KeyUpdateMessage> { |
| 41 | + |
| 42 | + private static final Logger LOGGER = LogManager.getLogger(); |
| 43 | + |
| 44 | + public KeyUpdateHandler(TlsContext tlsContext) { |
| 45 | + super(tlsContext); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void adjustTLSContext(KeyUpdateMessage message) { |
| 50 | + if (tlsContext.getChooser().getTalkingConnectionEnd() != tlsContext.getChooser().getConnectionEndType()) { |
| 51 | + adjustApplicationTrafficSecrets(); |
| 52 | + setRecordCipher(Tls13KeySetType.APPLICATION_TRAFFIC_SECRETS); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void adjustTlsContextAfterSerialize(KeyUpdateMessage message) { |
| 58 | + adjustApplicationTrafficSecrets(); |
| 59 | + setRecordCipher(Tls13KeySetType.APPLICATION_TRAFFIC_SECRETS); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public ProtocolMessageParser getParser(byte[] message, int pointer) { |
| 64 | + return new KeyUpdateParser(pointer, message, tlsContext.getChooser().getSelectedProtocolVersion(), |
| 65 | + tlsContext.getConfig()); |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public ProtocolMessagePreparator getPreparator(KeyUpdateMessage message) { |
| 71 | + return new KeyUpdatePreparator(tlsContext.getChooser(), message); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public ProtocolMessageSerializer getSerializer(KeyUpdateMessage message) { |
| 76 | + return new KeyUpdateSerializer(message, tlsContext.getChooser().getSelectedProtocolVersion()); |
| 77 | + } |
| 78 | + |
| 79 | + private void adjustApplicationTrafficSecrets() { |
| 80 | + HKDFAlgorithm hkdfAlgortihm = |
| 81 | + AlgorithmResolver.getHKDFAlgorithm(tlsContext.getChooser().getSelectedCipherSuite()); |
| 82 | + |
| 83 | + try { |
| 84 | + Mac mac = Mac.getInstance(hkdfAlgortihm.getMacAlgorithm().getJavaName()); |
| 85 | + |
| 86 | + if (tlsContext.getChooser().getTalkingConnectionEnd() == ConnectionEndType.CLIENT) { |
| 87 | + |
| 88 | + byte[] clientApplicationTrafficSecret = |
| 89 | + HKDFunction.expandLabel(hkdfAlgortihm, tlsContext.getClientApplicationTrafficSecret(), |
| 90 | + HKDFunction.TRAFFICUPD, new byte[0], mac.getMacLength()); |
| 91 | + |
| 92 | + tlsContext.setClientApplicationTrafficSecret(clientApplicationTrafficSecret); |
| 93 | + LOGGER.debug("Set clientApplicationTrafficSecret in Context to " |
| 94 | + + ArrayConverter.bytesToHexString(clientApplicationTrafficSecret)); |
| 95 | + |
| 96 | + } else { |
| 97 | + |
| 98 | + byte[] serverApplicationTrafficSecret = |
| 99 | + HKDFunction.expandLabel(hkdfAlgortihm, tlsContext.getServerApplicationTrafficSecret(), |
| 100 | + HKDFunction.TRAFFICUPD, new byte[0], mac.getMacLength()); |
| 101 | + |
| 102 | + tlsContext.setServerApplicationTrafficSecret(serverApplicationTrafficSecret); |
| 103 | + LOGGER.debug("Set serverApplicationTrafficSecret in Context to " |
| 104 | + + ArrayConverter.bytesToHexString(serverApplicationTrafficSecret)); |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + } catch (NoSuchAlgorithmException | CryptoException ex) { |
| 109 | + throw new AdjustmentException(ex); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private KeySet getKeySet(TlsContext context, Tls13KeySetType keySetType) { |
| 114 | + try { |
| 115 | + LOGGER.debug("Generating new KeySet"); |
| 116 | + KeySet keySet = |
| 117 | + KeySetGenerator.generateKeySet(context, context.getChooser().getSelectedProtocolVersion(), keySetType); |
| 118 | + |
| 119 | + return keySet; |
| 120 | + } catch (NoSuchAlgorithmException | CryptoException ex) { |
| 121 | + throw new UnsupportedOperationException("The specified Algorithm is not supported", ex); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private void setRecordCipher(Tls13KeySetType keySetType) { |
| 126 | + try { |
| 127 | + int AEAD_IV_LENGTH = 12; |
| 128 | + KeySet keySet; |
| 129 | + HKDFAlgorithm hkdfAlgortihm = |
| 130 | + AlgorithmResolver.getHKDFAlgorithm(tlsContext.getChooser().getSelectedCipherSuite()); |
| 131 | + |
| 132 | + if (tlsContext.getChooser().getTalkingConnectionEnd() == ConnectionEndType.CLIENT) { |
| 133 | + |
| 134 | + tlsContext.setActiveClientKeySetType(keySetType); |
| 135 | + LOGGER.debug("Setting cipher for client to use " + keySetType); |
| 136 | + keySet = getKeySet(tlsContext, tlsContext.getActiveClientKeySetType()); |
| 137 | + |
| 138 | + } else { |
| 139 | + tlsContext.setActiveServerKeySetType(keySetType); |
| 140 | + LOGGER.debug("Setting cipher for server to use " + keySetType); |
| 141 | + keySet = getKeySet(tlsContext, tlsContext.getActiveServerKeySetType()); |
| 142 | + } |
| 143 | + |
| 144 | + if (tlsContext.getChooser().getTalkingConnectionEnd() == tlsContext.getChooser().getConnectionEndType()) { |
| 145 | + |
| 146 | + if (tlsContext.getChooser().getConnectionEndType() == ConnectionEndType.CLIENT) { |
| 147 | + |
| 148 | + keySet.setClientWriteIv(HKDFunction.expandLabel(hkdfAlgortihm, |
| 149 | + tlsContext.getClientApplicationTrafficSecret(), HKDFunction.IV, new byte[0], AEAD_IV_LENGTH)); |
| 150 | + |
| 151 | + keySet.setClientWriteKey(HKDFunction.expandLabel(hkdfAlgortihm, tlsContext |
| 152 | + .getClientApplicationTrafficSecret(), HKDFunction.KEY, new byte[0], AlgorithmResolver |
| 153 | + .getCipher(tlsContext.getChooser().getSelectedCipherSuite()).getKeySize())); |
| 154 | + } else { |
| 155 | + |
| 156 | + keySet.setServerWriteIv(HKDFunction.expandLabel(hkdfAlgortihm, |
| 157 | + tlsContext.getServerApplicationTrafficSecret(), HKDFunction.IV, new byte[0], AEAD_IV_LENGTH)); |
| 158 | + |
| 159 | + keySet.setServerWriteKey(HKDFunction.expandLabel(hkdfAlgortihm, tlsContext |
| 160 | + .getServerApplicationTrafficSecret(), HKDFunction.KEY, new byte[0], AlgorithmResolver |
| 161 | + .getCipher(tlsContext.getChooser().getSelectedCipherSuite()).getKeySize())); |
| 162 | + } |
| 163 | + |
| 164 | + RecordCipher recordCipherClient = |
| 165 | + RecordCipherFactory.getRecordCipher(tlsContext, keySet, tlsContext.getChooser() |
| 166 | + .getSelectedCipherSuite()); |
| 167 | + tlsContext.getRecordLayer().setRecordCipher(recordCipherClient); |
| 168 | + |
| 169 | + tlsContext.setWriteSequenceNumber(0); |
| 170 | + tlsContext.getRecordLayer().updateEncryptionCipher(); |
| 171 | + |
| 172 | + } else if (tlsContext.getChooser().getTalkingConnectionEnd() != tlsContext.getChooser() |
| 173 | + .getConnectionEndType()) { |
| 174 | + |
| 175 | + if (tlsContext.getChooser().getTalkingConnectionEnd() == ConnectionEndType.SERVER) { |
| 176 | + |
| 177 | + keySet.setServerWriteIv(HKDFunction.expandLabel(hkdfAlgortihm, |
| 178 | + tlsContext.getServerApplicationTrafficSecret(), HKDFunction.IV, new byte[0], AEAD_IV_LENGTH)); |
| 179 | + |
| 180 | + keySet.setServerWriteKey(HKDFunction.expandLabel(hkdfAlgortihm, tlsContext |
| 181 | + .getServerApplicationTrafficSecret(), HKDFunction.KEY, new byte[0], AlgorithmResolver |
| 182 | + .getCipher(tlsContext.getChooser().getSelectedCipherSuite()).getKeySize())); |
| 183 | + |
| 184 | + } else { |
| 185 | + |
| 186 | + keySet.setClientWriteIv(HKDFunction.expandLabel(hkdfAlgortihm, |
| 187 | + tlsContext.getClientApplicationTrafficSecret(), HKDFunction.IV, new byte[0], AEAD_IV_LENGTH)); |
| 188 | + |
| 189 | + keySet.setClientWriteKey(HKDFunction.expandLabel(hkdfAlgortihm, tlsContext |
| 190 | + .getClientApplicationTrafficSecret(), HKDFunction.KEY, new byte[0], AlgorithmResolver |
| 191 | + .getCipher(tlsContext.getChooser().getSelectedCipherSuite()).getKeySize())); |
| 192 | + } |
| 193 | + |
| 194 | + RecordCipher recordCipherClient = |
| 195 | + RecordCipherFactory.getRecordCipher(tlsContext, keySet, tlsContext.getChooser() |
| 196 | + .getSelectedCipherSuite()); |
| 197 | + tlsContext.getRecordLayer().setRecordCipher(recordCipherClient); |
| 198 | + |
| 199 | + tlsContext.setReadSequenceNumber(0); |
| 200 | + tlsContext.getRecordLayer().updateDecryptionCipher(); |
| 201 | + |
| 202 | + } |
| 203 | + |
| 204 | + } catch (CryptoException ex) { |
| 205 | + throw new AdjustmentException(ex); |
| 206 | + } |
| 207 | + |
| 208 | + } |
| 209 | +} |
0 commit comments