summaryrefslogtreecommitdiff
path: root/src/v1.erl
blob: c7624db5540c596623c2bd17c6d02aac1b2e2da5 (plain)
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
-module(v1).
-export([add_chain/3, add_pre_chain/3, get_sth/3,
 get_sth_consistency/3, get_proof_by_hash/3, get_entries/3,
 get_roots/3, get_entry_and_proof/3]).
-export([hello/3]).
-include("/home/linus/usr/src/ct/plop/include/plop.hrl").
-define(PROTOCOL_VERSION, 1).

%% Public functions.
add_chain(SessionID, _Env, Input) ->
    Res = case (catch jiffy:decode(Input)) of
              {error, E} -> html("add-chain: bad input; see RFC 6962", 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, []},
                       {signature, base64:encode(SPT#spt.signature)}],
                  jiffy:encode({R});
              _ -> html("add-chain: missing input: chain; see RFC 6962", Input)
          end,
    deliver(SessionID, Res).

add_pre_chain(SessionID, _Env, _Input) ->
    niy(SessionID).

get_sth(SessionID, _Env, _Input) ->
    #sth{
       treesize = Treesize,
       timestamp = Timestamp,
       roothash = Roothash,
       signature = Signature} = plop:sth(),
    R = [{tree_size, Treesize},
         {timestamp, Timestamp},
         {sha256_root_hash, base64:encode(Roothash)},
         {tree_head_signature, base64:encode(Signature)}],
    deliver(SessionID, jiffy:encode({R})).

get_sth_consistency(SessionID, _Env, _Input) ->
    niy(SessionID).
get_proof_by_hash(SessionID, _Env, _Input) ->
    niy(SessionID).
get_entries(SessionID, _Env, _Input) ->
    niy(SessionID).
get_entry_and_proof(SessionID, _Env, _Input) ->
    niy(SessionID).

get_roots(SessionID, _Env, _Input) ->
    R = [{certificates, []}],                   % NIY.
    deliver(SessionID, jiffy:encode({R})).

%% For testing. FIXME: Remove.
hello(SessionID, Env, Input) ->
    Query = httpd:parse_query(Input),
    mod_esi:deliver(SessionID, io_lib:format(
                                 "Content-Type: text/html\r\n\r\n" ++
                                     "<html><body>hello again, erlang world" ++
                                     "<p>SessionID: ~p~n" ++
                                     "<p>Env: ~p~n" ++
                                     "<p>Input, raw: ~p~n" ++
                                     "<p>Input, parsed: ~p~n" ++
                                     "</body></html>", [SessionID, Env, Input, Query])).

%% Private functions.
html(Text, Input) ->
    io_lib:format(
      "Content-Type: text/html\r\n\r\n" ++
          "<html><body><p>~n" ++
          "~s~n" ++
          "~p~n" ++
          "</body></html>~n", [Text, Input]).

niy(S) ->
    mod_esi:deliver(S, html("NIY - Not Yet Implemented|", [])).

deliver(Session, Data) ->
    mod_esi:deliver(Session, binary_to_list(Data)).