libosmosim 1.13.1.1-afb4 Osmocom SIM library

Functions
core.c File Reference

Core routines for SIM/UICC/USIM access. More...

`#include <stdlib.h>`
`#include <stdint.h>`
`#include <string.h>`
`#include <errno.h>`
`#include <osmocom/core/talloc.h>`
`#include <osmocom/sim/sim.h>`
`#include "sim_int.h"`

## Functions
struct osim_decoded_data * | osim_file_decode (struct osim_file *file, int len, uint8_t *data)
struct msgb * | osim_file_encode (const struct osim_file_desc *desc, const struct osim_decoded_data *data)
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 * | 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)
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 * | alloc_adf_with_ef (void *ctx, 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 * | osim_file_desc_find_name (struct osim_file_desc *parent, const char *name)
struct osim_file_desc * | osim_file_desc_find_aid (struct osim_file_desc *parent, const uint8_t *aid, uint8_t aid_len)
struct osim_file_desc * | osim_file_desc_find_fid (struct osim_file_desc *parent, uint16_t fid)
struct osim_file_desc * | osim_file_desc_find_sfid (struct osim_file_desc *parent, uint8_t sfid)
static | LLIST_HEAD (g_app_profiles)
void | osim_app_profile_register (struct osim_card_app_profile *aprof)
| Register an application profile. More...
const struct osim_card_app_profile * | osim_app_profile_find_by_name (const char *name)
| Find any registered application profile based on its name (e.g. More...
const struct osim_card_app_profile * | osim_app_profile_find_by_aid (const uint8_t *aid, uint8_t aid_len)
| Find any registered application profile based on its AID. More...
struct osim_card_app_hdl * | osim_card_hdl_find_app (struct osim_card_hdl *ch, const uint8_t *aid, uint8_t aid_len)
int | osim_card_hdl_add_app (struct osim_card_hdl *ch, const uint8_t *aid, uint8_t aid_len, const char *label)
| Add an application to a given card. More...
struct msgb * | osim_new_apdumsg (uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2, uint16_t lc, uint16_t le)
| Generate an APDU message and initialize APDU command header. More...
char * | osim_print_sw_buf (char *buf, size_t buf_len, const struct osim_chan_hdl *ch, uint16_t sw_in)
char * | osim_print_sw (const struct osim_chan_hdl *ch, uint16_t sw_in)
char * | osim_print_sw_c (const void *ctx, const struct osim_chan_hdl *ch, uint16_t sw_in)
const struct osim_card_sw * | osim_cprof_find_sw (const struct osim_card_profile *cp, uint16_t sw_in)
| Find status word within given card profile. More...
const struct osim_card_sw * | osim_app_profile_find_sw (const struct osim_card_app_profile *ap, uint16_t sw_in)
| Find application-specific status word within given card application profile. More...
enum osim_card_sw_class | osim_sw_class (const struct osim_chan_hdl *ch, uint16_t sw_in)
int | default_decode (struct osim_decoded_data *dd, const struct osim_file_desc *desc, int len, uint8_t *data)
int | osim_init (void *ctx)

## Detailed Description

Core routines for SIM/UICC/USIM access.

## Function Documentation

## ◆ __element_alloc()

static struct osim_decoded_element * __element_alloc | ( | void * | ctx,
|  | const char * | name,
|  | enum osim_element_type | type,
|  | enum osim_element_repr | repr
| ) |  |
| static

References ELEM_T_GROUP, INIT_LLIST_HEAD, osim_decoded_element::name, osim_decoded_element::representation, osim_decoded_element::siblings, osim_decoded_element::type, and osim_decoded_element::u.

Referenced by element_alloc(), and element_alloc_sub().

## ◆ add_df_with_ef()

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
| ) |  |

References add_filedesc(), alloc_df(), osim_file_desc::child_list, osim_file_desc::fid, osim_file_desc::list, llist_add_tail(), name, and osim_file_desc::parent.

Referenced by osim_aprof_usim(), osim_cprof_tsim(), osim_int_cprof_add_gsm(), and osim_int_cprof_add_telecom().

## ◆ add_filedesc()

void add_filedesc | ( | struct osim_file_desc * | root,
|  | const struct osim_file_desc * | in,
|  | int | num
| ) |  |

References osim_file_desc::child_list, osim_file_desc::list, and llist_add_tail().

Referenced by add_df_with_ef(), alloc_adf_with_ef(), osim_cprof_sim(), osim_cprof_tsim(), and osim_cprof_uicc().

## ◆ alloc_adf_with_ef()

struct osim_file_desc * alloc_adf_with_ef | ( | void * | ctx,
|  | const uint8_t * | adf_name,
|  | uint8_t | adf_name_len,
|  | const char * | name,
|  | const struct osim_file_desc * | in,
|  | int | num
| ) |  |

References add_filedesc(), alloc_df(), osim_file_desc::df_name, osim_file_desc::df_name_len, name, osim_file_desc::type, and TYPE_ADF.

Referenced by osim_aprof_hpsim(), osim_aprof_isim(), and osim_aprof_usim().

## ◆ alloc_df()

struct osim_file_desc * alloc_df | ( | void * | ctx,
|  | uint16_t | fid,
|  | const char * | name
| ) |  |

References osim_file_desc::child_list, osim_file_desc::fid, INIT_LLIST_HEAD, name, osim_file_desc::short_name, osim_file_desc::type, and TYPE_DF.

Referenced by add_df_with_ef(), alloc_adf_with_ef(), osim_cprof_sim(), osim_cprof_tsim(), and osim_cprof_uicc().

## ◆ default_decode()

int default_decode | ( | struct osim_decoded_data * | dd,
|  | const struct osim_file_desc * | desc,
|  | int | len,
|  | uint8_t * | data
| ) |  |

References osim_decoded_element::buf, data, ELEM_REPR_HEX, ELEM_T_BYTES, element_alloc(), len(), and osim_decoded_element::u.

## ◆ element_alloc()

struct osim_decoded_element * element_alloc | ( | struct osim_decoded_data * | dd,
|  | const char * | name,
|  | enum osim_element_type | type,
|  | enum osim_element_repr | repr
| ) |  |

References __element_alloc(), osim_decoded_data::decoded_elements, osim_decoded_element::list, llist_add_tail(), osim_decoded_element::name, and osim_decoded_element::type.

Referenced by default_decode(), elp_decode(), gsm_hpplmn_decode(), gsm_imsi_decode(), gsm_kc_decode(), gsm_lp_decode(), gsm_plmnsel_decode(), and iccid_decode().

## ◆ element_alloc_sub()

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
| ) |  |

