Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NIFI-7468 Updated SSLSocketChannel to support TLS 1.3 #5152

Closed
wants to merge 4 commits into from

Conversation

exceptionfactory
Copy link
Contributor

Description of PR

NIFI-7468 Resolves several issues with SSLSocketChannel handshake processing in order to work with TLS 1.3.

SSLSocketChannel includes custom handling of javax.net.ssl.SSLEngine, which did not respond properly to Post-Handshake Messages in TLS 1.3, described in RFC 8446 Section 4.6. The SSLSocketChannel.connect() method was clearing stream and application buffers after completing the TLS handshake, resulting in the loss of Post-Handshake messages as well as network packets. This loss of data resulted in exceptions such Tag mismatch under both TLS 1.2 and TLS 1.3 when using AES-GCM cipher suites.

The SSLSocketChannelSender.close() method was also closing the connected SocketChannel, resulting in SSLSocketChannel being unable to process TLS close notify messages.

NIFI-7468 introduces a private ChannelStatus enum and convenience logging methods to support detailed logging at the TRACE level when necessary. Logs and exception messages now include both the remote address and remote port number to improve troubleshooting options.

The new SSLSocketChannelTest leverages a TLS server and client using the Netty framework to verify expected behavior for both TLS 1.2 and 1.3. Unit testing and runtime testing of TLS 1.3 requires either Java 11, or a version of Java 8 with update 272 or higher.

These changes address issues with several processors, including as ListenTCP and PutTCP, as well as Distributed Cache Clients running with TLS.

In order to streamline the review of the contribution we ask you
to ensure the following steps have been taken:

For all changes:

  • Is there a JIRA ticket associated with this PR? Is it referenced
    in the commit message?

  • Does your PR title start with NIFI-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.

  • Has your PR been rebased against the latest commit within the target branch (typically main)?

  • Is your initial contribution a single, squashed commit? Additional commits in response to PR reviewer feedback should be made on this branch and pushed to allow change tracking. Do not squash or use --force when pushing to allow for clean monitoring of changes.

For code changes:

  • Have you ensured that the full suite of tests is executed via mvn -Pcontrib-check clean install at the root nifi folder?
  • Have you written or updated unit tests to verify your changes?
  • Have you verified that the full build is successful on JDK 8?
  • Have you verified that the full build is successful on JDK 11?
  • If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?
  • If applicable, have you updated the LICENSE file, including the main LICENSE file under nifi-assembly?
  • If applicable, have you updated the NOTICE file, including the main NOTICE file found under nifi-assembly?
  • If adding new Properties, have you added .displayName in addition to .name (programmatic access) for each of the new properties?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered?

Note:

Please ensure that once the PR is submitted, you check GitHub Actions CI for build issues and submit an update to your PR as soon as possible.

- Handling additional FINISHED Handshake Status for TLS 1.3 Post-Handshake Messages per RFC 8446 Section 4.6
- Removed clearing buffers after handshake to avoid losing packets
- Updated read() method to check Handshake Status after SSLEngine.unwrap()
- Changed SSLSocketChannelSender to close SSLSocketChannel before other resources
- Added ChannelStatus enum and convenience logging methods for tracing status
- Added unit tests for TLS 1.2 and 1.3 using Netty server and client handlers
Copy link
Contributor

@gresockj gresockj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested with PutTCP/ListenTCP, works smoothly with TLSv1.3. I feel the updated code is more readable, and the trace log statements are a great addition for any future gotchas. I don't have any issues with the code, so I'm a +1.

@exceptionfactory
Copy link
Contributor Author

Tested with PutTCP/ListenTCP, works smoothly with TLSv1.3. I feel the updated code is more readable, and the trace log statements are a great addition for any future gotchas. I don't have any issues with the code, so I'm a +1.

Thanks for the review and verification @gresockj!

@markobean
Copy link
Contributor

@gresockj Did you test using Java 8 or 11 (or both)?

@gresockj
Copy link
Contributor

gresockj commented Jun 15, 2021

@gresockj Did you test using Java 8 or 11 (or both)?

Only 8. I'll try 11 tomorrow

@thenatog
Copy link
Contributor

Will test this out

@thenatog
Copy link
Contributor

