-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
v4.4: feat(listener): TLS partial_chain validation #10553
Merged
qzhuyan
merged 13 commits into
emqx:main-v4.4
from
qzhuyan:dev/william/tls-root-fun-verify-partial-chain
May 10, 2023
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a18018b
feat(tls-partial-chain): add partial_chain support for TLS listeners
qzhuyan ea35b20
test(tls): test refactoring
qzhuyan 11c8e93
docs: changelog for TLS listener partial certificate chain validation
qzhuyan 8503d3c
test(tls-partial-chains): renewed intermediate_cacert
qzhuyan c3430b8
feat(tls-partial-chain): just return trusted CA.
qzhuyan 30cccab
chore: review comments
qzhuyan 6d0a768
feat(tls-partial-chains): error handling for invalid cacertfile
qzhuyan 1520bd7
feat(tls-partial-chains): update appup src
qzhuyan 90efea4
fix(test): sign CSR2
qzhuyan f8bb1b7
fix(tls-partial-chain): stack trace
qzhuyan 151176a
test(tls-partial-chain): add more tests
qzhuyan 285d3da
feat(tls-partial-chain): support CAcert renewal
qzhuyan 7894bb0
chore: update app up src
qzhuyan File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1646,6 +1646,10 @@ end}. | |
{datatype, atom} | ||
]}. | ||
|
||
{mapping, "listener.ssl.$name.partial_chain", "emqx.listeners", [ | ||
{datatype, atom} | ||
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. {datatype, {enum, [true, two_cacerts_from_cacertfile, cacert_from_cacertfile]}} |
||
]}. | ||
|
||
{mapping, "listener.ssl.$name.fail_if_no_peer_cert", "emqx.listeners", [ | ||
{datatype, {enum, [true, false]}} | ||
]}. | ||
|
@@ -2377,6 +2381,7 @@ end}. | |
{certfile, cuttlefish:conf_get(Prefix ++ ".certfile", Conf, undefined)}, | ||
{cacertfile, cuttlefish:conf_get(Prefix ++ ".cacertfile", Conf, undefined)}, | ||
{verify, cuttlefish:conf_get(Prefix ++ ".verify", Conf, undefined)}, | ||
{partial_chain, cuttlefish:conf_get(Prefix ++ ".partial_chain", Conf, undefined)}, | ||
{fail_if_no_peer_cert, cuttlefish:conf_get(Prefix ++ ".fail_if_no_peer_cert", Conf, undefined)}, | ||
{secure_renegotiate, cuttlefish:conf_get(Prefix ++ ".secure_renegotiate", Conf, undefined)}, | ||
{reuse_sessions, cuttlefish:conf_get(Prefix ++ ".reuse_sessions", Conf, undefined)}, | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
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,45 @@ | ||
%%-------------------------------------------------------------------- | ||
%% Copyright (c) 2023 EMQ Technologies Co., Ltd. All Rights Reserved. | ||
%% | ||
%% 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. | ||
%% | ||
%% @doc Never update this module, create a v3 instead. | ||
%%-------------------------------------------------------------------- | ||
|
||
-module(emqx_const_v2). | ||
|
||
-export([ make_tls_root_fun/2 | ||
]). | ||
|
||
%% @doc Build a root fun for verify TLS partial_chain. | ||
%% The `InputChain' is composed by OTP SSL with local cert store | ||
%% AND the cert (chain if any) from the client. | ||
%% @end | ||
make_tls_root_fun(cacert_from_cacertfile, [Trusted]) -> | ||
%% Allow only one trusted ca cert, and just return the defined trusted CA cert, | ||
fun(_InputChain) -> | ||
%% Note, returing `trusted_ca` doesn't really mean it accepts the connection | ||
%% OTP SSL app will do the path validation, signature validation subsequently. | ||
{trusted_ca, Trusted} | ||
end; | ||
make_tls_root_fun(cacert_from_cacertfile, [TrustedOne, TrustedTwo]) -> | ||
%% Allow two trusted CA certs in case of CA cert renewal | ||
%% This is a little expensive call as it compares the binaries. | ||
fun(InputChain) -> | ||
case lists:member(TrustedOne, InputChain) of | ||
true -> | ||
{trusted_ca, TrustedOne}; | ||
false -> | ||
{trusted_ca, TrustedTwo} | ||
end | ||
end. |
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 |
---|---|---|
@@ -0,0 +1,173 @@ | ||
%%-------------------------------------------------------------------- | ||
%% Copyright (c) 2023 EMQ Technologies Co., Ltd. All Rights Reserved. | ||
%% | ||
%% 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. | ||
%%-------------------------------------------------------------------- | ||
-module(emqx_listener_tls_verify_chain_SUITE). | ||
|
||
-compile(export_all). | ||
-compile(nowarn_export_all). | ||
|
||
-include_lib("emqx/include/emqx.hrl"). | ||
-include_lib("emqx/include/emqx_mqtt.hrl"). | ||
-include_lib("eunit/include/eunit.hrl"). | ||
-include_lib("common_test/include/ct.hrl"). | ||
|
||
-import(emqx_test_tls_certs_helper, [ fail_when_ssl_error/1 | ||
, fail_when_no_ssl_alert/2 | ||
, generate_tls_certs/1 | ||
]). | ||
|
||
|
||
all() -> emqx_ct:all(?MODULE). | ||
|
||
init_per_suite(Config) -> | ||
generate_tls_certs(Config), | ||
application:ensure_all_started(esockd), | ||
[{ssl_config, ssl_config_verify_peer()} | Config]. | ||
|
||
end_per_suite(_Config) -> | ||
application:stop(esockd). | ||
|
||
t_conn_fail_with_intermediate_ca_cert(Config) -> | ||
Port = emqx_test_tls_certs_helper:select_free_port(ssl), | ||
DataDir = ?config(data_dir, Config), | ||
Options = [{ssl_options, [ {cacertfile, filename:join(DataDir, "intermediate1.pem")} | ||
, {certfile, filename:join(DataDir, "server1.pem")} | ||
, {keyfile, filename:join(DataDir, "server1.key")} | ||
| ?config(ssl_config, Config) | ||
]}], | ||
emqx_listeners:start_listener(ssl, Port, Options), | ||
{ok, Socket} = ssl:connect({127, 0, 0, 1}, Port, [{keyfile, filename:join(DataDir, "client1.key")}, | ||
{certfile, filename:join(DataDir, "client1.pem")} | ||
], 1000), | ||
|
||
fail_when_no_ssl_alert(Socket, unknown_ca), | ||
ok = ssl:close(Socket). | ||
|
||
|
||
t_conn_fail_with_other_intermediate_ca_cert(Config) -> | ||
Port = emqx_test_tls_certs_helper:select_free_port(ssl), | ||
DataDir = ?config(data_dir, Config), | ||
Options = [{ssl_options, [ {cacertfile, filename:join(DataDir, "intermediate1.pem")} | ||
, {certfile, filename:join(DataDir, "server1.pem")} | ||
, {keyfile, filename:join(DataDir, "server1.key")} | ||
| ?config(ssl_config, Config) | ||
]}], | ||
emqx_listeners:start_listener(ssl, Port, Options), | ||
{ok, Socket} = ssl:connect({127, 0, 0, 1}, Port, [{keyfile, filename:join(DataDir, "client2.key")}, | ||
{certfile, filename:join(DataDir, "client2.pem")} | ||
], 1000), | ||
|
||
fail_when_no_ssl_alert(Socket, unknown_ca), | ||
ok = ssl:close(Socket). | ||
|
||
t_conn_success_with_server_client_composed_complete_chain(Config) -> | ||
Port = emqx_test_tls_certs_helper:select_free_port(ssl), | ||
DataDir = ?config(data_dir, Config), | ||
%% Server has root ca cert | ||
Options = [{ssl_options, [ {cacertfile, filename:join(DataDir, "root.pem")} | ||
, {certfile, filename:join(DataDir, "server2.pem")} | ||
, {keyfile, filename:join(DataDir, "server2.key")} | ||
| ?config(ssl_config, Config) | ||
]}], | ||
%% Client has complete chain | ||
emqx_listeners:start_listener(ssl, Port, Options), | ||
{ok, Socket} = ssl:connect({127, 0, 0, 1}, Port, [{keyfile, filename:join(DataDir, "client2.key")}, | ||
{certfile, filename:join(DataDir, "client2-intermediate2-bundle.pem")} | ||
], 1000), | ||
fail_when_ssl_error(Socket), | ||
ok = ssl:close(Socket). | ||
|
||
t_conn_success_with_other_signed_client_composed_complete_chain(Config) -> | ||
Port = emqx_test_tls_certs_helper:select_free_port(ssl), | ||
DataDir = ?config(data_dir, Config), | ||
%% Server has root ca cert | ||
Options = [{ssl_options, [ {cacertfile, filename:join(DataDir, "root.pem")} | ||
, {certfile, filename:join(DataDir, "server1.pem")} | ||
, {keyfile, filename:join(DataDir, "server1.key")} | ||
| ?config(ssl_config, Config) | ||
]}], | ||
%% Client has partial_chain | ||
emqx_listeners:start_listener(ssl, Port, Options), | ||
{ok, Socket} = ssl:connect({127, 0, 0, 1}, Port, [{keyfile, filename:join(DataDir, "client2.key")}, | ||
{certfile, filename:join(DataDir, "client2-intermediate2-bundle.pem")} | ||
], 1000), | ||
fail_when_ssl_error(Socket), | ||
ok = ssl:close(Socket). | ||
|
||
t_conn_success_with_renewed_intermediate_root_bundle(Config) -> | ||
Port = emqx_test_tls_certs_helper:select_free_port(ssl), | ||
DataDir = ?config(data_dir, Config), | ||
%% Server has root ca cert | ||
Options = [{ssl_options, [ {cacertfile, filename:join(DataDir, "intermediate1_renewed-root-bundle.pem")} | ||
, {certfile, filename:join(DataDir, "server1.pem")} | ||
, {keyfile, filename:join(DataDir, "server1.key")} | ||
| ?config(ssl_config, Config) | ||
]}], | ||
emqx_listeners:start_listener(ssl, Port, Options), | ||
{ok, Socket} = ssl:connect({127, 0, 0, 1}, Port, [{keyfile, filename:join(DataDir, "client1.key")}, | ||
{certfile, filename:join(DataDir, "client1.pem")} | ||
], 1000), | ||
fail_when_ssl_error(Socket), | ||
ok = ssl:close(Socket). | ||
|
||
t_conn_success_with_client_complete_cert_chain(Config) -> | ||
Port = emqx_test_tls_certs_helper:select_free_port(ssl), | ||
DataDir = ?config(data_dir, Config), | ||
Options = [{ssl_options, [ {cacertfile, filename:join(DataDir, "root.pem")} | ||
, {certfile, filename:join(DataDir, "server2.pem")} | ||
, {keyfile, filename:join(DataDir, "server2.key")} | ||
| ?config(ssl_config, Config) | ||
]}], | ||
emqx_listeners:start_listener(ssl, Port, Options), | ||
{ok, Socket} = ssl:connect({127, 0, 0, 1}, Port, [{keyfile, filename:join(DataDir, "client2.key")}, | ||
{certfile, filename:join(DataDir, "client2-complete-bundle.pem")} | ||
], 1000), | ||
fail_when_ssl_error(Socket), | ||
ok = ssl:close(Socket). | ||
|
||
t_conn_fail_with_server_partial_chain(Config) -> | ||
Port = emqx_test_tls_certs_helper:select_free_port(ssl), | ||
DataDir = ?config(data_dir, Config), | ||
Options = [{ssl_options, [{cacertfile, filename:join(DataDir, "intermediate2.pem")} %% imcomplete at server side | ||
, {certfile, filename:join(DataDir, "server2.pem")} | ||
, {keyfile, filename:join(DataDir, "server2.key")} | ||
| ?config(ssl_config, Config) | ||
]}], | ||
emqx_listeners:start_listener(ssl, Port, Options), | ||
{ok, Socket} = ssl:connect({127, 0, 0, 1}, Port, [{keyfile, filename:join(DataDir, "client2.key")}, | ||
{certfile, filename:join(DataDir, "client2-complete-bundle.pem")} | ||
], 1000), | ||
fail_when_no_ssl_alert(Socket, unknown_ca), | ||
ok = ssl:close(Socket). | ||
|
||
t_conn_fail_without_root_cacert(Config) -> | ||
Port = emqx_test_tls_certs_helper:select_free_port(ssl), | ||
DataDir = ?config(data_dir, Config), | ||
Options = [{ssl_options, [ {cacertfile, filename:join(DataDir, "intermediate2.pem")} | ||
, {certfile, filename:join(DataDir, "server2.pem")} | ||
, {keyfile, filename:join(DataDir, "server2.key")} | ||
| ?config(ssl_config, Config) | ||
]}], | ||
emqx_listeners:start_listener(ssl, Port, Options), | ||
{ok, Socket} = ssl:connect({127, 0, 0, 1}, Port, [{keyfile, filename:join(DataDir, "client2.key")}, | ||
{certfile, filename:join(DataDir, "client2-intermediate2-bundle.pem")} | ||
], 1000), | ||
fail_when_no_ssl_alert(Socket, unknown_ca), | ||
ok = ssl:close(Socket). | ||
|
||
ssl_config_verify_peer() -> | ||
[ {verify, verify_peer} | ||
, {fail_if_no_peer_cert, true} | ||
]. |
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.
最后一个证书 ->
最后一个或两个证书