|
| 1 | +/** |
| 2 | + * TLS-Attacker - A Modular Penetration Testing Framework for TLS |
| 3 | + * |
| 4 | + * Copyright 2014-2017 Ruhr University Bochum / Hackmanit GmbH |
| 5 | + * |
| 6 | + * Licensed under Apache License 2.0 |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + */ |
| 9 | +package de.rub.nds.tlsattacker.core.workflow.action; |
| 10 | + |
| 11 | +import de.rub.nds.modifiablevariable.ModifiableVariable; |
| 12 | +import de.rub.nds.tlsattacker.core.constants.AlgorithmResolver; |
| 13 | +import de.rub.nds.tlsattacker.core.constants.CipherSuite; |
| 14 | +import de.rub.nds.tlsattacker.core.exceptions.WorkflowExecutionException; |
| 15 | +import de.rub.nds.tlsattacker.core.protocol.ModifiableVariableHolder; |
| 16 | +import de.rub.nds.tlsattacker.core.protocol.message.CertificateMessage; |
| 17 | +import de.rub.nds.tlsattacker.core.protocol.message.ProtocolMessage; |
| 18 | +import de.rub.nds.tlsattacker.core.protocol.message.ServerKeyExchangeMessage; |
| 19 | +import de.rub.nds.tlsattacker.core.record.AbstractRecord; |
| 20 | +import de.rub.nds.tlsattacker.core.state.State; |
| 21 | +import de.rub.nds.tlsattacker.core.state.TlsContext; |
| 22 | +import de.rub.nds.tlsattacker.core.workflow.action.executor.MessageActionResult; |
| 23 | +import de.rub.nds.tlsattacker.core.workflow.factory.WorkflowConfigurationFactory; |
| 24 | +import java.io.IOException; |
| 25 | +import java.lang.reflect.Field; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.LinkedList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Objects; |
| 30 | +import org.apache.logging.log4j.LogManager; |
| 31 | +import org.apache.logging.log4j.Logger; |
| 32 | + |
| 33 | +public class SendDynamicServerCertificateAction extends MessageAction implements SendingAction { |
| 34 | + |
| 35 | + private static final Logger LOGGER = LogManager.getLogger(); |
| 36 | + |
| 37 | + public SendDynamicServerCertificateAction() { |
| 38 | + super(); |
| 39 | + } |
| 40 | + |
| 41 | + public SendDynamicServerCertificateAction(String connectionAlias) { |
| 42 | + super(connectionAlias); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void execute(State state) throws WorkflowExecutionException { |
| 47 | + TlsContext tlsContext = state.getTlsContext(connectionAlias); |
| 48 | + |
| 49 | + if (isExecuted()) { |
| 50 | + throw new WorkflowExecutionException("Action already executed!"); |
| 51 | + } |
| 52 | + messages = new LinkedList<>(); |
| 53 | + WorkflowConfigurationFactory workflowFactory = new WorkflowConfigurationFactory(state.getConfig()); |
| 54 | + CipherSuite selectedCipherSuite = tlsContext.getSelectedCipherSuite(); |
| 55 | + if (workflowFactory.shouldServerSendACertificate(selectedCipherSuite)) { |
| 56 | + messages.add(new CertificateMessage()); |
| 57 | + } |
| 58 | + String sending = getReadableString(messages); |
| 59 | + if (hasDefaultAlias()) { |
| 60 | + LOGGER.info("Sending Dynamic Certificate: " + sending); |
| 61 | + } else { |
| 62 | + LOGGER.info("Sending Dynamic Certificate (" + connectionAlias + "): " + sending); |
| 63 | + } |
| 64 | + |
| 65 | + try { |
| 66 | + MessageActionResult result = sendMessageHelper.sendMessages(messages, records, tlsContext); |
| 67 | + messages = new ArrayList<>(result.getMessageList()); |
| 68 | + records = new ArrayList<>(result.getRecordList()); |
| 69 | + setExecuted(true); |
| 70 | + } catch (IOException E) { |
| 71 | + tlsContext.setReceivedTransportHandlerException(true); |
| 72 | + LOGGER.debug(E); |
| 73 | + setExecuted(false); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public String toString() { |
| 79 | + StringBuilder sb; |
| 80 | + if (isExecuted()) { |
| 81 | + sb = new StringBuilder("Send Dynamic Certificate Action:\n"); |
| 82 | + } else { |
| 83 | + sb = new StringBuilder("Send Dynamic Certificate: (not executed)\n"); |
| 84 | + } |
| 85 | + sb.append("\tMessages:"); |
| 86 | + if (messages != null) { |
| 87 | + for (ProtocolMessage message : messages) { |
| 88 | + sb.append(message.toCompactString()); |
| 89 | + sb.append(", "); |
| 90 | + } |
| 91 | + sb.append("\n"); |
| 92 | + } else { |
| 93 | + sb.append("null (no messages set)"); |
| 94 | + } |
| 95 | + return sb.toString(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public String toCompactString() { |
| 100 | + StringBuilder sb = new StringBuilder(super.toCompactString()); |
| 101 | + if ((messages != null) && (!messages.isEmpty())) { |
| 102 | + sb.append(" ("); |
| 103 | + for (ProtocolMessage message : messages) { |
| 104 | + sb.append(message.toCompactString()); |
| 105 | + sb.append(","); |
| 106 | + } |
| 107 | + sb.deleteCharAt(sb.lastIndexOf(",")).append(")"); |
| 108 | + } else { |
| 109 | + sb.append(" (no messages set)"); |
| 110 | + } |
| 111 | + return sb.toString(); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public boolean executedAsPlanned() { |
| 116 | + return isExecuted(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void setRecords(List<AbstractRecord> records) { |
| 121 | + this.records = records; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void reset() { |
| 126 | + List<ModifiableVariableHolder> holders = new LinkedList<>(); |
| 127 | + if (messages != null) { |
| 128 | + for (ProtocolMessage message : messages) { |
| 129 | + holders.addAll(message.getAllModifiableVariableHolders()); |
| 130 | + } |
| 131 | + } |
| 132 | + if (getRecords() != null) { |
| 133 | + for (AbstractRecord record : getRecords()) { |
| 134 | + holders.addAll(record.getAllModifiableVariableHolders()); |
| 135 | + } |
| 136 | + } |
| 137 | + for (ModifiableVariableHolder holder : holders) { |
| 138 | + List<Field> fields = holder.getAllModifiableVariableFields(); |
| 139 | + for (Field f : fields) { |
| 140 | + f.setAccessible(true); |
| 141 | + |
| 142 | + ModifiableVariable mv = null; |
| 143 | + try { |
| 144 | + mv = (ModifiableVariable) f.get(holder); |
| 145 | + } catch (IllegalArgumentException | IllegalAccessException ex) { |
| 146 | + LOGGER.warn("Could not retrieve ModifiableVariables"); |
| 147 | + LOGGER.debug(ex); |
| 148 | + } |
| 149 | + if (mv != null) { |
| 150 | + if (mv.getModification() != null || mv.isCreateRandomModification()) { |
| 151 | + mv.setOriginalValue(null); |
| 152 | + } else { |
| 153 | + try { |
| 154 | + f.set(holder, null); |
| 155 | + } catch (IllegalArgumentException | IllegalAccessException ex) { |
| 156 | + LOGGER.warn("Could not strip ModifiableVariable without Modification"); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + setExecuted(null); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public List<ProtocolMessage> getSendMessages() { |
| 167 | + return messages; |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public List<AbstractRecord> getSendRecords() { |
| 172 | + return records; |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public boolean equals(Object obj) { |
| 177 | + if (this == obj) { |
| 178 | + return true; |
| 179 | + } |
| 180 | + if (obj == null) { |
| 181 | + return false; |
| 182 | + } |
| 183 | + if (getClass() != obj.getClass()) { |
| 184 | + return false; |
| 185 | + } |
| 186 | + final SendDynamicClientKeyExchangeAction other = (SendDynamicClientKeyExchangeAction) obj; |
| 187 | + if (!Objects.equals(this.messages, other.messages)) { |
| 188 | + return false; |
| 189 | + } |
| 190 | + if (!Objects.equals(this.records, other.records)) { |
| 191 | + return false; |
| 192 | + } |
| 193 | + return super.equals(obj); |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public int hashCode() { |
| 198 | + int hash = super.hashCode(); |
| 199 | + hash = 67 * hash + Objects.hashCode(this.messages); |
| 200 | + hash = 67 * hash + Objects.hashCode(this.records); |
| 201 | + |
| 202 | + return hash; |
| 203 | + } |
| 204 | + |
| 205 | +} |
0 commit comments