Skip to content

Commit 8f44024

Browse files
committed
SSL patch to periodically renegotiate session key.
In order to reduce the risk of cryptanalysis during extended sessions (or brief ones involving a substantial amount of data), this patch renegotiates the session key after 64kib has been transferred. Bear Giles
1 parent 55d0532 commit 8f44024

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/backend/libpq/be-secure.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/libpq/be-secure.c,v 1.2 2002/06/14 04:31:49 momjian Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/libpq/be-secure.c,v 1.3 2002/06/14 04:33:53 momjian Exp $
1515
*
1616
* Since the server static private key ($DataDir/server.key)
1717
* will normally be stored unencrypted so that the database
@@ -39,6 +39,12 @@
3939
* session. In this case you'll need to temporarily disable
4040
* EDH by commenting out the callback.
4141
*
42+
* ...
43+
*
44+
* Because the risk of cryptanalysis increases as large
45+
* amounts of data are sent with the same session key, the
46+
* session keys are periodically renegotiated.
47+
*
4248
* PATCH LEVEL
4349
* milestone 1: fix basic coding errors
4450
* [*] existing SSL code pulled out of existing files.
@@ -52,7 +58,7 @@
5258
* milestone 3: improve confidentially, support perfect forward secrecy
5359
* [ ] use 'random' file, read from '/dev/urandom?'
5460
* [*] emphermal DH keys, default values
55-
* [ ] periodic renegotiation
61+
* [*] periodic renegotiation
5662
* [ ] private key permissions
5763
*
5864
* milestone 4: provide endpoint authentication (client)
@@ -126,6 +132,12 @@ static const char *SSLerrmessage(void);
126132
#endif
127133

128134
#ifdef USE_SSL
135+
/*
136+
* How much data can be sent across a secure connection
137+
* (total in both directions) before we require renegotiation.
138+
*/
139+
#define RENEGOTIATION_LIMIT (64 * 1024)
140+
129141
static SSL_CTX *SSL_context = NULL;
130142
#endif
131143

@@ -261,10 +273,17 @@ secure_read (Port *port, void *ptr, size_t len)
261273
#ifdef USE_SSL
262274
if (port->ssl)
263275
{
276+
if (port->count > RENEGOTIATION_LIMIT)
277+
{
278+
SSL_renegotiate(port->ssl);
279+
port->count = 0;
280+
}
281+
264282
n = SSL_read(port->ssl, ptr, len);
265283
switch (SSL_get_error(port->ssl, n))
266284
{
267285
case SSL_ERROR_NONE:
286+
port->count += n;
268287
break;
269288
case SSL_ERROR_WANT_READ:
270289
break;
@@ -304,10 +323,17 @@ secure_write (Port *port, const void *ptr, size_t len)
304323
#ifdef USE_SSL
305324
if (port->ssl)
306325
{
326+
if (port->count > RENEGOTIATION_LIMIT)
327+
{
328+
SSL_renegotiate(port->ssl);
329+
port->count = 0;
330+
}
331+
307332
n = SSL_write(port->ssl, ptr, len);
308333
switch (SSL_get_error(port->ssl, n))
309334
{
310335
case SSL_ERROR_NONE:
336+
port->count += n;
311337
break;
312338
case SSL_ERROR_WANT_WRITE:
313339
break;
@@ -574,6 +600,7 @@ open_server_SSL (Port *port)
574600
close_SSL(port);
575601
return -1;
576602
}
603+
port->count = 0;
577604

578605
return 0;
579606
}

src/include/libpq/libpq-be.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
14-
* $Id: libpq-be.h,v 1.29 2002/06/14 04:09:37 momjian Exp $
14+
* $Id: libpq-be.h,v 1.30 2002/06/14 04:33:53 momjian Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -70,6 +70,7 @@ typedef struct Port
7070
*/
7171
#ifdef USE_SSL
7272
SSL *ssl;
73+
unsigned long count;
7374
#endif
7475
} Port;
7576

0 commit comments

Comments
 (0)