Skip to content

Commit

Permalink
commit generated EST
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Oct 24, 2024
1 parent 3cb2bf2 commit f95b6d4
Show file tree
Hide file tree
Showing 2 changed files with 376 additions and 0 deletions.
14 changes: 14 additions & 0 deletions include/EST.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
%% Generated by the Erlang ASN.1 compiler. Version: 5.0.17
%% Purpose: Erlang record definitions for each named and unnamed
%% SEQUENCE and SET, and macro definitions for each value
%% definition in module EST.

-ifndef(_EST_HRL_).
-define(_EST_HRL_, true).

-record('Attribute', {
type,
values
}).

-endif. %% _EST_HRL_
362 changes: 362 additions & 0 deletions src/EST.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,362 @@
%% Generated by the Erlang ASN.1 BER compiler. Version: 5.0.17
%% Purpose: Encoding and decoding of the types in EST.

-module('EST').
-compile(nowarn_unused_vars).
-dialyzer(no_improper_lists).
-dialyzer(no_match).
-include_lib("ca/include/EST.hrl").
-asn1_info([{vsn,'5.0.17'},
{module,'EST'},
{options,[warnings,ber,errors,
{cwd,"/home/maxim/depot/arvo-computer/ca/priv/csr"},
{outdir,"/home/maxim/depot/arvo-computer/ca/priv/csr"},
{i,"."},
{i,"/home/maxim/depot/arvo-computer/ca/priv/csr"}]}]).

-export([encoding_rule/0,maps/0,bit_string_format/0,
legacy_erlang_types/0]).
-export(['dialyzer-suppressions'/1]).
-export([
enc_CsrAttrs/2,
enc_AttrOrOID/2,
enc_Attribute/2
]).

-export([
dec_CsrAttrs/2,
dec_AttrOrOID/2,
dec_Attribute/2
]).

-export([info/0]).

-export([encode/2,decode/2]).

encoding_rule() -> ber.

maps() -> false.

bit_string_format() -> bitstring.

legacy_erlang_types() -> false.

encode(Type, Data) ->
try iolist_to_binary(element(1, encode_disp(Type, Data))) of
Bytes ->
{ok,Bytes}
catch
Class:Exception:Stk when Class =:= error; Class =:= exit ->
case Exception of
{error,{asn1,Reason}} ->
{error,{asn1,{Reason,Stk}}};
Reason ->
{error,{asn1,{Reason,Stk}}}
end
end.

decode(Type, Data) ->
try
Result = decode_disp(Type, element(1, ber_decode_nif(Data))),
{ok,Result}
catch
Class:Exception:Stk when Class =:= error; Class =:= exit ->
case Exception of
{error,{asn1,Reason}} ->
{error,{asn1,{Reason,Stk}}};
Reason ->
{error,{asn1,{Reason,Stk}}}
end
end.

encode_disp('CsrAttrs', Data) -> enc_CsrAttrs(Data);
encode_disp('AttrOrOID', Data) -> enc_AttrOrOID(Data);
encode_disp('Attribute', Data) -> enc_Attribute(Data);
encode_disp(Type, _Data) -> exit({error,{asn1,{undefined_type,Type}}}).

decode_disp('CsrAttrs', Data) -> dec_CsrAttrs(Data);
decode_disp('AttrOrOID', Data) -> dec_AttrOrOID(Data);
decode_disp('Attribute', Data) -> dec_Attribute(Data);
decode_disp(Type, _Data) -> exit({error,{asn1,{undefined_type,Type}}}).

info() ->
case ?MODULE:module_info(attributes) of
Attributes when is_list(Attributes) ->
case lists:keyfind(asn1_info, 1, Attributes) of
{_,Info} when is_list(Info) ->
Info;
_ ->
[]
end;
_ ->
[]
end.


%%================================
%% CsrAttrs
%%================================
enc_CsrAttrs(Val) ->
enc_CsrAttrs(Val, [<<48>>]).

enc_CsrAttrs(Val, TagIn) ->
{EncBytes,EncLen} = 'enc_CsrAttrs_components'(Val,[],0),
encode_tags(TagIn, EncBytes, EncLen).

'enc_CsrAttrs_components'([], AccBytes, AccLen) ->
{lists:reverse(AccBytes),AccLen};

