1 /*****************************************************************************
3 * Monitoring Plugins SSL utilities
6 * Copyright (c) 2005-2010 Monitoring 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
34 static SSL_CTX
*c
=NULL
;
36 static int initialized
=0;
38 int np_net_ssl_init(int sd
) {
39 return np_net_ssl_init_with_hostname(sd
, NULL
);
42 int np_net_ssl_init_with_hostname(int sd
, char *host_name
) {
43 return np_net_ssl_init_with_hostname_and_version(sd
, host_name
, 0);
46 int np_net_ssl_init_with_hostname_and_version(int sd
, char *host_name
, int version
) {
47 return np_net_ssl_init_with_hostname_version_and_cert(sd
, host_name
, version
, NULL
, NULL
);
50 int np_net_ssl_init_with_hostname_version_and_cert(int sd
, char *host_name
, int version
, char *cert
, char *privkey
) {
51 const SSL_METHOD
*method
= NULL
;
55 case MP_SSLv2
: /* SSLv2 protocol */
56 #if defined(USE_GNUTLS) || defined(OPENSSL_NO_SSL2)
57 printf("%s\n", _("UNKNOWN - SSL protocol version 2 is not supported by your SSL library."));
60 method
= SSLv2_client_method();
63 case MP_SSLv3
: /* SSLv3 protocol */
64 #if defined(OPENSSL_NO_SSL3)
65 printf("%s\n", _("UNKNOWN - SSL protocol version 3 is not supported by your SSL library."));
68 method
= SSLv3_client_method();
71 case MP_TLSv1
: /* TLSv1 protocol */
72 #if defined(OPENSSL_NO_TLS1)
73 printf("%s\n", _("UNKNOWN - TLS protocol version 1 is not supported by your SSL library."));
76 method
= TLSv1_client_method();
79 case MP_TLSv1_1
: /* TLSv1.1 protocol */
80 #if !defined(SSL_OP_NO_TLSv1_1)
81 printf("%s\n", _("UNKNOWN - TLS protocol version 1.1 is not supported by your SSL library."));
84 method
= TLSv1_1_client_method();
87 case MP_TLSv1_2
: /* TLSv1.2 protocol */
88 #if !defined(SSL_OP_NO_TLSv1_2)
89 printf("%s\n", _("UNKNOWN - TLS protocol version 1.2 is not supported by your SSL library."));
92 method
= TLSv1_2_client_method();
95 case MP_TLSv1_2_OR_NEWER
:
96 #if !defined(SSL_OP_NO_TLSv1_1)
97 printf("%s\n", _("UNKNOWN - Disabling TLSv1.1 is not supported by your SSL library."));
100 options
|= SSL_OP_NO_TLSv1_1
;
103 case MP_TLSv1_1_OR_NEWER
:
104 #if !defined(SSL_OP_NO_TLSv1)
105 printf("%s\n", _("UNKNOWN - Disabling TLSv1 is not supported by your SSL library."));
106 return STATE_UNKNOWN
;
108 options
|= SSL_OP_NO_TLSv1
;
111 case MP_TLSv1_OR_NEWER
:
112 #if defined(SSL_OP_NO_SSLv3)
113 options
|= SSL_OP_NO_SSLv3
;
116 case MP_SSLv3_OR_NEWER
:
117 #if defined(SSL_OP_NO_SSLv2)
118 options
|= SSL_OP_NO_SSLv2
;
120 case MP_SSLv2_OR_NEWER
:
122 default: /* Default to auto negotiation */
123 method
= SSLv23_client_method();
126 /* Initialize SSL context */
127 SSLeay_add_ssl_algorithms();
128 SSL_load_error_strings();
129 OpenSSL_add_all_algorithms();
132 if ((c
= SSL_CTX_new(method
)) == NULL
) {
133 printf("%s\n", _("CRITICAL - Cannot create SSL context."));
134 return STATE_CRITICAL
;
136 if (cert
&& privkey
) {
137 SSL_CTX_use_certificate_file(c
, cert
, SSL_FILETYPE_PEM
);
138 SSL_CTX_use_PrivateKey_file(c
, privkey
, SSL_FILETYPE_PEM
);
140 if (!SSL_CTX_check_private_key(c
)) {
141 printf ("%s\n", _("CRITICAL - Private key does not seem to match certificate!\n"));
142 return STATE_CRITICAL
;
146 #ifdef SSL_OP_NO_TICKET
147 options
|= SSL_OP_NO_TICKET
;
149 SSL_CTX_set_options(c
, options
);
150 SSL_CTX_set_mode(c
, SSL_MODE_AUTO_RETRY
);
151 if ((s
= SSL_new(c
)) != NULL
) {
152 #ifdef SSL_set_tlsext_host_name
153 if (host_name
!= NULL
)
154 SSL_set_tlsext_host_name(s
, host_name
);
157 if (SSL_connect(s
) == 1) {
160 printf("%s\n", _("CRITICAL - Cannot make SSL connection."));
161 # ifdef USE_OPENSSL /* XXX look into ERR_error_string */
162 ERR_print_errors_fp(stdout
);
163 # endif /* USE_OPENSSL */
166 printf("%s\n", _("CRITICAL - Cannot initiate SSL handshake."));
168 return STATE_CRITICAL
;
171 void np_net_ssl_cleanup() {
173 #ifdef SSL_set_tlsext_host_name
174 SSL_set_tlsext_host_name(s
, NULL
);
186 int np_net_ssl_write(const void *buf
, int num
) {
187 return SSL_write(s
, buf
, num
);
190 int np_net_ssl_read(void *buf
, int num
) {
191 return SSL_read(s
, buf
, num
);
194 int np_net_ssl_check_cert(int days_till_exp_warn
, int days_till_exp_crit
){
196 X509
*certificate
=NULL
;
197 X509_NAME
*subj
=NULL
;
198 char timestamp
[50] = "";
199 char cn
[MAX_CN_LENGTH
]= "";
203 int status
=STATE_UNKNOWN
;
213 certificate
=SSL_get_peer_certificate(s
);
215 printf("%s\n",_("CRITICAL - Cannot retrieve server certificate."));
216 return STATE_CRITICAL
;
219 /* Extract CN from certificate subject */
220 subj
=X509_get_subject_name(certificate
);
223 printf("%s\n",_("CRITICAL - Cannot retrieve certificate subject."));
224 return STATE_CRITICAL
;
226 cnlen
= X509_NAME_get_text_by_NID(subj
, NID_commonName
, cn
, sizeof(cn
));
228 strcpy(cn
, _("Unknown CN"));
230 /* Retrieve timestamp of certificate */
231 tm
= X509_get_notAfter(certificate
);
233 /* Generate tm structure to process timestamp */
234 if (tm
->type
== V_ASN1_UTCTIME
) {
235 if (tm
->length
< 10) {
236 printf("%s\n", _("CRITICAL - Wrong time format in certificate."));
237 return STATE_CRITICAL
;
239 stamp
.tm_year
= (tm
->data
[0] - '0') * 10 + (tm
->data
[1] - '0');
240 if (stamp
.tm_year
< 50)
241 stamp
.tm_year
+= 100;
245 if (tm
->length
< 12) {
246 printf("%s\n", _("CRITICAL - Wrong time format in certificate."));
247 return STATE_CRITICAL
;
250 (tm
->data
[0] - '0') * 1000 + (tm
->data
[1] - '0') * 100 +
251 (tm
->data
[2] - '0') * 10 + (tm
->data
[3] - '0');
252 stamp
.tm_year
-= 1900;
257 (tm
->data
[2 + offset
] - '0') * 10 + (tm
->data
[3 + offset
] - '0') - 1;
259 (tm
->data
[4 + offset
] - '0') * 10 + (tm
->data
[5 + offset
] - '0');
261 (tm
->data
[6 + offset
] - '0') * 10 + (tm
->data
[7 + offset
] - '0');
263 (tm
->data
[8 + offset
] - '0') * 10 + (tm
->data
[9 + offset
] - '0');
265 (tm
->data
[10 + offset
] - '0') * 10 + (tm
->data
[11 + offset
] - '0');
268 tm_t
= timegm(&stamp
);
269 time_left
= difftime(tm_t
, time(NULL
));
270 days_left
= time_left
/ 86400;
272 setenv("TZ", "GMT", 1);
274 strftime(timestamp
, 50, "%c %z", localtime(&tm_t
));
281 if (days_left
> 0 && days_left
<= days_till_exp_warn
) {
282 printf (_("%s - Certificate '%s' expires in %d day(s) (%s).\n"), (days_left
>days_till_exp_crit
)?"WARNING":"CRITICAL", cn
, days_left
, timestamp
);
283 if (days_left
> days_till_exp_crit
)
284 status
= STATE_WARNING
;
286 status
= STATE_CRITICAL
;
287 } else if (days_left
== 0 && time_left
> 0) {
288 if (time_left
>= 3600)
289 time_remaining
= (int) time_left
/ 3600;
291 time_remaining
= (int) time_left
/ 60;
293 printf (_("%s - Certificate '%s' expires in %u %s (%s)\n"),
294 (days_left
>days_till_exp_crit
) ? "WARNING" : "CRITICAL", cn
, time_remaining
,
295 time_left
>= 3600 ? "hours" : "minutes", timestamp
);
297 if ( days_left
> days_till_exp_crit
)
298 status
= STATE_WARNING
;
300 status
= STATE_CRITICAL
;
301 } else if (time_left
< 0) {
302 printf(_("CRITICAL - Certificate '%s' expired on %s.\n"), cn
, timestamp
);
303 status
=STATE_CRITICAL
;
304 } else if (days_left
== 0) {
305 printf (_("%s - Certificate '%s' just expired (%s).\n"), (days_left
>days_till_exp_crit
)?"WARNING":"CRITICAL", cn
, timestamp
);
306 if (days_left
> days_till_exp_crit
)
307 status
= STATE_WARNING
;
309 status
= STATE_CRITICAL
;
311 printf(_("OK - Certificate '%s' will expire on %s.\n"), cn
, timestamp
);
314 X509_free(certificate
);
316 # else /* ifndef USE_OPENSSL */
317 printf("%s\n", _("WARNING - Plugin does not support checking certificates."));
318 return STATE_WARNING
;
319 # endif /* USE_OPENSSL */
322 #endif /* HAVE_SSL */