Skip to contentSkip to conversation

sim-auth: Parse auth response according to TS 31.102

PATCH In review Slava Monich 2 participants 2 messages May 3, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sim-auth: Parse auth response according to TS 31.102

---
 src/sim-auth.c      |  34 ++++++-----
 src/simutil.c       | 146 +++++++++++++++++++++++++++++++++-----------
 src/simutil.h       |  13 ++--
 unit/test-simutil.c | 125 ++++++++++++++++++++++++++++---------
 4 files changed, 234 insertions(+), 84 deletions(-)
src/sim-auth.c +19 -15
index 3c3f35e7..6dab52ee 100644
@@ -207,14 +207,10 @@ static void handle_umts(struct ofono_sim_auth *sa, const uint8_t *resp,
 	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;
+	struct data_block res, ck, ik, auts, sres, kc;
 
 	if (!sim_parse_umts_authenticate(resp, len, &res, &ck, &ik,
-			&auts, &kc))
+			&auts, &sres, &kc))
 		goto umts_end;
 
 	reply = dbus_message_new_method_return(sa->pending->msg);
@@ -224,15 +220,23 @@ static void handle_umts(struct ofono_sim_auth *sa, const uint8_t *resp,
 	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
 			"{say}", &dict);
 
-	if (auts) {
-		append_dict_byte_array(&dict, "AUTS", auts, 14);
-	} 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);
-	}
+	if (auts.data)
+		append_dict_byte_array(&dict, "AUTS", auts.data, auts.len);
+
+	if (sres.data)
+		append_dict_byte_array(&dict, "SRES", sres.data, sres.len);
+
+	if (res.data)
+		append_dict_byte_array(&dict, "RES", res.data, res.len);
+
+	if (ck.data)
+		append_dict_byte_array(&dict, "CK", ck.data, ck.len);
+
+	if (ik.data)
+		append_dict_byte_array(&dict, "IK", ik.data, ik.len);
+
+	if (kc.data)
+		append_dict_byte_array(&dict, "Kc", kc.data, kc.len);
 
 	dbus_message_iter_close_container(&iter, &dict);
 
src/simutil.c +109 -37

172 lines not on this page · raw message

src/simutil.h +9 -4
index 33b775a7..f908bbb4 100644
@@ -371,6 +371,11 @@ struct comprehension_tlv_builder {
 	struct ber_tlv_builder *parent;
 };
 
+struct data_block {
+	const unsigned char *data;
+	unsigned int len;
+};
+
 void simple_tlv_iter_init(struct simple_tlv_iter *iter,
 				const unsigned char *pdu, unsigned int len);
 gboolean simple_tlv_iter_next(struct simple_tlv_iter *iter);
@@ -527,10 +532,10 @@ int sim_build_umts_authenticate(unsigned char *buffer, int len,
 int sim_build_gsm_authenticate(unsigned char *buffer, int len,
 		const unsigned char *rand);
 
-gboolean sim_parse_umts_authenticate(const unsigned char *buffer,
-		int len, const unsigned char **res, const unsigned char **ck,
-		const unsigned char **ik, const unsigned char **auts,
-		const unsigned char **kc);
+gboolean sim_parse_umts_authenticate(const unsigned char *buffer, int len,
+		struct data_block *res, struct data_block *ck,
+		struct data_block *ik, struct data_block *auts,
+		struct data_block *sres, struct data_block *kc);
 
 gboolean sim_parse_gsm_authenticate(const unsigned char *buffer, int len,
 		const unsigned char **sres, const unsigned char **kc);
unit/test-simutil.c +97 -28

166 lines not on this page · raw message

Signature · 1 line
2.25.1
#1 lore raw

Hi Slava,

Quoted 1 line · Slava Monich

---

So what is this trying to fix? I mean I can understand 1 line patch fixes that come without a description. These tend to be self-evident. 300+ line diff needs a least some explanation.

Quoted 5 lines
  src/sim-auth.c      |  34 ++++++-----
  src/simutil.c       | 146 +++++++++++++++++++++++++++++++++-----------
  src/simutil.h       |  13 ++--
  unit/test-simutil.c | 125 ++++++++++++++++++++++++++++---------
  4 files changed, 234 insertions(+), 84 deletions(-)

This really should be broken down into at least 2 commits. See HACKING, 'Submitting Patches' section, item 3.

Quoted 30 lines
src/sim-auth.c +2 -10
index 3c3f35e7..6dab52ee 100644
@@ -207,14 +207,10 @@ static void handle_umts(struct ofono_sim_auth *sa, const uint8_t *resp,
  	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;
+	struct data_block res, ck, ik, auts, sres, kc;
 
  	if (!sim_parse_umts_authenticate(resp, len, &res, &ck, &ik,
-			&auts, &kc))
+			&auts, &sres, &kc))
  		goto umts_end;
 
  	reply = dbus_message_new_method_return(sa->pending->msg);
