123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- #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' };
- static void _Utils_doBurn(char *ptr,unsigned int len)
- {
- for(unsigned int i=0;i<len;++i)
- ptr[i] = (char)0;
- }
- void (*volatile _Utils_doBurn_ptr)(char *,unsigned int) = _Utils_doBurn;
- void Utils::burn(void *ptr,unsigned int len)
- throw()
- {
-
-
-
-
- (_Utils_doBurn_ptr)((char *)ptr,len);
- }
- std::string Utils::hex(const void *data,unsigned int len)
- {
- std::string r;
- r.reserve(len * 2);
- for(unsigned int i=0;i<len;++i) {
- r.push_back(HEXCHARS[(((const unsigned char *)data)[i] & 0xf0) >> 4]);
- r.push_back(HEXCHARS[((const unsigned char *)data)[i] & 0x0f]);
- }
- return r;
- }
- std::string Utils::unhex(const char *hex,unsigned int maxlen)
- {
- int n = 1;
- unsigned char c,b = 0;
- const char *eof = hex + maxlen;
- std::string r;
- if (!maxlen)
- return r;
- while ((c = (unsigned char)*(hex++))) {
- if ((c >= 48)&&(c <= 57)) {
- if ((n ^= 1))
- r.push_back((char)(b | (c - 48)));
- else b = (c - 48) << 4;
- } else if ((c >= 65)&&(c <= 70)) {
- if ((n ^= 1))
- r.push_back((char)(b | (c - (65 - 10))));
- else b = (c - (65 - 10)) << 4;
- } else if ((c >= 97)&&(c <= 102)) {
- if ((n ^= 1))
- r.push_back((char)(b | (c - (97 - 10))));
- else b = (c - (97 - 10)) << 4;
- }
- if (hex == eof)
- break;
- }
- return r;
- }
- unsigned int Utils::unhex(const char *hex,unsigned int maxlen,void *buf,unsigned int len)
- {
- int n = 1;
- unsigned char c,b = 0;
- unsigned int l = 0;
- const char *eof = hex + maxlen;
- if (!maxlen)
- return 0;
- while ((c = (unsigned char)*(hex++))) {
- if ((c >= 48)&&(c <= 57)) {
- if ((n ^= 1)) {
- if (l >= len) break;
- ((unsigned char *)buf)[l++] = (b | (c - 48));
- } else b = (c - 48) << 4;
- } else if ((c >= 65)&&(c <= 70)) {
- if ((n ^= 1)) {
- if (l >= len) break;
- ((unsigned char *)buf)[l++] = (b | (c - (65 - 10)));
- } else b = (c - (65 - 10)) << 4;
- } else if ((c >= 97)&&(c <= 102)) {
- if ((n ^= 1)) {
- if (l >= len) break;
- ((unsigned char *)buf)[l++] = (b | (c - (97 - 10)));
- } else b = (c - (97 - 10)) << 4;
- }
- if (hex == eof)
- break;
- }
- return l;
- }
- void Utils::getSecureRandom(void *buf,unsigned int bytes)
- {
- static Mutex globalLock;
- static Salsa20 s20;
- static bool s20Initialized = false;
- Mutex::Lock _l(globalLock);
-
- if (!s20Initialized) {
- s20Initialized = true;
- uint64_t s20Key[4];
- s20Key[0] = (uint64_t)time(0);
- s20Key[1] = (uint64_t)buf;
- s20Key[2] = (uint64_t)s20Key;
- s20Key[3] = (uint64_t)&s20;
- s20.init(s20Key,256,s20Key);
- }
- #ifdef __WINDOWS__
- static HCRYPTPROV cryptProvider = NULL;
- 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);
- return;
- }
- }
- if (!CryptGenRandom(cryptProvider,(DWORD)bytes,(BYTE *)buf)) {
- fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() CryptGenRandom failed!\r\n");
- exit(1);
- }
- #else
- static char randomBuf[131072];
- static unsigned int randomPtr = sizeof(randomBuf);
- 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;
- }
- ((char *)buf)[i] = randomBuf[randomPtr++];
- }
- #endif
- s20.encrypt12(buf,buf,bytes);
- }
- std::vector<std::string> Utils::split(const char *s,const char *const sep,const char *esc,const char *quot)
- {
- std::vector<std::string> fields;
- std::string buf;
- if (!esc)
- esc = "";
- if (!quot)
- quot = "";
- bool escapeState = false;
- char quoteState = 0;
- while (*s) {
- if (escapeState) {
- escapeState = false;
- buf.push_back(*s);
- } else if (quoteState) {
- if (*s == quoteState) {
- quoteState = 0;
- fields.push_back(buf);
- buf.clear();
- } else buf.push_back(*s);
- } else {
- const char *quotTmp;
- if (strchr(esc,*s))
- escapeState = true;
- else if ((buf.size() <= 0)&&((quotTmp = strchr(quot,*s))))
- quoteState = *quotTmp;
- else if (strchr(sep,*s)) {
- if (buf.size() > 0) {
- fields.push_back(buf);
- buf.clear();
- }
- } else buf.push_back(*s);
- }
- ++s;
- }
- if (buf.size())
- fields.push_back(buf);
- return fields;
- }
- unsigned int Utils::snprintf(char *buf,unsigned int len,const char *fmt,...)
- throw(std::length_error)
- {
- va_list ap;
- va_start(ap,fmt);
- int n = (int)vsnprintf(buf,len,fmt,ap);
- va_end(ap);
- if ((n >= (int)len)||(n < 0)) {
- if (len)
- buf[len - 1] = (char)0;
- throw std::length_error("buf[] overflow in Utils::snprintf");
- }
- return (unsigned int)n;
- }
- }
|