summaryrefslogtreecommitdiff
path: root/src/ipcpd/common/enroll.c
blob: 5e35ce37896b0f5d4b599f77c611f65aa33b087a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * Ouroboros - Copyright (C) 2016 - 2024
 *
 * Enrollment Task
 *
 *    Dimitri Staessens <dimitri@ouroboros.rocks>
 *    Sander Vrijders   <sander@ouroboros.rocks>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#if defined(__linux__) || defined(__CYGWIN__)
#define _DEFAULT_SOURCE
#else
#define _POSIX_C_SOURCE 199309L
#endif

#define OUROBOROS_PREFIX "enrollment"

#include <ouroboros/dev.h>
#include <ouroboros/errno.h>
#include <ouroboros/logs.h>
#include <ouroboros/serdes-oep.h>
#include <ouroboros/time.h>

#include "common/connmgr.h"
#include "common/enroll.h"
#include "ipcp.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

#define ENROLL_COMP             "Enrollment"
#define ENROLL_PROTO            "OEP" /* Ouroboros enrollment protocol */
#define ENROLL_WARN_TIME_OFFSET 20
#define ENROLL_BUF_LEN          1024

enum enroll_state {
        ENROLL_NULL = 0,
        ENROLL_INIT,
        ENROLL_RUNNING
};

struct {
        struct ipcp_config conf;
        enum enroll_state  state;
        pthread_t          listener;
} enroll;

static void * enroll_handle(void * o)
{
        struct enroll_req  req;
        struct enroll_resp resp;
        struct enroll_ack  ack;
        struct conn        conn;
        uint8_t             __buf[ENROLL_BUF_LEN];
        buffer_t           buf;
        ssize_t            len;

        (void) o;

        buf.data = __buf;
        buf.len  = sizeof(__buf);

        resp.response = 0;
        resp.conf = enroll.conf;

        while (true) {
                buffer_t msg;
                int      fd;

                if (connmgr_wait(COMPID_ENROLL, &conn)) {
                        log_err("Failed to get next connection.");
                        continue;
                }

                fd = conn.flow_info.fd;

                log_info("Incoming enrollment connection on flow %d.", fd);

                len = flow_read(fd, buf.data, buf.len);
                if (len < 0) {
                        log_err("Failed to read from flow %d.", fd);
                        goto finish_flow;
                }

                msg.data = buf.data;
                msg.len = (size_t) len;

                if (enroll_req_des(&req, msg) < 0) {
                        log_err("Failed to unpack request message.");
                        goto finish_flow;
                }

                log_info_id(req.id, "Handling incoming enrollment.");

                /* TODO: authentication, timezone handling (UTC). */

                ack.result = -100;

                clock_gettime(CLOCK_REALTIME, &resp.t);

                memcpy(resp.id, req.id, ENROLL_ID_LEN);

                len = enroll_resp_ser(&resp, buf);
                if (len < 0) {
                        log_err_id(req.id, "Failed to pack reply.");
                        goto finish_enroll;
                }

                log_dbg_id(req.id, "Sending enrollment info (%zd bytes).", len);

                if (flow_write(conn.flow_info.fd, buf.data, len) < 0) {
                        log_err_id(req.id, "Failed te send response.");
                        goto finish_enroll;
                }

                len = flow_read(conn.flow_info.fd, buf.data, buf.len);
                if (len < 0) {
                        log_err_id(req.id, "Failed to read from flow.");
                        goto finish_enroll;
                }

                msg.data = buf.data;
                msg.len = (size_t) len;

                if (enroll_ack_des(&ack, msg) < 0) {
                        log_err_id(req.id, "Failed to unpack ack.");
                        goto finish_enroll;
                }

                if (memcmp(req.id, ack.id, ENROLL_ID_LEN) != 0)
                       log_warn_id(req.id, "Enrollment ID mismatch.");

         finish_enroll:
                switch(ack.result) {
                case 0:
                        log_info_id(req.id, "Enrollment completed.");
                        break;
                case -100:
                        log_warn_id(req.id, "Enrollment failed.");
                        break;
                default:
                        log_warn_id(req.id, "Enrollment failed at remote.");
                }
         finish_flow:
                connmgr_dealloc(COMPID_ENROLL, &conn);

                log_info("Enrollment flow %d closed.", fd);
        }

        return 0;
}

