-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathriak_api_ssl.erl
288 lines (273 loc) · 12 KB
/
riak_api_ssl.erl
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
%% -------------------------------------------------------------------
%%
%% riak_api_ssl: configuration for SSL/TLS connections over PB and HTTP
%%
%% Copyright (c) 2013-2014 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you 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 Configuration and validation routines for SSL/TLS connections
%% to clients.
-module(riak_api_ssl).
-export([options/0]).
-include_lib("public_key/include/public_key.hrl").
%% @doc Returns a list of common options for SSL/TLS connections.
-spec options() -> [ssl:ssl_option()].
options() ->
CoreSSL = app_helper:get_env(riak_core, ssl),
CACertFile = proplists:get_value(cacertfile, CoreSSL),
CertFile = proplists:get_value(certfile, CoreSSL),
KeyFile = proplists:get_value(keyfile, CoreSSL),
Versions = app_helper:get_env(riak_api, tls_protocols, ['tlsv1.2']),
HonorCipherOrder = app_helper:get_env(riak_api, honor_cipher_order, false),
CheckCRL = app_helper:get_env(riak_api, check_crl, false),
CACerts = riak_core_ssl_util:load_certs(CACertFile),
ciphers() ++
[{certfile, CertFile},
{keyfile, KeyFile},
{cacerts, CACerts},
{versions, Versions},
%% force peer validation, even though
%% we don't care if the peer doesn't
%% send a certificate
{verify, verify_peer},
{server_name_indication, disable},
{reuse_sessions, false} %% required!
] ++
%% conditionally include the honor cipher order, don't pass it if it
%% disabled because it will crash any
%% OTP installs that lack the patch to
%% implement honor_cipher_order
[{honor_cipher_order, true} || HonorCipherOrder ] ++
%% if we're validating CRLs, define a
%% verify_fun for them.
[{verify_fun, {fun validate_function/3, {CACerts, []}}} || CheckCRL ].
-ifdef(deprecated_22).
ciphers() -> [].
% ciphers not support as an option when starting a SSL handshake in OTP 22
-else.
ciphers() ->
{Ciphers, _} =
riak_core_ssl_util:parse_ciphers(riak_core_security:get_ciphers()),
[{ciphers, Ciphers}].
-endif.
%% @doc Validator function for SSL negotiation.
%%
validate_function(Cert, valid_peer, State) ->
lager:debug("validing peer ~p with ~p intermediate certs",
[riak_core_ssl_util:get_common_name(Cert),
length(element(2, State))]),
%% peer certificate validated, now check the CRL
Res = (catch check_crl(Cert, State)),
lager:debug("CRL validate result for ~p: ~p",
[riak_core_ssl_util:get_common_name(Cert), Res]),
{Res, State};
validate_function(Cert, valid, {TrustedCAs, IntermediateCerts}=State) ->
case public_key:pkix_is_self_signed(Cert) of
true ->
%% this is a root cert, no CRL
{valid, {TrustedCAs, [Cert|IntermediateCerts]}};
false ->
%% check is valid CA certificate, add to the list of
%% intermediates
Res = (catch check_crl(Cert, State)),
lager:debug("CRL intermediate CA validate result for ~p: ~p",
[riak_core_ssl_util:get_common_name(Cert), Res]),
{Res, {TrustedCAs, [Cert|IntermediateCerts]}}
end;
validate_function(_Cert, {bad_cert, _} = Reason, _UserState) ->
{fail, Reason};
validate_function(_Cert, {extension, _}, UserState) ->
{unknown, UserState}.
%% @doc Given a certificate, find CRL distribution points for the given
%% certificate, fetch, and attempt to validate each CRL through
%% issuer_function/4.
%%
check_crl(Cert, State) ->
%% pull the CRL distribution point(s) out of the certificate, if any
case pubkey_cert:select_extension(?'id-ce-cRLDistributionPoints',
pubkey_cert:extensions_list(Cert#'OTPCertificate'.tbsCertificate#'OTPTBSCertificate'.extensions)) of
undefined ->
lager:debug("no CRL distribution points for ~p",
[riak_core_ssl_util:get_common_name(Cert)]),
%% fail; we can't validate if there's no CRL
no_crl;
CRLExtension ->
CRLDistPoints = CRLExtension#'Extension'.extnValue,
DPointsAndCRLs = lists:foldl(fun(Point, Acc) ->
%% try to read the CRL over http or from a
%% local file
case fetch_point(Point) of
not_available ->
Acc;
Res ->
[{Point, Res} | Acc]
end
end, [], CRLDistPoints),
public_key:pkix_crls_validate(Cert,
DPointsAndCRLs,
[{issuer_fun,
{fun issuer_function/4, State}}])
end.
%% @doc Given a list of distribution points for CRLs, certificates and
%% both trusted and intermediary certificates, attempt to build and
%% authority chain back via build_chain to verify that it is valid.
%%
issuer_function(_DP, CRL, _Issuer, {TrustedCAs, IntermediateCerts}) ->
%% XXX the 'Issuer' we get passed here is the AuthorityKeyIdentifier,
%% which we are not currently smart enough to understand
%% Read the CA certs out of the file
Certs = [public_key:pkix_decode_cert(DER, otp) || DER <- TrustedCAs],
%% get the real issuer out of the CRL
Issuer = public_key:pkix_normalize_name(
pubkey_cert_records:transform(
CRL#'CertificateList'.tbsCertList#'TBSCertList'.issuer, decode)),
%% assume certificates are ordered from root to tip
case find_issuer(Issuer, IntermediateCerts ++ Certs) of
undefined ->
lager:debug("unable to find certificate matching CRL issuer ~p",
[Issuer]),
error;
IssuerCert ->
case build_chain({public_key:pkix_encode('OTPCertificate',
IssuerCert,
otp),
IssuerCert}, IntermediateCerts, Certs, []) of
undefined ->
error;
{OTPCert, Path} ->
{ok, OTPCert, Path}
end
end.
%% @doc Attempt to build authority chain back using intermediary
%% certificates, falling back on trusted certificates if the
%% intermediary chain of certificates does not fully extend to the
%% root.
%%
%% Returns: {RootCA :: #OTPCertificate{}, Chain :: [der_encoded()]}
%%
build_chain({DER, Cert}, IntCerts, TrustedCerts, Acc) ->
%% check if this cert is self-signed, if it is, we've reached the
%% root of the chain
Issuer = public_key:pkix_normalize_name(
Cert#'OTPCertificate'.tbsCertificate#'OTPTBSCertificate'.issuer),
Subject = public_key:pkix_normalize_name(
Cert#'OTPCertificate'.tbsCertificate#'OTPTBSCertificate'.subject),
case Issuer == Subject of
true ->
case find_issuer(Issuer, TrustedCerts) of
undefined ->
undefined;
TrustedCert ->
%% return the cert from the trusted list, to prevent
%% issuer spoofing
{TrustedCert,
[public_key:pkix_encode(
'OTPCertificate', TrustedCert, otp)|Acc]}
end;
false ->
Match = lists:foldl(
fun(C, undefined) ->
S = public_key:pkix_normalize_name(C#'OTPCertificate'.tbsCertificate#'OTPTBSCertificate'.subject),
%% compare the subject to the current issuer
case Issuer == S of
true ->
%% we've found our man
{public_key:pkix_encode('OTPCertificate', C, otp), C};
false ->
undefined
end;
(_E, A) ->
%% already matched
A
end, undefined, IntCerts),
case Match of
undefined when IntCerts /= TrustedCerts ->
%% continue the chain by using the trusted CAs
lager:debug("Ran out of intermediate certs, switching to trusted certs~n"),
build_chain({DER, Cert}, TrustedCerts, TrustedCerts, Acc);
undefined ->
lager:debug("Can't construct chain of trust beyond ~p",
[riak_core_ssl_util:get_common_name(Cert)]),
%% can't find the current cert's issuer
undefined;
Match ->
build_chain(Match, IntCerts, TrustedCerts, [DER|Acc])
end
end.
%% @doc Given a certificate and a list of trusted or intermediary
%% certificates, attempt to find a match in the list or bail with
%% undefined.
find_issuer(Issuer, Certs) ->
lists:foldl(
fun(OTPCert, undefined) ->
%% check if this certificate matches the issuer
Normal = public_key:pkix_normalize_name(
OTPCert#'OTPCertificate'.tbsCertificate#'OTPTBSCertificate'.subject),
case Normal == Issuer of
true ->
OTPCert;
false ->
undefined
end;
(_E, Acc) ->
%% already found a match
Acc
end, undefined, Certs).
%% @doc Find distribution points for a given CRL and then attempt to
%% fetch the CRL from the first available.
fetch_point(#'DistributionPoint'{distributionPoint={fullName, Names}}) ->
Decoded = [{NameType,
pubkey_cert_records:transform(Name, decode)}
|| {NameType, Name} <- Names],
fetch(Decoded).
%% @doc Given a list of locations to retrieve a CRL from, attempt to
%% retrieve either from a file or http resource and bail as soon as
%% it can be found.
%%
%% Currently, only hand a armored PEM or DER encoded file, with
%% defaulting to DER.
%%
fetch([]) ->
not_available;
fetch([{uniformResourceIdentifier, "file://"++_File}|Rest]) ->
lager:debug("fetching CRLs from file URIs is not supported"),
fetch(Rest);
fetch([{uniformResourceIdentifier, "http"++_=URL}|Rest]) ->
lager:debug("getting CRL from ~p~n", [URL]),
_ = inets:start(),
case httpc:request(get, {URL, []}, [], [{body_format, binary}]) of
{ok, {_Status, _Headers, Body}} ->
case Body of
<<"-----BEGIN", _/binary>> ->
[{'CertificateList',
DER, _}=CertList] = public_key:pem_decode(Body),
{DER, public_key:pem_entry_decode(CertList)};
_ ->
%% assume DER encoded
CertList = public_key:pem_entry_decode(
{'CertificateList', Body, not_encrypted}),
{Body, CertList}
end;
{error, _Reason} ->
lager:debug("failed to get CRL ~p~n", [_Reason]),
fetch(Rest)
end;
fetch([Loc|Rest]) ->
%% unsupported CRL location
lager:debug("unable to fetch CRL from unsupported location ~p",
[Loc]),
fetch(Rest).