WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 4590293

Browse files
committed
Add allow_expired_certs option
1 parent 71410b1 commit 4590293

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ Defaults to `grisp_keychain_filesystem`.
4343
{tls_verify, verify_peer}
4444
```
4545

46+
#### `allow_expired_certs`
47+
**Type:** `boolean()`
48+
**Default:** `false`
49+
**Description:** Allow expired or future certificates.
50+
51+
```erlang
52+
{allow_expired_certs, true}
53+
```
54+
4655
### Certificate and Key Paths
4756

4857
#### `client_certs`
@@ -174,7 +183,8 @@ DerCert = grisp_keychain:read_cert(primary, der).
174183
{client_certs, {priv, my_app, "certs/client.pem"}},
175184
{client_key, {priv, my_app, "keys/client-key.pem"}},
176185
{tls_client_trusted_certs, {priv, my_app, "certs/ca"}},
177-
{tls_server_trusted_certs, {priv, my_app, "certs/servers"}}
186+
{tls_server_trusted_certs, {priv, my_app, "certs/servers"}},
187+
{allow_expired_certs, false}
178188
]}
179189
```
180190

src/grisp_keychain.app.src

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
stdlib
99
]},
1010
{env, [
11-
{api_module, grisp_keychain_filesystem}
11+
{api_module, grisp_keychain_filesystem},
12+
{allow_expired_certs, false}
1213
]},
1314
{modules, []},
1415
{licenses, ["Apache-2.0"]},

src/grisp_keychain.erl

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ A property list of SSL/TLS client options compatible with `ssl:connect/3`.
3939
[ssl:tls_client_option()].
4040

4141
tls_options(Domain) ->
42-
delegate_call(?FUNCTION_NAME, [Domain]).
42+
TlsOpts0 = delegate_call(?FUNCTION_NAME, [Domain]),
43+
case application:get_env(grisp_keychain, allow_expired_certs) of
44+
{ok, true} -> wrap_verify_option(TlsOpts0, fun allow_expired_certs/3);
45+
_ -> TlsOpts0
46+
end.
4347

4448
-doc """
4549
Read a certificate in DER format.
@@ -73,3 +77,38 @@ read_cert(primary, der) ->
7377
delegate_call(Function, Args) ->
7478
{ok, Module} = application:get_env(grisp_keychain, api_module),
7579
erlang:apply(Module, Function, Args).
80+
81+
%--- TLS verify chaining -------------------------------------------------------
82+
83+
-doc false.
84+
wrap_verify_option(Opts0, Fun) when is_function(Fun, 3) ->
85+
Existing =
86+
case lists:keyfind(verify_fun, 1, Opts0) of
87+
{verify_fun, {Fun, State}} when is_function(Fun, 3) ->
88+
{Fun, State};
89+
{verify_fun, Fun} when is_function(Fun, 3) ->
90+
{Fun, undefined};
91+
_ ->
92+
undefined
93+
end,
94+
Opts1 = lists:keydelete(verify_fun, 1, Opts0),
95+
[{verify_fun, {Fun, Existing}} | Opts1].
96+
97+
-doc false.
98+
allow_expired_certs(_Cert, {bad_cert, cert_expired}, User) ->
99+
{valid, User};
100+
allow_expired_certs(Cert, Event, {UserFun, UserState}) ->
101+
case UserFun(Cert, Event, UserState) of
102+
{valid, NewState} -> {valid, {UserFun, NewState}};
103+
{valid_peer, NewState} -> {valid_peer, {UserFun, NewState}};
104+
{unknown, NewState} -> {unknown, {UserFun, NewState}};
105+
{fail, Reason} -> {fail, Reason}
106+
end;
107+
allow_expired_certs(_Cert, valid, State) ->
108+
{valid, State};
109+
allow_expired_certs(_Cert, valid_peer, State) ->
110+
{valid_peer, State};
111+
allow_expired_certs(_Cert, {extension, _}, State) ->
112+
{unknown, State};
113+
allow_expired_certs(_Cert, {bad_cert, Reason}, _State) ->
114+
{fail, Reason}.

0 commit comments

Comments
 (0)