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
|
/* $OpenBSD: auth.c,v 1.10 2006/04/24 20:18:03 claudio Exp $ */
/*
* Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <limits.h>
#include <md5.h>
#include <stdlib.h>
#include <string.h>
#include "ospfd.h"
#include "ospf.h"
#include "log.h"
#include "ospfe.h"
struct auth_md *md_list_find(struct auth_md_head *, u_int8_t);
int
auth_validate(void *buf, u_int16_t len, struct iface *iface, struct nbr *nbr)
{
MD5_CTX hash;
u_int8_t digest[MD5_DIGEST_LENGTH];
u_int8_t recv_digest[MD5_DIGEST_LENGTH];
struct ospf_hdr *ospf_hdr = buf;
struct auth_md *md;
char *auth_data;
if (ntohs(ospf_hdr->auth_type) != (u_int16_t)iface->auth_type) {
log_debug("auth_validate: wrong auth type, interface %s",
iface->name);
return (-1);
}
switch (iface->auth_type) {
case AUTH_SIMPLE:
if (bcmp(ospf_hdr->auth_key.simple, iface->auth_key,
sizeof(ospf_hdr->auth_key.simple))) {
log_debug("auth_validate: wrong password, interface %s",
iface->name);
return (-1);
}
/* FALLTHROUGH */
case AUTH_NONE:
/* clear the key before chksum */
bzero(ospf_hdr->auth_key.simple,
sizeof(ospf_hdr->auth_key.simple));
if (in_cksum(ospf_hdr, ntohs(ospf_hdr->len))) {
log_debug("auth_validate: invalid checksum, "
"interface %s", iface->name);
return (-1);
}
break;
case AUTH_CRYPT:
/*
* We must allow keys that are configured on the interface
* but not necessarily set as the transmit key
* (iface->auth_keyid). This allows for key rotation to new
* keys without taking down the network.
*/
if ((md = md_list_find(&iface->auth_md_list,
ospf_hdr->auth_key.crypt.keyid)) == NULL) {
log_debug("auth_validate: keyid %d not configured, "
"interface %s", ospf_hdr->auth_key.crypt.keyid,
iface->name);
return (-1);
}
if (nbr != NULL && ntohl(ospf_hdr->auth_key.crypt.seq_num) <
nbr->crypt_seq_num) {
log_debug("auth_validate: decreasing seq num, "
"interface %s", iface->name);
return (-1);
}
if (ospf_hdr->auth_key.crypt.len != MD5_DIGEST_LENGTH) {
log_debug("auth_validate: invalid key length, "
"interface %s", iface->name);
return (-1);
}
if (len - ntohs(ospf_hdr->len) < MD5_DIGEST_LENGTH) {
log_debug("auth_validate: invalid key length, "
"interface %s", iface->name);
return (-1);
}
auth_data = buf;
auth_data += ntohs(ospf_hdr->len);
/* save the received digest and clear it in the packet */
bcopy(auth_data, recv_digest, sizeof(recv_digest));
bzero(auth_data, MD5_DIGEST_LENGTH);
/* insert plaintext key */
bzero(digest, MD5_DIGEST_LENGTH);
strncpy(digest, md->key, MD5_DIGEST_LENGTH);
/* calculate MD5 digest */
MD5Init(&hash);
MD5Update(&hash, buf, ntohs(ospf_hdr->len));
MD5Update(&hash, digest, MD5_DIGEST_LENGTH);
MD5Final(digest, &hash);
if (bcmp(recv_digest, digest, sizeof(digest))) {
log_debug("auth_validate: invalid MD5 digest, "
"interface %s", iface->name);
return (-1);
}
if (nbr != NULL)
nbr->crypt_seq_num =
ntohl(ospf_hdr->auth_key.crypt.seq_num);
break;
default:
log_debug("auth_validate: unknown auth type, interface %s",
iface->name);
return (-1);
}
return (0);
}
int
auth_gen(struct buf *buf, struct iface *iface)
{
MD5_CTX hash;
u_int8_t digest[MD5_DIGEST_LENGTH];
struct ospf_hdr *ospf_hdr;
struct auth_md *md;
if ((ospf_hdr = buf_seek(buf, 0, sizeof(ospf_hdr))) == NULL)
fatalx("auth_gen: buf_seek failed");
/* update length */
if (buf->wpos > USHRT_MAX)
fatalx("auth_gen: resulting ospf packet too big");
ospf_hdr->len = htons((u_int16_t)buf->wpos);
/* clear auth_key field */
bzero(ospf_hdr->auth_key.simple, sizeof(ospf_hdr->auth_key.simple));
switch (iface->auth_type) {
case AUTH_NONE:
ospf_hdr->chksum = in_cksum(buf->buf, buf->wpos);
break;
case AUTH_SIMPLE:
ospf_hdr->chksum = in_cksum(buf->buf, buf->wpos);
strncpy(ospf_hdr->auth_key.simple, iface->auth_key,
sizeof(ospf_hdr->auth_key.simple));
break;
case AUTH_CRYPT:
ospf_hdr->chksum = 0;
ospf_hdr->auth_key.crypt.keyid = iface->auth_keyid;
ospf_hdr->auth_key.crypt.seq_num = htonl(iface->crypt_seq_num);
ospf_hdr->auth_key.crypt.len = MD5_DIGEST_LENGTH;
iface->crypt_seq_num++;
/* insert plaintext key */
if ((md = md_list_find(&iface->auth_md_list,
iface->auth_keyid)) == NULL) {
log_debug("auth_validate: keyid %d not configured, "
"interface %s", iface->auth_keyid, iface->name);
return (-1);
}
bzero(digest, MD5_DIGEST_LENGTH);
strncpy(digest, md->key, MD5_DIGEST_LENGTH);
/* calculate MD5 digest */
MD5Init(&hash);
MD5Update(&hash, buf->buf, buf->wpos);
MD5Update(&hash, digest, MD5_DIGEST_LENGTH);
MD5Final(digest, &hash);
return (buf_add(buf, digest, MD5_DIGEST_LENGTH));
default:
log_debug("auth_gen: unknown auth type, interface %s",
iface->name);
return (-1);
}
return (0);
}
/* md list */
void
md_list_add(struct auth_md_head *head, u_int8_t keyid, char *key)
{
struct auth_md *md;
if ((md = md_list_find(head, keyid)) != NULL) {
/* update key */
strncpy(md->key, key, sizeof(md->key));
return;
}
if ((md = calloc(1, sizeof(struct auth_md))) == NULL)
fatalx("md_list_add");
md->keyid = keyid;
strncpy(md->key, key, sizeof(md->key));
TAILQ_INSERT_TAIL(head, md, entry);
}
void
md_list_copy(struct auth_md_head *to, struct auth_md_head *from)
{
struct auth_md *m, *md;
TAILQ_INIT(to);
TAILQ_FOREACH(m, from, entry) {
if ((md = calloc(1, sizeof(struct auth_md))) == NULL)
fatalx("md_list_add");
md->keyid = m->keyid;
strncpy(md->key, m->key, sizeof(md->key));
TAILQ_INSERT_TAIL(to, md, entry);
}
}
void
md_list_clr(struct auth_md_head *head)
{
struct auth_md *m;
while ((m = TAILQ_FIRST(head)) != NULL) {
TAILQ_REMOVE(head, m, entry);
free(m);
}
}
struct auth_md *
md_list_find(struct auth_md_head *head, u_int8_t keyid)
{
struct auth_md *m;
TAILQ_FOREACH(m, head, entry)
if (m->keyid == keyid)
return (m);
return (NULL);
}
|