Skip to contentSkip to conversation

sim-auth: prep simauth/dbus headers

PATCH In review James Prestwood 2 participants 9 messages Sep 21, 2017

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 | 20 +++++++++++++++++++-
 2 files changed, 21 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 +19 -1
index 0a62adc..68c8602 100644
@@ -34,6 +34,14 @@ 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_gsm_authenticate_cb_t)(void *data, const guint8 *sres,
+		const guint8 *kc);
+
+typedef void (*ofono_umts_authenticate_cb_t)(void *data, const guint8 *res,
+		guint8 res_len, const guint8 *ck, const guint8 *ik);
+
 struct ofono_sim_auth_driver {
 	const char *name;
 	int (*probe)(struct ofono_sim_auth *sa, unsigned int vendor,
@@ -41,7 +49,17 @@ 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 (*gsm_authenticate)(struct ofono_sim_auth *sa,
+			ofono_gsm_authenticate_cb_t cb, int session_id,
+			const void *rand, void *data);
+	void (*umts_authenticate)(struct ofono_sim_auth *sa,
+			ofono_umts_authenticate_cb_t cb, int session_id,
+			const void *rand, const void *autn, 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 | 379 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 379 insertions(+)
src/sim-auth.c +379 -0
index 5d2f075..5c71644 100644
@@ -28,19 +28,94 @@
 #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 };
+
+enum aid_type {
+	AID_TYPE_USIM,
+	AID_TYPE_ISIM,
+	AID_TYPE_UNKNOWN
+};
+
+struct sim_auth_app {
+	/* Raw AID name returned from app discovery */
+	guint8 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;
 };
 
+/*
+ * 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;
+	gboolean umts;
+	const char *channel;
+};
+
+/*
+ * 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 +137,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 +191,310 @@ struct ofono_sim_auth *ofono_sim_auth_create(struct ofono_modem *modem,
 	return sa;
 }
 
+static void sim_auth_callback(void *data, const guint8 *sres,
+		const guint8 *kc)
+{
+	struct auth_handle *h = data;
+	struct ofono_sim_auth *sim = h->sim;
+	DBusMessage *reply;
+
+	if (!kc || !sres) {
+		reply = __ofono_error_not_supported(h->msg);
+		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:
+	__ofono_dbus_pending_reply(&h->msg, reply);
+
+	sim->driver->close_channel(sim, h->session_id);
+
+	g_free(h);
+}
+
+static void append_dict_byte_array(DBusMessageIter *iter, const char* key,
+		const void *arr, guint32 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 umts_auth_callback(void *data, const guint8 *res, guint8 res_len,
+		const guint8 *ck, const guint8 *ik)
+{
+	struct auth_handle *h = data;
+	struct ofono_sim_auth *sim = h->sim;
+	DBusMessage *reply;
+	DBusMessageIter iter;
+	DBusMessageIter dict;
+
+	if (!ck || !ik) {
+		reply = __ofono_error_not_supported(h->msg);
+		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);
+
+	/*
+	 * res == NULL and res_len == 0xFF means sync failure, in this case
+	 * both ck/ik will point to the AUTS value.
+	 */
+	if (!res && res_len == 0xff) {
+		append_dict_byte_array(&dict, "auts", ck, 16);
+	} else {
+		append_dict_byte_array(&dict, "res", res, res_len);
+		append_dict_byte_array(&dict, "ck", ck, 16);
+		append_dict_byte_array(&dict, "ik", ik, 16);
+	}
+
+	dbus_message_iter_close_container(&iter, &dict);
+
+umts_end:
+	__ofono_dbus_pending_reply(&h->msg, reply);
+
+	sim->driver->close_channel(sim, h->session_id);
+
+	g_free(h);
+
+	return;
+}
+
+static void open_channel_cb(int session_id, void *data)
+{
+	struct auth_handle *h = data;
+	struct ofono_sim_auth *sim = h->sim;
+
+	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;
+
+	if (h->umts) {
+		sim->driver->umts_authenticate(sim, umts_auth_callback,
+				session_id, h->rand, h->autn, h);
+	} else {
+		sim->driver->gsm_authenticate(sim, sim_auth_callback,
+				session_id, h->rand, h);
+	}
+}
+
+static DBusMessage *authenticate_common(DBusConnection *conn, DBusMessage *msg,
+					void *data, enum aid_type type,
+					gboolean umts)
+{
+	void *channel;
+	guint8 *rand = NULL;
+	guint8 *autn = NULL;
+	guint32 rlen;
+	guint32 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_gsm_authenticate(DBusConnection *conn,
+		DBusMessage *msg, void *data)
+{
+	return authenticate_common(conn, msg, data, AID_TYPE_ISIM, 0);
+}
+
+static DBusMessage *isim_umts_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 const GDBusMethodTable sim_auth_usim_app[] = {
+	{ GDBUS_ASYNC_METHOD("GSMAuthenticate",
+			GDBUS_ARGS({"rand", "ay"}),
+			GDBUS_ARGS({"sres", "ay"}, {"kc", "ay"}),
+			usim_gsm_authenticate) },
+	{ }
+};
+
+static const GDBusMethodTable sim_auth_isim_app[] = {
+	{ GDBUS_ASYNC_METHOD("GSMAuthenticate",
+			GDBUS_ARGS({"rand", "ay"}),
+			GDBUS_ARGS({"sres", "ay"}, {"kc", "ay"}),
+			isim_gsm_authenticate) },
+	{ GDBUS_ASYNC_METHOD("UMTSAuthenticate",
+			GDBUS_ARGS({"rand", "ay"}, {"autn", "ay"}),
+			GDBUS_ARGS({"return", "a{sv}"}),
+			isim_umts_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) {
+		guint8 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'",
+					*((guint16*)(aid_start + 5)));
+			/*
+			 * If we get here, the SIM application found should
+			 * probably be added to the supported types.
+			 *
+			 * If this was indeed a parsing error then free the
+			 * AID list.
+			 */
+			g_free(app);
+			goto parse_error;
+		}
+
+		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);
 }
 
 void ofono_sim_auth_remove(struct ofono_sim_auth *sa)
Signature · 1 line
2.7.4
#2 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
gsm_authenticate: Run GSM AUTHENTICATE with +CGLA
umts_authenticate: Run UMTS AUTHENTICATE with +CGLA

---
 drivers/atmodem/sim-auth.c | 232 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 232 insertions(+)
drivers/atmodem/sim-auth.c +232 -0
index 271ceed..71e7da3 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,230 @@ static void at_discover_apps(struct ofono_sim_auth *sa,
 	CALLBACK_WITH_FAILURE(cb, NULL, 0, data);
 }
 
+static void gsm_authenticate_cb(gboolean ok, GAtResult *result,
+		gpointer user_data)
+{
+	struct cb_data *cbd = user_data;
+	ofono_gsm_authenticate_cb_t cb = cbd->cb;
+	const char *values;
+	gint len = 0;
+	GAtResultIter iter;
+	guint8 sres[4];
+	guint8 kc[8];
+
+	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;
+
+	/* "04" + SRES(4) + "08" + KC(8) = 14 bytes * 2 = 28 characters */
+	if (len < 28)
+		goto error;
+
+	if (!g_at_result_iter_next_string(&iter, &values))
+		goto error;
+
+	if (strncmp(values, "04", 2))
+		goto error;
+
+	decode_hex_own_buf(values + 2, 8, NULL, 0, sres);
+
+	if (strncmp(values + 10, "08", 2))
+		goto error;
+
+	decode_hex_own_buf(values + 12, 16, NULL, 0, kc);
+
+	cb(cbd->data, sres, kc);
+
+	return;
+
+error:
+	cb(cbd->data, NULL, NULL);
+}
+
+static void at_gsm_authenticate(struct ofono_sim_auth *sa,
+		ofono_gsm_authenticate_cb_t cb, int session_id,
+		const void *rand, void *data)
+{
+	struct sim_auth_data *sad = ofono_sim_auth_get_data(sa);
+	struct cb_data *cbd = cb_data_new(cb, data);
+	char cmd[62];
+
+	int ret = sprintf(cmd, "AT+CGLA=%d,46,\"008800801110", session_id);
+
+	encode_hex_own_buf(rand, 16, 0, cmd + ret);
+	strcpy(cmd + ret + 32, "00\"");
+
+	if (g_at_chat_send(sad->chat, cmd, cgla_prefix, gsm_authenticate_cb,
+			cbd, g_free) > 0)
+		return;
+
+	g_free(cbd);
+
+	cb(data, NULL, NULL);
+}
+
+static void umts_authenticate_cb(gboolean ok, GAtResult *result,
+		gpointer user_data)
+{
+	struct cb_data *cbd = user_data;
+	ofono_umts_authenticate_cb_t cb = cbd->cb;
+	const char *values;
+	gint len = 0;
+	GAtResultIter iter;
+	guint8 res[8];
+	guint8 ck[16];
+	guint8 ik[16];
+
+	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, &values))
+		goto error;
+
+	if (!strncmp(values, "DB", 2)) {
+		/* 'DB' + '08' + RES(16) + '10' + CK(32) + '10' + IK(32) = 86 */
+		if (len < 86)
+			goto error;
+		/* success */
+		if (strncmp(values + 2, "08", 2))
+			goto error;
+
+		decode_hex_own_buf(values + 4, 16, NULL, 0, res);
+
+		if (strncmp(values + 20, "10", 2))
+			goto error;
+
+		decode_hex_own_buf(values + 22, 32, NULL, 0, ck);
+
+		if (strncmp(values + 54, "10", 2))
+			goto error;
+
+		decode_hex_own_buf(values + 56, 32, NULL, 0, ik);
+
+		cb(cbd->data, res, 8, ck, ik);
+	} else if (!strncmp(values, "DC", 2)) {
+		/* 'DB' + '10' + AUTS(16) = 36 */
+		if (len < 36)
+			goto error;
+		/* sync error */
+		if (strncmp(values + 2, "10", 2))
+			goto error;
+
+		decode_hex_own_buf(values + 4, 32, NULL, 0, ck);
+
+		cb(cbd->data, NULL, 0xff, ck, ck);
+	} else {
+		/* error */
+		goto error;
+	}
+
+	return;
+
+error:
+	cb(cbd->data, NULL, 0, NULL, NULL);
+}
+
+static void at_umts_authenticate(struct ofono_sim_auth *sa,
+		ofono_umts_authenticate_cb_t cb, int session_id,
+		const void *rand, const void *autn, void *data)
+{
+	struct sim_auth_data *sad = ofono_sim_auth_get_data(sa);
+	struct cb_data *cbd = cb_data_new(cb, data);
+	char cmd[150];
+
+	int ret = sprintf(cmd, "AT+CGLA=%d,80,\"008800812210", session_id);
+
+	encode_hex_own_buf(rand, 16, 0, cmd + ret);
+	ret += 32;
+
+	strcpy(cmd + ret, "10");
+	ret += 2;
+
+	encode_hex_own_buf(autn, 16, 0, cmd + ret);
+	ret += 32;
+
+	strcpy(cmd + ret, "00\"");
+
+	if (g_at_chat_send(sad->chat, cmd, cgla_prefix, umts_authenticate_cb,
+			cbd, g_free) > 0)
+		return;
+
+	g_free(cbd);
+
+	cb(data, NULL, 0, NULL, NULL);
+}
+
+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 gboolean at_sim_auth_register(gpointer user)
 {
 	struct ofono_sim_auth *sa = user;
@@ -151,6 +379,10 @@ 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,
+	.gsm_authenticate = at_gsm_authenticate,
+	.umts_authenticate = at_umts_authenticate
 };
 
 void at_sim_auth_init(void)
