2
0

idtool.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <string.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <iostream>
  32. #include "node/Identity.hpp"
  33. #include "node/Utils.hpp"
  34. #include "node/C25519.hpp"
  35. using namespace ZeroTier;
  36. static void printHelp(char *pn)
  37. {
  38. std::cout << "Usage: " << pn << " <command> [<args>]" << std::endl << std::endl;
  39. std::cout << "Commands:" << std::endl;
  40. std::cout << "\tgenerate [<identity.secret>]" << std::endl;
  41. std::cout << "\tvalidate <identity.secret/public>" << std::endl;
  42. std::cout << "\tgetpublic <identity.secret>" << std::endl;
  43. std::cout << "\tsign <identity.secret> <file>" << std::endl;
  44. std::cout << "\tverify <identity.secret/public> <file> <signature>" << std::endl;
  45. }
  46. static Identity getIdFromArg(char *arg)
  47. {
  48. Identity id;
  49. if ((strlen(arg) > 32)&&(arg[10] == ':')) { // identity is a literal on the command line
  50. if (id.fromString(arg))
  51. return id;
  52. } else { // identity is to be read from a file
  53. std::string idser;
  54. if (Utils::readFile(arg,idser)) {
  55. if (id.fromString(idser))
  56. return id;
  57. }
  58. }
  59. return Identity();
  60. }
  61. int main(int argc,char **argv)
  62. {
  63. if (argc < 2) {
  64. printHelp(argv[0]);
  65. return -1;
  66. }
  67. if (!strcmp(argv[1],"generate")) {
  68. Identity id;
  69. id.generate();
  70. std::string idser = id.toString(true);
  71. if (argc >= 3) {
  72. if (!Utils::writeFile(argv[2],idser)) {
  73. std::cerr << "Error writing to " << argv[2] << std::endl;
  74. return -1;
  75. } else std::cout << argv[2] << " written" << std::endl;
  76. } else std::cout << idser;
  77. } else if (!strcmp(argv[1],"validate")) {
  78. if (argc < 3) {
  79. printHelp(argv[0]);
  80. return -1;
  81. }
  82. Identity id = getIdFromArg(argv[2]);
  83. if (!id) {
  84. std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl;
  85. return -1;
  86. }
  87. if (!id.locallyValidate(true)) {
  88. std::cerr << argv[2] << " FAILED validation." << std::endl;
  89. return -1;
  90. } else std::cout << argv[2] << " is a valid identity (full check performed)" << std::endl;
  91. } else if (!strcmp(argv[1],"getpublic")) {
  92. if (argc < 3) {
  93. printHelp(argv[0]);
  94. return -1;
  95. }
  96. Identity id = getIdFromArg(argv[2]);
  97. if (!id) {
  98. std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl;
  99. return -1;
  100. }
  101. std::cout << id.toString(false);
  102. } else if (!strcmp(argv[1],"sign")) {
  103. if (argc < 4) {
  104. printHelp(argv[0]);
  105. return -1;
  106. }
  107. Identity id = getIdFromArg(argv[2]);
  108. if (!id) {
  109. std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl;
  110. return -1;
  111. }
  112. if (!id.hasPrivate()) {
  113. std::cerr << argv[2] << " does not contain a private key (must use private to sign)" << std::endl;
  114. return -1;
  115. }
  116. std::string inf;
  117. if (!Utils::readFile(argv[3],inf)) {
  118. std::cerr << argv[3] << " is not readable" << std::endl;
  119. return -1;
  120. }
  121. C25519::Signature signature = id.sign(inf.data(),inf.length());
  122. std::cout << Utils::hex(signature.data,signature.size());
  123. } else if (!strcmp(argv[1],"verify")) {
  124. if (argc < 4) {
  125. printHelp(argv[0]);
  126. return -1;
  127. }
  128. Identity id = getIdFromArg(argv[2]);
  129. if (!id) {
  130. std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl;
  131. return -1;
  132. }
  133. std::string inf;
  134. if (!Utils::readFile(argv[3],inf)) {
  135. std::cerr << argv[3] << " is not readable" << std::endl;
  136. return -1;
  137. }
  138. std::string signature(Utils::unhex(argv[4]));
  139. if ((signature.length() > ZT_ADDRESS_LENGTH)&&(id.verify(inf.data(),inf.length(),signature.data(),signature.length()))) {
  140. std::cout << argv[3] << " signature valid" << std::endl;
  141. } else {
  142. std::cerr << argv[3] << " signature check FAILED" << std::endl;
  143. return -1;
  144. }
  145. } else {
  146. printHelp(argv[0]);
  147. return -1;
  148. }
  149. return 0;
  150. }