Skip to contentSkip to conversation

Do the PIN check in SIMManager

RFC PATCH In review Andrzej Zaborowski 4 participants 7 messages Sep 5, 2009 – Sep 23, 2009

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 Do the PIN check in SIMManager

This adds checking whether PIN is required during SIM initialisation and delaying the sim ready notifications until after correct PIN is given. According to 31.102 the IMSI should be retrieved after the PIN check however on the SIMs I've tried it can be retrieved before authentication and I think it would make sense to provide the imsi to the clients so they know which card they're authenticating to if they're swapping cards.

---
 drivers/atmodem/sim.c |  171 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/sim.h         |   13 ++++
 include/types.h       |   20 ++++++
 src/sim.c             |  136 ++++++++++++++++++++++++++++++++++++++-
 src/simutil.c         |   24 +++++++
 src/simutil.h         |    2 +
 6 files changed, 365 insertions(+), 1 deletions(-)
drivers/atmodem/sim.c +171 -0
index 8333670..fdb972a 100644
@@ -431,6 +431,174 @@ error:
 	}
 }
 
+static struct {
+	enum ofono_sim_passwd_type type;
+	const char *name;
+} const at_sim_name[] = {
+	{ OFONO_PASSWD_SIM_PIN, "SIM PIN" },
+	{ OFONO_PASSWD_SIM_PUK, "SIM PUK" },
+	{ OFONO_PASSWD_PHSIM_PIN, "PH-SIM PIN" },
+	{ OFONO_PASSWD_PHFSIM_PIN, "PH-FSIM PIN" },
+	{ OFONO_PASSWD_PHFSIM_PUK, "PH-FSIM PUK" },
+	{ OFONO_PASSWD_SIM_PIN2, "SIM PIN2" },
+	{ OFONO_PASSWD_SIM_PUK2, "SIM PUK2" },
+	{ OFONO_PASSWD_PHNET_PIN, "PH-NET PIN" },
+	{ OFONO_PASSWD_PHNET_PUK, "PH-NET PUK" },
+	{ OFONO_PASSWD_PHNETSUB_PIN, "PH-NETSUB PIN" },
+	{ OFONO_PASSWD_PHNETSUB_PUK, "PH-NETSUB PUK" },
+	{ OFONO_PASSWD_PHSP_PIN, "PH-SP PIN" },
+	{ OFONO_PASSWD_PHSP_PUK, "PH-SP PUK" },
+	{ OFONO_PASSWD_PHCORP_PIN, "PH-CORP PIN" },
+	{ OFONO_PASSWD_PHCORP_PUK, "PH-CORP PUK" },
+};
+
+static void at_cpin_cb(gboolean ok, GAtResult *result, gpointer user_data)
+{
+	struct cb_data *cbd = user_data;
+	GAtResultIter iter;
+	ofono_sim_passwd_cb_t cb = cbd->cb;
+	struct ofono_error error;
+	const char *pin_required;
+	int pin_type;
+	int i;
+	int len = sizeof(at_sim_name) / sizeof(*at_sim_name);
+
+	dump_response("at_cpin_cb", ok, result);
+	decode_at_error(&error, g_at_result_final_response(result));
+
+	if (!ok) {
+		cb(&error, -1, cbd->data);
+		return;
+	}
+
+	g_at_result_iter_init(&iter, result);
+
+	if (!g_at_result_iter_next(&iter, "+CPIN:")) {
+		DECLARE_FAILURE(e);
+
+		cb(&e, -1, cbd->data);
+		return;
+	}
+
+	pin_required = g_at_result_iter_raw_line(&iter);
+
+	pin_type = -1;
+	if (!strcmp(pin_required, "READY"))
+		pin_type = OFONO_PASSWD_NONE;
+	else
+		for (i = 0; i < len; i++)
+			if (!g_str_has_suffix(pin_required,
+						at_sim_name[i].name))
+				pin_type = at_sim_name[i].type;
+
+	if (pin_type == -1) {
+		DECLARE_FAILURE(e);
+
+		cb(&e, -1, cbd->data);
+		return;
+	}
+
+	ofono_debug("crsm_pin_cb: %s", pin_required);
+
+	cb(&error, pin_type, cbd->data);
+}
+
+static void at_pin_query(struct ofono_sim *sim, ofono_sim_passwd_cb_t cb,
+			void *data)
+{
+	GAtChat *chat = ofono_sim_get_data(sim);
+	struct cb_data *cbd = cb_data_new(cb, data);
+
+	if (!cbd)
+		goto error;
+
+	if (g_at_chat_send(chat, "AT+CPIN?", NULL,
+				at_cpin_cb, cbd, g_free) > 0)
+		return;
+
+error:
+	if (cbd)
+		g_free(cbd);
+
+	{
+		DECLARE_FAILURE(error);
+		cb(&error, -1, data);
+	}
+}
+
+static void at_cpin_set_cb(gboolean ok, GAtResult *result, gpointer user_data)
+{
+	struct cb_data *cbd = user_data;
+	ofono_sim_passwd_send_cb_t cb = cbd->cb;
+	struct ofono_error error;
+
+	dump_response("at_cpin_set_cb", ok, result);
+	decode_at_error(&error, g_at_result_final_response(result));
+
+	cb(&error, cbd->data);
+}
+
+static void at_pin_send(struct ofono_sim *sim, const char *passwd,
+			ofono_sim_passwd_send_cb_t cb, void *data)
+{
+	GAtChat *chat = ofono_sim_get_data(sim);
+	struct cb_data *cbd = cb_data_new(cb, data);
+	char buf[64];
+	int ret;
+
+	if (!cbd)
+		goto error;
+
+	snprintf(buf, sizeof(buf), "AT+CPIN=\"%s\"", passwd);
+
+	ret = g_at_chat_send(chat, buf, NULL, at_cpin_set_cb, cbd, g_free);
+
+	memset(buf, 0, sizeof(buf));
+
+	if (ret > 0)
+		return;
+
+error:
+	if (cbd)
+		g_free(cbd);
+
+	{
+		DECLARE_FAILURE(error);
+		cb(&error, data);
+	}
+}
+
+static void at_pin_send_puk(struct ofono_sim *sim, const char *puk,
+				const char *passwd,
+				ofono_sim_passwd_send_cb_t cb, void *data)
+{
+	GAtChat *chat = ofono_sim_get_data(sim);
+	struct cb_data *cbd = cb_data_new(cb, data);
+	char buf[64];
+	int ret;
+
+	if (!cbd)
+		goto error;
+
+	snprintf(buf, sizeof(buf), "AT+CPIN=\"%s\",\"%s\"", puk, passwd);
+
+	ret = g_at_chat_send(chat, buf, NULL, at_cpin_set_cb, cbd, g_free);
+
+	memset(buf, 0, sizeof(buf));
+
+	if (ret > 0)
+		return;
+
+error:
+	if (cbd)
+		g_free(cbd);
+
+	{
+		DECLARE_FAILURE(error);
+		cb(&error, data);
+	}
+}
+
 static gboolean at_sim_register(gpointer user)
 {
 	struct ofono_sim *sim = user;
@@ -467,6 +635,9 @@ static struct ofono_sim_driver driver = {
 	.write_file_linear	= at_sim_update_record,
 	.write_file_cyclic	= at_sim_update_cyclic,
 	.read_imsi		= at_read_imsi,
+	.query_passwd_state	= at_pin_query,
+	.send_passwd		= at_pin_send,
+	.send_new_passwd	= at_pin_send_puk,
 };
 
 void at_sim_init()
include/sim.h +13 -0
index 1da486c..90bedae 100644
@@ -64,6 +64,12 @@ typedef void (*ofono_sim_file_read_cb_t)(int ok,
 
 typedef void (*ofono_sim_file_write_cb_t)(int ok, void *userdata);
 
+typedef void (*ofono_sim_passwd_cb_t)(const struct ofono_error *error,
+					int passwd_type, void *data);
+
+typedef void (*ofono_sim_passwd_send_cb_t)(const struct ofono_error *error,
+					void *data);
+
 struct ofono_sim_driver {
 	const char *name;
 	int (*probe)(struct ofono_sim *sim, unsigned int vendor, void *data);
@@ -90,6 +96,13 @@ struct ofono_sim_driver {
 			ofono_sim_write_cb_t cb, void *data);
 	void (*read_imsi)(struct ofono_sim *sim,
 			ofono_sim_imsi_cb_t cb, void *data);
+	void (*query_passwd_state)(struct ofono_sim *sim,
+			ofono_sim_passwd_cb_t cb, void *data);
+	void (*send_passwd)(struct ofono_sim *sim, const char *passwd,
+			ofono_sim_passwd_send_cb_t cb, void *data);
+	void (*send_new_passwd)(struct ofono_sim *sim, const char *puk,
+			const char *passwd,
+			ofono_sim_passwd_send_cb_t cb, void *data);
 };
 
 int ofono_sim_driver_register(const struct ofono_sim_driver *d);
include/types.h +20 -0
index 6a9681d..99e2ca5 100644
@@ -91,6 +91,26 @@ struct ofono_call {
 	int clip_validity;
 };
 
+enum ofono_sim_passwd_type {
+	OFONO_PASSWD_NONE = 0,
+
+	OFONO_PASSWD_SIM_PIN,
+	OFONO_PASSWD_SIM_PUK,
+	OFONO_PASSWD_PHSIM_PIN,
+	OFONO_PASSWD_PHFSIM_PIN,
+	OFONO_PASSWD_PHFSIM_PUK,
+	OFONO_PASSWD_SIM_PIN2,
+	OFONO_PASSWD_SIM_PUK2,
+	OFONO_PASSWD_PHNET_PIN,
+	OFONO_PASSWD_PHNET_PUK,
+	OFONO_PASSWD_PHNETSUB_PIN,
+	OFONO_PASSWD_PHNETSUB_PUK,
+	OFONO_PASSWD_PHSP_PIN,
+	OFONO_PASSWD_PHSP_PUK,
+	OFONO_PASSWD_PHCORP_PIN,
+	OFONO_PASSWD_PHCORP_PUK,
+};
+
 #ifdef __cplusplus
 }
 #endif
src/sim.c +135 -1

194 lines not on this page · raw message

src/simutil.c +24 -0
index b80e014..2b26700 100644
@@ -488,3 +488,27 @@ void sim_adn_build(unsigned char *data, int length,
 	/* Ext1 unused */
 	*data++ = 0xff;
 }
+
+static const char *const passwd_name[] = {
+	[OFONO_PASSWD_NONE] = "None",
+	[OFONO_PASSWD_SIM_PIN] = "SIM PIN",
+	[OFONO_PASSWD_SIM_PUK] = "SIM PUK",
+	[OFONO_PASSWD_PHSIM_PIN] = "PH-SIM PIN",
+	[OFONO_PASSWD_PHFSIM_PIN] = "PH-FSIM PIN",
+	[OFONO_PASSWD_PHFSIM_PUK] = "PH-FSIM PUK",
+	[OFONO_PASSWD_SIM_PIN2] = "SIM PIN2",
+	[OFONO_PASSWD_SIM_PUK2] = "SIM PUK2",
+	[OFONO_PASSWD_PHNET_PIN] = "PH-NET PIN",
+	[OFONO_PASSWD_PHNET_PUK] = "PH-NET PUK",
+	[OFONO_PASSWD_PHNETSUB_PIN] = "PH-NETSUB PIN",
+	[OFONO_PASSWD_PHNETSUB_PUK] = "PH-NETSUB PUK",
+	[OFONO_PASSWD_PHSP_PIN] = "PH-SP PIN",
+	[OFONO_PASSWD_PHSP_PUK] = "PH-SP PUK",
+	[OFONO_PASSWD_PHCORP_PIN] = "PH-CORP PIN",
+	[OFONO_PASSWD_PHCORP_PUK] = "PH-CORP PUK",
+};
+
+const char *sim_passwd_name(enum ofono_sim_passwd_type type)
+{
+	return passwd_name[type];
+}
src/simutil.h +2 -0
index de3e55e..fc4f063 100644
@@ -88,3 +88,5 @@ gboolean sim_adn_parse(const unsigned char *data, int length,
 void sim_adn_build(unsigned char *data, int length,
 			const struct ofono_phone_number *ph,
 			const char *identifier);
+
+const char *sim_passwd_name(enum ofono_sim_passwd_type type);
Signature · 1 line
1.6.1
#1 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

drivers/atmodem/sim.c error: · line 492

Quoted 11 lines · Andrzej Zaborowski

This adds checking whether PIN is required during SIM initialisation and delaying the sim ready notifications until after correct PIN is given. According to 31.102 the IMSI should be retrieved after the PIN check however on the SIMs I've tried it can be retrieved before authentication and I think it would make sense to provide the imsi to the clients so they know which card they're authenticating to if they're swapping cards.

drivers/atmodem/sim.c +4 -0
+		for (i = 0; i < len; i++)
+			if (!g_str_has_suffix(pin_required,
+						at_sim_name[i].name))
+				pin_type = at_sim_name[i].type;

Should this be:

drivers/atmodem/sim.c error: · line 499

Quoted 6 lines
drivers/atmodem/sim.c +6 -0
+		for (i = 0; i < len; i++)
+			if (g_str_has_suffix(pin_required,
+						at_sim_name[i].name)) {
+				pin_type = at_sim_name[i].type;
+				break;
+			}

With the original code, I always get PH-CORP PUK (or PH-CORP PIN, should
the required code be PH-CORP PUK).

- --
Santtu Lakkala
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkqk2AwACgkQX9Rc0+po4p1YVQCgl//9VD6QQ+BmRBqtIn9/pRMH ticAn0QT9CvLrtQp/6cVLT0dwg8xe5q4
=R/Q0
-----END PGP SIGNATURE-----

#2 lore raw

Hi Santtu,

2009/9/7 Santtu Lakkala <[email protected]>:

Quoted 10 lines
Quoted 4 lines · Andrzej Zaborowski

+             for (i = 0; i < len; i++)
+                     if (!g_str_has_suffix(pin_required,
+                                             at_sim_name[i].name))
+                             pin_type = at_sim_name[i].type;

Should this be:

Quoted 3 lines

+             for (i = 0; i < len; i++)
+                     if (g_str_has_suffix(pin_required,
+                                             at_sim_name[i].name)) {

Ugh, yes, this was wrong but what I wanted was g_str_has_prefix() and no break; so that the loop would effectively find the longest matching prefix. I now replaced it with full strcmp() and made use of Denis' newly added g_at_result_iter_next_unquoted_string call.

Maybe we should pass the PIN type as string instead of enum and allow any name beside the 27.007-defined ones.

Regards,
Andrew

From 1a5bf29f7ef14fd010d637d94dadaea9d3f8f62c Mon Sep 17 00:00:00 2001
From: Andrzej Zaborowski <[email protected]>
Date: Tue, 8 Sep 2009 06:33:25 +0200
Subject: [PATCH] Do the PIN check in SIMManager

This adds checking whether PIN is required during SIM initialisation and delaying the sim ready notifications until after correct PIN is given. According to 31.102 the IMSI should be retrieved after the PIN check however on the SIMs I've tried it can be retrieved before authentication and I think it would make sense to provide the imsi to the clients so they know which card they're authenticating to if they're swapping cards.

---
 drivers/atmodem/sim.c |  172 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/sim.h         |   13 ++++
 include/types.h       |   20 ++++++
 src/sim.c             |  136 ++++++++++++++++++++++++++++++++++++++-
 src/simutil.c         |   24 +++++++
 src/simutil.h         |    2 +
 6 files changed, 366 insertions(+), 1 deletions(-)
drivers/atmodem/sim.c +172 -0
index fcd7895..8288c78 100644
@@ -431,6 +431,175 @@ error:
 	}
 }
 
+static struct {
+	enum ofono_sim_passwd_type type;
+	const char *name;
+} const at_sim_name[] = {
+	{ OFONO_PASSWD_SIM_PIN, "SIM PIN" },
+	{ OFONO_PASSWD_SIM_PUK, "SIM PUK" },
+	{ OFONO_PASSWD_PHSIM_PIN, "PH-SIM PIN" },
+	{ OFONO_PASSWD_PHFSIM_PIN, "PH-FSIM PIN" },
+	{ OFONO_PASSWD_PHFSIM_PUK, "PH-FSIM PUK" },
+	{ OFONO_PASSWD_SIM_PIN2, "SIM PIN2" },
+	{ OFONO_PASSWD_SIM_PUK2, "SIM PUK2" },
+	{ OFONO_PASSWD_PHNET_PIN, "PH-NET PIN" },
+	{ OFONO_PASSWD_PHNET_PUK, "PH-NET PUK" },
+	{ OFONO_PASSWD_PHNETSUB_PIN, "PH-NETSUB PIN" },
+	{ OFONO_PASSWD_PHNETSUB_PUK, "PH-NETSUB PUK" },
+	{ OFONO_PASSWD_PHSP_PIN, "PH-SP PIN" },
+	{ OFONO_PASSWD_PHSP_PUK, "PH-SP PUK" },
+	{ OFONO_PASSWD_PHCORP_PIN, "PH-CORP PIN" },
+	{ OFONO_PASSWD_PHCORP_PUK, "PH-CORP PUK" },
+};
+
+static void at_cpin_cb(gboolean ok, GAtResult *result, gpointer user_data)
+{
+	struct cb_data *cbd = user_data;
+	GAtResultIter iter;
+	ofono_sim_passwd_cb_t cb = cbd->cb;
+	struct ofono_error error;
+	const char *pin_required;
+	int pin_type;
+	int i;
+	int len = sizeof(at_sim_name) / sizeof(*at_sim_name);
+
+	dump_response("at_cpin_cb", ok, result);
+	decode_at_error(&error, g_at_result_final_response(result));
+
+	if (!ok) {
+		cb(&error, -1, cbd->data);
+		return;
+	}
+
+	g_at_result_iter_init(&iter, result);
+
+	if (!g_at_result_iter_next(&iter, "+CPIN:")) {
+		DECLARE_FAILURE(e);
+
+		cb(&e, -1, cbd->data);
+		return;
+	}
+
+	g_at_result_iter_next_unquoted_string(&iter, &pin_required);
+
+	pin_type = -1;
+	if (!strcmp(pin_required, "READY"))
+		pin_type = OFONO_PASSWD_NONE;
+	else
+		for (i = 0; i < len; i++)
+			if (!strcmp(pin_required, at_sim_name[i].name)) {
+				pin_type = at_sim_name[i].type;
+				break;
+			}
+
+	if (pin_type == -1) {
+		DECLARE_FAILURE(e);
+
+		cb(&e, -1, cbd->data);
+		return;
+	}
+
+	ofono_debug("crsm_pin_cb: %s", pin_required);
+
+	cb(&error, pin_type, cbd->data);
+}
+
+static void at_pin_query(struct ofono_sim *sim, ofono_sim_passwd_cb_t cb,
+			void *data)
+{
+	GAtChat *chat = ofono_sim_get_data(sim);
+	struct cb_data *cbd = cb_data_new(cb, data);
+
+	if (!cbd)
+		goto error;
+
+	if (g_at_chat_send(chat, "AT+CPIN?", NULL,
+				at_cpin_cb, cbd, g_free) > 0)
+		return;
+
+error:
+	if (cbd)
+		g_free(cbd);
+
+	{
+		DECLARE_FAILURE(error);
+		cb(&error, -1, data);
+	}
+}
+
+static void at_cpin_set_cb(gboolean ok, GAtResult *result, gpointer user_data)
+{
+	struct cb_data *cbd = user_data;
+	ofono_sim_passwd_send_cb_t cb = cbd->cb;
+	struct ofono_error error;
+
+	dump_response("at_cpin_set_cb", ok, result);
+	decode_at_error(&error, g_at_result_final_response(result));
+
+	cb(&error, cbd->data);
+}
+
+static void at_pin_send(struct ofono_sim *sim, const char *passwd,
+			ofono_sim_passwd_send_cb_t cb, void *data)
+{
+	GAtChat *chat = ofono_sim_get_data(sim);
+	struct cb_data *cbd = cb_data_new(cb, data);
+	char buf[64];
+	int ret;
+
+	if (!cbd)
+		goto error;
+
+	snprintf(buf, sizeof(buf), "AT+CPIN=\"%s\"", passwd);
+
+	ret = g_at_chat_send(chat, buf, NULL, at_cpin_set_cb, cbd, g_free);
+
+	memset(buf, 0, sizeof(buf));
+
+	if (ret > 0)
+		return;
+
+error:
+	if (cbd)
+		g_free(cbd);
+
+	{
+		DECLARE_FAILURE(error);
+		cb(&error, data);
+	}
+}
+
+static void at_pin_send_puk(struct ofono_sim *sim, const char *puk,
+				const char *passwd,
+				ofono_sim_passwd_send_cb_t cb, void *data)
+{
+	GAtChat *chat = ofono_sim_get_data(sim);
+	struct cb_data *cbd = cb_data_new(cb, data);
+	char buf[64];
+	int ret;
+
+	if (!cbd)
+		goto error;
+
+	snprintf(buf, sizeof(buf), "AT+CPIN=\"%s\",\"%s\"", puk, passwd);
+
+	ret = g_at_chat_send(chat, buf, NULL, at_cpin_set_cb, cbd, g_free);
+
+	memset(buf, 0, sizeof(buf));
+
+	if (ret > 0)
+		return;
+
+error:
+	if (cbd)
+		g_free(cbd);
+
+	{
+		DECLARE_FAILURE(error);
+		cb(&error, data);
+	}
+}
+
 static gboolean at_sim_register(gpointer user)
 {
 	struct ofono_sim *sim = user;
@@ -467,6 +636,9 @@ static struct ofono_sim_driver driver = {
 	.write_file_linear	= at_sim_update_record,
 	.write_file_cyclic	= at_sim_update_cyclic,
 	.read_imsi		= at_read_imsi,
+	.query_passwd_state	= at_pin_query,
+	.send_passwd		= at_pin_send,
+	.send_new_passwd	= at_pin_send_puk,
 };
 
 void at_sim_init()
include/sim.h +13 -0
index 1da486c..90bedae 100644
@@ -64,6 +64,12 @@ typedef void (*ofono_sim_file_read_cb_t)(int ok,
 
 typedef void (*ofono_sim_file_write_cb_t)(int ok, void *userdata);
 
+typedef void (*ofono_sim_passwd_cb_t)(const struct ofono_error *error,
+					int passwd_type, void *data);
+
+typedef void (*ofono_sim_passwd_send_cb_t)(const struct ofono_error *error,
+					void *data);
+
 struct ofono_sim_driver {
 	const char *name;
 	int (*probe)(struct ofono_sim *sim, unsigned int vendor, void *data);
@@ -90,6 +96,13 @@ struct ofono_sim_driver {
 			ofono_sim_write_cb_t cb, void *data);
 	void (*read_imsi)(struct ofono_sim *sim,
 			ofono_sim_imsi_cb_t cb, void *data);
+	void (*query_passwd_state)(struct ofono_sim *sim,
+			ofono_sim_passwd_cb_t cb, void *data);
+	void (*send_passwd)(struct ofono_sim *sim, const char *passwd,
+			ofono_sim_passwd_send_cb_t cb, void *data);
+	void (*send_new_passwd)(struct ofono_sim *sim, const char *puk,
+			const char *passwd,
+			ofono_sim_passwd_send_cb_t cb, void *data);
 };
 
 int ofono_sim_driver_register(const struct ofono_sim_driver *d);
include/types.h +20 -0
index 6a9681d..99e2ca5 100644
@@ -91,6 +91,26 @@ struct ofono_call {
 	int clip_validity;
 };
 
+enum ofono_sim_passwd_type {
+	OFONO_PASSWD_NONE = 0,
+
+	OFONO_PASSWD_SIM_PIN,
+	OFONO_PASSWD_SIM_PUK,
+	OFONO_PASSWD_PHSIM_PIN,
+	OFONO_PASSWD_PHFSIM_PIN,
+	OFONO_PASSWD_PHFSIM_PUK,
+	OFONO_PASSWD_SIM_PIN2,
+	OFONO_PASSWD_SIM_PUK2,
+	OFONO_PASSWD_PHNET_PIN,
+	OFONO_PASSWD_PHNET_PUK,
+	OFONO_PASSWD_PHNETSUB_PIN,
+	OFONO_PASSWD_PHNETSUB_PUK,
+	OFONO_PASSWD_PHSP_PIN,
+	OFONO_PASSWD_PHSP_PUK,
+	OFONO_PASSWD_PHCORP_PIN,
+	OFONO_PASSWD_PHCORP_PUK,
+};
+
 #ifdef __cplusplus
 }
 #endif
src/sim.c +135 -1

194 lines not on this page · raw message

src/simutil.c +24 -0
index b80e014..2b26700 100644
@@ -488,3 +488,27 @@ void sim_adn_build(unsigned char *data, int length,
 	/* Ext1 unused */
 	*data++ = 0xff;
 }
+
+static const char *const passwd_name[] = {
+	[OFONO_PASSWD_NONE] = "None",
+	[OFONO_PASSWD_SIM_PIN] = "SIM PIN",
+	[OFONO_PASSWD_SIM_PUK] = "SIM PUK",
+	[OFONO_PASSWD_PHSIM_PIN] = "PH-SIM PIN",
+	[OFONO_PASSWD_PHFSIM_PIN] = "PH-FSIM PIN",
+	[OFONO_PASSWD_PHFSIM_PUK] = "PH-FSIM PUK",
+	[OFONO_PASSWD_SIM_PIN2] = "SIM PIN2",
+	[OFONO_PASSWD_SIM_PUK2] = "SIM PUK2",
+	[OFONO_PASSWD_PHNET_PIN] = "PH-NET PIN",
+	[OFONO_PASSWD_PHNET_PUK] = "PH-NET PUK",
+	[OFONO_PASSWD_PHNETSUB_PIN] = "PH-NETSUB PIN",
+	[OFONO_PASSWD_PHNETSUB_PUK] = "PH-NETSUB PUK",
+	[OFONO_PASSWD_PHSP_PIN] = "PH-SP PIN",
+	[OFONO_PASSWD_PHSP_PUK] = "PH-SP PUK",
+	[OFONO_PASSWD_PHCORP_PIN] = "PH-CORP PIN",
+	[OFONO_PASSWD_PHCORP_PUK] = "PH-CORP PUK",
+};
+
+const char *sim_passwd_name(enum ofono_sim_passwd_type type)
+{
+	return passwd_name[type];
+}
src/simutil.h +2 -0
index de3e55e..fc4f063 100644
@@ -88,3 +88,5 @@ gboolean sim_adn_parse(const unsigned char *data, int length,
 void sim_adn_build(unsigned char *data, int length,
 			const struct ofono_phone_number *ph,
 			const char *identifier);
+
+const char *sim_passwd_name(enum ofono_sim_passwd_type type);
Signature · 1 line
1.6.1
#3 lore raw

And here's a version that implements most of what's specified in doc/sim-api.txt. There are two caveats: when you re-enable SIM PIN authentication, you have to give the old password from before it was changed to "", i.e to disable and enable SIM PIN, do:
SimManager.ChangePin("pin", "1234", "") SimManager.ChangePin("pin", "1234", "1234")

rather than the more logical

SimManager.ChangePin("pin", "1234", "")
SimManager.ChangePin("pin, "", "1234)

The other problem is that if changing the password succeeds but enabling authentiation fails, ChangePin still returns failure.

Regards

From 7209a4406224a0c4f8ad1c241ea355ee00e1fb5b Mon Sep 17 00:00:00 2001
From: Andrzej Zaborowski <[email protected]>
Date: Thu, 17 Sep 2009 22:43:22 +0200
Subject: [PATCH 2/2] Do PIN authentication

This adds checking whether PIN is required during SIM initialisation and delaying the sim ready notifications until after correct PIN is given.

---
 drivers/atmodem/sim.c |  241 +++++++++++++++++++++++++++++++++++++++++
 include/sim.h         |   19 ++++
 include/types.h       |   20 ++++
 src/sim.c             |  286 ++++++++++++++++++++++++++++++++++++++++++++++++-
 src/simutil.c         |   36 ++++++
 src/simutil.h         |    3 +
 6 files changed, 604 insertions(+), 1 deletions(-)
drivers/atmodem/sim.c +241 -0
index c629ec1..b6f34c2 100644
@@ -398,6 +398,242 @@ error:
 	CALLBACK_WITH_FAILURE(cb, NULL, data);
 }
 
+static struct {
+	enum ofono_passwd_type type;
+	const char *name;
+} const at_sim_name[] = {
+	{ OFONO_PASSWD_SIM_PIN, "SIM PIN" },
+	{ OFONO_PASSWD_SIM_PUK, "SIM PUK" },
+	{ OFONO_PASSWD_PHSIM_PIN, "PH-SIM PIN" },
+	{ OFONO_PASSWD_PHFSIM_PIN, "PH-FSIM PIN" },
+	{ OFONO_PASSWD_PHFSIM_PUK, "PH-FSIM PUK" },
+	{ OFONO_PASSWD_SIM_PIN2, "SIM PIN2" },
+	{ OFONO_PASSWD_SIM_PUK2, "SIM PUK2" },
+	{ OFONO_PASSWD_PHNET_PIN, "PH-NET PIN" },
+	{ OFONO_PASSWD_PHNET_PUK, "PH-NET PUK" },
+	{ OFONO_PASSWD_PHNETSUB_PIN, "PH-NETSUB PIN" },
+	{ OFONO_PASSWD_PHNETSUB_PUK, "PH-NETSUB PUK" },
+	{ OFONO_PASSWD_PHSP_PIN, "PH-SP PIN" },
+	{ OFONO_PASSWD_PHSP_PUK, "PH-SP PUK" },
+	{ OFONO_PASSWD_PHCORP_PIN, "PH-CORP PIN" },
+	{ OFONO_PASSWD_PHCORP_PUK, "PH-CORP PUK" },
+};
+
+static void at_cpin_cb(gboolean ok, GAtResult *result, gpointer user_data)
+{
+	struct cb_data *cbd = user_data;
+	GAtResultIter iter;
+	ofono_sim_passwd_cb_t cb = cbd->cb;
+	struct ofono_error error;
+	const char *pin_required;
+	int pin_type;
+	int i;
+	int len = sizeof(at_sim_name) / sizeof(*at_sim_name);
+
+	dump_response("at_cpin_cb", ok, result);
+	decode_at_error(&error, g_at_result_final_response(result));
+
+	if (!ok) {
+		cb(&error, -1, cbd->data);
+		return;
+	}
+
+	g_at_result_iter_init(&iter, result);
+
+	if (!g_at_result_iter_next(&iter, "+CPIN:")) {
+		CALLBACK_WITH_FAILURE(cb, -1, cbd->data);
+		return;
+	}
+
+	g_at_result_iter_next_unquoted_string(&iter, &pin_required);
+
+	pin_type = -1;
+	if (!strcmp(pin_required, "READY"))
+		pin_type = OFONO_PASSWD_NONE;
+	else
+		for (i = 0; i < len; i++)
+			if (!strcmp(pin_required, at_sim_name[i].name)) {
+				pin_type = at_sim_name[i].type;
+				break;
+			}
+
+	if (pin_type == -1) {
+		CALLBACK_WITH_FAILURE(cb, -1, cbd->data);
+		return;
+	}
+
+	ofono_debug("crsm_pin_cb: %s", pin_required);
+
+	cb(&error, pin_type, cbd->data);
+}
+
+static void at_pin_query(struct ofono_sim *sim, ofono_sim_passwd_cb_t cb,
+			void *data)
+{
+	GAtChat *chat = ofono_sim_get_data(sim);
+	struct cb_data *cbd = cb_data_new(cb, data);
+
+	if (!cbd)
+		goto error;
+
+	if (g_at_chat_send(chat, "AT+CPIN?", NULL,
+				at_cpin_cb, cbd, g_free) > 0)
+		return;
+
+error:
+	if (cbd)
+		g_free(cbd);
+
+	CALLBACK_WITH_FAILURE(cb, -1, data);
+}
+
+static void at_lock_unlock_cb(gboolean ok, GAtResult *result,
+				gpointer user_data)
+{
+	struct cb_data *cbd = user_data;
+	ofono_sim_lock_unlock_cb_t cb = cbd->cb;
+	struct ofono_error error;
+
+	dump_response("at_lock_unlock_cb", ok, result);
+	decode_at_error(&error, g_at_result_final_response(result));
+
+	cb(&error, cbd->data);
+}
+
+static void at_pin_send(struct ofono_sim *sim, const char *passwd,
+			ofono_sim_lock_unlock_cb_t cb, void *data)
+{
+	GAtChat *chat = ofono_sim_get_data(sim);
+	struct cb_data *cbd = cb_data_new(cb, data);
+	char buf[64];
+	int ret;
+
+	if (!cbd)
+		goto error;
+
+	snprintf(buf, sizeof(buf), "AT+CPIN=\"%s\"", passwd);
+
+	ret = g_at_chat_send(chat, buf, NULL, at_lock_unlock_cb, cbd, g_free);
+
+	memset(buf, 0, sizeof(buf));
+
+	if (ret > 0)
+		return;
+
+error:
+	if (cbd)
+		g_free(cbd);
+
+	CALLBACK_WITH_FAILURE(cb, data);
+}
+
+static void at_pin_send_puk(struct ofono_sim *sim, const char *puk,
+				const char *passwd,
+				ofono_sim_lock_unlock_cb_t cb, void *data)
+{
+	GAtChat *chat = ofono_sim_get_data(sim);
+	struct cb_data *cbd = cb_data_new(cb, data);
+	char buf[64];
+	int ret;
+
+	if (!cbd)
+		goto error;
+
+	snprintf(buf, sizeof(buf), "AT+CPIN=\"%s\",\"%s\"", puk, passwd);
+
+	ret = g_at_chat_send(chat, buf, NULL, at_lock_unlock_cb, cbd, g_free);
+
+	memset(buf, 0, sizeof(buf));
+
+	if (ret > 0)
+		return;
+
+error:
+	if (cbd)
+		g_free(cbd);
+
+	CALLBACK_WITH_FAILURE(cb, data);
+}
+
+static const char *const at_clck_cpwd_fac[] = {
+	[OFONO_PASSWD_SIM_PIN] = "SC",
+	[OFONO_PASSWD_SIM_PIN2] = "P2",
+	[OFONO_PASSWD_PHSIM_PIN] = "PS",
+	[OFONO_PASSWD_PHFSIM_PIN] = "PF",
+	[OFONO_PASSWD_PHNET_PIN] = "PN",
+	[OFONO_PASSWD_PHNETSUB_PIN] = "PU",
+	[OFONO_PASSWD_PHSP_PIN] = "PP",
+	[OFONO_PASSWD_PHCORP_PIN] = "PC",
+};
+
+static void at_pin_enable(struct ofono_sim *sim, int passwd_type, int enable,
+			const char *passwd,
+			ofono_sim_lock_unlock_cb_t cb, void *data)
+{
+	GAtChat *chat = ofono_sim_get_data(sim);
+	struct cb_data *cbd = cb_data_new(cb, data);
+	char buf[64];
+	int ret;
+	int len = sizeof(at_clck_cpwd_fac) / sizeof(*at_clck_cpwd_fac);
+
+	if (!cbd)
+		goto error;
+
+	if (passwd_type < 0 || passwd_type >= len ||
+			!at_clck_cpwd_fac[passwd_type])
+		goto error;
+
+	snprintf(buf, sizeof(buf), "AT+CLCK=\"%s\",%i,\"%s\"",
+			at_clck_cpwd_fac[passwd_type], enable ? 1 : 0, passwd);
+
+	ret = g_at_chat_send(chat, buf, NULL, at_lock_unlock_cb, cbd, g_free);
+
+	memset(buf, 0, sizeof(buf));
+
+	if (ret > 0)
+		return;
+
+error:
+	if (cbd)
+		g_free(cbd);
+
+	CALLBACK_WITH_FAILURE(cb, data);
+}
+
+static void at_change_passwd(struct ofono_sim *sim, int passwd_type,
+			const char *old, const char *new,
+			ofono_sim_lock_unlock_cb_t cb, void *data)
+{
+	GAtChat *chat = ofono_sim_get_data(sim);
+	struct cb_data *cbd = cb_data_new(cb, data);
+	char buf[64];
+	int ret;
+	int len = sizeof(at_clck_cpwd_fac) / sizeof(*at_clck_cpwd_fac);
+
+	if (!cbd)
+		goto error;
+
+	if (passwd_type < 0 || passwd_type >= len ||
+			!at_clck_cpwd_fac[passwd_type])
+		goto error;
+
+	snprintf(buf, sizeof(buf), "AT+CPWD=\"%s\",\"%s\",\"%s\"",
+			at_clck_cpwd_fac[passwd_type], old, new);
+
+	ret = g_at_chat_send(chat, buf, NULL, at_lock_unlock_cb, cbd, g_free);
+
+	memset(buf, 0, sizeof(buf));
+
+	if (ret > 0)
+		return;
+
+error:
+	if (cbd)
+		g_free(cbd);
+
+	CALLBACK_WITH_FAILURE(cb, data);
+}
+
 static gboolean at_sim_register(gpointer user)
 {
 	struct ofono_sim *sim = user;
@@ -434,6 +670,11 @@ static struct ofono_sim_driver driver = {
 	.write_file_linear	= at_sim_update_record,
 	.write_file_cyclic	= at_sim_update_cyclic,
 	.read_imsi		= at_read_imsi,
+	.query_passwd_state	= at_pin_query,
+	.send_passwd		= at_pin_send,
+	.reset_passwd		= at_pin_send_puk,
+	.lock			= at_pin_enable,
+	.change_passwd		= at_change_passwd,
 };
 
 void at_sim_init()
include/sim.h +19 -0
index 1da486c..751ae10 100644
@@ -64,6 +64,12 @@ typedef void (*ofono_sim_file_read_cb_t)(int ok,
 
 typedef void (*ofono_sim_file_write_cb_t)(int ok, void *userdata);
 
+typedef void (*ofono_sim_passwd_cb_t)(const struct ofono_error *error,
+					int passwd_type, void *data);
+
+typedef void (*ofono_sim_lock_unlock_cb_t)(const struct ofono_error *error,
+					void *data);
+
 struct ofono_sim_driver {
 	const char *name;
 	int (*probe)(struct ofono_sim *sim, unsigned int vendor, void *data);
@@ -90,6 +96,19 @@ struct ofono_sim_driver {
 			ofono_sim_write_cb_t cb, void *data);
 	void (*read_imsi)(struct ofono_sim *sim,
 			ofono_sim_imsi_cb_t cb, void *data);
+	void (*query_passwd_state)(struct ofono_sim *sim,
+			ofono_sim_passwd_cb_t cb, void *data);
+	void (*send_passwd)(struct ofono_sim *sim, const char *passwd,
+			ofono_sim_lock_unlock_cb_t cb, void *data);
+	void (*reset_passwd)(struct ofono_sim *sim, const char *puk,
+			const char *passwd,
+			ofono_sim_lock_unlock_cb_t cb, void *data);
+	void (*change_passwd)(struct ofono_sim *sim, int passwd_type,
+			const char *old, const char *new,
+			ofono_sim_lock_unlock_cb_t cb, void *data);
+	void (*lock)(struct ofono_sim *sim, int passwd_type, int enable,
+			const char *passwd,
+			ofono_sim_lock_unlock_cb_t cb, void *data);
 };
 
 int ofono_sim_driver_register(const struct ofono_sim_driver *d);
include/types.h +20 -0
index 6a9681d..82a3216 100644
@@ -91,6 +91,26 @@ struct ofono_call {
 	int clip_validity;
 };
 
+enum ofono_passwd_type {
+	OFONO_PASSWD_NONE = 0,
+
+	OFONO_PASSWD_SIM_PIN,
+	OFONO_PASSWD_SIM_PUK,
+	OFONO_PASSWD_PHSIM_PIN,
+	OFONO_PASSWD_PHFSIM_PIN,
+	OFONO_PASSWD_PHFSIM_PUK,
+	OFONO_PASSWD_SIM_PIN2,
+	OFONO_PASSWD_SIM_PUK2,
+	OFONO_PASSWD_PHNET_PIN,
+	OFONO_PASSWD_PHNET_PUK,
+	OFONO_PASSWD_PHNETSUB_PIN,
+	OFONO_PASSWD_PHNETSUB_PUK,
+	OFONO_PASSWD_PHSP_PIN,
+	OFONO_PASSWD_PHSP_PUK,
+	OFONO_PASSWD_PHCORP_PIN,
+	OFONO_PASSWD_PHCORP_PUK,
+};
+
 #ifdef __cplusplus
 }
 #endif
src/sim.c +285 -1

344 lines not on this page · raw message

src/simutil.c +36 -0
index b80e014..e1b0f2a 100644
@@ -488,3 +488,39 @@ void sim_adn_build(unsigned char *data, int length,
 	/* Ext1 unused */
 	*data++ = 0xff;
 }
+
+static const char *const passwd_name[] = {
+	[OFONO_PASSWD_NONE] = "none",
+	[OFONO_PASSWD_SIM_PIN] = "pin",
+	[OFONO_PASSWD_SIM_PUK] = "puk",
+	[OFONO_PASSWD_PHSIM_PIN] = "phone",
+	[OFONO_PASSWD_PHFSIM_PIN] = "firstphone",
+	[OFONO_PASSWD_PHFSIM_PUK] = "firstphonepuk",
+	[OFONO_PASSWD_SIM_PIN2] = "pin2",
+	[OFONO_PASSWD_SIM_PUK2] = "puk2",
+	[OFONO_PASSWD_PHNET_PIN] = "network",
+	[OFONO_PASSWD_PHNET_PUK] = "networkpuk",
+	[OFONO_PASSWD_PHNETSUB_PIN] = "netsub",
+	[OFONO_PASSWD_PHNETSUB_PUK] = "netsubpuk",
+	[OFONO_PASSWD_PHSP_PIN] = "service",
+	[OFONO_PASSWD_PHSP_PUK] = "servicepuk",
+	[OFONO_PASSWD_PHCORP_PIN] = "corp",
+	[OFONO_PASSWD_PHCORP_PUK] = "corppuk",
+};
+
+const char *sim_passwd_name(enum ofono_passwd_type type)
+{
+	return passwd_name[type];
+}
+
+enum ofono_passwd_type sim_string_to_passwd(const char *name)
+{
+	int len = sizeof(passwd_name) / sizeof(*passwd_name);
+	int i;
+
+	for (i = 0; i < len; i++)
+		if (!strcmp(passwd_name[i], name))
+			return i;
+
+	return OFONO_PASSWD_NONE;
+}
src/simutil.h +3 -0
index 2cd3b73..df84416 100644
@@ -93,3 +93,6 @@ gboolean sim_adn_parse(const unsigned char *data, int length,
 void sim_adn_build(unsigned char *data, int length,
 			const struct ofono_phone_number *ph,
 			const char *identifier);
+
+const char *sim_passwd_name(enum ofono_passwd_type type);
+enum ofono_passwd_type sim_string_to_passwd(const char *name);
Signature · 1 line
1.6.1
#4 lore raw

Hi Andrew,

Quoted 2 lines

And here's a version that implements most of what's specified in doc/sim-api.txt. There are two caveats: when you re-enable SIM PIN

Patch has been applied, thanks.

Quoted 5 lines

authentication, you have to give the old password from before it was changed to "", i.e to disable and enable SIM PIN, do:
SimManager.ChangePin("pin", "1234", "") SimManager.ChangePin("pin", "1234", "1234")

Quoted 7 lines

rather than the more logical

SimManager.ChangePin("pin", "1234", "")
SimManager.ChangePin("pin, "", "1234)

The other problem is that if changing the password succeeds but enabling authentiation fails, ChangePin still returns failure

I added LockPin and UnlockPin methods to take care of these two cases.

Regards,
-Denis

#5 lore raw

Hi,

2009/9/22 Denis Kenzior <[email protected]>:

Quoted 7 lines
Quoted 7 lines

authentication, you have to give the old password from before it was changed to "", i.e to disable and enable SIM PIN, do:
SimManager.ChangePin("pin", "1234", "") SimManager.ChangePin("pin", "1234", "1234")

rather than the more logical

Quoted 7 lines
Quoted 5 lines

SimManager.ChangePin("pin", "1234", "")
SimManager.ChangePin("pin, "", "1234)

The other problem is that if changing the password succeeds but enabling authentiation fails, ChangePin still returns failure

I added LockPin and UnlockPin methods to take care of these two cases.

Should we have a read-only property so clients know what the current state is? Unfortunately this would be some 6 properties and 6 more commands on startup, for little gain. I attached a diff to add plugin interface for this without dbus interface. The other two changes are just clean-up.

Regards

From 8f63f7860271f634a5106f68d10ff7331343dea5 Mon Sep 17 00:00:00 2001
From: Andrzej Zaborowski <[email protected]>
Date: Wed, 23 Sep 2009 23:17:06 +0200
Subject: [PATCH] Update sim-api.txt: ChangePin no longer disables pin.

---
 doc/sim-api.txt |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
doc/sim-api.txt +1 -2
index caf2dc4..f87dbeb 100644
@@ -14,8 +14,7 @@ Methods		dict GetProperties()
 
 		ChangePin(string type, string oldpin, string newpin)
 
-			Changes the pin given by string type.  If newpin is
-			empty, this has the effect of disabling the pin.
+			Changes the pin given by string type.
 
 		EnterPin(string type, string pin)
 
Signature · 1 line
1.6.1

From ab0131e797fe09049b31beb2a1bf622515eab78f Mon Sep 17 00:00:00 2001
From: Andrzej Zaborowski <[email protected]>
Date: Thu, 24 Sep 2009 00:33:28 +0200
Subject: [PATCH] Add plugin interface for getting PIN lock state.

It may be useful to have the information of whether card is currently locked and emit events when this changes but if we want to have it as a property, we would need properties for all types of locks and it wouldn't be all that useful.

---
 drivers/atmodem/sim.c |   61 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/sim.h         |    6 +++++
 2 files changed, 67 insertions(+), 0 deletions(-)
drivers/atmodem/sim.c +61 -0

73 lines not on this page · raw message

include/sim.h +6 -0
index af2fd1e..fa5276b 100644
@@ -91,6 +91,9 @@ typedef void (*ofono_sim_passwd_cb_t)(const struct ofono_error *error,
 typedef void (*ofono_sim_lock_unlock_cb_t)(const struct ofono_error *error,
 					void *data);
 
+typedef void (*ofono_sim_locked_cb_t)(const struct ofono_error *error,
+					int locked, void *data);
+
 struct ofono_sim_driver {
 	const char *name;
 	int (*probe)(struct ofono_sim *sim, unsigned int vendor, void *data);
@@ -131,6 +134,9 @@ struct ofono_sim_driver {
 	void (*lock)(struct ofono_sim *sim, enum ofono_sim_password_type type,
 			int enable, const char *passwd,
 			ofono_sim_lock_unlock_cb_t cb, void *data);
+	void (*query_locked)(struct ofono_sim *sim,
+			enum ofono_sim_password_type type,
+			ofono_sim_locked_cb_t cb, void *data);
 };
 
 int ofono_sim_driver_register(const struct ofono_sim_driver *d);
Signature · 1 line
1.6.1

From 35546966ef1107115003481928aa12d6b472ce7d Mon Sep 17 00:00:00 2001
From: Andrzej Zaborowski <[email protected]>
Date: Thu, 24 Sep 2009 00:35:15 +0200
Subject: [PATCH] Remove unused struct field.

---
 src/sim.c |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)
src/sim.c +0 -1
index ec003ee..746c7a8 100644
@@ -86,7 +86,6 @@ struct ofono_sim {
 	unsigned char efmsisdn_records;
 	unsigned char *efli;
 	unsigned char efli_length;
-	unsigned int next_ready_watch_id;
 	struct ofono_watchlist *ready_watches;
 	const struct ofono_sim_driver *driver;
 	void *driver_data;
Signature · 1 line
1.6.1
#6 lore raw

Hi Andrew,

Quoted 2 lines

Should we have a read-only property so clients know what the current state is? Unfortunately this would be some 6 properties and 6 more

Actually more like 7. However, right now we only really care about pin lock. We can theoretically optimize the query somewhat by looking at whether the CPIN is being asked for at startup. If it is, the PIN is locked, and unlocked otherwise. Then the LockPin / UnlockPin callbacks can update the state appropriately.

So the thought right now is to add LockedPins property, which will be a list of pins that are currently locked. If nothing is locked, an empty list is returned.

For now I'm planning to implement LockedPins support for pin only, but the others will have to be queried eventually as well.

Quoted 3 lines

commands on startup, for little gain. I attached a diff to add plugin interface for this without dbus interface. The other two changes are just clean-up.

Patches have been applied, thanks.

Regards,
-Denis