idtool.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. std::cout << "\tencrypt <identity.secret> <identity.public (recipient)> [<file>] [<outfile>]" << std::endl;
  45. std::cout << "\tdecrypt <identity.secret> <identity.public (sender)> [<file>] [<outfile>]" << std::endl;
  46. }
  47. static Identity getIdFromArg(char *arg)
  48. {
  49. Identity id;
  50. if ((strlen(arg) > 32)&&(arg[10] == ':')) { // identity is a literal on the command line
  51. if (id.fromString(arg))
  52. return id;
  53. } else { // identity is to be read from a file
  54. std::string idser;
  55. if (Utils::readFile(arg,idser)) {
  56. if (id.fromString(idser))
  57. return id;
  58. }
  59. }
  60. return Identity();
  61. }
  62. int main(int argc,char **argv)
  63. {
  64. if (argc < 2) {
  65. printHelp(argv[0]);
  66. return -1;
  67. }
  68. if (!strcmp(argv[1],"generate")) {
  69. Identity id;
  70. id.generate();
  71. std::string idser = id.toString(true);
  72. if (argc >= 3) {
  73. if (!Utils::writeFile(argv[2],idser)) {
  74. std::cerr << "Error writing to " << argv[2] << std::endl;
  75. return -1;
  76. } else std::cout << argv[2] << " written" << std::endl;
  77. } else std::cout << idser;
  78. } else if (!strcmp(argv[1],"validate")) {
  79. if (argc < 3) {
  80. printHelp(argv[0]);
  81. return -1;
  82. }
  83. Identity id = getIdFromArg(argv[2]);
  84. if (!id) {
  85. std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl;
  86. return -1;
  87. }
  88. if (!id.locallyValidate(true)) {
  89. std::cerr << argv[2] << " FAILED validation." << std::endl;
  90. return -1;
  91. } else std::cout << argv[2] << " is a valid identity (full check performed)" << std::endl;
  92. } else if (!strcmp(argv[1],"getpublic")) {
  93. if (argc < 3) {
  94. printHelp(argv[0]);
  95. return -1;
  96. }
  97. Identity id = getIdFromArg(argv[2]);
  98. if (!id) {
  99. std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl;
  100. return -1;
  101. }
  102. std::cout << id.toString(false);
  103. } else if (!strcmp(argv[1],"sign")) {
  104. if (argc < 4) {
  105. printHelp(argv[0]);
  106. return -1;
  107. }
  108. Identity id = getIdFromArg(argv[2]);
  109. if (!id) {
  110. std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl;
  111. return -1;
  112. }
  113. if (!id.hasPrivate()) {
  114. std::cerr << argv[2] << " does not contain a private key (must use private to sign)" << std::endl;
  115. return -1;
  116. }
  117. std::string inf;
  118. if (!Utils::readFile(argv[3],inf)) {
  119. std::cerr << argv[3] << " is not readable" << std::endl;
  120. return -1;
  121. }
  122. std::string signature = id.sign(inf.data(),inf.length());
  123. if (!signature.length()) {
  124. std::cerr << "Error computing SHA256 or signature (bug?)" << std::endl;
  125. return -1;
  126. }
  127. std::cout << Utils::base64Encode(signature.data(),signature.length());
  128. } else if (!strcmp(argv[1],"verify")) {
  129. if (argc < 4) {
  130. printHelp(argv[0]);
  131. return -1;
  132. }
  133. Identity id = getIdFromArg(argv[2]);
  134. if (!id) {
  135. std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl;
  136. return -1;
  137. }
  138. std::string inf;
  139. if (!Utils::readFile(argv[3],inf)) {
  140. std::cerr << argv[3] << " is not readable" << std::endl;
  141. return -1;
  142. }
  143. std::string signature(Utils::base64Decode(argv[4],strlen(argv[4])));
  144. if ((signature.length() > ZEROTIER_ADDRESS_LENGTH)&&(id.verifySignature(inf.data(),inf.length(),signature))) {
  145. std::cout << argv[3] << " signature valid" << std::endl;
  146. } else {
  147. std::cerr << argv[3] << " signature check FAILED" << std::endl;
  148. return -1;
  149. }
  150. } else if (!strcmp(argv[1],"encrypt")) {
  151. if (argc < 4) {
  152. printHelp(argv[0]);
  153. return -1;
  154. }
  155. Identity from = getIdFromArg(argv[2]);
  156. if (!from) {
  157. std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl;
  158. return -1;
  159. }
  160. if (!from.hasPrivate()) {
  161. std::cerr << argv[2] << " must contain a secret key" << std::endl;
  162. return -1;
  163. }
  164. Identity to = getIdFromArg(argv[3]);
  165. if (!to) {
  166. std::cerr << "Identity argument invalid or file unreadable: " << argv[3] << std::endl;
  167. return -1;
  168. }
  169. FILE *inf;
  170. if (argc > 4) {
  171. inf = fopen(argv[4],"r");
  172. if (!inf) {
  173. std::cerr << "Could not open input file " << argv[4] << std::endl;
  174. return -1;
  175. }
  176. } else inf = stdin;
  177. int inbuflen = 131072;
  178. char *inbuf = (char *)malloc(inbuflen);
  179. if (!inbuf) {
  180. std::cerr << "Could not malloc()" << std::endl;
  181. return -1;
  182. }
  183. int inlen = 0;
  184. int n;
  185. while ((n = (int)fread(inbuf + inlen,1,inbuflen - inlen,inf)) > 0) {
  186. inlen += n;
  187. if ((inbuflen - inlen) < 1024) {
  188. inbuf = (char *)realloc(inbuf,inbuflen += 131072);
  189. if (!inbuf) {
  190. std::cerr << "Could not malloc()" << std::endl;
  191. return -1;
  192. }
  193. }
  194. }
  195. if (inf != stdin)
  196. fclose(inf);
  197. std::string crypted(from.encrypt(to,inbuf,inlen));
  198. if (!crypted.length()) {
  199. std::cerr << "Failure encrypting data, check from/to identities" << std::endl;
  200. return -1;
  201. }
  202. if (argc > 5)
  203. Utils::writeFile(argv[5],crypted.data(),crypted.length());
  204. else fwrite(crypted.data(),1,crypted.length(),stdout);
  205. free(inbuf);
  206. } else if (!strcmp(argv[1],"decrypt")) {
  207. if (argc < 4) {
  208. printHelp(argv[0]);
  209. return -1;
  210. }
  211. Identity to = getIdFromArg(argv[2]);
  212. if (!to) {
  213. std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl;
  214. return -1;
  215. }
  216. if (!to.hasPrivate()) {
  217. std::cerr << argv[2] << " must contain a secret key" << std::endl;
  218. return -1;
  219. }
  220. Identity from = getIdFromArg(argv[3]);
  221. if (!from) {
  222. std::cerr << "Identity argument invalid or file unreadable: " << argv[3] << std::endl;
  223. return -1;
  224. }
  225. FILE *inf;
  226. if (argc > 4) {
  227. inf = fopen(argv[4],"r");
  228. if (!inf) {
  229. std::cerr << "Could not open input file " << argv[4] << std::endl;
  230. return -1;
  231. }
  232. } else inf = stdin;
  233. int inbuflen = 131072;
  234. char *inbuf = (char *)malloc(inbuflen);
  235. if (!inbuf) {
  236. std::cerr << "Could not malloc()" << std::endl;
  237. return -1;
  238. }
  239. int inlen = 0;
  240. int n;
  241. while ((n = (int)fread(inbuf + inlen,1,inbuflen - inlen,inf)) > 0) {
  242. inlen += n;
  243. if ((inbuflen - inlen) < 1024) {
  244. inbuf = (char *)realloc(inbuf,inbuflen += 131072);
  245. if (!inbuf) {
  246. std::cerr << "Could not malloc()" << std::endl;
  247. return -1;
  248. }
  249. }
  250. }
  251. if (inf != stdin)
  252. fclose(inf);
  253. std::string dec(to.decrypt(from,inbuf,inlen));
  254. free(inbuf);
  255. if (!dec.length()) {
  256. std::cerr << "Failure decrypting data, check from/to identities" << std::endl;
  257. return -1;
  258. }
  259. if (argc > 5)
  260. Utils::writeFile(argv[5],dec.data(),dec.length());
  261. else fwrite(dec.data(),1,dec.length(),stdout);
  262. } else {
  263. printHelp(argv[0]);
  264. return -1;
  265. }
  266. return 0;
  267. }