Skip to content

Commit 58122ec

Browse files
committed
Support DTLS 1.3 client authentication during the handshake, relates to github #1468.
1 parent 74b9e36 commit 58122ec

4 files changed

Lines changed: 450 additions & 47 deletions

File tree

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

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -534,16 +534,13 @@ protected DTLSTransport clientHandshake13(ClientHandshakeState state, DTLSReliab
534534
{
535535
/*
536536
* RFC 8446 4.3.2. A server which is authenticating with a certificate MAY optionally request a
537-
* certificate from the client. Record it so that it is not silently discarded, then fail: the
538-
* client cannot yet answer one.
539-
*
540-
* TODO[dtls13] Replaced by real in-handshake client authentication (send Certificate and
541-
* CertificateVerify in the client's flight, or an empty certificate list when declining).
537+
* certificate from the client. Answered at the end of this method, once the server's Finished has
538+
* been verified. If no CertificateRequest arrives, 'state.certificateRequest' simply stays null,
539+
* which is what TlsClientProtocol.skip13CertificateRequest expresses for its dispatcher.
542540
*/
543541
receive13CertificateRequest(state, serverMessage.getBody());
544542

545-
throw new TlsFatalAlert(AlertDescription.unexpected_message,
546-
"DTLS 1.3 client authentication is not implemented");
543+
serverMessage = handshake.receiveMessage();
547544
}
548545

549546
if (serverMessage.getType() == HandshakeType.certificate)
@@ -581,6 +578,41 @@ protected DTLSTransport clientHandshake13(ClientHandshakeState state, DTLSReliab
581578
* change_cipher_spec message in either direction.
582579
*/
583580

581+
if (null != state.certificateRequest)
582+
{
583+
TlsCredentialedSigner clientCredentials = TlsUtils.establish13ClientCredentials(state.authentication,
584+
state.certificateRequest);
585+
586+
Certificate clientCertificate = null;
587+
if (null != clientCredentials)
588+
{
589+
clientCertificate = clientCredentials.getCertificate();
590+
}
591+
592+
if (null == clientCertificate)
593+
{
594+
/*
595+
* RFC 8446 4.4.2. A client that declines still answers, with an empty certificate list. In
596+
* this calling context, certificate_request_context is length 0.
597+
*/
598+
clientCertificate = Certificate.EMPTY_CHAIN_TLS13;
599+
}
600+
601+
sendCertificateMessage(clientContext, handshake, clientCertificate, null);
602+
603+
if (null != clientCredentials)
604+
{
605+
/*
606+
* NOTE: Signed over the transcript through the Certificate just sent, and exclusive of the
607+
* CertificateVerify message itself.
608+
*/
609+
DigitallySigned certificateVerify = TlsUtils.generate13CertificateVerify(clientContext,
610+
clientCredentials, handshake.getHandshakeHash());
611+
handshake.sendMessage(HandshakeType.certificate_verify,
612+
generateCertificateVerify(state, certificateVerify));
613+
}
614+
}
615+
584616
// NOTE: Calculated exclusive of the Finished message itself, and sent at the handshake epoch
585617
securityParameters.localVerifyData = TlsUtils.calculateVerifyData(clientContext,
586618
handshake.getHandshakeHash(), false);

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

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -475,13 +475,29 @@ protected DTLSTransport serverHandshake13(ServerHandshakeState state, DTLSReliab
475475
byte[] serverFinishedTranscriptHash = send13ServerHelloCoda(state, handshake, afterHelloRetryRequest);
476476

477477
/*
478-
* RFC 8446 4.4. With no CertificateRequest sent, the client's flight is its Finished alone; a
479-
* Certificate or CertificateVerify here is unexpected, which is what receiveMessageDelayedDigest
480-
* reports.
481-
*
482-
* TODO[dtls13] Task 6 adds in-handshake client authentication (CertificateRequest, then the
483-
* client's Certificate and CertificateVerify ahead of its Finished).
478+
* RFC 8446 4.4.2. A client that was sent a CertificateRequest always answers with a Certificate,
479+
* carrying an empty certificate list when it declines, and follows it with a CertificateVerify only
480+
* when that list is not empty. With no CertificateRequest sent, the client's flight is its Finished
481+
* alone, and a Certificate or CertificateVerify here is unexpected - which is what the type check in
482+
* receiveMessageDelayedDigest reports. This is the counterpart of TlsServerProtocol's
483+
* skip13ClientCertificate and skip13ClientCertificateVerify, which make the same two rules explicit
484+
* because its dispatcher reaches the Finished handler from either state.
484485
*/
486+
if (null != state.certificateRequest)
487+
{
488+
receive13ClientCertificate(state, handshake.receiveMessageBody(HandshakeType.certificate));
489+
490+
if (expectCertificateVerifyMessage(state))
491+
{
492+
// NOTE: Verified over the transcript excluding the CertificateVerify message itself
493+
DTLSReliableHandshake.Message certificateVerifyMessage = handshake.receiveMessageDelayedDigest(
494+
HandshakeType.certificate_verify);
495+
receive13ClientCertificateVerify(state, certificateVerifyMessage.getBody(),
496+
handshake.getHandshakeHash());
497+
handshake.updateHandshakeMessagesDigest(certificateVerifyMessage);
498+
}
499+
}
500+
485501
{
486502
// NOTE: Calculated exclusive of the actual Finished message from the client
487503
DTLSReliableHandshake.Message finishedMessage = handshake.receiveMessageDelayedDigest(
@@ -566,16 +582,22 @@ protected byte[] send13ServerHelloCoda(ServerHandshakeState state, DTLSReliableH
566582
{
567583
// CertificateRequest
568584
{
569-
/*
570-
* TODO[dtls13] Task 6 sends the CertificateRequest here and processes the client's
571-
* Certificate/CertificateVerify. Until then a server configured to request client
572-
* authentication must fail rather than silently complete an unauthenticated handshake.
573-
*/
574585
state.certificateRequest = server.getCertificateRequest();
575586
if (null != state.certificateRequest)
576587
{
577-
throw new TlsFatalAlert(AlertDescription.internal_error,
578-
"DTLS 1.3 client authentication is not implemented");
588+
/*
589+
* RFC 8446 4.3.2. In a handshake the 'certificate_request_context' is zero length; a
590+
* non-empty one belongs to post-handshake authentication, which this does not support.
591+
*/
592+
if (!state.certificateRequest.hasCertificateRequestContext(TlsUtils.EMPTY_BYTES))
593+
{
594+
throw new TlsFatalAlert(AlertDescription.internal_error);
595+
}
596+
597+
TlsUtils.establishServerSigAlgs(securityParameters, state.certificateRequest);
598+
599+
handshake.sendMessage(HandshakeType.certificate_request,
600+
generateCertificateRequest(state, state.certificateRequest));
579601
}
580602
}
581603

@@ -1026,6 +1048,45 @@ protected byte[] generateCertificateVerify(ServerHandshakeState state, Digitally
10261048
return buf.toByteArray();
10271049
}
10281050

1051+
/**
1052+
* Mirrors TlsServerProtocol.receive13ClientCertificate. The message is parsed exactly as the DTLS 1.2
1053+
* path parses it; RFC 8446 4.4.2's extra rule is only that the client must not send one unasked.
1054+
*/
1055+
protected void receive13ClientCertificate(ServerHandshakeState state, byte[] body)
1056+
throws IOException
1057+
{
1058+
if (null == state.certificateRequest)
1059+
{
1060+
throw new TlsFatalAlert(AlertDescription.unexpected_message);
1061+
}
1062+
1063+
processClientCertificate(state, body);
1064+
}
1065+
1066+
/**
1067+
* Mirrors TlsServerProtocol.receive13ClientCertificateVerify. The transcript passed in must exclude the
1068+
* CertificateVerify message itself, which is why it is read with a delayed digest.
1069+
*/
1070+
protected void receive13ClientCertificateVerify(ServerHandshakeState state, byte[] body,
1071+
TlsHandshakeHash handshakeHash) throws IOException
1072+
{
1073+
TlsServerContextImpl serverContext = state.serverContext;
1074+
1075+
Certificate clientCertificate = serverContext.getSecurityParametersHandshake().getPeerCertificate();
1076+
if (null == clientCertificate || clientCertificate.isEmpty())
1077+
{
1078+
throw new TlsFatalAlert(AlertDescription.internal_error);
1079+
}
1080+
1081+
ByteArrayInputStream buf = new ByteArrayInputStream(body);
1082+
1083+
CertificateVerify certificateVerify = CertificateVerify.parse(serverContext, buf);
1084+
1085+
TlsProtocol.assertEmpty(buf);
1086+
1087+
TlsUtils.verify13CertificateVerifyClient(serverContext, handshakeHash, certificateVerify);
1088+
}
1089+
10291090
/**
10301091
* Mirrors TlsServerProtocol.receive13ClientFinished (TlsProtocol.process13FinishedMessage). The
10311092
* transcript passed in must exclude the Finished message itself.

0 commit comments

Comments
 (0)