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
|
%%% Copyright (c) 2014-2016, NORDUnet A/S.
%%% See LICENSE for licensing information.
%%% @doc Certificate Transparency (RFC 6962)
-module(v1).
%% API (URL)
-export([request/4]).
-define(APPURL_CT_V1, "dt/v1").
check_valid_sth() ->
case plop:sth() of
noentry ->
lager:error("No valid STH found"),
exit({internalerror, "No valid STH found"});
{struct, PropList} ->
Now = plop:generate_timestamp(),
Timestamp = proplists:get_value(<<"timestamp">>, PropList),
MMD = application:get_env(catlfish, mmd, 86400) * 1000,
if
Now - Timestamp > MMD ->
lager:error("Old STH found, " ++
"now: ~p, STH timestamp: ~p, diff: ~p",
[Now, Timestamp, Now - Timestamp]),
exit({internalerror, "No valid STH found"});
true ->
ok
end
end.
%% Public functions, i.e. part of URL.
request(post, ?APPURL_CT_V1, "add-rr-chain", Input) ->
check_valid_sth(),
add_rr_chain(Input);
request(get, ?APPURL_CT_V1, "get-sth", _Query) ->
check_valid_sth(),
case plop:sth() of
noentry ->
lager:error("No valid STH found"),
internalerror("No valid STH found");
R ->
success(R)
end;
request(get, ?APPURL_CT_V1, "get-sth-consistency", Query) ->
check_valid_sth(),
case lists:sort(Query) of
[{"first", FirstInput}, {"second", SecondInput}] ->
{First, _} = string:to_integer(FirstInput),
{Second, _} = string:to_integer(SecondInput),
case lists:member(error, [First, Second]) of
true ->
err400("get-sth-consistency: bad input:",
[FirstInput, SecondInput]);
false ->
success(
{[{consistency,
[base64:encode(X) ||
X <- plop:consistency(First, Second)]}]})
end;
_ -> err400("get-sth-consistency: bad input:", Query)
end;
request(get, ?APPURL_CT_V1, "get-proof-by-hash", Query) ->
check_valid_sth(),
case lists:sort(Query) of
[{"hash", HashInput}, {"tree_size", TreeSizeInput}] ->
Hash = case (catch base64:decode(HashInput)) of
{'EXIT', _} -> error;
H -> H
end,
{TreeSize, _} = string:to_integer(TreeSizeInput),
case lists:member(error, [Hash, TreeSize]) of
true ->
err400("get-proof-by-hash: bad input:",
[HashInput, TreeSizeInput]);
false ->
case plop:inclusion(Hash, TreeSize) of
{ok, Index, Path} ->
success({[{leaf_index, Index},
{audit_path,
[base64:encode(X) || X <- Path]}]});
{notfound, Msg} ->
err400("get-proof-by-hash: hash not found", Msg)
end
end;
_ -> err400("get-proof-by-hash: bad input:", Query)
end;
request(get, ?APPURL_CT_V1, "get-entries", Query) ->
check_valid_sth(),
case lists:sort(Query) of
[{"end", EndInput}, {"start", StartInput}] ->
{Start, _} = string:to_integer(StartInput),
{End, _} = string:to_integer(EndInput),
case lists:member(error, [Start, End]) of
true -> err400("get-entries: bad input:", [Start, End]);
false -> success(
catlfish:entries(Start, min(End, Start + 999)))
end;
_ -> err400("get-entries: bad input:", Query)
end;
request(get, ?APPURL_CT_V1, "get-entry-and-proof", Query) ->
check_valid_sth(),
case lists:sort(Query) of
[{"leaf_index", IndexInput}, {"tree_size", TreeSizeInput}] ->
{Index, _} = string:to_integer(IndexInput),
{TreeSize, _} = string:to_integer(TreeSizeInput),
case lists:member(error, [Index, TreeSize]) of
true ->
err400("get-entry-and-proof: not integers: ",
[IndexInput, TreeSizeInput]);
false -> success(catlfish:entry_and_proof(Index, TreeSize))
end;
_ -> err400("get-entry-and-proof: bad input:", Query)
end;
request(get, ?APPURL_CT_V1, "get-roots", _Query) ->
check_valid_sth(),
R = [{certificates,
[base64:encode(Der) ||
Der <- catlfish:update_known_roots()]}],
success({R});
request(_Method, _App, _Fun, _) ->
none.
%% Private functions.
err400(Text, Input) ->
{400, [{"Content-Type", "text/html"}],
io_lib:format(
"<html><body><p>~n" ++
"~s~n" ++
"~p~n" ++
"</body></html>~n", [Text, Input])}.
success(Data) ->
{200, [{"Content-Type", "text/json"}], mochijson2:encode(Data)}.
internalerror(Text) ->
{500, [{"Content-Type", "text/html"}],
io_lib:format(
"<html><body><p>~n" ++
"~s~n" ++
"</body></html>~n", [Text])}.
-spec add_rr_chain(any()) -> any().
add_rr_chain(Input) ->
case (catch mochijson2:decode(Input)) of
{error, E} ->
err400("add-rr-chain: bad input:", E);
{struct, [{<<"chain">>, B64}]} ->
case (catch base64:decode(B64)) of
{'EXIT', _} ->
err400("add-rr-chain: invalid base64-encoding:", B64);
Data ->
case dnssecport:validate(Data) of
{ok, [DS | Chain]} ->
lager:debug("succesful DNSSEC validation"),
success(catlfish:add_chain(DS, Chain, normal));
{error, ErrorCode} ->
err400(io_lib:format(
"add-rr-chain: invalid DS record: ~p",
[ErrorCode]),
Data)
end
end;
_ ->
err400("add-rr-chain: missing input: chain", Input)
end.
|