ssl.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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];
  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 (is_alnum(*ptr) || *ptr == '-' || *ptr == '.' || *ptr == '_')
  96. {
  97. ptr++;
  98. }
  99. else
  100. {
  101. return 0;
  102. }
  103. }
  104. sprintf(result, "%s/ssl/%s.crt", gtd->system->tt_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", gtd->system->tt_dir);
  141. if (mkdir(filename, 0755) && 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/ssl", gtd->system->tt_dir);
  147. if (mkdir(filename, 0755) && errno != EEXIST)
  148. {
  149. tintin_printf(ses, "#SSL: CANNOT CREATE CERTS DIR %s (%s)", filename, strerror(errno));
  150. return;
  151. }
  152. if (!get_cert_file(ses, filename))
  153. {
  154. return;
  155. }
  156. if (new)
  157. {
  158. tintin_printf(ses, "#SSL: THIS IS THE FIRST TIME YOU CONNECT TO THIS SERVER.");
  159. }
  160. tintin_printf(ses, "#SSL: SAVING SERVER CERTIFICATE TO %s", filename);
  161. if ((fp = fopen(filename, "w")) == NULL)
  162. {
  163. tintin_printf(ses, "#SSL: SAVE FAILED (%s)", strerror(errno));
  164. return;
  165. }
  166. if (fwrite(buf, 1, len, fp) != len)
  167. {
  168. tintin_printf(ses, "#SSL: SAVE FAILED (%s)", strerror(errno));
  169. fclose(fp);
  170. unlink(filename);
  171. return;
  172. }
  173. if (fclose(fp))
  174. {
  175. tintin_printf(ses, "#SSL: SAVE FAILED (%s)", strerror(errno));
  176. }
  177. }
  178. static int diff_certs(gnutls_x509_crt_t c1, gnutls_x509_crt_t c2)
  179. {
  180. char buf1[STRING_SIZE], buf2[STRING_SIZE];
  181. size_t len1, len2;
  182. len1 = len2 = STRING_SIZE;
  183. if (gnutls_x509_crt_export(c1, GNUTLS_X509_FMT_DER, buf1, &len1))
  184. {
  185. return 1;
  186. }
  187. if (gnutls_x509_crt_export(c2, GNUTLS_X509_FMT_DER, buf2, &len2))
  188. {
  189. return 1;
  190. }
  191. if (len1 != len2)
  192. {
  193. return 1;
  194. }
  195. return memcmp(buf1, buf2, len1);
  196. }
  197. static int ssl_check_cert(struct session *ses, gnutls_session_t ssl_ses)
  198. {
  199. char filename[BUFFER_SIZE], buf2[BUFFER_SIZE];
  200. time_t t;
  201. gnutls_x509_crt_t cert, oldcert;
  202. const gnutls_datum_t *cert_list;
  203. unsigned int cert_list_size;
  204. char *err = 0;
  205. oldcert = 0;
  206. load_cert(ses, &oldcert);
  207. if (gnutls_certificate_type_get(ssl_ses) != GNUTLS_CRT_X509)
  208. {
  209. err = "#SSL: SERVER DOES NOT USE x509 -> NO KEY RETENTION.";
  210. goto nocert;
  211. }
  212. if ((cert_list = gnutls_certificate_get_peers(ssl_ses, &cert_list_size)) == NULL)
  213. {
  214. err = "#SSL: SERVER HAS NO x509 CERTIFICATE -> NO KEY RETENTION.";
  215. goto nocert;
  216. }
  217. gnutls_x509_crt_init(&cert);
  218. if (gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER) < 0)
  219. {
  220. err = "#SSL: SERVER'S CERTIFICATE IS INVALID.";
  221. goto badcert;
  222. }
  223. t = time(0);
  224. if (gnutls_x509_crt_get_activation_time(cert) > t)
  225. {
  226. sprintf(buf2, "CERTIFICATE ACTIVATION TIME IS IN THE FUTURE (%s)", str_time(ses, "%c", gnutls_x509_crt_get_activation_time(cert)));
  227. err = buf2;
  228. }
  229. if (gnutls_x509_crt_get_expiration_time(cert) < t)
  230. {
  231. sprintf(buf2, "CERTIFICATE HAS EXPIRED (%s)", str_time(ses, "%c", gnutls_x509_crt_get_expiration_time(cert)));
  232. err = buf2;
  233. }
  234. if (!oldcert)
  235. {
  236. save_cert(ses, cert, 1);
  237. }
  238. else if (diff_certs(cert, oldcert))
  239. {
  240. t -= gnutls_x509_crt_get_expiration_time(oldcert);
  241. if (err || t < -7*24*3600)
  242. {
  243. if (err)
  244. {
  245. sprintf(buf2, "CERTIFICATE MISMATCH, AND NEW %s", err);
  246. }
  247. else
  248. {
  249. sprintf(buf2, "SERVER CERTIFICATE IS DIFFERENT FROM THE SAVED ONE.");
  250. }
  251. err = buf2;
  252. }
  253. else
  254. {
  255. if (t > 0)
  256. {
  257. tintin_printf(ses, "#SSL: SERVER CERTIFICATE HAS CHANGED, BUT THE OLD ONE WAS EXPIRED.");
  258. }
  259. else
  260. {
  261. tintin_printf(ses, "#SSL: SERVER CERTIFICATE HAS CHANGED, BUT THE OLD ONE WAS ABOUT TO EXPIRE.");
  262. }
  263. /* Replace the old cert */
  264. save_cert(ses, cert, 0);
  265. gnutls_x509_crt_deinit(oldcert);
  266. oldcert = 0;
  267. }
  268. }
  269. else
  270. {
  271. /* All is well */
  272. gnutls_x509_crt_deinit(oldcert);
  273. oldcert = 0;
  274. }
  275. badcert:
  276. gnutls_x509_crt_deinit(cert);
  277. nocert:
  278. if (oldcert)
  279. {
  280. gnutls_x509_crt_deinit(oldcert);
  281. }
  282. if (err)
  283. {
  284. if (oldcert)
  285. {
  286. tintin_printf(ses, "#SSL ERROR: %s", err);
  287. get_cert_file(ses, filename);
  288. tintin_printf(ses, "#SSL ALERT: THE SERVER'S SETTINGS WERE CHANGED IN AN UNEXPECTED WAY.");
  289. tintin_printf(ses, "#SSL ALERT: YOU MAY BE VULNERABLE TO MAN-IN-THE-MIDDLE ATTACKS.");
  290. tintin_printf(ses, "#SSL ALERT: TO CONTINUE, PLEASE DELETE THE FILE:");
  291. tintin_printf(ses, "#SSL ALERT: %s", filename);
  292. tintin_printf(ses, "#SSL ERROR: ABORTING CONNECTION.");
  293. return 0;
  294. }
  295. else
  296. {
  297. tintin_printf(ses, "#SSL ALERT: %s", err);
  298. tintin_printf(ses, "#SSL ALERT: YOU MAY BE VULNERABLE TO MAN-IN-THE-MIDDLE ATTACKS.");
  299. return 2;
  300. }
  301. }
  302. else
  303. {
  304. return 1;
  305. }
  306. }
  307. #else
  308. DO_COMMAND(do_ssl)
  309. {
  310. tintin_printf2(ses, "The GnuTLS library wasn't found. Install GnuTLS, run ./configure, and recompile for SSL support.");
  311. return ses;
  312. }
  313. #endif