1010
1111import de .rub .nds .modifiablevariable .util .ArrayConverter ;
1212import de .rub .nds .tlsattacker .core .certificate .CertificateKeyPair ;
13+ import de .rub .nds .tlsattacker .core .constants .CertificateType ;
1314import de .rub .nds .tlsattacker .core .constants .HandshakeByteLength ;
1415import 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 ;
1519import de .rub .nds .tlsattacker .core .exceptions .AdjustmentException ;
1620import de .rub .nds .tlsattacker .core .protocol .handler .extension .ExtensionHandler ;
1721import de .rub .nds .tlsattacker .core .protocol .handler .factory .HandlerFactory ;
3034import java .io .IOException ;
3135import org .apache .logging .log4j .LogManager ;
3236import 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 ;
3341import org .bouncycastle .crypto .tls .Certificate ;
3442
3543public 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 ) {
0 commit comments