Skip to content

Commit 310bf16

Browse files
committed
Add functions to get erlang or openssl formatted ciphers.
SSL application provides API to get ciphers by format or by default/all/anonymous, but not both, so it's not possible to get all openssl-formatted ciphers. OTP-20 and OTP-21 have different modules containing cipher formatting functions - using function_exported to support both. Addresses rabbitmq/rabbitmq-cli#342
1 parent 314b644 commit 310bf16

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

src/rabbit_ssl.erl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,78 @@
2020

2121
-export([peer_cert_issuer/1, peer_cert_subject/1, peer_cert_validity/1]).
2222
-export([peer_cert_subject_items/2, peer_cert_auth_name/1]).
23+
-export([cipher_suites_erlang/2, cipher_suites_erlang/1,
24+
cipher_suites_openssl/2, cipher_suites_openssl/1,
25+
cipher_suites/1]).
2326

2427
%%--------------------------------------------------------------------------
2528

2629
-export_type([certificate/0]).
2730

2831
-type certificate() :: rabbit_cert_info:certificate().
2932

33+
-type cipher_suites_mode() :: default | all | anonymous.
34+
35+
-spec cipher_suites(cipher_suites_mode()) -> ssl:ciphers().
36+
cipher_suites(Mode) ->
37+
Version = get_highest_protocol_version(),
38+
ssl:cipher_suites(Mode, Version).
39+
40+
-spec cipher_suites_erlang(cipher_suites_mode()) ->
41+
[ssl:old_cipher_suite()].
42+
cipher_suites_erlang(Mode) ->
43+
Version = get_highest_protocol_version(),
44+
cipher_suites_erlang(Mode, Version).
45+
46+
-spec cipher_suites_erlang(cipher_suites_mode(),
47+
ssl:protocol_version() | tls_record:tls_version()) ->
48+
[ssl:old_cipher_suite()].
49+
cipher_suites_erlang(Mode, Version) ->
50+
[ format_cipher_erlang(C)
51+
|| C <- ssl:cipher_suites(Mode, Version) ].
52+
53+
-spec cipher_suites_openssl(cipher_suites_mode()) ->
54+
[ssl:old_cipher_suite()].
55+
cipher_suites_openssl(Mode) ->
56+
Version = get_highest_protocol_version(),
57+
cipher_suites_openssl(Mode, Version).
58+
59+
-spec cipher_suites_openssl(cipher_suites_mode(),
60+
ssl:protocol_version() | tls_record:tls_version()) ->
61+
[ssl:old_cipher_suite()].
62+
cipher_suites_openssl(Mode, Version) ->
63+
lists:filtermap(fun(C) ->
64+
OpenSSL = format_cipher_openssl(C),
65+
case is_list(OpenSSL) of
66+
true -> {true, OpenSSL};
67+
false -> false
68+
end
69+
end,
70+
ssl:cipher_suites(Mode, Version)).
71+
72+
73+
%% OTP-20.3 and OTP-21 have different modules containing cipher format functions
74+
%% This is not a hot codepath and `function_exported` should not slow things down much.
75+
format_cipher_erlang(Cipher) ->
76+
case erlang:function_exported(ssl_cipher_format, suite, 1) of
77+
true ->
78+
ssl_cipher_format:erl_suite_definition(ssl_cipher_format:suite(Cipher));
79+
false ->
80+
ssl_cipher:erl_suite_definition(ssl_cipher:suite(Cipher))
81+
end.
82+
83+
format_cipher_openssl(Cipher) ->
84+
case erlang:function_exported(ssl_cipher_format, suite, 1) of
85+
true ->
86+
ssl_cipher_format:openssl_suite_name(ssl_cipher_format:suite(Cipher));
87+
false ->
88+
ssl_cipher:openssl_suite_name(ssl_cipher:suite(Cipher))
89+
end.
90+
91+
-spec get_highest_protocol_version() -> tls_record:tls_version().
92+
get_highest_protocol_version() ->
93+
tls_record:highest_protocol_version([]).
94+
3095
%%--------------------------------------------------------------------------
3196
%% High-level functions used by reader
3297
%%--------------------------------------------------------------------------

0 commit comments

Comments
 (0)