2017-06-09 14:49:27 +02:00
|
|
|
-module (ejabberd_acme).
|
|
|
|
|
|
|
|
-export([ scenario/3
|
2017-06-09 17:53:54 +02:00
|
|
|
, scenario0/1
|
2017-06-09 14:49:27 +02:00
|
|
|
, directory/1
|
2017-06-12 14:31:48 +02:00
|
|
|
|
2017-06-09 14:49:27 +02:00
|
|
|
, get_account/3
|
|
|
|
, new_account/4
|
|
|
|
, update_account/4
|
2017-06-12 14:31:48 +02:00
|
|
|
, delete_account/3
|
|
|
|
% , key_roll_over/5
|
|
|
|
|
|
|
|
, new_authz/4
|
2017-06-09 14:49:27 +02:00
|
|
|
]).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-include("logger.hrl").
|
|
|
|
-include("xmpp.hrl").
|
|
|
|
-include_lib("public_key/include/public_key.hrl").
|
|
|
|
|
|
|
|
-define(REQUEST_TIMEOUT, 5000). % 5 seconds.
|
|
|
|
|
|
|
|
|
2017-06-09 17:53:54 +02:00
|
|
|
-type nonce() :: string().
|
|
|
|
-type url() :: string().
|
|
|
|
-type proplist() :: [{_, _}].
|
|
|
|
-type jws() :: map().
|
2017-06-09 14:49:27 +02:00
|
|
|
|
2017-06-09 17:53:54 +02:00
|
|
|
-spec directory(url()) ->
|
|
|
|
{ok, map(), nonce()} | {error, _}.
|
2017-06-09 18:47:50 +02:00
|
|
|
directory(Url) ->
|
2017-06-09 14:49:27 +02:00
|
|
|
Options = [],
|
|
|
|
HttpOptions = [{timeout, ?REQUEST_TIMEOUT}],
|
2017-06-09 18:47:50 +02:00
|
|
|
case httpc:request(get, {Url, []}, HttpOptions, Options) of
|
2017-06-09 14:49:27 +02:00
|
|
|
{ok, {{_, Code, _}, Head, Body}} when Code >= 200, Code =< 299 ->
|
2017-06-09 18:47:50 +02:00
|
|
|
case decode(Body) of
|
|
|
|
{error, Reason} ->
|
|
|
|
?ERROR_MSG("Problem decoding: ~s", [Body]),
|
|
|
|
{error, Reason};
|
|
|
|
Directories ->
|
|
|
|
StrDirectories = [{bitstring_to_list(X), bitstring_to_list(Y)} ||
|
|
|
|
{X,Y} <- Directories],
|
|
|
|
Nonce = get_nonce(Head),
|
|
|
|
%% Return Map of Directories
|
|
|
|
NewDirs = maps:from_list(StrDirectories),
|
|
|
|
{ok, NewDirs, Nonce}
|
|
|
|
end;
|
|
|
|
Error ->
|
|
|
|
failed_http_request(Error, Url)
|
2017-06-09 14:49:27 +02:00
|
|
|
end.
|
|
|
|
|
2017-06-12 14:31:48 +02:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
%%
|
|
|
|
%% Account Handling
|
|
|
|
%%
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2017-06-09 17:53:54 +02:00
|
|
|
-spec new_account(url(), jose_jwk:key(), proplist(), nonce()) ->
|
|
|
|
{ok, {url(), proplist()}, nonce()} | {error, _}.
|
2017-06-09 18:47:50 +02:00
|
|
|
new_account(Url, PrivateKey, Req, Nonce) ->
|
2017-06-09 14:49:27 +02:00
|
|
|
%% Make the request body
|
|
|
|
ReqBody = jiffy:encode({[{ <<"resource">>, <<"new-reg">>}] ++ Req}),
|
2017-06-09 17:53:54 +02:00
|
|
|
{_, SignedBody} = sign_json_jose(PrivateKey, ReqBody, Nonce),
|
2017-06-09 14:49:27 +02:00
|
|
|
%% Encode the Signed body with jiffy
|
|
|
|
FinalBody = jiffy:encode(SignedBody),
|
2017-06-12 14:31:48 +02:00
|
|
|
case make_post_request(Url, FinalBody) of
|
|
|
|
{ok, Head, Return} ->
|
|
|
|
TOSUrl = get_tos(Head),
|
|
|
|
NewNonce = get_nonce(Head),
|
|
|
|
{ok, {TOSUrl, Return}, NewNonce};
|
2017-06-09 18:47:50 +02:00
|
|
|
Error ->
|
2017-06-12 14:31:48 +02:00
|
|
|
Error
|
2017-06-09 14:49:27 +02:00
|
|
|
end.
|
|
|
|
|
2017-06-09 17:53:54 +02:00
|
|
|
-spec update_account(url(), jose_jwk:key(), proplist(), nonce()) ->
|
|
|
|
{ok, proplist(), nonce()} | {error, _}.
|
2017-06-09 18:47:50 +02:00
|
|
|
update_account(Url, PrivateKey, Req, Nonce) ->
|
2017-06-09 14:49:27 +02:00
|
|
|
%% Make the request body
|
|
|
|
ReqBody = jiffy:encode({[{ <<"resource">>, <<"reg">>}] ++ Req}),
|
2017-06-09 17:53:54 +02:00
|
|
|
{_, SignedBody} = sign_json_jose(PrivateKey, ReqBody, Nonce),
|
2017-06-09 14:49:27 +02:00
|
|
|
%% Encode the Signed body with jiffy
|
|
|
|
FinalBody = jiffy:encode(SignedBody),
|
2017-06-12 14:31:48 +02:00
|
|
|
case make_post_request(Url, FinalBody) of
|
|
|
|
{ok, Head, Return} ->
|
|
|
|
NewNonce = get_nonce(Head),
|
|
|
|
{ok, Return, NewNonce};
|
2017-06-09 18:47:50 +02:00
|
|
|
Error ->
|
2017-06-12 14:31:48 +02:00
|
|
|
Error
|
2017-06-09 14:49:27 +02:00
|
|
|
end.
|
|
|
|
|
2017-06-09 17:53:54 +02:00
|
|
|
-spec get_account(url(), jose_jwk:key(), nonce()) ->
|
|
|
|
{ok, {url(), proplist()}, nonce()} | {error, _}.
|
2017-06-09 18:47:50 +02:00
|
|
|
get_account(Url, PrivateKey, Nonce) ->
|
2017-06-09 14:49:27 +02:00
|
|
|
%% Make the request body
|
2017-06-09 17:53:54 +02:00
|
|
|
ReqBody = jiffy:encode({[{<<"resource">>, <<"reg">>}]}),
|
2017-06-09 14:49:27 +02:00
|
|
|
%% Jose Sign
|
2017-06-09 17:53:54 +02:00
|
|
|
{_, SignedBody} = sign_json_jose(PrivateKey, ReqBody, Nonce),
|
2017-06-09 14:49:27 +02:00
|
|
|
%% Encode the Signed body with jiffy
|
|
|
|
FinalBody = jiffy:encode(SignedBody),
|
2017-06-12 14:31:48 +02:00
|
|
|
case make_post_request(Url, FinalBody) of
|
|
|
|
{ok, Head, Return} ->
|
|
|
|
TOSUrl = get_tos(Head),
|
|
|
|
NewNonce = get_nonce(Head),
|
|
|
|
{ok, {TOSUrl, Return}, NewNonce};
|
2017-06-09 18:47:50 +02:00
|
|
|
Error ->
|
2017-06-12 14:31:48 +02:00
|
|
|
Error
|
2017-06-09 14:49:27 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2017-06-12 14:31:48 +02:00
|
|
|
-spec delete_account(url(), jose_jwk:key(), nonce()) ->
|
|
|
|
{ok, proplist(), nonce()} | {error, _}.
|
|
|
|
delete_account(Url, PrivateKey, Nonce) ->
|
|
|
|
%% Make the request body
|
|
|
|
ReqBody = jiffy:encode({
|
|
|
|
[ {<<"resource">>, <<"reg">>}
|
|
|
|
, {<<"status">>, <<"deactivated">>}
|
|
|
|
]}),
|
|
|
|
%% Jose Sign
|
|
|
|
{_, SignedBody} = sign_json_jose(PrivateKey, ReqBody, Nonce),
|
|
|
|
%% Encode the Signed body with jiffy
|
|
|
|
FinalBody = jiffy:encode(SignedBody),
|
|
|
|
case make_post_request(Url, FinalBody) of
|
|
|
|
{ok, Head, Return} ->
|
|
|
|
NewNonce = get_nonce(Head),
|
|
|
|
{ok, Return, NewNonce};
|
|
|
|
Error ->
|
|
|
|
Error
|
|
|
|
end.
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
%%
|
|
|
|
%% Authorization Handling
|
|
|
|
%%
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
-spec new_authz(url(), jose_jwk:key(), proplist(), nonce()) ->
|
|
|
|
{ok, proplist(), nonce()} | {error, _}.
|
|
|
|
new_authz(Url, PrivateKey, Req, Nonce) ->
|
|
|
|
%% Make the request body
|
|
|
|
ReqBody = jiffy:encode({
|
|
|
|
[ { <<"resource">>, <<"new-authz">>}] ++ Req}),
|
|
|
|
{_, SignedBody} = sign_json_jose(PrivateKey, ReqBody, Nonce),
|
|
|
|
%% Encode the Signed body with jiffy
|
|
|
|
FinalBody = jiffy:encode(SignedBody),
|
|
|
|
case make_post_request(Url, FinalBody) of
|
|
|
|
{ok, Head, Return} ->
|
|
|
|
NewNonce = get_nonce(Head),
|
|
|
|
{ok, Return, NewNonce};
|
|
|
|
Error ->
|
|
|
|
Error
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-09 14:49:27 +02:00
|
|
|
%%
|
|
|
|
%% Useful funs
|
|
|
|
%%
|
2017-06-09 17:53:54 +02:00
|
|
|
-spec get_nonce(proplist()) -> nonce() | 'none'.
|
2017-06-09 14:49:27 +02:00
|
|
|
get_nonce(Head) ->
|
2017-06-09 17:53:54 +02:00
|
|
|
case proplists:lookup("replay-nonce", Head) of
|
|
|
|
{"replay-nonce", Nonce} -> Nonce;
|
|
|
|
none -> none
|
|
|
|
end.
|
2017-06-09 14:49:27 +02:00
|
|
|
|
|
|
|
%% Very bad way to extract this
|
|
|
|
%% TODO: Find a better way
|
2017-06-09 17:53:54 +02:00
|
|
|
-spec get_tos(proplist()) -> url() | 'none'.
|
|
|
|
get_tos(Head) ->
|
2017-06-09 14:49:27 +02:00
|
|
|
try
|
2017-06-09 17:53:54 +02:00
|
|
|
[{_, Link}] = [{K, V} || {K, V} <- Head,
|
2017-06-09 14:49:27 +02:00
|
|
|
K =:= "link" andalso lists:suffix("\"terms-of-service\"", V)],
|
|
|
|
[Link1, _] = string:tokens(Link, ";"),
|
|
|
|
Link2 = string:strip(Link1, left, $<),
|
|
|
|
string:strip(Link2, right, $>)
|
|
|
|
catch
|
|
|
|
_:_ ->
|
2017-06-09 17:53:54 +02:00
|
|
|
none
|
2017-06-09 14:49:27 +02:00
|
|
|
end.
|
|
|
|
|
2017-06-12 14:31:48 +02:00
|
|
|
make_post_request(Url, ReqBody) ->
|
|
|
|
Options = [],
|
|
|
|
HttpOptions = [{timeout, ?REQUEST_TIMEOUT}],
|
|
|
|
case httpc:request(post,
|
|
|
|
{Url, [], "application/jose+json", ReqBody}, HttpOptions, Options) of
|
|
|
|
{ok, {{_, Code, _}, Head, Body}} when Code >= 200, Code =< 299 ->
|
|
|
|
case decode(Body) of
|
|
|
|
{error, Reason} ->
|
|
|
|
?ERROR_MSG("Problem decoding: ~s", [Body]),
|
|
|
|
{error, Reason};
|
|
|
|
Return ->
|
|
|
|
{ok, Head, Return}
|
|
|
|
end;
|
|
|
|
Error ->
|
|
|
|
failed_http_request(Error, Url)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec sign_json_jose(jose_jwk:key(), string()) -> jws().
|
|
|
|
sign_json_jose(Key, Json) ->
|
|
|
|
PubKey = jose_jwk:to_public(Key),
|
|
|
|
{_, BinaryPubKey} = jose_jwk:to_binary(PubKey),
|
|
|
|
PubKeyJson = jiffy:decode(BinaryPubKey),
|
|
|
|
% Jws object containing the algorithm
|
|
|
|
%% TODO: Dont hardcode the alg
|
|
|
|
JwsObj = jose_jws:from(
|
|
|
|
#{ <<"alg">> => <<"ES256">>
|
|
|
|
% , <<"b64">> => true
|
|
|
|
, <<"jwk">> => PubKeyJson
|
|
|
|
}),
|
|
|
|
%% Signed Message
|
|
|
|
jose_jws:sign(Key, Json, JwsObj).
|
|
|
|
|
2017-06-09 17:53:54 +02:00
|
|
|
-spec sign_json_jose(jose_jwk:key(), string(), nonce()) -> jws().
|
|
|
|
sign_json_jose(Key, Json, Nonce) ->
|
2017-06-09 14:49:27 +02:00
|
|
|
% Generate a public key
|
|
|
|
PubKey = jose_jwk:to_public(Key),
|
2017-06-12 14:31:48 +02:00
|
|
|
% ?INFO_MSG("Key: ~p", [Key]),
|
2017-06-09 14:49:27 +02:00
|
|
|
{_, BinaryPubKey} = jose_jwk:to_binary(PubKey),
|
2017-06-12 14:31:48 +02:00
|
|
|
% ?INFO_MSG("Key Record: ~p", [jose_jwk:to_map(Key)]),
|
2017-06-09 14:49:27 +02:00
|
|
|
PubKeyJson = jiffy:decode(BinaryPubKey),
|
|
|
|
% Jws object containing the algorithm
|
|
|
|
%% TODO: Dont hardcode the alg
|
|
|
|
JwsObj = jose_jws:from(
|
|
|
|
#{ <<"alg">> => <<"ES256">>
|
|
|
|
% , <<"b64">> => true
|
|
|
|
, <<"jwk">> => PubKeyJson
|
|
|
|
, <<"nonce">> => list_to_bitstring(Nonce)
|
|
|
|
}),
|
|
|
|
|
|
|
|
%% Signed Message
|
|
|
|
jose_jws:sign(Key, Json, JwsObj).
|
|
|
|
|
2017-06-09 18:47:50 +02:00
|
|
|
decode(Json) ->
|
|
|
|
try
|
|
|
|
{Result} = jiffy:decode(Json),
|
|
|
|
Result
|
|
|
|
catch
|
|
|
|
_:Reason ->
|
|
|
|
{error, Reason}
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec failed_http_request({ok, _} | {error, _}, url()) -> {error, _}.
|
|
|
|
failed_http_request({ok, {{_, Code, _}, _Head, Body}}, Url) ->
|
|
|
|
?ERROR_MSG("Got unexpected status code from <~s>: ~B, Body: ~s",
|
|
|
|
[Url, Code, Body]),
|
|
|
|
{error, unexpected_code};
|
|
|
|
failed_http_request({error, Reason}, Url) ->
|
|
|
|
?ERROR_MSG("Error making a request to <~s>: ~p",
|
|
|
|
[Url, Reason]),
|
|
|
|
{error, Reason}.
|
|
|
|
|
|
|
|
|
2017-06-09 14:49:27 +02:00
|
|
|
%%
|
|
|
|
%% Debugging Funcs -- They are only used for the development phase
|
|
|
|
%%
|
|
|
|
|
|
|
|
%% A typical acme workflow
|
|
|
|
scenario(CAUrl, AccId, PrivateKey) ->
|
2017-06-09 17:53:54 +02:00
|
|
|
DirURL = CAUrl ++ "/directory",
|
2017-06-09 14:49:27 +02:00
|
|
|
{ok, Dirs, Nonce0} = directory(DirURL),
|
|
|
|
|
2017-06-09 17:53:54 +02:00
|
|
|
AccURL = CAUrl ++ "/acme/reg/" ++ AccId,
|
2017-06-12 14:31:48 +02:00
|
|
|
{ok, {_TOS, Account}, Nonce1} = get_account(AccURL, PrivateKey, Nonce0),
|
|
|
|
|
|
|
|
#{"new-authz" := NewAuthz} = Dirs,
|
|
|
|
Req =
|
|
|
|
[ { <<"identifier">>, {
|
|
|
|
[ {<<"type">>, <<"dns">>}
|
|
|
|
, {<<"value">>, <<"my-acme-test.com">>}
|
|
|
|
] }}
|
|
|
|
, {<<"existing">>, <<"accept">>}
|
|
|
|
],
|
|
|
|
{ok, Authz, Nonce2} = new_authz(NewAuthz, PrivateKey, Req, Nonce1),
|
|
|
|
|
|
|
|
{Account, Authz, PrivateKey}.
|
|
|
|
|
2017-06-09 14:49:27 +02:00
|
|
|
|
|
|
|
new_user_scenario(CAUrl) ->
|
|
|
|
PrivateKey = generate_key(),
|
|
|
|
|
2017-06-09 17:53:54 +02:00
|
|
|
DirURL = CAUrl ++ "/directory",
|
2017-06-09 14:49:27 +02:00
|
|
|
{ok, Dirs, Nonce0} = directory(DirURL),
|
2017-06-12 14:31:48 +02:00
|
|
|
?INFO_MSG("Directories: ~p", [Dirs]),
|
2017-06-09 14:49:27 +02:00
|
|
|
|
|
|
|
#{"new-reg" := NewAccURL} = Dirs,
|
|
|
|
Req0 = [{ <<"contact">>, [<<"mailto:cert-example-admin@example2.com">>]}],
|
|
|
|
{ok, {TOS, Account}, Nonce1} = new_account(NewAccURL, PrivateKey, Req0, Nonce0),
|
|
|
|
|
|
|
|
{_, AccId} = proplists:lookup(<<"id">>, Account),
|
2017-06-09 17:53:54 +02:00
|
|
|
AccURL = CAUrl ++ "/acme/reg/" ++ integer_to_list(AccId),
|
2017-06-12 14:31:48 +02:00
|
|
|
{ok, {_TOS, Account1}, Nonce2} = get_account(AccURL, PrivateKey, Nonce1),
|
|
|
|
?INFO_MSG("Old account: ~p~n", [Account1]),
|
|
|
|
|
2017-06-09 14:49:27 +02:00
|
|
|
Req1 = [{ <<"agreement">>, list_to_bitstring(TOS)}],
|
2017-06-12 14:31:48 +02:00
|
|
|
{ok, Account2, Nonce3} = update_account(AccURL, PrivateKey, Req1, Nonce2),
|
|
|
|
|
|
|
|
%%
|
|
|
|
%% Delete account
|
|
|
|
%%
|
|
|
|
|
|
|
|
{ok, Account3, Nonce4} = delete_account(AccURL, PrivateKey, Nonce3),
|
|
|
|
{ok, {_TOS, Account4}, Nonce5} = get_account(AccURL, PrivateKey, Nonce4),
|
|
|
|
?INFO_MSG("New account: ~p~n", [Account4]),
|
2017-06-09 14:49:27 +02:00
|
|
|
|
2017-06-12 14:31:48 +02:00
|
|
|
% NewKey = generate_key(),
|
|
|
|
% KeyChangeUrl = CAUrl ++ "/acme/key-change/",
|
|
|
|
% {ok, Account3, Nonce4} = key_roll_over(KeyChangeUrl, AccURL, PrivateKey, NewKey, Nonce3),
|
|
|
|
% ?INFO_MSG("Changed key: ~p~n", [Account3]),
|
|
|
|
|
|
|
|
% {ok, {_TOS, Account4}, Nonce5} = get_account(AccURL, NewKey, Nonce4),
|
|
|
|
% ?INFO_MSG("New account:~p~n", [Account4]),
|
|
|
|
|
|
|
|
{Account4, PrivateKey}.
|
2017-06-09 14:49:27 +02:00
|
|
|
|
|
|
|
generate_key() ->
|
|
|
|
jose_jwk:generate_key({ec, secp256r1}).
|
|
|
|
|
|
|
|
%% Just a test
|
2017-06-09 17:53:54 +02:00
|
|
|
scenario0(KeyFile) ->
|
|
|
|
PrivateKey = jose_jwk:from_file(KeyFile),
|
2017-06-12 14:31:48 +02:00
|
|
|
% scenario("http://localhost:4000", "2", PrivateKey).
|
|
|
|
new_user_scenario("http://localhost:4000").
|
|
|
|
|
|
|
|
% ejabberd_acme:scenario0("/home/konstantinos/Desktop/Programming/ejabberd/private_key_temporary").
|