Packet.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  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. #include "Packet.hpp"
  19. namespace ZeroTier {
  20. 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 };
  21. #ifdef ZT_TRACE
  22. const char *Packet::verbString(Verb v)
  23. throw()
  24. {
  25. switch(v) {
  26. case VERB_NOP: return "NOP";
  27. case VERB_HELLO: return "HELLO";
  28. case VERB_ERROR: return "ERROR";
  29. case VERB_OK: return "OK";
  30. case VERB_WHOIS: return "WHOIS";
  31. case VERB_RENDEZVOUS: return "RENDEZVOUS";
  32. case VERB_FRAME: return "FRAME";
  33. case VERB_EXT_FRAME: return "EXT_FRAME";
  34. case VERB_ECHO: return "ECHO";
  35. case VERB_MULTICAST_LIKE: return "MULTICAST_LIKE";
  36. case VERB_NETWORK_CREDENTIALS: return "NETWORK_CREDENTIALS";
  37. case VERB_NETWORK_CONFIG_REQUEST: return "NETWORK_CONFIG_REQUEST";
  38. case VERB_MULTICAST_GATHER: return "MULTICAST_GATHER";
  39. case VERB_MULTICAST_FRAME: return "MULTICAST_FRAME";
  40. case VERB_PUSH_DIRECT_PATHS: return "PUSH_DIRECT_PATHS";
  41. case VERB_CIRCUIT_TEST: return "CIRCUIT_TEST";
  42. case VERB_CIRCUIT_TEST_REPORT: return "CIRCUIT_TEST_REPORT";
  43. case VERB_REQUEST_PROOF_OF_WORK: return "REQUEST_PROOF_OF_WORK";
  44. }
  45. return "(unknown)";
  46. }
  47. const char *Packet::errorString(ErrorCode e)
  48. throw()
  49. {
  50. switch(e) {
  51. case ERROR_NONE: return "NONE";
  52. case ERROR_INVALID_REQUEST: return "INVALID_REQUEST";
  53. case ERROR_BAD_PROTOCOL_VERSION: return "BAD_PROTOCOL_VERSION";
  54. case ERROR_OBJ_NOT_FOUND: return "OBJECT_NOT_FOUND";
  55. case ERROR_IDENTITY_COLLISION: return "IDENTITY_COLLISION";
  56. case ERROR_UNSUPPORTED_OPERATION: return "UNSUPPORTED_OPERATION";
  57. case ERROR_NETWORK_ACCESS_DENIED_: return "NETWORK_ACCESS_DENIED";
  58. case ERROR_UNWANTED_MULTICAST: return "UNWANTED_MULTICAST";
  59. }
  60. return "(unknown)";
  61. }
  62. #endif // ZT_TRACE
  63. void Packet::armor(const void *key,bool encryptPayload)
  64. {
  65. unsigned char mangledKey[32];
  66. unsigned char macKey[32];
  67. unsigned char mac[16];
  68. const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
  69. unsigned char *const payload = field(ZT_PACKET_IDX_VERB,payloadLen);
  70. // Set flag now, since it affects key mangle function
  71. setCipher(encryptPayload ? ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012 : ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE);
  72. _salsa20MangleKey((const unsigned char *)key,mangledKey);
  73. Salsa20 s20(mangledKey,256,field(ZT_PACKET_IDX_IV,8)/*,ZT_PROTO_SALSA20_ROUNDS*/);
  74. // MAC key is always the first 32 bytes of the Salsa20 key stream
  75. // This is the same construction DJB's NaCl library uses
  76. s20.encrypt12(ZERO_KEY,macKey,sizeof(macKey));
  77. if (encryptPayload)
  78. s20.encrypt12(payload,payload,payloadLen);
  79. Poly1305::compute(mac,payload,payloadLen,macKey);
  80. memcpy(field(ZT_PACKET_IDX_MAC,8),mac,8);
  81. }
  82. bool Packet::dearmor(const void *key)
  83. {
  84. unsigned char mangledKey[32];
  85. unsigned char macKey[32];
  86. unsigned char mac[16];
  87. const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
  88. unsigned char *const payload = field(ZT_PACKET_IDX_VERB,payloadLen);
  89. unsigned int cs = cipher();
  90. if ((cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE)||(cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012)) {
  91. _salsa20MangleKey((const unsigned char *)key,mangledKey);
  92. Salsa20 s20(mangledKey,256,field(ZT_PACKET_IDX_IV,8)/*,ZT_PROTO_SALSA20_ROUNDS*/);
  93. s20.encrypt12(ZERO_KEY,macKey,sizeof(macKey));
  94. Poly1305::compute(mac,payload,payloadLen,macKey);
  95. if (!Utils::secureEq(mac,field(ZT_PACKET_IDX_MAC,8),8))
  96. return false;
  97. if (cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012)
  98. s20.decrypt12(payload,payload,payloadLen);
  99. return true;
  100. } else return false; // unrecognized cipher suite
  101. }
  102. bool Packet::compress()
  103. {
  104. unsigned char buf[ZT_PROTO_MAX_PACKET_LENGTH * 2];
  105. if ((!compressed())&&(size() > (ZT_PACKET_IDX_PAYLOAD + 32))) {
  106. int pl = (int)(size() - ZT_PACKET_IDX_PAYLOAD);
  107. int cl = LZ4_compress((const char *)field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)pl),(char *)buf,pl);
  108. if ((cl > 0)&&(cl < pl)) {
  109. (*this)[ZT_PACKET_IDX_VERB] |= (char)ZT_PROTO_VERB_FLAG_COMPRESSED;
  110. setSize((unsigned int)cl + ZT_PACKET_IDX_PAYLOAD);
  111. memcpy(field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)cl),buf,cl);
  112. return true;
  113. }
  114. }
  115. (*this)[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
  116. return false;
  117. }
  118. bool Packet::uncompress()
  119. {
  120. unsigned char buf[ZT_PROTO_MAX_PACKET_LENGTH];
  121. if ((compressed())&&(size() >= ZT_PROTO_MIN_PACKET_LENGTH)) {
  122. if (size() > ZT_PACKET_IDX_PAYLOAD) {
  123. unsigned int compLen = size() - ZT_PACKET_IDX_PAYLOAD;
  124. int ucl = LZ4_decompress_safe((const char *)field(ZT_PACKET_IDX_PAYLOAD,compLen),(char *)buf,compLen,sizeof(buf));
  125. if ((ucl > 0)&&(ucl <= (int)(capacity() - ZT_PACKET_IDX_PAYLOAD))) {
  126. setSize((unsigned int)ucl + ZT_PACKET_IDX_PAYLOAD);
  127. memcpy(field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)ucl),buf,ucl);
  128. } else return false;
  129. }
  130. (*this)[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
  131. }
  132. return true;
  133. }
  134. } // namespace ZeroTier