1 /*****************************************************************************
3 * Monitoring Plugins SSL utilities
6 * Copyright (c) 2005-2024 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
*ctx
= NULL
;
37 int np_net_ssl_init(int sd
) { return np_net_ssl_init_with_hostname(sd
, NULL
); }
39 int np_net_ssl_init_with_hostname(int sd
, char *host_name
) { return np_net_ssl_init_with_hostname_and_version(sd
, host_name
, 0); }
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
);
45 int np_net_ssl_init_with_hostname_version_and_cert(int sd
, char *host_name
, int version
, char *cert
, char *privkey
) {
48 if ((ctx
= SSL_CTX_new(TLS_client_method())) == NULL
) {
49 printf("%s\n", _("CRITICAL - Cannot create SSL context."));
50 return STATE_CRITICAL
;
54 case MP_SSLv2
: /* SSLv2 protocol */
55 printf("%s\n", _("UNKNOWN - SSL protocol version 2 is not supported by your SSL library."));
57 case MP_SSLv3
: /* SSLv3 protocol */
58 # if defined(OPENSSL_NO_SSL3)
59 printf("%s\n", _("UNKNOWN - SSL protocol version 3 is not supported by your SSL library."));
62 SSL_CTX_set_min_proto_version(ctx
, SSL3_VERSION
);
63 SSL_CTX_set_max_proto_version(ctx
, SSL3_VERSION
);
66 case MP_TLSv1
: /* TLSv1 protocol */
67 # if defined(OPENSSL_NO_TLS1)
68 printf("%s\n", _("UNKNOWN - TLS protocol version 1 is not supported by your SSL library."));
71 SSL_CTX_set_min_proto_version(ctx
, TLS1_VERSION
);
72 SSL_CTX_set_max_proto_version(ctx
, TLS1_VERSION
);
75 case MP_TLSv1_1
: /* TLSv1.1 protocol */
76 # if !defined(SSL_OP_NO_TLSv1_1)
77 printf("%s\n", _("UNKNOWN - TLS protocol version 1.1 is not supported by your SSL library."));
80 SSL_CTX_set_min_proto_version(ctx
, TLS1_1_VERSION
);
81 SSL_CTX_set_max_proto_version(ctx
, TLS1_1_VERSION
);
84 case MP_TLSv1_2
: /* TLSv1.2 protocol */
85 # if !defined(SSL_OP_NO_TLSv1_2)
86 printf("%s\n", _("UNKNOWN - TLS protocol version 1.2 is not supported by your SSL library."));
89 SSL_CTX_set_min_proto_version(ctx
, TLS1_2_VERSION
);
90 SSL_CTX_set_max_proto_version(ctx
, TLS1_2_VERSION
);
93 case MP_TLSv1_2_OR_NEWER
:
94 # if !defined(SSL_OP_NO_TLSv1_1)
95 printf("%s\n", _("UNKNOWN - Disabling TLSv1.1 is not supported by your SSL library."));
98 SSL_CTX_set_min_proto_version(ctx
, TLS1_2_VERSION
);
101 case MP_TLSv1_1_OR_NEWER
:
102 # if !defined(SSL_OP_NO_TLSv1)
103 printf("%s\n", _("UNKNOWN - Disabling TLSv1 is not supported by your SSL library."));
104 return STATE_UNKNOWN
;
106 SSL_CTX_set_min_proto_version(ctx
, TLS1_1_VERSION
);
109 case MP_TLSv1_OR_NEWER
:
110 # if defined(SSL_OP_NO_SSLv3)
111 SSL_CTX_set_min_proto_version(ctx
, TLS1_VERSION
);
114 case MP_SSLv3_OR_NEWER
:
115 # if defined(SSL_OP_NO_SSLv2)
116 SSL_CTX_set_min_proto_version(ctx
, SSL3_VERSION
);
121 if (cert
&& privkey
) {
123 if (!SSL_CTX_use_certificate_chain_file(ctx
, cert
)) {
125 if (!SSL_CTX_use_certificate_file(ctx
, cert
, SSL_FILETYPE_PEM
)) {
127 # error Unported for unknown SSL library
129 printf("%s\n", _("CRITICAL - Unable to open certificate chain file!\n"));
130 return STATE_CRITICAL
;
132 SSL_CTX_use_PrivateKey_file(ctx
, privkey
, SSL_FILETYPE_PEM
);
134 if (!SSL_CTX_check_private_key(ctx
)) {
135 printf("%s\n", _("CRITICAL - Private key does not seem to match certificate!\n"));
136 return STATE_CRITICAL
;
140 # ifdef SSL_OP_NO_TICKET
141 options
|= SSL_OP_NO_TICKET
;
143 SSL_CTX_set_options(ctx
, options
);
144 SSL_CTX_set_mode(ctx
, SSL_MODE_AUTO_RETRY
);
145 if ((s
= SSL_new(ctx
)) != NULL
) {
146 # ifdef SSL_set_tlsext_host_name
147 if (host_name
!= NULL
)
148 SSL_set_tlsext_host_name(s
, host_name
);
151 if (SSL_connect(s
) == 1) {
154 printf("%s\n", _("CRITICAL - Cannot make SSL connection."));
155 # ifdef USE_OPENSSL /* XXX look into ERR_error_string */
156 ERR_print_errors_fp(stdout
);
157 # endif /* USE_OPENSSL */
160 printf("%s\n", _("CRITICAL - Cannot initiate SSL handshake."));
162 return STATE_CRITICAL
;
165 void np_net_ssl_cleanup() {
167 # ifdef SSL_set_tlsext_host_name
168 SSL_set_tlsext_host_name(s
, NULL
);
180 int np_net_ssl_write(const void *buf
, int num
) { return SSL_write(s
, buf
, num
); }
182 int np_net_ssl_read(void *buf
, int num
) { return SSL_read(s
, buf
, num
); }
184 int np_net_ssl_check_certificate(X509
*certificate
, int days_till_exp_warn
, int days_till_exp_crit
) {
186 X509_NAME
*subj
= NULL
;
187 char timestamp
[50] = "";
188 char cn
[MAX_CN_LENGTH
] = "";
192 int status
= STATE_UNKNOWN
;
203 printf("%s\n", _("CRITICAL - Cannot retrieve server certificate."));
204 return STATE_CRITICAL
;
207 /* Extract CN from certificate subject */
208 subj
= X509_get_subject_name(certificate
);
211 printf("%s\n", _("CRITICAL - Cannot retrieve certificate subject."));
212 return STATE_CRITICAL
;
214 cnlen
= X509_NAME_get_text_by_NID(subj
, NID_commonName
, cn
, sizeof(cn
));
216 strcpy(cn
, _("Unknown CN"));
218 /* Retrieve timestamp of certificate */
219 tm
= X509_get_notAfter(certificate
);
221 /* Generate tm structure to process timestamp */
222 if (tm
->type
== V_ASN1_UTCTIME
) {
223 if (tm
->length
< 10) {
224 printf("%s\n", _("CRITICAL - Wrong time format in certificate."));
225 return STATE_CRITICAL
;
227 stamp
.tm_year
= (tm
->data
[0] - '0') * 10 + (tm
->data
[1] - '0');
228 if (stamp
.tm_year
< 50)
229 stamp
.tm_year
+= 100;
233 if (tm
->length
< 12) {
234 printf("%s\n", _("CRITICAL - Wrong time format in certificate."));
235 return STATE_CRITICAL
;
237 stamp
.tm_year
= (tm
->data
[0] - '0') * 1000 + (tm
->data
[1] - '0') * 100 + (tm
->data
[2] - '0') * 10 + (tm
->data
[3] - '0');
238 stamp
.tm_year
-= 1900;
242 stamp
.tm_mon
= (tm
->data
[2 + offset
] - '0') * 10 + (tm
->data
[3 + offset
] - '0') - 1;
243 stamp
.tm_mday
= (tm
->data
[4 + offset
] - '0') * 10 + (tm
->data
[5 + offset
] - '0');
244 stamp
.tm_hour
= (tm
->data
[6 + offset
] - '0') * 10 + (tm
->data
[7 + offset
] - '0');
245 stamp
.tm_min
= (tm
->data
[8 + offset
] - '0') * 10 + (tm
->data
[9 + offset
] - '0');
246 stamp
.tm_sec
= (tm
->data
[10 + offset
] - '0') * 10 + (tm
->data
[11 + offset
] - '0');
249 tm_t
= timegm(&stamp
);
250 time_left
= difftime(tm_t
, time(NULL
));
251 days_left
= time_left
/ 86400;
253 setenv("TZ", "GMT", 1);
255 strftime(timestamp
, 50, "%c %z", localtime(&tm_t
));
262 if (days_left
> 0 && days_left
<= days_till_exp_warn
) {
263 printf(_("%s - Certificate '%s' expires in %d day(s) (%s).\n"), (days_left
> days_till_exp_crit
) ? "WARNING" : "CRITICAL", cn
,
264 days_left
, timestamp
);
265 if (days_left
> days_till_exp_crit
)
266 status
= STATE_WARNING
;
268 status
= STATE_CRITICAL
;
269 } else if (days_left
== 0 && time_left
> 0) {
270 if (time_left
>= 3600)
271 time_remaining
= (int)time_left
/ 3600;
273 time_remaining
= (int)time_left
/ 60;
275 printf(_("%s - Certificate '%s' expires in %u %s (%s)\n"), (days_left
> days_till_exp_crit
) ? "WARNING" : "CRITICAL", cn
,
276 time_remaining
, time_left
>= 3600 ? "hours" : "minutes", timestamp
);
278 if (days_left
> days_till_exp_crit
)
279 status
= STATE_WARNING
;
281 status
= STATE_CRITICAL
;
282 } else if (time_left
< 0) {
283 printf(_("CRITICAL - Certificate '%s' expired on %s.\n"), cn
, timestamp
);
284 status
= STATE_CRITICAL
;
285 } else if (days_left
== 0) {
286 printf(_("%s - Certificate '%s' just expired (%s).\n"), (days_left
> days_till_exp_crit
) ? "WARNING" : "CRITICAL", cn
, timestamp
);
287 if (days_left
> days_till_exp_crit
)
288 status
= STATE_WARNING
;
290 status
= STATE_CRITICAL
;
292 printf(_("OK - Certificate '%s' will expire on %s.\n"), cn
, timestamp
);
295 X509_free(certificate
);
297 # else /* ifndef USE_OPENSSL */
298 printf("%s\n", _("WARNING - Plugin does not support checking certificates."));
299 return STATE_WARNING
;
300 # endif /* USE_OPENSSL */
303 int np_net_ssl_check_cert(int days_till_exp_warn
, int days_till_exp_crit
) {
305 X509
*certificate
= NULL
;
306 certificate
= SSL_get_peer_certificate(s
);
307 return (np_net_ssl_check_certificate(certificate
, days_till_exp_warn
, days_till_exp_crit
));
308 # else /* ifndef USE_OPENSSL */
309 printf("%s\n", _("WARNING - Plugin does not support checking certificates."));
310 return STATE_WARNING
;
311 # endif /* USE_OPENSSL */
314 #endif /* HAVE_SSL */