blob: f94cc832a0bf0cfd076c6729bfe28a33d65192bf [file] [log] [blame]
Harald Weltefdd366e2018-07-30 12:04:21 +02001/* Osmocom Authentication Protocol API */
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
Harald Weltec53afd92019-05-27 23:13:11 +02009 * 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
Harald Weltefdd366e2018-07-30 12:04:21 +020011 * (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 Weltec53afd92019-05-27 23:13:11 +020016 * GNU General Public License for more details.
Harald Weltefdd366e2018-07-30 12:04:21 +020017 *
Harald Weltec53afd92019-05-27 23:13:11 +020018 * You should have received a copy of the GNU General Public License
Harald Weltefdd366e2018-07-30 12:04:21 +020019 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
Harald Weltec53afd92019-05-27 23:13:11 +020021 * SPDX-License-Identifier: GPL-2.0+
Harald Weltefdd366e2018-07-30 12:04:21 +020022 */
23
24#pragma once
25
26#include <stdint.h>
27
28struct msgb;
29struct osmo_oap_message;
30
31/* This is the config part for vty. It is essentially copied in
32 * oap_client_state, where values are copied over once the config is
33 * considered valid. */
Harald Welte9b04c172018-07-30 12:06:31 +020034struct osmo_oap_client_config {
Harald Weltefdd366e2018-07-30 12:04:21 +020035 uint16_t client_id;
36 int secret_k_present;
37 uint8_t secret_k[16];
38 int secret_opc_present;
39 uint8_t secret_opc[16];
40};
41
42/* The runtime state of the OAP client. client_id and the secrets are in fact
43 * duplicated from oap_client_config, so that a separate validation of the
44 * config data is possible, and so that only a struct oap_client_state* is
45 * passed around. */
Harald Welte9b04c172018-07-30 12:06:31 +020046struct osmo_oap_client_state {
Harald Weltefdd366e2018-07-30 12:04:21 +020047 enum {
Harald Welte9b04c172018-07-30 12:06:31 +020048 OSMO_OAP_UNINITIALIZED = 0, /* just allocated. */
49 OSMO_OAP_DISABLED, /* disabled by config. */
50 OSMO_OAP_INITIALIZED, /* enabled, config is valid. */
51 OSMO_OAP_REQUESTED_CHALLENGE,
52 OSMO_OAP_SENT_CHALLENGE_RESULT,
53 OSMO_OAP_REGISTERED
Harald Weltefdd366e2018-07-30 12:04:21 +020054 } state;
55 uint16_t client_id;
56 uint8_t secret_k[16];
57 uint8_t secret_opc[16];
58 int registration_failures;
59};
60
61/* From config, initialize state. Return 0 on success. */
Harald Welte9b04c172018-07-30 12:06:31 +020062int osmo_oap_client_init(struct osmo_oap_client_config *config,
63 struct osmo_oap_client_state *state);
Harald Weltefdd366e2018-07-30 12:04:21 +020064
65/* Construct an OAP registration message and return in *msg_tx. Use
66 * state->client_id and update state->state.
67 * Return 0 on success, or a negative value on error.
68 * If an error is returned, *msg_tx is guaranteed to be NULL. */
Harald Welte9b04c172018-07-30 12:06:31 +020069int osmo_oap_client_register(struct osmo_oap_client_state *state, struct msgb **msg_tx);
Harald Weltefdd366e2018-07-30 12:04:21 +020070
71/* Decode and act on a received OAP message msg_rx. Update state->state. If a
72 * non-NULL pointer is returned in *msg_tx, that msgb should be sent to the OAP
73 * server (and freed) by the caller. The received msg_rx is not freed.
74 * Return 0 on success, or a negative value on error.
75 * If an error is returned, *msg_tx is guaranteed to be NULL. */
Harald Welte9b04c172018-07-30 12:06:31 +020076int osmo_oap_client_handle(struct osmo_oap_client_state *state,
77 const struct msgb *msg_rx, struct msgb **msg_tx);
Harald Weltefdd366e2018-07-30 12:04:21 +020078
79/* Allocate a msgb and in it, return the encoded oap_client_msg. Return
80 * NULL on error. (Like oap_client_encode(), but also allocates a msgb.)
81 * About the name: the idea is do_something(oap_client_encoded(my_struct))
82 */
Harald Welte9b04c172018-07-30 12:06:31 +020083struct msgb *osmo_oap_client_encoded(const struct osmo_oap_message *oap_client_msg);