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 |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 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 |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 17 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <openbsc/db.h> |
| 24 | #include <openbsc/debug.h> |
Harald Welte | 86dda08 | 2010-12-23 18:07:49 +0100 | [diff] [blame] | 25 | #include <openbsc/auth.h> |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 26 | #include <openbsc/gsm_data.h> |
| 27 | |
| 28 | #include <osmocore/comp128.h> |
| 29 | |
| 30 | #include <stdlib.h> |
| 31 | |
| 32 | |
| 33 | static int |
Sylvain Munaut | e824d9c | 2010-06-11 00:19:42 +0200 | [diff] [blame] | 34 | _use_xor(struct gsm_auth_info *ainfo, struct gsm_auth_tuple *atuple) |
| 35 | { |
| 36 | int i, l = ainfo->a3a8_ki_len; |
| 37 | |
| 38 | if ((l > A38_XOR_MAX_KEY_LEN) || (l < A38_XOR_MIN_KEY_LEN)) { |
Harald Welte | 082b01f | 2010-12-23 22:54:06 +0100 | [diff] [blame] | 39 | LOGP(DMM, LOGL_ERROR, "Invalid XOR key (len=%d) %s\n", |
Sylvain Munaut | e824d9c | 2010-06-11 00:19:42 +0200 | [diff] [blame] | 40 | ainfo->a3a8_ki_len, |
| 41 | hexdump(ainfo->a3a8_ki, ainfo->a3a8_ki_len)); |
| 42 | return -1; |
| 43 | } |
| 44 | |
| 45 | for (i=0; i<4; i++) |
| 46 | atuple->sres[i] = atuple->rand[i] ^ ainfo->a3a8_ki[i]; |
Sylvain Munaut | 0230b34 | 2010-09-21 19:03:09 +0200 | [diff] [blame] | 47 | for (i=4; i<12; i++) |
Sylvain Munaut | e824d9c | 2010-06-11 00:19:42 +0200 | [diff] [blame] | 48 | atuple->kc[i-4] = atuple->rand[i] ^ ainfo->a3a8_ki[i]; |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static int |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 54 | _use_comp128_v1(struct gsm_auth_info *ainfo, struct gsm_auth_tuple *atuple) |
| 55 | { |
| 56 | if (ainfo->a3a8_ki_len != A38_COMP128_KEY_LEN) { |
Harald Welte | 082b01f | 2010-12-23 22:54:06 +0100 | [diff] [blame] | 57 | LOGP(DMM, LOGL_ERROR, "Invalid COMP128v1 key (len=%d) %s\n", |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 58 | ainfo->a3a8_ki_len, |
| 59 | hexdump(ainfo->a3a8_ki, ainfo->a3a8_ki_len)); |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | comp128(ainfo->a3a8_ki, atuple->rand, atuple->sres, atuple->kc); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | /* Return values |
| 69 | * -1 -> Internal error |
| 70 | * 0 -> Not available |
| 71 | * 1 -> Tuple returned, need to do auth, then enable cipher |
| 72 | * 2 -> Tuple returned, need to enable cipher |
| 73 | */ |
| 74 | int auth_get_tuple_for_subscr(struct gsm_auth_tuple *atuple, |
| 75 | struct gsm_subscriber *subscr, int key_seq) |
| 76 | { |
| 77 | struct gsm_auth_info ainfo; |
| 78 | int i, rc; |
| 79 | |
| 80 | /* Get subscriber info (if any) */ |
| 81 | rc = db_get_authinfo_for_subscr(&ainfo, subscr); |
| 82 | if (rc < 0) { |
Harald Welte | 082b01f | 2010-12-23 22:54:06 +0100 | [diff] [blame] | 83 | LOGP(DMM, LOGL_NOTICE, |
| 84 | "No retrievable Ki for subscriber, skipping auth\n"); |
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 | 082b01f | 2010-12-23 22:54:06 +0100 | [diff] [blame] | 96 | DEBUGP(DMM, "Auth tuple use < 3, just doing ciphering\n"); |
Harald Welte | 86dda08 | 2010-12-23 18:07:49 +0100 | [diff] [blame] | 97 | return AUTH_DO_CIPH; |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* Generate a new one */ |
| 101 | atuple->use_count = 1; |
| 102 | atuple->key_seq = (atuple->key_seq + 1) % 7; |
| 103 | for (i=0; i<sizeof(atuple->rand); i++) |
| 104 | atuple->rand[i] = random() & 0xff; |
| 105 | |
| 106 | switch (ainfo.auth_algo) { |
Harald Welte | 082b01f | 2010-12-23 22:54:06 +0100 | [diff] [blame] | 107 | case AUTH_ALGO_NONE: |
| 108 | DEBUGP(DMM, "No authentication for subscriber\n"); |
| 109 | return 0; |
| 110 | |
| 111 | case AUTH_ALGO_XOR: |
| 112 | if (_use_xor(&ainfo, atuple)) |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 113 | return 0; |
Harald Welte | 082b01f | 2010-12-23 22:54:06 +0100 | [diff] [blame] | 114 | break; |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 115 | |
Harald Welte | 082b01f | 2010-12-23 22:54:06 +0100 | [diff] [blame] | 116 | case AUTH_ALGO_COMP128v1: |
| 117 | if (_use_comp128_v1(&ainfo, atuple)) |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 118 | return 0; |
Harald Welte | 082b01f | 2010-12-23 22:54:06 +0100 | [diff] [blame] | 119 | break; |
| 120 | |
| 121 | default: |
| 122 | DEBUGP(DMM, "Unsupported auth type algo_id=%d\n", |
| 123 | ainfo.auth_algo); |
| 124 | return 0; |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | db_sync_lastauthtuple_for_subscr(atuple, subscr); |
| 128 | |
Harald Welte | 082b01f | 2010-12-23 22:54:06 +0100 | [diff] [blame] | 129 | DEBUGP(DMM, "Need to do authentication and ciphering\n"); |
Harald Welte | 86dda08 | 2010-12-23 18:07:49 +0100 | [diff] [blame] | 130 | return AUTH_DO_AUTH_THAN_CIPH; |
Sylvain Munaut | 30a1538 | 2009-12-24 00:27:26 +0100 | [diff] [blame] | 131 | } |
| 132 | |