Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 1 | /* Authentication related functions */ |
| 2 | |
| 3 | /* |
| 4 | * (C) 2010 by Sylvain Munaut <tnt@246tNt.com> |
| 5 | * |
| 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 | #include <openbsc/db.h> |
| 25 | #include <openbsc/debug.h> |
Harald Welte | 86dda08 | 2010-12-23 18:07:49 +0100 | [diff] [blame] | 26 | #include <openbsc/auth.h> |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 27 | #include <openbsc/gsm_data.h> |
| 28 | |
| 29 | #include <osmocore/comp128.h> |
| 30 | |
| 31 | #include <stdlib.h> |
| 32 | |
| 33 | |
| 34 | static int |
Sylvain Munaut | e824d9c | 2010-06-11 00:19:42 +0200 | [diff] [blame] | 35 | _use_xor(struct gsm_auth_info *ainfo, struct gsm_auth_tuple *atuple) |
| 36 | { |
| 37 | int i, l = ainfo->a3a8_ki_len; |
| 38 | |
| 39 | if ((l > A38_XOR_MAX_KEY_LEN) || (l < A38_XOR_MIN_KEY_LEN)) { |
| 40 | DEBUGP(DMM, "Invalid XOR key (len=%d) %s", |
| 41 | ainfo->a3a8_ki_len, |
| 42 | hexdump(ainfo->a3a8_ki, ainfo->a3a8_ki_len)); |
| 43 | return -1; |
| 44 | } |
| 45 | |
| 46 | for (i=0; i<4; i++) |
| 47 | atuple->sres[i] = atuple->rand[i] ^ ainfo->a3a8_ki[i]; |
Sylvain Munaut | 0230b34 | 2010-09-21 19:03:09 +0200 | [diff] [blame] | 48 | for (i=4; i<12; i++) |
Sylvain Munaut | e824d9c | 2010-06-11 00:19:42 +0200 | [diff] [blame] | 49 | atuple->kc[i-4] = atuple->rand[i] ^ ainfo->a3a8_ki[i]; |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 55 | _use_comp128_v1(struct gsm_auth_info *ainfo, struct gsm_auth_tuple *atuple) |
| 56 | { |
| 57 | if (ainfo->a3a8_ki_len != A38_COMP128_KEY_LEN) { |
| 58 | DEBUGP(DMM, "Invalid COMP128v1 key (len=%d) %s", |
| 59 | ainfo->a3a8_ki_len, |
| 60 | hexdump(ainfo->a3a8_ki, ainfo->a3a8_ki_len)); |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | comp128(ainfo->a3a8_ki, atuple->rand, atuple->sres, atuple->kc); |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | /* Return values |
| 70 | * -1 -> Internal error |
| 71 | * 0 -> Not available |
| 72 | * 1 -> Tuple returned, need to do auth, then enable cipher |
| 73 | * 2 -> Tuple returned, need to enable cipher |
| 74 | */ |
| 75 | int auth_get_tuple_for_subscr(struct gsm_auth_tuple *atuple, |
| 76 | struct gsm_subscriber *subscr, int key_seq) |
| 77 | { |
| 78 | struct gsm_auth_info ainfo; |
| 79 | int i, rc; |
| 80 | |
| 81 | /* Get subscriber info (if any) */ |
| 82 | rc = db_get_authinfo_for_subscr(&ainfo, subscr); |
| 83 | if (rc < 0) { |
| 84 | DEBUGP(DMM, "No retrievable Ki for subscriber, skipping auth"); |
Harald Welte | 86dda08 | 2010-12-23 18:07:49 +0100 | [diff] [blame] | 85 | return rc == -ENOENT ? AUTH_NOT_AVAIL : -1; |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* If possible, re-use the last tuple and skip auth */ |
| 89 | rc = db_get_lastauthtuple_for_subscr(atuple, subscr); |
| 90 | if ((rc == 0) && |
Sylvain Munaut | cd8e810 | 2010-09-21 19:04:07 +0200 | [diff] [blame] | 91 | (key_seq != GSM_KEY_SEQ_INVAL) && |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 92 | (atuple->use_count < 3)) |
| 93 | { |
| 94 | atuple->use_count++; |
| 95 | db_sync_lastauthtuple_for_subscr(atuple, subscr); |
Harald Welte | 86dda08 | 2010-12-23 18:07:49 +0100 | [diff] [blame] | 96 | return AUTH_DO_CIPH; |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* Generate a new one */ |
| 100 | atuple->use_count = 1; |
| 101 | atuple->key_seq = (atuple->key_seq + 1) % 7; |
| 102 | for (i=0; i<sizeof(atuple->rand); i++) |
| 103 | atuple->rand[i] = random() & 0xff; |
| 104 | |
| 105 | switch (ainfo.auth_algo) { |
| 106 | case AUTH_ALGO_NONE: |
| 107 | return 0; |
| 108 | |
Sylvain Munaut | e824d9c | 2010-06-11 00:19:42 +0200 | [diff] [blame] | 109 | case AUTH_ALGO_XOR: |
| 110 | if (_use_xor(&ainfo, atuple)) |
| 111 | return 0; |
| 112 | break; |
| 113 | |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 114 | case AUTH_ALGO_COMP128v1: |
| 115 | if (_use_comp128_v1(&ainfo, atuple)) |
| 116 | return 0; |
| 117 | break; |
| 118 | |
| 119 | default: |
| 120 | DEBUGP(DMM, "Unsupported auth type algo_id=%d\n", |
| 121 | ainfo.auth_algo); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | db_sync_lastauthtuple_for_subscr(atuple, subscr); |
| 126 | |
Harald Welte | 86dda08 | 2010-12-23 18:07:49 +0100 | [diff] [blame] | 127 | return AUTH_DO_AUTH_THAN_CIPH; |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 128 | } |
| 129 | |