Skip to contentSkip to conversation

sim-auth: prep simauth/dbus headers

PATCH Superseded James Prestwood 2 participants 9 messages Sep 26, 2017 – Oct 4, 2017

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/7 sim-auth: prep simauth/dbus headers

Added new dbus interfaces for SimAuth module as well as function prototype definitions to simauth header

---
 include/dbus.h     |  2 ++
 include/sim-auth.h | 16 +++++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)
include/dbus.h +2 -0
index a6519c7..f0e437c 100644
@@ -58,6 +58,8 @@ extern "C" {
 #define OFONO_LOCATION_REPORTING_INTERFACE OFONO_SERVICE ".LocationReporting"
 #define OFONO_GNSS_INTERFACE "org.ofono.AssistedSatelliteNavigation"
 #define OFONO_GNSS_POSR_AGENT_INTERFACE "org.ofono.PositioningRequestAgent"
+#define OFONO_USIM_APPLICATION_INTERFACE "org.ofono.USimApplication"
+#define OFONO_ISIM_APPLICATION_INTERFACE "org.ofono.ISimApplication"
 #define OFONO_HANDSFREE_INTERFACE OFONO_SERVICE ".Handsfree"
 #define OFONO_SIRI_INTERFACE OFONO_SERVICE ".Siri"
 #define OFONO_NETMON_INTERFACE OFONO_SERVICE ".NetworkMonitor"
include/sim-auth.h +15 -1
index 0a62adc..b57fe25 100644
@@ -26,6 +26,8 @@
 extern "C" {
 #endif
 
+#include <stdint.h>
+
 #include <ofono/types.h>
 
 struct ofono_sim_auth;
@@ -34,6 +36,11 @@ typedef void (*ofono_sim_list_apps_cb_t)(const struct ofono_error *error,
 					const unsigned char *dataobj,
 					int len, void *data);
 
+typedef void (*ofono_sim_open_channel_cb_t)(int session_id, void *data);
+
+typedef void (*ofono_logical_access_cb_t)(const uint8_t *resp,
+		uint16_t len, void *data);
+
 struct ofono_sim_auth_driver {
 	const char *name;
 	int (*probe)(struct ofono_sim_auth *sa, unsigned int vendor,
@@ -41,7 +48,14 @@ struct ofono_sim_auth_driver {
 	void (*remove)(struct ofono_sim_auth *sa);
 
 	void (*list_apps)(struct ofono_sim_auth *sa,
-				ofono_sim_list_apps_cb_t cb, void *data);
+			ofono_sim_list_apps_cb_t cb, void *data);
+	void (*open_channel)(struct ofono_sim_auth *sa,
+			ofono_sim_open_channel_cb_t cb,
+			const void *channel, void *data);
+	void (*close_channel)(struct ofono_sim_auth *sa, int session_id);
+	void (*logical_access)(struct ofono_sim_auth *sa,
+			ofono_logical_access_cb_t cb, int session_id,
+			const uint8_t *pdu, uint16_t len, void *data);
 };
 
 int ofono_sim_auth_driver_register(const struct ofono_sim_auth_driver *d);
Signature · 1 line
2.7.4
#1 lore raw

PATCH 2/7 sim-auth: implementation of core sim-auth atom

The sim-auth module atom can now be used for SIM application discovery and authentication. The atom will automatically discovery SIM applications available on the SIM and register a DBus interface for each. These interfaces will contain the supported USIM/ISIM authentication algorithms.

---
 src/sim-auth.c | 479 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 479 insertions(+)
src/sim-auth.c +479 -0
index 5d2f075..8626a79 100644
@@ -28,19 +28,98 @@
 #include <glib.h>
 #include <errno.h>
 #include <unistd.h>
+#include <gdbus.h>
+#include <string.h>
 
 #include "ofono.h"
 
 #include "simutil.h"
+#include "util.h"
 
 static GSList *g_drivers = NULL;
 
+static const char aid_isim[] = { 0x10, 0x04 };
+static const char aid_usim[] = { 0x10, 0x02 };
+static const char sim_auth_cmd[] = { 0x00, 0x88, 0x00 };
+
+enum aid_type {
+	AID_TYPE_USIM,
+	AID_TYPE_ISIM,
+	AID_TYPE_UNKNOWN
+};
+
+struct sim_auth_app {
+	/* Raw AID name returned from app discovery */
+	uint8_t aid[16];
+	enum aid_type type;
+	struct sim_auth_app *next;
+};
+
 struct ofono_sim_auth {
 	const struct ofono_sim_auth_driver *driver;
 	void *driver_data;
 	struct ofono_atom *atom;
+	struct sim_auth_app *app_list;
+	struct ofono_sim *sim;
+	uint8_t gsm_access : 1;
+	uint8_t gsm_context : 1;
+};
+
+/*
+ * Temporary handle used for the command authentication sequence.
+ */
+struct auth_handle {
+	struct ofono_sim_auth *sim;
+	DBusMessage *msg;
+	int session_id;
+	void *rand;
+	void *autn;
+	const char *channel;
+	uint8_t umts : 1;
 };
 
+/*
+ * Find an AID channel by the human readable name (USIM, ISIM etc)
+ */
+static gboolean find_channel(struct sim_auth_app *list, enum aid_type type,
+		void **channel)
+{
+	struct sim_auth_app *cur = list;
+
+	while (cur) {
+		if (cur->type == type) {
+			*channel = cur->aid;
+			return 1;
+		}
+		cur = cur->next;
+	}
+	return 0;
+}
+
+/*
+ * Free all discovered AID's
+ */
+static void free_apps(struct ofono_sim_auth *sa)
+{
+	DBusConnection *conn = ofono_dbus_get_connection();
+	const char *path = __ofono_atom_get_path(sa->atom);
+	struct sim_auth_app *cur = sa->app_list;
+	void *tmp;
+
+	while (cur) {
+		if (cur->type == AID_TYPE_USIM)
+			g_dbus_unregister_interface(conn, path,
+					OFONO_USIM_APPLICATION_INTERFACE);
+		else if (cur->type == AID_TYPE_ISIM)
+			g_dbus_unregister_interface(conn, path,
+					OFONO_ISIM_APPLICATION_INTERFACE);
+
+		tmp = cur->next;
+		g_free(cur);
+		cur = tmp;
+	}
+}
+
 int ofono_sim_auth_driver_register(const struct ofono_sim_auth_driver *d)
 {
 	DBG("driver: %p, name: %s", d, d->name);
@@ -62,6 +141,9 @@ void ofono_sim_auth_driver_unregister(const struct ofono_sim_auth_driver *d)
 
 static void sim_auth_unregister(struct ofono_atom *atom)
 {
+	struct ofono_sim_auth *sa = __ofono_atom_get_data(atom);
+
+	free_apps(sa);
 }
 
 static void sim_auth_remove(struct ofono_atom *atom)
@@ -113,9 +195,406 @@ struct ofono_sim_auth *ofono_sim_auth_create(struct ofono_modem *modem,
 	return sa;
 }
 
+static void append_dict_byte_array(DBusMessageIter *iter, const char *key,
+		const void *arr, uint32_t len)
+{
+	DBusMessageIter keyiter;
+	DBusMessageIter valueiter;
+
+	dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL,
+			&keyiter);
+	dbus_message_iter_append_basic(&keyiter, DBUS_TYPE_STRING, &key);
+	dbus_message_iter_open_container(&keyiter, DBUS_TYPE_ARRAY,
+			"y", &valueiter);
+	dbus_message_iter_append_fixed_array(&valueiter, DBUS_TYPE_BYTE, &arr,
+			len);
+	dbus_message_iter_close_container(&keyiter, &valueiter);
+	dbus_message_iter_close_container(iter, &keyiter);
+}
+
+static void handle_umts(struct auth_handle *h, const uint8_t *resp,
+		uint16_t len)
+{
+	struct ofono_sim_auth *sim = h->sim;
+	DBusMessage *reply = NULL;
+	DBusMessageIter iter;
+	DBusMessageIter dict;
+	const uint8_t *res = NULL;
+	const uint8_t *ck = NULL;
+	const uint8_t *ik = NULL;
+	const uint8_t *auts = NULL;
+	const uint8_t *kc = NULL;
+
+	switch (resp[0]) {
+	case 0xdb:
+		/* 'DB' + '08' + RES(16) + '10' + CK(32) + '10' + IK(32) = 43 */
+		if (len < 43)
+			goto umts_end;
+
+		/* success */
+		if (resp[1] != 0x08)
+			goto umts_end;
+
+		res = resp + 2;
+
+		if (resp[10] != 0x10)
+			goto umts_end;
+
+		ck = resp + 11;
+
+		if (resp[27] != 0x10)
+			goto umts_end;
+
+		ik = resp + 28;
+
+		if (sim->gsm_access) {
+			if (resp[42] != 0x08)
+				goto umts_end;
+
+			kc = resp + 43;
+		}
+
+		break;
+	case 0xdc:
+		/* 'DB' + '10' + AUTS(16) = 18 */
+		if (len < 18)
+			goto umts_end;
+
+		/* sync error */
+		if (resp[1] != 0x10)
+			goto umts_end;
+
+		auts = resp + 2;
+
+		break;
+	default:
+		goto umts_end;
+	}
+
+	if (!auts && (!ik || !ck || !res))
+		goto umts_end;
+
+	reply = dbus_message_new_method_return(h->msg);
+
+	dbus_message_iter_init_append(reply, &iter);
+
+	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
+			"{say}", &dict);
+
+	if (auts) {
+		append_dict_byte_array(&dict, "auts", auts, 16);
+	} else {
+		append_dict_byte_array(&dict, "res", res, 8);
+		append_dict_byte_array(&dict, "ck", ck, 16);
+		append_dict_byte_array(&dict, "ik", ik, 16);
+		if (kc)
+			append_dict_byte_array(&dict, "kc", kc, 8);
+	}
+
+	dbus_message_iter_close_container(&iter, &dict);
+
+umts_end:
+	if (!reply)
+		reply = __ofono_error_not_supported(h->msg);
+
+	__ofono_dbus_pending_reply(&h->msg, reply);
+
+	sim->driver->close_channel(sim, h->session_id);
+
+	g_free(h);
+}
+
+static void handle_gsm(struct auth_handle *h, const uint8_t *resp, uint16_t len)
+{
+	struct ofono_sim_auth *sim = h->sim;
+	DBusMessage *reply = NULL;
+	const uint8_t *sres = NULL;
+	const uint8_t *kc = NULL;
+
+	if (len < 14 || !resp)
+		goto gsm_end;
+
+	if (resp[0] != 0x04)
+		goto gsm_end;
+
+	sres = resp + 1;
+
+	if (resp[5] != 0x08)
+		goto gsm_end;
+
+	kc = resp + 6;
+
+	if (!sres || !kc)
+		goto gsm_end;
+
+	reply = dbus_message_new_method_return(h->msg);
+
+	dbus_message_append_args(reply, DBUS_TYPE_ARRAY,
+			DBUS_TYPE_BYTE, &sres, 4, DBUS_TYPE_INVALID);
+	dbus_message_append_args(reply, DBUS_TYPE_ARRAY,
+			DBUS_TYPE_BYTE, &kc, 8, DBUS_TYPE_INVALID);
+
+gsm_end:
+	if (!reply)
+		reply = __ofono_error_not_supported(h->msg);
+
+	__ofono_dbus_pending_reply(&h->msg, reply);
+
+	sim->driver->close_channel(sim, h->session_id);
+
+	g_free(h);
+}
+
+static void logical_access_cb(const uint8_t *resp, uint16_t len, void *data)
+{
+	struct auth_handle *h = data;
+
+	if (h->umts)
+		handle_umts(h, resp, len);
+	else
+		handle_gsm(h, resp, len);
+}
+
+static void open_channel_cb(int session_id, void *data)
+{
+	struct auth_handle *h = data;
+	struct ofono_sim_auth *sim = h->sim;
+	uint8_t *cmd;
+	int ret = 0;
+
+	if (session_id == -1) {
+		__ofono_dbus_pending_reply(&h->msg,
+				__ofono_error_failed(h->msg));
+		g_free(h);
+		return;
+	}
+
+	/* save session ID for close_channel() */
+	h->session_id = session_id;
+
+	cmd = alloca(sizeof(sim_auth_cmd) + (h->umts ? 35 : 17));
+
+	memcpy(cmd, sim_auth_cmd, sizeof(sim_auth_cmd));
+	ret += sizeof(sim_auth_cmd);
+
+	/*
+	 * UMTS AUTHENTICATE: 0x81, 0x22, 0x10, <rand>, 0x10, <autn>, 0x00
+	 * GSM AUTHENTICATE: 0x80, 0x11, 0x10, <rand>, 0x00
+	 */
+	cmd[ret++] = h->umts ? 0x81 : 0x80;
+	cmd[ret++] = h->umts ? 0x22 : 0x11;
+	cmd[ret++] = 0x10;
+
+	memcpy(cmd + ret, h->rand, 16);
+	ret += 16;
+
+	if (h->umts) {
+		cmd[ret++] = 0x10;
+		memcpy(cmd + ret, h->autn, 16);
+		ret += 16;
+	}
+
+	cmd[ret++] = 0x00;
+
+	sim->driver->logical_access(sim, logical_access_cb, session_id, cmd,
+			ret, h);
+}
+
+static DBusMessage *authenticate_common(DBusConnection *conn, DBusMessage *msg,
+					void *data, enum aid_type type,
+					gboolean umts)
+{
+	void *channel;
+	uint8_t *rand = NULL;
+	uint8_t *autn = NULL;
+	uint32_t rlen;
+	uint32_t alen;
+	struct ofono_sim_auth *sim = data;
+	struct auth_handle *h;
+
+	/* get RAND/(AUTN) and setup handle args */
+	if (umts)
+		dbus_message_get_args(msg, NULL, DBUS_TYPE_ARRAY,
+				DBUS_TYPE_BYTE, &rand, &rlen, DBUS_TYPE_ARRAY,
+				DBUS_TYPE_BYTE, &autn, &alen,
+				DBUS_TYPE_INVALID);
+	else
+		dbus_message_get_args(msg, NULL, DBUS_TYPE_ARRAY,
+				DBUS_TYPE_BYTE, &rand, &rlen,
+				DBUS_TYPE_INVALID);
+
+	h = malloc(sizeof(struct auth_handle));
+	h->sim = sim;
+	h->msg = dbus_message_ref(msg);
+	h->rand = rand;
+	h->autn = autn;
+	h->umts = umts;
+
+	if (find_channel(sim->app_list, type, &channel)) {
+		sim->driver->open_channel(sim, open_channel_cb, channel, h);
+	} else {
+		dbus_message_unref(h->msg);
+		g_free(h);
+		return __ofono_error_not_supported(msg);
+	}
+
+	return NULL;
+}
+
+static DBusMessage *isim_ims_authenticate(DBusConnection *conn,
+		DBusMessage *msg, void *data)
+{
+	return authenticate_common(conn, msg, data, AID_TYPE_ISIM, 1);
+}
+
+static DBusMessage *usim_gsm_authenticate(DBusConnection *conn,
+		DBusMessage *msg, void *data)
+{
+	return authenticate_common(conn, msg, data, AID_TYPE_USIM, 0);
+}
+
+static DBusMessage *usim_umts_authenticate(DBusConnection *conn,
+		DBusMessage *msg, void *data)
+{
+	return authenticate_common(conn, msg, data, AID_TYPE_USIM, 1);
+}
+
+static const GDBusMethodTable sim_auth_usim_app[] = {
+	{ GDBUS_ASYNC_METHOD("GsmAuthenticate",
+			GDBUS_ARGS({"rand", "ay"}),
+			GDBUS_ARGS({"sres", "ay"}, {"kc", "ay"}),
+			usim_gsm_authenticate) },
+	{ GDBUS_ASYNC_METHOD("UmtsAuthenticate",
+			GDBUS_ARGS({"rand", "ay"}, {"autn", "ay"}),
+			GDBUS_ARGS({"return", "a{sv}"}),
+			usim_umts_authenticate) },
+	{ }
+};
+
+static const GDBusMethodTable sim_auth_isim_app[] = {
+	{ GDBUS_ASYNC_METHOD("ImsAuthenticate",
+			GDBUS_ARGS({"rand", "ay"}, {"autn", "ay"}),
+			GDBUS_ARGS({"return", "a{sv}"}),
+			isim_ims_authenticate) },
+	{ }
+};
+
+static void discover_apps_cb(const struct ofono_error *error,
+		const unsigned char *dataobj,
+		int len, void *data)
+{
+	DBusConnection *conn = ofono_dbus_get_connection();
+	struct ofono_sim_auth *sim = data;
+	const char *path = __ofono_atom_get_path(sim->atom);
+	int i = 0;
+	const unsigned char *aid_start = NULL;
+
+	sim->app_list = NULL;
+
+	while (i < len) {
+		uint8_t len;
+		struct sim_auth_app *app = NULL;
+
+		/*
+		 * TODO: There seems to be strings of 'FF' after each AID which
+		 * do not match up to the encoded lengths. Skipping these
+		 * sections until we reach the start of the next AID (not 0xff)
+		 */
+		while (dataobj[i] == 0xff)
+			i++;
+
+		/*
+		 * This just means we are@the end of the AID list, not
+		 * a parse error. Any error cases past this point are parse
+		 * errors.
+		 */
+		if (dataobj[i++] != 0x61)
+			break;
+
+		i++;
+
+		if (dataobj[i++] != 0x4f)
+			goto parse_error;
+
+		app = g_malloc(sizeof(struct sim_auth_app));
+		if (!app) {
+			ofono_error("failed to alloc SIM application");
+			goto parse_error;
+		}
+
+		len = dataobj[i++];
+
+		aid_start = dataobj + i;
+
+		i += len;
+
+		memcpy(app->aid, aid_start, 16);
+
+		if (dataobj[i++] != 0x50) {
+			g_free(app->aid);
+			g_free(app);
+			goto parse_error;
+		}
+
+		len = dataobj[i++];
+
+		i += len;
+
+		if (!memcmp(aid_start + 5, aid_usim, 2)) {
+			app->type = AID_TYPE_USIM;
+			g_dbus_register_interface(conn, path,
+					OFONO_USIM_APPLICATION_INTERFACE,
+					sim_auth_usim_app, NULL, NULL,
+					sim, NULL);
+		} else if (!memcmp(aid_start + 5, aid_isim, 2)) {
+			app->type = AID_TYPE_ISIM;
+			g_dbus_register_interface(conn, path,
+					OFONO_ISIM_APPLICATION_INTERFACE,
+					sim_auth_isim_app, NULL, NULL,
+					sim, NULL);
+		} else {
+			DBG("Unknown SIM application '%04x'",
+					*((uint16_t *)(aid_start + 5)));
+			/*
+			 * If we get here, the SIM application was not ISIM
+			 * or USIM, skip.
+			 */
+			g_free(app);
+			continue;
+		}
+
+		app->next = sim->app_list;
+		sim->app_list = app;
+	}
+
+	return;
+
+parse_error:
+	/*
+	 * Something went wrong parsing the AID list, it can't be assumed that
+	 * any previously parsed AID's are valid so free them all.
+	 */
+	DBG("Error parsing app list");
+	free_apps(sim);
+}
+
 void ofono_sim_auth_register(struct ofono_sim_auth *sa)
 {
+	struct ofono_modem *modem = __ofono_atom_get_modem(sa->atom);
+
+	ofono_modem_add_interface(modem, OFONO_PHONEBOOK_INTERFACE);
+
 	__ofono_atom_register(sa->atom, sim_auth_unregister);
+
+	/* Do SIM application discovery, the cb will register DBus ifaces */
+	sa->driver->list_apps(sa, discover_apps_cb, sa);
+
+	sa->sim = __ofono_atom_find(OFONO_ATOM_TYPE_SIM, modem);
+
+	sa->gsm_access = __ofono_sim_service_available(sa->sim,
+			SIM_UST_SERVICE_GSM_ACCESS, 0);
+	sa->gsm_context = __ofono_sim_service_available(sa->sim,
+			SIM_UST_SERVICE_GSM_SECURITY_CONTEXT, 0);
 }
 
 void ofono_sim_auth_remove(struct ofono_sim_auth *sa)
Signature · 1 line
2.7.4
#4 lore raw

PATCH 3/7 atmodem: implemented sim-auth functionality in atmodem

In reply to James Prestwood

Implemented the core API's needed for sim-auth:

list_apps: already implemented
open_channel: Opens a logical channel with +CCHO
close_channel: Closes logical channel with +CCHC
logical_access: Access an opened channel with +CGLA

---
 drivers/atmodem/sim-auth.c | 129 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)
drivers/atmodem/sim-auth.c +129 -0
index 271ceed..9a745f9 100644
@@ -25,6 +25,7 @@
 
 #define _GNU_SOURCE
 #include <string.h>
+#include <stdio.h>
 
 #include <glib.h>
 
@@ -35,6 +36,7 @@
 #include "gatresult.h"
 #include "simutil.h"
 #include "vendor.h"
+#include "util.h"
 
 #include "atmodem.h"
 
@@ -44,6 +46,8 @@ struct sim_auth_data {
 };
 
 static const char *cuad_prefix[] = { "+CUAD:", NULL };
+static const char *ccho_prefix[] = { "+CCHO:", NULL };
+static const char *cgla_prefix[] = { "+CGLA:", NULL };
 
 static void at_discover_apps_cb(gboolean ok, GAtResult *result,
 				gpointer user_data)
@@ -110,6 +114,128 @@ static void at_discover_apps(struct ofono_sim_auth *sa,
 	CALLBACK_WITH_FAILURE(cb, NULL, 0, data);
 }
 
+static void at_open_channel_cb(gboolean ok, GAtResult *result,
+		gpointer user_data)
+{
+	struct cb_data *cbd = user_data;
+	GAtResultIter iter;
+	ofono_sim_open_channel_cb_t cb = cbd->cb;
+	int session_id = -1;
+
+	g_at_result_iter_init(&iter, result);
+
+	if (!g_at_result_iter_next(&iter, "+CCHO:"))
+		goto error;
+
+	if (!g_at_result_iter_next_number(&iter, &session_id))
+		goto error;
+
+	cb(session_id, cbd->data);
+
+	return;
+
+error:
+	cb(-1, cbd->data);
+}
+
+static void at_open_channel(struct ofono_sim_auth *sa,
+		ofono_sim_open_channel_cb_t cb, const void *channel,
+		void *data)
+{
+	struct sim_auth_data *sad = ofono_sim_auth_get_data(sa);
+	struct cb_data *cbd = cb_data_new(cb, data);
+	char cmd[43];
+	int ret = 0;
+
+	strcpy(cmd, "AT+CCHO=\"");
+	ret += 9;
+
+	encode_hex_own_buf(channel, 16, 0, cmd + ret);
+	ret += 32;
+
+	strcpy(cmd + ret, "\"");
+
+	if (g_at_chat_send(sad->chat, cmd, ccho_prefix, at_open_channel_cb,
+			cbd, g_free) > 0)
+		return;
+
+	g_free(cbd);
+
+	cb(-1, data);
+}
+
+static void at_close_channel(struct ofono_sim_auth *sa, int session_id)
+{
+	struct sim_auth_data *sad = ofono_sim_auth_get_data(sa);
+	char cmd[15];
+
+	sprintf(cmd, "AT+CCHC=%d", session_id);
+
+	g_at_chat_send(sad->chat, cmd, NULL, NULL, NULL, NULL);
+}
+
+static void logical_access_cb(gboolean ok, GAtResult *result,
+		gpointer user_data)
+{
+	struct cb_data *cbd = user_data;
+	ofono_logical_access_cb_t cb = cbd->cb;
+	const char *str_data;
+	uint8_t *raw;
+	gint len = 0;
+	GAtResultIter iter;
+
+	if (!ok)
+		goto error;
+
+	g_at_result_iter_init(&iter, result);
+
+	if (!g_at_result_iter_next(&iter, "+CGLA:"))
+		goto error;
+
+	if (!g_at_result_iter_next_number(&iter, &len))
+		goto error;
+
+	if (!g_at_result_iter_next_string(&iter, &str_data))
+		goto error;
+
+	raw = alloca(len / 2);
+
+	decode_hex_own_buf(str_data, len, NULL, 0, raw);
+
+	cb(raw, len / 2, cbd->data);
+
+	return;
+
+error:
+	cb(NULL, 0, cbd->data);
+}
+
+static void at_logical_access(struct ofono_sim_auth *sa,
+		ofono_logical_access_cb_t cb, int session_id,
+		const uint8_t *pdu, uint16_t len, void *data)
+
+{
+	struct sim_auth_data *sad = ofono_sim_auth_get_data(sa);
+	struct cb_data *cbd = cb_data_new(cb, data);
+	int ret = 0;
+	char cmd[(len * 2) + 19];
+
+	ret = sprintf(cmd, "AT+CGLA=%d,%d,\"", session_id, len * 2);
+
+	encode_hex_own_buf(pdu, len, 0, cmd + ret);
+	ret += len * 2;
+
+	strcpy(cmd + ret, "\"");
+
+	if (g_at_chat_send(sad->chat, cmd, cgla_prefix, logical_access_cb,
+			cbd, g_free) > 0)
+		return;
+
+	g_free(cbd);
+
+	cb(NULL, 0, data);
+}
+
 static gboolean at_sim_auth_register(gpointer user)
 {
 	struct ofono_sim_auth *sa = user;
@@ -151,6 +277,9 @@ static struct ofono_sim_auth_driver driver = {
 	.probe		= at_sim_auth_probe,
 	.remove		= at_sim_auth_remove,
 	.list_apps	= at_discover_apps,
+	.open_channel	= at_open_channel,
+	.close_channel	= at_close_channel,
+	.logical_access = at_logical_access
 };
 
 void at_sim_auth_init(void)
Signature · 1 line
2.7.4
#5 lore raw

PATCH 4/7 xmm7xxx: add sim-auth driver to xmm7xxx plugin

In reply to James Prestwood
---
 plugins/xmm7xxx.c | 3 +++
 1 file changed, 3 insertions(+)
plugins/xmm7xxx.c +3 -0
index 4443d4c..50ec3e3 100644
@@ -48,6 +48,7 @@
 #include <ofono/gprs-context.h>
 #include <ofono/stk.h>
 #include <ofono/lte.h>
+#include <ofono/sim-auth.h>
 
 #include <drivers/atmodem/atutil.h>
 #include <drivers/atmodem/vendor.h>
@@ -60,6 +61,7 @@ struct xmm7xxx_data {
 	struct ofono_sim *sim;
 	ofono_bool_t have_sim;
 	ofono_bool_t sms_phonebook_added;
+	struct ofono_sim_auth *sim_auth;
 };
 
 static void xmm7xxx_debug(const char *str, void *user_data)
@@ -285,6 +287,7 @@ static void xmm7xxx_pre_sim(struct ofono_modem *modem)
 	ofono_devinfo_create(modem, OFONO_VENDOR_IFX, "atmodem", data->chat);
 	data->sim = ofono_sim_create(modem, OFONO_VENDOR_IFX, "atmodem",
 					data->chat);
+	data->sim_auth = ofono_sim_auth_create(modem, 0, "atmodem", data->chat);
 }
 
 static void set_online_cb(gboolean ok, GAtResult *result, gpointer user_data)
Signature · 1 line
2.7.4
#6 lore raw

PATCH 5/7 phonesim: Added sim-auth to phonesim plugin

In reply to James Prestwood
---
 plugins/phonesim.c | 6 ++++++
 1 file changed, 6 insertions(+)
plugins/phonesim.c +6 -0
index 16bccd5..0469b32 100644
@@ -61,6 +61,7 @@
 #include <ofono/gnss.h>
 #include <ofono/handsfree.h>
 #include <ofono/siri.h>
+#include <ofono/sim-auth.h>
 
 #include <drivers/atmodem/vendor.h>
 #include <drivers/atmodem/atutil.h>
@@ -84,6 +85,7 @@ struct phonesim_data {
 	unsigned int hfp_watch;
 	int batt_level;
 	struct ofono_sim *sim;
+	struct ofono_sim_auth *sim_auth;
 };
 
 struct gprs_context_data {
@@ -835,10 +837,14 @@ static void phonesim_pre_sim(struct ofono_modem *modem)
 	ofono_devinfo_create(modem, 0, "atmodem", data->chat);
 	data->sim = ofono_sim_create(modem, 0, "atmodem", data->chat);
 
+	printf("PHONESIM SIM=%p\n", data->sim);
+
 	if (data->calypso)
 		ofono_voicecall_create(modem, 0, "calypsomodem", data->chat);
 	else
 		ofono_voicecall_create(modem, 0, "atmodem", data->chat);
+
+	data->sim_auth = ofono_sim_auth_create(modem, 0, "atmodem", data->chat);
 }
 
 static void phonesim_post_sim(struct ofono_modem *modem)
Signature · 1 line
2.7.4
#7 lore raw

PATCH 6/7 test: added tests for GSM/UMTS auth algorithms

In reply to James Prestwood
---
 test/run-isim-umts-auth | 25 +++++++++++++++++++++++++
 test/run-usim-gsm-auth  | 17 +++++++++++++++++
 test/run-usim-umts-auth | 25 +++++++++++++++++++++++++
 3 files changed, 67 insertions(+)
 create mode 100755 test/run-isim-umts-auth
 create mode 100755 test/run-usim-gsm-auth
 create mode 100755 test/run-usim-umts-auth
test/run-isim-umts-auth new file +25 -0
new file mode 100755
index 0000000..12fa2f0
@@ -0,0 +1,25 @@
+#!/usr/bin/python3
+
+import dbus
+import sys
+
+bus = dbus.SystemBus()
+
+if len(sys.argv) == 4:
+	path = sys.argv[1]
+	rand = sys.argv[2]
+	autn = sys.argv[3]
+
+	isim_auth = dbus.Interface(bus.get_object('org.ofono', path), 'org.ofono.ISimApplication')
+	ret = isim_auth.ImsAuthenticate(bytearray.fromhex(rand), bytearray.fromhex(autn))
+
+	if 'auts' in ret:
+		print('Sync Failure')
+		print('AUTS: ' + ''.join('%02x' % x for x in ret['auts']))
+	else:
+		print('Success')
+		print('RES: ' +  ''.join('%02x' % x for x in ret['res']))
+		print('CK: ' + ''.join('%02x' % x for x in ret['ck']))
+		print('IK: ' + ''.join('%02x' % x for x in ret['ik']))
+else:
+	print("./run-isim-umts-auth <modem> <rand> <autn>")
test/run-usim-gsm-auth new file +17 -0
new file mode 100755
index 0000000..034f7e4
@@ -0,0 +1,17 @@
+#!/usr/bin/python3
+
+import dbus
+import sys
+
+bus = dbus.SystemBus()
+
+if len(sys.argv) == 3:
+	path = sys.argv[1]
+	rand = sys.argv[2]
+
+	usim_auth = dbus.Interface(bus.get_object('org.ofono', path), 'org.ofono.USimApplication')
+	sres, kc = usim_auth.GsmAuthenticate(bytearray.fromhex(rand))
+	print('SRES: ' + ''.join('%02x' % x for x in sres))
+	print('KC: ' + ''.join('%02x' % x for x in kc))
+else:
+	print("./run-usim-gsm-auth <modem> <rand>")
test/run-usim-umts-auth new file +25 -0
new file mode 100755
index 0000000..8930b0f
@@ -0,0 +1,25 @@
+#!/usr/bin/python3
+
+import dbus
+import sys
+
+bus = dbus.SystemBus()
+
+if len(sys.argv) == 4:
+	path = sys.argv[1]
+	rand = sys.argv[2]
+	autn = sys.argv[3]
+
+	usim_auth = dbus.Interface(bus.get_object('org.ofono', path), 'org.ofono.USimApplication')
+	ret = usim_auth.UmtsAuthenticate(bytearray.fromhex(rand), bytearray.fromhex(autn))
+
+	if 'auts' in ret:
+		print('Sync Failure')
+		print('AUTS: ' + ''.join('%02x' % x for x in ret['auts']))
+	else:
+		print('Success')
+		print('RES: ' +  ''.join('%02x' % x for x in ret['res']))
+		print('CK: ' + ''.join('%02x' % x for x in ret['ck']))
+		print('IK: ' + ''.join('%02x' % x for x in ret['ik']))
+else:
+	print("./run-usim-umts-auth <modem> <rand> <autn>")
Signature · 1 line
2.7.4
#8 lore raw

PATCH 7/7 doc: documentation for SimAuth dbus interfaces

In reply to James Prestwood
---
 doc/sim-auth-api.txt | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 doc/sim-auth-api.txt
doc/sim-auth-api.txt new file +41 -0
new file mode 100644
index 0000000..f9c666f
@@ -0,0 +1,41 @@
+SimAuth USIM application heiarchy [experimental]
+===========================================
+
+Service		org.ofono
+Interface	org.ofono.USimApplication
+Object path	[variable prefix]/{modem0,modem1,...}
+
+Methods		array{byte},array{byte} GsmAuthenticate(array{byte} rand)
+
+			Run the USIM application GSM AUTHENTICATE algorithm
+			with the random challenge 'rand'. Returns the derived
+			SRES and KC values as two arrays of bytes.
+
+			Possible Errors: [service].Error.NotSupported
+
+Methods		dict UmtsAuthenticate(array{byte} rand, array{byte} autn)
+
+			Run the UMTS AUTHENTICATE algorithm in the 3G
+			context with 'rand' and 'autn'. A dictionary will be
+			returned containing 'res', 'ck', 'ik' and possibly
+			'kc' if service 27 is available. If there was a
+			sync error 'auts' will be returned.
+
+			Possible Errors: [service].Error.NotSupported
+
+SimAuth ISIM application heiarchy [experimental]
+===========================================
+
+Service		org.ofono
+Interface	org.ofono.ISimApplication
+Object		[variable prefix]/{modem0,modem1,...}
+
+Methods		dict ImsAuthenticate(array{byte} rand, array{byte} autn)
+
+			Run the UMTS AUTHENTICATE algorithm in the IMS
+			context with 'rand' and 'autn'. A dictionary will be
+			returned containing 'res', 'ck', 'ik' and possibly
+			'kc' if service 27 is available. If there was a
+			sync error 'auts' will be returned.
+
+			Possible Errors: [service].Error.NotSupported
Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH 2/7] sim-auth: implementation of core sim-auth atom

