Identity.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. namespace ZeroTier {
  37. /*
  38. * This is the hashcash criterion
  39. */
  40. struct _Identity_generate_cond
  41. {
  42. _Identity_generate_cond() throw() {}
  43. _Identity_generate_cond(char *sb) throw() : sha512buf(sb) {}
  44. inline bool operator()(const C25519::Pair &kp) const
  45. throw()
  46. {
  47. SHA512::hash(sha512buf,kp.pub.data,kp.pub.size());
  48. if ((!sha512buf[0])&&(!(sha512buf[1] & 0xf0)))
  49. return true;
  50. return false;
  51. }
  52. char *sha512buf;
  53. };
  54. void Identity::generate()
  55. {
  56. char sha512buf[64];
  57. C25519::Pair kp;
  58. do {
  59. kp = C25519::generateSatisfying(_Identity_generate_cond(sha512buf));
  60. _address.setTo(sha512buf + 59,ZT_ADDRESS_LENGTH); // last 5 bytes are address
  61. } while (_address.isReserved());
  62. _publicKey = kp.pub;
  63. if (!_privateKey)
  64. _privateKey = new C25519::Private();
  65. *_privateKey = kp.priv;
  66. }
  67. bool Identity::locallyValidate(bool doAddressDerivationCheck) const
  68. {
  69. return true;
  70. }
  71. std::string Identity::toString(bool includePrivate) const
  72. {
  73. std::string r;
  74. r.append(_address.toString());
  75. r.append(":0:"); // 0 == IDENTITY_TYPE_C25519
  76. r.append(Utils::hex(_publicKey.data,_publicKey.size()));
  77. if ((_privateKey)&&(includePrivate)) {
  78. r.push_back(':');
  79. r.append(Utils::hex(_privateKey->data,_privateKey->size()));
  80. }
  81. return r;
  82. }
  83. bool Identity::fromString(const char *str)
  84. {
  85. char *saveptr = (char *)0;
  86. char tmp[4096];
  87. if (!Utils::scopy(tmp,sizeof(tmp),str))
  88. return false;
  89. delete _privateKey;
  90. _privateKey = (C25519::Private *)0;
  91. int fno = 0;
  92. for(char *f=Utils::stok(tmp,":",&saveptr);(f);f=Utils::stok((char *)0,":",&saveptr)) {
  93. switch(fno++) {
  94. case 0:
  95. _address = Address(f);
  96. if (_address.isReserved())
  97. return false;
  98. break;
  99. case 1:
  100. if (f[0] != '0')
  101. return false;
  102. break;
  103. case 2:
  104. if (Utils::unhex(f,_publicKey.data,_publicKey.size()) != _publicKey.size())
  105. return false;
  106. break;
  107. case 3:
  108. _privateKey = new C25519::Private();
  109. if (Utils::unhex(f,_privateKey->data,_privateKey->size()) != _privateKey->size())
  110. return false;
  111. break;
  112. default:
  113. return false;
  114. }
  115. }
  116. if (fno < 4)
  117. return false;
  118. return true;
  119. }
  120. } // namespace ZeroTier