helpers: allow 19-digit reported ICCIDs

The mm_3gpp_parse_iccid() method does validation of the ICCID string
and was originally implemented to handle +CRSM reported values. The
implementation was looking for 20-digit strings, even for 19-digit
ICCIDs (those finished with a trailing 'F').

We now extend the logic to also validate ICCID strings reported as
19-digit values directly, and when that happens we won't allow
swapping of the digits (a +CRSM specific requirement) or trailing 'F'
characters (as that is only required when reporting 19-digit ICCIDs
with 20-digit strings).

This change allows us to e.g. use the u-blox specific AT+CCID command
and validate the returned ICCID with the same helper method, which
currently fails:

  (ttyACM2): --> 'AT+CCID<CR>'
  (ttyACM2): <-- '<CR><LF>+CCID: 8934077700015848638<CR><LF><CR><LF>OK<CR><LF>'
  couldn't load SIM identifier: 'Invalid ICCID response size (was 19, expected 20)'
This commit is contained in:
Aleksander Morgado
2018-09-12 17:17:36 +00:00
committed by Dan Williams
parent 4c669d3bf4
commit 0282123287
2 changed files with 32 additions and 8 deletions
+17 -8
View File
@@ -3537,10 +3537,10 @@ mm_3gpp_parse_iccid (const char *raw_iccid, GError **error)
goto error;
}
/* BCD encoded ICCIDs are 20 digits long */
if (len != 20) {
/* ICCIDs are 19 or 20 digits long */
if (len < 19 || len > 20) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Invalid ICCID response size (was %zd, expected 20)",
"Invalid ICCID response size (was %zd, expected 19 or 20)",
len);
goto error;
}
@@ -3549,9 +3549,16 @@ mm_3gpp_parse_iccid (const char *raw_iccid, GError **error)
* should be '89' for telecommunication purposes according to ISO/IEC 7812.
*/
if (buf[0] == '8' && buf[1] == '9') {
swap = FALSE;
swap = FALSE;
} else if (buf[0] == '9' && buf[1] == '8') {
swap = TRUE;
/* swapped digits are only expected in raw +CRSM responses, which must all
* be 20-bytes long */
if (len != 20) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Invalid ICCID response size while swap needed (expected 20)");
goto error;
}
swap = TRUE;
} else {
/* FIXME: Instead of erroring out, revisit this solution if we find any SIM
* that doesn't use '89' as the major industry identifier of the ICCID.
@@ -3562,9 +3569,10 @@ mm_3gpp_parse_iccid (const char *raw_iccid, GError **error)
}
/* Ensure if there's an 'F' that it's second-to-last if swap = TRUE,
* otherwise last if swap = FALSE */
* otherwise last if swap = FALSE. Also fail if an F is found within a
* 19-digit reported ICCID. */
if (f_pos >= 0) {
if ((swap && (f_pos != len - 2)) || (!swap && (f_pos != len - 1))) {
if ((len != 20) || (swap && (f_pos != len - 2)) || (!swap && (f_pos != len - 1))) {
g_set_error_literal (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Invalid ICCID length (unexpected F position)");
goto error;
@@ -3577,7 +3585,8 @@ mm_3gpp_parse_iccid (const char *raw_iccid, GError **error)
*
* 21436587 -> 12345678
*/
swapped = g_malloc0 (25);
g_assert (len == 20);
swapped = g_malloc0 (21);
for (i = 0; i < 10; i++) {
swapped[i * 2] = buf[(i * 2) + 1];
swapped[(i * 2) + 1] = buf[i * 2];
+15
View File
@@ -2090,6 +2090,20 @@ test_iccid_parse_unquoted_unswapped_19_digit (void *f, gpointer d)
g_free (parsed);
}
static void
test_iccid_parse_unquoted_unswapped_19_digit_no_f (void *f, gpointer d)
{
const char *raw_iccid = "8944200053671052499";
const char *expected = "8944200053671052499";
char *parsed;
GError *error = NULL;
parsed = mm_3gpp_parse_iccid (raw_iccid, &error);
g_assert_no_error (error);
g_assert_cmpstr (parsed, ==, expected);
g_free (parsed);
}
static void
test_iccid_parse_quoted_unswapped_20_digit (void *f, gpointer d)
{
@@ -3924,6 +3938,7 @@ int main (int argc, char **argv)
g_test_suite_add (suite, TESTCASE (test_iccid_parse_quoted_swap_19_digit, NULL));
g_test_suite_add (suite, TESTCASE (test_iccid_parse_unquoted_swap_20_digit, NULL));
g_test_suite_add (suite, TESTCASE (test_iccid_parse_unquoted_unswapped_19_digit, NULL));
g_test_suite_add (suite, TESTCASE (test_iccid_parse_unquoted_unswapped_19_digit_no_f, NULL));
g_test_suite_add (suite, TESTCASE (test_iccid_parse_quoted_unswapped_20_digit, NULL));
g_test_suite_add (suite, TESTCASE (test_iccid_parse_short, NULL));
g_test_suite_add (suite, TESTCASE (test_iccid_parse_invalid_chars, NULL));