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> |
| 28 | #include <getopt.h> |
Harald Welte | b53717f | 2012-08-02 08:42:59 +0200 | [diff] [blame] | 29 | #include <unistd.h> |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 30 | |
| 31 | #include <osmocom/crypt/auth.h> |
| 32 | #include <osmocom/core/utils.h> |
| 33 | |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 34 | static void dump_triplets_dat(struct osmo_auth_vector *vec) |
| 35 | { |
| 36 | if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) { |
| 37 | fprintf(stderr, "triplets.dat doesn't support UMTS!\n"); |
| 38 | return; |
| 39 | } |
| 40 | printf("imsi,"); |
| 41 | printf("%s,", osmo_hexdump_nospc(vec->rand, sizeof(vec->rand))); |
| 42 | printf("%s,", osmo_hexdump_nospc(vec->sres, sizeof(vec->sres))); |
| 43 | printf("%s\n", osmo_hexdump_nospc(vec->kc, sizeof(vec->kc))); |
| 44 | } |
| 45 | |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 46 | static void dump_auth_vec(struct osmo_auth_vector *vec) |
| 47 | { |
| 48 | printf("RAND:\t%s\n", osmo_hexdump(vec->rand, sizeof(vec->rand))); |
| 49 | |
| 50 | if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) { |
| 51 | printf("AUTN:\t%s\n", osmo_hexdump(vec->autn, sizeof(vec->autn))); |
| 52 | printf("IK:\t%s\n", osmo_hexdump(vec->ik, sizeof(vec->ik))); |
| 53 | printf("CK:\t%s\n", osmo_hexdump(vec->ck, sizeof(vec->ck))); |
| 54 | printf("RES:\t%s\n", osmo_hexdump(vec->res, vec->res_len)); |
| 55 | } |
| 56 | |
| 57 | if (vec->auth_types & OSMO_AUTH_TYPE_GSM) { |
| 58 | printf("SRES:\t%s\n", osmo_hexdump(vec->sres, sizeof(vec->sres))); |
| 59 | printf("Kc:\t%s\n", osmo_hexdump(vec->kc, sizeof(vec->kc))); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | static struct osmo_sub_auth_data test_aud = { |
| 64 | .type = OSMO_AUTH_TYPE_NONE, |
| 65 | .algo = OSMO_AUTH_ALG_NONE, |
| 66 | }; |
| 67 | |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 68 | static void help() |
| 69 | { |
| 70 | printf( "-2 --2g\tUse 2G (GSM) authentication\n" |
| 71 | "-3 --3g\tUse 3G (UMTS) authentication\n" |
| 72 | "-a --algorithm\tSpecify name of the algorithm\n" |
| 73 | "-k --key\tSpecify Ki / K\n" |
| 74 | "-o --opc\tSpecify OPC (only for 3G)\n" |
Harald Welte | a72e47b | 2012-03-21 09:03:16 +0100 | [diff] [blame] | 75 | "-O --op\tSpecify OP (only for 3G)\n" |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 76 | "-a --amf\tSpecify AMF (only for 3G)\n" |
| 77 | "-s --sqn\tSpecify SQN (only for 3G)\n" |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 78 | "-A --auts\tSpecify AUTS (only for 3G)\n" |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 79 | "-r --rand\tSpecify random value\n" |
| 80 | "-I --ipsec\tOutput in triplets.dat format for strongswan\n"); |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 83 | int main(int argc, char **argv) |
| 84 | { |
| 85 | struct osmo_auth_vector _vec; |
| 86 | struct osmo_auth_vector *vec = &_vec; |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 87 | uint8_t _rand[16], _auts[16]; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 88 | int rc, option_index; |
| 89 | int rand_is_set = 0; |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 90 | int auts_is_set = 0; |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 91 | int fmt_triplets_dat = 0; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 92 | |
Harald Welte | bc6f56c | 2012-03-21 17:37:53 +0100 | [diff] [blame] | 93 | printf("osmo-auc-gen (C) 2011-2012 by Harald Welte\n"); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 94 | printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n"); |
| 95 | |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 96 | memset(_auts, 0, sizeof(_auts)); |
| 97 | |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 98 | while (1) { |
| 99 | int c; |
| 100 | unsigned long ul; |
| 101 | static struct option long_options[] = { |
| 102 | { "2g", 0, 0, '2' }, |
| 103 | { "3g", 0, 0, '3' }, |
| 104 | { "algorithm", 1, 0, 'a' }, |
| 105 | { "key", 1, 0, 'k' }, |
| 106 | { "opc", 1, 0, 'o' }, |
Harald Welte | a72e47b | 2012-03-21 09:03:16 +0100 | [diff] [blame] | 107 | { "op", 1, 0, 'O' }, |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 108 | { "amf", 1, 0, 'f' }, |
| 109 | { "sqn", 1, 0, 's' }, |
| 110 | { "rand", 1, 0, 'r' }, |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 111 | { "auts", 1, 0, 'A' }, |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 112 | { "help", 0, 0, 'h' }, |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 113 | { 0, 0, 0, 0 } |
| 114 | }; |
| 115 | |
| 116 | rc = 0; |
| 117 | |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 118 | 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] | 119 | &option_index); |
| 120 | |
| 121 | if (c == -1) |
| 122 | break; |
| 123 | |
| 124 | switch (c) { |
| 125 | case '2': |
| 126 | test_aud.type = OSMO_AUTH_TYPE_GSM; |
| 127 | break; |
| 128 | case '3': |
| 129 | test_aud.type = OSMO_AUTH_TYPE_UMTS; |
| 130 | break; |
| 131 | case 'a': |
| 132 | rc = osmo_auth_alg_parse(optarg); |
| 133 | if (rc < 0) |
| 134 | break; |
| 135 | test_aud.algo = rc; |
| 136 | break; |
| 137 | case 'k': |
| 138 | switch (test_aud.type) { |
| 139 | case OSMO_AUTH_TYPE_GSM: |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 140 | rc = osmo_hexparse(optarg, test_aud.u.gsm.ki, |
| 141 | sizeof(test_aud.u.gsm.ki)); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 142 | break; |
| 143 | case OSMO_AUTH_TYPE_UMTS: |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 144 | rc = osmo_hexparse(optarg, test_aud.u.umts.k, |
| 145 | sizeof(test_aud.u.umts.k)); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 146 | break; |
| 147 | default: |
| 148 | fprintf(stderr, "please specify 2g/3g first!\n"); |
| 149 | } |
| 150 | break; |
| 151 | case 'o': |
| 152 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 153 | fprintf(stderr, "Only UMTS has OPC\n"); |
| 154 | exit(2); |
| 155 | } |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 156 | rc = osmo_hexparse(optarg, test_aud.u.umts.opc, |
| 157 | sizeof(test_aud.u.umts.opc)); |
Harald Welte | a72e47b | 2012-03-21 09:03:16 +0100 | [diff] [blame] | 158 | test_aud.u.umts.opc_is_op = 0; |
| 159 | break; |
| 160 | case 'O': |
| 161 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 162 | fprintf(stderr, "Only UMTS has OP\n"); |
| 163 | exit(2); |
| 164 | } |
| 165 | rc = osmo_hexparse(optarg, test_aud.u.umts.opc, |
| 166 | sizeof(test_aud.u.umts.opc)); |
| 167 | test_aud.u.umts.opc_is_op = 1; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 168 | break; |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 169 | case 'A': |
| 170 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 171 | fprintf(stderr, "Only UMTS has AUTS\n"); |
| 172 | exit(2); |
| 173 | } |
| 174 | rc = osmo_hexparse(optarg, _auts, sizeof(_auts)); |
| 175 | auts_is_set = 1; |
| 176 | break; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 177 | case 'f': |
| 178 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 179 | fprintf(stderr, "Only UMTS has AMF\n"); |
| 180 | exit(2); |
| 181 | } |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 182 | rc = osmo_hexparse(optarg, test_aud.u.umts.amf, |
| 183 | sizeof(test_aud.u.umts.amf)); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 184 | break; |
| 185 | case 's': |
| 186 | if (test_aud.type != OSMO_AUTH_TYPE_UMTS) { |
| 187 | fprintf(stderr, "Only UMTS has SQN\n"); |
| 188 | exit(2); |
| 189 | } |
| 190 | ul = strtoul(optarg, 0, 10); |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 191 | test_aud.u.umts.sqn = ul; |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 192 | break; |
| 193 | case 'r': |
| 194 | rc = osmo_hexparse(optarg, _rand, sizeof(_rand)); |
| 195 | rand_is_set = 1; |
| 196 | break; |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 197 | case 'I': |
| 198 | fmt_triplets_dat = 1; |
| 199 | break; |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 200 | case 'h': |
| 201 | help(); |
| 202 | exit(0); |
| 203 | default: |
| 204 | help(); |
| 205 | exit(1); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | if (rc < 0) { |
| 209 | fprintf(stderr, "Error parsing argument of option `%c'\n", c); |
| 210 | exit(2); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if (!rand_is_set) { |
| 215 | printf("WARNING: We're using really weak random numbers!\n\n"); |
| 216 | srand(time(NULL)); |
| 217 | *(uint32_t *)&_rand[0] = rand(); |
| 218 | *(uint32_t *)(&_rand[4]) = rand(); |
| 219 | *(uint32_t *)(&_rand[8]) = rand(); |
| 220 | *(uint32_t *)(&_rand[12]) = rand(); |
| 221 | } |
| 222 | |
Harald Welte | 5fb795e | 2012-03-21 08:51:48 +0100 | [diff] [blame] | 223 | if (test_aud.type == OSMO_AUTH_TYPE_NONE || |
| 224 | test_aud.algo == OSMO_AUTH_ALG_NONE) { |
| 225 | help(); |
| 226 | exit(2); |
| 227 | } |
| 228 | |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 229 | memset(vec, 0, sizeof(*vec)); |
| 230 | |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 231 | if (!auts_is_set) |
| 232 | rc = osmo_auth_gen_vec(vec, &test_aud, _rand); |
| 233 | else |
| 234 | rc = osmo_auth_gen_vec_auts(vec, &test_aud, _auts, _rand, _rand); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 235 | if (rc < 0) { |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 236 | if (!auts_is_set) |
| 237 | fprintf(stderr, "error generating auth vector\n"); |
| 238 | else |
| 239 | fprintf(stderr, "AUTS from MS seems incorrect\n"); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 240 | exit(1); |
| 241 | } |
| 242 | |
Harald Welte | 57799ed | 2012-06-27 15:06:19 +0200 | [diff] [blame] | 243 | if (fmt_triplets_dat) |
| 244 | dump_triplets_dat(vec); |
| 245 | else |
| 246 | dump_auth_vec(vec); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 247 | |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 248 | if (auts_is_set) |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 249 | printf("AUTS success: SEQ.MS = %lu\n", test_aud.u.umts.sqn); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 250 | |
Harald Welte | cebf3f0 | 2012-03-22 16:45:23 +0100 | [diff] [blame] | 251 | exit(0); |
Harald Welte | 915e0ef | 2011-12-07 02:38:42 +0100 | [diff] [blame] | 252 | } |