summaryrefslogtreecommitdiff
path: root/components/authorize/src/author.erl
blob: c158e4488928dd7b8dbdc40acefef08cd0cf0f04 (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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
%% -*- mode: erlang; indent-tabs-mode: nil; -*-
-module(author).

-define(verbose(Fmt, Args), case verbose() of true ->
				    io:fwrite(Fmt, Args);
				false ->
				    ok
			    end).

-export([main/1]).

-import(proplists, [get_value/2, get_value/3]).

main([]) ->
    help(),
    halt(1);
main(Args) ->
    Opts = opts(Args),
    check_verbose(Opts),
    case lists:keyfind(command, 1, Opts) of
        {_, Cmd} ->
            ?verbose("Cmd = ~p; Options = ~p~n",
                     [Cmd, lists:keydelete(command, 1, Opts)]),
            cmd(Cmd, Opts);
        false ->
            fail("No command given~n", [])
    end.

opts(["-v"   , "true"   |T]) -> [{v, true}|opts(T)];
opts(["-v"   , "false"  |T]) -> [{v, false}|opts(T)];
opts(["-v"              |T]) -> [{v, true}|opts(T)];
opts(["-pub" , PubKey   |T]) -> [{pub, PubKey}|opts(T)];
opts(["-priv", PrivKey  |T]) -> [{priv, PrivKey}|opts(T)];
opts(["-root", RootKey  |T]) -> [{root, RootKey}|opts(T)];
opts(["-sig", SigFile   |T]) -> [{sig, SigFile}|opts(T)];
opts(["-o"   , OutF     |T]) -> [{o, OutF}|opts(T)];
opts(["-c"   , Cert     |T]) -> [{cert, Cert}|opts(T)];
opts(["-b"   , Bits     |T]) -> [{b, l2i(Bits)}|opts(T)];
opts(["-fmt" , Fmt      |T]) -> [{fmt, Fmt}|opts(T)];
opts(["-decode", "true" |T]) -> [{decode,true}|opts(T)];
opts(["-decode", "false"|T]) -> [{decode,false}|opts(T)];
opts([Cmd]) ->
    [{command, Cmd}];
opts([]) ->
    [].

check_verbose(Opts) ->
    V = get_value(v, Opts, false),
    put({?MODULE, verbose}, V),
    V.

verbose() ->
    get({?MODULE, verbose}).

cmd("make_auth", Opts) ->
    case {get_value(root, Opts), get_value(pub, Opts), jwt_fmt(Opts)} of
	{undefined, _, "jwt"} ->
	    fail("Cannot create JWT without root key~n", []);
	{_Root, undefined, "jwt"} ->
	    fail("Cannot create JWT without pub key~n", []);
	{Root, Pub, Fmt} ->
            {RPriv, _} = get_key_pair(Root),
            PubKey = get_pub_key(Pub),
            make_auth(RPriv, PubKey, Fmt, Opts)
    end;
cmd("make_root", Opts) ->
    [Out] = mandatory([o], Opts),
    Bits = bits(Opts),
    make_root(Out, Bits, Opts);
cmd("make_dev", Opts) ->
    [Root, Out] = mandatory([root, o], Opts),
    Bits = bits(Opts),
    make_dev(Root, Out, Bits, Opts);
cmd("read_sig", Opts) ->
    [Root, Sig] = mandatory([root, sig], Opts),
    {_, Pub} = get_key_pair(Root),
    case file:read_file(Sig) of
        {ok, JWT} ->
            case authorize_sig:decode_jwt(strip_nl(JWT), Pub) of
                invalid ->
                    fail("Cannot validate ~s~n", [Sig]);
                {Header, Payload} ->
                    io:fwrite("Header: ~s~n"
                              "Payload: ~s~n",
                              [exo_json:encode(Header),
                               exo_json:encode(Payload)]),
                    case proplists:get_value(decode, Opts, false) of
                        true ->
                            decode_keys(Payload);
                        false ->
                            ok
                    end
            end;
        {error, E} ->
            fail("Cannot read ~s (~w)~n", [Sig, E])
    end;
cmd("read_key", Opts) ->
    case [{K, get_value(K, Opts)} || K <- [root, pub, priv]] of
        [] ->
            fail("No key given~n", []);
        Keys ->
            lists:foreach(
              fun({K, F}) ->
                      case authorize_keys:get_key_pair_from_pem(
                             openssl, F) of
                          {undefined, undefined} ->
                              case authorize_keys:get_pub_key(F) of
                                  undefined ->
                                      io:fwrite("~p: Cannot read~n", [K]);
                                  PubKey ->
                                      io:fwrite("~p: ~p~n", [K, PubKey])
                              end;
                          {Priv, Pub} ->
                              io:fwrite("~p priv: ~p~n"
                                        "    pub: ~p~n", [K, Priv, Pub])
                      end
              end, [{K,F} || {K,F} <- Keys, F =/= undefined])
    end.


make_root_msg(X) ->
    {"~s_priv.pem - private root key~n"
     "~s_pub.pem  - public root key~n"
     "~n"
     "Use ./author -root ~s_priv.pem [-b <bits>] -o <device_key_file>"
     " make_dev~n"
     "to create a device key pair signed by the generated private root key.~n"
     "~n"
     "Use ./rvi_create_certificate.py ... --root_key=~s_priv.pem ...~n"
     "to sign a created certificate with the generated private root key.~n",
     [X, X, X, X]}.

get_key_pair(Root) ->
    case authorize_keys:get_key_pair_from_pem(openssl, Root) of
        {undefined, undefined} ->
            fail("Cannot read root key (~p)~n", [Root]);
        {_RPriv, _RPub} = Pair ->
            Pair
    end.

get_pub_key(Pub) ->
    case authorize_keys:get_pub_key(Pub) of
        undefined ->
            fail("Cannot read pub key (~p)~n", [Pub]);
        PubKey ->
            PubKey
    end.

decode_keys({struct, Elems}) ->
    case lists:keyfind("keys", 1, Elems) of
        {_, {array, Keys}} ->
            lists:foreach(
              fun(K) ->
                      io:fwrite(
                        "~p~n", [authorize_keys:json_to_public_key(K)])
              end, Keys);
        _ ->
            ok
    end.

mandatory(Keys, Opts) ->
    lists:map(
      fun(K) ->
              case lists:keyfind(K, 1, Opts) of
                  false ->
                      fail("Mandatory option: -~w~n", [K]);
                  {_, V} ->
                      V
              end
      end, Keys).

bits(Opts) ->
    get_value(b, Opts, 2048).

l2i(Str) ->
    try list_to_integer(Str)
    catch
        error:_ ->
            fail("Invalid number argument, ~s~n", [Str])
    end.

i2l(I) ->
    integer_to_list(I).

strip_nl(Bin) ->
    case re:split(Bin,"\\s+$",[{return,binary},trim]) of
	[Trimmed] -> Trimmed;
	_ -> Bin
    end.

make_auth(RPriv, Pub, Fmt, Opts) ->
    case Fmt of
	"json" ->
	    JSON = authorize_sig:ensure_json(
		     authorize_keys:public_key_to_json(Pub)),
	    out(JSON, Opts);
	"jwt" ->
	    JWT = authorize_keys:signed_public_key(Pub, RPriv),
	    out(JWT, Opts)
    end.

make_root(Out, Bits, _Opts) ->
    make_key_pair(Out, Bits),
    {Fmt, Args} = make_root_msg(Out),
    io:fwrite(Fmt, Args).

make_dev(Root, Out, Bits, Opts) ->
    make_key_pair(Out, Bits),
    {RPriv, _} = get_key_pair(Root),
    Pub = get_pub_key(pub_f(Out)),
    make_auth(RPriv, Pub, "jwt", [{o, Out ++ "_pub_sign.jwt"}|Opts]).

make_key_pair(Out, Bits) ->
    os:cmd(["openssl genrsa -out ", priv_f(Out), " ", i2l(Bits)]),
    os:cmd(["openssl rsa -pubout -in ", priv_f(Out), " -out ", pub_f(Out)]).

priv_f(Out) -> Out ++ "_priv.pem".
pub_f (Out) -> Out ++ "_pub.pem".

out(Str, Opts) ->
    case get_value(o, Opts, tty) of
	tty ->
	    io:fwrite("~s", [Str]);
	OutF when is_list(OutF) ->
	    case file:open(OutF, [write]) of
		{ok, Fd} ->
		    try io:fwrite(Fd, "~s", [Str])
		    after
			file:close(Fd)
		    end;
		Error ->
		    fail("Cannot write output (~p): ~p~n", [OutF, Error])
	    end
    end.

jwt_fmt(Opts) ->
    case get_value(fmt, Opts) of
        undefined ->
            case get_value(o, Opts) of
                tty ->
                    "json";
                [_|_] ->
                    "jwt"
            end;
        Fmt when Fmt=="json"; Fmt=="jwt" ->
            Fmt;
        Other ->
            fail("Unknown format: ~s~n", [Other])
    end.

help() ->
    io:fwrite(
      "Usage: " ++ escript:script_name() ++ "[Options] Cmd~n"
      "Options:~n"
      "  -pem PemFile   : Name of OpenSSL (2048 bit RSA) pem key file~n"
      "  -c CertFile    : Name of JSON-formatted RVI Certificate~n"
      "  -fmt Format    : Output format (json|jwt)~n"
      "  -o OutFile     : Name of output (tty output, if not specified)~n"
      "Command:~n"
      "  make_auth      : Create an authorization JWT~n"
      "   Options: -root <RootKeyFile> -pub <PubKey> [-o <Outfile>]~n"
      "  make_root      : Create a provisioning key pair~n"
      "   Options: [-b <Bits>] -o <Outfile>~n"
      "  read_sig       : Validate and read a JWT~n"
      "   Options: -sig <SignatureFile> -root <RootKey>~n", []).

fail(Fmt, Args) ->
    io:fwrite(Fmt, Args),
    help(),
    erlang:halt(1).