blob: d81723fd6ed2dc06d0ec87331854597781adf702 [file] [log] [blame]
Neels Hofmeyrd739f092015-10-12 11:57:34 +02001/* Osmocom Authentication Protocol message encoder/decoder */
2
3/* (C) 2015 by Sysmocom s.f.m.c. GmbH
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * This program is free software; you can redistribute it and/or modify
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
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 Affero General Public License for more details.
17 *
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/>.
20 *
21 */
22
Harald Welte50f1c0a2016-04-25 19:01:26 +020023#include <osmocom/core/utils.h>
Neels Hofmeyrd739f092015-10-12 11:57:34 +020024#include <openbsc/oap_messages.h>
25
26#include <openbsc/debug.h>
Neels Hofmeyrd739f092015-10-12 11:57:34 +020027
28#include <osmocom/gsm/tlv.h>
29#include <osmocom/core/msgb.h>
30
31#include <stdint.h>
32
33
Harald Welte5d547a42016-04-27 18:21:16 +020034int osmo_oap_decode(struct osmo_oap_message *oap_msg,
35 const uint8_t *const_data, size_t data_len)
Neels Hofmeyrd739f092015-10-12 11:57:34 +020036{
37 int rc;
38 uint8_t tag;
39 /* the shift/match functions expect non-const pointers, but we'll
40 * either copy the data or cast pointers back to const before returning
41 * them
42 */
43 uint8_t *data = (uint8_t *)const_data;
44 uint8_t *value;
45 size_t value_len;
46
47 memset(oap_msg, 0, sizeof(*oap_msg));
48
49 /* message type */
Harald Welte842674b2016-04-25 15:14:01 +020050 rc = osmo_shift_v_fixed(&data, &data_len, 1, &value);
Neels Hofmeyrd739f092015-10-12 11:57:34 +020051 if (rc < 0)
52 return -GMM_CAUSE_INV_MAND_INFO;
Harald Welte50f1c0a2016-04-25 19:01:26 +020053 oap_msg->message_type = osmo_decode_big_endian(value, 1);
Neels Hofmeyrd739f092015-10-12 11:57:34 +020054
55 /* specific parts */
56 while (data_len > 0) {
57 enum oap_iei iei;
58
Harald Welte842674b2016-04-25 15:14:01 +020059 rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len);
Neels Hofmeyrd739f092015-10-12 11:57:34 +020060 if (rc < 0)
61 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
62
63 iei = tag;
64
65 switch (iei) {
66 case OAP_CLIENT_ID_IE:
67 if (value_len != 2) {
68 LOGP(DGPRS, LOGL_NOTICE,
69 "OAP IE type client ID (%d) should be 2 octets, but has %d\n",
70 (int)iei, (int)value_len);
71 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
72 }
73
Harald Welte50f1c0a2016-04-25 19:01:26 +020074 oap_msg->client_id = osmo_decode_big_endian(value, value_len);
Neels Hofmeyrd739f092015-10-12 11:57:34 +020075
76 if (oap_msg->client_id == 0) {
77 LOGP(DGPRS, LOGL_NOTICE,
78 "OAP IE type client ID (%d): client ID must be nonzero.\n",
79 (int)iei);
80 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
81 }
82 break;
83
84 case OAP_AUTN_IE:
85 if (value_len != sizeof(oap_msg->autn)) {
86 LOGP(DGPRS, LOGL_NOTICE,
87 "OAP IE type AUTN (%d) should be %d octets, but has %d\n",
88 (int)iei, (int)sizeof(oap_msg->autn), (int)value_len);
89 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
90 }
91 memcpy(oap_msg->autn, value, value_len);
92 oap_msg->autn_present = value_len;
93 break;
94
95 case OAP_RAND_IE:
96 if (value_len != sizeof(oap_msg->rand)) {
97 LOGP(DGPRS, LOGL_NOTICE,
98 "OAP IE type RAND (%d) should be %d octets, but has %d\n",
99 (int)iei, (int)sizeof(oap_msg->rand), (int)value_len);
100 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
101 }
102 memcpy(oap_msg->rand, value, value_len);
103 oap_msg->rand_present = value_len;
104 break;
105
106 case OAP_XRES_IE:
107 if (value_len != sizeof(oap_msg->xres)) {
108 LOGP(DGPRS, LOGL_NOTICE,
109 "OAP IE type XRES (%d) should be %d octets, but has %d\n",
110 (int)iei, (int)sizeof(oap_msg->xres), (int)value_len);
111 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
112 }
113 memcpy(oap_msg->xres, value, value_len);
114 oap_msg->xres_present = value_len;
115 break;
116
117 case OAP_AUTS_IE:
118 if (value_len != sizeof(oap_msg->auts)) {
119 LOGP(DGPRS, LOGL_NOTICE,
120 "OAP IE type AUTS (%d) should be %d octets, but has %d\n",
121 (int)iei, (int)sizeof(oap_msg->auts), (int)value_len);
122 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
123 }
124 memcpy(oap_msg->auts, value, value_len);
125 oap_msg->auts_present = value_len;
126 break;
127
128 case OAP_CAUSE_IE:
129 if (value_len > 1) {
130 LOGP(DGPRS, LOGL_ERROR,
131 "OAP cause may not exceed one octet, is %d", (int)value_len);
132 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
133 }
134 oap_msg->cause = *value;
135 break;
136
137 default:
138 LOGP(DGPRS, LOGL_NOTICE,
139 "OAP IE type %d unknown\n", iei);
140 continue;
141 }
142 }
143
144 return 0;
145}
146
Harald Welte564c0652016-04-27 18:14:14 +0200147void osmo_oap_encode(struct msgb *msg, const struct osmo_oap_message *oap_msg)
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200148{
149 uint8_t u8;
150
151 /* generic part */
152 OSMO_ASSERT(oap_msg->message_type);
153 msgb_v_put(msg, (uint8_t)oap_msg->message_type);
154
155 /* specific parts */
156 if ((u8 = oap_msg->cause))
157 msgb_tlv_put(msg, OAP_CAUSE_IE, sizeof(u8), &u8);
158
159 if (oap_msg->client_id > 0)
160 msgb_tlv_put(msg, OAP_CLIENT_ID_IE, sizeof(oap_msg->client_id),
Harald Welte50f1c0a2016-04-25 19:01:26 +0200161 osmo_encode_big_endian(oap_msg->client_id,
162 sizeof(oap_msg->client_id)));
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200163
164 if (oap_msg->rand_present)
165 msgb_tlv_put(msg, OAP_RAND_IE, sizeof(oap_msg->rand), oap_msg->rand);
166
167 if (oap_msg->autn_present)
168 msgb_tlv_put(msg, OAP_AUTN_IE, sizeof(oap_msg->autn), oap_msg->autn);
169
170 if (oap_msg->auts_present)
171 msgb_tlv_put(msg, OAP_AUTS_IE, sizeof(oap_msg->auts), oap_msg->auts);
172
173 if (oap_msg->xres_present)
174 msgb_tlv_put(msg, OAP_XRES_IE, sizeof(oap_msg->xres), oap_msg->xres);
175
176 msg->l2h = msg->data;
177}
178