blob: 3ec08937881abca6bc2b904632fefcfb8f301a0c [file] [log] [blame]
Harald Weltea854b482023-05-30 17:27:32 +02001/* (C) 2015-2023 by Harald Welte <laforge@gnumonks.org>
Harald Weltee72cf552016-04-28 07:18:49 +02002 *
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 Hofmeyr8d97d342017-02-21 22:46:35 +010021#include <inttypes.h>
Harald Weltee72cf552016-04-28 07:18:49 +020022
23#include <osmocom/core/utils.h>
24#include <osmocom/crypt/auth.h>
25
Neels Hofmeyr2f758032019-11-20 00:37:07 +010026#include <osmocom/hlr/logging.h>
27#include <osmocom/hlr/rand.h>
Harald Weltee72cf552016-04-28 07:18:49 +020028
Neels Hofmeyr8d97d342017-02-21 22:46:35 +010029#define hexb(buf) osmo_hexdump_nospc((void*)buf, sizeof(buf))
30#define hex(buf,sz) osmo_hexdump_nospc((void*)buf, sz)
31
Harald Weltee72cf552016-04-28 07:18:49 +020032/* compute given number of vectors using either aud2g or aud2g or a combination
Neels Hofmeyr5b581ac2017-01-19 15:54:01 +010033 * of both. Handles re-synchronization if rand_auts and auts are set */
Harald Weltee72cf552016-04-28 07:18:49 +020034int auc_compute_vectors(struct osmo_auth_vector *vec, unsigned int num_vec,
Harald Weltea854b482023-05-30 17:27:32 +020035 struct osmo_sub_auth_data2 *aud2g,
36 struct osmo_sub_auth_data2 *aud3g,
Harald Weltee72cf552016-04-28 07:18:49 +020037 const uint8_t *rand_auts, const uint8_t *auts)
38{
39 unsigned int i;
40 uint8_t rand[16];
Neels Hofmeyr8d97d342017-02-21 22:46:35 +010041 struct osmo_auth_vector vtmp;
Harald Weltee72cf552016-04-28 07:18:49 +020042 int rc;
43
Neels Hofmeyr8d97d342017-02-21 22:46:35 +010044 /* no need to iterate the log categories all the time */
45 int dbg = log_check_level(DAUC, LOGL_DEBUG);
46#define DBGP(args ...) if (dbg) DEBUGP(DAUC, ##args)
47#define DBGVB(member) DBGP("vector [%u]: " #member " = %s\n", \
48 i, hexb(vec[i].member))
49#define DBGVV(fmt, member) DBGP("vector [%u]: " #member " = " fmt "\n", \
50 i, vec[i].member)
51
Neels Hofmeyr569d3222017-02-21 22:57:11 +010052 if (aud2g && (aud2g->algo == OSMO_AUTH_ALG_NONE
53 || aud2g->type == OSMO_AUTH_TYPE_NONE))
Harald Weltee72cf552016-04-28 07:18:49 +020054 aud2g = NULL;
Neels Hofmeyr569d3222017-02-21 22:57:11 +010055 if (aud3g && (aud3g->algo == OSMO_AUTH_ALG_NONE
56 || aud3g->type == OSMO_AUTH_TYPE_NONE))
Harald Weltee72cf552016-04-28 07:18:49 +020057 aud3g = NULL;
58
Neels Hofmeyr569d3222017-02-21 22:57:11 +010059 if (!aud2g && !aud3g) {
60 LOGP(DAUC, LOGL_ERROR, "auc_compute_vectors() called"
61 " with neither 2G nor 3G auth data available\n");
Harald Weltee72cf552016-04-28 07:18:49 +020062 return -1;
Neels Hofmeyr569d3222017-02-21 22:57:11 +010063 }
64
65 if (aud2g && aud2g->type != OSMO_AUTH_TYPE_GSM) {
66 LOGP(DAUC, LOGL_ERROR, "auc_compute_vectors() called"
67 " with non-2G auth data passed for aud2g arg\n");
68 return -1;
69 }
70
71 if (aud3g && aud3g->type != OSMO_AUTH_TYPE_UMTS) {
72 LOGP(DAUC, LOGL_ERROR, "auc_compute_vectors() called"
73 " with non-3G auth data passed for aud3g arg\n");
74 return -1;
75 }
76
77 if ((rand_auts != NULL) != (auts != NULL)) {
78 LOGP(DAUC, LOGL_ERROR, "auc_compute_vectors() with only one"
79 " of AUTS and AUTS_RAND given, need both or neither\n");
80 return -1;
81 }
82
83 if (auts && !aud3g) {
84 LOGP(DAUC, LOGL_ERROR, "auc_compute_vectors() with AUTS called"
85 " but no 3G auth data passed\n");
86 return -1;
87 }
Harald Weltee72cf552016-04-28 07:18:49 +020088
Neels Hofmeyr8d97d342017-02-21 22:46:35 +010089 DBGP("Computing %d auth vector%s: %s%s\n",
90 num_vec, num_vec == 1 ? "" : "s",
91 aud3g? (aud2g? "3G + separate 2G"
92 : "3G only (2G derived from 3G keys)")
93 : "2G only",
94 auts? ", with AUTS resync" : "");
95 if (aud3g) {
Harald Weltea854b482023-05-30 17:27:32 +020096 DBGP("3G: k = %s\n", hex(aud3g->u.umts.k, aud3g->u.umts.k_len));
Neels Hofmeyr8d97d342017-02-21 22:46:35 +010097 DBGP("3G: %s = %s\n",
98 aud3g->u.umts.opc_is_op? "OP" : "opc",
Harald Weltea854b482023-05-30 17:27:32 +020099 hex(aud3g->u.umts.opc, aud3g->u.umts.opc_len));
Neels Hofmeyredebc222017-03-16 04:58:58 +0100100 DBGP("3G: for sqn ind %u, previous sqn was %" PRIu64 "\n",
101 aud3g->u.umts.ind, aud3g->u.umts.sqn);
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100102 }
103 if (aud2g)
104 DBGP("2G: ki = %s\n", hexb(aud2g->u.gsm.ki));
105
Harald Weltee72cf552016-04-28 07:18:49 +0200106 for (i = 0; i < num_vec; i++) {
107 rc = rand_get(rand, sizeof(rand));
108 if (rc != sizeof(rand)) {
109 LOGP(DAUC, LOGL_ERROR, "Unable to read %zu random "
110 "bytes: rc=%d\n", sizeof(rand), rc);
111 goto out;
112 }
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100113 DBGP("vector [%u]: rand = %s\n", i, hexb(rand));
Harald Weltee72cf552016-04-28 07:18:49 +0200114
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100115 if (aud3g) {
Harald Weltee72cf552016-04-28 07:18:49 +0200116 /* 3G or 3G + 2G case */
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100117
Harald Weltea854b482023-05-30 17:27:32 +0200118 /* backwards-compatibiliy: We assume all RES are 8 bytes long */
119 vec[i].res_len = 8;
120
Neels Hofmeyrb5b11e32017-02-22 01:42:43 +0100121 /* Do AUTS only for the first vector or we would use
122 * the same SQN for each following key. */
123 if ((i == 0) && auts) {
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100124 DBGP("vector [%u]: resync: auts = %s\n",
125 i, hex(auts, 14));
126 DBGP("vector [%u]: resync: rand_auts = %s\n",
127 i, hex(rand_auts, 16));
128
Harald Weltea854b482023-05-30 17:27:32 +0200129 rc = osmo_auth_gen_vec_auts2(vec+i, aud3g, auts,
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100130 rand_auts, rand);
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100131 } else {
Harald Weltea854b482023-05-30 17:27:32 +0200132 rc = osmo_auth_gen_vec2(vec+i, aud3g, rand);
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100133 }
Harald Weltee72cf552016-04-28 07:18:49 +0200134 if (rc < 0) {
135 LOGP(DAUC, LOGL_ERROR, "Error in 3G vector "
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100136 "generation: [%u]: rc = %d\n", i, rc);
Harald Weltee72cf552016-04-28 07:18:49 +0200137 goto out;
138 }
Neels Hofmeyree392bb2017-03-16 05:20:50 +0100139 DBGP("vector [%u]: sqn = %" PRIu64 "\n",
140 i, aud3g->u.umts.sqn);
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100141
142 DBGVB(autn);
143 DBGVB(ck);
144 DBGVB(ik);
145 DBGVB(res);
146 DBGVV("%u", res_len);
147
148 if (!aud2g) {
149 /* use the 2G tokens from 3G keys */
Neels Hofmeyr84c2f432017-12-14 20:58:54 +0100150 DBGP("vector [%u]: deriving 2G from 3G\n", i);
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100151 DBGVB(kc);
152 DBGVB(sres);
153 DBGVV("0x%x", auth_types);
154 continue;
155 }
156 /* calculate 2G separately */
157
Neels Hofmeyr84c2f432017-12-14 20:58:54 +0100158 DBGP("vector [%u]: calculating 2G separately\n", i);
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100159
Harald Weltea854b482023-05-30 17:27:32 +0200160 rc = osmo_auth_gen_vec2(&vtmp, aud2g, rand);
Harald Weltee72cf552016-04-28 07:18:49 +0200161 if (rc < 0) {
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100162 LOGP(DAUC, LOGL_ERROR, "Error in 2G vector"
163 "generation: [%u]: rc = %d\n", i, rc);
Harald Weltee72cf552016-04-28 07:18:49 +0200164 goto out;
165 }
166 memcpy(&vec[i].kc, vtmp.kc, sizeof(vec[i].kc));
167 memcpy(&vec[i].sres, vtmp.sres, sizeof(vec[i].sres));
168 vec[i].auth_types |= OSMO_AUTH_TYPE_GSM;
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100169 } else {
170 /* 2G only case */
Harald Weltea854b482023-05-30 17:27:32 +0200171 rc = osmo_auth_gen_vec2(vec+i, aud2g, rand);
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100172 if (rc < 0) {
173 LOGP(DAUC, LOGL_ERROR, "Error in 2G vector "
174 "generation: [%u]: rc = %d\n", i, rc);
175 goto out;
176 }
Harald Weltee72cf552016-04-28 07:18:49 +0200177 }
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100178
179 DBGVB(kc);
180 DBGVB(sres);
181 DBGVV("0x%x", auth_types);
Harald Weltee72cf552016-04-28 07:18:49 +0200182 }
183out:
184 return i;
Neels Hofmeyr8d97d342017-02-21 22:46:35 +0100185#undef DBGVV
186#undef DBGVB
187#undef DBGP
Harald Weltee72cf552016-04-28 07:18:49 +0200188}