4
0

sslutils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*****************************************************************************
  2. *
  3. * Nagios plugins SSL utilities
  4. *
  5. * License: GPL
  6. * Copyright (c) 2005-2014 Nagios Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains common functions for plugins that require SSL.
  11. *
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. *
  27. *****************************************************************************/
  28. #define MAX_CN_LENGTH 256
  29. #include "common.h"
  30. #include "netutils.h"
  31. int check_hostname = 0;
  32. #ifdef HAVE_SSL
  33. static SSL_CTX *c=NULL;
  34. static SSL *s=NULL;
  35. static int initialized=0;
  36. int np_net_ssl_init(int sd) {
  37. return np_net_ssl_init_with_hostname(sd, NULL);
  38. }
  39. int np_net_ssl_init_with_hostname(int sd, char *host_name) {
  40. return np_net_ssl_init_with_hostname_and_version(sd, host_name, 0);
  41. }
  42. int np_net_ssl_init_with_hostname_and_version(int sd, char *host_name, int version) {
  43. return np_net_ssl_init_with_hostname_version_and_cert(sd, host_name, version, NULL, NULL);
  44. }
  45. int np_net_ssl_init_with_hostname_version_and_cert(int sd, char *host_name, int version, char *cert, char *privkey) {
  46. const SSL_METHOD *method = NULL;
  47. long options = 0; /*SSL_OP_ALL | SSL_OP_SINGLE_DH_USE;*/
  48. switch (version) {
  49. case MP_SSLv2: /* SSLv2 protocol */
  50. #if defined(USE_GNUTLS) || defined(OPENSSL_NO_SSL2)
  51. printf("%s\n", _("UNKNOWN - SSL protocol version 2 is not supported by your SSL library."));
  52. return STATE_UNKNOWN;
  53. #else
  54. method = SSLv2_client_method();
  55. break;
  56. #endif
  57. case MP_SSLv3: /* SSLv3 protocol */
  58. #if defined(OPENSSL_NO_SSL3)
  59. printf("%s\n", _("UNKNOWN - SSL protocol version 3 is not supported by your SSL library."));
  60. return STATE_UNKNOWN;
  61. #else
  62. method = SSLv3_client_method();
  63. options = SSL_OP_NO_TLSv1;
  64. #if defined(SSL_OP_NO_SSLv2)
  65. options |= SSL_OP_NO_SSLv2;
  66. #endif
  67. break;
  68. #endif
  69. case MP_TLSv1: /* TLSv1 protocol */
  70. #if defined(OPENSSL_NO_TLS1)
  71. printf("%s\n", _("UNKNOWN - TLS protocol version 1 is not supported by your SSL library."));
  72. return STATE_UNKNOWN;
  73. #else
  74. method = TLSv1_client_method();
  75. /* options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3; */
  76. break;
  77. #endif
  78. case MP_TLSv1_1: /* TLSv1.1 protocol */
  79. #if !defined(SSL_OP_NO_TLSv1_1)
  80. printf("%s\n", _("UNKNOWN - TLS protocol version 1.1 is not supported by your SSL library."));
  81. return STATE_UNKNOWN;
  82. #else
  83. method = TLSv1_1_client_method();
  84. break;
  85. #endif
  86. case MP_TLSv1_2: /* TLSv1.2 protocol */
  87. #if !defined(SSL_OP_NO_TLSv1_2)
  88. printf("%s\n", _("UNKNOWN - TLS protocol version 1.2 is not supported by your SSL library."));
  89. return STATE_UNKNOWN;
  90. #else
  91. method = TLSv1_2_client_method();
  92. break;
  93. #endif
  94. case MP_TLSv1_3: /* TLSv1.3 protocol */
  95. #if !defined(SSL_OP_NO_TLSv1_3)
  96. printf ("%s\n", _("Your OpenSSL version hasn't been compiled with TLS 1.3."));
  97. return STATE_UNKNOWN;
  98. #else
  99. method = TLS_client_method();
  100. options |= SSL_OP_NO_SSLv2;
  101. options |= SSL_OP_NO_SSLv3;
  102. options |= SSL_OP_NO_TLSv1;
  103. options |= SSL_OP_NO_TLSv1_1;
  104. options |= SSL_OP_NO_TLSv1_2;
  105. break;
  106. #endif
  107. case MP_TLSv1_3_OR_NEWER:
  108. #if !defined(SSL_OP_NO_TLSv1_2)
  109. printf("%s\n", _("UNKNOWN - Disabling TLSv1.2 is not supported by your SSL library."));
  110. return STATE_UNKNOWN;
  111. #else
  112. options |= SSL_OP_NO_TLSv1_2;
  113. #endif
  114. case MP_TLSv1_2_OR_NEWER:
  115. #if !defined(SSL_OP_NO_TLSv1_1)
  116. printf("%s\n", _("UNKNOWN - Disabling TLSv1.1 is not supported by your SSL library."));
  117. return STATE_UNKNOWN;
  118. #else
  119. options |= SSL_OP_NO_TLSv1_1;
  120. #endif
  121. /* FALLTHROUGH */
  122. case MP_TLSv1_1_OR_NEWER:
  123. #if !defined(SSL_OP_NO_TLSv1)
  124. printf("%s\n", _("UNKNOWN - Disabling TLSv1 is not supported by your SSL library."));
  125. return STATE_UNKNOWN;
  126. #else
  127. options |= SSL_OP_NO_TLSv1;
  128. #endif
  129. /* FALLTHROUGH */
  130. case MP_TLSv1_OR_NEWER:
  131. #if defined(SSL_OP_NO_SSLv3)
  132. options |= SSL_OP_NO_SSLv3;
  133. #endif
  134. /* FALLTHROUGH */
  135. case MP_SSLv3_OR_NEWER:
  136. #if defined(SSL_OP_NO_SSLv2)
  137. options |= SSL_OP_NO_SSLv2;
  138. #endif
  139. case MP_SSLv2_OR_NEWER:
  140. /* FALLTHROUGH */
  141. default: /* Default to auto negotiation */
  142. method = SSLv23_client_method();
  143. }
  144. if (!initialized) {
  145. /* Initialize SSL context */
  146. SSLeay_add_ssl_algorithms();
  147. SSL_load_error_strings();
  148. OpenSSL_add_all_algorithms();
  149. initialized = 1;
  150. }
  151. if ((c = SSL_CTX_new(method)) == NULL) {
  152. printf("%s\n", _("CRITICAL - Cannot create SSL context."));
  153. return STATE_CRITICAL;
  154. }
  155. if (cert && privkey) {
  156. SSL_CTX_use_certificate_file(c, cert, SSL_FILETYPE_PEM);
  157. SSL_CTX_use_PrivateKey_file(c, privkey, SSL_FILETYPE_PEM);
  158. #ifdef USE_OPENSSL
  159. if (!SSL_CTX_check_private_key(c)) {
  160. printf ("%s\n", _("CRITICAL - Private key does not seem to match certificate!\n"));
  161. return STATE_CRITICAL;
  162. }
  163. #endif
  164. }
  165. #ifdef SSL_OP_NO_TICKET
  166. options |= SSL_OP_NO_TICKET;
  167. #endif
  168. SSL_CTX_set_options(c, options);
  169. #ifdef SSL_CTX_set_post_handshake_auth
  170. SSL_CTX_set_post_handshake_auth(c, 1);
  171. #endif
  172. SSL_CTX_set_mode(c, SSL_MODE_AUTO_RETRY);
  173. if ((s = SSL_new(c)) != NULL) {
  174. #ifdef SSL_set_tlsext_host_name
  175. if (host_name != NULL)
  176. SSL_set_tlsext_host_name(s, host_name);
  177. #endif
  178. SSL_set_fd(s, sd);
  179. if (SSL_connect(s) == 1) {
  180. #if OPENSSL_VERSION_NUMBER >= 0x10002000L
  181. if (check_hostname && host_name && *host_name) {
  182. X509 *certificate=SSL_get_peer_certificate(s);
  183. int rc = X509_check_host(certificate, host_name, 0, 0, NULL);
  184. if (rc != 1) {
  185. printf("%s\n", _("CRITICAL - Hostname mismatch."));
  186. return STATE_CRITICAL;
  187. }
  188. }
  189. #endif
  190. return OK;
  191. } else {
  192. printf("%s\n", _("CRITICAL - Cannot make SSL connection."));
  193. # ifdef USE_OPENSSL /* XXX look into ERR_error_string */
  194. ERR_print_errors_fp(stdout);
  195. # endif /* USE_OPENSSL */
  196. }
  197. } else {
  198. printf("%s\n", _("CRITICAL - Cannot initiate SSL handshake."));
  199. }
  200. return STATE_CRITICAL;
  201. }
  202. void np_net_ssl_cleanup() {
  203. if (s) {
  204. #ifdef SSL_set_tlsext_host_name
  205. SSL_set_tlsext_host_name(s, NULL);
  206. #endif
  207. SSL_shutdown(s);
  208. SSL_free(s);
  209. if (c) {
  210. SSL_CTX_free(c);
  211. c=NULL;
  212. }
  213. s=NULL;
  214. }
  215. }
  216. int np_net_ssl_write(const void *buf, int num) {
  217. return SSL_write(s, buf, num);
  218. }
  219. int np_net_ssl_read(void *buf, int num) {
  220. return SSL_read(s, buf, num);
  221. }
  222. int np_net_ssl_check_cert(int days_till_exp_warn, int days_till_exp_crit){
  223. # ifdef USE_OPENSSL
  224. return np_net_ssl_check_cert_real(s, days_till_exp_warn, days_till_exp_crit);
  225. # else /* ifndef USE_OPENSSL */
  226. printf ("%s\n", _("WARNING - Plugin does not support checking certificates."));
  227. return STATE_WARNING;
  228. # endif /* USE_OPENSSL */
  229. }
  230. int np_net_ssl_check_cert_real(SSL *ssl, int days_till_exp_warn, int days_till_exp_crit){
  231. # ifdef USE_OPENSSL
  232. X509 *certificate=NULL;
  233. X509_NAME *subj=NULL;
  234. char timestamp[50] = "";
  235. char cn[MAX_CN_LENGTH]= "";
  236. int cnlen =-1;
  237. int status=STATE_UNKNOWN;
  238. ASN1_STRING *tm;
  239. int offset;
  240. struct tm stamp;
  241. float time_left;
  242. int days_left;
  243. int time_remaining;
  244. time_t tm_t;
  245. // Prefix whatever we're about to print with SSL
  246. printf("SSL ");
  247. certificate=SSL_get_peer_certificate(ssl);
  248. if (!certificate) {
  249. printf("%s\n",_("CRITICAL - Cannot retrieve server certificate."));
  250. return STATE_CRITICAL;
  251. }
  252. /* Extract CN from certificate subject */
  253. subj=X509_get_subject_name(certificate);
  254. if (!subj) {
  255. printf("%s\n",_("CRITICAL - Cannot retrieve certificate subject."));
  256. return STATE_CRITICAL;
  257. }
  258. cnlen = X509_NAME_get_text_by_NID(subj, NID_commonName, cn, sizeof(cn)-1);
  259. if (cnlen == -1)
  260. strncpy(cn, _("Unknown CN\0"), 12);
  261. /* Retrieve timestamp of certificate */
  262. tm = X509_get_notAfter(certificate);
  263. /* Generate tm structure to process timestamp */
  264. if (tm->type == V_ASN1_UTCTIME) {
  265. if (tm->length < 10) {
  266. printf("%s\n", _("CRITICAL - Wrong time format in certificate."));
  267. return STATE_CRITICAL;
  268. } else {
  269. stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
  270. if (stamp.tm_year < 50)
  271. stamp.tm_year += 100;
  272. offset = 0;
  273. }
  274. } else {
  275. if (tm->length < 12) {
  276. printf("%s\n", _("CRITICAL - Wrong time format in certificate."));
  277. return STATE_CRITICAL;
  278. } else {
  279. stamp.tm_year =
  280. (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 +
  281. (tm->data[2] - '0') * 10 + (tm->data[3] - '0');
  282. stamp.tm_year -= 1900;
  283. offset = 2;
  284. }
  285. }
  286. stamp.tm_mon =
  287. (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
  288. stamp.tm_mday =
  289. (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
  290. stamp.tm_hour =
  291. (tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
  292. stamp.tm_min =
  293. (tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
  294. stamp.tm_sec =
  295. (tm->data[10 + offset] - '0') * 10 + (tm->data[11 + offset] - '0');
  296. stamp.tm_isdst = -1;
  297. tm_t = timegm(&stamp);
  298. time_left = difftime(tm_t, time(NULL));
  299. days_left = time_left / 86400;
  300. strftime(timestamp, 50, "%F %R %z/%Z", localtime(&tm_t));
  301. if (days_left > 0 && days_left <= days_till_exp_warn) {
  302. printf (_("%s - Certificate '%s' expires in %d day(s) (%s).\n"), (days_left>days_till_exp_crit)?"WARNING":"CRITICAL", cn, days_left, timestamp);
  303. if (days_left > days_till_exp_crit)
  304. status = STATE_WARNING;
  305. else
  306. status = STATE_CRITICAL;
  307. } else if (days_left == 0 && time_left > 0) {
  308. if (time_left >= 3600)
  309. time_remaining = (int) time_left / 3600;
  310. else
  311. time_remaining = (int) time_left / 60;
  312. printf (_("%s - Certificate '%s' expires in %u %s (%s)\n"),
  313. (days_left>days_till_exp_crit) ? "WARNING" : "CRITICAL", cn, time_remaining,
  314. time_left >= 3600 ? "hours" : "minutes", timestamp);
  315. if ( days_left > days_till_exp_crit)
  316. status = STATE_WARNING;
  317. else
  318. status = STATE_CRITICAL;
  319. } else if (time_left < 0) {
  320. printf(_("CRITICAL - Certificate '%s' expired on %s.\n"), cn, timestamp);
  321. status=STATE_CRITICAL;
  322. } else if (days_left == 0) {
  323. printf (_("%s - Certificate '%s' just expired (%s).\n"), (days_left>days_till_exp_crit)?"WARNING":"CRITICAL", cn, timestamp);
  324. if (days_left > days_till_exp_crit)
  325. status = STATE_WARNING;
  326. else
  327. status = STATE_CRITICAL;
  328. } else {
  329. printf(_("OK - Certificate '%s' will expire in %u days on %s.\n"), cn, days_left, timestamp);
  330. status = STATE_OK;
  331. }
  332. X509_free(certificate);
  333. return status;
  334. # else /* ifndef USE_OPENSSL */
  335. printf("%s\n", _("WARNING - Plugin does not support checking certificates."));
  336. return STATE_WARNING;
  337. # endif /* USE_OPENSSL */
  338. }
  339. #endif /* HAVE_SSL */