'enc_CsrAttrs_components'([H|T],AccBytes, AccLen) ->
{EncBytes,EncLen} = 'enc_AttrOrOID'(H, []),
'enc_CsrAttrs_components'(T,[EncBytes|AccBytes], AccLen + EncLen).



dec_CsrAttrs(Tlv) ->
dec_CsrAttrs(Tlv, [16]).

dec_CsrAttrs(Tlv, TagIn) ->
%%-------------------------------------------------
%% decode tag and length
%%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),
['dec_AttrOrOID'(V1, []) || V1 <- Tlv1].




%%================================
%% AttrOrOID
%%================================
enc_AttrOrOID(Val) ->
enc_AttrOrOID(Val, []).

enc_AttrOrOID(Val, TagIn) ->
{EncBytes,EncLen} = case element(1,Val) of
oid ->
encode_object_identifier(element(2,Val), [<<6>>]);
attribute ->
'enc_Attribute'(element(2,Val), [<<48>>]);
Else ->
exit({error,{asn1,{invalid_choice_type,Else}}})
end,

encode_tags(TagIn, EncBytes, EncLen).




dec_AttrOrOID(Tlv) ->
dec_AttrOrOID(Tlv, []).

dec_AttrOrOID(Tlv, TagIn) ->
Tlv1 = match_tags(Tlv, TagIn),
case (case Tlv1 of [CtempTlv1] -> CtempTlv1; _ -> Tlv1 end) of

%% 'oid'
{6, V1} ->
{oid, decode_object_identifier(V1, [])};


%% 'attribute'
{16, V1} ->
{attribute, 'dec_Attribute'(V1, [])};

Else ->
exit({error,{asn1,{invalid_choice_tag,Else}}})
end
.


%%================================
%% Attribute
%%================================
enc_Attribute(Val) ->
enc_Attribute(Val, [<<48>>]).

enc_Attribute(Val, TagIn) ->
{_,Cindex1,Cindex2} = Val,

%%-------------------------------------------------
%% attribute type(1) with type OBJECT IDENTIFIER
%%-------------------------------------------------
{EncBytes1,EncLen1} = encode_object_identifier(Cindex1, [<<6>>]),

%%-------------------------------------------------
%% attribute values(2) with type SET OF
%%-------------------------------------------------
{EncBytes2,EncLen2} = 'enc_Attribute_values'(Cindex2, [<<49>>]),

BytesSoFar = [EncBytes1, EncBytes2],
LenSoFar = EncLen1 + EncLen2,
encode_tags(TagIn, BytesSoFar, LenSoFar).



%%================================
%% Attribute_values
%%================================
enc_Attribute_values(Val, TagIn) ->
{EncBytes,EncLen} = 'enc_Attribute_values_components'(Val,[],0),
encode_tags(TagIn, EncBytes, EncLen).

'enc_Attribute_values_components'([], AccBytes, AccLen) ->
{lists:reverse(AccBytes),AccLen};

'enc_Attribute_values_components'([H|T],AccBytes, AccLen) ->
{EncBytes,EncLen} = encode_open_type(H, []),
'enc_Attribute_values_components'(T,[EncBytes|AccBytes], AccLen + EncLen).



dec_Attribute(Tlv) ->
dec_Attribute(Tlv, [16]).

dec_Attribute(Tlv, TagIn) ->
%%-------------------------------------------------
%% decode tag and length
%%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute type(1) with type OBJECT IDENTIFIER
%%-------------------------------------------------
[V1|Tlv2] = Tlv1,
Term1 = decode_object_identifier(V1, [6]),

%%-------------------------------------------------
%% attribute values(2) with type SET OF
%%-------------------------------------------------
[V2|Tlv3] = Tlv2,
Term2 = 'dec_Attribute_values'(V2, [17]),

case Tlv3 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed
end,
Res1 = {'Attribute',Term1,Term2},
Res1.
'dec_Attribute_values'(Tlv, TagIn) ->
%%-------------------------------------------------
%% decode tag and length
%%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),
[decode_open_type_as_binary(V1, []) || V1 <- Tlv1].



%%%
%%% Run-time functions.
%%%

'dialyzer-suppressions'(Arg) ->
ok.

ber_decode_nif(B) ->
asn1rt_nif:decode_ber_tlv(B).

