From afc2cb84e3d36beb0d4311b70b10e86862302f1a Mon Sep 17 00:00:00 2001 From: Yang Gu Date: Fri, 2 Apr 2010 14:20:28 +0800 Subject: [PATCH] Add parser for c-apdu objects --- src/stkutil.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/stkutil.h | 15 +++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/stkutil.c b/src/stkutil.c index a96132cc..99e66229 100644 --- a/src/stkutil.c +++ b/src/stkutil.c @@ -789,6 +789,48 @@ static gboolean parse_dataobj_card_atr( return TRUE; } +/* Defined in TS 102.223 Section 8.35 */ +static gboolean parse_dataobj_c_apdu( + struct comprehension_tlv_iter *iter, void *user) +{ + struct stk_c_apdu *ca = user; + const unsigned char *data; + unsigned int len = comprehension_tlv_iter_get_length(iter); + + if ((len < 4) || (len > 241)) + return FALSE; + + data = comprehension_tlv_iter_get_data(iter); + ca->cla = data[0]; + ca->ins = data[1]; + ca->p1 = data[2]; + ca->p2 = data[3]; + + /* lc is 0 has the same meaning as lc is absent. But le is 0 means + * the maximum number of bytes expected in the response data field + * is 256. So we need to rely on has_le to know if it presents. + */ + if (len > 5) { + ca->lc = data[4]; + if (ca->lc > sizeof(ca->data)) + return FALSE; + + memcpy(ca->data, data+5, ca->lc); + + if ((len - ca->lc) == 6) { + ca->le = data[len-1]; + ca->has_le = TRUE; + } else if (len - ca->lc != 5) + return FALSE; + } else if (len == 5) { + ca->lc = 0; + ca->le = data[4]; + ca->has_le = TRUE; + } + + return TRUE; +} + /* Defined in 102.223 Section 8.43 */ static gboolean parse_dataobj_imm_resp(struct comprehension_tlv_iter *iter, void *user) @@ -897,6 +939,8 @@ static dataobj_handler handler_for_type(enum stk_data_object_type type) return parse_dataobj_card_reader_status; case STK_DATA_OBJECT_TYPE_CARD_ATR: return parse_dataobj_card_atr; + case STK_DATA_OBJECT_TYPE_C_APDU: + return parse_dataobj_c_apdu; case STK_DATA_OBJECT_TYPE_IMMEDIATE_RESPONSE: return parse_dataobj_imm_resp; case STK_DATA_OBJECT_TYPE_TEXT_ATTRIBUTE: diff --git a/src/stkutil.h b/src/stkutil.h index 8047ed11..c6bd4e11 100644 --- a/src/stkutil.h +++ b/src/stkutil.h @@ -468,6 +468,21 @@ struct stk_card_atr { unsigned int len; }; +/* + * Defined in TS 102.223 Section 8.35. According to it, the maximum size + * of data is 236. + */ +struct stk_c_apdu { + unsigned char cla; + unsigned char ins; + unsigned char p1; + unsigned char p2; + unsigned char lc; + unsigned char data[236]; + ofono_bool_t has_le; + unsigned char le; +}; + /* * According to 102.223 Section 8.72 the length of text attribute CTLV is 1 * byte. This means that the maximum size is 127 according to the rules