sslutils.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #ifdef HAVE_SSL
  32. static SSL_CTX *c=NULL;
  33. static SSL *s=NULL;
  34. static int initialized=0;
  35. int np_net_ssl_init(int sd) {
  36. return np_net_ssl_init_with_hostname(sd, NULL);
  37. }
  38. int np_net_ssl_init_with_hostname(int sd, char *host_name) {
  39. return np_net_ssl_init_with_hostname_and_version(sd, host_name, 0);
  40. }
  41. int np_net_ssl_init_with_hostname_and_version(int sd, char *host_name, int version) {
  42. return np_net_ssl_init_with_hostname_version_and_cert(sd, host_name, version, NULL, NULL);
  43. }
  44. int np_net_ssl_init_with_hostname_version_and_cert(int sd, char *host_name, int version, char *cert, char *privkey) {
  45. const SSL_METHOD *method = NULL;
  46. long ssl_options = NULL;
  47. switch (version) {
  48. case 0: /* Deafult to auto negotiation */
  49. method = SSLv23_client_method();
  50. ssl_options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
  51. break;
  52. case 1: /* TLSv1 protocol */
  53. method = TLSv1_client_method();
  54. ssl_options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
  55. break;
  56. case 2: /* SSLv2 protocol */
  57. #if defined(USE_GNUTLS) || defined(OPENSSL_NO_SSL2)
  58. printf(("%s\n", _("CRITICAL - SSL protocol version 2 is not supported by your SSL library.")));
  59. return STATE_CRITICAL;
  60. #else
  61. method = SSLv2_client_method();
  62. ssl_options = SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1;
  63. #endif
  64. break;
  65. case 3: /* SSLv3 protocol */
  66. #if defined(OPENSSL_NO_SSL3)
  67. printf(("%s\n", _("CRITICAL - SSL protocol version 3 is not supported by your SSL library.")));
  68. return STATE_CRITICAL;
  69. #else
  70. method = SSLv3_client_method();
  71. ssl_options = SSL_OP_NO_SSLv2 | SSL_OP_NO_TLSv1;
  72. break;
  73. default: /* Unsupported */
  74. printf("%s\n", _("CRITICAL - Unsupported SSL protocol version."));
  75. return STATE_CRITICAL;
  76. }
  77. if (!initialized) {
  78. /* Initialize SSL context */
  79. SSLeay_add_ssl_algorithms();
  80. SSL_load_error_strings();
  81. OpenSSL_add_all_algorithms();
  82. initialized = 1;
  83. }
  84. if ((c = SSL_CTX_new(method)) == NULL) {
  85. printf("%s\n", _("CRITICAL - Cannot create SSL context."));
  86. return STATE_CRITICAL;
  87. }
  88. if (cert && privkey) {
  89. SSL_CTX_use_certificate_file(c, cert, SSL_FILETYPE_PEM);
  90. SSL_CTX_use_PrivateKey_file(c, privkey, SSL_FILETYPE_PEM);
  91. #ifdef USE_OPENSSL
  92. if (!SSL_CTX_check_private_key(c)) {
  93. printf ("%s\n", _("CRITICAL - Private key does not seem to match certificate!\n"));
  94. return STATE_CRITICAL;
  95. }
  96. #endif
  97. }
  98. #ifdef SSL_OP_NO_TICKET
  99. ssl_options |= SSL_OP_NO_TICKET;
  100. #endif
  101. if (ssl_options) SSL_CTX_set_options(c, ssl_options);
  102. SSL_CTX_set_mode(c, SSL_MODE_AUTO_RETRY);
  103. if ((s = SSL_new(c)) != NULL) {
  104. #ifdef SSL_set_tlsext_host_name
  105. if (host_name != NULL)
  106. SSL_set_tlsext_host_name(s, host_name);
  107. #endif
  108. SSL_set_fd(s, sd);
  109. if (SSL_connect(s) == 1) {
  110. return OK;
  111. } else {
  112. printf("%s\n", _("CRITICAL - Cannot make SSL connection."));
  113. # ifdef USE_OPENSSL /* XXX look into ERR_error_string */
  114. ERR_print_errors_fp(stdout);
  115. # endif /* USE_OPENSSL */
  116. }
  117. } else {
  118. printf("%s\n", _("CRITICAL - Cannot initiate SSL handshake."));
  119. }
  120. return STATE_CRITICAL;
  121. }
  122. void np_net_ssl_cleanup() {
  123. if (s) {
  124. #ifdef SSL_set_tlsext_host_name
  125. SSL_set_tlsext_host_name(s, NULL);
  126. #endif
  127. SSL_shutdown(s);
  128. SSL_free(s);
  129. if (c) {
  130. SSL_CTX_free(c);
  131. c=NULL;
  132. }
  133. s=NULL;
  134. }
  135. }
  136. int np_net_ssl_write(const void *buf, int num) {
  137. return SSL_write(s, buf, num);
  138. }
  139. int np_net_ssl_read(void *buf, int num) {
  140. return SSL_read(s, buf, num);
  141. }
  142. int np_net_ssl_check_cert(int days_till_exp_warn, int days_till_exp_crit){
  143. # ifdef USE_OPENSSL
  144. X509 *certificate=NULL;
  145. X509_NAME *subj=NULL;
  146. char cn[MAX_CN_LENGTH]= "";
  147. int cnlen =-1;
  148. int status=STATE_UNKNOWN;
  149. ASN1_STRING *tm;
  150. int offset;
  151. struct tm stamp;
  152. float time_left;
  153. int days_left;
  154. char timestamp[50] = "";
  155. time_t tm_t;
  156. certificate=SSL_get_peer_certificate(s);
  157. if (!certificate) {
  158. printf("%s\n",_("CRITICAL - Cannot retrieve server certificate."));
  159. return STATE_CRITICAL;
  160. }
  161. /* Extract CN from certificate subject */
  162. subj=X509_get_subject_name(certificate);
  163. if (!subj) {
  164. printf("%s\n",_("CRITICAL - Cannot retrieve certificate subject."));
  165. return STATE_CRITICAL;
  166. }
  167. cnlen = X509_NAME_get_text_by_NID(subj, NID_commonName, cn, sizeof(cn)-1);
  168. if (cnlen == -1)
  169. strncpy(cn, _("Unknown CN\0"), 12);
  170. /* Retrieve timestamp of certificate */
  171. tm = X509_get_notAfter(certificate);
  172. /* Generate tm structure to process timestamp */
  173. if (tm->type == V_ASN1_UTCTIME) {
  174. if (tm->length < 10) {
  175. printf("%s\n", _("CRITICAL - Wrong time format in certificate."));
  176. return STATE_CRITICAL;
  177. } else {
  178. stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
  179. if (stamp.tm_year < 50)
  180. stamp.tm_year += 100;
  181. offset = 0;
  182. }
  183. } else {
  184. if (tm->length < 12) {
  185. printf("%s\n", _("CRITICAL - Wrong time format in certificate."));
  186. return STATE_CRITICAL;
  187. } else {
  188. stamp.tm_year =
  189. (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 +
  190. (tm->data[2] - '0') * 10 + (tm->data[3] - '0');
  191. stamp.tm_year -= 1900;
  192. offset = 2;
  193. }
  194. }
  195. stamp.tm_mon =
  196. (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
  197. stamp.tm_mday =
  198. (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
  199. stamp.tm_hour =
  200. (tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
  201. stamp.tm_min =
  202. (tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
  203. stamp.tm_sec = 0;
  204. stamp.tm_isdst = -1;
  205. time_left = difftime(timegm(&stamp), time(NULL));
  206. days_left = time_left / 86400;
  207. tm_t = mktime (&stamp);
  208. strftime(timestamp, 50, "%c", localtime(&tm_t));
  209. if (days_left > 0 && days_left <= days_till_exp_warn) {
  210. printf (_("%s - Certificate '%s' expires in %d day(s) (%s).\n"), (days_left>days_till_exp_crit)?"WARNING":"CRITICAL", cn, days_left, timestamp);
  211. if (days_left > days_till_exp_crit)
  212. return STATE_WARNING;
  213. else
  214. return STATE_CRITICAL;
  215. } else if (time_left < 0) {
  216. printf(_("CRITICAL - Certificate '%s' expired on %s.\n"), cn, timestamp);
  217. status=STATE_CRITICAL;
  218. } else if (days_left == 0) {
  219. printf (_("%s - Certificate '%s' expires today (%s).\n"), (days_left>days_till_exp_crit)?"WARNING":"CRITICAL", cn, timestamp);
  220. if (days_left > days_till_exp_crit)
  221. return STATE_WARNING;
  222. else
  223. return STATE_CRITICAL;
  224. } else {
  225. printf(_("OK - Certificate '%s' will expire on %s.\n"), cn, timestamp);
  226. status=STATE_OK;
  227. }
  228. X509_free(certificate);
  229. return status;
  230. # else /* ifndef USE_OPENSSL */
  231. printf("%s\n", _("WARNING - Plugin does not support checking certificates."));
  232. return STATE_WARNING;
  233. # endif /* USE_OPENSSL */
  234. }
  235. #endif /* HAVE_SSL */