sslutils.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. if (!initialized) {
  41. /* Initialize SSL context */
  42. SSLeay_add_ssl_algorithms ();
  43. SSL_load_error_strings ();
  44. OpenSSL_add_all_algorithms ();
  45. initialized = 1;
  46. }
  47. if ((c = SSL_CTX_new (SSLv23_client_method ())) == NULL) {
  48. printf ("%s\n", _("CRITICAL - Cannot create SSL context."));
  49. return STATE_CRITICAL;
  50. }
  51. #ifdef SSL_OP_NO_TICKET
  52. SSL_CTX_set_options(c, SSL_OP_NO_TICKET);
  53. #endif
  54. if ((s = SSL_new (c)) != NULL){
  55. #ifdef SSL_set_tlsext_host_name
  56. if (host_name != NULL)
  57. SSL_set_tlsext_host_name(s, host_name);
  58. #endif
  59. SSL_set_fd (s, sd);
  60. if (SSL_connect(s) == 1){
  61. return OK;
  62. } else {
  63. printf ("%s\n", _("CRITICAL - Cannot make SSL connection "));
  64. # ifdef USE_OPENSSL /* XXX look into ERR_error_string */
  65. ERR_print_errors_fp (stdout);
  66. # endif /* USE_OPENSSL */
  67. }
  68. } else {
  69. printf ("%s\n", _("CRITICAL - Cannot initiate SSL handshake."));
  70. }
  71. return STATE_CRITICAL;
  72. }
  73. void np_net_ssl_cleanup (){
  74. if(s){
  75. #ifdef SSL_set_tlsext_host_name
  76. SSL_set_tlsext_host_name(s, NULL);
  77. #endif
  78. SSL_shutdown (s);
  79. SSL_free (s);
  80. if(c) {
  81. SSL_CTX_free (c);
  82. c=NULL;
  83. }
  84. s=NULL;
  85. }
  86. }
  87. int np_net_ssl_write(const void *buf, int num){
  88. return SSL_write(s, buf, num);
  89. }
  90. int np_net_ssl_read(void *buf, int num){
  91. return SSL_read(s, buf, num);
  92. }
  93. int np_net_ssl_check_cert(int days_till_exp){
  94. # ifdef USE_OPENSSL
  95. X509 *certificate=NULL;
  96. X509_NAME *subj=NULL;
  97. char cn[MAX_CN_LENGTH]= "";
  98. int cnlen =-1;
  99. int status=STATE_UNKNOWN;
  100. ASN1_STRING *tm;
  101. int offset;
  102. struct tm stamp;
  103. float time_left;
  104. int days_left;
  105. char timestamp[17] = "";
  106. certificate=SSL_get_peer_certificate(s);
  107. if(! certificate){
  108. printf ("%s\n",_("CRITICAL - Cannot retrieve server certificate."));
  109. return STATE_CRITICAL;
  110. }
  111. /* Extract CN from certificate subject */
  112. subj=X509_get_subject_name(certificate);
  113. if(! subj){
  114. printf ("%s\n",_("CRITICAL - Cannot retrieve certificate subject."));
  115. return STATE_CRITICAL;
  116. }
  117. cnlen = X509_NAME_get_text_by_NID (subj, NID_commonName, cn, sizeof(cn));
  118. if ( cnlen == -1 )
  119. strcpy(cn , _("Unknown CN"));
  120. /* Retrieve timestamp of certificate */
  121. tm = X509_get_notAfter (certificate);
  122. /* Generate tm structure to process timestamp */
  123. if (tm->type == V_ASN1_UTCTIME) {
  124. if (tm->length < 10) {
  125. printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
  126. return STATE_CRITICAL;
  127. } else {
  128. stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
  129. if (stamp.tm_year < 50)
  130. stamp.tm_year += 100;
  131. offset = 0;
  132. }
  133. } else {
  134. if (tm->length < 12) {
  135. printf ("%s\n", _("CRITICAL - Wrong time format in certificate."));
  136. return STATE_CRITICAL;
  137. } else {
  138. stamp.tm_year =
  139. (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 +
  140. (tm->data[2] - '0') * 10 + (tm->data[3] - '0');
  141. stamp.tm_year -= 1900;
  142. offset = 2;
  143. }
  144. }
  145. stamp.tm_mon =
  146. (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
  147. stamp.tm_mday =
  148. (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
  149. stamp.tm_hour =
  150. (tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
  151. stamp.tm_min =
  152. (tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
  153. stamp.tm_sec = 0;
  154. stamp.tm_isdst = -1;
  155. time_left = difftime(timegm(&stamp), time(NULL));
  156. days_left = time_left / 86400;
  157. snprintf
  158. (timestamp, 17, "%02d/%02d/%04d %02d:%02d",
  159. stamp.tm_mon + 1,
  160. stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min);
  161. if (days_left > 0 && days_left <= days_till_exp) {
  162. printf (_("WARNING - Certificate '%s' expires in %d day(s) (%s).\n"), cn, days_left, timestamp);
  163. status=STATE_WARNING;
  164. } else if (time_left < 0) {
  165. printf (_("CRITICAL - Certificate '%s' expired on %s.\n"), cn, timestamp);
  166. status=STATE_CRITICAL;
  167. } else if (days_left == 0) {
  168. printf (_("WARNING - Certificate '%s' expires today (%s).\n"), cn, timestamp);
  169. status=STATE_WARNING;
  170. } else {
  171. printf (_("OK - Certificate '%s' will expire on %s.\n"), cn, timestamp);
  172. status=STATE_OK;
  173. }
  174. X509_free (certificate);
  175. return status;
  176. # else /* ifndef USE_OPENSSL */
  177. printf ("%s\n", _("WARNING - Plugin does not support checking certificates."));
  178. return STATE_WARNING;
  179. # endif /* USE_OPENSSL */
  180. }
  181. #endif /* HAVE_SSL */