Packages
diint_utilites_common_app
1.4.21
1.4.23
1.4.22
1.4.21
1.4.20
1.4.19
1.4.18
1.4.17
1.4.16
1.4.15
1.4.14
1.4.12
1.4.11
1.4.10
1.4.9
1.4.8
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.9
1.3.8
1.3.7
1.3.6
1.3.5
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.2.101
1.2.11
1.2.10
1.2.9
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.13
1.0.12
1.0.11
1.0.10
1.0.9
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
библиотеки для работы с ребитом и протоколы
Current section
Files
Jump to
Current section
Files
src/security/diint_openssl.erl
%%%-------------------------------------------------------------------
%%% @author cheese
%%% @copyright (C) 2016, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 04. Jul 2016 4:30 PM
%%%-------------------------------------------------------------------
-module(diint_openssl).
-author("cheese").
-include_lib("kernel/include/logger.hrl").
%% API
-export([execute/2, wait_for_port/1, mk_args/1, mk_val/1, pem_2_der/1]).
execute(Command, ArgsIn) ->
OpenSSL = os:find_executable("openssl"),
?LOG_INFO("OpenSSL: ~p~n", [OpenSSL]),
Args = [atom_to_list(Command) | mk_args(ArgsIn)],
Port = open_port({spawn_executable, OpenSSL}, [use_stdio, binary, {line, 1000}, {args, Args}, stderr_to_stdout, exit_status]),
wait_for_port(Port).
wait_for_port(Port) ->
wait_for_port(Port, <<>>).
wait_for_port(Port, Reply) ->
receive
{Port, {data, {eol, Data}}} ->
?LOG_INFO("wait eol~n", []),
wait_for_port(Port, <<Reply/binary, Data/binary>>);
{Port, {data, Data}} ->
?LOG_INFO("wait data: ~p~n", [Data]),
wait_for_port(Port, <<Reply/binary, Data/binary>>);
{Port, {exit_status, 0}} ->
?LOG_INFO("wait exit_status: 0~n", []),
{ok, Reply};
{Port, {exit_status, S}} ->
?LOG_INFO("wait exit_status: ~p~n", [S]),
{error, S, Reply}
end.
mk_args([]) ->
[];
mk_args([{K, V} | R]) ->
[mk_val(K), mk_val(V) | mk_args(R)];
mk_args([K | R]) ->
[mk_val(K) | mk_args(R)].
mk_val(I) when is_integer(I) ->
integer_to_list(I);
mk_val(A) when is_atom(A) ->
[$- | atom_to_list(A)];
mk_val(L) when is_list(L) ->
L;
mk_val(B) when is_binary(B) ->
binary_to_list(B).
pem_2_der(PemBin) ->
FromFile = lib:nonl(os:cmd("mktemp")),
file:write_file(FromFile, PemBin),
ToFile = lib:nonl(os:cmd("mktemp")),
case diint_openssl:execute(x509, [{in, FromFile}, {out, ToFile}, {outform, "DER"}]) of
{ok, Reply} ->
?LOG_INFO("openssl res or: ~s~n", [Reply]),
file:read_file(ToFile);
{error, S, Reply} ->
?LOG_INFO("openssl res error: ~p # ~s~n", [S, Reply]),
{error, {openssl, {S, Reply}}}
end.