Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 1 | /* GSM/GPRS/3G authentication testing tool */ |
| 2 | |
Harald Welte | bc6f56c | 2012-03-21 17:37:53 +0100 | [diff] [blame] | 3 | /* (C) 2010-2012 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 4 | * |
| 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 Engelhardt | a6d8393 | 2013-06-03 01:38:57 +0200 | [diff] [blame] | 28 | #include <time.h> |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 29 | #include <getopt.h> |
Harald Welte | b53717f | 2012-08-02 08:42:59 +0200 | [diff] [blame] | 30 | #include <unistd.h> |
Holger Hans Peter Freyther | a652abc | 2013-07-14 09:11:47 +0200 | [diff] [blame] | 31 | #include <inttypes.h> |
| 32 | #include <time.h> |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 33 | |
| 34 | #include <osmocom/crypt/auth.h> |
| 35 | #include <osmocom/core/utils.h> |
| 36 | |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 37 | static 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 Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 49 | static void dump_auth_vec(struct osmo_auth_vector *vec) |
| 50 | { |
Harald Welte | 4f511b6 | 2016-05-18 19:36:42 +0200 | [diff] [blame] | 51 | printf("RAND:\t%s\n", osmo_hexdump_nospc(vec->rand, sizeof(vec->rand))); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 52 | |
| 53 | if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) { |
Harald Welte | 4f511b6 | 2016-05-18 19:36:42 +0200 | [diff] [blame] | 54 | 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 Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | if (vec->auth_types & OSMO_AUTH_TYPE_GSM) { |
Harald Welte | 4f511b6 | 2016-05-18 19:36:42 +0200 | [diff] [blame] | 61 | 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 Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | static struct osmo_sub_auth_data test_aud = { |
| 67 | .type = OSMO_AUTH_TYPE_NONE, |
| 68 | .algo = OSMO_AUTH_ALG_NONE, |
| 69 | }; |
| 70 | |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 71 | static void help() |
| 72 | { |
Neels Hofmeyr | 18d65be | 2017-02-03 18:36:32 +0100 | [diff] [blame] | 73 | int alg; |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 74 | 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 Welte | a72e47b | 2012-03-21 09:03:16 +0100 | [diff] [blame] | 79 | "-O --op\tSpecify OP (only for 3G)\n" |
Holger Hans Peter Freyther | 91ff17c | 2015-05-26 00:11:37 +0800 | [diff] [blame] | 80 | "-f --amf\tSpecify AMF (only for 3G)\n" |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 81 | "-s --sqn\tSpecify SQN (only for 3G)\n" |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 82 | "-A --auts\tSpecify AUTS (only for 3G)\n" |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 83 | "-r --rand\tSpecify random value\n" |
| 84 | "-I --ipsec\tOutput in triplets.dat format for strongswan\n"); |
Neels Hofmeyr | 18d65be | 2017-02-03 18:36:32 +0100 | [diff] [blame] | 85 | |
| 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 Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 92 | int main(int argc, char **argv) |
| 93 | { |
| 94 | struct osmo_auth_vector _vec; |
| 95 | struct osmo_auth_vector *vec = &_vec; |
Neels Hofmeyr | 8352d31 | 2017-02-02 20:05:14 +0100 | [diff] [blame] | 96 | uint8_t _rand[16], _auts[14]; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 97 | int rc, option_index; |
| 98 | int rand_is_set = 0; |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 99 | int auts_is_set = 0; |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 100 | int fmt_triplets_dat = 0; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 101 | |
Harald Welte | bc6f56c | 2012-03-21 17:37:53 +0100 | [diff] [blame] | 102 | printf("osmo-auc-gen (C) 2011-2012 by Harald Welte\n"); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 103 | printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n"); |
| 104 | |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 105 | memset(_auts, 0, sizeof(_auts)); |
| 106 | |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 107 | while (1) { |
| 108 | int c; |
| 109 | unsigned long ul; |
| 110 | static struct option long_options[] = { |
| 111 | { "2g", 0, 0, '2' }, |
| 112 | { "3g", 0, 0, '3' }, |
| 113 | { "algorithm", 1, 0, 'a' }, |
| 114 | { "key", 1, 0, 'k' }, |
| 115 | { "opc", 1, 0, 'o' }, |
Harald Welte | a72e47b | 2012-03-21 09:03:16 +0100 | [diff] [blame] | 116 | { "op", 1, 0, 'O' }, |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 117 | { "amf", 1, 0, 'f' }, |
| 118 | { "sqn", 1, 0, 's' }, |
| 119 | { "rand", 1, 0, 'r' }, |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 120 | { "auts", 1, 0, 'A' }, |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 121 | { "help", 0, 0, 'h' }, |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 122 | { 0, 0, 0, 0 } |
| 123 | }; |
| 124 | |
| 125 | rc = 0; |
| 126 | |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 127 | c = getopt_long(argc, argv, "23a:k:o:f:s:r:hO:A:I", long_options, |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 128 | &option_index); |
| 129 | |
| 130 | if (c == -1) |
| 131 | break; |
| 132 | |
| 133 | switch (c) { |
| 134 | case '2': |
| 135 | test_aud.type = OSMO_AUTH_TYPE_GSM; |
| 136 | break; |
| 137 | case '3': |
| 138 | test_aud.type = OSMO_AUTH_TYPE_UMTS; |
| 139 | break; |
| 140 | case 'a': |
| 141 | rc = osmo_auth_alg_parse(optarg); |
| 142 | if (rc < 0) |
| 143 | break; |
| 144 | test_aud.algo = rc; |
| 145 | break; |
| 146 | case 'k': |
| 147 | switch (test_aud.type) { |
| 148 | case OSMO_AUTH_TYPE_GSM: |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 149 | rc = osmo_hexparse(optarg, test_aud.u.gsm.ki, |
| 150 | sizeof(test_aud.u.gsm.ki)); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 151 | break; |
| 152 | case OSMO_AUTH_TYPE_UMTS: |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 153 | rc = osmo_hexparse(optarg, test_aud.u.umts.k, |
| 154 | sizeof(test_aud.u.umts.k)); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 155 | break; |
| 156 | default: |
| 157 | fprintf(stderr, "please specify 2g/3g first!\n"); |
| 158 | } |
| 159 | break; |
| 160 | case 'o': |
| 161 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 162 | fprintf(stderr, "Only UMTS has OPC\n"); |
| 163 | exit(2); |
| 164 | } |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 165 | rc = osmo_hexparse(optarg, test_aud.u.umts.opc, |
| 166 | sizeof(test_aud.u.umts.opc)); |
Harald Welte | a72e47b | 2012-03-21 09:03:16 +0100 | [diff] [blame] | 167 | test_aud.u.umts.opc_is_op = 0; |
| 168 | break; |
| 169 | case 'O': |
| 170 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 171 | fprintf(stderr, "Only UMTS has OP\n"); |
| 172 | exit(2); |
| 173 | } |
| 174 | rc = osmo_hexparse(optarg, test_aud.u.umts.opc, |
| 175 | sizeof(test_aud.u.umts.opc)); |
| 176 | test_aud.u.umts.opc_is_op = 1; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 177 | break; |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 178 | case 'A': |
| 179 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 180 | fprintf(stderr, "Only UMTS has AUTS\n"); |
| 181 | exit(2); |
| 182 | } |
| 183 | rc = osmo_hexparse(optarg, _auts, sizeof(_auts)); |
| 184 | auts_is_set = 1; |
| 185 | break; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 186 | case 'f': |
| 187 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 188 | fprintf(stderr, "Only UMTS has AMF\n"); |
| 189 | exit(2); |
| 190 | } |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 191 | rc = osmo_hexparse(optarg, test_aud.u.umts.amf, |
| 192 | sizeof(test_aud.u.umts.amf)); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 193 | break; |
| 194 | case 's': |
| 195 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 196 | fprintf(stderr, "Only UMTS has SQN\n"); |
| 197 | exit(2); |
| 198 | } |
| 199 | ul = strtoul(optarg, 0, 10); |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 200 | test_aud.u.umts.sqn = ul; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 201 | break; |
| 202 | case 'r': |
| 203 | rc = osmo_hexparse(optarg, _rand, sizeof(_rand)); |
| 204 | rand_is_set = 1; |
| 205 | break; |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 206 | case 'I': |
| 207 | fmt_triplets_dat = 1; |
| 208 | break; |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 209 | case 'h': |
| 210 | help(); |
| 211 | exit(0); |
| 212 | default: |
| 213 | help(); |
| 214 | exit(1); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | if (rc < 0) { |
Neels Hofmeyr | 18d65be | 2017-02-03 18:36:32 +0100 | [diff] [blame] | 218 | help(); |
| 219 | fprintf(stderr, "\nError parsing argument of option `%c'\n", c); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 220 | exit(2); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (!rand_is_set) { |
Holger Hans Peter Freyther | 17aa6b2 | 2014-06-22 16:53:55 +0200 | [diff] [blame] | 225 | int i; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 226 | printf("WARNING: We're using really weak random numbers!\n\n"); |
| 227 | srand(time(NULL)); |
Holger Hans Peter Freyther | 17aa6b2 | 2014-06-22 16:53:55 +0200 | [diff] [blame] | 228 | |
| 229 | for (i = 0; i < 4; ++i) { |
| 230 | uint32_t r; |
| 231 | r = rand(); |
| 232 | memcpy(&_rand[i*4], &r, 4); |
| 233 | } |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 236 | if (test_aud.type == OSMO_AUTH_TYPE_NONE || |
| 237 | test_aud.algo == OSMO_AUTH_ALG_NONE) { |
| 238 | help(); |
Neels Hofmeyr | 18d65be | 2017-02-03 18:36:32 +0100 | [diff] [blame] | 239 | fprintf(stderr, "\nError: you need to pass at least" |
| 240 | " -2 or -3, as well as an algorithm to use.\n"); |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 241 | exit(2); |
| 242 | } |
| 243 | |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 244 | memset(vec, 0, sizeof(*vec)); |
| 245 | |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 246 | if (!auts_is_set) |
| 247 | rc = osmo_auth_gen_vec(vec, &test_aud, _rand); |
| 248 | else |
| 249 | rc = osmo_auth_gen_vec_auts(vec, &test_aud, _auts, _rand, _rand); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 250 | if (rc < 0) { |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 251 | if (!auts_is_set) |
| 252 | fprintf(stderr, "error generating auth vector\n"); |
| 253 | else |
| 254 | fprintf(stderr, "AUTS from MS seems incorrect\n"); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 255 | exit(1); |
| 256 | } |
| 257 | |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 258 | if (fmt_triplets_dat) |
| 259 | dump_triplets_dat(vec); |
| 260 | else |
| 261 | dump_auth_vec(vec); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 262 | |
Neels Hofmeyr | 94e0460 | 2017-02-03 03:17:51 +0100 | [diff] [blame] | 263 | /* Print SQN from AUTS. It makes sense to print actually three SQN |
| 264 | * to clarify: |
| 265 | * After recovering SQN.MS from AUTS, milenage_gen_vec_auts() does: |
| 266 | * aud->u.umts.sqn = 1 + (osmo_load64be_ext(sqn_out, 6) >> 16); |
| 267 | * Then calls milenage_gen_vec(), which, after it is done, does: |
| 268 | * aud->u.umts.sqn++; |
| 269 | */ |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 270 | if (auts_is_set) |
Neels Hofmeyr | 94e0460 | 2017-02-03 03:17:51 +0100 | [diff] [blame] | 271 | printf("AUTS success: SQN.MS = %" PRIu64 |
| 272 | ", generated vector with SQN = %" PRIu64 |
| 273 | ", next SQN = %" PRIu64 "\n", |
| 274 | test_aud.u.umts.sqn - 2, |
| 275 | test_aud.u.umts.sqn - 1, |
| 276 | test_aud.u.umts.sqn |
| 277 | ); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 278 | |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 279 | exit(0); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 280 | } |