Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 1 | /* (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 Hofmeyr | ee392bb | 2017-03-16 05:20:50 +0100 | [diff] [blame] | 21 | #include <inttypes.h> |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 22 | |
| 23 | #include <osmocom/core/utils.h> |
| 24 | #include <osmocom/crypt/auth.h> |
| 25 | |
| 26 | #include <sqlite3.h> |
| 27 | |
| 28 | #include "logging.h" |
| 29 | #include "db.h" |
| 30 | #include "auc.h" |
| 31 | #include "rand.h" |
| 32 | |
| 33 | #define LOGAUC(imsi, level, fmt, args ...) LOGP(DAUC, level, "%s: " fmt, imsi, ## args) |
| 34 | |
| 35 | /* update the SQN for a given subscriber ID */ |
| 36 | int db_update_sqn(struct db_context *dbc, uint64_t id, |
| 37 | uint64_t new_sqn) |
| 38 | { |
| 39 | sqlite3_stmt *stmt = dbc->stmt[AUC_UPD_SQN]; |
| 40 | int rc; |
| 41 | |
| 42 | /* bind new SQN and subscriber ID */ |
| 43 | rc = sqlite3_bind_int64(stmt, 1, new_sqn); |
| 44 | if (rc != SQLITE_OK) { |
| 45 | LOGP(DAUC, LOGL_ERROR, "Error binding SQN: %d\n", rc); |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | rc = sqlite3_bind_int64(stmt, 2, id); |
| 50 | if (rc != SQLITE_OK) { |
| 51 | LOGP(DAUC, LOGL_ERROR, "Error binding Subscrber ID: %d\n", rc); |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | /* execute the statement */ |
| 56 | rc = sqlite3_step(stmt); |
| 57 | if (rc != SQLITE_DONE) { |
| 58 | LOGP(DAUC, LOGL_ERROR, "Error updating SQN: %d\n", rc); |
| 59 | return -2; |
| 60 | } |
| 61 | |
| 62 | /* remove bindings and reset statement to be re-executed */ |
| 63 | rc = sqlite3_clear_bindings(stmt); |
| 64 | if (rc != SQLITE_OK) { |
| 65 | LOGP(DAUC, LOGL_ERROR, "Error clerearing bindings: %d\n", rc); |
| 66 | } |
| 67 | rc = sqlite3_reset(stmt); |
| 68 | if (rc != SQLITE_OK) { |
| 69 | LOGP(DAUC, LOGL_ERROR, "Error in sqlite3_reset: %d\n", rc); |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 75 | /* obtain the authentication data for a given imsi |
| 76 | * returns -1 in case of error, 0 for unknown IMSI, 1 for success */ |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 77 | int db_get_auth_data(struct db_context *dbc, const char *imsi, |
| 78 | struct osmo_sub_auth_data *aud2g, |
| 79 | struct osmo_sub_auth_data *aud3g, |
| 80 | uint64_t *subscr_id) |
| 81 | { |
| 82 | sqlite3_stmt *stmt = dbc->stmt[AUC_BY_IMSI]; |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 83 | int ret = 0; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 84 | int rc; |
| 85 | |
| 86 | memset(aud2g, 0, sizeof(*aud2g)); |
| 87 | memset(aud3g, 0, sizeof(*aud3g)); |
| 88 | |
| 89 | /* bind the IMSI value */ |
| 90 | rc = sqlite3_bind_text(stmt, 1, imsi, -1, |
| 91 | SQLITE_STATIC); |
| 92 | if (rc != SQLITE_OK) { |
| 93 | LOGAUC(imsi, LOGL_ERROR, "Error binding IMSI: %d\n", rc); |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 94 | ret = -1; |
| 95 | goto out; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* execute the statement */ |
| 99 | rc = sqlite3_step(stmt); |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 100 | if (rc == SQLITE_DONE) { |
| 101 | LOGAUC(imsi, LOGL_INFO, "Unknown\n"); |
| 102 | ret = 0; |
| 103 | goto out; |
| 104 | } else if (rc != SQLITE_ROW) { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 105 | LOGAUC(imsi, LOGL_ERROR, "Error executing SQL: %d\n", rc); |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 106 | ret = -1; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 107 | goto out; |
| 108 | } |
| 109 | |
| 110 | /* as an optimization, we retrieve the subscriber ID, to ensure we can |
| 111 | * update the SQN later without having to go back via a JOIN with the |
| 112 | * subscriber table. */ |
| 113 | if (subscr_id) |
| 114 | *subscr_id = sqlite3_column_int64(stmt, 0); |
| 115 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 116 | /* obtain result values using sqlite3_column_*() */ |
| 117 | if (sqlite3_column_type(stmt, 1) == SQLITE_INTEGER) { |
| 118 | /* we do have some 2G authentication data */ |
| 119 | const uint8_t *ki; |
| 120 | |
| 121 | aud2g->algo = sqlite3_column_int(stmt, 1); |
| 122 | ki = sqlite3_column_text(stmt, 2); |
| 123 | #if 0 |
| 124 | if (sqlite3_column_bytes(stmt, 2) != sizeof(aud2g->u.gsm.ki)) { |
| 125 | LOGAUC(imsi, LOGL_ERROR, "Error reading Ki: %d\n", rc); |
| 126 | goto end_2g; |
| 127 | } |
| 128 | #endif |
Neels Hofmeyr | ec1b959 | 2016-12-11 01:22:23 +0100 | [diff] [blame] | 129 | osmo_hexparse((void*)ki, (void*)&aud2g->u.gsm.ki, sizeof(aud2g->u.gsm.ki)); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 130 | aud2g->type = OSMO_AUTH_TYPE_GSM; |
| 131 | } else |
| 132 | LOGAUC(imsi, LOGL_DEBUG, "No 2G Auth Data\n"); |
| 133 | //end_2g: |
| 134 | if (sqlite3_column_type(stmt, 3) == SQLITE_INTEGER) { |
| 135 | /* we do have some 3G authentication data */ |
| 136 | const uint8_t *k, *op, *opc; |
| 137 | |
| 138 | aud3g->algo = sqlite3_column_int(stmt, 3); |
| 139 | k = sqlite3_column_text(stmt, 4); |
| 140 | if (!k) { |
| 141 | LOGAUC(imsi, LOGL_ERROR, "Error reading K: %d\n", rc); |
| 142 | goto out; |
| 143 | } |
Neels Hofmeyr | ec1b959 | 2016-12-11 01:22:23 +0100 | [diff] [blame] | 144 | osmo_hexparse((void*)k, (void*)&aud3g->u.umts.k, sizeof(aud3g->u.umts.k)); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 145 | /* UMTS Subscribers can have either OP or OPC */ |
| 146 | op = sqlite3_column_text(stmt, 5); |
| 147 | if (!op) { |
| 148 | opc = sqlite3_column_text(stmt, 6); |
| 149 | if (!opc) { |
| 150 | LOGAUC(imsi, LOGL_ERROR, "Error reading OPC: %d\n", rc); |
| 151 | goto out; |
| 152 | } |
Neels Hofmeyr | ec1b959 | 2016-12-11 01:22:23 +0100 | [diff] [blame] | 153 | osmo_hexparse((void*)opc, (void*)&aud3g->u.umts.opc, |
Harald Welte | 64f3ca3 | 2016-05-05 17:07:17 +0200 | [diff] [blame] | 154 | sizeof(aud3g->u.umts.opc)); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 155 | aud3g->u.umts.opc_is_op = 0; |
| 156 | } else { |
Neels Hofmeyr | ec1b959 | 2016-12-11 01:22:23 +0100 | [diff] [blame] | 157 | osmo_hexparse((void*)op, (void*)&aud3g->u.umts.opc, |
Harald Welte | 64f3ca3 | 2016-05-05 17:07:17 +0200 | [diff] [blame] | 158 | sizeof(aud3g->u.umts.opc)); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 159 | aud3g->u.umts.opc_is_op = 1; |
| 160 | } |
| 161 | aud3g->u.umts.sqn = sqlite3_column_int64(stmt, 7); |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 162 | aud3g->u.umts.ind_bitlen = sqlite3_column_int(stmt, 8); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 163 | /* FIXME: amf? */ |
| 164 | aud3g->type = OSMO_AUTH_TYPE_UMTS; |
| 165 | } else |
| 166 | LOGAUC(imsi, LOGL_DEBUG, "No 3G Auth Data\n"); |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 167 | |
| 168 | if (aud2g->type == 0 && aud3g->type == 0) |
| 169 | ret = -1; |
| 170 | else |
| 171 | ret = 1; |
| 172 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 173 | out: |
| 174 | /* remove bindings and reset statement to be re-executed */ |
| 175 | rc = sqlite3_clear_bindings(stmt); |
| 176 | if (rc != SQLITE_OK) { |
| 177 | LOGAUC(imsi, LOGL_ERROR, "Error in sqlite3_clear_bindings(): %d\n", rc); |
| 178 | } |
| 179 | rc = sqlite3_reset(stmt); |
| 180 | if (rc != SQLITE_OK) { |
| 181 | LOGAUC(imsi, LOGL_ERROR, "Error in sqlite3_reset(): %d\n", rc); |
| 182 | } |
| 183 | |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 184 | return ret; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 187 | /* return -1 in case of error, 0 for unknown imsi, positive for number |
| 188 | * of vectors generated */ |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 189 | int db_get_auc(struct db_context *dbc, const char *imsi, |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 190 | unsigned int auc_3g_ind, struct osmo_auth_vector *vec, |
| 191 | unsigned int num_vec, const uint8_t *rand_auts, |
| 192 | const uint8_t *auts) |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 193 | { |
| 194 | struct osmo_sub_auth_data aud2g, aud3g; |
| 195 | uint64_t subscr_id; |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 196 | int ret = 0; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 197 | int rc; |
| 198 | |
| 199 | rc = db_get_auth_data(dbc, imsi, &aud2g, &aud3g, &subscr_id); |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 200 | if (rc <= 0) |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 201 | return rc; |
| 202 | |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 203 | aud3g.u.umts.ind = auc_3g_ind; |
| 204 | if (aud3g.type == OSMO_AUTH_TYPE_UMTS |
| 205 | && aud3g.u.umts.ind >= (1U << aud3g.u.umts.ind_bitlen)) { |
| 206 | LOGAUC(imsi, LOGL_NOTICE, "3G auth: SQN's IND bitlen %u is" |
| 207 | " too small to hold an index of %u. Truncating. This" |
| 208 | " may cause numerous additional AUTS resyncing.\n", |
| 209 | aud3g.u.umts.ind_bitlen, aud3g.u.umts.ind); |
| 210 | aud3g.u.umts.ind &= (1U << aud3g.u.umts.ind_bitlen) - 1; |
| 211 | } |
| 212 | |
Neels Hofmeyr | 0acd31e | 2016-12-16 16:11:36 +0100 | [diff] [blame] | 213 | LOGAUC(imsi, LOGL_DEBUG, "Calling to generate %u vectors\n", num_vec); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 214 | rc = auc_compute_vectors(vec, num_vec, &aud2g, &aud3g, rand_auts, auts); |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 215 | if (rc < 0) { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 216 | num_vec = 0; |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 217 | ret = -1; |
| 218 | } else { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 219 | num_vec = rc; |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 220 | ret = num_vec; |
| 221 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 222 | LOGAUC(imsi, LOGL_INFO, "Generated %u vectors\n", num_vec); |
| 223 | |
| 224 | /* Update SQN in database, as needed */ |
| 225 | if (aud3g.algo) { |
Neels Hofmeyr | edebc22 | 2017-03-16 04:58:58 +0100 | [diff] [blame] | 226 | LOGAUC(imsi, LOGL_DEBUG, "Updating SQN=%" PRIu64 " in DB\n", |
| 227 | aud3g.u.umts.sqn); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 228 | rc = db_update_sqn(dbc, subscr_id, aud3g.u.umts.sqn); |
| 229 | /* don't tell caller we generated any triplets in case of |
| 230 | * update error */ |
| 231 | if (rc < 0) { |
| 232 | LOGAUC(imsi, LOGL_ERROR, "Error updating SQN: %d\n", rc); |
| 233 | num_vec = 0; |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 234 | ret = -1; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 238 | return ret; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 239 | } |