blob: d469920065318839e8ab677a1a6ce7bdd845f103 [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 Hofmeyr32633e22017-10-06 04:26:21 +020037int db_update_sqn(struct db_context *dbc, int64_t id,
Harald Weltee72cf552016-04-28 07:18:49 +020038 uint64_t new_sqn)
39{
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020040 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_AUC_UPD_SQN];
Harald Weltee72cf552016-04-28 07:18:49 +020041 int rc;
42
43 /* bind new SQN and subscriber ID */
44 rc = sqlite3_bind_int64(stmt, 1, new_sqn);
45 if (rc != SQLITE_OK) {
46 LOGP(DAUC, LOGL_ERROR, "Error binding SQN: %d\n", rc);
47 return -1;
48 }
49
50 rc = sqlite3_bind_int64(stmt, 2, id);
51 if (rc != SQLITE_OK) {
52 LOGP(DAUC, LOGL_ERROR, "Error binding Subscrber ID: %d\n", rc);
53 return -1;
54 }
55
56 /* execute the statement */
57 rc = sqlite3_step(stmt);
58 if (rc != SQLITE_DONE) {
59 LOGP(DAUC, LOGL_ERROR, "Error updating SQN: %d\n", rc);
60 return -2;
61 }
62
Neels Hofmeyr76328e52017-10-09 22:49:25 +020063 db_remove_reset(stmt);
Harald Weltee72cf552016-04-28 07:18:49 +020064 return 0;
65}
66
Harald Weltecfc752b2016-05-05 16:38:14 +020067/* obtain the authentication data for a given imsi
68 * returns -1 in case of error, 0 for unknown IMSI, 1 for success */
Harald Weltee72cf552016-04-28 07:18:49 +020069int db_get_auth_data(struct db_context *dbc, const char *imsi,
70 struct osmo_sub_auth_data *aud2g,
71 struct osmo_sub_auth_data *aud3g,
Neels Hofmeyr32633e22017-10-06 04:26:21 +020072 int64_t *subscr_id)
Harald Weltee72cf552016-04-28 07:18:49 +020073{
Neels Hofmeyr4bde9492017-10-06 03:09:34 +020074 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_AUC_BY_IMSI];
Harald Weltecfc752b2016-05-05 16:38:14 +020075 int ret = 0;
Harald Weltee72cf552016-04-28 07:18:49 +020076 int rc;
77
78 memset(aud2g, 0, sizeof(*aud2g));
79 memset(aud3g, 0, sizeof(*aud3g));
80
81 /* bind the IMSI value */
82 rc = sqlite3_bind_text(stmt, 1, imsi, -1,
83 SQLITE_STATIC);
84 if (rc != SQLITE_OK) {
85 LOGAUC(imsi, LOGL_ERROR, "Error binding IMSI: %d\n", rc);
Neels Hofmeyr57a87922017-10-09 17:51:13 +020086 ret = -EIO;
Harald Weltecfc752b2016-05-05 16:38:14 +020087 goto out;
Harald Weltee72cf552016-04-28 07:18:49 +020088 }
89
90 /* execute the statement */
91 rc = sqlite3_step(stmt);
Harald Weltecfc752b2016-05-05 16:38:14 +020092 if (rc == SQLITE_DONE) {
Neels Hofmeyr40aa61c2017-10-09 17:56:04 +020093 LOGAUC(imsi, LOGL_INFO, "No such subscriber\n");
Neels Hofmeyr57a87922017-10-09 17:51:13 +020094 ret = -ENOENT;
Harald Weltecfc752b2016-05-05 16:38:14 +020095 goto out;
96 } else if (rc != SQLITE_ROW) {
Harald Weltee72cf552016-04-28 07:18:49 +020097 LOGAUC(imsi, LOGL_ERROR, "Error executing SQL: %d\n", rc);
Neels Hofmeyr57a87922017-10-09 17:51:13 +020098 ret = -EIO;
Harald Weltee72cf552016-04-28 07:18:49 +020099 goto out;
100 }
101
102 /* as an optimization, we retrieve the subscriber ID, to ensure we can
103 * update the SQN later without having to go back via a JOIN with the
104 * subscriber table. */
105 if (subscr_id)
106 *subscr_id = sqlite3_column_int64(stmt, 0);
107
Harald Weltee72cf552016-04-28 07:18:49 +0200108 /* obtain result values using sqlite3_column_*() */
109 if (sqlite3_column_type(stmt, 1) == SQLITE_INTEGER) {
110 /* we do have some 2G authentication data */
111 const uint8_t *ki;
112
113 aud2g->algo = sqlite3_column_int(stmt, 1);
114 ki = sqlite3_column_text(stmt, 2);
115#if 0
116 if (sqlite3_column_bytes(stmt, 2) != sizeof(aud2g->u.gsm.ki)) {
117 LOGAUC(imsi, LOGL_ERROR, "Error reading Ki: %d\n", rc);
118 goto end_2g;
119 }
120#endif
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100121 osmo_hexparse((void*)ki, (void*)&aud2g->u.gsm.ki, sizeof(aud2g->u.gsm.ki));
Harald Weltee72cf552016-04-28 07:18:49 +0200122 aud2g->type = OSMO_AUTH_TYPE_GSM;
123 } else
124 LOGAUC(imsi, LOGL_DEBUG, "No 2G Auth Data\n");
125//end_2g:
126 if (sqlite3_column_type(stmt, 3) == SQLITE_INTEGER) {
127 /* we do have some 3G authentication data */
128 const uint8_t *k, *op, *opc;
129
130 aud3g->algo = sqlite3_column_int(stmt, 3);
131 k = sqlite3_column_text(stmt, 4);
132 if (!k) {
133 LOGAUC(imsi, LOGL_ERROR, "Error reading K: %d\n", rc);
Neels Hofmeyr57a87922017-10-09 17:51:13 +0200134 ret = -EIO;
Harald Weltee72cf552016-04-28 07:18:49 +0200135 goto out;
136 }
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100137 osmo_hexparse((void*)k, (void*)&aud3g->u.umts.k, sizeof(aud3g->u.umts.k));
Harald Weltee72cf552016-04-28 07:18:49 +0200138 /* UMTS Subscribers can have either OP or OPC */
139 op = sqlite3_column_text(stmt, 5);
140 if (!op) {
141 opc = sqlite3_column_text(stmt, 6);
142 if (!opc) {
143 LOGAUC(imsi, LOGL_ERROR, "Error reading OPC: %d\n", rc);
Neels Hofmeyr57a87922017-10-09 17:51:13 +0200144 ret = -EIO;
Harald Weltee72cf552016-04-28 07:18:49 +0200145 goto out;
146 }
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100147 osmo_hexparse((void*)opc, (void*)&aud3g->u.umts.opc,
Harald Welte64f3ca32016-05-05 17:07:17 +0200148 sizeof(aud3g->u.umts.opc));
Harald Weltee72cf552016-04-28 07:18:49 +0200149 aud3g->u.umts.opc_is_op = 0;
150 } else {
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100151 osmo_hexparse((void*)op, (void*)&aud3g->u.umts.opc,
Harald Welte64f3ca32016-05-05 17:07:17 +0200152 sizeof(aud3g->u.umts.opc));
Harald Weltee72cf552016-04-28 07:18:49 +0200153 aud3g->u.umts.opc_is_op = 1;
154 }
155 aud3g->u.umts.sqn = sqlite3_column_int64(stmt, 7);
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100156 aud3g->u.umts.ind_bitlen = sqlite3_column_int(stmt, 8);
Harald Weltee72cf552016-04-28 07:18:49 +0200157 /* FIXME: amf? */
158 aud3g->type = OSMO_AUTH_TYPE_UMTS;
159 } else
160 LOGAUC(imsi, LOGL_DEBUG, "No 3G Auth Data\n");
Harald Weltecfc752b2016-05-05 16:38:14 +0200161
162 if (aud2g->type == 0 && aud3g->type == 0)
Neels Hofmeyr57a87922017-10-09 17:51:13 +0200163 ret = -ENOENT;
Harald Weltecfc752b2016-05-05 16:38:14 +0200164
Harald Weltee72cf552016-04-28 07:18:49 +0200165out:
Neels Hofmeyr76328e52017-10-09 22:49:25 +0200166 db_remove_reset(stmt);
Harald Weltecfc752b2016-05-05 16:38:14 +0200167 return ret;
Harald Weltee72cf552016-04-28 07:18:49 +0200168}
169
Harald Weltecfc752b2016-05-05 16:38:14 +0200170/* return -1 in case of error, 0 for unknown imsi, positive for number
171 * of vectors generated */
Harald Weltee72cf552016-04-28 07:18:49 +0200172int db_get_auc(struct db_context *dbc, const char *imsi,
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100173 unsigned int auc_3g_ind, struct osmo_auth_vector *vec,
174 unsigned int num_vec, const uint8_t *rand_auts,
175 const uint8_t *auts)
Harald Weltee72cf552016-04-28 07:18:49 +0200176{
177 struct osmo_sub_auth_data aud2g, aud3g;
Neels Hofmeyr32633e22017-10-06 04:26:21 +0200178 int64_t subscr_id;
Harald Weltecfc752b2016-05-05 16:38:14 +0200179 int ret = 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200180 int rc;
181
182 rc = db_get_auth_data(dbc, imsi, &aud2g, &aud3g, &subscr_id);
Neels Hofmeyr57a87922017-10-09 17:51:13 +0200183 if (rc)
Harald Weltee72cf552016-04-28 07:18:49 +0200184 return rc;
185
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100186 aud3g.u.umts.ind = auc_3g_ind;
187 if (aud3g.type == OSMO_AUTH_TYPE_UMTS
188 && aud3g.u.umts.ind >= (1U << aud3g.u.umts.ind_bitlen)) {
189 LOGAUC(imsi, LOGL_NOTICE, "3G auth: SQN's IND bitlen %u is"
190 " too small to hold an index of %u. Truncating. This"
191 " may cause numerous additional AUTS resyncing.\n",
192 aud3g.u.umts.ind_bitlen, aud3g.u.umts.ind);
193 aud3g.u.umts.ind &= (1U << aud3g.u.umts.ind_bitlen) - 1;
194 }
195
Neels Hofmeyr0acd31e2016-12-16 16:11:36 +0100196 LOGAUC(imsi, LOGL_DEBUG, "Calling to generate %u vectors\n", num_vec);
Harald Weltee72cf552016-04-28 07:18:49 +0200197 rc = auc_compute_vectors(vec, num_vec, &aud2g, &aud3g, rand_auts, auts);
Harald Weltecfc752b2016-05-05 16:38:14 +0200198 if (rc < 0) {
Harald Weltee72cf552016-04-28 07:18:49 +0200199 num_vec = 0;
Harald Weltecfc752b2016-05-05 16:38:14 +0200200 ret = -1;
201 } else {
Harald Weltee72cf552016-04-28 07:18:49 +0200202 num_vec = rc;
Harald Weltecfc752b2016-05-05 16:38:14 +0200203 ret = num_vec;
204 }
Harald Weltee72cf552016-04-28 07:18:49 +0200205 LOGAUC(imsi, LOGL_INFO, "Generated %u vectors\n", num_vec);
206
207 /* Update SQN in database, as needed */
208 if (aud3g.algo) {
Neels Hofmeyredebc222017-03-16 04:58:58 +0100209 LOGAUC(imsi, LOGL_DEBUG, "Updating SQN=%" PRIu64 " in DB\n",
210 aud3g.u.umts.sqn);
Harald Weltee72cf552016-04-28 07:18:49 +0200211 rc = db_update_sqn(dbc, subscr_id, aud3g.u.umts.sqn);
212 /* don't tell caller we generated any triplets in case of
213 * update error */
214 if (rc < 0) {
215 LOGAUC(imsi, LOGL_ERROR, "Error updating SQN: %d\n", rc);
216 num_vec = 0;
Harald Weltecfc752b2016-05-05 16:38:14 +0200217 ret = -1;
Harald Weltee72cf552016-04-28 07:18:49 +0200218 }
219 }
220
Harald Weltecfc752b2016-05-05 16:38:14 +0200221 return ret;
Harald Weltee72cf552016-04-28 07:18:49 +0200222}