1 /*****************************************************************************
3 * Nagios plugins SSL utilities
6 * Copyright (c) 2005-2010 Nagios Plugins Development Team
10 * This file contains common functions for plugins that require SSL.
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.
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.
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/>.
27 *****************************************************************************/
29 #define MAX_CN_LENGTH 256
30 #define LOCAL_TIMEOUT_ALARM_HANDLER
35 static SSL_CTX
*c
=NULL
;
37 static int initialized
=0;
39 int np_net_ssl_init(int sd
) {
40 return np_net_ssl_init_with_hostname(sd
, NULL
);
43 int np_net_ssl_init_with_hostname(int sd
, char *host_name
) {
44 return np_net_ssl_init_with_hostname_and_version(sd
, host_name
, 0);
47 int np_net_ssl_init_with_hostname_and_version(int sd
, char *host_name
, int version
) {
48 return np_net_ssl_init_with_hostname_version_and_certificate(sd
, host_name
, version
, NULL
, NULL
);
51 int np_net_ssl_init_with_hostname_version_and_certificate(int sd
, char *host_name
, int version
, char *cert
, char *privkey
) {
52 const SSL_METHOD
*method
= NULL
;
55 case 0: /* Deafult to auto negotiation */
56 method
= SSLv23_client_method();
58 case 1: /* TLSv1 protocol */
59 method
= TLSv1_client_method();
61 case 2: /* SSLv2 protocol */
62 #if defined(USE_GNUTLS) || defined(OPENSSL_NO_SSL2)
63 printf(("%s\n", _("CRITICAL - SSL protocol version 2 is not supported by your SSL library.")));
64 return STATE_CRITICAL
;
66 method
= SSLv2_client_method();
69 case 3: /* SSLv3 protocol */
70 method
= SSLv3_client_method();
72 default: /* Unsupported */
73 printf("%s\n", _("CRITICAL - Unsupported SSL protocol version."));
74 return STATE_CRITICAL
;
77 /* Initialize SSL context */
78 SSLeay_add_ssl_algorithms();
79 SSL_load_error_strings();
80 OpenSSL_add_all_algorithms();
83 if ((c
= SSL_CTX_new(method
)) == NULL
) {
84 printf("%s\n", _("CRITICAL - Cannot create SSL context."));
85 return STATE_CRITICAL
;
87 if (cert
&& privkey
) {
88 SSL_CTX_use_certificate_file(c
, cert
, SSL_FILETYPE_PEM
);
89 SSL_CTX_use_PrivateKey_file(c
, privkey
, SSL_FILETYPE_PEM
);
90 if (!SSL_CTX_check_private_key(c
)) {
91 printf ("%s\n", _("CRITICAL - Private key does not seem to match certificate!\n"));
92 return STATE_CRITICAL
;
95 #ifdef SSL_OP_NO_TICKET
96 SSL_CTX_set_options(c
, SSL_OP_NO_TICKET
);
98 if ((s
= SSL_new(c
)) != NULL
) {
99 #ifdef SSL_set_tlsext_host_name
100 if (host_name
!= NULL
)
101 SSL_set_tlsext_host_name(s
, host_name
);
104 if (SSL_connect(s
) == 1) {
107 printf("%s\n", _("CRITICAL - Cannot make SSL connection."));
108 # ifdef USE_OPENSSL /* XXX look into ERR_error_string */
109 ERR_print_errors_fp(stdout
);
110 # endif /* USE_OPENSSL */
113 printf("%s\n", _("CRITICAL - Cannot initiate SSL handshake."));
115 return STATE_CRITICAL
;
118 void np_net_ssl_cleanup() {
120 #ifdef SSL_set_tlsext_host_name
121 SSL_set_tlsext_host_name(s
, NULL
);
133 int np_net_ssl_write(const void *buf
, int num
) {
134 return SSL_write(s
, buf
, num
);
137 int np_net_ssl_read(void *buf
, int num
) {
138 return SSL_read(s
, buf
, num
);
141 int np_net_ssl_check_cert(int days_till_exp_warn
, int days_till_exp_crit
){
143 X509
*certificate
=NULL
;
144 X509_NAME
*subj
=NULL
;
145 char cn
[MAX_CN_LENGTH
]= "";
147 int status
=STATE_UNKNOWN
;
154 char timestamp
[17] = "";
156 certificate
=SSL_get_peer_certificate(s
);
158 printf("%s\n",_("CRITICAL - Cannot retrieve server certificate."));
159 return STATE_CRITICAL
;
162 /* Extract CN from certificate subject */
163 subj
=X509_get_subject_name(certificate
);
166 printf("%s\n",_("CRITICAL - Cannot retrieve certificate subject."));
167 return STATE_CRITICAL
;
169 cnlen
= X509_NAME_get_text_by_NID(subj
, NID_commonName
, cn
, sizeof(cn
));
171 strcpy(cn
, _("Unknown CN"));
173 /* Retrieve timestamp of certificate */
174 tm
= X509_get_notAfter(certificate
);
176 /* Generate tm structure to process timestamp */
177 if (tm
->type
== V_ASN1_UTCTIME
) {
178 if (tm
->length
< 10) {
179 printf("%s\n", _("CRITICAL - Wrong time format in certificate."));
180 return STATE_CRITICAL
;
182 stamp
.tm_year
= (tm
->data
[0] - '0') * 10 + (tm
->data
[1] - '0');
183 if (stamp
.tm_year
< 50)
184 stamp
.tm_year
+= 100;
188 if (tm
->length
< 12) {
189 printf("%s\n", _("CRITICAL - Wrong time format in certificate."));
190 return STATE_CRITICAL
;
193 (tm
->data
[0] - '0') * 1000 + (tm
->data
[1] - '0') * 100 +
194 (tm
->data
[2] - '0') * 10 + (tm
->data
[3] - '0');
195 stamp
.tm_year
-= 1900;
200 (tm
->data
[2 + offset
] - '0') * 10 + (tm
->data
[3 + offset
] - '0') - 1;
202 (tm
->data
[4 + offset
] - '0') * 10 + (tm
->data
[5 + offset
] - '0');
204 (tm
->data
[6 + offset
] - '0') * 10 + (tm
->data
[7 + offset
] - '0');
206 (tm
->data
[8 + offset
] - '0') * 10 + (tm
->data
[9 + offset
] - '0');
210 time_left
= difftime(timegm(&stamp
), time(NULL
));
211 days_left
= time_left
/ 86400;
213 (timestamp
, 17, "%02d/%02d/%04d %02d:%02d",
215 stamp
.tm_mday
, stamp
.tm_year
+ 1900, stamp
.tm_hour
, stamp
.tm_min
);
217 if (days_left
> 0 && days_left
<= days_till_exp_warn
) {
218 printf (_("%s - Certificate '%s' expires in %d day(s) (%s).\n"), (days_left
>days_till_exp_crit
)?"WARNING":"CRITICAL", cn
, days_left
, timestamp
);
219 if (days_left
> days_till_exp_crit
)
220 return STATE_WARNING
;
222 return STATE_CRITICAL
;
223 } else if (time_left
< 0) {
224 printf(_("CRITICAL - Certificate '%s' expired on %s.\n"), cn
, timestamp
);
225 status
=STATE_CRITICAL
;
226 } else if (days_left
== 0) {
227 printf (_("%s - Certificate '%s' expires today (%s).\n"), (days_left
>days_till_exp_crit
)?"WARNING":"CRITICAL", cn
, timestamp
);
228 if (days_left
> days_till_exp_crit
)
229 return STATE_WARNING
;
231 return STATE_CRITICAL
;
233 printf(_("OK - Certificate '%s' will expire on %s.\n"), cn
, timestamp
);
236 X509_free(certificate
);
238 # else /* ifndef USE_OPENSSL */
239 printf("%s\n", _("WARNING - Plugin does not support checking certificates."));
240 return STATE_WARNING
;
241 # endif /* USE_OPENSSL */
244 #endif /* HAVE_SSL */