Identity.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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 <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdint.h>
  31. #include "Constants.hpp"
  32. #include "Identity.hpp"
  33. #include "SHA512.hpp"
  34. #include "Salsa20.hpp"
  35. #include "Utils.hpp"
  36. #define ZT_IDENTITY_SHA_BYTE1_MASK 0xf8
  37. namespace ZeroTier {
  38. /*
  39. * This is the hashcash criterion
  40. */
  41. struct _Identity_generate_cond
  42. {
  43. _Identity_generate_cond() throw() {}
  44. _Identity_generate_cond(char *sb) throw() : sha512buf(sb) {}
  45. inline bool operator()(const C25519::Pair &kp) const
  46. throw()
  47. {
  48. SHA512::hash(sha512buf,kp.pub.data,kp.pub.size());
  49. if ((!sha512buf[0])&&(!(sha512buf[1] & ZT_IDENTITY_SHA_BYTE1_MASK)))
  50. return true;
  51. return false;
  52. }
  53. char *sha512buf;
  54. };
  55. void Identity::generate()
  56. {
  57. char sha512buf[64];
  58. C25519::Pair kp;
  59. do {
  60. kp = C25519::generateSatisfying(_Identity_generate_cond(sha512buf));
  61. _address.setTo(sha512buf + 59,ZT_ADDRESS_LENGTH); // last 5 bytes are address
  62. } while (_address.isReserved());
  63. _publicKey = kp.pub;
  64. if (!_privateKey)
  65. _privateKey = new C25519::Private();
  66. *_privateKey = kp.priv;
  67. }
  68. bool Identity::locallyValidate() const
  69. {
  70. char sha512buf[64];
  71. char addrb[5];
  72. _address.copyTo(addrb,5);
  73. SHA512::hash(sha512buf,_publicKey.data,_publicKey.size());
  74. return (
  75. (!sha512buf[0])&&
  76. (!(sha512buf[1] & ZT_IDENTITY_SHA_BYTE1_MASK))&&
  77. (sha512buf[59] == addrb[0])&&
  78. (sha512buf[60] == addrb[1])&&
  79. (sha512buf[61] == addrb[2])&&
  80. (sha512buf[62] == addrb[3])&&
  81. (sha512buf[63] == addrb[4]));
  82. }
  83. std::string Identity::toString(bool includePrivate) const
  84. {
  85. std::string r;
  86. r.append(_address.toString());
  87. r.append(":0:"); // 0 == IDENTITY_TYPE_C25519
  88. r.append(Utils::hex(_publicKey.data,_publicKey.size()));
  89. if ((_privateKey)&&(includePrivate)) {
  90. r.push_back(':');
  91. r.append(Utils::hex(_privateKey->data,_privateKey->size()));
  92. }
  93. return r;
  94. }
  95. bool Identity::fromString(const char *str)
  96. {
  97. char *saveptr = (char *)0;
  98. char tmp[4096];
  99. if (!Utils::scopy(tmp,sizeof(tmp),str))
  100. return false;
  101. delete _privateKey;
  102. _privateKey = (C25519::Private *)0;
  103. int fno = 0;
  104. for(char *f=Utils::stok(tmp,":",&saveptr);(f);f=Utils::stok((char *)0,":",&saveptr)) {
  105. switch(fno++) {
  106. case 0:
  107. _address = Address(f);
  108. if (_address.isReserved())
  109. return false;
  110. break;
  111. case 1:
  112. if (f[0] != '0')
  113. return false;
  114. break;
  115. case 2:
  116. if (Utils::unhex(f,_publicKey.data,_publicKey.size()) != _publicKey.size())
  117. return false;
  118. break;
  119. case 3:
  120. _privateKey = new C25519::Private();
  121. if (Utils::unhex(f,_privateKey->data,_privateKey->size()) != _privateKey->size())
  122. return false;
  123. break;
  124. default:
  125. return false;
  126. }
  127. }
  128. if (fno < 4)
  129. return false;
  130. return true;
  131. }
  132. } // namespace ZeroTier