1010-include_lib (" public_key/include/public_key.hrl" ).
1111
1212-export ([peer_cert_issuer /1 , peer_cert_subject /1 , peer_cert_validity /1 ]).
13- -export ([peer_cert_subject_items /2 , peer_cert_auth_name /1 ]).
13+ -export ([peer_cert_subject_items /2 , peer_cert_auth_name /1 , peer_cert_auth_name / 2 ]).
1414-export ([cipher_suites_erlang /2 , cipher_suites_erlang /1 ,
1515 cipher_suites_openssl /2 , cipher_suites_openssl /1 ,
1616 cipher_suites /1 ]).
1717-export ([info /2 , cert_info /2 ]).
1818
1919% %--------------------------------------------------------------------------
2020
21- -export_type ([certificate / 0 ]).
21+ -export_type ([certificate / 0 , ssl_cert_login_type / 0 ]).
2222
2323% Due to API differences between OTP releases.
2424-dialyzer (no_missing_calls ).
@@ -109,28 +109,51 @@ peer_cert_subject_alternative_names(Cert, Type) ->
109109peer_cert_validity (Cert ) ->
110110 rabbit_cert_info :validity (Cert ).
111111
112+ -type ssl_cert_login_type () ::
113+ {subject_alternative_name | subject_alt_name , atom (), integer ()} |
114+ {distinguished_name | common_name , undefined , undefined }.
115+
116+ -spec extract_ssl_cert_login_settings () -> none | ssl_cert_login_type ().
117+ extract_ssl_cert_login_settings () ->
118+ case application :get_env (rabbit , ssl_cert_login_from ) of
119+ {ok , Mode } ->
120+ case Mode of
121+ subject_alternative_name -> extract_san_login_type (Mode );
122+ subject_alt_name -> extract_san_login_type (Mode );
123+ _ -> {Mode , undefined , undefined }
124+ end ;
125+ undefined -> none
126+ end .
127+
128+ extract_san_login_type (Mode ) ->
129+ {Mode ,
130+ application :get_env (rabbit , ssl_cert_login_san_type , dns ),
131+ application :get_env (rabbit , ssl_cert_login_san_index , 0 )
132+ }.
133+
112134% % Extract a username from the certificate
113135-spec peer_cert_auth_name (certificate ()) -> binary () | 'not_found' | 'unsafe' .
114136peer_cert_auth_name (Cert ) ->
115- {ok , Mode } = application :get_env (rabbit , ssl_cert_login_from ),
116- peer_cert_auth_name (Mode , Cert ).
137+ case extract_ssl_cert_login_settings () of
138+ none -> 'not_found' ;
139+ Settings -> peer_cert_auth_name (Settings , Cert )
140+ end .
117141
118- -spec peer_cert_auth_name (atom (), certificate ()) -> binary () | 'not_found' | 'unsafe' .
119- peer_cert_auth_name (distinguished_name , Cert ) ->
142+ -spec peer_cert_auth_name (ssl_cert_login_type (), certificate ()) -> binary () | 'not_found' | 'unsafe' .
143+ peer_cert_auth_name ({ distinguished_name , _ , _ } , Cert ) ->
120144 case auth_config_sane () of
121145 true -> iolist_to_binary (peer_cert_subject (Cert ));
122146 false -> unsafe
123147 end ;
124148
125- peer_cert_auth_name (subject_alt_name , Cert ) ->
126- peer_cert_auth_name (subject_alternative_name , Cert );
149+ peer_cert_auth_name ({ subject_alt_name , Type , Index0 } , Cert ) ->
150+ peer_cert_auth_name ({ subject_alternative_name , Type , Index0 } , Cert );
127151
128- peer_cert_auth_name (subject_alternative_name , Cert ) ->
152+ peer_cert_auth_name ({ subject_alternative_name , Type , Index0 } , Cert ) ->
129153 case auth_config_sane () of
130154 true ->
131- Type = application :get_env (rabbit , ssl_cert_login_san_type , dns ),
132155 % % lists:nth/2 is 1-based
133- Index = application : get_env ( rabbit , ssl_cert_login_san_index , 0 ) + 1 ,
156+ Index = Index0 + 1 ,
134157 OfType = peer_cert_subject_alternative_names (Cert , otp_san_type (Type )),
135158 rabbit_log :debug (" Peer certificate SANs of type ~ts : ~tp , index to use with lists:nth/2: ~b " , [Type , OfType , Index ]),
136159 case length (OfType ) of
@@ -152,7 +175,7 @@ peer_cert_auth_name(subject_alternative_name, Cert) ->
152175 false -> unsafe
153176 end ;
154177
155- peer_cert_auth_name (common_name , Cert ) ->
178+ peer_cert_auth_name ({ common_name , _ , _ } , Cert ) ->
156179 % % If there is more than one CN then we join them with "," in a
157180 % % vaguely DN-like way. But this is more just so we do something
158181 % % more intelligent than crashing, if you actually want to escape
0 commit comments