+1, changes look good. I tested this in a 2 node secure cluster with a S2S and PostHTTP and ListenHTTP using RestrictedSSLContext for TLS 1.2 and TLS 1.3 using Java 8 (Zulu 1.8.0_292-b10) and Java 11 (11.0.10+9-LTS) at run time. Also tested PutTCP/ListenTCP with TLS 1.3 and Java 11 (11.0.10+9-LTS).

@gresockj
Copy link
Contributor

@exceptionfactory did you say the DistributedMapCacheClient should benefit from this fix? I just tried a flow with DetectDuplicate using DistributedMapCacheClient configured with a RestrictedSSLContext on TLS 1.3 (Java 11.0.11+9-LTS), and got the following exception:

2021-06-17 06:23:21,952 ERROR [Timer-Driven Process Thread-5] o.a.n.p.standard.DetectDuplicate DetectDuplicate[id=197ce08d-017a-1000-0000-0000344cdf04] Unable to communicate with cache when processing StandardFlowFileRecord[uuid=86078a69-0508-4ef6-897d-35a54a4daf1c,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1623925229794-1, container=default, section=1], offset=74372, length=4],offset=0,name=86078a69-0508-4ef6-897d-35a54a4daf1c,size=4] due to javax.net.ssl.SSLException: Tag mismatch!: javax.net.ssl.SSLException: Tag mismatch!
javax.net.ssl.SSLException: Tag mismatch!
        at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:133)
        at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:349)
        at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:292)
        at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:287)
        at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:122)
        at java.base/sun.security.ssl.SSLEngineImpl.decode(SSLEngineImpl.java:681)
        at java.base/sun.security.ssl.SSLEngineImpl.readRecord(SSLEngineImpl.java:636)
        at java.base/sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:454)
        at java.base/sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:433)
        at java.base/javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:637)
        at org.apache.nifi.remote.io.socket.ssl.SSLSocketChannel.unwrap(SSLSocketChannel.java:662)
        at org.apache.nifi.remote.io.socket.ssl.SSLSocketChannel.read(SSLSocketChannel.java:371)
        at org.apache.nifi.remote.io.socket.ssl.SSLSocketChannelInputStream.read(SSLSocketChannelInputStream.java:46)
        at java.base/java.io.BufferedInputStream.fill(BufferedInputStream.java:252)
        at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:271)
        at java.base/java.io.FilterInputStream.read(FilterInputStream.java:83)
        at org.apache.nifi.distributed.cache.protocol.ProtocolHandshake.initiateVersionNegotiation(ProtocolHandshake.java:83)
        at org.apache.nifi.distributed.cache.protocol.ProtocolHandshake.initiateHandshake(ProtocolHandshake.java:57)
        at org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService.leaseCommsSession(DistributedMapCacheClientService.java:426)
        at org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService.withCommsSession(DistributedMapCacheClientService.java:489)
        at org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService.getAndPutIfAbsent(DistributedMapCacheClientService.java:172)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.apache.nifi.controller.service.StandardControllerServiceInvocationHandler.invoke(StandardControllerServiceInvocationHandler.java:254)
        at org.apache.nifi.controller.service.StandardControllerServiceInvocationHandler.invoke(StandardControllerServiceInvocationHandler.java:105)
        at com.sun.proxy.$Proxy175.getAndPutIfAbsent(Unknown Source)
        at org.apache.nifi.processors.standard.DetectDuplicate.onTrigger(DetectDuplicate.java:183)
        at org.apache.nifi.processor.AbstractProcessor.onTrigger(AbstractProcessor.java:27)
        at org.apache.nifi.controller.StandardProcessorNode.onTrigger(StandardProcessorNode.java:1180)
        at org.apache.nifi.controller.tasks.ConnectableTask.invoke(ConnectableTask.java:214)
        at org.apache.nifi.controller.scheduling.TimerDrivenSchedulingAgent$1.run(TimerDrivenSchedulingAgent.java:103)
        at org.apache.nifi.engine.FlowEngine$2.run(FlowEngine.java:110)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
        at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305)
        at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: javax.crypto.AEADBadTagException: Tag mismatch!
        at java.base/com.sun.crypto.provider.GaloisCounterMode.decryptFinal(GaloisCounterMode.java:623)
        at java.base/com.sun.crypto.provider.CipherCore.finalNoPadding(CipherCore.java:1116)
        at java.base/com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:1053)
        at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:941)
        at java.base/com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:491)
        at java.base/javax.crypto.CipherSpi.bufferCrypt(CipherSpi.java:779)
        at java.base/javax.crypto.CipherSpi.engineDoFinal(CipherSpi.java:730)
        at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2497)
        at java.base/sun.security.ssl.SSLCipher$T13GcmReadCipherGenerator$GcmReadCipher.decrypt(SSLCipher.java:1929)
        at java.base/sun.security.ssl.SSLEngineInputRecord.decodeInputRecord(SSLEngineInputRecord.java:240)
        at java.base/sun.security.ssl.SSLEngineInputRecord.decode(SSLEngineInputRecord.java:197)
        at java.base/sun.security.ssl.SSLEngineInputRecord.decode(SSLEngineInputRecord.java:160)
        at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:110)
        ... 35 common frames omitted

