blob: 918bf82fad13c25c503b1724f1a80351b0646e17 [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>
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 */
35int 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 */
75int 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
Harald Weltee72cf552016-04-28 07:18:49 +0200106 /* obtain result values using sqlite3_column_*() */
107 if (sqlite3_column_type(stmt, 1) == SQLITE_INTEGER) {
108 /* we do have some 2G authentication data */
109 const uint8_t *ki;
110
111 aud2g->algo = sqlite3_column_int(stmt, 1);
112 ki = sqlite3_column_text(stmt, 2);
113#if 0
114 if (sqlite3_column_bytes(stmt, 2) != sizeof(aud2g->u.gsm.ki)) {
115 LOGAUC(imsi, LOGL_ERROR, "Error reading Ki: %d\n", rc);
116 goto end_2g;
117 }
118#endif
119 memcpy(&aud2g->u.gsm.ki, ki, sizeof(aud2g->u.gsm.ki));
120 aud2g->type = OSMO_AUTH_TYPE_GSM;
121 } else
122 LOGAUC(imsi, LOGL_DEBUG, "No 2G Auth Data\n");
123//end_2g:
124 if (sqlite3_column_type(stmt, 3) == SQLITE_INTEGER) {
125 /* we do have some 3G authentication data */
126 const uint8_t *k, *op, *opc;
127
128 aud3g->algo = sqlite3_column_int(stmt, 3);
129 k = sqlite3_column_text(stmt, 4);
130 if (!k) {
131 LOGAUC(imsi, LOGL_ERROR, "Error reading K: %d\n", rc);
132 goto out;
133 }
134 memcpy(&aud3g->u.umts.k, k, sizeof(aud3g->u.umts.k));
135 /* UMTS Subscribers can have either OP or OPC */
136 op = sqlite3_column_text(stmt, 5);
137 if (!op) {
138 opc = sqlite3_column_text(stmt, 6);
139 if (!opc) {
140 LOGAUC(imsi, LOGL_ERROR, "Error reading OPC: %d\n", rc);
141 goto out;
142 }
143 memcpy(&aud3g->u.umts.opc, opc, sizeof(aud3g->u.umts.opc));
144 aud3g->u.umts.opc_is_op = 0;
145 } else {
146 memcpy(&aud3g->u.umts.opc, op, sizeof(aud3g->u.umts.opc));
147 aud3g->u.umts.opc_is_op = 1;
148 }
149 aud3g->u.umts.sqn = sqlite3_column_int64(stmt, 7);
150 /* FIXME: amf? */
151 aud3g->type = OSMO_AUTH_TYPE_UMTS;
152 } else
153 LOGAUC(imsi, LOGL_DEBUG, "No 3G Auth Data\n");
154out:
155 /* remove bindings and reset statement to be re-executed */
156 rc = sqlite3_clear_bindings(stmt);
157 if (rc != SQLITE_OK) {
158 LOGAUC(imsi, LOGL_ERROR, "Error in sqlite3_clear_bindings(): %d\n", rc);
159 }
160 rc = sqlite3_reset(stmt);
161 if (rc != SQLITE_OK) {
162 LOGAUC(imsi, LOGL_ERROR, "Error in sqlite3_reset(): %d\n", rc);
163 }
164
165 if (aud2g->type == 0 && aud3g->type == 0)
166 return -1;
167
168 return 0;
169}
170
171int db_get_auc(struct db_context *dbc, const char *imsi,
172 struct osmo_auth_vector *vec, unsigned int num_vec,
173 const uint8_t *rand_auts, const uint8_t *auts)
174{
175 struct osmo_sub_auth_data aud2g, aud3g;
176 uint64_t subscr_id;
177 int rc;
178
179 rc = db_get_auth_data(dbc, imsi, &aud2g, &aud3g, &subscr_id);
180 if (rc < 0)
181 return rc;
182
183 LOGAUC(imsi, LOGL_INFO, "Calling to generate %u vectors\n", num_vec);
184 rc = auc_compute_vectors(vec, num_vec, &aud2g, &aud3g, rand_auts, auts);
185 if (rc < 0)
186 num_vec = 0;
187 else
188 num_vec = rc;
189 LOGAUC(imsi, LOGL_INFO, "Generated %u vectors\n", num_vec);
190
191 /* Update SQN in database, as needed */
192 if (aud3g.algo) {
193 LOGAUC(imsi, LOGL_DEBUG, "Updating SQN in DB\n");
194 rc = db_update_sqn(dbc, subscr_id, aud3g.u.umts.sqn);
195 /* don't tell caller we generated any triplets in case of
196 * update error */
197 if (rc < 0) {
198 LOGAUC(imsi, LOGL_ERROR, "Error updating SQN: %d\n", rc);
199 num_vec = 0;
200 }
201 }
202
203 return num_vec;
204}