FreeTDS API
Loading...
Searching...
No Matches
tls.h
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 */
50TDSRET tds_ssl_init(TDSSOCKET *tds, bool full);
51void tds_ssl_deinit(TDSCONNECTION *conn);
58size_t tds_ssl_get_cb(TDSCONNECTION * conn, void *cb, size_t cblen);
59
60# ifdef HAVE_GNUTLS
61/*
62 * GnuTLS definitions
63 */
64static inline int
65tds_ssl_pending(TDSCONNECTION *conn)
66{
67 return gnutls_record_check_pending((gnutls_session_t) conn->tls_session);
68}
69
70static inline int
71tds_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 rc = gnutls_record_recv((gnutls_session_t) conn->tls_session, buf, buflen);
85 } while ((rc == GNUTLS_E_AGAIN || rc == GNUTLS_E_INTERRUPTED) && !TDS_IS_SOCKET_INVALID(conn->s));
86 return rc;
87}
88
89static inline int
90tds_ssl_write(TDSCONNECTION *conn, const unsigned char *buf, int buflen)
91{
92 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
105static inline int
106tds_ssl_pending(TDSCONNECTION *conn)
107{
108 return SSL_pending((SSL *) conn->tls_session);
109}
110
111static inline int
112tds_ssl_read(TDSCONNECTION *conn, unsigned char *buf, int buflen)
113{
114 return SSL_read((SSL *) conn->tls_session, buf, buflen);
115}
116
117static inline int
118tds_ssl_write(TDSCONNECTION *conn, const unsigned char *buf, int buflen)
119{
120 return SSL_write((SSL *) conn->tls_session, buf, buflen);
121}
122# endif
123
124#else
125/*
126 * Definitions if TLS is not enabled
127 */
128static inline TDSRET
129tds_ssl_init(TDSSOCKET *tds TDS_UNUSED, bool full TDS_UNUSED)
130{
131 return TDS_FAIL;
132}
133
134static inline void
135tds_ssl_deinit(TDSCONNECTION *conn TDS_UNUSED)
136{
137}
138
139static inline int
140tds_ssl_pending(TDSCONNECTION *conn TDS_UNUSED)
141{
142 return 0;
143}
144
145static inline int
146tds_ssl_read(TDSCONNECTION *conn TDS_UNUSED, unsigned char *buf TDS_UNUSED, int buflen TDS_UNUSED)
147{
148 return -1;
149}
150
151static inline int
152tds_ssl_write(TDSCONNECTION *conn TDS_UNUSED, const unsigned char *buf TDS_UNUSED, int buflen TDS_UNUSED)
153{
154 return -1;
155}
156
157static inline size_t
158tds_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_ */
TDS_SYS_SOCKET s
tcp socket, INVALID_SOCKET if not connected
Definition tds.h:1118