add realm
This commit is contained in:
parent
28598cccf0
commit
4c14806f36
@ -12,7 +12,7 @@
|
||||
-copyright("Craig Everett <zxq9@zxq9.com>").
|
||||
-license("GPL-3.0").
|
||||
|
||||
-export([ensure_keypair/1, have_key/2, keypath/2,
|
||||
-export([ensure_keypair/1, have_key/2, path/2,
|
||||
prompt_keygen/0, generate_rsa/1,
|
||||
load/2, verify/3]).
|
||||
|
||||
@ -51,19 +51,19 @@ ensure_keypair(KeyID = {Realm, KeyName}) ->
|
||||
%% Determine whether the indicated key is present.
|
||||
|
||||
have_key(Type, KeyID) ->
|
||||
filelib:is_regular(keypath(Type, KeyID)).
|
||||
filelib:is_regular(path(Type, KeyID)).
|
||||
|
||||
|
||||
-spec keypath(Type, KeyID) -> Path
|
||||
-spec path(Type, KeyID) -> Path
|
||||
when Type :: public | private,
|
||||
KeyID :: zx:key_id(),
|
||||
Path :: file:filename().
|
||||
%% @private
|
||||
%% Given KeyID, return the path to the key type indicated.
|
||||
|
||||
keypath(public, {Realm, KeyName}) ->
|
||||
path(public, {Realm, KeyName}) ->
|
||||
filename:join(zx_lib:path(key, Realm), KeyName ++ ".pub.der");
|
||||
keypath(private, {Realm, KeyName}) ->
|
||||
path(private, {Realm, KeyName}) ->
|
||||
filename:join(zx_lib:path(key, Realm), KeyName ++ ".key.der").
|
||||
|
||||
|
||||
@ -116,8 +116,8 @@ prompt_keygen() ->
|
||||
|
||||
generate_rsa(KeyID = {Realm, KeyName}) ->
|
||||
PemFile = filename:join(zx_lib:path(key, Realm), KeyName ++ ".pub.pem"),
|
||||
KeyFile = keypath(private, KeyID),
|
||||
PubFile = keypath(public, KeyID),
|
||||
KeyFile = path(private, KeyID),
|
||||
PubFile = path(public, KeyID),
|
||||
ok = lists:foreach(fun zx_lib:halt_if_exists/1, [PemFile, KeyFile, PubFile]),
|
||||
ok = log(info, "Generating ~p and ~p. Please be patient...", [KeyFile, PubFile]),
|
||||
ok = gen_p_key(KeyFile),
|
||||
@ -234,7 +234,7 @@ load(Type, KeyID) ->
|
||||
private -> 'RSAPrivateKey';
|
||||
public -> 'RSAPublicKey'
|
||||
end,
|
||||
Path = keypath(Type, KeyID),
|
||||
Path = path(Type, KeyID),
|
||||
ok = log(info, "Loading key from file ~ts", [Path]),
|
||||
case file:read_file(Path) of
|
||||
{ok, Bin} -> {ok, public_key:der_decode(DerType, Bin)};
|
||||
|
||||
@ -786,7 +786,8 @@ rm(Path) ->
|
||||
|
||||
b_to_t(Binary) ->
|
||||
try
|
||||
binary_to_term(Binary)
|
||||
Term = binary_to_term(Binary),
|
||||
{ok, Term}
|
||||
catch
|
||||
error:badarg -> error
|
||||
end.
|
||||
|
||||
@ -464,7 +464,7 @@ add_realm(Path) ->
|
||||
{ok, Data} ->
|
||||
Digest = crypto:hash(sha512, Data),
|
||||
Text = integer_to_list(binary:decode_unsigned(Digest, big), 16),
|
||||
ok = log(info, "SHA512 of ~ts: ~ts", [Path, Text]),
|
||||
ok = log(info, "SHA-512 of ~ts: ~ts", [Path, Text]),
|
||||
add_realm2(Data);
|
||||
{error, enoent} ->
|
||||
{error, "Realm bundle (.zrf) does not exist.", 2};
|
||||
@ -482,9 +482,9 @@ add_realm2(Data) ->
|
||||
Realm = maps:get(realm, RealmConf),
|
||||
ok = make_realm_dirs(Realm),
|
||||
ConfPath = zx_lib:realm_conf(Realm),
|
||||
zx_lib:write_terms(ConfPath, maps:to_list(RealmConf)),
|
||||
ok = zx_lib:write_terms(ConfPath, maps:to_list(RealmConf)),
|
||||
KeyName = maps:get(key, RealmConf),
|
||||
KeyPath = zx_lib:keypath(public, {Realm, KeyName}),
|
||||
KeyPath = zx_key:path(public, {Realm, KeyName}),
|
||||
ok = file:write_file(KeyPath, KeyDER),
|
||||
log(info, "Added realm ~tp.", [Realm]);
|
||||
error ->
|
||||
@ -1050,7 +1050,7 @@ create_realmfile(Realm, Dir) ->
|
||||
{ok, RealmConf} = zx_lib:load_realm_conf(Realm),
|
||||
ok = log(info, "Realm found, creating realm file..."),
|
||||
KeyName = maps:get(key, RealmConf),
|
||||
PubKeyPath = zx_key:keypath(public, {Realm, KeyName}),
|
||||
PubKeyPath = zx_key:path(public, {Realm, KeyName}),
|
||||
{ok, PubDER} = file:read_file(PubKeyPath),
|
||||
Blob = term_to_binary({RealmConf, PubDER}),
|
||||
ZRF = filename:join(Dir, Realm ++ ".zrf"),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user