This only occurred once at the very beginning of processing ~100,000 flow files, and with a retry, it was successful.

@exceptionfactory
Copy link
Contributor Author

@gresockj The DistributedMapCacheClientService depends on SSLSocketChannel as seen in the stack trace, so it should also work with TLS 1.3.

Thanks for noting that problem, the updated read() method was incorrect and was not returning a single byte, which ProtocolHandshake.initiateVersionNegotiation() uses to determine status.

I corrected the SSLSocketChannel.read() method and updated one of the unit test methods to exercise the method as well as the read(byte[]) method.

@gresockj
Copy link
Contributor

Thanks for the update, @exceptionfactory! This appears to have resolved the issue.

@exceptionfactory
Copy link
Contributor Author

Thanks for confirming the read method changes @gresockj.

@thenatog do you have any additional feedback?

@thenatog
Copy link
Contributor

Tested again, nothing more from me. Merging.

@thenatog thenatog closed this in 6a83115 Jun 23, 2021
timeabarna pushed a commit to timeabarna/nifi that referenced this pull request Jul 6, 2021
- Handling additional FINISHED Handshake Status for TLS 1.3 Post-Handshake Messages per RFC 8446 Section 4.6
- Removed clearing buffers after handshake to avoid losing packets
- Updated read() method to check Handshake Status after SSLEngine.unwrap()
- Changed SSLSocketChannelSender to close SSLSocketChannel before other resources
- Added ChannelStatus enum and convenience logging methods for tracing status
- Added unit tests for TLS 1.2 and 1.3 using Netty server and client handlers

NIFI-8704 Updated netty-handler to 4.1.65.Final

NIFI-7468 Corrected SSLSocketChannel.read() to return byte read

NIFI-7468 Adjusted comment formatting

Signed-off-by: Nathan Gough <thenatog@gmail.com>

This closes apache#5152.
timeabarna pushed a commit to timeabarna/nifi that referenced this pull request Jul 21, 2021
- Handling additional FINISHED Handshake Status for TLS 1.3 Post-Handshake Messages per RFC 8446 Section 4.6
- Removed clearing buffers after handshake to avoid losing packets
- Updated read() method to check Handshake Status after SSLEngine.unwrap()
- Changed SSLSocketChannelSender to close SSLSocketChannel before other resources
- Added ChannelStatus enum and convenience logging methods for tracing status
- Added unit tests for TLS 1.2 and 1.3 using Netty server and client handlers

NIFI-8704 Updated netty-handler to 4.1.65.Final

NIFI-7468 Corrected SSLSocketChannel.read() to return byte read

NIFI-7468 Adjusted comment formatting

Signed-off-by: Nathan Gough <thenatog@gmail.com>

This closes apache#5152.
krisztina-zsihovszki pushed a commit to krisztina-zsihovszki/nifi that referenced this pull request Jun 28, 2022
- Handling additional FINISHED Handshake Status for TLS 1.3 Post-Handshake Messages per RFC 8446 Section 4.6
- Removed clearing buffers after handshake to avoid losing packets
- Updated read() method to check Handshake Status after SSLEngine.unwrap()
- Changed SSLSocketChannelSender to close SSLSocketChannel before other resources
- Added ChannelStatus enum and convenience logging methods for tracing status
- Added unit tests for TLS 1.2 and 1.3 using Netty server and client handlers

NIFI-8704 Updated netty-handler to 4.1.65.Final

NIFI-7468 Corrected SSLSocketChannel.read() to return byte read

NIFI-7468 Adjusted comment formatting

Signed-off-by: Nathan Gough <thenatog@gmail.com>

This closes apache#5152.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants