sslutils.c 7.0 KB

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