2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 Daniel Borkmann.
4 * Subject to the GPL, version 2.
15 #include <arpa/inet.h>
18 #include "ct_usermgmt.h"
26 #include "crypto_verify_32.h"
27 #include "crypto_hash_sha512.h"
28 #include "crypto_box_curve25519xsalsa20poly1305.h"
29 #include "crypto_auth_hmacsha512256.h"
31 #define crypto_box_pub_key_size crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES
33 /* Config line format: username;pubkey\n */
37 unsigned char publickey
[crypto_box_pub_key_size
];
38 struct curve25519_proto proto_inf
;
39 struct user_store
*next
;
42 struct sock_map_entry
{
44 struct curve25519_proto
*proto
;
45 struct sock_map_entry
*next
;
48 struct sockaddr_map_entry
{
49 struct sockaddr_storage
*sa
;
51 struct curve25519_proto
*proto
;
52 struct sockaddr_map_entry
*next
;
55 static struct user_store
*store
= NULL
;
56 static struct rwlock store_lock
;
58 static struct hash_table sock_mapper
;
59 static struct rwlock sock_map_lock
;
61 static struct hash_table sockaddr_mapper
;
62 static struct rwlock sockaddr_map_lock
;
64 static unsigned char token
[crypto_auth_hmacsha512256_KEYBYTES
];
66 static void init_sock_mapper(void)
68 rwlock_init(&sock_map_lock
);
70 rwlock_wr_lock(&sock_map_lock
);
72 memset(&sock_mapper
, 0, sizeof(sock_mapper
));
73 init_hash(&sock_mapper
);
75 rwlock_unlock(&sock_map_lock
);
78 static void init_sockaddr_mapper(void)
80 rwlock_init(&sockaddr_map_lock
);
81 rwlock_wr_lock(&sockaddr_map_lock
);
83 memset(&sockaddr_mapper
, 0, sizeof(sockaddr_mapper
));
84 init_hash(&sockaddr_mapper
);
86 rwlock_unlock(&sockaddr_map_lock
);
89 static int cleanup_batch_sock_mapper(void *ptr
)
91 struct sock_map_entry
*next
;
92 struct sock_map_entry
*e
= ptr
;
97 while ((next
= e
->next
)) {
108 static void destroy_sock_mapper(void)
110 rwlock_wr_lock(&sock_map_lock
);
111 for_each_hash(&sock_mapper
, cleanup_batch_sock_mapper
);
112 free_hash(&sock_mapper
);
113 rwlock_unlock(&sock_map_lock
);
115 rwlock_destroy(&sock_map_lock
);
118 static int cleanup_batch_sockaddr_mapper(void *ptr
)
120 struct sockaddr_map_entry
*next
;
121 struct sockaddr_map_entry
*e
= ptr
;
126 while ((next
= e
->next
)) {
136 static void destroy_sockaddr_mapper(void)
138 rwlock_wr_lock(&sockaddr_map_lock
);
139 for_each_hash(&sockaddr_mapper
, cleanup_batch_sockaddr_mapper
);
140 free_hash(&sockaddr_mapper
);
141 rwlock_unlock(&sockaddr_map_lock
);
143 rwlock_destroy(&sockaddr_map_lock
);
146 static struct user_store
*user_store_alloc(void)
148 return xzmalloc(sizeof(struct user_store
));
151 static void user_store_free(struct user_store
*us
)
155 memset(us
, 0, sizeof(struct user_store
));
159 /* already in lock */
160 static int __check_duplicate_username(char *username
, size_t len
)
163 struct user_store
*elem
= store
;
166 if (!memcmp(elem
->username
, username
,
167 strlen(elem
->username
) + 1)) {
177 /* already in lock */
178 static int __check_duplicate_pubkey(unsigned char *pubkey
, size_t len
)
181 struct user_store
*elem
= store
;
184 if (!memcmp(elem
->publickey
, pubkey
,
185 sizeof(elem
->publickey
))) {
201 static int parse_line(char *line
, char *homedir
)
205 enum parse_states s
= PARSE_USERNAME
;
206 struct user_store
*elem
;
207 unsigned char pkey
[crypto_box_pub_key_size
];
209 elem
= user_store_alloc();
212 str
= strtok(line
, ";");
213 for (; str
!= NULL
;) {
216 if (__check_duplicate_username(str
, strlen(str
) + 1))
218 strlcpy(elem
->username
, str
, sizeof(elem
->username
));
222 if (!curve25519_pubkey_hexparse_32(pkey
, sizeof(pkey
),
225 if (__check_duplicate_pubkey(pkey
, sizeof(pkey
)))
227 memcpy(elem
->publickey
, pkey
, sizeof(elem
->publickey
));
228 ret
= curve25519_proto_init(&elem
->proto_inf
,
230 sizeof(elem
->publickey
),
242 str
= strtok(NULL
, ";");
246 return s
== PARSE_DONE
? 0 : -EIO
;
249 void parse_userfile_and_generate_user_store_or_die(char *homedir
)
252 char path
[PATH_MAX
], buff
[512];
253 int line
= 1, ret
, fd
;
255 memset(path
, 0, sizeof(path
));
256 slprintf(path
, sizeof(path
), "%s/%s", homedir
, FILE_CLIENTS
);
258 rwlock_init(&store_lock
);
259 rwlock_wr_lock(&store_lock
);
261 fp
= fopen(path
, "r");
263 panic("Cannot open client file!\n");
265 memset(buff
, 0, sizeof(buff
));
266 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
267 buff
[sizeof(buff
) - 1] = 0;
268 /* A comment. Skip this line */
269 if (buff
[0] == '#' || buff
[0] == '\n') {
270 memset(buff
, 0, sizeof(buff
));
275 ret
= parse_line(buff
, homedir
);
277 panic("Cannot parse line %d from clients!\n", line
);
279 memset(buff
, 0, sizeof(buff
));
285 panic("No registered clients found!\n");
287 rwlock_unlock(&store_lock
);
290 init_sockaddr_mapper();
293 * Pubkey is also used as a hmac of the initial packet to check
294 * the integrity of the packet, so that we know if it's just random
295 * garbage or a 'valid' packet. Again, just for the integrity!
298 memset(path
, 0, sizeof(path
));
299 slprintf(path
, sizeof(path
), "%s/%s", homedir
, FILE_PUBKEY
);
301 fd
= open_or_die(path
, O_RDONLY
);
302 ret
= read(fd
, token
, sizeof(token
));
303 if (ret
!= crypto_auth_hmacsha512256_KEYBYTES
)
304 panic("Cannot read public key!\n");
308 void dump_user_store(void)
311 struct user_store
*elem
;
313 rwlock_rd_lock(&store_lock
);
317 printf("%s -> ", elem
->username
);
318 for (i
= 0; i
< sizeof(elem
->publickey
); ++i
)
319 if (i
== (sizeof(elem
->publickey
) - 1))
320 printf("%02x\n", (unsigned char)
323 printf("%02x:", (unsigned char)
328 rwlock_unlock(&store_lock
);
331 void destroy_user_store(void)
333 struct user_store
*elem
, *nelem
= NULL
;
335 rwlock_wr_lock(&store_lock
);
341 user_store_free(elem
);
344 rwlock_unlock(&store_lock
);
346 rwlock_destroy(&store_lock
);
348 destroy_sock_mapper();
349 destroy_sockaddr_mapper();
352 int username_msg(char *username
, size_t len
, char *dst
, size_t dlen
)
357 unsigned char h
[crypto_hash_sha512_BYTES
];
358 struct username_struct
*us
= (struct username_struct
*) dst
;
362 if (dlen
< sizeof(struct username_struct
))
366 uname
= xzmalloc(uname_len
);
368 fd
= open_or_die("/dev/random", O_RDONLY
);
369 ret
= read_exact(fd
, &salt
, sizeof(salt
), 0);
370 if (ret
!= sizeof(salt
))
371 panic("Cannot read from /dev/random!\n");
374 slprintf(uname
, uname_len
, "%s%u", username
, salt
);
375 crypto_hash_sha512(h
, (unsigned char *) uname
, strlen(uname
));
377 us
->salt
= htonl(salt
);
378 memcpy(us
->hash
, h
, sizeof(us
->hash
));
384 enum is_user_enum
username_msg_is_user(char *src
, size_t slen
, char *username
,
390 struct username_struct
*us
= (struct username_struct
*) src
;
391 unsigned char h
[crypto_hash_sha512_BYTES
];
393 if (slen
< sizeof(struct username_struct
)) {
395 return USERNAMES_ERR
;
399 uname
= xzmalloc(uname_len
);
401 salt
= ntohl(us
->salt
);
403 slprintf(uname
, uname_len
, "%s%u", username
, salt
);
404 crypto_hash_sha512(h
, (unsigned char *) uname
, strlen(uname
));
407 if (!crypto_verify_32(&h
[0], &us
->hash
[0]) &&
408 !crypto_verify_32(&h
[32], &us
->hash
[32]))
414 static int register_user_by_socket(int fd
, struct curve25519_proto
*proto
)
417 struct sock_map_entry
*entry
;
419 rwlock_wr_lock(&sock_map_lock
);
421 entry
= xzmalloc(sizeof(*entry
));
423 entry
->proto
= proto
;
425 pos
= insert_hash(entry
->fd
, entry
, &sock_mapper
);
427 entry
->next
= (*pos
);
431 rwlock_unlock(&sock_map_lock
);
436 static int register_user_by_sockaddr(struct sockaddr_storage
*sa
,
438 struct curve25519_proto
*proto
)
441 struct sockaddr_map_entry
*entry
;
442 unsigned int hash
= hash_name((char *) sa
, sa_len
);
444 rwlock_wr_lock(&sockaddr_map_lock
);
446 entry
= xzmalloc(sizeof(*entry
));
447 entry
->sa
= xmemdupz(sa
, sa_len
);
448 entry
->sa_len
= sa_len
;
449 entry
->proto
= proto
;
451 pos
= insert_hash(hash
, entry
, &sockaddr_mapper
);
453 entry
->next
= (*pos
);
457 rwlock_unlock(&sockaddr_map_lock
);
462 int try_register_user_by_socket(struct curve25519_struct
*c
,
463 char *src
, size_t slen
, int sock
, int log
)
467 size_t real_len
= 132;
469 struct user_store
*elem
;
470 enum is_user_enum err
;
471 unsigned char auth
[crypto_auth_hmacsha512256_BYTES
];
472 struct taia arrival_taia
;
474 /* assert(132 == clen + sizeof(auth)); */
476 * Check hmac first, if malicious, drop immediately before we
477 * investigate more efforts.
482 taia_now(&arrival_taia
);
484 memcpy(auth
, src
, sizeof(auth
));
487 real_len
-= sizeof(auth
);
489 if (crypto_auth_hmacsha512256_verify(auth
, (unsigned char *) src
,
491 syslog(LOG_ERR
, "Bad packet hmac for id %d! Dropping!\n", sock
);
495 syslog(LOG_INFO
, "Good packet hmac for id %d!\n", sock
);
498 rwlock_rd_lock(&store_lock
);
502 clen
= curve25519_decode(c
, &elem
->proto_inf
,
503 (unsigned char *) src
, real_len
,
504 (unsigned char **) &cbuff
,
511 cbuff
+= crypto_box_zerobytes
;
512 clen
-= crypto_box_zerobytes
;
515 syslog(LOG_INFO
, "Packet decoded sucessfully for id %d!\n", sock
);
517 err
= username_msg_is_user(cbuff
, clen
, elem
->username
,
518 strlen(elem
->username
) + 1);
519 if (err
== USERNAMES_OK
) {
521 syslog(LOG_INFO
, "Found user %s for id %d! Registering ...\n",
522 elem
->username
, sock
);
523 ret
= register_user_by_socket(sock
, &elem
->proto_inf
);
530 rwlock_unlock(&store_lock
);
533 syslog(LOG_ERR
, "User not found! Dropping connection!\n");
538 int try_register_user_by_sockaddr(struct curve25519_struct
*c
,
539 char *src
, size_t slen
,
540 struct sockaddr_storage
*sa
,
541 size_t sa_len
, int log
)
545 struct user_store
*elem
;
547 size_t real_len
= 132;
548 enum is_user_enum err
;
549 unsigned char auth
[crypto_auth_hmacsha512256_BYTES
];
550 struct taia arrival_taia
;
552 /* assert(132 == clen + sizeof(auth)); */
554 * Check hmac first, if malicious, drop immediately before we
555 * investigate more efforts.
560 taia_now(&arrival_taia
);
562 memcpy(auth
, src
, sizeof(auth
));
565 real_len
-= sizeof(auth
);
567 if (crypto_auth_hmacsha512256_verify(auth
, (unsigned char *) src
,
569 syslog(LOG_ERR
, "Got bad packet hmac! Dropping!\n");
573 syslog(LOG_INFO
, "Got good packet hmac!\n");
576 rwlock_rd_lock(&store_lock
);
580 clen
= curve25519_decode(c
, &elem
->proto_inf
,
581 (unsigned char *) src
, real_len
,
582 (unsigned char **) &cbuff
,
589 cbuff
+= crypto_box_zerobytes
;
590 clen
-= crypto_box_zerobytes
;
593 syslog(LOG_INFO
, "Packet decoded sucessfully!\n");
595 err
= username_msg_is_user(cbuff
, clen
, elem
->username
,
596 strlen(elem
->username
) + 1);
597 if (err
== USERNAMES_OK
) {
599 syslog(LOG_INFO
, "Found user %s! Registering ...\n",
601 ret
= register_user_by_sockaddr(sa
, sa_len
,
609 rwlock_unlock(&store_lock
);
612 syslog(LOG_ERR
, "User not found! Dropping connection!\n");
617 int get_user_by_socket(int fd
, struct curve25519_proto
**proto
)
620 struct sock_map_entry
*entry
;
624 rwlock_rd_lock(&sock_map_lock
);
626 entry
= lookup_hash(fd
, &sock_mapper
);
627 while (entry
&& fd
!= entry
->fd
)
629 if (entry
&& fd
== entry
->fd
) {
630 (*proto
) = entry
->proto
;
637 rwlock_unlock(&sock_map_lock
);
642 int get_user_by_sockaddr(struct sockaddr_storage
*sa
, size_t sa_len
,
643 struct curve25519_proto
**proto
)
646 struct sockaddr_map_entry
*entry
;
647 unsigned int hash
= hash_name((char *) sa
, sa_len
);
651 rwlock_rd_lock(&sockaddr_map_lock
);
653 entry
= lookup_hash(hash
, &sockaddr_mapper
);
654 while (entry
&& entry
->sa_len
== sa_len
&&
655 memcmp(sa
, entry
->sa
, entry
->sa_len
))
657 if (entry
&& entry
->sa_len
== sa_len
&&
658 !memcmp(sa
, entry
->sa
, entry
->sa_len
)) {
659 (*proto
) = entry
->proto
;
666 rwlock_unlock(&sockaddr_map_lock
);
671 static struct sock_map_entry
*socket_to_sock_map_entry(int fd
)
673 struct sock_map_entry
*entry
, *ret
= NULL
;
677 rwlock_rd_lock(&sock_map_lock
);
679 entry
= lookup_hash(fd
, &sock_mapper
);
680 while (entry
&& fd
!= entry
->fd
)
682 if (entry
&& fd
== entry
->fd
)
687 rwlock_unlock(&sock_map_lock
);
692 void remove_user_by_socket(int fd
)
694 struct sock_map_entry
*pos
;
695 struct sock_map_entry
*entry
= socket_to_sock_map_entry(fd
);
700 rwlock_wr_lock(&sock_map_lock
);
702 pos
= remove_hash(entry
->fd
, entry
, entry
->next
, &sock_mapper
);
703 while (pos
&& pos
->next
&& pos
->next
!= entry
)
705 if (pos
&& pos
->next
&& pos
->next
== entry
)
706 pos
->next
= entry
->next
;
708 memset(entry
->proto
->enonce
, 0, sizeof(entry
->proto
->enonce
));
709 memset(entry
->proto
->dnonce
, 0, sizeof(entry
->proto
->dnonce
));
716 rwlock_unlock(&sock_map_lock
);
719 static struct sockaddr_map_entry
*
720 sockaddr_to_sockaddr_map_entry(struct sockaddr_storage
*sa
, size_t sa_len
)
722 struct sockaddr_map_entry
*entry
, *ret
= NULL
;
723 unsigned int hash
= hash_name((char *) sa
, sa_len
);
727 rwlock_rd_lock(&sockaddr_map_lock
);
729 entry
= lookup_hash(hash
, &sockaddr_mapper
);
730 while (entry
&& entry
->sa_len
== sa_len
&&
731 memcmp(sa
, entry
->sa
, entry
->sa_len
))
733 if (entry
&& entry
->sa_len
== sa_len
&&
734 !memcmp(sa
, entry
->sa
, entry
->sa_len
))
739 rwlock_unlock(&sockaddr_map_lock
);
744 void remove_user_by_sockaddr(struct sockaddr_storage
*sa
, size_t sa_len
)
746 struct sockaddr_map_entry
*pos
;
747 struct sockaddr_map_entry
*entry
;
748 unsigned int hash
= hash_name((char *) sa
, sa_len
);
750 entry
= sockaddr_to_sockaddr_map_entry(sa
, sa_len
);
754 rwlock_wr_lock(&sockaddr_map_lock
);
756 pos
= remove_hash(hash
, entry
, entry
->next
, &sockaddr_mapper
);
757 while (pos
&& pos
->next
&& pos
->next
!= entry
)
759 if (pos
&& pos
->next
&& pos
->next
== entry
)
760 pos
->next
= entry
->next
;
762 memset(entry
->proto
->enonce
, 0, sizeof(entry
->proto
->enonce
));
763 memset(entry
->proto
->dnonce
, 0, sizeof(entry
->proto
->dnonce
));
771 rwlock_unlock(&sockaddr_map_lock
);