Skip to content

Commit db41e77

Browse files
committed
TLS: Updates from bc-java
1 parent c2e150a commit db41e77

8 files changed

Lines changed: 82 additions & 30 deletions

File tree

‎tls/src/main/java/org/bouncycastle/tls/CertificateStatus.java‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,15 @@ public static CertificateStatus parse(TlsContext context, InputStream input) thr
147147
{
148148
case CertificateStatusType.ocsp:
149149
{
150-
requireStatusRequestVersion(1, statusRequestVersion);
150+
checkStatusType(status_type, 1, statusRequestVersion);
151151

152152
byte[] derEncoding = TlsUtils.readOpaque24(input, 1);
153153
response = parseOCSPResponse(derEncoding);
154154
break;
155155
}
156156
case CertificateStatusType.ocsp_multi:
157157
{
158-
requireStatusRequestVersion(2, statusRequestVersion);
158+
checkStatusType(status_type, 2, statusRequestVersion);
159159

160160
byte[] ocsp_response_list = TlsUtils.readOpaque24(input, 1);
161161
ByteArrayInputStream buf = new ByteArrayInputStream(ocsp_response_list);
@@ -235,6 +235,17 @@ protected static OCSPResponse parseOCSPResponse(byte[] derEncoding) throws IOExc
235235
return ocspResponse;
236236
}
237237

238+
protected static void checkStatusType(short status_type, int minVersion, int statusRequestVersion)
239+
throws IOException
240+
{
241+
if (statusRequestVersion < minVersion)
242+
{
243+
throw new TlsFatalAlert(AlertDescription.decode_error,
244+
"Invalid status_type " + CertificateStatusType.getText(status_type));
245+
}
246+
}
247+
248+
/** @deprecated Use {@link #checkStatusType(short, int, int)} instead. */
238249
protected static void requireStatusRequestVersion(int minVersion, int statusRequestVersion)
239250
throws IOException
240251
{

‎tls/src/main/java/org/bouncycastle/tls/CertificateStatusType.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,22 @@ public class CertificateStatusType
1111
* RFC 6961
1212
*/
1313
public static final short ocsp_multi = 2;
14+
15+
public static String getName(short certificateStatusType)
16+
{
17+
switch (certificateStatusType)
18+
{
19+
case ocsp:
20+
return "ocsp";
21+
case ocsp_multi:
22+
return "ocsp_multi";
23+
default:
24+
return "UNKNOWN";
25+
}
26+
}
27+
28+
public static String getText(short certificateStatusType)
29+
{
30+
return getName(certificateStatusType) + "(" + certificateStatusType + ")";
31+
}
1432
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ protected DTLSTransport clientHandshake(ClientHandshakeState state)
302302
}
303303
else
304304
{
305-
state.keyExchange.processClientCredentials(clientAuthCredentials);
305+
state.keyExchange.processClientCredentials(clientAuthCredentials);
306306
}
307307

308308
Vector clientSupplementalData = client.getClientSupplementalData();
@@ -547,8 +547,7 @@ protected byte[] generateClientHello(ClientHandshakeState state)
547547
* TLS_EMPTY_RENEGOTIATION_INFO_SCSV signaling cipher suite value in the ClientHello.
548548
* Including both is NOT RECOMMENDED.
549549
*/
550-
boolean noRenegExt = (null == TlsUtils.getExtensionData(state.clientExtensions,
551-
TlsProtocol.EXT_RenegotiationInfo));
550+
boolean noRenegExt = !TlsUtils.hasExtension(state.clientExtensions, TlsProtocol.EXT_RenegotiationInfo);
552551
boolean noRenegSCSV = !Arrays.contains(state.offeredCipherSuites,
553552
CipherSuite.TLS_EMPTY_RENEGOTIATION_INFO_SCSV);
554553

@@ -852,7 +851,6 @@ protected void processServerHello(ClientHandshakeState state, byte[] body)
852851
}
853852

