Module iso7816

Utilities for parsing and generating ISO7816 APDUs.

Behaviours: apdu_transform.

Description

Utilities for parsing and generating ISO7816 APDUs. Also a apdu_transform implementation which does the same.

Data Types

atr_info()

atr_info() = #{t0 => #{wi => integer()}, t1 => #{ifsc => integer(), cwi => integer(), bwi => integer(), checksum => lrc | crc}, status => {iso_sw(), LCS::integer()}, fi => integer(), fmax => integer(), di => integer(), guardn => integer(), historical_bytes => {proprietary, binary()} | standard, country_code => binary(), issuer => binary(), aid => binary(), initial_data_cmd => apdu:cmd(), initial_data => binary(), service_data => binary(), isseru_data => binary(), preissuing_data => binary(), df_selection => [full_name | part_name | path | file_id], implicit_df => boolean(), short_ef => boolean(), record_num => boolean(), record_id => boolean(), ef_ber_tlv => boolean(), write_functions => one_time | proprietary | write_or | write_and, long_private_tags => boolean(), data_unit_size => integer(), chain_support => boolean(), extended_length => boolean(), extended_length_atr => boolean(), logical_chan_assign => [card | host], logical_chans => integer(), application_family => binary(), leftovers => binary()}

Information decoded from an ATR value.

ber_tlv_tag()

ber_tlv_tag() = {Tag::integer(), Data::binary() | [ber_tlv_tag()]}

channel()

channel() = integer()

cls()

cls() = iso | {iso, chain} | {iso, channel()} | {iso, chain, channel()} | integer()

The ISO7816 "class" field for a command APDU.

ins()

ins() = iso_ins() | integer()

The ISO7816 "instruction" field for a command APDU.

iso_error_ch()

iso_error_ch() = changed | memory_failure | {changed, integer()}

iso_error_cla()

iso_error_cla() = no_info | bad_chain | not_supported | {not_supported, channel | secure_msg | chain}

iso_error_cmd()

iso_error_cmd() = no_info | incompat_file | security_status | auth_method | conditions | no_ef | requires_secure_msg | bad_secure_msg | integer()

iso_error_format()

iso_error_format() = length | apdu | lc | lc_tlv_mismatch

iso_error_sec()

iso_error_sec() = integer()

iso_error_unch()

iso_error_unch() = unchanged | immediate_response | channel_share_denied | channel_denied | {unchanged, integer()}

iso_ins()

iso_ins() = change_ref_data | external_auth | general_auth | generate_asym_key | get_response | get_data | put_data | select | verify | reset_retry_counter

iso_sw()

iso_sw() = ok | {continue, integer()} | {requires_le, integer()} | {counter, integer()} | {warning, iso_warning_unch()} | {warning, iso_warning_ch()} | {error, {system, iso_error_unch()}} | {error, {system, iso_error_ch()}} | {error, {security, iso_error_sec()}} | {error, {denied, iso_error_cmd()}} | {error, {format, iso_error_format()}} | {error, {cla, iso_error_cla()}} | {error, {ins, not_supported}} | {error, {file, not_found}} | {error, {file, invalid}} | {error, {file, out_of_memory}} | {error, {file, exists}} | {error, {record, not_found}} | {error, {ref_data, not_found | unusable}} | {error, data_required} | {error, func_not_supported} | {error, p1p2} | {error, wrong_data} | {error, general_failure} | {desfire, integer()} | {error, integer()}

iso_warning_ch()

iso_warning_ch() = changed | bad_compare | file_full | {changed, integer()}

iso_warning_unch()

iso_warning_unch() = unchanged | part_corrupted | eof | deactivated | bad_format | terminated | no_input | {unchanged, integer()}

le()

le() = none | integer()

Expected length of reply field (optional).

p1()

p1() = integer()

First parameter byte in a command APDU.

p2()

p2() = integer()

Second parameter byte in a command APDU.

sw()

sw() = iso_sw() | integer()

Status word in a reply APDU.

tlv_inv_map()

tlv_inv_map() = #{atom() => integer() | {integer(), tlv_inv_map()} | [integer()] | [{integer(), tlv_inv_map()}]} | [{atom(), integer() | {integer(), tlv_inv_map()} | [integer()] | [{integer(), tlv_inv_map()}]}]

The inverted form of a tlv_map(), used with encode_ber_tlvs_map/2. Can be represented as a list instead of a map (for order-sensitive uses).

tlv_map()

tlv_map() = #{integer() => atom() | {atom(), tlv_map() | [atom()] | [{atom(), tlv_map()}]}}

A map used with decode_ber_tlvs_map/2 to decode BER-TLV binary data into structured maps.

Function Index

decode_apdu_cmd/1
decode_apdu_reply/1
decode_atr/1Decodes an ATR into the different encoded fields and information.
decode_ber_tlv/1
decode_ber_tlvs/1
decode_ber_tlvs_map/2Decodes BER-TLV data into structured maps using a provided schema.
encode_apdu_cmd/1
encode_apdu_reply/1
encode_ber_tlv/2
encode_ber_tlvs/1
encode_ber_tlvs_map/2Encodes structured maps into BER-TLV data using a provided schema.
invert_tlv_map/1Converts a tlv_map() into a tlv_inv_map().

Function Details

decode_apdu_cmd/1

decode_apdu_cmd(X1::binary()) -> apdu:cmd()

decode_apdu_reply/1

decode_apdu_reply(Bin::binary()) -> apdu:reply()

decode_atr/1

decode_atr(X1::binary()) -> atr_info()

Decodes an ATR into the different encoded fields and information.

decode_ber_tlv/1

decode_ber_tlv(X1::binary()) -> {ok, integer(), binary(), binary()} | {error, term()}

decode_ber_tlvs/1

decode_ber_tlvs(Bin::binary()) -> {ok, [ber_tlv_tag()]} | {error, term()}

decode_ber_tlvs_map/2

decode_ber_tlvs_map(Bin::binary(), Map::tlv_map()) -> {ok, map()}

Decodes BER-TLV data into structured maps using a provided schema.

An example schema (for the NIST PIV APT):


 #{
   16#61 => {apt, #{
     16#4F => aid,
     16#50 => app_label,
     16#79 => {alloc_auth, #{
       16#4F => aid
     }},
     16#5F50 => uri,
     16#AC => {algos, #{
       16#80 => [algo],
       16#06 => oid
     }}
   }}
 }

Will produce maps like the following:


   #{
       apt => #{
           aid => <<1,2,3,4>>,
           app_label => <<"Testing">>,
           uri => <<"https://test.com">>,
           algos => #{
               algo => [<<1>>, <<2>>, <<3>>]
           }
       }
   }

encode_apdu_cmd/1

encode_apdu_cmd(Apdu_cmd::apdu:cmd()) -> binary()

encode_apdu_reply/1

encode_apdu_reply(Apdu_reply::apdu:reply()) -> binary()

encode_ber_tlv/2

encode_ber_tlv(Tag::integer(), Data::binary()) -> binary()

encode_ber_tlvs/1

encode_ber_tlvs(Tlvs::[ber_tlv_tag()]) -> binary()

encode_ber_tlvs_map/2

encode_ber_tlvs_map(Map::map(), InvTagMap::tlv_inv_map()) -> binary()

Encodes structured maps into BER-TLV data using a provided schema.

invert_tlv_map/1

invert_tlv_map(Map::tlv_map()) -> tlv_inv_map()

Converts a tlv_map() into a tlv_inv_map().


Generated by EDoc