Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/s2n.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extern int s2n_connection_set_write_fd(struct s2n_connection *conn, int readfd);

typedef enum { S2N_BUILT_IN_BLINDING, S2N_SELF_SERVICE_BLINDING } s2n_blinding;
extern int s2n_connection_set_blinding(struct s2n_connection *conn, s2n_blinding blinding);
extern int s2n_connection_get_delay(struct s2n_connection *conn);
extern int64_t s2n_connection_get_delay(struct s2n_connection *conn);

extern int s2n_set_server_name(struct s2n_connection *conn, const char *server_name);
extern const char *s2n_get_server_name(struct s2n_connection *conn);
Expand Down
11 changes: 10 additions & 1 deletion bin/echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ int echo(struct s2n_connection *conn, int sockfd)
printf("Cipher negotiated: %s\n", s2n_connection_get_cipher(conn));

/* Act as a simple proxy between stdin and the SSL connection */
while (poll(readers, 2, -1) > 0) {
int p;
POLL:
while ((p = poll(readers, 2, -1)) > 0) {
char buffer[10240];
int bytes_read, bytes_written;

Expand Down Expand Up @@ -121,8 +123,12 @@ int echo(struct s2n_connection *conn, int sockfd)
}

/* Read as many bytes as we think we can */
READ:
bytes_read = read(STDIN_FILENO, buffer, bytes_available);
if (bytes_read < 0) {
if (errno == EINTR) {
goto READ;
}
fprintf(stderr, "Error reading from stdin\n");
exit(1);
}
Expand All @@ -144,6 +150,9 @@ int echo(struct s2n_connection *conn, int sockfd)
} while (bytes_available || more);
}
}
if (p < 0 && errno == EINTR) {
goto POLL;
}

return 0;
}
6 changes: 3 additions & 3 deletions docs/USAGE-GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Setting the **S2N_SELF_SERVICE_BLINDING** option with **s2n_connection_set_blind
turns off this behavior. This is useful for applications that are handling many connections
in a single thread. In that case, if s2n_recv() or s2n_negotiate() return an error,
self-service applications should call **s2n_connection_get_delay** and pause
activity on the connection for the specified number of microseconds before calling
activity on the connection for the specified number of nanoseconds before calling
close() or shutdown().

