PostgreSQL Source Code git master
Loading...
Searching...
No Matches
be-secure.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * be-secure.c
4 * functions related to setting up a secure connection to the frontend.
5 * Secure connections are expected to provide confidentiality,
6 * message integrity and endpoint authentication.
7 *
8 *
9 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
11 *
12 *
13 * IDENTIFICATION
14 * src/backend/libpq/be-secure.c
15 *
16 *-------------------------------------------------------------------------
17 */
18
19#include "postgres.h"
20
21#include <signal.h>
22#include <fcntl.h>
23#include <ctype.h>
24#include <sys/socket.h>
25#include <netdb.h>
26#include <netinet/in.h>
27#include <netinet/tcp.h>
28#include <arpa/inet.h>
29
30#include "libpq/libpq.h"
31#include "miscadmin.h"
32#include "storage/latch.h"
33#include "tcop/tcopprot.h"
35#include "utils/wait_event.h"
36
46
47#ifdef USE_SSL
49#endif
50
51/* GUC variable controlling SSL cipher list */
54
55/* GUC variable for default ECHD curve. */
57
58/* GUC variable: if false, prefer client ciphers */
60
63
64/* GUC variable: if false, discards hostname extensions in handshake */
65bool ssl_sni = false;
66
67/* ------------------------------------------------------------ */
68/* Procedures common to all secure sessions */
69/* ------------------------------------------------------------ */
70
71/*
72 * Initialize global context.
73 *
74 * If isServerStart is true, report any errors as FATAL (so we don't return).
75 * Otherwise, log errors at LOG level and return -1 to indicate trouble,
76 * preserving the old SSL state if any. Returns 0 if OK.
77 */
78int
80{
81#ifdef USE_SSL
83#else
84 return 0;
85#endif
86}
87
88/*
89 * Destroy global context, if any.
90 */
91void
93{
94#ifdef USE_SSL
96#endif
97}
98
99/*
100 * Indicate if we have loaded the root CA store to verify certificates
101 */
102bool
104{
105#ifdef USE_SSL
107#else
108 return false;
109#endif
110}
111
112/*
113 * Attempt to negotiate secure session.
114 */
115int
117{
118#ifdef USE_SSL
119 int r = 0;
120 ssize_t len;
121
122 /* push unencrypted buffered data back through SSL setup */
124 if (len > 0)
125 {
126 char *buf = palloc(len);
127
129 if (pq_getbytes(buf, len) == EOF)
130 return STATUS_ERROR; /* shouldn't be possible */
132 port->raw_buf = buf;
133 port->raw_buf_remaining = len;
134 port->raw_buf_consumed = 0;
135 }
137
138 INJECTION_POINT("backend-ssl-startup", NULL);
139
141
142 if (port->raw_buf_remaining > 0)
143 {
144 /*
145 * This shouldn't be possible -- it would mean the client sent
146 * encrypted data before we established a session key...
147 */
148 elog(LOG, "buffered unencrypted data remains after negotiating SSL connection");
149 return STATUS_ERROR;
150 }
151 if (port->raw_buf != NULL)
152 {
153 pfree(port->raw_buf);
154 port->raw_buf = NULL;
155 }
156
158 (errmsg_internal("SSL connection from DN:\"%s\" CN:\"%s\"",
159 port->peer_dn ? port->peer_dn : "(anonymous)",
160 port->peer_cn ? port->peer_cn : "(anonymous)")));
161 return r;
162#else
163 return 0;
164#endif
165}
166
167/*
168 * Close secure session.
169 */
170void
172{
173#ifdef USE_SSL
174 if (port->ssl_in_use)
176#endif
177}
178
179/*
180 * Read data from a secure connection.
181 */
183secure_read(Port *port, void *ptr, size_t len)
184{
185 ssize_t n;
186 int waitfor;
187
188 /* Deal with any already-pending interrupt condition. */
190
191retry:
192#ifdef USE_SSL
193 waitfor = 0;
194 if (port->ssl_in_use)
195 {
196 n = be_tls_read(port, ptr, len, &waitfor);
197 }
198 else
199#endif
200#ifdef ENABLE_GSS
201 if (port->gss && port->gss->enc)
202 {
203 n = be_gssapi_read(port, ptr, len);
205 }
206 else
207#endif
208 {
209 n = secure_raw_read(port, ptr, len);
211 }
212
213 /* In blocking mode, wait until the socket is ready */
214 if (n < 0 && !port->noblock && (errno == EWOULDBLOCK || errno == EAGAIN))
215 {
216 WaitEvent event;
217
219
221
222 WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1,
224
225 /*
226 * If the postmaster has died, it's not safe to continue running,
227 * because it is the postmaster's job to kill us if some other backend
228 * exits uncleanly. Moreover, we won't run very well in this state;
229 * helper processes like walwriter and the bgwriter will exit, so
230 * performance may be poor. Finally, if we don't exit, pg_ctl will be
231 * unable to restart the postmaster without manual intervention, so no
232 * new connections can be accepted. Exiting clears the deck for a
233 * postmaster restart.
234 *
235 * (Note that we only make this check when we would otherwise sleep on
236 * our latch. We might still continue running for a while if the
237 * postmaster is killed in mid-query, or even through multiple queries
238 * if we never have to wait for read. We don't want to burn too many
239 * cycles checking for this very rare condition, and this should cause
240 * us to exit quickly in most cases.)
241 */
242 if (event.events & WL_POSTMASTER_DEATH)
245 errmsg("terminating connection due to unexpected postmaster exit")));
246
247 /* Handle interrupt. */
248 if (event.events & WL_LATCH_SET)
249 {
252
253 /*
254 * We'll retry the read. Most likely it will return immediately
255 * because there's still no data available, and we'll wait for the
256 * socket to become ready again.
257 */
258 }
259 goto retry;
260 }
261
262 /*
263 * Process interrupts that happened during a successful (or non-blocking,
264 * or hard-failed) read.
265 */
267
268 return n;
269}
270
272secure_raw_read(Port *port, void *ptr, size_t len)
273{
274 ssize_t n;
275
276 /* Read from the "unread" buffered data first. c.f. libpq-be.h */
277 if (port->raw_buf_remaining > 0)
278 {
279 /* consume up to len bytes from the raw_buf */
280 if (len > port->raw_buf_remaining)
281 len = port->raw_buf_remaining;
282 Assert(port->raw_buf);
283 memcpy(ptr, port->raw_buf + port->raw_buf_consumed, len);
284 port->raw_buf_consumed += len;
285 port->raw_buf_remaining -= len;
286 return len;
287 }
288
289 /*
290 * Try to read from the socket without blocking. If it succeeds we're
291 * done, otherwise we'll wait for the socket using the latch mechanism.
292 */
293#ifdef WIN32
294 pgwin32_noblock = true;
295#endif
296 n = recv(port->sock, ptr, len, 0);
297#ifdef WIN32
298 pgwin32_noblock = false;
299#endif
300
301 return n;
302}
303
304
305/*
306 * Write data to a secure connection.
307 */
309secure_write(Port *port, const void *ptr, size_t len)
310{
311 ssize_t n;
312 int waitfor;
313
314 /* Deal with any already-pending interrupt condition. */
316
317retry:
318 waitfor = 0;
319#ifdef USE_SSL
320 if (port->ssl_in_use)
321 {
322 n = be_tls_write(port, ptr, len, &waitfor);
323 }
324 else
325#endif
326#ifdef ENABLE_GSS
327 if (port->gss && port->gss->enc)
328 {
329 n = be_gssapi_write(port, ptr, len);
331 }
332 else
333#endif
334 {
335 n = secure_raw_write(port, ptr, len);
337 }
338
339 if (n < 0 && !port->noblock && (errno == EWOULDBLOCK || errno == EAGAIN))
340 {
341 WaitEvent event;
342
344
346
347 WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1,
349
350 /* See comments in secure_read. */
351 if (event.events & WL_POSTMASTER_DEATH)
354 errmsg("terminating connection due to unexpected postmaster exit")));
355
356 /* Handle interrupt. */
357 if (event.events & WL_LATCH_SET)
358 {
361
362 /*
363 * We'll retry the write. Most likely it will return immediately
364 * because there's still no buffer space available, and we'll wait
365 * for the socket to become ready again.
366 */
367 }
368 goto retry;
369 }
370
371 /*
372 * Process interrupts that happened during a successful (or non-blocking,
373 * or hard-failed) write.
374 */
376
377 return n;
378}
379
381secure_raw_write(Port *port, const void *ptr, size_t len)
382{
383 ssize_t n;
384
385#ifdef WIN32
386 pgwin32_noblock = true;
387#endif
388 n = send(port->sock, ptr, len, 0);
389#ifdef WIN32
390 pgwin32_noblock = false;
391#endif
392
393 return n;
394}
ssize_t be_gssapi_write(Port *port, const void *ptr, size_t len)
ssize_t be_gssapi_read(Port *port, void *ptr, size_t len)
ssize_t be_tls_write(Port *port, const void *ptr, size_t len, int *waitfor)
void be_tls_destroy(void)
int be_tls_init(bool isServerStart)
int be_tls_open_server(Port *port)
void be_tls_close(Port *port)
ssize_t be_tls_read(Port *port, void *ptr, size_t len, int *waitfor)
void secure_destroy(void)
Definition be-secure.c:92
char * ssl_crl_dir
Definition be-secure.c:42
char * ssl_dh_params_file
Definition be-secure.c:43
int ssl_min_protocol_version
Definition be-secure.c:61
ssize_t secure_raw_read(Port *port, void *ptr, size_t len)
Definition be-secure.c:272
int secure_initialize(bool isServerStart)
Definition be-secure.c:79
char * ssl_cert_file
Definition be-secure.c:38
bool SSLPreferServerCiphers
Definition be-secure.c:59
bool ssl_sni
Definition be-secure.c:65
ssize_t secure_write(Port *port, const void *ptr, size_t len)
Definition be-secure.c:309
char * ssl_library
Definition be-secure.c:37
int ssl_max_protocol_version
Definition be-secure.c:62
char * ssl_passphrase_command
Definition be-secure.c:44
int secure_open_server(Port *port)
Definition be-secure.c:116
bool ssl_passphrase_command_supports_reload
Definition be-secure.c:45
char * SSLCipherSuites
Definition be-secure.c:52
bool secure_loaded_verify_locations(void)
Definition be-secure.c:103
char * SSLECDHCurve
Definition be-secure.c:56
char * SSLCipherList
Definition be-secure.c:53
char * ssl_key_file
Definition be-secure.c:39
void secure_close(Port *port)
Definition be-secure.c:171
ssize_t secure_read(Port *port, void *ptr, size_t len)
Definition be-secure.c:183
ssize_t secure_raw_write(Port *port, const void *ptr, size_t len)
Definition be-secure.c:381
char * ssl_crl_file
Definition be-secure.c:41
char * ssl_ca_file
Definition be-secure.c:40
#define Assert(condition)
Definition c.h:945
#define STATUS_ERROR
Definition c.h:1261
int errcode(int sqlerrcode)
Definition elog.c:874
#define LOG
Definition elog.h:31
#define FATAL
Definition elog.h:41
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define DEBUG2
Definition elog.h:29
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
struct Latch * MyLatch
Definition globals.c:63
#define INJECTION_POINT(name, arg)
void ResetLatch(Latch *latch)
Definition latch.c:374
@ PG_TLS1_2_VERSION
Definition libpq.h:155
@ PG_TLS_ANY
Definition libpq.h:152
#define FeBeWaitSetSocketPos
Definition libpq.h:66
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc(Size size)
Definition mcxt.c:1387
static char * errmsg
const void size_t len
static int port
Definition pg_regress.c:115
static char buf[DEFAULT_XLOG_SEG_SIZE]
void ProcessClientReadInterrupt(bool blocked)
Definition postgres.c:502
void ProcessClientWriteInterrupt(bool blocked)
Definition postgres.c:548
ssize_t pq_buffer_remaining_data(void)
Definition pqcomm.c:1128
int pq_getbytes(void *b, size_t len)
Definition pqcomm.c:1063
WaitEventSet * FeBeWaitSet
Definition pqcomm.c:167
void pq_endmsgread(void)
Definition pqcomm.c:1166
void pq_startmsgread(void)
Definition pqcomm.c:1142
static int fb(int x)
int pgwin32_noblock
Definition socket.c:28
uint32 events
void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
#define WL_SOCKET_READABLE
#define WL_LATCH_SET
#define WL_POSTMASTER_DEATH
#define WL_SOCKET_WRITEABLE
#define EWOULDBLOCK
Definition win32_port.h:367
#define recv(s, buf, len, flags)
Definition win32_port.h:501
#define send(s, buf, len, flags)
Definition win32_port.h:502
#define EAGAIN
Definition win32_port.h:359