idtool.cpp 4.8 KB

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