summaryrefslogtreecommitdiff
path: root/src/lib/pol/cacep_anonymous_auth.c
blob: 1ad8a5332ef3edee1d6f995b3d123de7ced4af0f (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
/*
 * Ouroboros - Copyright (C) 2016 - 2017
 *
 * Anonymous policy for CACEP
 *
 *    Dimitri Staessens <dimitri.staessens@ugent.be>
 *    Sander Vrijders   <sander.vrijders@ugent.be>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <ouroboros/config.h>
#include <ouroboros/cacep.h>
#include <ouroboros/time_utils.h>
#include <ouroboros/dev.h>
#include <ouroboros/errno.h>

#include "cacep_proto.h"
#include "cacep_anonymous_auth.h"

#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdio.h>

#include "cacep_anonymous_auth.pb-c.h"
typedef CacepAnonymousAuthMsg cacep_anonymous_auth_msg_t;
typedef CacepProtoMsg cacep_proto_msg_t;

#define BUF_SIZE 2048
#define NAME_LEN 8

/* this policy generates a hex string */
static struct cacep_info * anonymous_info(void)
{
        struct cacep_info * info;
        struct timespec t;

        info = malloc(sizeof(*info));
        if (info == NULL)
                return NULL;

        info->name = malloc(NAME_LEN + 1);
        if (info->name == NULL) {
                free(info);
                return NULL;
        }

        clock_gettime(CLOCK_REALTIME, &t);
        srand(t.tv_nsec);

        sprintf(info->name, "%8x",
                (uint32_t)((rand() % RAND_MAX) & 0xFFFFFFFF));

        info->addr = 0;

        return info;
}

static struct cacep_info * read_msg(int fd)
{
        struct cacep_info *          tmp;
        uint8_t                      buf[BUF_SIZE];
        cacep_anonymous_auth_msg_t * msg;
        ssize_t                      len;

        len = flow_read(fd, buf, BUF_SIZE);
        if (len < 0)
                return NULL;

        msg = cacep_anonymous_auth_msg__unpack(NULL, len, buf);
        if (msg == NULL)
                return NULL;

        tmp = anonymous_info();
        if (tmp == NULL) {
                cacep_anonymous_auth_msg__free_unpacked(msg, NULL);
                return NULL;
        }

        tmp->proto.protocol = strdup(msg->proto->protocol);
        if (tmp->proto.protocol == NULL) {
                free(tmp);
                cacep_anonymous_auth_msg__free_unpacked(msg, NULL);
                return NULL;
        }

        tmp->proto.pref_version = msg->proto->pref_version;
        tmp->proto.pref_syntax  = code_to_syntax(msg->proto->pref_syntax);
        if (tmp->proto.pref_syntax < 0) {
                free(tmp->proto.protocol);
                free(tmp);
                cacep_anonymous_auth_msg__free_unpacked(msg, NULL);
                return NULL;
        }

        cacep_anonymous_auth_msg__free_unpacked(msg, NULL);

        return tmp;
}

static int send_msg(int                       fd,
                    const struct cacep_info * info)
{
        cacep_anonymous_auth_msg_t msg  = CACEP_ANONYMOUS_AUTH_MSG__INIT;
        cacep_proto_msg_t          cmsg = CACEP_PROTO_MSG__INIT;
        int                        ret  = 0;
        uint8_t *                  data = NULL;
        size_t                     len  = 0;

        cmsg.protocol     = info->proto.protocol;
        cmsg.pref_version = info->proto.pref_version;
        cmsg.pref_syntax  = syntax_to_code(info->proto.pref_syntax);
        if (cmsg.pref_syntax < 0)
                return -1;

        msg.proto = &cmsg;

        len = cacep_anonymous_auth_msg__get_packed_size(&msg);
        if (len == 0)
                return -1;

        data = malloc(len);
        if (data == NULL)
                return -ENOMEM;

        cacep_anonymous_auth_msg__pack(&msg, data);

        if (flow_write(fd, data, len) < 0)
                ret = -1;

        free(data);

        return ret;
}

struct cacep_info * cacep_anonymous_auth(int                       fd,
                                         const struct cacep_info * info)
{
        struct cacep_info * tmp;

        if (send_msg(fd, info))
                return NULL;

        tmp = read_msg(fd);
        if (tmp == NULL)
                return NULL;

        if (strcmp(info->proto.protocol, tmp->proto.protocol) ||
            info->proto.pref_version != tmp->proto.pref_version ||
            info->proto.pref_syntax != tmp->proto.pref_syntax) {
                free(tmp);
                return NULL;
        }

        tmp->data = NULL;

        return tmp;
}


struct cacep_info * cacep_anonymous_auth_wait(int                       fd,
                                              const struct cacep_info * info)
{
        struct cacep_info * tmp;

        tmp = read_msg(fd);
        if (tmp == NULL)
                return NULL;

        if (send_msg(fd, info)) {
                free(tmp);
                return NULL;
        }

        if (strcmp(info->proto.protocol, tmp->proto.protocol) ||
            info->proto.pref_version != tmp->proto.pref_version ||
            info->proto.pref_syntax != tmp->proto.pref_syntax) {
                free(tmp);
                return NULL;
        }

        tmp->data = NULL;

        return tmp;
}