Skip to content

Commit bcb96b9

Browse files
author
Paul Fiterau
committed
Merge remote-tracking branch 'origin/raw_publickeys' into dtls_fixes
2 parents bf2254b + 21f972e commit bcb96b9

16 files changed

+371
-129
lines changed

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/config/Config.java

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public static Config createEmptyConfig() {
195195
/**
196196
* Which Signature and Hash algorithms we support
197197
*/
198-
private List<SignatureAndHashAlgorithm> supportedSignatureAndHashAlgorithms;
198+
private List<SignatureAndHashAlgorithm> defaultClientSupportedSignatureAndHashAlgorithms;
199199

200200
/**
201201
* Which Ciphersuites we support by default
@@ -844,12 +844,14 @@ public static Config createEmptyConfig() {
844844
*/
845845
private CipherSuite defaultSelectedCipherSuite = CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA;
846846

847+
private CertificateType defaultSelectedServerCertificateType = CertificateType.X509;
848+
849+
private CertificateType defaultSelectedClientCertificateType = CertificateType.X509;
850+
847851
private List<ECPointFormat> defaultServerSupportedPointFormats;
848852

849853
private List<ECPointFormat> defaultClientSupportedPointFormats;
850854

851-
private List<SignatureAndHashAlgorithm> defaultClientSupportedSignatureAndHashAlgorithms;
852-
853855
private List<SignatureAndHashAlgorithm> defaultServerSupportedSignatureAndHashAlgorithms;
854856

855857
private SignatureAndHashAlgorithm defaultSelectedSignatureAndHashAlgorithm = SignatureAndHashAlgorithm.RSA_SHA1;
@@ -1126,8 +1128,8 @@ public static Config createEmptyConfig() {
11261128
defaultServerConnection = new InboundConnection("server", 443, "localhost");
11271129
workflowTraceType = WorkflowTraceType.HANDSHAKE;
11281130

1129-
supportedSignatureAndHashAlgorithms = new LinkedList<>();
1130-
supportedSignatureAndHashAlgorithms.addAll(SignatureAndHashAlgorithm.getImplemented());
1131+
defaultClientSupportedSignatureAndHashAlgorithms = new LinkedList<>();
1132+
defaultClientSupportedSignatureAndHashAlgorithms.addAll(SignatureAndHashAlgorithm.getImplemented());
11311133
defaultClientSupportedCompressionMethods = new LinkedList<>();
11321134
defaultClientSupportedCompressionMethods.add(CompressionMethod.NULL);
11331135
defaultServerSupportedCompressionMethods = new LinkedList<>();
@@ -1228,6 +1230,22 @@ public Config createCopy() {
12281230
return ConfigIO.read(new ByteArrayInputStream(stream.toByteArray()));
12291231
}
12301232

1233+
public CertificateType getDefaultSelectedServerCertificateType() {
1234+
return defaultSelectedServerCertificateType;
1235+
}
1236+
1237+
public void setDefaultSelectedServerCertificateType(CertificateType defaultSelectedServerCertificateType) {
1238+
this.defaultSelectedServerCertificateType = defaultSelectedServerCertificateType;
1239+
}
1240+
1241+
public CertificateType getDefaultSelectedClientCertificateType() {
1242+
return defaultSelectedClientCertificateType;
1243+
}
1244+
1245+
public void setDefaultSelectedClientCertificateType(CertificateType defaultSelectedClientCertificateType) {
1246+
this.defaultSelectedClientCertificateType = defaultSelectedClientCertificateType;
1247+
}
1248+
12311249
public ECPointFormat getDefaultSelectedPointFormat() {
12321250
return defaultSelectedPointFormat;
12331251
}
@@ -1924,21 +1942,6 @@ public final void setDefaultClientSNIEntries(SNIEntry... defaultClientSNIEntryLi
19241942
this.defaultClientSNIEntryList = new ArrayList(Arrays.asList(defaultClientSNIEntryList));
19251943
}
19261944

1927-
public List<SignatureAndHashAlgorithm> getDefaultClientSupportedSignatureAndHashAlgorithms() {
1928-
return defaultClientSupportedSignatureAndHashAlgorithms;
1929-
}
1930-
1931-
public void setDefaultClientSupportedSignatureAndHashAlgorithms(
1932-
List<SignatureAndHashAlgorithm> defaultClientSupportedSignatureAndHashAlgorithms) {
1933-
this.defaultClientSupportedSignatureAndHashAlgorithms = defaultClientSupportedSignatureAndHashAlgorithms;
1934-
}
1935-
1936-
public final void setDefaultClientSupportedSignatureAndHashAlgorithms(
1937-
SignatureAndHashAlgorithm... defaultClientSupportedSignatureAndHashAlgorithms) {
1938-
this.defaultClientSupportedSignatureAndHashAlgorithms = Arrays
1939-
.asList(defaultClientSupportedSignatureAndHashAlgorithms);
1940-
}
1941-
19421945
public List<ECPointFormat> getDefaultServerSupportedPointFormats() {
19431946
return defaultServerSupportedPointFormats;
19441947
}
@@ -2271,18 +2274,19 @@ public void setClientAuthentication(Boolean clientAuthentication) {
22712274
this.clientAuthentication = clientAuthentication;
22722275
}
22732276

2274-
public List<SignatureAndHashAlgorithm> getSupportedSignatureAndHashAlgorithms() {
2275-
return supportedSignatureAndHashAlgorithms;
2277+
public List<SignatureAndHashAlgorithm> getDefaultClientSupportedSignatureAndHashAlgorithms() {
2278+
return defaultClientSupportedSignatureAndHashAlgorithms;
22762279
}
22772280

2278-
public void setSupportedSignatureAndHashAlgorithms(
2279-
List<SignatureAndHashAlgorithm> supportedSignatureAndHashAlgorithms) {
2280-
this.supportedSignatureAndHashAlgorithms = supportedSignatureAndHashAlgorithms;
2281+
public void setDefaultClientSupportedSignatureAndHashAlgorithms(
2282+
List<SignatureAndHashAlgorithm> defaultClientSupportedSignatureAndHashAlgorithms) {
2283+
this.defaultClientSupportedSignatureAndHashAlgorithms = defaultClientSupportedSignatureAndHashAlgorithms;
22812284
}
22822285

22832286
public final void setSupportedSignatureAndHashAlgorithms(
22842287
SignatureAndHashAlgorithm... supportedSignatureAndHashAlgorithms) {
2285-
this.supportedSignatureAndHashAlgorithms = new ArrayList(Arrays.asList(supportedSignatureAndHashAlgorithms));
2288+
this.defaultClientSupportedSignatureAndHashAlgorithms = new ArrayList(
2289+
Arrays.asList(supportedSignatureAndHashAlgorithms));
22862290
}
22872291

22882292
public List<ProtocolVersion> getSupportedVersions() {

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/config/delegate/SignatureAndHashAlgorithmDelegate.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public void setSignatureAndHashAlgorithms(List<SignatureAndHashAlgorithm> signat
3838
public void applyDelegate(Config config) {
3939
if (signatureAndHashAlgorithms != null) {
4040
config.setAddSignatureAndHashAlgorithmsExtension(true);
41-
config.setSupportedSignatureAndHashAlgorithms(signatureAndHashAlgorithms);
41+
config.setDefaultClientSupportedSignatureAndHashAlgorithms(signatureAndHashAlgorithms);
42+
config.setDefaultServerSupportedSignatureAndHashAlgorithms(signatureAndHashAlgorithms);
4243
config.setDefaultSelectedSignatureAndHashAlgorithm(signatureAndHashAlgorithms.get(0));
4344
}
4445
}

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/protocol/handler/CertificateMessageHandler.java

Lines changed: 128 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010

1111
import de.rub.nds.modifiablevariable.util.ArrayConverter;
1212
import de.rub.nds.tlsattacker.core.certificate.CertificateKeyPair;
13+
import de.rub.nds.tlsattacker.core.constants.CertificateType;
1314
import de.rub.nds.tlsattacker.core.constants.HandshakeByteLength;
1415
import de.rub.nds.tlsattacker.core.constants.HandshakeMessageType;
16+
import de.rub.nds.tlsattacker.core.constants.NamedGroup;
17+
import de.rub.nds.tlsattacker.core.crypto.ec.Point;
18+
import de.rub.nds.tlsattacker.core.crypto.ec.PointFormatter;
1519
import de.rub.nds.tlsattacker.core.exceptions.AdjustmentException;
1620
import de.rub.nds.tlsattacker.core.protocol.handler.extension.ExtensionHandler;
1721
import de.rub.nds.tlsattacker.core.protocol.handler.factory.HandlerFactory;
@@ -30,6 +34,10 @@
3034
import java.io.IOException;
3135
import org.apache.logging.log4j.LogManager;
3236
import org.apache.logging.log4j.Logger;
37+
import org.bouncycastle.asn1.ASN1InputStream;
38+
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
39+
import org.bouncycastle.asn1.DERBitString;
40+
import org.bouncycastle.asn1.DLSequence;
3341
import org.bouncycastle.crypto.tls.Certificate;
3442

3543
public class CertificateMessageHandler extends HandshakeMessageHandler<CertificateMessage> {
@@ -55,55 +63,132 @@ public CertificateMessageSerializer getSerializer(CertificateMessage message) {
5563
return new CertificateMessageSerializer(message, tlsContext.getChooser().getSelectedProtocolVersion());
5664
}
5765

66+
private CertificateType selectTypeInternally() {
67+
if (tlsContext.getTalkingConnectionEndType() == ConnectionEndType.SERVER) {
68+
return tlsContext.getChooser().getSelectedServerCertificateType();
69+
} else {
70+
return tlsContext.getChooser().getSelectedClientCertificateType();
71+
}
72+
}
73+
5874
@Override
5975
public void adjustTLSContext(CertificateMessage message) {
60-
Certificate cert;
61-
if (tlsContext.getChooser().getSelectedProtocolVersion().isTLS13()) {
62-
ByteArrayOutputStream stream = new ByteArrayOutputStream();
63-
int certificatesLength = 0;
64-
try {
65-
for (CertificatePair pair : message.getCertificatesList()) {
66-
stream.write(ArrayConverter.intToBytes(pair.getCertificateLength().getValue(),
67-
HandshakeByteLength.CERTIFICATE_LENGTH));
68-
stream.write(pair.getCertificate().getValue());
69-
certificatesLength += pair.getCertificateLength().getValue()
70-
+ HandshakeByteLength.CERTIFICATE_LENGTH;
76+
switch (selectTypeInternally()) {
77+
case OPEN_PGP:
78+
throw new UnsupportedOperationException("We do not support OpenPGP keys");
79+
case RAW_PUBLIC_KEY:
80+
LOGGER.debug("Adjusting context for RAW PUBLIC KEY ceritifate message");
81+
try {
82+
// TODO Temporary parsing, we need to redo this once
83+
// x509/asn1 attacker is integrated
84+
ASN1InputStream asn1Stream = new ASN1InputStream(message.getCertificatesListBytes().getValue());
85+
DLSequence dlSeq = (DLSequence) asn1Stream.readObject();
86+
DLSequence identifier = (DLSequence) dlSeq.getObjectAt(0);
87+
NamedGroup group = null;
88+
ASN1ObjectIdentifier keyType = (ASN1ObjectIdentifier) identifier.getObjectAt(0);
89+
if (keyType.getId().equals("1.2.840.10045.2.1")) {
90+
ASN1ObjectIdentifier curveType = (ASN1ObjectIdentifier) identifier.getObjectAt(1);
91+
if (curveType.getId().equals("1.2.840.10045.3.1.7")) {
92+
group = NamedGroup.SECP256R1;
93+
} else {
94+
throw new UnsupportedOperationException(
95+
"We currently do only support secp256r1 public keys. Sorry...");
96+
}
97+
DERBitString publicKey = (DERBitString) dlSeq.getObjectAt(1);
98+
byte[] pointBytes = publicKey.getBytes();
99+
Point publicKeyPoint = PointFormatter.formatFromByteArray(group, pointBytes);
100+
if (tlsContext.getTalkingConnectionEndType() == ConnectionEndType.SERVER) {
101+
tlsContext.setServerEcPublicKey(publicKeyPoint); // TODO
102+
// this
103+
// needs
104+
// to
105+
// be
106+
// a
107+
// new
108+
// field
109+
// in
110+
// the
111+
// context
112+
} else {
113+
tlsContext.setClientEcPublicKey(publicKeyPoint); // TODO
114+
// this
115+
// needs
116+
// to
117+
// be
118+
// a
119+
// new
120+
// field
121+
// in
122+
// the
123+
// context
124+
}
125+
} else {
126+
throw new UnsupportedOperationException(
127+
"We currently do only support EC raw public keys. Sorry...");
128+
}
129+
130+
asn1Stream.close();
131+
} catch (Exception E) {
132+
LOGGER.warn("Could read RAW PublicKey. Not adjusting context", E);
133+
71134
}
72-
} catch (IOException ex) {
73-
throw new AdjustmentException("Could not concatenate certificates bytes", ex);
74-
}
75-
cert = parseCertificate(certificatesLength, stream.toByteArray());
76-
} else {
77-
cert = parseCertificate(message.getCertificatesListLength().getValue(), message.getCertificatesListBytes()
78-
.getValue());
79-
}
80-
if (tlsContext.getTalkingConnectionEndType() == ConnectionEndType.CLIENT) {
81-
LOGGER.debug("Setting ClientCertificate in Context");
82-
tlsContext.setClientCertificate(cert);
83-
} else {
84-
LOGGER.debug("Setting ServerCertificate in Context");
85-
tlsContext.setServerCertificate(cert);
86-
}
87-
if (message.getCertificateKeyPair() != null) {
88-
LOGGER.debug("Found a certificate key pair. Adjusting in context");
89-
message.getCertificateKeyPair().adjustInContext(tlsContext, tlsContext.getTalkingConnectionEndType());
90-
} else if (cert != null) {
91-
if (cert.isEmpty()) {
92-
LOGGER.debug("Certificate is empty - no adjustments");
93-
} else {
94-
LOGGER.debug("No CertificatekeyPair found, creating new one");
95-
CertificateKeyPair pair = new CertificateKeyPair(cert);
96-
message.setCertificateKeyPair(pair);
97-
message.getCertificateKeyPair().adjustInContext(tlsContext, tlsContext.getTalkingConnectionEndType());
98-
}
135+
break;
136+
case X509:
137+
LOGGER.debug("Adjusting context for x509 ceritifate message");
138+
Certificate cert;
139+
if (tlsContext.getChooser().getSelectedProtocolVersion().isTLS13()) {
140+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
141+
int certificatesLength = 0;
142+
try {
143+
for (CertificatePair pair : message.getCertificatesList()) {
144+
stream.write(ArrayConverter.intToBytes(pair.getCertificateLength().getValue(),
145+
HandshakeByteLength.CERTIFICATE_LENGTH));
146+
stream.write(pair.getCertificate().getValue());
147+
certificatesLength += pair.getCertificateLength().getValue()
148+
+ HandshakeByteLength.CERTIFICATE_LENGTH;
149+
}
150+
} catch (IOException ex) {
151+
throw new AdjustmentException("Could not concatenate certificates bytes", ex);
152+
}
153+
cert = parseCertificate(certificatesLength, stream.toByteArray());
154+
} else {
155+
cert = parseCertificate(message.getCertificatesListLength().getValue(), message
156+
.getCertificatesListBytes().getValue());
157+
}
158+
if (tlsContext.getTalkingConnectionEndType() == ConnectionEndType.CLIENT) {
159+
LOGGER.debug("Setting ClientCertificate in Context");
160+
tlsContext.setClientCertificate(cert);
161+
} else {
162+
LOGGER.debug("Setting ServerCertificate in Context");
163+
tlsContext.setServerCertificate(cert);
164+
}
165+
if (message.getCertificateKeyPair() != null) {
166+
LOGGER.debug("Found a certificate key pair. Adjusting in context");
167+
message.getCertificateKeyPair().adjustInContext(tlsContext,
168+
tlsContext.getTalkingConnectionEndType());
169+
} else if (cert != null) {
170+
if (cert.isEmpty()) {
171+
LOGGER.debug("Certificate is empty - no adjustments");
172+
} else {
173+
LOGGER.debug("No CertificatekeyPair found, creating new one");
174+
CertificateKeyPair pair = new CertificateKeyPair(cert);
175+
message.setCertificateKeyPair(pair);
176+
message.getCertificateKeyPair().adjustInContext(tlsContext,
177+
tlsContext.getTalkingConnectionEndType());
178+
}
99179

100-
} else {
101-
LOGGER.debug("Ceritificate not parseable - no adjustments");
102-
}
180+
} else {
181+
LOGGER.debug("Ceritificate not parseable - no adjustments");
182+
}
103183

104-
if (tlsContext.getChooser().getSelectedProtocolVersion().isTLS13()) {
105-
adjustExtensions(message);
184+
if (tlsContext.getChooser().getSelectedProtocolVersion().isTLS13()) {
185+
adjustExtensions(message);
186+
}
187+
break;
188+
default:
189+
throw new UnsupportedOperationException("Unsupported CertificateType!");
106190
}
191+
107192
}
108193

109194
private Certificate parseCertificate(int lengthBytes, byte[] bytesToParse) {

TLS-Core/src/main/java/de/rub/nds/tlsattacker/core/protocol/handler/CertificateRequestHandler.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
package de.rub.nds.tlsattacker.core.protocol.handler;
1010

11+
import com.google.common.collect.Sets;
1112
import de.rub.nds.modifiablevariable.util.ArrayConverter;
1213
import de.rub.nds.tlsattacker.core.constants.ClientCertificateType;
1314
import de.rub.nds.tlsattacker.core.constants.SignatureAndHashAlgorithm;
@@ -16,6 +17,7 @@
1617
import de.rub.nds.tlsattacker.core.protocol.preparator.CertificateRequestPreparator;
1718
import de.rub.nds.tlsattacker.core.protocol.serializer.CertificateRequestSerializer;
1819
import de.rub.nds.tlsattacker.core.state.TlsContext;
20+
import java.util.Collections;
1921
import java.util.LinkedList;
2022
import java.util.List;
2123
import org.apache.logging.log4j.LogManager;
@@ -49,6 +51,7 @@ public void adjustTLSContext(CertificateRequestMessage message) {
4951
adjustClientCertificateTypes(message);
5052
adjustDistinguishedNames(message);
5153
adjustServerSupportedSignatureAndHashAlgorithms(message);
54+
adjustSelectedSignatureAndHashAlgorithm();
5255
}
5356

5457
private void adjustServerSupportedSignatureAndHashAlgorithms(CertificateRequestMessage message) {
@@ -106,4 +109,19 @@ private List<SignatureAndHashAlgorithm> convertSignatureAndHashAlgorithms(byte[]
106109
}
107110
return list;
108111
}
112+
113+
private void adjustSelectedSignatureAndHashAlgorithm() {
114+
if (Collections.disjoint(tlsContext.getChooser().getClientSupportedSignatureAndHashAlgorithms(), tlsContext
115+
.getChooser().getServerSupportedSignatureAndHashAlgorithms())) {
116+
LOGGER.warn("Client and Server have no signature and hash algorithm in common");
117+
} else {
118+
Sets.SetView<SignatureAndHashAlgorithm> intersection = Sets.intersection(
119+
Sets.newHashSet(tlsContext.getChooser().getClientSupportedSignatureAndHashAlgorithms()),
120+
Sets.newHashSet(tlsContext.getChooser().getServerSupportedSignatureAndHashAlgorithms()));
121+
SignatureAndHashAlgorithm algo = (SignatureAndHashAlgorithm) intersection.toArray()[0];
122+
tlsContext.setSelectedSignatureAndHashAlgorithm(algo);
123+
LOGGER.debug("Adjusting selected signature and hash algorithm to: " + algo.name());
124+
125+
}
126+
}
109127
}

0 commit comments

Comments
 (0)