idtool.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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>] [<identity.public>]" << 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. if (argc >= 4) {
  77. idser = id.toString(false);
  78. if (!Utils::writeFile(argv[3],idser)) {
  79. std::cerr << "Error writing to " << argv[3] << std::endl;
  80. return -1;
  81. } else std::cout << argv[3] << " written" << std::endl;
  82. }
  83. } else std::cout << idser;
  84. } else if (!strcmp(argv[1],"validate")) {
  85. if (argc < 3) {
  86. printHelp(argv[0]);
  87. return -1;
  88. }
  89. Identity id = getIdFromArg(argv[2]);
  90. if (!id) {
  91. std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl;
  92. return -1;
  93. }
  94. if (!id.locallyValidate()) {
  95. std::cerr << argv[2] << " FAILED validation." << std::endl;
  96. return -1;
  97. } else std::cout << argv[2] << " is a valid identity (full check performed)" << std::endl;
  98. } else if (!strcmp(argv[1],"getpublic")) {
  99. if (argc < 3) {
  100. printHelp(argv[0]);
  101. return -1;
  102. }
  103. Identity id = getIdFromArg(argv[2]);
  104. if (!id) {
  105. std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl;
  106. return -1;
  107. }
  108. std::cout << id.toString(false);
  109. } else if (!strcmp(argv[1],"sign")) {
  110. if (argc < 4) {
  111. printHelp(argv[0]);
  112. return -1;
  113. }
  114. Identity id = getIdFromArg(argv[2]);
  115. if (!id) {
  116. std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl;
  117. return -1;
  118. }
  119. if (!id.hasPrivate()) {
  120. std::cerr << argv[2] << " does not contain a private key (must use private to sign)" << std::endl;
  121. return -1;
  122. }
  123. std::string inf;
  124. if (!Utils::readFile(argv[3],inf)) {
  125. std::cerr << argv[3] << " is not readable" << std::endl;
  126. return -1;
  127. }
  128. C25519::Signature signature = id.sign(inf.data(),inf.length());
  129. std::cout << Utils::hex(signature.data,signature.size());
  130. } else if (!strcmp(argv[1],"verify")) {
  131. if (argc < 4) {
  132. printHelp(argv[0]);
  133. return -1;
  134. }
  135. Identity id = getIdFromArg(argv[2]);
  136. if (!id) {
  137. std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl;
  138. return -1;
  139. }
  140. std::string inf;
  141. if (!Utils::readFile(argv[3],inf)) {
  142. std::cerr << argv[3] << " is not readable" << std::endl;
  143. return -1;
  144. }
  145. std::string signature(Utils::unhex(argv[4]));
  146. if ((signature.length() > ZT_ADDRESS_LENGTH)&&(id.verify(inf.data(),inf.length(),signature.data(),signature.length()))) {
  147. std::cout << argv[3] << " signature valid" << std::endl;
  148. } else {
  149. std::cerr << argv[3] << " signature check FAILED" << std::endl;
  150. return -1;
  151. }
  152. } else {
  153. printHelp(argv[0]);
  154. return -1;
  155. }
  156. return 0;
  157. }