@@ -224,15 +220,23 @@ static void handle_umts(struct ofono_sim_auth *sa, const uint8_t *resp,
  	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
  			"{say}", &dict);
 
-	if (auts) {
-		append_dict_byte_array(&dict, "AUTS", auts, 14);
-	} else {
-		append_dict_byte_array(&dict, "RES", res, 8);

src/sim-auth.c static void handle_umts(struct ofono_sim_auth *sa, const uint8_t *resp, · line 227

Quoted 10 lines
src/sim-auth.c +5 -5
-		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);
-	}
+	if (auts.data)
+		append_dict_byte_array(&dict, "AUTS", auts.data, auts.len);
+
+	if (sres.data)
+		append_dict_byte_array(&dict, "SRES", sres.data, sres.len);

You're modifying the behavior of the D-Bus API without actually updating the relevant docs.

Also, this part looks suspect. SRES is only done in GSM context, so shouldn't this be taken care sim_parse_gsm_authenticate()?

Quoted 21 lines
src/sim-auth.c +12 -0
+
+	if (res.data)
+		append_dict_byte_array(&dict, "RES", res.data, res.len);
+
+	if (ck.data)
+		append_dict_byte_array(&dict, "CK", ck.data, ck.len);
+
+	if (ik.data)
+		append_dict_byte_array(&dict, "IK", ik.data, ik.len);
+
+	if (kc.data)
+		append_dict_byte_array(&dict, "Kc", kc.data, kc.len);
 
  	dbus_message_iter_close_container(&iter, &dict);
 
src/simutil.c +0 -0
index 5d2aa6a2..30b76f05 100644
@@ -1672,63 +1672,135 @@ int sim_build_gsm_authenticate(unsigned char *buffer, int len,
  	return build_authenticate(buffer, rand, NULL);

src/simutil.c int sim_build_gsm_authenticate(unsigned char *buffer, int len, · line 1678

Quoted 10 lines
src/simutil.c +4 -4
  }
 
-gboolean sim_parse_umts_authenticate(const unsigned char *buffer,
-		int len, const unsigned char **res, const unsigned char **ck,
-		const unsigned char **ik, const unsigned char **auts,
-		const unsigned char **kc)
+gboolean sim_parse_umts_authenticate(const unsigned char *buffer, int len,
+		struct data_block *res, struct data_block *ck,
+		struct data_block *ik, struct data_block *auts,
+		struct data_block *sres, struct data_block *kc)

At least by my reading of 33.102, lengths of ck, ik, kc and sres are set in stone, are they not?

Also, RFC 4187 also sets in stone the definition of AUTS (and RES, AUTN, etc). So you probably have to explain how and when AUTS length could be not in line with that RFC? The authentication API is mostly meant to be used by implementations of this RFC, so we would need to take appropriate steps that nothing is broken by this change.

Quoted 6 lines
src/simutil.c +4 -1
  {
-	if (len < 16 || !buffer)
+	const unsigned char *ptr = buffer;
+	const unsigned char *end = ptr + len;
+	unsigned int l;
+

src/simutil.c int sim_build_gsm_authenticate(unsigned char *buffer, int len, · line 1692

Quoted 10 lines
src/simutil.c +7 -1
+	if (!buffer || len < 2)
  		return FALSE;
 
-	switch (buffer[0]) {
+	memset(res, 0, sizeof(*res));
+	memset(ck, 0, sizeof(*ck));
+	memset(ik, 0, sizeof(*ik));
+	memset(kc, 0, sizeof(*kc));
+	memset(auts, 0, sizeof(*auts));
+	memset(sres, 0, sizeof(*sres));

Generally we don't like side-effecting arguments on error. If you're going through the trouble of introducing a new structure, might as well just have it represent the result of the parse operation and cut down on arguments. Maybe something like:

struct sim_auth_umts_result {
	uint8_t success;
	union {
		struct {
			const unsigned char *auts;
			uint8_t auts_len;
		}
		struct {
			const unsigned char *res;
			uint8_t res_len;
			const unsigned char *ck;
			const unsigned char *ik;
			const unsigned char *ic;
		}
	};
}

Regards,
-Denis