Use public_key instead of os:cmd/1 to openssl for key generation.

This commit is contained in:
Craig Everett 2020-06-17 07:23:19 +09:00
parent 6f43712a40
commit 65824583ab
49 changed files with 34 additions and 128 deletions

View File

@ -1 +1 @@
0.10.8 0.10.9

View File

@ -1,6 +1,6 @@
{application,zx, {application,zx,
[{description,"An Erlang development tool and Zomp user client"}, [{description,"An Erlang development tool and Zomp user client"},
{vsn,"0.10.8"}, {vsn,"0.10.9"},
{applications,[stdlib,kernel]}, {applications,[stdlib,kernel]},
{modules,[zx,zx_auth,zx_conn,zx_conn_sup,zx_daemon,zx_key, {modules,[zx,zx_auth,zx_conn,zx_conn_sup,zx_daemon,zx_key,
zx_lib,zx_local,zx_net,zx_peer,zx_peer_man, zx_lib,zx_local,zx_net,zx_peer,zx_peer_man,

View File

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

@ -24,7 +24,7 @@
%%% @end %%% @end
-module(zx). -module(zx).
-vsn("0.10.8"). -vsn("0.10.9").
-behavior(application). -behavior(application).
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -9,7 +9,7 @@
%%% @end %%% @end
-module(zx_auth). -module(zx_auth).
-vsn("0.10.8"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -7,7 +7,7 @@
%%% @end %%% @end
-module(zx_conn). -module(zx_conn).
-vsn("0.10.8"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -5,7 +5,7 @@
%%% @end %%% @end
-module(zx_conn_sup). -module(zx_conn_sup).
-vsn("0.10.8"). -vsn("0.10.9").
-behavior(supervisor). -behavior(supervisor).
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -138,7 +138,7 @@
%%% @end %%% @end
-module(zx_daemon). -module(zx_daemon).
-vsn("0.10.8"). -vsn("0.10.9").
-behavior(gen_server). -behavior(gen_server).
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -8,7 +8,7 @@
%%% @end %%% @end
-module(zx_key). -module(zx_key).
-vsn("0.10.8"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").
@ -20,6 +20,7 @@
sign/2, verify/3]). sign/2, verify/3]).
-include("zx_logger.hrl"). -include("zx_logger.hrl").
-include_lib("public_key/include/OTP-PUB-KEY.hrl").
-spec generate_rsa(Owner) -> Result -spec generate_rsa(Owner) -> Result
@ -32,125 +33,30 @@
%% NOTE: The current version of this command is likely to only work on a unix system. %% NOTE: The current version of this command is likely to only work on a unix system.
generate_rsa(Owner) -> generate_rsa(Owner) ->
{ok, TmpDir} = zx_lib:mktemp_dir({"otpr", "zx"}), ok = tell("Generating keys. This can take several minutes. Please be patient..."),
PemFile = filename:join(TmpDir, "zx-tmp.pem"), Key =
PubFile = filename:join(TmpDir, "zx-tmp.pub.der"), #'RSAPrivateKey'{modulus = Mod,
KeyFile = filename:join(TmpDir, "zx-tmp.key.der"), publicExponent = PE} =
ok = tell("Generating keys. Please be patient..."), public_key:generate_key({rsa, 16384, 65537}),
case gen_p_key(KeyFile) of Pub =
ok -> #'RSAPublicKey'{modulus = Mod,
ok = der_to_pem(KeyFile, PemFile), publicExponent = PE},
{ok, PemBin} = file:read_file(PemFile),
[PemData] = public_key:pem_decode(PemBin),
Pub = public_key:pem_entry_decode(PemData),
PubDer = public_key:der_encode('RSAPublicKey', Pub),
ok = file:write_file(PubFile, PubDer),
generate_rsa2(Owner, PemFile, KeyFile, PubFile);
{error, no_ssl} ->
ok = tell(error, "OpenSSL not found."),
{error, keygen_fail}
end.
generate_rsa2(Owner, PemFile, KeyFile, PubFile) ->
{ok, PubBin} = file:read_file(PubFile),
{ok, KeyBin} = file:read_file(KeyFile),
Pub = public_key:der_decode('RSAPublicKey', PubBin),
Key = public_key:der_decode('RSAPrivateKey', KeyBin),
TestMessage = <<"Some test data to sign.">>, TestMessage = <<"Some test data to sign.">>,
Signature = public_key:sign(TestMessage, sha512, Key), Signature = public_key:sign(TestMessage, sha512, Key),
case public_key:verify(TestMessage, sha512, Signature, Pub) of case public_key:verify(TestMessage, sha512, Signature, Pub) of
true -> true ->
PubHash = crypto:hash(sha512, PubBin), KeyDER = public_key:der_encode('RSAPrivateKey', Key),
KeyData = {PubHash, {none, PubBin}, {none, KeyBin}}, PubDER = public_key:der_encode('RSAPublicKey', Pub),
PubHash = crypto:hash(sha512, PubDER),
KeyData = {PubHash, {none, PubDER}, {none, KeyDER}},
ok = zx_daemon:register_key(Owner, KeyData), ok = zx_daemon:register_key(Owner, KeyData),
ok = zx_lib:rm_rf(filename:dirname(KeyFile)),
{ok, PubHash}; {ok, PubHash};
false -> false ->
ok = lists:foreach(fun file:delete/1, [PemFile, KeyFile, PubFile]),
ok = tell(error, "Something has gone wrong."), ok = tell(error, "Something has gone wrong."),
{error, keygen_fail} {error, keygen_fail}
end. end.
-spec gen_p_key(KeyFile) -> Result
when KeyFile :: file:filename(),
Result :: ok
| {error, no_ssl}.
%% @private
%% Format an openssl shell command that will generate proper 16k RSA keys.
gen_p_key(KeyFile) ->
case openssl() of
{ok, OpenSSL} ->
Command =
io_lib:format("~ts genpkey"
" -algorithm rsa"
" -out ~ts"
" -outform DER"
" -pkeyopt rsa_keygen_bits:16384",
[OpenSSL, KeyFile]),
Out = os:cmd(Command),
io:format(Out);
Error ->
Error
end.
-spec der_to_pem(KeyFile, PemFile) -> ok
when KeyFile :: file:filename(),
PemFile :: file:filename().
%% @private
%% Format an openssl shell command that will convert the given keyfile to a pemfile.
%% The reason for this conversion is to sidestep some formatting weirdness that OpenSSL
%% injects into its generated DER formatted key output (namely, a few empty headers)
%% which Erlang's ASN.1 defintion files do not take into account. A conversion to PEM
%% then a conversion back to DER (via Erlang's ASN.1 module) resolves this in a reliable
%% way.
der_to_pem(KeyFile, PemFile) ->
case openssl() of
{ok, OpenSSL} ->
Command =
io_lib:format("~ts rsa"
" -inform DER"
" -in ~ts"
" -outform PEM"
" -pubout"
" -out ~ts",
[OpenSSL, KeyFile, PemFile]),
Out = os:cmd(Command),
io:format(Out);
Error ->
Error
end.
-spec openssl() -> Result
when Result :: {ok, Executable}
| {error, no_ssl},
Executable :: file:filename().
%% @private
%% Attempt to locate the installed openssl executable for use in shell commands.
%% TODO: Determine whether it is even worth it to perform this check VS restricting
%% os:cmd/1 directed zx_key functions by platform.
openssl() ->
OpenSSL =
case os:type() of
{unix, _} -> "openssl";
{win32, _} -> "openssl.exe"
end,
case os:find_executable(OpenSSL) of
false ->
M = "OpenSSL not foud in PATH. Install OpenSSL or add to path and retry.",
ok = tell(error, M),
{error, no_ssl};
Path ->
ok = tell("OpenSSL executable found at: ~ts", [Path]),
{ok, OpenSSL}
end.
-spec save(Type, KeyID, Key) -> Result -spec save(Type, KeyID, Key) -> Result
when Type :: private | public, when Type :: private | public,
KeyID :: zx:key_id(), KeyID :: zx:key_id(),

View File

@ -10,7 +10,7 @@
%%% @end %%% @end
-module(zx_lib). -module(zx_lib).
-vsn("0.10.8"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -6,7 +6,7 @@
%%% @end %%% @end
-module(zx_local). -module(zx_local).
-vsn("0.10.8"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -5,7 +5,7 @@
%%% @end %%% @end
-module(zx_net). -module(zx_net).
-vsn("0.10.8"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -8,7 +8,7 @@
%%% @end %%% @end
-module(zx_peer). -module(zx_peer).
-vsn("0.10.8"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -9,7 +9,7 @@
%%% @end %%% @end
-module(zx_peer_man). -module(zx_peer_man).
-vsn("0.10.8"). -vsn("0.10.9").
-behavior(gen_server). -behavior(gen_server).
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -6,7 +6,7 @@
%%% @end %%% @end
-module(zx_peer_sup). -module(zx_peer_sup).
-vsn("0.10.8"). -vsn("0.10.9").
-behaviour(supervisor). -behaviour(supervisor).
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -10,7 +10,7 @@
%%% @end %%% @end
-module(zx_peers). -module(zx_peers).
-vsn("0.10.8"). -vsn("0.10.9").
-behavior(supervisor). -behavior(supervisor).
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -5,7 +5,7 @@
%%% @end %%% @end
-module(zx_proxy). -module(zx_proxy).
-vsn("0.10.8"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -5,7 +5,7 @@
%%% @end %%% @end
-module(zx_sup). -module(zx_sup).
-vsn("0.10.8"). -vsn("0.10.9").
-behavior(supervisor). -behavior(supervisor).
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -6,7 +6,7 @@
%%% @end %%% @end
-module(zx_tty). -module(zx_tty).
-vsn("0.10.8"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -5,7 +5,7 @@
%%% @end %%% @end
-module(zx_userconf). -module(zx_userconf).
-vsn("0.10.8"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -7,7 +7,7 @@
%%% @end %%% @end
-module(zx_zsp). -module(zx_zsp).
-vsn("0.10.8"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -9,7 +9,7 @@
{license,"MIT"}. {license,"MIT"}.
{modules,[]}. {modules,[]}.
{name,"zx"}. {name,"zx"}.
{package_id,{"otpr","zx",{0,10,8}}}. {package_id,{"otpr","zx",{0,10,9}}}.
{prefix,"zx_"}. {prefix,"zx_"}.
{repo_url,"https://gitlab.com/zxq9/zx"}. {repo_url,"https://gitlab.com/zxq9/zx"}.
{tags,["tools","package manager","erlang"]}. {tags,["tools","package manager","erlang"]}.