mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
parent
f2b6090eca
commit
2c8fb05f6e
|
@ -6,11 +6,11 @@
|
||||||
# - A number of small libraries (.a/.so/...) containing independent components
|
# - A number of small libraries (.a/.so/...) containing independent components
|
||||||
# of toxcore. E.g. the DHT has its own library, and the system/network
|
# of toxcore. E.g. the DHT has its own library, and the system/network
|
||||||
# abstractions are in their own library as well. These libraries are not
|
# abstractions are in their own library as well. These libraries are not
|
||||||
# installed on `make install`. The toxdns, toxav, and toxencryptsave
|
# installed on `make install`. The toxav, and toxencryptsave libraries are
|
||||||
# libraries are also not installed.
|
# also not installed.
|
||||||
# - A number of small programs, statically linked if possible.
|
# - A number of small programs, statically linked if possible.
|
||||||
# - One big library containing all of the toxcore, toxav, toxdns, and
|
# - One big library containing all of the toxcore, toxav, and toxencryptsave
|
||||||
# toxencryptsave code.
|
# code.
|
||||||
#
|
#
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
|
@ -328,15 +328,10 @@ endif()
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
#
|
#
|
||||||
# :: ToxDNS and block encryption libraries
|
# :: Block encryption libraries
|
||||||
#
|
#
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
add_submodule(toxcore toxdns
|
|
||||||
toxdns/toxdns.c)
|
|
||||||
target_link_modules(toxdns toxnetwork)
|
|
||||||
set(toxdns_API_HEADERS ${toxcore_SOURCE_DIR}/toxdns/toxdns.h^tox)
|
|
||||||
|
|
||||||
apidsl(toxencryptsave/toxencryptsave.api.h)
|
apidsl(toxencryptsave/toxencryptsave.api.h)
|
||||||
add_submodule(toxcore toxencryptsave
|
add_submodule(toxcore toxencryptsave
|
||||||
toxencryptsave/toxencryptsave.c
|
toxencryptsave/toxencryptsave.c
|
||||||
|
@ -570,9 +565,6 @@ target_link_modules(DHT_test toxdht)
|
||||||
add_c_executable(Messenger_test testing/Messenger_test.c)
|
add_c_executable(Messenger_test testing/Messenger_test.c)
|
||||||
target_link_modules(Messenger_test toxmessenger)
|
target_link_modules(Messenger_test toxmessenger)
|
||||||
|
|
||||||
add_c_executable(dns3_test testing/dns3_test.c)
|
|
||||||
target_link_modules(dns3_test toxdns)
|
|
||||||
|
|
||||||
if(NOT WIN32)
|
if(NOT WIN32)
|
||||||
add_c_executable(tox_sync testing/tox_sync.c)
|
add_c_executable(tox_sync testing/tox_sync.c)
|
||||||
target_link_modules(tox_sync toxcore)
|
target_link_modules(tox_sync toxcore)
|
||||||
|
|
|
@ -243,7 +243,6 @@ If it worked, you should have all the toxcore dylibs in /usr/local/lib: (besides
|
||||||
$ ls -la /usr/local/lib/libtox*.dylib
|
$ ls -la /usr/local/lib/libtox*.dylib
|
||||||
libtoxav.0.dylib
|
libtoxav.0.dylib
|
||||||
libtoxcore.0.dylib
|
libtoxcore.0.dylib
|
||||||
libtoxdns.0.dylib
|
|
||||||
libtoxencryptsave.0.dylib
|
libtoxencryptsave.0.dylib
|
||||||
to check what CPU architecture they're compiled for:
|
to check what CPU architecture they're compiled for:
|
||||||
$ lipo -i /usr/local/lib/libtoxencryptsave.0.dylib
|
$ lipo -i /usr/local/lib/libtoxencryptsave.0.dylib
|
||||||
|
@ -414,7 +413,6 @@ mkdir tmp
|
||||||
cd tmp
|
cd tmp
|
||||||
$WINDOWS_TOOLCHAIN-ar x ../lib/libtoxcore.a
|
$WINDOWS_TOOLCHAIN-ar x ../lib/libtoxcore.a
|
||||||
$WINDOWS_TOOLCHAIN-ar x ../lib/libtoxav.a
|
$WINDOWS_TOOLCHAIN-ar x ../lib/libtoxav.a
|
||||||
$WINDOWS_TOOLCHAIN-ar x ../lib/libtoxdns.a
|
|
||||||
$WINDOWS_TOOLCHAIN-ar x ../lib/libtoxencryptsave.a
|
$WINDOWS_TOOLCHAIN-ar x ../lib/libtoxencryptsave.a
|
||||||
$WINDOWS_TOOLCHAIN-gcc -Wl,--export-all-symbols -Wl,--out-implib=libtox.dll.a -shared -o libtox.dll *.o ../lib/*.a /usr/$WINDOWS_TOOLCHAIN/lib/libwinpthread.a -liphlpapi -lws2_32 -static-libgcc
|
$WINDOWS_TOOLCHAIN-gcc -Wl,--export-all-symbols -Wl,--out-implib=libtox.dll.a -shared -o libtox.dll *.o ../lib/*.a /usr/$WINDOWS_TOOLCHAIN/lib/libwinpthread.a -liphlpapi -lws2_32 -static-libgcc
|
||||||
```
|
```
|
||||||
|
|
|
@ -17,7 +17,6 @@ LT_LDFLAGS=-avoid-version
|
||||||
endif
|
endif
|
||||||
|
|
||||||
include ../toxcore/Makefile.inc
|
include ../toxcore/Makefile.inc
|
||||||
include ../toxdns/Makefile.inc
|
|
||||||
include ../toxencryptsave/Makefile.inc
|
include ../toxencryptsave/Makefile.inc
|
||||||
include ../toxav/Makefile.inc
|
include ../toxav/Makefile.inc
|
||||||
include ../other/Makefile.inc
|
include ../other/Makefile.inc
|
||||||
|
|
|
@ -7,5 +7,5 @@ Name: libtoxcore
|
||||||
Description: Tox protocol library
|
Description: Tox protocol library
|
||||||
Requires:
|
Requires:
|
||||||
Version: @PACKAGE_VERSION@
|
Version: @PACKAGE_VERSION@
|
||||||
Libs: @NACL_OBJECTS_PKGCONFIG@ -L${libdir} -ltoxcore @NACL_LDFLAGS@ -ltoxdns -ltoxencryptsave @NACL_LIBS@ @LIBS@ @MATH_LDFLAGS@ @PTHREAD_LDFLAGS@
|
Libs: @NACL_OBJECTS_PKGCONFIG@ -L${libdir} -ltoxcore @NACL_LDFLAGS@ -ltoxencryptsave @NACL_LIBS@ @LIBS@ @MATH_LDFLAGS@ @PTHREAD_LDFLAGS@
|
||||||
Cflags: -I${includedir}
|
Cflags: -I${includedir}
|
||||||
|
|
|
@ -8,7 +8,7 @@ This directory can house various tools and utilities.
|
||||||
|
|
||||||
Run from ``toxcore`` directory:
|
Run from ``toxcore`` directory:
|
||||||
```bash
|
```bash
|
||||||
astyle --options=./other/astyle/astylerc ./toxcore/*.c ./toxcore/*.h ./toxdns/*.c ./toxdns/*.h ./testing/*.c ./toxav/*.c ./toxav/*.h ./other/*.c ./other/bootstrap_daemon/*.c ./toxencryptsave/*.c ./toxencryptsave/*.h ./auto_tests/*.c
|
astyle --options=./other/astyle/astylerc ./toxcore/*.c ./toxcore/*.h ./testing/*.c ./toxav/*.c ./toxav/*.h ./other/*.c ./other/bootstrap_daemon/*.c ./toxencryptsave/*.c ./toxencryptsave/*.h ./auto_tests/*.c
|
||||||
```
|
```
|
||||||
|
|
||||||
### For selected file
|
### For selected file
|
||||||
|
|
|
@ -23,8 +23,7 @@ endif
|
||||||
if BUILD_TESTING
|
if BUILD_TESTING
|
||||||
|
|
||||||
noinst_PROGRAMS += DHT_test \
|
noinst_PROGRAMS += DHT_test \
|
||||||
Messenger_test \
|
Messenger_test
|
||||||
dns3_test
|
|
||||||
|
|
||||||
DHT_test_SOURCES = ../testing/DHT_test.c
|
DHT_test_SOURCES = ../testing/DHT_test.c
|
||||||
|
|
||||||
|
@ -56,23 +55,6 @@ Messenger_test_LDADD = $(LIBSODIUM_LDFLAGS) \
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
dns3_test_SOURCES = \
|
|
||||||
../testing/dns3_test.c
|
|
||||||
|
|
||||||
dns3_test_CFLAGS = \
|
|
||||||
$(LIBSODIUM_CFLAGS) \
|
|
||||||
$(NACL_CFLAGS)
|
|
||||||
|
|
||||||
dns3_test_LDADD = \
|
|
||||||
$(LIBSODIUM_LDFLAGS) \
|
|
||||||
$(NACL_LDFLAGS) \
|
|
||||||
libtoxdns.la \
|
|
||||||
libtoxcore.la \
|
|
||||||
$(LIBSODIUM_LIBS) \
|
|
||||||
$(NACL_OBJECTS) \
|
|
||||||
$(NACL_LIBS) \
|
|
||||||
$(WINSOCK2_LIBS)
|
|
||||||
|
|
||||||
if !WIN32
|
if !WIN32
|
||||||
|
|
||||||
noinst_PROGRAMS += tox_sync
|
noinst_PROGRAMS += tox_sync
|
||||||
|
|
|
@ -1,106 +0,0 @@
|
||||||
#include "../toxcore/network.h"
|
|
||||||
#include "../toxcore/tox.h"
|
|
||||||
#include "../toxdns/toxdns.h"
|
|
||||||
#include "misc_tools.c"
|
|
||||||
|
|
||||||
static uint32_t create_packet(uint8_t *packet, uint8_t *string, uint8_t str_len, uint8_t id)
|
|
||||||
{
|
|
||||||
memset(packet, 0, str_len + 13 + 16);
|
|
||||||
packet[0] = id;
|
|
||||||
packet[1] = rand();
|
|
||||||
packet[5] = 1;
|
|
||||||
packet[11] = 1;
|
|
||||||
packet[12] = '.';
|
|
||||||
memcpy(packet + 13, string, str_len);
|
|
||||||
uint32_t i, c = 0;
|
|
||||||
|
|
||||||
for (i = str_len + 12; i != 11; --i) {
|
|
||||||
if (packet[i] == '.') {
|
|
||||||
packet[i] = c;
|
|
||||||
c = 0;
|
|
||||||
} else {
|
|
||||||
++c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
packet[str_len + 13 + 2] = 16;
|
|
||||||
packet[str_len + 13 + 4] = 1;
|
|
||||||
packet[str_len + 13 + 7] = 0x29;
|
|
||||||
packet[str_len + 13 + 8] = 16;
|
|
||||||
packet[str_len + 13 + 12] = 0x80;
|
|
||||||
return str_len + 13 + 16;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
if (argc < 4) {
|
|
||||||
printf("Usage: %s domain domain_public_key queried_username\nEX: %s utox.org D3154F65D28A5B41A05D4AC7E4B39C6B1C233CC857FB365C56E8392737462A12 username\n",
|
|
||||||
argv[0], argv[0]);
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
IP ip = {0};
|
|
||||||
ip.family = TOX_AF_INET;
|
|
||||||
Socket sock = net_socket(ip.family, TOX_SOCK_DGRAM, TOX_PROTO_UDP);
|
|
||||||
|
|
||||||
if (!sock_valid(sock)) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!addr_resolve_or_parse_ip(argv[1], &ip, 0)) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t request_id;
|
|
||||||
uint8_t string[1024] = {0};
|
|
||||||
void *d = tox_dns3_new(hex_string_to_bin(argv[2]));
|
|
||||||
int len = tox_generate_dns3_string(d, string + 1, sizeof(string) - 1, &request_id, (uint8_t *)argv[3], strlen(argv[3]));
|
|
||||||
|
|
||||||
if (len == -1) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
string[0] = '_';
|
|
||||||
memcpy(string + len + 1, "._tox.", sizeof("._tox."));
|
|
||||||
memcpy((char *)(string + len + 1 + sizeof("._tox.") - 1), argv[1], strlen(argv[1]));
|
|
||||||
uint8_t packet[512];
|
|
||||||
uint8_t id = rand();
|
|
||||||
uint32_t p_len = create_packet(packet, string, strlen((char *)string), id);
|
|
||||||
|
|
||||||
IP_Port ip_port;
|
|
||||||
ip_port.port = 53;
|
|
||||||
ip_port.ip = ip;
|
|
||||||
|
|
||||||
if (net_sendto_ip4(sock, (char *)packet, p_len, ip_port) != p_len) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t buffer[512] = {0};
|
|
||||||
int r_len = recv(sock, (char *)buffer, sizeof(buffer), 0);
|
|
||||||
|
|
||||||
if (r_len < (int)p_len) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int i;
|
|
||||||
|
|
||||||
for (i = r_len - 1; i != 0 && buffer[i] != '='; --i) {
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t tox_id[TOX_ADDRESS_SIZE];
|
|
||||||
|
|
||||||
if (tox_decrypt_dns3_TXT(d, tox_id, buffer + i + 1, r_len - (i + 1), request_id) != 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("The Tox id for username %s is:\n", argv[3]);
|
|
||||||
|
|
||||||
//unsigned int i;
|
|
||||||
for (i = 0; i < TOX_ADDRESS_SIZE; ++i) {
|
|
||||||
printf("%02hhX", tox_id[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
|
@ -1411,20 +1411,6 @@ Socket net_socket(int domain, int type, int protocol)
|
||||||
return socket(platform_domain, platform_type, platform_prot);
|
return socket(platform_domain, platform_type, platform_prot);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: Remove, when tox DNS support will be removed.
|
|
||||||
* Used only by dns3_test.c
|
|
||||||
*/
|
|
||||||
size_t net_sendto_ip4(Socket sock, const char *buf, size_t n, IP_Port ip_port)
|
|
||||||
{
|
|
||||||
struct sockaddr_in target;
|
|
||||||
size_t addrsize = sizeof(target);
|
|
||||||
target.sin_family = make_family(ip_port.ip.family);
|
|
||||||
target.sin_port = net_htons(ip_port.port);
|
|
||||||
fill_addr4(ip_port.ip.ip4, &target.sin_addr);
|
|
||||||
|
|
||||||
return (size_t)sendto(sock, buf, n, 0, (struct sockaddr *)&target, addrsize);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t net_htonl(uint32_t hostlong)
|
uint32_t net_htonl(uint32_t hostlong)
|
||||||
{
|
{
|
||||||
return htonl(hostlong);
|
return htonl(hostlong);
|
||||||
|
|
|
@ -403,8 +403,6 @@ void net_freeipport(IP_Port *ip_ports);
|
||||||
*/
|
*/
|
||||||
int bind_to_port(Socket sock, int family, uint16_t port);
|
int bind_to_port(Socket sock, int family, uint16_t port);
|
||||||
|
|
||||||
size_t net_sendto_ip4(Socket sock, const char *buf, size_t n, IP_Port ip_port);
|
|
||||||
|
|
||||||
/* Initialize networking.
|
/* Initialize networking.
|
||||||
* bind to ip and port.
|
* bind to ip and port.
|
||||||
* ip must be in network order EX: 127.0.0.1 = (7F000001).
|
* ip must be in network order EX: 127.0.0.1 = (7F000001).
|
||||||
|
|
|
@ -1,35 +0,0 @@
|
||||||
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 = $(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
|
|
||||||
|
|
||||||
if SET_SO_VERSION
|
|
||||||
|
|
||||||
EXTRA_libtoxdns_la_DEPENDENCIES = ../so.version
|
|
||||||
|
|
||||||
endif
|
|
243
toxdns/toxdns.c
243
toxdns/toxdns.c
|
@ -1,243 +0,0 @@
|
||||||
/*
|
|
||||||
* Tox secure username DNS toxid resolving functions.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Copyright © 2016-2017 The TokTok team.
|
|
||||||
* Copyright © 2013 Tox project.
|
|
||||||
*
|
|
||||||
* This file is part of Tox, the free peer to peer instant messenger.
|
|
||||||
*
|
|
||||||
* 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] = {
|
|
||||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
|
|
||||||
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
|
||||||
'0', '1', '2', '3', '4', '5',
|
|
||||||
};
|
|
||||||
|
|
||||||
#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_PUBLIC_KEY_SIZE];
|
|
||||||
uint8_t temp_sk[CRYPTO_SECRET_KEY_SIZE];
|
|
||||||
uint8_t server_public_key[CRYPTO_PUBLIC_KEY_SIZE];
|
|
||||||
uint8_t shared_key[CRYPTO_SYMMETRIC_KEY_SIZE];
|
|
||||||
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_new_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 = (DNS_Object *)malloc(sizeof(DNS_Object));
|
|
||||||
|
|
||||||
if (d == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(d->server_public_key, server_public_key, CRYPTO_PUBLIC_KEY_SIZE);
|
|
||||||
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 success.
|
|
||||||
* 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_PUBLIC_KEY_SIZE + name_len + CRYPTO_MAC_SIZE);
|
|
||||||
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 = (DNS_Object *)dns3_object;
|
|
||||||
uint8_t buffer[1024];
|
|
||||||
uint8_t nonce[CRYPTO_NONCE_SIZE] = {0};
|
|
||||||
memcpy(nonce, &d->nonce, sizeof(uint32_t));
|
|
||||||
memcpy(buffer, &d->nonce, sizeof(uint32_t));
|
|
||||||
memcpy(buffer + sizeof(uint32_t), d->temp_pk, CRYPTO_PUBLIC_KEY_SIZE);
|
|
||||||
int len = encrypt_data_symmetric(d->shared_key, nonce, name, name_len,
|
|
||||||
buffer + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE);
|
|
||||||
|
|
||||||
if (len == -1) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int total_len = len + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE;
|
|
||||||
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) {
|
|
||||||
// TODO(iphydf): This currently has no access to a logger.
|
|
||||||
LOGGER_ERROR(NULL, "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++;
|
|
||||||
|
|
||||||
if ('A' <= ch && ch <= 'Z') {
|
|
||||||
ch = ch - 'A';
|
|
||||||
} else if ('a' <= ch && ch <= 'z') {
|
|
||||||
ch = ch - 'a';
|
|
||||||
} else if ('0' <= ch && ch <= '5') {
|
|
||||||
ch = ch - '0' + 26;
|
|
||||||
} else {
|
|
||||||
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 = (DNS_Object *)dns3_object;
|
|
||||||
|
|
||||||
if (id_record_len != 87) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if 0
|
|
||||||
|
|
||||||
if (id_record_len > 255 || id_record_len <= (sizeof(uint32_t) + CRYPTO_MAC_SIZE)) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
VLA(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;
|
|
||||||
VLA(uint8_t, data, id_record_len);
|
|
||||||
int length = decode(data, id_record_null);
|
|
||||||
|
|
||||||
if (length == -1) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t nonce[CRYPTO_NONCE_SIZE] = {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;
|
|
||||||
}
|
|
|
@ -1,96 +0,0 @@
|
||||||
/*
|
|
||||||
* Tox secure username DNS toxid resolving functions.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Copyright © 2016-2017 The TokTok team.
|
|
||||||
* Copyright © 2014 Tox project.
|
|
||||||
*
|
|
||||||
* This file is part of Tox, the free peer to peer instant messenger.
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
/* Clients are encouraged to set this as the maximum length names can have. */
|
|
||||||
#define TOXDNS_MAX_RECOMMENDED_NAME_LENGTH 32
|
|
||||||
|
|
||||||
/* 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 success.
|
|
||||||
* 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);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
Loading…
Reference in New Issue
Block a user