From d54c2ee8c51b41b7f7a5a469efd6bb391a0c2b75 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Tue, 17 Jan 2012 18:25:50 +0100 Subject: initial checkin of 'libosmosim' --- Makefile.am | 3 +- configure.ac | 2 + include/Makefile.am | 3 +- include/osmocom/sim/sim.h | 268 ++++++++++++++++++++++++++++++++ libosmosim.pc.in | 11 ++ src/sim/Makefile.am | 16 ++ src/sim/card_fs_sim.c | 385 ++++++++++++++++++++++++++++++++++++++++++++++ src/sim/card_fs_uicc.c | 185 ++++++++++++++++++++++ src/sim/card_fs_usim.c | 288 ++++++++++++++++++++++++++++++++++ src/sim/core.c | 172 +++++++++++++++++++++ src/sim/file_codec.c | 34 ++++ src/sim/gsm_int.h | 12 ++ src/sim/reader.c | 224 +++++++++++++++++++++++++++ src/sim/reader_pcsc.c | 133 ++++++++++++++++ src/sim/sim_int.h | 38 +++++ utils/Makefile.am | 5 + utils/osmo-sim-test.c | 370 ++++++++++++++++++++++++++++++++++++++++++++ 17 files changed, 2147 insertions(+), 2 deletions(-) create mode 100644 include/osmocom/sim/sim.h create mode 100644 libosmosim.pc.in create mode 100644 src/sim/Makefile.am create mode 100644 src/sim/card_fs_sim.c create mode 100644 src/sim/card_fs_uicc.c create mode 100644 src/sim/card_fs_usim.c create mode 100644 src/sim/core.c create mode 100644 src/sim/file_codec.c create mode 100644 src/sim/gsm_int.h create mode 100644 src/sim/reader.c create mode 100644 src/sim/reader_pcsc.c create mode 100644 src/sim/sim_int.h create mode 100644 utils/osmo-sim-test.c diff --git a/Makefile.am b/Makefile.am index faf7a832..c897f96b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,7 @@ ACLOCAL_AMFLAGS = -I m4 -SUBDIRS = include src src/vty src/codec src/gsm src/gb src/ctrl tests utils +AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include +SUBDIRS = include src src/vty src/codec src/gsm src/gb src/ctrl src/sim tests utils pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libosmocore.pc libosmocodec.pc libosmovty.pc libosmogsm.pc \ diff --git a/configure.ac b/configure.ac index deaa8bf7..7fc72fda 100644 --- a/configure.ac +++ b/configure.ac @@ -187,10 +187,12 @@ AC_OUTPUT( libosmogsm.pc libosmogb.pc libosmoctrl.pc + libosmosim.pc include/Makefile src/Makefile src/vty/Makefile src/codec/Makefile + src/sim/Makefile src/gsm/Makefile src/gb/Makefile src/ctrl/Makefile diff --git a/include/Makefile.am b/include/Makefile.am index c59f9b21..149e29fa 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -83,7 +83,8 @@ nobase_include_HEADERS = \ osmocom/gsm/rsl.h \ osmocom/gsm/rxlev_stat.h \ osmocom/gsm/sysinfo.h \ - osmocom/gsm/tlv.h + osmocom/gsm/tlv.h \ + osmocom/sim/sim.h if ENABLE_PLUGIN nobase_include_HEADERS += osmocom/core/plugin.h diff --git a/include/osmocom/sim/sim.h b/include/osmocom/sim/sim.h new file mode 100644 index 00000000..5ad1643e --- /dev/null +++ b/include/osmocom/sim/sim.h @@ -0,0 +1,268 @@ +#ifndef _OSMOCOM_SIM_H +#define _OSMOCOM_SIM_H + +#include +#include + +#define APDU_HDR_LEN 5 + +/* ISO 7816 / 5.3.1 / Figure 3 + Figure 4 */ +enum osim_apdu_case { + APDU_CASE_1, + APDU_CASE_2, + APDU_CASE_2_EXT, + APDU_CASE_3, + APDU_CASE_3_EXT, + APDU_CASE_4, + APDU_CASE_4_EXT +}; + +struct osim_apdu_cmd_hdr { + uint8_t cla; + uint8_t ins; + uint8_t p1; + uint8_t p2; + uint8_t p3; +} __attribute__ ((packed)); + +#define msgb_apdu_dr(__x) + +struct osim_msgb_cb { + enum osim_apdu_case apduc; + uint16_t lc; + uint16_t le; + uint16_t sw; +}; +#define OSIM_MSGB_CB(__msgb) ((struct osim_msgb_cb *)&((__msgb)->cb[0])) +/*! \brief status word from msgb->cb */ +#define msgb_apdu_case(__x) OSIM_MSGB_CB(__x)->apduc +#define msgb_apdu_lc(__x) OSIM_MSGB_CB(__x)->lc +#define msgb_apdu_le(__x) OSIM_MSGB_CB(__x)->le +#define msgb_apdu_sw(__x) OSIM_MSGB_CB(__x)->sw +/*! \brief pointer to the command header of the APDU */ +#define msgb_apdu_h(__x) ((struct osim_apdu_cmd_hdr *)(__x)->l2h) + +#define msgb_apdu_dc(__x) ((__x)->l2h + sizeof(struct osim_apdu_cmd_hdr)) +#define msgb_apdu_de(__x) ((__x)->l2h + sizeof(struct osim_apdu_cmd_hdr) + msgb_apdu_lc(__x)) + +struct osim_file; +struct osim_file_desc; +struct osim_decoded_data; + +struct osim_file_ops { + int (*parse)(struct osim_decoded_data *dd, + const struct osim_file_desc *desc, + int len, uint8_t *data); + struct msgb * (*encode)(const struct osim_file *file, + const struct osim_decoded_data *decoded); +}; + +enum osim_element_type { + ELEM_T_NONE, + ELEM_T_BOOL, /*!< a boolean flag */ + ELEM_T_UINT8, /*!< unsigned integer */ + ELEM_T_UINT16, /*!< unsigned integer */ + ELEM_T_UINT32, /*!< unsigned integer */ + ELEM_T_STRING, /*!< generic string */ + ELEM_T_BCD, /*!< BCD encoded digits */ + ELEM_T_BYTES, /*!< BCD encoded digits */ + ELEM_T_GROUP, /*!< group container, has siblings */ +}; + +enum osim_element_repr { + ELEM_REPR_NONE, + ELEM_REPR_DEC, + ELEM_REPR_HEX, +}; + +struct osim_decoded_element { + struct llist_head list; + + enum osim_element_type type; + enum osim_element_repr representation; + const char *name; + + unsigned int length; + union { + uint8_t u8; + uint16_t u16; + uint32_t u32; + uint8_t *buf; + struct llist_head siblings; + } u; +}; + +struct osim_decoded_data { + /*! file to which we belong */ + const struct osim_file *file; + /*! list of 'struct decoded_element' */ + struct llist_head decoded_elements; +}; + + +enum osim_file_type { + TYPE_NONE, + TYPE_DF, + TYPE_ADF, + TYPE_EF, + TYPE_EF_INT, +}; + +enum osim_ef_type { + EF_TYPE_TRANSP, + EF_TYPE_RECORD_FIXED, + EF_TYPE_RECORD_CYCLIC, +}; + +#define F_OPTIONAL 0x0001 + +struct osim_file_desc { + struct llist_head list; /*!< local element in list */ + struct llist_head child_list; /*!< list of children EF in DF */ + struct osim_file_desc *parent; /*!< parent DF */ + + enum osim_file_type type; + enum osim_ef_type ef_type; + + uint16_t fid; /*!< File Identifier */ + uint8_t sfid; /*!< Short File IDentifier */ + const char *df_name; + uint8_t df_name_len; + + const char *short_name; /*!< Short Name (like EF.ICCID) */ + const char *long_name; /*!< Long / description */ + unsigned int flags; + + struct osim_file_ops ops; +}; + +struct osim_file { + const struct osim_file_desc *desc; + + struct msgb *encoded_data; + struct osim_decoded_data *decoded_data; +}; + +#define EF(pfid, pns, pflags, pnl, ptype, pdec, penc) \ + { \ + .fid = pfid, \ + .type = TYPE_EF, \ + .ef_type = ptype, \ + .short_name = pns, \ + .long_name = pnl, \ + .flags = pflags, \ + .ops = { .encode = penc, .parse = pdec }, \ + } + + +#define EF_TRANSP(fid, ns, flags, nl, dec, enc) \ + EF(fid, ns, flags, nl, EF_TYPE_TRANSP, dec, enc) +#define EF_TRANSP_N(fid, ns, flags, nl) \ + EF_TRANSP(fid, ns, flags, nl, &default_decode, NULL) + +#define EF_CYCLIC(fid, ns, flags, nl, dec, enc) \ + EF(fid, ns, flags, nl, EF_TYPE_RECORD_CYCLIC, dec, enc) +#define EF_CYCLIC_N(fid, ns, flags, nl) \ + EF_CYCLIC(fid, ns, flags, nl, &default_decode, NULL) + +#define EF_LIN_FIX(fid, ns, flags, nl, dec, enc) \ + EF(fid, ns, flags, nl, EF_TYPE_RECORD_FIXED, dec, enc) +#define EF_LIN_FIX_N(fid, ns, flags, nl) \ + EF_LIN_FIX(fid, ns, flags, nl, &default_decode, NULL) + +struct osim_file_desc * +osim_file_find_name(struct osim_file_desc *parent, const char *name); + +enum osim_card_sw_type { + SW_TYPE_NONE, + SW_TYPE_STR, +}; + +enum osim_card_sw_class { + SW_CLS_NONE, + SW_CLS_OK, + SW_CLS_POSTP, + SW_CLS_WARN, + SW_CLS_ERROR, +}; + +struct osim_card_sw { + uint16_t code; + uint16_t mask; + enum osim_card_sw_type type; + enum osim_card_sw_class class; + union { + const char *str; + } u; +}; + +#define OSIM_CARD_SW_LAST { \ + .code = 0, .mask = 0, .type = SW_TYPE_NONE, \ + .class = SW_CLS_NONE, .u.str = NULL \ +} + +struct osim_card_profile { + const char *name; + struct osim_file_desc *mf; + struct osim_card_sw **sws; +}; + +extern const struct tlv_definition ts102221_fcp_tlv_def; +const struct value_string ts102221_fcp_vals[14]; + +/* 11.1.1.3 */ +enum ts102221_fcp_tag { + UICC_FCP_T_FCP = 0x62, + UICC_FCP_T_FILE_SIZE = 0x80, + UICC_FCP_T_TOT_F_SIZE = 0x81, + UICC_FCP_T_FILE_DESC = 0x82, + UICC_FCP_T_FILE_ID = 0x83, + UICC_FCP_T_DF_NAME = 0x84, + UICC_FCP_T_SFID = 0x88, + UICC_FCP_T_LIFEC_STS = 0x8A, + UICC_FCP_T_SEC_ATTR_REFEXP= 0x8B, + UICC_FCP_T_SEC_ATTR_COMP= 0x8C, + UICC_FCP_T_PROPRIETARY = 0xA5, + UICC_FCP_T_SEC_ATTR_EXP = 0xAB, + UICC_FCP_T_PIN_STS_DO = 0xC6, +}; + +struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1, + uint8_t p2, uint16_t lc, uint16_t le); + +struct osim_reader_ops; + +struct osim_reader_hdl { + /*! \brief member in global list of readers */ + struct llist_head list; + struct osim_reader_ops *ops; + void *priv; + /*! \brief current card, if any */ + struct osim_card_hdl *card; +}; + +struct osim_card_hdl { + /*! \brief member in global list of cards */ + struct llist_head list; + /*! \brief reader through which card is accessed */ + struct osim_reader_hdl *reader; + /*! \brief card profile */ + struct osim_card_profile *prof; + + /*! \brief list of channels for this card */ + struct llist_head channels; +}; + +struct osim_chan_hdl { + /*! \brief linked to card->channels */ + struct llist_head list; + /*! \brief card to which this channel belongs */ + struct osim_card_hdl *card; + const struct osim_file_desc *cwd; +}; + +/* reader.c */ +int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg); +struct osim_reader_hdl *osim_reader_open(int idx, const char *name); +struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh); +#endif /* _OSMOCOM_SIM_H */ diff --git a/libosmosim.pc.in b/libosmosim.pc.in new file mode 100644 index 00000000..cec877b9 --- /dev/null +++ b/libosmosim.pc.in @@ -0,0 +1,11 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: Osmocom SIM card related utilities Library +Description: C Utility Library +Version: @VERSION@ +Libs: -L${libdir} -losmosim +Cflags: -I${includedir}/ + diff --git a/src/sim/Makefile.am b/src/sim/Makefile.am new file mode 100644 index 00000000..f3f8069c --- /dev/null +++ b/src/sim/Makefile.am @@ -0,0 +1,16 @@ +# This is _NOT_ the library release version, it's an API version. +# Please read Chapter 6 "Library interface versions" of the libtool documentation before making any modification +LIBVERSION=0:0:0 + +INCLUDES = $(all_includes) -I$(top_srcdir)/include +AM_CFLAGS = -fPIC -Wall + +noinst_HEADERS = sim_int.h gsm_int.h + +lib_LTLIBRARIES = libosmosim.la + +libosmosim_la_SOURCES = core.c card_fs_sim.c card_fs_usim.c card_fs_uicc.c file_codec.c reader.c reader_pcsc.c +# FIXME: use autoconf to determine pcsc include path / library name +libosmosim_la_LDFLAGS = -version-info $(LIBVERSION) -lpcsclite +libosmosim_la_CFLAGS = -I/usr/include/PCSC + diff --git a/src/sim/card_fs_sim.c b/src/sim/card_fs_sim.c new file mode 100644 index 00000000..d8421505 --- /dev/null +++ b/src/sim/card_fs_sim.c @@ -0,0 +1,385 @@ + +#include +#include + +#include +#include +#include + +#include "sim_int.h" + +/* TS 11.11 / Chapter 9.4 */ +static const struct osim_card_sw ts11_11_sw[] = { + { + 0x9000, 0xffff, SW_TYPE_STR, SW_CLS_OK, + .u.str = "Normal ending of the command", + }, { + 0x9100, 0xff00, SW_TYPE_STR, SW_CLS_OK, + .u.str = "Normal ending of the command - proactive command from SIM pending", + }, { + 0x9e00, 0xff00, SW_TYPE_STR, SW_CLS_OK, + .u.str = "Normal ending of the command - response data for SIM data download", + }, { + 0x9f00, 0xff00, SW_TYPE_STR, SW_CLS_OK, + .u.str = "Normal ending of the command - response data available", + }, { + 0x9300, 0xffff, SW_TYPE_STR, SW_CLS_POSTP, + .u.str = "SIM Application Toolkit is busy, command cannot be executed at present", + }, { + 0x9200, 0xfff0, SW_TYPE_STR, SW_CLS_WARN, + .u.str = "Memory management - Command successful but after using an internal updat retry X times", + }, { + 0x9240, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Memory management - Memory problem", + }, { + 0x9400, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Referencing management - no EF selected", + }, { + 0x9402, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Referencing management - out of range (invalid address)", + }, { + 0x9404, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Referencing management - file ID not found / pattern not found", + }, { + 0x9802, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Security management - no CHV initialized", + }, { + 0x9804, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Security management - access condition not fulfilled", + }, { + 0x9808, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Security management - in contradiction with CHV status", + }, { + 0x9810, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Security management - in contradiction with invalidation status", + }, { + 0x9840, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Security management - unsuccessful CHV verification, no attempt left", + }, { + 0x9850, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Security management - increase cannot be performed, max value reached", + }, { + 0x6700, 0xff00, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Application independent - incorrect parameter P3", + }, { + 0x6b00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Application independent - incorrect parameter P1 or P2", + }, { + 0x6d00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Application independent - unknown instruction code", + }, { + 0x6e00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Application independent - wrong instruction class", + }, { + 0x6f00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Application independent - technical problem with no diagnostic given", + }, + OSIM_CARD_SW_LAST +}; + +static const struct osim_card_sw *sim_card_sws[] = { + ts11_11_sw, + NULL +}; + +static int iccid_decode(struct osim_decoded_data *dd, + const struct osim_file_desc *desc, + int len, uint8_t *data) +{ + struct osim_decoded_element *elem; + + elem = element_alloc(dd, "ICCID", ELEM_T_BCD, ELEM_REPR_DEC); + elem->length = len; + elem->u.buf = talloc_memdup(elem, data, len); + + return 0; +} + +static int elp_decode(struct osim_decoded_data *dd, + const struct osim_file_desc *desc, + int len, uint8_t *data) +{ + int i, num_lp = len / 2; + + for (i = 0; i < num_lp; i++) { + uint8_t *cur = data + i*2; + struct osim_decoded_element *elem; + elem = element_alloc(dd, "Language Code", ELEM_T_STRING, ELEM_REPR_NONE); + elem->u.buf = (uint8_t *) talloc_strndup(elem, (const char *) cur, 2); + } + + return 0; +} + +static int default_decode(struct osim_decoded_data *dd, + const struct osim_file_desc *desc, + int len, uint8_t *data) +{ + struct osim_decoded_element *elem; + + elem = element_alloc(dd, "Unknown Payload", ELEM_T_BYTES, ELEM_REPR_HEX); + elem->u.buf = talloc_memdup(elem, data, len); + + return 0; +} + + +/* 10.3.1 */ +int gsm_lp_decode(struct osim_decoded_data *dd, + const struct osim_file_desc *desc, + int len, uint8_t *data) +{ + int i; + + for (i = 0; i < len; i++) { + struct osim_decoded_element *elem; + elem = element_alloc(dd, "Language Code", ELEM_T_UINT8, ELEM_REPR_DEC); + elem->u.u8 = data[i]; + } + + return 0; +} + +/* 10.3.2 */ +int gsm_imsi_decode(struct osim_decoded_data *dd, + const struct osim_file_desc *desc, + int len, uint8_t *data) +{ + struct osim_decoded_element *elem; + + if (len < 2) + return -EINVAL; + + elem = element_alloc(dd, "IMSI", ELEM_T_BCD, ELEM_REPR_DEC); + elem->length = data[0]; + elem->u.buf = talloc_memdup(elem, data+1, len-1); + + return 0; +} + +/* 10.3.3 */ +static int gsm_kc_decode(struct osim_decoded_data *dd, + const struct osim_file_desc *desc, + int len, uint8_t *data) +{ + struct osim_decoded_element *kc, *cksn; + + if (len < 9) + return -EINVAL; + + kc = element_alloc(dd, "Kc", ELEM_T_BYTES, ELEM_REPR_HEX); + kc->u.buf = talloc_memdup(kc, data, 8); + cksn = element_alloc(dd, "CKSN", ELEM_T_UINT8, ELEM_REPR_DEC); + cksn->u.u8 = data[8]; + + return 0; +} + +/* 10.3.4 */ +static int gsm_plmnsel_decode(struct osim_decoded_data *dd, + const struct osim_file_desc *desc, + int len, uint8_t *data) +{ + int i, n_plmn = len / 3; + + if (n_plmn < 1) + return -EINVAL; + + for (i = 0; i < n_plmn; i++) { + uint8_t *cur = data + 3*i; + struct osim_decoded_element *elem, *mcc, *mnc; + uint8_t ra_buf[6]; + struct gprs_ra_id ra_id; + + memset(ra_buf, 0, sizeof(ra_buf)); + memcpy(ra_buf, cur, 3); + gsm48_parse_ra(&ra_id, ra_buf); + + elem = element_alloc(dd, "PLMN", ELEM_T_GROUP, ELEM_REPR_NONE); + + mcc = element_alloc_sub(elem, "MCC", ELEM_T_UINT16, ELEM_REPR_DEC); + mcc->u.u16 = ra_id.mcc; + + mnc = element_alloc_sub(elem, "MNC", ELEM_T_UINT16, ELEM_REPR_DEC); + mnc->u.u16 = ra_id.mnc; + } + + return 0; +} + +/* 10.3.5 */ +int gsm_hpplmn_decode(struct osim_decoded_data *dd, + const struct osim_file_desc *desc, + int len, uint8_t *data) +{ + struct osim_decoded_element *elem; + + elem = element_alloc(dd, "Time interval", ELEM_T_UINT8, ELEM_REPR_DEC); + elem->u.u8 = *data; + + return 0; +} + +/* Chapter 10.2.x */ +static const struct osim_file_desc sim_ef_in_mf[] = { + EF_TRANSP(0x2FE2, "EF.ICCID", 0, + "ICC Identification", &iccid_decode, NULL), + EF_TRANSP(0x2F05, "EF.ELP", F_OPTIONAL, + "Extended language preference", &elp_decode, NULL), +}; + +/* Chapter 10.3.x */ +static const struct osim_file_desc sim_ef_in_gsm[] = { + EF_TRANSP(0x6F05, "EF.LP", 0, + "Language preference", &gsm_lp_decode, NULL), + EF_TRANSP(0x6F07, "EF.IMSI", 0, + "IMSI", &gsm_imsi_decode, NULL), + EF_TRANSP(0x6F20, "EF.Kc", 0, + "Ciphering key Kc", &gsm_kc_decode, NULL), + EF_TRANSP(0x6F30, "EF.PLMNsel", F_OPTIONAL, + "PLMN selector", &gsm_plmnsel_decode, NULL), + EF_TRANSP(0x6F31, "EF.HPPLMN", 0, + "Higher Priority PLMN search period", &gsm_hpplmn_decode, NULL), + EF_TRANSP_N(0x6F37, "EF.ACMmax", F_OPTIONAL, + "ACM maximum value"), + EF_TRANSP_N(0x6F38, "EF.SST", 0, + "SIM service table"), + EF_CYCLIC_N(0x6F39, "EF.ACM", F_OPTIONAL, + "Accumulated call meter"), + EF_TRANSP_N(0x6F3E, "EF.GID1", F_OPTIONAL, + "Group Identifier Level 1"), + EF_TRANSP_N(0x6F3F, "EF.GID2", F_OPTIONAL, + "Group Identifier Level 2"), + EF_TRANSP_N(0x6F46, "EF.SPN", F_OPTIONAL, + "Service Provider Name"), + EF_TRANSP_N(0x6F41, "EF.PUCT", F_OPTIONAL, + "Price per unit and currency table"), + EF_TRANSP_N(0x6F45, "EF.CBMI", F_OPTIONAL, + "Cell broadcast massage identifier selection"), + EF_TRANSP_N(0x6F74, "EF.BCCH", 0, + "Broadcast control channels"), + EF_TRANSP_N(0x6F78, "EF.ACC", 0, + "Access control class"), + EF_TRANSP_N(0x6F7B, "EF.FPLMN", 0, + "Forbidden PLMNs"), + EF_TRANSP_N(0x6F7E, "EF.LOCI", 0, + "Location information"), + EF_TRANSP_N(0x6FAD, "EF.AD", 0, + "Administrative data"), + EF_TRANSP_N(0x6FAE, "EF.Phase", 0, + "Phase identification"), + EF_TRANSP_N(0x6FB1, "EF.VGCS", F_OPTIONAL, + "Voice Group Call Service"), + EF_TRANSP_N(0x6FB2, "EF.VGCSS", F_OPTIONAL, + "Voice Group Call Service Status"), + EF_TRANSP_N(0x6FB3, "EF.VBS", F_OPTIONAL, + "Voice Broadcast Service"), + EF_TRANSP_N(0x6FB4, "EF.VBSS", F_OPTIONAL, + "Voice Broadcast Service Status"), + EF_TRANSP_N(0x6FB5, "EF.eMLPP", F_OPTIONAL, + "enhanced Mult Level Pre-emption and Priority"), + EF_TRANSP_N(0x6FB6, "EF.AAeM", F_OPTIONAL, + "Automatic Answer for eMLPP Service"), + EF_TRANSP_N(0x6F48, "EF.CBMID", F_OPTIONAL, + "Cell Broadcast Message Identifier for Data Download"), + EF_TRANSP_N(0x6FB7, "EF.ECC", F_OPTIONAL, + "Emergency Call Code"), + EF_TRANSP_N(0x6F50, "EF.CBMIR", F_OPTIONAL, + "Cell broadcast message identifier range selection"), + EF_TRANSP_N(0x6F2C, "EF.DCK", F_OPTIONAL, + "De-personalization Control Keys"), + EF_TRANSP_N(0x6F32, "EF.CNL", F_OPTIONAL, + "Co-operative Network List"), + EF_LIN_FIX_N(0x6F51, "EF.NIA", F_OPTIONAL, + "Network's Indication of Alerting"), + EF_TRANSP_N(0x6F52, "EF.KcGPRS", F_OPTIONAL, + "GPRS Ciphering key KcGPRS"), + EF_TRANSP_N(0x6F53, "EF.LOCIGPRS", F_OPTIONAL, + "GPRS location information"), + EF_TRANSP_N(0x6F54, "EF.SUME", F_OPTIONAL, + "SetUpMenu Elements"), + EF_TRANSP_N(0x6F60, "EF.PLMNwAcT", F_OPTIONAL, + "User controlled PLMN Selector with Access Technology"), + EF_TRANSP_N(0x6F61, "EF.OPLMNwAcT", F_OPTIONAL, + "Operator controlled PLMN Selector with Access Technology"), + EF_TRANSP_N(0x6F62, "EF.HPLMNwAcT", F_OPTIONAL, + "HPLMN Selector with Access Technology"), + EF_TRANSP_N(0x6F63, "EF.CPBCCH", F_OPTIONAL, + "CPBCCH Information"), + EF_TRANSP_N(0x6F64, "EF.InvScan", F_OPTIONAL, + "Investigation Scan"), +}; + +/* 10.5. */ +static const struct osim_file_desc sim_ef_in_telecom[] = { + EF_LIN_FIX_N(0x6F3A, "EF.ADN", F_OPTIONAL, + "Abbreviated dialling numbers"), + EF_LIN_FIX_N(0x6F3B, "EF.FDN", F_OPTIONAL, + "Fixed dialling numbers"), + EF_LIN_FIX_N(0x6F3C, "EF.SMS", F_OPTIONAL, + "Short messages"), + EF_LIN_FIX_N(0x6F3D, "EF.CCP", F_OPTIONAL, + "Capability configuration parameters"), + EF_LIN_FIX_N(0x6F4F, "EF.ECCP", F_OPTIONAL, + "Extended Capability configuration parameters"), + EF_LIN_FIX_N(0x6F40, "EF.MSISDN", F_OPTIONAL, + "MSISDN"), + EF_LIN_FIX_N(0x6F42, "EF.SMSP", F_OPTIONAL, + "Short message service parameters"), + EF_TRANSP_N(0x6F43, "EF.SMSS", F_OPTIONAL, + "SMS Status"), + EF_CYCLIC_N(0x6F44, "EF.LND", F_OPTIONAL, + "Last number dialled"), + EF_LIN_FIX_N(0x6F4A, "EF.EXT1", F_OPTIONAL, + "Extension 1"), + EF_LIN_FIX_N(0x6F4B, "EF.EXT2", F_OPTIONAL, + "Extension 2"), + EF_LIN_FIX_N(0x6F4C, "EF.EXT3", F_OPTIONAL, + "Extension 3"), + EF_LIN_FIX_N(0x6F4D, "EF.BDN", F_OPTIONAL, + "Barred dialling numbers"), + EF_LIN_FIX_N(0x6F4E, "EF.EXT4", F_OPTIONAL, + "Extension 4"), + EF_LIN_FIX_N(0x6F47, "EF.SMSR", F_OPTIONAL, + "Short message status reports"), + EF_LIN_FIX_N(0x6F58, "EF.CMI", F_OPTIONAL, + "Comparison Method Information"), +}; + + +/* 10.6. */ +static const struct osim_file_desc sim_ef_in_graphics[] = { + EF_LIN_FIX_N(0x4F20, "EF.IMG", F_OPTIONAL, + "Image"), +}; + +struct osim_card_profile *osim_cprof_sim(void *ctx) +{ + struct osim_card_profile *cprof; + struct osim_file_desc *mf, *gsm, *tc; + + cprof = talloc_zero(ctx, struct osim_card_profile); + cprof->name = "GSM SIM"; + cprof->sws = sim_card_sws; + + mf = alloc_df(cprof, 0x3f00, "MF"); + + cprof->mf = mf; + + add_filedesc(mf, sim_ef_in_mf, ARRAY_SIZE(sim_ef_in_mf)); + gsm = add_df_with_ef(mf, 0x7F20, "DF.GSM", sim_ef_in_gsm, + ARRAY_SIZE(sim_ef_in_gsm)); + add_df_with_ef(gsm, 0x5F30, "DF.IRIDIUM", NULL, 0); + add_df_with_ef(gsm, 0x5F31, "DF.GLOBST", NULL, 0); + add_df_with_ef(gsm, 0x5F32, "DF.ICO", NULL, 0); + add_df_with_ef(gsm, 0x5F33, "DF.ACeS", NULL, 0); + add_df_with_ef(gsm, 0x5F40, "DF.ACeS", NULL, 0); + add_df_with_ef(gsm, 0x5F60, "DF.CTS", NULL, 0); + add_df_with_ef(gsm, 0x5F70, "DF.SoLSA", NULL, 0); + tc = add_df_with_ef(mf, 0x7F10, "DF.TELECOM", sim_ef_in_telecom, + ARRAY_SIZE(sim_ef_in_telecom)); + add_df_with_ef(tc, 0x5F50, "DF.GRAPHICS", sim_ef_in_graphics, + ARRAY_SIZE(sim_ef_in_graphics)); + + return cprof; +} diff --git a/src/sim/card_fs_uicc.c b/src/sim/card_fs_uicc.c new file mode 100644 index 00000000..03dbad32 --- /dev/null +++ b/src/sim/card_fs_uicc.c @@ -0,0 +1,185 @@ +#include +#include + +/* TS 102 221 V10.0.0 / 10.2.1 */ +const struct osim_card_sw ts102221_uicc_sw[] = { + { + 0x9000, 0xffff, SW_TYPE_STR, SW_CLS_OK, + .u.str = "Normal ending of the command", + }, { + 0x9100, 0xff00, SW_TYPE_STR, SW_CLS_OK, + .u.str = "Normal ending of the command, extra info proactive", + }, { + 0x9200, 0xff00, SW_TYPE_STR, SW_CLS_OK, + .u.str = "Normal ending of the command, extra info regarding transfer session", + }, { + 0x9300, 0xff00, SW_TYPE_STR, SW_CLS_POSTP, + .u.str = "SIM Application Toolkit is busy, command cannot be executed at present", + }, { + 0x6200, 0xffff, SW_TYPE_STR, SW_CLS_WARN, + .u.str = "No information given, state of non volatile memory unchanged", + }, { + 0x6281, 0xffff, SW_TYPE_STR, SW_CLS_WARN, + .u.str = "Part of returned data may be corrupted", + }, { + 0x6282, 0xffff, SW_TYPE_STR, SW_CLS_WARN, + .u.str = "End of file/record reached before reading Le bytes", + }, { + 0x6283, 0xffff, SW_TYPE_STR, SW_CLS_WARN, + .u.str = "Selected file invalidated", + }, { + 0x6285, 0xffff, SW_TYPE_STR, SW_CLS_WARN, + .u.str = "Selected file in termination state", + }, { + 0x62f1, 0xffff, SW_TYPE_STR, SW_CLS_WARN, + .u.str = "More data available", + }, { + 0x62f2, 0xffff, SW_TYPE_STR, SW_CLS_WARN, + .u.str = "More data available and proactive command pending", + }, { + 0x62f3, 0xffff, SW_TYPE_STR, SW_CLS_WARN, + .u.str = "Response data available", + }, { + 0x63f1, 0xffff, SW_TYPE_STR, SW_CLS_WARN, + .u.str = "More data expected", + }, { + 0x63c0, 0xfff0, SW_TYPE_STR, SW_CLS_WARN, + .u.str = "Verification falied, X retries remaining", + }, { + 0x6400, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Execution - No information given, state of non-volatile memory unchanged", + }, { + 0x6500, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Execution - No information given, state of non-volatile memory changed", + }, { + 0x6581, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Execution - Memory problem", + }, { + 0x6700, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Checking - Wrong length", + }, { + 0x6700, 0xff00, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Checking - Command dependent error", + }, { + 0x6b00, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Checking - Wrong parameter(s) P1-P2", + }, { + 0x6d00, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Checking - Instruction code not supported or valid", + }, { + 0x6e00, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Checking - Class not supported", + }, { + 0x6f00, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Checking - Technical problem, no precise diagnostics", + }, { + 0x6f00, 0xff00, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Checking - Command dependent error", + }, { + 0x6800, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Function in CLA not supported - No information given", + }, { + 0x6881, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Function in CLA not supported - Logical channel not supported", + }, { + 0x6882, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Function in CLA not supportied - Secure messaging not supported", + }, { + 0x6900, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Command not allowed - No information given", + }, { + 0x6981, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Command not allowed - Command incompatible with file structure", + }, { + 0x6982, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Command not allowed - Security status not satisfied", + }, { + 0x6983, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Command not allowed - Authentication/PIN method blocked", + }, { + 0x6984, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Command not allowed - Referenced data invalidated", + }, { + 0x6985, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Command not allowed - Conditions of use not satisfied", + }, { + 0x6986, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Command not allowed - Noe EF selected", + }, { + 0x6989, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Command not allowed - secure channel - security not satisfied", + }, { + 0x6a80, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Wrong parameters - Incorrect parameters in the data field", + }, { + 0x6a81, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Wrong parameters - Function not supported", + }, { + 0x6a82, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Wrong parameters - File not found", + }, { + 0x6a83, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Wrong parameters - Record not found", + }, { + 0x6a84, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Wrong parameters - Not enough memory space", + }, { + 0x6a86, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Wrong parameters - Incorrect parameters P1 to P2", + }, { + 0x6a87, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Wrong parameters - Lc inconsistent with P1 ot P2", + }, { + 0x6a88, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Wrong parameters - Referenced data not found", + }, { + 0x9850, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Application error - INCREASE cannot be performed, max value reached", + }, { + 0x9862, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Application error - Authentication error, application specific", + }, { + 0x9863, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Application error - Security session or association expired", + }, + OSIM_CARD_SW_LAST +}; + +const struct value_string ts102221_fcp_vals[14] = { + { UICC_FCP_T_FCP, "File control parameters" }, + { UICC_FCP_T_FILE_SIZE, "File size" }, + { UICC_FCP_T_TOT_F_SIZE, "Total size of files" }, + { UICC_FCP_T_FILE_DESC, "File descriptor" }, + { UICC_FCP_T_FILE_ID, "File identifier" }, + { UICC_FCP_T_DF_NAME, "DF name" }, + { UICC_FCP_T_SFID, "Short file identifier" }, + { UICC_FCP_T_LIFEC_STS, "Lifecycle status integer" }, + { UICC_FCP_T_SEC_ATTR_REFEXP, "Security attributes (Referenced/Expanded)" }, + { UICC_FCP_T_SEC_ATTR_COMP, "Security attributes (Compact)" }, + { UICC_FCP_T_PROPRIETARY, "Proprietary" }, + { UICC_FCP_T_SEC_ATTR_EXP, "Security attributes (Expanded)" }, + { UICC_FCP_T_PIN_STS_DO, "PIN Status DO" }, + { 0, NULL } +}; + +/* FIXME: Ber-TLV ?? */ +const struct tlv_definition ts102221_fcp_tlv_def = { + .def = { + [UICC_FCP_T_FCP] = { TLV_TYPE_TLV }, + [UICC_FCP_T_FILE_SIZE] = { TLV_TYPE_TLV }, + [UICC_FCP_T_TOT_F_SIZE] = { TLV_TYPE_TLV }, + [UICC_FCP_T_FILE_DESC] = { TLV_TYPE_TLV }, + [UICC_FCP_T_FILE_ID] = { TLV_TYPE_TLV }, + [UICC_FCP_T_DF_NAME] = { TLV_TYPE_TLV }, + [UICC_FCP_T_SFID] = { TLV_TYPE_TLV }, + [UICC_FCP_T_LIFEC_STS] = { TLV_TYPE_TLV }, + [UICC_FCP_T_SEC_ATTR_REFEXP] = { TLV_TYPE_TLV }, + [UICC_FCP_T_SEC_ATTR_COMP] = { TLV_TYPE_TLV }, + [UICC_FCP_T_PROPRIETARY] = { TLV_TYPE_TLV }, + [UICC_FCP_T_SEC_ATTR_EXP] = { TLV_TYPE_TLV }, + [UICC_FCP_T_PIN_STS_DO] = { TLV_TYPE_TLV }, + }, +}; + +/* Annex E - TS 101 220 */ +static const uint8_t adf_uicc_aid[] = { 0xA0, 0x00, 0x00, 0x00, 0x87, 0x10, 0x01 }; diff --git a/src/sim/card_fs_usim.c b/src/sim/card_fs_usim.c new file mode 100644 index 00000000..361c2e88 --- /dev/null +++ b/src/sim/card_fs_usim.c @@ -0,0 +1,288 @@ + +#include +#include + +#include +#include +#include + +#include "sim_int.h" +#include "gsm_int.h" + +/* TS 31.102 Version 7.7.0 / Chapoter 7.3 */ +const struct osim_card_sw ts31_102_sw[] = { + { + 0x9862, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Security management - Authentication error, incorrect MAC", + }, { + 0x9864, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Security management - Authentication error, security context not supported", + }, { + 0x9865, 0xffff, SW_TYPE_STR, SW_CLS_ERROR, + .u.str = "Security management - Key freshness error", + }, + OSIM_CARD_SW_LAST +}; + +static const struct osim_card_sw *usim_card_sws[] = { + ts31_102_sw, + ts102221_uicc_sw, + NULL +}; + + +static int default_decode(struct osim_decoded_data *dd, + const struct osim_file_desc *desc, + int len, uint8_t *data) +{ + struct osim_decoded_element *elem; + + elem = element_alloc(dd, "Unknown Payload", ELEM_T_BYTES, ELEM_REPR_HEX); + elem->u.buf = talloc_memdup(elem, data, len); + + return 0; +} + +/* TS 102 221 Chapter 13.1 */ +static const struct osim_file_desc uicc_ef_in_mf[] = { + EF_LIN_FIX_N(0x2f00, "EF.DIR", 0, + "Application directory"), + EF_TRANSP_N(0x2FE2, "EF.ICCID", 0, + "ICC Identification"), + EF_TRANSP_N(0x2F05, "EF.PL", 0, + "Preferred Languages"), + EF_LIN_FIX_N(0x2F06, "EF.ARR", F_OPTIONAL, + "Access Rule Reference"), +}; + +static const struct osim_file_desc usim_ef_in_df_gsm_access[] = { + EF_TRANSP_N(0x4f20, "EF.Kc", 0, + "Ciphering Key Kc"), + EF_TRANSP_N(0x4f52, "EF.KcGPRS", 0, + "GPRS Ciphering key KcGPRS"), + EF_TRANSP_N(0x4f63, "EF.CPBCCH", F_OPTIONAL, + "CPBCCH Information"), + EF_TRANSP_N(0x4f64, "EF.invSCAN", F_OPTIONAL, + "Investigation Scan"), +}; + +/* 31.102 Chapter 4.2 */ +static const struct osim_file_desc usim_ef_in_adf_usim[] = { + EF_TRANSP(0x6F05, "EF.LI", 0, + "Language Indication", &gsm_lp_decode, NULL), + EF_TRANSP(0x6F07, "EF.IMSI", 0, + "IMSI", &gsm_imsi_decode, NULL), + EF_TRANSP_N(0x6F08, "EF.Keys", 0, + "Ciphering and Integrity Keys"), + EF_TRANSP_N(0x6F09, "EF.KeysPS", 0, + "Ciphering and Integrity Keys for Packet Switched domain"), + EF_TRANSP_N(0x6F60, "EF.PLMNwAcT", F_OPTIONAL, + "User controlled PLMN Selector with Access Technology"), + EF_TRANSP(0x6F31, "EF.HPPLMN", 0, + "Higher Priority PLMN search period", &gsm_hpplmn_decode, NULL), + EF_TRANSP_N(0x6F37, "EF.ACMmax", F_OPTIONAL, + "ACM maximum value"), + EF_TRANSP_N(0x6F38, "EF.UST", 0, + "USIM Service Table"), + EF_CYCLIC_N(0x6F39, "EF.ACM", F_OPTIONAL, + "Accumulated call meter"), + EF_TRANSP_N(0x6F3E, "EF.GID1", F_OPTIONAL, + "Group Identifier Level 1"), + EF_TRANSP_N(0x6F3F, "EF.GID2", F_OPTIONAL, + "Group Identifier Level 2"), + EF_TRANSP_N(0x6F46, "EF.SPN", F_OPTIONAL, + "Service Provider Name"), + EF_TRANSP_N(0x6F41, "EF.PUCT", F_OPTIONAL, + "Price per unit and currency table"), + EF_TRANSP_N(0x6F45, "EF.CBMI", F_OPTIONAL, + "Cell broadcast massage identifier selection"), + EF_TRANSP_N(0x6F78, "EF.ACC", 0, + "Access control class"), + EF_TRANSP_N(0x6F7B, "EF.FPLMN", 0, + "Forbidden PLMNs"), + EF_TRANSP_N(0x6F7E, "EF.LOCI", 0, + "Location information"), + EF_TRANSP_N(0x6FAD, "EF.AD", 0, + "Administrative data"), + EF_TRANSP_N(0x6F48, "EF.CBMID", F_OPTIONAL, + "Cell Broadcast Message Identifier for Data Download"), + EF_TRANSP_N(0x6FB7, "EF.ECC", F_OPTIONAL, + "Emergency Call Code"), + EF_TRANSP_N(0x6F50, "EF.CBMIR", F_OPTIONAL, + "Cell broadcast message identifier range selection"), + EF_TRANSP_N(0x6F73, "EF.PSLOCI", 0, + "Pacet Switched location information"), + EF_LIN_FIX_N(0x6F3B, "EF.FDN", F_OPTIONAL, + "Fixed dialling numbers"), + EF_LIN_FIX_N(0x6F3C, "EF.SMS", F_OPTIONAL, + "Short messages"), + EF_LIN_FIX_N(0x6F40, "EF.MSISDN", F_OPTIONAL, + "MSISDN"), + EF_LIN_FIX_N(0x6F42, "EF.SMSP", F_OPTIONAL, + "Short message service parameters"), + EF_TRANSP_N(0x6F43, "EF.SMSS", F_OPTIONAL, + "SMS Status"), + EF_LIN_FIX_N(0x6F49, "EF.SDN", F_OPTIONAL, + "Service Dialling Numbers"), + EF_LIN_FIX_N(0x6F4B, "EF.EXT2", F_OPTIONAL, + "Extension 2"), + EF_LIN_FIX_N(0x6F4C, "EF.EXT3", F_OPTIONAL, + "Extension 3"), + EF_LIN_FIX_N(0x6F47, "EF.SMSR", F_OPTIONAL, + "Short message status reports"), + EF_CYCLIC_N(0x6F80, "EF.ICI", F_OPTIONAL, + "Incoming Calling Information"), + EF_CYCLIC_N(0x6F81, "EF.OCI", F_OPTIONAL, + "Outgoing Calling Information"), + EF_CYCLIC_N(0x6F82, "EF.ICT", F_OPTIONAL, + "Incoming Call Timer"), + EF_CYCLIC_N(0x6F83, "EF.OCT", F_OPTIONAL, + "Outgoing Call Timer"), + EF_LIN_FIX_N(0x6F4E, "EF.EXT5", F_OPTIONAL, + "Extension 5"), + EF_LIN_FIX_N(0x6F4F, "EF.CCP2", F_OPTIONAL, + "Capability Configuration Parameters 2"), + EF_TRANSP_N(0x6FB5, "EF.eMLPP", F_OPTIONAL, + "enhanced Multi Level Precedence and Pre-emption"), + EF_TRANSP_N(0x6FB6, "EF.AAeM", F_OPTIONAL, + "Automatic Answer for eMLPP Service"), + EF_TRANSP_N(0x6FC3, "EF.Hiddenkey", F_OPTIONAL, + "Key for hidden phone book entries"), + EF_LIN_FIX_N(0x6F4D, "EF.BDN", F_OPTIONAL, + "Barred Dialling Numbers"), + EF_LIN_FIX_N(0x6F4E, "EF.EXT4", F_OPTIONAL, + "Extension 4"), + EF_LIN_FIX_N(0x6F58, "EF.CMI", F_OPTIONAL, + "Comparison Method Information"), + EF_TRANSP_N(0x6F56, "EF.EST", F_OPTIONAL, + "Enhanced Services Table"), + EF_TRANSP_N(0x6F57, "EF.ACL", F_OPTIONAL, + "Access Point Name Control List"), + EF_TRANSP_N(0x6F2C, "EF.DCK", F_OPTIONAL, + "Depersonalisation Control Keys"), + EF_TRANSP_N(0x6F32, "EF.CNL", F_OPTIONAL, + "Co-operative Network List"), + EF_TRANSP_N(0x6F5B, "EF.START-HFN", 0, + "Initialisation values for Hyperframe number"), + EF_TRANSP_N(0x6F5C, "EF.THRESHOLD", 0, + "Maximum value of START"), + EF_TRANSP_N(0x6F61, "EF.OPLMNwAcT", F_OPTIONAL, + "Operator controlled PLMN Selector with Access Technology"), + EF_TRANSP_N(0x6F62, "EF.HPLMNwAcT", F_OPTIONAL, + "HPLMN Selector with Access Technology"), + EF_LIN_FIX_N(0x6F06, "EF.ARR", 0, + "Access Rule Reference"), + EF_TRANSP_N(0x6FC4, "EF.NETPAR", 0, + "Network Parameters"), + EF_LIN_FIX_N(0x6FC5, "EF.PNN", F_OPTIONAL, + "PLMN Network Name"), + EF_LIN_FIX_N(0x6FC6, "EF.OPL", F_OPTIONAL, + "Operator PLMN List"), + EF_LIN_FIX_N(0x6FC7, "EF.MBDN", F_OPTIONAL, + "Mailbox Dialling Numbers"), + EF_LIN_FIX_N(0x6FC8, "EF.EXT6", F_OPTIONAL, + "Extension 6"), + EF_LIN_FIX_N(0x6FC9, "EF.MBI", F_OPTIONAL, + "Mailbox Identifier"), + EF_LIN_FIX_N(0x6FCA, "EF.MWIS", F_OPTIONAL, + "Message Waiting Indication Status"), + EF_LIN_FIX_N(0x6FCB, "EF.CFIS", F_OPTIONAL, + "Call Forwarding Indication Status"), + EF_LIN_FIX_N(0x6FCC, "EF.EXT7", F_OPTIONAL, + "Extension 7"), + EF_TRANSP_N(0x6FCD, "EF.SPDI", F_OPTIONAL, + "Service Provider Display Information"), + EF_LIN_FIX_N(0x6FCE, "EF.MMSN", F_OPTIONAL, + "MMS Notification"), + EF_LIN_FIX_N(0x6FCF, "EF.EXT8", F_OPTIONAL, + "Extension 8"), + EF_TRANSP_N(0x6FD0, "EF.MMSICP", F_OPTIONAL, + "MMS Issuer Connectivity Parameters"), + EF_LIN_FIX_N(0x6FD1, "EF.MMSUP", F_OPTIONAL, + "MMS User Preferences"), + EF_TRANSP_N(0x6FD2, "EF.MMSUCP", F_OPTIONAL, + "MMS User Connectivity Parameters"), + EF_LIN_FIX_N(0x6FD3, "EF.NIA", F_OPTIONAL, + "Network's Indication of Alerting"), + EF_TRANSP_N(0x6FB1, "EF.VGCS", F_OPTIONAL, + "Voice Group Call Service"), + EF_TRANSP_N(0x6FB2, "EF.VGCSS", F_OPTIONAL, + "Voice Group Call Service Status"), + EF_TRANSP_N(0x6FB3, "EF.VBS", F_OPTIONAL, + "Voice Broadcast Service"), + EF_TRANSP_N(0x6FB4, "EF.VBSS", F_OPTIONAL, + "Voice Broadcast Service Status"), + EF_TRANSP_N(0x6FD4, "EF.VGCSCA", F_OPTIONAL, + "Voice Group Call Service Ciphering Algorithm"), + EF_TRANSP_N(0x6FD5, "EF.VBSCA", F_OPTIONAL, + "Voice Broadcast Service Ciphering Algorithm"), + EF_TRANSP_N(0x6FD6, "EF.GBABP", F_OPTIONAL, + "GBA Bootstrapping parameters"), + EF_LIN_FIX_N(0x6FD7, "EF.MSK", F_OPTIONAL, + "MBMS Serviec Key List"), + EF_LIN_FIX_N(0x6FD8, "EF.MUK", F_OPTIONAL, + "MBMS User Key"), + EF_LIN_FIX_N(0x6FDA, "EF.GBANL", F_OPTIONAL, + "GBA NAF List"), + EF_TRANSP_N(0x6FD9, "EF.EHPLMN", F_OPTIONAL, + "Equivalent HPLMN"), +}; + + + +/* 31.102 Chapter 4.4.1 */ +static const struct osim_file_desc usim_ef_in_solsa[] = { + EF_TRANSP_N(0x4F30, "EF.SAI", F_OPTIONAL, + "SoLSA Access Indicator"), + EF_LIN_FIX_N(0x4F31, "EF.SLL", F_OPTIONAL, + "SoLSA LSA List"), + /* LSA descriptor files 4Fxx, hard to represent here */ +}; + +/* Annex E - TS 101 220 */ +static const uint8_t adf_usim_aid[] = { 0xA0, 0x00, 0x00, 0x00, 0x87, 0x10, 0x02 }; + +struct osim_card_profile *osim_cprof_usim(void *ctx) +{ + struct osim_card_profile *cprof; + struct osim_file_desc *mf, *gsm, *tc, *uadf; + + cprof = talloc_zero(ctx, struct osim_card_profile); + cprof->name = "3GPP USIM"; + cprof->sws = usim_card_sws; + + mf = alloc_df(cprof, 0x3f00, "MF"); + + cprof->mf = mf; + + /* Core UICC Files */ + add_filedesc(mf, uicc_ef_in_mf, ARRAY_SIZE(uicc_ef_in_mf)); + + /* ADF.USIM with its EF siblings */ + uadf = add_adf_with_ef(mf, adf_usim_aid, sizeof(adf_usim_aid), + "ADF.USIM", usim_ef_in_adf_usim, + ARRAY_SIZE(usim_ef_in_adf_usim)); + + /* DFs under ADF.USIM */ + add_df_with_ef(uadf, 0x5F3A, "DF.PHONEBOOK", NULL, 0); + add_df_with_ef(uadf, 0x5F3B, "DF.GSM-ACCESS", usim_ef_in_df_gsm_access, + ARRAY_SIZE(usim_ef_in_df_gsm_access)); + add_df_with_ef(uadf, 0x5F3C, "DF.MExE", NULL, 0); + add_df_with_ef(uadf, 0x5F40, "DF.WLAN", NULL, 0); + add_df_with_ef(uadf, 0x5F70, "DF.SoLSA", usim_ef_in_solsa, ARRAY_SIZE(usim_ef_in_solsa)); + +#if 0 + /* DF.TELECOM as sub-directory of MF */ + tc = add_df_with_ef(mf, 0x7F10, "DF.TELECOM", sim_ef_in_telecom, + ARRAY_SIZE(sim_ef_in_telecom)); + add_df_with_ef(tc, 0x5F50, "DF.GRAPHICS", sim_ef_in_graphics, + ARRAY_SIZE(sim_ef_in_graphics)); + + /* DF.GSM for backwards compatibility */ + gsm = add_df_with_ef(mf, 0x7F20, "DF.GSM", sim_ef_in_gsm, + ARRAY_SIZE(sim_ef_in_gsm)); + /* FIXME: DF's below DF.GSM (51.011) */ +#endif + + return cprof; +} diff --git a/src/sim/core.c b/src/sim/core.c new file mode 100644 index 00000000..40a49cf2 --- /dev/null +++ b/src/sim/core.c @@ -0,0 +1,172 @@ +#include +#include + +#include +#include + +static struct osim_decoded_element * +__element_alloc(void *ctx, const char *name, enum osim_element_type type, + enum osim_element_repr repr) +{ + struct osim_decoded_element *elem; + + elem = talloc_zero(ctx, struct osim_decoded_element); + if (!elem) + return NULL; + elem->name = name; + elem->type = type; + elem->representation = repr; + + if (elem->type == ELEM_T_GROUP) + INIT_LLIST_HEAD(&elem->u.siblings); + + return elem; +} + + +struct osim_decoded_element * +element_alloc(struct osim_decoded_data *dd, const char *name, + enum osim_element_type type, enum osim_element_repr repr) +{ + struct osim_decoded_element *elem; + + elem = __element_alloc(dd, name, type, repr); + if (!elem) + return NULL; + + llist_add_tail(&elem->list, &dd->decoded_elements); + + return elem; +} + +struct osim_decoded_element * +element_alloc_sub(struct osim_decoded_element *ee, const char *name, + enum osim_element_type type, enum osim_element_repr repr) +{ + struct osim_decoded_element *elem; + + elem = __element_alloc(ee, name, type, repr); + if (!elem) + return NULL; + + llist_add(&elem->list, &ee->u.siblings); + + return elem; +} + + +void add_filedesc(struct osim_file_desc *root, const struct osim_file_desc *in, int num) +{ + int i; + + for (i = 0; i < num; i++) { + struct osim_file_desc *ofd = talloc_memdup(root, &in[i], sizeof(*in)); + llist_add_tail(&ofd->list, &root->child_list); + } +} + +struct osim_file_desc *alloc_df(void *ctx, uint16_t fid, const char *name) +{ + struct osim_file_desc *mf; + + mf = talloc_zero(ctx, struct osim_file_desc); + mf->type = TYPE_DF; + mf->fid = fid; + mf->short_name = name; + INIT_LLIST_HEAD(&mf->child_list); + + return mf; +} + +struct osim_file_desc * +add_df_with_ef(struct osim_file_desc *parent, + uint16_t fid, const char *name, + const struct osim_file_desc *in, int num) +{ + struct osim_file_desc *df; + + df = alloc_df(parent, fid, name); + df->parent = parent; + llist_add_tail(&df->list, &parent->child_list); + add_filedesc(df, in, num); + + return df; +} + +struct osim_file_desc * +add_adf_with_ef(struct osim_file_desc *parent, + const uint8_t *adf_name, uint8_t adf_name_len, + const char *name, const struct osim_file_desc *in, + int num) +{ + struct osim_file_desc *df; + + df = alloc_df(parent, 0xffff, name); + df->type = TYPE_ADF; + df->df_name = adf_name; + df->df_name_len = adf_name_len; + df->parent = parent; + llist_add_tail(&df->list, &parent->child_list); + add_filedesc(df, in, num); + + return df; +} + +struct osim_file_desc * +osim_file_find_name(struct osim_file_desc *parent, const char *name) +{ + struct osim_file_desc *ofd; + llist_for_each_entry(ofd, &parent->child_list, list) { + if (!strcmp(ofd->short_name, name)) { + return ofd; + } + } + return NULL; +} + + + +struct msgb *osim_new_apdumsg(uint8_t cla, uint8_t ins, uint8_t p1, + uint8_t p2, uint16_t lc, uint16_t le) +{ + struct osim_apdu_cmd_hdr *ch; + struct msgb *msg = msgb_alloc(lc+le+sizeof(*ch)+2, "APDU"); + if (!msg) + return NULL; + + ch = (struct osim_apdu_cmd_hdr *) msgb_put(msg, sizeof(*ch)); + msg->l2h = (char *) ch; + + ch->cla = cla; + ch->ins = ins; + ch->p1 = p1; + ch->p2 = p2; + + msgb_apdu_lc(msg) = lc; + msgb_apdu_le(msg) = le; + + if (lc == 0 && le == 0) + msgb_apdu_case(msg) = APDU_CASE_1; + else if (lc == 0 && le >= 1) { + if (le <= 256) + msgb_apdu_case(msg) = APDU_CASE_2; + else + msgb_apdu_case(msg) = APDU_CASE_2_EXT; + } else if (le == 0 && lc >= 1) { + if (lc <= 255) + msgb_apdu_case(msg) = APDU_CASE_3; + else + msgb_apdu_case(msg) = APDU_CASE_3_EXT; + } else if (lc >= 1 && le >= 1) { + if (lc <= 255 & le <= 256) + msgb_apdu_case(msg) = APDU_CASE_4; + else + msgb_apdu_case(msg) = APDU_CASE_4_EXT; + } + + return msg; +} + + + + diff --git a/src/sim/file_codec.c b/src/sim/file_codec.c new file mode 100644 index 00000000..5bf5bc68 --- /dev/null +++ b/src/sim/file_codec.c @@ -0,0 +1,34 @@ + +#include + +#include +#include + +struct osim_decoded_data *osim_file_decode(struct osim_file *file, + int len, uint8_t *data) +{ + struct osim_decoded_data *dd; + + if (!file->desc->ops.parse) + return NULL; + + dd = talloc_zero(file, struct osim_decoded_data); + dd->file = file; + + if (file->desc->ops.parse(dd, file->desc, len, data) < 0) { + talloc_free(dd); + return NULL; + } else + return dd; +} + +struct msgb *osim_file_encode(const struct osim_file_desc *desc, + const struct osim_decoded_data *data) +{ + if (!desc->ops.encode) + return NULL; + + return desc->ops.encode(desc, data); +} + + diff --git a/src/sim/gsm_int.h b/src/sim/gsm_int.h new file mode 100644 index 00000000..3abb3e6f --- /dev/null +++ b/src/sim/gsm_int.h @@ -0,0 +1,12 @@ + +int gsm_hpplmn_decode(struct osim_decoded_data *dd, + const struct osim_file_desc *desc, + int len, uint8_t *data); + +int gsm_lp_decode(struct osim_decoded_data *dd, + const struct osim_file_desc *desc, + int len, uint8_t *data); + +int gsm_imsi_decode(struct osim_decoded_data *dd, + const struct osim_file_desc *desc, + int len, uint8_t *data); diff --git a/src/sim/reader.c b/src/sim/reader.c new file mode 100644 index 00000000..52cf18e9 --- /dev/null +++ b/src/sim/reader.c @@ -0,0 +1,224 @@ +#include +#include +#include +#include +#include + +#include + + +#include "sim_int.h" + +static int get_sw(struct msgb *resp) +{ + int ret; + + if (!msgb_apdu_de(resp) || msgb_apdu_le(resp) < 2) + return -EIO; + + ret = resp->data[resp->len-2] << 8; + ret |= resp->data[resp->len-1]; + + return ret; +} + +/* According to ISO7816-4 Annex A */ +static int transceive_apdu_t0(struct osim_card_hdl *st, struct msgb *amsg) +{ + struct osim_reader_hdl *rh = st->reader; + struct msgb *tmsg = msgb_alloc(1024, "TPDU"); + struct osim_apdu_cmd_hdr *tpduh; + uint8_t *cur; + uint16_t sw; + int rc, num_resp = 0; + + /* create TPDU header from APDU header */ + tpduh = (struct osim_apdu_cmd_hdr *) msgb_put(tmsg, sizeof(*tpduh)); + memcpy(tpduh, msgb_apdu_h(amsg), sizeof(*tpduh)); + + switch (msgb_apdu_case(amsg)) { + case APDU_CASE_1: + tpduh->p3 = 0x00; + break; + case APDU_CASE_2: + tpduh->p3 = msgb_apdu_le(amsg); + break; + case APDU_CASE_2_EXT: + if (msgb_apdu_le(amsg) <= 256) { + /* case 2E.1 */ + tpduh->p3 = msgb_apdu_le(amsg) & 0xff; + } else { + /* case 2E.2 */ + tpduh->p3 = 0; + msgb_put_u16(tmsg, msgb_apdu_le(amsg)); + } + break; + case APDU_CASE_3: + case APDU_CASE_4: + tpduh->p3 = msgb_apdu_lc(amsg); + cur = msgb_put(tmsg, tpduh->p3); + memcpy(cur, msgb_apdu_dc(amsg), tpduh->p3); + break; + case APDU_CASE_3_EXT: + case APDU_CASE_4_EXT: + if (msgb_apdu_lc(amsg) < 256) { + /* Case 3E.1 */ + tpduh->p3 = msgb_apdu_lc(amsg); + } else { + /* Case 3E.2 */ + /* FXIME: Split using ENVELOPE! */ + return -1; + } + break; + } + +transceive_again: + + /* store pointer to start of response */ + tmsg->l3h = tmsg->tail; + + /* transceive */ + rc = rh->ops->transceive(st->reader, tmsg); + if (rc < 0) { + msgb_free(tmsg); + return rc; + } + msgb_apdu_sw(tmsg) = get_sw(tmsg); + + /* increase number of responsese received */ + num_resp++; + + /* save SW */ + sw = msgb_apdu_sw(tmsg); + printf("sw = 0x%04x\n", sw); + msgb_apdu_sw(amsg) = sw; + + switch (msgb_apdu_case(amsg)) { + case APDU_CASE_1: + case APDU_CASE_3: + /* just copy SW */ + break; + case APDU_CASE_2: +case_2s: + switch (sw >> 8) { + case 0x67: /* Case 2S.2: Le definitely not accepted */ + break; + case 0x6c: /* Case 2S.3: Le not accepted, La indicated */ + tpduh->p3 = sw & 0xff; + /* re-issue the command with La as */ + goto transceive_again; + break; + case 0x90: + /* Case 2S.1, fall-through */ + case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: + case 0x96: case 0x97: case 0x98: case 0x99: case 0x9a: + case 0x9b: case 0x9c: case 0x9d: case 0x9e: case 0x9f: + /* Case 2S.4 */ + /* copy response data over */ + cur = msgb_put(amsg, msgb_l3len(tmsg)); + memcpy(cur, tmsg->l3h, msgb_l3len(tmsg)); + } + break; + case APDU_CASE_4: + /* FIXME: this is 4S.2 only for 2nd... response: */ + if (num_resp >= 2) + goto case_2s; + + switch (sw >> 8) { + case 0x60: case 0x62: case 0x63: case 0x64: case 0x65: + case 0x66: case 0x67: case 0x68: case 0x69: case 0x6a: + case 0x6b: case 0x6c: case 0x6d: case 0x6e: case 0x6f: + /* Case 4S.1: Command not accepted: just copy SW */ + break; + case 0x90: + /* case 4S.2: Command accepted */ + tpduh->ins = 0xC0; + tpduh->p1 = tpduh->p2 = 0; + tpduh->p3 = msgb_apdu_le(amsg); + /* strip off current result */ + msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh)); + goto transceive_again; + break; + case 0x61: /* Case 4S.3: command accepted with info added */ + tpduh->ins = 0xC0; + tpduh->p1 = tpduh->p2 = 0; + tpduh->p3 = OSMO_MIN(msgb_apdu_le(amsg), sw & 0xff); + /* strip off current result */ + msgb_get(tmsg, msgb_length(tmsg)-sizeof(*tpduh)); + goto transceive_again; + break; + } + /* Case 4S.2: Command accepted: just copy SW */ + /* Case 4S.4: Just copy SW */ + break; + case APDU_CASE_2_EXT: + if (msgb_apdu_le(amsg) <= 256) { + /* Case 2E.1: Le <= 256 */ + goto case_2s; + } + switch (sw >> 8) { + case 0x67: + /* Case 2E.2a: wrong length, abort */ + break; + case 0x6c: + /* Case 2E.2b: wrong length, La given */ + tpduh->p3 = sw & 0xff; + /* re-issue the command with La as given */ + goto transceive_again; + break; + case 0x90: + /* Case 2E.2c: */ + break; + case 0x61: + /* Case 2E.2d: more data available */ + /* FIXME: issue yet another GET RESPONSE */ + break; + } + break; + case APDU_CASE_3_EXT: + /* FIXME: handling for ENVELOPE splitting */ + break; + case APDU_CASE_4_EXT: + break; + } + + msgb_free(tmsg); + + /* compute total length of response data */ + msgb_apdu_le(amsg) = amsg->tail - msgb_apdu_de(amsg); + + return sw; +} + +/* According to ISO7816-4 Annex B */ +static int transceive_apdu_t1(struct osim_card_hdl *st, struct msgb *amsg) +{ + return -1; +} + +int osim_transceive_apdu(struct osim_chan_hdl *st, struct msgb *amsg) +{ + /* FIXME: check for protocol */ + return transceive_apdu_t0(st->card, amsg); +} + + + +struct osim_reader_hdl *osim_reader_open(int idx, const char *name) +{ + /* FIXME: support multiple drivers */ + const struct osim_reader_ops *ops = &pcsc_reader_ops; + struct osim_reader_hdl *rh; + + rh = ops->reader_open(idx, name); + if (!rh) + return NULL; + rh->ops = ops; + + return rh; +} + +struct osim_card_hdl *osim_card_open(struct osim_reader_hdl *rh) +{ + return rh->ops->card_open(rh); +} diff --git a/src/sim/reader_pcsc.c b/src/sim/reader_pcsc.c new file mode 100644 index 00000000..0a36f49c --- /dev/null +++ b/src/sim/reader_pcsc.c @@ -0,0 +1,133 @@ +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include "sim_int.h" + +#define PCSC_ERROR(rv, text) \ +if (rv != SCARD_S_SUCCESS) { \ + fprintf(stderr, text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \ + goto end; \ +} else { \ + printf(text ": OK\n\n"); \ +} + + + +struct pcsc_reader_state { + SCARDCONTEXT hContext; + SCARDHANDLE hCard; + DWORD dwActiveProtocol; + const SCARD_IO_REQUEST *pioSendPci; + SCARD_IO_REQUEST pioRecvPci; + char *name; +}; + +static struct osim_reader_hdl *pcsc_reader_open(int num, const char *id, void *ctx) +{ + struct osim_reader_hdl *rh; + struct pcsc_reader_state *st; + long rc; + LPSTR mszReaders = NULL; + DWORD dwReaders; + unsigned int num_readers; + char *ptr; + + /* FIXME: implement matching on id or num */ + + rh = talloc_zero(ctx, struct osim_reader_hdl); + st = rh->priv = talloc_zero(rh, struct pcsc_reader_state); + + rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, + &st->hContext); + if (rc != SCARD_S_SUCCESS) + goto end; + + dwReaders = SCARD_AUTOALLOCATE; + rc = SCardListReaders(st->hContext, NULL, (LPSTR)&mszReaders, &dwReaders); + PCSC_ERROR(rc, "SCardListReaders"); + + num_readers = 0; + ptr = mszReaders; + while (*ptr != '\0') { + ptr += strlen(ptr)+1; + num_readers++; + } + + if (num_readers == 0) + goto end; + + st->name = talloc_strdup(rh, mszReaders); + st->dwActiveProtocol = -1; + + return rh; +end: + talloc_free(rh); + return NULL; +} + +static struct osim_card_hdl *pcsc_card_open(struct osim_reader_hdl *rh) +{ + struct pcsc_reader_state *st = rh->priv; + struct osim_card_hdl *card; + struct osim_chan_hdl *chan; + int rc; + + rc = SCardConnect(st->hContext, st->name, SCARD_SHARE_SHARED, + SCARD_PROTOCOL_T0, &st->hCard, &st->dwActiveProtocol); + PCSC_ERROR(rc, "SCardConnect"); + + st->pioSendPci = SCARD_PCI_T0; + + card = talloc_zero(rh, struct osim_card_hdl); + INIT_LLIST_HEAD(&card->channels); + card->reader = rh; + rh->card = card; + + /* create a default channel */ + chan = talloc_zero(card, struct osim_chan_hdl); + chan->card = card; + llist_add(&chan->list, &card->channels); + + return card; + +end: + return NULL; +} + + +static int pcsc_transceive(struct osim_reader_hdl *rh, struct msgb *msg) +{ + struct pcsc_reader_state *st = rh->priv; + DWORD rlen = msgb_tailroom(msg); + int rc; + + printf("TX: %s\n", osmo_hexdump(msg->data, msg->len)); + + rc = SCardTransmit(st->hCard, st->pioSendPci, msg->data, msgb_length(msg), + &st->pioRecvPci, msg->tail, &rlen); + PCSC_ERROR(rc, "SCardEndTransaction"); + + printf("RX: %s\n", osmo_hexdump(msg->tail, rlen)); + msgb_put(msg, rlen); + msgb_apdu_le(msg) = rlen; + + return 0; +end: + return -EIO; +} + +const struct osim_reader_ops pcsc_reader_ops = { + .name = "PC/SC", + .reader_open = pcsc_reader_open, + .card_open = pcsc_card_open, + .transceive = pcsc_transceive, +}; + diff --git a/src/sim/sim_int.h b/src/sim/sim_int.h new file mode 100644 index 00000000..411c3225 --- /dev/null +++ b/src/sim/sim_int.h @@ -0,0 +1,38 @@ +#ifndef _SIM_INT_H + +#include + +struct osim_decoded_element * +element_alloc(struct osim_decoded_data *dd, const char *name, + enum osim_element_type type, enum osim_element_repr repr); + +struct osim_decoded_element * +element_alloc_sub(struct osim_decoded_element *ee, const char *name, + enum osim_element_type type, enum osim_element_repr repr); + +extern const struct osim_card_sw ts102221_uicc_sw[0]; + + +void add_filedesc(struct osim_file_desc *root, const struct osim_file_desc *in, int num); +struct osim_file_desc *alloc_df(void *ctx, uint16_t fid, const char *name); +struct osim_file_desc * +add_df_with_ef(struct osim_file_desc *parent, + uint16_t fid, const char *name, + const struct osim_file_desc *in, int num); + +struct osim_file_desc * +add_adf_with_ef(struct osim_file_desc *parent, + const uint8_t *adf_name, uint8_t adf_name_len, + const char *name, const struct osim_file_desc *in, + int num); + +struct osim_reader_ops { + const char *name; + struct osim_reader_hdl *(*reader_open)(int idx, const char *name); + struct osim_card_hdl *(*card_open)(struct osim_reader_hdl *rh); + int (*transceive)(struct osim_reader_hdl *rh, struct msgb *msg); +}; + +const struct osim_reader_ops pcsc_reader_ops; + +#endif diff --git a/utils/Makefile.am b/utils/Makefile.am index 51c884a1..7d784bd2 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -3,10 +3,15 @@ AM_CPPFLAGS = -I$(top_srcdir)/include AM_CFLAGS = -Wall bin_PROGRAMS = osmo-arfcn osmo-auc-gen +noinst_PROGRAMS = osmo-sim-test osmo_arfcn_SOURCES = osmo-arfcn.c osmo_arfcn_LDADD = $(top_builddir)/src/libosmocore.la $(top_builddir)/src/gsm/libosmogsm.la osmo_auc_gen_SOURCES = osmo-auc-gen.c osmo_auc_gen_LDADD = $(top_builddir)/src/libosmocore.la $(top_builddir)/src/gsm/libosmogsm.la + +osmo_sim_test_SOURCES = osmo-sim-test.c +osmo_sim_test_LDADD = $(top_builddir)/src/libosmocore.la $(top_builddir)/src/gsm/libosmogsm.la $(top_builddir)/src/sim/libosmosim.la -lpcsclite +osmo_sim_test_CFLAGS = -I/usr/include/PCSC endif diff --git a/utils/osmo-sim-test.c b/utils/osmo-sim-test.c new file mode 100644 index 00000000..5fbb03f2 --- /dev/null +++ b/utils/osmo-sim-test.c @@ -0,0 +1,370 @@ +#include +#include +#include +#include + +#include +#include +#include +#include + + + + + + + +/* 11.1.1 */ +static struct msgb *_select_file(struct osim_chan_hdl *st, uint8_t p1, uint8_t p2, + const uint8_t *data, uint8_t data_len) +{ + struct msgb *msg, *resp; + char *dst; + + msg = osim_new_apdumsg(0x00, 0xA4, p1, p2, data_len, 256); + dst = msgb_put(msg, data_len); + memcpy(dst, data, data_len); + + osim_transceive_apdu(st, msg); + + return msg; +} + +/* 11.1.1 */ +static struct msgb *select_adf(struct osim_chan_hdl *st, const uint8_t *adf, uint8_t adf_len) +{ + int sw; + + return _select_file(st, 0x04, 0x04, adf,adf_len); +} + +/* 11.1.1 */ +static struct msgb *select_file(struct osim_chan_hdl *st, uint16_t fid) +{ + uint16_t cfid = htons(fid); + + return _select_file(st, 0x00, 0x04, (uint8_t *)&cfid, 2); +} + +/* 11.1.9 */ +static int verify_pin(struct osim_chan_hdl *st, uint8_t pin_nr, uint8_t *pin) +{ + struct msgb *msg; + char *pindst; + int sw; + + if (strlen(pin) > 8) + return -EINVAL; + + msg = osim_new_apdumsg(0x00, 0x20, 0x00, pin_nr, 8, 0); + pindst = msgb_put(msg, 8); + memset(pindst, 0xFF, 8); + strncpy(pindst, pin, strlen(pin)); + + return osim_transceive_apdu(st, msg); +} + +/* 11.1.5 */ +static struct msgb *read_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr, uint16_t rec_size) +{ + struct msgb *msg; + + msg = osim_new_apdumsg(0x00, 0xB2, rec_nr, 0x04, 0, rec_size); + + osim_transceive_apdu(st, msg); + + return msg; +} + +/* 11.1.6 */ +static struct msgb *update_record_nr(struct osim_chan_hdl *st, uint8_t rec_nr, + const uint8_t *data, uint16_t rec_size) +{ + struct msgb *msg; + uint8_t *cur; + + msg = osim_new_apdumsg(0x00, 0xDC, rec_nr, 0x04, rec_size, 0); + cur = msgb_put(msg, rec_size); + memcpy(cur, data, rec_size); + + osim_transceive_apdu(st, msg); + + return msg; +} + +/* 11.1.3 */ +static struct msgb *read_binary(struct osim_chan_hdl *st, uint16_t offset, uint16_t len) +{ + struct msgb *msg; + + if (offset > 0x7fff || len > 256) + return NULL; + + msg = osim_new_apdumsg(0x00, 0xB0, offset >> 8, offset & 0xff, 0, len & 0xff); + + osim_transceive_apdu(st, msg); + + return msg; +} + +/* 11.1.4 */ +static struct msgb *update_binary(struct osim_chan_hdl *st, uint16_t offset, + const uint8_t *data, uint16_t len) +{ + struct msgb *msg; + uint8_t *cur; + + if (offset > 0x7fff || len > 256) + return NULL; + + msg = osim_new_apdumsg(0x00, 0xD6, offset >> 8, offset & 0xff, len & 0xff, 0); + cur = msgb_put(msg, len); + memcpy(cur, data, len); + + osim_transceive_apdu(st, msg); + + return msg; +} + +static int dump_fcp_template(struct tlv_parsed *tp) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(tp->lv); i++) { + if (TLVP_PRESENT(tp, i)) + printf("Tag 0x%02x (%s): %s\n", i, + get_value_string(ts102221_fcp_vals, i), + osmo_hexdump(TLVP_VAL(tp, i), TLVP_LEN(tp, i))); + } + + return 0; +} + +static int dump_fcp_template_msg(struct msgb *msg) +{ + struct tlv_parsed tp; + int rc; + + rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-4, 0, 0); + if (rc < 0) + return rc; + + return dump_fcp_template(&tp); +} + +struct osim_fcp_fd_decoded { + enum osim_file_type type; + enum osim_ef_type ef_type; + uint16_t rec_len; + uint8_t num_rec; +}; + +static const enum osim_file_type iso2ftype[8] = { + [0] = TYPE_EF, + [1] = TYPE_EF_INT, + [7] = TYPE_DF, +}; + +static const enum osim_ef_type iso2eftype[8] = { + [1] = EF_TYPE_TRANSP, + [2] = EF_TYPE_RECORD_FIXED, + [6] = EF_TYPE_RECORD_CYCLIC, +}; + +static int osim_fcp_fd_decode(struct osim_fcp_fd_decoded *ofd, const uint8_t *fcp, int fcp_len) +{ + memset(ofd, 0, sizeof(*ofd)); + + if (fcp_len != 2 && fcp_len != 5) + return -EINVAL; + + ofd->type = iso2ftype[(fcp[0] >> 3) & 7]; + if (ofd->type != TYPE_DF) + ofd->ef_type = iso2eftype[fcp[0] & 7]; + + if (fcp[1] != 0x21) + return -EINVAL; + + if (fcp_len >= 5) { + ofd->rec_len = ntohs(*(uint16_t *)(fcp+2)); + ofd->num_rec = fcp[4]; + } + + return 0; +} + +extern struct osim_card_profile *osim_cprof_usim(void *ctx); + +static struct msgb *try_select_adf_usim(struct osim_chan_hdl *st) +{ + struct tlv_parsed tp; + struct osim_fcp_fd_decoded ofd; + struct msgb *msg, *msg2; + uint8_t *cur; + int rc, i; + + msg = select_file(st, 0x2f00); + rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-4, 0, 0); + if (rc < 0) + return NULL; + + dump_fcp_template(&tp); + + if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) || + TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 5) { + msgb_free(msg); + return NULL; + } + + rc = osim_fcp_fd_decode(&ofd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC), + TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC)); + if (rc < 0) { + msgb_free(msg); + return NULL; + } + + if (ofd.type != TYPE_EF || ofd.ef_type != EF_TYPE_RECORD_FIXED) { + msgb_free(msg); + return NULL; + } + + msgb_free(msg); + + printf("ofd rec_len = %u, num_rec = %u\n", ofd.rec_len, ofd.num_rec); + + for (i = 0; i < ofd.num_rec; i++) { + msg = read_record_nr(st, i+1, ofd.rec_len); + if (!msg) + return NULL; + + cur = msgb_apdu_de(msg); + if (msgb_apdu_le(msg) < 5) { + msgb_free(msg); + return NULL; + } + + if (cur[0] != 0x61 || cur[1] < 0x03 || cur[1] > 0x7f || + cur[2] != 0x4F || cur[3] < 0x01 || cur[3] > 0x10) { + msgb_free(msg); + return NULL; + } + + /* FIXME: actually check if it is an AID that we support, or + * iterate until we find one that we support */ + + msg2 = select_adf(st, cur+4, cur[3]); + + /* attach the USIM profile, FIXME: do this based on AID match */ + st->card->prof = osim_cprof_usim(st->card); + st->cwd = osim_file_find_name(st->card->prof->mf, "ADF.USIM"); + + msgb_free(msg); + + return msg2; + } + + return NULL; +} + +static int dump_file(struct osim_chan_hdl *chan, uint16_t fid) +{ + struct tlv_parsed tp; + struct osim_fcp_fd_decoded ffdd; + struct msgb *msg; + int rc, i; + + msg = select_file(chan, fid); + if (!msg) + return -EIO; + if (msgb_apdu_sw(msg) != 0x9000) + goto out; + + rc = tlv_parse(&tp, &ts102221_fcp_tlv_def, msgb_apdu_de(msg)+2, msgb_apdu_le(msg)-4, 0, 0); + if (rc < 0) + goto out; + + if (!TLVP_PRESENT(&tp, UICC_FCP_T_FILE_DESC) || + TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC) < 5) + goto out; + + rc = osim_fcp_fd_decode(&ffdd, TLVP_VAL(&tp, UICC_FCP_T_FILE_DESC), + TLVP_LEN(&tp, UICC_FCP_T_FILE_DESC)); + if (rc < 0) + goto out; + + if (ffdd.type != TYPE_EF) + goto out; + + switch (ffdd.ef_type) { + case EF_TYPE_RECORD_FIXED: + for (i = 0; i < ffdd.num_rec; i++) { + struct msgb *rmsg = read_record_nr(chan, i+1, ffdd.rec_len); + if (!msg) + return NULL; + printf("Rec %03u: %s\n", i+1, + osmo_hexdump(msgb_apdu_de(rmsg), msgb_apdu_le(rmsg))); + } + break; + case EF_TYPE_TRANSP: + break; + default: + goto out; + } + +out: + msgb_free(msg); + return -EINVAL; +} + +int main(int argc, char **argv) +{ + struct osim_reader_hdl *reader; + struct osim_card_hdl *card; + struct osim_chan_hdl *chan; + struct msgb *msg; + int rc; + + reader = osim_reader_open(0, NULL); + if (!reader) + exit(1); + card = osim_card_open(reader); + if (!card) + exit(2); + chan = llist_entry(card->channels.next, struct osim_chan_hdl, list); + if (!chan) + exit(3); + + msg = try_select_adf_usim(chan); + if (!msg || msgb_apdu_sw(msg) != 0x9000) + exit(4); + dump_fcp_template_msg(msg); + msgb_free(msg); + + msg = select_file(chan, 0x6fc5); + dump_fcp_template_msg(msg); + msgb_free(msg); + + verify_pin(chan, 1, "1653"); + + msg = select_file(chan, 0x6f06); + dump_fcp_template_msg(msg); + msgb_free(msg); + +#if 1 + { + struct osim_file_desc *ofd; + llist_for_each_entry(ofd, &chan->cwd->child_list, list) { + struct msgb *m; + printf("\n\n========