hlr_auc_gw: Simplify string parsers with str_token()

The helper function allows these string parsers to be made much simpler.

Signed-off-by: Jouni Malinen <j@w1.fi>
This commit is contained in:
Jouni Malinen 2015-12-18 20:13:39 +02:00
parent d67e63d5a0
commit 0fc5707dde

View file

@ -1,6 +1,6 @@
/* /*
* HLR/AuC testing gateway for hostapd EAP-SIM/AKA database/authenticator * HLR/AuC testing gateway for hostapd EAP-SIM/AKA database/authenticator
* Copyright (c) 2005-2007, 2012-2013, Jouni Malinen <j@w1.fi> * Copyright (c) 2005-2007, 2012-2015, Jouni Malinen <j@w1.fi>
* *
* This software may be distributed under the terms of the BSD license. * This software may be distributed under the terms of the BSD license.
* See README for more details. * See README for more details.
@ -312,62 +312,37 @@ static int read_gsm_triplets(const char *fname)
} }
/* IMSI */ /* IMSI */
pos2 = strchr(pos, ':'); pos2 = NULL;
if (pos2 == NULL) { pos = str_token(buf, ":", &pos2);
printf("%s:%d - Invalid IMSI (%s)\n", if (!pos || os_strlen(pos) >= sizeof(g->imsi)) {
fname, line, pos); printf("%s:%d - Invalid IMSI\n", fname, line);
ret = -1;
break;
}
*pos2 = '\0';
if (strlen(pos) >= sizeof(g->imsi)) {
printf("%s:%d - Too long IMSI (%s)\n",
fname, line, pos);
ret = -1; ret = -1;
break; break;
} }
os_strlcpy(g->imsi, pos, sizeof(g->imsi)); os_strlcpy(g->imsi, pos, sizeof(g->imsi));
pos = pos2 + 1;
/* Kc */ /* Kc */
pos2 = strchr(pos, ':'); pos = str_token(buf, ":", &pos2);
if (pos2 == NULL) { if (!pos || os_strlen(pos) != 16 || hexstr2bin(pos, g->kc, 8)) {
printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos); printf("%s:%d - Invalid Kc\n", fname, line);
ret = -1; ret = -1;
break; break;
} }
*pos2 = '\0';
if (strlen(pos) != 16 || hexstr2bin(pos, g->kc, 8)) {
printf("%s:%d - Invalid Kc (%s)\n", fname, line, pos);
ret = -1;
break;
}
pos = pos2 + 1;
/* SRES */ /* SRES */
pos2 = strchr(pos, ':'); pos = str_token(buf, ":", &pos2);
if (pos2 == NULL) { if (!pos || os_strlen(pos) != 8 ||
printf("%s:%d - Invalid SRES (%s)\n", fname, line, hexstr2bin(pos, g->sres, 4)) {
pos); printf("%s:%d - Invalid SRES\n", fname, line);
ret = -1; ret = -1;
break; break;
} }
*pos2 = '\0';
if (strlen(pos) != 8 || hexstr2bin(pos, g->sres, 4)) {
printf("%s:%d - Invalid SRES (%s)\n", fname, line,
pos);
ret = -1;
break;
}
pos = pos2 + 1;
/* RAND */ /* RAND */
pos2 = strchr(pos, ':'); pos = str_token(buf, ":", &pos2);
if (pos2) if (!pos || os_strlen(pos) != 32 ||
*pos2 = '\0'; hexstr2bin(pos, g->_rand, 16)) {
if (strlen(pos) != 32 || hexstr2bin(pos, g->_rand, 16)) { printf("%s:%d - Invalid RAND\n", fname, line);
printf("%s:%d - Invalid RAND (%s)\n", fname, line,
pos);
ret = -1; ret = -1;
break; break;
} }
@ -449,86 +424,58 @@ static int read_milenage(const char *fname)
} }
/* IMSI */ /* IMSI */
pos2 = strchr(pos, ' '); pos2 = NULL;
if (pos2 == NULL) { pos = str_token(buf, " ", &pos2);
printf("%s:%d - Invalid IMSI (%s)\n", if (!pos || os_strlen(pos) >= sizeof(m->imsi)) {
fname, line, pos); printf("%s:%d - Invalid IMSI\n", fname, line);
ret = -1;
break;
}
*pos2 = '\0';
if (strlen(pos) >= sizeof(m->imsi)) {
printf("%s:%d - Too long IMSI (%s)\n",
fname, line, pos);
ret = -1; ret = -1;
break; break;
} }
os_strlcpy(m->imsi, pos, sizeof(m->imsi)); os_strlcpy(m->imsi, pos, sizeof(m->imsi));
pos = pos2 + 1;
/* Ki */ /* Ki */
pos2 = strchr(pos, ' '); pos = str_token(buf, " ", &pos2);
if (pos2 == NULL) { if (!pos || os_strlen(pos) != 32 ||
printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos); hexstr2bin(pos, m->ki, 16)) {
printf("%s:%d - Invalid Ki\n", fname, line);
ret = -1; ret = -1;
break; break;
} }
*pos2 = '\0';
if (strlen(pos) != 32 || hexstr2bin(pos, m->ki, 16)) {
printf("%s:%d - Invalid Ki (%s)\n", fname, line, pos);
ret = -1;
break;
}
pos = pos2 + 1;
/* OPc */ /* OPc */
pos2 = strchr(pos, ' '); pos = str_token(buf, " ", &pos2);
if (pos2 == NULL) { if (!pos || os_strlen(pos) != 32 ||
printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos); hexstr2bin(pos, m->opc, 16)) {
printf("%s:%d - Invalid OPc\n", fname, line);
ret = -1; ret = -1;
break; break;
} }
*pos2 = '\0';
if (strlen(pos) != 32 || hexstr2bin(pos, m->opc, 16)) {
printf("%s:%d - Invalid OPc (%s)\n", fname, line, pos);
ret = -1;
break;
}
pos = pos2 + 1;
/* AMF */ /* AMF */
pos2 = strchr(pos, ' '); pos = str_token(buf, " ", &pos2);
if (pos2 == NULL) { if (!pos || os_strlen(pos) != 4 || hexstr2bin(pos, m->amf, 2)) {
printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos); printf("%s:%d - Invalid AMF\n", fname, line);
ret = -1; ret = -1;
break; break;
} }
*pos2 = '\0';
if (strlen(pos) != 4 || hexstr2bin(pos, m->amf, 2)) {
printf("%s:%d - Invalid AMF (%s)\n", fname, line, pos);
ret = -1;
break;
}
pos = pos2 + 1;
/* SQN */ /* SQN */
pos2 = strchr(pos, ' '); pos = str_token(buf, " ", &pos2);
if (pos2) if (!pos || os_strlen(pos) != 12 ||
*pos2 = '\0'; hexstr2bin(pos, m->sqn, 6)) {
if (strlen(pos) != 12 || hexstr2bin(pos, m->sqn, 6)) { printf("%s:%d - Invalid SEQ\n", fname, line);
printf("%s:%d - Invalid SEQ (%s)\n", fname, line, pos);
ret = -1; ret = -1;
break; break;
} }
if (pos2) { pos = str_token(buf, " ", &pos2);
pos = pos2 + 1; if (pos) {
m->res_len = atoi(pos); m->res_len = atoi(pos);
if (m->res_len && if (m->res_len &&
(m->res_len < EAP_AKA_RES_MIN_LEN || (m->res_len < EAP_AKA_RES_MIN_LEN ||
m->res_len > EAP_AKA_RES_MAX_LEN)) { m->res_len > EAP_AKA_RES_MAX_LEN)) {
printf("%s:%d - Invalid RES_len (%s)\n", printf("%s:%d - Invalid RES_len\n",
fname, line, pos); fname, line);
ret = -1; ret = -1;
break; break;
} }
@ -1026,7 +973,7 @@ static void usage(void)
{ {
printf("HLR/AuC testing gateway for hostapd EAP-SIM/AKA " printf("HLR/AuC testing gateway for hostapd EAP-SIM/AKA "
"database/authenticator\n" "database/authenticator\n"
"Copyright (c) 2005-2007, 2012-2013, Jouni Malinen <j@w1.fi>\n" "Copyright (c) 2005-2007, 2012-2015, Jouni Malinen <j@w1.fi>\n"
"\n" "\n"
"usage:\n" "usage:\n"
"hlr_auc_gw [-hu] [-s<socket path>] [-g<triplet file>] " "hlr_auc_gw [-hu] [-s<socket path>] [-g<triplet file>] "