Signature · 1 line
2.7.4
#3 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
#4 lore raw

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

In reply to James Prestwood
---
 plugins/phonesim.c | 4 ++++
 1 file changed, 4 insertions(+)
plugins/phonesim.c +4 -0
index 16bccd5..345c41e 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 {
@@ -839,6 +841,8 @@ static void phonesim_pre_sim(struct ofono_modem *modem)
 		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
#5 lore raw

PATCH 6/7 test: added test to run USIM auth algorithm

In reply to James Prestwood
---
 test/run-isim-umts-auth | 25 +++++++++++++++++++++++++
 test/run-usim-gsm-auth  | 17 +++++++++++++++++
 2 files changed, 42 insertions(+)
 create mode 100755 test/run-isim-umts-auth
 create mode 100755 test/run-usim-gsm-auth
test/run-isim-umts-auth new file +25 -0
new file mode 100755
index 0000000..7341cf5
@@ -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.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-isim-umts-auth <modem> <rand> <autn>")
test/run-usim-gsm-auth new file +17 -0
new file mode 100755
index 0000000..21ee748
@@ -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.ISimApplication')
+	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>")
Signature · 1 line
2.7.4
#6 lore raw

PATCH 7/7 doc: documentation for SimAuth dbus interfaces

In reply to James Prestwood
---
 doc/sim-auth-api.txt | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
 create mode 100644 doc/sim-auth-api.txt
doc/sim-auth-api.txt new file +27 -0
new file mode 100644
index 0000000..466ca3a
@@ -0,0 +1,27 @@
+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
+
+Interface	org.ofono.ISimApplication
+
+Methods		dict UMTSAuthenticate(array{byte} rand, array{byte} autn)
+
+			Run the ISIM applications UMTS AUTHENTICATE algorithm
+			with 'rand' and 'autn'. A dictionary will be returned
+			containing either 'res', 'ck' and 'ik' OR 'auts'.
+			A dict containing 'auts' signifies there was a sync
+			failure.
+
+			Possible Errors: [service].Error.NotSupported
Signature · 1 line
2.7.4
#7 lore raw

Re: [PATCH 7/7] doc: documentation for SimAuth dbus interfaces

Hi James,

Quoted 11 lines · James Prestwood
---
  doc/sim-auth-api.txt | 27 +++++++++++++++++++++++++++
  1 file changed, 27 insertions(+)

create mode 100644 doc/sim-auth-api.txt

doc/sim-auth-api.txt new file +0 -0
new file mode 100644
index 0000000..466ca3a

doc/sim-auth-api.txt line 9

Quoted 9 lines
doc/sim-auth-api.txt new file +9 -0
@@ -0,0 +1,27 @@
+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)

