Packet.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include "Packet.hpp"
  28. namespace ZeroTier {
  29. const unsigned char Packet::ZERO_KEY[32] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  30. const char *Packet::verbString(Verb v)
  31. throw()
  32. {
  33. switch(v) {
  34. case VERB_NOP: return "NOP";
  35. case VERB_HELLO: return "HELLO";
  36. case VERB_ERROR: return "ERROR";
  37. case VERB_OK: return "OK";
  38. case VERB_WHOIS: return "WHOIS";
  39. case VERB_RENDEZVOUS: return "RENDEZVOUS";
  40. case VERB_FRAME: return "FRAME";
  41. case VERB_EXT_FRAME: return "EXT_FRAME";
  42. case VERB_P5_MULTICAST_FRAME: return "P5_MULTICAST_FRAME";
  43. case VERB_MULTICAST_LIKE: return "MULTICAST_LIKE";
  44. case VERB_NETWORK_MEMBERSHIP_CERTIFICATE: return "NETWORK_MEMBERSHIP_CERTIFICATE";
  45. case VERB_NETWORK_CONFIG_REQUEST: return "NETWORK_CONFIG_REQUEST";
  46. case VERB_NETWORK_CONFIG_REFRESH: return "NETWORK_CONFIG_REFRESH";
  47. case VERB_MULTICAST_GATHER: return "MULTICAST_GATHER";
  48. case VERB_MULTICAST_FRAME: return "MULTICAST_FRAME";
  49. }
  50. return "(unknown)";
  51. }
  52. const char *Packet::errorString(ErrorCode e)
  53. throw()
  54. {
  55. switch(e) {
  56. case ERROR_NONE: return "NONE";
  57. case ERROR_INVALID_REQUEST: return "INVALID_REQUEST";
  58. case ERROR_BAD_PROTOCOL_VERSION: return "BAD_PROTOCOL_VERSION";
  59. case ERROR_OBJ_NOT_FOUND: return "OBJECT_NOT_FOUND";
  60. case ERROR_IDENTITY_COLLISION: return "IDENTITY_COLLISION";
  61. case ERROR_UNSUPPORTED_OPERATION: return "UNSUPPORTED_OPERATION";
  62. case ERROR_NEED_MEMBERSHIP_CERTIFICATE: return "NEED_MEMBERSHIP_CERTIFICATE";
  63. case ERROR_NETWORK_ACCESS_DENIED_: return "NETWORK_ACCESS_DENIED";
  64. case ERROR_UNWANTED_MULTICAST: return "UNWANTED_MULTICAST";
  65. }
  66. return "(unknown)";
  67. }
  68. void Packet::armor(const void *key,bool encryptPayload)
  69. {
  70. unsigned char mangledKey[32];
  71. unsigned char macKey[32];
  72. unsigned char mac[16];
  73. const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
  74. unsigned char *const payload = field(ZT_PACKET_IDX_VERB,payloadLen);
  75. // Set flag now, since it affects key mangle function
  76. setCipher(encryptPayload ? ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012 : ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE);
  77. _salsa20MangleKey((const unsigned char *)key,mangledKey);
  78. Salsa20 s20(mangledKey,256,field(ZT_PACKET_IDX_IV,8),ZT_PROTO_SALSA20_ROUNDS);
  79. // MAC key is always the first 32 bytes of the Salsa20 key stream
  80. // This is the same construction DJB's NaCl library uses
  81. s20.encrypt(ZERO_KEY,macKey,sizeof(macKey));
  82. if (encryptPayload)
  83. s20.encrypt(payload,payload,payloadLen);
  84. Poly1305::compute(mac,payload,payloadLen,macKey);
  85. memcpy(field(ZT_PACKET_IDX_MAC,8),mac,8);
  86. }
  87. bool Packet::dearmor(const void *key)
  88. {
  89. unsigned char mangledKey[32];
  90. unsigned char macKey[32];
  91. unsigned char mac[16];
  92. const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
  93. unsigned char *const payload = field(ZT_PACKET_IDX_VERB,payloadLen);
  94. unsigned int cs = cipher();
  95. if ((cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE)||(cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012)) {
  96. _salsa20MangleKey((const unsigned char *)key,mangledKey);
  97. Salsa20 s20(mangledKey,256,field(ZT_PACKET_IDX_IV,8),ZT_PROTO_SALSA20_ROUNDS);
  98. s20.encrypt(ZERO_KEY,macKey,sizeof(macKey));
  99. Poly1305::compute(mac,payload,payloadLen,macKey);
  100. if (!Utils::secureEq(mac,field(ZT_PACKET_IDX_MAC,8),8))
  101. return false;
  102. if (cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012)
  103. s20.decrypt(payload,payload,payloadLen);
  104. return true;
  105. } else if (cs == ZT_PROTO_CIPHER_SUITE__C25519_AES256_GCM) {
  106. return false; // not implemented yet
  107. } else return false; // unrecognized cipher suite
  108. }
  109. bool Packet::compress()
  110. {
  111. unsigned char buf[ZT_PROTO_MAX_PACKET_LENGTH * 2];
  112. if ((!compressed())&&(size() > (ZT_PACKET_IDX_PAYLOAD + 32))) {
  113. int pl = (int)(size() - ZT_PACKET_IDX_PAYLOAD);
  114. int cl = LZ4_compress((const char *)field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)pl),(char *)buf,pl);
  115. if ((cl > 0)&&(cl < pl)) {
  116. (*this)[ZT_PACKET_IDX_VERB] |= (char)ZT_PROTO_VERB_FLAG_COMPRESSED;
  117. setSize((unsigned int)cl + ZT_PACKET_IDX_PAYLOAD);
  118. memcpy(field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)cl),buf,cl);
  119. return true;
  120. }
  121. }
  122. (*this)[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
  123. return false;
  124. }
  125. bool Packet::uncompress()
  126. {
  127. unsigned char buf[ZT_PROTO_MAX_PACKET_LENGTH];
  128. if ((compressed())&&(size() >= ZT_PROTO_MIN_PACKET_LENGTH)) {
  129. if (size() > ZT_PACKET_IDX_PAYLOAD) {
  130. unsigned int compLen = size() - ZT_PACKET_IDX_PAYLOAD;
  131. int ucl = LZ4_decompress_safe((const char *)field(ZT_PACKET_IDX_PAYLOAD,compLen),(char *)buf,compLen,sizeof(buf));
  132. if ((ucl > 0)&&(ucl <= (int)(capacity() - ZT_PACKET_IDX_PAYLOAD))) {
  133. setSize((unsigned int)ucl + ZT_PACKET_IDX_PAYLOAD);
  134. memcpy(field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)ucl),buf,ucl);
  135. } else return false;
  136. }
  137. (*this)[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
  138. }
  139. return true;
  140. }
  141. } // namespace ZeroTier