diff --git a/lib/cmp.ex b/lib/cmp.ex index 15f08e5..8262c2a 100644 --- a/lib/cmp.ex +++ b/lib/cmp.ex @@ -5,6 +5,10 @@ defmodule CA.CMP do # openssl cmp -cmd genm -server 127.0.0.1:829 \ # -recipient "/CN=CMPserver" -ref 1234 -secret pass:0000 + # openssl cmp -cmd ir -server 127.0.0.1:829 \ + # -path priv/certs -srvcert caroot.pem -ref NewUser \ + # -secret pass:0000 -certout x.pem -newkey maxim.key.enc -subject "/CN=maxim/O=SYNRC/ST=Kyiv/C=UA" + def start(), do: :erlang.spawn(fn -> listen(829) end) def listen(port) do {:ok, socket} = :gen_tcp.listen(port, diff --git a/src/PKCS-10.erl b/src/PKCS-10.erl new file mode 100644 index 0000000..373e681 --- /dev/null +++ b/src/PKCS-10.erl @@ -0,0 +1,955 @@ +%% Generated by the Erlang ASN.1 BER compiler. Version: 5.1 +%% Purpose: Encoding and decoding of the types in PKCS-10. + +-module('PKCS-10'). +-compile(nowarn_unused_vars). +-dialyzer(no_improper_lists). +-dialyzer(no_match). +-include("PKCS-10.hrl"). +-asn1_info([{vsn,'5.1'}, + {module,'PKCS-10'}, + {options,[warnings,ber,errors, + {cwd,"/Users/maxim/depot/synrc/ca/priv/csr"}, + {outdir,"/Users/maxim/depot/synrc/ca/priv/csr"}, + {i,"."}, + {i,"/Users/maxim/depot/synrc/ca/priv/csr"}]}]). + +-export([encoding_rule/0,maps/0,bit_string_format/0, + legacy_erlang_types/0]). +-export(['dialyzer-suppressions'/1]). +-export([ +enc_CertificationRequestInfo/2, +enc_CertificationRequest/2 +]). + +-export([ +dec_CertificationRequestInfo/2, +dec_CertificationRequest/2 +]). + +-export([ +getenc_PKInfoAlgorithms/1, +getenc_CRIAttributes/1, +getenc_SignatureAlgorithms/1, +getenc_internal_object_set_argument_8/1, +getenc_internal_object_set_argument_6/1, +getenc_internal_object_set_argument_5/1, +getenc_internal_object_set_argument_4/1, +getenc_internal_object_set_argument_2/1 +]). + +-export([ +getdec_PKInfoAlgorithms/1, +getdec_CRIAttributes/1, +getdec_SignatureAlgorithms/1, +getdec_internal_object_set_argument_8/1, +getdec_internal_object_set_argument_6/1, +getdec_internal_object_set_argument_5/1, +getdec_internal_object_set_argument_4/1, +getdec_internal_object_set_argument_2/1 +]). + +-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('CertificationRequestInfo', Data) -> enc_CertificationRequestInfo(Data); +encode_disp('CertificationRequest', Data) -> enc_CertificationRequest(Data); +encode_disp(Type, _Data) -> exit({error,{asn1,{undefined_type,Type}}}). + +decode_disp('CertificationRequestInfo', Data) -> dec_CertificationRequestInfo(Data); +decode_disp('CertificationRequest', Data) -> dec_CertificationRequest(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. + + +%%================================ +%% CertificationRequestInfo +%%================================ +enc_CertificationRequestInfo(Val) -> + enc_CertificationRequestInfo(Val, [<<48>>]). + +enc_CertificationRequestInfo(Val, TagIn) -> +{_,Cindex1,Cindex2,Cindex3,Cindex4} = Val, + +%%------------------------------------------------- +%% attribute version(1) with type INTEGER +%%------------------------------------------------- + {EncBytes1,EncLen1} = encode_integer(Cindex1, [{v1,0}], [<<2>>]), + +%%------------------------------------------------- +%% attribute subject(2) External PKIX1Explicit-2009:Name +%%------------------------------------------------- + {EncBytes2,EncLen2} = 'PKIX1Explicit-2009':'enc_Name'(Cindex2, []), + +%%------------------------------------------------- +%% attribute subjectPKInfo(3) with type SEQUENCE +%%------------------------------------------------- + {EncBytes3,EncLen3} = 'enc_CertificationRequestInfo_subjectPKInfo'(Cindex3, [<<48>>]), + +%%------------------------------------------------- +%% attribute attributes(4) with type SET OF +%%------------------------------------------------- + {EncBytes4,EncLen4} = 'enc_CertificationRequestInfo_attributes'(Cindex4, [<<160>>]), + + BytesSoFar = [EncBytes1, EncBytes2, EncBytes3, EncBytes4], +LenSoFar = EncLen1 + EncLen2 + EncLen3 + EncLen4, +encode_tags(TagIn, BytesSoFar, LenSoFar). + + + +%%================================ +%% CertificationRequestInfo_subjectPKInfo +%%================================ +enc_CertificationRequestInfo_subjectPKInfo(Val, TagIn) -> + {_,Cindex1,Cindex2} = Val, + +%%------------------------------------------------- +%% attribute algorithm(1) with type SEQUENCE +%%------------------------------------------------- + {EncBytes1,EncLen1} = 'enc_CertificationRequestInfo_subjectPKInfo_algorithm'(Cindex1, [<<48>>]), + +%%------------------------------------------------- +%% attribute subjectPublicKey(2) with type BIT STRING +%%------------------------------------------------- + {EncBytes2,EncLen2} = encode_unnamed_bit_string(Cindex2, [<<3>>]), + + BytesSoFar = [EncBytes1, EncBytes2], +LenSoFar = EncLen1 + EncLen2, +encode_tags(TagIn, BytesSoFar, LenSoFar). + + + +%%================================ +%% CertificationRequestInfo_subjectPKInfo_algorithm +%%================================ +enc_CertificationRequestInfo_subjectPKInfo_algorithm(Val, TagIn) -> + {_,Cindex1,Cindex2} = Val, +Objalgorithm = + 'PKCS-10':'getenc_internal_object_set_argument_4'( Cindex1), + +%%------------------------------------------------- +%% attribute algorithm(1) with type OBJECT IDENTIFIER +%%------------------------------------------------- + {EncBytes1,EncLen1} = encode_object_identifier(Cindex1, [<<6>>]), + +%%------------------------------------------------- +%% attribute parameters(2) with type typefieldParams OPTIONAL +%%------------------------------------------------- + {EncBytes2,EncLen2} = case Cindex2 of + asn1_NOVALUE -> {<<>>,0}; + _ -> + {TmpBytes2,_ } = Objalgorithm('Params', Cindex2, []), + encode_open_type(TmpBytes2, []) + end, + + BytesSoFar = [EncBytes1, EncBytes2], +LenSoFar = EncLen1 + EncLen2, +encode_tags(TagIn, BytesSoFar, LenSoFar). + + + +%%================================ +%% CertificationRequestInfo_attributes +%%================================ +enc_CertificationRequestInfo_attributes(Val, TagIn) -> + {EncBytes,EncLen} = 'enc_CertificationRequestInfo_attributes_components'(Val,[],0), + encode_tags(TagIn, EncBytes, EncLen). + +'enc_CertificationRequestInfo_attributes_components'([], AccBytes, AccLen) -> + {lists:reverse(AccBytes),AccLen}; + +'enc_CertificationRequestInfo_attributes_components'([H|T],AccBytes, AccLen) -> + {EncBytes,EncLen} = 'enc_CertificationRequestInfo_attributes_Attribute'(H, [<<48>>]), + 'enc_CertificationRequestInfo_attributes_components'(T,[EncBytes|AccBytes], AccLen + EncLen). + + + + +%%================================ +%% CertificationRequestInfo_attributes_Attribute +%%================================ +enc_CertificationRequestInfo_attributes_Attribute(Val, TagIn) -> + {_,Cindex1,Cindex2} = Val, +Objtype = + 'PKCS-10':'getenc_internal_object_set_argument_6'( Cindex1), + +%%------------------------------------------------- +%% attribute type(1) with type OBJECT IDENTIFIER +%%------------------------------------------------- + {EncBytes1,EncLen1} = encode_object_identifier(Cindex1, [<<6>>]), + +%%------------------------------------------------- +%% attribute values(2) with type SET OF +%%------------------------------------------------- + {EncBytes2,EncLen2} = 'enc_CertificationRequestInfo_attributes_Attribute_values'(Cindex2, [<<49>>], Objtype), + + BytesSoFar = [EncBytes1, EncBytes2], +LenSoFar = EncLen1 + EncLen2, +encode_tags(TagIn, BytesSoFar, LenSoFar). + + + +%%================================ +%% CertificationRequestInfo_attributes_Attribute_values +%%================================ +enc_CertificationRequestInfo_attributes_Attribute_values(Val, TagIn, ObjFun) -> + {EncBytes,EncLen} = 'enc_CertificationRequestInfo_attributes_Attribute_values_components'(Val, ObjFun,[],0), + encode_tags(TagIn, EncBytes, EncLen). + +'enc_CertificationRequestInfo_attributes_Attribute_values_components'([], _, AccBytes, AccLen) -> + {lists:reverse(AccBytes),AccLen}; + +'enc_CertificationRequestInfo_attributes_Attribute_values_components'([H|T], ObjFun,AccBytes, AccLen) -> + {TmpBytes,_} = ObjFun('Type', H, []), + {EncBytes,EncLen} = encode_open_type(TmpBytes, []) +, + 'enc_CertificationRequestInfo_attributes_Attribute_values_components'(T, ObjFun,[EncBytes|AccBytes], AccLen + EncLen). + + + +dec_CertificationRequestInfo(Tlv) -> + dec_CertificationRequestInfo(Tlv, [16]). + +dec_CertificationRequestInfo(Tlv, TagIn) -> + %%------------------------------------------------- + %% decode tag and length + %%------------------------------------------------- +Tlv1 = match_tags(Tlv, TagIn), + +%%------------------------------------------------- +%% attribute version(1) with type INTEGER +%%------------------------------------------------- +[V1|Tlv2] = Tlv1, +Term1 = begin +Val1 = decode_integer(V1, [2]), +number2name(Val1, [{v1,0}]) +end +, + +%%------------------------------------------------- +%% attribute subject(2) External PKIX1Explicit-2009:Name +%%------------------------------------------------- +[V2|Tlv3] = Tlv2, +Term2 = 'PKIX1Explicit-2009':'dec_Name'(V2, []), + +%%------------------------------------------------- +%% attribute subjectPKInfo(3) with type SEQUENCE +%%------------------------------------------------- +[V3|Tlv4] = Tlv3, +Term3 = 'dec_CertificationRequestInfo_subjectPKInfo'(V3, [16]), + +%%------------------------------------------------- +%% attribute attributes(4) with type SET OF +%%------------------------------------------------- +[V4|Tlv5] = Tlv4, +Term4 = 'dec_CertificationRequestInfo_attributes'(V4, [131072]), + +case Tlv5 of +[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv5}}}) % extra fields not allowed +end, +Res1 = {'CertificationRequestInfo',Term1,Term2,Term3,Term4}, +Res1. +'dec_CertificationRequestInfo_subjectPKInfo'(Tlv, TagIn) -> + %%------------------------------------------------- + %% decode tag and length + %%------------------------------------------------- +Tlv1 = match_tags(Tlv, TagIn), + +%%------------------------------------------------- +%% attribute algorithm(1) with type SEQUENCE +%%------------------------------------------------- +[V1|Tlv2] = Tlv1, +Term1 = 'dec_CertificationRequestInfo_subjectPKInfo_algorithm'(V1, [16]), + +%%------------------------------------------------- +%% attribute subjectPublicKey(2) with type BIT STRING +%%------------------------------------------------- +[V2|Tlv3] = Tlv2, +Term2 = decode_native_bit_string(V2, [3]), + +case Tlv3 of +[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed +end, +Res1 = {'CertificationRequestInfo_subjectPKInfo',Term1,Term2}, +Res1. +'dec_CertificationRequestInfo_subjectPKInfo_algorithm'(Tlv, TagIn) -> + %%------------------------------------------------- + %% decode tag and length + %%------------------------------------------------- +Tlv1 = match_tags(Tlv, TagIn), + +%%------------------------------------------------- +%% attribute algorithm(1) with type OBJECT IDENTIFIER +%%------------------------------------------------- +[V1|Tlv2] = Tlv1, +Term1 = decode_object_identifier(V1, [6]), + +%%------------------------------------------------- +%% attribute parameters(2) with type typefieldParams OPTIONAL +%%------------------------------------------------- +{Tmpterm1,Tlv3} = case Tlv2 of +[V2|TempTlv3] -> + {decode_open_type(V2, []), TempTlv3}; + _ -> + { asn1_NOVALUE, Tlv2} +end, + +DecObjalgorithmTerm1 = + 'PKCS-10':'getdec_internal_object_set_argument_4'(Term1), +Term2 = + case Tmpterm1 of + asn1_NOVALUE ->asn1_NOVALUE; + _ -> + case (catch DecObjalgorithmTerm1('Params', Tmpterm1, [])) of + {'EXIT', Reason1} -> + exit({'Type not compatible with table constraint',Reason1}); + Tmpterm2 -> + Tmpterm2 + end + end, + +case Tlv3 of +[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed +end, +Res1 = {'CertificationRequestInfo_subjectPKInfo_algorithm',Term1,Term2}, +Res1. +'dec_CertificationRequestInfo_attributes'(Tlv, TagIn) -> + %%------------------------------------------------- + %% decode tag and length + %%------------------------------------------------- +Tlv1 = match_tags(Tlv, TagIn), +['dec_CertificationRequestInfo_attributes_Attribute'(V1, [16]) || V1 <- Tlv1]. + + +'dec_CertificationRequestInfo_attributes_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]), +ObjFun = 'PKCS-10':'getdec_internal_object_set_argument_6'(Term1), + +%%------------------------------------------------- +%% attribute values(2) with type SET OF +%%------------------------------------------------- +[V2|Tlv3] = Tlv2, +Term2 = 'dec_CertificationRequestInfo_attributes_Attribute_values'(V2, [17], ObjFun), + +case Tlv3 of +[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed +end, +Res1 = {'Attribute',Term1,Term2}, +Res1. +'dec_CertificationRequestInfo_attributes_Attribute_values'(Tlv, TagIn, ObjFun) -> + %%------------------------------------------------- + %% decode tag and length + %%------------------------------------------------- +Tlv1 = match_tags(Tlv, TagIn), +[ + begin + Tmptlv1 = decode_open_type(V1, []), + case (catch ObjFun('Type', Tmptlv1, [])) of + {'EXIT',Reason1} -> + exit({'Type not compatible with table constraint', Reason1}); + Tmpterm1 -> + Tmpterm1 + end + end + || V1 <- Tlv1]. + + + + +%%================================ +%% CertificationRequest +%%================================ +enc_CertificationRequest(Val) -> + enc_CertificationRequest(Val, [<<48>>]). + +enc_CertificationRequest(Val, TagIn) -> +{_,Cindex1,Cindex2,Cindex3} = Val, + +%%------------------------------------------------- +%% attribute certificationRequestInfo(1) External PKCS-10:CertificationRequestInfo +%%------------------------------------------------- + {EncBytes1,EncLen1} = 'enc_CertificationRequestInfo'(Cindex1, [<<48>>]), + +%%------------------------------------------------- +%% attribute signatureAlgorithm(2) with type SEQUENCE +%%------------------------------------------------- + {EncBytes2,EncLen2} = 'enc_CertificationRequest_signatureAlgorithm'(Cindex2, [<<48>>]), + +%%------------------------------------------------- +%% attribute signature(3) with type BIT STRING +%%------------------------------------------------- + {EncBytes3,EncLen3} = encode_unnamed_bit_string(Cindex3, [<<3>>]), + + BytesSoFar = [EncBytes1, EncBytes2, EncBytes3], +LenSoFar = EncLen1 + EncLen2 + EncLen3, +encode_tags(TagIn, BytesSoFar, LenSoFar). + + + +%%================================ +%% CertificationRequest_signatureAlgorithm +%%================================ +enc_CertificationRequest_signatureAlgorithm(Val, TagIn) -> + {_,Cindex1,Cindex2} = Val, +Objalgorithm = + 'PKCS-10':'getenc_internal_object_set_argument_8'( Cindex1), + +%%------------------------------------------------- +%% attribute algorithm(1) with type OBJECT IDENTIFIER +%%------------------------------------------------- + {EncBytes1,EncLen1} = encode_object_identifier(Cindex1, [<<6>>]), + +%%------------------------------------------------- +%% attribute parameters(2) with type typefieldParams OPTIONAL +%%------------------------------------------------- + {EncBytes2,EncLen2} = case Cindex2 of + asn1_NOVALUE -> {<<>>,0}; + _ -> + {TmpBytes2,_ } = Objalgorithm('Params', Cindex2, []), + encode_open_type(TmpBytes2, []) + end, + + BytesSoFar = [EncBytes1, EncBytes2], +LenSoFar = EncLen1 + EncLen2, +encode_tags(TagIn, BytesSoFar, LenSoFar). + + +dec_CertificationRequest(Tlv) -> + dec_CertificationRequest(Tlv, [16]). + +dec_CertificationRequest(Tlv, TagIn) -> + %%------------------------------------------------- + %% decode tag and length + %%------------------------------------------------- +Tlv1 = match_tags(Tlv, TagIn), + +%%------------------------------------------------- +%% attribute certificationRequestInfo(1) External PKCS-10:CertificationRequestInfo +%%------------------------------------------------- +[V1|Tlv2] = Tlv1, +Term1 = 'dec_CertificationRequestInfo'(V1, [16]), + +%%------------------------------------------------- +%% attribute signatureAlgorithm(2) with type SEQUENCE +%%------------------------------------------------- +[V2|Tlv3] = Tlv2, +Term2 = 'dec_CertificationRequest_signatureAlgorithm'(V2, [16]), + +%%------------------------------------------------- +%% attribute signature(3) with type BIT STRING +%%------------------------------------------------- +[V3|Tlv4] = Tlv3, +Term3 = decode_native_bit_string(V3, [3]), + +case Tlv4 of +[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv4}}}) % extra fields not allowed +end, +Res1 = {'CertificationRequest',Term1,Term2,Term3}, +Res1. +'dec_CertificationRequest_signatureAlgorithm'(Tlv, TagIn) -> + %%------------------------------------------------- + %% decode tag and length + %%------------------------------------------------- +Tlv1 = match_tags(Tlv, TagIn), + +%%------------------------------------------------- +%% attribute algorithm(1) with type OBJECT IDENTIFIER +%%------------------------------------------------- +[V1|Tlv2] = Tlv1, +Term1 = decode_object_identifier(V1, [6]), + +%%------------------------------------------------- +%% attribute parameters(2) with type typefieldParams OPTIONAL +%%------------------------------------------------- +{Tmpterm1,Tlv3} = case Tlv2 of +[V2|TempTlv3] -> + {decode_open_type(V2, []), TempTlv3}; + _ -> + { asn1_NOVALUE, Tlv2} +end, + +DecObjalgorithmTerm1 = + 'PKCS-10':'getdec_internal_object_set_argument_8'(Term1), +Term2 = + case Tmpterm1 of + asn1_NOVALUE ->asn1_NOVALUE; + _ -> + case (catch DecObjalgorithmTerm1('Params', Tmpterm1, [])) of + {'EXIT', Reason1} -> + exit({'Type not compatible with table constraint',Reason1}); + Tmpterm2 -> + Tmpterm2 + end + end, + +case Tlv3 of +[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed +end, +Res1 = {'CertificationRequest_signatureAlgorithm',Term1,Term2}, +Res1. + + + +%%================================ +%% PKInfoAlgorithms +%%================================ +getenc_PKInfoAlgorithms(_) -> + fun(_, Val, _RestPrimFieldName) -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end. + +getdec_PKInfoAlgorithms(_) -> + fun(_,Bytes, _RestPrimFieldName) -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end. + + + + + +%%================================ +%% CRIAttributes +%%================================ +getenc_CRIAttributes(_) -> + fun(_, Val, _RestPrimFieldName) -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end. + +getdec_CRIAttributes(_) -> + fun(_,Bytes, _RestPrimFieldName) -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end. + + + + + +%%================================ +%% SignatureAlgorithms +%%================================ +getenc_SignatureAlgorithms(_) -> + fun(_, Val, _RestPrimFieldName) -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end. + +getdec_SignatureAlgorithms(_) -> + fun(_,Bytes, _RestPrimFieldName) -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end. + + + + + +%%================================ +%% internal_object_set_argument_8 +%%================================ +getenc_internal_object_set_argument_8(_) -> + fun(_, Val, _RestPrimFieldName) -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end. + +getdec_internal_object_set_argument_8(_) -> + fun(_,Bytes, _RestPrimFieldName) -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end. + + + + + +%%================================ +%% internal_object_set_argument_6 +%%================================ +getenc_internal_object_set_argument_6(_) -> + fun(_, Val, _RestPrimFieldName) -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end. + +getdec_internal_object_set_argument_6(_) -> + fun(_,Bytes, _RestPrimFieldName) -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end. + + + + + +%%================================ +%% internal_object_set_argument_5 +%%================================ +getenc_internal_object_set_argument_5(_) -> + fun(_, Val, _RestPrimFieldName) -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end. + +getdec_internal_object_set_argument_5(_) -> + fun(_,Bytes, _RestPrimFieldName) -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end. + + + + + +%%================================ +%% internal_object_set_argument_4 +%%================================ +getenc_internal_object_set_argument_4(_) -> + fun(_, Val, _RestPrimFieldName) -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end. + +getdec_internal_object_set_argument_4(_) -> + fun(_,Bytes, _RestPrimFieldName) -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end. + + + + + +%%================================ +%% internal_object_set_argument_2 +%%================================ +getenc_internal_object_set_argument_2(_) -> + fun(_, Val, _RestPrimFieldName) -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end. + +getdec_internal_object_set_argument_2(_) -> + fun(_,Bytes, _RestPrimFieldName) -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end. + + + +%%% +%%% 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). + +collect_parts(TlvList) -> + collect_parts(TlvList, []). + +collect_parts([{_, L} | Rest], Acc) when is_list(L) -> + collect_parts(Rest, [collect_parts(L) | Acc]); +collect_parts([{3, <>} | Rest], _Acc) -> + collect_parts_bit(Rest, [Bits], Unused); +collect_parts([{_T, V} | Rest], Acc) -> + collect_parts(Rest, [V | Acc]); +collect_parts([], Acc) -> + list_to_binary(lists:reverse(Acc)). + +collect_parts_bit([{3, <>} | Rest], Acc, Uacc) -> + collect_parts_bit(Rest, [Bits | Acc], Unused + Uacc); +collect_parts_bit([], Acc, Uacc) -> + list_to_binary([Uacc | lists:reverse(Acc)]). + +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(<>, Av, Al) -> + dec_subidentifiers(T, 0, [Av bsl 7 + H | Al]). + +decode_integer(Tlv, TagIn) -> + Bin = match_tags(Tlv, TagIn), + Len = byte_size(Bin), + <> = Bin, + Int. + +decode_native_bit_string(Buffer, Tags) -> + case match_and_collect(Buffer, Tags) of + <<0>> -> + <<>>; + <> -> + Size = bit_size(Bits) - Unused, + <> = Bits, + Val + end. + +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(Tlv, TagIn) -> + case match_tags(Tlv, TagIn) of + Bin when is_binary(Bin) -> + {InnerTlv, _} = ber_decode_nif(Bin), + InnerTlv; + TlvBytes -> + TlvBytes + end. + +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_integer(Val) -> + Bytes = + if + Val >= 0 -> + encode_integer_pos(Val, []); + true -> + encode_integer_neg(Val, []) + end, + {Bytes, length(Bytes)}. + +encode_integer(Val, NamedNumberList, Tag) when is_atom(Val) -> + case lists:keyfind(Val, 1, NamedNumberList) of + {_, NewVal} -> + encode_tags(Tag, encode_integer(NewVal)); + _ -> + exit({error, {asn1, {encode_integer_namednumber, Val}}}) + end; +encode_integer(Val, _NamedNumberList, Tag) -> + encode_tags(Tag, encode_integer(Val)). + +encode_integer_neg(-1, [B1 | _T] = L) when B1 > 127 -> + L; +encode_integer_neg(N, Acc) -> + encode_integer_neg(N bsr 8, [N band 255 | Acc]). + +encode_integer_pos(0, [B | _Acc] = L) when B < 128 -> + L; +encode_integer_pos(N, Acc) -> + encode_integer_pos(N bsr 8, [N band 255 | Acc]). + +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}. + +encode_unnamed_bit_string(Bits, TagIn) -> + Unused = (8 - bit_size(Bits) band 7) band 7, + Bin = <>, + encode_tags(TagIn, Bin, byte_size(Bin)). + +match_and_collect(Tlv, TagsIn) -> + Val = match_tags(Tlv, TagsIn), + case Val of + [_ | _] = PartList -> + collect_parts(PartList); + Bin when is_binary(Bin) -> + Bin + end. + +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). + +number2name(Int, NamedNumberList) -> + case lists:keyfind(Int, 2, NamedNumberList) of + {NamedVal, _} -> + NamedVal; + _ -> + Int + end. diff --git a/src/PKCS-10.hrl b/src/PKCS-10.hrl new file mode 100644 index 0000000..80cd604 --- /dev/null +++ b/src/PKCS-10.hrl @@ -0,0 +1,52 @@ +%% Generated by the Erlang ASN.1 compiler. Version: 5.1 +%% Purpose: Erlang record definitions for each named and unnamed +%% SEQUENCE and SET, and macro definitions for each value +%% definition in module PKCS-10. + +-ifndef(_PKCS_10_HRL_). +-define(_PKCS_10_HRL_, true). + +-record('SubjectPublicKeyInfo', { + algorithm, + subjectPublicKey +}). + +-record('Attribute', { + type, + values +}). + +-record('CertificationRequestInfo', { + version, + subject, + subjectPKInfo, + attributes +}). + +-record('CertificationRequestInfo_subjectPKInfo', { + algorithm, + subjectPublicKey +}). + +-record('CertificationRequestInfo_subjectPKInfo_algorithm', { + algorithm, + parameters = asn1_NOVALUE +}). + +-record('CertificationRequestInfo_attributes_SETOF', { + type, + values +}). + +-record('CertificationRequest', { + certificationRequestInfo, + signatureAlgorithm, + signature +}). + +-record('CertificationRequest_signatureAlgorithm', { + algorithm, + parameters = asn1_NOVALUE +}). + +-endif. %% _PKCS_10_HRL_ diff --git a/src/PKIXAlgs-2009.erl b/src/PKIXAlgs-2009.erl new file mode 100644 index 0000000..6eb37d9 --- /dev/null +++ b/src/PKIXAlgs-2009.erl @@ -0,0 +1,2113 @@ +%% Generated by the Erlang ASN.1 BER compiler. Version: 5.1 +%% Purpose: Encoding and decoding of the types in PKIXAlgs-2009. + +-module('PKIXAlgs-2009'). +-compile(nowarn_unused_vars). +-dialyzer(no_improper_lists). +-dialyzer(no_match). +-include("PKIXAlgs-2009.hrl"). +-asn1_info([{vsn,'5.1'}, + {module,'PKIXAlgs-2009'}, + {options,[warnings,ber,errors, + {cwd,"/Users/maxim/depot/synrc/ca/priv/csr"}, + {outdir,"/Users/maxim/depot/synrc/ca/priv/csr"}, + {i,"."}, + {i,"/Users/maxim/depot/synrc/ca/priv/csr"}]}]). + +-export([encoding_rule/0,maps/0,bit_string_format/0, + legacy_erlang_types/0]). +-export(['dialyzer-suppressions'/1]). +-export([ +enc_RSAPublicKey/2, +'enc_DSA-Params'/2, +enc_DSAPublicKey/2, +enc_DomainParameters/2, +enc_ValidationParams/2, +enc_DiffieHellmanPublicNumber/2, +enc_DHPublicKey/2, +'enc_KEA-Params-Id'/2, +enc_ECPoint/2, +enc_ECParameters/2, +'enc_DSA-Sig-Value'/2, +'enc_ECDSA-Sig-Value'/2 +]). + +-export([ +dec_RSAPublicKey/2, +'dec_DSA-Params'/2, +dec_DSAPublicKey/2, +dec_DomainParameters/2, +dec_ValidationParams/2, +dec_DiffieHellmanPublicNumber/2, +dec_DHPublicKey/2, +'dec_KEA-Params-Id'/2, +dec_ECPoint/2, +dec_ECParameters/2, +'dec_DSA-Sig-Value'/2, +'dec_ECDSA-Sig-Value'/2 +]). + +-export([ +'enc_pk-rsa'/3, +'enc_pk-dsa'/3, +'enc_pk-dh'/3, +'enc_pk-kea'/3, +'enc_pk-ec'/3, +'enc_pk-ecDH'/3, +'enc_pk-ecMQV'/3, +'enc_sa-rsaWithMD2'/3, +'enc_sa-rsaWithMD5'/3, +'enc_sa-rsaWithSHA1'/3, +'enc_sa-dsaWithSHA1'/3, +'enc_sa-dsaWithSHA224'/3, +'enc_sa-dsaWithSHA256'/3, +'enc_sa-ecdsaWithSHA1'/3, +'enc_sa-ecdsaWithSHA224'/3, +'enc_sa-ecdsaWithSHA256'/3, +'enc_sa-ecdsaWithSHA384'/3, +'enc_sa-ecdsaWithSHA512'/3, +'enc_mda-md2'/3, +'enc_mda-md5'/3, +'enc_mda-sha1'/3, +'enc_sa-ecdsaWithSHA512_smimeCaps'/3, +'enc_sa-ecdsaWithSHA384_smimeCaps'/3, +'enc_sa-ecdsaWithSHA256_smimeCaps'/3, +'enc_sa-ecdsaWithSHA224_smimeCaps'/3, +'enc_sa-dsaWithSHA1_smimeCaps'/3, +'enc_sa-rsaWithSHA1_smimeCaps'/3, +'enc_sa-dsaWithSHA256_smimeCaps'/3, +'enc_sa-dsaWithSHA224_smimeCaps'/3, +'enc_sa-ecdsaWithSHA1_smimeCaps'/3, +'enc_sa-rsaWithMD5_smimeCaps'/3, +'enc_sa-rsaWithMD2_smimeCaps'/3 +]). + +-export([ +'dec_pk-rsa'/3, +'dec_pk-dsa'/3, +'dec_pk-dh'/3, +'dec_pk-kea'/3, +'dec_pk-ec'/3, +'dec_pk-ecDH'/3, +'dec_pk-ecMQV'/3, +'dec_sa-rsaWithMD2'/3, +'dec_sa-rsaWithMD5'/3, +'dec_sa-rsaWithSHA1'/3, +'dec_sa-dsaWithSHA1'/3, +'dec_sa-dsaWithSHA224'/3, +'dec_sa-dsaWithSHA256'/3, +'dec_sa-ecdsaWithSHA1'/3, +'dec_sa-ecdsaWithSHA224'/3, +'dec_sa-ecdsaWithSHA256'/3, +'dec_sa-ecdsaWithSHA384'/3, +'dec_sa-ecdsaWithSHA512'/3, +'dec_mda-md2'/3, +'dec_mda-md5'/3, +'dec_mda-sha1'/3, +'dec_sa-ecdsaWithSHA512_smimeCaps'/3, +'dec_sa-ecdsaWithSHA384_smimeCaps'/3, +'dec_sa-ecdsaWithSHA256_smimeCaps'/3, +'dec_sa-ecdsaWithSHA224_smimeCaps'/3, +'dec_sa-dsaWithSHA1_smimeCaps'/3, +'dec_sa-rsaWithSHA1_smimeCaps'/3, +'dec_sa-dsaWithSHA256_smimeCaps'/3, +'dec_sa-dsaWithSHA224_smimeCaps'/3, +'dec_sa-ecdsaWithSHA1_smimeCaps'/3, +'dec_sa-rsaWithMD5_smimeCaps'/3, +'dec_sa-rsaWithMD2_smimeCaps'/3 +]). + +-export([ +getenc_PublicKeys/1, +getenc_SignatureAlgs/1, +getenc_SMimeCaps/1, +getenc_NamedCurve/1, +getenc_HashAlgs/1 +]). + +-export([ +getdec_PublicKeys/1, +getdec_SignatureAlgs/1, +getdec_SMimeCaps/1, +getdec_NamedCurve/1, +getdec_HashAlgs/1 +]). + +-export([ +rsaEncryption/0, +'id-dsa'/0, +dhpublicnumber/0, +'id-keyExchangeAlgorithm'/0, +'id-ecPublicKey'/0, +'id-ecDH'/0, +'id-ecMQV'/0, +secp192r1/0, +sect163k1/0, +sect163r2/0, +secp224r1/0, +sect233k1/0, +sect233r1/0, +secp256r1/0, +sect283k1/0, +sect283r1/0, +secp384r1/0, +sect409k1/0, +sect409r1/0, +secp521r1/0, +sect571k1/0, +sect571r1/0, +md2WithRSAEncryption/0, +md5WithRSAEncryption/0, +sha1WithRSAEncryption/0, +'dsa-with-sha1'/0, +'dsa-with-sha224'/0, +'dsa-with-sha256'/0, +'ecdsa-with-SHA1'/0, +'ecdsa-with-SHA224'/0, +'ecdsa-with-SHA256'/0, +'ecdsa-with-SHA384'/0, +'ecdsa-with-SHA512'/0, +'id-md2'/0, +'id-md5'/0, +'id-sha1'/0 +]). + +-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('RSAPublicKey', Data) -> enc_RSAPublicKey(Data); +encode_disp('DSA-Params', Data) -> 'enc_DSA-Params'(Data); +encode_disp('DSAPublicKey', Data) -> enc_DSAPublicKey(Data); +encode_disp('DomainParameters', Data) -> enc_DomainParameters(Data); +encode_disp('ValidationParams', Data) -> enc_ValidationParams(Data); +encode_disp('DiffieHellmanPublicNumber', Data) -> enc_DiffieHellmanPublicNumber(Data); +encode_disp('DHPublicKey', Data) -> enc_DHPublicKey(Data); +encode_disp('KEA-Params-Id', Data) -> 'enc_KEA-Params-Id'(Data); +encode_disp('ECPoint', Data) -> enc_ECPoint(Data); +encode_disp('ECParameters', Data) -> enc_ECParameters(Data); +encode_disp('DSA-Sig-Value', Data) -> 'enc_DSA-Sig-Value'(Data); +encode_disp('ECDSA-Sig-Value', Data) -> 'enc_ECDSA-Sig-Value'(Data); +encode_disp(Type, _Data) -> exit({error,{asn1,{undefined_type,Type}}}). + +decode_disp('RSAPublicKey', Data) -> dec_RSAPublicKey(Data); +decode_disp('DSA-Params', Data) -> 'dec_DSA-Params'(Data); +decode_disp('DSAPublicKey', Data) -> dec_DSAPublicKey(Data); +decode_disp('DomainParameters', Data) -> dec_DomainParameters(Data); +decode_disp('ValidationParams', Data) -> dec_ValidationParams(Data); +decode_disp('DiffieHellmanPublicNumber', Data) -> dec_DiffieHellmanPublicNumber(Data); +decode_disp('DHPublicKey', Data) -> dec_DHPublicKey(Data); +decode_disp('KEA-Params-Id', Data) -> 'dec_KEA-Params-Id'(Data); +decode_disp('ECPoint', Data) -> dec_ECPoint(Data); +decode_disp('ECParameters', Data) -> dec_ECParameters(Data); +decode_disp('DSA-Sig-Value', Data) -> 'dec_DSA-Sig-Value'(Data); +decode_disp('ECDSA-Sig-Value', Data) -> 'dec_ECDSA-Sig-Value'(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. + + +%%================================ +%% RSAPublicKey +%%================================ +enc_RSAPublicKey(Val) -> + enc_RSAPublicKey(Val, [<<48>>]). + +enc_RSAPublicKey(Val, TagIn) -> +{_,Cindex1,Cindex2} = Val, + +%%------------------------------------------------- +%% attribute modulus(1) with type INTEGER +%%------------------------------------------------- + {EncBytes1,EncLen1} = encode_integer(Cindex1, [<<2>>]), + +%%------------------------------------------------- +%% attribute publicExponent(2) with type INTEGER +%%------------------------------------------------- + {EncBytes2,EncLen2} = encode_integer(Cindex2, [<<2>>]), + + BytesSoFar = [EncBytes1, EncBytes2], +LenSoFar = EncLen1 + EncLen2, +encode_tags(TagIn, BytesSoFar, LenSoFar). + + +dec_RSAPublicKey(Tlv) -> + dec_RSAPublicKey(Tlv, [16]). + +dec_RSAPublicKey(Tlv, TagIn) -> + %%------------------------------------------------- + %% decode tag and length + %%------------------------------------------------- +Tlv1 = match_tags(Tlv, TagIn), + +%%------------------------------------------------- +%% attribute modulus(1) with type INTEGER +%%------------------------------------------------- +[V1|Tlv2] = Tlv1, +Term1 = decode_integer(V1, [2]), + +%%------------------------------------------------- +%% attribute publicExponent(2) with type INTEGER +%%------------------------------------------------- +[V2|Tlv3] = Tlv2, +Term2 = decode_integer(V2, [2]), + +case Tlv3 of +[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed +end, +Res1 = {'RSAPublicKey',Term1,Term2}, +Res1. + + +%%================================ +%% DSA-Params +%%================================ +'enc_DSA-Params'(Val) -> + 'enc_DSA-Params'(Val, [<<48>>]). + +'enc_DSA-Params'(Val, TagIn) -> +{_,Cindex1,Cindex2,Cindex3} = Val, + +%%------------------------------------------------- +%% attribute p(1) with type INTEGER +%%------------------------------------------------- + {EncBytes1,EncLen1} = encode_integer(Cindex1, [<<2>>]), + +%%------------------------------------------------- +%% attribute q(2) with type INTEGER +%%------------------------------------------------- + {EncBytes2,EncLen2} = encode_integer(Cindex2, [<<2>>]), + +%%------------------------------------------------- +%% attribute g(3) with type INTEGER +%%------------------------------------------------- + {EncBytes3,EncLen3} = encode_integer(Cindex3, [<<2>>]), + + BytesSoFar = [EncBytes1, EncBytes2, EncBytes3], +LenSoFar = EncLen1 + EncLen2 + EncLen3, +encode_tags(TagIn, BytesSoFar, LenSoFar). + + +'dec_DSA-Params'(Tlv) -> + 'dec_DSA-Params'(Tlv, [16]). + +'dec_DSA-Params'(Tlv, TagIn) -> + %%------------------------------------------------- + %% decode tag and length + %%------------------------------------------------- +Tlv1 = match_tags(Tlv, TagIn), + +%%------------------------------------------------- +%% attribute p(1) with type INTEGER +%%------------------------------------------------- +[V1|Tlv2] = Tlv1, +Term1 = decode_integer(V1, [2]), + +%%------------------------------------------------- +%% attribute q(2) with type INTEGER +%%------------------------------------------------- +[V2|Tlv3] = Tlv2, +Term2 = decode_integer(V2, [2]), + +%%------------------------------------------------- +%% attribute g(3) with type INTEGER +%%------------------------------------------------- +[V3|Tlv4] = Tlv3, +Term3 = decode_integer(V3, [2]), + +case Tlv4 of +[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv4}}}) % extra fields not allowed +end, +Res1 = {'DSA-Params',Term1,Term2,Term3}, +Res1. + + +%%================================ +%% DSAPublicKey +%%================================ +enc_DSAPublicKey(Val) -> + enc_DSAPublicKey(Val, [<<2>>]). + +enc_DSAPublicKey(Val, TagIn) -> +encode_integer(Val, TagIn). + + +dec_DSAPublicKey(Tlv) -> + dec_DSAPublicKey(Tlv, [2]). + +dec_DSAPublicKey(Tlv, TagIn) -> +decode_integer(Tlv, TagIn). + + + +%%================================ +%% DomainParameters +%%================================ +enc_DomainParameters(Val) -> + enc_DomainParameters(Val, [<<48>>]). + +enc_DomainParameters(Val, TagIn) -> +{_,Cindex1,Cindex2,Cindex3,Cindex4,Cindex5} = Val, + +%%------------------------------------------------- +%% attribute p(1) with type INTEGER +%%------------------------------------------------- + {EncBytes1,EncLen1} = encode_integer(Cindex1, [<<2>>]), + +%%------------------------------------------------- +%% attribute g(2) with type INTEGER +%%------------------------------------------------- + {EncBytes2,EncLen2} = encode_integer(Cindex2, [<<2>>]), + +%%------------------------------------------------- +%% attribute q(3) with type INTEGER +%%------------------------------------------------- + {EncBytes3,EncLen3} = encode_integer(Cindex3, [<<2>>]), + +%%------------------------------------------------- +%% attribute j(4) with type INTEGER OPTIONAL +%%------------------------------------------------- + {EncBytes4,EncLen4} = case Cindex4 of + asn1_NOVALUE -> {<<>>,0}; + _ -> + encode_integer(Cindex4, [<<2>>]) + end, + +%%------------------------------------------------- +%% attribute validationParams(5) External PKIXAlgs-2009:ValidationParams OPTIONAL +%%------------------------------------------------- + {EncBytes5,EncLen5} = case Cindex5 of + asn1_NOVALUE -> {<<>>,0}; + _ -> + 'enc_ValidationParams'(Cindex5, [<<48>>]) + end, + + BytesSoFar = [EncBytes1, EncBytes2, EncBytes3, EncBytes4, EncBytes5], +LenSoFar = EncLen1 + EncLen2 + EncLen3 + EncLen4 + EncLen5, +encode_tags(TagIn, BytesSoFar, LenSoFar). + + +dec_DomainParameters(Tlv) -> + dec_DomainParameters(Tlv, [16]). + +dec_DomainParameters(Tlv, TagIn) -> + %%------------------------------------------------- + %% decode tag and length + %%------------------------------------------------- +Tlv1 = match_tags(Tlv, TagIn), + +%%------------------------------------------------- +%% attribute p(1) with type INTEGER +%%------------------------------------------------- +[V1|Tlv2] = Tlv1, +Term1 = decode_integer(V1, [2]), + +%%------------------------------------------------- +%% attribute g(2) with type INTEGER +%%------------------------------------------------- +[V2|Tlv3] = Tlv2, +Term2 = decode_integer(V2, [2]), + +%%------------------------------------------------- +%% attribute q(3) with type INTEGER +%%------------------------------------------------- +[V3|Tlv4] = Tlv3, +Term3 = decode_integer(V3, [2]), + +%%------------------------------------------------- +%% attribute j(4) with type INTEGER OPTIONAL +%%------------------------------------------------- +{Term4,Tlv5} = case Tlv4 of +[{2,V4}|TempTlv5] -> + {decode_integer(V4, []), TempTlv5}; + _ -> + { asn1_NOVALUE, Tlv4} +end, + +%%------------------------------------------------- +%% attribute validationParams(5) External PKIXAlgs-2009:ValidationParams OPTIONAL +%%------------------------------------------------- +{Term5,Tlv6} = case Tlv5 of +[{16,V5}|TempTlv6] -> + {'dec_ValidationParams'(V5, []), TempTlv6}; + _ -> + { asn1_NOVALUE, Tlv5} +end, + +case Tlv6 of +[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv6}}}) % extra fields not allowed +end, +Res1 = {'DomainParameters',Term1,Term2,Term3,Term4,Term5}, +Res1. + + +%%================================ +%% ValidationParams +%%================================ +enc_ValidationParams(Val) -> + enc_ValidationParams(Val, [<<48>>]). + +enc_ValidationParams(Val, TagIn) -> +{_,Cindex1,Cindex2} = Val, + +%%------------------------------------------------- +%% attribute seed(1) with type BIT STRING +%%------------------------------------------------- + {EncBytes1,EncLen1} = encode_unnamed_bit_string(Cindex1, [<<3>>]), + +%%------------------------------------------------- +%% attribute pgenCounter(2) with type INTEGER +%%------------------------------------------------- + {EncBytes2,EncLen2} = encode_integer(Cindex2, [<<2>>]), + + BytesSoFar = [EncBytes1, EncBytes2], +LenSoFar = EncLen1 + EncLen2, +encode_tags(TagIn, BytesSoFar, LenSoFar). + + +dec_ValidationParams(Tlv) -> + dec_ValidationParams(Tlv, [16]). + +dec_ValidationParams(Tlv, TagIn) -> + %%------------------------------------------------- + %% decode tag and length + %%------------------------------------------------- +Tlv1 = match_tags(Tlv, TagIn), + +%%------------------------------------------------- +%% attribute seed(1) with type BIT STRING +%%------------------------------------------------- +[V1|Tlv2] = Tlv1, +Term1 = decode_native_bit_string(V1, [3]), + +%%------------------------------------------------- +%% attribute pgenCounter(2) with type INTEGER +%%------------------------------------------------- +[V2|Tlv3] = Tlv2, +Term2 = decode_integer(V2, [2]), + +case Tlv3 of +[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed +end, +Res1 = {'ValidationParams',Term1,Term2}, +Res1. + + +%%================================ +%% DiffieHellmanPublicNumber +%%================================ +enc_DiffieHellmanPublicNumber(Val) -> + enc_DiffieHellmanPublicNumber(Val, [<<2>>]). + +enc_DiffieHellmanPublicNumber(Val, TagIn) -> +encode_integer(Val, TagIn). + + +dec_DiffieHellmanPublicNumber(Tlv) -> + dec_DiffieHellmanPublicNumber(Tlv, [2]). + +dec_DiffieHellmanPublicNumber(Tlv, TagIn) -> +decode_integer(Tlv, TagIn). + + + +%%================================ +%% DHPublicKey +%%================================ +enc_DHPublicKey(Val) -> + enc_DHPublicKey(Val, [<<2>>]). + +enc_DHPublicKey(Val, TagIn) -> +encode_integer(Val, TagIn). + + +dec_DHPublicKey(Tlv) -> + dec_DHPublicKey(Tlv, [2]). + +dec_DHPublicKey(Tlv, TagIn) -> +decode_integer(Tlv, TagIn). + + + +%%================================ +%% KEA-Params-Id +%%================================ +'enc_KEA-Params-Id'(Val) -> + 'enc_KEA-Params-Id'(Val, [<<4>>]). + +'enc_KEA-Params-Id'(Val, TagIn) -> +encode_restricted_string(Val, TagIn). + + +'dec_KEA-Params-Id'(Tlv) -> + 'dec_KEA-Params-Id'(Tlv, [4]). + +'dec_KEA-Params-Id'(Tlv, TagIn) -> +decode_octet_string(Tlv, TagIn). + + + +%%================================ +%% ECPoint +%%================================ +enc_ECPoint(Val) -> + enc_ECPoint(Val, [<<4>>]). + +enc_ECPoint(Val, TagIn) -> +encode_restricted_string(Val, TagIn). + + +dec_ECPoint(Tlv) -> + dec_ECPoint(Tlv, [4]). + +dec_ECPoint(Tlv, TagIn) -> +decode_octet_string(Tlv, TagIn). + + + +%%================================ +%% ECParameters +%%================================ +enc_ECParameters(Val) -> + enc_ECParameters(Val, []). + +enc_ECParameters(Val, TagIn) -> + {EncBytes,EncLen} = case element(1,Val) of + namedCurve -> + encode_object_identifier(element(2,Val), [<<6>>]); + Else -> + exit({error,{asn1,{invalid_choice_type,Else}}}) + end, + +encode_tags(TagIn, EncBytes, EncLen). + + + + +dec_ECParameters(Tlv) -> + dec_ECParameters(Tlv, []). + +dec_ECParameters(Tlv, TagIn) -> +Tlv1 = match_tags(Tlv, TagIn), +case (case Tlv1 of [CtempTlv1] -> CtempTlv1; _ -> Tlv1 end) of + +%% 'namedCurve' + {6, V1} -> + {namedCurve, decode_object_identifier(V1, [])}; + + Else -> + exit({error,{asn1,{invalid_choice_tag,Else}}}) + end +. + + +%%================================ +%% DSA-Sig-Value +%%================================ +'enc_DSA-Sig-Value'(Val) -> + 'enc_DSA-Sig-Value'(Val, [<<48>>]). + +'enc_DSA-Sig-Value'(Val, TagIn) -> +{_,Cindex1,Cindex2} = Val, + +%%------------------------------------------------- +%% attribute r(1) with type INTEGER +%%------------------------------------------------- + {EncBytes1,EncLen1} = encode_integer(Cindex1, [<<2>>]), + +%%------------------------------------------------- +%% attribute s(2) with type INTEGER +%%------------------------------------------------- + {EncBytes2,EncLen2} = encode_integer(Cindex2, [<<2>>]), + + BytesSoFar = [EncBytes1, EncBytes2], +LenSoFar = EncLen1 + EncLen2, +encode_tags(TagIn, BytesSoFar, LenSoFar). + + +'dec_DSA-Sig-Value'(Tlv) -> + 'dec_DSA-Sig-Value'(Tlv, [16]). + +'dec_DSA-Sig-Value'(Tlv, TagIn) -> + %%------------------------------------------------- + %% decode tag and length + %%------------------------------------------------- +Tlv1 = match_tags(Tlv, TagIn), + +%%------------------------------------------------- +%% attribute r(1) with type INTEGER +%%------------------------------------------------- +[V1|Tlv2] = Tlv1, +Term1 = decode_integer(V1, [2]), + +%%------------------------------------------------- +%% attribute s(2) with type INTEGER +%%------------------------------------------------- +[V2|Tlv3] = Tlv2, +Term2 = decode_integer(V2, [2]), + +case Tlv3 of +[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed +end, +Res1 = {'DSA-Sig-Value',Term1,Term2}, +Res1. + + +%%================================ +%% ECDSA-Sig-Value +%%================================ +'enc_ECDSA-Sig-Value'(Val) -> + 'enc_ECDSA-Sig-Value'(Val, [<<48>>]). + +'enc_ECDSA-Sig-Value'(Val, TagIn) -> +{_,Cindex1,Cindex2} = Val, + +%%------------------------------------------------- +%% attribute r(1) with type INTEGER +%%------------------------------------------------- + {EncBytes1,EncLen1} = encode_integer(Cindex1, [<<2>>]), + +%%------------------------------------------------- +%% attribute s(2) with type INTEGER +%%------------------------------------------------- + {EncBytes2,EncLen2} = encode_integer(Cindex2, [<<2>>]), + + BytesSoFar = [EncBytes1, EncBytes2], +LenSoFar = EncLen1 + EncLen2, +encode_tags(TagIn, BytesSoFar, LenSoFar). + + +'dec_ECDSA-Sig-Value'(Tlv) -> + 'dec_ECDSA-Sig-Value'(Tlv, [16]). + +'dec_ECDSA-Sig-Value'(Tlv, TagIn) -> + %%------------------------------------------------- + %% decode tag and length + %%------------------------------------------------- +Tlv1 = match_tags(Tlv, TagIn), + +%%------------------------------------------------- +%% attribute r(1) with type INTEGER +%%------------------------------------------------- +[V1|Tlv2] = Tlv1, +Term1 = decode_integer(V1, [2]), + +%%------------------------------------------------- +%% attribute s(2) with type INTEGER +%%------------------------------------------------- +[V2|Tlv3] = Tlv2, +Term2 = decode_integer(V2, [2]), + +case Tlv3 of +[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed +end, +Res1 = {'ECDSA-Sig-Value',Term1,Term2}, +Res1. +rsaEncryption() -> +{1,2,840,113549,1,1,1}. + +'id-dsa'() -> +{1,2,840,10040,4,1}. + +dhpublicnumber() -> +{1,2,840,10046,2,1}. + +'id-keyExchangeAlgorithm'() -> +{2,16,840,1,101,2,1,1,22}. + +'id-ecPublicKey'() -> +{1,2,840,10045,2,1}. + +'id-ecDH'() -> +{1,3,132,1,12}. + +'id-ecMQV'() -> +{1,3,132,1,13}. + +secp192r1() -> +{1,2,840,10045,3,1,1}. + +sect163k1() -> +{1,3,132,0,1}. + +sect163r2() -> +{1,3,132,0,15}. + +secp224r1() -> +{1,3,132,0,33}. + +sect233k1() -> +{1,3,132,0,26}. + +sect233r1() -> +{1,3,132,0,27}. + +secp256r1() -> +{1,2,840,10045,3,1,7}. + +sect283k1() -> +{1,3,132,0,16}. + +sect283r1() -> +{1,3,132,0,17}. + +secp384r1() -> +{1,3,132,0,34}. + +sect409k1() -> +{1,3,132,0,36}. + +sect409r1() -> +{1,3,132,0,37}. + +secp521r1() -> +{1,3,132,0,35}. + +sect571k1() -> +{1,3,132,0,38}. + +sect571r1() -> +{1,3,132,0,39}. + +md2WithRSAEncryption() -> +{1,2,840,113549,1,1,2}. + +md5WithRSAEncryption() -> +{1,2,840,113549,1,1,4}. + +sha1WithRSAEncryption() -> +{1,2,840,113549,1,1,5}. + +'dsa-with-sha1'() -> +{1,2,840,10040,4,3}. + +'dsa-with-sha224'() -> +{2,16,840,1,101,3,4,3,1}. + +'dsa-with-sha256'() -> +{2,16,840,1,101,3,4,3,2}. + +'ecdsa-with-SHA1'() -> +{1,2,840,10045,4,1}. + +'ecdsa-with-SHA224'() -> +{1,2,840,10045,4,3,1}. + +'ecdsa-with-SHA256'() -> +{1,2,840,10045,4,3,2}. + +'ecdsa-with-SHA384'() -> +{1,2,840,10045,4,3,3}. + +'ecdsa-with-SHA512'() -> +{1,2,840,10045,4,3,4}. + +'id-md2'() -> +{1,2,840,113549,2,2}. + +'id-md5'() -> +{1,2,840,113549,2,5}. + +'id-sha1'() -> +{1,3,14,3,2,26}. + + + + +%%================================ +%% pk-rsa +%%================================ +'enc_pk-rsa'('KeyValue', Val, _RestPrimFieldName) -> + enc_RSAPublicKey(Val, [<<48>>]); +'enc_pk-rsa'('Params', Val, _RestPrimFieldName) -> +encode_null(Val, [<<5>>]); +'enc_pk-rsa'('PrivateKey', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_pk-rsa'('KeyValue', Bytes,_) -> + Tlv = tlv_format(Bytes), + dec_RSAPublicKey(Tlv, [16]); +'dec_pk-rsa'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), +decode_null(Tlv, [5]); +'dec_pk-rsa'('PrivateKey', Bytes,_) -> + Bytes. + +tlv_format(Bytes) when is_binary(Bytes) -> + {Tlv,_} = ber_decode_nif(Bytes), + Tlv; +tlv_format(Bytes) -> + Bytes. + + + +%%================================ +%% pk-dsa +%%================================ +'enc_pk-dsa'('KeyValue', Val, _RestPrimFieldName) -> + enc_DSAPublicKey(Val, [<<2>>]); +'enc_pk-dsa'('Params', Val, _RestPrimFieldName) -> + 'enc_DSA-Params'(Val, [<<48>>]); +'enc_pk-dsa'('PrivateKey', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_pk-dsa'('KeyValue', Bytes,_) -> + Tlv = tlv_format(Bytes), + dec_DSAPublicKey(Tlv, [2]); +'dec_pk-dsa'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), + 'dec_DSA-Params'(Tlv, [16]); +'dec_pk-dsa'('PrivateKey', Bytes,_) -> + Bytes. + + + + +%%================================ +%% pk-dh +%%================================ +'enc_pk-dh'('KeyValue', Val, _RestPrimFieldName) -> + enc_DHPublicKey(Val, [<<2>>]); +'enc_pk-dh'('Params', Val, _RestPrimFieldName) -> + enc_DomainParameters(Val, [<<48>>]); +'enc_pk-dh'('PrivateKey', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_pk-dh'('KeyValue', Bytes,_) -> + Tlv = tlv_format(Bytes), + dec_DHPublicKey(Tlv, [2]); +'dec_pk-dh'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), + dec_DomainParameters(Tlv, [16]); +'dec_pk-dh'('PrivateKey', Bytes,_) -> + Bytes. + + + + +%%================================ +%% pk-kea +%%================================ +'enc_pk-kea'('KeyValue', Val, _RestPrimFieldName) -> + {Val,0}; +'enc_pk-kea'('Params', Val, _RestPrimFieldName) -> + 'enc_KEA-Params-Id'(Val, [<<4>>]); +'enc_pk-kea'('PrivateKey', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_pk-kea'('KeyValue', Bytes,_) -> + Bytes; +'dec_pk-kea'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), + 'dec_KEA-Params-Id'(Tlv, [4]); +'dec_pk-kea'('PrivateKey', Bytes,_) -> + Bytes. + + + + +%%================================ +%% pk-ec +%%================================ +'enc_pk-ec'('KeyValue', Val, _RestPrimFieldName) -> + enc_ECPoint(Val, [<<4>>]); +'enc_pk-ec'('Params', Val, _RestPrimFieldName) -> + enc_ECParameters(Val, []); +'enc_pk-ec'('PrivateKey', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_pk-ec'('KeyValue', Bytes,_) -> + Tlv = tlv_format(Bytes), + dec_ECPoint(Tlv, [4]); +'dec_pk-ec'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), + dec_ECParameters(Tlv, []); +'dec_pk-ec'('PrivateKey', Bytes,_) -> + Bytes. + + + + +%%================================ +%% pk-ecDH +%%================================ +'enc_pk-ecDH'('KeyValue', Val, _RestPrimFieldName) -> + enc_ECPoint(Val, [<<4>>]); +'enc_pk-ecDH'('Params', Val, _RestPrimFieldName) -> + enc_ECParameters(Val, []); +'enc_pk-ecDH'('PrivateKey', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_pk-ecDH'('KeyValue', Bytes,_) -> + Tlv = tlv_format(Bytes), + dec_ECPoint(Tlv, [4]); +'dec_pk-ecDH'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), + dec_ECParameters(Tlv, []); +'dec_pk-ecDH'('PrivateKey', Bytes,_) -> + Bytes. + + + + +%%================================ +%% pk-ecMQV +%%================================ +'enc_pk-ecMQV'('KeyValue', Val, _RestPrimFieldName) -> + enc_ECPoint(Val, [<<4>>]); +'enc_pk-ecMQV'('Params', Val, _RestPrimFieldName) -> + enc_ECParameters(Val, []); +'enc_pk-ecMQV'('PrivateKey', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_pk-ecMQV'('KeyValue', Bytes,_) -> + Tlv = tlv_format(Bytes), + dec_ECPoint(Tlv, [4]); +'dec_pk-ecMQV'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), + dec_ECParameters(Tlv, []); +'dec_pk-ecMQV'('PrivateKey', Bytes,_) -> + Bytes. + + + + +%%================================ +%% sa-rsaWithMD2 +%%================================ +'enc_sa-rsaWithMD2'('Value', Val, _RestPrimFieldName) -> + {Val,0}; +'enc_sa-rsaWithMD2'('Params', Val, _RestPrimFieldName) -> +encode_null(Val, [<<5>>]); +'enc_sa-rsaWithMD2'(smimeCaps, Val, [H|T]) -> + 'enc_sa-rsaWithMD2_smimeCaps'(H, Val, T). + + +'dec_sa-rsaWithMD2'('Value', Bytes,_) -> + Bytes; +'dec_sa-rsaWithMD2'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), +decode_null(Tlv, [5]); +'dec_sa-rsaWithMD2'(smimeCaps, Bytes,[H|T]) -> + 'dec_sa-rsaWithMD2_smimeCaps'(H, Bytes, T). + + + + +%%================================ +%% sa-rsaWithMD5 +%%================================ +'enc_sa-rsaWithMD5'('Value', Val, _RestPrimFieldName) -> + {Val,0}; +'enc_sa-rsaWithMD5'('Params', Val, _RestPrimFieldName) -> +encode_null(Val, [<<5>>]); +'enc_sa-rsaWithMD5'(smimeCaps, Val, [H|T]) -> + 'enc_sa-rsaWithMD5_smimeCaps'(H, Val, T). + + +'dec_sa-rsaWithMD5'('Value', Bytes,_) -> + Bytes; +'dec_sa-rsaWithMD5'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), +decode_null(Tlv, [5]); +'dec_sa-rsaWithMD5'(smimeCaps, Bytes,[H|T]) -> + 'dec_sa-rsaWithMD5_smimeCaps'(H, Bytes, T). + + + + +%%================================ +%% sa-rsaWithSHA1 +%%================================ +'enc_sa-rsaWithSHA1'('Value', Val, _RestPrimFieldName) -> + {Val,0}; +'enc_sa-rsaWithSHA1'('Params', Val, _RestPrimFieldName) -> +encode_null(Val, [<<5>>]); +'enc_sa-rsaWithSHA1'(smimeCaps, Val, [H|T]) -> + 'enc_sa-rsaWithSHA1_smimeCaps'(H, Val, T). + + +'dec_sa-rsaWithSHA1'('Value', Bytes,_) -> + Bytes; +'dec_sa-rsaWithSHA1'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), +decode_null(Tlv, [5]); +'dec_sa-rsaWithSHA1'(smimeCaps, Bytes,[H|T]) -> + 'dec_sa-rsaWithSHA1_smimeCaps'(H, Bytes, T). + + + + +%%================================ +%% sa-dsaWithSHA1 +%%================================ +'enc_sa-dsaWithSHA1'('Value', Val, _RestPrimFieldName) -> + 'enc_DSA-Sig-Value'(Val, [<<48>>]); +'enc_sa-dsaWithSHA1'('Params', Val, _RestPrimFieldName) -> +encode_null(Val, [<<5>>]); +'enc_sa-dsaWithSHA1'(smimeCaps, Val, [H|T]) -> + 'enc_sa-dsaWithSHA1_smimeCaps'(H, Val, T). + + +'dec_sa-dsaWithSHA1'('Value', Bytes,_) -> + Tlv = tlv_format(Bytes), + 'dec_DSA-Sig-Value'(Tlv, [16]); +'dec_sa-dsaWithSHA1'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), +decode_null(Tlv, [5]); +'dec_sa-dsaWithSHA1'(smimeCaps, Bytes,[H|T]) -> + 'dec_sa-dsaWithSHA1_smimeCaps'(H, Bytes, T). + + + + +%%================================ +%% sa-dsaWithSHA224 +%%================================ +'enc_sa-dsaWithSHA224'('Value', Val, _RestPrimFieldName) -> + 'enc_DSA-Sig-Value'(Val, [<<48>>]); +'enc_sa-dsaWithSHA224'('Params', Val, _RestPrimFieldName) -> +encode_null(Val, [<<5>>]); +'enc_sa-dsaWithSHA224'(smimeCaps, Val, [H|T]) -> + 'enc_sa-dsaWithSHA224_smimeCaps'(H, Val, T). + + +'dec_sa-dsaWithSHA224'('Value', Bytes,_) -> + Tlv = tlv_format(Bytes), + 'dec_DSA-Sig-Value'(Tlv, [16]); +'dec_sa-dsaWithSHA224'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), +decode_null(Tlv, [5]); +'dec_sa-dsaWithSHA224'(smimeCaps, Bytes,[H|T]) -> + 'dec_sa-dsaWithSHA224_smimeCaps'(H, Bytes, T). + + + + +%%================================ +%% sa-dsaWithSHA256 +%%================================ +'enc_sa-dsaWithSHA256'('Value', Val, _RestPrimFieldName) -> + 'enc_DSA-Sig-Value'(Val, [<<48>>]); +'enc_sa-dsaWithSHA256'('Params', Val, _RestPrimFieldName) -> +encode_null(Val, [<<5>>]); +'enc_sa-dsaWithSHA256'(smimeCaps, Val, [H|T]) -> + 'enc_sa-dsaWithSHA256_smimeCaps'(H, Val, T). + + +'dec_sa-dsaWithSHA256'('Value', Bytes,_) -> + Tlv = tlv_format(Bytes), + 'dec_DSA-Sig-Value'(Tlv, [16]); +'dec_sa-dsaWithSHA256'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), +decode_null(Tlv, [5]); +'dec_sa-dsaWithSHA256'(smimeCaps, Bytes,[H|T]) -> + 'dec_sa-dsaWithSHA256_smimeCaps'(H, Bytes, T). + + + + +%%================================ +%% sa-ecdsaWithSHA1 +%%================================ +'enc_sa-ecdsaWithSHA1'('Value', Val, _RestPrimFieldName) -> + 'enc_ECDSA-Sig-Value'(Val, [<<48>>]); +'enc_sa-ecdsaWithSHA1'('Params', Val, _RestPrimFieldName) -> +encode_null(Val, [<<5>>]); +'enc_sa-ecdsaWithSHA1'(smimeCaps, Val, [H|T]) -> + 'enc_sa-ecdsaWithSHA1_smimeCaps'(H, Val, T). + + +'dec_sa-ecdsaWithSHA1'('Value', Bytes,_) -> + Tlv = tlv_format(Bytes), + 'dec_ECDSA-Sig-Value'(Tlv, [16]); +'dec_sa-ecdsaWithSHA1'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), +decode_null(Tlv, [5]); +'dec_sa-ecdsaWithSHA1'(smimeCaps, Bytes,[H|T]) -> + 'dec_sa-ecdsaWithSHA1_smimeCaps'(H, Bytes, T). + + + + +%%================================ +%% sa-ecdsaWithSHA224 +%%================================ +'enc_sa-ecdsaWithSHA224'('Value', Val, _RestPrimFieldName) -> + 'enc_ECDSA-Sig-Value'(Val, [<<48>>]); +'enc_sa-ecdsaWithSHA224'('Params', Val, _RestPrimFieldName) -> +encode_null(Val, [<<5>>]); +'enc_sa-ecdsaWithSHA224'(smimeCaps, Val, [H|T]) -> + 'enc_sa-ecdsaWithSHA224_smimeCaps'(H, Val, T). + + +'dec_sa-ecdsaWithSHA224'('Value', Bytes,_) -> + Tlv = tlv_format(Bytes), + 'dec_ECDSA-Sig-Value'(Tlv, [16]); +'dec_sa-ecdsaWithSHA224'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), +decode_null(Tlv, [5]); +'dec_sa-ecdsaWithSHA224'(smimeCaps, Bytes,[H|T]) -> + 'dec_sa-ecdsaWithSHA224_smimeCaps'(H, Bytes, T). + + + + +%%================================ +%% sa-ecdsaWithSHA256 +%%================================ +'enc_sa-ecdsaWithSHA256'('Value', Val, _RestPrimFieldName) -> + 'enc_ECDSA-Sig-Value'(Val, [<<48>>]); +'enc_sa-ecdsaWithSHA256'('Params', Val, _RestPrimFieldName) -> +encode_null(Val, [<<5>>]); +'enc_sa-ecdsaWithSHA256'(smimeCaps, Val, [H|T]) -> + 'enc_sa-ecdsaWithSHA256_smimeCaps'(H, Val, T). + + +'dec_sa-ecdsaWithSHA256'('Value', Bytes,_) -> + Tlv = tlv_format(Bytes), + 'dec_ECDSA-Sig-Value'(Tlv, [16]); +'dec_sa-ecdsaWithSHA256'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), +decode_null(Tlv, [5]); +'dec_sa-ecdsaWithSHA256'(smimeCaps, Bytes,[H|T]) -> + 'dec_sa-ecdsaWithSHA256_smimeCaps'(H, Bytes, T). + + + + +%%================================ +%% sa-ecdsaWithSHA384 +%%================================ +'enc_sa-ecdsaWithSHA384'('Value', Val, _RestPrimFieldName) -> + 'enc_ECDSA-Sig-Value'(Val, [<<48>>]); +'enc_sa-ecdsaWithSHA384'('Params', Val, _RestPrimFieldName) -> +encode_null(Val, [<<5>>]); +'enc_sa-ecdsaWithSHA384'(smimeCaps, Val, [H|T]) -> + 'enc_sa-ecdsaWithSHA384_smimeCaps'(H, Val, T). + + +'dec_sa-ecdsaWithSHA384'('Value', Bytes,_) -> + Tlv = tlv_format(Bytes), + 'dec_ECDSA-Sig-Value'(Tlv, [16]); +'dec_sa-ecdsaWithSHA384'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), +decode_null(Tlv, [5]); +'dec_sa-ecdsaWithSHA384'(smimeCaps, Bytes,[H|T]) -> + 'dec_sa-ecdsaWithSHA384_smimeCaps'(H, Bytes, T). + + + + +%%================================ +%% sa-ecdsaWithSHA512 +%%================================ +'enc_sa-ecdsaWithSHA512'('Value', Val, _RestPrimFieldName) -> + 'enc_ECDSA-Sig-Value'(Val, [<<48>>]); +'enc_sa-ecdsaWithSHA512'('Params', Val, _RestPrimFieldName) -> +encode_null(Val, [<<5>>]); +'enc_sa-ecdsaWithSHA512'(smimeCaps, Val, [H|T]) -> + 'enc_sa-ecdsaWithSHA512_smimeCaps'(H, Val, T). + + +'dec_sa-ecdsaWithSHA512'('Value', Bytes,_) -> + Tlv = tlv_format(Bytes), + 'dec_ECDSA-Sig-Value'(Tlv, [16]); +'dec_sa-ecdsaWithSHA512'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), +decode_null(Tlv, [5]); +'dec_sa-ecdsaWithSHA512'(smimeCaps, Bytes,[H|T]) -> + 'dec_sa-ecdsaWithSHA512_smimeCaps'(H, Bytes, T). + + + + +%%================================ +%% mda-md2 +%%================================ +'enc_mda-md2'('Params', Val, _RestPrimFieldName) -> +encode_null(Val, [<<5>>]). + + +'dec_mda-md2'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), +decode_null(Tlv, [5]). + + + + +%%================================ +%% mda-md5 +%%================================ +'enc_mda-md5'('Params', Val, _RestPrimFieldName) -> +encode_null(Val, [<<5>>]). + + +'dec_mda-md5'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), +decode_null(Tlv, [5]). + + + + +%%================================ +%% mda-sha1 +%%================================ +'enc_mda-sha1'('Params', Val, _RestPrimFieldName) -> +encode_null(Val, [<<5>>]). + + +'dec_mda-sha1'('Params', Bytes,_) -> + Tlv = tlv_format(Bytes), +decode_null(Tlv, [5]). + + + + +%%================================ +%% sa-ecdsaWithSHA512_smimeCaps +%%================================ +'enc_sa-ecdsaWithSHA512_smimeCaps'('Type', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_sa-ecdsaWithSHA512_smimeCaps'('Type', Bytes,_) -> + Bytes. + + + + +%%================================ +%% sa-ecdsaWithSHA384_smimeCaps +%%================================ +'enc_sa-ecdsaWithSHA384_smimeCaps'('Type', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_sa-ecdsaWithSHA384_smimeCaps'('Type', Bytes,_) -> + Bytes. + + + + +%%================================ +%% sa-ecdsaWithSHA256_smimeCaps +%%================================ +'enc_sa-ecdsaWithSHA256_smimeCaps'('Type', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_sa-ecdsaWithSHA256_smimeCaps'('Type', Bytes,_) -> + Bytes. + + + + +%%================================ +%% sa-ecdsaWithSHA224_smimeCaps +%%================================ +'enc_sa-ecdsaWithSHA224_smimeCaps'('Type', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_sa-ecdsaWithSHA224_smimeCaps'('Type', Bytes,_) -> + Bytes. + + + + +%%================================ +%% sa-dsaWithSHA1_smimeCaps +%%================================ +'enc_sa-dsaWithSHA1_smimeCaps'('Type', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_sa-dsaWithSHA1_smimeCaps'('Type', Bytes,_) -> + Bytes. + + + + +%%================================ +%% sa-rsaWithSHA1_smimeCaps +%%================================ +'enc_sa-rsaWithSHA1_smimeCaps'('Type', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_sa-rsaWithSHA1_smimeCaps'('Type', Bytes,_) -> + Bytes. + + + + +%%================================ +%% sa-dsaWithSHA256_smimeCaps +%%================================ +'enc_sa-dsaWithSHA256_smimeCaps'('Type', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_sa-dsaWithSHA256_smimeCaps'('Type', Bytes,_) -> + Bytes. + + + + +%%================================ +%% sa-dsaWithSHA224_smimeCaps +%%================================ +'enc_sa-dsaWithSHA224_smimeCaps'('Type', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_sa-dsaWithSHA224_smimeCaps'('Type', Bytes,_) -> + Bytes. + + + + +%%================================ +%% sa-ecdsaWithSHA1_smimeCaps +%%================================ +'enc_sa-ecdsaWithSHA1_smimeCaps'('Type', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_sa-ecdsaWithSHA1_smimeCaps'('Type', Bytes,_) -> + Bytes. + + + + +%%================================ +%% sa-rsaWithMD5_smimeCaps +%%================================ +'enc_sa-rsaWithMD5_smimeCaps'('Type', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_sa-rsaWithMD5_smimeCaps'('Type', Bytes,_) -> + Bytes. + + + + +%%================================ +%% sa-rsaWithMD2_smimeCaps +%%================================ +'enc_sa-rsaWithMD2_smimeCaps'('Type', Val, _RestPrimFieldName) -> + {Val,0}. + + +'dec_sa-rsaWithMD2_smimeCaps'('Type', Bytes,_) -> + Bytes. + + + + +%%================================ +%% PublicKeys +%%================================ +getenc_PublicKeys(Id) when Id =:= {1,3,132,1,12} -> + fun 'enc_pk-ecDH'/3; +getenc_PublicKeys(Id) when Id =:= {1,3,132,1,13} -> + fun 'enc_pk-ecMQV'/3; +getenc_PublicKeys(Id) when Id =:= {1,2,840,10040,4,1} -> + fun 'enc_pk-dsa'/3; +getenc_PublicKeys(Id) when Id =:= {1,2,840,10045,2,1} -> + fun 'enc_pk-ec'/3; +getenc_PublicKeys(Id) when Id =:= {1,2,840,10046,2,1} -> + fun 'enc_pk-dh'/3; +getenc_PublicKeys(Id) when Id =:= {1,2,840,113549,1,1,1} -> + fun 'enc_pk-rsa'/3; +getenc_PublicKeys(Id) when Id =:= {2,16,840,1,101,2,1,1,22} -> + fun 'enc_pk-kea'/3; +getenc_PublicKeys(_) -> + fun(_, Val, _RestPrimFieldName) -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end. + +getdec_PublicKeys(Id) when Id =:= {1,3,132,1,12} -> + fun 'dec_pk-ecDH'/3; +getdec_PublicKeys(Id) when Id =:= {1,3,132,1,13} -> + fun 'dec_pk-ecMQV'/3; +getdec_PublicKeys(Id) when Id =:= {1,2,840,10040,4,1} -> + fun 'dec_pk-dsa'/3; +getdec_PublicKeys(Id) when Id =:= {1,2,840,10045,2,1} -> + fun 'dec_pk-ec'/3; +getdec_PublicKeys(Id) when Id =:= {1,2,840,10046,2,1} -> + fun 'dec_pk-dh'/3; +getdec_PublicKeys(Id) when Id =:= {1,2,840,113549,1,1,1} -> + fun 'dec_pk-rsa'/3; +getdec_PublicKeys(Id) when Id =:= {2,16,840,1,101,2,1,1,22} -> + fun 'dec_pk-kea'/3; +getdec_PublicKeys(_) -> + fun(_,Bytes, _RestPrimFieldName) -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end. + + + + + +%%================================ +%% SignatureAlgs +%%================================ +getenc_SignatureAlgs(Id) when Id =:= {1,2,840,10040,4,3} -> + fun 'enc_sa-dsaWithSHA1'/3; +getenc_SignatureAlgs(Id) when Id =:= {1,2,840,10045,4,1} -> + fun 'enc_sa-ecdsaWithSHA1'/3; +getenc_SignatureAlgs(Id) when Id =:= {1,2,840,10045,4,3,1} -> + fun 'enc_sa-ecdsaWithSHA224'/3; +getenc_SignatureAlgs(Id) when Id =:= {1,2,840,10045,4,3,2} -> + fun 'enc_sa-ecdsaWithSHA256'/3; +getenc_SignatureAlgs(Id) when Id =:= {1,2,840,10045,4,3,3} -> + fun 'enc_sa-ecdsaWithSHA384'/3; +getenc_SignatureAlgs(Id) when Id =:= {1,2,840,10045,4,3,4} -> + fun 'enc_sa-ecdsaWithSHA512'/3; +getenc_SignatureAlgs(Id) when Id =:= {1,2,840,113549,1,1,2} -> + fun 'enc_sa-rsaWithMD2'/3; +getenc_SignatureAlgs(Id) when Id =:= {1,2,840,113549,1,1,4} -> + fun 'enc_sa-rsaWithMD5'/3; +getenc_SignatureAlgs(Id) when Id =:= {1,2,840,113549,1,1,5} -> + fun 'enc_sa-rsaWithSHA1'/3; +getenc_SignatureAlgs(Id) when Id =:= {2,16,840,1,101,3,4,3,1} -> + fun 'enc_sa-dsaWithSHA224'/3; +getenc_SignatureAlgs(Id) when Id =:= {2,16,840,1,101,3,4,3,2} -> + fun 'enc_sa-dsaWithSHA256'/3; +getenc_SignatureAlgs(_) -> + fun(_, Val, _RestPrimFieldName) -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end. + +getdec_SignatureAlgs(Id) when Id =:= {1,2,840,10040,4,3} -> + fun 'dec_sa-dsaWithSHA1'/3; +getdec_SignatureAlgs(Id) when Id =:= {1,2,840,10045,4,1} -> + fun 'dec_sa-ecdsaWithSHA1'/3; +getdec_SignatureAlgs(Id) when Id =:= {1,2,840,10045,4,3,1} -> + fun 'dec_sa-ecdsaWithSHA224'/3; +getdec_SignatureAlgs(Id) when Id =:= {1,2,840,10045,4,3,2} -> + fun 'dec_sa-ecdsaWithSHA256'/3; +getdec_SignatureAlgs(Id) when Id =:= {1,2,840,10045,4,3,3} -> + fun 'dec_sa-ecdsaWithSHA384'/3; +getdec_SignatureAlgs(Id) when Id =:= {1,2,840,10045,4,3,4} -> + fun 'dec_sa-ecdsaWithSHA512'/3; +getdec_SignatureAlgs(Id) when Id =:= {1,2,840,113549,1,1,2} -> + fun 'dec_sa-rsaWithMD2'/3; +getdec_SignatureAlgs(Id) when Id =:= {1,2,840,113549,1,1,4} -> + fun 'dec_sa-rsaWithMD5'/3; +getdec_SignatureAlgs(Id) when Id =:= {1,2,840,113549,1,1,5} -> + fun 'dec_sa-rsaWithSHA1'/3; +getdec_SignatureAlgs(Id) when Id =:= {2,16,840,1,101,3,4,3,1} -> + fun 'dec_sa-dsaWithSHA224'/3; +getdec_SignatureAlgs(Id) when Id =:= {2,16,840,1,101,3,4,3,2} -> + fun 'dec_sa-dsaWithSHA256'/3; +getdec_SignatureAlgs(_) -> + fun(_,Bytes, _RestPrimFieldName) -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end. + + + + + +%%================================ +%% SMimeCaps +%%================================ +getenc_SMimeCaps({1,2,840,10040,4,3}) -> + fun(Type, Val, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end + end; +getenc_SMimeCaps({1,2,840,10045,4,1}) -> + fun(Type, Val, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end + end; +getenc_SMimeCaps({1,2,840,10045,4,3,1}) -> + fun(Type, Val, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end + end; +getenc_SMimeCaps({1,2,840,10045,4,3,2}) -> + fun(Type, Val, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end + end; +getenc_SMimeCaps({1,2,840,10045,4,3,3}) -> + fun(Type, Val, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end + end; +getenc_SMimeCaps({1,2,840,10045,4,3,4}) -> + fun(Type, Val, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end + end; +getenc_SMimeCaps({1,2,840,113549,1,1,2}) -> + fun(Type, Val, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end + end; +getenc_SMimeCaps({1,2,840,113549,1,1,4}) -> + fun(Type, Val, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end + end; +getenc_SMimeCaps({1,2,840,113549,1,1,5}) -> + fun(Type, Val, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end + end; +getenc_SMimeCaps({2,16,840,1,101,3,4,3,1}) -> + fun(Type, Val, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end + end; +getenc_SMimeCaps({2,16,840,1,101,3,4,3,2}) -> + fun(Type, Val, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end + end; +getenc_SMimeCaps(_) -> + fun(_, Val, _RestPrimFieldName) -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end. + +'getdec_SMimeCaps'({1,2,840,10040,4,3}) -> + fun(Type, Bytes, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end + end; +'getdec_SMimeCaps'({1,2,840,10045,4,1}) -> + fun(Type, Bytes, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end + end; +'getdec_SMimeCaps'({1,2,840,10045,4,3,1}) -> + fun(Type, Bytes, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end + end; +'getdec_SMimeCaps'({1,2,840,10045,4,3,2}) -> + fun(Type, Bytes, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end + end; +'getdec_SMimeCaps'({1,2,840,10045,4,3,3}) -> + fun(Type, Bytes, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end + end; +'getdec_SMimeCaps'({1,2,840,10045,4,3,4}) -> + fun(Type, Bytes, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end + end; +'getdec_SMimeCaps'({1,2,840,113549,1,1,2}) -> + fun(Type, Bytes, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end + end; +'getdec_SMimeCaps'({1,2,840,113549,1,1,4}) -> + fun(Type, Bytes, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end + end; +'getdec_SMimeCaps'({1,2,840,113549,1,1,5}) -> + fun(Type, Bytes, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end + end; +'getdec_SMimeCaps'({2,16,840,1,101,3,4,3,1}) -> + fun(Type, Bytes, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end + end; +'getdec_SMimeCaps'({2,16,840,1,101,3,4,3,2}) -> + fun(Type, Bytes, _RestPrimFieldName) -> + case Type of + 'Type' -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end + end; +getdec_SMimeCaps(_) -> + fun(_,Bytes, _RestPrimFieldName) -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end. + + + + + +%%================================ +%% NamedCurve +%%================================ +getenc_NamedCurve(_) -> + fun(_, Val, _RestPrimFieldName) -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end. + +getdec_NamedCurve(_) -> + fun(_,Bytes, _RestPrimFieldName) -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end. + + + + + +%%================================ +%% HashAlgs +%%================================ +getenc_HashAlgs(Id) when Id =:= {1,2,840,113549,2,2} -> + fun 'enc_mda-md2'/3; +getenc_HashAlgs(Id) when Id =:= {1,2,840,113549,2,5} -> + fun 'enc_mda-md5'/3; +getenc_HashAlgs(Id) when Id =:= {1,3,14,3,2,26} -> + fun 'enc_mda-sha1'/3; +getenc_HashAlgs(_) -> + fun(_, Val, _RestPrimFieldName) -> + case Val of + {asn1_OPENTYPE,Bin} when is_binary(Bin) -> + {Bin,byte_size(Bin)} + end + end. + +getdec_HashAlgs(Id) when Id =:= {1,2,840,113549,2,2} -> + fun 'dec_mda-md2'/3; +getdec_HashAlgs(Id) when Id =:= {1,2,840,113549,2,5} -> + fun 'dec_mda-md5'/3; +getdec_HashAlgs(Id) when Id =:= {1,3,14,3,2,26} -> + fun 'dec_mda-sha1'/3; +getdec_HashAlgs(_) -> + fun(_,Bytes, _RestPrimFieldName) -> + case Bytes of + Bin when is_binary(Bin) -> + {asn1_OPENTYPE,Bin}; + _ -> + {asn1_OPENTYPE,ber_encode(Bytes)} + end + end. + + + +%%% +%%% 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). + +collect_parts(TlvList) -> + collect_parts(TlvList, []). + +collect_parts([{_, L} | Rest], Acc) when is_list(L) -> + collect_parts(Rest, [collect_parts(L) | Acc]); +collect_parts([{3, <>} | Rest], _Acc) -> + collect_parts_bit(Rest, [Bits], Unused); +collect_parts([{_T, V} | Rest], Acc) -> + collect_parts(Rest, [V | Acc]); +collect_parts([], Acc) -> + list_to_binary(lists:reverse(Acc)). + +collect_parts_bit([{3, <>} | Rest], Acc, Uacc) -> + collect_parts_bit(Rest, [Bits | Acc], Unused + Uacc); +collect_parts_bit([], Acc, Uacc) -> + list_to_binary([Uacc | lists:reverse(Acc)]). + +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(<>, Av, Al) -> + dec_subidentifiers(T, 0, [Av bsl 7 + H | Al]). + +decode_integer(Tlv, TagIn) -> + Bin = match_tags(Tlv, TagIn), + Len = byte_size(Bin), + <> = Bin, + Int. + +decode_native_bit_string(Buffer, Tags) -> + case match_and_collect(Buffer, Tags) of + <<0>> -> + <<>>; + <> -> + Size = bit_size(Bits) - Unused, + <> = Bits, + Val + end. + +decode_null(Tlv, Tags) -> + Val = match_tags(Tlv, Tags), + case Val of + <<>> -> + 'NULL'; + _ -> + exit({error, {asn1, {decode_null, Val}}}) + end. + +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_octet_string(Tlv, TagsIn) -> + Bin = match_and_collect(Tlv, TagsIn), + binary:copy(Bin). + +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_integer(Val) -> + Bytes = + if + Val >= 0 -> + encode_integer_pos(Val, []); + true -> + encode_integer_neg(Val, []) + end, + {Bytes, length(Bytes)}. + +encode_integer(Val, Tag) when is_integer(Val) -> + encode_tags(Tag, encode_integer(Val)); +encode_integer(Val, _Tag) -> + exit({error, {asn1, {encode_integer, Val}}}). + +encode_integer_neg(-1, [B1 | _T] = L) when B1 > 127 -> + L; +encode_integer_neg(N, Acc) -> + encode_integer_neg(N bsr 8, [N band 255 | Acc]). + +encode_integer_pos(0, [B | _Acc] = L) when B < 128 -> + L; +encode_integer_pos(N, Acc) -> + encode_integer_pos(N bsr 8, [N band 255 | Acc]). + +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_null(_Val, TagIn) -> + encode_tags(TagIn, [], 0). + +encode_object_identifier(Val, TagIn) -> + encode_tags(TagIn, e_object_identifier(Val)). + +encode_restricted_string(OctetList, TagIn) when is_binary(OctetList) -> + encode_tags(TagIn, OctetList, byte_size(OctetList)); +encode_restricted_string(OctetList, TagIn) when is_list(OctetList) -> + encode_tags(TagIn, OctetList, length(OctetList)). + +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}. + +encode_unnamed_bit_string(Bits, TagIn) -> + Unused = (8 - bit_size(Bits) band 7) band 7, + Bin = <>, + encode_tags(TagIn, Bin, byte_size(Bin)). + +match_and_collect(Tlv, TagsIn) -> + Val = match_tags(Tlv, TagsIn), + case Val of + [_ | _] = PartList -> + collect_parts(PartList); + Bin when is_binary(Bin) -> + Bin + end. + +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). diff --git a/src/PKIXAlgs-2009.hrl b/src/PKIXAlgs-2009.hrl new file mode 100644 index 0000000..0f9b74c --- /dev/null +++ b/src/PKIXAlgs-2009.hrl @@ -0,0 +1,79 @@ +%% Generated by the Erlang ASN.1 compiler. Version: 5.1 +%% Purpose: Erlang record definitions for each named and unnamed +%% SEQUENCE and SET, and macro definitions for each value +%% definition in module PKIXAlgs-2009. + +-ifndef(_PKIXALGS_2009_HRL_). +-define(_PKIXALGS_2009_HRL_, true). + +-record('RSAPublicKey', { + modulus, + publicExponent +}). + +-record('DSA-Params', { + p, + q, + g +}). + +-record('DomainParameters', { + p, + g, + q, + j = asn1_NOVALUE, + validationParams = asn1_NOVALUE +}). + +-record('ValidationParams', { + seed, + pgenCounter +}). + +-record('DSA-Sig-Value', { + r, + s +}). + +-record('ECDSA-Sig-Value', { + r, + s +}). + +-define('rsaEncryption', {1,2,840,113549,1,1,1}). +-define('id-dsa', {1,2,840,10040,4,1}). +-define('dhpublicnumber', {1,2,840,10046,2,1}). +-define('id-keyExchangeAlgorithm', {2,16,840,1,101,2,1,1,22}). +-define('id-ecPublicKey', {1,2,840,10045,2,1}). +-define('id-ecDH', {1,3,132,1,12}). +-define('id-ecMQV', {1,3,132,1,13}). +-define('secp192r1', {1,2,840,10045,3,1,1}). +-define('sect163k1', {1,3,132,0,1}). +-define('sect163r2', {1,3,132,0,15}). +-define('secp224r1', {1,3,132,0,33}). +-define('sect233k1', {1,3,132,0,26}). +-define('sect233r1', {1,3,132,0,27}). +-define('secp256r1', {1,2,840,10045,3,1,7}). +-define('sect283k1', {1,3,132,0,16}). +-define('sect283r1', {1,3,132,0,17}). +-define('secp384r1', {1,3,132,0,34}). +-define('sect409k1', {1,3,132,0,36}). +-define('sect409r1', {1,3,132,0,37}). +-define('secp521r1', {1,3,132,0,35}). +-define('sect571k1', {1,3,132,0,38}). +-define('sect571r1', {1,3,132,0,39}). +-define('md2WithRSAEncryption', {1,2,840,113549,1,1,2}). +-define('md5WithRSAEncryption', {1,2,840,113549,1,1,4}). +-define('sha1WithRSAEncryption', {1,2,840,113549,1,1,5}). +-define('dsa-with-sha1', {1,2,840,10040,4,3}). +-define('dsa-with-sha224', {2,16,840,1,101,3,4,3,1}). +-define('dsa-with-sha256', {2,16,840,1,101,3,4,3,2}). +-define('ecdsa-with-SHA1', {1,2,840,10045,4,1}). +-define('ecdsa-with-SHA224', {1,2,840,10045,4,3,1}). +-define('ecdsa-with-SHA256', {1,2,840,10045,4,3,2}). +-define('ecdsa-with-SHA384', {1,2,840,10045,4,3,3}). +-define('ecdsa-with-SHA512', {1,2,840,10045,4,3,4}). +-define('id-md2', {1,2,840,113549,2,2}). +-define('id-md5', {1,2,840,113549,2,5}). +-define('id-sha1', {1,3,14,3,2,26}). +-endif. %% _PKIXALGS_2009_HRL_