Skip to content

Commit 1c5e998

Browse files
committed
DTLS: Throw TlsFatalAlertReceived when fatal alert received
1 parent 160db88 commit 1c5e998

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

tls/src/main/java/org/bouncycastle/tls/DTLSClientProtocol.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public DTLSTransport connect(TlsClient client, DatagramTransport transport)
5151
{
5252
return clientHandshake(state, recordLayer);
5353
}
54+
catch (TlsFatalAlertReceived fatalAlertReceived)
55+
{
56+
// assert recordLayer.isFailed();
57+
invalidateSession(state);
58+
throw fatalAlertReceived;
59+
}
5460
catch (TlsFatalAlert fatalAlert)
5561
{
5662
abortClientHandshake(state, recordLayer, fatalAlert.getAlertDescription());

tls/src/main/java/org/bouncycastle/tls/DTLSRecordLayer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ boolean isClosed()
150150
return closed;
151151
}
152152

153+
boolean isFailed()
154+
{
155+
return failed;
156+
}
157+
153158
void resetAfterHelloVerifyRequestServer(long recordSeq)
154159
{
155160
this.inConnection = true;
@@ -740,7 +745,7 @@ else if (null != retransmitEpoch && epoch == retransmitEpoch.getEpoch())
740745
if (alertLevel == AlertLevel.fatal)
741746
{
742747
failed();
743-
throw new TlsFatalAlert(alertDescription);
748+
throw new TlsFatalAlertReceived(alertDescription);
744749
}
745750

746751
// TODO Can close_notify be a fatal alert?

tls/src/main/java/org/bouncycastle/tls/DTLSServerProtocol.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ public DTLSTransport accept(TlsServer server, DatagramTransport transport, DTLSR
6767
{
6868
return serverHandshake(state, recordLayer, request);
6969
}
70+
catch (TlsFatalAlertReceived fatalAlertReceived)
71+
{
72+
// assert recordLayer.isFailed();
73+
invalidateSession(state);
74+
throw fatalAlertReceived;
75+
}
7076
catch (TlsFatalAlert fatalAlert)
7177
{
7278
abortServerHandshake(state, recordLayer, fatalAlert.getAlertDescription());

tls/src/main/java/org/bouncycastle/tls/DTLSTransport.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public int receive(byte[] buf, int off, int len, int waitMillis, DTLSRecordCallb
5555
{
5656
return recordLayer.receive(buf, off, len, waitMillis, recordCallback);
5757
}
58+
catch (TlsFatalAlertReceived fatalAlertReceived)
59+
{
60+
// assert recordLayer.isFailed();
61+
throw fatalAlertReceived;
62+
}
5863
catch (TlsFatalAlert fatalAlert)
5964
{
6065
if (AlertDescription.bad_record_mac == fatalAlert.getAlertDescription())
@@ -107,6 +112,11 @@ public int receivePending(byte[] buf, int off, int len, DTLSRecordCallback recor
107112
{
108113
return recordLayer.receivePending(buf, off, len, recordCallback);
109114
}
115+
catch (TlsFatalAlertReceived fatalAlertReceived)
116+
{
117+
// assert recordLayer.isFailed();
118+
throw fatalAlertReceived;
119+
}
110120
catch (TlsFatalAlert fatalAlert)
111121
{
112122
if (AlertDescription.bad_record_mac == fatalAlert.getAlertDescription())

tls/src/main/java/org/bouncycastle/tls/TlsProtocol.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ protected boolean isTLSv13ConnectionState()
144144
private TlsOutputStream tlsOutputStream = null;
145145

146146
private volatile boolean closed = false;
147-
private volatile boolean failedWithError = false;
147+
private volatile boolean failed = false;
148148
private volatile boolean appDataReady = false;
149149
private volatile boolean appDataSplitEnabled = true;
150150
private volatile boolean keyUpdateEnabled = false;
@@ -324,7 +324,7 @@ protected void handleException(short alertDescription, String message, Throwable
324324
protected void handleFailure() throws IOException
325325
{
326326
this.closed = true;
327-
this.failedWithError = true;
327+
this.failed = true;
328328

329329
/*
330330
* RFC 2246 7.2.1. The session becomes unresumable if any connection is terminated
@@ -819,7 +819,7 @@ public int readApplicationData(byte[] buf, int off, int len)
819819
{
820820
if (this.closed)
821821
{
822-
if (this.failedWithError)
822+
if (this.failed)
823823
{
824824
throw new IOException("Cannot read application data on failed TLS connection");
825825
}
@@ -885,7 +885,7 @@ protected void safeReadRecord()
885885
}
886886
catch (TlsFatalAlertReceived e)
887887
{
888-
// Connection failure already handled at source
888+
// assert isFailed();
889889
throw e;
890890
}
891891
catch (TlsFatalAlert e)
@@ -916,6 +916,11 @@ protected boolean safeReadFullRecord(byte[] input, int inputOff, int inputLen)
916916
{
917917
return recordStream.readFullRecord(input, inputOff, inputLen);
918918
}
919+
catch (TlsFatalAlertReceived e)
920+
{
921+
// assert isFailed();
922+
throw e;
923+
}
919924
catch (TlsFatalAlert e)
920925
{
921926
handleException(e.getAlertDescription(), "Failed to process record", e);
@@ -1917,6 +1922,11 @@ public boolean isConnected()
19171922
return null != context && context.isConnected();
19181923
}
19191924

1925+
public boolean isFailed()
1926+
{
1927+
return failed;
1928+
}
1929+
19201930
public boolean isHandshaking()
19211931
{
19221932
if (closed)

0 commit comments

Comments
 (0)