ssl.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2008-2019 Adam Borowski and Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  19. ******************************************************************************/
  20. /******************************************************************************
  21. * (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t *
  22. * *
  23. * coded by Adam Borowski 2008 *
  24. * modifications by Igor van den Hoven 2014 *
  25. ******************************************************************************/
  26. #include "tintin.h"
  27. #ifdef HAVE_GNUTLS_H
  28. static gnutls_certificate_credentials_t ssl_cred = 0;
  29. static int ssl_check_cert(struct session *ses, gnutls_session_t sslses);
  30. DO_COMMAND(do_ssl)
  31. {
  32. char temp[BUFFER_SIZE], arg1[BUFFER_SIZE];
  33. substitute(ses, arg, temp, SUB_VAR|SUB_FUN);
  34. arg = temp;
  35. arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
  36. if (*arg1 == 0 || *arg == 0)
  37. {
  38. show_error(ses, LIST_COMMAND, "#SYNTAX: #SSL {name} {host} {port}");
  39. }
  40. else
  41. {
  42. ses = new_session(ses, arg1, arg, 0, 1);
  43. }
  44. return ses;
  45. }
  46. gnutls_session_t ssl_negotiate(struct session *ses)
  47. {
  48. gnutls_session_t ssl_ses;
  49. int ret;
  50. if (!ssl_cred)
  51. {
  52. gnutls_global_init();
  53. gnutls_certificate_allocate_credentials(&ssl_cred);
  54. }
  55. gnutls_init(&ssl_ses, GNUTLS_CLIENT);
  56. gnutls_set_default_priority(ssl_ses);
  57. gnutls_credentials_set(ssl_ses, GNUTLS_CRD_CERTIFICATE, ssl_cred);
  58. gnutls_transport_set_ptr(ssl_ses, (gnutls_transport_ptr_t) (long int) ses->socket);
  59. do
  60. {
  61. ret = gnutls_handshake(ssl_ses);
  62. }
  63. while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
  64. if (ret)
  65. {
  66. tintin_printf2(ses, "#SSL: handshake failed error: %s", gnutls_strerror(ret));
  67. gnutls_deinit(ssl_ses);
  68. return 0;
  69. }
  70. /*
  71. {
  72. char *debug = gnutls_session_get_desc(ssl_ses);
  73. tintin_printf2(ses, "#SSL: %s", debug);
  74. gnutls_free(debug);
  75. }
  76. */
  77. if (!ssl_check_cert(ses, ssl_ses))
  78. {
  79. gnutls_deinit(ssl_ses);
  80. return 0;
  81. }
  82. return ssl_ses;
  83. }
  84. static int get_cert_file(struct session *ses, char *result)
  85. {
  86. char name[BUFFER_SIZE], *ptr;
  87. sprintf(name, "%s_%s", ses->session_host, ses->session_port);
  88. ptr = name;
  89. while (*ptr)
  90. {
  91. if (*ptr == ':')
  92. {
  93. *ptr++ = '.';
  94. }
  95. else if (isalnum((int) *ptr) || *ptr == '-' || *ptr == '.' || *ptr == '_')
  96. {
  97. ptr++;
  98. }
  99. else
  100. {
  101. return 0;
  102. }
  103. }
  104. sprintf(result, "%s/%s/ssl/%s.crt", gtd->home, TINTIN_DIR, name);
  105. return 1;
  106. }
  107. static void load_cert(struct session *ses, gnutls_x509_crt_t *cert)
  108. {
  109. char cert_file[STRING_SIZE];
  110. FILE *fp;
  111. gnutls_datum_t bptr;
  112. if (!get_cert_file(ses, cert_file))
  113. {
  114. return;
  115. }
  116. if ((fp = fopen(cert_file, "r")) == NULL)
  117. {
  118. return;
  119. }
  120. bptr.size = fread(cert_file, 1, STRING_SIZE, fp);
  121. bptr.data = (unsigned char *) cert_file;
  122. fclose(fp);
  123. gnutls_x509_crt_init(cert);
  124. if (gnutls_x509_crt_import(*cert, &bptr, GNUTLS_X509_FMT_PEM))
  125. {
  126. gnutls_x509_crt_deinit(*cert);
  127. *cert = 0;
  128. }
  129. }
  130. static void save_cert(struct session *ses, gnutls_x509_crt_t cert, int new)
  131. {
  132. char filename[BUFFER_SIZE], buf[STRING_SIZE];
  133. FILE *fp;
  134. size_t len;
  135. len = STRING_SIZE;
  136. if (gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_PEM, buf, &len))
  137. {
  138. return;
  139. }
  140. sprintf(filename, "%s/%s", gtd->home, TINTIN_DIR);
  141. if (mkdir(filename, 0777) && errno != EEXIST)
  142. {
  143. tintin_printf(ses, "#SSL: FAILED TO CREATE TINTIN DIR %s (%s)", filename, strerror(errno));
  144. return;
  145. }
  146. sprintf(filename, "%s/%s/ssl", gtd->home, TINTIN_DIR);
  147. mkdir(filename, 0755);
  148. if (mkdir(filename, 0755) && errno != EEXIST)
  149. {
  150. tintin_printf(ses, "#SSL: CANNOT CREATE CERTS DIR %s (%s)", filename, strerror(errno));
  151. return;
  152. }
  153. if (!get_cert_file(ses, filename))
  154. {
  155. return;
  156. }
  157. if (new)
  158. {
  159. tintin_printf(ses, "#SSL: THIS IS THE FIRST TIME YOU CONNECT TO THIS SERVER.");
  160. }
  161. tintin_printf(ses, "#SSL: SAVING SERVER CERTIFICATE TO %s", filename);
  162. if ((fp = fopen(filename, "w")) == NULL)
  163. {
  164. tintin_printf(ses, "#SSL: SAVE FAILED (%s)", strerror(errno));
  165. return;
  166. }
  167. if (fwrite(buf, 1, len, fp) != len)
  168. {
  169. tintin_printf(ses, "#SSL: SAVE FAILED (%s)", strerror(errno));
  170. fclose(fp);
  171. unlink(filename);
  172. return;
  173. }
  174. if (fclose(fp))
  175. {
  176. tintin_printf(ses, "#SSL: SAVE FAILED (%s)", strerror(errno));
  177. }
  178. }
  179. static int diff_certs(gnutls_x509_crt_t c1, gnutls_x509_crt_t c2)
  180. {
  181. char buf1[STRING_SIZE], buf2[STRING_SIZE];
  182. size_t len1, len2;
  183. len1 = len2 = STRING_SIZE;
  184. if (gnutls_x509_crt_export(c1, GNUTLS_X509_FMT_DER, buf1, &len1))
  185. {
  186. return 1;
  187. }
  188. if (gnutls_x509_crt_export(c2, GNUTLS_X509_FMT_DER, buf2, &len2))
  189. {
  190. return 1;
  191. }
  192. if (len1 != len2)
  193. {
  194. return 1;
  195. }
  196. return memcmp(buf1, buf2, len1);
  197. }
  198. static int ssl_check_cert(struct session *ses, gnutls_session_t ssl_ses)
  199. {
  200. char filename[BUFFER_SIZE], buf2[BUFFER_SIZE], *bptr;
  201. time_t t;
  202. gnutls_x509_crt_t cert, oldcert;
  203. const gnutls_datum_t *cert_list;
  204. unsigned int cert_list_size;
  205. char *err = 0;
  206. oldcert = 0;
  207. load_cert(ses, &oldcert);
  208. if (gnutls_certificate_type_get(ssl_ses) != GNUTLS_CRT_X509)
  209. {
  210. err = "#SSL: SERVER DOES NOT USE x509 -> NO KEY RETENTION.";
  211. goto nocert;
  212. }
  213. if ((cert_list = gnutls_certificate_get_peers(ssl_ses, &cert_list_size)) == NULL)
  214. {
  215. err = "#SSL: SERVER HAS NO x509 CERTIFICATE -> NO KEY RETENTION.";
  216. goto nocert;
  217. }
  218. gnutls_x509_crt_init(&cert);
  219. if (gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER) < 0)
  220. {
  221. err = "#SSL: SERVER'S CERTIFICATE IS INVALID.";
  222. goto badcert;
  223. }
  224. t = time(0);
  225. if (gnutls_x509_crt_get_activation_time(cert) > t)
  226. {
  227. sprintf(buf2, "%s", ctime(&t));
  228. if ((bptr = strchr(buf2, '\n')))
  229. {
  230. *bptr = 0;
  231. }
  232. sprintf(filename, "CERTIFICATE ACTIVATION TIME IS IN THE FUTURE (%s)", buf2);
  233. err = filename;
  234. }
  235. if (gnutls_x509_crt_get_expiration_time(cert)<t)
  236. {
  237. sprintf(buf2, "%s", ctime(&t));
  238. if ((bptr = strchr(buf2, '\n')))
  239. {
  240. *bptr = 0;
  241. }
  242. sprintf(filename, "CERTIFICATE HAS EXPIRED (%s)", buf2);
  243. err = filename;
  244. }
  245. if (!oldcert)
  246. {
  247. save_cert(ses, cert, 1);
  248. }
  249. else if (diff_certs(cert, oldcert))
  250. {
  251. t -= gnutls_x509_crt_get_expiration_time(oldcert);
  252. if (err || t < -7*24*3600)
  253. {
  254. if (err)
  255. {
  256. sprintf(buf2, "CERTIFICATE MISMATCH, AND NEW %s", err);
  257. }
  258. else
  259. {
  260. sprintf(buf2, "SERVER CERTIFICATE IS DIFFERENT FROM THE SAVED ONE.");
  261. }
  262. err = buf2;
  263. }
  264. else
  265. {
  266. if (t > 0)
  267. {
  268. tintin_printf(ses, "#SSL: SERVER CERTIFICATE HAS CHANGED, BUT THE OLD ONE WAS EXPIRED.");
  269. }
  270. else
  271. {
  272. tintin_printf(ses, "#SSL: SERVER CERTIFICATE HAS CHANGED, BUT THE OLD ONE WAS ABOUT TO EXPIRE.");
  273. }
  274. /* Replace the old cert */
  275. save_cert(ses, cert, 0);
  276. gnutls_x509_crt_deinit(oldcert);
  277. oldcert = 0;
  278. }
  279. }
  280. else
  281. {
  282. /* All is well */
  283. gnutls_x509_crt_deinit(oldcert);
  284. oldcert = 0;
  285. }
  286. badcert:
  287. gnutls_x509_crt_deinit(cert);
  288. nocert:
  289. if (oldcert)
  290. {
  291. gnutls_x509_crt_deinit(oldcert);
  292. }
  293. if (err)
  294. {
  295. if (oldcert)
  296. {
  297. tintin_printf(ses, "#SSL ERROR: %s", err);
  298. tintin_printf(ses, "#SSL ALERT: THE SERVER'S SETTINGS WERE CHANGED IN AN UNEXPECTED WAY.");
  299. tintin_printf(ses, "#SSL ALERT: YOU MAY BE VULNERABLE TO MAN-IN-THE-MIDDLE ATTACKS.");
  300. tintin_printf(ses, "#SSL ALERT: TO CONTINUE, PLEASE DELETE THE FILE:");
  301. tintin_printf(ses, "#SSL ALERT: %s", filename);
  302. tintin_printf(ses, "#SSL ERROR: ABORTING CONNECTION.");
  303. return 0;
  304. }
  305. else
  306. {
  307. tintin_printf(ses, "#SSL ALERT: %s", err);
  308. tintin_printf(ses, "#SSL ALERT: YOU MAY BE VULNERABLE TO MAN-IN-THE-MIDDLE ATTACKS.");
  309. return 2;
  310. }
  311. }
  312. else
  313. {
  314. return 1;
  315. }
  316. }
  317. #else
  318. DO_COMMAND(do_ssl)
  319. {
  320. tintin_printf2(ses, "The GnuTLS library wasn't found. Install GnuTLS, run ./configure, and recompile for SSL support.");
  321. return ses;
  322. }
  323. #endif