aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/tsi
diff options
context:
space:
mode:
authorGravatar Craig Tiller <ctiller@google.com>2015-09-22 12:33:20 -0700
committerGravatar Craig Tiller <ctiller@google.com>2015-09-22 12:33:20 -0700
commita82950e68318a6aab6fe894fa39f7fa616c4647b (patch)
tree7d02bd1e9e1cbae1f14ad4ad1e06d3ae81a96dfe /src/core/tsi
parent8af4c337181322cc4fb396199c90f574cfb4163f (diff)
clang-format all core files
Diffstat (limited to 'src/core/tsi')
-rw-r--r--src/core/tsi/fake_transport_security.c670
-rw-r--r--src/core/tsi/fake_transport_security.h10
-rw-r--r--src/core/tsi/ssl_transport_security.c2096
-rw-r--r--src/core/tsi/ssl_transport_security.h33
-rw-r--r--src/core/tsi/transport_security.c323
-rw-r--r--src/core/tsi/transport_security.h89
-rw-r--r--src/core/tsi/transport_security_interface.h110
7 files changed, 1566 insertions, 1765 deletions
diff --git a/src/core/tsi/fake_transport_security.c b/src/core/tsi/fake_transport_security.c
index 3a8383d4b0..9ef4b163be 100644
--- a/src/core/tsi/fake_transport_security.c
+++ b/src/core/tsi/fake_transport_security.c
@@ -52,8 +52,7 @@
| size | data |
where the size field value is the size of the size field plus the size of
the data encoded in little endian on 4 bytes. */
-typedef struct
-{
+typedef struct {
unsigned char *data;
size_t size;
size_t allocated_size;
@@ -61,8 +60,7 @@ typedef struct
int needs_draining;
} tsi_fake_frame;
-typedef enum
-{
+typedef enum {
TSI_FAKE_CLIENT_INIT = 0,
TSI_FAKE_SERVER_INIT = 1,
TSI_FAKE_CLIENT_FINISHED = 2,
@@ -70,8 +68,7 @@ typedef enum
TSI_FAKE_HANDSHAKE_MESSAGE_MAX = 4
} tsi_fake_handshake_message;
-typedef struct
-{
+typedef struct {
tsi_handshaker base;
int is_client;
tsi_fake_handshake_message next_message_to_send;
@@ -81,8 +78,7 @@ typedef struct
tsi_result result;
} tsi_fake_handshaker;
-typedef struct
-{
+typedef struct {
tsi_frame_protector base;
tsi_fake_frame protect_frame;
tsi_fake_frame unprotect_frame;
@@ -92,187 +88,156 @@ typedef struct
/* --- Utils. ---*/
static const char *tsi_fake_handshake_message_strings[] = {
- "CLIENT_INIT", "SERVER_INIT", "CLIENT_FINISHED", "SERVER_FINISHED"
-};
+ "CLIENT_INIT", "SERVER_INIT", "CLIENT_FINISHED", "SERVER_FINISHED"};
-static const char *
-tsi_fake_handshake_message_to_string (int msg)
-{
- if (msg < 0 || msg >= TSI_FAKE_HANDSHAKE_MESSAGE_MAX)
- {
- gpr_log (GPR_ERROR, "Invalid message %d", msg);
- return "UNKNOWN";
- }
+static const char *tsi_fake_handshake_message_to_string(int msg) {
+ if (msg < 0 || msg >= TSI_FAKE_HANDSHAKE_MESSAGE_MAX) {
+ gpr_log(GPR_ERROR, "Invalid message %d", msg);
+ return "UNKNOWN";
+ }
return tsi_fake_handshake_message_strings[msg];
}
-static tsi_result
-tsi_fake_handshake_message_from_string (const char *msg_string, tsi_fake_handshake_message * msg)
-{
+static tsi_result tsi_fake_handshake_message_from_string(
+ const char *msg_string, tsi_fake_handshake_message *msg) {
tsi_fake_handshake_message i;
- for (i = 0; i < TSI_FAKE_HANDSHAKE_MESSAGE_MAX; i++)
- {
- if (strncmp (msg_string, tsi_fake_handshake_message_strings[i], strlen (tsi_fake_handshake_message_strings[i])) == 0)
- {
- *msg = i;
- return TSI_OK;
- }
+ for (i = 0; i < TSI_FAKE_HANDSHAKE_MESSAGE_MAX; i++) {
+ if (strncmp(msg_string, tsi_fake_handshake_message_strings[i],
+ strlen(tsi_fake_handshake_message_strings[i])) == 0) {
+ *msg = i;
+ return TSI_OK;
}
- gpr_log (GPR_ERROR, "Invalid handshake message.");
+ }
+ gpr_log(GPR_ERROR, "Invalid handshake message.");
return TSI_DATA_CORRUPTED;
}
-static gpr_uint32
-load32_little_endian (const unsigned char *buf)
-{
- return ((gpr_uint32) (buf[0]) | (gpr_uint32) (buf[1] << 8) | (gpr_uint32) (buf[2] << 16) | (gpr_uint32) (buf[3] << 24));
+static gpr_uint32 load32_little_endian(const unsigned char *buf) {
+ return ((gpr_uint32)(buf[0]) | (gpr_uint32)(buf[1] << 8) |
+ (gpr_uint32)(buf[2] << 16) | (gpr_uint32)(buf[3] << 24));
}
-static void
-store32_little_endian (gpr_uint32 value, unsigned char *buf)
-{
- buf[3] = (unsigned char) (value >> 24) & 0xFF;
- buf[2] = (unsigned char) (value >> 16) & 0xFF;
- buf[1] = (unsigned char) (value >> 8) & 0xFF;
- buf[0] = (unsigned char) (value) & 0xFF;
+static void store32_little_endian(gpr_uint32 value, unsigned char *buf) {
+ buf[3] = (unsigned char)(value >> 24) & 0xFF;
+ buf[2] = (unsigned char)(value >> 16) & 0xFF;
+ buf[1] = (unsigned char)(value >> 8) & 0xFF;
+ buf[0] = (unsigned char)(value)&0xFF;
}
-static void
-tsi_fake_frame_reset (tsi_fake_frame * frame, int needs_draining)
-{
+static void tsi_fake_frame_reset(tsi_fake_frame *frame, int needs_draining) {
frame->offset = 0;
frame->needs_draining = needs_draining;
- if (!needs_draining)
- frame->size = 0;
+ if (!needs_draining) frame->size = 0;
}
/* Returns 1 if successful, 0 otherwise. */
-static int
-tsi_fake_frame_ensure_size (tsi_fake_frame * frame)
-{
- if (frame->data == NULL)
- {
- frame->allocated_size = frame->size;
- frame->data = malloc (frame->allocated_size);
- if (frame->data == NULL)
- return 0;
- }
- else if (frame->size > frame->allocated_size)
- {
- unsigned char *new_data = realloc (frame->data, frame->size);
- if (new_data == NULL)
- {
- free (frame->data);
- frame->data = NULL;
- return 0;
- }
- frame->data = new_data;
- frame->allocated_size = frame->size;
+static int tsi_fake_frame_ensure_size(tsi_fake_frame *frame) {
+ if (frame->data == NULL) {
+ frame->allocated_size = frame->size;
+ frame->data = malloc(frame->allocated_size);
+ if (frame->data == NULL) return 0;
+ } else if (frame->size > frame->allocated_size) {
+ unsigned char *new_data = realloc(frame->data, frame->size);
+ if (new_data == NULL) {
+ free(frame->data);
+ frame->data = NULL;
+ return 0;
}
+ frame->data = new_data;
+ frame->allocated_size = frame->size;
+ }
return 1;
}
/* This method should not be called if frame->needs_framing is not 0. */
-static tsi_result
-fill_frame_from_bytes (const unsigned char *incoming_bytes, size_t * incoming_bytes_size, tsi_fake_frame * frame)
-{
+static tsi_result fill_frame_from_bytes(const unsigned char *incoming_bytes,
+ size_t *incoming_bytes_size,
+ tsi_fake_frame *frame) {
size_t available_size = *incoming_bytes_size;
size_t to_read_size = 0;
const unsigned char *bytes_cursor = incoming_bytes;
- if (frame->needs_draining)
- return TSI_INTERNAL_ERROR;
- if (frame->data == NULL)
- {
- frame->allocated_size = TSI_FAKE_FRAME_INITIAL_ALLOCATED_SIZE;
- frame->data = malloc (frame->allocated_size);
- if (frame->data == NULL)
- return TSI_OUT_OF_RESOURCES;
- }
-
- if (frame->offset < TSI_FAKE_FRAME_HEADER_SIZE)
- {
- to_read_size = TSI_FAKE_FRAME_HEADER_SIZE - frame->offset;
- if (to_read_size > available_size)
- {
- /* Just fill what we can and exit. */
- memcpy (frame->data + frame->offset, bytes_cursor, available_size);
- bytes_cursor += available_size;
- frame->offset += available_size;
- *incoming_bytes_size = (size_t) (bytes_cursor - incoming_bytes);
- return TSI_INCOMPLETE_DATA;
- }
- memcpy (frame->data + frame->offset, bytes_cursor, to_read_size);
- bytes_cursor += to_read_size;
- frame->offset += to_read_size;
- available_size -= to_read_size;
- frame->size = load32_little_endian (frame->data);
- if (!tsi_fake_frame_ensure_size (frame))
- return TSI_OUT_OF_RESOURCES;
- }
-
- to_read_size = frame->size - frame->offset;
- if (to_read_size > available_size)
- {
- memcpy (frame->data + frame->offset, bytes_cursor, available_size);
- frame->offset += available_size;
+ if (frame->needs_draining) return TSI_INTERNAL_ERROR;
+ if (frame->data == NULL) {
+ frame->allocated_size = TSI_FAKE_FRAME_INITIAL_ALLOCATED_SIZE;
+ frame->data = malloc(frame->allocated_size);
+ if (frame->data == NULL) return TSI_OUT_OF_RESOURCES;
+ }
+
+ if (frame->offset < TSI_FAKE_FRAME_HEADER_SIZE) {
+ to_read_size = TSI_FAKE_FRAME_HEADER_SIZE - frame->offset;
+ if (to_read_size > available_size) {
+ /* Just fill what we can and exit. */
+ memcpy(frame->data + frame->offset, bytes_cursor, available_size);
bytes_cursor += available_size;
- *incoming_bytes_size = (size_t) (bytes_cursor - incoming_bytes);
+ frame->offset += available_size;
+ *incoming_bytes_size = (size_t)(bytes_cursor - incoming_bytes);
return TSI_INCOMPLETE_DATA;
}
- memcpy (frame->data + frame->offset, bytes_cursor, to_read_size);
+ memcpy(frame->data + frame->offset, bytes_cursor, to_read_size);
+ bytes_cursor += to_read_size;
+ frame->offset += to_read_size;
+ available_size -= to_read_size;
+ frame->size = load32_little_endian(frame->data);
+ if (!tsi_fake_frame_ensure_size(frame)) return TSI_OUT_OF_RESOURCES;
+ }
+
+ to_read_size = frame->size - frame->offset;
+ if (to_read_size > available_size) {
+ memcpy(frame->data + frame->offset, bytes_cursor, available_size);
+ frame->offset += available_size;
+ bytes_cursor += available_size;
+ *incoming_bytes_size = (size_t)(bytes_cursor - incoming_bytes);
+ return TSI_INCOMPLETE_DATA;
+ }
+ memcpy(frame->data + frame->offset, bytes_cursor, to_read_size);
bytes_cursor += to_read_size;
- *incoming_bytes_size = (size_t) (bytes_cursor - incoming_bytes);
- tsi_fake_frame_reset (frame, 1 /* needs_draining */ );
+ *incoming_bytes_size = (size_t)(bytes_cursor - incoming_bytes);
+ tsi_fake_frame_reset(frame, 1 /* needs_draining */);
return TSI_OK;
}
/* This method should not be called if frame->needs_framing is 0. */
-static tsi_result
-drain_frame_to_bytes (unsigned char *outgoing_bytes, size_t * outgoing_bytes_size, tsi_fake_frame * frame)
-{
+static tsi_result drain_frame_to_bytes(unsigned char *outgoing_bytes,
+ size_t *outgoing_bytes_size,
+ tsi_fake_frame *frame) {
size_t to_write_size = frame->size - frame->offset;
- if (!frame->needs_draining)
- return TSI_INTERNAL_ERROR;
- if (*outgoing_bytes_size < to_write_size)
- {
- memcpy (outgoing_bytes, frame->data + frame->offset, *outgoing_bytes_size);
- frame->offset += *outgoing_bytes_size;
- return TSI_INCOMPLETE_DATA;
- }
- memcpy (outgoing_bytes, frame->data + frame->offset, to_write_size);
+ if (!frame->needs_draining) return TSI_INTERNAL_ERROR;
+ if (*outgoing_bytes_size < to_write_size) {
+ memcpy(outgoing_bytes, frame->data + frame->offset, *outgoing_bytes_size);
+ frame->offset += *outgoing_bytes_size;
+ return TSI_INCOMPLETE_DATA;
+ }
+ memcpy(outgoing_bytes, frame->data + frame->offset, to_write_size);
*outgoing_bytes_size = to_write_size;
- tsi_fake_frame_reset (frame, 0 /* needs_draining */ );
+ tsi_fake_frame_reset(frame, 0 /* needs_draining */);
return TSI_OK;
}
-static tsi_result
-bytes_to_frame (unsigned char *bytes, size_t bytes_size, tsi_fake_frame * frame)
-{
+static tsi_result bytes_to_frame(unsigned char *bytes, size_t bytes_size,
+ tsi_fake_frame *frame) {
frame->offset = 0;
frame->size = bytes_size + TSI_FAKE_FRAME_HEADER_SIZE;
- if (!tsi_fake_frame_ensure_size (frame))
- return TSI_OUT_OF_RESOURCES;
- store32_little_endian ((gpr_uint32) frame->size, frame->data);
- memcpy (frame->data + TSI_FAKE_FRAME_HEADER_SIZE, bytes, bytes_size);
- tsi_fake_frame_reset (frame, 1 /* needs draining */ );
+ if (!tsi_fake_frame_ensure_size(frame)) return TSI_OUT_OF_RESOURCES;
+ store32_little_endian((gpr_uint32)frame->size, frame->data);
+ memcpy(frame->data + TSI_FAKE_FRAME_HEADER_SIZE, bytes, bytes_size);
+ tsi_fake_frame_reset(frame, 1 /* needs draining */);
return TSI_OK;
}
-static void
-tsi_fake_frame_destruct (tsi_fake_frame * frame)
-{
- if (frame->data != NULL)
- free (frame->data);
+static void tsi_fake_frame_destruct(tsi_fake_frame *frame) {
+ if (frame->data != NULL) free(frame->data);
}
/* --- tsi_frame_protector methods implementation. ---*/
-static tsi_result
-fake_protector_protect (tsi_frame_protector * self, const unsigned char *unprotected_bytes, size_t * unprotected_bytes_size, unsigned char *protected_output_frames, size_t * protected_output_frames_size)
-{
+static tsi_result fake_protector_protect(tsi_frame_protector *self,
+ const unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size,
+ unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size) {
tsi_result result = TSI_OK;
- tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *) self;
+ tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *)self;
unsigned char frame_header[TSI_FAKE_FRAME_HEADER_SIZE];
tsi_fake_frame *frame = &impl->protect_frame;
size_t saved_output_size = *protected_output_frames_size;
@@ -281,86 +246,79 @@ fake_protector_protect (tsi_frame_protector * self, const unsigned char *unprote
*num_bytes_written = 0;
/* Try to drain first. */
- if (frame->needs_draining)
- {
- drained_size = saved_output_size - *num_bytes_written;
- result = drain_frame_to_bytes (protected_output_frames, &drained_size, frame);
- *num_bytes_written += drained_size;
- protected_output_frames += drained_size;
- if (result != TSI_OK)
- {
- if (result == TSI_INCOMPLETE_DATA)
- {
- *unprotected_bytes_size = 0;
- result = TSI_OK;
- }
- return result;
- }
+ if (frame->needs_draining) {
+ drained_size = saved_output_size - *num_bytes_written;
+ result =
+ drain_frame_to_bytes(protected_output_frames, &drained_size, frame);
+ *num_bytes_written += drained_size;
+ protected_output_frames += drained_size;
+ if (result != TSI_OK) {
+ if (result == TSI_INCOMPLETE_DATA) {
+ *unprotected_bytes_size = 0;
+ result = TSI_OK;
+ }
+ return result;
}
+ }
/* Now process the unprotected_bytes. */
- if (frame->needs_draining)
- return TSI_INTERNAL_ERROR;
- if (frame->size == 0)
- {
- /* New frame, create a header. */
- size_t written_in_frame_size = 0;
- store32_little_endian ((gpr_uint32) impl->max_frame_size, frame_header);
- written_in_frame_size = TSI_FAKE_FRAME_HEADER_SIZE;
- result = fill_frame_from_bytes (frame_header, &written_in_frame_size, frame);
- if (result != TSI_INCOMPLETE_DATA)
- {
- gpr_log (GPR_ERROR, "fill_frame_from_bytes returned %s", tsi_result_to_string (result));
- return result;
- }
- }
- result = fill_frame_from_bytes (unprotected_bytes, unprotected_bytes_size, frame);
- if (result != TSI_OK)
- {
- if (result == TSI_INCOMPLETE_DATA)
- result = TSI_OK;
+ if (frame->needs_draining) return TSI_INTERNAL_ERROR;
+ if (frame->size == 0) {
+ /* New frame, create a header. */
+ size_t written_in_frame_size = 0;
+ store32_little_endian((gpr_uint32)impl->max_frame_size, frame_header);
+ written_in_frame_size = TSI_FAKE_FRAME_HEADER_SIZE;
+ result = fill_frame_from_bytes(frame_header, &written_in_frame_size, frame);
+ if (result != TSI_INCOMPLETE_DATA) {
+ gpr_log(GPR_ERROR, "fill_frame_from_bytes returned %s",
+ tsi_result_to_string(result));
return result;
}
+ }
+ result =
+ fill_frame_from_bytes(unprotected_bytes, unprotected_bytes_size, frame);
+ if (result != TSI_OK) {
+ if (result == TSI_INCOMPLETE_DATA) result = TSI_OK;
+ return result;
+ }
/* Try to drain again. */
- if (!frame->needs_draining)
- return TSI_INTERNAL_ERROR;
- if (frame->offset != 0)
- return TSI_INTERNAL_ERROR;
+ if (!frame->needs_draining) return TSI_INTERNAL_ERROR;
+ if (frame->offset != 0) return TSI_INTERNAL_ERROR;
drained_size = saved_output_size - *num_bytes_written;
- result = drain_frame_to_bytes (protected_output_frames, &drained_size, frame);
+ result = drain_frame_to_bytes(protected_output_frames, &drained_size, frame);
*num_bytes_written += drained_size;
- if (result == TSI_INCOMPLETE_DATA)
- result = TSI_OK;
+ if (result == TSI_INCOMPLETE_DATA) result = TSI_OK;
return result;
}
-static tsi_result
-fake_protector_protect_flush (tsi_frame_protector * self, unsigned char *protected_output_frames, size_t * protected_output_frames_size, size_t * still_pending_size)
-{
+static tsi_result fake_protector_protect_flush(
+ tsi_frame_protector *self, unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size, size_t *still_pending_size) {
tsi_result result = TSI_OK;
- tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *) self;
+ tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *)self;
tsi_fake_frame *frame = &impl->protect_frame;
- if (!frame->needs_draining)
- {
- /* Create a short frame. */
- frame->size = frame->offset;
- frame->offset = 0;
- frame->needs_draining = 1;
- store32_little_endian ((gpr_uint32) frame->size, frame->data); /* Overwrite header. */
- }
- result = drain_frame_to_bytes (protected_output_frames, protected_output_frames_size, frame);
- if (result == TSI_INCOMPLETE_DATA)
- result = TSI_OK;
+ if (!frame->needs_draining) {
+ /* Create a short frame. */
+ frame->size = frame->offset;
+ frame->offset = 0;
+ frame->needs_draining = 1;
+ store32_little_endian((gpr_uint32)frame->size,
+ frame->data); /* Overwrite header. */
+ }
+ result = drain_frame_to_bytes(protected_output_frames,
+ protected_output_frames_size, frame);
+ if (result == TSI_INCOMPLETE_DATA) result = TSI_OK;
*still_pending_size = frame->size - frame->offset;
return result;
}
-static tsi_result
-fake_protector_unprotect (tsi_frame_protector * self, const unsigned char *protected_frames_bytes, size_t * protected_frames_bytes_size, unsigned char *unprotected_bytes, size_t * unprotected_bytes_size)
-{
+static tsi_result fake_protector_unprotect(
+ tsi_frame_protector *self, const unsigned char *protected_frames_bytes,
+ size_t *protected_frames_bytes_size, unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size) {
tsi_result result = TSI_OK;
- tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *) self;
+ tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *)self;
tsi_fake_frame *frame = &impl->unprotect_frame;
size_t saved_output_size = *unprotected_bytes_size;
size_t drained_size = 0;
@@ -368,232 +326,202 @@ fake_protector_unprotect (tsi_frame_protector * self, const unsigned char *prote
*num_bytes_written = 0;
/* Try to drain first. */
- if (frame->needs_draining)
- {
- /* Go past the header if needed. */
- if (frame->offset == 0)
- frame->offset = TSI_FAKE_FRAME_HEADER_SIZE;
- drained_size = saved_output_size - *num_bytes_written;
- result = drain_frame_to_bytes (unprotected_bytes, &drained_size, frame);
- unprotected_bytes += drained_size;
- *num_bytes_written += drained_size;
- if (result != TSI_OK)
- {
- if (result == TSI_INCOMPLETE_DATA)
- {
- *protected_frames_bytes_size = 0;
- result = TSI_OK;
- }
- return result;
- }
+ if (frame->needs_draining) {
+ /* Go past the header if needed. */
+ if (frame->offset == 0) frame->offset = TSI_FAKE_FRAME_HEADER_SIZE;
+ drained_size = saved_output_size - *num_bytes_written;
+ result = drain_frame_to_bytes(unprotected_bytes, &drained_size, frame);
+ unprotected_bytes += drained_size;
+ *num_bytes_written += drained_size;
+ if (result != TSI_OK) {
+ if (result == TSI_INCOMPLETE_DATA) {
+ *protected_frames_bytes_size = 0;
+ result = TSI_OK;
+ }
+ return result;
}
+ }
/* Now process the protected_bytes. */
- if (frame->needs_draining)
- return TSI_INTERNAL_ERROR;
- result = fill_frame_from_bytes (protected_frames_bytes, protected_frames_bytes_size, frame);
- if (result != TSI_OK)
- {
- if (result == TSI_INCOMPLETE_DATA)
- result = TSI_OK;
- return result;
- }
+ if (frame->needs_draining) return TSI_INTERNAL_ERROR;
+ result = fill_frame_from_bytes(protected_frames_bytes,
+ protected_frames_bytes_size, frame);
+ if (result != TSI_OK) {
+ if (result == TSI_INCOMPLETE_DATA) result = TSI_OK;
+ return result;
+ }
/* Try to drain again. */
- if (!frame->needs_draining)
- return TSI_INTERNAL_ERROR;
- if (frame->offset != 0)
- return TSI_INTERNAL_ERROR;
- frame->offset = TSI_FAKE_FRAME_HEADER_SIZE; /* Go past the header. */
+ if (!frame->needs_draining) return TSI_INTERNAL_ERROR;
+ if (frame->offset != 0) return TSI_INTERNAL_ERROR;
+ frame->offset = TSI_FAKE_FRAME_HEADER_SIZE; /* Go past the header. */
drained_size = saved_output_size - *num_bytes_written;
- result = drain_frame_to_bytes (unprotected_bytes, &drained_size, frame);
+ result = drain_frame_to_bytes(unprotected_bytes, &drained_size, frame);
*num_bytes_written += drained_size;
- if (result == TSI_INCOMPLETE_DATA)
- result = TSI_OK;
+ if (result == TSI_INCOMPLETE_DATA) result = TSI_OK;
return result;
}
-static void
-fake_protector_destroy (tsi_frame_protector * self)
-{
- tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *) self;
- tsi_fake_frame_destruct (&impl->protect_frame);
- tsi_fake_frame_destruct (&impl->unprotect_frame);
- free (self);
+static void fake_protector_destroy(tsi_frame_protector *self) {
+ tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *)self;
+ tsi_fake_frame_destruct(&impl->protect_frame);
+ tsi_fake_frame_destruct(&impl->unprotect_frame);
+ free(self);
}
static const tsi_frame_protector_vtable frame_protector_vtable = {
- fake_protector_protect, fake_protector_protect_flush,
- fake_protector_unprotect, fake_protector_destroy,
+ fake_protector_protect, fake_protector_protect_flush,
+ fake_protector_unprotect, fake_protector_destroy,
};
/* --- tsi_handshaker methods implementation. ---*/
-static tsi_result
-fake_handshaker_get_bytes_to_send_to_peer (tsi_handshaker * self, unsigned char *bytes, size_t * bytes_size)
-{
- tsi_fake_handshaker *impl = (tsi_fake_handshaker *) self;
+static tsi_result fake_handshaker_get_bytes_to_send_to_peer(
+ tsi_handshaker *self, unsigned char *bytes, size_t *bytes_size) {
+ tsi_fake_handshaker *impl = (tsi_fake_handshaker *)self;
tsi_result result = TSI_OK;
- if (impl->needs_incoming_message || impl->result == TSI_OK)
- {
- *bytes_size = 0;
- return TSI_OK;
+ if (impl->needs_incoming_message || impl->result == TSI_OK) {
+ *bytes_size = 0;
+ return TSI_OK;
+ }
+ if (!impl->outgoing.needs_draining) {
+ tsi_fake_handshake_message next_message_to_send =
+ impl->next_message_to_send + 2;
+ const char *msg_string =
+ tsi_fake_handshake_message_to_string(impl->next_message_to_send);
+ result = bytes_to_frame((unsigned char *)msg_string, strlen(msg_string),
+ &impl->outgoing);
+ if (result != TSI_OK) return result;
+ if (next_message_to_send > TSI_FAKE_HANDSHAKE_MESSAGE_MAX) {
+ next_message_to_send = TSI_FAKE_HANDSHAKE_MESSAGE_MAX;
}
- if (!impl->outgoing.needs_draining)
- {
- tsi_fake_handshake_message next_message_to_send = impl->next_message_to_send + 2;
- const char *msg_string = tsi_fake_handshake_message_to_string (impl->next_message_to_send);
- result = bytes_to_frame ((unsigned char *) msg_string, strlen (msg_string), &impl->outgoing);
- if (result != TSI_OK)
- return result;
- if (next_message_to_send > TSI_FAKE_HANDSHAKE_MESSAGE_MAX)
- {
- next_message_to_send = TSI_FAKE_HANDSHAKE_MESSAGE_MAX;
- }
- if (tsi_tracing_enabled)
- {
- gpr_log (GPR_INFO, "%s prepared %s.", impl->is_client ? "Client" : "Server", tsi_fake_handshake_message_to_string (impl->next_message_to_send));
- }
- impl->next_message_to_send = next_message_to_send;
+ if (tsi_tracing_enabled) {
+ gpr_log(GPR_INFO, "%s prepared %s.",
+ impl->is_client ? "Client" : "Server",
+ tsi_fake_handshake_message_to_string(impl->next_message_to_send));
}
- result = drain_frame_to_bytes (bytes, bytes_size, &impl->outgoing);
- if (result != TSI_OK)
- return result;
- if (!impl->is_client && impl->next_message_to_send == TSI_FAKE_HANDSHAKE_MESSAGE_MAX)
- {
- /* We're done. */
- if (tsi_tracing_enabled)
- {
- gpr_log (GPR_INFO, "Server is done.");
- }
- impl->result = TSI_OK;
- }
- else
- {
- impl->needs_incoming_message = 1;
+ impl->next_message_to_send = next_message_to_send;
+ }
+ result = drain_frame_to_bytes(bytes, bytes_size, &impl->outgoing);
+ if (result != TSI_OK) return result;
+ if (!impl->is_client &&
+ impl->next_message_to_send == TSI_FAKE_HANDSHAKE_MESSAGE_MAX) {
+ /* We're done. */
+ if (tsi_tracing_enabled) {
+ gpr_log(GPR_INFO, "Server is done.");
}
+ impl->result = TSI_OK;
+ } else {
+ impl->needs_incoming_message = 1;
+ }
return TSI_OK;
}
-static tsi_result
-fake_handshaker_process_bytes_from_peer (tsi_handshaker * self, const unsigned char *bytes, size_t * bytes_size)
-{
+static tsi_result fake_handshaker_process_bytes_from_peer(
+ tsi_handshaker *self, const unsigned char *bytes, size_t *bytes_size) {
tsi_result result = TSI_OK;
- tsi_fake_handshaker *impl = (tsi_fake_handshaker *) self;
+ tsi_fake_handshaker *impl = (tsi_fake_handshaker *)self;
tsi_fake_handshake_message expected_msg = impl->next_message_to_send - 1;
tsi_fake_handshake_message received_msg;
- if (!impl->needs_incoming_message || impl->result == TSI_OK)
- {
- *bytes_size = 0;
- return TSI_OK;
- }
- result = fill_frame_from_bytes (bytes, bytes_size, &impl->incoming);
- if (result != TSI_OK)
- return result;
+ if (!impl->needs_incoming_message || impl->result == TSI_OK) {
+ *bytes_size = 0;
+ return TSI_OK;
+ }
+ result = fill_frame_from_bytes(bytes, bytes_size, &impl->incoming);
+ if (result != TSI_OK) return result;
/* We now have a complete frame. */
- result = tsi_fake_handshake_message_from_string ((const char *) impl->incoming.data + TSI_FAKE_FRAME_HEADER_SIZE, &received_msg);
- if (result != TSI_OK)
- {
- impl->result = result;
- return result;
- }
- if (received_msg != expected_msg)
- {
- gpr_log (GPR_ERROR, "Invalid received message (%s instead of %s)", tsi_fake_handshake_message_to_string (received_msg), tsi_fake_handshake_message_to_string (expected_msg));
- }
- if (tsi_tracing_enabled)
- {
- gpr_log (GPR_INFO, "%s received %s.", impl->is_client ? "Client" : "Server", tsi_fake_handshake_message_to_string (received_msg));
- }
- tsi_fake_frame_reset (&impl->incoming, 0 /* needs_draining */ );
+ result = tsi_fake_handshake_message_from_string(
+ (const char *)impl->incoming.data + TSI_FAKE_FRAME_HEADER_SIZE,
+ &received_msg);
+ if (result != TSI_OK) {
+ impl->result = result;
+ return result;
+ }
+ if (received_msg != expected_msg) {
+ gpr_log(GPR_ERROR, "Invalid received message (%s instead of %s)",
+ tsi_fake_handshake_message_to_string(received_msg),
+ tsi_fake_handshake_message_to_string(expected_msg));
+ }
+ if (tsi_tracing_enabled) {
+ gpr_log(GPR_INFO, "%s received %s.", impl->is_client ? "Client" : "Server",
+ tsi_fake_handshake_message_to_string(received_msg));
+ }
+ tsi_fake_frame_reset(&impl->incoming, 0 /* needs_draining */);
impl->needs_incoming_message = 0;
- if (impl->next_message_to_send == TSI_FAKE_HANDSHAKE_MESSAGE_MAX)
- {
- /* We're done. */
- if (tsi_tracing_enabled)
- {
- gpr_log (GPR_INFO, "%s is done.", impl->is_client ? "Client" : "Server");
- }
- impl->result = TSI_OK;
+ if (impl->next_message_to_send == TSI_FAKE_HANDSHAKE_MESSAGE_MAX) {
+ /* We're done. */
+ if (tsi_tracing_enabled) {
+ gpr_log(GPR_INFO, "%s is done.", impl->is_client ? "Client" : "Server");
}
+ impl->result = TSI_OK;
+ }
return TSI_OK;
}
-static tsi_result
-fake_handshaker_get_result (tsi_handshaker * self)
-{
- tsi_fake_handshaker *impl = (tsi_fake_handshaker *) self;
+static tsi_result fake_handshaker_get_result(tsi_handshaker *self) {
+ tsi_fake_handshaker *impl = (tsi_fake_handshaker *)self;
return impl->result;
}
-static tsi_result
-fake_handshaker_extract_peer (tsi_handshaker * self, tsi_peer * peer)
-{
- tsi_result result = tsi_construct_peer (1, peer);
- if (result != TSI_OK)
- return result;
- result = tsi_construct_string_peer_property_from_cstring (TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_FAKE_CERTIFICATE_TYPE, &peer->properties[0]);
- if (result != TSI_OK)
- tsi_peer_destruct (peer);
+static tsi_result fake_handshaker_extract_peer(tsi_handshaker *self,
+ tsi_peer *peer) {
+ tsi_result result = tsi_construct_peer(1, peer);
+ if (result != TSI_OK) return result;
+ result = tsi_construct_string_peer_property_from_cstring(
+ TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_FAKE_CERTIFICATE_TYPE,
+ &peer->properties[0]);
+ if (result != TSI_OK) tsi_peer_destruct(peer);
return result;
}
-static tsi_result
-fake_handshaker_create_frame_protector (tsi_handshaker * self, size_t * max_protected_frame_size, tsi_frame_protector ** protector)
-{
- *protector = tsi_create_fake_protector (max_protected_frame_size);
- if (*protector == NULL)
- return TSI_OUT_OF_RESOURCES;
+static tsi_result fake_handshaker_create_frame_protector(
+ tsi_handshaker *self, size_t *max_protected_frame_size,
+ tsi_frame_protector **protector) {
+ *protector = tsi_create_fake_protector(max_protected_frame_size);
+ if (*protector == NULL) return TSI_OUT_OF_RESOURCES;
return TSI_OK;
}
-static void
-fake_handshaker_destroy (tsi_handshaker * self)
-{
- tsi_fake_handshaker *impl = (tsi_fake_handshaker *) self;
- tsi_fake_frame_destruct (&impl->incoming);
- tsi_fake_frame_destruct (&impl->outgoing);
- free (self);
+static void fake_handshaker_destroy(tsi_handshaker *self) {
+ tsi_fake_handshaker *impl = (tsi_fake_handshaker *)self;
+ tsi_fake_frame_destruct(&impl->incoming);
+ tsi_fake_frame_destruct(&impl->outgoing);
+ free(self);
}
static const tsi_handshaker_vtable handshaker_vtable = {
- fake_handshaker_get_bytes_to_send_to_peer,
- fake_handshaker_process_bytes_from_peer,
- fake_handshaker_get_result,
- fake_handshaker_extract_peer,
- fake_handshaker_create_frame_protector,
- fake_handshaker_destroy,
+ fake_handshaker_get_bytes_to_send_to_peer,
+ fake_handshaker_process_bytes_from_peer,
+ fake_handshaker_get_result,
+ fake_handshaker_extract_peer,
+ fake_handshaker_create_frame_protector,
+ fake_handshaker_destroy,
};
-tsi_handshaker *
-tsi_create_fake_handshaker (int is_client)
-{
- tsi_fake_handshaker *impl = calloc (1, sizeof (tsi_fake_handshaker));
+tsi_handshaker *tsi_create_fake_handshaker(int is_client) {
+ tsi_fake_handshaker *impl = calloc(1, sizeof(tsi_fake_handshaker));
impl->base.vtable = &handshaker_vtable;
impl->is_client = is_client;
impl->result = TSI_HANDSHAKE_IN_PROGRESS;
- if (is_client)
- {
- impl->needs_incoming_message = 0;
- impl->next_message_to_send = TSI_FAKE_CLIENT_INIT;
- }
- else
- {
- impl->needs_incoming_message = 1;
- impl->next_message_to_send = TSI_FAKE_SERVER_INIT;
- }
+ if (is_client) {
+ impl->needs_incoming_message = 0;
+ impl->next_message_to_send = TSI_FAKE_CLIENT_INIT;
+ } else {
+ impl->needs_incoming_message = 1;
+ impl->next_message_to_send = TSI_FAKE_SERVER_INIT;
+ }
return &impl->base;
}
-tsi_frame_protector *
-tsi_create_fake_protector (size_t * max_protected_frame_size)
-{
- tsi_fake_frame_protector *impl = calloc (1, sizeof (tsi_fake_frame_protector));
- if (impl == NULL)
- return NULL;
- impl->max_frame_size = (max_protected_frame_size == NULL) ? TSI_FAKE_DEFAULT_FRAME_SIZE : *max_protected_frame_size;
+tsi_frame_protector *tsi_create_fake_protector(
+ size_t *max_protected_frame_size) {
+ tsi_fake_frame_protector *impl = calloc(1, sizeof(tsi_fake_frame_protector));
+ if (impl == NULL) return NULL;
+ impl->max_frame_size = (max_protected_frame_size == NULL)
+ ? TSI_FAKE_DEFAULT_FRAME_SIZE
+ : *max_protected_frame_size;
impl->base.vtable = &frame_protector_vtable;
return &impl->base;
}
diff --git a/src/core/tsi/fake_transport_security.h b/src/core/tsi/fake_transport_security.h
index 27b4008c94..fe295aa536 100644
--- a/src/core/tsi/fake_transport_security.h
+++ b/src/core/tsi/fake_transport_security.h
@@ -37,8 +37,7 @@
#include "src/core/tsi/transport_security_interface.h"
#ifdef __cplusplus
-extern "C"
-{
+extern "C" {
#endif
/* Value for the TSI_CERTIFICATE_TYPE_PEER_PROPERTY property for FAKE certs. */
@@ -49,13 +48,14 @@ extern "C"
No cryptography is performed in these objects. They just simulate handshake
messages going back and forth for the handshaker and do some framing on
cleartext data for the protector. */
- tsi_handshaker *tsi_create_fake_handshaker (int is_client);
+tsi_handshaker *tsi_create_fake_handshaker(int is_client);
/* Creates a protector directly without going through the handshake phase. */
- tsi_frame_protector *tsi_create_fake_protector (size_t * max_protected_frame_size);
+tsi_frame_protector *tsi_create_fake_protector(
+ size_t *max_protected_frame_size);
#ifdef __cplusplus
}
#endif
-#endif /* GRPC_INTERNAL_CORE_TSI_FAKE_TRANSPORT_SECURITY_H */
+#endif /* GRPC_INTERNAL_CORE_TSI_FAKE_TRANSPORT_SECURITY_H */
diff --git a/src/core/tsi/ssl_transport_security.c b/src/core/tsi/ssl_transport_security.c
index a4e3503a96..ad6b2d7684 100644
--- a/src/core/tsi/ssl_transport_security.c
+++ b/src/core/tsi/ssl_transport_security.c
@@ -43,7 +43,7 @@
#include "src/core/tsi/transport_security.h"
#include <openssl/bio.h>
-#include <openssl/crypto.h> /* For OPENSSL_free */
+#include <openssl/crypto.h> /* For OPENSSL_free */
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <openssl/x509.h>
@@ -67,22 +67,21 @@
/* --- Structure definitions. ---*/
-struct tsi_ssl_handshaker_factory
-{
- tsi_result (*create_handshaker) (tsi_ssl_handshaker_factory * self, const char *server_name_indication, tsi_handshaker ** handshaker);
- void (*destroy) (tsi_ssl_handshaker_factory * self);
+struct tsi_ssl_handshaker_factory {
+ tsi_result (*create_handshaker)(tsi_ssl_handshaker_factory *self,
+ const char *server_name_indication,
+ tsi_handshaker **handshaker);
+ void (*destroy)(tsi_ssl_handshaker_factory *self);
};
-typedef struct
-{
+typedef struct {
tsi_ssl_handshaker_factory base;
SSL_CTX *ssl_context;
unsigned char *alpn_protocol_list;
size_t alpn_protocol_list_length;
} tsi_ssl_client_handshaker_factory;
-typedef struct
-{
+typedef struct {
tsi_ssl_handshaker_factory base;
/* Several contexts to support SNI.
@@ -95,8 +94,7 @@ typedef struct
size_t alpn_protocol_list_length;
} tsi_ssl_server_handshaker_factory;
-typedef struct
-{
+typedef struct {
tsi_handshaker base;
SSL *ssl;
BIO *into_ssl;
@@ -104,8 +102,7 @@ typedef struct
tsi_result result;
} tsi_ssl_handshaker;
-typedef struct
-{
+typedef struct {
tsi_frame_protector base;
SSL *ssl;
BIO *into_ssl;
@@ -120,52 +117,39 @@ typedef struct
static gpr_once init_openssl_once = GPR_ONCE_INIT;
static gpr_mu *openssl_mutexes = NULL;
-static void
-openssl_locking_cb (int mode, int type, const char *file, int line)
-{
- if (mode & CRYPTO_LOCK)
- {
- gpr_mu_lock (&openssl_mutexes[type]);
- }
- else
- {
- gpr_mu_unlock (&openssl_mutexes[type]);
- }
+static void openssl_locking_cb(int mode, int type, const char *file, int line) {
+ if (mode & CRYPTO_LOCK) {
+ gpr_mu_lock(&openssl_mutexes[type]);
+ } else {
+ gpr_mu_unlock(&openssl_mutexes[type]);
+ }
}
-static unsigned long
-openssl_thread_id_cb (void)
-{
- return (unsigned long) gpr_thd_currentid ();
+static unsigned long openssl_thread_id_cb(void) {
+ return (unsigned long)gpr_thd_currentid();
}
-static void
-init_openssl (void)
-{
+static void init_openssl(void) {
int i;
int num_locks;
- SSL_library_init ();
- SSL_load_error_strings ();
- OpenSSL_add_all_algorithms ();
- num_locks = CRYPTO_num_locks ();
- GPR_ASSERT (num_locks > 0);
- openssl_mutexes = malloc ((size_t) num_locks * sizeof (gpr_mu));
- GPR_ASSERT (openssl_mutexes != NULL);
- for (i = 0; i < CRYPTO_num_locks (); i++)
- {
- gpr_mu_init (&openssl_mutexes[i]);
- }
- CRYPTO_set_locking_callback (openssl_locking_cb);
- CRYPTO_set_id_callback (openssl_thread_id_cb);
+ SSL_library_init();
+ SSL_load_error_strings();
+ OpenSSL_add_all_algorithms();
+ num_locks = CRYPTO_num_locks();
+ GPR_ASSERT(num_locks > 0);
+ openssl_mutexes = malloc((size_t)num_locks * sizeof(gpr_mu));
+ GPR_ASSERT(openssl_mutexes != NULL);
+ for (i = 0; i < CRYPTO_num_locks(); i++) {
+ gpr_mu_init(&openssl_mutexes[i]);
+ }
+ CRYPTO_set_locking_callback(openssl_locking_cb);
+ CRYPTO_set_id_callback(openssl_thread_id_cb);
}
/* --- Ssl utils. ---*/
-static const char *
-ssl_error_string (int error)
-{
- switch (error)
- {
+static const char *ssl_error_string(int error) {
+ switch (error) {
case SSL_ERROR_NONE:
return "SSL_ERROR_NONE";
case SSL_ERROR_ZERO_RETURN:
@@ -186,885 +170,791 @@ ssl_error_string (int error)
return "SSL_ERROR_SSL";
default:
return "Unknown error";
- }
+ }
}
/* TODO(jboeuf): Remove when we are past the debugging phase with this code. */
-static void
-ssl_log_where_info (const SSL * ssl, int where, int flag, const char *msg)
-{
- if ((where & flag) && tsi_tracing_enabled)
- {
- gpr_log (GPR_INFO, "%20.20s - %30.30s - %5.10s", msg, SSL_state_string_long (ssl), SSL_state_string (ssl));
- }
+static void ssl_log_where_info(const SSL *ssl, int where, int flag,
+ const char *msg) {
+ if ((where & flag) && tsi_tracing_enabled) {
+ gpr_log(GPR_INFO, "%20.20s - %30.30s - %5.10s", msg,
+ SSL_state_string_long(ssl), SSL_state_string(ssl));
+ }
}
/* Used for debugging. TODO(jboeuf): Remove when code is mature enough. */
-static void
-ssl_info_callback (const SSL * ssl, int where, int ret)
-{
- if (ret == 0)
- {
- gpr_log (GPR_ERROR, "ssl_info_callback: error occured.\n");
- return;
- }
+static void ssl_info_callback(const SSL *ssl, int where, int ret) {
+ if (ret == 0) {
+ gpr_log(GPR_ERROR, "ssl_info_callback: error occured.\n");
+ return;
+ }
- ssl_log_where_info (ssl, where, SSL_CB_LOOP, "LOOP");
- ssl_log_where_info (ssl, where, SSL_CB_HANDSHAKE_START, "HANDSHAKE START");
- ssl_log_where_info (ssl, where, SSL_CB_HANDSHAKE_DONE, "HANDSHAKE DONE");
+ ssl_log_where_info(ssl, where, SSL_CB_LOOP, "LOOP");
+ ssl_log_where_info(ssl, where, SSL_CB_HANDSHAKE_START, "HANDSHAKE START");
+ ssl_log_where_info(ssl, where, SSL_CB_HANDSHAKE_DONE, "HANDSHAKE DONE");
}
/* Returns 1 if name looks like an IP address, 0 otherwise.
This is a very rough heuristic as it does not handle IPV6 or things like:
0300.0250.00.01, 0xC0.0Xa8.0x0.0x1, 000030052000001, 0xc0.052000001 */
-static int
-looks_like_ip_address (const char *name)
-{
+static int looks_like_ip_address(const char *name) {
size_t i;
size_t dot_count = 0;
size_t num_size = 0;
- for (i = 0; i < strlen (name); i++)
- {
- if (name[i] >= '0' && name[i] <= '9')
- {
- if (num_size > 3)
- return 0;
- num_size++;
- }
- else if (name[i] == '.')
- {
- if (dot_count > 3 || num_size == 0)
- return 0;
- dot_count++;
- num_size = 0;
- }
- else
- {
- return 0;
- }
+ for (i = 0; i < strlen(name); i++) {
+ if (name[i] >= '0' && name[i] <= '9') {
+ if (num_size > 3) return 0;
+ num_size++;
+ } else if (name[i] == '.') {
+ if (dot_count > 3 || num_size == 0) return 0;
+ dot_count++;
+ num_size = 0;
+ } else {
+ return 0;
}
- if (dot_count < 3 || num_size == 0)
- return 0;
+ }
+ if (dot_count < 3 || num_size == 0) return 0;
return 1;
}
/* Gets the subject CN from an X509 cert. */
-static tsi_result
-ssl_get_x509_common_name (X509 * cert, unsigned char **utf8, size_t * utf8_size)
-{
+static tsi_result ssl_get_x509_common_name(X509 *cert, unsigned char **utf8,
+ size_t *utf8_size) {
int common_name_index = -1;
X509_NAME_ENTRY *common_name_entry = NULL;
ASN1_STRING *common_name_asn1 = NULL;
- X509_NAME *subject_name = X509_get_subject_name (cert);
+ X509_NAME *subject_name = X509_get_subject_name(cert);
int utf8_returned_size = 0;
- if (subject_name == NULL)
- {
- gpr_log (GPR_ERROR, "Could not get subject name from certificate.");
- return TSI_NOT_FOUND;
- }
- common_name_index = X509_NAME_get_index_by_NID (subject_name, NID_commonName, -1);
- if (common_name_index == -1)
- {
- gpr_log (GPR_ERROR, "Could not get common name of subject from certificate.");
- return TSI_NOT_FOUND;
- }
- common_name_entry = X509_NAME_get_entry (subject_name, common_name_index);
- if (common_name_entry == NULL)
- {
- gpr_log (GPR_ERROR, "Could not get common name entry from certificate.");
- return TSI_INTERNAL_ERROR;
- }
- common_name_asn1 = X509_NAME_ENTRY_get_data (common_name_entry);
- if (common_name_asn1 == NULL)
- {
- gpr_log (GPR_ERROR, "Could not get common name entry asn1 from certificate.");
- return TSI_INTERNAL_ERROR;
- }
- utf8_returned_size = ASN1_STRING_to_UTF8 (utf8, common_name_asn1);
- if (utf8_returned_size < 0)
- {
- gpr_log (GPR_ERROR, "Could not extract utf8 from asn1 string.");
- return TSI_OUT_OF_RESOURCES;
- }
- *utf8_size = (size_t) utf8_returned_size;
+ if (subject_name == NULL) {
+ gpr_log(GPR_ERROR, "Could not get subject name from certificate.");
+ return TSI_NOT_FOUND;
+ }
+ common_name_index =
+ X509_NAME_get_index_by_NID(subject_name, NID_commonName, -1);
+ if (common_name_index == -1) {
+ gpr_log(GPR_ERROR,
+ "Could not get common name of subject from certificate.");
+ return TSI_NOT_FOUND;
+ }
+ common_name_entry = X509_NAME_get_entry(subject_name, common_name_index);
+ if (common_name_entry == NULL) {
+ gpr_log(GPR_ERROR, "Could not get common name entry from certificate.");
+ return TSI_INTERNAL_ERROR;
+ }
+ common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry);
+ if (common_name_asn1 == NULL) {
+ gpr_log(GPR_ERROR,
+ "Could not get common name entry asn1 from certificate.");
+ return TSI_INTERNAL_ERROR;
+ }
+ utf8_returned_size = ASN1_STRING_to_UTF8(utf8, common_name_asn1);
+ if (utf8_returned_size < 0) {
+ gpr_log(GPR_ERROR, "Could not extract utf8 from asn1 string.");
+ return TSI_OUT_OF_RESOURCES;
+ }
+ *utf8_size = (size_t)utf8_returned_size;
return TSI_OK;
}
/* Gets the subject CN of an X509 cert as a tsi_peer_property. */
-static tsi_result
-peer_property_from_x509_common_name (X509 * cert, tsi_peer_property * property)
-{
+static tsi_result peer_property_from_x509_common_name(
+ X509 *cert, tsi_peer_property *property) {
unsigned char *common_name;
size_t common_name_size;
- tsi_result result = ssl_get_x509_common_name (cert, &common_name, &common_name_size);
- if (result != TSI_OK)
- {
- if (result == TSI_NOT_FOUND)
- {
- common_name = NULL;
- common_name_size = 0;
- }
- else
- {
- return result;
- }
+ tsi_result result =
+ ssl_get_x509_common_name(cert, &common_name, &common_name_size);
+ if (result != TSI_OK) {
+ if (result == TSI_NOT_FOUND) {
+ common_name = NULL;
+ common_name_size = 0;
+ } else {
+ return result;
}
- result = tsi_construct_string_peer_property (TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, common_name == NULL ? "" : (const char *) common_name, common_name_size, property);
- OPENSSL_free (common_name);
+ }
+ result = tsi_construct_string_peer_property(
+ TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY,
+ common_name == NULL ? "" : (const char *)common_name, common_name_size,
+ property);
+ OPENSSL_free(common_name);
return result;
}
/* Gets the subject SANs from an X509 cert as a tsi_peer_property. */
-static tsi_result
-add_subject_alt_names_properties_to_peer (tsi_peer * peer, GENERAL_NAMES * subject_alt_names, size_t subject_alt_name_count)
-{
+static tsi_result add_subject_alt_names_properties_to_peer(
+ tsi_peer *peer, GENERAL_NAMES *subject_alt_names,
+ size_t subject_alt_name_count) {
size_t i;
tsi_result result = TSI_OK;
/* Reset for DNS entries filtering. */
peer->property_count -= subject_alt_name_count;
- for (i = 0; i < subject_alt_name_count; i++)
- {
- GENERAL_NAME *subject_alt_name = sk_GENERAL_NAME_value (subject_alt_names, (int) i);
- /* Filter out the non-dns entries names. */
- if (subject_alt_name->type == GEN_DNS)
- {
- unsigned char *dns_name = NULL;
- int dns_name_size = ASN1_STRING_to_UTF8 (&dns_name, subject_alt_name->d.dNSName);
- if (dns_name_size < 0)
- {
- gpr_log (GPR_ERROR, "Could not get utf8 from asn1 string.");
- result = TSI_INTERNAL_ERROR;
- break;
- }
- result = tsi_construct_string_peer_property (TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, (const char *) dns_name, (size_t) dns_name_size, &peer->properties[peer->property_count++]);
- OPENSSL_free (dns_name);
- if (result != TSI_OK)
- break;
- }
+ for (i = 0; i < subject_alt_name_count; i++) {
+ GENERAL_NAME *subject_alt_name =
+ sk_GENERAL_NAME_value(subject_alt_names, (int)i);
+ /* Filter out the non-dns entries names. */
+ if (subject_alt_name->type == GEN_DNS) {
+ unsigned char *dns_name = NULL;
+ int dns_name_size =
+ ASN1_STRING_to_UTF8(&dns_name, subject_alt_name->d.dNSName);
+ if (dns_name_size < 0) {
+ gpr_log(GPR_ERROR, "Could not get utf8 from asn1 string.");
+ result = TSI_INTERNAL_ERROR;
+ break;
+ }
+ result = tsi_construct_string_peer_property(
+ TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
+ (const char *)dns_name, (size_t)dns_name_size,
+ &peer->properties[peer->property_count++]);
+ OPENSSL_free(dns_name);
+ if (result != TSI_OK) break;
}
+ }
return result;
}
/* Gets information about the peer's X509 cert as a tsi_peer object. */
-static tsi_result
-peer_from_x509 (X509 * cert, int include_certificate_type, tsi_peer * peer)
-{
+static tsi_result peer_from_x509(X509 *cert, int include_certificate_type,
+ tsi_peer *peer) {
/* TODO(jboeuf): Maybe add more properties. */
- GENERAL_NAMES *subject_alt_names = X509_get_ext_d2i (cert, NID_subject_alt_name, 0, 0);
- int subject_alt_name_count = (subject_alt_names != NULL) ? sk_GENERAL_NAME_num (subject_alt_names) : 0;
+ GENERAL_NAMES *subject_alt_names =
+ X509_get_ext_d2i(cert, NID_subject_alt_name, 0, 0);
+ int subject_alt_name_count =
+ (subject_alt_names != NULL) ? sk_GENERAL_NAME_num(subject_alt_names) : 0;
size_t property_count;
tsi_result result;
- GPR_ASSERT (subject_alt_name_count >= 0);
- property_count = (include_certificate_type ? (size_t) 1 : 0) + 1 /* common name */ + (size_t) subject_alt_name_count;
- result = tsi_construct_peer (property_count, peer);
- if (result != TSI_OK)
- return result;
- do
- {
- if (include_certificate_type)
- {
- result = tsi_construct_string_peer_property_from_cstring (TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_X509_CERTIFICATE_TYPE, &peer->properties[0]);
- if (result != TSI_OK)
- break;
- }
- result = peer_property_from_x509_common_name (cert, &peer->properties[include_certificate_type ? 1 : 0]);
- if (result != TSI_OK)
- break;
-
- if (subject_alt_name_count != 0)
- {
- result = add_subject_alt_names_properties_to_peer (peer, subject_alt_names, (size_t) subject_alt_name_count);
- if (result != TSI_OK)
- break;
- }
- }
- while (0);
-
- if (subject_alt_names != NULL)
- {
- sk_GENERAL_NAME_pop_free (subject_alt_names, GENERAL_NAME_free);
- }
- if (result != TSI_OK)
- tsi_peer_destruct (peer);
+ GPR_ASSERT(subject_alt_name_count >= 0);
+ property_count = (include_certificate_type ? (size_t)1 : 0) +
+ 1 /* common name */ + (size_t)subject_alt_name_count;
+ result = tsi_construct_peer(property_count, peer);
+ if (result != TSI_OK) return result;
+ do {
+ if (include_certificate_type) {
+ result = tsi_construct_string_peer_property_from_cstring(
+ TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_X509_CERTIFICATE_TYPE,
+ &peer->properties[0]);
+ if (result != TSI_OK) break;
+ }
+ result = peer_property_from_x509_common_name(
+ cert, &peer->properties[include_certificate_type ? 1 : 0]);
+ if (result != TSI_OK) break;
+
+ if (subject_alt_name_count != 0) {
+ result = add_subject_alt_names_properties_to_peer(
+ peer, subject_alt_names, (size_t)subject_alt_name_count);
+ if (result != TSI_OK) break;
+ }
+ } while (0);
+
+ if (subject_alt_names != NULL) {
+ sk_GENERAL_NAME_pop_free(subject_alt_names, GENERAL_NAME_free);
+ }
+ if (result != TSI_OK) tsi_peer_destruct(peer);
return result;
}
/* Logs the SSL error stack. */
-static void
-log_ssl_error_stack (void)
-{
+static void log_ssl_error_stack(void) {
unsigned long err;
- while ((err = ERR_get_error ()) != 0)
- {
- char details[256];
- ERR_error_string_n (err, details, sizeof (details));
- gpr_log (GPR_ERROR, "%s", details);
- }
+ while ((err = ERR_get_error()) != 0) {
+ char details[256];
+ ERR_error_string_n(err, details, sizeof(details));
+ gpr_log(GPR_ERROR, "%s", details);
+ }
}
/* Performs an SSL_read and handle errors. */
-static tsi_result
-do_ssl_read (SSL * ssl, unsigned char *unprotected_bytes, size_t * unprotected_bytes_size)
-{
+static tsi_result do_ssl_read(SSL *ssl, unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size) {
int read_from_ssl;
- GPR_ASSERT (*unprotected_bytes_size <= INT_MAX);
- read_from_ssl = SSL_read (ssl, unprotected_bytes, (int) *unprotected_bytes_size);
- if (read_from_ssl == 0)
- {
- gpr_log (GPR_ERROR, "SSL_read returned 0 unexpectedly.");
- return TSI_INTERNAL_ERROR;
- }
- if (read_from_ssl < 0)
- {
- read_from_ssl = SSL_get_error (ssl, read_from_ssl);
- switch (read_from_ssl)
- {
- case SSL_ERROR_WANT_READ:
- /* We need more data to finish the frame. */
- *unprotected_bytes_size = 0;
- return TSI_OK;
- case SSL_ERROR_WANT_WRITE:
- gpr_log (GPR_ERROR, "Peer tried to renegotiate SSL connection. This is unsupported.");
- return TSI_UNIMPLEMENTED;
- case SSL_ERROR_SSL:
- gpr_log (GPR_ERROR, "Corruption detected.");
- log_ssl_error_stack ();
- return TSI_DATA_CORRUPTED;
- default:
- gpr_log (GPR_ERROR, "SSL_read failed with error %s.", ssl_error_string (read_from_ssl));
- return TSI_PROTOCOL_FAILURE;
- }
+ GPR_ASSERT(*unprotected_bytes_size <= INT_MAX);
+ read_from_ssl =
+ SSL_read(ssl, unprotected_bytes, (int)*unprotected_bytes_size);
+ if (read_from_ssl == 0) {
+ gpr_log(GPR_ERROR, "SSL_read returned 0 unexpectedly.");
+ return TSI_INTERNAL_ERROR;
+ }
+ if (read_from_ssl < 0) {
+ read_from_ssl = SSL_get_error(ssl, read_from_ssl);
+ switch (read_from_ssl) {
+ case SSL_ERROR_WANT_READ:
+ /* We need more data to finish the frame. */
+ *unprotected_bytes_size = 0;
+ return TSI_OK;
+ case SSL_ERROR_WANT_WRITE:
+ gpr_log(
+ GPR_ERROR,
+ "Peer tried to renegotiate SSL connection. This is unsupported.");
+ return TSI_UNIMPLEMENTED;
+ case SSL_ERROR_SSL:
+ gpr_log(GPR_ERROR, "Corruption detected.");
+ log_ssl_error_stack();
+ return TSI_DATA_CORRUPTED;
+ default:
+ gpr_log(GPR_ERROR, "SSL_read failed with error %s.",
+ ssl_error_string(read_from_ssl));
+ return TSI_PROTOCOL_FAILURE;
}
- *unprotected_bytes_size = (size_t) read_from_ssl;
+ }
+ *unprotected_bytes_size = (size_t)read_from_ssl;
return TSI_OK;
}
/* Performs an SSL_write and handle errors. */
-static tsi_result
-do_ssl_write (SSL * ssl, unsigned char *unprotected_bytes, size_t unprotected_bytes_size)
-{
+static tsi_result do_ssl_write(SSL *ssl, unsigned char *unprotected_bytes,
+ size_t unprotected_bytes_size) {
int ssl_write_result;
- GPR_ASSERT (unprotected_bytes_size <= INT_MAX);
- ssl_write_result = SSL_write (ssl, unprotected_bytes, (int) unprotected_bytes_size);
- if (ssl_write_result < 0)
- {
- ssl_write_result = SSL_get_error (ssl, ssl_write_result);
- if (ssl_write_result == SSL_ERROR_WANT_READ)
- {
- gpr_log (GPR_ERROR, "Peer tried to renegotiate SSL connection. This is unsupported.");
- return TSI_UNIMPLEMENTED;
- }
- else
- {
- gpr_log (GPR_ERROR, "SSL_write failed with error %s.", ssl_error_string (ssl_write_result));
- return TSI_INTERNAL_ERROR;
- }
+ GPR_ASSERT(unprotected_bytes_size <= INT_MAX);
+ ssl_write_result =
+ SSL_write(ssl, unprotected_bytes, (int)unprotected_bytes_size);
+ if (ssl_write_result < 0) {
+ ssl_write_result = SSL_get_error(ssl, ssl_write_result);
+ if (ssl_write_result == SSL_ERROR_WANT_READ) {
+ gpr_log(GPR_ERROR,
+ "Peer tried to renegotiate SSL connection. This is unsupported.");
+ return TSI_UNIMPLEMENTED;
+ } else {
+ gpr_log(GPR_ERROR, "SSL_write failed with error %s.",
+ ssl_error_string(ssl_write_result));
+ return TSI_INTERNAL_ERROR;
}
+ }
return TSI_OK;
}
/* Loads an in-memory PEM certificate chain into the SSL context. */
-static tsi_result
-ssl_ctx_use_certificate_chain (SSL_CTX * context, const unsigned char *pem_cert_chain, size_t pem_cert_chain_size)
-{
+static tsi_result ssl_ctx_use_certificate_chain(
+ SSL_CTX *context, const unsigned char *pem_cert_chain,
+ size_t pem_cert_chain_size) {
tsi_result result = TSI_OK;
X509 *certificate = NULL;
BIO *pem;
- GPR_ASSERT (pem_cert_chain_size <= INT_MAX);
- pem = BIO_new_mem_buf ((void *) pem_cert_chain, (int) pem_cert_chain_size);
- if (pem == NULL)
- return TSI_OUT_OF_RESOURCES;
+ GPR_ASSERT(pem_cert_chain_size <= INT_MAX);
+ pem = BIO_new_mem_buf((void *)pem_cert_chain, (int)pem_cert_chain_size);
+ if (pem == NULL) return TSI_OUT_OF_RESOURCES;
- do
- {
- certificate = PEM_read_bio_X509_AUX (pem, NULL, NULL, "");
- if (certificate == NULL)
- {
- result = TSI_INVALID_ARGUMENT;
- break;
- }
- if (!SSL_CTX_use_certificate (context, certificate))
- {
- result = TSI_INVALID_ARGUMENT;
- break;
- }
- while (1)
- {
- X509 *certificate_authority = PEM_read_bio_X509 (pem, NULL, NULL, "");
- if (certificate_authority == NULL)
- {
- ERR_clear_error ();
- break; /* Done reading. */
- }
- if (!SSL_CTX_add_extra_chain_cert (context, certificate_authority))
- {
- X509_free (certificate_authority);
- result = TSI_INVALID_ARGUMENT;
- break;
- }
- /* We don't need to free certificate_authority as its ownership has been
- transfered to the context. That is not the case for certificate though.
- */
- }
+ do {
+ certificate = PEM_read_bio_X509_AUX(pem, NULL, NULL, "");
+ if (certificate == NULL) {
+ result = TSI_INVALID_ARGUMENT;
+ break;
+ }
+ if (!SSL_CTX_use_certificate(context, certificate)) {
+ result = TSI_INVALID_ARGUMENT;
+ break;
+ }
+ while (1) {
+ X509 *certificate_authority = PEM_read_bio_X509(pem, NULL, NULL, "");
+ if (certificate_authority == NULL) {
+ ERR_clear_error();
+ break; /* Done reading. */
+ }
+ if (!SSL_CTX_add_extra_chain_cert(context, certificate_authority)) {
+ X509_free(certificate_authority);
+ result = TSI_INVALID_ARGUMENT;
+ break;
+ }
+ /* We don't need to free certificate_authority as its ownership has been
+ transfered to the context. That is not the case for certificate though.
+ */
}
- while (0);
+ } while (0);
- if (certificate != NULL)
- X509_free (certificate);
- BIO_free (pem);
+ if (certificate != NULL) X509_free(certificate);
+ BIO_free(pem);
return result;
}
/* Loads an in-memory PEM private key into the SSL context. */
-static tsi_result
-ssl_ctx_use_private_key (SSL_CTX * context, const unsigned char *pem_key, size_t pem_key_size)
-{
+static tsi_result ssl_ctx_use_private_key(SSL_CTX *context,
+ const unsigned char *pem_key,
+ size_t pem_key_size) {
tsi_result result = TSI_OK;
EVP_PKEY *private_key = NULL;
BIO *pem;
- GPR_ASSERT (pem_key_size <= INT_MAX);
- pem = BIO_new_mem_buf ((void *) pem_key, (int) pem_key_size);
- if (pem == NULL)
- return TSI_OUT_OF_RESOURCES;
- do
- {
- private_key = PEM_read_bio_PrivateKey (pem, NULL, NULL, "");
- if (private_key == NULL)
- {
- result = TSI_INVALID_ARGUMENT;
- break;
- }
- if (!SSL_CTX_use_PrivateKey (context, private_key))
- {
- result = TSI_INVALID_ARGUMENT;
- break;
- }
+ GPR_ASSERT(pem_key_size <= INT_MAX);
+ pem = BIO_new_mem_buf((void *)pem_key, (int)pem_key_size);
+ if (pem == NULL) return TSI_OUT_OF_RESOURCES;
+ do {
+ private_key = PEM_read_bio_PrivateKey(pem, NULL, NULL, "");
+ if (private_key == NULL) {
+ result = TSI_INVALID_ARGUMENT;
+ break;
}
- while (0);
- if (private_key != NULL)
- EVP_PKEY_free (private_key);
- BIO_free (pem);
+ if (!SSL_CTX_use_PrivateKey(context, private_key)) {
+ result = TSI_INVALID_ARGUMENT;
+ break;
+ }
+ } while (0);
+ if (private_key != NULL) EVP_PKEY_free(private_key);
+ BIO_free(pem);
return result;
}
/* Loads in-memory PEM verification certs into the SSL context and optionally
returns the verification cert names (root_names can be NULL). */
-static tsi_result
-ssl_ctx_load_verification_certs (SSL_CTX * context, const unsigned char *pem_roots, size_t pem_roots_size, STACK_OF (X509_NAME) * *root_names)
-{
+static tsi_result ssl_ctx_load_verification_certs(
+ SSL_CTX *context, const unsigned char *pem_roots, size_t pem_roots_size,
+ STACK_OF(X509_NAME) * *root_names) {
tsi_result result = TSI_OK;
size_t num_roots = 0;
X509 *root = NULL;
X509_NAME *root_name = NULL;
BIO *pem;
X509_STORE *root_store;
- GPR_ASSERT (pem_roots_size <= INT_MAX);
- pem = BIO_new_mem_buf ((void *) pem_roots, (int) pem_roots_size);
- root_store = SSL_CTX_get_cert_store (context);
- if (root_store == NULL)
- return TSI_INVALID_ARGUMENT;
- if (pem == NULL)
- return TSI_OUT_OF_RESOURCES;
- if (root_names != NULL)
- {
- *root_names = sk_X509_NAME_new_null ();
- if (*root_names == NULL)
- return TSI_OUT_OF_RESOURCES;
- }
+ GPR_ASSERT(pem_roots_size <= INT_MAX);
+ pem = BIO_new_mem_buf((void *)pem_roots, (int)pem_roots_size);
+ root_store = SSL_CTX_get_cert_store(context);
+ if (root_store == NULL) return TSI_INVALID_ARGUMENT;
+ if (pem == NULL) return TSI_OUT_OF_RESOURCES;
+ if (root_names != NULL) {
+ *root_names = sk_X509_NAME_new_null();
+ if (*root_names == NULL) return TSI_OUT_OF_RESOURCES;
+ }
- while (1)
- {
- root = PEM_read_bio_X509_AUX (pem, NULL, NULL, "");
- if (root == NULL)
- {
- ERR_clear_error ();
- break; /* We're at the end of stream. */
- }
- if (root_names != NULL)
- {
- root_name = X509_get_subject_name (root);
- if (root_name == NULL)
- {
- gpr_log (GPR_ERROR, "Could not get name from root certificate.");
- result = TSI_INVALID_ARGUMENT;
- break;
- }
- root_name = X509_NAME_dup (root_name);
- if (root_name == NULL)
- {
- result = TSI_OUT_OF_RESOURCES;
- break;
- }
- sk_X509_NAME_push (*root_names, root_name);
- root_name = NULL;
- }
- if (!X509_STORE_add_cert (root_store, root))
- {
- gpr_log (GPR_ERROR, "Could not add root certificate to ssl context.");
- result = TSI_INTERNAL_ERROR;
- break;
- }
- X509_free (root);
- num_roots++;
+ while (1) {
+ root = PEM_read_bio_X509_AUX(pem, NULL, NULL, "");
+ if (root == NULL) {
+ ERR_clear_error();
+ break; /* We're at the end of stream. */
+ }
+ if (root_names != NULL) {
+ root_name = X509_get_subject_name(root);
+ if (root_name == NULL) {
+ gpr_log(GPR_ERROR, "Could not get name from root certificate.");
+ result = TSI_INVALID_ARGUMENT;
+ break;
+ }
+ root_name = X509_NAME_dup(root_name);
+ if (root_name == NULL) {
+ result = TSI_OUT_OF_RESOURCES;
+ break;
+ }
+ sk_X509_NAME_push(*root_names, root_name);
+ root_name = NULL;
}
-
- if (num_roots == 0)
- {
- gpr_log (GPR_ERROR, "Could not load any root certificate.");
- result = TSI_INVALID_ARGUMENT;
+ if (!X509_STORE_add_cert(root_store, root)) {
+ gpr_log(GPR_ERROR, "Could not add root certificate to ssl context.");
+ result = TSI_INTERNAL_ERROR;
+ break;
}
+ X509_free(root);
+ num_roots++;
+ }
- if (result != TSI_OK)
- {
- if (root != NULL)
- X509_free (root);
- if (root_names != NULL)
- {
- sk_X509_NAME_pop_free (*root_names, X509_NAME_free);
- *root_names = NULL;
- if (root_name != NULL)
- X509_NAME_free (root_name);
- }
+ if (num_roots == 0) {
+ gpr_log(GPR_ERROR, "Could not load any root certificate.");
+ result = TSI_INVALID_ARGUMENT;
+ }
+
+ if (result != TSI_OK) {
+ if (root != NULL) X509_free(root);
+ if (root_names != NULL) {
+ sk_X509_NAME_pop_free(*root_names, X509_NAME_free);
+ *root_names = NULL;
+ if (root_name != NULL) X509_NAME_free(root_name);
}
- BIO_free (pem);
+ }
+ BIO_free(pem);
return result;
}
/* Populates the SSL context with a private key and a cert chain, and sets the
cipher list and the ephemeral ECDH key. */
-static tsi_result
-populate_ssl_context (SSL_CTX * context, const unsigned char *pem_private_key, size_t pem_private_key_size, const unsigned char *pem_certificate_chain, size_t pem_certificate_chain_size, const char *cipher_list)
-{
+static tsi_result populate_ssl_context(
+ SSL_CTX *context, const unsigned char *pem_private_key,
+ size_t pem_private_key_size, const unsigned char *pem_certificate_chain,
+ size_t pem_certificate_chain_size, const char *cipher_list) {
tsi_result result = TSI_OK;
- if (pem_certificate_chain != NULL)
- {
- result = ssl_ctx_use_certificate_chain (context, pem_certificate_chain, pem_certificate_chain_size);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Invalid cert chain file.");
- return result;
- }
- }
- if (pem_private_key != NULL)
- {
- result = ssl_ctx_use_private_key (context, pem_private_key, pem_private_key_size);
- if (result != TSI_OK || !SSL_CTX_check_private_key (context))
- {
- gpr_log (GPR_ERROR, "Invalid private key.");
- return result != TSI_OK ? result : TSI_INVALID_ARGUMENT;
- }
+ if (pem_certificate_chain != NULL) {
+ result = ssl_ctx_use_certificate_chain(context, pem_certificate_chain,
+ pem_certificate_chain_size);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Invalid cert chain file.");
+ return result;
}
- if ((cipher_list != NULL) && !SSL_CTX_set_cipher_list (context, cipher_list))
- {
- gpr_log (GPR_ERROR, "Invalid cipher list: %s.", cipher_list);
- return TSI_INVALID_ARGUMENT;
+ }
+ if (pem_private_key != NULL) {
+ result =
+ ssl_ctx_use_private_key(context, pem_private_key, pem_private_key_size);
+ if (result != TSI_OK || !SSL_CTX_check_private_key(context)) {
+ gpr_log(GPR_ERROR, "Invalid private key.");
+ return result != TSI_OK ? result : TSI_INVALID_ARGUMENT;
}
+ }
+ if ((cipher_list != NULL) && !SSL_CTX_set_cipher_list(context, cipher_list)) {
+ gpr_log(GPR_ERROR, "Invalid cipher list: %s.", cipher_list);
+ return TSI_INVALID_ARGUMENT;
+ }
{
- EC_KEY *ecdh = EC_KEY_new_by_curve_name (NID_X9_62_prime256v1);
- if (!SSL_CTX_set_tmp_ecdh (context, ecdh))
- {
- gpr_log (GPR_ERROR, "Could not set ephemeral ECDH key.");
- EC_KEY_free (ecdh);
- return TSI_INTERNAL_ERROR;
- }
- SSL_CTX_set_options (context, SSL_OP_SINGLE_ECDH_USE);
- EC_KEY_free (ecdh);
+ EC_KEY *ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
+ if (!SSL_CTX_set_tmp_ecdh(context, ecdh)) {
+ gpr_log(GPR_ERROR, "Could not set ephemeral ECDH key.");
+ EC_KEY_free(ecdh);
+ return TSI_INTERNAL_ERROR;
+ }
+ SSL_CTX_set_options(context, SSL_OP_SINGLE_ECDH_USE);
+ EC_KEY_free(ecdh);
}
return TSI_OK;
}
/* Extracts the CN and the SANs from an X509 cert as a peer object. */
-static tsi_result
-extract_x509_subject_names_from_pem_cert (const unsigned char *pem_cert, size_t pem_cert_size, tsi_peer * peer)
-{
+static tsi_result extract_x509_subject_names_from_pem_cert(
+ const unsigned char *pem_cert, size_t pem_cert_size, tsi_peer *peer) {
tsi_result result = TSI_OK;
X509 *cert = NULL;
BIO *pem;
- GPR_ASSERT (pem_cert_size <= INT_MAX);
- pem = BIO_new_mem_buf ((void *) pem_cert, (int) pem_cert_size);
- if (pem == NULL)
- return TSI_OUT_OF_RESOURCES;
-
- cert = PEM_read_bio_X509 (pem, NULL, NULL, "");
- if (cert == NULL)
- {
- gpr_log (GPR_ERROR, "Invalid certificate");
- result = TSI_INVALID_ARGUMENT;
- }
- else
- {
- result = peer_from_x509 (cert, 0, peer);
- }
- if (cert != NULL)
- X509_free (cert);
- BIO_free (pem);
+ GPR_ASSERT(pem_cert_size <= INT_MAX);
+ pem = BIO_new_mem_buf((void *)pem_cert, (int)pem_cert_size);
+ if (pem == NULL) return TSI_OUT_OF_RESOURCES;
+
+ cert = PEM_read_bio_X509(pem, NULL, NULL, "");
+ if (cert == NULL) {
+ gpr_log(GPR_ERROR, "Invalid certificate");
+ result = TSI_INVALID_ARGUMENT;
+ } else {
+ result = peer_from_x509(cert, 0, peer);
+ }
+ if (cert != NULL) X509_free(cert);
+ BIO_free(pem);
return result;
}
/* Builds the alpn protocol name list according to rfc 7301. */
-static tsi_result
-build_alpn_protocol_name_list (const unsigned char **alpn_protocols, const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols, unsigned char **protocol_name_list, size_t * protocol_name_list_length)
-{
+static tsi_result build_alpn_protocol_name_list(
+ const unsigned char **alpn_protocols,
+ const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols,
+ unsigned char **protocol_name_list, size_t *protocol_name_list_length) {
uint16_t i;
unsigned char *current;
*protocol_name_list = NULL;
*protocol_name_list_length = 0;
- if (num_alpn_protocols == 0)
- return TSI_INVALID_ARGUMENT;
- for (i = 0; i < num_alpn_protocols; i++)
- {
- if (alpn_protocols_lengths[i] == 0)
- {
- gpr_log (GPR_ERROR, "Invalid 0-length protocol name.");
- return TSI_INVALID_ARGUMENT;
- }
- *protocol_name_list_length += (size_t) alpn_protocols_lengths[i] + 1;
+ if (num_alpn_protocols == 0) return TSI_INVALID_ARGUMENT;
+ for (i = 0; i < num_alpn_protocols; i++) {
+ if (alpn_protocols_lengths[i] == 0) {
+ gpr_log(GPR_ERROR, "Invalid 0-length protocol name.");
+ return TSI_INVALID_ARGUMENT;
}
- *protocol_name_list = malloc (*protocol_name_list_length);
- if (*protocol_name_list == NULL)
- return TSI_OUT_OF_RESOURCES;
+ *protocol_name_list_length += (size_t)alpn_protocols_lengths[i] + 1;
+ }
+ *protocol_name_list = malloc(*protocol_name_list_length);
+ if (*protocol_name_list == NULL) return TSI_OUT_OF_RESOURCES;
current = *protocol_name_list;
- for (i = 0; i < num_alpn_protocols; i++)
- {
- *(current++) = alpn_protocols_lengths[i];
- memcpy (current, alpn_protocols[i], alpn_protocols_lengths[i]);
- current += alpn_protocols_lengths[i];
- }
+ for (i = 0; i < num_alpn_protocols; i++) {
+ *(current++) = alpn_protocols_lengths[i];
+ memcpy(current, alpn_protocols[i], alpn_protocols_lengths[i]);
+ current += alpn_protocols_lengths[i];
+ }
/* Safety check. */
- if ((current < *protocol_name_list) || ((gpr_uintptr) (current - *protocol_name_list) != *protocol_name_list_length))
- {
- return TSI_INTERNAL_ERROR;
- }
+ if ((current < *protocol_name_list) ||
+ ((gpr_uintptr)(current - *protocol_name_list) !=
+ *protocol_name_list_length)) {
+ return TSI_INTERNAL_ERROR;
+ }
return TSI_OK;
}
/* --- tsi_frame_protector methods implementation. ---*/
-static tsi_result
-ssl_protector_protect (tsi_frame_protector * self, const unsigned char *unprotected_bytes, size_t * unprotected_bytes_size, unsigned char *protected_output_frames, size_t * protected_output_frames_size)
-{
- tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *) self;
+static tsi_result ssl_protector_protect(tsi_frame_protector *self,
+ const unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size,
+ unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size) {
+ tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *)self;
int read_from_ssl;
size_t available;
tsi_result result = TSI_OK;
/* First see if we have some pending data in the SSL BIO. */
- int pending_in_ssl = BIO_pending (impl->from_ssl);
- if (pending_in_ssl > 0)
- {
- *unprotected_bytes_size = 0;
- GPR_ASSERT (*protected_output_frames_size <= INT_MAX);
- read_from_ssl = BIO_read (impl->from_ssl, protected_output_frames, (int) *protected_output_frames_size);
- if (read_from_ssl < 0)
- {
- gpr_log (GPR_ERROR, "Could not read from BIO even though some data is pending");
- return TSI_INTERNAL_ERROR;
- }
- *protected_output_frames_size = (size_t) read_from_ssl;
- return TSI_OK;
+ int pending_in_ssl = BIO_pending(impl->from_ssl);
+ if (pending_in_ssl > 0) {
+ *unprotected_bytes_size = 0;
+ GPR_ASSERT(*protected_output_frames_size <= INT_MAX);
+ read_from_ssl = BIO_read(impl->from_ssl, protected_output_frames,
+ (int)*protected_output_frames_size);
+ if (read_from_ssl < 0) {
+ gpr_log(GPR_ERROR,
+ "Could not read from BIO even though some data is pending");
+ return TSI_INTERNAL_ERROR;
}
+ *protected_output_frames_size = (size_t)read_from_ssl;
+ return TSI_OK;
+ }
/* Now see if we can send a complete frame. */
available = impl->buffer_size - impl->buffer_offset;
- if (available > *unprotected_bytes_size)
- {
- /* If we cannot, just copy the data in our internal buffer. */
- memcpy (impl->buffer + impl->buffer_offset, unprotected_bytes, *unprotected_bytes_size);
- impl->buffer_offset += *unprotected_bytes_size;
- *protected_output_frames_size = 0;
- return TSI_OK;
- }
+ if (available > *unprotected_bytes_size) {
+ /* If we cannot, just copy the data in our internal buffer. */
+ memcpy(impl->buffer + impl->buffer_offset, unprotected_bytes,
+ *unprotected_bytes_size);
+ impl->buffer_offset += *unprotected_bytes_size;
+ *protected_output_frames_size = 0;
+ return TSI_OK;
+ }
/* If we can, prepare the buffer, send it to SSL_write and read. */
- memcpy (impl->buffer + impl->buffer_offset, unprotected_bytes, available);
- result = do_ssl_write (impl->ssl, impl->buffer, impl->buffer_size);
- if (result != TSI_OK)
- return result;
-
- GPR_ASSERT (*protected_output_frames_size <= INT_MAX);
- read_from_ssl = BIO_read (impl->from_ssl, protected_output_frames, (int) *protected_output_frames_size);
- if (read_from_ssl < 0)
- {
- gpr_log (GPR_ERROR, "Could not read from BIO after SSL_write.");
- return TSI_INTERNAL_ERROR;
- }
- *protected_output_frames_size = (size_t) read_from_ssl;
+ memcpy(impl->buffer + impl->buffer_offset, unprotected_bytes, available);
+ result = do_ssl_write(impl->ssl, impl->buffer, impl->buffer_size);
+ if (result != TSI_OK) return result;
+
+ GPR_ASSERT(*protected_output_frames_size <= INT_MAX);
+ read_from_ssl = BIO_read(impl->from_ssl, protected_output_frames,
+ (int)*protected_output_frames_size);
+ if (read_from_ssl < 0) {
+ gpr_log(GPR_ERROR, "Could not read from BIO after SSL_write.");
+ return TSI_INTERNAL_ERROR;
+ }
+ *protected_output_frames_size = (size_t)read_from_ssl;
*unprotected_bytes_size = available;
impl->buffer_offset = 0;
return TSI_OK;
}
-static tsi_result
-ssl_protector_protect_flush (tsi_frame_protector * self, unsigned char *protected_output_frames, size_t * protected_output_frames_size, size_t * still_pending_size)
-{
+static tsi_result ssl_protector_protect_flush(
+ tsi_frame_protector *self, unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size, size_t *still_pending_size) {
tsi_result result = TSI_OK;
- tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *) self;
+ tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *)self;
int read_from_ssl = 0;
int pending;
- if (impl->buffer_offset != 0)
- {
- result = do_ssl_write (impl->ssl, impl->buffer, impl->buffer_offset);
- if (result != TSI_OK)
- return result;
- impl->buffer_offset = 0;
- }
-
- pending = BIO_pending (impl->from_ssl);
- GPR_ASSERT (pending >= 0);
- *still_pending_size = (size_t) pending;
- if (*still_pending_size == 0)
- return TSI_OK;
+ if (impl->buffer_offset != 0) {
+ result = do_ssl_write(impl->ssl, impl->buffer, impl->buffer_offset);
+ if (result != TSI_OK) return result;
+ impl->buffer_offset = 0;
+ }
- GPR_ASSERT (*protected_output_frames_size <= INT_MAX);
- read_from_ssl = BIO_read (impl->from_ssl, protected_output_frames, (int) *protected_output_frames_size);
- if (read_from_ssl <= 0)
- {
- gpr_log (GPR_ERROR, "Could not read from BIO after SSL_write.");
- return TSI_INTERNAL_ERROR;
- }
- *protected_output_frames_size = (size_t) read_from_ssl;
- pending = BIO_pending (impl->from_ssl);
- GPR_ASSERT (pending >= 0);
- *still_pending_size = (size_t) pending;
+ pending = BIO_pending(impl->from_ssl);
+ GPR_ASSERT(pending >= 0);
+ *still_pending_size = (size_t)pending;
+ if (*still_pending_size == 0) return TSI_OK;
+
+ GPR_ASSERT(*protected_output_frames_size <= INT_MAX);
+ read_from_ssl = BIO_read(impl->from_ssl, protected_output_frames,
+ (int)*protected_output_frames_size);
+ if (read_from_ssl <= 0) {
+ gpr_log(GPR_ERROR, "Could not read from BIO after SSL_write.");
+ return TSI_INTERNAL_ERROR;
+ }
+ *protected_output_frames_size = (size_t)read_from_ssl;
+ pending = BIO_pending(impl->from_ssl);
+ GPR_ASSERT(pending >= 0);
+ *still_pending_size = (size_t)pending;
return TSI_OK;
}
-static tsi_result
-ssl_protector_unprotect (tsi_frame_protector * self, const unsigned char *protected_frames_bytes, size_t * protected_frames_bytes_size, unsigned char *unprotected_bytes, size_t * unprotected_bytes_size)
-{
+static tsi_result ssl_protector_unprotect(
+ tsi_frame_protector *self, const unsigned char *protected_frames_bytes,
+ size_t *protected_frames_bytes_size, unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size) {
tsi_result result = TSI_OK;
int written_into_ssl = 0;
size_t output_bytes_size = *unprotected_bytes_size;
size_t output_bytes_offset = 0;
- tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *) self;
+ tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *)self;
/* First, try to read remaining data from ssl. */
- result = do_ssl_read (impl->ssl, unprotected_bytes, unprotected_bytes_size);
- if (result != TSI_OK)
- return result;
- if (*unprotected_bytes_size == output_bytes_size)
- {
- /* We have read everything we could and cannot process any more input. */
- *protected_frames_bytes_size = 0;
- return TSI_OK;
- }
+ result = do_ssl_read(impl->ssl, unprotected_bytes, unprotected_bytes_size);
+ if (result != TSI_OK) return result;
+ if (*unprotected_bytes_size == output_bytes_size) {
+ /* We have read everything we could and cannot process any more input. */
+ *protected_frames_bytes_size = 0;
+ return TSI_OK;
+ }
output_bytes_offset = *unprotected_bytes_size;
unprotected_bytes += output_bytes_offset;
*unprotected_bytes_size = output_bytes_size - output_bytes_offset;
/* Then, try to write some data to ssl. */
- GPR_ASSERT (*protected_frames_bytes_size <= INT_MAX);
- written_into_ssl = BIO_write (impl->into_ssl, protected_frames_bytes, (int) *protected_frames_bytes_size);
- if (written_into_ssl < 0)
- {
- gpr_log (GPR_ERROR, "Sending protected frame to ssl failed with %d", written_into_ssl);
- return TSI_INTERNAL_ERROR;
- }
- *protected_frames_bytes_size = (size_t) written_into_ssl;
+ GPR_ASSERT(*protected_frames_bytes_size <= INT_MAX);
+ written_into_ssl = BIO_write(impl->into_ssl, protected_frames_bytes,
+ (int)*protected_frames_bytes_size);
+ if (written_into_ssl < 0) {
+ gpr_log(GPR_ERROR, "Sending protected frame to ssl failed with %d",
+ written_into_ssl);
+ return TSI_INTERNAL_ERROR;
+ }
+ *protected_frames_bytes_size = (size_t)written_into_ssl;
/* Now try to read some data again. */
- result = do_ssl_read (impl->ssl, unprotected_bytes, unprotected_bytes_size);
- if (result == TSI_OK)
- {
- /* Don't forget to output the total number of bytes read. */
- *unprotected_bytes_size += output_bytes_offset;
- }
+ result = do_ssl_read(impl->ssl, unprotected_bytes, unprotected_bytes_size);
+ if (result == TSI_OK) {
+ /* Don't forget to output the total number of bytes read. */
+ *unprotected_bytes_size += output_bytes_offset;
+ }
return result;
}
-static void
-ssl_protector_destroy (tsi_frame_protector * self)
-{
- tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *) self;
- if (impl->buffer != NULL)
- free (impl->buffer);
- if (impl->ssl != NULL)
- SSL_free (impl->ssl);
- free (self);
+static void ssl_protector_destroy(tsi_frame_protector *self) {
+ tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *)self;
+ if (impl->buffer != NULL) free(impl->buffer);
+ if (impl->ssl != NULL) SSL_free(impl->ssl);
+ free(self);
}
static const tsi_frame_protector_vtable frame_protector_vtable = {
- ssl_protector_protect, ssl_protector_protect_flush, ssl_protector_unprotect,
- ssl_protector_destroy,
+ ssl_protector_protect, ssl_protector_protect_flush, ssl_protector_unprotect,
+ ssl_protector_destroy,
};
/* --- tsi_handshaker methods implementation. ---*/
-static tsi_result
-ssl_handshaker_get_bytes_to_send_to_peer (tsi_handshaker * self, unsigned char *bytes, size_t * bytes_size)
-{
- tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *) self;
+static tsi_result ssl_handshaker_get_bytes_to_send_to_peer(tsi_handshaker *self,
+ unsigned char *bytes,
+ size_t *bytes_size) {
+ tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self;
int bytes_read_from_ssl = 0;
- if (bytes == NULL || bytes_size == NULL || *bytes_size == 0 || *bytes_size > INT_MAX)
- {
- return TSI_INVALID_ARGUMENT;
- }
- GPR_ASSERT (*bytes_size <= INT_MAX);
- bytes_read_from_ssl = BIO_read (impl->from_ssl, bytes, (int) *bytes_size);
- if (bytes_read_from_ssl < 0)
- {
- *bytes_size = 0;
- if (!BIO_should_retry (impl->from_ssl))
- {
- impl->result = TSI_INTERNAL_ERROR;
- return impl->result;
- }
- else
- {
- return TSI_OK;
- }
+ if (bytes == NULL || bytes_size == NULL || *bytes_size == 0 ||
+ *bytes_size > INT_MAX) {
+ return TSI_INVALID_ARGUMENT;
+ }
+ GPR_ASSERT(*bytes_size <= INT_MAX);
+ bytes_read_from_ssl = BIO_read(impl->from_ssl, bytes, (int)*bytes_size);
+ if (bytes_read_from_ssl < 0) {
+ *bytes_size = 0;
+ if (!BIO_should_retry(impl->from_ssl)) {
+ impl->result = TSI_INTERNAL_ERROR;
+ return impl->result;
+ } else {
+ return TSI_OK;
}
- *bytes_size = (size_t) bytes_read_from_ssl;
- return BIO_pending (impl->from_ssl) == 0 ? TSI_OK : TSI_INCOMPLETE_DATA;
+ }
+ *bytes_size = (size_t)bytes_read_from_ssl;
+ return BIO_pending(impl->from_ssl) == 0 ? TSI_OK : TSI_INCOMPLETE_DATA;
}
-static tsi_result
-ssl_handshaker_get_result (tsi_handshaker * self)
-{
- tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *) self;
- if ((impl->result == TSI_HANDSHAKE_IN_PROGRESS) && SSL_is_init_finished (impl->ssl))
- {
- impl->result = TSI_OK;
- }
+static tsi_result ssl_handshaker_get_result(tsi_handshaker *self) {
+ tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self;
+ if ((impl->result == TSI_HANDSHAKE_IN_PROGRESS) &&
+ SSL_is_init_finished(impl->ssl)) {
+ impl->result = TSI_OK;
+ }
return impl->result;
}
-static tsi_result
-ssl_handshaker_process_bytes_from_peer (tsi_handshaker * self, const unsigned char *bytes, size_t * bytes_size)
-{
- tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *) self;
+static tsi_result ssl_handshaker_process_bytes_from_peer(
+ tsi_handshaker *self, const unsigned char *bytes, size_t *bytes_size) {
+ tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self;
int bytes_written_into_ssl_size = 0;
- if (bytes == NULL || bytes_size == 0 || *bytes_size > INT_MAX)
- {
- return TSI_INVALID_ARGUMENT;
- }
- GPR_ASSERT (*bytes_size <= INT_MAX);
- bytes_written_into_ssl_size = BIO_write (impl->into_ssl, bytes, (int) *bytes_size);
- if (bytes_written_into_ssl_size < 0)
- {
- gpr_log (GPR_ERROR, "Could not write to memory BIO.");
- impl->result = TSI_INTERNAL_ERROR;
- return impl->result;
- }
- *bytes_size = (size_t) bytes_written_into_ssl_size;
-
- if (!tsi_handshaker_is_in_progress (self))
- {
- impl->result = TSI_OK;
- return impl->result;
- }
- else
- {
- /* Get ready to get some bytes from SSL. */
- int ssl_result = SSL_do_handshake (impl->ssl);
- ssl_result = SSL_get_error (impl->ssl, ssl_result);
- switch (ssl_result)
- {
- case SSL_ERROR_WANT_READ:
- if (BIO_pending (impl->from_ssl) == 0)
- {
- /* We need more data. */
- return TSI_INCOMPLETE_DATA;
- }
- else
- {
- return TSI_OK;
- }
- case SSL_ERROR_NONE:
- return TSI_OK;
- default:
- {
- char err_str[256];
- ERR_error_string_n (ERR_get_error (), err_str, sizeof (err_str));
- gpr_log (GPR_ERROR, "Handshake failed with fatal error %s: %s.", ssl_error_string (ssl_result), err_str);
- impl->result = TSI_PROTOCOL_FAILURE;
- return impl->result;
- }
- }
+ if (bytes == NULL || bytes_size == 0 || *bytes_size > INT_MAX) {
+ return TSI_INVALID_ARGUMENT;
+ }
+ GPR_ASSERT(*bytes_size <= INT_MAX);
+ bytes_written_into_ssl_size =
+ BIO_write(impl->into_ssl, bytes, (int)*bytes_size);
+ if (bytes_written_into_ssl_size < 0) {
+ gpr_log(GPR_ERROR, "Could not write to memory BIO.");
+ impl->result = TSI_INTERNAL_ERROR;
+ return impl->result;
+ }
+ *bytes_size = (size_t)bytes_written_into_ssl_size;
+
+ if (!tsi_handshaker_is_in_progress(self)) {
+ impl->result = TSI_OK;
+ return impl->result;
+ } else {
+ /* Get ready to get some bytes from SSL. */
+ int ssl_result = SSL_do_handshake(impl->ssl);
+ ssl_result = SSL_get_error(impl->ssl, ssl_result);
+ switch (ssl_result) {
+ case SSL_ERROR_WANT_READ:
+ if (BIO_pending(impl->from_ssl) == 0) {
+ /* We need more data. */
+ return TSI_INCOMPLETE_DATA;
+ } else {
+ return TSI_OK;
+ }
+ case SSL_ERROR_NONE:
+ return TSI_OK;
+ default: {
+ char err_str[256];
+ ERR_error_string_n(ERR_get_error(), err_str, sizeof(err_str));
+ gpr_log(GPR_ERROR, "Handshake failed with fatal error %s: %s.",
+ ssl_error_string(ssl_result), err_str);
+ impl->result = TSI_PROTOCOL_FAILURE;
+ return impl->result;
+ }
}
+ }
}
-static tsi_result
-ssl_handshaker_extract_peer (tsi_handshaker * self, tsi_peer * peer)
-{
+static tsi_result ssl_handshaker_extract_peer(tsi_handshaker *self,
+ tsi_peer *peer) {
tsi_result result = TSI_OK;
const unsigned char *alpn_selected = NULL;
unsigned int alpn_selected_len;
- tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *) self;
- X509 *peer_cert = SSL_get_peer_certificate (impl->ssl);
- if (peer_cert != NULL)
- {
- result = peer_from_x509 (peer_cert, 1, peer);
- X509_free (peer_cert);
- if (result != TSI_OK)
- return result;
- }
+ tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self;
+ X509 *peer_cert = SSL_get_peer_certificate(impl->ssl);
+ if (peer_cert != NULL) {
+ result = peer_from_x509(peer_cert, 1, peer);
+ X509_free(peer_cert);
+ if (result != TSI_OK) return result;
+ }
#if TSI_OPENSSL_ALPN_SUPPORT
- SSL_get0_alpn_selected (impl->ssl, &alpn_selected, &alpn_selected_len);
+ SSL_get0_alpn_selected(impl->ssl, &alpn_selected, &alpn_selected_len);
#endif /* TSI_OPENSSL_ALPN_SUPPORT */
- if (alpn_selected == NULL)
- {
- /* Try npn. */
- SSL_get0_next_proto_negotiated (impl->ssl, &alpn_selected, &alpn_selected_len);
- }
- if (alpn_selected != NULL)
- {
- size_t i;
- tsi_peer_property *new_properties = calloc (1, sizeof (tsi_peer_property) * (peer->property_count + 1));
- if (new_properties == NULL)
- return TSI_OUT_OF_RESOURCES;
- for (i = 0; i < peer->property_count; i++)
- {
- new_properties[i] = peer->properties[i];
- }
- result = tsi_construct_string_peer_property (TSI_SSL_ALPN_SELECTED_PROTOCOL, (const char *) alpn_selected, alpn_selected_len, &new_properties[peer->property_count]);
- if (result != TSI_OK)
- {
- free (new_properties);
- return result;
- }
- if (peer->properties != NULL)
- free (peer->properties);
- peer->property_count++;
- peer->properties = new_properties;
+ if (alpn_selected == NULL) {
+ /* Try npn. */
+ SSL_get0_next_proto_negotiated(impl->ssl, &alpn_selected,
+ &alpn_selected_len);
+ }
+ if (alpn_selected != NULL) {
+ size_t i;
+ tsi_peer_property *new_properties =
+ calloc(1, sizeof(tsi_peer_property) * (peer->property_count + 1));
+ if (new_properties == NULL) return TSI_OUT_OF_RESOURCES;
+ for (i = 0; i < peer->property_count; i++) {
+ new_properties[i] = peer->properties[i];
+ }
+ result = tsi_construct_string_peer_property(
+ TSI_SSL_ALPN_SELECTED_PROTOCOL, (const char *)alpn_selected,
+ alpn_selected_len, &new_properties[peer->property_count]);
+ if (result != TSI_OK) {
+ free(new_properties);
+ return result;
}
+ if (peer->properties != NULL) free(peer->properties);
+ peer->property_count++;
+ peer->properties = new_properties;
+ }
return result;
}
-static tsi_result
-ssl_handshaker_create_frame_protector (tsi_handshaker * self, size_t * max_output_protected_frame_size, tsi_frame_protector ** protector)
-{
- size_t actual_max_output_protected_frame_size = TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND;
- tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *) self;
- tsi_ssl_frame_protector *protector_impl = calloc (1, sizeof (tsi_ssl_frame_protector));
- if (protector_impl == NULL)
- {
- return TSI_OUT_OF_RESOURCES;
- }
+static tsi_result ssl_handshaker_create_frame_protector(
+ tsi_handshaker *self, size_t *max_output_protected_frame_size,
+ tsi_frame_protector **protector) {
+ size_t actual_max_output_protected_frame_size =
+ TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND;
+ tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self;
+ tsi_ssl_frame_protector *protector_impl =
+ calloc(1, sizeof(tsi_ssl_frame_protector));
+ if (protector_impl == NULL) {
+ return TSI_OUT_OF_RESOURCES;
+ }
- if (max_output_protected_frame_size != NULL)
- {
- if (*max_output_protected_frame_size > TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND)
- {
- *max_output_protected_frame_size = TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND;
- }
- else if (*max_output_protected_frame_size < TSI_SSL_MAX_PROTECTED_FRAME_SIZE_LOWER_BOUND)
- {
- *max_output_protected_frame_size = TSI_SSL_MAX_PROTECTED_FRAME_SIZE_LOWER_BOUND;
- }
- actual_max_output_protected_frame_size = *max_output_protected_frame_size;
- }
- protector_impl->buffer_size = actual_max_output_protected_frame_size - TSI_SSL_MAX_PROTECTION_OVERHEAD;
- protector_impl->buffer = malloc (protector_impl->buffer_size);
- if (protector_impl->buffer == NULL)
- {
- gpr_log (GPR_ERROR, "Could not allocated buffer for tsi_ssl_frame_protector.");
- free (protector_impl);
- return TSI_INTERNAL_ERROR;
- }
+ if (max_output_protected_frame_size != NULL) {
+ if (*max_output_protected_frame_size >
+ TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND) {
+ *max_output_protected_frame_size =
+ TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND;
+ } else if (*max_output_protected_frame_size <
+ TSI_SSL_MAX_PROTECTED_FRAME_SIZE_LOWER_BOUND) {
+ *max_output_protected_frame_size =
+ TSI_SSL_MAX_PROTECTED_FRAME_SIZE_LOWER_BOUND;
+ }
+ actual_max_output_protected_frame_size = *max_output_protected_frame_size;
+ }
+ protector_impl->buffer_size =
+ actual_max_output_protected_frame_size - TSI_SSL_MAX_PROTECTION_OVERHEAD;
+ protector_impl->buffer = malloc(protector_impl->buffer_size);
+ if (protector_impl->buffer == NULL) {
+ gpr_log(GPR_ERROR,
+ "Could not allocated buffer for tsi_ssl_frame_protector.");
+ free(protector_impl);
+ return TSI_INTERNAL_ERROR;
+ }
/* Transfer ownership of ssl to the frame protector. It is OK as the caller
* cannot call anything else but destroy on the handshaker after this call. */
@@ -1078,106 +968,91 @@ ssl_handshaker_create_frame_protector (tsi_handshaker * self, size_t * max_outpu
return TSI_OK;
}
-static void
-ssl_handshaker_destroy (tsi_handshaker * self)
-{
- tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *) self;
- SSL_free (impl->ssl); /* The BIO objects are owned by ssl */
- free (impl);
+static void ssl_handshaker_destroy(tsi_handshaker *self) {
+ tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self;
+ SSL_free(impl->ssl); /* The BIO objects are owned by ssl */
+ free(impl);
}
static const tsi_handshaker_vtable handshaker_vtable = {
- ssl_handshaker_get_bytes_to_send_to_peer,
- ssl_handshaker_process_bytes_from_peer,
- ssl_handshaker_get_result,
- ssl_handshaker_extract_peer,
- ssl_handshaker_create_frame_protector,
- ssl_handshaker_destroy,
+ ssl_handshaker_get_bytes_to_send_to_peer,
+ ssl_handshaker_process_bytes_from_peer,
+ ssl_handshaker_get_result,
+ ssl_handshaker_extract_peer,
+ ssl_handshaker_create_frame_protector,
+ ssl_handshaker_destroy,
};
/* --- tsi_ssl_handshaker_factory common methods. --- */
-tsi_result
-tsi_ssl_handshaker_factory_create_handshaker (tsi_ssl_handshaker_factory * self, const char *server_name_indication, tsi_handshaker ** handshaker)
-{
- if (self == NULL || handshaker == NULL)
- return TSI_INVALID_ARGUMENT;
- return self->create_handshaker (self, server_name_indication, handshaker);
+tsi_result tsi_ssl_handshaker_factory_create_handshaker(
+ tsi_ssl_handshaker_factory *self, const char *server_name_indication,
+ tsi_handshaker **handshaker) {
+ if (self == NULL || handshaker == NULL) return TSI_INVALID_ARGUMENT;
+ return self->create_handshaker(self, server_name_indication, handshaker);
}
-void
-tsi_ssl_handshaker_factory_destroy (tsi_ssl_handshaker_factory * self)
-{
- if (self == NULL)
- return;
- self->destroy (self);
+void tsi_ssl_handshaker_factory_destroy(tsi_ssl_handshaker_factory *self) {
+ if (self == NULL) return;
+ self->destroy(self);
}
-static tsi_result
-create_tsi_ssl_handshaker (SSL_CTX * ctx, int is_client, const char *server_name_indication, tsi_handshaker ** handshaker)
-{
- SSL *ssl = SSL_new (ctx);
+static tsi_result create_tsi_ssl_handshaker(SSL_CTX *ctx, int is_client,
+ const char *server_name_indication,
+ tsi_handshaker **handshaker) {
+ SSL *ssl = SSL_new(ctx);
BIO *into_ssl = NULL;
BIO *from_ssl = NULL;
tsi_ssl_handshaker *impl = NULL;
*handshaker = NULL;
- if (ctx == NULL)
- {
- gpr_log (GPR_ERROR, "SSL Context is null. Should never happen.");
- return TSI_INTERNAL_ERROR;
- }
- if (ssl == NULL)
- {
- return TSI_OUT_OF_RESOURCES;
- }
- SSL_set_info_callback (ssl, ssl_info_callback);
-
- into_ssl = BIO_new (BIO_s_mem ());
- from_ssl = BIO_new (BIO_s_mem ());
- if (into_ssl == NULL || from_ssl == NULL)
- {
- gpr_log (GPR_ERROR, "BIO_new failed.");
- SSL_free (ssl);
- if (into_ssl != NULL)
- BIO_free (into_ssl);
- if (from_ssl != NULL)
- BIO_free (into_ssl);
- return TSI_OUT_OF_RESOURCES;
- }
- SSL_set_bio (ssl, into_ssl, from_ssl);
- if (is_client)
- {
- int ssl_result;
- SSL_set_connect_state (ssl);
- if (server_name_indication != NULL)
- {
- if (!SSL_set_tlsext_host_name (ssl, server_name_indication))
- {
- gpr_log (GPR_ERROR, "Invalid server name indication %s.", server_name_indication);
- SSL_free (ssl);
- return TSI_INTERNAL_ERROR;
- }
- }
- ssl_result = SSL_do_handshake (ssl);
- ssl_result = SSL_get_error (ssl, ssl_result);
- if (ssl_result != SSL_ERROR_WANT_READ)
- {
- gpr_log (GPR_ERROR, "Unexpected error received from first SSL_do_handshake call: %s", ssl_error_string (ssl_result));
- SSL_free (ssl);
- return TSI_INTERNAL_ERROR;
- }
+ if (ctx == NULL) {
+ gpr_log(GPR_ERROR, "SSL Context is null. Should never happen.");
+ return TSI_INTERNAL_ERROR;
+ }
+ if (ssl == NULL) {
+ return TSI_OUT_OF_RESOURCES;
+ }
+ SSL_set_info_callback(ssl, ssl_info_callback);
+
+ into_ssl = BIO_new(BIO_s_mem());
+ from_ssl = BIO_new(BIO_s_mem());
+ if (into_ssl == NULL || from_ssl == NULL) {
+ gpr_log(GPR_ERROR, "BIO_new failed.");
+ SSL_free(ssl);
+ if (into_ssl != NULL) BIO_free(into_ssl);
+ if (from_ssl != NULL) BIO_free(into_ssl);
+ return TSI_OUT_OF_RESOURCES;
+ }
+ SSL_set_bio(ssl, into_ssl, from_ssl);
+ if (is_client) {
+ int ssl_result;
+ SSL_set_connect_state(ssl);
+ if (server_name_indication != NULL) {
+ if (!SSL_set_tlsext_host_name(ssl, server_name_indication)) {
+ gpr_log(GPR_ERROR, "Invalid server name indication %s.",
+ server_name_indication);
+ SSL_free(ssl);
+ return TSI_INTERNAL_ERROR;
+ }
}
- else
- {
- SSL_set_accept_state (ssl);
+ ssl_result = SSL_do_handshake(ssl);
+ ssl_result = SSL_get_error(ssl, ssl_result);
+ if (ssl_result != SSL_ERROR_WANT_READ) {
+ gpr_log(GPR_ERROR,
+ "Unexpected error received from first SSL_do_handshake call: %s",
+ ssl_error_string(ssl_result));
+ SSL_free(ssl);
+ return TSI_INTERNAL_ERROR;
}
+ } else {
+ SSL_set_accept_state(ssl);
+ }
- impl = calloc (1, sizeof (tsi_ssl_handshaker));
- if (impl == NULL)
- {
- SSL_free (ssl);
- return TSI_OUT_OF_RESOURCES;
- }
+ impl = calloc(1, sizeof(tsi_ssl_handshaker));
+ if (impl == NULL) {
+ SSL_free(ssl);
+ return TSI_OUT_OF_RESOURCES;
+ }
impl->ssl = ssl;
impl->into_ssl = into_ssl;
impl->from_ssl = from_ssl;
@@ -1187,412 +1062,407 @@ create_tsi_ssl_handshaker (SSL_CTX * ctx, int is_client, const char *server_name
return TSI_OK;
}
-static int
-select_protocol_list (const unsigned char **out, unsigned char *outlen, const unsigned char *client_list, size_t client_list_len, const unsigned char *server_list, size_t server_list_len)
-{
+static int select_protocol_list(const unsigned char **out,
+ unsigned char *outlen,
+ const unsigned char *client_list,
+ size_t client_list_len,
+ const unsigned char *server_list,
+ size_t server_list_len) {
const unsigned char *client_current = client_list;
- while ((unsigned int) (client_current - client_list) < client_list_len)
- {
- unsigned char client_current_len = *(client_current++);
- const unsigned char *server_current = server_list;
- while ((server_current >= server_list) && (gpr_uintptr) (server_current - server_list) < server_list_len)
- {
- unsigned char server_current_len = *(server_current++);
- if ((client_current_len == server_current_len) && !memcmp (client_current, server_current, server_current_len))
- {
- *out = server_current;
- *outlen = server_current_len;
- return SSL_TLSEXT_ERR_OK;
- }
- server_current += server_current_len;
- }
- client_current += client_current_len;
+ while ((unsigned int)(client_current - client_list) < client_list_len) {
+ unsigned char client_current_len = *(client_current++);
+ const unsigned char *server_current = server_list;
+ while ((server_current >= server_list) &&
+ (gpr_uintptr)(server_current - server_list) < server_list_len) {
+ unsigned char server_current_len = *(server_current++);
+ if ((client_current_len == server_current_len) &&
+ !memcmp(client_current, server_current, server_current_len)) {
+ *out = server_current;
+ *outlen = server_current_len;
+ return SSL_TLSEXT_ERR_OK;
+ }
+ server_current += server_current_len;
}
+ client_current += client_current_len;
+ }
return SSL_TLSEXT_ERR_NOACK;
}
/* --- tsi_ssl__client_handshaker_factory methods implementation. --- */
-static tsi_result
-ssl_client_handshaker_factory_create_handshaker (tsi_ssl_handshaker_factory * self, const char *server_name_indication, tsi_handshaker ** handshaker)
-{
- tsi_ssl_client_handshaker_factory *impl = (tsi_ssl_client_handshaker_factory *) self;
- return create_tsi_ssl_handshaker (impl->ssl_context, 1, server_name_indication, handshaker);
+static tsi_result ssl_client_handshaker_factory_create_handshaker(
+ tsi_ssl_handshaker_factory *self, const char *server_name_indication,
+ tsi_handshaker **handshaker) {
+ tsi_ssl_client_handshaker_factory *impl =
+ (tsi_ssl_client_handshaker_factory *)self;
+ return create_tsi_ssl_handshaker(impl->ssl_context, 1, server_name_indication,
+ handshaker);
}
-static void
-ssl_client_handshaker_factory_destroy (tsi_ssl_handshaker_factory * self)
-{
- tsi_ssl_client_handshaker_factory *impl = (tsi_ssl_client_handshaker_factory *) self;
- if (impl->ssl_context != NULL)
- SSL_CTX_free (impl->ssl_context);
- if (impl->alpn_protocol_list != NULL)
- free (impl->alpn_protocol_list);
- free (impl);
+static void ssl_client_handshaker_factory_destroy(
+ tsi_ssl_handshaker_factory *self) {
+ tsi_ssl_client_handshaker_factory *impl =
+ (tsi_ssl_client_handshaker_factory *)self;
+ if (impl->ssl_context != NULL) SSL_CTX_free(impl->ssl_context);
+ if (impl->alpn_protocol_list != NULL) free(impl->alpn_protocol_list);
+ free(impl);
}
-static int
-client_handshaker_factory_npn_callback (SSL * ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
-{
- tsi_ssl_client_handshaker_factory *factory = (tsi_ssl_client_handshaker_factory *) arg;
- return select_protocol_list ((const unsigned char **) out, outlen, factory->alpn_protocol_list, factory->alpn_protocol_list_length, in, inlen);
+static int client_handshaker_factory_npn_callback(SSL *ssl, unsigned char **out,
+ unsigned char *outlen,
+ const unsigned char *in,
+ unsigned int inlen,
+ void *arg) {
+ tsi_ssl_client_handshaker_factory *factory =
+ (tsi_ssl_client_handshaker_factory *)arg;
+ return select_protocol_list((const unsigned char **)out, outlen,
+ factory->alpn_protocol_list,
+ factory->alpn_protocol_list_length, in, inlen);
}
/* --- tsi_ssl_server_handshaker_factory methods implementation. --- */
-static tsi_result
-ssl_server_handshaker_factory_create_handshaker (tsi_ssl_handshaker_factory * self, const char *server_name_indication, tsi_handshaker ** handshaker)
-{
- tsi_ssl_server_handshaker_factory *impl = (tsi_ssl_server_handshaker_factory *) self;
- if (impl->ssl_context_count == 0 || server_name_indication != NULL)
- {
- return TSI_INVALID_ARGUMENT;
- }
+static tsi_result ssl_server_handshaker_factory_create_handshaker(
+ tsi_ssl_handshaker_factory *self, const char *server_name_indication,
+ tsi_handshaker **handshaker) {
+ tsi_ssl_server_handshaker_factory *impl =
+ (tsi_ssl_server_handshaker_factory *)self;
+ if (impl->ssl_context_count == 0 || server_name_indication != NULL) {
+ return TSI_INVALID_ARGUMENT;
+ }
/* Create the handshaker with the first context. We will switch if needed
because of SNI in ssl_server_handshaker_factory_servername_callback. */
- return create_tsi_ssl_handshaker (impl->ssl_contexts[0], 0, NULL, handshaker);
+ return create_tsi_ssl_handshaker(impl->ssl_contexts[0], 0, NULL, handshaker);
}
-static void
-ssl_server_handshaker_factory_destroy (tsi_ssl_handshaker_factory * self)
-{
- tsi_ssl_server_handshaker_factory *impl = (tsi_ssl_server_handshaker_factory *) self;
+static void ssl_server_handshaker_factory_destroy(
+ tsi_ssl_handshaker_factory *self) {
+ tsi_ssl_server_handshaker_factory *impl =
+ (tsi_ssl_server_handshaker_factory *)self;
size_t i;
- for (i = 0; i < impl->ssl_context_count; i++)
- {
- if (impl->ssl_contexts[i] != NULL)
- {
- SSL_CTX_free (impl->ssl_contexts[i]);
- tsi_peer_destruct (&impl->ssl_context_x509_subject_names[i]);
- }
+ for (i = 0; i < impl->ssl_context_count; i++) {
+ if (impl->ssl_contexts[i] != NULL) {
+ SSL_CTX_free(impl->ssl_contexts[i]);
+ tsi_peer_destruct(&impl->ssl_context_x509_subject_names[i]);
}
- if (impl->ssl_contexts != NULL)
- free (impl->ssl_contexts);
- if (impl->ssl_context_x509_subject_names != NULL)
- {
- free (impl->ssl_context_x509_subject_names);
- }
- if (impl->alpn_protocol_list != NULL)
- free (impl->alpn_protocol_list);
- free (impl);
+ }
+ if (impl->ssl_contexts != NULL) free(impl->ssl_contexts);
+ if (impl->ssl_context_x509_subject_names != NULL) {
+ free(impl->ssl_context_x509_subject_names);
+ }
+ if (impl->alpn_protocol_list != NULL) free(impl->alpn_protocol_list);
+ free(impl);
}
-static int
-does_entry_match_name (const char *entry, size_t entry_length, const char *name)
-{
+static int does_entry_match_name(const char *entry, size_t entry_length,
+ const char *name) {
const char *dot;
const char *name_subdomain = NULL;
- size_t name_length = strlen (name);
+ size_t name_length = strlen(name);
size_t name_subdomain_length;
- if (entry_length == 0)
- return 0;
+ if (entry_length == 0) return 0;
/* Take care of '.' terminations. */
- if (name[name_length - 1] == '.')
- {
- name_length--;
- }
- if (entry[entry_length - 1] == '.')
- {
- entry_length--;
- if (entry_length == 0)
- return 0;
- }
+ if (name[name_length - 1] == '.') {
+ name_length--;
+ }
+ if (entry[entry_length - 1] == '.') {
+ entry_length--;
+ if (entry_length == 0) return 0;
+ }
- if ((name_length == entry_length) && strncmp (name, entry, entry_length) == 0)
- {
- return 1; /* Perfect match. */
- }
- if (entry[0] != '*')
- return 0;
+ if ((name_length == entry_length) &&
+ strncmp(name, entry, entry_length) == 0) {
+ return 1; /* Perfect match. */
+ }
+ if (entry[0] != '*') return 0;
/* Wildchar subdomain matching. */
- if (entry_length < 3 || entry[1] != '.')
- { /* At least *.x */
- gpr_log (GPR_ERROR, "Invalid wildchar entry.");
- return 0;
- }
- name_subdomain = strchr (name, '.');
- if (name_subdomain == NULL)
- return 0;
- name_subdomain_length = strlen (name_subdomain);
- if (name_subdomain_length < 2)
+ if (entry_length < 3 || entry[1] != '.') { /* At least *.x */
+ gpr_log(GPR_ERROR, "Invalid wildchar entry.");
return 0;
- name_subdomain++; /* Starts after the dot. */
+ }
+ name_subdomain = strchr(name, '.');
+ if (name_subdomain == NULL) return 0;
+ name_subdomain_length = strlen(name_subdomain);
+ if (name_subdomain_length < 2) return 0;
+ name_subdomain++; /* Starts after the dot. */
name_subdomain_length--;
- entry += 2; /* Remove *. */
+ entry += 2; /* Remove *. */
entry_length -= 2;
- dot = strchr (name_subdomain, '.');
- if ((dot == NULL) || (dot == &name_subdomain[name_subdomain_length - 1]))
- {
- gpr_log (GPR_ERROR, "Invalid toplevel subdomain: %s", name_subdomain);
- return 0;
- }
- if (name_subdomain[name_subdomain_length - 1] == '.')
- {
- name_subdomain_length--;
- }
- return ((entry_length > 0) && (name_subdomain_length == entry_length) && strncmp (entry, name_subdomain, entry_length) == 0);
+ dot = strchr(name_subdomain, '.');
+ if ((dot == NULL) || (dot == &name_subdomain[name_subdomain_length - 1])) {
+ gpr_log(GPR_ERROR, "Invalid toplevel subdomain: %s", name_subdomain);
+ return 0;
+ }
+ if (name_subdomain[name_subdomain_length - 1] == '.') {
+ name_subdomain_length--;
+ }
+ return ((entry_length > 0) && (name_subdomain_length == entry_length) &&
+ strncmp(entry, name_subdomain, entry_length) == 0);
}
-static int
-ssl_server_handshaker_factory_servername_callback (SSL * ssl, int *ap, void *arg)
-{
- tsi_ssl_server_handshaker_factory *impl = (tsi_ssl_server_handshaker_factory *) arg;
+static int ssl_server_handshaker_factory_servername_callback(SSL *ssl, int *ap,
+ void *arg) {
+ tsi_ssl_server_handshaker_factory *impl =
+ (tsi_ssl_server_handshaker_factory *)arg;
size_t i = 0;
- const char *servername = SSL_get_servername (ssl, TLSEXT_NAMETYPE_host_name);
- if (servername == NULL || strlen (servername) == 0)
- {
- return SSL_TLSEXT_ERR_NOACK;
- }
+ const char *servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
+ if (servername == NULL || strlen(servername) == 0) {
+ return SSL_TLSEXT_ERR_NOACK;
+ }
- for (i = 0; i < impl->ssl_context_count; i++)
- {
- if (tsi_ssl_peer_matches_name (&impl->ssl_context_x509_subject_names[i], servername))
- {
- SSL_set_SSL_CTX (ssl, impl->ssl_contexts[i]);
- return SSL_TLSEXT_ERR_OK;
- }
+ for (i = 0; i < impl->ssl_context_count; i++) {
+ if (tsi_ssl_peer_matches_name(&impl->ssl_context_x509_subject_names[i],
+ servername)) {
+ SSL_set_SSL_CTX(ssl, impl->ssl_contexts[i]);
+ return SSL_TLSEXT_ERR_OK;
}
- gpr_log (GPR_ERROR, "No match found for server name: %s.", servername);
+ }
+ gpr_log(GPR_ERROR, "No match found for server name: %s.", servername);
return SSL_TLSEXT_ERR_ALERT_WARNING;
}
#if TSI_OPENSSL_ALPN_SUPPORT
-static int
-server_handshaker_factory_alpn_callback (SSL * ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
-{
- tsi_ssl_server_handshaker_factory *factory = (tsi_ssl_server_handshaker_factory *) arg;
- return select_protocol_list (out, outlen, in, inlen, factory->alpn_protocol_list, factory->alpn_protocol_list_length);
+static int server_handshaker_factory_alpn_callback(
+ SSL *ssl, const unsigned char **out, unsigned char *outlen,
+ const unsigned char *in, unsigned int inlen, void *arg) {
+ tsi_ssl_server_handshaker_factory *factory =
+ (tsi_ssl_server_handshaker_factory *)arg;
+ return select_protocol_list(out, outlen, in, inlen,
+ factory->alpn_protocol_list,
+ factory->alpn_protocol_list_length);
}
#endif /* TSI_OPENSSL_ALPN_SUPPORT */
-static int
-server_handshaker_factory_npn_advertised_callback (SSL * ssl, const unsigned char **out, unsigned int *outlen, void *arg)
-{
- tsi_ssl_server_handshaker_factory *factory = (tsi_ssl_server_handshaker_factory *) arg;
+static int server_handshaker_factory_npn_advertised_callback(
+ SSL *ssl, const unsigned char **out, unsigned int *outlen, void *arg) {
+ tsi_ssl_server_handshaker_factory *factory =
+ (tsi_ssl_server_handshaker_factory *)arg;
*out = factory->alpn_protocol_list;
- GPR_ASSERT (factory->alpn_protocol_list_length <= UINT_MAX);
- *outlen = (unsigned int) factory->alpn_protocol_list_length;
+ GPR_ASSERT(factory->alpn_protocol_list_length <= UINT_MAX);
+ *outlen = (unsigned int)factory->alpn_protocol_list_length;
return SSL_TLSEXT_ERR_OK;
}
/* --- tsi_ssl_handshaker_factory constructors. --- */
-tsi_result
-tsi_create_ssl_client_handshaker_factory (const unsigned char *pem_private_key, size_t pem_private_key_size, const unsigned char *pem_cert_chain, size_t pem_cert_chain_size, const unsigned char *pem_root_certs, size_t pem_root_certs_size, const char *cipher_list, const unsigned char **alpn_protocols, const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols, tsi_ssl_handshaker_factory ** factory)
-{
+tsi_result tsi_create_ssl_client_handshaker_factory(
+ const unsigned char *pem_private_key, size_t pem_private_key_size,
+ const unsigned char *pem_cert_chain, size_t pem_cert_chain_size,
+ const unsigned char *pem_root_certs, size_t pem_root_certs_size,
+ const char *cipher_list, const unsigned char **alpn_protocols,
+ const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols,
+ tsi_ssl_handshaker_factory **factory) {
SSL_CTX *ssl_context = NULL;
tsi_ssl_client_handshaker_factory *impl = NULL;
tsi_result result = TSI_OK;
- gpr_once_init (&init_openssl_once, init_openssl);
+ gpr_once_init(&init_openssl_once, init_openssl);
- if (factory == NULL)
- return TSI_INVALID_ARGUMENT;
+ if (factory == NULL) return TSI_INVALID_ARGUMENT;
*factory = NULL;
- if (pem_root_certs == NULL)
- return TSI_INVALID_ARGUMENT;
+ if (pem_root_certs == NULL) return TSI_INVALID_ARGUMENT;
- ssl_context = SSL_CTX_new (TLSv1_2_method ());
- if (ssl_context == NULL)
- {
- gpr_log (GPR_ERROR, "Could not create ssl context.");
- return TSI_INVALID_ARGUMENT;
- }
+ ssl_context = SSL_CTX_new(TLSv1_2_method());
+ if (ssl_context == NULL) {
+ gpr_log(GPR_ERROR, "Could not create ssl context.");
+ return TSI_INVALID_ARGUMENT;
+ }
- impl = calloc (1, sizeof (tsi_ssl_client_handshaker_factory));
- if (impl == NULL)
- {
- SSL_CTX_free (ssl_context);
- return TSI_OUT_OF_RESOURCES;
- }
+ impl = calloc(1, sizeof(tsi_ssl_client_handshaker_factory));
+ if (impl == NULL) {
+ SSL_CTX_free(ssl_context);
+ return TSI_OUT_OF_RESOURCES;
+ }
impl->ssl_context = ssl_context;
- do
- {
- result = populate_ssl_context (ssl_context, pem_private_key, pem_private_key_size, pem_cert_chain, pem_cert_chain_size, cipher_list);
- if (result != TSI_OK)
- break;
- result = ssl_ctx_load_verification_certs (ssl_context, pem_root_certs, pem_root_certs_size, NULL);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Cannot load server root certificates.");
- break;
- }
-
- if (num_alpn_protocols != 0)
- {
- result = build_alpn_protocol_name_list (alpn_protocols, alpn_protocols_lengths, num_alpn_protocols, &impl->alpn_protocol_list, &impl->alpn_protocol_list_length);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Building alpn list failed with error %s.", tsi_result_to_string (result));
- break;
- }
+ do {
+ result =
+ populate_ssl_context(ssl_context, pem_private_key, pem_private_key_size,
+ pem_cert_chain, pem_cert_chain_size, cipher_list);
+ if (result != TSI_OK) break;
+ result = ssl_ctx_load_verification_certs(ssl_context, pem_root_certs,
+ pem_root_certs_size, NULL);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Cannot load server root certificates.");
+ break;
+ }
+
+ if (num_alpn_protocols != 0) {
+ result = build_alpn_protocol_name_list(
+ alpn_protocols, alpn_protocols_lengths, num_alpn_protocols,
+ &impl->alpn_protocol_list, &impl->alpn_protocol_list_length);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Building alpn list failed with error %s.",
+ tsi_result_to_string(result));
+ break;
+ }
#if TSI_OPENSSL_ALPN_SUPPORT
- GPR_ASSERT (impl->alpn_protocol_list_length < UINT_MAX);
- if (SSL_CTX_set_alpn_protos (ssl_context, impl->alpn_protocol_list, (unsigned int) impl->alpn_protocol_list_length))
- {
- gpr_log (GPR_ERROR, "Could not set alpn protocol list to context.");
- result = TSI_INVALID_ARGUMENT;
- break;
- }
+ GPR_ASSERT(impl->alpn_protocol_list_length < UINT_MAX);
+ if (SSL_CTX_set_alpn_protos(
+ ssl_context, impl->alpn_protocol_list,
+ (unsigned int)impl->alpn_protocol_list_length)) {
+ gpr_log(GPR_ERROR, "Could not set alpn protocol list to context.");
+ result = TSI_INVALID_ARGUMENT;
+ break;
+ }
#endif /* TSI_OPENSSL_ALPN_SUPPORT */
- SSL_CTX_set_next_proto_select_cb (ssl_context, client_handshaker_factory_npn_callback, impl);
- }
+ SSL_CTX_set_next_proto_select_cb(
+ ssl_context, client_handshaker_factory_npn_callback, impl);
}
- while (0);
- if (result != TSI_OK)
- {
- ssl_client_handshaker_factory_destroy (&impl->base);
- return result;
- }
- SSL_CTX_set_verify (ssl_context, SSL_VERIFY_PEER, NULL);
+ } while (0);
+ if (result != TSI_OK) {
+ ssl_client_handshaker_factory_destroy(&impl->base);
+ return result;
+ }
+ SSL_CTX_set_verify(ssl_context, SSL_VERIFY_PEER, NULL);
/* TODO(jboeuf): Add revocation verification. */
- impl->base.create_handshaker = ssl_client_handshaker_factory_create_handshaker;
+ impl->base.create_handshaker =
+ ssl_client_handshaker_factory_create_handshaker;
impl->base.destroy = ssl_client_handshaker_factory_destroy;
*factory = &impl->base;
return TSI_OK;
}
-tsi_result
-tsi_create_ssl_server_handshaker_factory (const unsigned char **pem_private_keys, const size_t * pem_private_keys_sizes, const unsigned char **pem_cert_chains, const size_t * pem_cert_chains_sizes, size_t key_cert_pair_count, const unsigned char *pem_client_root_certs, size_t pem_client_root_certs_size, int force_client_auth, const char *cipher_list, const unsigned char **alpn_protocols, const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols, tsi_ssl_handshaker_factory ** factory)
-{
+tsi_result tsi_create_ssl_server_handshaker_factory(
+ const unsigned char **pem_private_keys,
+ const size_t *pem_private_keys_sizes, const unsigned char **pem_cert_chains,
+ const size_t *pem_cert_chains_sizes, size_t key_cert_pair_count,
+ const unsigned char *pem_client_root_certs,
+ size_t pem_client_root_certs_size, int force_client_auth,
+ const char *cipher_list, const unsigned char **alpn_protocols,
+ const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols,
+ tsi_ssl_handshaker_factory **factory) {
tsi_ssl_server_handshaker_factory *impl = NULL;
tsi_result result = TSI_OK;
size_t i = 0;
- gpr_once_init (&init_openssl_once, init_openssl);
+ gpr_once_init(&init_openssl_once, init_openssl);
- if (factory == NULL)
- return TSI_INVALID_ARGUMENT;
+ if (factory == NULL) return TSI_INVALID_ARGUMENT;
*factory = NULL;
- if (key_cert_pair_count == 0 || pem_private_keys == NULL || pem_cert_chains == NULL)
- {
- return TSI_INVALID_ARGUMENT;
- }
+ if (key_cert_pair_count == 0 || pem_private_keys == NULL ||
+ pem_cert_chains == NULL) {
+ return TSI_INVALID_ARGUMENT;
+ }
- impl = calloc (1, sizeof (tsi_ssl_server_handshaker_factory));
- if (impl == NULL)
- return TSI_OUT_OF_RESOURCES;
- impl->base.create_handshaker = ssl_server_handshaker_factory_create_handshaker;
+ impl = calloc(1, sizeof(tsi_ssl_server_handshaker_factory));
+ if (impl == NULL) return TSI_OUT_OF_RESOURCES;
+ impl->base.create_handshaker =
+ ssl_server_handshaker_factory_create_handshaker;
impl->base.destroy = ssl_server_handshaker_factory_destroy;
- impl->ssl_contexts = calloc (key_cert_pair_count, sizeof (SSL_CTX *));
- impl->ssl_context_x509_subject_names = calloc (key_cert_pair_count, sizeof (tsi_peer));
- if (impl->ssl_contexts == NULL || impl->ssl_context_x509_subject_names == NULL)
- {
- tsi_ssl_handshaker_factory_destroy (&impl->base);
- return TSI_OUT_OF_RESOURCES;
- }
+ impl->ssl_contexts = calloc(key_cert_pair_count, sizeof(SSL_CTX *));
+ impl->ssl_context_x509_subject_names =
+ calloc(key_cert_pair_count, sizeof(tsi_peer));
+ if (impl->ssl_contexts == NULL ||
+ impl->ssl_context_x509_subject_names == NULL) {
+ tsi_ssl_handshaker_factory_destroy(&impl->base);
+ return TSI_OUT_OF_RESOURCES;
+ }
impl->ssl_context_count = key_cert_pair_count;
- if (num_alpn_protocols > 0)
- {
- result = build_alpn_protocol_name_list (alpn_protocols, alpn_protocols_lengths, num_alpn_protocols, &impl->alpn_protocol_list, &impl->alpn_protocol_list_length);
- if (result != TSI_OK)
- {
- tsi_ssl_handshaker_factory_destroy (&impl->base);
- return result;
- }
+ if (num_alpn_protocols > 0) {
+ result = build_alpn_protocol_name_list(
+ alpn_protocols, alpn_protocols_lengths, num_alpn_protocols,
+ &impl->alpn_protocol_list, &impl->alpn_protocol_list_length);
+ if (result != TSI_OK) {
+ tsi_ssl_handshaker_factory_destroy(&impl->base);
+ return result;
}
+ }
+
+ for (i = 0; i < key_cert_pair_count; i++) {
+ do {
+ impl->ssl_contexts[i] = SSL_CTX_new(TLSv1_2_method());
+ if (impl->ssl_contexts[i] == NULL) {
+ gpr_log(GPR_ERROR, "Could not create ssl context.");
+ result = TSI_OUT_OF_RESOURCES;
+ break;
+ }
+ result = populate_ssl_context(
+ impl->ssl_contexts[i], pem_private_keys[i], pem_private_keys_sizes[i],
+ pem_cert_chains[i], pem_cert_chains_sizes[i], cipher_list);
+ if (result != TSI_OK) break;
+
+ if (pem_client_root_certs != NULL) {
+ int flags = SSL_VERIFY_PEER;
+ STACK_OF(X509_NAME) *root_names = NULL;
+ result = ssl_ctx_load_verification_certs(
+ impl->ssl_contexts[i], pem_client_root_certs,
+ pem_client_root_certs_size, &root_names);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Invalid verification certs.");
+ break;
+ }
+ SSL_CTX_set_client_CA_list(impl->ssl_contexts[i], root_names);
+ if (force_client_auth) flags |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
+ SSL_CTX_set_verify(impl->ssl_contexts[i], flags, NULL);
+ /* TODO(jboeuf): Add revocation verification. */
+ }
+
+ result = extract_x509_subject_names_from_pem_cert(
+ pem_cert_chains[i], pem_cert_chains_sizes[i],
+ &impl->ssl_context_x509_subject_names[i]);
+ if (result != TSI_OK) break;
- for (i = 0; i < key_cert_pair_count; i++)
- {
- do
- {
- impl->ssl_contexts[i] = SSL_CTX_new (TLSv1_2_method ());
- if (impl->ssl_contexts[i] == NULL)
- {
- gpr_log (GPR_ERROR, "Could not create ssl context.");
- result = TSI_OUT_OF_RESOURCES;
- break;
- }
- result = populate_ssl_context (impl->ssl_contexts[i], pem_private_keys[i], pem_private_keys_sizes[i], pem_cert_chains[i], pem_cert_chains_sizes[i], cipher_list);
- if (result != TSI_OK)
- break;
-
- if (pem_client_root_certs != NULL)
- {
- int flags = SSL_VERIFY_PEER;
- STACK_OF (X509_NAME) * root_names = NULL;
- result = ssl_ctx_load_verification_certs (impl->ssl_contexts[i], pem_client_root_certs, pem_client_root_certs_size, &root_names);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Invalid verification certs.");
- break;
- }
- SSL_CTX_set_client_CA_list (impl->ssl_contexts[i], root_names);
- if (force_client_auth)
- flags |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
- SSL_CTX_set_verify (impl->ssl_contexts[i], flags, NULL);
- /* TODO(jboeuf): Add revocation verification. */
- }
-
- result = extract_x509_subject_names_from_pem_cert (pem_cert_chains[i], pem_cert_chains_sizes[i], &impl->ssl_context_x509_subject_names[i]);
- if (result != TSI_OK)
- break;
-
- SSL_CTX_set_tlsext_servername_callback (impl->ssl_contexts[i], ssl_server_handshaker_factory_servername_callback);
- SSL_CTX_set_tlsext_servername_arg (impl->ssl_contexts[i], impl);
+ SSL_CTX_set_tlsext_servername_callback(
+ impl->ssl_contexts[i],
+ ssl_server_handshaker_factory_servername_callback);
+ SSL_CTX_set_tlsext_servername_arg(impl->ssl_contexts[i], impl);
#if TSI_OPENSSL_ALPN_SUPPORT
- SSL_CTX_set_alpn_select_cb (impl->ssl_contexts[i], server_handshaker_factory_alpn_callback, impl);
+ SSL_CTX_set_alpn_select_cb(impl->ssl_contexts[i],
+ server_handshaker_factory_alpn_callback, impl);
#endif /* TSI_OPENSSL_ALPN_SUPPORT */
- SSL_CTX_set_next_protos_advertised_cb (impl->ssl_contexts[i], server_handshaker_factory_npn_advertised_callback, impl);
- }
- while (0);
-
- if (result != TSI_OK)
- {
- tsi_ssl_handshaker_factory_destroy (&impl->base);
- return result;
- }
+ SSL_CTX_set_next_protos_advertised_cb(
+ impl->ssl_contexts[i],
+ server_handshaker_factory_npn_advertised_callback, impl);
+ } while (0);
+
+ if (result != TSI_OK) {
+ tsi_ssl_handshaker_factory_destroy(&impl->base);
+ return result;
}
+ }
*factory = &impl->base;
return TSI_OK;
}
/* --- tsi_ssl utils. --- */
-int
-tsi_ssl_peer_matches_name (const tsi_peer * peer, const char *name)
-{
+int tsi_ssl_peer_matches_name(const tsi_peer *peer, const char *name) {
size_t i = 0;
size_t san_count = 0;
const tsi_peer_property *cn_property = NULL;
/* For now reject what looks like an IP address. */
- if (looks_like_ip_address (name))
- return 0;
+ if (looks_like_ip_address(name)) return 0;
/* Check the SAN first. */
- for (i = 0; i < peer->property_count; i++)
- {
- const tsi_peer_property *property = &peer->properties[i];
- if (property->name == NULL)
- continue;
- if (strcmp (property->name, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY) == 0)
- {
- san_count++;
- if (does_entry_match_name (property->value.data, property->value.length, name))
- {
- return 1;
- }
- }
- else if (strcmp (property->name, TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY) == 0)
- {
- cn_property = property;
- }
+ for (i = 0; i < peer->property_count; i++) {
+ const tsi_peer_property *property = &peer->properties[i];
+ if (property->name == NULL) continue;
+ if (strcmp(property->name,
+ TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY) == 0) {
+ san_count++;
+ if (does_entry_match_name(property->value.data, property->value.length,
+ name)) {
+ return 1;
+ }
+ } else if (strcmp(property->name,
+ TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY) == 0) {
+ cn_property = property;
}
+ }
/* If there's no SAN, try the CN. */
- if (san_count == 0 && cn_property != NULL)
- {
- if (does_entry_match_name (cn_property->value.data, cn_property->value.length, name))
- {
- return 1;
- }
+ if (san_count == 0 && cn_property != NULL) {
+ if (does_entry_match_name(cn_property->value.data,
+ cn_property->value.length, name)) {
+ return 1;
}
+ }
- return 0; /* Not found. */
+ return 0; /* Not found. */
}
diff --git a/src/core/tsi/ssl_transport_security.h b/src/core/tsi/ssl_transport_security.h
index c3c4c2f973..51c0003a85 100644
--- a/src/core/tsi/ssl_transport_security.h
+++ b/src/core/tsi/ssl_transport_security.h
@@ -37,8 +37,7 @@
#include "src/core/tsi/transport_security_interface.h"
#ifdef __cplusplus
-extern "C"
-{
+extern "C" {
#endif
/* Value for the TSI_CERTIFICATE_TYPE_PEER_PROPERTY property for X509 certs. */
@@ -56,7 +55,7 @@ extern "C"
This object creates tsi_handshaker objects implemented in terms of the
TLS 1.2 specificiation. */
- typedef struct tsi_ssl_handshaker_factory tsi_ssl_handshaker_factory;
+typedef struct tsi_ssl_handshaker_factory tsi_ssl_handshaker_factory;
/* Creates a client handshaker factory.
- pem_private_key is the buffer containing the PEM encoding of the client's
@@ -85,7 +84,13 @@ extern "C"
- This method returns TSI_OK on success or TSI_INVALID_PARAMETER in the case
where a parameter is invalid. */
- tsi_result tsi_create_ssl_client_handshaker_factory (const unsigned char *pem_private_key, size_t pem_private_key_size, const unsigned char *pem_cert_chain, size_t pem_cert_chain_size, const unsigned char *pem_root_certs, size_t pem_root_certs_size, const char *cipher_suites, const unsigned char **alpn_protocols, const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols, tsi_ssl_handshaker_factory ** factory);
+tsi_result tsi_create_ssl_client_handshaker_factory(
+ const unsigned char *pem_private_key, size_t pem_private_key_size,
+ const unsigned char *pem_cert_chain, size_t pem_cert_chain_size,
+ const unsigned char *pem_root_certs, size_t pem_root_certs_size,
+ const char *cipher_suites, const unsigned char **alpn_protocols,
+ const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols,
+ tsi_ssl_handshaker_factory **factory);
/* Creates a server handshaker factory.
- version indicates which version of the specification to use.
@@ -125,7 +130,15 @@ extern "C"
- This method returns TSI_OK on success or TSI_INVALID_PARAMETER in the case
where a parameter is invalid. */
- tsi_result tsi_create_ssl_server_handshaker_factory (const unsigned char **pem_private_keys, const size_t * pem_private_keys_sizes, const unsigned char **pem_cert_chains, const size_t * pem_cert_chains_sizes, size_t key_cert_pair_count, const unsigned char *pem_client_root_certs, size_t pem_client_root_certs_size, int force_client_auth, const char *cipher_suites, const unsigned char **alpn_protocols, const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols, tsi_ssl_handshaker_factory ** factory);
+tsi_result tsi_create_ssl_server_handshaker_factory(
+ const unsigned char **pem_private_keys,
+ const size_t *pem_private_keys_sizes, const unsigned char **pem_cert_chains,
+ const size_t *pem_cert_chains_sizes, size_t key_cert_pair_count,
+ const unsigned char *pem_client_root_certs,
+ size_t pem_client_root_certs_size, int force_client_auth,
+ const char *cipher_suites, const unsigned char **alpn_protocols,
+ const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols,
+ tsi_ssl_handshaker_factory **factory);
/* Creates a handshaker.
- self is the factory from which the handshaker will be created.
@@ -137,11 +150,13 @@ extern "C"
- This method returns TSI_OK on success or TSI_INVALID_PARAMETER in the case
where a parameter is invalid. */
- tsi_result tsi_ssl_handshaker_factory_create_handshaker (tsi_ssl_handshaker_factory * self, const char *server_name_indication, tsi_handshaker ** handshaker);
+tsi_result tsi_ssl_handshaker_factory_create_handshaker(
+ tsi_ssl_handshaker_factory *self, const char *server_name_indication,
+ tsi_handshaker **handshaker);
/* Destroys the handshaker factory. WARNING: it is unsafe to destroy a factory
while handshakers created with this factory are still in use. */
- void tsi_ssl_handshaker_factory_destroy (tsi_ssl_handshaker_factory * self);
+void tsi_ssl_handshaker_factory_destroy(tsi_ssl_handshaker_factory *self);
/* Util that checks that an ssl peer matches a specific name.
Still TODO(jboeuf):
@@ -149,10 +164,10 @@ extern "C"
- handle %encoded chars.
- handle public suffix wildchar more strictly (e.g. *.co.uk)
- handle IP addresses in SAN. */
- int tsi_ssl_peer_matches_name (const tsi_peer * peer, const char *name);
+int tsi_ssl_peer_matches_name(const tsi_peer *peer, const char *name);
#ifdef __cplusplus
}
#endif
-#endif /* GRPC_INTERNAL_CORE_TSI_SSL_TRANSPORT_SECURITY_H */
+#endif /* GRPC_INTERNAL_CORE_TSI_SSL_TRANSPORT_SECURITY_H */
diff --git a/src/core/tsi/transport_security.c b/src/core/tsi/transport_security.c
index c26ee40c4c..c39e584496 100644
--- a/src/core/tsi/transport_security.c
+++ b/src/core/tsi/transport_security.c
@@ -42,28 +42,21 @@ int tsi_tracing_enabled = 0;
/* --- Utils. --- */
-char *
-tsi_strdup (const char *src)
-{
+char *tsi_strdup(const char *src) {
char *dst;
size_t len;
- if (!src)
- return NULL;
- len = strlen (src) + 1;
- dst = malloc (len);
- if (!dst)
- return NULL;
- memcpy (dst, src, len);
+ if (!src) return NULL;
+ len = strlen(src) + 1;
+ dst = malloc(len);
+ if (!dst) return NULL;
+ memcpy(dst, src, len);
return dst;
}
/* --- tsi_result common implementation. --- */
-const char *
-tsi_result_to_string (tsi_result result)
-{
- switch (result)
- {
+const char *tsi_result_to_string(tsi_result result) {
+ switch (result) {
case TSI_OK:
return "TSI_OK";
case TSI_UNKNOWN_ERROR:
@@ -92,228 +85,196 @@ tsi_result_to_string (tsi_result result)
return "TSI_OUT_OF_RESOURCES";
default:
return "UNKNOWN";
- }
+ }
}
/* --- tsi_frame_protector common implementation. ---
Calls specific implementation after state/input validation. */
-tsi_result
-tsi_frame_protector_protect (tsi_frame_protector * self, const unsigned char *unprotected_bytes, size_t * unprotected_bytes_size, unsigned char *protected_output_frames, size_t * protected_output_frames_size)
-{
- if (self == NULL || unprotected_bytes == NULL || unprotected_bytes_size == NULL || protected_output_frames == NULL || protected_output_frames_size == NULL)
- {
- return TSI_INVALID_ARGUMENT;
- }
- return self->vtable->protect (self, unprotected_bytes, unprotected_bytes_size, protected_output_frames, protected_output_frames_size);
+tsi_result tsi_frame_protector_protect(tsi_frame_protector *self,
+ const unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size,
+ unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size) {
+ if (self == NULL || unprotected_bytes == NULL ||
+ unprotected_bytes_size == NULL || protected_output_frames == NULL ||
+ protected_output_frames_size == NULL) {
+ return TSI_INVALID_ARGUMENT;
+ }
+ return self->vtable->protect(self, unprotected_bytes, unprotected_bytes_size,
+ protected_output_frames,
+ protected_output_frames_size);
}
-tsi_result
-tsi_frame_protector_protect_flush (tsi_frame_protector * self, unsigned char *protected_output_frames, size_t * protected_output_frames_size, size_t * still_pending_size)
-{
- if (self == NULL || protected_output_frames == NULL || protected_output_frames == NULL || still_pending_size == NULL)
- {
- return TSI_INVALID_ARGUMENT;
- }
- return self->vtable->protect_flush (self, protected_output_frames, protected_output_frames_size, still_pending_size);
+tsi_result tsi_frame_protector_protect_flush(
+ tsi_frame_protector *self, unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size, size_t *still_pending_size) {
+ if (self == NULL || protected_output_frames == NULL ||
+ protected_output_frames == NULL || still_pending_size == NULL) {
+ return TSI_INVALID_ARGUMENT;
+ }
+ return self->vtable->protect_flush(self, protected_output_frames,
+ protected_output_frames_size,
+ still_pending_size);
}
-tsi_result
-tsi_frame_protector_unprotect (tsi_frame_protector * self, const unsigned char *protected_frames_bytes, size_t * protected_frames_bytes_size, unsigned char *unprotected_bytes, size_t * unprotected_bytes_size)
-{
- if (self == NULL || protected_frames_bytes == NULL || protected_frames_bytes_size == NULL || unprotected_bytes == NULL || unprotected_bytes_size == NULL)
- {
- return TSI_INVALID_ARGUMENT;
- }
- return self->vtable->unprotect (self, protected_frames_bytes, protected_frames_bytes_size, unprotected_bytes, unprotected_bytes_size);
+tsi_result tsi_frame_protector_unprotect(
+ tsi_frame_protector *self, const unsigned char *protected_frames_bytes,
+ size_t *protected_frames_bytes_size, unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size) {
+ if (self == NULL || protected_frames_bytes == NULL ||
+ protected_frames_bytes_size == NULL || unprotected_bytes == NULL ||
+ unprotected_bytes_size == NULL) {
+ return TSI_INVALID_ARGUMENT;
+ }
+ return self->vtable->unprotect(self, protected_frames_bytes,
+ protected_frames_bytes_size, unprotected_bytes,
+ unprotected_bytes_size);
}
-void
-tsi_frame_protector_destroy (tsi_frame_protector * self)
-{
- if (self == NULL)
- return;
- self->vtable->destroy (self);
+void tsi_frame_protector_destroy(tsi_frame_protector *self) {
+ if (self == NULL) return;
+ self->vtable->destroy(self);
}
/* --- tsi_handshaker common implementation. ---
Calls specific implementation after state/input validation. */
-tsi_result
-tsi_handshaker_get_bytes_to_send_to_peer (tsi_handshaker * self, unsigned char *bytes, size_t * bytes_size)
-{
- if (self == NULL)
- return TSI_INVALID_ARGUMENT;
- if (self->frame_protector_created)
- return TSI_FAILED_PRECONDITION;
- return self->vtable->get_bytes_to_send_to_peer (self, bytes, bytes_size);
+tsi_result tsi_handshaker_get_bytes_to_send_to_peer(tsi_handshaker *self,
+ unsigned char *bytes,
+ size_t *bytes_size) {
+ if (self == NULL) return TSI_INVALID_ARGUMENT;
+ if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
+ return self->vtable->get_bytes_to_send_to_peer(self, bytes, bytes_size);
}
-tsi_result
-tsi_handshaker_process_bytes_from_peer (tsi_handshaker * self, const unsigned char *bytes, size_t * bytes_size)
-{
- if (self == NULL)
- return TSI_INVALID_ARGUMENT;
- if (self->frame_protector_created)
- return TSI_FAILED_PRECONDITION;
- return self->vtable->process_bytes_from_peer (self, bytes, bytes_size);
+tsi_result tsi_handshaker_process_bytes_from_peer(tsi_handshaker *self,
+ const unsigned char *bytes,
+ size_t *bytes_size) {
+ if (self == NULL) return TSI_INVALID_ARGUMENT;
+ if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
+ return self->vtable->process_bytes_from_peer(self, bytes, bytes_size);
}
-tsi_result
-tsi_handshaker_get_result (tsi_handshaker * self)
-{
- if (self == NULL)
- return TSI_INVALID_ARGUMENT;
- if (self->frame_protector_created)
- return TSI_FAILED_PRECONDITION;
- return self->vtable->get_result (self);
+tsi_result tsi_handshaker_get_result(tsi_handshaker *self) {
+ if (self == NULL) return TSI_INVALID_ARGUMENT;
+ if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
+ return self->vtable->get_result(self);
}
-tsi_result
-tsi_handshaker_extract_peer (tsi_handshaker * self, tsi_peer * peer)
-{
- if (self == NULL || peer == NULL)
- return TSI_INVALID_ARGUMENT;
- memset (peer, 0, sizeof (tsi_peer));
- if (self->frame_protector_created)
+tsi_result tsi_handshaker_extract_peer(tsi_handshaker *self, tsi_peer *peer) {
+ if (self == NULL || peer == NULL) return TSI_INVALID_ARGUMENT;
+ memset(peer, 0, sizeof(tsi_peer));
+ if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
+ if (tsi_handshaker_get_result(self) != TSI_OK) {
return TSI_FAILED_PRECONDITION;
- if (tsi_handshaker_get_result (self) != TSI_OK)
- {
- return TSI_FAILED_PRECONDITION;
- }
- return self->vtable->extract_peer (self, peer);
+ }
+ return self->vtable->extract_peer(self, peer);
}
-tsi_result
-tsi_handshaker_create_frame_protector (tsi_handshaker * self, size_t * max_protected_frame_size, tsi_frame_protector ** protector)
-{
+tsi_result tsi_handshaker_create_frame_protector(
+ tsi_handshaker *self, size_t *max_protected_frame_size,
+ tsi_frame_protector **protector) {
tsi_result result;
- if (self == NULL || protector == NULL)
- return TSI_INVALID_ARGUMENT;
- if (self->frame_protector_created)
+ if (self == NULL || protector == NULL) return TSI_INVALID_ARGUMENT;
+ if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
+ if (tsi_handshaker_get_result(self) != TSI_OK) {
return TSI_FAILED_PRECONDITION;
- if (tsi_handshaker_get_result (self) != TSI_OK)
- {
- return TSI_FAILED_PRECONDITION;
- }
- result = self->vtable->create_frame_protector (self, max_protected_frame_size, protector);
- if (result == TSI_OK)
- {
- self->frame_protector_created = 1;
- }
+ }
+ result = self->vtable->create_frame_protector(self, max_protected_frame_size,
+ protector);
+ if (result == TSI_OK) {
+ self->frame_protector_created = 1;
+ }
return result;
}
-void
-tsi_handshaker_destroy (tsi_handshaker * self)
-{
- if (self == NULL)
- return;
- self->vtable->destroy (self);
+void tsi_handshaker_destroy(tsi_handshaker *self) {
+ if (self == NULL) return;
+ self->vtable->destroy(self);
}
/* --- tsi_peer implementation. --- */
-tsi_peer_property
-tsi_init_peer_property (void)
-{
+tsi_peer_property tsi_init_peer_property(void) {
tsi_peer_property property;
- memset (&property, 0, sizeof (tsi_peer_property));
+ memset(&property, 0, sizeof(tsi_peer_property));
return property;
}
-static void
-tsi_peer_destroy_list_property (tsi_peer_property * children, size_t child_count)
-{
+static void tsi_peer_destroy_list_property(tsi_peer_property *children,
+ size_t child_count) {
size_t i;
- for (i = 0; i < child_count; i++)
- {
- tsi_peer_property_destruct (&children[i]);
- }
- free (children);
+ for (i = 0; i < child_count; i++) {
+ tsi_peer_property_destruct(&children[i]);
+ }
+ free(children);
}
-void
-tsi_peer_property_destruct (tsi_peer_property * property)
-{
- if (property->name != NULL)
- {
- free (property->name);
- }
- if (property->value.data != NULL)
- {
- free (property->value.data);
- }
- *property = tsi_init_peer_property (); /* Reset everything to 0. */
+void tsi_peer_property_destruct(tsi_peer_property *property) {
+ if (property->name != NULL) {
+ free(property->name);
+ }
+ if (property->value.data != NULL) {
+ free(property->value.data);
+ }
+ *property = tsi_init_peer_property(); /* Reset everything to 0. */
}
-void
-tsi_peer_destruct (tsi_peer * self)
-{
- if (self == NULL)
- return;
- if (self->properties != NULL)
- {
- tsi_peer_destroy_list_property (self->properties, self->property_count);
- self->properties = NULL;
- }
+void tsi_peer_destruct(tsi_peer *self) {
+ if (self == NULL) return;
+ if (self->properties != NULL) {
+ tsi_peer_destroy_list_property(self->properties, self->property_count);
+ self->properties = NULL;
+ }
self->property_count = 0;
}
-tsi_result
-tsi_construct_allocated_string_peer_property (const char *name, size_t value_length, tsi_peer_property * property)
-{
- *property = tsi_init_peer_property ();
- if (name != NULL)
- {
- property->name = tsi_strdup (name);
- if (property->name == NULL)
- return TSI_OUT_OF_RESOURCES;
- }
- if (value_length > 0)
- {
- property->value.data = calloc (1, value_length);
- if (property->value.data == NULL)
- {
- tsi_peer_property_destruct (property);
- return TSI_OUT_OF_RESOURCES;
- }
- property->value.length = value_length;
+tsi_result tsi_construct_allocated_string_peer_property(
+ const char *name, size_t value_length, tsi_peer_property *property) {
+ *property = tsi_init_peer_property();
+ if (name != NULL) {
+ property->name = tsi_strdup(name);
+ if (property->name == NULL) return TSI_OUT_OF_RESOURCES;
+ }
+ if (value_length > 0) {
+ property->value.data = calloc(1, value_length);
+ if (property->value.data == NULL) {
+ tsi_peer_property_destruct(property);
+ return TSI_OUT_OF_RESOURCES;
}
+ property->value.length = value_length;
+ }
return TSI_OK;
}
-tsi_result
-tsi_construct_string_peer_property_from_cstring (const char *name, const char *value, tsi_peer_property * property)
-{
- return tsi_construct_string_peer_property (name, value, strlen (value), property);
+tsi_result tsi_construct_string_peer_property_from_cstring(
+ const char *name, const char *value, tsi_peer_property *property) {
+ return tsi_construct_string_peer_property(name, value, strlen(value),
+ property);
}
-tsi_result
-tsi_construct_string_peer_property (const char *name, const char *value, size_t value_length, tsi_peer_property * property)
-{
- tsi_result result = tsi_construct_allocated_string_peer_property (name, value_length, property);
- if (result != TSI_OK)
- return result;
- if (value_length > 0)
- {
- memcpy (property->value.data, value, value_length);
- }
+tsi_result tsi_construct_string_peer_property(const char *name,
+ const char *value,
+ size_t value_length,
+ tsi_peer_property *property) {
+ tsi_result result = tsi_construct_allocated_string_peer_property(
+ name, value_length, property);
+ if (result != TSI_OK) return result;
+ if (value_length > 0) {
+ memcpy(property->value.data, value, value_length);
+ }
return TSI_OK;
}
-tsi_result
-tsi_construct_peer (size_t property_count, tsi_peer * peer)
-{
- memset (peer, 0, sizeof (tsi_peer));
- if (property_count > 0)
- {
- peer->properties = calloc (property_count, sizeof (tsi_peer_property));
- if (peer->properties == NULL)
- return TSI_OUT_OF_RESOURCES;
- peer->property_count = property_count;
- }
+tsi_result tsi_construct_peer(size_t property_count, tsi_peer *peer) {
+ memset(peer, 0, sizeof(tsi_peer));
+ if (property_count > 0) {
+ peer->properties = calloc(property_count, sizeof(tsi_peer_property));
+ if (peer->properties == NULL) return TSI_OUT_OF_RESOURCES;
+ peer->property_count = property_count;
+ }
return TSI_OK;
}
diff --git a/src/core/tsi/transport_security.h b/src/core/tsi/transport_security.h
index 3ae82a31cc..4077737473 100644
--- a/src/core/tsi/transport_security.h
+++ b/src/core/tsi/transport_security.h
@@ -37,58 +37,75 @@
#include "src/core/tsi/transport_security_interface.h"
#ifdef __cplusplus
-extern "C"
-{
+extern "C" {
#endif
- extern int tsi_tracing_enabled;
+extern int tsi_tracing_enabled;
/* Base for tsi_frame_protector implementations.
See transport_security_interface.h for documentation. */
- typedef struct
- {
- tsi_result (*protect) (tsi_frame_protector * self, const unsigned char *unprotected_bytes, size_t * unprotected_bytes_size, unsigned char *protected_output_frames, size_t * protected_output_frames_size);
- tsi_result (*protect_flush) (tsi_frame_protector * self, unsigned char *protected_output_frames, size_t * protected_output_frames_size, size_t * still_pending_size);
- tsi_result (*unprotect) (tsi_frame_protector * self, const unsigned char *protected_frames_bytes, size_t * protected_frames_bytes_size, unsigned char *unprotected_bytes, size_t * unprotected_bytes_size);
- void (*destroy) (tsi_frame_protector * self);
- } tsi_frame_protector_vtable;
+typedef struct {
+ tsi_result (*protect)(tsi_frame_protector *self,
+ const unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size,
+ unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size);
+ tsi_result (*protect_flush)(tsi_frame_protector *self,
+ unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size,
+ size_t *still_pending_size);
+ tsi_result (*unprotect)(tsi_frame_protector *self,
+ const unsigned char *protected_frames_bytes,
+ size_t *protected_frames_bytes_size,
+ unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size);
+ void (*destroy)(tsi_frame_protector *self);
+} tsi_frame_protector_vtable;
- struct tsi_frame_protector
- {
- const tsi_frame_protector_vtable *vtable;
- };
+struct tsi_frame_protector {
+ const tsi_frame_protector_vtable *vtable;
+};
/* Base for tsi_handshaker implementations.
See transport_security_interface.h for documentation. */
- typedef struct
- {
- tsi_result (*get_bytes_to_send_to_peer) (tsi_handshaker * self, unsigned char *bytes, size_t * bytes_size);
- tsi_result (*process_bytes_from_peer) (tsi_handshaker * self, const unsigned char *bytes, size_t * bytes_size);
- tsi_result (*get_result) (tsi_handshaker * self);
- tsi_result (*extract_peer) (tsi_handshaker * self, tsi_peer * peer);
- tsi_result (*create_frame_protector) (tsi_handshaker * self, size_t * max_protected_frame_size, tsi_frame_protector ** protector);
- void (*destroy) (tsi_handshaker * self);
- } tsi_handshaker_vtable;
+typedef struct {
+ tsi_result (*get_bytes_to_send_to_peer)(tsi_handshaker *self,
+ unsigned char *bytes,
+ size_t *bytes_size);
+ tsi_result (*process_bytes_from_peer)(tsi_handshaker *self,
+ const unsigned char *bytes,
+ size_t *bytes_size);
+ tsi_result (*get_result)(tsi_handshaker *self);
+ tsi_result (*extract_peer)(tsi_handshaker *self, tsi_peer *peer);
+ tsi_result (*create_frame_protector)(tsi_handshaker *self,
+ size_t *max_protected_frame_size,
+ tsi_frame_protector **protector);
+ void (*destroy)(tsi_handshaker *self);
+} tsi_handshaker_vtable;
- struct tsi_handshaker
- {
- const tsi_handshaker_vtable *vtable;
- int frame_protector_created;
- };
+struct tsi_handshaker {
+ const tsi_handshaker_vtable *vtable;
+ int frame_protector_created;
+};
/* Peer and property construction/destruction functions. */
- tsi_result tsi_construct_peer (size_t property_count, tsi_peer * peer);
- tsi_peer_property tsi_init_peer_property (void);
- void tsi_peer_property_destruct (tsi_peer_property * property);
- tsi_result tsi_construct_string_peer_property (const char *name, const char *value, size_t value_length, tsi_peer_property * property);
- tsi_result tsi_construct_allocated_string_peer_property (const char *name, size_t value_length, tsi_peer_property * property);
- tsi_result tsi_construct_string_peer_property_from_cstring (const char *name, const char *value, tsi_peer_property * property);
+tsi_result tsi_construct_peer(size_t property_count, tsi_peer *peer);
+tsi_peer_property tsi_init_peer_property(void);
+void tsi_peer_property_destruct(tsi_peer_property *property);
+tsi_result tsi_construct_string_peer_property(const char *name,
+ const char *value,
+ size_t value_length,
+ tsi_peer_property *property);
+tsi_result tsi_construct_allocated_string_peer_property(
+ const char *name, size_t value_length, tsi_peer_property *property);
+tsi_result tsi_construct_string_peer_property_from_cstring(
+ const char *name, const char *value, tsi_peer_property *property);
/* Utils. */
- char *tsi_strdup (const char *src); /* Sadly, no strdup in C89. */
+char *tsi_strdup(const char *src); /* Sadly, no strdup in C89. */
#ifdef __cplusplus
}
#endif
-#endif /* GRPC_INTERNAL_CORE_TSI_TRANSPORT_SECURITY_H */
+#endif /* GRPC_INTERNAL_CORE_TSI_TRANSPORT_SECURITY_H */
diff --git a/src/core/tsi/transport_security_interface.h b/src/core/tsi/transport_security_interface.h
index e43b31fd06..69ee17ae91 100644
--- a/src/core/tsi/transport_security_interface.h
+++ b/src/core/tsi/transport_security_interface.h
@@ -38,42 +38,40 @@
#include <stdlib.h>
#ifdef __cplusplus
-extern "C"
-{
+extern "C" {
#endif
/* --- tsi result --- */
- typedef enum
- {
- TSI_OK = 0,
- TSI_UNKNOWN_ERROR = 1,
- TSI_INVALID_ARGUMENT = 2,
- TSI_PERMISSION_DENIED = 3,
- TSI_INCOMPLETE_DATA = 4,
- TSI_FAILED_PRECONDITION = 5,
- TSI_UNIMPLEMENTED = 6,
- TSI_INTERNAL_ERROR = 7,
- TSI_DATA_CORRUPTED = 8,
- TSI_NOT_FOUND = 9,
- TSI_PROTOCOL_FAILURE = 10,
- TSI_HANDSHAKE_IN_PROGRESS = 11,
- TSI_OUT_OF_RESOURCES = 12
- } tsi_result;
-
- const char *tsi_result_to_string (tsi_result result);
+typedef enum {
+ TSI_OK = 0,
+ TSI_UNKNOWN_ERROR = 1,
+ TSI_INVALID_ARGUMENT = 2,
+ TSI_PERMISSION_DENIED = 3,
+ TSI_INCOMPLETE_DATA = 4,
+ TSI_FAILED_PRECONDITION = 5,
+ TSI_UNIMPLEMENTED = 6,
+ TSI_INTERNAL_ERROR = 7,
+ TSI_DATA_CORRUPTED = 8,
+ TSI_NOT_FOUND = 9,
+ TSI_PROTOCOL_FAILURE = 10,
+ TSI_HANDSHAKE_IN_PROGRESS = 11,
+ TSI_OUT_OF_RESOURCES = 12
+} tsi_result;
+
+const char *tsi_result_to_string(tsi_result result);
/* --- tsi tracing --- */
/* Set this early to avoid races */
- extern int tsi_tracing_enabled;
+extern int tsi_tracing_enabled;
/* --- tsi_frame_protector object ---
This object protects and unprotects buffers once the handshake is done.
Implementations of this object must be thread compatible. */
- typedef struct tsi_frame_protector tsi_frame_protector;
+typedef struct tsi_frame_protector tsi_frame_protector;
/* Outputs protected frames.
- unprotected_bytes is an input only parameter and points to the data
@@ -128,7 +126,11 @@ extern "C"
if (result != TSI_OK) HandleError(result);
------------------------------------------------------------------------ */
- tsi_result tsi_frame_protector_protect (tsi_frame_protector * self, const unsigned char *unprotected_bytes, size_t * unprotected_bytes_size, unsigned char *protected_output_frames, size_t * protected_output_frames_size);
+tsi_result tsi_frame_protector_protect(tsi_frame_protector *self,
+ const unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size,
+ unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size);
/* Indicates that we need to flush the bytes buffered in the protector and get
the resulting frame.
@@ -138,7 +140,9 @@ extern "C"
caller to specify how many bytes are available in protected_output_frames.
- still_pending_bytes is an output parameter indicating the number of bytes
that still need to be flushed from the protector.*/
- tsi_result tsi_frame_protector_protect_flush (tsi_frame_protector * self, unsigned char *protected_output_frames, size_t * protected_output_frames_size, size_t * still_pending_size);
+tsi_result tsi_frame_protector_protect_flush(
+ tsi_frame_protector *self, unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size, size_t *still_pending_size);
/* Outputs unprotected bytes.
- protected_frames_bytes is an input only parameter and points to the
@@ -162,10 +166,13 @@ extern "C"
unprotected_bytes_size will be set to 0 and cases where the internal buffer
needs to be read before new protected data can be processed in which case
protected_frames_size will be set to 0. */
- tsi_result tsi_frame_protector_unprotect (tsi_frame_protector * self, const unsigned char *protected_frames_bytes, size_t * protected_frames_bytes_size, unsigned char *unprotected_bytes, size_t * unprotected_bytes_size);
+tsi_result tsi_frame_protector_unprotect(
+ tsi_frame_protector *self, const unsigned char *protected_frames_bytes,
+ size_t *protected_frames_bytes_size, unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size);
/* Destroys the tsi_frame_protector object. */
- void tsi_frame_protector_destroy (tsi_frame_protector * self);
+void tsi_frame_protector_destroy(tsi_frame_protector *self);
/* --- tsi_peer objects ---
@@ -176,24 +183,21 @@ extern "C"
/* Property values may contain NULL characters just like C++ strings.
The length field gives the length of the string. */
- typedef struct tsi_peer_property
- {
- char *name;
- struct
- {
- char *data;
- size_t length;
- } value;
- } tsi_peer_property;
-
- typedef struct
- {
- tsi_peer_property *properties;
- size_t property_count;
- } tsi_peer;
+typedef struct tsi_peer_property {
+ char *name;
+ struct {
+ char *data;
+ size_t length;
+ } value;
+} tsi_peer_property;
+
+typedef struct {
+ tsi_peer_property *properties;
+ size_t property_count;
+} tsi_peer;
/* Destructs the tsi_peer object. */
- void tsi_peer_destruct (tsi_peer * self);
+void tsi_peer_destruct(tsi_peer *self);
/* --- tsi_handshaker objects ----
@@ -262,7 +266,7 @@ extern "C"
}
...
------------------------------------------------------------------------ */
- typedef struct tsi_handshaker tsi_handshaker;
+typedef struct tsi_handshaker tsi_handshaker;
/* Gets bytes that need to be sent to the peer.
- bytes is the buffer that will be written with the data to be sent to the
@@ -275,7 +279,9 @@ extern "C"
needs to be called again to get all the bytes to send to the peer (there
was more data to write than the specified bytes_size). In case of a fatal
error in the handshake, another specific error code is returned. */
- tsi_result tsi_handshaker_get_bytes_to_send_to_peer (tsi_handshaker * self, unsigned char *bytes, size_t * bytes_size);
+tsi_result tsi_handshaker_get_bytes_to_send_to_peer(tsi_handshaker *self,
+ unsigned char *bytes,
+ size_t *bytes_size);
/* Processes bytes received from the peer.
- bytes is the buffer containing the data.
@@ -286,14 +292,16 @@ extern "C"
needs to be called again to complete the data needed for processing. In
case of a fatal error in the handshake, another specific error code is
returned. */
- tsi_result tsi_handshaker_process_bytes_from_peer (tsi_handshaker * self, const unsigned char *bytes, size_t * bytes_size);
+tsi_result tsi_handshaker_process_bytes_from_peer(tsi_handshaker *self,
+ const unsigned char *bytes,
+ size_t *bytes_size);
/* Gets the result of the handshaker.
Returns TSI_OK if the hanshake completed successfully and there has been no
errors. Returns TSI_HANDSHAKE_IN_PROGRESS if the handshaker is not done yet
but no error has been encountered so far. Otherwise the handshaker failed
with the returned error. */
- tsi_result tsi_handshaker_get_result (tsi_handshaker * self);
+tsi_result tsi_handshaker_get_result(tsi_handshaker *self);
/* Returns 1 if the handshake is in progress, 0 otherwise. */
#define tsi_handshaker_is_in_progress(h) \
@@ -303,7 +311,7 @@ extern "C"
tsi_handshaker_is_in_progress returns 1, it returns TSI_OK otherwise
assuming the handshaker is not in a fatal error state.
The caller is responsible for destructing the peer. */
- tsi_result tsi_handshaker_extract_peer (tsi_handshaker * self, tsi_peer * peer);
+tsi_result tsi_handshaker_extract_peer(tsi_handshaker *self, tsi_peer *peer);
/* This method creates a tsi_frame_protector object after the handshake phase
is done. After this method has been called successfully, the only method
@@ -321,14 +329,16 @@ extern "C"
tsi_handshaker_is_in_progress returns 1, it returns TSI_OK otherwise assuming
the handshaker is not in a fatal error state.
The caller is responsible for destroying the protector. */
- tsi_result tsi_handshaker_create_frame_protector (tsi_handshaker * self, size_t * max_output_protected_frame_size, tsi_frame_protector ** protector);
+tsi_result tsi_handshaker_create_frame_protector(
+ tsi_handshaker *self, size_t *max_output_protected_frame_size,
+ tsi_frame_protector **protector);
/* This method releases the tsi_handshaker object. After this method is called,
no other method can be called on the object. */
- void tsi_handshaker_destroy (tsi_handshaker * self);
+void tsi_handshaker_destroy(tsi_handshaker *self);
#ifdef __cplusplus
}
#endif
-#endif /* GRPC_INTERNAL_CORE_TSI_TRANSPORT_SECURITY_INTERFACE_H */
+#endif /* GRPC_INTERNAL_CORE_TSI_TRANSPORT_SECURITY_INTERFACE_H */