123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- /*
- * ZeroTier One - Network Virtualization Everywhere
- * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
- *
- * This program 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.
- *
- * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * --
- *
- * You can be released from the requirements of the license by purchasing
- * a commercial license. Buying such a license is mandatory as soon as you
- * develop commercial closed-source software that incorporates or links
- * directly against ZeroTier software without disclosing the source code
- * of your own application.
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdarg.h>
- #include <time.h>
- #include <sys/stat.h>
- #include "Constants.hpp"
- #ifdef __UNIX_LIKE__
- #include <unistd.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/uio.h>
- #include <dirent.h>
- #endif
- #ifdef __WINDOWS__
- #include <wincrypt.h>
- #endif
- #include "Utils.hpp"
- #include "Mutex.hpp"
- #include "Salsa20.hpp"
- namespace ZeroTier {
- const char Utils::HEXCHARS[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
- // Crazy hack to force memory to be securely zeroed in spite of the best efforts of optimizing compilers.
- static void _Utils_doBurn(volatile uint8_t *ptr,unsigned int len)
- {
- volatile uint8_t *const end = ptr + len;
- while (ptr != end) *(ptr++) = (uint8_t)0;
- }
- static void (*volatile _Utils_doBurn_ptr)(volatile uint8_t *,unsigned int) = _Utils_doBurn;
- void Utils::burn(void *ptr,unsigned int len) { (_Utils_doBurn_ptr)((volatile uint8_t *)ptr,len); }
- static unsigned long _Utils_itoa(unsigned long n,char *s)
- {
- if (n == 0)
- return 0;
- unsigned long pos = _Utils_itoa(n / 10,s);
- if (pos >= 22) // sanity check, should be impossible
- pos = 22;
- s[pos] = '0' + (char)(n % 10);
- return pos + 1;
- }
- char *Utils::decimal(unsigned long n,char s[24])
- {
- if (n == 0) {
- s[0] = '0';
- s[1] = (char)0;
- return s;
- }
- s[_Utils_itoa(n,s)] = (char)0;
- return s;
- }
- void Utils::getSecureRandom(void *buf,unsigned int bytes)
- {
- static Mutex globalLock;
- static Salsa20 s20;
- static bool s20Initialized = false;
- static uint8_t randomBuf[65536];
- static unsigned int randomPtr = sizeof(randomBuf);
- Mutex::Lock _l(globalLock);
- /* Just for posterity we Salsa20 encrypt the result of whatever system
- * CSPRNG we use. There have been several bugs at the OS or OS distribution
- * level in the past that resulted in systematically weak or predictable
- * keys due to random seeding problems. This mitigates that by grabbing
- * a bit of extra entropy and further randomizing the result, and comes
- * at almost no cost and with no real downside if the random source is
- * good. */
- if (!s20Initialized) {
- s20Initialized = true;
- uint64_t s20Key[4];
- s20Key[0] = (uint64_t)time(0); // system clock
- s20Key[1] = (uint64_t)buf; // address of buf
- s20Key[2] = (uint64_t)s20Key; // address of s20Key[]
- s20Key[3] = (uint64_t)&s20; // address of s20
- s20.init(s20Key,s20Key);
- }
- #ifdef __WINDOWS__
- static HCRYPTPROV cryptProvider = NULL;
- for(unsigned int i=0;i<bytes;++i) {
- if (randomPtr >= sizeof(randomBuf)) {
- if (cryptProvider == NULL) {
- if (!CryptAcquireContextA(&cryptProvider,NULL,NULL,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT|CRYPT_SILENT)) {
- fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to obtain WinCrypt context!\r\n");
- exit(1);
- }
- }
- if (!CryptGenRandom(cryptProvider,(DWORD)sizeof(randomBuf),(BYTE *)randomBuf)) {
- fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() CryptGenRandom failed!\r\n");
- exit(1);
- }
- randomPtr = 0;
- s20.crypt12(randomBuf,randomBuf,sizeof(randomBuf));
- s20.init(randomBuf,randomBuf);
- }
- ((uint8_t *)buf)[i] = randomBuf[randomPtr++];
- }
- #else // not __WINDOWS__
- static int devURandomFd = -1;
- if (devURandomFd < 0) {
- devURandomFd = ::open("/dev/urandom",O_RDONLY);
- if (devURandomFd < 0) {
- fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to open /dev/urandom\n");
- exit(1);
- return;
- }
- }
- for(unsigned int i=0;i<bytes;++i) {
- if (randomPtr >= sizeof(randomBuf)) {
- for(;;) {
- if ((int)::read(devURandomFd,randomBuf,sizeof(randomBuf)) != (int)sizeof(randomBuf)) {
- ::close(devURandomFd);
- devURandomFd = ::open("/dev/urandom",O_RDONLY);
- if (devURandomFd < 0) {
- fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to open /dev/urandom\n");
- exit(1);
- return;
- }
- } else break;
- }
- randomPtr = 0;
- s20.crypt12(randomBuf,randomBuf,sizeof(randomBuf));
- s20.init(randomBuf,randomBuf);
- }
- ((uint8_t *)buf)[i] = randomBuf[randomPtr++];
- }
- #endif // __WINDOWS__ or not
- }
- int Utils::b32d(const char *encoded, uint8_t *result, int bufSize)
- {
- int buffer = 0;
- int bitsLeft = 0;
- int count = 0;
- for (const uint8_t *ptr = (const uint8_t *)encoded;count<bufSize && *ptr; ++ptr) {
- uint8_t ch = *ptr;
- if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '-' || ch == '.') {
- continue;
- }
- buffer <<= 5;
- if (ch == '0') {
- ch = 'O';
- } else if (ch == '1') {
- ch = 'L';
- } else if (ch == '8') {
- ch = 'B';
- }
- if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
- ch = (ch & 0x1F) - 1;
- } else if (ch >= '2' && ch <= '7') {
- ch -= '2' - 26;
- } else {
- return -1;
- }
- buffer |= ch;
- bitsLeft += 5;
- if (bitsLeft >= 8) {
- result[count++] = buffer >> (bitsLeft - 8);
- bitsLeft -= 8;
- }
- }
- if (count < bufSize)
- result[count] = (uint8_t)0;
- return count;
- }
- int Utils::b32e(const uint8_t *data,int length,char *result,int bufSize)
- {
- if (length < 0 || length > (1 << 28))
- return -1;
- int count = 0;
- if (length > 0) {
- int buffer = data[0];
- int next = 1;
- int bitsLeft = 8;
- while (count < bufSize && (bitsLeft > 0 || next < length)) {
- if (bitsLeft < 5) {
- if (next < length) {
- buffer <<= 8;
- buffer |= data[next++] & 0xFF;
- bitsLeft += 8;
- } else {
- int pad = 5 - bitsLeft;
- buffer <<= pad;
- bitsLeft += pad;
- }
- }
- int index = 0x1F & (buffer >> (bitsLeft - 5));
- bitsLeft -= 5;
- result[count++] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"[index];
- }
- }
- if (count < bufSize)
- result[count] = (char)0;
- return count;
- }
- } // namespace ZeroTier
|