blob: 446d8ec573f975793cba27f12baba3175515f8ed [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file osmo-auc-gen.c
2 * GSM/GPRS/3G authentication testing tool. */
3/*
Harald Welte53b4bbb2021-09-16 21:36:16 +02004 * (C) 2010-2021 by Harald Welte <laforge@gnumonks.org>
Harald Welte915e0ef2011-12-07 02:38:42 +01005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <errno.h>
28#include <string.h>
Jan Engelhardta6d83932013-06-03 01:38:57 +020029#include <time.h>
Harald Welte915e0ef2011-12-07 02:38:42 +010030#include <getopt.h>
Harald Welteb53717f2012-08-02 08:42:59 +020031#include <unistd.h>
Holger Hans Peter Freythera652abc2013-07-14 09:11:47 +020032#include <inttypes.h>
33#include <time.h>
Harald Welte915e0ef2011-12-07 02:38:42 +010034
35#include <osmocom/crypt/auth.h>
36#include <osmocom/core/utils.h>
Harald Welte53b4bbb2021-09-16 21:36:16 +020037#include <osmocom/core/base64.h>
Max4b2b0cc2017-07-10 14:32:48 +020038#include <osmocom/gsm/gsm_utils.h>
Harald Welte915e0ef2011-12-07 02:38:42 +010039
Harald Welte53b4bbb2021-09-16 21:36:16 +020040static void print_base64(const char *fmt, const uint8_t *data, unsigned int len)
41{
42 uint8_t outbuf[256];
43 size_t olen;
44
45 OSMO_ASSERT(osmo_base64_encode(outbuf, sizeof(outbuf), &olen, data, len) == 0);
46 OSMO_ASSERT(sizeof(outbuf) > olen);
47 outbuf[olen] = '\0';
48 printf(fmt, outbuf);
49}
50
Harald Welte57799ed2012-06-27 15:06:19 +020051static void dump_triplets_dat(struct osmo_auth_vector *vec)
52{
53 if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) {
54 fprintf(stderr, "triplets.dat doesn't support UMTS!\n");
55 return;
56 }
57 printf("imsi,");
58 printf("%s,", osmo_hexdump_nospc(vec->rand, sizeof(vec->rand)));
59 printf("%s,", osmo_hexdump_nospc(vec->sres, sizeof(vec->sres)));
60 printf("%s\n", osmo_hexdump_nospc(vec->kc, sizeof(vec->kc)));
61}
62
Harald Welte915e0ef2011-12-07 02:38:42 +010063static void dump_auth_vec(struct osmo_auth_vector *vec)
64{
Harald Welte4f511b62016-05-18 19:36:42 +020065 printf("RAND:\t%s\n", osmo_hexdump_nospc(vec->rand, sizeof(vec->rand)));
Harald Welte915e0ef2011-12-07 02:38:42 +010066
67 if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) {
Harald Welte53b4bbb2021-09-16 21:36:16 +020068 uint8_t inbuf[sizeof(vec->rand) + sizeof(vec->autn)];
69
Harald Welte4f511b62016-05-18 19:36:42 +020070 printf("AUTN:\t%s\n", osmo_hexdump_nospc(vec->autn, sizeof(vec->autn)));
71 printf("IK:\t%s\n", osmo_hexdump_nospc(vec->ik, sizeof(vec->ik)));
72 printf("CK:\t%s\n", osmo_hexdump_nospc(vec->ck, sizeof(vec->ck)));
73 printf("RES:\t%s\n", osmo_hexdump_nospc(vec->res, vec->res_len));
Harald Welte53b4bbb2021-09-16 21:36:16 +020074
75 memcpy(inbuf, vec->rand, sizeof(vec->rand));
76 memcpy(inbuf + sizeof(vec->rand), vec->autn, sizeof(vec->autn));
77 print_base64("IMS nonce:\t%s\n", inbuf, sizeof(inbuf));
78 print_base64("IMS res:\t%s\n", vec->res, vec->res_len);
Harald Welte915e0ef2011-12-07 02:38:42 +010079 }
80
81 if (vec->auth_types & OSMO_AUTH_TYPE_GSM) {
Harald Welte4f511b62016-05-18 19:36:42 +020082 printf("SRES:\t%s\n", osmo_hexdump_nospc(vec->sres, sizeof(vec->sres)));
83 printf("Kc:\t%s\n", osmo_hexdump_nospc(vec->kc, sizeof(vec->kc)));
Harald Welte915e0ef2011-12-07 02:38:42 +010084 }
85}
86
87static struct osmo_sub_auth_data test_aud = {
88 .type = OSMO_AUTH_TYPE_NONE,
89 .algo = OSMO_AUTH_ALG_NONE,
90};
91
Harald Welte5fb795e2012-03-21 08:51:48 +010092static void help()
93{
Neels Hofmeyr18d65be2017-02-03 18:36:32 +010094 int alg;
Harald Welte5fb795e2012-03-21 08:51:48 +010095 printf( "-2 --2g\tUse 2G (GSM) authentication\n"
96 "-3 --3g\tUse 3G (UMTS) authentication\n"
97 "-a --algorithm\tSpecify name of the algorithm\n"
98 "-k --key\tSpecify Ki / K\n"
99 "-o --opc\tSpecify OPC (only for 3G)\n"
Harald Weltea72e47b2012-03-21 09:03:16 +0100100 "-O --op\tSpecify OP (only for 3G)\n"
Holger Hans Peter Freyther91ff17c2015-05-26 00:11:37 +0800101 "-f --amf\tSpecify AMF (only for 3G)\n"
Harald Welte5fb795e2012-03-21 08:51:48 +0100102 "-s --sqn\tSpecify SQN (only for 3G)\n"
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200103 "-i --ind\tSpecify IND slot for new SQN after AUTS (only for 3G)\n"
Neels Hofmeyrb1af6ef2017-08-26 21:38:51 +0200104 "-l --ind-len\tSpecify IND bit length (default=5) (only for 3G)\n"
Harald Weltecebf3f02012-03-22 16:45:23 +0100105 "-A --auts\tSpecify AUTS (only for 3G)\n"
Harald Welte57799ed2012-06-27 15:06:19 +0200106 "-r --rand\tSpecify random value\n"
107 "-I --ipsec\tOutput in triplets.dat format for strongswan\n");
Neels Hofmeyr18d65be2017-02-03 18:36:32 +0100108
109 fprintf(stderr, "\nAvailable algorithms for option -a:\n");
110 for (alg = 1; alg < _OSMO_AUTH_ALG_NUM; alg++)
111 fprintf(stderr, " %s\n",
112 osmo_auth_alg_name(alg));
Harald Welte5fb795e2012-03-21 08:51:48 +0100113}
114
Harald Welte915e0ef2011-12-07 02:38:42 +0100115int main(int argc, char **argv)
116{
117 struct osmo_auth_vector _vec;
118 struct osmo_auth_vector *vec = &_vec;
Neels Hofmeyr8352d312017-02-02 20:05:14 +0100119 uint8_t _rand[16], _auts[14];
Pau Espin Pedrolc30a76b2017-11-16 16:37:55 +0100120 uint64_t sqn = 0;
Neels Hofmeyrb2e41cc2017-09-12 03:25:43 +0200121 unsigned int ind = 0;
Harald Welte915e0ef2011-12-07 02:38:42 +0100122 int rc, option_index;
123 int rand_is_set = 0;
Harald Weltecebf3f02012-03-22 16:45:23 +0100124 int auts_is_set = 0;
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200125 int sqn_is_set = 0;
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200126 int ind_is_set = 0;
Harald Welte57799ed2012-06-27 15:06:19 +0200127 int fmt_triplets_dat = 0;
Neels Hofmeyrb2e41cc2017-09-12 03:25:43 +0200128 uint64_t ind_mask = 0;
Harald Welte915e0ef2011-12-07 02:38:42 +0100129
Harald Weltebc6f56c2012-03-21 17:37:53 +0100130 printf("osmo-auc-gen (C) 2011-2012 by Harald Welte\n");
Harald Welte915e0ef2011-12-07 02:38:42 +0100131 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
132
Harald Weltecebf3f02012-03-22 16:45:23 +0100133 memset(_auts, 0, sizeof(_auts));
134
Harald Welte915e0ef2011-12-07 02:38:42 +0100135 while (1) {
136 int c;
Harald Welte915e0ef2011-12-07 02:38:42 +0100137 static struct option long_options[] = {
138 { "2g", 0, 0, '2' },
139 { "3g", 0, 0, '3' },
140 { "algorithm", 1, 0, 'a' },
141 { "key", 1, 0, 'k' },
142 { "opc", 1, 0, 'o' },
Harald Weltea72e47b2012-03-21 09:03:16 +0100143 { "op", 1, 0, 'O' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100144 { "amf", 1, 0, 'f' },
145 { "sqn", 1, 0, 's' },
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200146 { "ind", 1, 0, 'i' },
Neels Hofmeyrb1af6ef2017-08-26 21:38:51 +0200147 { "ind-len", 1, 0, 'l' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100148 { "rand", 1, 0, 'r' },
Harald Weltecebf3f02012-03-22 16:45:23 +0100149 { "auts", 1, 0, 'A' },
Harald Welte5fb795e2012-03-21 08:51:48 +0100150 { "help", 0, 0, 'h' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100151 { 0, 0, 0, 0 }
152 };
153
154 rc = 0;
155
Neels Hofmeyrb1af6ef2017-08-26 21:38:51 +0200156 c = getopt_long(argc, argv, "23a:k:o:f:s:i:l:r:hO:A:I", long_options,
Harald Welte915e0ef2011-12-07 02:38:42 +0100157 &option_index);
158
159 if (c == -1)
160 break;
161
162 switch (c) {
163 case '2':
164 test_aud.type = OSMO_AUTH_TYPE_GSM;
165 break;
166 case '3':
167 test_aud.type = OSMO_AUTH_TYPE_UMTS;
Neels Hofmeyr4315e012017-08-26 21:40:11 +0200168 test_aud.u.umts.ind_bitlen = 5;
Harald Welte915e0ef2011-12-07 02:38:42 +0100169 break;
170 case 'a':
171 rc = osmo_auth_alg_parse(optarg);
172 if (rc < 0)
173 break;
174 test_aud.algo = rc;
175 break;
176 case 'k':
177 switch (test_aud.type) {
178 case OSMO_AUTH_TYPE_GSM:
Harald Welteaae23622011-12-07 11:35:02 +0100179 rc = osmo_hexparse(optarg, test_aud.u.gsm.ki,
180 sizeof(test_aud.u.gsm.ki));
Harald Welte915e0ef2011-12-07 02:38:42 +0100181 break;
182 case OSMO_AUTH_TYPE_UMTS:
Harald Welteaae23622011-12-07 11:35:02 +0100183 rc = osmo_hexparse(optarg, test_aud.u.umts.k,
184 sizeof(test_aud.u.umts.k));
Harald Welte915e0ef2011-12-07 02:38:42 +0100185 break;
186 default:
187 fprintf(stderr, "please specify 2g/3g first!\n");
188 }
189 break;
190 case 'o':
191 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
192 fprintf(stderr, "Only UMTS has OPC\n");
193 exit(2);
194 }
Harald Welteaae23622011-12-07 11:35:02 +0100195 rc = osmo_hexparse(optarg, test_aud.u.umts.opc,
196 sizeof(test_aud.u.umts.opc));
Harald Weltea72e47b2012-03-21 09:03:16 +0100197 test_aud.u.umts.opc_is_op = 0;
198 break;
199 case 'O':
200 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
201 fprintf(stderr, "Only UMTS has OP\n");
202 exit(2);
203 }
204 rc = osmo_hexparse(optarg, test_aud.u.umts.opc,
205 sizeof(test_aud.u.umts.opc));
206 test_aud.u.umts.opc_is_op = 1;
Harald Welte915e0ef2011-12-07 02:38:42 +0100207 break;
Harald Weltecebf3f02012-03-22 16:45:23 +0100208 case 'A':
209 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
210 fprintf(stderr, "Only UMTS has AUTS\n");
211 exit(2);
212 }
213 rc = osmo_hexparse(optarg, _auts, sizeof(_auts));
214 auts_is_set = 1;
215 break;
Harald Welte915e0ef2011-12-07 02:38:42 +0100216 case 'f':
217 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
218 fprintf(stderr, "Only UMTS has AMF\n");
219 exit(2);
220 }
Harald Welteaae23622011-12-07 11:35:02 +0100221 rc = osmo_hexparse(optarg, test_aud.u.umts.amf,
222 sizeof(test_aud.u.umts.amf));
Harald Welte915e0ef2011-12-07 02:38:42 +0100223 break;
224 case 's':
225 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
226 fprintf(stderr, "Only UMTS has SQN\n");
227 exit(2);
228 }
Harald Welteb48f4382021-09-17 07:56:09 +0200229 sqn = strtoull(optarg, 0, 0);
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200230 sqn_is_set = 1;
Harald Welte915e0ef2011-12-07 02:38:42 +0100231 break;
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200232 case 'i':
233 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
234 fprintf(stderr, "Only UMTS has IND\n");
235 exit(2);
236 }
237 ind = atoi(optarg);
238 ind_is_set = 1;
239 break;
Neels Hofmeyrb1af6ef2017-08-26 21:38:51 +0200240 case 'l':
241 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
242 fprintf(stderr, "Only UMTS has IND bitlen\n");
243 exit(2);
244 }
245 test_aud.u.umts.ind_bitlen = atoi(optarg);
246 break;
Harald Welte915e0ef2011-12-07 02:38:42 +0100247 case 'r':
248 rc = osmo_hexparse(optarg, _rand, sizeof(_rand));
249 rand_is_set = 1;
250 break;
Harald Welte57799ed2012-06-27 15:06:19 +0200251 case 'I':
252 fmt_triplets_dat = 1;
253 break;
Harald Welte5fb795e2012-03-21 08:51:48 +0100254 case 'h':
255 help();
256 exit(0);
257 default:
258 help();
259 exit(1);
Harald Welte915e0ef2011-12-07 02:38:42 +0100260 }
261
262 if (rc < 0) {
Neels Hofmeyr18d65be2017-02-03 18:36:32 +0100263 help();
264 fprintf(stderr, "\nError parsing argument of option `%c'\n", c);
Harald Welte915e0ef2011-12-07 02:38:42 +0100265 exit(2);
266 }
267 }
268
Harald Welte278a6c82019-12-03 21:35:12 +0100269 if (argc > optind) {
270 fprintf(stderr, "Unsupported positional arguments in command line\n");
271 exit(2);
272 }
273
Harald Welte915e0ef2011-12-07 02:38:42 +0100274 if (!rand_is_set) {
Max4b2b0cc2017-07-10 14:32:48 +0200275 rc = osmo_get_rand_id(_rand, 16);
276 if (rc < 0) {
277 fprintf(stderr, "\nError: unable to obtain secure random numbers: %s!\n",
278 strerror(-rc));
279 exit(3);
Holger Hans Peter Freyther17aa6b22014-06-22 16:53:55 +0200280 }
Harald Welte915e0ef2011-12-07 02:38:42 +0100281 }
282
Harald Welte5fb795e2012-03-21 08:51:48 +0100283 if (test_aud.type == OSMO_AUTH_TYPE_NONE ||
284 test_aud.algo == OSMO_AUTH_ALG_NONE) {
285 help();
Neels Hofmeyr18d65be2017-02-03 18:36:32 +0100286 fprintf(stderr, "\nError: you need to pass at least"
287 " -2 or -3, as well as an algorithm to use.\n");
Harald Welte5fb795e2012-03-21 08:51:48 +0100288 exit(2);
289 }
290
Harald Welte915e0ef2011-12-07 02:38:42 +0100291 memset(vec, 0, sizeof(*vec));
292
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200293 if (test_aud.type == OSMO_AUTH_TYPE_UMTS) {
Neels Hofmeyrb2e41cc2017-09-12 03:25:43 +0200294 uint64_t seq_1 = 1LL << test_aud.u.umts.ind_bitlen;
Neels Hofmeyre6e64462017-08-26 22:29:51 +0200295 ind_mask = seq_1 - 1;
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200296
297 if (sqn_is_set) {
298 /* Before calculating the UMTS auth vector, osmo_auth_gen_vec() increments SEQ.
299 * To end up with the SQN passed in by the user, we need to pass in SEQ-1, and
300 * indicate which IND slot to target. */
301 test_aud.u.umts.sqn = sqn - seq_1;
302 test_aud.u.umts.ind = sqn & ind_mask;
303 }
Neels Hofmeyr3cb08272017-08-26 21:45:33 +0200304
305 if (sqn_is_set && ind_is_set) {
306 fprintf(stderr, "Requesting --sqn %"PRIu64" implies IND=%u,"
307 " so no further --ind argument is allowed.\n",
308 sqn, test_aud.u.umts.ind);
309 exit(2);
310 }
311
312 if (ind_is_set) {
313 if (ind >= (1 << test_aud.u.umts.ind_bitlen)) {
314 fprintf(stderr, "Requested --ind %u is too large for IND bitlen of %u\n",
315 ind, test_aud.u.umts.ind_bitlen);
316 exit(2);
317 }
318 test_aud.u.umts.ind = ind;
319 }
Neels Hofmeyrd157bbb2017-08-26 22:08:36 +0200320 }
321
Harald Weltecebf3f02012-03-22 16:45:23 +0100322 if (!auts_is_set)
323 rc = osmo_auth_gen_vec(vec, &test_aud, _rand);
324 else
325 rc = osmo_auth_gen_vec_auts(vec, &test_aud, _auts, _rand, _rand);
Harald Welte915e0ef2011-12-07 02:38:42 +0100326 if (rc < 0) {
Harald Weltecebf3f02012-03-22 16:45:23 +0100327 if (!auts_is_set)
328 fprintf(stderr, "error generating auth vector\n");
329 else
330 fprintf(stderr, "AUTS from MS seems incorrect\n");
Harald Welte915e0ef2011-12-07 02:38:42 +0100331 exit(1);
332 }
333
Harald Welte57799ed2012-06-27 15:06:19 +0200334 if (fmt_triplets_dat)
335 dump_triplets_dat(vec);
Neels Hofmeyr5fe3d1b2017-03-15 01:16:43 +0100336 else {
Harald Welte57799ed2012-06-27 15:06:19 +0200337 dump_auth_vec(vec);
Neels Hofmeyre6e64462017-08-26 22:29:51 +0200338 if (test_aud.type == OSMO_AUTH_TYPE_UMTS) {
Neels Hofmeyr82c9a0e2017-03-13 17:36:17 +0100339 printf("SQN:\t%" PRIu64 "\n", test_aud.u.umts.sqn);
Neels Hofmeyre6e64462017-08-26 22:29:51 +0200340 printf("IND:\t%u\n", (unsigned int)(test_aud.u.umts.sqn & ind_mask));
Neels Hofmeyr2066a422017-08-26 22:43:50 +0200341 if (auts_is_set)
342 printf("SQN.MS:\t%" PRIu64 "\n", test_aud.u.umts.sqn_ms);
Neels Hofmeyre6e64462017-08-26 22:29:51 +0200343 }
Neels Hofmeyr5fe3d1b2017-03-15 01:16:43 +0100344 }
Harald Welte915e0ef2011-12-07 02:38:42 +0100345
Harald Weltecebf3f02012-03-22 16:45:23 +0100346 exit(0);
Harald Welte915e0ef2011-12-07 02:38:42 +0100347}