LCOV - code coverage report
Current view: top level - include/freetds/tds - tls.h (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 9 9 100.0 %
Date: 2026-11-25 04:57:11 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2015  Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : #ifndef _tdsguard_hpUeh3TzYOzN1FtT39tMHz_
      21             : #define _tdsguard_hpUeh3TzYOzN1FtT39tMHz_
      22             : 
      23             : #ifndef _tdsguard_hfOrWb5znoUCWdBPoNQvqN_
      24             : #error tds.h must be included before tls.h
      25             : #endif
      26             : 
      27             : #ifdef HAVE_GNUTLS
      28             : #  if defined(_THREAD_SAFE) && defined(TDS_HAVE_PTHREAD_MUTEX)
      29             : #    include <freetds/thread.h>
      30             : #    ifndef GNUTLS_USE_NETTLE
      31             : #      include <gcrypt.h>
      32             : #    endif
      33             : #  endif
      34             : #  include <gnutls/gnutls.h>
      35             : #  include <gnutls/x509.h>
      36             : #  include <gnutls/crypto.h>
      37             : #elif defined(HAVE_OPENSSL)
      38             : #  include <openssl/ssl.h>
      39             : #  include <openssl/x509v3.h>
      40             : #  include <openssl/err.h>
      41             : #endif
      42             : 
      43             : #include <freetds/pushvis.h>
      44             : 
      45             : #if defined(HAVE_GNUTLS) || defined(HAVE_OPENSSL)
      46             : 
      47             : /*
      48             :  * Common definitions
      49             :  */
      50             : TDSRET tds_ssl_init(TDSSOCKET *tds, bool full);
      51             : void tds_ssl_deinit(TDSCONNECTION *conn);
      52             : /**
      53             :  * Get TLS channel binding application data (RFC 5929).
      54             :  * The data includes the type prefix, either "tls-unique:" or, for TLS 1.3
      55             :  * where tls-unique is not defined, "tls-server-end-point:".
      56             :  * @return length of data, 0 if not available
      57             :  */
      58             : size_t tds_ssl_get_cb(TDSCONNECTION * conn, void *cb, size_t cblen);
      59             : 
      60             : #  ifdef HAVE_GNUTLS
      61             : /*
      62             :  * GnuTLS definitions
      63             :  */
      64             : static inline int
      65             : tds_ssl_pending(TDSCONNECTION *conn)
      66             : {
      67      117558 :         return gnutls_record_check_pending((gnutls_session_t) conn->tls_session);
      68             : }
      69             : 
      70             : static inline int
      71       49564 : tds_ssl_read(TDSCONNECTION *conn, unsigned char *buf, int buflen)
      72             : {
      73             :         int rc;
      74             : 
      75             :         /*
      76             :          * GNUTLS_E_AGAIN is returned also after processing a post handshake
      77             :          * message (like TLS 1.3 NewSessionTicket) with no application data,
      78             :          * in this case try again.
      79             :          * Pull functions block and their failures are reported as
      80             :          * GNUTLS_E_PULL_ERROR (see tds_pull_errno), so each retry consumes
      81             :          * data from the network.
      82             :          */
      83             :         do {
      84       49564 :                 rc = gnutls_record_recv((gnutls_session_t) conn->tls_session, buf, buflen);
      85       49564 :         } while ((rc == GNUTLS_E_AGAIN || rc == GNUTLS_E_INTERRUPTED) && !TDS_IS_SOCKET_INVALID(conn->s));
      86       49564 :         return rc;
      87             : }
      88             : 
      89             : static inline int
      90             : tds_ssl_write(TDSCONNECTION *conn, const unsigned char *buf, int buflen)
      91             : {
      92       20357 :         return gnutls_record_send((gnutls_session_t) conn->tls_session, buf, buflen);
      93             : }
      94             : 
      95             : #  else
      96             : /*
      97             :  * OpenSSL definitions
      98             :  */
      99             : 
     100             : /* compatibility for LibreSSL 2.7  */
     101             : #ifdef LIBRESSL_VERSION_NUMBER
     102             : #define TLS_ST_OK SSL_ST_OK
     103             : #endif
     104             : 
     105             : static inline int
     106             : tds_ssl_pending(TDSCONNECTION *conn)
     107             : {
     108       47095 :         return SSL_pending((SSL *) conn->tls_session);
     109             : }
     110             : 
     111             : static inline int
     112             : tds_ssl_read(TDSCONNECTION *conn, unsigned char *buf, int buflen)
     113             : {
     114       39196 :         return SSL_read((SSL *) conn->tls_session, buf, buflen);
     115             : }
     116             : 
     117             : static inline int
     118             : tds_ssl_write(TDSCONNECTION *conn, const unsigned char *buf, int buflen)
     119             : {
     120       19542 :         return SSL_write((SSL *) conn->tls_session, buf, buflen);
     121             : }
     122             : #  endif
     123             : 
     124             : #else
     125             : /*
     126             :  * Definitions if TLS is not enabled
     127             :  */
     128             : static inline TDSRET
     129             : tds_ssl_init(TDSSOCKET *tds TDS_UNUSED, bool full TDS_UNUSED)
     130             : {
     131             :         return TDS_FAIL;
     132             : }
     133             : 
     134             : static inline void
     135             : tds_ssl_deinit(TDSCONNECTION *conn TDS_UNUSED)
     136             : {
     137             : }
     138             : 
     139             : static inline int
     140             : tds_ssl_pending(TDSCONNECTION *conn TDS_UNUSED)
     141             : {
     142             :         return 0;
     143             : }
     144             : 
     145             : static inline int
     146             : tds_ssl_read(TDSCONNECTION *conn TDS_UNUSED, unsigned char *buf TDS_UNUSED, int buflen TDS_UNUSED)
     147             : {
     148             :         return -1;
     149             : }
     150             : 
     151             : static inline int
     152             : tds_ssl_write(TDSCONNECTION *conn TDS_UNUSED, const unsigned char *buf TDS_UNUSED, int buflen TDS_UNUSED)
     153             : {
     154             :         return -1;
     155             : }
     156             : 
     157             : static inline size_t
     158             : tds_ssl_get_cb(TDSCONNECTION *conn TDS_UNUSED, void *cb TDS_UNUSED, size_t cblen TDS_UNUSED)
     159             : {
     160             :         return 0;
     161             : }
     162             : #endif
     163             : 
     164             : #include <freetds/popvis.h>
     165             : 
     166             : #endif /* _tdsguard_hpUeh3TzYOzN1FtT39tMHz_ */

Generated by: LCOV version 1.13