ber_encode([Tlv]) ->
ber_encode(Tlv);
ber_encode(Tlv) when is_binary(Tlv) ->
Tlv;
ber_encode(Tlv) ->
asn1rt_nif:encode_ber_tlv(Tlv).

dec_subidentifiers(<<>>, _Av, Al) ->
lists:reverse(Al);
dec_subidentifiers(<<1:1,H:7,T/binary>>, Av, Al) ->
dec_subidentifiers(T, Av bsl 7 + H, Al);
dec_subidentifiers(<<H,T/binary>>, Av, Al) ->
dec_subidentifiers(T, 0, [Av bsl 7 + H | Al]).

decode_object_identifier(Tlv, Tags) ->
Val = match_tags(Tlv, Tags),
[AddedObjVal | ObjVals] = dec_subidentifiers(Val, 0, []),
{Val1, Val2} =
if
AddedObjVal < 40 ->
{0, AddedObjVal};
AddedObjVal < 80 ->
{1, AddedObjVal - 40};
true ->
{2, AddedObjVal - 80}
end,
list_to_tuple([Val1, Val2 | ObjVals]).

decode_open_type_as_binary(Tlv, TagIn) ->
ber_encode(match_tags(Tlv, TagIn)).

e_object_identifier({'OBJECT IDENTIFIER', V}) ->
e_object_identifier(V);
e_object_identifier(V) when is_tuple(V) ->
e_object_identifier(tuple_to_list(V));
e_object_identifier([E1, E2 | Tail]) ->
Head = 40 * E1 + E2,
{H, Lh} = mk_object_val(Head),
{R, Lr} = lists:mapfoldl(fun enc_obj_id_tail/2, 0, Tail),
{[H | R], Lh + Lr}.

enc_obj_id_tail(H, Len) ->
{B, L} = mk_object_val(H),
{B, Len + L}.

encode_length(L) when L =< 127 ->
{[L], 1};
encode_length(L) ->
Oct = minimum_octets(L),
Len = length(Oct),
if
Len =< 126 ->
{[128 bor Len | Oct], Len + 1};
true ->
exit({error, {asn1, too_long_length_oct, Len}})
end.

encode_object_identifier(Val, TagIn) ->
encode_tags(TagIn, e_object_identifier(Val)).

encode_open_type(Val, T) when is_list(Val) ->
encode_open_type(list_to_binary(Val), T);
encode_open_type(Val, Tag) ->
encode_tags(Tag, Val, byte_size(Val)).

encode_tags(TagIn, {BytesSoFar, LenSoFar}) ->
encode_tags(TagIn, BytesSoFar, LenSoFar).

encode_tags([Tag | Trest], BytesSoFar, LenSoFar) ->
{Bytes2, L2} = encode_length(LenSoFar),
encode_tags(Trest,
[Tag, Bytes2 | BytesSoFar],
LenSoFar + byte_size(Tag) + L2);
encode_tags([], BytesSoFar, LenSoFar) ->
{BytesSoFar, LenSoFar}.

match_tags({T, V}, [T]) ->
V;
match_tags({T, V}, [T | Tt]) ->
match_tags(V, Tt);
match_tags([{T, V}], [T | Tt]) ->
match_tags(V, Tt);
match_tags([{T, _V} | _] = Vlist, [T]) ->
Vlist;
match_tags(Tlv, []) ->
Tlv;
match_tags({Tag, _V} = Tlv, [T | _Tt]) ->
exit({error, {asn1, {wrong_tag, {{expected, T}, {got, Tag, Tlv}}}}}).

minimum_octets(0, Acc) ->
Acc;
minimum_octets(Val, Acc) ->
minimum_octets(Val bsr 8, [Val band 255 | Acc]).

minimum_octets(Val) ->
minimum_octets(Val, []).

mk_object_val(0, Ack, Len) ->
{Ack, Len};
mk_object_val(Val, Ack, Len) ->
mk_object_val(Val bsr 7, [Val band 127 bor 128 | Ack], Len + 1).

mk_object_val(Val) when Val =< 127 ->
{[255 band Val], 1};
mk_object_val(Val) ->
mk_object_val(Val bsr 7, [Val band 127], 1).

0 comments on commit f95b6d4

Please sign in to comment.