blob: 7bbc93f7eb590ad4bae9be17e76aa6b761b9670d [file] [log] [blame]
Harald Weltee72cf552016-04-28 07:18:49 +02001/* (C) 2015 by Harald Welte <laforge@gnumonks.org>
2 *
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20#include <string.h>
Neels Hofmeyree392bb2017-03-16 05:20:50 +010021#include <inttypes.h>
Neels Hofmeyr57a87922017-10-09 17:51:13 +020022#include <errno.h>
Harald Weltee72cf552016-04-28 07:18:49 +020023
24#include <osmocom/core/utils.h>
25#include <osmocom/crypt/auth.h>
26
27#include <sqlite3.h>
28
29#include "logging.h"
30#include "db.h"
31#include "auc.h"
32#include "rand.h"
33
Neels Hofmeyr40aa61c2017-10-09 17:56:04 +020034#define LOGAUC(imsi, level, fmt, args ...) LOGP(DAUC, level, "IMSI='%s': " fmt, imsi, ## args)
Harald Weltee72cf552016-04-28 07:18:49 +020035
36/* update the SQN for a given subscriber ID */
Neels Hofmeyr1cbdb702017-10-09 23:03:57 +020037int db_update_sqn(struct db_context *dbc, int64_t subscr_id, uint64_t new_sqn)
Harald Weltee72cf552016-04-28 07:18:49 +020038{
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020039 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_AUC_UPD_SQN];
Harald Weltee72cf552016-04-28 07:18:49 +020040 int rc;
Neels Hofmeyr1cbdb702017-10-09 23:03:57 +020041 int ret = 0;
Harald Weltee72cf552016-04-28 07:18:49 +020042
Neels Hofmeyr1cbdb702017-10-09 23:03:57 +020043 if (!db_bind_int64(stmt, "$sqn", new_sqn))
44 return -EIO;
Harald Weltee72cf552016-04-28 07:18:49 +020045
Neels Hofmeyr1cbdb702017-10-09 23:03:57 +020046 if (!db_bind_int64(stmt, "$subscriber_id", subscr_id))
47 return -EIO;
Harald Weltee72cf552016-04-28 07:18:49 +020048
49 /* execute the statement */
50 rc = sqlite3_step(stmt);
51 if (rc != SQLITE_DONE) {
Neels Hofmeyr1cbdb702017-10-09 23:03:57 +020052 LOGP(DAUC, LOGL_ERROR, "Cannot update SQN for subscriber ID=%"PRId64
53 ": SQL error: (%d) %s\n",
54 subscr_id, rc, sqlite3_errmsg(dbc->db));
55 ret = -EIO;
56 goto out;
Harald Weltee72cf552016-04-28 07:18:49 +020057 }
58
Neels Hofmeyr1cbdb702017-10-09 23:03:57 +020059 /* verify execution result */
60 rc = sqlite3_changes(dbc->db);
61 if (!rc) {
62 LOGP(DAUC, LOGL_ERROR, "Cannot update SQN for subscriber ID=%"PRId64
63 ": no auc_3g entry for such subscriber\n", subscr_id);
64 ret = -ENOENT;
65 } else if (rc != 1) {
66 LOGP(DAUC, LOGL_ERROR, "Update SQN for subscriber ID=%"PRId64
67 ": SQL modified %d rows (expected 1)\n", subscr_id, rc);
68 ret = -EIO;
69 }
70
71out:
Neels Hofmeyr76328e52017-10-09 22:49:25 +020072 db_remove_reset(stmt);
Neels Hofmeyr1cbdb702017-10-09 23:03:57 +020073 return ret;
Harald Weltee72cf552016-04-28 07:18:49 +020074}
75
Harald Weltecfc752b2016-05-05 16:38:14 +020076/* obtain the authentication data for a given imsi
77 * returns -1 in case of error, 0 for unknown IMSI, 1 for success */
Harald Weltee72cf552016-04-28 07:18:49 +020078int db_get_auth_data(struct db_context *dbc, const char *imsi,
79 struct osmo_sub_auth_data *aud2g,
80 struct osmo_sub_auth_data *aud3g,
Neels Hofmeyr32633e22017-10-06 04:26:21 +020081 int64_t *subscr_id)
Harald Weltee72cf552016-04-28 07:18:49 +020082{
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020083 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_AUC_BY_IMSI];
Harald Weltecfc752b2016-05-05 16:38:14 +020084 int ret = 0;
Harald Weltee72cf552016-04-28 07:18:49 +020085 int rc;
86
87 memset(aud2g, 0, sizeof(*aud2g));
88 memset(aud3g, 0, sizeof(*aud3g));
89
Neels Hofmeyrc5122f22017-10-09 23:12:57 +020090 if (!db_bind_text(stmt, "$imsi", imsi))
91 return -EIO;
Harald Weltee72cf552016-04-28 07:18:49 +020092
93 /* execute the statement */
94 rc = sqlite3_step(stmt);
Harald Weltecfc752b2016-05-05 16:38:14 +020095 if (rc == SQLITE_DONE) {
Neels Hofmeyr40aa61c2017-10-09 17:56:04 +020096 LOGAUC(imsi, LOGL_INFO, "No such subscriber\n");
Neels Hofmeyr57a87922017-10-09 17:51:13 +020097 ret = -ENOENT;
Harald Weltecfc752b2016-05-05 16:38:14 +020098 goto out;
99 } else if (rc != SQLITE_ROW) {
Harald Weltee72cf552016-04-28 07:18:49 +0200100 LOGAUC(imsi, LOGL_ERROR, "Error executing SQL: %d\n", rc);
Neels Hofmeyr57a87922017-10-09 17:51:13 +0200101 ret = -EIO;
Harald Weltee72cf552016-04-28 07:18:49 +0200102 goto out;
103 }
104
105 /* as an optimization, we retrieve the subscriber ID, to ensure we can
106 * update the SQN later without having to go back via a JOIN with the
107 * subscriber table. */
108 if (subscr_id)
109 *subscr_id = sqlite3_column_int64(stmt, 0);
110
Harald Weltee72cf552016-04-28 07:18:49 +0200111 /* obtain result values using sqlite3_column_*() */
112 if (sqlite3_column_type(stmt, 1) == SQLITE_INTEGER) {
113 /* we do have some 2G authentication data */
114 const uint8_t *ki;
115
116 aud2g->algo = sqlite3_column_int(stmt, 1);
117 ki = sqlite3_column_text(stmt, 2);
118#if 0
119 if (sqlite3_column_bytes(stmt, 2) != sizeof(aud2g->u.gsm.ki)) {
120 LOGAUC(imsi, LOGL_ERROR, "Error reading Ki: %d\n", rc);
121 goto end_2g;
122 }
123#endif
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100124 osmo_hexparse((void*)ki, (void*)&aud2g->u.gsm.ki, sizeof(aud2g->u.gsm.ki));
Harald Weltee72cf552016-04-28 07:18:49 +0200125 aud2g->type = OSMO_AUTH_TYPE_GSM;
126 } else
127 LOGAUC(imsi, LOGL_DEBUG, "No 2G Auth Data\n");
128//end_2g:
129 if (sqlite3_column_type(stmt, 3) == SQLITE_INTEGER) {
130 /* we do have some 3G authentication data */
131 const uint8_t *k, *op, *opc;
132
133 aud3g->algo = sqlite3_column_int(stmt, 3);
134 k = sqlite3_column_text(stmt, 4);
135 if (!k) {
136 LOGAUC(imsi, LOGL_ERROR, "Error reading K: %d\n", rc);
Neels Hofmeyr57a87922017-10-09 17:51:13 +0200137 ret = -EIO;
Harald Weltee72cf552016-04-28 07:18:49 +0200138 goto out;
139 }
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100140 osmo_hexparse((void*)k, (void*)&aud3g->u.umts.k, sizeof(aud3g->u.umts.k));
Harald Weltee72cf552016-04-28 07:18:49 +0200141 /* UMTS Subscribers can have either OP or OPC */
142 op = sqlite3_column_text(stmt, 5);
143 if (!op) {
144 opc = sqlite3_column_text(stmt, 6);
145 if (!opc) {
146 LOGAUC(imsi, LOGL_ERROR, "Error reading OPC: %d\n", rc);
Neels Hofmeyr57a87922017-10-09 17:51:13 +0200147 ret = -EIO;
Harald Weltee72cf552016-04-28 07:18:49 +0200148 goto out;
149 }
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100150 osmo_hexparse((void*)opc, (void*)&aud3g->u.umts.opc,
Harald Welte64f3ca32016-05-05 17:07:17 +0200151 sizeof(aud3g->u.umts.opc));
Harald Weltee72cf552016-04-28 07:18:49 +0200152 aud3g->u.umts.opc_is_op = 0;
153 } else {
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100154 osmo_hexparse((void*)op, (void*)&aud3g->u.umts.opc,
Harald Welte64f3ca32016-05-05 17:07:17 +0200155 sizeof(aud3g->u.umts.opc));
Harald Weltee72cf552016-04-28 07:18:49 +0200156 aud3g->u.umts.opc_is_op = 1;
157 }
158 aud3g->u.umts.sqn = sqlite3_column_int64(stmt, 7);
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100159 aud3g->u.umts.ind_bitlen = sqlite3_column_int(stmt, 8);
Harald Weltee72cf552016-04-28 07:18:49 +0200160 /* FIXME: amf? */
161 aud3g->type = OSMO_AUTH_TYPE_UMTS;
162 } else
163 LOGAUC(imsi, LOGL_DEBUG, "No 3G Auth Data\n");
Harald Weltecfc752b2016-05-05 16:38:14 +0200164
165 if (aud2g->type == 0 && aud3g->type == 0)
Neels Hofmeyr57a87922017-10-09 17:51:13 +0200166 ret = -ENOENT;
Harald Weltecfc752b2016-05-05 16:38:14 +0200167
Harald Weltee72cf552016-04-28 07:18:49 +0200168out:
Neels Hofmeyr76328e52017-10-09 22:49:25 +0200169 db_remove_reset(stmt);
Harald Weltecfc752b2016-05-05 16:38:14 +0200170 return ret;
Harald Weltee72cf552016-04-28 07:18:49 +0200171}
172
Harald Weltecfc752b2016-05-05 16:38:14 +0200173/* return -1 in case of error, 0 for unknown imsi, positive for number
174 * of vectors generated */
Harald Weltee72cf552016-04-28 07:18:49 +0200175int db_get_auc(struct db_context *dbc, const char *imsi,
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100176 unsigned int auc_3g_ind, struct osmo_auth_vector *vec,
177 unsigned int num_vec, const uint8_t *rand_auts,
178 const uint8_t *auts)
Harald Weltee72cf552016-04-28 07:18:49 +0200179{
180 struct osmo_sub_auth_data aud2g, aud3g;
Neels Hofmeyr32633e22017-10-06 04:26:21 +0200181 int64_t subscr_id;
Harald Weltecfc752b2016-05-05 16:38:14 +0200182 int ret = 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200183 int rc;
184
185 rc = db_get_auth_data(dbc, imsi, &aud2g, &aud3g, &subscr_id);
Neels Hofmeyr57a87922017-10-09 17:51:13 +0200186 if (rc)
Harald Weltee72cf552016-04-28 07:18:49 +0200187 return rc;
188
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100189 aud3g.u.umts.ind = auc_3g_ind;
190 if (aud3g.type == OSMO_AUTH_TYPE_UMTS
191 && aud3g.u.umts.ind >= (1U << aud3g.u.umts.ind_bitlen)) {
192 LOGAUC(imsi, LOGL_NOTICE, "3G auth: SQN's IND bitlen %u is"
193 " too small to hold an index of %u. Truncating. This"
194 " may cause numerous additional AUTS resyncing.\n",
195 aud3g.u.umts.ind_bitlen, aud3g.u.umts.ind);
196 aud3g.u.umts.ind &= (1U << aud3g.u.umts.ind_bitlen) - 1;
197 }
198
Neels Hofmeyr0acd31e2016-12-16 16:11:36 +0100199 LOGAUC(imsi, LOGL_DEBUG, "Calling to generate %u vectors\n", num_vec);
Harald Weltee72cf552016-04-28 07:18:49 +0200200 rc = auc_compute_vectors(vec, num_vec, &aud2g, &aud3g, rand_auts, auts);
Harald Weltecfc752b2016-05-05 16:38:14 +0200201 if (rc < 0) {
Harald Weltee72cf552016-04-28 07:18:49 +0200202 num_vec = 0;
Harald Weltecfc752b2016-05-05 16:38:14 +0200203 ret = -1;
204 } else {
Harald Weltee72cf552016-04-28 07:18:49 +0200205 num_vec = rc;
Harald Weltecfc752b2016-05-05 16:38:14 +0200206 ret = num_vec;
207 }
Harald Weltee72cf552016-04-28 07:18:49 +0200208 LOGAUC(imsi, LOGL_INFO, "Generated %u vectors\n", num_vec);
209
210 /* Update SQN in database, as needed */
211 if (aud3g.algo) {
Neels Hofmeyredebc222017-03-16 04:58:58 +0100212 LOGAUC(imsi, LOGL_DEBUG, "Updating SQN=%" PRIu64 " in DB\n",
213 aud3g.u.umts.sqn);
Harald Weltee72cf552016-04-28 07:18:49 +0200214 rc = db_update_sqn(dbc, subscr_id, aud3g.u.umts.sqn);
215 /* don't tell caller we generated any triplets in case of
216 * update error */
217 if (rc < 0) {
218 LOGAUC(imsi, LOGL_ERROR, "Error updating SQN: %d\n", rc);
219 num_vec = 0;
Harald Weltecfc752b2016-05-05 16:38:14 +0200220 ret = -1;
Harald Weltee72cf552016-04-28 07:18:49 +0200221 }
222 }
223
Harald Weltecfc752b2016-05-05 16:38:14 +0200224 return ret;
Harald Weltee72cf552016-04-28 07:18:49 +0200225}