blob: 6b1e6239df5e2138e573f931637a71c6c479d349 [file] [log] [blame]
Harald Welte915e0ef2011-12-07 02:38:42 +01001/* GSM/GPRS/3G authentication testing tool */
2
Harald Weltebc6f56c2012-03-21 17:37:53 +01003/* (C) 2010-2012 by Harald Welte <laforge@gnumonks.org>
Harald Welte915e0ef2011-12-07 02:38:42 +01004 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23
24#include <stdlib.h>
25#include <stdio.h>
26#include <errno.h>
27#include <string.h>
Jan Engelhardta6d83932013-06-03 01:38:57 +020028#include <time.h>
Harald Welte915e0ef2011-12-07 02:38:42 +010029#include <getopt.h>
Harald Welteb53717f2012-08-02 08:42:59 +020030#include <unistd.h>
Holger Hans Peter Freythera652abc2013-07-14 09:11:47 +020031#include <inttypes.h>
32#include <time.h>
Harald Welte915e0ef2011-12-07 02:38:42 +010033
34#include <osmocom/crypt/auth.h>
35#include <osmocom/core/utils.h>
36
Harald Welte57799ed2012-06-27 15:06:19 +020037static void dump_triplets_dat(struct osmo_auth_vector *vec)
38{
39 if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) {
40 fprintf(stderr, "triplets.dat doesn't support UMTS!\n");
41 return;
42 }
43 printf("imsi,");
44 printf("%s,", osmo_hexdump_nospc(vec->rand, sizeof(vec->rand)));
45 printf("%s,", osmo_hexdump_nospc(vec->sres, sizeof(vec->sres)));
46 printf("%s\n", osmo_hexdump_nospc(vec->kc, sizeof(vec->kc)));
47}
48
Harald Welte915e0ef2011-12-07 02:38:42 +010049static void dump_auth_vec(struct osmo_auth_vector *vec)
50{
Harald Welte4f511b62016-05-18 19:36:42 +020051 printf("RAND:\t%s\n", osmo_hexdump_nospc(vec->rand, sizeof(vec->rand)));
Harald Welte915e0ef2011-12-07 02:38:42 +010052
53 if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) {
Harald Welte4f511b62016-05-18 19:36:42 +020054 printf("AUTN:\t%s\n", osmo_hexdump_nospc(vec->autn, sizeof(vec->autn)));
55 printf("IK:\t%s\n", osmo_hexdump_nospc(vec->ik, sizeof(vec->ik)));
56 printf("CK:\t%s\n", osmo_hexdump_nospc(vec->ck, sizeof(vec->ck)));
57 printf("RES:\t%s\n", osmo_hexdump_nospc(vec->res, vec->res_len));
Harald Welte915e0ef2011-12-07 02:38:42 +010058 }
59
60 if (vec->auth_types & OSMO_AUTH_TYPE_GSM) {
Harald Welte4f511b62016-05-18 19:36:42 +020061 printf("SRES:\t%s\n", osmo_hexdump_nospc(vec->sres, sizeof(vec->sres)));
62 printf("Kc:\t%s\n", osmo_hexdump_nospc(vec->kc, sizeof(vec->kc)));
Harald Welte915e0ef2011-12-07 02:38:42 +010063 }
64}
65
66static struct osmo_sub_auth_data test_aud = {
67 .type = OSMO_AUTH_TYPE_NONE,
68 .algo = OSMO_AUTH_ALG_NONE,
69};
70
Harald Welte5fb795e2012-03-21 08:51:48 +010071static void help()
72{
Neels Hofmeyr18d65be2017-02-03 18:36:32 +010073 int alg;
Harald Welte5fb795e2012-03-21 08:51:48 +010074 printf( "-2 --2g\tUse 2G (GSM) authentication\n"
75 "-3 --3g\tUse 3G (UMTS) authentication\n"
76 "-a --algorithm\tSpecify name of the algorithm\n"
77 "-k --key\tSpecify Ki / K\n"
78 "-o --opc\tSpecify OPC (only for 3G)\n"
Harald Weltea72e47b2012-03-21 09:03:16 +010079 "-O --op\tSpecify OP (only for 3G)\n"
Holger Hans Peter Freyther91ff17c2015-05-26 00:11:37 +080080 "-f --amf\tSpecify AMF (only for 3G)\n"
Harald Welte5fb795e2012-03-21 08:51:48 +010081 "-s --sqn\tSpecify SQN (only for 3G)\n"
Harald Weltecebf3f02012-03-22 16:45:23 +010082 "-A --auts\tSpecify AUTS (only for 3G)\n"
Harald Welte57799ed2012-06-27 15:06:19 +020083 "-r --rand\tSpecify random value\n"
84 "-I --ipsec\tOutput in triplets.dat format for strongswan\n");
Neels Hofmeyr18d65be2017-02-03 18:36:32 +010085
86 fprintf(stderr, "\nAvailable algorithms for option -a:\n");
87 for (alg = 1; alg < _OSMO_AUTH_ALG_NUM; alg++)
88 fprintf(stderr, " %s\n",
89 osmo_auth_alg_name(alg));
Harald Welte5fb795e2012-03-21 08:51:48 +010090}
91
Harald Welte915e0ef2011-12-07 02:38:42 +010092int main(int argc, char **argv)
93{
94 struct osmo_auth_vector _vec;
95 struct osmo_auth_vector *vec = &_vec;
Neels Hofmeyr8352d312017-02-02 20:05:14 +010096 uint8_t _rand[16], _auts[14];
Harald Welte915e0ef2011-12-07 02:38:42 +010097 int rc, option_index;
98 int rand_is_set = 0;
Harald Weltecebf3f02012-03-22 16:45:23 +010099 int auts_is_set = 0;
Harald Welte57799ed2012-06-27 15:06:19 +0200100 int fmt_triplets_dat = 0;
Harald Welte915e0ef2011-12-07 02:38:42 +0100101
Harald Weltebc6f56c2012-03-21 17:37:53 +0100102 printf("osmo-auc-gen (C) 2011-2012 by Harald Welte\n");
Harald Welte915e0ef2011-12-07 02:38:42 +0100103 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
104
Harald Weltecebf3f02012-03-22 16:45:23 +0100105 memset(_auts, 0, sizeof(_auts));
106
Harald Welte915e0ef2011-12-07 02:38:42 +0100107 while (1) {
108 int c;
Harald Welte915e0ef2011-12-07 02:38:42 +0100109 static struct option long_options[] = {
110 { "2g", 0, 0, '2' },
111 { "3g", 0, 0, '3' },
112 { "algorithm", 1, 0, 'a' },
113 { "key", 1, 0, 'k' },
114 { "opc", 1, 0, 'o' },
Harald Weltea72e47b2012-03-21 09:03:16 +0100115 { "op", 1, 0, 'O' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100116 { "amf", 1, 0, 'f' },
117 { "sqn", 1, 0, 's' },
118 { "rand", 1, 0, 'r' },
Harald Weltecebf3f02012-03-22 16:45:23 +0100119 { "auts", 1, 0, 'A' },
Harald Welte5fb795e2012-03-21 08:51:48 +0100120 { "help", 0, 0, 'h' },
Harald Welte915e0ef2011-12-07 02:38:42 +0100121 { 0, 0, 0, 0 }
122 };
123
124 rc = 0;
125
Harald Welte57799ed2012-06-27 15:06:19 +0200126 c = getopt_long(argc, argv, "23a:k:o:f:s:r:hO:A:I", long_options,
Harald Welte915e0ef2011-12-07 02:38:42 +0100127 &option_index);
128
129 if (c == -1)
130 break;
131
132 switch (c) {
133 case '2':
134 test_aud.type = OSMO_AUTH_TYPE_GSM;
135 break;
136 case '3':
137 test_aud.type = OSMO_AUTH_TYPE_UMTS;
138 break;
139 case 'a':
140 rc = osmo_auth_alg_parse(optarg);
141 if (rc < 0)
142 break;
143 test_aud.algo = rc;
144 break;
145 case 'k':
146 switch (test_aud.type) {
147 case OSMO_AUTH_TYPE_GSM:
Harald Welteaae23622011-12-07 11:35:02 +0100148 rc = osmo_hexparse(optarg, test_aud.u.gsm.ki,
149 sizeof(test_aud.u.gsm.ki));
Harald Welte915e0ef2011-12-07 02:38:42 +0100150 break;
151 case OSMO_AUTH_TYPE_UMTS:
Harald Welteaae23622011-12-07 11:35:02 +0100152 rc = osmo_hexparse(optarg, test_aud.u.umts.k,
153 sizeof(test_aud.u.umts.k));
Harald Welte915e0ef2011-12-07 02:38:42 +0100154 break;
155 default:
156 fprintf(stderr, "please specify 2g/3g first!\n");
157 }
158 break;
159 case 'o':
160 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
161 fprintf(stderr, "Only UMTS has OPC\n");
162 exit(2);
163 }
Harald Welteaae23622011-12-07 11:35:02 +0100164 rc = osmo_hexparse(optarg, test_aud.u.umts.opc,
165 sizeof(test_aud.u.umts.opc));
Harald Weltea72e47b2012-03-21 09:03:16 +0100166 test_aud.u.umts.opc_is_op = 0;
167 break;
168 case 'O':
169 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
170 fprintf(stderr, "Only UMTS has OP\n");
171 exit(2);
172 }
173 rc = osmo_hexparse(optarg, test_aud.u.umts.opc,
174 sizeof(test_aud.u.umts.opc));
175 test_aud.u.umts.opc_is_op = 1;
Harald Welte915e0ef2011-12-07 02:38:42 +0100176 break;
Harald Weltecebf3f02012-03-22 16:45:23 +0100177 case 'A':
178 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
179 fprintf(stderr, "Only UMTS has AUTS\n");
180 exit(2);
181 }
182 rc = osmo_hexparse(optarg, _auts, sizeof(_auts));
183 auts_is_set = 1;
184 break;
Harald Welte915e0ef2011-12-07 02:38:42 +0100185 case 'f':
186 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
187 fprintf(stderr, "Only UMTS has AMF\n");
188 exit(2);
189 }
Harald Welteaae23622011-12-07 11:35:02 +0100190 rc = osmo_hexparse(optarg, test_aud.u.umts.amf,
191 sizeof(test_aud.u.umts.amf));
Harald Welte915e0ef2011-12-07 02:38:42 +0100192 break;
193 case 's':
194 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
195 fprintf(stderr, "Only UMTS has SQN\n");
196 exit(2);
197 }
Neels Hofmeyr14477992017-03-16 00:50:06 +0100198 test_aud.u.umts.sqn = strtoull(optarg, 0, 10);
Neels Hofmeyr82c9a0e2017-03-13 17:36:17 +0100199 /* Before calculating the UMTS auth vector,
200 * osmo_auth_gen_vec() increments the SQN. SQN-1 here
201 * to end up with the SQN the user requested. */
202 test_aud.u.umts.sqn--;
Harald Welte915e0ef2011-12-07 02:38:42 +0100203 break;
204 case 'r':
205 rc = osmo_hexparse(optarg, _rand, sizeof(_rand));
206 rand_is_set = 1;
207 break;
Harald Welte57799ed2012-06-27 15:06:19 +0200208 case 'I':
209 fmt_triplets_dat = 1;
210 break;
Harald Welte5fb795e2012-03-21 08:51:48 +0100211 case 'h':
212 help();
213 exit(0);
214 default:
215 help();
216 exit(1);
Harald Welte915e0ef2011-12-07 02:38:42 +0100217 }
218
219 if (rc < 0) {
Neels Hofmeyr18d65be2017-02-03 18:36:32 +0100220 help();
221 fprintf(stderr, "\nError parsing argument of option `%c'\n", c);
Harald Welte915e0ef2011-12-07 02:38:42 +0100222 exit(2);
223 }
224 }
225
226 if (!rand_is_set) {
Holger Hans Peter Freyther17aa6b22014-06-22 16:53:55 +0200227 int i;
Harald Welte915e0ef2011-12-07 02:38:42 +0100228 printf("WARNING: We're using really weak random numbers!\n\n");
229 srand(time(NULL));
Holger Hans Peter Freyther17aa6b22014-06-22 16:53:55 +0200230
231 for (i = 0; i < 4; ++i) {
232 uint32_t r;
233 r = rand();
234 memcpy(&_rand[i*4], &r, 4);
235 }
Harald Welte915e0ef2011-12-07 02:38:42 +0100236 }
237
Harald Welte5fb795e2012-03-21 08:51:48 +0100238 if (test_aud.type == OSMO_AUTH_TYPE_NONE ||
239 test_aud.algo == OSMO_AUTH_ALG_NONE) {
240 help();
Neels Hofmeyr18d65be2017-02-03 18:36:32 +0100241 fprintf(stderr, "\nError: you need to pass at least"
242 " -2 or -3, as well as an algorithm to use.\n");
Harald Welte5fb795e2012-03-21 08:51:48 +0100243 exit(2);
244 }
245
Harald Welte915e0ef2011-12-07 02:38:42 +0100246 memset(vec, 0, sizeof(*vec));
247
Harald Weltecebf3f02012-03-22 16:45:23 +0100248 if (!auts_is_set)
249 rc = osmo_auth_gen_vec(vec, &test_aud, _rand);
250 else
251 rc = osmo_auth_gen_vec_auts(vec, &test_aud, _auts, _rand, _rand);
Harald Welte915e0ef2011-12-07 02:38:42 +0100252 if (rc < 0) {
Harald Weltecebf3f02012-03-22 16:45:23 +0100253 if (!auts_is_set)
254 fprintf(stderr, "error generating auth vector\n");
255 else
256 fprintf(stderr, "AUTS from MS seems incorrect\n");
Harald Welte915e0ef2011-12-07 02:38:42 +0100257 exit(1);
258 }
259
Harald Welte57799ed2012-06-27 15:06:19 +0200260 if (fmt_triplets_dat)
261 dump_triplets_dat(vec);
Neels Hofmeyr5fe3d1b2017-03-15 01:16:43 +0100262 else {
Harald Welte57799ed2012-06-27 15:06:19 +0200263 dump_auth_vec(vec);
Neels Hofmeyr5fe3d1b2017-03-15 01:16:43 +0100264 if (test_aud.type == OSMO_AUTH_TYPE_UMTS)
Neels Hofmeyr82c9a0e2017-03-13 17:36:17 +0100265 printf("SQN:\t%" PRIu64 "\n", test_aud.u.umts.sqn);
Neels Hofmeyr5fe3d1b2017-03-15 01:16:43 +0100266 }
Harald Welte915e0ef2011-12-07 02:38:42 +0100267
Neels Hofmeyr5fe3d1b2017-03-15 01:16:43 +0100268 /* After recovering SQN.MS from AUTS, milenage_gen_vec_auts() does
Neels Hofmeyr82c9a0e2017-03-13 17:36:17 +0100269 * aud->u.umts.sqn++, so to show SQN.MS we need to -1 */
Harald Weltecebf3f02012-03-22 16:45:23 +0100270 if (auts_is_set)
Neels Hofmeyr5fe3d1b2017-03-15 01:16:43 +0100271 printf("AUTS success: SQN.MS = %" PRIu64 "\n",
Neels Hofmeyr82c9a0e2017-03-13 17:36:17 +0100272 test_aud.u.umts.sqn - 1);
Harald Welte915e0ef2011-12-07 02:38:42 +0100273
Harald Weltecebf3f02012-03-22 16:45:23 +0100274 exit(0);
Harald Welte915e0ef2011-12-07 02:38:42 +0100275}