In reply to James Prestwood

Hi James,

Quoted 3 lines · James Prestwood

The sim-auth module atom can now be used for SIM application discovery and authentication. The atom will automatically discovery SIM applications available on the SIM and register

'discover' ?

Quoted 17 lines

a DBus interface for each. These interfaces will contain the supported USIM/ISIM authentication algorithms.

---
  src/sim-auth.c | 479 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 479 insertions(+)
src/sim-auth.c +2 -0
index 5d2f075..8626a79 100644
@@ -28,19 +28,98 @@
  #include <glib.h>
  #include <errno.h>
  #include <unistd.h>
+#include <gdbus.h>
+#include <string.h>
 

src/sim-auth.c line 43

Quoted 10 lines
src/sim-auth.c +4 -0
  #include "ofono.h"
 
  #include "simutil.h"
+#include "util.h"
 
  static GSList *g_drivers = NULL;
 
+static const char aid_isim[] = { 0x10, 0x04 };
+static const char aid_usim[] = { 0x10, 0x02 };
+static const char sim_auth_cmd[] = { 0x00, 0x88, 0x00 };

unsigned char? Or simply start using uint8_t

Quoted 25 lines
src/sim-auth.c +21 -0
+
+enum aid_type {
+	AID_TYPE_USIM,
+	AID_TYPE_ISIM,
+	AID_TYPE_UNKNOWN
+};
+
+struct sim_auth_app {
+	/* Raw AID name returned from app discovery */
+	uint8_t aid[16];
+	enum aid_type type;
+	struct sim_auth_app *next;
+};
+
  struct ofono_sim_auth {
  	const struct ofono_sim_auth_driver *driver;
  	void *driver_data;
  	struct ofono_atom *atom;
+	struct sim_auth_app *app_list;
+	struct ofono_sim *sim;
+	uint8_t gsm_access : 1;
+	uint8_t gsm_context : 1;
+};
+
+/*

src/sim-auth.c line 78

Quoted 10 lines
src/sim-auth.c +10 -0
+ * Temporary handle used for the command authentication sequence.
+ */
+struct auth_handle {
+	struct ofono_sim_auth *sim;
+	DBusMessage *msg;
+	int session_id;
+	void *rand;
+	void *autn;
+	const char *channel;
+	uint8_t umts : 1;

How are you planning to clean these up in case sim_auth atom is forcefully removed? e.g. a if the USB is pulled and ofono_sim_auth_remove, etc is called?

This is why we generally take the easy way out and only allow a single outstanding request that is part of the main atom data structure. Check 'pending' in most of our atom implementations.

src/sim-auth.c line 83

Quoted 5 lines
src/sim-auth.c +3 -0
  };
 
