summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/v1.erl71
1 files changed, 43 insertions, 28 deletions
diff --git a/src/v1.erl b/src/v1.erl
index d6b833b..5f0afc6 100644
--- a/src/v1.erl
+++ b/src/v1.erl
@@ -16,22 +16,31 @@
%% Public functions, i.e. part of URL.
'add-chain'(SessionID, _Env, Input) ->
- Res = case (catch jiffy:decode(Input)) of
- {error, E} -> html("add-chain: bad input:", E);
- {[{<<"chain">>, Chain}]} ->
- Entry = #plop_entry{type = x509,
- data = list_to_binary(Chain)},
- SPT = plop:add(#timestamped_entry{entry = Entry}),
- R = [{sct_version, ?PROTOCOL_VERSION},
- {id, base64:encode(SPT#spt.logid)},
- {timestamp, SPT#spt.timestamp},
- {extensions, base64:encode("")},
- {signature, base64:encode(
- plop:serialise(SPT#spt.signature))}],
- binary_to_list(jiffy:encode({R}));
- _ -> html("add-chain: missing input: chain", Input)
- end,
- deliver(SessionID, Res).
+ R = case (catch jiffy:decode(Input)) of
+ {error, E} ->
+ html("add-chain: bad input:", E);
+ {[{<<"chain">>, ChainBase64}]} ->
+ case (catch [base64:decode(X) || X <- ChainBase64]) of
+ {'EXIT', _} ->
+ html("add-chain: invalid base64-encoded chain: ",
+ [ChainBase64]);
+ [LeafCert | CertChain] ->
+ Entry = #plop_entry{type = x509, data = LeafCert},
+ SPT = plop:add(#timestamped_entry{entry = Entry},
+ list_to_binary(CertChain)),
+ R2 = [{sct_version, ?PROTOCOL_VERSION},
+ {id, base64:encode(SPT#spt.logid)},
+ {timestamp, SPT#spt.timestamp},
+ {extensions, base64:encode("")},
+ {signature, base64:encode(
+ plop:serialise(SPT#spt.signature))}],
+ binary_to_list(jiffy:encode({R2}));
+ Invalid ->
+ html("add-chain: chain is not a list: ", [Invalid])
+ end;
+ _ -> html("add-chain: missing input: chain", Input)
+ end,
+ deliver(SessionID, R).
'add-pre-chain'(SessionID, _Env, _Input) ->
niy(SessionID).
@@ -120,15 +129,17 @@
{Index, _} = string:to_integer(IndexInput),
{TreeSize, _} = string:to_integer(TreeSizeInput),
case lists:member(error, [Index, TreeSize]) of
- true -> html("get-entry-and-proof: bad input:", [fixme]);
+ true ->
+ html("get-entry-and-proof: not integers: ",
+ [IndexInput, TreeSizeInput]);
false ->
binary_to_list(
jiffy:encode(
- case plop:inclusion_and_leaf(Index, TreeSize) of
- {ok, Data, Path} ->
+ case plop:inclusion_and_more(Index, TreeSize) of
+ {ok, Leaf, Chain, Path} ->
{[{leaf_input,
- base64:encode(plop:serialise(Data))},
- {extra_data, base64:encode([])},
+ base64:encode(plop:serialise(Leaf))},
+ {extra_data, base64:encode(Chain)},
{audit_path,
[base64:encode(X) || X <- Path]}]};
{notfound, Msg} ->
@@ -158,14 +169,18 @@ hello(SessionID, Env, Input) ->
[SessionID, Env, Input, Query])).
%% Private functions.
+-spec encode_entries([{mtl(), binary()}]) -> list().
encode_entries(Entries) ->
- binary_to_list(jiffy:encode({[{entries, encode_entries2(Entries)}]})).
-encode_entries2([H|T]) ->
- LeafInput = base64:encode(plop:serialise(H)),
- ExtraData = base64:encode(""),
- [{[{leaf_input, LeafInput}, {extra_data, ExtraData}]} | encode_entries2(T)];
-encode_entries2([]) ->
- [].
+ binary_to_list(jiffy:encode({[{entries, unpack_entries(Entries)}]})).
+
+-spec unpack_entries([{mtl(), binary()}]) -> list().
+unpack_entries([]) ->
+ [];
+unpack_entries([H|T]) ->
+ {MTL, Extra} = H,
+ LeafInput = base64:encode(plop:serialise(MTL)),
+ ExtraData = base64:encode(Extra),
+ [{[{leaf_input, LeafInput}, {extra_data, ExtraData}]} | unpack_entries(T)].
html(Text, Input) ->
io_lib:format(