854853
/*
855-
*
856854
* RFC 3546 2.2 Note that the extended server hello message is only sent in response to an
857855
* extended client hello message. However, see RFC 5746 exception below. We always include
858856
* the SCSV, so an Extended Server Hello is always allowed.
@@ -884,7 +882,7 @@ protected void processServerHello(ClientHandshakeState state, byte[] body)
884882
* associated ClientHello, it MUST abort the handshake with an unsupported_extension
885883
* fatal alert.
886884
*/
887-
if (null == TlsUtils.getExtensionData(state.clientExtensions, extType))
885+
if (!TlsUtils.hasExtension(state.clientExtensions, extType))
888886
{
889887
throw new TlsFatalAlert(AlertDescription.unsupported_extension,
890888
"Unrequested extension in ServerHello: " + ExtensionType.getText(extType.intValue()));

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ protected DTLSTransport serverHandshake(ServerHandshakeState state, DTLSRequest
324324
/*
325325
* RFC 5246 If no suitable certificate is available, the client MUST send a
326326
* certificate message containing no certificates.
327-
*
327+
*
328328
* NOTE: In previous RFCs, this was SHOULD instead of MUST.
329329
*/
330330
throw new TlsFatalAlert(AlertDescription.unexpected_message);
@@ -612,9 +612,8 @@ else if (resumedSession)
612612
*/
613613
if (securityParameters.isSecureRenegotiation())
614614
{
615-
byte[] serverRenegExtData = TlsUtils.getExtensionData(state.serverExtensions,
615+
boolean noRenegExt = !TlsUtils.hasExtension(state.serverExtensions,
616616
TlsProtocol.EXT_RenegotiationInfo);
617-
boolean noRenegExt = (null == serverRenegExtData);
618617

619618
if (noRenegExt)
620619
{
@@ -904,7 +903,7 @@ protected void processClientHello(ServerHandshakeState state, ClientHello client
904903
server.notifyOfferedCipherSuites(offeredCipherSuites);
905904

906905
/*
907-
* TODO[resumption] Check RFC 7627 5.4. for required behaviour
906+
* TODO[resumption] Check RFC 7627 5.4. for required behaviour
908907
*/
909908

910909
byte[] clientRenegExtData = TlsUtils.getExtensionData(clientHelloExtensions,

‎tls/src/main/java/org/bouncycastle/tls/ExtensionType.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ public static boolean isRecognized(int extensionType)
284284
case key_share:
285285
case connection_id:
286286
case renegotiation_info:
287+
288+
/*
289+
* WARNING: When adding a new "recognized" extension type, also register with
290+
* TlsUtils.isPermittedExtensionType13 the TLS 1.3 messages it may appear in (if any). A recognized
291+
* type not registered there is rejected with illegal_parameter in any TLS 1.3 message that is checked.
292+
*/
293+
287294
return true;
288295
default:
289296
return false;

‎tls/src/main/java/org/bouncycastle/tls/TlsClientProtocol.java‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ protected void handleHandshakeMessage(short type, HandshakeMessageInput buf)
624624
}
625625
else
626626
{
627-
keyExchange.processClientCredentials(clientAuthCredentials);
627+
keyExchange.processClientCredentials(clientAuthCredentials);
628628
}
629629

630630
Vector clientSupplementalData = tlsClient.getClientSupplementalData();
@@ -637,7 +637,7 @@ protected void handleHandshakeMessage(short type, HandshakeMessageInput buf)
637637
if (certificateRequest != null)
638638
{
639639
sendCertificateMessage(clientAuthCertificate, null);
640-
this.connection_state = CS_CLIENT_CERTIFICATE;
640+
this.connection_state = CS_CLIENT_CERTIFICATE;
641641
}
642642

643643
sendClientKeyExchange();
@@ -879,7 +879,7 @@ protected void process13HelloRetryRequest(ServerHello helloRetryRequest)
879879
continue;
880880
}
881881

882-
if (null == TlsUtils.getExtensionData(clientExtensions, extType))
882+
if (!TlsUtils.hasExtension(clientExtensions, extType))
883883
{
884884
throw new TlsFatalAlert(AlertDescription.unsupported_extension,
885885
"Unrequested extension in HelloRetryRequest: " + ExtensionType.getText(extensionType));
@@ -1252,7 +1252,7 @@ protected void processServerHello(ServerHello serverHello)
12521252
/*
12531253
* RFC 3546 2.2 Note that the extended server hello message is only sent in response to an
12541254
* extended client hello message.
1255-
*
1255+
*
12561256
* However, see RFC 5746 exception below. We always include the SCSV, so an Extended Server
12571257
* Hello is always allowed.
12581258
*/
@@ -1283,7 +1283,7 @@ protected void processServerHello(ServerHello serverHello)
12831283
* associated ClientHello, it MUST abort the handshake with an unsupported_extension
12841284
* fatal alert.
12851285
*/
1286-
if (null == TlsUtils.getExtensionData(clientHelloExtensions, extType))
1286+
if (!TlsUtils.hasExtension(clientHelloExtensions, extType))
12871287
{
12881288
throw new TlsFatalAlert(AlertDescription.unsupported_extension,
12891289
"Unrequested extension in ServerHello: " + ExtensionType.getText(extType.intValue()));
@@ -1508,7 +1508,7 @@ protected void receive13CertificateRequest(ByteArrayInputStream buf, boolean pos
15081508
throw new TlsFatalAlert(AlertDescription.internal_error);
15091509
}
15101510

1511-
/*
1511+
/*
15121512
* RFC 8446 4.3.2. A server which is authenticating with a certificate MAY optionally
15131513
* request a certificate from the client.
15141514
*/
@@ -1554,7 +1554,7 @@ protected void receive13EncryptedExtensions(ByteArrayInputStream buf)
15541554
{
15551555
Integer extType = (Integer)e.nextElement();
15561556

1557-
if (null == TlsUtils.getExtensionData(clientExtensions, extType))
1557+
if (!TlsUtils.hasExtension(clientExtensions, extType))
15581558
{
15591559
throw new TlsFatalAlert(AlertDescription.unsupported_extension,
15601560
"Unrequested extension in EncryptedExtensions: " + ExtensionType.getText(extType.intValue()));
@@ -1918,7 +1918,7 @@ protected void sendClientHello()
19181918
this.clientBinders = TlsUtils.addPreSharedKeyToClientHello(tlsClientContext, tlsClient, clientExtensions,
19191919
offeredCipherSuites);
19201920

1921-
// TODO[tls13-psk] Perhaps don't add key_share if external PSK(s) offered and 'psk_dhe_ke' not offered
1921+
// TODO[tls13-psk] Perhaps don't add key_share if external PSK(s) offered and 'psk_dhe_ke' not offered
19221922
this.clientAgreements = TlsUtils.addKeyShareToClientHello(tlsClientContext, tlsClient, clientExtensions);
19231923

19241924
if (shouldUseEMS && TlsUtils.isExtendedMasterSecretOptional(supportedVersions))
@@ -1967,7 +1967,7 @@ else if (offeringTLSv12Minus)
19671967
* TLS_EMPTY_RENEGOTIATION_INFO_SCSV signaling cipher suite value in the ClientHello.
19681968
* Including both is NOT RECOMMENDED.
19691969
*/
1970-
boolean noRenegExt = (null == TlsUtils.getExtensionData(clientExtensions, EXT_RenegotiationInfo));
1970+
boolean noRenegExt = !TlsUtils.hasExtension(clientExtensions, EXT_RenegotiationInfo);
19711971
boolean noRenegSCSV = !Arrays.contains(offeredCipherSuites, CipherSuite.TLS_EMPTY_RENEGOTIATION_INFO_SCSV);
19721972

19731973
if (noRenegExt && noRenegSCSV)

‎tls/src/main/java/org/bouncycastle/tls/TlsServerProtocol.java‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ protected ServerHello generate13ServerHello(ClientHello clientHello, HandshakeMe
202202

203203
/*
204204
* TODO[tls13] Confirm fields in the ClientHello haven't changed
205-
*
205+
*
206206
* RFC 8446 4.1.2 [..] when the server has responded to its ClientHello with a
207207
* HelloRetryRequest [..] the client MUST send the same ClientHello without
208208
* modification, except as follows: [key_share, early_data, cookie, pre_shared_key,
@@ -259,7 +259,7 @@ protected ServerHello generate13ServerHello(ClientHello clientHello, HandshakeMe
259259

260260
/*
261261
* NOTE: Currently no server support for session resumption
262-
*
262+
*
263263
* If adding support, ensure securityParameters.tlsUnique is set to the localVerifyData, but
264264
* ONLY when extended_master_secret has been negotiated (otherwise NULL).
265265
*/
@@ -448,7 +448,7 @@ protected ServerHello generateServerHello(ClientHello clientHello, HandshakeMess
448448
this.offeredCipherSuites = clientHello.getCipherSuites();
449449

450450

451-
451+
452452
SecurityParameters securityParameters = tlsServerContext.getSecurityParametersHandshake();
453453

454454
tlsServerContext.setClientSupportedVersions(
@@ -469,7 +469,7 @@ protected ServerHello generateServerHello(ClientHello clientHello, HandshakeMess
469469
clientVersion = ProtocolVersion.getLatestTLS(tlsServerContext.getClientSupportedVersions());
470470
}
471471

472-
// Set the legacy_record_version to use for early alerts
472+
// Set the legacy_record_version to use for early alerts
473473
recordStream.setWriteVersion(clientVersion);
474474

475475
if (!ProtocolVersion.SERVER_EARLIEST_SUPPORTED_TLS.isEqualOrEarlierVersionOf(clientVersion))
@@ -784,8 +784,7 @@ else if (resumedSession)
784784
*/
785785
if (securityParameters.isSecureRenegotiation())
786786
{
787-
byte[] serverRenegExtData = TlsUtils.getExtensionData(this.serverExtensions, EXT_RenegotiationInfo);
788-
boolean noRenegExt = (null == serverRenegExtData);
787+
boolean noRenegExt = !TlsUtils.hasExtension(this.serverExtensions, EXT_RenegotiationInfo);
789788

790789
if (noRenegExt)
791790
{
@@ -882,7 +881,7 @@ protected void handle13HandshakeMessage(short type, HandshakeMessageInput buf)
882881

883882
/*
884883
* TODO[tls13] Abbreviated handshakes (PSK resumption)
885-
*
884+
*
886885
* NOTE: No CertificateRequest, Certificate, CertificateVerify messages, but client
887886
* might now send EndOfEarlyData after receiving server Finished message.
888887
*/
@@ -1292,7 +1291,7 @@ else if (TlsUtils.isTLSv12(tlsServerContext))
12921291
/*
12931292
* RFC 5246 If no suitable certificate is available, the client MUST send a
12941293
* certificate message containing no certificates.
1295-
*
1294+
*
12961295
* NOTE: In previous RFCs, this was SHOULD instead of MUST.
12971296
*/
12981297
throw new TlsFatalAlert(AlertDescription.unexpected_message);

‎tls/src/main/java/org/bouncycastle/tls/TlsUtils.java‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,11 @@ public static byte[] getExtensionData(Hashtable extensions, Integer extensionTyp
12981298
return extensions == null ? null : (byte[])extensions.get(extensionType);
12991299
}
13001300

1301+
public static boolean hasExtension(Hashtable extensions, Integer extensionType)
1302+
{
1303+
return extensions != null && extensions.containsKey(extensionType);
1304+
}
1305+
13011306
public static boolean hasExpectedEmptyExtensionData(Hashtable extensions, Integer extensionType,
13021307
short alertDescription) throws IOException
13031308
{
@@ -5870,8 +5875,8 @@ static Certificate add13CertificateStatus(Certificate certificate, CertificateSt
58705875
? (OCSPResponse)ocspResponseList.elementAt(i)
58715876
: null;
58725877

5873-
if (null != ocspResponse
5874-
&& null == TlsUtils.getExtensionData(extensions, TlsExtensionsUtils.EXT_status_request))
5878+
if (null != ocspResponse &&
5879+
!TlsUtils.hasExtension(extensions, TlsExtensionsUtils.EXT_status_request))
58755880
{
58765881
byte[] extensionData = TlsExtensionsUtils.createStatusRequestExtension13(
58775882
new CertificateStatus(CertificateStatusType.ocsp, ocspResponse));
@@ -6152,13 +6157,26 @@ static boolean isPermittedExtensionType13(int handshakeType, int extensionType)
61526157
switch (extensionType)
61536158
{
61546159
case ExtensionType.server_name:
6160+
{
6161+
switch (handshakeType)
6162+
{
6163+
case HandshakeType.client_hello:
6164+
case HandshakeType.encrypted_extensions:
6165+
case HandshakeType.certificate_request:
6166+
return true;
6167+
default:
6168+
return false;
6169+
}
6170+
}
61556171
case ExtensionType.max_fragment_length:
61566172
case ExtensionType.supported_groups:
61576173
case ExtensionType.use_srtp:
61586174
case ExtensionType.heartbeat:
61596175
case ExtensionType.application_layer_protocol_negotiation:
61606176
case ExtensionType.client_certificate_type:
61616177
case ExtensionType.server_certificate_type:
6178+
case ExtensionType.cached_info:
6179+
case ExtensionType.record_size_limit:
61626180
{
61636181
switch (handshakeType)
61646182
{
@@ -6222,6 +6240,8 @@ static boolean isPermittedExtensionType13(int handshakeType, int extensionType)
62226240
}
62236241
}
62246242
case ExtensionType.pre_shared_key:
6243+
// TODO DTLS 1.3 differences
6244+
// case ExtensionType.connection_id:
62256245
{
62266246
switch (handshakeType)
62276247
{

0 commit comments

Comments
 (0)