References __element_alloc(), osim_decoded_element::list, llist_add(), osim_decoded_element::name, osim_decoded_element::siblings, osim_decoded_element::type, and osim_decoded_element::u.

Referenced by gsm_plmnsel_decode().

## ◆ LLIST_HEAD()

static LLIST_HEAD | ( | g_app_profiles |  | ) |
| static

## ◆ osim_app_profile_find_by_aid()

const struct osim_card_app_profile * osim_app_profile_find_by_aid | ( | const uint8_t * | aid,
|  | uint8_t | aid_len
| ) |  |

Find any registered application profile based on its AID.

References osim_card_app_profile::aid, osim_card_app_profile::aid_len, osim_card_app_profile::list, and llist_for_each_entry.

Referenced by osim_app_profile_register(), and osim_card_hdl_add_app().

## ◆ osim_app_profile_find_by_name()

const struct osim_card_app_profile * osim_app_profile_find_by_name | ( | const char * | name | ) |

Find any registered application profile based on its name (e.g.

"ADF.USIM")

References osim_card_app_profile::list, llist_for_each_entry, and osim_card_app_profile::name.

Referenced by osim_app_profile_register().

## ◆ osim_app_profile_find_sw()

const struct osim_card_sw * osim_app_profile_find_sw | ( | const struct osim_card_app_profile * | ap,
|  | uint16_t | sw_in
| ) |  |

Find application-specific status word within given card application profile.

References sw, and osim_card_app_profile::sw.

Referenced by osim_print_sw_buf(), and osim_sw_class().

## ◆ osim_app_profile_register()

void osim_app_profile_register | ( | struct osim_card_app_profile * | aprof | ) |

Register an application profile.

Typically called at early start-up.

References osim_card_app_profile::aid, osim_card_app_profile::aid_len, osim_card_app_profile::list, llist_add_tail(), osim_card_app_profile::name, osim_app_profile_find_by_aid(), osim_app_profile_find_by_name(), and OSMO_ASSERT.

Referenced by osim_init().

## ◆ osim_card_hdl_add_app()

int osim_card_hdl_add_app | ( | struct osim_card_hdl * | ch,
|  | const uint8_t * | aid,
|  | uint8_t | aid_len,
|  | const char * | label
| ) |  |

Add an application to a given card.

References osim_card_app_hdl::aid, osim_card_app_hdl::aid_len, osim_card_hdl::apps, osim_card_app_hdl::label, osim_card_app_hdl::list, llist_add_tail(), MAX_AID_LEN, osim_app_profile_find_by_aid(), osim_card_hdl_find_app(), and osim_card_app_hdl::prof.

## ◆ osim_card_hdl_find_app()

struct osim_card_app_hdl * osim_card_hdl_find_app | ( | struct osim_card_hdl * | ch,
|  | const uint8_t * | aid,
|  | uint8_t | aid_len
| ) |  |

References osim_card_app_hdl::aid, osim_card_app_hdl::aid_len, osim_card_hdl::apps, osim_card_app_hdl::list, llist_for_each_entry, and MAX_AID_LEN.

Referenced by osim_card_hdl_add_app().

## ◆ osim_cprof_find_sw()

const struct osim_card_sw * osim_cprof_find_sw | ( | const struct osim_card_profile * | cp,
|  | uint16_t | sw_in
| ) |  |

Find status word within given card profile.

References sw, and osim_card_profile::sws.

Referenced by osim_print_sw_buf(), and osim_sw_class().

## ◆ osim_file_decode()

struct osim_decoded_data * osim_file_decode | ( | struct osim_file * | file,
|  | int | len,
|  | uint8_t * | data
| ) |  |