We like our CamelCase, so GsmAuthenticate

doc/sim-auth-api.txt line 15

Quoted 6 lines
doc/sim-auth-api.txt new file +6 -0
+
+			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

USim Application can run both the GSM (if service 38 is available) and 3G security context, right? So there might need to be two methods here:
GsmAuthenticate
UmtsAuthenticate

doc/sim-auth-api.txt line 19

Quoted 4 lines
doc/sim-auth-api.txt new file +4 -0
+
+Interface	org.ofono.ISimApplication
+
+Methods		dict UMTSAuthenticate(array{byte} rand, array{byte} autn)

UmtsAuthenticate

doc/sim-auth-api.txt line 23

Quoted 4 lines
doc/sim-auth-api.txt new file +4 -0
+
+			Run the ISIM applications UMTS AUTHENTICATE algorithm
+			with 'rand' and 'autn'. A dictionary will be returned
+			containing either 'res', 'ck' and 'ik' OR 'auts'.

Also, according to 31.102 the Kc could also be returned.

doc/sim-auth-api.txt line 25

Quoted 2 lines
doc/sim-auth-api.txt new file +2 -0
+			A dict containing 'auts' signifies there was a sync
+			failure.

ISimApplication runs AKA-IMS security context, so maybe 'ImsAuthenticate'?