+/*
+ * Find an AID channel by the human readable name (USIM, ISIM etc)
+ */

Human readable?

src/sim-auth.c line 96

Quoted 13 lines
src/sim-auth.c +13 -0
+static gboolean find_channel(struct sim_auth_app *list, enum aid_type type,
+		void **channel)
+{
+	struct sim_auth_app *cur = list;
+
+	while (cur) {
+		if (cur->type == type) {
+			*channel = cur->aid;
+			return 1;
+		}
+		cur = cur->next;
+	}
+	return 0;

doc/coding-style.txt item M1. Also might as well use TRUE/FALSE

Is the assumption that there can be only 1 SIM application of a given type? Is this a safe assumption? It might be that we would need to add an intermediate interface to hang all these application objects on. e.g.

org.ofono.SimAuthentication on path /modem1

SimAuthentication would have a property with signature 'a{sv}' Applications

where
- 'o' Path -> Path to the application, something like /modem1/aid
- 's' Type -> Gsm/Umts/Ims. The interface (USimApplication, ISimApplication, etc) would depend on the type.
- 's' Name -> Human readable name

With such a setup you could just create an object for each application and store this info there with a back-pointer to the ofono_sim_auth structure. Then give the application-specific object to g_dbus_register_interface.

Quoted 138 lines
src/sim-auth.c +124 -0
+}
+
+/*
+ * Free all discovered AID's
+ */
+static void free_apps(struct ofono_sim_auth *sa)
+{
+	DBusConnection *conn = ofono_dbus_get_connection();
+	const char *path = __ofono_atom_get_path(sa->atom);
+	struct sim_auth_app *cur = sa->app_list;
+	void *tmp;
+
+	while (cur) {
+		if (cur->type == AID_TYPE_USIM)
+			g_dbus_unregister_interface(conn, path,
+					OFONO_USIM_APPLICATION_INTERFACE);
+		else if (cur->type == AID_TYPE_ISIM)
+			g_dbus_unregister_interface(conn, path,
+					OFONO_ISIM_APPLICATION_INTERFACE);
+
+		tmp = cur->next;
+		g_free(cur);
+		cur = tmp;
+	}
+}
+
  int ofono_sim_auth_driver_register(const struct ofono_sim_auth_driver *d)
  {
  	DBG("driver: %p, name: %s", d, d->name);
@@ -62,6 +141,9 @@ void ofono_sim_auth_driver_unregister(const struct ofono_sim_auth_driver *d)
 
  static void sim_auth_unregister(struct ofono_atom *atom)
  {
+	struct ofono_sim_auth *sa = __ofono_atom_get_data(atom);
+
+	free_apps(sa);
  }
 
  static void sim_auth_remove(struct ofono_atom *atom)
@@ -113,9 +195,406 @@ struct ofono_sim_auth *ofono_sim_auth_create(struct ofono_modem *modem,
  	return sa;
  }
 
+static void append_dict_byte_array(DBusMessageIter *iter, const char *key,
+		const void *arr, uint32_t len)
+{
+	DBusMessageIter keyiter;
+	DBusMessageIter valueiter;
+
+	dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL,
+			&keyiter);
+	dbus_message_iter_append_basic(&keyiter, DBUS_TYPE_STRING, &key);
+	dbus_message_iter_open_container(&keyiter, DBUS_TYPE_ARRAY,
+			"y", &valueiter);
+	dbus_message_iter_append_fixed_array(&valueiter, DBUS_TYPE_BYTE, &arr,
+			len);
+	dbus_message_iter_close_container(&keyiter, &valueiter);
+	dbus_message_iter_close_container(iter, &keyiter);
+}
+
+static void handle_umts(struct auth_handle *h, const uint8_t *resp,
+		uint16_t len)
+{
+	struct ofono_sim_auth *sim = h->sim;
+	DBusMessage *reply = NULL;
+	DBusMessageIter iter;
+	DBusMessageIter dict;
+	const uint8_t *res = NULL;
+	const uint8_t *ck = NULL;
+	const uint8_t *ik = NULL;
+	const uint8_t *auts = NULL;
+	const uint8_t *kc = NULL;
+
+	switch (resp[0]) {
+	case 0xdb:
+		/* 'DB' + '08' + RES(16) + '10' + CK(32) + '10' + IK(32) = 43 */
+		if (len < 43)
+			goto umts_end;
+
+		/* success */
+		if (resp[1] != 0x08)
+			goto umts_end;
+
+		res = resp + 2;
+
+		if (resp[10] != 0x10)
+			goto umts_end;
+
+		ck = resp + 11;
+
+		if (resp[27] != 0x10)
+			goto umts_end;
+
+		ik = resp + 28;
+
+		if (sim->gsm_access) {
+			if (resp[42] != 0x08)
+				goto umts_end;
+
+			kc = resp + 43;
+		}
+
+		break;
+	case 0xdc:
+		/* 'DB' + '10' + AUTS(16) = 18 */
+		if (len < 18)
+			goto umts_end;
+
+		/* sync error */
+		if (resp[1] != 0x10)
+			goto umts_end;
+
+		auts = resp + 2;
+
+		break;
+	default:
+		goto umts_end;
+	}
+
+	if (!auts && (!ik || !ck || !res))
+		goto umts_end;
+
+	reply = dbus_message_new_method_return(h->msg);
+
+	dbus_message_iter_init_append(reply, &iter);
+
+	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
+			"{say}", &dict);
+
+	if (auts) {
+		append_dict_byte_array(&dict, "auts", auts, 16);
+	} else {
+		append_dict_byte_array(&dict, "res", res, 8);
+		append_dict_byte_array(&dict, "ck", ck, 16);
+		append_dict_byte_array(&dict, "ik", ik, 16);
+		if (kc)
+			append_dict_byte_array(&dict, "kc", kc, 8);
+	}

