summaryrefslogtreecommitdiff
path: root/protocols/Tox/toxcore/toxdns
diff options
context:
space:
mode:
authorKirill Volinsky <mataes2007@gmail.com>2014-08-09 18:03:54 +0000
committerKirill Volinsky <mataes2007@gmail.com>2014-08-09 18:03:54 +0000
commitfc6d64207886ac6921d8f13587c158bfaae76839 (patch)
tree3a8e243ae439ed9a7d1f16b16e159e452e058509 /protocols/Tox/toxcore/toxdns
parentab807bf3e87863ef97412faf83dc2cac697d6a23 (diff)
toxcore sources added
git-svn-id: http://svn.miranda-ng.org/main/trunk@10139 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'protocols/Tox/toxcore/toxdns')
-rw-r--r--protocols/Tox/toxcore/toxdns/Makefile.inc29
-rw-r--r--protocols/Tox/toxcore/toxdns/toxdns.c238
-rw-r--r--protocols/Tox/toxcore/toxdns/toxdns.h85
3 files changed, 352 insertions, 0 deletions
diff --git a/protocols/Tox/toxcore/toxdns/Makefile.inc b/protocols/Tox/toxcore/toxdns/Makefile.inc
new file mode 100644
index 0000000000..689a58a5c8
--- /dev/null
+++ b/protocols/Tox/toxcore/toxdns/Makefile.inc
@@ -0,0 +1,29 @@
+lib_LTLIBRARIES += libtoxdns.la
+
+libtoxdns_la_include_HEADERS = \
+ ../toxdns/toxdns.h
+
+libtoxdns_la_includedir = $(includedir)/tox
+
+libtoxdns_la_SOURCES = ../toxdns/toxdns.h \
+ ../toxdns/toxdns.c
+
+libtoxdns_la_CFLAGS = -I$(top_srcdir) \
+ -I$(top_srcdir)/toxcore \
+ $(LIBSODIUM_CFLAGS) \
+ $(NACL_CFLAGS) \
+ $(PTHREAD_CFLAGS)
+
+libtoxdns_la_LDFLAGS = $(TOXCORE_LT_LDFLAGS) \
+ $(EXTRA_LT_LDFLAGS) \
+ $(LIBSODIUM_LDFLAGS) \
+ $(NACL_LDFLAGS) \
+ $(MATH_LDFLAGS) \
+ $(RT_LIBS) \
+ $(WINSOCK2_LIBS)
+
+libtoxdns_la_LIBADD = $(LIBSODIUM_LIBS) \
+ $(NACL_OBJECTS) \
+ $(NAC_LIBS) \
+ $(PTHREAD_LIBS) \
+ libtoxcore.la
diff --git a/protocols/Tox/toxcore/toxdns/toxdns.c b/protocols/Tox/toxcore/toxdns/toxdns.c
new file mode 100644
index 0000000000..7a7a052dd1
--- /dev/null
+++ b/protocols/Tox/toxcore/toxdns/toxdns.c
@@ -0,0 +1,238 @@
+/* toxdns.c
+ *
+ * Tox secure username DNS toxid resolving functions.
+ *
+ * Copyright (C) 2013 Tox project All Rights Reserved.
+ *
+ * This file is part of Tox.
+ *
+ * Tox is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tox 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 Tox. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "../toxcore/Messenger.h"
+#include "../toxcore/logger.h"
+#include "toxdns.h"
+
+static const char base32[32] = {"abcdefghijklmnopqrstuvwxyz012345"};
+
+#define _encode(a, b, c) \
+{ \
+uint8_t i = 0; \
+ while(i != c) { \
+ *a++ = base32[((b[0] >> bits) | (b[1] << (8 - bits))) & 0x1F]; \
+ bits += 5; \
+ if(bits >= 8) { \
+ bits -= 8; \
+ b++; \
+ i++; \
+ } \
+ } \
+} \
+
+typedef struct {
+ uint8_t temp_pk[crypto_box_PUBLICKEYBYTES];
+ uint8_t temp_sk[crypto_box_SECRETKEYBYTES];
+ uint8_t server_public_key[crypto_box_PUBLICKEYBYTES];
+ uint8_t shared_key[crypto_box_KEYBYTES];
+ uint32_t nonce;
+ uint32_t nonce_start;
+} DNS_Object;
+
+static void dns_new_temp_keys(DNS_Object *d)
+{
+ d->nonce = d->nonce_start = random_int();
+ crypto_box_keypair(d->temp_pk, d->temp_sk);
+ encrypt_precompute(d->server_public_key, d->temp_sk, d->shared_key);
+}
+
+/* Create a new tox_dns3 object for server with server_public_key.
+ *
+ * return Null on failure.
+ * return pointer object on success.
+ */
+void *tox_dns3_new(uint8_t *server_public_key)
+{
+ DNS_Object *d = malloc(sizeof(DNS_Object));
+
+ if (d == NULL)
+ return NULL;
+
+ memcpy(d->server_public_key, server_public_key, crypto_box_PUBLICKEYBYTES);
+ dns_new_temp_keys(d);
+ return d;
+}
+
+/* Destroy the tox dns3 object.
+ */
+void tox_dns3_kill(void *dns3_object)
+{
+ memset(dns3_object, 0, sizeof(DNS_Object));
+ free(dns3_object);
+}
+
+/* Generate a dns3 string of string_max_len used to query the dns server referred to by to
+ * dns3_object for a tox id registered to user with name of name_len.
+ *
+ * the uint32_t pointed by request_id will be set to the request id which must be passed to
+ * tox_decrypt_dns3_TXT() to correctly decode the response.
+ *
+ * This is what the string returned looks like:
+ * 4haaaaipr1o3mz0bxweox541airydbovqlbju51mb4p0ebxq.rlqdj4kkisbep2ks3fj2nvtmk4daduqiueabmexqva1jc
+ *
+ * returns length of string on sucess.
+ * returns -1 on failure.
+ */
+int tox_generate_dns3_string(void *dns3_object, uint8_t *string, uint16_t string_max_len, uint32_t *request_id,
+ uint8_t *name, uint8_t name_len)
+{
+#define DOT_INTERVAL (6 * 5)
+ int base = (sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + name_len + crypto_box_MACBYTES);
+ int end_len = ((base * 8) / 5) + (base / DOT_INTERVAL) + !!(base % 5);
+ end_len -= !(base % DOT_INTERVAL);
+
+ if (end_len > string_max_len)
+ return -1;
+
+ DNS_Object *d = dns3_object;
+ uint8_t buffer[1024];
+ uint8_t nonce[crypto_box_NONCEBYTES] = {0};
+ memcpy(nonce, &d->nonce, sizeof(uint32_t));
+ memcpy(buffer, &d->nonce, sizeof(uint32_t));
+ memcpy(buffer + sizeof(uint32_t), d->temp_pk, crypto_box_PUBLICKEYBYTES);
+ int len = encrypt_data_symmetric(d->shared_key, nonce, name, name_len,
+ buffer + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES);
+
+ if (len == -1)
+ return -1;
+
+ int total_len = len + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES;
+ uint8_t *buff = buffer, *old_str = string;
+ buffer[total_len] = 0;
+ uint8_t bits = 0;
+ int i;
+
+ for (i = !(total_len % DOT_INTERVAL); i < (total_len / DOT_INTERVAL); ++i) {
+ _encode(string, buff, DOT_INTERVAL);
+ *string = '.';
+ ++string;
+ }
+
+ int left = total_len - (buff - buffer);
+ _encode(string, buff, left);
+#undef DOT_INTERVAL
+ *request_id = d->nonce;
+ ++d->nonce;
+
+ if (d->nonce == d->nonce_start) {
+ dns_new_temp_keys(d);
+ }
+
+ if (end_len != string - old_str) {
+ LOGGER_ERROR("tox_generate_dns3_string Fail, %u != %lu\n", end_len, string - old_str);
+ return -1;
+ }
+
+ return string - old_str;
+}
+
+
+static int decode(uint8_t *dest, uint8_t *src)
+{
+ uint8_t *p = src, *op = dest, bits = 0;
+ *op = 0;
+
+ while (*p) {
+ uint8_t ch = *p++;
+
+ switch (ch) {
+ case 'A' ... 'Z': {
+ ch = ch - 'A';
+ break;
+ }
+
+ case 'a' ... 'z': {
+ ch = ch - 'a';
+ break;
+ }
+
+ case '0' ... '5': {
+ ch = ch - '0' + 26;
+ break;
+ }
+
+ default: {
+ return - 1;
+ }
+ }
+
+ *op |= (ch << bits);
+ bits += 5;
+
+ if (bits >= 8) {
+ bits -= 8;
+ ++op;
+ *op = (ch >> (5 - bits));
+ }
+ }
+
+ return op - dest;
+}
+
+/* Decode and decrypt the id_record returned of length id_record_len into
+ * tox_id (needs to be at least TOX_FRIEND_ADDRESS_SIZE).
+ *
+ * request_id is the request id given by tox_generate_dns3_string() when creating the request.
+ *
+ * the id_record passed to this function should look somewhat like this:
+ * 2vgcxuycbuctvauik3plsv3d3aadv4zfjfhi3thaizwxinelrvigchv0ah3qjcsx5qhmaksb2lv2hm5cwbtx0yp
+ *
+ * returns -1 on failure.
+ * returns 0 on success.
+ *
+ */
+int tox_decrypt_dns3_TXT(void *dns3_object, uint8_t *tox_id, uint8_t *id_record, uint32_t id_record_len,
+ uint32_t request_id)
+{
+ DNS_Object *d = dns3_object;
+
+ if (id_record_len != 87)
+ return -1;
+
+ /*if (id_record_len > 255 || id_record_len <= (sizeof(uint32_t) + crypto_box_MACBYTES))
+ return -1;*/
+
+ uint8_t id_record_null[id_record_len + 1];
+ memcpy(id_record_null, id_record, id_record_len);
+ id_record_null[id_record_len] = 0;
+ uint8_t data[id_record_len];
+ int length = decode(data, id_record_null);
+
+ if (length == -1)
+ return -1;
+
+ uint8_t nonce[crypto_box_NONCEBYTES] = {0};
+ memcpy(nonce, &request_id, sizeof(uint32_t));
+ nonce[sizeof(uint32_t)] = 1;
+ int len = decrypt_data_symmetric(d->shared_key, nonce, data, length, tox_id);
+
+ if (len != FRIEND_ADDRESS_SIZE)
+ return -1;
+
+ return 0;
+}
diff --git a/protocols/Tox/toxcore/toxdns/toxdns.h b/protocols/Tox/toxcore/toxdns/toxdns.h
new file mode 100644
index 0000000000..a0cc323b43
--- /dev/null
+++ b/protocols/Tox/toxcore/toxdns/toxdns.h
@@ -0,0 +1,85 @@
+/* toxdns.h
+ *
+ * Tox secure username DNS toxid resolving functions.
+ *
+ * Copyright (C) 2014 Tox project All Rights Reserved.
+ *
+ * This file is part of Tox.
+ *
+ * Tox is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tox 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 Tox. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef TOXDNS_H
+#define TOXDNS_H
+
+#include <stdint.h>
+
+/* How to use this api to make secure tox dns3 requests:
+ *
+ * 1. Get the public key of a server that supports tox dns3.
+ * 2. use tox_dns3_new() to create a new object to create DNS requests
+ * and handle responses for that server.
+ * 3. Use tox_generate_dns3_string() to generate a string based on the name we want to query and a request_id
+ * that must be stored somewhere for when we want to decrypt the response.
+ * 4. take the string and use it for your DNS request like this:
+ * _4haaaaipr1o3mz0bxweox541airydbovqlbju51mb4p0ebxq.rlqdj4kkisbep2ks3fj2nvtmk4daduqiueabmexqva1jc._tox.utox.org
+ * 5. The TXT in the DNS you receive should look like this:
+ * v=tox3;id=2vgcxuycbuctvauik3plsv3d3aadv4zfjfhi3thaizwxinelrvigchv0ah3qjcsx5qhmaksb2lv2hm5cwbtx0yp
+ * 6. Take the id string and use it with tox_decrypt_dns3_TXT() and the request_id corresponding to the
+ * request we stored earlier to get the Tox id returned by the DNS server.
+ */
+
+/* Create a new tox_dns3 object for server with server_public_key of size TOX_CLIENT_ID_SIZE.
+ *
+ * return Null on failure.
+ * return pointer object on success.
+ */
+void *tox_dns3_new(uint8_t *server_public_key);
+
+/* Destroy the tox dns3 object.
+ */
+void tox_dns3_kill(void *dns3_object);
+
+/* Generate a dns3 string of string_max_len used to query the dns server referred to by to
+ * dns3_object for a tox id registered to user with name of name_len.
+ *
+ * the uint32_t pointed by request_id will be set to the request id which must be passed to
+ * tox_decrypt_dns3_TXT() to correctly decode the response.
+ *
+ * This is what the string returned looks like:
+ * 4haaaaipr1o3mz0bxweox541airydbovqlbju51mb4p0ebxq.rlqdj4kkisbep2ks3fj2nvtmk4daduqiueabmexqva1jc
+ *
+ * returns length of string on sucess.
+ * returns -1 on failure.
+ */
+int tox_generate_dns3_string(void *dns3_object, uint8_t *string, uint16_t string_max_len, uint32_t *request_id,
+ uint8_t *name, uint8_t name_len);
+
+/* Decode and decrypt the id_record returned of length id_record_len into
+ * tox_id (needs to be at least TOX_FRIEND_ADDRESS_SIZE).
+ *
+ * request_id is the request id given by tox_generate_dns3_string() when creating the request.
+ *
+ * the id_record passed to this function should look somewhat like this:
+ * 2vgcxuycbuctvauik3plsv3d3aadv4zfjfhi3thaizwxinelrvigchv0ah3qjcsx5qhmaksb2lv2hm5cwbtx0yp
+ *
+ * returns -1 on failure.
+ * returns 0 on success.
+ *
+ */
+int tox_decrypt_dns3_TXT(void *dns3_object, uint8_t *tox_id, uint8_t *id_record, uint32_t id_record_len,
+ uint32_t request_id);
+
+#endif