References data, file(), osim_decoded_data::file, and len().

## ◆ osim_file_desc_find_aid()

struct osim_file_desc * osim_file_desc_find_aid | ( | struct osim_file_desc * | parent,
|  | const uint8_t * | aid,
|  | uint8_t | aid_len
| ) |  |

References osim_file_desc::child_list, osim_file_desc::df_name, osim_file_desc::df_name_len, osim_file_desc::list, llist_for_each_entry, osim_file_desc::parent, osim_file_desc::type, and TYPE_ADF.

## ◆ osim_file_desc_find_fid()

struct osim_file_desc * osim_file_desc_find_fid | ( | struct osim_file_desc * | parent,
|  | uint16_t | fid
| ) |  |

References osim_file_desc::child_list, osim_file_desc::fid, osim_file_desc::list, llist_for_each_entry, and osim_file_desc::parent.

## ◆ osim_file_desc_find_name()

struct osim_file_desc * osim_file_desc_find_name | ( | struct osim_file_desc * | parent,
|  | const char * | name
| ) |  |

References osim_file_desc::child_list, osim_file_desc::list, llist_for_each_entry, name, osim_file_desc::parent, and osim_file_desc::short_name.

## ◆ osim_file_desc_find_sfid()

struct osim_file_desc * osim_file_desc_find_sfid | ( | struct osim_file_desc * | parent,
|  | uint8_t | sfid
| ) |  |

References osim_file_desc::child_list, osim_file_desc::list, llist_for_each_entry, osim_file_desc::parent, SFI_NONE, and osim_file_desc::sfid.

## ◆ osim_file_encode()

struct msgb * osim_file_encode | ( | const struct osim_file_desc * | desc,
|  | const struct osim_decoded_data * | data
| ) |  |

References data.

## ◆ osim_init()

int osim_init | ( | void * | ctx | ) |

References osim_app_profile_register(), osim_aprof_hpsim(), osim_aprof_isim(), and osim_aprof_usim().

## ◆ osim_new_apdumsg()

struct msgb * osim_new_apdumsg | ( | uint8_t | cla,
|  | uint8_t | ins,
|  | uint8_t | p1,
|  | uint8_t | p2,
|  | uint16_t | lc,
|  | uint16_t | le
| ) |  |

Generate an APDU message and initialize APDU command header.

Parameters

  [in] | cla | CLASS byte
  [in] | ins | INSTRUCTION byte
  [in] | p1 | Parameter 1 byte
  [in] | p2 | Parameter 2 byte
  [in] | lc | number of bytes in the command data field Nc, which will encoded in 0, 1 or 3 bytes into Lc
  [in] | le | maximum number of bytes expected in the response data field, which will encoded in 0, 1, 2 or 3 bytes into Le

Returns
  an APDU message generated using provided APDU parameters

This function generates an APDU message, as defined in ISO/IEC 7816-4:2005(E) §5.1. The APDU command header, command and response fields lengths are initialized using the parameters. The APDU case is determined by the command and response fields lengths.

References APDU_CASE_1, APDU_CASE_2E, APDU_CASE_2S, APDU_CASE_3E, APDU_CASE_3S, APDU_CASE_4E, APDU_CASE_4S, cla, osim_apdu_cmd_hdr::cla, ins, osim_apdu_cmd_hdr::ins, msgb::l2h, lc, le, msgb_alloc(), msgb_apdu_case, msgb_apdu_lc, msgb_apdu_le, msgb_put(), p1, osim_apdu_cmd_hdr::p1, p2, and osim_apdu_cmd_hdr::p2.

## ◆ osim_print_sw()

char * osim_print_sw | ( | const struct osim_chan_hdl * | ch,
|  | uint16_t | sw_in
| ) |  |

References osim_print_sw_buf().

## ◆ osim_print_sw_buf()

char * osim_print_sw_buf | ( | char * | buf,
|  | size_t | buf_len,
|  | const struct osim_chan_hdl * | ch,
|  | uint16_t | sw_in
| ) |  |

References osim_chan_hdl::card, osim_chan_hdl::cur_app, osim_app_profile_find_sw(), osim_cprof_find_sw(), osim_card_app_hdl::prof, osim_card_hdl::prof, osim_card_sw::str, SW_TYPE_STR, osim_card_sw::type, and osim_card_sw::u.

Referenced by osim_print_sw(), and osim_print_sw_c().

## ◆ osim_print_sw_c()

char * osim_print_sw_c | ( | const void * | ctx,
|  | const struct osim_chan_hdl * | ch,
|  | uint16_t | sw_in
| ) |  |

References osim_print_sw_buf().

## ◆ osim_sw_class()

enum osim_card_sw_class osim_sw_class | ( | const struct osim_chan_hdl * | ch,
|  | uint16_t | sw_in
| ) |  |

References osim_chan_hdl::card, osim_card_sw::class, osim_chan_hdl::cur_app, osim_app_profile_find_sw(), osim_cprof_find_sw(), OSMO_ASSERT, osim_card_app_hdl::prof, osim_card_hdl::prof, and SW_CLS_NONE.