Debian Codegraph @

runtime/nsd_ossl.c

raw
1 /* nsd_ossl.c
2 *
3 * An implementation of the nsd interface for OpenSSL.
4 *
5 * Copyright 2018-2025 Adiscon GmbH.
6 * Author: Andre Lorbach
7 *
8 * This file is part of the rsyslog runtime library.
9 *
10 * The rsyslog runtime library is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * The rsyslog runtime library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the rsyslog runtime library. If not, see <http://www.gnu.org/licenses/>.
22 *
23 * A copy of the GPL can be found in the file "COPYING" in this distribution.
24 * A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution.
25 */
26 #include "config.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <string.h>
31 #include <strings.h>
32
33 #include <errno.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <pthread.h>
38
39 #include "rsyslog.h"
40 #include "syslogd-types.h"
41 #include "module-template.h"
42 #include "cfsysline.h"
43 #include "obj.h"
44 #include "stringbuf.h"
45 #include "errmsg.h"
46 #include "net.h"
47 #include "netstrm.h"
48 #include "netstrms.h"
49 #include "datetime.h"
50 #include "net_ossl.h" // Include OpenSSL Helpers
51 #include "nsd_ptcp.h"
52 #include "nsd_ossl.h"
53 #include "unicode-helper.h"
54 #include "rsconf.h"
55
56 MODULE_TYPE_LIB
57 MODULE_TYPE_KEEP;
58
59 /* static data */
60 DEFobjStaticHelpers;
61 DEFobjCurrIf(glbl) DEFobjCurrIf(net) DEFobjCurrIf(datetime) DEFobjCurrIf(nsd_ptcp) DEFobjCurrIf(net_ossl)
62
63 /* Some prototypes for helper functions used inside openssl driver */
64 static rsRetVal applyGnutlsPriorityString(nsd_ossl_t *const pNsd);
65
66 /* retry an interrupted OSSL operation */
67 static rsRetVal doRetry(nsd_ossl_t *pNsd) {
68 DEFiRet;
69 nsd_ossl_t *pNsdOSSL = (nsd_ossl_t *)pNsd;
70
71 dbgprintf("doRetry: requested retry of %d operation - executing\n", pNsd->rtryCall);
72
73 /* We follow a common scheme here: first, we do the systen call and
74 * then we check the result. So far, the result is checked after the
75 * switch, because the result check is the same for all calls. Note that
76 * this may change once we deal with the read and write calls (but
77 * probably this becomes an issue only when we begin to work on TLS
78 * for relp). -- rgerhards, 2008-04-30
79 */
80 switch (pNsd->rtryCall) {
81 case osslRtry_handshake:
82 dbgprintf("doRetry: start osslHandshakeCheck, nsd: %p\n", pNsd);
83 /* Reset retry flag before calling handshake check.
84 * If it needs more retries, it will set it again.
85 */
86 pNsd->rtryCall = osslRtry_None;
87 /* Do the handshake again*/
88 CHKiRet(osslHandshakeCheck(pNsdOSSL));
89 break;
90 case osslRtry_recv:
91 case osslRtry_None:
92 default:
93 assert(0); /* this shall not happen! */
94 dbgprintf("doRetry: ERROR, pNsd->rtryCall invalid in nsdsel_ossl.c:%d\n", __LINE__);
95 break;
96 }
97 finalize_it:
98 if (iRet != RS_RET_OK && iRet != RS_RET_CLOSED && iRet != RS_RET_RETRY) pNsd->bAbortConn = 1; /* request abort */
99 RETiRet;
100 }
101
102 /*--------------------------------------OpenSSL helpers ------------------------------------------*/
103 void nsd_ossl_lastOpenSSLErrorMsg(
104 nsd_ossl_t const *pThis, const int ret, SSL *ssl, int severity, const char *pszCallSource, const char *pszOsslApi) {
105 uchar *fromHost = NULL;
106 int errno_store = errno;
107
108 if (pThis != NULL) {
109 nsd_ptcp.GetRemoteHName((nsd_t *)pThis->pTcp, &fromHost);
110 }
111
112 // Call helper in net_ossl
113 net_ossl.osslLastOpenSSLErrorMsg(fromHost, ret, ssl, severity, pszCallSource, pszOsslApi);
114
115 free(fromHost);
116 errno = errno_store;
117 }
118
119 #ifndef ENABLE_WOLFSSL
120 #if OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(LIBRESSL_VERSION_NUMBER)
121 long BIO_debug_callback_ex(BIO *bio,
122 int cmd,
123 const char __attribute__((unused)) * argp,
124 size_t __attribute__((unused)) len,
125 int argi,
126 long __attribute__((unused)) argl,
127 int ret,
128 size_t __attribute__((unused)) * processed)
129 #else
130 long BIO_debug_callback(
131 BIO *bio, int cmd, const char __attribute__((unused)) * argp, int argi, long __attribute__((unused)) argl, long ret)
132 #endif
133 {
134 long ret2 = ret; // Helper value to avoid printf compile errors long<>int
135 long r = 1;
136 if (BIO_CB_RETURN & cmd) r = ret;
137 dbgprintf("openssl debugmsg: BIO[%p]: ", (void *)bio);
138 switch (cmd) {
139 case BIO_CB_FREE:
140 dbgprintf("Free - %s\n", RSYSLOG_BIO_method_name(bio));
141 break;
142 /* Disabled due API changes for OpenSSL 1.1.0+ */
143 #if OPENSSL_VERSION_NUMBER < 0x10100000L
144 case BIO_CB_READ:
145 if (bio->method->type & BIO_TYPE_DESCRIPTOR)
146 dbgprintf("read(%d,%lu) - %s fd=%d\n", RSYSLOG_BIO_number_read(bio), (unsigned long)argi,
147 RSYSLOG_BIO_method_name(bio), RSYSLOG_BIO_number_read(bio));
148 else
149 dbgprintf("read(%d,%lu) - %s\n", RSYSLOG_BIO_number_read(bio), (unsigned long)argi,
150 RSYSLOG_BIO_method_name(bio));
151 break;
152 case BIO_CB_WRITE:
153 if (bio->method->type & BIO_TYPE_DESCRIPTOR)
154 dbgprintf("write(%d,%lu) - %s fd=%d\n", RSYSLOG_BIO_number_written(bio), (unsigned long)argi,
155 RSYSLOG_BIO_method_name(bio), RSYSLOG_BIO_number_written(bio));
156 else
157 dbgprintf("write(%d,%lu) - %s\n", RSYSLOG_BIO_number_written(bio), (unsigned long)argi,
158 RSYSLOG_BIO_method_name(bio));
159 break;
160 #else
161 case BIO_CB_READ:
162 dbgprintf("read %s\n", RSYSLOG_BIO_method_name(bio));
163 break;
164 case BIO_CB_WRITE:
165 dbgprintf("write %s\n", RSYSLOG_BIO_method_name(bio));
166 break;
167 #endif
168 case BIO_CB_PUTS:
169 dbgprintf("puts() - %s\n", RSYSLOG_BIO_method_name(bio));
170 break;
171 case BIO_CB_GETS:
172 dbgprintf("gets(%lu) - %s\n", (unsigned long)argi, RSYSLOG_BIO_method_name(bio));
173 break;
174 case BIO_CB_CTRL:
175 dbgprintf("ctrl(%lu) - %s\n", (unsigned long)argi, RSYSLOG_BIO_method_name(bio));
176 break;
177 case BIO_CB_RETURN | BIO_CB_READ:
178 dbgprintf("read return %ld\n", ret2);
179 break;
180 case BIO_CB_RETURN | BIO_CB_WRITE:
181 dbgprintf("write return %ld\n", ret2);
182 break;
183 case BIO_CB_RETURN | BIO_CB_GETS:
184 dbgprintf("gets return %ld\n", ret2);
185 break;
186 case BIO_CB_RETURN | BIO_CB_PUTS:
187 dbgprintf("puts return %ld\n", ret2);
188 break;
189 case BIO_CB_RETURN | BIO_CB_CTRL:
190 dbgprintf("ctrl return %ld\n", ret2);
191 break;
192 default:
193 dbgprintf("bio callback - unknown type (%d)\n", cmd);
194 break;
195 }
196
197 return (r);
198 }
199 #endif /* !ENABLE_WOLFSSL */
200
201 /*
202 * SNI should not be used if the hostname is a bare IP address
203 */
204 static rsRetVal SetServerNameIfPresent(nsd_ossl_t *pThis, uchar *host) {
205 struct sockaddr_in sa;
206 struct sockaddr_in6 sa6;
207 int inet_pton_ret;
208 DEFiRet;
209
210 /* Always use the configured remote SNI if present */
211 if (pThis->remoteSNI != NULL) {
212 if (SSL_set_tlsext_host_name(pThis->pNetOssl->ssl, (char *)pThis->remoteSNI) != 1) {
213 nsd_ossl_lastOpenSSLErrorMsg(pThis, 0, pThis->pNetOssl->ssl, LOG_ERR, "SetServerNameIfPresent",
214 "SSL_set_tlsext_host_name");
215 ABORT_FINALIZE(RS_RET_SYS_ERR);
216 }
217 FINALIZE;
218 }
219
220 /* Otherwise, figure out if host is an IP address */
221 inet_pton_ret = inet_pton(AF_INET, CHAR_CONVERT(host), &(sa.sin_addr));
222
223 if (inet_pton_ret == 0) { // host wasn't a bare IPv4 address: try IPv6
224 inet_pton_ret = inet_pton(AF_INET6, CHAR_CONVERT(host), &(sa6.sin6_addr));
225 }
226
227 /* Then make a decision */
228 switch (inet_pton_ret) {
229 case 1: // host is a valid IP address: don't use SNI
230 FINALIZE;
231 case 0: // host isn't a valid IP address: assume it's a domain name, use SNI
232 if (SSL_set_tlsext_host_name(pThis->pNetOssl->ssl, (char *)host) != 1) {
233 nsd_ossl_lastOpenSSLErrorMsg(pThis, 0, pThis->pNetOssl->ssl, LOG_ERR, "SetServerNameIfPresent",
234 "SSL_set_tlsext_host_name");
235 ABORT_FINALIZE(RS_RET_SYS_ERR);
236 }
237 FINALIZE;
238 default: // unexpected error
239 nsd_ossl_lastOpenSSLErrorMsg(pThis, 0, pThis->pNetOssl->ssl, LOG_ERR, "SetServerNameIfPresent",
240 "inet_pton");
241 ABORT_FINALIZE(RS_RET_INVALID_HNAME);
242 }
243
244 finalize_it:
245 RETiRet;
246 }
247
248 /* try to receive a record from the remote peer. This works with
249 * our own abstraction and handles local buffering and EAGAIN.
250 * See details on local buffering in Rcv(9 header-comment.
251 * This function MUST only be called when the local buffer is
252 * empty. Calling it otherwise will cause losss of current buffer
253 * data.
254 * rgerhards, 2008-06-24
255 */
256 rsRetVal osslRecordRecv(nsd_ossl_t *pThis, unsigned *const nextIODirection) {
257 ssize_t lenRcvd;
258 DEFiRet;
259 int err;
260
261 ISOBJ_TYPE_assert(pThis, nsd_ossl);
262 DBGPRINTF("osslRecordRecv: start\n");
263
264 lenRcvd = SSL_read(pThis->pNetOssl->ssl, pThis->pszRcvBuf, NSD_OSSL_MAX_RCVBUF);
265 if (lenRcvd > 0) {
266 DBGPRINTF("osslRecordRecv: SSL_read received %zd bytes\n", lenRcvd);
267 pThis->lenRcvBuf = lenRcvd;
268 pThis->ptrRcvBuf = 0;
269
270 /* Check for additional data in SSL buffer */
271 int iBytesLeft = SSL_pending(pThis->pNetOssl->ssl);
272 if (iBytesLeft > 0) {
273 DBGPRINTF("osslRecordRecv: %d Bytes pending after SSL_Read, expand buffer.\n", iBytesLeft);
274 /* realloc buffer size and preserve char content */
275 char *const newbuf = realloc(pThis->pszRcvBuf, NSD_OSSL_MAX_RCVBUF + iBytesLeft);
276 CHKmalloc(newbuf);
277 pThis->pszRcvBuf = newbuf;
278
279 /* 2nd read will read missing bytes from the current SSL Packet */
280 lenRcvd = SSL_read(pThis->pNetOssl->ssl, pThis->pszRcvBuf + NSD_OSSL_MAX_RCVBUF, iBytesLeft);
281 if (lenRcvd > 0) {
282 DBGPRINTF("osslRecordRecv: 2nd SSL_read received %zd bytes\n", (NSD_OSSL_MAX_RCVBUF + lenRcvd));
283 pThis->lenRcvBuf = NSD_OSSL_MAX_RCVBUF + lenRcvd;
284 } else {
285 goto sslerr;
286 }
287 }
288 } else {
289 sslerr:
290 err = SSL_get_error(pThis->pNetOssl->ssl, lenRcvd);
291 if (err == SSL_ERROR_ZERO_RETURN) {
292 DBGPRINTF("osslRecordRecv: SSL_ERROR_ZERO_RETURN received, connection may closed already\n");
293 ABORT_FINALIZE(RS_RET_RETRY);
294 } else if (err == SSL_ERROR_SYSCALL) {
295 /* Output error and abort */
296 nsd_ossl_lastOpenSSLErrorMsg(pThis, lenRcvd, pThis->pNetOssl->ssl, LOG_INFO, "osslRecordRecv",
297 "SSL_read 1");
298 iRet = RS_RET_TLS_ERR_SYSCALL;
299 /* Check for underlaying socket errors **/
300 if (errno == ECONNRESET) {
301 DBGPRINTF("osslRecordRecv: SSL_ERROR_SYSCALL Errno %d, connection reset by peer\n", errno);
302 /* Connection was dropped from remote site */
303 iRet = RS_RET_CLOSED;
304 } else {
305 DBGPRINTF("osslRecordRecv: SSL_ERROR_SYSCALL Errno %d\n", errno);
306 }
307 ABORT_FINALIZE(iRet);
308 } else if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE) {
309 DBGPRINTF("osslRecordRecv: SSL_get_error #1 = %d, lenRcvd=%zd\n", err, lenRcvd);
310 /* Output OpenSSL error*/
311 nsd_ossl_lastOpenSSLErrorMsg(pThis, lenRcvd, pThis->pNetOssl->ssl, LOG_ERR, "osslRecordRecv", "SSL_read 2");
312 ABORT_FINALIZE(RS_RET_NO_ERRCODE);
313 } else {
314 DBGPRINTF("osslRecordRecv: SSL_get_error #2 = %d, lenRcvd=%zd\n", err, lenRcvd);
315 pThis->rtryCall = osslRtry_recv;
316 pThis->rtryOsslErr = err; /* Store SSL ErrorCode into*/
317 ABORT_FINALIZE(RS_RET_RETRY);
318 }
319 }
320
321
322 finalize_it:
323 if (pThis->rtryCall != osslRtry_None && pThis->rtryOsslErr == SSL_ERROR_WANT_WRITE) {
324 *nextIODirection = NSDSEL_WR;
325 } else {
326 *nextIODirection = NSDSEL_RD;
327 }
328 dbgprintf("osslRecordRecv return. nsd %p, iRet %d, lenRcvd %zd, lenRcvBuf %d, ptrRcvBuf %d\n", pThis, iRet, lenRcvd,
329 pThis->lenRcvBuf, pThis->ptrRcvBuf);
330 RETiRet;
331 }
332
333 static rsRetVal osslInitSession(nsd_ossl_t *pThis, osslSslState_t osslType) /* , nsd_ossl_t *pServer) */
334 {
335 DEFiRet;
336 BIO *conn;
337 char pristringBuf[4096];
338 nsd_ptcp_t *pPtcp = (nsd_ptcp_t *)pThis->pTcp;
339
340 if (!(pThis->pNetOssl->ssl = SSL_new(pThis->pNetOssl->ctx))) {
341 pThis->pNetOssl->ssl = NULL;
342 nsd_ossl_lastOpenSSLErrorMsg(pThis, 0, pThis->pNetOssl->ssl, LOG_ERR, "osslInitSession", "SSL_new");
343 ABORT_FINALIZE(RS_RET_TLS_BASEINIT_FAIL);
344 }
345
346 // Set SSL_MODE_AUTO_RETRY to SSL obj
347 SSL_set_mode(pThis->pNetOssl->ssl, SSL_MODE_AUTO_RETRY);
348
349 if (pThis->pNetOssl->authMode != OSSL_AUTH_CERTANON) {
350 dbgprintf("osslInitSession: enable certificate checking (Mode=%d, VerifyDepth=%d)\n", pThis->pNetOssl->authMode,
351 pThis->DrvrVerifyDepth);
352 /* Enable certificate valid checking */
353 net_ossl.osslSetSslVerifyCallback(pThis->pNetOssl->ssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
354 if (pThis->DrvrVerifyDepth != 0) {
355 SSL_set_verify_depth(pThis->pNetOssl->ssl, pThis->DrvrVerifyDepth);
356 }
357 } else if (pThis->gnutlsPriorityString == NULL) {
358 /* Allow ANON Ciphers only in ANON Mode and if no custom priority string is defined */
359 #ifdef ENABLE_WOLFSSL
360 strncpy(pristringBuf, "ADH-AES256-GCM-SHA384:ADH-AES128-SHA", sizeof(pristringBuf));
361 #elif OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
362 /* NOTE: do never use: +eNULL, it DISABLES encryption! */
363 strncpy(pristringBuf, "ALL:+COMPLEMENTOFDEFAULT:+ADH:+ECDH:+aNULL@SECLEVEL=0", sizeof(pristringBuf));
364 #else
365 strncpy(pristringBuf, "ALL:+COMPLEMENTOFDEFAULT:+ADH:+ECDH:+aNULL", sizeof(pristringBuf));
366 #endif
367
368 dbgprintf("osslInitSession: setting anon ciphers: %s\n", pristringBuf);
369 if (SSL_set_cipher_list(pThis->pNetOssl->ssl, pristringBuf) == 0) {
370 #ifdef ENABLE_WOLFSSL
371 LogMsg(0, RS_RET_SYS_ERR, LOG_ERR,
372 "osslInitSession: Error setting anon ciphers '%s' (wolfSSL requires --enable-anon)\n", pristringBuf);
373 #else
374 dbgprintf("osslInitSession: Error setting ciphers '%s'\n", pristringBuf);
375 #endif
376 ABORT_FINALIZE(RS_RET_SYS_ERR);
377 }
378 }
379
380 /* Create BIO from ptcp socket!
381 * Use BIO_NOCLOSE to prevent OpenSSL from closing the socket when
382 * SSL_free is called. The socket is owned and will be closed by ptcp.
383 */
384 conn = BIO_new_socket(pPtcp->sock, BIO_NOCLOSE);
385 dbgprintf("osslInitSession: Init conn BIO[%p] done\n", (void *)conn);
386
387 /* Set debug Callback for conn BIO as well! */
388 net_ossl.osslSetBioCallback(conn);
389
390 /* TODO: still needed? Set to NON blocking ! */
391 BIO_set_nbio(conn, 1);
392 SSL_set_bio(pThis->pNetOssl->ssl, conn, conn);
393
394 if (osslType == osslServer) {
395 /* Server Socket */
396 SSL_set_accept_state(pThis->pNetOssl->ssl); /* sets ssl to work in server mode. */
397 pThis->pNetOssl->sslState = osslServer; /*set Server state */
398 } else {
399 /* Client Socket */
400 SSL_set_connect_state(pThis->pNetOssl->ssl); /*sets ssl to work in client mode.*/
401 pThis->pNetOssl->sslState = osslClient; /*set Client state */
402 }
403 pThis->bHaveSess = 1;
404
405 /* we are done */
406 FINALIZE;
407
408 finalize_it:
409 RETiRet;
410 }
411
412
413 /* check if it is OK to talk to the remote peer
414 * rgerhards, 2008-05-21
415 */
416 rsRetVal osslChkPeerAuth(nsd_ossl_t *pThis) {
417 DEFiRet;
418 X509 *certpeer = NULL;
419
420 ISOBJ_TYPE_assert(pThis, nsd_ossl);
421 uchar *fromHostIP = NULL;
422 nsd_ptcp.GetRemoteHName((nsd_t *)pThis->pTcp, &fromHostIP);
423
424 /* call the actual function based on current auth mode */
425 switch (pThis->pNetOssl->authMode) {
426 case OSSL_AUTH_CERTNAME:
427 /* if we check the name, we must ensure the cert is valid */
428 certpeer = net_ossl.osslGetpeercert(pThis->pNetOssl, pThis->pNetOssl->ssl, fromHostIP);
429 dbgprintf("osslChkPeerAuth: Check peer certname[%p]=%s\n", (void *)pThis->pNetOssl->ssl,
430 (certpeer != NULL ? "VALID" : "NULL"));
431 CHKiRet(net_ossl.osslChkpeercertvalidity(pThis->pNetOssl, pThis->pNetOssl->ssl, fromHostIP));
432 CHKiRet(net_ossl.osslChkpeername(pThis->pNetOssl, certpeer, fromHostIP));
433 break;
434 case OSSL_AUTH_CERTFINGERPRINT:
435 certpeer = net_ossl.osslGetpeercert(pThis->pNetOssl, pThis->pNetOssl->ssl, fromHostIP);
436 dbgprintf("osslChkPeerAuth: Check peer fingerprint[%p]=%s\n", (void *)pThis->pNetOssl->ssl,
437 (certpeer != NULL ? "VALID" : "NULL"));
438 CHKiRet(net_ossl.osslChkpeercertvalidity(pThis->pNetOssl, pThis->pNetOssl->ssl, fromHostIP));
439 CHKiRet(net_ossl.osslPeerfingerprint(pThis->pNetOssl, certpeer, fromHostIP));
440
441 break;
442 case OSSL_AUTH_CERTVALID:
443 certpeer = net_ossl.osslGetpeercert(pThis->pNetOssl, pThis->pNetOssl->ssl, fromHostIP);
444 dbgprintf("osslChkPeerAuth: Check peer valid[%p]=%s\n", (void *)pThis->pNetOssl->ssl,
445 (certpeer != NULL ? "VALID" : "NULL"));
446 CHKiRet(net_ossl.osslChkpeercertvalidity(pThis->pNetOssl, pThis->pNetOssl->ssl, fromHostIP));
447 break;
448 case OSSL_AUTH_CERTANON:
449 default:
450 break;
451 }
452 finalize_it:
453 if (certpeer != NULL) {
454 X509_free(certpeer);
455 }
456 if (fromHostIP != NULL) {
457 free(fromHostIP);
458 }
459 RETiRet;
460 }
461
462 /* end a OpenSSL session
463 * The function checks if we have a session and ends it only if so. So it can
464 * always be called, even if there currently is no session.
465 */
466 static rsRetVal osslEndSess(nsd_ossl_t *pThis) {
467 DEFiRet;
468 uchar *fromHostIP = NULL;
469 int ret;
470 int err;
471
472 /* try closing SSL Connection */
473 if (pThis->bHaveSess) {
474 DBGPRINTF("osslEndSess: closing SSL Session ...\n");
475 ret = SSL_shutdown(pThis->pNetOssl->ssl);
476 nsd_ptcp.GetRemoteHName((nsd_t *)pThis->pTcp, &fromHostIP);
477 if (ret <= 0) {
478 err = SSL_get_error(pThis->pNetOssl->ssl, ret);
479 DBGPRINTF("osslEndSess: shutdown failed with err = %d\n", err);
480
481 /* ignore those SSL Errors on shutdown */
482 if (err != SSL_ERROR_SYSCALL && err != SSL_ERROR_ZERO_RETURN && err != SSL_ERROR_WANT_READ &&
483 err != SSL_ERROR_WANT_WRITE) {
484 /* Output Warning only */
485 nsd_ossl_lastOpenSSLErrorMsg(pThis, ret, pThis->pNetOssl->ssl, LOG_WARNING, "osslEndSess",
486 "SSL_shutdown");
487 }
488 /* Shutdown not finished, call SSL_read to do a bidirectional shutdown, see doc for more:
489 * https://www.openssl.org/docs/man1.1.1/man3/SSL_shutdown.html
490 */
491 char rcvBuf[NSD_OSSL_MAX_RCVBUF];
492 int iBytesRet = SSL_read(pThis->pNetOssl->ssl, rcvBuf, NSD_OSSL_MAX_RCVBUF);
493 DBGPRINTF("osslEndSess: Forcing ssl shutdown SSL_read (%d) to do a bidirectional shutdown\n", iBytesRet);
494 if (ret < 0) {
495 /* Unsuccessful shutdown, log as INFO */
496 LogMsg(0, RS_RET_NO_ERRCODE, LOG_INFO,
497 "nsd_ossl: "
498 "TLS session terminated successfully to remote syslog server '%s' with SSL Error '%d':"
499 " End Session",
500 fromHostIP, ret);
501 }
502 dbgprintf(
503 "osslEndSess: TLS session terminated successfully to remote syslog server '%s'"
504 " End Session",
505 fromHostIP);
506 } else {
507 dbgprintf(
508 "osslEndSess: TLS session terminated successfully with remote syslog server '%s':"
509 " End Session",
510 fromHostIP);
511 }
512
513 /* Session closed */
514 pThis->bHaveSess = 0;
515 }
516
517 if (fromHostIP != NULL) {
518 free(fromHostIP);
519 }
520 RETiRet;
521 }
522
523 static void osslAbortSess(nsd_ossl_t *pThis) {
524 if (pThis->pNetOssl->ssl != NULL) {
525 SSL_free(pThis->pNetOssl->ssl);
526 pThis->pNetOssl->ssl = NULL;
527 }
528 pThis->bHaveSess = 0;
529 pThis->bAbortConn = 1;
530 pThis->iMode = 0;
531 pThis->rtryCall = osslRtry_None;
532 }
533 /* ---------------------------- end OpenSSL specifics ---------------------------- */
534
535
536 /* Standard-Constructor */
537 BEGINobjConstruct(nsd_ossl) /* be sure to specify the object type also in END macro! */
538 DBGPRINTF("nsd_ossl_construct: [%p]\n", pThis);
539 /* construct nsd_ptcp helper */
540 CHKiRet(nsd_ptcp.Construct(&pThis->pTcp));
541 /* construct net_ossl helper */
542 CHKiRet(net_ossl.Construct(&pThis->pNetOssl));
543 finalize_it:
544 ENDobjConstruct(nsd_ossl)
545
546
547 /* destructor for the nsd_ossl object */
548 BEGINobjDestruct(nsd_ossl) /* be sure to specify the object type also in END and CODESTART macros! */
549 CODESTARTobjDestruct(nsd_ossl);
550 DBGPRINTF("nsd_ossl_destruct: [%p] Mode %d\n", pThis, pThis->iMode);
551 if (pThis->iMode == 1) {
552 osslEndSess(pThis);
553 }
554 /* TODO MOVE Free SSL obj also if we do not have a session - or are NOT in TLS mode! */
555 if (pThis->pNetOssl->ssl != NULL) {
556 DBGPRINTF("nsd_ossl_destruct: [%p] FREE pThis->pNetOssl->ssl \n", pThis);
557 /* If pTcp is NULL, we configure the BIO to close it on SSL_free
558 This is a purely defensive measure and is this case is not actually expected
559 */
560 assert(pThis->pTcp != NULL);
561 if (pThis->pTcp == NULL) {
562 BIO *bio = SSL_get_rbio(pThis->pNetOssl->ssl);
563 if (bio != NULL) {
564 BIO_set_close(bio, BIO_CLOSE);
565 }
566 }
567 SSL_free(pThis->pNetOssl->ssl);
568 pThis->pNetOssl->ssl = NULL;
569 }
570 /**/
571 if (pThis->pTcp != NULL) {
572 nsd_ptcp.Destruct(&pThis->pTcp);
573 }
574 if (pThis->pNetOssl != NULL) {
575 net_ossl.Destruct(&pThis->pNetOssl);
576 }
577
578 free(pThis->pszConnectHost);
579 free(pThis->pszRcvBuf);
580 free(pThis->remoteSNI);
581 ENDobjDestruct(nsd_ossl)
582
583
584 /* Set the driver mode. For us, this has the following meaning:
585 * 0 - work in plain tcp mode, without tls (e.g. before a STARTTLS)
586 * 1 - work in TLS mode
587 * rgerhards, 2008-04-28
588 */
589 static rsRetVal SetMode(nsd_t *pNsd, const int mode) {
590 DEFiRet;
591 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
592
593 ISOBJ_TYPE_assert((pThis), nsd_ossl);
594 if (mode != 0 && mode != 1) {
595 LogError(0, RS_RET_INVALID_DRVR_MODE,
596 "error: driver mode %d not supported by"
597 " ossl netstream driver",
598 mode);
599 }
600 pThis->iMode = mode;
601
602 RETiRet;
603 }
604
605 /* Set the authentication mode. For us, the following is supported:
606 * anon - no certificate checks whatsoever (discouraged, but supported)
607 * x509/certvalid - (just) check certificate validity
608 * x509/fingerprint - certificate fingerprint
609 * x509/name - cerfificate name check
610 * mode == NULL is valid and defaults to x509/name
611 * rgerhards, 2008-05-16
612 */
613 static rsRetVal SetAuthMode(nsd_t *const pNsd, uchar *const mode) {
614 DEFiRet;
615 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
616
617 ISOBJ_TYPE_assert((pThis), nsd_ossl);
618 if (mode == NULL || !strcasecmp((char *)mode, "x509/name")) {
619 pThis->pNetOssl->authMode = OSSL_AUTH_CERTNAME;
620 } else if (!strcasecmp((char *)mode, "x509/fingerprint")) {
621 pThis->pNetOssl->authMode = OSSL_AUTH_CERTFINGERPRINT;
622 } else if (!strcasecmp((char *)mode, "x509/certvalid")) {
623 pThis->pNetOssl->authMode = OSSL_AUTH_CERTVALID;
624 } else if (!strcasecmp((char *)mode, "anon")) {
625 pThis->pNetOssl->authMode = OSSL_AUTH_CERTANON;
626
627 } else {
628 LogError(0, RS_RET_VALUE_NOT_SUPPORTED,
629 "error: authentication mode '%s' not supported by "
630 "ossl netstream driver",
631 mode);
632 ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED);
633 }
634
635 dbgprintf("SetAuthMode: Set Mode %s/%d\n", mode, pThis->pNetOssl->authMode);
636
637 finalize_it:
638 RETiRet;
639 }
640
641
642 /* Set the PermitExpiredCerts mode. For us, the following is supported:
643 * on - fail if certificate is expired
644 * off - ignore expired certificates
645 * warn - warn if certificate is expired
646 * alorbach, 2018-12-20
647 */
648 static rsRetVal SetPermitExpiredCerts(nsd_t *pNsd, uchar *mode) {
649 DEFiRet;
650 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
651
652 ISOBJ_TYPE_assert((pThis), nsd_ossl);
653 /* default is set to off! */
654 if (mode == NULL || !strcasecmp((char *)mode, "off")) {
655 pThis->permitExpiredCerts = OSSL_EXPIRED_DENY;
656 } else if (!strcasecmp((char *)mode, "warn")) {
657 pThis->permitExpiredCerts = OSSL_EXPIRED_WARN;
658 } else if (!strcasecmp((char *)mode, "on")) {
659 pThis->permitExpiredCerts = OSSL_EXPIRED_PERMIT;
660 } else {
661 LogError(0, RS_RET_VALUE_NOT_SUPPORTED,
662 "error: permitexpiredcerts mode '%s' not supported by "
663 "ossl netstream driver",
664 mode);
665 ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED);
666 }
667
668 dbgprintf("SetPermitExpiredCerts: Set Mode %s/%d\n", mode, pThis->permitExpiredCerts);
669
670 /* TODO: clear stored IDs! */
671
672 finalize_it:
673 RETiRet;
674 }
675
676
677 /* Set permitted peers. It is depending on the auth mode if this are
678 * fingerprints or names. -- rgerhards, 2008-05-19
679 */
680 static rsRetVal SetPermPeers(nsd_t *pNsd, permittedPeers_t *pPermPeers) {
681 DEFiRet;
682 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
683
684 ISOBJ_TYPE_assert((pThis), nsd_ossl);
685 if (pPermPeers == NULL) FINALIZE;
686
687 if (pThis->pNetOssl->authMode != OSSL_AUTH_CERTFINGERPRINT && pThis->pNetOssl->authMode != OSSL_AUTH_CERTNAME) {
688 LogError(0, RS_RET_VALUE_NOT_IN_THIS_MODE,
689 "authentication not supported by "
690 "ossl netstream driver in the configured authentication mode - ignored");
691 ABORT_FINALIZE(RS_RET_VALUE_NOT_IN_THIS_MODE);
692 }
693 pThis->pNetOssl->pPermPeers = pPermPeers;
694
695 finalize_it:
696 RETiRet;
697 }
698
699
700 /* Provide access to the underlying OS socket. This is primarily
701 * useful for other drivers (like nsd_ossl) who utilize ourselfs
702 * for some of their functionality. -- rgerhards, 2008-04-18
703 */
704 static rsRetVal SetSock(nsd_t *pNsd, int sock) {
705 DEFiRet;
706 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
707 ISOBJ_TYPE_assert((pThis), nsd_ossl);
708 assert(sock >= 0);
709
710 DBGPRINTF("SetSock for [%p]: Setting sock %d\n", pNsd, sock);
711 nsd_ptcp.SetSock(pThis->pTcp, sock);
712
713 RETiRet;
714 }
715
716
717 /* Keep Alive Options
718 */
719 static rsRetVal SetKeepAliveIntvl(nsd_t *pNsd, int keepAliveIntvl) {
720 DEFiRet;
721 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
722
723 ISOBJ_TYPE_assert((pThis), nsd_ossl);
724 assert(keepAliveIntvl >= 0);
725
726 dbgprintf("SetKeepAliveIntvl: keepAliveIntvl=%d\n", keepAliveIntvl);
727 nsd_ptcp.SetKeepAliveIntvl(pThis->pTcp, keepAliveIntvl);
728
729 RETiRet;
730 }
731
732
733 /* Keep Alive Options
734 */
735 static rsRetVal SetKeepAliveProbes(nsd_t *pNsd, int keepAliveProbes) {
736 DEFiRet;
737 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
738
739 ISOBJ_TYPE_assert((pThis), nsd_ossl);
740 assert(keepAliveProbes >= 0);
741
742 dbgprintf("SetKeepAliveProbes: keepAliveProbes=%d\n", keepAliveProbes);
743 nsd_ptcp.SetKeepAliveProbes(pThis->pTcp, keepAliveProbes);
744
745 RETiRet;
746 }
747
748
749 /* Keep Alive Options
750 */
751 static rsRetVal SetKeepAliveTime(nsd_t *pNsd, int keepAliveTime) {
752 DEFiRet;
753 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
754
755 ISOBJ_TYPE_assert((pThis), nsd_ossl);
756 assert(keepAliveTime >= 0);
757
758 dbgprintf("SetKeepAliveTime: keepAliveTime=%d\n", keepAliveTime);
759 nsd_ptcp.SetKeepAliveTime(pThis->pTcp, keepAliveTime);
760
761 RETiRet;
762 }
763
764
765 /* abort a connection. This is meant to be called immediately
766 * before the Destruct call. -- rgerhards, 2008-03-24
767 */
768 static rsRetVal Abort(nsd_t *pNsd) {
769 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
770 DEFiRet;
771
772 ISOBJ_TYPE_assert((pThis), nsd_ossl);
773
774 if (pThis->iMode == 0) {
775 nsd_ptcp.Abort(pThis->pTcp);
776 }
777
778 RETiRet;
779 }
780
781
782 /* Callback after netstrm obj init in nsd_ptcp - permits us to add some data */
783 static rsRetVal LstnInitDrvr(netstrm_t *const pThis) {
784 DEFiRet;
785 nsd_ossl_t *pNsdOssl = (nsd_ossl_t *)pThis->pDrvrData;
786 /* Create main CTX Object. Use SSLv23_method for < Openssl 1.1.0 and TLS_method for all newer versions! */
787 #if OPENSSL_VERSION_NUMBER < 0x10100000L
788 CHKiRet(net_ossl.osslCtxInit(pNsdOssl->pNetOssl, SSLv23_method()));
789 #else
790 CHKiRet(net_ossl.osslCtxInit(pNsdOssl->pNetOssl, TLS_method()));
791 #endif
792 // Apply PriorityString after Ctx Creation
793 applyGnutlsPriorityString(pNsdOssl);
794 finalize_it:
795 RETiRet;
796 }
797
798
799 /* initialize the tcp socket for a listner
800 * Here, we use the ptcp driver - because there is nothing special
801 * at this point with OpenSSL. Things become special once we accept
802 * a session, but not during listener setup.
803 */
804 static rsRetVal LstnInit(netstrms_t *pNS,
805 void *pUsr,
806 rsRetVal (*fAddLstn)(void *, netstrm_t *),
807 const int iSessMax,
808 const tcpLstnParams_t *const cnf_params) {
809 DEFiRet;
810
811 dbgprintf("LstnInit for openssl: entering LstnInit (%p) for %s:%s SessMax=%d\n", fAddLstn, cnf_params->pszAddr,
812 cnf_params->pszPort, iSessMax);
813
814 pNS->fLstnInitDrvr = LstnInitDrvr;
815 /* Init TCP Listener using base ptcp class */
816 iRet = nsd_ptcp.LstnInit(pNS, pUsr, fAddLstn, iSessMax, cnf_params);
817 RETiRet;
818 }
819
820
821 /* This function checks if the connection is still alive - well, kind of...
822 * This is a dummy here. For details, check function common in ptcp driver.
823 * rgerhards, 2008-06-09
824 */
825 static rsRetVal CheckConnection(nsd_t __attribute__((unused)) * pNsd) {
826 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
827 ISOBJ_TYPE_assert(pThis, nsd_ossl);
828
829 dbgprintf("CheckConnection for %p\n", pNsd);
830 return nsd_ptcp.CheckConnection(pThis->pTcp);
831 }
832
833
834 /* Provide access to the underlying OS socket.
835 */
836 static rsRetVal GetSock(nsd_t *pNsd, int *pSock) {
837 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
838 return nsd_ptcp.GetSock(pThis->pTcp, pSock);
839 }
840
841 /* get the remote hostname. The returned hostname must be freed by the caller.
842 * rgerhards, 2008-04-25
843 */
844 static rsRetVal GetRemoteHName(nsd_t *pNsd, uchar **ppszHName) {
845 DEFiRet;
846 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
847 ISOBJ_TYPE_assert(pThis, nsd_ossl);
848 iRet = nsd_ptcp.GetRemoteHName(pThis->pTcp, ppszHName);
849 dbgprintf("GetRemoteHName for %p = %s\n", pNsd, *ppszHName);
850 RETiRet;
851 }
852
853
854 /* Provide access to the sockaddr_storage of the remote peer. This
855 * is needed by the legacy ACL system. --- gerhards, 2008-12-01
856 */
857 static rsRetVal GetRemAddr(nsd_t *pNsd, struct sockaddr_storage **ppAddr) {
858 DEFiRet;
859 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
860 ISOBJ_TYPE_assert(pThis, nsd_ossl);
861 iRet = nsd_ptcp.GetRemAddr(pThis->pTcp, ppAddr);
862 dbgprintf("GetRemAddr for %p\n", pNsd);
863 RETiRet;
864 }
865
866
867 /* get the remote host's IP address. Caller must Destruct the object. */
868 static rsRetVal GetRemoteIP(nsd_t *pNsd, prop_t **ip) {
869 DEFiRet;
870 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
871 ISOBJ_TYPE_assert(pThis, nsd_ossl);
872 iRet = nsd_ptcp.GetRemoteIP(pThis->pTcp, ip);
873 dbgprintf("GetRemoteIP for %p\n", pNsd);
874 RETiRet;
875 }
876
877
878 /* Perform all necessary checks after Handshake
879 */
880 rsRetVal osslPostHandshakeCheck(nsd_ossl_t *pNsd) {
881 DEFiRet;
882 uchar *fromHostIP = NULL;
883 char szDbg[255];
884 const SSL_CIPHER *sslCipher;
885
886 nsd_ptcp.GetRemoteHName((nsd_t *)pNsd->pTcp, &fromHostIP);
887
888 /* Some extra output for debugging openssl */
889 if (SSL_get_shared_ciphers(pNsd->pNetOssl->ssl, szDbg, sizeof szDbg) != NULL)
890 dbgprintf("osslPostHandshakeCheck: Debug Shared ciphers = %s\n", szDbg);
891
892 #if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(ENABLE_WOLFSSL)
893 if (SSL_get_shared_curve(pNsd->pNetOssl->ssl, -1) == 0) {
894 // This is not a failure
895 LogMsg(0, RS_RET_NO_ERRCODE, LOG_INFO,
896 "nsd_ossl: "
897 "Information, no shared curve between syslog client '%s' and server",
898 fromHostIP);
899 }
900 #endif
901 dbgprintf("osslPostHandshakeCheck: Debug Protocol Version: %s\n", SSL_get_version(pNsd->pNetOssl->ssl));
902
903 sslCipher = (const SSL_CIPHER *)SSL_get_current_cipher(pNsd->pNetOssl->ssl);
904 if (sslCipher != NULL) {
905 if (SSL_CIPHER_get_version(sslCipher) == NULL) {
906 LogError(0, RS_RET_NO_ERRCODE,
907 "nsd_ossl:"
908 "TLS version mismatch between syslog client '%s' and server.",
909 fromHostIP);
910 }
911 dbgprintf("osslPostHandshakeCheck: Debug Cipher Version: %s Name: %s\n", SSL_CIPHER_get_version(sslCipher),
912 SSL_CIPHER_get_name(sslCipher));
913 } else {
914 LogError(0, RS_RET_NO_ERRCODE, "nsd_ossl:No shared ciphers between syslog client '%s' and server.", fromHostIP);
915 }
916
917 FINALIZE;
918
919 finalize_it:
920 if (fromHostIP != NULL) {
921 free(fromHostIP);
922 }
923 RETiRet;
924 }
925
926
927 /* Perform all necessary actions for Handshake
928 */
929 rsRetVal osslHandshakeCheck(nsd_ossl_t *pNsd) {
930 DEFiRet;
931 uchar *fromHostIP = NULL;
932 int remotePort = -1;
933 uchar remotePortStr[8];
934 int res, resErr;
935 dbgprintf("osslHandshakeCheck: Starting TLS Handshake for ssl[%p]\n", (void *)pNsd->pNetOssl->ssl);
936
937 if (pNsd->pNetOssl->sslState == osslServer) {
938 /* Handle Server SSL Object */
939 if ((res = SSL_accept(pNsd->pNetOssl->ssl)) <= 0) {
940 /* Obtain SSL Error code */
941 nsd_ptcp.GetRemoteHName(pNsd->pTcp, &fromHostIP);
942 nsd_ptcp.GetRemotePort(pNsd->pTcp, &remotePort);
943 resErr = SSL_get_error(pNsd->pNetOssl->ssl, res);
944 if (resErr == SSL_ERROR_WANT_READ || resErr == SSL_ERROR_WANT_WRITE) {
945 pNsd->rtryCall = osslRtry_handshake;
946 pNsd->rtryOsslErr = resErr; /* Store SSL ErrorCode into*/
947 dbgprintf(
948 "osslHandshakeCheck: OpenSSL Server handshake does not complete "
949 "immediately - setting to retry (this is OK and normal)\n");
950 FINALIZE;
951 } else {
952 nsd_ptcp.FmtRemotePortStr(remotePort, remotePortStr, sizeof(remotePortStr));
953 if (resErr == SSL_ERROR_SYSCALL) {
954 dbgprintf(
955 "osslHandshakeCheck: OpenSSL Server handshake failed with "
956 "SSL_ERROR_SYSCALL - Aborting handshake.\n");
957 nsd_ossl_lastOpenSSLErrorMsg(pNsd, res, pNsd->pNetOssl->ssl, LOG_WARNING,
958 "osslHandshakeCheck Server", "SSL_accept");
959 LogMsg(0, RS_RET_NO_ERRCODE, LOG_WARNING,
960 "nsd_ossl:TLS session terminated with remote client '%s:%s': "
961 "Handshake failed with SSL_ERROR_SYSCALL",
962 fromHostIP, remotePortStr);
963 osslAbortSess(pNsd);
964 ABORT_FINALIZE(RS_RET_NO_ERRCODE);
965 } else {
966 nsd_ossl_lastOpenSSLErrorMsg(pNsd, res, pNsd->pNetOssl->ssl, LOG_ERR, "osslHandshakeCheck Server",
967 "SSL_accept");
968 LogMsg(0, RS_RET_NO_ERRCODE, LOG_WARNING,
969 "nsd_ossl:TLS session terminated with remote client '%s:%s': "
970 "Handshake failed with error code: %d",
971 fromHostIP, remotePortStr, resErr);
972 osslAbortSess(pNsd);
973 ABORT_FINALIZE(RS_RET_NO_ERRCODE);
974 }
975 }
976 }
977 } else {
978 /* Handle Client SSL Object */
979 if ((res = SSL_do_handshake(pNsd->pNetOssl->ssl)) <= 0) {
980 /* Obtain SSL Error code */
981 nsd_ptcp.GetRemoteHName(pNsd->pTcp, &fromHostIP);
982 resErr = SSL_get_error(pNsd->pNetOssl->ssl, res);
983 if (resErr == SSL_ERROR_WANT_READ || resErr == SSL_ERROR_WANT_WRITE) {
984 pNsd->rtryCall = osslRtry_handshake;
985 pNsd->rtryOsslErr = resErr; /* Store SSL ErrorCode into*/
986 dbgprintf(
987 "osslHandshakeCheck: OpenSSL Client handshake does not complete "
988 "immediately - setting to retry (this is OK and normal)\n");
989 FINALIZE;
990 } else if (resErr == SSL_ERROR_SYSCALL) {
991 dbgprintf(
992 "osslHandshakeCheck: OpenSSL Client handshake failed with SSL_ERROR_SYSCALL "
993 "- Aborting handshake.\n");
994 nsd_ossl_lastOpenSSLErrorMsg(pNsd, res, pNsd->pNetOssl->ssl, LOG_WARNING, "osslHandshakeCheck Client",
995 "SSL_do_handshake");
996 osslAbortSess(pNsd);
997 ABORT_FINALIZE(RS_RET_NO_ERRCODE /*RS_RET_RETRY*/);
998 } else {
999 dbgprintf(
1000 "osslHandshakeCheck: OpenSSL Client handshake failed with %d "
1001 "- Aborting handshake.\n",
1002 resErr);
1003 nsd_ossl_lastOpenSSLErrorMsg(pNsd, res, pNsd->pNetOssl->ssl, LOG_ERR, "osslHandshakeCheck Client",
1004 "SSL_do_handshake");
1005 nsd_ptcp.GetRemotePort(pNsd->pTcp, &remotePort);
1006 nsd_ptcp.FmtRemotePortStr(remotePort, remotePortStr, sizeof(remotePortStr));
1007 LogMsg(0, RS_RET_NO_ERRCODE, LOG_WARNING,
1008 "nsd_ossl:TLS session terminated with remote syslog server '%s:%s':"
1009 "Handshake failed with error code: %d",
1010 fromHostIP, remotePortStr, resErr);
1011 osslAbortSess(pNsd);
1012 ABORT_FINALIZE(RS_RET_NO_ERRCODE);
1013 }
1014 }
1015 }
1016
1017 /* Do post handshake stuff */
1018 CHKiRet(osslPostHandshakeCheck(pNsd));
1019
1020 /* Now check authorization */
1021 CHKiRet(osslChkPeerAuth(pNsd));
1022 finalize_it:
1023 if (fromHostIP != NULL) {
1024 free(fromHostIP);
1025 }
1026 if (iRet == RS_RET_OK) {
1027 /* If no error occurred, set socket to SSL mode */
1028 pNsd->iMode = 1;
1029 }
1030
1031 RETiRet;
1032 }
1033
1034
1035 /* accept an incoming connection request - here, we do the usual accept
1036 * handling. TLS specific handling is done thereafter (and if we run in TLS
1037 * mode at this time).
1038 * rgerhards, 2008-04-25
1039 */
1040 static rsRetVal AcceptConnReq(nsd_t *pNsd, nsd_t **ppNew, char *const connInfo) {
1041 DEFiRet;
1042 nsd_ossl_t *pNew = NULL;
1043 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
1044
1045 ISOBJ_TYPE_assert((pThis), nsd_ossl);
1046 CHKiRet(nsd_osslConstruct(&pNew));
1047 CHKiRet(nsd_ptcp.Destruct(&pNew->pTcp));
1048 dbgprintf("AcceptConnReq for [%p]: Accepting connection ... \n", (void *)pThis);
1049 CHKiRet(nsd_ptcp.AcceptConnReq(pThis->pTcp, &pNew->pTcp, connInfo));
1050
1051 if (pThis->iMode == 0) {
1052 /*we are in non-TLS mode, so we are done */
1053 DBGPRINTF("AcceptConnReq: NOT in TLS mode!\n");
1054 *ppNew = (nsd_t *)pNew;
1055 FINALIZE;
1056 }
1057
1058 /* If we reach this point, we are in TLS mode */
1059 pNew->pNetOssl->authMode = pThis->pNetOssl->authMode;
1060 pNew->permitExpiredCerts = pThis->permitExpiredCerts;
1061 pNew->pNetOssl->pPermPeers = pThis->pNetOssl->pPermPeers;
1062 pNew->pNetOssl->bSANpriority = pThis->pNetOssl->bSANpriority;
1063 pNew->DrvrVerifyDepth = pThis->DrvrVerifyDepth;
1064 pNew->DrvrTlsRevocationCheck = pThis->DrvrTlsRevocationCheck;
1065 pNew->gnutlsPriorityString = pThis->gnutlsPriorityString;
1066 pNew->pNetOssl->ctx = pThis->pNetOssl->ctx;
1067 pNew->pNetOssl->ctx_is_copy = 1; // do not free on pNew Destruction
1068 CHKiRet(osslInitSession(pNew, osslServer));
1069
1070 /* Store nsd_ossl_t* reference in SSL obj
1071 * Index allocation: 0=pTcp, 1=permitExpiredCerts, 2=imdtls instance, 3=revocationCheck
1072 */
1073 SSL_set_ex_data(pNew->pNetOssl->ssl, 0, pNew->pTcp);
1074 SSL_set_ex_data(pNew->pNetOssl->ssl, 1, &pNew->permitExpiredCerts);
1075 SSL_set_ex_data(pNew->pNetOssl->ssl, 3, &pNew->DrvrTlsRevocationCheck);
1076
1077 /* We now do the handshake */
1078 CHKiRet(osslHandshakeCheck(pNew));
1079
1080 *ppNew = (nsd_t *)pNew;
1081 finalize_it:
1082 /* Accept appears to be done here */
1083 if (pNew != NULL) {
1084 DBGPRINTF("AcceptConnReq: END iRet = %d, pNew=[%p], pNsd->rtryCall=%d\n", iRet, pNew, pNew->rtryCall);
1085 }
1086 if (iRet != RS_RET_OK) {
1087 if (pNew != NULL) {
1088 nsd_osslDestruct(&pNew);
1089 }
1090 }
1091 RETiRet;
1092 }
1093
1094
1095 /* receive data from a tcp socket
1096 * The lenBuf parameter must contain the max buffer size on entry and contains
1097 * the number of octets read on exit. This function
1098 * never blocks, not even when called on a blocking socket. That is important
1099 * for client sockets, which are set to block during send, but should not
1100 * block when trying to read data. -- rgerhards, 2008-03-17
1101 * The function now follows the usual iRet calling sequence.
1102 * With GnuTLS, we may need to restart a recv() system call. If so, we need
1103 * to supply the SAME buffer on the retry. We can not assure this, as the
1104 * caller is free to call us with any buffer location (and in current
1105 * implementation, it is on the stack and extremely likely to change). To
1106 * work-around this problem, we allocate a buffer ourselfs and always receive
1107 * into that buffer. We pass data on to the caller only after we have received it.
1108 * To save some space, we allocate that internal buffer only when it is actually
1109 * needed, which means when we reach this function for the first time. To keep
1110 * the algorithm simple, we always supply data only from the internal buffer,
1111 * even if it is a single byte. As we have a stream, the caller must be prepared
1112 * to accept messages in any order, so we do not need to take care about this.
1113 * Please note that the logic also forces us to do some "faking" in select(), as
1114 * we must provide a fake "is ready for readign" status if we have data inside our
1115 * buffer. -- rgerhards, 2008-06-23
1116 */
1117 static rsRetVal Rcv(nsd_t *pNsd, uchar *pBuf, ssize_t *pLenBuf, int *const oserr, unsigned *const nextIODirection) {
1118 DEFiRet;
1119 ssize_t iBytesCopy; /* how many bytes are to be copied to the client buffer? */
1120 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
1121 ISOBJ_TYPE_assert(pThis, nsd_ossl);
1122 DBGPRINTF("Rcv for %p\n", pNsd);
1123
1124 if (pThis->bAbortConn) ABORT_FINALIZE(RS_RET_CONNECTION_ABORTREQ);
1125
1126 if (pThis->iMode == 0) {
1127 CHKiRet(nsd_ptcp.Rcv(pThis->pTcp, pBuf, pLenBuf, oserr, nextIODirection));
1128 FINALIZE;
1129 }
1130
1131 /* --- in TLS mode now --- */
1132 if (pThis->rtryCall == osslRtry_handshake) {
1133 /* note: we are in receive, so we acually will retry receive in any case */
1134 CHKiRet(doRetry(pThis));
1135 ABORT_FINALIZE(RS_RET_RETRY);
1136 }
1137
1138 /* Buffer logic applies only if we are in TLS mode. Here we
1139 * assume that we will switch from plain to TLS, but never back. This
1140 * assumption may be unsafe, but it is the model for the time being and I
1141 * do not see any valid reason why we should switch back to plain TCP after
1142 * we were in TLS mode. However, in that case we may lose something that
1143 * is already in the receive buffer ... risk accepted. -- rgerhards, 2008-06-23
1144 */
1145
1146 if (pThis->pszRcvBuf == NULL) {
1147 /* we have no buffer, so we need to malloc one */
1148 CHKmalloc(pThis->pszRcvBuf = malloc(NSD_OSSL_MAX_RCVBUF));
1149 pThis->lenRcvBuf = -1;
1150 }
1151
1152 /* now check if we have something in our buffer. If so, we satisfy
1153 * the request from buffer contents.
1154 */
1155 if (pThis->lenRcvBuf == -1) { /* no data present, must read */
1156 CHKiRet(osslRecordRecv(pThis, nextIODirection));
1157 }
1158
1159 if (pThis->lenRcvBuf == 0) { /* EOS */
1160 *oserr = errno;
1161 ABORT_FINALIZE(RS_RET_CLOSED);
1162 }
1163
1164 /* if we reach this point, data is present in the buffer and must be copied */
1165 iBytesCopy = pThis->lenRcvBuf - pThis->ptrRcvBuf;
1166 if (iBytesCopy > *pLenBuf) {
1167 iBytesCopy = *pLenBuf;
1168 } else {
1169 pThis->lenRcvBuf = -1; /* buffer will be emptied below */
1170 }
1171
1172 memcpy(pBuf, pThis->pszRcvBuf + pThis->ptrRcvBuf, iBytesCopy);
1173 pThis->ptrRcvBuf += iBytesCopy;
1174 *pLenBuf = iBytesCopy;
1175
1176 finalize_it:
1177 if (iRet != RS_RET_OK) {
1178 if (iRet == RS_RET_CLOSED) {
1179 if (pThis->pNetOssl->ssl != NULL) {
1180 /* Set SSL Shutdown */
1181 SSL_shutdown(pThis->pNetOssl->ssl);
1182 dbgprintf("osslRcv SSL_shutdown done\n");
1183 }
1184 } else if (iRet != RS_RET_RETRY) {
1185 /* We need to free the receive buffer in error error case unless a retry is wanted. , if we
1186 * allocated one. -- rgerhards, 2008-12-03 -- moved here by alorbach, 2015-12-01
1187 */
1188 *pLenBuf = 0;
1189 free(pThis->pszRcvBuf);
1190 pThis->pszRcvBuf = NULL;
1191 } else {
1192 /* RS_RET_RETRY | Check for SSL Shutdown */
1193 if (SSL_get_shutdown(pThis->pNetOssl->ssl) == SSL_RECEIVED_SHUTDOWN) {
1194 dbgprintf("osslRcv received SSL_RECEIVED_SHUTDOWN!\n");
1195 iRet = RS_RET_CLOSED;
1196
1197 /* Send Shutdown message back */
1198 SSL_shutdown(pThis->pNetOssl->ssl);
1199 }
1200 }
1201 }
1202 dbgprintf("osslRcv return. nsd %p, iRet %d, lenRcvBuf %d, ptrRcvBuf %d\n", pThis, iRet, pThis->lenRcvBuf,
1203 pThis->ptrRcvBuf);
1204 RETiRet;
1205 }
1206
1207
1208 /* send a buffer. On entry, pLenBuf contains the number of octets to
1209 * write. On exit, it contains the number of octets actually written.
1210 * If this number is lower than on entry, only a partial buffer has
1211 * been written.
1212 * rgerhards, 2008-03-19
1213 */
1214 static rsRetVal Send(nsd_t *pNsd, uchar *pBuf, ssize_t *pLenBuf) {
1215 DEFiRet;
1216 int iSent;
1217 int err;
1218 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
1219 DBGPRINTF("Send for %p\n", pNsd);
1220
1221 ISOBJ_TYPE_assert(pThis, nsd_ossl);
1222
1223 if (pThis->bAbortConn) ABORT_FINALIZE(RS_RET_CONNECTION_ABORTREQ);
1224
1225 if (pThis->iMode == 0) {
1226 CHKiRet(nsd_ptcp.Send(pThis->pTcp, pBuf, pLenBuf));
1227 FINALIZE;
1228 }
1229
1230 while (1) {
1231 iSent = SSL_write(pThis->pNetOssl->ssl, pBuf, *pLenBuf);
1232 if (iSent > 0) {
1233 *pLenBuf = iSent;
1234 break;
1235 } else {
1236 err = SSL_get_error(pThis->pNetOssl->ssl, iSent);
1237 if (err == SSL_ERROR_ZERO_RETURN) {
1238 DBGPRINTF("Send: SSL_ERROR_ZERO_RETURN received, connection closed by peer\n");
1239 ABORT_FINALIZE(RS_RET_CLOSED);
1240 } else if (err == SSL_ERROR_SYSCALL) {
1241 /* Output error and abort */
1242 nsd_ossl_lastOpenSSLErrorMsg(pThis, iSent, pThis->pNetOssl->ssl, LOG_INFO, "Send", "SSL_write");
1243 iRet = RS_RET_TLS_ERR_SYSCALL;
1244 /* Check for underlaying socket errors **/
1245 if (errno == ECONNRESET) {
1246 dbgprintf("Send: SSL_ERROR_SYSCALL Connection was reset by remote\n");
1247 /* Connection was dropped from remote site */
1248 iRet = RS_RET_CLOSED;
1249 } else {
1250 DBGPRINTF("Send: SSL_ERROR_SYSCALL Errno %d\n", errno);
1251 }
1252 ABORT_FINALIZE(iRet);
1253 } else if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE) {
1254 /* Output error and abort */
1255 nsd_ossl_lastOpenSSLErrorMsg(pThis, iSent, pThis->pNetOssl->ssl, LOG_ERR, "Send", "SSL_write");
1256 ABORT_FINALIZE(RS_RET_NO_ERRCODE);
1257 } else {
1258 /*
1259 * OpenSSL needs us to READ or WRITE to make progress.
1260 * In TLS 1.3, servers may send post-handshake messages (e.g. KeyUpdate)
1261 * which require the client to read before it can continue writing.
1262 * Use the buffered receive helper to preserve any application data.
1263 */
1264 if (err == SSL_ERROR_WANT_READ) {
1265 unsigned nextIODirection ATTR_UNUSED;
1266 rsRetVal rcvRet = osslRecordRecv(pThis, &nextIODirection);
1267 if (rcvRet == RS_RET_CLOSED) {
1268 ABORT_FINALIZE(RS_RET_CLOSED);
1269 } else if (rcvRet != RS_RET_OK && rcvRet != RS_RET_RETRY) {
1270 ABORT_FINALIZE(rcvRet);
1271 }
1272 if (SSL_get_shutdown(pThis->pNetOssl->ssl) == SSL_RECEIVED_SHUTDOWN) {
1273 dbgprintf("Send: detected SSL_RECEIVED_SHUTDOWN while handling WANT_READ\n");
1274 ABORT_FINALIZE(RS_RET_CLOSED);
1275 }
1276 /* Continue loop to retry SSL_write */
1277 } else {
1278 /* Check for SSL Shutdown */
1279 if (SSL_get_shutdown(pThis->pNetOssl->ssl) == SSL_RECEIVED_SHUTDOWN) {
1280 dbgprintf("osslRcv received SSL_RECEIVED_SHUTDOWN!\n");
1281 ABORT_FINALIZE(RS_RET_CLOSED);
1282 }
1283 }
1284 }
1285 }
1286 }
1287 finalize_it:
1288 RETiRet;
1289 }
1290
1291
1292 /* Enable KEEPALIVE handling on the socket.
1293 * rgerhards, 2009-06-02
1294 */
1295 static rsRetVal EnableKeepAlive(nsd_t *pNsd) {
1296 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
1297 ISOBJ_TYPE_assert(pThis, nsd_ossl);
1298 return nsd_ptcp.EnableKeepAlive(pThis->pTcp);
1299 }
1300
1301
1302 /* open a connection to a remote host (server). With OpenSSL, we always
1303 * open a plain tcp socket and then, if in TLS mode, do a handshake on it.
1304 */
1305 static rsRetVal Connect(nsd_t *pNsd, int family, uchar *port, uchar *host, char *device) {
1306 DEFiRet;
1307 DBGPRINTF("openssl: entering Connect family=%d, device=%s\n", family, device);
1308 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
1309 uchar *fromHostIP = NULL;
1310
1311 ISOBJ_TYPE_assert(pThis, nsd_ossl);
1312 assert(port != NULL);
1313 assert(host != NULL);
1314
1315 /* Create main CTX Object. Use SSLv23_method for < Openssl 1.1.0 and TLS_method for all newer versions! */
1316 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1317 CHKiRet(net_ossl.osslCtxInit(pThis->pNetOssl, SSLv23_method()));
1318 #else
1319 CHKiRet(net_ossl.osslCtxInit(pThis->pNetOssl, TLS_method()));
1320 #endif
1321 // Apply PriorityString after Ctx Creation
1322 applyGnutlsPriorityString(pThis);
1323
1324 // Perform TCP Connect
1325 CHKiRet(nsd_ptcp.Connect(pThis->pTcp, family, port, host, device));
1326
1327 if (pThis->iMode == 0) {
1328 /*we are in non-TLS mode, so we are done */
1329 DBGPRINTF("Connect: NOT in TLS mode!\n");
1330 FINALIZE;
1331 }
1332
1333 nsd_ptcp.GetRemoteHName((nsd_t *)pThis->pTcp, &fromHostIP);
1334
1335
1336 LogMsg(0, RS_RET_NO_ERRCODE, LOG_INFO,
1337 "nsd_ossl: "
1338 "TLS Connection initiated with remote syslog server '%s'.",
1339 fromHostIP);
1340 /*if we reach this point we are in tls mode */
1341 DBGPRINTF("Connect: TLS Mode\n");
1342
1343 /* Do SSL Session init */
1344 CHKiRet(osslInitSession(pThis, osslClient));
1345
1346 CHKiRet(SetServerNameIfPresent(pThis, host));
1347
1348 /* Store nsd_ossl_t* reference in SSL obj
1349 * Index allocation: 0=pTcp, 1=permitExpiredCerts, 2=imdtls instance, 3=revocationCheck
1350 */
1351 SSL_set_ex_data(pThis->pNetOssl->ssl, 0, pThis->pTcp);
1352 SSL_set_ex_data(pThis->pNetOssl->ssl, 1, &pThis->permitExpiredCerts);
1353 SSL_set_ex_data(pThis->pNetOssl->ssl, 3, &pThis->DrvrTlsRevocationCheck);
1354
1355 /* We now do the handshake */
1356 iRet = osslHandshakeCheck(pThis);
1357 finalize_it:
1358 if (fromHostIP != NULL) {
1359 free(fromHostIP);
1360 }
1361 /* Connect appears to be done here */
1362 dbgprintf("Connect: END iRet = %d, pThis=[%p], pNsd->rtryCall=%d\n", iRet, pThis, pThis->rtryCall);
1363 if (iRet != RS_RET_OK) {
1364 if (pThis->bHaveSess) {
1365 pThis->bHaveSess = 0;
1366 SSL_free(pThis->pNetOssl->ssl);
1367 pThis->pNetOssl->ssl = NULL;
1368 }
1369 }
1370 RETiRet;
1371 }
1372
1373 static rsRetVal SetGnutlsPriorityString(nsd_t *const pNsd, uchar *const gnutlsPriorityString) {
1374 DEFiRet;
1375 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
1376 ISOBJ_TYPE_assert(pThis, nsd_ossl);
1377
1378 #if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER) && !defined(ENABLE_WOLFSSL)
1379 sbool ApplySettings = 0;
1380 if ((gnutlsPriorityString != NULL && pThis->gnutlsPriorityString == NULL) ||
1381 (gnutlsPriorityString != NULL &&
1382 strcmp((const char *)pThis->gnutlsPriorityString, (const char *)gnutlsPriorityString) != 0)) {
1383 ApplySettings = 1;
1384 }
1385
1386 pThis->gnutlsPriorityString = gnutlsPriorityString;
1387 dbgprintf("gnutlsPriorityString: set to '%s' Apply %s\n",
1388 (gnutlsPriorityString != NULL ? (char *)gnutlsPriorityString : "NULL"),
1389 (ApplySettings == 1 ? "TRUE" : "FALSE"));
1390 if (ApplySettings == 1) {
1391 /* Apply Settings if necessary */
1392 applyGnutlsPriorityString(pThis);
1393 }
1394
1395 #else
1396 LogError(0, RS_RET_SYS_ERR,
1397 "Warning: TLS library does not support SSL_CONF_cmd API"
1398 "(maybe it is too old?). Cannot use gnutlsPriorityString ('%s'). For more see: "
1399 "https://www.rsyslog.com/doc/master/configuration/modules/imtcp.html#gnutlsprioritystring",
1400 gnutlsPriorityString);
1401 #endif
1402 RETiRet;
1403 }
1404
1405
1406 static rsRetVal applyGnutlsPriorityString(nsd_ossl_t *const pThis) {
1407 DEFiRet;
1408 ISOBJ_TYPE_assert(pThis, nsd_ossl);
1409
1410 #if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER) && !defined(ENABLE_WOLFSSL)
1411 /* Note: we disable unkonwn functions. The corresponding error message is
1412 * generated during SetGntuTLSPriorityString().
1413 */
1414 if (pThis->gnutlsPriorityString == NULL || pThis->pNetOssl->ctx == NULL) {
1415 FINALIZE;
1416 } else {
1417 CHKiRet(net_ossl.osslApplyTlscgfcmd(pThis->pNetOssl, pThis->gnutlsPriorityString));
1418 }
1419 #endif
1420
1421 #if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER) && !defined(ENABLE_WOLFSSL)
1422 finalize_it:
1423 #endif
1424 RETiRet;
1425 }
1426
1427 /* Set the driver cert extended key usage check setting, for now it is empty wrapper.
1428 * TODO: implement openSSL version
1429 * jvymazal, 2019-08-16
1430 */
1431 static rsRetVal SetCheckExtendedKeyUsage(nsd_t __attribute__((unused)) * pNsd, int ChkExtendedKeyUsage) {
1432 DEFiRet;
1433 if (ChkExtendedKeyUsage != 0) {
1434 LogError(0, RS_RET_VALUE_NOT_SUPPORTED,
1435 "error: driver ChkExtendedKeyUsage %d "
1436 "not supported by ossl netstream driver",
1437 ChkExtendedKeyUsage);
1438 ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED);
1439 }
1440 finalize_it:
1441 RETiRet;
1442 }
1443
1444 /* Set the driver name checking strictness
1445 * 0 - less strict per RFC 5280, section 4.1.2.6 - either SAN or CN match is good
1446 * 1 - more strict per RFC 6125 - if any SAN present it must match (CN is ignored)
1447 * jvymazal, 2019-08-16, csiltala, 2025-07-28
1448 */
1449 static rsRetVal SetPrioritizeSAN(nsd_t *pNsd, int prioritizeSan) {
1450 DEFiRet;
1451 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
1452
1453 ISOBJ_TYPE_assert((pThis), nsd_ossl);
1454 if (prioritizeSan != 0 && prioritizeSan != 1) {
1455 LogError(0, RS_RET_VALUE_NOT_SUPPORTED,
1456 "error: driver prioritizeSan %d "
1457 "not supported by ossl netstream driver",
1458 prioritizeSan);
1459 ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED);
1460 }
1461
1462 pThis->pNetOssl->bSANpriority = prioritizeSan;
1463
1464 finalize_it:
1465 RETiRet;
1466 }
1467
1468 /* Set the driver tls verifyDepth
1469 * alorbach, 2019-12-20
1470 */
1471 static rsRetVal SetTlsVerifyDepth(nsd_t *pNsd, int verifyDepth) {
1472 DEFiRet;
1473 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
1474
1475 ISOBJ_TYPE_assert((pThis), nsd_ossl);
1476 if (verifyDepth == 0) {
1477 FINALIZE;
1478 }
1479 assert(verifyDepth >= 2);
1480 pThis->DrvrVerifyDepth = verifyDepth;
1481
1482 finalize_it:
1483 RETiRet;
1484 }
1485
1486 /* Set TLS revocation check (OCSP/CRL)
1487 * 0 - disable revocation checking (default for compatibility)
1488 * 1 - enable revocation checking via OCSP (and CRL when implemented)
1489 */
1490 static rsRetVal SetTlsRevocationCheck(nsd_t *pNsd, int enabled) {
1491 DEFiRet;
1492 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
1493
1494 ISOBJ_TYPE_assert((pThis), nsd_ossl);
1495 pThis->DrvrTlsRevocationCheck = (enabled != 0) ? 1 : 0;
1496 dbgprintf("SetTlsRevocationCheck: revocation check %s\n", pThis->DrvrTlsRevocationCheck ? "enabled" : "disabled");
1497
1498 RETiRet;
1499 }
1500
1501
1502 static rsRetVal SetTlsCAFile(nsd_t *pNsd, const uchar *const caFile) {
1503 DEFiRet;
1504 nsd_ossl_t *const pThis = (nsd_ossl_t *)pNsd;
1505
1506 ISOBJ_TYPE_assert((pThis), nsd_ossl);
1507 free((void *)pThis->pNetOssl->pszCAFile);
1508 pThis->pNetOssl->pszCAFile = NULL;
1509
1510 if (caFile != NULL) {
1511 CHKmalloc(pThis->pNetOssl->pszCAFile = (const uchar *)strdup((const char *)caFile));
1512 }
1513
1514 finalize_it:
1515 RETiRet;
1516 }
1517
1518 static rsRetVal SetTlsCRLFile(nsd_t *pNsd, const uchar *const crlFile) {
1519 DEFiRet;
1520 nsd_ossl_t *const pThis = (nsd_ossl_t *)pNsd;
1521
1522 ISOBJ_TYPE_assert((pThis), nsd_ossl);
1523 free((void *)pThis->pNetOssl->pszCRLFile);
1524 pThis->pNetOssl->pszCRLFile = NULL;
1525
1526 if (crlFile != NULL) {
1527 CHKmalloc(pThis->pNetOssl->pszCRLFile = (const uchar *)strdup((const char *)crlFile));
1528 }
1529
1530 finalize_it:
1531 RETiRet;
1532 }
1533
1534
1535 static rsRetVal SetTlsKeyFile(nsd_t *pNsd, const uchar *const pszFile) {
1536 DEFiRet;
1537 nsd_ossl_t *const pThis = (nsd_ossl_t *)pNsd;
1538
1539 ISOBJ_TYPE_assert((pThis), nsd_ossl);
1540 free((void *)pThis->pNetOssl->pszKeyFile);
1541 pThis->pNetOssl->pszKeyFile = NULL;
1542
1543 if (pszFile != NULL) {
1544 CHKmalloc(pThis->pNetOssl->pszKeyFile = (const uchar *)strdup((const char *)pszFile));
1545 }
1546
1547 finalize_it:
1548 RETiRet;
1549 }
1550
1551 static rsRetVal SetTlsCertFile(nsd_t *pNsd, const uchar *const pszFile) {
1552 DEFiRet;
1553 nsd_ossl_t *const pThis = (nsd_ossl_t *)pNsd;
1554
1555 ISOBJ_TYPE_assert((pThis), nsd_ossl);
1556 free((void *)pThis->pNetOssl->pszCertFile);
1557 pThis->pNetOssl->pszCertFile = NULL;
1558
1559 if (pszFile != NULL) {
1560 CHKmalloc(pThis->pNetOssl->pszCertFile = (const uchar *)strdup((const char *)pszFile));
1561 }
1562
1563 finalize_it:
1564 RETiRet;
1565 }
1566
1567 /* Set the TLS SNI of the remote server */
1568 static rsRetVal SetRemoteSNI(nsd_t *pNsd, uchar *remoteSNI) {
1569 DEFiRet;
1570 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
1571
1572 ISOBJ_TYPE_assert(pThis, nsd_ossl);
1573
1574 free(pThis->remoteSNI);
1575 if (remoteSNI == NULL) {
1576 pThis->remoteSNI = NULL;
1577 } else {
1578 CHKmalloc(pThis->remoteSNI = (uchar *)strdup((char *)remoteSNI));
1579 }
1580
1581 finalize_it:
1582 RETiRet;
1583 }
1584
1585 static rsRetVal GetRemotePort(nsd_t *pNsd, int *port) {
1586 nsd_ossl_t *pThis = (nsd_ossl_t *)pNsd;
1587 ISOBJ_TYPE_assert((pThis), nsd_ossl);
1588 return nsd_ptcp.GetRemotePort(pThis->pTcp, port);
1589 }
1590
1591 static rsRetVal FmtRemotePortStr(const int port, uchar *const buf, const size_t len) {
1592 return nsd_ptcp.FmtRemotePortStr(port, buf, len);
1593 }
1594
1595
1596 /* queryInterface function */
1597 BEGINobjQueryInterface(nsd_ossl)
1598 CODESTARTobjQueryInterface(nsd_ossl);
1599 if (pIf->ifVersion != nsdCURR_IF_VERSION) { /* check for current version, increment on each change */
1600 ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED);
1601 }
1602
1603 /* ok, we have the right interface, so let's fill it
1604 * Please note that we may also do some backwards-compatibility
1605 * work here (if we can support an older interface version - that,
1606 * of course, also affects the "if" above).
1607 */
1608 pIf->Construct = (rsRetVal(*)(nsd_t **))nsd_osslConstruct;
1609 pIf->Destruct = (rsRetVal(*)(nsd_t **))nsd_osslDestruct;
1610 pIf->Abort = Abort;
1611 pIf->LstnInit = LstnInit;
1612 pIf->AcceptConnReq = AcceptConnReq;
1613 pIf->Rcv = Rcv;
1614 pIf->Send = Send;
1615 pIf->Connect = Connect;
1616 pIf->GetSock = GetSock;
1617 pIf->SetSock = SetSock;
1618 pIf->SetMode = SetMode;
1619 pIf->SetAuthMode = SetAuthMode;
1620 pIf->SetPermitExpiredCerts = SetPermitExpiredCerts;
1621 pIf->SetPermPeers = SetPermPeers;
1622 pIf->CheckConnection = CheckConnection;
1623 pIf->GetRemoteHName = GetRemoteHName;
1624 pIf->GetRemoteIP = GetRemoteIP;
1625 pIf->GetRemAddr = GetRemAddr;
1626 pIf->EnableKeepAlive = EnableKeepAlive;
1627 pIf->SetKeepAliveIntvl = SetKeepAliveIntvl;
1628 pIf->SetKeepAliveProbes = SetKeepAliveProbes;
1629 pIf->SetKeepAliveTime = SetKeepAliveTime;
1630 pIf->SetGnutlsPriorityString = SetGnutlsPriorityString; /* we don't NEED this interface! */
1631 pIf->SetCheckExtendedKeyUsage = SetCheckExtendedKeyUsage; /* we don't NEED this interface! */
1632 pIf->SetPrioritizeSAN = SetPrioritizeSAN;
1633 pIf->SetTlsVerifyDepth = SetTlsVerifyDepth;
1634 pIf->SetTlsRevocationCheck = SetTlsRevocationCheck;
1635 pIf->SetTlsCAFile = SetTlsCAFile;
1636 pIf->SetTlsCRLFile = SetTlsCRLFile;
1637 pIf->SetTlsKeyFile = SetTlsKeyFile;
1638 pIf->SetTlsCertFile = SetTlsCertFile;
1639 pIf->GetRemotePort = GetRemotePort;
1640 pIf->FmtRemotePortStr = FmtRemotePortStr;
1641 pIf->SetRemoteSNI = SetRemoteSNI;
1642
1643 finalize_it:
1644 ENDobjQueryInterface(nsd_ossl)
1645
1646
1647 /* exit our class
1648 */
1649 BEGINObjClassExit(nsd_ossl, OBJ_IS_LOADABLE_MODULE) /* CHANGE class also in END MACRO! */
1650 CODESTARTObjClassExit(nsd_ossl);
1651 /* release objects we no longer need */
1652 objRelease(net_ossl, CORE_COMPONENT);
1653 objRelease(nsd_ptcp, LM_NSD_PTCP_FILENAME);
1654 objRelease(net, LM_NET_FILENAME);
1655 objRelease(glbl, CORE_COMPONENT);
1656 objRelease(datetime, CORE_COMPONENT);
1657 ENDObjClassExit(nsd_ossl)
1658
1659
1660 /* Initialize the nsd_ossl class. Must be called as the very first method
1661 * before anything else is called inside this class.
1662 */
1663 BEGINObjClassInit(nsd_ossl, 1, OBJ_IS_LOADABLE_MODULE) /* class, version */
1664 /* request objects we use */
1665 CHKiRet(objUse(datetime, CORE_COMPONENT));
1666 CHKiRet(objUse(glbl, CORE_COMPONENT));
1667 CHKiRet(objUse(net, LM_NET_FILENAME));
1668 CHKiRet(objUse(nsd_ptcp, LM_NSD_PTCP_FILENAME));
1669 CHKiRet(objUse(net_ossl, CORE_COMPONENT));
1670 ENDObjClassInit(nsd_ossl)
1671
1672
1673 /* --------------- here now comes the plumbing that makes as a library module --------------- */
1674
1675
1676 BEGINmodExit
1677 CODESTARTmodExit;
1678 nsd_osslClassExit();
1679 net_osslClassExit();
1680 ENDmodExit
1681
1682
1683 BEGINqueryEtryPt
1684 CODESTARTqueryEtryPt;
1685 CODEqueryEtryPt_STD_LIB_QUERIES;
1686 ENDqueryEtryPt
1687
1688
1689 BEGINmodInit()
1690 CODESTARTmodInit;
1691 *ipIFVersProvided = CURR_MOD_IF_VERSION; /* we only support the current interface specification */
1692
1693 /* Initialize all classes that are in our module - this includes ourselfs */
1694 DBGPRINTF("modInit\n");
1695 CHKiRet(net_osslClassInit(pModInfo)); /* must be done after tcps_sess, as we use it */
1696 CHKiRet(nsd_osslClassInit(pModInfo)); /* must be done after tcps_sess, as we use it */
1697 ENDmodInit