int enroll_boot(struct conn *   conn,
                const uint8_t * id)
{
        uint8_t            __buf[ENROLL_BUF_LEN];
        buffer_t           buf;
        buffer_t           msg;
        ssize_t            len;
        ssize_t            delta_t;
        struct timespec    t0;
        struct timespec    rtt;
        int                fd;
        int                ret;
        struct enroll_req  req;
        struct enroll_resp resp;

        fd = conn->flow_info.fd;

        buf.data = __buf;
        buf.len  = sizeof(__buf);

        memcpy(req.id, id, ENROLL_ID_LEN);

        len = enroll_req_ser(&req, buf);
        if (len < 0) {
                log_err_id(id, "Failed to pack request message.");
                return -1;
        }

        clock_gettime(CLOCK_REALTIME, &t0);

        if (flow_write(fd, buf.data, len) < 0) {
                log_err_id(id, "Failed to send request message.");
                return -1;
        }

        len = flow_read(fd, buf.data, buf.len);
        if (len < 0) {
                log_err_id(id, "No reply received.");
                return -1;
        }

        log_dbg_id(id, "Received configuration info (%zd bytes).", len);

        msg.data = buf.data;
        msg.len  = len;

        ret = enroll_resp_des(&resp, msg);
        if (ret < 0) {
                log_err_id(id, "Failed to unpack response message.");
                return -1;
        }

        if (memcmp(resp.id, id, ENROLL_ID_LEN) != 0) {
                log_err_id(id, "Enrollment ID mismatch.");
                return -1;
        }

        if (resp.response < 0) {
                log_warn_id(id, "Remote denied request: %d.", resp.response);
                return -1;
        }

        if (resp.conf.type != ipcpi.type) {
                log_err_id(id, "Wrong type in enrollment response %d (%d).",
                           resp.conf.type, ipcpi.type);
                return -1;
        }

        clock_gettime(CLOCK_REALTIME, &rtt);

        delta_t = ts_diff_ms(&t0, &rtt);

        rtt.tv_sec  = resp.t.tv_sec;
        rtt.tv_nsec = resp.t.tv_nsec;

        if (labs(ts_diff_ms(&t0, &rtt)) - delta_t > ENROLL_WARN_TIME_OFFSET)
                log_warn_id(id, "Clock offset above threshold.");

        enroll.conf = resp.conf;

        return 0;
}

int enroll_ack(struct conn *   conn,
               const uint8_t * id,
               const int       result)
{
        struct enroll_ack ack;
        uint8_t           __buf[ENROLL_BUF_LEN];
        buffer_t          buf;
        ssize_t           len;

        buf.data = __buf;
        buf.len  = sizeof(__buf);

        ack.result = result;

        memcpy(ack.id, id, ENROLL_ID_LEN);

        len = enroll_ack_ser(&ack, buf);
        if (len < 0) {
                log_err_id(id, "Failed to pack acknowledgement.");
                return -1;
        }

        if (flow_write(conn->flow_info.fd, buf.data, len) < 0) {
                log_err_id(id, "Failed to send acknowledgment.");
                return -1;
        }

        return 0;
}

void enroll_bootstrap(const struct ipcp_config * conf)
{
        assert(conf);

        memcpy(&enroll.conf, conf, sizeof(enroll.conf));
}

struct ipcp_config * enroll_get_conf(void)
{
        return &enroll.conf;
}

int enroll_init(void)
{
        struct conn_info info;

        memset(&info, 0, sizeof(info));

        strcpy(info.comp_name, ENROLL_COMP);
        strcpy(info.protocol, ENROLL_PROTO);
        info.pref_version = 1;
        info.pref_syntax  = PROTO_GPB;
        info.addr         = 0;

        if (connmgr_comp_init(COMPID_ENROLL, &info)) {
                log_err("Failed to register with connmgr.");
                return -1;
        }

        enroll.state = ENROLL_INIT;

        return 0;
}

void enroll_fini(void)
{
        if (enroll.state == ENROLL_RUNNING)
                pthread_join(enroll.listener, NULL);

        connmgr_comp_fini(COMPID_ENROLL);
}

int enroll_start(void)
{
        if (pthread_create(&enroll.listener, NULL, enroll_handle, NULL))
                return -1;

        enroll.state = ENROLL_RUNNING;

        return 0;
}

void enroll_stop(void)
{
        if (enroll.state == ENROLL_RUNNING)
                pthread_cancel(enroll.listener);
}