blob: ee1e291c41c4e5b57799db27bf681ee85a368341 [file] [log] [blame]
Sylvain Munaut30a15382009-12-24 00:27:26 +01001/* 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>
26#include <openbsc/gsm_data.h>
27
28#include <osmocore/comp128.h>
29
30#include <stdlib.h>
31
32
33static int
Sylvain Munaute824d9c2010-06-11 00:19:42 +020034_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)) {
39 DEBUGP(DMM, "Invalid XOR key (len=%d) %s",
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 Munaut0230b342010-09-21 19:03:09 +020047 for (i=4; i<12; i++)
Sylvain Munaute824d9c2010-06-11 00:19:42 +020048 atuple->kc[i-4] = atuple->rand[i] ^ ainfo->a3a8_ki[i];
49
50 return 0;
51}
52
53static int
Sylvain Munaut30a15382009-12-24 00:27:26 +010054_use_comp128_v1(struct gsm_auth_info *ainfo, struct gsm_auth_tuple *atuple)
55{
56 if (ainfo->a3a8_ki_len != A38_COMP128_KEY_LEN) {
57 DEBUGP(DMM, "Invalid COMP128v1 key (len=%d) %s",
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 */
74int 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) {
83 DEBUGP(DMM, "No retrievable Ki for subscriber, skipping auth");
84 return rc == -ENOENT ? 0 : -1;
85 }
86
87 /* If possible, re-use the last tuple and skip auth */
88 rc = db_get_lastauthtuple_for_subscr(atuple, subscr);
89 if ((rc == 0) &&
Sylvain Munautcd8e8102010-09-21 19:04:07 +020090 (key_seq != GSM_KEY_SEQ_INVAL) &&
Sylvain Munaut30a15382009-12-24 00:27:26 +010091 (atuple->use_count < 3))
92 {
93 atuple->use_count++;
94 db_sync_lastauthtuple_for_subscr(atuple, subscr);
95 return 2;
96 }
97
98 /* Generate a new one */
99 atuple->use_count = 1;
100 atuple->key_seq = (atuple->key_seq + 1) % 7;
101 for (i=0; i<sizeof(atuple->rand); i++)
102 atuple->rand[i] = random() & 0xff;
103
104 switch (ainfo.auth_algo) {
105 case AUTH_ALGO_NONE:
106 return 0;
107
Sylvain Munaute824d9c2010-06-11 00:19:42 +0200108 case AUTH_ALGO_XOR:
109 if (_use_xor(&ainfo, atuple))
110 return 0;
111 break;
112
Sylvain Munaut30a15382009-12-24 00:27:26 +0100113 case AUTH_ALGO_COMP128v1:
114 if (_use_comp128_v1(&ainfo, atuple))
115 return 0;
116 break;
117
118 default:
119 DEBUGP(DMM, "Unsupported auth type algo_id=%d\n",
120 ainfo.auth_algo);
121 return 0;
122 }
123
124 db_sync_lastauthtuple_for_subscr(atuple, subscr);
125
126 return 1;
127}
128