doc/sim-auth-api.txt line 27

Quoted 3 lines
doc/sim-auth-api.txt new file +2 -0
+
+			Possible Errors: [service].Error.NotSupported

So to summarize. USimApplication would have two methods, and ISimApplication would have one. Eventually we can add SimApplication for 2G SIMs, but that isn't a priority.

Regards,
-Denis

#8 lore raw
In reply to James Prestwood

Hi James,

Quoted 25 lines · James Prestwood

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

---
  include/dbus.h     |  2 ++
  include/sim-auth.h | 20 +++++++++++++++++++-
  2 files changed, 21 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 +0 -0
index 0a62adc..68c8602 100644

include/sim-auth.h typedef void (*ofono_sim_list_apps_cb_t)(const struct ofono_error *error, · line 40

Quoted 7 lines
include/sim-auth.h +4 -0
@@ -34,6 +34,14 @@ 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_gsm_authenticate_cb_t)(void *data, const guint8 *sres,
+		const guint8 *kc);

No glib types in include/* please. While both core & drivers use it, it might no longer be the case soon :)

Also, can't we just simply have the driver return the raw PDU? The core should be decoding the TLVs & such.

include/sim-auth.h typedef void (*ofono_sim_list_apps_cb_t)(const struct ofono_error *error, · line 43

Quoted 3 lines
include/sim-auth.h +3 -0
+
+typedef void (*ofono_umts_authenticate_cb_t)(void *data, const guint8 *res,
+		guint8 res_len, const guint8 *ck, const guint8 *ik);

As above

Quoted 10 lines
include/sim-auth.h +2 -1
+
  struct ofono_sim_auth_driver {
  	const char *name;
  	int (*probe)(struct ofono_sim_auth *sa, unsigned int vendor,
@@ -41,7 +49,17 @@ 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);

include/sim-auth.h struct ofono_sim_auth_driver { · line 62

Quoted 10 lines
include/sim-auth.h +10 -0
+	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 (*gsm_authenticate)(struct ofono_sim_auth *sa,
+			ofono_gsm_authenticate_cb_t cb, int session_id,
+			const void *rand, void *data);
+	void (*umts_authenticate)(struct ofono_sim_auth *sa,
+			ofono_umts_authenticate_cb_t cb, int session_id,
+			const void *rand, const void *autn, void *data);

I think we should do a logical_access method here and have the core compose the PDUs. Similar to how stk atom driver works.

include/sim-auth.h struct ofono_sim_auth_driver { · line 65

Quoted 4 lines
include/sim-auth.h +0 -0
  };
 
  int ofono_sim_auth_driver_register(const struct ofono_sim_auth_driver *d);

Regards,
-Denis