Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | Copyright (c) 2007, Adobe Systems, Incorporated |
michael@0 | 3 | All rights reserved. |
michael@0 | 4 | |
michael@0 | 5 | Redistribution and use in source and binary forms, with or without |
michael@0 | 6 | modification, are permitted provided that the following conditions are |
michael@0 | 7 | met: |
michael@0 | 8 | |
michael@0 | 9 | * Redistributions of source code must retain the above copyright |
michael@0 | 10 | notice, this list of conditions and the following disclaimer. |
michael@0 | 11 | |
michael@0 | 12 | * Redistributions in binary form must reproduce the above copyright |
michael@0 | 13 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 14 | documentation and/or other materials provided with the distribution. |
michael@0 | 15 | |
michael@0 | 16 | * Neither the name of Adobe Systems, Network Resonance nor the names of its |
michael@0 | 17 | contributors may be used to endorse or promote products derived from |
michael@0 | 18 | this software without specific prior written permission. |
michael@0 | 19 | |
michael@0 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 21 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 24 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 28 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 31 | */ |
michael@0 | 32 | |
michael@0 | 33 | |
michael@0 | 34 | static char *RCSSTRING __UNUSED__="$Id: stun_util.c,v 1.2 2008/04/28 18:21:30 ekr Exp $"; |
michael@0 | 35 | |
michael@0 | 36 | #include <errno.h> |
michael@0 | 37 | #include <csi_platform.h> |
michael@0 | 38 | |
michael@0 | 39 | #ifdef WIN32 |
michael@0 | 40 | #include <winsock2.h> |
michael@0 | 41 | #include <stdlib.h> |
michael@0 | 42 | #include <io.h> |
michael@0 | 43 | #include <time.h> |
michael@0 | 44 | #else /* UNIX */ |
michael@0 | 45 | #include <string.h> |
michael@0 | 46 | #endif /* end UNIX */ |
michael@0 | 47 | #include <assert.h> |
michael@0 | 48 | |
michael@0 | 49 | #include "stun.h" |
michael@0 | 50 | #include "stun_reg.h" |
michael@0 | 51 | #include "registry.h" |
michael@0 | 52 | #include "addrs.h" |
michael@0 | 53 | #include "transport_addr_reg.h" |
michael@0 | 54 | #include "nr_crypto.h" |
michael@0 | 55 | #include "hex.h" |
michael@0 | 56 | |
michael@0 | 57 | |
michael@0 | 58 | int NR_LOG_STUN = 0; |
michael@0 | 59 | |
michael@0 | 60 | int |
michael@0 | 61 | nr_stun_startup(void) |
michael@0 | 62 | { |
michael@0 | 63 | int r,_status; |
michael@0 | 64 | |
michael@0 | 65 | if ((r=r_log_register("stun", &NR_LOG_STUN))) |
michael@0 | 66 | ABORT(r); |
michael@0 | 67 | |
michael@0 | 68 | _status=0; |
michael@0 | 69 | abort: |
michael@0 | 70 | return _status; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | int |
michael@0 | 74 | nr_stun_xor_mapped_address(UINT4 magicCookie, nr_transport_addr *from, nr_transport_addr *to) |
michael@0 | 75 | { |
michael@0 | 76 | int _status; |
michael@0 | 77 | |
michael@0 | 78 | switch (from->ip_version) { |
michael@0 | 79 | case NR_IPV4: |
michael@0 | 80 | nr_ip4_port_to_transport_addr( |
michael@0 | 81 | (ntohl(from->u.addr4.sin_addr.s_addr) ^ magicCookie), |
michael@0 | 82 | (ntohs(from->u.addr4.sin_port) ^ (magicCookie>>16)), |
michael@0 | 83 | from->protocol, to); |
michael@0 | 84 | break; |
michael@0 | 85 | case NR_IPV6: |
michael@0 | 86 | assert(0); |
michael@0 | 87 | ABORT(R_INTERNAL); |
michael@0 | 88 | break; |
michael@0 | 89 | default: |
michael@0 | 90 | assert(0); |
michael@0 | 91 | ABORT(R_INTERNAL); |
michael@0 | 92 | break; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | _status = 0; |
michael@0 | 96 | abort: |
michael@0 | 97 | return _status; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | int |
michael@0 | 101 | nr_stun_find_local_addresses(nr_local_addr addrs[], int maxaddrs, int *count) |
michael@0 | 102 | { |
michael@0 | 103 | int r,_status; |
michael@0 | 104 | NR_registry *children = 0; |
michael@0 | 105 | |
michael@0 | 106 | if ((r=NR_reg_get_child_count(NR_STUN_REG_PREF_ADDRESS_PRFX, (unsigned int*)count))) |
michael@0 | 107 | if (r == R_NOT_FOUND) |
michael@0 | 108 | *count = 0; |
michael@0 | 109 | else |
michael@0 | 110 | ABORT(r); |
michael@0 | 111 | |
michael@0 | 112 | if (*count == 0) { |
michael@0 | 113 | if ((r=nr_stun_get_addrs(addrs, maxaddrs, 1, count))) |
michael@0 | 114 | ABORT(r); |
michael@0 | 115 | |
michael@0 | 116 | goto done; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | if (*count >= maxaddrs) { |
michael@0 | 120 | r_log(NR_LOG_STUN, LOG_INFO, "Address list truncated from %d to %d", *count, maxaddrs); |
michael@0 | 121 | *count = maxaddrs; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | #if 0 |
michael@0 | 125 | if (*count > 0) { |
michael@0 | 126 | /* TODO(ekr@rtfm.com): Commented out 2012-07-26. |
michael@0 | 127 | |
michael@0 | 128 | This code is currently not used in Firefox and needs to be |
michael@0 | 129 | ported to 64-bit */ |
michael@0 | 130 | children = RCALLOC((*count + 10) * sizeof(*children)); |
michael@0 | 131 | if (!children) |
michael@0 | 132 | ABORT(R_NO_MEMORY); |
michael@0 | 133 | |
michael@0 | 134 | assert(sizeof(size_t) == sizeof(*count)); |
michael@0 | 135 | |
michael@0 | 136 | if ((r=NR_reg_get_children(NR_STUN_REG_PREF_ADDRESS_PRFX, children, (size_t)(*count + 10), (size_t*)count))) |
michael@0 | 137 | ABORT(r); |
michael@0 | 138 | |
michael@0 | 139 | for (i = 0; i < *count; ++i) { |
michael@0 | 140 | if ((r=nr_reg_get_transport_addr(children[i], 0, &addrs[i].addr))) |
michael@0 | 141 | ABORT(r); |
michael@0 | 142 | } |
michael@0 | 143 | } |
michael@0 | 144 | #endif |
michael@0 | 145 | |
michael@0 | 146 | done: |
michael@0 | 147 | |
michael@0 | 148 | _status=0; |
michael@0 | 149 | abort: |
michael@0 | 150 | RFREE(children); |
michael@0 | 151 | return _status; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | int |
michael@0 | 155 | nr_stun_different_transaction(UCHAR *msg, int len, nr_stun_message *req) |
michael@0 | 156 | { |
michael@0 | 157 | int _status; |
michael@0 | 158 | nr_stun_message_header header; |
michael@0 | 159 | char reqid[44]; |
michael@0 | 160 | char msgid[44]; |
michael@0 | 161 | int len2; |
michael@0 | 162 | |
michael@0 | 163 | if (sizeof(header) > len) |
michael@0 | 164 | ABORT(R_FAILED); |
michael@0 | 165 | |
michael@0 | 166 | assert(sizeof(header.id) == sizeof(UINT12)); |
michael@0 | 167 | |
michael@0 | 168 | memcpy(&header, msg, sizeof(header)); |
michael@0 | 169 | |
michael@0 | 170 | if (memcmp(&req->header.id, &header.id, sizeof(header.id))) { |
michael@0 | 171 | nr_nbin2hex((UCHAR*)&req->header.id, sizeof(req->header.id), reqid, sizeof(reqid), &len2); |
michael@0 | 172 | nr_nbin2hex((UCHAR*)&header.id, sizeof(header.id), msgid, sizeof(msgid), &len2); |
michael@0 | 173 | r_log(NR_LOG_STUN, LOG_DEBUG, "Mismatched message IDs %s/%s", reqid, msgid); |
michael@0 | 174 | ABORT(R_NOT_FOUND); |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | _status=0; |
michael@0 | 178 | abort: |
michael@0 | 179 | return _status; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | char* |
michael@0 | 183 | nr_stun_msg_type(int type) |
michael@0 | 184 | { |
michael@0 | 185 | char *ret = 0; |
michael@0 | 186 | |
michael@0 | 187 | switch (type) { |
michael@0 | 188 | case NR_STUN_MSG_BINDING_REQUEST: |
michael@0 | 189 | ret = "BINDING-REQUEST"; |
michael@0 | 190 | break; |
michael@0 | 191 | case NR_STUN_MSG_BINDING_INDICATION: |
michael@0 | 192 | ret = "BINDING-INDICATION"; |
michael@0 | 193 | break; |
michael@0 | 194 | case NR_STUN_MSG_BINDING_RESPONSE: |
michael@0 | 195 | ret = "BINDING-RESPONSE"; |
michael@0 | 196 | break; |
michael@0 | 197 | case NR_STUN_MSG_BINDING_ERROR_RESPONSE: |
michael@0 | 198 | ret = "BINDING-ERROR-RESPONSE"; |
michael@0 | 199 | break; |
michael@0 | 200 | |
michael@0 | 201 | #ifdef USE_TURN |
michael@0 | 202 | case NR_STUN_MSG_ALLOCATE_REQUEST: |
michael@0 | 203 | ret = "ALLOCATE-REQUEST"; |
michael@0 | 204 | break; |
michael@0 | 205 | case NR_STUN_MSG_ALLOCATE_RESPONSE: |
michael@0 | 206 | ret = "ALLOCATE-RESPONSE"; |
michael@0 | 207 | break; |
michael@0 | 208 | case NR_STUN_MSG_ALLOCATE_ERROR_RESPONSE: |
michael@0 | 209 | ret = "ALLOCATE-ERROR-RESPONSE"; |
michael@0 | 210 | break; |
michael@0 | 211 | case NR_STUN_MSG_REFRESH_REQUEST: |
michael@0 | 212 | ret = "REFRESH-REQUEST"; |
michael@0 | 213 | break; |
michael@0 | 214 | case NR_STUN_MSG_REFRESH_RESPONSE: |
michael@0 | 215 | ret = "REFRESH-RESPONSE"; |
michael@0 | 216 | break; |
michael@0 | 217 | case NR_STUN_MSG_REFRESH_ERROR_RESPONSE: |
michael@0 | 218 | ret = "REFRESH-ERROR-RESPONSE"; |
michael@0 | 219 | break; |
michael@0 | 220 | case NR_STUN_MSG_SEND_INDICATION: |
michael@0 | 221 | ret = "SEND-INDICATION"; |
michael@0 | 222 | break; |
michael@0 | 223 | case NR_STUN_MSG_DATA_INDICATION: |
michael@0 | 224 | ret = "DATA-INDICATION"; |
michael@0 | 225 | break; |
michael@0 | 226 | case NR_STUN_MSG_PERMISSION_REQUEST: |
michael@0 | 227 | ret = "PERMISSION-REQUEST"; |
michael@0 | 228 | break; |
michael@0 | 229 | case NR_STUN_MSG_PERMISSION_RESPONSE: |
michael@0 | 230 | ret = "PERMISSION-RESPONSE"; |
michael@0 | 231 | break; |
michael@0 | 232 | case NR_STUN_MSG_PERMISSION_ERROR_RESPONSE: |
michael@0 | 233 | ret = "PERMISSION-ERROR-RESPONSE"; |
michael@0 | 234 | break; |
michael@0 | 235 | #endif /* USE_TURN */ |
michael@0 | 236 | |
michael@0 | 237 | default: |
michael@0 | 238 | /* ret remains 0 */ |
michael@0 | 239 | break; |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | return ret; |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | int |
michael@0 | 246 | nr_random_alphanum(char *alphanum, int size) |
michael@0 | 247 | { |
michael@0 | 248 | static char alphanums[256] = { |
michael@0 | 249 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', |
michael@0 | 250 | 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', |
michael@0 | 251 | 'U', 'V', 'W', 'X', 'Y', 'Z', |
michael@0 | 252 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', |
michael@0 | 253 | 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', |
michael@0 | 254 | 'u', 'v', 'w', 'x', 'y', 'z', |
michael@0 | 255 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
michael@0 | 256 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', |
michael@0 | 257 | 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', |
michael@0 | 258 | 'U', 'V', 'W', 'X', 'Y', 'Z', |
michael@0 | 259 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', |
michael@0 | 260 | 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', |
michael@0 | 261 | 'u', 'v', 'w', 'x', 'y', 'z', |
michael@0 | 262 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
michael@0 | 263 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', |
michael@0 | 264 | 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', |
michael@0 | 265 | 'U', 'V', 'W', 'X', 'Y', 'Z', |
michael@0 | 266 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', |
michael@0 | 267 | 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', |
michael@0 | 268 | 'u', 'v', 'w', 'x', 'y', 'z', |
michael@0 | 269 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
michael@0 | 270 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', |
michael@0 | 271 | 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', |
michael@0 | 272 | 'U', 'V', 'W', 'X', 'Y', 'Z', |
michael@0 | 273 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', |
michael@0 | 274 | 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', |
michael@0 | 275 | 'u', 'v', 'w', 'x', 'y', 'z', |
michael@0 | 276 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
michael@0 | 277 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' }; |
michael@0 | 278 | int i; |
michael@0 | 279 | |
michael@0 | 280 | nr_crypto_random_bytes((UCHAR*)alphanum, size); |
michael@0 | 281 | |
michael@0 | 282 | /* now convert from binary to alphanumeric */ |
michael@0 | 283 | for (i = 0; i < size; ++i) |
michael@0 | 284 | alphanum[i] = alphanums[(UCHAR)alphanum[i]]; |
michael@0 | 285 | |
michael@0 | 286 | return 0; |
michael@0 | 287 | } |