```c
Expand Down Expand Up @@ -431,10 +431,10 @@ built-in blinding (set blinding to S2N_BUILT_IN_BLINDING) or self-service blindi
### s2n\_connection\_get\_delay

```c
int s2n_connection_get_delay(struct s2n_connection *conn);
int64_t s2n_connection_get_delay(struct s2n_connection *conn);
```

**s2n_connection_get_delay** returns the number of microseconds an application
**s2n_connection_get_delay** returns the number of nanoseconds an application
using self-service blinding should pause before calling close() or shutdown().

### s2n\_connection\_get\_wire\_bytes
Expand Down
4 changes: 4 additions & 0 deletions stuffer/s2n_stuffer_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ int s2n_stuffer_alloc_ro_from_file(struct s2n_stuffer *stuffer, const char *file
{
int fd;

OPEN:
fd = open(file, O_RDONLY);
if (fd < 0) {
if (errno == EINTR) {
goto OPEN;
}
S2N_ERROR(S2N_ERR_OPEN);
}

Expand Down
5 changes: 3 additions & 2 deletions tests/unit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ include ../../s2n.mk
CRUFT += $(wildcard *_test)

ifeq ($(shell uname),Darwin)
LIBS = -lpthread -lm
LIBS = -lpthread -lm -lcrypto
else ifeq ($(shell uname),FreeBSD)
LIBS = -lthr -lcrypto
else
Expand All @@ -36,7 +36,8 @@ endif
# Suppress the unreachable code warning, because tests involve what should be
# unreachable code
CFLAGS += -Wno-unreachable-code -I../../libcrypto-root/include/ -I../../ -I../../api/
LDFLAGS += -L../../lib/ -L../../libcrypto-root/lib -L../testlib/ -ltests2n -ls2n ${LIBS}
#LDFLAGS += -L../../lib/ -L../../libcrypto-root/lib -L../testlib/ -ltests2n -ls2n ${LIBS}
LDFLAGS += -L../../lib/ -L../testlib/ -ltests2n -ls2n ${LIBS}

$(TESTS)::
@${CC} ${CFLAGS} -o $@ [email protected] ${LDFLAGS} 2>&1
Expand Down
22 changes: 14 additions & 8 deletions tls/s2n_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
* permissions and limitations under the License.
*/

#define _XOPEN_SOURCE 500 /* For usleep() */

#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
Expand Down Expand Up @@ -347,20 +345,28 @@ int s2n_connection_set_blinding(struct s2n_connection *conn, s2n_blinding blindi
return 0;
}

int s2n_connection_get_delay(struct s2n_connection *conn)
#define ONE_MS INT64_C(1000000)
#define ONE_S INT64_C(1000000000)
#define TEN_S INT64_C(10000000000)

int64_t s2n_connection_get_delay(struct s2n_connection *conn)
{
/* Delay between 1ms and 10 seconds in microseconds */
int min = 1000, max = 10 * 1000 * 1000;
/* Delay between 1ms and 10 seconds in nanoseconds */
int64_t min = ONE_MS, max = TEN_S;
return min + s2n_public_random(max - min);
}

int s2n_sleep_delay(struct s2n_connection *conn)
{
if (conn->blinding == S2N_BUILT_IN_BLINDING) {
int delay;
int delay, r;
GUARD(delay = s2n_connection_get_delay(conn));
GUARD(sleep(delay / 1000000));
GUARD(usleep(delay % 1000000));
struct timespec sleep_time = { .tv_sec = delay / ONE_S, .tv_nsec = delay % ONE_S };

do {
r = nanosleep(&sleep_time, &sleep_time);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do {
r = nanosleep(&sleep_time, &sleep_time);
} while (r != 0);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much nicer, done!

while (r != 0);
}

return 0;
Expand Down
1 change: 1 addition & 0 deletions tls/s2n_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <stdint.h>
#include <signal.h>
#include <errno.h>
#include <s2n.h>

#include "tls/s2n_tls_parameters.h"
Expand Down
10 changes: 7 additions & 3 deletions utils/s2n_random.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ int s2n_get_urandom_data(struct s2n_blob *blob)
return 0;
}

int s2n_public_random(int max)
int64_t s2n_public_random(int64_t max)
{
unsigned int r;
uint64_t r;

gt_check(max, 0);

Expand All @@ -136,7 +136,7 @@ int s2n_public_random(int max)
* But since 'max' is an int and INT_MAX is <= UINT_MAX / 2,
* in the worst case we discard 25% - 1 r's.
*/
if (r < (UINT_MAX - (UINT_MAX % max))) {
if (r < (UINT64_MAX - (UINT64_MAX % max))) {
return r % max;
}
}
Expand Down Expand Up @@ -178,8 +178,12 @@ RAND_METHOD s2n_openssl_rand_method = {

int s2n_init(void)
{
OPEN:
entropy_fd = open(ENTROPY_SOURCE, O_RDONLY);
if (entropy_fd == -1) {
if (errno == EINTR) {
goto OPEN;
}
S2N_ERROR(S2N_ERR_OPEN_RANDOM);
}

Expand Down
2 changes: 1 addition & 1 deletion utils/s2n_random.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
extern int s2n_get_public_random_data(struct s2n_blob *blob);
extern int s2n_get_private_random_data(struct s2n_blob *blob);
extern int s2n_get_urandom_data(struct s2n_blob *blob);
extern int s2n_public_random(int max);
extern int64_t s2n_public_random(int64_t max);