main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #include "jliball.hpp"
  15. #include "securesocket.hpp"
  16. #ifdef _WIN32
  17. #include <conio.h>
  18. #else
  19. #include <unistd.h>
  20. #endif
  21. char *myfgets(char *s, int size, FILE *stream)
  22. {
  23. int len = strlen(fgets(s, size, stream));
  24. if(len > 0)
  25. s[len - 1] = '\0';
  26. return s;
  27. }
  28. void inputpassword(const char* prompt, StringBuffer& passwd)
  29. {
  30. passwd.clear();
  31. #ifdef _WIN32
  32. printf("%s", prompt);
  33. char input=0;
  34. short num_entries=0;
  35. while (0x0d != (input = (char)getch()))
  36. {
  37. if (input == '\b')
  38. {
  39. printf("\b \b");
  40. if (num_entries)
  41. {
  42. num_entries--;
  43. }
  44. continue;
  45. }
  46. passwd.append(input);
  47. num_entries++;
  48. printf("*");
  49. }
  50. #else
  51. const char* pass = getpass(prompt);
  52. passwd.append(pass);
  53. #endif
  54. }
  55. void getpassword(const char* prompt, StringBuffer& passwd)
  56. {
  57. passwd.clear();
  58. StringBuffer passwd1, passwd2;
  59. int tries = 0;
  60. while(1)
  61. {
  62. if(tries++ >= 3)
  63. {
  64. exit(-1);
  65. }
  66. inputpassword(prompt, passwd1);
  67. inputpassword("\nVerifying password, retype: ", passwd2);
  68. if(passwd1.length() < 4)
  69. {
  70. printf("\npassword too short, should be 4 chars or longer\n");
  71. }
  72. else if(strcmp(passwd1.str(), passwd2.str()) != 0)
  73. {
  74. printf("\npasswords don't match.\n");
  75. }
  76. else
  77. break;
  78. }
  79. passwd.append(passwd1.str());
  80. }
  81. void usage()
  82. {
  83. // For now this tool only generates x509 certificates.
  84. // New functions can be added.
  85. printf("usage: myssl [-crt|-csr|-sign] [options]\n");
  86. printf("-crt: create self-signed certificate and privatekey pair\n");
  87. printf("-csr: create certificate signing request, using an existing privatekey or generating a new privatekey\n");
  88. printf("-sign: sign a CSR with your own certificate/privatekey pair\n");
  89. printf("options:\n");
  90. printf(" -b : batchmode\n");
  91. printf(" -c <country>\n");
  92. printf(" -s <state>\n");
  93. printf(" -l <locality>\n");
  94. printf(" -org <organization>\n");
  95. printf(" -ou <organizational unit>\n");
  96. printf(" -cn <common name, normally hostname or ip address\n");
  97. printf(" -e <email>\n");
  98. printf(" -days <number-of-days-to-be-valid>\n");
  99. printf(" -ip <input-private-key-file>\n");
  100. printf(" -ic <input-certificate-file>\n");
  101. printf(" -ir <input-csr-file>\n");
  102. printf(" -op <outputfile for privatekey>\n");
  103. printf(" -oc <outputfile for certificate>\n");
  104. printf(" -or <outputfile for csr>\n");
  105. printf(" -p <passphrase>\n");
  106. }
  107. enum MysslAction
  108. {
  109. CRT=0,
  110. CSR=1,
  111. SIGN=2
  112. };
  113. int main(int argc, char* argv[])
  114. {
  115. InitModuleObjects();
  116. StringBuffer passwd;
  117. if(argc < 2)
  118. {
  119. usage();
  120. return -1;
  121. }
  122. else if (stricmp(argv[1], "-?") == 0 || stricmp(argv[1], "-h") == 0 || stricmp(argv[1], "-help") == 0)
  123. {
  124. usage();
  125. return 0;
  126. }
  127. bool isBatchMode = false;
  128. MysslAction action = CRT;
  129. StringBuffer opfname, ocfname, orfname, cbuf, sbuf, lbuf, orgbuf, oubuf, cnbuf, ebuf, daysbuf, pfbuf, cfbuf, rfbuf, pbuf;
  130. int i;
  131. for (i=1; i<argc; i++)
  132. {
  133. if (stricmp(argv[i], "-crt") == 0)
  134. {
  135. action = CRT;
  136. }
  137. else if (stricmp(argv[i], "-csr") == 0)
  138. {
  139. action = CSR;
  140. }
  141. else if (stricmp(argv[i], "-sign") == 0)
  142. {
  143. action = SIGN;
  144. }
  145. else if (stricmp(argv[i], "-b") == 0)
  146. {
  147. isBatchMode = true;
  148. }
  149. else if(stricmp(argv[i], "-op") == 0)
  150. {
  151. i++;
  152. opfname.append(argv[i]);
  153. }
  154. else if(stricmp(argv[i], "-oc") == 0)
  155. {
  156. i++;
  157. ocfname.append(argv[i]);
  158. }
  159. else if(stricmp(argv[i], "-or") == 0)
  160. {
  161. i++;
  162. orfname.append(argv[i]);
  163. }
  164. else if(stricmp(argv[i], "-c") == 0)
  165. {
  166. i++;
  167. cbuf.append(argv[i]);
  168. }
  169. else if(stricmp(argv[i], "-s") == 0)
  170. {
  171. i++;
  172. sbuf.append(argv[i]);
  173. }
  174. else if(stricmp(argv[i], "-l") == 0)
  175. {
  176. i++;
  177. lbuf.append(argv[i]);
  178. }
  179. else if(stricmp(argv[i], "-org") == 0)
  180. {
  181. i++;
  182. orgbuf.append(argv[i]);
  183. }
  184. else if(stricmp(argv[i], "-ou") == 0)
  185. {
  186. i++;
  187. oubuf.append(argv[i]);
  188. }
  189. else if(stricmp(argv[i], "-cn") == 0)
  190. {
  191. i++;
  192. cnbuf.append(argv[i]);
  193. }
  194. else if(stricmp(argv[i], "-e") == 0)
  195. {
  196. i++;
  197. ebuf.append(argv[i]);
  198. }
  199. else if(stricmp(argv[i], "-days") == 0)
  200. {
  201. i++;
  202. daysbuf.append(argv[i]);
  203. }
  204. else if(stricmp(argv[i], "-ip") == 0)
  205. {
  206. i++;
  207. pfbuf.append(argv[i]);
  208. }
  209. else if(stricmp(argv[i], "-ic") == 0)
  210. {
  211. i++;
  212. cfbuf.append(argv[i]);
  213. }
  214. else if(stricmp(argv[i], "-ir") == 0)
  215. {
  216. i++;
  217. rfbuf.append(argv[i]);
  218. }
  219. else if(stricmp(argv[i], "-p") == 0)
  220. {
  221. i++;
  222. pbuf.append(argv[i]);
  223. }
  224. else
  225. {
  226. printf("unknown option %s\n", argv[i]);
  227. return -1;
  228. }
  229. }
  230. char buf[128];
  231. if(!isBatchMode)
  232. {
  233. if(action == CSR || action == CRT)
  234. {
  235. if(cbuf.length() == 0)
  236. {
  237. printf("Country Name (2 letter code): ");
  238. myfgets(buf,128,stdin);
  239. if(*buf == '\0')
  240. strcpy(buf, "US");
  241. cbuf.append(buf);
  242. }
  243. if(sbuf.length() == 0)
  244. {
  245. printf("State (full name): ");
  246. myfgets(buf,128,stdin);
  247. sbuf.append(buf);
  248. }
  249. if(lbuf.length() == 0)
  250. {
  251. printf("Locality Name (eg, city): ");
  252. myfgets(buf,128,stdin);
  253. lbuf.append(buf);
  254. }
  255. if(orgbuf.length() == 0)
  256. {
  257. printf("Organization Name (eg, company): ");
  258. myfgets(buf,128,stdin);
  259. orgbuf.append(buf);
  260. }
  261. if(oubuf.length() == 0)
  262. {
  263. printf("Organizational Unit Name: ");
  264. myfgets(buf,128,stdin);
  265. oubuf.append(buf);
  266. }
  267. if(ebuf.length() == 0)
  268. {
  269. printf("Email: ");
  270. myfgets(buf, 128, stdin);
  271. ebuf.append(buf);
  272. }
  273. if(cnbuf.length() == 0)
  274. {
  275. printf("Common Name (Server's hostname or IP address): ");
  276. myfgets(buf, 128, stdin);
  277. cnbuf.append(buf);
  278. }
  279. if(action == CRT)
  280. {
  281. printf("Number of days for the certificate to be valid: ");
  282. myfgets(buf, 128, stdin);
  283. if(strlen(buf) > 0)
  284. daysbuf.append(buf);
  285. printf("Private Key file(leave it blank if you want to generate a private key): ");
  286. myfgets(buf,128,stdin);
  287. pfbuf.append(buf);
  288. getpassword("Enter PEM pass phrase: ", pbuf);
  289. }
  290. else if(action == CSR)
  291. {
  292. printf("Private Key file(leave it blank if you want to generate a private key): ");
  293. myfgets(buf,128,stdin);
  294. pfbuf.append(buf);
  295. getpassword("Enter PEM pass phrase: ", pbuf);
  296. }
  297. }
  298. else if(action == SIGN)
  299. {
  300. printf("csr file: ");
  301. myfgets(buf,128,stdin);
  302. rfbuf.append(buf);
  303. printf("CA certificate file: ");
  304. myfgets(buf,128,stdin);
  305. cfbuf.append(buf);
  306. printf("CA privatekey file: ");
  307. myfgets(buf,128,stdin);
  308. pfbuf.append(buf);
  309. getpassword("CA private key passphrase: ", pbuf);
  310. printf("\nNumber of days for the certificate to be valid: ");
  311. myfgets(buf,128,stdin);
  312. daysbuf.append(buf);
  313. }
  314. }
  315. try
  316. {
  317. Owned<IFile> opf;
  318. Owned<IFileIO> opfio;
  319. if(opfname.length() > 0)
  320. {
  321. opf.setown(createIFile(opfname.str()));
  322. opfio.setown(opf->open(IFOcreate));
  323. }
  324. Owned<IFile> ocf;
  325. Owned<IFileIO> ocfio;
  326. if(ocfname.length() > 0)
  327. {
  328. ocf.setown(createIFile(ocfname.str()));
  329. ocfio.setown(ocf->open(IFOcreate));
  330. }
  331. Owned<IFile> orf;
  332. Owned<IFileIO> orfio;
  333. if(orfname.length() > 0)
  334. {
  335. orf.setown(createIFile(orfname.str()));
  336. orfio.setown(orf->open(IFOcreate));
  337. }
  338. if(action == CRT || action == CSR)
  339. {
  340. Owned<ICertificate> cc = createCertificate();
  341. if(cbuf.length() > 0)
  342. cc->setCountry(cbuf.str());
  343. if(sbuf.length() > 0)
  344. cc->setState(sbuf.str());
  345. if(lbuf.length() > 0)
  346. cc->setCity(lbuf.str());
  347. if(orgbuf.length() > 0)
  348. cc->setOrganization(orgbuf.str());
  349. if(oubuf.length() > 0)
  350. cc->setOrganizationalUnit(oubuf.str());
  351. if(ebuf.length() > 0)
  352. cc->setEmail(ebuf.str());
  353. if(cnbuf.length() > 0)
  354. cc->setDestAddr(cnbuf.str());
  355. if(action == CSR)
  356. {
  357. if(pbuf.length() > 0)
  358. cc->setPassphrase(pbuf.str());
  359. else
  360. throw MakeStringException(-1, "passphrase not specified.");
  361. StringBuffer csrbuf, privkey;
  362. if(pfbuf.length() == 0)
  363. {
  364. cc->generateCSR(privkey, csrbuf);
  365. if(opfio.get() != NULL)
  366. opfio->write(0, privkey.length(), privkey.str());
  367. else
  368. printf("\n%s\n", privkey.str());
  369. if(orfio.get() != NULL)
  370. orfio->write(0, csrbuf.length(), csrbuf.str());
  371. else
  372. printf("\n%s\n", csrbuf.str());
  373. }
  374. else
  375. {
  376. privkey.loadFile(pfbuf.str());
  377. cc->generateCSR(privkey.str(), csrbuf);
  378. if(orfio.get() != NULL)
  379. orfio->write(0, csrbuf.length(), csrbuf.str());
  380. else
  381. printf("\n%s\n", csrbuf.str());
  382. }
  383. }
  384. else if(action == CRT)
  385. {
  386. if(daysbuf.length() > 0)
  387. {
  388. cc->setDays(atoi(daysbuf.str()));
  389. }
  390. if(pbuf.length() > 0)
  391. cc->setPassphrase(pbuf.str());
  392. else
  393. throw MakeStringException(-1, "passphrase not specified.");
  394. StringBuffer certbuf, privkey;
  395. if(pfbuf.length() == 0)
  396. {
  397. cc->generate(certbuf, privkey);
  398. if(opfio.get() != NULL)
  399. opfio->write(0, privkey.length(), privkey.str());
  400. else
  401. printf("\n%s\n", privkey.str());
  402. if(ocfio.get() != NULL)
  403. ocfio->write(0, certbuf.length(), certbuf.str());
  404. else
  405. printf("\n%s\n", certbuf.str());
  406. }
  407. else
  408. {
  409. privkey.loadFile(pfbuf.str());
  410. cc->generate(certbuf, privkey.str());
  411. if(ocfio.get() != NULL)
  412. ocfio->write(0, certbuf.length(), certbuf.str());
  413. else
  414. printf("\n%s\n\n", certbuf.str());
  415. }
  416. }
  417. }
  418. else if(stricmp(argv[1], "-sign") == 0)
  419. {
  420. StringBuffer csrbuf, ca_cert, ca_privkey, certbuf;
  421. if(rfbuf.length() == 0 || cfbuf.length() == 0 || pfbuf.length() == 0)
  422. throw MakeStringException(-1, "You need to specify csr file, certificate file and privatekey file");
  423. csrbuf.loadFile(rfbuf.str());
  424. ca_cert.loadFile(cfbuf.str());
  425. ca_privkey.loadFile(pfbuf.str());
  426. if(pbuf.length() == 0)
  427. throw MakeStringException(-1, "passphrase not specified.");
  428. int days = 365;
  429. if(daysbuf.length() > 0)
  430. {
  431. days = atoi(daysbuf.str());
  432. }
  433. signCertificate(csrbuf.str(),ca_cert.str(), ca_privkey.str(), pbuf.str(), days, certbuf);
  434. if(ocfio.get() != NULL)
  435. ocfio->write(0, certbuf.length(), certbuf.str());
  436. else
  437. printf("\n%s\n", certbuf.str());
  438. }
  439. else
  440. {
  441. usage();
  442. return -1;
  443. }
  444. }
  445. catch(IException* e)
  446. {
  447. StringBuffer errmsg;
  448. printf("\nError - %s\n", e->errorMessage(errmsg).str());
  449. e->Release();
  450. }
  451. catch(...)
  452. {
  453. printf("\nUnknown error.");
  454. }
  455. releaseAtoms();
  456. return 0;
  457. }