-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
TLS PSK implementation #1777
Open
sunnysingh85
wants to merge
11
commits into
Netflix:master
Choose a base branch
from
sunnysingh85:tls-psk
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
TLS PSK implementation #1777
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
29124da
TLS PSK implementation
sunnysingh85 e906fb9
Breaking apart the PSK creation to an interface
sunnysingh85 0ef6c0a
Added license
sunnysingh85 99d644a
Store PSK info in handshake info
sunnysingh85 a19fa60
Addressed review comments
sunnysingh85 3cc8293
Added license
sunnysingh85 a3c5d39
1) moving read of byte buff and release to helper method in TLSPSKHan…
deeptiv1991 44de590
adding license header
deeptiv1991 4777374
Merge branch 'master' into tls-psk
deeptiv1991 4ba06ab
Merge branch 'Netflix:master' into tls-psk
deeptiv1991 9ce8a3d
adding back old SslHandshakeInfo constructor for backward compatibility
deeptiv1991 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
import com.netflix.netty.common.channel.config.CommonChannelConfigKeys; | ||
import com.netflix.netty.common.http2.DynamicHttp2FrameLogger; | ||
import com.netflix.zuul.netty.server.BaseZuulChannelInitializer; | ||
import com.netflix.zuul.netty.server.psk.TlsPskHandler; | ||
import io.netty.channel.ChannelHandler; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelPipeline; | ||
|
@@ -33,12 +34,13 @@ | |
import io.netty.handler.logging.LogLevel; | ||
import io.netty.handler.ssl.ApplicationProtocolNames; | ||
import io.netty.handler.ssl.ApplicationProtocolNegotiationHandler; | ||
import io.netty.handler.ssl.SslHandshakeCompletionEvent; | ||
import io.netty.util.AttributeKey; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Http2 Or Http Handler | ||
* | ||
* <p> | ||
* Author: Arthur Gonigberg | ||
* Date: December 15, 2017 | ||
*/ | ||
|
@@ -47,6 +49,8 @@ public class Http2OrHttpHandler extends ApplicationProtocolNegotiationHandler { | |
public static final String PROTOCOL_HTTP_1_1 = "HTTP/1.1"; | ||
public static final String PROTOCOL_HTTP_2 = "HTTP/2"; | ||
|
||
private static final String FALLBACK_APPLICATION_PROTOCOL = ApplicationProtocolNames.HTTP_1_1; | ||
|
||
private static final DynamicHttp2FrameLogger FRAME_LOGGER = | ||
new DynamicHttp2FrameLogger(LogLevel.DEBUG, Http2FrameCodec.class); | ||
|
||
|
@@ -62,7 +66,7 @@ public Http2OrHttpHandler( | |
ChannelHandler http2StreamHandler, | ||
ChannelConfig channelConfig, | ||
Consumer<ChannelPipeline> addHttpHandlerFn) { | ||
super(ApplicationProtocolNames.HTTP_1_1); | ||
super(FALLBACK_APPLICATION_PROTOCOL); | ||
this.http2StreamHandler = http2StreamHandler; | ||
this.maxConcurrentStreams = channelConfig.get(CommonChannelConfigKeys.maxConcurrentStreams); | ||
this.initialWindowSize = channelConfig.get(CommonChannelConfigKeys.initialWindowSize); | ||
|
@@ -72,6 +76,45 @@ public Http2OrHttpHandler( | |
this.addHttpHandlerFn = addHttpHandlerFn; | ||
} | ||
|
||
/** | ||
* this method is inspired by ApplicationProtocolNegotiationHandler.userEventTriggered | ||
*/ | ||
@Override | ||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: add a javadoc saying this method is inspired by |
||
if (evt instanceof SslHandshakeCompletionEvent handshakeEvent) { | ||
if (handshakeEvent.isSuccess()) { | ||
TlsPskHandler tlsPskHandler = ctx.channel().pipeline().get(TlsPskHandler.class); | ||
if (tlsPskHandler != null) { | ||
// PSK mode | ||
try { | ||
String tlsPskApplicationProtocol = tlsPskHandler.getApplicationProtocol(); | ||
configurePipeline( | ||
ctx, | ||
tlsPskApplicationProtocol != null | ||
? tlsPskApplicationProtocol | ||
: FALLBACK_APPLICATION_PROTOCOL); | ||
} catch (Throwable cause) { | ||
exceptionCaught(ctx, cause); | ||
} finally { | ||
// Handshake failures are handled in exceptionCaught(...). | ||
if (handshakeEvent.isSuccess()) { | ||
removeSelfIfPresent(ctx); | ||
} | ||
} | ||
} else { | ||
// non PSK mode | ||
super.userEventTriggered(ctx, evt); | ||
} | ||
} else { | ||
// handshake failures | ||
// TODO sunnys - handle PSK handshake failures | ||
super.userEventTriggered(ctx, evt); | ||
} | ||
} else { | ||
super.userEventTriggered(ctx, evt); | ||
} | ||
} | ||
|
||
@Override | ||
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) throws Exception { | ||
if (ApplicationProtocolNames.HTTP_2.equals(protocol)) { | ||
|
@@ -120,4 +163,11 @@ private void configureHttp2(ChannelPipeline pipeline) { | |
private void configureHttp1(ChannelPipeline pipeline) { | ||
addHttpHandlerFn.accept(pipeline); | ||
} | ||
|
||
private void removeSelfIfPresent(ChannelHandlerContext ctx) { | ||
ChannelPipeline pipeline = ctx.pipeline(); | ||
if (!ctx.isRemoved()) { | ||
pipeline.remove(this); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
zuul-core/src/main/java/com/netflix/zuul/netty/server/psk/ClientPSKIdentityInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 2024 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.zuul.netty.server.psk; | ||
|
||
public record ClientPSKIdentityInfo(byte[] clientPSKIdentity) { | ||
} |
22 changes: 22 additions & 0 deletions
22
zuul-core/src/main/java/com/netflix/zuul/netty/server/psk/ExternalTlsPskProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2024 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.zuul.netty.server.psk; | ||
|
||
|
||
public interface ExternalTlsPskProvider { | ||
byte[] provide(byte[] clientPskIdentity, byte[] clientRandom) throws PskCreationFailureException; | ||
} |
46 changes: 46 additions & 0 deletions
46
zuul-core/src/main/java/com/netflix/zuul/netty/server/psk/PskCreationFailureException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2024 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.zuul.netty.server.psk; | ||
|
||
public class PskCreationFailureException extends Exception { | ||
|
||
public enum TlsAlertMessage { | ||
/** | ||
* The server does not recognize the (client) PSK identity | ||
*/ | ||
unknown_psk_identity, | ||
/** | ||
* The (client) PSK identity existed but the key was incorrect | ||
*/ | ||
decrypt_error, | ||
} | ||
|
||
private final TlsAlertMessage tlsAlertMessage; | ||
|
||
public PskCreationFailureException(TlsAlertMessage tlsAlertMessage, String message) { | ||
super(message); | ||
this.tlsAlertMessage = tlsAlertMessage; | ||
} | ||
|
||
public PskCreationFailureException(TlsAlertMessage tlsAlertMessage, String message, Throwable cause) { | ||
super(message, cause); | ||
this.tlsAlertMessage = tlsAlertMessage; | ||
} | ||
|
||
public TlsAlertMessage getTlsAlertMessage() { | ||
return tlsAlertMessage; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
zuul-core/src/main/java/com/netflix/zuul/netty/server/psk/TlsPskDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2024 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.zuul.netty.server.psk; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.ChannelFutureListener; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.ByteToMessageDecoder; | ||
import io.netty.handler.ssl.SslHandshakeCompletionEvent; | ||
import org.bouncycastle.tls.TlsFatalAlert; | ||
|
||
import java.util.List; | ||
|
||
public class TlsPskDecoder extends ByteToMessageDecoder { | ||
|
||
private final TlsPskServerProtocol tlsPskServerProtocol; | ||
|
||
public TlsPskDecoder(TlsPskServerProtocol tlsPskServerProtocol) { | ||
this.tlsPskServerProtocol = tlsPskServerProtocol; | ||
} | ||
|
||
@Override | ||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { | ||
final byte[] bytesRead = in.hasArray() ? in.array() : TlsPskUtils.readDirect(in); | ||
try { | ||
tlsPskServerProtocol.offerInput(bytesRead); | ||
} catch (TlsFatalAlert tlsFatalAlert) { | ||
writeOutputIfAvailable(ctx); | ||
ctx.fireUserEventTriggered(new SslHandshakeCompletionEvent(tlsFatalAlert)); | ||
ctx.close(); | ||
return; | ||
} | ||
writeOutputIfAvailable(ctx); | ||
final int appDataAvailable = tlsPskServerProtocol.getAvailableInputBytes(); | ||
if (appDataAvailable > 0) { | ||
byte[] appData = new byte[appDataAvailable]; | ||
tlsPskServerProtocol.readInput(appData, 0, appDataAvailable); | ||
out.add(Unpooled.wrappedBuffer(appData)); | ||
} | ||
} | ||
|
||
private void writeOutputIfAvailable(ChannelHandlerContext ctx) { | ||
final int availableOutputBytes = tlsPskServerProtocol.getAvailableOutputBytes(); | ||
// output is available immediately (handshake not complete), pipe that back to the client right away | ||
if (availableOutputBytes != 0) { | ||
byte[] outputBytes = new byte[availableOutputBytes]; | ||
tlsPskServerProtocol.readOutput(outputBytes, 0, availableOutputBytes); | ||
ctx.writeAndFlush(Unpooled.wrappedBuffer(outputBytes)) | ||
.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider keeping the original constructor for backwards compatibility, and adding a new constructor with these two new parameters