src/sim-auth.c struct ofono_sim_auth *ofono_sim_auth_create(struct ofono_modem *modem, · line 302

Quoted 10 lines
src/sim-auth.c +10 -0
+
+	dbus_message_iter_close_container(&iter, &dict);
+
+umts_end:
+	if (!reply)
+		reply = __ofono_error_not_supported(h->msg);
+
+	__ofono_dbus_pending_reply(&h->msg, reply);
+
+	sim->driver->close_channel(sim, h->session_id);

Its a bit weird that this doesn't have a callback. All our methods do...

Quoted 85 lines
src/sim-auth.c +85 -0
+
+	g_free(h);
+}
+
+static void handle_gsm(struct auth_handle *h, const uint8_t *resp, uint16_t len)
+{
+	struct ofono_sim_auth *sim = h->sim;
+	DBusMessage *reply = NULL;
+	const uint8_t *sres = NULL;
+	const uint8_t *kc = NULL;
+
+	if (len < 14 || !resp)
+		goto gsm_end;
+
+	if (resp[0] != 0x04)
+		goto gsm_end;
+
+	sres = resp + 1;
+
+	if (resp[5] != 0x08)
+		goto gsm_end;
+
+	kc = resp + 6;
+
+	if (!sres || !kc)
+		goto gsm_end;
+
+	reply = dbus_message_new_method_return(h->msg);
+
+	dbus_message_append_args(reply, DBUS_TYPE_ARRAY,
+			DBUS_TYPE_BYTE, &sres, 4, DBUS_TYPE_INVALID);
+	dbus_message_append_args(reply, DBUS_TYPE_ARRAY,
+			DBUS_TYPE_BYTE, &kc, 8, DBUS_TYPE_INVALID);
+
+gsm_end:
+	if (!reply)
+		reply = __ofono_error_not_supported(h->msg);
+
+	__ofono_dbus_pending_reply(&h->msg, reply);
+
+	sim->driver->close_channel(sim, h->session_id);
+
+	g_free(h);
+}
+
+static void logical_access_cb(const uint8_t *resp, uint16_t len, void *data)
+{
+	struct auth_handle *h = data;
+
+	if (h->umts)
+		handle_umts(h, resp, len);
+	else
+		handle_gsm(h, resp, len);
+}
+
+static void open_channel_cb(int session_id, void *data)
+{
+	struct auth_handle *h = data;
+	struct ofono_sim_auth *sim = h->sim;
+	uint8_t *cmd;
+	int ret = 0;
+
+	if (session_id == -1) {
+		__ofono_dbus_pending_reply(&h->msg,
+				__ofono_error_failed(h->msg));
+		g_free(h);
+		return;
+	}
+
+	/* save session ID for close_channel() */
+	h->session_id = session_id;
+
+	cmd = alloca(sizeof(sim_auth_cmd) + (h->umts ? 35 : 17));
+
+	memcpy(cmd, sim_auth_cmd, sizeof(sim_auth_cmd));
+	ret += sizeof(sim_auth_cmd);
+
+	/*
+	 * UMTS AUTHENTICATE: 0x81, 0x22, 0x10, <rand>, 0x10, <autn>, 0x00
+	 * GSM AUTHENTICATE: 0x80, 0x11, 0x10, <rand>, 0x00
+	 */
+	cmd[ret++] = h->umts ? 0x81 : 0x80;
+	cmd[ret++] = h->umts ? 0x22 : 0x11;
+	cmd[ret++] = 0x10;
+

src/sim-auth.c struct ofono_sim_auth *ofono_sim_auth_create(struct ofono_modem *modem, · line 397

Quoted 10 lines
src/sim-auth.c +10 -0
+	memcpy(cmd + ret, h->rand, 16);
+	ret += 16;
+
+	if (h->umts) {
+		cmd[ret++] = 0x10;
+		memcpy(cmd + ret, h->autn, 16);
+		ret += 16;
+	}
+
+	cmd[ret++] = 0x00;

The composition of the Authenticate PDU really belongs in src/simutil.[ch]. That way you can isolate it into a unit test inside unit/test-simutil and test these easily.

It also makes it much easier to read the main logic code...

Quoted 134 lines
src/sim-auth.c +134 -0
+
+	sim->driver->logical_access(sim, logical_access_cb, session_id, cmd,
+			ret, h);
+}
+
+static DBusMessage *authenticate_common(DBusConnection *conn, DBusMessage *msg,
+					void *data, enum aid_type type,
+					gboolean umts)
+{
+	void *channel;
+	uint8_t *rand = NULL;
+	uint8_t *autn = NULL;
+	uint32_t rlen;
+	uint32_t alen;
+	struct ofono_sim_auth *sim = data;
+	struct auth_handle *h;
+
+	/* get RAND/(AUTN) and setup handle args */
+	if (umts)
+		dbus_message_get_args(msg, NULL, DBUS_TYPE_ARRAY,
+				DBUS_TYPE_BYTE, &rand, &rlen, DBUS_TYPE_ARRAY,
+				DBUS_TYPE_BYTE, &autn, &alen,
+				DBUS_TYPE_INVALID);
+	else
+		dbus_message_get_args(msg, NULL, DBUS_TYPE_ARRAY,
+				DBUS_TYPE_BYTE, &rand, &rlen,
+				DBUS_TYPE_INVALID);
+
+	h = malloc(sizeof(struct auth_handle));
+	h->sim = sim;
+	h->msg = dbus_message_ref(msg);
+	h->rand = rand;
+	h->autn = autn;
+	h->umts = umts;
+
+	if (find_channel(sim->app_list, type, &channel)) {
+		sim->driver->open_channel(sim, open_channel_cb, channel, h);
+	} else {
+		dbus_message_unref(h->msg);
+		g_free(h);
+		return __ofono_error_not_supported(msg);
+	}
+
+	return NULL;
+}
+
+static DBusMessage *isim_ims_authenticate(DBusConnection *conn,
+		DBusMessage *msg, void *data)
+{
+	return authenticate_common(conn, msg, data, AID_TYPE_ISIM, 1);
+}
+
+static DBusMessage *usim_gsm_authenticate(DBusConnection *conn,
+		DBusMessage *msg, void *data)
+{
+	return authenticate_common(conn, msg, data, AID_TYPE_USIM, 0);
+}
+
+static DBusMessage *usim_umts_authenticate(DBusConnection *conn,
+		DBusMessage *msg, void *data)
+{
+	return authenticate_common(conn, msg, data, AID_TYPE_USIM, 1);
+}
+
+static const GDBusMethodTable sim_auth_usim_app[] = {
+	{ GDBUS_ASYNC_METHOD("GsmAuthenticate",
+			GDBUS_ARGS({"rand", "ay"}),
+			GDBUS_ARGS({"sres", "ay"}, {"kc", "ay"}),
+			usim_gsm_authenticate) },
+	{ GDBUS_ASYNC_METHOD("UmtsAuthenticate",
+			GDBUS_ARGS({"rand", "ay"}, {"autn", "ay"}),
+			GDBUS_ARGS({"return", "a{sv}"}),
+			usim_umts_authenticate) },
+	{ }
+};
+
+static const GDBusMethodTable sim_auth_isim_app[] = {
+	{ GDBUS_ASYNC_METHOD("ImsAuthenticate",
+			GDBUS_ARGS({"rand", "ay"}, {"autn", "ay"}),
+			GDBUS_ARGS({"return", "a{sv}"}),
+			isim_ims_authenticate) },
+	{ }
+};
+
+static void discover_apps_cb(const struct ofono_error *error,
+		const unsigned char *dataobj,
+		int len, void *data)
+{
+	DBusConnection *conn = ofono_dbus_get_connection();
+	struct ofono_sim_auth *sim = data;
+	const char *path = __ofono_atom_get_path(sim->atom);
+	int i = 0;
+	const unsigned char *aid_start = NULL;
+
+	sim->app_list = NULL;
+
+	while (i < len) {
+		uint8_t len;
+		struct sim_auth_app *app = NULL;
+
+		/*
+		 * TODO: There seems to be strings of 'FF' after each AID which
+		 * do not match up to the encoded lengths. Skipping these
+		 * sections until we reach the start of the next AID (not 0xff)
+		 */
+		while (dataobj[i] == 0xff)
+			i++;
+
+		/*
+		 * This just means we are at the end of the AID list, not
+		 * a parse error. Any error cases past this point are parse
+		 * errors.
+		 */
+		if (dataobj[i++] != 0x61)
+			break;
+
+		i++;
+
+		if (dataobj[i++] != 0x4f)
+			goto parse_error;
+
+		app = g_malloc(sizeof(struct sim_auth_app));
+		if (!app) {
+			ofono_error("failed to alloc SIM application");
+			goto parse_error;
+		}
+
+		len = dataobj[i++];
+
+		aid_start = dataobj + i;
+
+		i += len;
+
+		memcpy(app->aid, aid_start, 16);

src/sim-auth.c struct ofono_sim_auth *ofono_sim_auth_create(struct ofono_modem *modem, · line 541

Quoted 10 lines
src/sim-auth.c +10 -0
+
+		if (dataobj[i++] != 0x50) {
+			g_free(app->aid);
+			g_free(app);
+			goto parse_error;
+		}
+
+		len = dataobj[i++];
+
+		i += len;

We do have sim_parse_app_template_entries() and struct sim_app_record inside simutil.[ch]. Maybe that one does what you want?

Quoted 46 lines
src/sim-auth.c +43 -0
+
+		if (!memcmp(aid_start + 5, aid_usim, 2)) {
+			app->type = AID_TYPE_USIM;
+			g_dbus_register_interface(conn, path,
+					OFONO_USIM_APPLICATION_INTERFACE,
+					sim_auth_usim_app, NULL, NULL,
+					sim, NULL);
+		} else if (!memcmp(aid_start + 5, aid_isim, 2)) {
+			app->type = AID_TYPE_ISIM;
+			g_dbus_register_interface(conn, path,
+					OFONO_ISIM_APPLICATION_INTERFACE,
+					sim_auth_isim_app, NULL, NULL,
+					sim, NULL);
+		} else {
+			DBG("Unknown SIM application '%04x'",
+					*((uint16_t *)(aid_start + 5)));
+			/*
+			 * If we get here, the SIM application was not ISIM
+			 * or USIM, skip.
+			 */
+			g_free(app);
+			continue;
+		}
+
+		app->next = sim->app_list;
+		sim->app_list = app;
+	}
+
+	return;
+
+parse_error:
+	/*
+	 * Something went wrong parsing the AID list, it can't be assumed that
+	 * any previously parsed AID's are valid so free them all.
+	 */
+	DBG("Error parsing app list");
+	free_apps(sim);
+}
+
  void ofono_sim_auth_register(struct ofono_sim_auth *sa)
  {
+	struct ofono_modem *modem = __ofono_atom_get_modem(sa->atom);
+
+	ofono_modem_add_interface(modem, OFONO_PHONEBOOK_INTERFACE);
+
  	__ofono_atom_register(sa->atom, sim_auth_unregister);

src/sim-auth.c struct ofono_sim_auth *ofono_sim_auth_create(struct ofono_modem *modem, · line 597

Quoted 10 lines
src/sim-auth.c +10 -0
+
+	/* Do SIM application discovery, the cb will register DBus ifaces */
+	sa->driver->list_apps(sa, discover_apps_cb, sa);
+
+	sa->sim = __ofono_atom_find(OFONO_ATOM_TYPE_SIM, modem);
+
+	sa->gsm_access = __ofono_sim_service_available(sa->sim,
+			SIM_UST_SERVICE_GSM_ACCESS, 0);
+	sa->gsm_context = __ofono_sim_service_available(sa->sim,
+			SIM_UST_SERVICE_GSM_SECURITY_CONTEXT, 0);

I think this is actually checking SIM_SST_SERVICE_CHV1_DISABLE inadvertently... Not really in practice, but we should probably introduce a utility to only check the UST for this purpose.

src/sim-auth.c struct ofono_sim_auth *ofono_sim_auth_create(struct ofono_modem *modem, · line 600

Quoted 4 lines
src/sim-auth.c +0 -0
  }
 
  void ofono_sim_auth_remove(struct ofono_sim_auth *sa)

Regards,
-Denis

#3 lore raw

Re: [PATCH 2/7] sim-auth: implementation of core sim-auth atom

Quoted 4 lines

org.ofono.SimAuthentication on path /modem1

SimAuthentication would have a property with signature 'a{sv}' Applications

Actually reminds me that all our other interfaces do it using the array{object,dict} pattern.

E.g. array{object,dict} GetModems()
array{object,dict} GetCards()
array{object,dict} GetContexts()

So we should stick to that.

Quoted 2 lines

where
- 'o' Path -> Path to the application, something like /modem1/aid

So this part would just be the 'object' above serving as the key.

Quoted 4 lines

- 's' Type -> Gsm/Umts/Ims.  The interface (USimApplication, ISimApplication, etc) would depend on the type.
- 's' Name -> Human readable name

Regards,
-Denis