-
Notifications
You must be signed in to change notification settings - Fork 176
/
SSLSessionHandlerAdapter.java
110 lines (95 loc) · 3.93 KB
/
SSLSessionHandlerAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright 2017 the original author or authors.
*
* 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 io.r2dbc.postgresql.client;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import io.r2dbc.postgresql.message.frontend.SSLRequest;
import reactor.core.publisher.Mono;
import java.net.SocketAddress;
/**
* SSL handler assuming the endpoint a Postgres endpoint following the {@link SSLRequest} flow.
*
* @see <a href="https://www.postgresql.org/docs/current/protocol-flow.html#id-1.10.5.7.11">https://www.postgresql.org/docs/current/protocol-flow.html#id-1.10.5.7.11</a>
*/
final class SSLSessionHandlerAdapter extends AbstractPostgresSSLHandlerAdapter {
private final ByteBufAllocator alloc;
private final SSLConfig sslConfig;
private boolean negotiating = true;
SSLSessionHandlerAdapter(ByteBufAllocator alloc, SocketAddress socketAddress, SSLConfig sslConfig) {
super(alloc, socketAddress, sslConfig);
this.alloc = alloc;
this.sslConfig = sslConfig;
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
if (negotiating) {
Mono.from(SSLRequest.INSTANCE.encode(this.alloc)).subscribe(ctx::writeAndFlush);
}
super.channelActive(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
if (negotiating) {
// If we receive channel inactive before negotiated, then the inbound has closed early.
PostgresqlSslException e = new PostgresqlSslException("Connection closed during SSL negotiation");
completeHandshakeExceptionally(e);
}
super.channelInactive(ctx);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (negotiating) {
ByteBuf buf = (ByteBuf) msg;
char response = (char) buf.readByte();
try {
switch (response) {
case 'S':
processSslEnabled(ctx, buf);
break;
case 'N':
processSslDisabled();
break;
default:
throw new IllegalStateException("Unknown SSLResponse from server: '" + response + "'");
}
} finally {
buf.release();
negotiating = false;
}
} else {
super.channelRead(ctx, msg);
}
}
private void processSslDisabled() {
if (this.sslConfig.getSslMode().requireSsl()) {
PostgresqlSslException e =
new PostgresqlSslException("Server support for SSL connection is disabled, but client was configured with SSL mode " + this.sslConfig.getSslMode());
completeHandshakeExceptionally(e);
} else {
completeHandshake();
}
}
private void processSslEnabled(ChannelHandlerContext ctx, ByteBuf msg) {
if (this.sslConfig.getSslMode() == SSLMode.DISABLE) {
PostgresqlSslException e = new PostgresqlSslException("Server requires SSL handshake, but client was configured with SSL mode DISABLE");
completeHandshakeExceptionally(e);
return;
}
ctx.channel().pipeline().addFirst(this.getSslHandler());
ctx.fireChannelRead(msg.retain());
}
}