6 * Copyright (c) 2004, Andreas Tonnesen(andreto@olsr.org)
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * * Neither the name of olsrd, olsr.org nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
38 /* Adjustments made to ensure data going out is converted to network
39 * byte ordering. Also, to ensure incoming data is converted before
40 * it is used and before checksums are calculated as well.
41 * Rusty Haddock AE5AE -- for the HSMM-MESH project.
45 * Dynamic linked library for the olsr.org olsr daemon
48 #include "olsrd_secure.h"
62 #include "scheduler.h"
68 #include <openssl/sha.h>
71 #define SCHEME SHA1_INCLUDING_KEY
73 #else /* USE_OPENSSL */
75 /* Homebrewn checksuming */
79 MD5_checksum(const uint8_t * data, const uint16_t data_len, uint8_t * hashbuf)
84 MD5Update(&context, data, data_len);
85 MD5Final(hashbuf, &context);
88 #define CHECKSUM MD5_checksum
89 #define SCHEME MD5_INCLUDING_KEY
91 #endif /* USE_OPENSSL */
98 #define close(x) closesocket(x)
100 #define EWOULDBLOCK WSAEWOULDBLOCK
104 #define OS "GNU/Linux"
105 #endif /* __linux__ */
106 #if defined __FreeBSD__ || defined __FreeBSD_kernel__
108 #endif /* defined __FreeBSD__ || defined __FreeBSD_kernel__ */
111 #define OS "Undefined"
114 static struct timeval now;
118 union olsr_ip_addr addr;
119 /* Timestamp difference */
123 uint32_t valtime; /* Validity time */
124 uint32_t conftime; /* Reconfiguration time */
129 /* Seconds to cache a valid timestamp entry */
130 #define TIMESTAMP_HOLD_TIME 30
132 /* Seconds to cache a not verified timestamp entry */
133 #define EXCHANGE_HOLD_TIME 5
135 static struct stamp timestamps[HASHSIZE];
137 char keyfile[FILENAME_MAX + 1];
140 /* Event function to register with the sceduler */
141 static int send_challenge(struct interface *olsr_if, const union olsr_ip_addr *);
142 static int send_cres(struct interface *olsr_if, union olsr_ip_addr *, union olsr_ip_addr *, uint32_t, struct stamp *);
143 static int send_rres(struct interface *olsr_if, union olsr_ip_addr *, union olsr_ip_addr *, uint32_t);
144 static int parse_challenge(struct interface *olsr_if, char *);
145 static int parse_cres(struct interface *olsr_if, char *);
146 static int parse_rres(char *);
147 static int check_auth(struct interface *olsr_if, char *, int *);
148 static int add_signature(uint8_t *, int *);
149 static int validate_packet(struct interface *olsr_if, const char *, int *);
150 static char *secure_preprocessor(char *packet, struct interface *olsr_if, union olsr_ip_addr *from_addr, int *length);
151 static void timeout_timestamps(void *);
152 static int check_timestamp(struct interface *olsr_if, const union olsr_ip_addr *, time_t);
153 static struct stamp *lookup_timestamp_entry(const union olsr_ip_addr *);
154 static int read_key_from_file(const char *);
157 *Do initialization here
159 *This function is called by the my_init
160 *function in uolsrd_plugin.c
164 secure_plugin_init(void)
168 /* Initialize the timestamp database */
169 for (i = 0; i < HASHSIZE; i++) {
170 timestamps[i].next = ×tamps[i];
171 timestamps[i].prev = ×tamps[i];
173 olsr_printf(1, "Timestamp database initialized\n");
175 if (!strlen(keyfile))
176 strscpy(keyfile, KEYFILE, sizeof(keyfile));
178 i = read_key_from_file(keyfile);
181 olsr_printf(1, "[ENC]Could not read key from file %s!\nExitting!\n\n", keyfile);
185 olsr_printf(1, "[ENC]There was a problem reading key from file %s. Is the key long enough?\nExitting!\n\n", keyfile);
189 /* Register the packet transform function */
190 add_ptf(&add_signature);
192 olsr_preprocessor_add_function(&secure_preprocessor);
194 /* Register timeout - poll every 2 seconds */
195 olsr_start_timer(2 * MSEC_PER_SEC, 0, OLSR_TIMER_PERIODIC, &timeout_timestamps, NULL, 0);
201 plugin_ipc_init(void)
207 * destructor - called at unload
210 secure_plugin_exit(void)
212 olsr_preprocessor_remove_function(&secure_preprocessor);
216 secure_preprocessor(char *packet, struct interface *olsr_if, union olsr_ip_addr *from_addr, int *length)
218 struct olsr *olsr = (struct olsr *)packet;
219 struct ipaddr_str buf;
222 * Check for challenge/response messages
224 check_auth(olsr_if, packet, length);
230 if (!validate_packet(olsr_if, packet, length)) {
231 olsr_printf(1, "[ENC]Rejecting packet from %s\n", olsr_ip_to_string(&buf, from_addr));
235 olsr_printf(1, "[ENC]Packet from %s OK size %d\n", olsr_ip_to_string(&buf, from_addr), *length);
237 /* Fix OLSR packet header */
238 olsr->olsr_packlen = htons(*length);
243 * Check a incoming OLSR packet for
244 * challenge/responses.
245 * They need not be verified as they
246 * are signed in the message.
250 check_auth(struct interface *olsr_if, char *pck, int *size __attribute__ ((unused)))
253 olsr_printf(3, "[ENC]Checking packet for challenge response message...\n");
256 case (TYPE_CHALLENGE):
257 parse_challenge(olsr_if, &pck[4]);
260 case (TYPE_CRESPONSE):
261 parse_cres(olsr_if, &pck[4]);
264 case (TYPE_RRESPONSE):
276 * Packet transform function
277 * Build a SHA-1/MD5 hash of the original message
278 * + the signature message(-digest) + key
280 * Then add the signature message to the packet and
284 add_signature(uint8_t * pck, int *size)
286 struct s_olsrmsg *msg;
290 const uint8_t *sigmsg;
293 olsr_printf(2, "[ENC]Adding signature for packet size %d\n", *size);
296 msg = (struct s_olsrmsg *)ARM_NOWARN_ALIGN(&pck[*size]);
298 ((struct olsr *)pck)->olsr_packlen = htons(*size + sizeof(struct s_olsrmsg));
300 /* Fill packet header */
301 msg->olsr_msgtype = MESSAGE_TYPE;
303 msg->olsr_msgsize = htons(sizeof(struct s_olsrmsg));
304 memcpy(&msg->originator, &olsr_cnf->main_addr, olsr_cnf->ipsize);
307 msg->seqno = htons(get_msg_seqno());
310 msg->sig.type = ONE_CHECKSUM;
311 msg->sig.algorithm = SCHEME;
312 memset(&msg->sig.reserved, 0, 2);
315 msg->sig.timestamp = htonl(now.tv_sec);
317 olsr_printf(3, "[ENC]timestamp: %lld\n", (long long)now.tv_sec);
319 /* Set the new size */
320 *size += sizeof(struct s_olsrmsg);
323 uint8_t checksum_cache[1512 + KEYLENGTH];
324 /* Create packet + key cache */
325 /* First the OLSR packet + signature message - digest */
326 memcpy(checksum_cache, pck, *size - SIGNATURE_SIZE);
328 memcpy(&checksum_cache[*size - SIGNATURE_SIZE], aes_key, KEYLENGTH);
330 /* Create the hash */
331 CHECKSUM(checksum_cache, (*size - SIGNATURE_SIZE) + KEYLENGTH, &pck[*size - SIGNATURE_SIZE]);
335 olsr_printf(1, "Signature message:\n");
338 sigmsg = (uint8_t *) msg;
340 for (i = 0; i < sizeof(struct s_olsrmsg); i++) {
341 olsr_printf(1, " %3i", sigmsg[i]);
344 olsr_printf(1, "\n");
350 olsr_printf(3, "[ENC] Message signed\n");
356 validate_packet(struct interface *olsr_if, const char *pck, int *size)
359 uint8_t sha1_hash[SIGNATURE_SIZE];
360 const struct s_olsrmsg *sig;
366 const uint8_t *sigmsg;
369 /* Find size - signature message */
370 packetsize = *size - sizeof(struct s_olsrmsg);
375 sig = (const struct s_olsrmsg *)CONST_ARM_NOWARN_ALIGN(&pck[packetsize]);
377 //olsr_printf(1, "Size: %d\n", packetsize);
380 olsr_printf(1, "Input message:\n");
383 sigmsg = (const uint8_t *)sig;
385 for (i = 0; i < sizeof(struct s_olsrmsg); i++) {
386 olsr_printf(1, " %3i", sigmsg[i]);
389 olsr_printf(1, "\n");
395 /* Sanity check first */
396 if ((sig->olsr_msgtype != MESSAGE_TYPE) || (sig->olsr_vtime != 0)
397 || (sig->olsr_msgsize != ntohs(sizeof(struct s_olsrmsg))) || (sig->ttl != 1) || (sig->hopcnt != 0)) {
398 olsr_printf(1, "[ENC]Packet not sane!\n");
402 /* Check scheme and type */
403 switch (sig->sig.type) {
405 switch (sig->sig.algorithm) {
407 goto one_checksum_SHA; /* Ahhh... fix this */
415 olsr_printf(1, "[ENC]Unsupported sceme: %d enc: %d!\n", sig->sig.type, sig->sig.algorithm);
418 //olsr_printf(1, "Packet sane...\n");
423 uint8_t checksum_cache[1512 + KEYLENGTH];
424 /* Create packet + key cache */
425 /* First the OLSR packet + signature message - digest */
426 memcpy(checksum_cache, pck, *size - SIGNATURE_SIZE);
428 memcpy(&checksum_cache[*size - SIGNATURE_SIZE], aes_key, KEYLENGTH);
431 CHECKSUM(checksum_cache, *size - SIGNATURE_SIZE + KEYLENGTH, sha1_hash);
435 olsr_printf(1, "Recevied hash:\n");
437 sigmsg = (const uint8_t *)sig->sig.signature;
439 for (i = 0; i < SIGNATURE_SIZE; i++) {
440 olsr_printf(1, " %3i", sigmsg[i]);
442 olsr_printf(1, "\n");
444 olsr_printf(1, "Calculated hash:\n");
448 for (i = 0; i < SIGNATURE_SIZE; i++) {
449 olsr_printf(1, " %3i", sigmsg[i]);
451 olsr_printf(1, "\n");
454 if (memcmp(sha1_hash, sig->sig.signature, SIGNATURE_SIZE) != 0) {
455 olsr_printf(1, "[ENC]Signature missmatch\n");
459 /* Check timestamp */
460 rec_time = ntohl(sig->sig.timestamp);
462 if (!check_timestamp(olsr_if, (const union olsr_ip_addr *)&sig->originator, rec_time)) {
463 struct ipaddr_str buf;
464 olsr_printf(1, "[ENC]Timestamp missmatch in packet from %s!\n",
465 olsr_ip_to_string(&buf, (const union olsr_ip_addr *)&sig->originator));
469 olsr_printf(1, "[ENC]Received timestamp %lld diff: %lld\n", (long long)rec_time, (long long)now.tv_sec - (long long)rec_time);
471 /* Remove signature message */
477 check_timestamp(struct interface *olsr_if, const union olsr_ip_addr *originator, time_t tstamp)
482 entry = lookup_timestamp_entry(originator);
485 /* Initiate timestamp negotiation */
487 send_challenge(olsr_if, originator);
492 if (!entry->validated) {
493 olsr_printf(1, "[ENC]Message from non-validated host!\n");
497 diff = entry->diff - (now.tv_sec - tstamp);
499 olsr_printf(3, "[ENC]Timestamp slack: %d\n", diff);
501 if ((diff > UPPER_DIFF) || (diff < LOWER_DIFF)) {
502 olsr_printf(1, "[ENC]Timestamp scew detected!!\n");
506 /* ok - update diff */
507 entry->diff = ((now.tv_sec - tstamp) + entry->diff) ? ((now.tv_sec - tstamp) + entry->diff) / 2 : 0;
509 olsr_printf(3, "[ENC]Diff set to : %d\n", entry->diff);
511 /* update validtime */
513 entry->valtime = GET_TIMESTAMP(TIMESTAMP_HOLD_TIME * 1000);
519 * Create and send a timestamp
520 * challenge message to new_host
522 * The host is registered in the timestamps
523 * repository with valid=0
527 send_challenge(struct interface *olsr_if, const union olsr_ip_addr *new_host)
529 struct challengemsg cmsg;
531 uint32_t challenge, hash;
532 struct ipaddr_str buf;
534 olsr_printf(1, "[ENC]Building CHALLENGE message\n");
536 /* Set the size including OLSR packet size */
538 challenge = rand() << 16;
541 /* Fill challengemessage */
542 cmsg.olsr_msgtype = TYPE_CHALLENGE;
544 cmsg.olsr_msgsize = htons(sizeof(struct challengemsg));
545 memcpy(&cmsg.originator, &olsr_cnf->main_addr, olsr_cnf->ipsize);
548 cmsg.seqno = htons(get_msg_seqno());
551 memcpy(&cmsg.destination, new_host, olsr_cnf->ipsize);
552 cmsg.challenge = htonl(challenge);
554 olsr_printf(3, "[ENC]Size: %lu\n", (unsigned long)sizeof(struct challengemsg));
557 uint8_t checksum_cache[1512 + KEYLENGTH];
558 /* Create packet + key cache */
559 /* First the OLSR packet + signature message - digest */
560 memcpy(checksum_cache, &cmsg, sizeof(struct challengemsg) - SIGNATURE_SIZE);
562 memcpy(&checksum_cache[sizeof(struct challengemsg) - SIGNATURE_SIZE], aes_key, KEYLENGTH);
564 /* Create the hash */
565 CHECKSUM(checksum_cache, (sizeof(struct challengemsg) - SIGNATURE_SIZE) + KEYLENGTH, cmsg.signature);
567 olsr_printf(3, "[ENC]Sending timestamp request to %s challenge 0x%x\n",
568 olsr_ip_to_string(&buf, new_host), challenge);
571 net_outbuffer_push(olsr_if, &cmsg, sizeof(struct challengemsg));
573 /* Send the request */
576 /* Create new entry */
577 entry = malloc(sizeof(struct stamp));
580 entry->validated = 0;
581 entry->challenge = challenge;
583 memcpy(&entry->addr, new_host, olsr_cnf->ipsize);
585 /* update validtime - not validated */
586 entry->conftime = GET_TIMESTAMP(EXCHANGE_HOLD_TIME * 1000);
588 hash = olsr_ip_hashing(new_host);
591 timestamps[hash].next->prev = entry;
592 entry->next = timestamps[hash].next;
593 timestamps[hash].next = entry;
594 entry->prev = ×tamps[hash];
601 parse_cres(struct interface *olsr_if, char *in_msg)
603 struct c_respmsg *msg;
604 uint8_t sha1_hash[SIGNATURE_SIZE];
606 struct ipaddr_str buf;
608 msg = (struct c_respmsg *)ARM_NOWARN_ALIGN(in_msg);
610 olsr_printf(1, "[ENC]Challenge-response message received\n");
611 olsr_printf(3, "[ENC]To: %s\n", olsr_ip_to_string(&buf, (union olsr_ip_addr *)&msg->destination));
613 if (if_ifwithaddr((union olsr_ip_addr *)&msg->destination) == NULL) {
614 olsr_printf(3, "[ENC]Not for us...\n");
618 olsr_printf(3, "[ENC]Challenge: 0x%lx\n", (unsigned long)ntohl(msg->challenge)); /* ntohl() returns a unsignedlong onwin32 */
620 /* Check signature */
623 uint8_t checksum_cache[1512 + KEYLENGTH];
624 /* Create packet + key cache */
625 /* First the OLSR packet + signature message - digest */
626 memcpy(checksum_cache, msg, sizeof(struct c_respmsg) - SIGNATURE_SIZE);
628 memcpy(&checksum_cache[sizeof(struct c_respmsg) - SIGNATURE_SIZE], aes_key, KEYLENGTH);
630 /* Create the hash */
631 CHECKSUM(checksum_cache, (sizeof(struct c_respmsg) - SIGNATURE_SIZE) + KEYLENGTH, sha1_hash);
634 if (memcmp(sha1_hash, &msg->signature, SIGNATURE_SIZE) != 0) {
635 olsr_printf(1, "[ENC]Signature missmatch in challenge-response!\n");
639 olsr_printf(3, "[ENC]Signature verified\n");
641 /* Now to check the digest from the emitted challenge */
642 if ((entry = lookup_timestamp_entry((const union olsr_ip_addr *)&msg->originator)) == NULL) {
643 olsr_printf(1, "[ENC]Received challenge-response from non-registered node %s!\n",
644 olsr_ip_to_string(&buf, (union olsr_ip_addr *)&msg->originator));
648 /* Generate the digest */
649 olsr_printf(3, "[ENC]Entry-challenge 0x%x\n", entry->challenge);
652 uint8_t checksum_cache[1512 + KEYLENGTH];
653 uint32_t netorder_challenge;
655 /* First the challenge received */
656 /* But we have to calculate our hash with the challenge in
657 * network order just like the remote host did! 6-Jun-2011 AE5AE */
658 netorder_challenge = htonl(entry->challenge);
659 memcpy(checksum_cache, &netorder_challenge, sizeof(uint32_t));
660 /* memcpy(checksum_cache, &entry->challenge, 4); */
662 /* Then the local IP */
663 memcpy(&checksum_cache[sizeof(uint32_t)], &msg->originator, olsr_cnf->ipsize);
665 /* Create the hash */
666 CHECKSUM(checksum_cache, sizeof(uint32_t) + olsr_cnf->ipsize, sha1_hash);
669 if (memcmp(msg->res_sig, sha1_hash, SIGNATURE_SIZE) != 0) {
670 olsr_printf(1, "[ENC]Error in challenge signature from %s!\n",
671 olsr_ip_to_string(&buf, (union olsr_ip_addr *)&msg->originator));
676 olsr_printf(3, "[ENC]Challenge-response signature ok\n");
680 entry->challenge = 0;
681 entry->validated = 1;
683 /* Bring timestamp to host order before arith. 2011/05/31 AE5AE */
684 entry->diff = now.tv_sec - ntohl(msg->timestamp);
686 /* update validtime - validated entry */
687 entry->valtime = GET_TIMESTAMP(TIMESTAMP_HOLD_TIME * 1000);
689 olsr_printf(1, "[ENC]%s registered with diff %d!\n",
690 olsr_ip_to_string(&buf, (union olsr_ip_addr *)&msg->originator),
693 /* Send response-response */
694 send_rres(olsr_if, (union olsr_ip_addr *)&msg->originator,
695 (union olsr_ip_addr *)&msg->destination, msg->challenge);
696 /* (union olsr_ip_addr *)&msg->destination, ntohl(msg->challenge)); */
697 /* Don't give send_rres() the challenge in host order, as the checksum needs to
698 * be calculated in network order. 06-Jun-2011 AE5AE */
704 parse_rres(char *in_msg)
706 struct r_respmsg *msg;
707 uint8_t sha1_hash[SIGNATURE_SIZE];
709 struct ipaddr_str buf;
711 msg = (struct r_respmsg *)ARM_NOWARN_ALIGN(in_msg);
713 olsr_printf(1, "[ENC]Response-response message received\n");
714 olsr_printf(3, "[ENC]To: %s\n", olsr_ip_to_string(&buf, (union olsr_ip_addr *)&msg->destination));
716 if (if_ifwithaddr((union olsr_ip_addr *)&msg->destination) == NULL) {
717 olsr_printf(1, "[ENC]Not for us...\n");
721 /* Check signature */
724 uint8_t checksum_cache[1512 + KEYLENGTH];
725 /* Create packet + key cache */
726 /* First the OLSR packet + signature message - digest */
727 memcpy(checksum_cache, msg, sizeof(struct r_respmsg) - SIGNATURE_SIZE);
729 memcpy(&checksum_cache[sizeof(struct r_respmsg) - SIGNATURE_SIZE], aes_key, KEYLENGTH);
731 /* Create the hash */
732 CHECKSUM(checksum_cache, (sizeof(struct r_respmsg) - SIGNATURE_SIZE) + KEYLENGTH, sha1_hash);
735 if (memcmp(sha1_hash, &msg->signature, SIGNATURE_SIZE) != 0) {
736 olsr_printf(1, "[ENC]Signature missmatch in response-response!\n");
740 olsr_printf(3, "[ENC]Signature verified\n");
742 /* Now to check the digest from the emitted challenge */
743 if ((entry = lookup_timestamp_entry((const union olsr_ip_addr *)&msg->originator)) == NULL) {
744 olsr_printf(1, "[ENC]Received response-response from non-registered node %s!\n",
745 olsr_ip_to_string(&buf, (union olsr_ip_addr *)&msg->originator));
749 /* Generate the digest */
750 olsr_printf(3, "[ENC]Entry-challenge 0x%x\n", entry->challenge);
753 uint8_t checksum_cache[1512 + KEYLENGTH];
754 uint32_t netorder_challenge;
756 /* First the challenge received */
757 /* But we have to calculate our hash with the challenge in network order! 6-Jun-2011 AE5AE */
758 netorder_challenge = htonl(entry->challenge);
759 memcpy(checksum_cache, &netorder_challenge, sizeof(uint32_t));
760 /* memcpy(checksum_cache, &entry->challenge, 4); */
762 /* Then the local IP */
763 memcpy(&checksum_cache[sizeof(uint32_t)], &msg->originator, olsr_cnf->ipsize);
765 /* Create the hash */
766 CHECKSUM(checksum_cache, sizeof(uint32_t) + olsr_cnf->ipsize, sha1_hash);
769 if (memcmp(msg->res_sig, sha1_hash, SIGNATURE_SIZE) != 0) {
770 olsr_printf(1, "[ENC]Error in response signature from %s!\n", olsr_ip_to_string(&buf, (union olsr_ip_addr *)&msg->originator));
775 olsr_printf(3, "[ENC]Challenge-response signature ok\n");
779 entry->challenge = 0;
780 entry->validated = 1;
782 /* Bring timestamp to host order before arith. 2011/05/31 AE5AE */
783 entry->diff = now.tv_sec - ntohl(msg->timestamp);
786 /* update validtime - validated entry */
787 entry->valtime = GET_TIMESTAMP(TIMESTAMP_HOLD_TIME * 1000);
789 olsr_printf(1, "[ENC]%s registered with diff %d!\n", olsr_ip_to_string(&buf, (union olsr_ip_addr *)&msg->originator),
796 parse_challenge(struct interface *olsr_if, char *in_msg)
798 struct challengemsg *msg;
799 uint8_t sha1_hash[SIGNATURE_SIZE];
802 struct ipaddr_str buf;
804 msg = (struct challengemsg *)ARM_NOWARN_ALIGN(in_msg);
806 olsr_printf(1, "[ENC]Challenge message received\n");
807 olsr_printf(3, "[ENC]To: %s\n", olsr_ip_to_string(&buf, (union olsr_ip_addr *)&msg->destination));
809 if (if_ifwithaddr((union olsr_ip_addr *)&msg->destination) == NULL) {
810 olsr_printf(1, "[ENC]Not for us...\n");
814 /* Create entry if not registered */
815 if ((entry = lookup_timestamp_entry((const union olsr_ip_addr *)&msg->originator)) == NULL) {
816 entry = malloc(sizeof(struct stamp));
817 memcpy(&entry->addr, &msg->originator, olsr_cnf->ipsize);
819 hash = olsr_ip_hashing((union olsr_ip_addr *)&msg->originator);
822 timestamps[hash].next->prev = entry;
823 entry->next = timestamps[hash].next;
824 timestamps[hash].next = entry;
825 entry->prev = ×tamps[hash];
827 /* Check configuration timeout */
828 if (!TIMED_OUT(entry->conftime)) {
829 /* If registered - do not accept! */
830 olsr_printf(1, "[ENC]Challenge from registered node...dropping!\n");
833 olsr_printf(1, "[ENC]Challenge from registered node...accepted!\n");
837 olsr_printf(3, "[ENC]Challenge: 0x%lx\n", (unsigned long)ntohl(msg->challenge)); /* ntohl() returns a unsignedlong onwin32 */
839 /* Check signature */
842 uint8_t checksum_cache[1512 + KEYLENGTH];
843 /* Create packet + key cache */
844 /* First the OLSR packet + signature message - digest */
845 memcpy(checksum_cache, msg, sizeof(struct challengemsg) - SIGNATURE_SIZE);
847 memcpy(&checksum_cache[sizeof(struct challengemsg) - SIGNATURE_SIZE], aes_key, KEYLENGTH);
849 /* Create the hash */
850 CHECKSUM(checksum_cache, (sizeof(struct challengemsg) - SIGNATURE_SIZE) + KEYLENGTH, sha1_hash);
852 if (memcmp(sha1_hash, &msg->signature, SIGNATURE_SIZE) != 0) {
853 olsr_printf(1, "[ENC]Signature missmatch in challenge!\n");
857 olsr_printf(3, "[ENC]Signature verified\n");
860 entry->validated = 0;
862 /* update validtime - not validated */
863 entry->conftime = GET_TIMESTAMP(EXCHANGE_HOLD_TIME * 1000);
865 /* Build and send response */
867 send_cres(olsr_if, (union olsr_ip_addr *)&msg->originator,
868 (union olsr_ip_addr *)&msg->destination, msg->challenge, entry);
869 /* (union olsr_ip_addr *)&msg->destination, ntohl(msg->challenge), entry); */
870 /* Don't give send_cres() the challenge in host order, as the checksum needs to
871 * be calculated with network order. 06-Jun-2011 AE5AE */
877 * Build and transmit a challenge response
882 send_cres(struct interface *olsr_if, union olsr_ip_addr *to, union olsr_ip_addr *from, uint32_t chal_in, struct stamp *entry)
884 struct c_respmsg crmsg;
886 struct ipaddr_str buf;
888 olsr_printf(1, "[ENC]Building CRESPONSE message\n");
890 challenge = rand() << 16;
893 entry->challenge = challenge;
895 olsr_printf(3, "[ENC]Challenge-response: 0x%x\n", challenge);
897 /* Fill challengemessage */
898 crmsg.olsr_msgtype = TYPE_CRESPONSE;
899 crmsg.olsr_vtime = 0;
900 crmsg.olsr_msgsize = htons(sizeof(struct c_respmsg));
901 memcpy(&crmsg.originator, &olsr_cnf->main_addr, olsr_cnf->ipsize);
904 crmsg.seqno = htons(get_msg_seqno());
907 /* but swap the byte order to the network order before sending! 2011/05/28 AE5AE */
908 crmsg.timestamp = htonl(now.tv_sec);
910 /* Don't print htonl()'d time, use now.tv_sec 2011/05/31 AE5AE */
911 /* olsr_printf(3, "[ENC]Timestamp %lld\n", (long long)crmsg.timestamp); */
912 olsr_printf(3, "[ENC]Timestamp %lld\n", (long long)now.tv_sec);
916 assert(olsr_cnf->ipsize == sizeof(crmsg.destination));
917 memcpy(&crmsg.destination, to, olsr_cnf->ipsize);
918 crmsg.challenge = htonl(challenge);
920 /* Create digest of received challenge + IP */
923 uint8_t checksum_cache[sizeof(chal_in) + olsr_cnf->ipsize];
924 /* Create packet + key cache */
925 /* First the challenge received */
926 memcpy(checksum_cache, &chal_in, sizeof(chal_in));
927 /* Then the local IP */
928 memcpy(&checksum_cache[sizeof(chal_in)], from, olsr_cnf->ipsize);
930 /* Create the hash */
931 CHECKSUM(checksum_cache, sizeof(chal_in) + olsr_cnf->ipsize, crmsg.res_sig);
934 /* Now create the digest of the message and the key */
937 uint8_t checksum_cache[(sizeof(crmsg) - sizeof(crmsg.signature)) + KEYLENGTH];
938 /* Create packet + key cache */
939 /* First the OLSR packet + signature message - digest */
940 memcpy(checksum_cache, &crmsg, sizeof(crmsg) - sizeof(crmsg.signature));
942 memcpy(&checksum_cache[sizeof(crmsg) - sizeof(crmsg.signature)], aes_key, KEYLENGTH);
944 /* Create the hash */
945 CHECKSUM(checksum_cache, (sizeof(crmsg) - sizeof(crmsg.signature)) + KEYLENGTH, crmsg.signature);
948 olsr_printf(3, "[ENC]Sending challenge response to %s challenge 0x%x\n", olsr_ip_to_string(&buf, to), challenge);
951 net_outbuffer_push(olsr_if, &crmsg, sizeof(struct c_respmsg));
952 /* Send the request */
959 * Build and transmit a response response
964 send_rres(struct interface *olsr_if, union olsr_ip_addr *to, union olsr_ip_addr *from, uint32_t chal_in)
966 struct r_respmsg rrmsg;
967 struct ipaddr_str buf;
969 olsr_printf(1, "[ENC]Building RRESPONSE message\n");
971 /* initialise rrmsg */
972 memset(&rrmsg, 0, sizeof(rrmsg));
974 /* Fill challengemessage */
975 rrmsg.olsr_msgtype = TYPE_RRESPONSE;
976 rrmsg.olsr_msgsize = htons(sizeof(struct r_respmsg));
977 memcpy(&rrmsg.originator, &olsr_cnf->main_addr, olsr_cnf->ipsize);
979 rrmsg.seqno = htons(get_msg_seqno());
982 /* But swap the byte order to the network order! 2011/05/28 AE5AE */
983 rrmsg.timestamp = htonl(now.tv_sec);
986 /* olsr_printf(3, "[ENC]Timestamp %lld\n", (long long)rrmsg.timestamp); */
987 /* don't print htonl()'d time, use now. 2011/05/31 AE5AE */
988 olsr_printf(3, "[ENC]Timestamp %lld\n", (long long)now.tv_sec);
991 assert(olsr_cnf->ipsize == sizeof(rrmsg.destination));
992 memcpy(&rrmsg.destination, to, olsr_cnf->ipsize);
994 /* Create digest of received challenge + IP */
997 uint8_t checksum_cache[sizeof(chal_in) + sizeof(union olsr_ip_addr)];
998 /* Create packet + key cache */
999 /* First the challenge received */
1000 memcpy(checksum_cache, &chal_in, sizeof(chal_in));
1001 /* Then the local IP */
1002 memcpy(&checksum_cache[sizeof(chal_in)], from, olsr_cnf->ipsize);
1004 /* Create the hash */
1005 CHECKSUM(checksum_cache, sizeof(chal_in) + olsr_cnf->ipsize, rrmsg.res_sig);
1008 /* Now create the digest of the message and the key */
1011 uint8_t checksum_cache[(sizeof(rrmsg) - sizeof(rrmsg.signature)) + KEYLENGTH];
1012 /* Create packet + key cache */
1013 /* First the OLSR packet + signature message - digest */
1014 memcpy(checksum_cache, &rrmsg, sizeof(rrmsg) - sizeof(rrmsg.signature));
1016 memcpy(&checksum_cache[sizeof(rrmsg) - sizeof(rrmsg.signature)], aes_key, KEYLENGTH);
1018 /* Create the hash */
1019 CHECKSUM(checksum_cache, (sizeof(rrmsg) - sizeof(rrmsg.signature)) + KEYLENGTH, rrmsg.signature);
1022 olsr_printf(3, "[ENC]Sending response response to %s\n", olsr_ip_to_string(&buf, to));
1025 net_outbuffer_push(olsr_if, &rrmsg, sizeof(struct r_respmsg));
1027 /* Send the request */
1028 net_output(olsr_if);
1033 static struct stamp *
1034 lookup_timestamp_entry(const union olsr_ip_addr *adr)
1037 struct stamp *entry;
1038 struct ipaddr_str buf;
1040 hash = olsr_ip_hashing(adr);
1042 for (entry = timestamps[hash].next; entry != ×tamps[hash]; entry = entry->next) {
1043 if (memcmp(&entry->addr, adr, olsr_cnf->ipsize) == 0) {
1044 olsr_printf(3, "[ENC]Match for %s\n", olsr_ip_to_string(&buf, adr));
1049 olsr_printf(1, "[ENC]No match for %s\n", olsr_ip_to_string(&buf, adr));
1055 *Find timed out entries and delete them
1060 timeout_timestamps(void *foo __attribute__ ((unused)))
1062 struct stamp *tmp_list;
1063 struct stamp *entry_to_delete;
1066 /* Update our local timestamp */
1067 gettimeofday(&now, NULL);
1069 for (idx = 0; idx < HASHSIZE; idx++) {
1070 tmp_list = timestamps[idx].next;
1071 /*Traverse MID list */
1072 while (tmp_list != ×tamps[idx]) {
1073 /*Check if the entry is timed out */
1074 if ((TIMED_OUT(tmp_list->valtime)) && (TIMED_OUT(tmp_list->conftime))) {
1075 struct ipaddr_str buf;
1076 entry_to_delete = tmp_list;
1077 tmp_list = tmp_list->next;
1079 olsr_printf(1, "[ENC]timestamp info for %s timed out.. deleting it\n",
1080 olsr_ip_to_string(&buf, &entry_to_delete->addr));
1083 entry_to_delete->next->prev = entry_to_delete->prev;
1084 entry_to_delete->prev->next = entry_to_delete->next;
1086 free(entry_to_delete);
1088 tmp_list = tmp_list->next;
1096 read_key_from_file(const char *file)
1102 kf = fopen(file, "r");
1104 olsr_printf(1, "[ENC]Reading key from file \"%s\"\n", file);
1107 olsr_printf(1, "[ENC]Could not open keyfile %s!\nError: %s\n", file, strerror(errno));
1111 if (fread(aes_key, 1, keylen, kf) != keylen) {
1112 olsr_printf(1, "[ENC]Could not read key from keyfile %s!\nError: %s\n", file, strerror(errno));
1124 * indent-tabs-mode: nil