summaryrefslogtreecommitdiff
path: root/toxdns/toxdns.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-06-14 11:31:56 -0400
committerirungentoo <irungentoo@gmail.com>2014-06-14 11:31:56 -0400
commitd6a9ce3175bc80f1b04694b3c1415807a1fbab18 (patch)
tree2550e74e7872539aa0b9f4cd3443472018a493f5 /toxdns/toxdns.c
parent9c11c15385ff44609ac8438378a5bcb05d47c572 (diff)
Fixed some issues in toxdns and added a test.
Added request_id. request_id must be obtained with tox_generate_dns3_string, stored, then passed to tox_decrypt_dns3_TXT when we want to decrypt the received response.
Diffstat (limited to 'toxdns/toxdns.c')
-rw-r--r--toxdns/toxdns.c33
1 files changed, 21 insertions, 12 deletions
diff --git a/toxdns/toxdns.c b/toxdns/toxdns.c
index 20a4486c..31269c15 100644
--- a/toxdns/toxdns.c
+++ b/toxdns/toxdns.c
@@ -84,21 +84,25 @@ void tox_dns3_kill(void *dns3_object)
84 free(dns3_object); 84 free(dns3_object);
85} 85}
86 86
87/* Generate a dns3 string of string_max_len used to query the dns server reffered to by to 87/* Generate a dns3 string of string_max_len used to query the dns server referred to by to
88 * dns3_object for a tox id registered to user with name of name_len. 88 * dns3_object for a tox id registered to user with name of name_len.
89 * 89 *
90 * the uint32_t pointed by request_id will be set to the request id which must be passed to
91 * tox_decrypt_dns3_TXT() to correctly decode the response.
92 *
90 * This is what the string returned looks like: 93 * This is what the string returned looks like:
91 * 4haaaaipr1o3mz0bxweox541airydbovqlbju51mb4p0ebxq.rlqdj4kkisbep2ks3fj2nvtmk4daduqiueabmexqva1jc 94 * 4haaaaipr1o3mz0bxweox541airydbovqlbju51mb4p0ebxq.rlqdj4kkisbep2ks3fj2nvtmk4daduqiueabmexqva1jc
92 * 95 *
93 * returns length of string on sucess. 96 * returns length of string on sucess.
94 * returns -1 on failure. 97 * returns -1 on failure.
95 */ 98 */
96int tox_generate_dns3_string(void *dns3_object, uint8_t *string, uint16_t string_max_len, uint8_t *name, 99int tox_generate_dns3_string(void *dns3_object, uint8_t *string, uint16_t string_max_len, uint32_t *request_id,
97 uint8_t name_len) 100 uint8_t *name, uint8_t name_len)
98{ 101{
99#define DOT_INTERVAL (6 * 5) 102#define DOT_INTERVAL (6 * 5)
100 int base = (sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + name_len + crypto_box_MACBYTES); 103 int base = (sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + name_len + crypto_box_MACBYTES);
101 int end_len = ((base * 8) / 5) + (base / DOT_INTERVAL) + !!(base % 5); 104 int end_len = ((base * 8) / 5) + (base / DOT_INTERVAL) + !!(base % 5);
105 end_len -= !(base % DOT_INTERVAL);
102 106
103 if (end_len > string_max_len) 107 if (end_len > string_max_len)
104 return -1; 108 return -1;
@@ -121,14 +125,16 @@ int tox_generate_dns3_string(void *dns3_object, uint8_t *string, uint16_t string
121 uint8_t bits = 0; 125 uint8_t bits = 0;
122 int i; 126 int i;
123 127
124 for (i = 0; i < (total_len / DOT_INTERVAL); ++i) { 128 for (i = !(total_len % DOT_INTERVAL); i < (total_len / DOT_INTERVAL); ++i) {
125 _encode(string, buff, DOT_INTERVAL); 129 _encode(string, buff, DOT_INTERVAL);
126 *string = '.'; 130 *string = '.';
127 ++string; 131 ++string;
128 } 132 }
129 133
130 _encode(string, buff, total_len % DOT_INTERVAL); 134 int left = total_len - (buff - buffer);
135 _encode(string, buff, left);
131#undef DOT_INTERVAL 136#undef DOT_INTERVAL
137 *request_id = d->nonce;
132 ++d->nonce; 138 ++d->nonce;
133 139
134 if (d->nonce == d->nonce_start) { 140 if (d->nonce == d->nonce_start) {
@@ -136,7 +142,7 @@ int tox_generate_dns3_string(void *dns3_object, uint8_t *string, uint16_t string
136 } 142 }
137 143
138 if (end_len != string - old_str) { 144 if (end_len != string - old_str) {
139 printf("tox_generate_dns3_string Fail\n"); 145 printf("tox_generate_dns3_string Fail, %u != %u\n", end_len, string - old_str);
140 return -1; 146 return -1;
141 } 147 }
142 148
@@ -189,18 +195,21 @@ static int decode(uint8_t *dest, uint8_t *src)
189/* Decode and decrypt the id_record returned of length id_record_len into 195/* Decode and decrypt the id_record returned of length id_record_len into
190 * tox_id (needs to be at least TOX_FRIEND_ADDRESS_SIZE). 196 * tox_id (needs to be at least TOX_FRIEND_ADDRESS_SIZE).
191 * 197 *
198 * request_id is the request id given by tox_generate_dns3_string() when creating the request.
199 *
192 * the id_record passed to this function should look somewhat like this: 200 * the id_record passed to this function should look somewhat like this:
193 * 4haaaa2vgcxuycbuctvauik3plsv3d3aadv4zfjfhi3thaizwxinelrvigchv0ah3qjcsx5qhmaksb2lv2hm5cwbtx0yp 201 * 2vgcxuycbuctvauik3plsv3d3aadv4zfjfhi3thaizwxinelrvigchv0ah3qjcsx5qhmaksb2lv2hm5cwbtx0yp
194 * 202 *
195 * returns -1 on failure. 203 * returns -1 on failure.
196 * returns 0 on success. 204 * returns 0 on success.
197 * 205 *
198 */ 206 */
199int tox_decrypt_dns3_TXT(void *dns3_object, uint8_t *tox_id, uint8_t *id_record, uint32_t id_record_len) 207int tox_decrypt_dns3_TXT(void *dns3_object, uint8_t *tox_id, uint8_t *id_record, uint32_t id_record_len,
208 uint32_t request_id)
200{ 209{
201 DNS_Object *d = dns3_object; 210 DNS_Object *d = dns3_object;
202 211
203 if (id_record_len != 93) 212 if (id_record_len != 87)
204 return -1; 213 return -1;
205 214
206 /*if (id_record_len > 255 || id_record_len <= (sizeof(uint32_t) + crypto_box_MACBYTES)) 215 /*if (id_record_len > 255 || id_record_len <= (sizeof(uint32_t) + crypto_box_MACBYTES))
@@ -213,12 +222,12 @@ int tox_decrypt_dns3_TXT(void *dns3_object, uint8_t *tox_id, uint8_t *id_record,
213 return -1; 222 return -1;
214 223
215 uint8_t nonce[crypto_box_NONCEBYTES] = {0}; 224 uint8_t nonce[crypto_box_NONCEBYTES] = {0};
216 memcpy(nonce, data, sizeof(uint32_t)); 225 memcpy(nonce, &request_id, sizeof(uint32_t));
217 nonce[sizeof(uint32_t)] = 1; 226 nonce[sizeof(uint32_t)] = 1;
218 int len = decrypt_data_symmetric(d->shared_key, nonce, data + sizeof(uint32_t), length - sizeof(uint32_t), tox_id); 227 int len = decrypt_data_symmetric(d->shared_key, nonce, data, length, tox_id);
219 228
220 if (len != FRIEND_ADDRESS_SIZE) 229 if (len != FRIEND_ADDRESS_SIZE)
221 return -1; 230 return -1;
222 231
223 return 0; 232 return 0;
224} \ No newline at end of file 233}