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