blob: 975eb2d576f9ebf2a6c6997d0fa75153c0055d9e [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
Neels Hofmeyr2f758032019-11-20 00:37:07 +010029#include <osmocom/hlr/logging.h>
30#include <osmocom/hlr/db.h>
31#include <osmocom/hlr/auc.h>
32#include <osmocom/hlr/rand.h>
Harald Weltee72cf552016-04-28 07:18:49 +020033
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) {
Stefan Sperling705b61b2018-12-07 12:44:50 +010052 LOGP(DAUC, LOGL_ERROR, "Cannot update SQN for subscriber ID=%" PRId64
Neels Hofmeyr1cbdb702017-10-09 23:03:57 +020053 ": 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) {
Stefan Sperling705b61b2018-12-07 12:44:50 +010062 LOGP(DAUC, LOGL_ERROR, "Cannot update SQN for subscriber ID=%" PRId64
Neels Hofmeyr1cbdb702017-10-09 23:03:57 +020063 ": no auc_3g entry for such subscriber\n", subscr_id);
64 ret = -ENOENT;
65 } else if (rc != 1) {
Stefan Sperling705b61b2018-12-07 12:44:50 +010066 LOGP(DAUC, LOGL_ERROR, "Update SQN for subscriber ID=%" PRId64
Neels Hofmeyr1cbdb702017-10-09 23:03:57 +020067 ": 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
Oliver Smith6401b902019-07-24 11:32:45 +020076/* hexparse a specific column of a sqlite prepared statement into dst (with length check)
77 * returns 0 for success, -EIO on error */
78static int hexparse_stmt(uint8_t *dst, size_t dst_len, sqlite3_stmt *stmt, int col, const char *col_name,
79 const char *imsi)
80{
81 const uint8_t *text;
82 size_t col_len;
83
84 /* Bytes are stored as hex strings in database, hence divide length by two */
85 col_len = sqlite3_column_bytes(stmt, col) / 2;
86
87 if (col_len != dst_len) {
88 LOGAUC(imsi, LOGL_ERROR, "Error reading %s, expected length %lu but has length %lu\n", col_name,
89 dst_len, col_len);
90 return -EIO;
91 }
92
93 text = sqlite3_column_text(stmt, col);
94 if (!text) {
95 LOGAUC(imsi, LOGL_ERROR, "Error reading %s\n", col_name);
96 return -EIO;
97 }
98 osmo_hexparse((void *)text, dst, dst_len);
99 return 0;
100}
101
Harald Weltecfc752b2016-05-05 16:38:14 +0200102/* obtain the authentication data for a given imsi
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100103 * returns 0 for success, negative value on error:
104 * -ENOENT if the IMSI is not known, -ENOKEY if the IMSI is known but has no auth data,
105 * -EIO on db failure */
Harald Weltee72cf552016-04-28 07:18:49 +0200106int db_get_auth_data(struct db_context *dbc, const char *imsi,
107 struct osmo_sub_auth_data *aud2g,
108 struct osmo_sub_auth_data *aud3g,
Neels Hofmeyr32633e22017-10-06 04:26:21 +0200109 int64_t *subscr_id)
Harald Weltee72cf552016-04-28 07:18:49 +0200110{
Neels Hofmeyr4bde9492017-10-06 03:09:34 +0200111 sqlite3_stmt *stmt = dbc->stmt[DB_STMT_AUC_BY_IMSI];
Harald Weltecfc752b2016-05-05 16:38:14 +0200112 int ret = 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200113 int rc;
114
115 memset(aud2g, 0, sizeof(*aud2g));
116 memset(aud3g, 0, sizeof(*aud3g));
117
Neels Hofmeyrc5122f22017-10-09 23:12:57 +0200118 if (!db_bind_text(stmt, "$imsi", imsi))
119 return -EIO;
Harald Weltee72cf552016-04-28 07:18:49 +0200120
121 /* execute the statement */
122 rc = sqlite3_step(stmt);
Harald Weltecfc752b2016-05-05 16:38:14 +0200123 if (rc == SQLITE_DONE) {
Neels Hofmeyr40aa61c2017-10-09 17:56:04 +0200124 LOGAUC(imsi, LOGL_INFO, "No such subscriber\n");
Neels Hofmeyr57a87922017-10-09 17:51:13 +0200125 ret = -ENOENT;
Harald Weltecfc752b2016-05-05 16:38:14 +0200126 goto out;
127 } else if (rc != SQLITE_ROW) {
Harald Weltee72cf552016-04-28 07:18:49 +0200128 LOGAUC(imsi, LOGL_ERROR, "Error executing SQL: %d\n", rc);
Neels Hofmeyr57a87922017-10-09 17:51:13 +0200129 ret = -EIO;
Harald Weltee72cf552016-04-28 07:18:49 +0200130 goto out;
131 }
132
133 /* as an optimization, we retrieve the subscriber ID, to ensure we can
134 * update the SQN later without having to go back via a JOIN with the
135 * subscriber table. */
136 if (subscr_id)
137 *subscr_id = sqlite3_column_int64(stmt, 0);
138
Harald Weltee72cf552016-04-28 07:18:49 +0200139 /* obtain result values using sqlite3_column_*() */
140 if (sqlite3_column_type(stmt, 1) == SQLITE_INTEGER) {
141 /* we do have some 2G authentication data */
Oliver Smith6401b902019-07-24 11:32:45 +0200142 if (hexparse_stmt(aud2g->u.gsm.ki, sizeof(aud2g->u.gsm.ki), stmt, 2, "Ki", imsi))
Harald Weltee72cf552016-04-28 07:18:49 +0200143 goto end_2g;
Oliver Smith6401b902019-07-24 11:32:45 +0200144 aud2g->algo = sqlite3_column_int(stmt, 1);
Harald Weltee72cf552016-04-28 07:18:49 +0200145 aud2g->type = OSMO_AUTH_TYPE_GSM;
146 } else
147 LOGAUC(imsi, LOGL_DEBUG, "No 2G Auth Data\n");
Oliver Smith6401b902019-07-24 11:32:45 +0200148end_2g:
Harald Weltee72cf552016-04-28 07:18:49 +0200149 if (sqlite3_column_type(stmt, 3) == SQLITE_INTEGER) {
150 /* we do have some 3G authentication data */
Oliver Smith6401b902019-07-24 11:32:45 +0200151 if (hexparse_stmt(aud3g->u.umts.k, sizeof(aud3g->u.umts.k), stmt, 4, "K", imsi)) {
Neels Hofmeyr57a87922017-10-09 17:51:13 +0200152 ret = -EIO;
Harald Weltee72cf552016-04-28 07:18:49 +0200153 goto out;
154 }
Oliver Smith6401b902019-07-24 11:32:45 +0200155 aud3g->algo = sqlite3_column_int(stmt, 3);
156
Harald Weltee72cf552016-04-28 07:18:49 +0200157 /* UMTS Subscribers can have either OP or OPC */
Oliver Smith6401b902019-07-24 11:32:45 +0200158 if (sqlite3_column_text(stmt, 5)) {
159 if (hexparse_stmt(aud3g->u.umts.opc, sizeof(aud3g->u.umts.opc), stmt, 5, "OP", imsi)) {
Neels Hofmeyr57a87922017-10-09 17:51:13 +0200160 ret = -EIO;
Harald Weltee72cf552016-04-28 07:18:49 +0200161 goto out;
162 }
Harald Weltee72cf552016-04-28 07:18:49 +0200163 aud3g->u.umts.opc_is_op = 1;
Oliver Smith6401b902019-07-24 11:32:45 +0200164 } else {
165 if (hexparse_stmt(aud3g->u.umts.opc, sizeof(aud3g->u.umts.opc), stmt, 6, "OPC", imsi)) {
166 ret = -EIO;
167 goto out;
168 }
169 aud3g->u.umts.opc_is_op = 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200170 }
171 aud3g->u.umts.sqn = sqlite3_column_int64(stmt, 7);
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100172 aud3g->u.umts.ind_bitlen = sqlite3_column_int(stmt, 8);
Harald Weltee72cf552016-04-28 07:18:49 +0200173 /* FIXME: amf? */
174 aud3g->type = OSMO_AUTH_TYPE_UMTS;
175 } else
176 LOGAUC(imsi, LOGL_DEBUG, "No 3G Auth Data\n");
Harald Weltecfc752b2016-05-05 16:38:14 +0200177
178 if (aud2g->type == 0 && aud3g->type == 0)
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100179 ret = -ENOKEY;
Harald Weltecfc752b2016-05-05 16:38:14 +0200180
Harald Weltee72cf552016-04-28 07:18:49 +0200181out:
Neels Hofmeyr76328e52017-10-09 22:49:25 +0200182 db_remove_reset(stmt);
Harald Weltecfc752b2016-05-05 16:38:14 +0200183 return ret;
Harald Weltee72cf552016-04-28 07:18:49 +0200184}
185
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100186/* return number of vectors generated, negative value on error:
187 * -ENOENT if the IMSI is not known, -ENOKEY if the IMSI is known but has no auth data,
188 * -EIO on db failure */
Harald Weltee72cf552016-04-28 07:18:49 +0200189int db_get_auc(struct db_context *dbc, const char *imsi,
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100190 unsigned int auc_3g_ind, struct osmo_auth_vector *vec,
191 unsigned int num_vec, const uint8_t *rand_auts,
Harald Welte06f5af22019-08-21 20:01:31 +0200192 const uint8_t *auts, bool separation_bit)
Harald Weltee72cf552016-04-28 07:18:49 +0200193{
194 struct osmo_sub_auth_data aud2g, aud3g;
Neels Hofmeyr32633e22017-10-06 04:26:21 +0200195 int64_t subscr_id;
Harald Weltecfc752b2016-05-05 16:38:14 +0200196 int ret = 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200197 int rc;
198
199 rc = db_get_auth_data(dbc, imsi, &aud2g, &aud3g, &subscr_id);
Neels Hofmeyr57a87922017-10-09 17:51:13 +0200200 if (rc)
Harald Weltee72cf552016-04-28 07:18:49 +0200201 return rc;
202
Neels Hofmeyrfc4af602019-12-12 17:06:22 +0100203 /* modulo by the IND bitlen value range. For example, ind_bitlen == 5 would modulo 32:
204 * 1 << 5 == 0b0100000 == 32
205 * - 1 == 0b0011111 == bitmask of 5 lowest bits
206 * x &= 0b0011111 == modulo 32
207 * Why do this? osmo-hlr cannot possibly choose individual VLR INDs always matching all subscribers' IND_bitlen,
208 * which might vary wildly. Instead, let hlr.c pass in an arbitrarily high number here, and the modulo does a
209 * round-robin if the IND pools that this subscriber has available. */
210 auc_3g_ind &= (1U << aud3g.u.umts.ind_bitlen) - 1;
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100211 aud3g.u.umts.ind = auc_3g_ind;
Neels Hofmeyrfc4af602019-12-12 17:06:22 +0100212
Harald Welte06f5af22019-08-21 20:01:31 +0200213 /* the first bit (bit0) cannot be used as AMF anymore, but has been
214 * re-appropriated as the separation bit. See 3GPP TS 33.102 Annex H
215 * together with 3GPP TS 33.401 / 33.402 / 33.501 */
216 aud3g.u.umts.amf[0] = aud3g.u.umts.amf[0] & 0x7f;
217 if (separation_bit)
218 aud3g.u.umts.amf[0] |= 0x80;
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100219
Neels Hofmeyr0acd31e2016-12-16 16:11:36 +0100220 LOGAUC(imsi, LOGL_DEBUG, "Calling to generate %u vectors\n", num_vec);
Harald Weltee72cf552016-04-28 07:18:49 +0200221 rc = auc_compute_vectors(vec, num_vec, &aud2g, &aud3g, rand_auts, auts);
Harald Weltecfc752b2016-05-05 16:38:14 +0200222 if (rc < 0) {
Harald Weltee72cf552016-04-28 07:18:49 +0200223 num_vec = 0;
Harald Weltecfc752b2016-05-05 16:38:14 +0200224 ret = -1;
225 } else {
Harald Weltee72cf552016-04-28 07:18:49 +0200226 num_vec = rc;
Harald Weltecfc752b2016-05-05 16:38:14 +0200227 ret = num_vec;
228 }
Harald Weltee72cf552016-04-28 07:18:49 +0200229 LOGAUC(imsi, LOGL_INFO, "Generated %u vectors\n", num_vec);
230
231 /* Update SQN in database, as needed */
232 if (aud3g.algo) {
Neels Hofmeyredebc222017-03-16 04:58:58 +0100233 LOGAUC(imsi, LOGL_DEBUG, "Updating SQN=%" PRIu64 " in DB\n",
234 aud3g.u.umts.sqn);
Harald Weltee72cf552016-04-28 07:18:49 +0200235 rc = db_update_sqn(dbc, subscr_id, aud3g.u.umts.sqn);
236 /* don't tell caller we generated any triplets in case of
237 * update error */
238 if (rc < 0) {
239 LOGAUC(imsi, LOGL_ERROR, "Error updating SQN: %d\n", rc);
240 num_vec = 0;
Harald Weltecfc752b2016-05-05 16:38:14 +0200241 ret = -1;
Harald Weltee72cf552016-04-28 07:18:49 +0200242 }
243 }
244
Harald Weltecfc752b2016-05-05 16:38:14 +0200245 return ret;
Harald Weltee72cf552016-04-28 07:18:49 +0200246}