aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/tsi/alts/handshaker
diff options
context:
space:
mode:
authorGravatar Yihua Zhang <yihuaz@google.com>2018-03-06 21:40:45 -0800
committerGravatar Yihua Zhang <yihuaz@google.com>2018-03-06 21:40:45 -0800
commiteaf64866777a42996835606e9c3626c974e728c5 (patch)
tree04c693051ba14d798554543123e9b7001d1a3e83 /src/core/tsi/alts/handshaker
parent14b470ac8f59ab1dc88c53335efa84f4849f3fa9 (diff)
Add ALTS code to grpc/core
Diffstat (limited to 'src/core/tsi/alts/handshaker')
-rw-r--r--src/core/tsi/alts/handshaker/alts_handshaker_client.cc316
-rw-r--r--src/core/tsi/alts/handshaker/alts_handshaker_client.h137
-rw-r--r--src/core/tsi/alts/handshaker/alts_handshaker_service_api.cc520
-rw-r--r--src/core/tsi/alts/handshaker/alts_handshaker_service_api.h323
-rw-r--r--src/core/tsi/alts/handshaker/alts_handshaker_service_api_util.cc143
-rw-r--r--src/core/tsi/alts/handshaker/alts_handshaker_service_api_util.h149
-rw-r--r--src/core/tsi/alts/handshaker/alts_tsi_event.cc73
-rw-r--r--src/core/tsi/alts/handshaker/alts_tsi_event.h93
-rw-r--r--src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc483
-rw-r--r--src/core/tsi/alts/handshaker/alts_tsi_handshaker.h83
-rw-r--r--src/core/tsi/alts/handshaker/alts_tsi_handshaker_private.h52
-rw-r--r--src/core/tsi/alts/handshaker/alts_tsi_utils.cc58
-rw-r--r--src/core/tsi/alts/handshaker/alts_tsi_utils.h52
-rw-r--r--src/core/tsi/alts/handshaker/altscontext.pb.c48
-rw-r--r--src/core/tsi/alts/handshaker/altscontext.pb.h64
-rw-r--r--src/core/tsi/alts/handshaker/handshaker.pb.c123
-rw-r--r--src/core/tsi/alts/handshaker/handshaker.pb.h255
-rw-r--r--src/core/tsi/alts/handshaker/proto/altscontext.proto41
-rw-r--r--src/core/tsi/alts/handshaker/proto/handshaker.options2
-rw-r--r--src/core/tsi/alts/handshaker/proto/handshaker.proto220
-rw-r--r--src/core/tsi/alts/handshaker/proto/transport_security_common.proto40
-rw-r--r--src/core/tsi/alts/handshaker/transport_security_common.pb.c50
-rw-r--r--src/core/tsi/alts/handshaker/transport_security_common.pb.h78
-rw-r--r--src/core/tsi/alts/handshaker/transport_security_common_api.cc196
-rw-r--r--src/core/tsi/alts/handshaker/transport_security_common_api.h163
25 files changed, 3762 insertions, 0 deletions
diff --git a/src/core/tsi/alts/handshaker/alts_handshaker_client.cc b/src/core/tsi/alts/handshaker/alts_handshaker_client.cc
new file mode 100644
index 0000000000..40f30e41ca
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/alts_handshaker_client.cc
@@ -0,0 +1,316 @@
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <grpc/support/port_platform.h>
+
+#include "src/core/tsi/alts/handshaker/alts_handshaker_client.h"
+
+#include <grpc/byte_buffer.h>
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+
+#include "src/core/tsi/alts/handshaker/alts_handshaker_service_api.h"
+
+const int kHandshakerClientOpNum = 4;
+
+typedef struct alts_grpc_handshaker_client {
+ alts_handshaker_client base;
+ grpc_call* call;
+ alts_grpc_caller grpc_caller;
+} alts_grpc_handshaker_client;
+
+static grpc_call_error grpc_start_batch(grpc_call* call, const grpc_op* ops,
+ size_t nops, void* tag) {
+ return grpc_call_start_batch(call, ops, nops, tag, nullptr);
+}
+
+/**
+ * Populate grpc operation data with the fields of ALTS TSI event and make a
+ * grpc call.
+ */
+static tsi_result make_grpc_call(alts_handshaker_client* client,
+ alts_tsi_event* event, bool is_start) {
+ GPR_ASSERT(client != nullptr && event != nullptr);
+ alts_grpc_handshaker_client* grpc_client =
+ reinterpret_cast<alts_grpc_handshaker_client*>(client);
+ grpc_op ops[kHandshakerClientOpNum];
+ memset(ops, 0, sizeof(ops));
+ grpc_op* op = ops;
+ if (is_start) {
+ op->op = GRPC_OP_SEND_INITIAL_METADATA;
+ op->data.send_initial_metadata.count = 0;
+ op++;
+ GPR_ASSERT(op - ops <= kHandshakerClientOpNum);
+ op->op = GRPC_OP_RECV_INITIAL_METADATA;
+ op->data.recv_initial_metadata.recv_initial_metadata =
+ &event->initial_metadata;
+ op++;
+ GPR_ASSERT(op - ops <= kHandshakerClientOpNum);
+ }
+ op->op = GRPC_OP_SEND_MESSAGE;
+ op->data.send_message.send_message = event->send_buffer;
+ op++;
+ GPR_ASSERT(op - ops <= kHandshakerClientOpNum);
+ op->op = GRPC_OP_RECV_MESSAGE;
+ op->data.recv_message.recv_message = &event->recv_buffer;
+ op++;
+ GPR_ASSERT(op - ops <= kHandshakerClientOpNum);
+ GPR_ASSERT(grpc_client->grpc_caller != nullptr);
+ if (grpc_client->grpc_caller(grpc_client->call, ops,
+ static_cast<size_t>(op - ops),
+ (void*)event) != GRPC_CALL_OK) {
+ gpr_log(GPR_ERROR, "Start batch operation failed");
+ return TSI_INTERNAL_ERROR;
+ }
+ return TSI_OK;
+}
+
+/* Create and populate a client_start handshaker request, then serialize it. */
+static grpc_byte_buffer* get_serialized_start_client(alts_tsi_event* event) {
+ bool ok = true;
+ grpc_gcp_handshaker_req* req =
+ grpc_gcp_handshaker_req_create(CLIENT_START_REQ);
+ ok &= grpc_gcp_handshaker_req_set_handshake_protocol(
+ req, grpc_gcp_HandshakeProtocol_ALTS);
+ ok &= grpc_gcp_handshaker_req_add_application_protocol(
+ req, ALTS_APPLICATION_PROTOCOL);
+ ok &= grpc_gcp_handshaker_req_add_record_protocol(req, ALTS_RECORD_PROTOCOL);
+ grpc_gcp_rpc_protocol_versions* versions = &event->options->rpc_versions;
+ ok &= grpc_gcp_handshaker_req_set_rpc_versions(
+ req, versions->max_rpc_version.major, versions->max_rpc_version.minor,
+ versions->min_rpc_version.major, versions->min_rpc_version.minor);
+ char* target_name = grpc_slice_to_c_string(event->target_name);
+ ok &= grpc_gcp_handshaker_req_set_target_name(req, target_name);
+ target_service_account* ptr =
+ (reinterpret_cast<grpc_alts_credentials_client_options*>(event->options))
+ ->target_account_list_head;
+ while (ptr != nullptr) {
+ grpc_gcp_handshaker_req_add_target_identity_service_account(req, ptr->data);
+ ptr = ptr->next;
+ }
+ grpc_slice slice;
+ ok &= grpc_gcp_handshaker_req_encode(req, &slice);
+ grpc_byte_buffer* buffer = nullptr;
+ if (ok) {
+ buffer = grpc_raw_byte_buffer_create(&slice, 1 /* number of slices */);
+ }
+ grpc_slice_unref(slice);
+ gpr_free(target_name);
+ grpc_gcp_handshaker_req_destroy(req);
+ return buffer;
+}
+
+static tsi_result handshaker_client_start_client(alts_handshaker_client* client,
+ alts_tsi_event* event) {
+ if (client == nullptr || event == nullptr) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to alts_grpc_handshaker_client_start_client()");
+ return TSI_INVALID_ARGUMENT;
+ }
+ grpc_byte_buffer* buffer = get_serialized_start_client(event);
+ if (buffer == nullptr) {
+ gpr_log(GPR_ERROR, "get_serialized_start_client() failed");
+ return TSI_INTERNAL_ERROR;
+ }
+ event->send_buffer = buffer;
+ tsi_result result = make_grpc_call(client, event, true /* is_start */);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "make_grpc_call() failed");
+ }
+ return result;
+}
+
+/* Create and populate a start_server handshaker request, then serialize it. */
+static grpc_byte_buffer* get_serialized_start_server(
+ alts_tsi_event* event, grpc_slice* bytes_received) {
+ GPR_ASSERT(bytes_received != nullptr);
+ grpc_gcp_handshaker_req* req =
+ grpc_gcp_handshaker_req_create(SERVER_START_REQ);
+ bool ok = grpc_gcp_handshaker_req_add_application_protocol(
+ req, ALTS_APPLICATION_PROTOCOL);
+ ok &= grpc_gcp_handshaker_req_param_add_record_protocol(
+ req, grpc_gcp_HandshakeProtocol_ALTS, ALTS_RECORD_PROTOCOL);
+ ok &= grpc_gcp_handshaker_req_set_in_bytes(
+ req, reinterpret_cast<const char*> GRPC_SLICE_START_PTR(*bytes_received),
+ GRPC_SLICE_LENGTH(*bytes_received));
+ grpc_gcp_rpc_protocol_versions* versions = &event->options->rpc_versions;
+ ok &= grpc_gcp_handshaker_req_set_rpc_versions(
+ req, versions->max_rpc_version.major, versions->max_rpc_version.minor,
+ versions->min_rpc_version.major, versions->min_rpc_version.minor);
+ grpc_slice req_slice;
+ ok &= grpc_gcp_handshaker_req_encode(req, &req_slice);
+ grpc_byte_buffer* buffer = nullptr;
+ if (ok) {
+ buffer = grpc_raw_byte_buffer_create(&req_slice, 1 /* number of slices */);
+ }
+ grpc_slice_unref(req_slice);
+ grpc_gcp_handshaker_req_destroy(req);
+ return buffer;
+}
+
+static tsi_result handshaker_client_start_server(alts_handshaker_client* client,
+ alts_tsi_event* event,
+ grpc_slice* bytes_received) {
+ if (client == nullptr || event == nullptr || bytes_received == nullptr) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to alts_grpc_handshaker_client_start_server()");
+ return TSI_INVALID_ARGUMENT;
+ }
+ grpc_byte_buffer* buffer = get_serialized_start_server(event, bytes_received);
+ if (buffer == nullptr) {
+ gpr_log(GPR_ERROR, "get_serialized_start_server() failed");
+ return TSI_INTERNAL_ERROR;
+ }
+ event->send_buffer = buffer;
+ tsi_result result = make_grpc_call(client, event, true /* is_start */);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "make_grpc_call() failed");
+ }
+ return result;
+}
+
+/* Create and populate a next handshaker request, then serialize it. */
+static grpc_byte_buffer* get_serialized_next(grpc_slice* bytes_received) {
+ GPR_ASSERT(bytes_received != nullptr);
+ grpc_gcp_handshaker_req* req = grpc_gcp_handshaker_req_create(NEXT_REQ);
+ bool ok = grpc_gcp_handshaker_req_set_in_bytes(
+ req, reinterpret_cast<const char*> GRPC_SLICE_START_PTR(*bytes_received),
+ GRPC_SLICE_LENGTH(*bytes_received));
+ grpc_slice req_slice;
+ ok &= grpc_gcp_handshaker_req_encode(req, &req_slice);
+ grpc_byte_buffer* buffer = nullptr;
+ if (ok) {
+ buffer = grpc_raw_byte_buffer_create(&req_slice, 1 /* number of slices */);
+ }
+ grpc_slice_unref(req_slice);
+ grpc_gcp_handshaker_req_destroy(req);
+ return buffer;
+}
+
+static tsi_result handshaker_client_next(alts_handshaker_client* client,
+ alts_tsi_event* event,
+ grpc_slice* bytes_received) {
+ if (client == nullptr || event == nullptr || bytes_received == nullptr) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to alts_grpc_handshaker_client_next()");
+ return TSI_INVALID_ARGUMENT;
+ }
+ grpc_byte_buffer* buffer = get_serialized_next(bytes_received);
+ if (buffer == nullptr) {
+ gpr_log(GPR_ERROR, "get_serialized_next() failed");
+ return TSI_INTERNAL_ERROR;
+ }
+ event->send_buffer = buffer;
+ tsi_result result = make_grpc_call(client, event, false /* is_start */);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "make_grpc_call() failed");
+ }
+ return result;
+}
+
+static void handshaker_client_destruct(alts_handshaker_client* client) {
+ if (client == nullptr) {
+ return;
+ }
+ alts_grpc_handshaker_client* grpc_client =
+ reinterpret_cast<alts_grpc_handshaker_client*>(client);
+ grpc_call_unref(grpc_client->call);
+}
+
+static const alts_handshaker_client_vtable vtable = {
+ handshaker_client_start_client, handshaker_client_start_server,
+ handshaker_client_next, handshaker_client_destruct};
+
+alts_handshaker_client* alts_grpc_handshaker_client_create(
+ grpc_channel* channel, grpc_completion_queue* queue,
+ const char* handshaker_service_url) {
+ if (channel == nullptr || queue == nullptr ||
+ handshaker_service_url == nullptr) {
+ gpr_log(GPR_ERROR, "Invalid arguments to alts_handshaker_client_create()");
+ return nullptr;
+ }
+ alts_grpc_handshaker_client* client =
+ static_cast<alts_grpc_handshaker_client*>(gpr_zalloc(sizeof(*client)));
+ client->grpc_caller = grpc_start_batch;
+ grpc_slice slice = grpc_slice_from_copied_string(handshaker_service_url);
+ client->call = grpc_channel_create_call(
+ channel, nullptr, GRPC_PROPAGATE_DEFAULTS, queue,
+ grpc_slice_from_static_string(ALTS_SERVICE_METHOD), &slice,
+ gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
+ client->base.vtable = &vtable;
+ grpc_slice_unref(slice);
+ return &client->base;
+}
+
+namespace grpc_core {
+namespace internal {
+
+void alts_handshaker_client_set_grpc_caller_for_testing(
+ alts_handshaker_client* client, alts_grpc_caller caller) {
+ GPR_ASSERT(client != nullptr && caller != nullptr);
+ alts_grpc_handshaker_client* grpc_client =
+ reinterpret_cast<alts_grpc_handshaker_client*>(client);
+ grpc_client->grpc_caller = caller;
+}
+
+} // namespace internal
+} // namespace grpc_core
+
+tsi_result alts_handshaker_client_start_client(alts_handshaker_client* client,
+ alts_tsi_event* event) {
+ if (client != nullptr && client->vtable != nullptr &&
+ client->vtable->client_start != nullptr) {
+ return client->vtable->client_start(client, event);
+ }
+ gpr_log(GPR_ERROR,
+ "client or client->vtable has not been initialized properly");
+ return TSI_INVALID_ARGUMENT;
+}
+
+tsi_result alts_handshaker_client_start_server(alts_handshaker_client* client,
+ alts_tsi_event* event,
+ grpc_slice* bytes_received) {
+ if (client != nullptr && client->vtable != nullptr &&
+ client->vtable->server_start != nullptr) {
+ return client->vtable->server_start(client, event, bytes_received);
+ }
+ gpr_log(GPR_ERROR,
+ "client or client->vtable has not been initialized properly");
+ return TSI_INVALID_ARGUMENT;
+}
+
+tsi_result alts_handshaker_client_next(alts_handshaker_client* client,
+ alts_tsi_event* event,
+ grpc_slice* bytes_received) {
+ if (client != nullptr && client->vtable != nullptr &&
+ client->vtable->next != nullptr) {
+ return client->vtable->next(client, event, bytes_received);
+ }
+ gpr_log(GPR_ERROR,
+ "client or client->vtable has not been initialized properly");
+ return TSI_INVALID_ARGUMENT;
+}
+
+void alts_handshaker_client_destroy(alts_handshaker_client* client) {
+ if (client != nullptr) {
+ if (client->vtable != nullptr && client->vtable->destruct != nullptr) {
+ client->vtable->destruct(client);
+ }
+ gpr_free(client);
+ }
+}
diff --git a/src/core/tsi/alts/handshaker/alts_handshaker_client.h b/src/core/tsi/alts/handshaker/alts_handshaker_client.h
new file mode 100644
index 0000000000..fb2d2cf68e
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/alts_handshaker_client.h
@@ -0,0 +1,137 @@
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_HANDSHAKER_CLIENT_H
+#define GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_HANDSHAKER_CLIENT_H
+
+#include <grpc/support/port_platform.h>
+
+#include <grpc/grpc.h>
+
+#include "src/core/tsi/alts/handshaker/alts_tsi_event.h"
+
+#define ALTS_SERVICE_METHOD "/grpc.gcp.HandshakerService/DoHandshake"
+#define ALTS_APPLICATION_PROTOCOL "grpc"
+#define ALTS_RECORD_PROTOCOL "ALTSRP_GCM_AES128_REKEY"
+
+const size_t kAltsAes128GcmRekeyKeyLength = 44;
+
+/**
+ * A ALTS handshaker client interface. It is used to communicate with
+ * ALTS handshaker service by scheduling a handshaker request that could be one
+ * of client_start, server_start, and next handshaker requests. All APIs in the
+ * header are thread-compatible.
+ */
+typedef struct alts_handshaker_client alts_handshaker_client;
+
+/* A function that makes the grpc call to the handshaker service. */
+typedef grpc_call_error (*alts_grpc_caller)(grpc_call* call, const grpc_op* ops,
+ size_t nops, void* tag);
+
+/* V-table for ALTS handshaker client operations. */
+typedef struct alts_handshaker_client_vtable {
+ tsi_result (*client_start)(alts_handshaker_client* client,
+ alts_tsi_event* event);
+ tsi_result (*server_start)(alts_handshaker_client* client,
+ alts_tsi_event* event, grpc_slice* bytes_received);
+ tsi_result (*next)(alts_handshaker_client* client, alts_tsi_event* event,
+ grpc_slice* bytes_received);
+ void (*destruct)(alts_handshaker_client* client);
+} alts_handshaker_client_vtable;
+
+struct alts_handshaker_client {
+ const alts_handshaker_client_vtable* vtable;
+};
+
+/**
+ * This method schedules a client_start handshaker request to ALTS handshaker
+ * service.
+ *
+ * - client: ALTS handshaker client instance.
+ * - event: ALTS TSI event instance.
+ *
+ * It returns TSI_OK on success and an error status code on failure.
+ */
+tsi_result alts_handshaker_client_start_client(alts_handshaker_client* client,
+ alts_tsi_event* event);
+
+/**
+ * This method schedules a server_start handshaker request to ALTS handshaker
+ * service.
+ *
+ * - client: ALTS handshaker client instance.
+ * - event: ALTS TSI event instance.
+ * - bytes_received: bytes in out_frames returned from the peer's handshaker
+ * response.
+ *
+ * It returns TSI_OK on success and an error status code on failure.
+ */
+tsi_result alts_handshaker_client_start_server(alts_handshaker_client* client,
+ alts_tsi_event* event,
+ grpc_slice* bytes_received);
+
+/**
+ * This method schedules a next handshaker request to ALTS handshaker service.
+ *
+ * - client: ALTS handshaker client instance.
+ * - event: ALTS TSI event instance.
+ * - bytes_received: bytes in out_frames returned from the peer's handshaker
+ * response.
+ *
+ * It returns TSI_OK on success and an error status code on failure.
+ */
+tsi_result alts_handshaker_client_next(alts_handshaker_client* client,
+ alts_tsi_event* event,
+ grpc_slice* bytes_received);
+
+/**
+ * This method destroys a ALTS handshaker client.
+ *
+ * - client: a ALTS handshaker client instance.
+ */
+void alts_handshaker_client_destroy(alts_handshaker_client* client);
+
+/**
+ * This method creates a ALTS handshaker client.
+ *
+ * - channel: grpc channel to ALTS handshaker service.
+ * - queue: grpc completion queue.
+ * - handshaker_service_url: address of ALTS handshaker service in the format of
+ * "host:port".
+ *
+ * It returns the created ALTS handshaker client on success, and NULL on
+ * failure.
+ */
+alts_handshaker_client* alts_grpc_handshaker_client_create(
+ grpc_channel* channel, grpc_completion_queue* queue,
+ const char* handshaker_service_url);
+
+namespace grpc_core {
+namespace internal {
+
+/**
+ * Unsafe, use for testing only. It allows the caller to change the way that
+ * GRPC calls are made to the handshaker service.
+ */
+void alts_handshaker_client_set_grpc_caller_for_testing(
+ alts_handshaker_client* client, alts_grpc_caller caller);
+
+} // namespace internal
+} // namespace grpc_core
+
+#endif /* GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_HANDSHAKER_CLIENT_H */
diff --git a/src/core/tsi/alts/handshaker/alts_handshaker_service_api.cc b/src/core/tsi/alts/handshaker/alts_handshaker_service_api.cc
new file mode 100644
index 0000000000..256e414ae4
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/alts_handshaker_service_api.cc
@@ -0,0 +1,520 @@
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <grpc/support/port_platform.h>
+
+#include "src/core/tsi/alts/handshaker/alts_handshaker_service_api.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "src/core/tsi/alts/handshaker/transport_security_common_api.h"
+
+/* HandshakerReq */
+grpc_gcp_handshaker_req* grpc_gcp_handshaker_req_create(
+ grpc_gcp_handshaker_req_type type) {
+ grpc_gcp_handshaker_req* req =
+ static_cast<grpc_gcp_handshaker_req*>(gpr_zalloc(sizeof(*req)));
+ switch (type) {
+ case CLIENT_START_REQ:
+ req->has_client_start = true;
+ break;
+ case SERVER_START_REQ:
+ req->has_server_start = true;
+ break;
+ case NEXT_REQ:
+ req->has_next = true;
+ break;
+ }
+ return req;
+}
+
+void grpc_gcp_handshaker_req_destroy(grpc_gcp_handshaker_req* req) {
+ if (req == nullptr) {
+ return;
+ }
+ if (req->has_client_start) {
+ /* Destroy client_start request. */
+ destroy_repeated_field_list_identity(
+ static_cast<repeated_field*>(req->client_start.target_identities.arg));
+ destroy_repeated_field_list_string(static_cast<repeated_field*>(
+ req->client_start.application_protocols.arg));
+ destroy_repeated_field_list_string(
+ static_cast<repeated_field*>(req->client_start.record_protocols.arg));
+ if (req->client_start.has_local_identity) {
+ destroy_slice(static_cast<grpc_slice*>(
+ req->client_start.local_identity.hostname.arg));
+ destroy_slice(static_cast<grpc_slice*>(
+ req->client_start.local_identity.service_account.arg));
+ }
+ if (req->client_start.has_local_endpoint) {
+ destroy_slice(static_cast<grpc_slice*>(
+ req->client_start.local_endpoint.ip_address.arg));
+ }
+ if (req->client_start.has_remote_endpoint) {
+ destroy_slice(static_cast<grpc_slice*>(
+ req->client_start.remote_endpoint.ip_address.arg));
+ }
+ destroy_slice(static_cast<grpc_slice*>(req->client_start.target_name.arg));
+ } else if (req->has_server_start) {
+ /* Destroy server_start request. */
+ size_t i = 0;
+ for (i = 0; i < req->server_start.handshake_parameters_count; i++) {
+ destroy_repeated_field_list_identity(
+ static_cast<repeated_field*>(req->server_start.handshake_parameters[i]
+ .value.local_identities.arg));
+ destroy_repeated_field_list_string(
+ static_cast<repeated_field*>(req->server_start.handshake_parameters[i]
+ .value.record_protocols.arg));
+ }
+ destroy_repeated_field_list_string(static_cast<repeated_field*>(
+ req->server_start.application_protocols.arg));
+ if (req->server_start.has_local_endpoint) {
+ destroy_slice(static_cast<grpc_slice*>(
+ req->server_start.local_endpoint.ip_address.arg));
+ }
+ if (req->server_start.has_remote_endpoint) {
+ destroy_slice(static_cast<grpc_slice*>(
+ req->server_start.remote_endpoint.ip_address.arg));
+ }
+ destroy_slice(static_cast<grpc_slice*>(req->server_start.in_bytes.arg));
+ } else {
+ /* Destroy next request. */
+ destroy_slice(static_cast<grpc_slice*>(req->next.in_bytes.arg));
+ }
+ gpr_free(req);
+}
+
+bool grpc_gcp_handshaker_req_set_handshake_protocol(
+ grpc_gcp_handshaker_req* req,
+ grpc_gcp_handshake_protocol handshake_protocol) {
+ if (req == nullptr || !req->has_client_start) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to "
+ "grpc_gcp_handshaker_req_set_handshake_protocol().");
+ return false;
+ }
+ req->client_start.has_handshake_security_protocol = true;
+ req->client_start.handshake_security_protocol = handshake_protocol;
+ return true;
+}
+
+bool grpc_gcp_handshaker_req_set_target_name(grpc_gcp_handshaker_req* req,
+ const char* target_name) {
+ if (req == nullptr || target_name == nullptr || !req->has_client_start) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to "
+ "grpc_gcp_handshaker_req_set_target_name().");
+ return false;
+ }
+ grpc_slice* slice = create_slice(target_name, strlen(target_name));
+ req->client_start.target_name.arg = slice;
+ req->client_start.target_name.funcs.encode = encode_string_or_bytes_cb;
+ return true;
+}
+
+bool grpc_gcp_handshaker_req_add_application_protocol(
+ grpc_gcp_handshaker_req* req, const char* application_protocol) {
+ if (req == nullptr || application_protocol == nullptr || req->has_next) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to "
+ "grpc_gcp_handshaker_req_add_application_protocol().");
+ return false;
+ }
+ grpc_slice* slice =
+ create_slice(application_protocol, strlen(application_protocol));
+ if (req->has_client_start) {
+ add_repeated_field(reinterpret_cast<repeated_field**>(
+ &req->client_start.application_protocols.arg),
+ slice);
+ req->client_start.application_protocols.funcs.encode =
+ encode_repeated_string_cb;
+ } else {
+ add_repeated_field(reinterpret_cast<repeated_field**>(
+ &req->server_start.application_protocols.arg),
+ slice);
+ req->server_start.application_protocols.funcs.encode =
+ encode_repeated_string_cb;
+ }
+ return true;
+}
+
+bool grpc_gcp_handshaker_req_add_record_protocol(grpc_gcp_handshaker_req* req,
+ const char* record_protocol) {
+ if (req == nullptr || record_protocol == nullptr || !req->has_client_start) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to "
+ "grpc_gcp_handshaker_req_add_record_protocol().");
+ return false;
+ }
+ grpc_slice* slice = create_slice(record_protocol, strlen(record_protocol));
+ add_repeated_field(reinterpret_cast<repeated_field**>(
+ &req->client_start.record_protocols.arg),
+ slice);
+ req->client_start.record_protocols.funcs.encode = encode_repeated_string_cb;
+ return true;
+}
+
+static void set_identity_hostname(grpc_gcp_identity* identity,
+ const char* hostname) {
+ grpc_slice* slice = create_slice(hostname, strlen(hostname));
+ identity->hostname.arg = slice;
+ identity->hostname.funcs.encode = encode_string_or_bytes_cb;
+}
+
+static void set_identity_service_account(grpc_gcp_identity* identity,
+ const char* service_account) {
+ grpc_slice* slice = create_slice(service_account, strlen(service_account));
+ identity->service_account.arg = slice;
+ identity->service_account.funcs.encode = encode_string_or_bytes_cb;
+}
+
+bool grpc_gcp_handshaker_req_add_target_identity_hostname(
+ grpc_gcp_handshaker_req* req, const char* hostname) {
+ if (req == nullptr || hostname == nullptr || !req->has_client_start) {
+ gpr_log(GPR_ERROR,
+ "Invalid nullptr arguments to "
+ "grpc_gcp_handshaker_req_add_target_identity_hostname().");
+ return false;
+ }
+ grpc_gcp_identity* target_identity =
+ static_cast<grpc_gcp_identity*>(gpr_zalloc(sizeof(*target_identity)));
+ set_identity_hostname(target_identity, hostname);
+ req->client_start.target_identities.funcs.encode =
+ encode_repeated_identity_cb;
+ add_repeated_field(reinterpret_cast<repeated_field**>(
+ &req->client_start.target_identities.arg),
+ target_identity);
+ return true;
+}
+
+bool grpc_gcp_handshaker_req_add_target_identity_service_account(
+ grpc_gcp_handshaker_req* req, const char* service_account) {
+ if (req == nullptr || service_account == nullptr || !req->has_client_start) {
+ gpr_log(GPR_ERROR,
+ "Invalid nullptr arguments to "
+ "grpc_gcp_handshaker_req_add_target_identity_service_account().");
+ return false;
+ }
+ grpc_gcp_identity* target_identity =
+ static_cast<grpc_gcp_identity*>(gpr_zalloc(sizeof(*target_identity)));
+ set_identity_service_account(target_identity, service_account);
+ req->client_start.target_identities.funcs.encode =
+ encode_repeated_identity_cb;
+ add_repeated_field(reinterpret_cast<repeated_field**>(
+ &req->client_start.target_identities.arg),
+ target_identity);
+ return true;
+}
+
+bool grpc_gcp_handshaker_req_set_local_identity_hostname(
+ grpc_gcp_handshaker_req* req, const char* hostname) {
+ if (req == nullptr || hostname == nullptr || !req->has_client_start) {
+ gpr_log(GPR_ERROR,
+ "Invalid nullptr arguments to "
+ "grpc_gcp_handshaker_req_set_local_identity_hostname().");
+ return false;
+ }
+ req->client_start.has_local_identity = true;
+ set_identity_hostname(&req->client_start.local_identity, hostname);
+ return true;
+}
+
+bool grpc_gcp_handshaker_req_set_local_identity_service_account(
+ grpc_gcp_handshaker_req* req, const char* service_account) {
+ if (req == nullptr || service_account == nullptr || !req->has_client_start) {
+ gpr_log(GPR_ERROR,
+ "Invalid nullptr arguments to "
+ "grpc_gcp_handshaker_req_set_local_identity_service_account().");
+ return false;
+ }
+ req->client_start.has_local_identity = true;
+ set_identity_service_account(&req->client_start.local_identity,
+ service_account);
+ return true;
+}
+
+static void set_endpoint(grpc_gcp_endpoint* endpoint, const char* ip_address,
+ size_t port, grpc_gcp_network_protocol protocol) {
+ grpc_slice* slice = create_slice(ip_address, strlen(ip_address));
+ endpoint->ip_address.arg = slice;
+ endpoint->ip_address.funcs.encode = encode_string_or_bytes_cb;
+ endpoint->has_port = true;
+ endpoint->port = static_cast<int32_t>(port);
+ endpoint->has_protocol = true;
+ endpoint->protocol = protocol;
+}
+
+bool grpc_gcp_handshaker_req_set_rpc_versions(grpc_gcp_handshaker_req* req,
+ uint32_t max_major,
+ uint32_t max_minor,
+ uint32_t min_major,
+ uint32_t min_minor) {
+ if (req == nullptr || req->has_next) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to "
+ "grpc_gcp_handshaker_req_set_rpc_versions().");
+ return false;
+ }
+ if (req->has_client_start) {
+ req->client_start.has_rpc_versions = true;
+ grpc_gcp_rpc_protocol_versions_set_max(&req->client_start.rpc_versions,
+ max_major, max_minor);
+ grpc_gcp_rpc_protocol_versions_set_min(&req->client_start.rpc_versions,
+ min_major, min_minor);
+ } else {
+ req->server_start.has_rpc_versions = true;
+ grpc_gcp_rpc_protocol_versions_set_max(&req->server_start.rpc_versions,
+ max_major, max_minor);
+ grpc_gcp_rpc_protocol_versions_set_min(&req->server_start.rpc_versions,
+ min_major, min_minor);
+ }
+ return true;
+}
+
+bool grpc_gcp_handshaker_req_set_local_endpoint(
+ grpc_gcp_handshaker_req* req, const char* ip_address, size_t port,
+ grpc_gcp_network_protocol protocol) {
+ if (req == nullptr || ip_address == nullptr || port > 65535 ||
+ req->has_next) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to "
+ "grpc_gcp_handshaker_req_set_local_endpoint().");
+ return false;
+ }
+ if (req->has_client_start) {
+ req->client_start.has_local_endpoint = true;
+ set_endpoint(&req->client_start.local_endpoint, ip_address, port, protocol);
+ } else {
+ req->server_start.has_local_endpoint = true;
+ set_endpoint(&req->server_start.local_endpoint, ip_address, port, protocol);
+ }
+ return true;
+}
+
+bool grpc_gcp_handshaker_req_set_remote_endpoint(
+ grpc_gcp_handshaker_req* req, const char* ip_address, size_t port,
+ grpc_gcp_network_protocol protocol) {
+ if (req == nullptr || ip_address == nullptr || port > 65535 ||
+ req->has_next) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to "
+ "grpc_gcp_handshaker_req_set_remote_endpoint().");
+ return false;
+ }
+ if (req->has_client_start) {
+ req->client_start.has_remote_endpoint = true;
+ set_endpoint(&req->client_start.remote_endpoint, ip_address, port,
+ protocol);
+ } else {
+ req->server_start.has_remote_endpoint = true;
+ set_endpoint(&req->server_start.remote_endpoint, ip_address, port,
+ protocol);
+ }
+ return true;
+}
+
+bool grpc_gcp_handshaker_req_set_in_bytes(grpc_gcp_handshaker_req* req,
+ const char* in_bytes, size_t size) {
+ if (req == nullptr || in_bytes == nullptr || req->has_client_start) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to "
+ "grpc_gcp_handshaker_req_set_in_bytes().");
+ return false;
+ }
+ grpc_slice* slice = create_slice(in_bytes, size);
+ if (req->has_next) {
+ req->next.in_bytes.arg = slice;
+ req->next.in_bytes.funcs.encode = &encode_string_or_bytes_cb;
+ } else {
+ req->server_start.in_bytes.arg = slice;
+ req->server_start.in_bytes.funcs.encode = &encode_string_or_bytes_cb;
+ }
+ return true;
+}
+
+static grpc_gcp_server_handshake_parameters* server_start_find_param(
+ grpc_gcp_handshaker_req* req, int32_t key) {
+ size_t i = 0;
+ for (i = 0; i < req->server_start.handshake_parameters_count; i++) {
+ if (req->server_start.handshake_parameters[i].key == key) {
+ return &req->server_start.handshake_parameters[i].value;
+ }
+ }
+ req->server_start
+ .handshake_parameters[req->server_start.handshake_parameters_count]
+ .has_key = true;
+ req->server_start
+ .handshake_parameters[req->server_start.handshake_parameters_count]
+ .has_value = true;
+ req->server_start
+ .handshake_parameters[req->server_start.handshake_parameters_count++]
+ .key = key;
+ return &req->server_start
+ .handshake_parameters
+ [req->server_start.handshake_parameters_count - 1]
+ .value;
+}
+
+bool grpc_gcp_handshaker_req_param_add_record_protocol(
+ grpc_gcp_handshaker_req* req, grpc_gcp_handshake_protocol key,
+ const char* record_protocol) {
+ if (req == nullptr || record_protocol == nullptr || !req->has_server_start) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to "
+ "grpc_gcp_handshaker_req_param_add_record_protocol().");
+ return false;
+ }
+ grpc_gcp_server_handshake_parameters* param =
+ server_start_find_param(req, key);
+ grpc_slice* slice = create_slice(record_protocol, strlen(record_protocol));
+ add_repeated_field(
+ reinterpret_cast<repeated_field**>(&param->record_protocols.arg), slice);
+ param->record_protocols.funcs.encode = &encode_repeated_string_cb;
+ return true;
+}
+
+bool grpc_gcp_handshaker_req_param_add_local_identity_hostname(
+ grpc_gcp_handshaker_req* req, grpc_gcp_handshake_protocol key,
+ const char* hostname) {
+ if (req == nullptr || hostname == nullptr || !req->has_server_start) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to "
+ "grpc_gcp_handshaker_req_param_add_local_identity_hostname().");
+ return false;
+ }
+ grpc_gcp_server_handshake_parameters* param =
+ server_start_find_param(req, key);
+ grpc_gcp_identity* local_identity =
+ static_cast<grpc_gcp_identity*>(gpr_zalloc(sizeof(*local_identity)));
+ set_identity_hostname(local_identity, hostname);
+ add_repeated_field(
+ reinterpret_cast<repeated_field**>(&param->local_identities.arg),
+ local_identity);
+ param->local_identities.funcs.encode = &encode_repeated_identity_cb;
+ return true;
+}
+
+bool grpc_gcp_handshaker_req_param_add_local_identity_service_account(
+ grpc_gcp_handshaker_req* req, grpc_gcp_handshake_protocol key,
+ const char* service_account) {
+ if (req == nullptr || service_account == nullptr || !req->has_server_start) {
+ gpr_log(
+ GPR_ERROR,
+ "Invalid arguments to "
+ "grpc_gcp_handshaker_req_param_add_local_identity_service_account().");
+ return false;
+ }
+ grpc_gcp_server_handshake_parameters* param =
+ server_start_find_param(req, key);
+ grpc_gcp_identity* local_identity =
+ static_cast<grpc_gcp_identity*>(gpr_zalloc(sizeof(*local_identity)));
+ set_identity_service_account(local_identity, service_account);
+ add_repeated_field(
+ reinterpret_cast<repeated_field**>(&param->local_identities.arg),
+ local_identity);
+ param->local_identities.funcs.encode = &encode_repeated_identity_cb;
+ return true;
+}
+
+bool grpc_gcp_handshaker_req_encode(grpc_gcp_handshaker_req* req,
+ grpc_slice* slice) {
+ if (req == nullptr || slice == nullptr) {
+ gpr_log(GPR_ERROR,
+ "Invalid nullptr arguments to grpc_gcp_handshaker_req_encode().");
+ return false;
+ }
+ pb_ostream_t size_stream;
+ memset(&size_stream, 0, sizeof(pb_ostream_t));
+ if (!pb_encode(&size_stream, grpc_gcp_HandshakerReq_fields, req)) {
+ gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&size_stream));
+ return false;
+ }
+ size_t encoded_length = size_stream.bytes_written;
+ *slice = grpc_slice_malloc(encoded_length);
+ pb_ostream_t output_stream =
+ pb_ostream_from_buffer(GRPC_SLICE_START_PTR(*slice), encoded_length);
+ if (!pb_encode(&output_stream, grpc_gcp_HandshakerReq_fields, req) != 0) {
+ gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&output_stream));
+ return false;
+ }
+ return true;
+}
+
+/* HandshakerResp. */
+grpc_gcp_handshaker_resp* grpc_gcp_handshaker_resp_create(void) {
+ grpc_gcp_handshaker_resp* resp =
+ static_cast<grpc_gcp_handshaker_resp*>(gpr_zalloc(sizeof(*resp)));
+ return resp;
+}
+
+void grpc_gcp_handshaker_resp_destroy(grpc_gcp_handshaker_resp* resp) {
+ if (resp != nullptr) {
+ destroy_slice(static_cast<grpc_slice*>(resp->out_frames.arg));
+ if (resp->has_status) {
+ destroy_slice(static_cast<grpc_slice*>(resp->status.details.arg));
+ }
+ if (resp->has_result) {
+ destroy_slice(
+ static_cast<grpc_slice*>(resp->result.application_protocol.arg));
+ destroy_slice(static_cast<grpc_slice*>(resp->result.record_protocol.arg));
+ destroy_slice(static_cast<grpc_slice*>(resp->result.key_data.arg));
+ if (resp->result.has_local_identity) {
+ destroy_slice(
+ static_cast<grpc_slice*>(resp->result.local_identity.hostname.arg));
+ destroy_slice(static_cast<grpc_slice*>(
+ resp->result.local_identity.service_account.arg));
+ }
+ if (resp->result.has_peer_identity) {
+ destroy_slice(
+ static_cast<grpc_slice*>(resp->result.peer_identity.hostname.arg));
+ destroy_slice(static_cast<grpc_slice*>(
+ resp->result.peer_identity.service_account.arg));
+ }
+ }
+ gpr_free(resp);
+ }
+}
+
+bool grpc_gcp_handshaker_resp_decode(grpc_slice encoded_handshaker_resp,
+ grpc_gcp_handshaker_resp* resp) {
+ if (resp == nullptr) {
+ gpr_log(GPR_ERROR,
+ "Invalid nullptr argument to grpc_gcp_handshaker_resp_decode().");
+ return false;
+ }
+ pb_istream_t stream =
+ pb_istream_from_buffer(GRPC_SLICE_START_PTR(encoded_handshaker_resp),
+ GRPC_SLICE_LENGTH(encoded_handshaker_resp));
+ resp->out_frames.funcs.decode = decode_string_or_bytes_cb;
+ resp->status.details.funcs.decode = decode_string_or_bytes_cb;
+ resp->result.application_protocol.funcs.decode = decode_string_or_bytes_cb;
+ resp->result.record_protocol.funcs.decode = decode_string_or_bytes_cb;
+ resp->result.key_data.funcs.decode = decode_string_or_bytes_cb;
+ resp->result.peer_identity.hostname.funcs.decode = decode_string_or_bytes_cb;
+ resp->result.peer_identity.service_account.funcs.decode =
+ decode_string_or_bytes_cb;
+ resp->result.local_identity.hostname.funcs.decode = decode_string_or_bytes_cb;
+ resp->result.local_identity.service_account.funcs.decode =
+ decode_string_or_bytes_cb;
+ if (!pb_decode(&stream, grpc_gcp_HandshakerResp_fields, resp)) {
+ gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream));
+ return false;
+ }
+ return true;
+}
diff --git a/src/core/tsi/alts/handshaker/alts_handshaker_service_api.h b/src/core/tsi/alts/handshaker/alts_handshaker_service_api.h
new file mode 100644
index 0000000000..5df56a86fa
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/alts_handshaker_service_api.h
@@ -0,0 +1,323 @@
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_HANDSHAKER_SERVICE_API_H
+#define GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_HANDSHAKER_SERVICE_API_H
+
+#include <grpc/support/port_platform.h>
+
+#include "src/core/tsi/alts/handshaker/alts_handshaker_service_api_util.h"
+
+/**
+ * An implementation of nanopb thin wrapper used to set/get and
+ * serialize/de-serialize of ALTS handshake requests and responses.
+ *
+ * All APIs in the header are thread-compatible. A typical usage of this API at
+ * the client side is as follows:
+ *
+ * -----------------------------------------------------------------------------
+ * // Create, populate, and serialize an ALTS client_start handshake request to
+ * // send to the server.
+ * grpc_gcp_handshaker_req* req =
+ * grpc_gcp_handshaker_req_create(CLIENT_START_REQ);
+ * grpc_gcp_handshaker_req_set_handshake_protocol(
+ req, grpc_gcp_HandshakeProtocol_ALTS);
+ * grpc_gcp_handshaker_req_add_application_protocol(req, "grpc");
+ * grpc_gcp_handshaker_req_add_record_protocol(req, "ALTSRP_GCM_AES128");
+ * grpc_slice client_slice;
+ * if (!grpc_gcp_handshaker_req_encode(req, &client_slice)) {
+ * fprintf(stderr, "ALTS handshake request encoding failed.";
+ * }
+ *
+ * // De-serialize a data stream received from the server, and store the result
+ * // at ALTS handshake response.
+ * grpc_gcp_handshaker_resp* resp = grpc_gcp_handshaker_resp_create();
+ * if (!grpc_gcp_handshaker_resp_decode(server_slice, resp)) {
+ * fprintf(stderr, "ALTS handshake response decoding failed.");
+ * }
+ * // To access a variable-length datatype field (i.e., pb_callback_t),
+ * // access its "arg" subfield (if it has been set).
+ * if (resp->out_frames.arg != nullptr) {
+ * grpc_slice* slice = resp->out_frames.arg;
+ * }
+ * // To access a fixed-length datatype field (i.e., not pb_calback_t),
+ * // access the field directly (if it has been set).
+ * if (resp->has_status && resp->status->has_code) {
+ * uint32_t code = resp->status->code;
+ * }
+ *------------------------------------------------------------------------------
+ */
+
+/**
+ * This method creates an ALTS handshake request.
+ *
+ * - type: an enum type value that can be either CLIENT_START_REQ,
+ * SERVER_START_REQ, or NEXT_REQ to indicate the created instance will be
+ * client_start, server_start, and next handshake request message
+ * respectively.
+ *
+ * The method returns a pointer to the created instance.
+ */
+grpc_gcp_handshaker_req* grpc_gcp_handshaker_req_create(
+ grpc_gcp_handshaker_req_type type);
+
+/**
+ * This method sets the value for handshake_security_protocol field of ALTS
+ * client_start handshake request.
+ *
+ * - req: an ALTS handshake request.
+ * - handshake_protocol: a enum type value representing the handshake security
+ * protocol.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_req_set_handshake_protocol(
+ grpc_gcp_handshaker_req* req,
+ grpc_gcp_handshake_protocol handshake_protocol);
+
+/**
+ * This method sets the value for target_name field of ALTS client_start
+ * handshake request.
+ *
+ * - req: an ALTS handshake request.
+ * - target_name: a target name to be set.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_req_set_target_name(grpc_gcp_handshaker_req* req,
+ const char* target_name);
+
+/**
+ * This method adds an application protocol supported by the server (or
+ * client) to ALTS server_start (or client_start) handshake request.
+ *
+ * - req: an ALTS handshake request.
+ * - application_protocol: an application protocol (e.g., grpc) to be added.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_req_add_application_protocol(
+ grpc_gcp_handshaker_req* req, const char* application_protocol);
+
+/**
+ * This method adds a record protocol supported by the client to ALTS
+ * client_start handshake request.
+ *
+ * - req: an ALTS handshake request.
+ * - record_protocol: a record protocol (e.g., ALTSRP_GCM_AES128) to be
+ * added.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_req_add_record_protocol(grpc_gcp_handshaker_req* req,
+ const char* record_protocol);
+
+/**
+ * This method adds a target server identity represented as hostname and
+ * acceptable by a client to ALTS client_start handshake request.
+ *
+ * - req: an ALTS handshake request.
+ * - hostname: a string representation of hostname at the connection
+ * endpoint to be added.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_req_add_target_identity_hostname(
+ grpc_gcp_handshaker_req* req, const char* hostname);
+
+/**
+ * This method adds a target server identity represented as service account and
+ * acceptable by a client to ALTS client_start handshake request.
+ *
+ * - req: an ALTS handshake request.
+ * - service_account: a string representation of service account at the
+ * connection endpoint to be added.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_req_add_target_identity_service_account(
+ grpc_gcp_handshaker_req* req, const char* service_account);
+
+/**
+ * This method sets the hostname for local_identity field of ALTS client_start
+ * handshake request.
+ *
+ * - req: an ALTS handshake request.
+ * - hostname: a string representation of hostname.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_req_set_local_identity_hostname(
+ grpc_gcp_handshaker_req* req, const char* hostname);
+
+/**
+ * This method sets the service account for local_identity field of ALTS
+ * client_start handshake request.
+ *
+ * - req: an ALTS handshake request.
+ * - service_account: a string representation of service account.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_req_set_local_identity_service_account(
+ grpc_gcp_handshaker_req* req, const char* service_account);
+
+/**
+ * This method sets the value for local_endpoint field of either ALTS
+ * client_start or server_start handshake request.
+ *
+ * - req: an ALTS handshake request.
+ * - ip_address: a string representation of ip address associated with the
+ * local endpoint, that could be either IPv4 or IPv6.
+ * - port: a port number associated with the local endpoint.
+ * - protocol: a network protocol (e.g., TCP or UDP) associated with the
+ * local endpoint.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_req_set_local_endpoint(
+ grpc_gcp_handshaker_req* req, const char* ip_address, size_t port,
+ grpc_gcp_network_protocol protocol);
+
+/**
+ * This method sets the value for remote_endpoint field of either ALTS
+ * client_start or server_start handshake request.
+ *
+ * - req: an ALTS handshake request.
+ * - ip_address: a string representation of ip address associated with the
+ * remote endpoint, that could be either IPv4 or IPv6.
+ * - port: a port number associated with the remote endpoint.
+ * - protocol: a network protocol (e.g., TCP or UDP) associated with the
+ * remote endpoint.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_req_set_remote_endpoint(
+ grpc_gcp_handshaker_req* req, const char* ip_address, size_t port,
+ grpc_gcp_network_protocol protocol);
+
+/**
+ * This method sets the value for in_bytes field of either ALTS server_start or
+ * next handshake request.
+ *
+ * - req: an ALTS handshake request.
+ * - in_bytes: a buffer containing bytes taken from out_frames of the peer's
+ * ALTS handshake response. It is possible that the peer's out_frames are
+ * split into multiple handshake request messages.
+ * - size: size of in_bytes buffer.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_req_set_in_bytes(grpc_gcp_handshaker_req* req,
+ const char* in_bytes, size_t size);
+
+/**
+ * This method adds a record protocol to handshake parameters mapped by the
+ * handshake protocol for ALTS server_start handshake request.
+ *
+ * - req: an ALTS handshake request.
+ * - key: an enum type value representing a handshake security protocol.
+ * - record_protocol: a record protocol to be added.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_req_param_add_record_protocol(
+ grpc_gcp_handshaker_req* req, grpc_gcp_handshake_protocol key,
+ const char* record_protocol);
+
+/**
+ * This method adds a local identity represented as hostname to handshake
+ * parameters mapped by the handshake protocol for ALTS server_start handshake
+ * request.
+ *
+ * - req: an ALTS handshake request.
+ * - key: an enum type value representing a handshake security protocol.
+ * - hostname: a string representation of hostname to be added.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_req_param_add_local_identity_hostname(
+ grpc_gcp_handshaker_req* req, grpc_gcp_handshake_protocol key,
+ const char* hostname);
+
+/**
+ * This method adds a local identity represented as service account to handshake
+ * parameters mapped by the handshake protocol for ALTS server_start handshake
+ * request.
+ *
+ * - req: an ALTS handshake request.
+ * - key: an enum type value representing a handshake security protocol.
+ * - service_account: a string representation of service account to be added.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_req_param_add_local_identity_service_account(
+ grpc_gcp_handshaker_req* req, grpc_gcp_handshake_protocol key,
+ const char* service_account);
+
+/**
+ * This method sets the value for rpc_versions field of either ALTS
+ * client_start or server_start handshake request.
+ *
+ * - req: an ALTS handshake request.
+ * - max_major: a major version of maximum supported RPC version.
+ * - max_minor: a minor version of maximum supported RPC version.
+ * - min_major: a major version of minimum supported RPC version.
+ * - min_minor: a minor version of minimum supported RPC version.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_req_set_rpc_versions(grpc_gcp_handshaker_req* req,
+ uint32_t max_major,
+ uint32_t max_minor,
+ uint32_t min_major,
+ uint32_t min_minor);
+
+/**
+ * This method serializes an ALTS handshake request and returns a data stream.
+ *
+ * - req: an ALTS handshake request.
+ * - slice: a data stream where the serialized result will be written.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_req_encode(grpc_gcp_handshaker_req* req,
+ grpc_slice* slice);
+
+/* This method destroys an ALTS handshake request. */
+void grpc_gcp_handshaker_req_destroy(grpc_gcp_handshaker_req* req);
+
+/* This method creates an ALTS handshake response. */
+grpc_gcp_handshaker_resp* grpc_gcp_handshaker_resp_create(void);
+
+/**
+ * This method de-serializes a data stream and stores the result
+ * in an ALTS handshake response.
+ *
+ * - slice: a data stream containing a serialized ALTS handshake response.
+ * - resp: an ALTS handshake response used to hold de-serialized result.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_handshaker_resp_decode(grpc_slice slice,
+ grpc_gcp_handshaker_resp* resp);
+
+/* This method destroys an ALTS handshake response. */
+void grpc_gcp_handshaker_resp_destroy(grpc_gcp_handshaker_resp* resp);
+
+#endif /* GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_HANDSHAKER_SERVICE_API_H */
diff --git a/src/core/tsi/alts/handshaker/alts_handshaker_service_api_util.cc b/src/core/tsi/alts/handshaker/alts_handshaker_service_api_util.cc
new file mode 100644
index 0000000000..e0e4184686
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/alts_handshaker_service_api_util.cc
@@ -0,0 +1,143 @@
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <grpc/support/port_platform.h>
+
+#include "src/core/tsi/alts/handshaker/alts_handshaker_service_api_util.h"
+
+void add_repeated_field(repeated_field** head, const void* data) {
+ repeated_field* field =
+ static_cast<repeated_field*>(gpr_zalloc(sizeof(*field)));
+ field->data = data;
+ if (*head == nullptr) {
+ *head = field;
+ (*head)->next = nullptr;
+ } else {
+ field->next = *head;
+ *head = field;
+ }
+}
+
+void destroy_repeated_field_list_identity(repeated_field* head) {
+ repeated_field* field = head;
+ while (field != nullptr) {
+ repeated_field* next_field = field->next;
+ const grpc_gcp_identity* identity =
+ static_cast<const grpc_gcp_identity*>(field->data);
+ destroy_slice(static_cast<grpc_slice*>(identity->hostname.arg));
+ destroy_slice(static_cast<grpc_slice*>(identity->service_account.arg));
+ gpr_free((void*)identity);
+ gpr_free(field);
+ field = next_field;
+ }
+}
+
+void destroy_repeated_field_list_string(repeated_field* head) {
+ repeated_field* field = head;
+ while (field != nullptr) {
+ repeated_field* next_field = field->next;
+ destroy_slice((grpc_slice*)field->data);
+ gpr_free(field);
+ field = next_field;
+ }
+}
+
+grpc_slice* create_slice(const char* data, size_t size) {
+ grpc_slice slice = grpc_slice_from_copied_buffer(data, size);
+ grpc_slice* cb_slice =
+ static_cast<grpc_slice*>(gpr_zalloc(sizeof(*cb_slice)));
+ memcpy(cb_slice, &slice, sizeof(*cb_slice));
+ return cb_slice;
+}
+
+void destroy_slice(grpc_slice* slice) {
+ if (slice != nullptr) {
+ grpc_slice_unref(*slice);
+ gpr_free(slice);
+ }
+}
+
+bool encode_string_or_bytes_cb(pb_ostream_t* stream, const pb_field_t* field,
+ void* const* arg) {
+ grpc_slice* slice = static_cast<grpc_slice*>(*arg);
+ if (!pb_encode_tag_for_field(stream, field)) return false;
+ return pb_encode_string(stream, GRPC_SLICE_START_PTR(*slice),
+ GRPC_SLICE_LENGTH(*slice));
+}
+
+bool encode_repeated_identity_cb(pb_ostream_t* stream, const pb_field_t* field,
+ void* const* arg) {
+ repeated_field* var = static_cast<repeated_field*>(*arg);
+ while (var != nullptr) {
+ if (!pb_encode_tag_for_field(stream, field)) return false;
+ if (!pb_encode_submessage(stream, grpc_gcp_Identity_fields,
+ (grpc_gcp_identity*)var->data))
+ return false;
+ var = var->next;
+ }
+ return true;
+}
+
+bool encode_repeated_string_cb(pb_ostream_t* stream, const pb_field_t* field,
+ void* const* arg) {
+ repeated_field* var = static_cast<repeated_field*>(*arg);
+ while (var != nullptr) {
+ if (!pb_encode_tag_for_field(stream, field)) return false;
+ const grpc_slice* slice = static_cast<const grpc_slice*>(var->data);
+ if (!pb_encode_string(stream, GRPC_SLICE_START_PTR(*slice),
+ GRPC_SLICE_LENGTH(*slice)))
+ return false;
+ var = var->next;
+ }
+ return true;
+}
+
+bool decode_string_or_bytes_cb(pb_istream_t* stream, const pb_field_t* field,
+ void** arg) {
+ grpc_slice slice = grpc_slice_malloc(stream->bytes_left);
+ grpc_slice* cb_slice =
+ static_cast<grpc_slice*>(gpr_zalloc(sizeof(*cb_slice)));
+ memcpy(cb_slice, &slice, sizeof(*cb_slice));
+ if (!pb_read(stream, GRPC_SLICE_START_PTR(*cb_slice), stream->bytes_left))
+ return false;
+ *arg = cb_slice;
+ return true;
+}
+
+bool decode_repeated_identity_cb(pb_istream_t* stream, const pb_field_t* field,
+ void** arg) {
+ grpc_gcp_identity* identity =
+ static_cast<grpc_gcp_identity*>(gpr_zalloc(sizeof(*identity)));
+ identity->hostname.funcs.decode = decode_string_or_bytes_cb;
+ identity->service_account.funcs.decode = decode_string_or_bytes_cb;
+ add_repeated_field(reinterpret_cast<repeated_field**>(arg), identity);
+ if (!pb_decode(stream, grpc_gcp_Identity_fields, identity)) return false;
+ return true;
+}
+
+bool decode_repeated_string_cb(pb_istream_t* stream, const pb_field_t* field,
+ void** arg) {
+ grpc_slice slice = grpc_slice_malloc(stream->bytes_left);
+ grpc_slice* cb_slice =
+ static_cast<grpc_slice*>(gpr_zalloc(sizeof(*cb_slice)));
+ memcpy(cb_slice, &slice, sizeof(grpc_slice));
+ if (!pb_read(stream, GRPC_SLICE_START_PTR(*cb_slice), stream->bytes_left))
+ return false;
+ add_repeated_field(reinterpret_cast<repeated_field**>(arg), cb_slice);
+ return true;
+}
diff --git a/src/core/tsi/alts/handshaker/alts_handshaker_service_api_util.h b/src/core/tsi/alts/handshaker/alts_handshaker_service_api_util.h
new file mode 100644
index 0000000000..8fe8f73f8b
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/alts_handshaker_service_api_util.h
@@ -0,0 +1,149 @@
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_HANDSHAKER_SERVICE_API_UTIL_H
+#define GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_HANDSHAKER_SERVICE_API_UTIL_H
+
+#include <grpc/support/port_platform.h>
+
+#include "third_party/nanopb/pb_decode.h"
+#include "third_party/nanopb/pb_encode.h"
+
+#include <grpc/slice.h>
+#include <grpc/slice_buffer.h>
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+
+#include "src/core/tsi/alts/handshaker/handshaker.pb.h"
+
+/**
+ * An implementation of utility functions used to serialize/
+ * de-serialize ALTS handshake requests/responses. All APIs in the header
+ * are thread-compatible.
+ */
+
+/* Renaming of message/field structs generated by nanopb compiler. */
+typedef grpc_gcp_HandshakeProtocol grpc_gcp_handshake_protocol;
+typedef grpc_gcp_NetworkProtocol grpc_gcp_network_protocol;
+typedef grpc_gcp_Identity grpc_gcp_identity;
+typedef grpc_gcp_NextHandshakeMessageReq grpc_gcp_next_handshake_message_req;
+typedef grpc_gcp_ServerHandshakeParameters grpc_gcp_server_handshake_parameters;
+typedef grpc_gcp_Endpoint grpc_gcp_endpoint;
+typedef grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry
+ grpc_gcp_handshake_parameters_entry;
+typedef grpc_gcp_StartClientHandshakeReq grpc_gcp_start_client_handshake_req;
+typedef grpc_gcp_StartServerHandshakeReq grpc_gcp_start_server_handshake_req;
+typedef grpc_gcp_HandshakerReq grpc_gcp_handshaker_req;
+typedef grpc_gcp_HandshakerResult grpc_gcp_handshaker_result;
+typedef grpc_gcp_HandshakerStatus grpc_gcp_handshaker_status;
+typedef grpc_gcp_HandshakerResp grpc_gcp_handshaker_resp;
+
+typedef enum {
+ CLIENT_START_REQ = 0, /* StartClientHandshakeReq. */
+ SERVER_START_REQ = 1, /* StartServerHandshakeReq. */
+ NEXT_REQ = 2, /* NextHandshakeMessageReq. */
+} grpc_gcp_handshaker_req_type;
+
+/**
+ * A struct representing a repeated field. The struct is used to organize all
+ * instances of a specific repeated field into a linked list, which then will
+ * be used at encode/decode phase. For instance at the encode phase, the encode
+ * function will iterate through the list, encode each field, and then output
+ * the result to the stream.
+ */
+typedef struct repeated_field_ {
+ struct repeated_field_* next;
+ const void* data;
+} repeated_field;
+
+/**
+ * This method adds a repeated field to the head of repeated field list.
+ *
+ * - head: a head of repeated field list.
+ * - field: a repeated field to be added to the list.
+ */
+void add_repeated_field(repeated_field** head, const void* field);
+
+/**
+ * This method destroys a repeated field list that consists of string type
+ * fields.
+ *
+ * - head: a head of repeated field list.
+ */
+void destroy_repeated_field_list_string(repeated_field* head);
+
+/**
+ * This method destroys a repeated field list that consists of
+ * grpc_gcp_identity type fields.
+ *
+ * - head: a head of repeated field list.
+ */
+void destroy_repeated_field_list_identity(repeated_field* head);
+
+/**
+ * This method creates a grpc_slice instance by copying a data buffer. It is
+ * similar to grpc_slice_from_copied_buffer() except that it returns an instance
+ * allocated from the heap.
+ *
+ * - data: a data buffer to be copied to grpc_slice instance.
+ * - size: size of data buffer.
+ */
+grpc_slice* create_slice(const char* data, size_t size);
+
+/* This method destroys a grpc_slice instance. */
+void destroy_slice(grpc_slice* slice);
+
+/**
+ * The following encode/decode functions will be assigned to encode/decode
+ * function pointers of pb_callback_t struct (defined in
+ * //third_party/nanopb/pb.h), that represent a repeated field with a dynamic
+ * length (e.g., a string type or repeated field).
+ */
+
+/* This method is an encode callback function for a string or byte array. */
+bool encode_string_or_bytes_cb(pb_ostream_t* stream, const pb_field_t* field,
+ void* const* arg);
+
+/**
+ * This method is an encode callback function for a repeated grpc_gcp_identity
+ * field.
+ */
+bool encode_repeated_identity_cb(pb_ostream_t* stream, const pb_field_t* field,
+ void* const* arg);
+
+/* This method is an encode callback function for a repeated string field. */
+bool encode_repeated_string_cb(pb_ostream_t* stream, const pb_field_t* field,
+ void* const* arg);
+
+/**
+ * This method is a decode callback function for a string or byte array field.
+ */
+bool decode_string_or_bytes_cb(pb_istream_t* stream, const pb_field_t* field,
+ void** arg);
+/**
+ * This method is a decode callback function for a repeated grpc_gcp_identity
+ * field.
+ */
+bool decode_repeated_identity_cb(pb_istream_t* stream, const pb_field_t* field,
+ void** arg);
+
+/* This method is a decode callback function for a repeated string field. */
+bool decode_repeated_string_cb(pb_istream_t* stream, const pb_field_t* field,
+ void** arg);
+
+#endif /* GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_HANDSHAKER_SERVICE_API_UTIL_H */
diff --git a/src/core/tsi/alts/handshaker/alts_tsi_event.cc b/src/core/tsi/alts/handshaker/alts_tsi_event.cc
new file mode 100644
index 0000000000..ec0bf12b95
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/alts_tsi_event.cc
@@ -0,0 +1,73 @@
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <grpc/support/port_platform.h>
+
+#include "src/core/tsi/alts/handshaker/alts_tsi_event.h"
+
+#include <grpc/grpc.h>
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+
+tsi_result alts_tsi_event_create(alts_tsi_handshaker* handshaker,
+ tsi_handshaker_on_next_done_cb cb,
+ void* user_data,
+ grpc_alts_credentials_options* options,
+ grpc_slice target_name,
+ alts_tsi_event** event) {
+ if (event == nullptr || handshaker == nullptr || cb == nullptr) {
+ gpr_log(GPR_ERROR, "Invalid arguments to alts_tsi_event_create()");
+ return TSI_INVALID_ARGUMENT;
+ }
+ alts_tsi_event* e = static_cast<alts_tsi_event*>(gpr_zalloc(sizeof(*e)));
+ e->handshaker = handshaker;
+ e->cb = cb;
+ e->user_data = user_data;
+ e->options = grpc_alts_credentials_options_copy(options);
+ e->target_name = grpc_slice_copy(target_name);
+ grpc_metadata_array_init(&e->initial_metadata);
+ grpc_metadata_array_init(&e->trailing_metadata);
+ *event = e;
+ return TSI_OK;
+}
+
+void alts_tsi_event_dispatch_to_handshaker(alts_tsi_event* event, bool is_ok) {
+ if (event == nullptr) {
+ gpr_log(
+ GPR_ERROR,
+ "ALTS TSI event is nullptr in alts_tsi_event_dispatch_to_handshaker()");
+ return;
+ }
+ alts_tsi_handshaker_handle_response(event->handshaker, event->recv_buffer,
+ event->status, &event->details, event->cb,
+ event->user_data, is_ok);
+}
+
+void alts_tsi_event_destroy(alts_tsi_event* event) {
+ if (event == nullptr) {
+ return;
+ }
+ grpc_byte_buffer_destroy(event->send_buffer);
+ grpc_byte_buffer_destroy(event->recv_buffer);
+ grpc_metadata_array_destroy(&event->initial_metadata);
+ grpc_metadata_array_destroy(&event->trailing_metadata);
+ grpc_slice_unref(event->details);
+ grpc_slice_unref(event->target_name);
+ grpc_alts_credentials_options_destroy(event->options);
+ gpr_free(event);
+}
diff --git a/src/core/tsi/alts/handshaker/alts_tsi_event.h b/src/core/tsi/alts/handshaker/alts_tsi_event.h
new file mode 100644
index 0000000000..043e75d4a9
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/alts_tsi_event.h
@@ -0,0 +1,93 @@
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_TSI_EVENT_H
+#define GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_TSI_EVENT_H
+
+#include <grpc/support/port_platform.h>
+
+#include <grpc/byte_buffer.h>
+#include <grpc/byte_buffer_reader.h>
+
+#include "src/core/tsi/alts/handshaker/alts_tsi_handshaker.h"
+#include "src/core/tsi/transport_security_interface.h"
+
+/**
+ * A ALTS TSI event interface. In asynchronous implementation of
+ * tsi_handshaker_next(), the function will exit after scheduling a handshaker
+ * request to ALTS handshaker service without waiting for response to return.
+ * The event is used to link the scheduled handshaker request with the
+ * corresponding response so that enough context information can be inferred
+ * from it to handle the response. All APIs in the header are thread-compatible.
+ */
+
+/**
+ * Main struct for ALTS TSI event. It retains ownership on send_buffer and
+ * recv_buffer, but not on handshaker.
+ */
+typedef struct alts_tsi_event {
+ alts_tsi_handshaker* handshaker;
+ grpc_byte_buffer* send_buffer;
+ grpc_byte_buffer* recv_buffer;
+ grpc_status_code status;
+ grpc_slice details;
+ grpc_metadata_array initial_metadata;
+ grpc_metadata_array trailing_metadata;
+ tsi_handshaker_on_next_done_cb cb;
+ void* user_data;
+ grpc_alts_credentials_options* options;
+ grpc_slice target_name;
+} alts_tsi_event;
+
+/**
+ * This method creates a ALTS TSI event.
+ *
+ * - handshaker: ALTS TSI handshaker instance associated with the event to be
+ * created. The created event does not own the handshaker instance.
+ * - cb: callback function to be called when handling data received from ALTS
+ * handshaker service.
+ * - user_data: argument to callback function.
+ * - options: ALTS credentials options.
+ * - target_name: name of endpoint used for secure naming check.
+ * - event: address of ALTS TSI event instance to be returned from the method.
+ *
+ * It returns TSI_OK on success and an error status code on failure.
+ */
+tsi_result alts_tsi_event_create(alts_tsi_handshaker* handshaker,
+ tsi_handshaker_on_next_done_cb cb,
+ void* user_data,
+ grpc_alts_credentials_options* options,
+ grpc_slice target_name,
+ alts_tsi_event** event);
+
+/**
+ * This method dispatches a ALTS TSI event received from the handshaker service,
+ * and a boolean flag indicating if the event is valid to read to ALTS TSI
+ * handshaker to process. It is called by TSI thread.
+ *
+ * - event: ALTS TSI event instance.
+ * - is_ok: a boolean value indicating if the event is valid to read.
+ */
+void alts_tsi_event_dispatch_to_handshaker(alts_tsi_event* event, bool is_ok);
+
+/**
+ * This method destroys the ALTS TSI event.
+ */
+void alts_tsi_event_destroy(alts_tsi_event* event);
+
+#endif /* GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_TSI_EVENT_H */
diff --git a/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc b/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc
new file mode 100644
index 0000000000..529f2103c7
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc
@@ -0,0 +1,483 @@
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <grpc/support/port_platform.h>
+
+#include "src/core/tsi/alts/handshaker/alts_tsi_handshaker.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+#include <grpc/support/sync.h>
+#include <grpc/support/thd_id.h>
+
+#include "src/core/lib/gpr/host_port.h"
+#include "src/core/lib/gprpp/thd.h"
+#include "src/core/tsi/alts/frame_protector/alts_frame_protector.h"
+#include "src/core/tsi/alts/handshaker/alts_handshaker_client.h"
+#include "src/core/tsi/alts/handshaker/alts_tsi_utils.h"
+#include "src/core/tsi/alts/zero_copy_frame_protector/alts_zero_copy_grpc_protector.h"
+#include "src/core/tsi/alts_transport_security.h"
+
+#define TSI_ALTS_INITIAL_BUFFER_SIZE 256
+
+static alts_shared_resource* kSharedResource = alts_get_shared_resource();
+
+/* Main struct for ALTS TSI handshaker. */
+typedef struct alts_tsi_handshaker {
+ tsi_handshaker base;
+ alts_handshaker_client* client;
+ grpc_slice recv_bytes;
+ grpc_slice target_name;
+ unsigned char* buffer;
+ size_t buffer_size;
+ bool is_client;
+ bool has_sent_start_message;
+ grpc_alts_credentials_options* options;
+} alts_tsi_handshaker;
+
+/* Main struct for ALTS TSI handshaker result. */
+typedef struct alts_tsi_handshaker_result {
+ tsi_handshaker_result base;
+ char* peer_identity;
+ char* key_data;
+ unsigned char* unused_bytes;
+ size_t unused_bytes_size;
+ grpc_slice rpc_versions;
+ bool is_client;
+} alts_tsi_handshaker_result;
+
+static tsi_result handshaker_result_extract_peer(
+ const tsi_handshaker_result* self, tsi_peer* peer) {
+ if (self == nullptr || peer == nullptr) {
+ gpr_log(GPR_ERROR, "Invalid argument to handshaker_result_extract_peer()");
+ return TSI_INVALID_ARGUMENT;
+ }
+ alts_tsi_handshaker_result* result =
+ reinterpret_cast<alts_tsi_handshaker_result*>(
+ const_cast<tsi_handshaker_result*>(self));
+ GPR_ASSERT(kTsiAltsNumOfPeerProperties == 3);
+ tsi_result ok = tsi_construct_peer(kTsiAltsNumOfPeerProperties, peer);
+ int index = 0;
+ if (ok != TSI_OK) {
+ gpr_log(GPR_ERROR, "Failed to construct tsi peer");
+ return ok;
+ }
+ GPR_ASSERT(&peer->properties[index] != nullptr);
+ ok = tsi_construct_string_peer_property_from_cstring(
+ TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_ALTS_CERTIFICATE_TYPE,
+ &peer->properties[index]);
+ if (ok != TSI_OK) {
+ tsi_peer_destruct(peer);
+ gpr_log(GPR_ERROR, "Failed to set tsi peer property");
+ return ok;
+ }
+ index++;
+ GPR_ASSERT(&peer->properties[index] != nullptr);
+ ok = tsi_construct_string_peer_property_from_cstring(
+ TSI_ALTS_SERVICE_ACCOUNT_PEER_PROPERTY, result->peer_identity,
+ &peer->properties[index]);
+ if (ok != TSI_OK) {
+ tsi_peer_destruct(peer);
+ gpr_log(GPR_ERROR, "Failed to set tsi peer property");
+ }
+ index++;
+ GPR_ASSERT(&peer->properties[index] != nullptr);
+ ok = tsi_construct_string_peer_property(
+ TSI_ALTS_RPC_VERSIONS,
+ reinterpret_cast<char*>(GRPC_SLICE_START_PTR(result->rpc_versions)),
+ GRPC_SLICE_LENGTH(result->rpc_versions), &peer->properties[2]);
+ if (ok != TSI_OK) {
+ tsi_peer_destruct(peer);
+ gpr_log(GPR_ERROR, "Failed to set tsi peer property");
+ }
+ GPR_ASSERT(++index == kTsiAltsNumOfPeerProperties);
+ return ok;
+}
+
+static tsi_result handshaker_result_create_zero_copy_grpc_protector(
+ const tsi_handshaker_result* self, size_t* max_output_protected_frame_size,
+ tsi_zero_copy_grpc_protector** protector) {
+ if (self == nullptr || protector == nullptr) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to create_zero_copy_grpc_protector()");
+ return TSI_INVALID_ARGUMENT;
+ }
+ alts_tsi_handshaker_result* result =
+ reinterpret_cast<alts_tsi_handshaker_result*>(
+ const_cast<tsi_handshaker_result*>(self));
+ tsi_result ok = alts_zero_copy_grpc_protector_create(
+ reinterpret_cast<const uint8_t*>(result->key_data),
+ kAltsAes128GcmRekeyKeyLength, /*is_rekey=*/true, result->is_client,
+ /*is_integrity_only=*/false, max_output_protected_frame_size, protector);
+ if (ok != TSI_OK) {
+ gpr_log(GPR_ERROR, "Failed to create zero-copy grpc protector");
+ }
+ return ok;
+}
+
+static tsi_result handshaker_result_create_frame_protector(
+ const tsi_handshaker_result* self, size_t* max_output_protected_frame_size,
+ tsi_frame_protector** protector) {
+ if (self == nullptr || protector == nullptr) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to handshaker_result_create_frame_protector()");
+ return TSI_INVALID_ARGUMENT;
+ }
+ alts_tsi_handshaker_result* result =
+ reinterpret_cast<alts_tsi_handshaker_result*>(
+ const_cast<tsi_handshaker_result*>(self));
+ tsi_result ok = alts_create_frame_protector(
+ reinterpret_cast<const uint8_t*>(result->key_data),
+ kAltsAes128GcmRekeyKeyLength, result->is_client, /*is_rekey=*/true,
+ max_output_protected_frame_size, protector);
+ if (ok != TSI_OK) {
+ gpr_log(GPR_ERROR, "Failed to create frame protector");
+ }
+ return ok;
+}
+
+static tsi_result handshaker_result_get_unused_bytes(
+ const tsi_handshaker_result* self, const unsigned char** bytes,
+ size_t* bytes_size) {
+ if (self == nullptr || bytes == nullptr || bytes_size == nullptr) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to handshaker_result_get_unused_bytes()");
+ return TSI_INVALID_ARGUMENT;
+ }
+ alts_tsi_handshaker_result* result =
+ reinterpret_cast<alts_tsi_handshaker_result*>(
+ const_cast<tsi_handshaker_result*>(self));
+ *bytes = result->unused_bytes;
+ *bytes_size = result->unused_bytes_size;
+ return TSI_OK;
+}
+
+static void handshaker_result_destroy(tsi_handshaker_result* self) {
+ if (self == nullptr) {
+ return;
+ }
+ alts_tsi_handshaker_result* result =
+ reinterpret_cast<alts_tsi_handshaker_result*>(
+ const_cast<tsi_handshaker_result*>(self));
+ gpr_free(result->peer_identity);
+ gpr_free(result->key_data);
+ gpr_free(result->unused_bytes);
+ grpc_slice_unref(result->rpc_versions);
+ gpr_free(result);
+}
+
+static const tsi_handshaker_result_vtable result_vtable = {
+ handshaker_result_extract_peer,
+ handshaker_result_create_zero_copy_grpc_protector,
+ handshaker_result_create_frame_protector,
+ handshaker_result_get_unused_bytes, handshaker_result_destroy};
+
+static tsi_result create_handshaker_result(grpc_gcp_handshaker_resp* resp,
+ bool is_client,
+ tsi_handshaker_result** self) {
+ if (self == nullptr || resp == nullptr) {
+ gpr_log(GPR_ERROR, "Invalid arguments to create_handshaker_result()");
+ return TSI_INVALID_ARGUMENT;
+ }
+ grpc_slice* key = static_cast<grpc_slice*>(resp->result.key_data.arg);
+ GPR_ASSERT(key != nullptr);
+ grpc_slice* identity =
+ static_cast<grpc_slice*>(resp->result.peer_identity.service_account.arg);
+ if (identity == nullptr) {
+ gpr_log(GPR_ERROR, "Invalid service account");
+ return TSI_FAILED_PRECONDITION;
+ }
+ if (GRPC_SLICE_LENGTH(*key) < kAltsAes128GcmRekeyKeyLength) {
+ gpr_log(GPR_ERROR, "Bad key length");
+ return TSI_FAILED_PRECONDITION;
+ }
+ alts_tsi_handshaker_result* result =
+ static_cast<alts_tsi_handshaker_result*>(gpr_zalloc(sizeof(*result)));
+ result->key_data =
+ static_cast<char*>(gpr_zalloc(kAltsAes128GcmRekeyKeyLength));
+ memcpy(result->key_data, GRPC_SLICE_START_PTR(*key),
+ kAltsAes128GcmRekeyKeyLength);
+ result->peer_identity = grpc_slice_to_c_string(*identity);
+ if (!resp->result.has_peer_rpc_versions) {
+ gpr_log(GPR_ERROR, "Peer does not set RPC protocol versions.");
+ return TSI_FAILED_PRECONDITION;
+ }
+ if (!grpc_gcp_rpc_protocol_versions_encode(&resp->result.peer_rpc_versions,
+ &result->rpc_versions)) {
+ gpr_log(GPR_ERROR, "Failed to serialize peer's RPC protocol versions.");
+ return TSI_FAILED_PRECONDITION;
+ }
+ result->is_client = is_client;
+ result->base.vtable = &result_vtable;
+ *self = &result->base;
+ return TSI_OK;
+}
+
+static tsi_result handshaker_next(
+ tsi_handshaker* self, const unsigned char* received_bytes,
+ size_t received_bytes_size, const unsigned char** bytes_to_send,
+ size_t* bytes_to_send_size, tsi_handshaker_result** result,
+ tsi_handshaker_on_next_done_cb cb, void* user_data) {
+ if (self == nullptr || cb == nullptr) {
+ gpr_log(GPR_ERROR, "Invalid arguments to handshaker_next()");
+ return TSI_INVALID_ARGUMENT;
+ }
+ alts_tsi_handshaker* handshaker =
+ reinterpret_cast<alts_tsi_handshaker*>(self);
+ tsi_result ok = TSI_OK;
+ alts_tsi_event* event = nullptr;
+ ok = alts_tsi_event_create(handshaker, cb, user_data, handshaker->options,
+ handshaker->target_name, &event);
+ if (ok != TSI_OK) {
+ gpr_log(GPR_ERROR, "Failed to create ALTS TSI event");
+ return ok;
+ }
+ grpc_slice slice = (received_bytes == nullptr || received_bytes_size == 0)
+ ? grpc_empty_slice()
+ : grpc_slice_from_copied_buffer(
+ reinterpret_cast<const char*>(received_bytes),
+ received_bytes_size);
+ if (!handshaker->has_sent_start_message) {
+ ok = handshaker->is_client
+ ? alts_handshaker_client_start_client(handshaker->client, event)
+ : alts_handshaker_client_start_server(handshaker->client, event,
+ &slice);
+ handshaker->has_sent_start_message = true;
+ } else {
+ if (!GRPC_SLICE_IS_EMPTY(handshaker->recv_bytes)) {
+ grpc_slice_unref(handshaker->recv_bytes);
+ }
+ handshaker->recv_bytes = grpc_slice_ref(slice);
+ ok = alts_handshaker_client_next(handshaker->client, event, &slice);
+ }
+ grpc_slice_unref(slice);
+ if (ok != TSI_OK) {
+ gpr_log(GPR_ERROR, "Failed to schedule ALTS handshaker requests");
+ return ok;
+ }
+ return TSI_ASYNC;
+}
+
+static void handshaker_destroy(tsi_handshaker* self) {
+ if (self == nullptr) {
+ return;
+ }
+ alts_tsi_handshaker* handshaker =
+ reinterpret_cast<alts_tsi_handshaker*>(self);
+ alts_handshaker_client_destroy(handshaker->client);
+ grpc_slice_unref(handshaker->recv_bytes);
+ grpc_slice_unref(handshaker->target_name);
+ grpc_alts_credentials_options_destroy(handshaker->options);
+ gpr_free(handshaker->buffer);
+ gpr_free(handshaker);
+}
+
+static const tsi_handshaker_vtable handshaker_vtable = {
+ nullptr, nullptr, nullptr, nullptr, nullptr, handshaker_destroy,
+ handshaker_next};
+
+static void thread_worker(void* arg) {
+ while (true) {
+ grpc_event event = grpc_completion_queue_next(
+ kSharedResource->cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
+ GPR_ASSERT(event.type != GRPC_QUEUE_TIMEOUT);
+ if (event.type == GRPC_QUEUE_SHUTDOWN) {
+ /* signal alts_tsi_shutdown() to destroy completion queue. */
+ grpc_tsi_alts_signal_for_cq_destroy();
+ break;
+ }
+ /* event.type == GRPC_OP_COMPLETE. */
+ alts_tsi_event* alts_event = static_cast<alts_tsi_event*>(event.tag);
+ alts_tsi_event_dispatch_to_handshaker(alts_event, event.success);
+ alts_tsi_event_destroy(alts_event);
+ }
+}
+
+static void init_shared_resources(const char* handshaker_service_url) {
+ GPR_ASSERT(handshaker_service_url != nullptr);
+ gpr_mu_lock(&kSharedResource->mu);
+ if (kSharedResource->channel == nullptr) {
+ gpr_cv_init(&kSharedResource->cv);
+ kSharedResource->channel =
+ grpc_insecure_channel_create(handshaker_service_url, nullptr, nullptr);
+ kSharedResource->cq = grpc_completion_queue_create_for_next(nullptr);
+ kSharedResource->thread =
+ grpc_core::Thread("alts_tsi_handshaker", &thread_worker, nullptr);
+ kSharedResource->thread.Start();
+ }
+ gpr_mu_unlock(&kSharedResource->mu);
+}
+
+tsi_result alts_tsi_handshaker_create(
+ const grpc_alts_credentials_options* options, const char* target_name,
+ const char* handshaker_service_url, bool is_client, tsi_handshaker** self) {
+ if (handshaker_service_url == nullptr || self == nullptr ||
+ options == nullptr || (is_client && target_name == nullptr)) {
+ gpr_log(GPR_ERROR, "Invalid arguments to alts_tsi_handshaker_create()");
+ return TSI_INVALID_ARGUMENT;
+ }
+ init_shared_resources(handshaker_service_url);
+ alts_handshaker_client* client = alts_grpc_handshaker_client_create(
+ kSharedResource->channel, kSharedResource->cq, handshaker_service_url);
+ if (client == nullptr) {
+ gpr_log(GPR_ERROR, "Failed to create ALTS handshaker client");
+ return TSI_FAILED_PRECONDITION;
+ }
+ alts_tsi_handshaker* handshaker =
+ static_cast<alts_tsi_handshaker*>(gpr_zalloc(sizeof(*handshaker)));
+ handshaker->client = client;
+ handshaker->buffer_size = TSI_ALTS_INITIAL_BUFFER_SIZE;
+ handshaker->buffer =
+ static_cast<unsigned char*>(gpr_zalloc(handshaker->buffer_size));
+ handshaker->is_client = is_client;
+ handshaker->has_sent_start_message = false;
+ handshaker->target_name = target_name == nullptr
+ ? grpc_empty_slice()
+ : grpc_slice_from_static_string(target_name);
+ handshaker->options = grpc_alts_credentials_options_copy(options);
+ handshaker->base.vtable = &handshaker_vtable;
+ *self = &handshaker->base;
+ return TSI_OK;
+}
+
+static bool is_handshake_finished_properly(grpc_gcp_handshaker_resp* resp) {
+ GPR_ASSERT(resp != nullptr);
+ if (resp->has_result) {
+ return true;
+ }
+ return false;
+}
+
+static void set_unused_bytes(tsi_handshaker_result* self,
+ grpc_slice* recv_bytes, size_t bytes_consumed) {
+ GPR_ASSERT(recv_bytes != nullptr && self != nullptr);
+ if (GRPC_SLICE_LENGTH(*recv_bytes) == bytes_consumed) {
+ return;
+ }
+ alts_tsi_handshaker_result* result =
+ reinterpret_cast<alts_tsi_handshaker_result*>(self);
+ result->unused_bytes_size = GRPC_SLICE_LENGTH(*recv_bytes) - bytes_consumed;
+ result->unused_bytes =
+ static_cast<unsigned char*>(gpr_zalloc(result->unused_bytes_size));
+ memcpy(result->unused_bytes,
+ GRPC_SLICE_START_PTR(*recv_bytes) + bytes_consumed,
+ result->unused_bytes_size);
+}
+
+void alts_tsi_handshaker_handle_response(alts_tsi_handshaker* handshaker,
+ grpc_byte_buffer* recv_buffer,
+ grpc_status_code status,
+ grpc_slice* details,
+ tsi_handshaker_on_next_done_cb cb,
+ void* user_data, bool is_ok) {
+ /* Invalid input check. */
+ if (cb == nullptr) {
+ gpr_log(GPR_ERROR,
+ "cb is nullptr in alts_tsi_handshaker_handle_response()");
+ return;
+ }
+ if (handshaker == nullptr || recv_buffer == nullptr) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to alts_tsi_handshaker_handle_response()");
+ cb(TSI_INTERNAL_ERROR, user_data, nullptr, 0, nullptr);
+ return;
+ }
+ /* Failed grpc call check. */
+ if (!is_ok || status != GRPC_STATUS_OK) {
+ gpr_log(GPR_ERROR, "grpc call made to handshaker service failed");
+ if (details != nullptr) {
+ char* error_details = grpc_slice_to_c_string(*details);
+ gpr_log(GPR_ERROR, "error details:%s", error_details);
+ gpr_free(error_details);
+ }
+ cb(TSI_INTERNAL_ERROR, user_data, nullptr, 0, nullptr);
+ return;
+ }
+ grpc_gcp_handshaker_resp* resp =
+ alts_tsi_utils_deserialize_response(recv_buffer);
+ /* Invalid handshaker response check. */
+ if (resp == nullptr) {
+ gpr_log(GPR_ERROR, "alts_tsi_utils_deserialize_response() failed");
+ cb(TSI_DATA_CORRUPTED, user_data, nullptr, 0, nullptr);
+ return;
+ }
+ grpc_slice* slice = static_cast<grpc_slice*>(resp->out_frames.arg);
+ unsigned char* bytes_to_send = nullptr;
+ size_t bytes_to_send_size = 0;
+ if (slice != nullptr) {
+ bytes_to_send_size = GRPC_SLICE_LENGTH(*slice);
+ while (bytes_to_send_size > handshaker->buffer_size) {
+ handshaker->buffer_size *= 2;
+ handshaker->buffer = static_cast<unsigned char*>(
+ gpr_realloc(handshaker->buffer, handshaker->buffer_size));
+ }
+ memcpy(handshaker->buffer, GRPC_SLICE_START_PTR(*slice),
+ bytes_to_send_size);
+ bytes_to_send = handshaker->buffer;
+ }
+ tsi_handshaker_result* result = nullptr;
+ if (is_handshake_finished_properly(resp)) {
+ create_handshaker_result(resp, handshaker->is_client, &result);
+ set_unused_bytes(result, &handshaker->recv_bytes, resp->bytes_consumed);
+ }
+ grpc_status_code code = static_cast<grpc_status_code>(resp->status.code);
+ grpc_gcp_handshaker_resp_destroy(resp);
+ cb(alts_tsi_utils_convert_to_tsi_result(code), user_data, bytes_to_send,
+ bytes_to_send_size, result);
+}
+
+namespace grpc_core {
+namespace internal {
+
+bool alts_tsi_handshaker_get_has_sent_start_message_for_testing(
+ alts_tsi_handshaker* handshaker) {
+ GPR_ASSERT(handshaker != nullptr);
+ return handshaker->has_sent_start_message;
+}
+
+bool alts_tsi_handshaker_get_is_client_for_testing(
+ alts_tsi_handshaker* handshaker) {
+ GPR_ASSERT(handshaker != nullptr);
+ return handshaker->is_client;
+}
+
+void alts_tsi_handshaker_set_recv_bytes_for_testing(
+ alts_tsi_handshaker* handshaker, grpc_slice* slice) {
+ GPR_ASSERT(handshaker != nullptr && slice != nullptr);
+ handshaker->recv_bytes = grpc_slice_ref(*slice);
+}
+
+grpc_slice alts_tsi_handshaker_get_recv_bytes_for_testing(
+ alts_tsi_handshaker* handshaker) {
+ GPR_ASSERT(handshaker != nullptr);
+ return handshaker->recv_bytes;
+}
+
+void alts_tsi_handshaker_set_client_for_testing(
+ alts_tsi_handshaker* handshaker, alts_handshaker_client* client) {
+ GPR_ASSERT(handshaker != nullptr && client != nullptr);
+ alts_handshaker_client_destroy(handshaker->client);
+ handshaker->client = client;
+}
+
+} // namespace internal
+} // namespace grpc_core
diff --git a/src/core/tsi/alts/handshaker/alts_tsi_handshaker.h b/src/core/tsi/alts/handshaker/alts_tsi_handshaker.h
new file mode 100644
index 0000000000..227b30ce53
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/alts_tsi_handshaker.h
@@ -0,0 +1,83 @@
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_TSI_HANDSHAKER_H
+#define GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_TSI_HANDSHAKER_H
+
+#include <grpc/support/port_platform.h>
+
+#include <grpc/grpc.h>
+
+#include "src/core/lib/security/credentials/alts/grpc_alts_credentials_options.h"
+#include "src/core/tsi/alts_transport_security.h"
+#include "src/core/tsi/transport_security.h"
+#include "src/core/tsi/transport_security_interface.h"
+
+#define TSI_ALTS_SERVICE_ACCOUNT_PEER_PROPERTY "service_accont"
+#define TSI_ALTS_CERTIFICATE_TYPE "ALTS"
+#define TSI_ALTS_RPC_VERSIONS "rpc_versions"
+
+const size_t kTsiAltsNumOfPeerProperties = 3;
+
+/**
+ * Main struct for ALTS TSI handshaker. All APIs in the header are
+ * thread-comptabile.
+ */
+typedef struct alts_tsi_handshaker alts_tsi_handshaker;
+
+/**
+ * This method creates a ALTS TSI handshaker instance.
+ *
+ * - options: ALTS credentials options containing information passed from TSI
+ * caller (e.g., rpc protocol versions).
+ * - target_name: the name of the endpoint that the channel is connecting to,
+ * and will be used for secure naming check.
+ * - handshaker_service_url: address of ALTS handshaker service in the format of
+ * "host:port".
+ * - is_client: boolean value indicating if the handshaker is used at the client
+ * (is_client = true) or server (is_client = false) side.
+ * - self: address of ALTS TSI handshaker instance to be returned from the
+ * method.
+ *
+ * It returns TSI_OK on success and an error status code on failure.
+ */
+tsi_result alts_tsi_handshaker_create(
+ const grpc_alts_credentials_options* options, const char* target_name,
+ const char* handshaker_service_url, bool is_client, tsi_handshaker** self);
+
+/**
+ * This method handles handshaker response returned from ALTS handshaker
+ * service.
+ *
+ * - handshaker: ALTS TSI handshaker instance.
+ * - recv_buffer: buffer holding data received from the handshaker service.
+ * - status: status of the grpc call made to the handshaker service.
+ * - details: error details of the grpc call made to the handshaker service.
+ * - cb: callback function of ALTS TSI event.
+ * - user_data: argument of callback function.
+ * - is_ok: a boolean value indicating if the handshaker response is ok to read.
+ *
+ */
+void alts_tsi_handshaker_handle_response(alts_tsi_handshaker* handshaker,
+ grpc_byte_buffer* recv_buffer,
+ grpc_status_code status,
+ grpc_slice* details,
+ tsi_handshaker_on_next_done_cb cb,
+ void* user_data, bool is_ok);
+
+#endif /* GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_TSI_HANDSHAKER_H */
diff --git a/src/core/tsi/alts/handshaker/alts_tsi_handshaker_private.h b/src/core/tsi/alts/handshaker/alts_tsi_handshaker_private.h
new file mode 100644
index 0000000000..9b7b9bb6b1
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/alts_tsi_handshaker_private.h
@@ -0,0 +1,52 @@
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_TSI_HANDSHAKER_PRIVATE_H
+#define GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_TSI_HANDSHAKER_PRIVATE_H
+
+#include <grpc/support/port_platform.h>
+
+#include "src/core/tsi/alts/handshaker/alts_handshaker_client.h"
+
+namespace grpc_core {
+namespace internal {
+
+/**
+ * Unsafe, use for testing only. It allows the caller to change the way the
+ * ALTS TSI handshaker schedules handshaker requests.
+ */
+void alts_tsi_handshaker_set_client_for_testing(alts_tsi_handshaker* handshaker,
+ alts_handshaker_client* client);
+
+/* For testing only. */
+bool alts_tsi_handshaker_get_has_sent_start_message_for_testing(
+ alts_tsi_handshaker* handshaker);
+
+bool alts_tsi_handshaker_get_is_client_for_testing(
+ alts_tsi_handshaker* handshaker);
+
+void alts_tsi_handshaker_set_recv_bytes_for_testing(
+ alts_tsi_handshaker* handshaker, grpc_slice* slice);
+
+grpc_slice alts_tsi_handshaker_get_recv_bytes_for_testing(
+ alts_tsi_handshaker* handshaker);
+
+} // namespace internal
+} // namespace grpc_core
+
+#endif /* GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_TSI_HANDSHAKER_PRIVATE_H */
diff --git a/src/core/tsi/alts/handshaker/alts_tsi_utils.cc b/src/core/tsi/alts/handshaker/alts_tsi_utils.cc
new file mode 100644
index 0000000000..d9b5e6c945
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/alts_tsi_utils.cc
@@ -0,0 +1,58 @@
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <grpc/support/port_platform.h>
+
+#include "src/core/tsi/alts/handshaker/alts_tsi_utils.h"
+
+#include <grpc/byte_buffer_reader.h>
+
+tsi_result alts_tsi_utils_convert_to_tsi_result(grpc_status_code code) {
+ switch (code) {
+ case GRPC_STATUS_OK:
+ return TSI_OK;
+ case GRPC_STATUS_UNKNOWN:
+ return TSI_UNKNOWN_ERROR;
+ case GRPC_STATUS_INVALID_ARGUMENT:
+ return TSI_INVALID_ARGUMENT;
+ case GRPC_STATUS_NOT_FOUND:
+ return TSI_NOT_FOUND;
+ case GRPC_STATUS_INTERNAL:
+ return TSI_INTERNAL_ERROR;
+ default:
+ return TSI_UNKNOWN_ERROR;
+ }
+}
+
+grpc_gcp_handshaker_resp* alts_tsi_utils_deserialize_response(
+ grpc_byte_buffer* resp_buffer) {
+ GPR_ASSERT(resp_buffer != nullptr);
+ grpc_byte_buffer_reader bbr;
+ grpc_byte_buffer_reader_init(&bbr, resp_buffer);
+ grpc_slice slice = grpc_byte_buffer_reader_readall(&bbr);
+ grpc_gcp_handshaker_resp* resp = grpc_gcp_handshaker_resp_create();
+ bool ok = grpc_gcp_handshaker_resp_decode(slice, resp);
+ grpc_slice_unref(slice);
+ grpc_byte_buffer_reader_destroy(&bbr);
+ if (!ok) {
+ grpc_gcp_handshaker_resp_destroy(resp);
+ gpr_log(GPR_ERROR, "grpc_gcp_handshaker_resp_decode() failed");
+ return nullptr;
+ }
+ return resp;
+}
diff --git a/src/core/tsi/alts/handshaker/alts_tsi_utils.h b/src/core/tsi/alts/handshaker/alts_tsi_utils.h
new file mode 100644
index 0000000000..9ef649de2b
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/alts_tsi_utils.h
@@ -0,0 +1,52 @@
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_TSI_UTILS_H
+#define GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_TSI_UTILS_H
+
+#include <grpc/support/port_platform.h>
+
+#include <grpc/byte_buffer.h>
+#include <grpc/grpc.h>
+
+#include "src/core/tsi/alts/handshaker/alts_handshaker_service_api.h"
+#include "src/core/tsi/transport_security_interface.h"
+
+/**
+ * This method converts grpc_status_code code to the corresponding tsi_result
+ * code.
+ *
+ * - code: grpc_status_code code.
+ *
+ * It returns the converted tsi_result code.
+ */
+tsi_result alts_tsi_utils_convert_to_tsi_result(grpc_status_code code);
+
+/**
+ * This method deserializes a handshaker response returned from ALTS handshaker
+ * service.
+ *
+ * - bytes_received: data returned from ALTS handshaker service.
+ *
+ * It returns a deserialized handshaker response on success and nullptr on
+ * failure.
+ */
+grpc_gcp_handshaker_resp* alts_tsi_utils_deserialize_response(
+ grpc_byte_buffer* resp_buffer);
+
+#endif /* GRPC_CORE_TSI_ALTS_HANDSHAKER_ALTS_TSI_UTILS_H */
diff --git a/src/core/tsi/alts/handshaker/altscontext.pb.c b/src/core/tsi/alts/handshaker/altscontext.pb.c
new file mode 100644
index 0000000000..81a82f5992
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/altscontext.pb.c
@@ -0,0 +1,48 @@
+/* Automatically generated nanopb constant definitions */
+/* Generated by nanopb-0.3.7-dev */
+
+#include "src/core/tsi/alts/handshaker/altscontext.pb.h"
+
+/* @@protoc_insertion_point(includes) */
+#if PB_PROTO_HEADER_VERSION != 30
+#error Regenerate this file with the current version of nanopb generator.
+#endif
+
+
+
+const pb_field_t grpc_gcp_AltsContext_fields[7] = {
+ PB_FIELD( 1, STRING , OPTIONAL, CALLBACK, FIRST, grpc_gcp_AltsContext, application_protocol, application_protocol, 0),
+ PB_FIELD( 2, STRING , OPTIONAL, CALLBACK, OTHER, grpc_gcp_AltsContext, record_protocol, application_protocol, 0),
+ PB_FIELD( 3, UENUM , OPTIONAL, STATIC , OTHER, grpc_gcp_AltsContext, security_level, record_protocol, 0),
+ PB_FIELD( 4, STRING , OPTIONAL, CALLBACK, OTHER, grpc_gcp_AltsContext, peer_service_account, security_level, 0),
+ PB_FIELD( 5, STRING , OPTIONAL, CALLBACK, OTHER, grpc_gcp_AltsContext, local_service_account, peer_service_account, 0),
+ PB_FIELD( 6, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_AltsContext, peer_rpc_versions, local_service_account, &grpc_gcp_RpcProtocolVersions_fields),
+ PB_LAST_FIELD
+};
+
+
+/* Check that field information fits in pb_field_t */
+#if !defined(PB_FIELD_32BIT)
+/* If you get an error here, it means that you need to define PB_FIELD_32BIT
+ * compile-time option. You can do that in pb.h or on compiler command line.
+ *
+ * The reason you need to do this is that some of your messages contain tag
+ * numbers or field sizes that are larger than what can fit in 8 or 16 bit
+ * field descriptors.
+ */
+PB_STATIC_ASSERT((pb_membersize(grpc_gcp_AltsContext, peer_rpc_versions) < 65536), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_grpc_gcp_AltsContext)
+#endif
+
+#if !defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)
+/* If you get an error here, it means that you need to define PB_FIELD_16BIT
+ * compile-time option. You can do that in pb.h or on compiler command line.
+ *
+ * The reason you need to do this is that some of your messages contain tag
+ * numbers or field sizes that are larger than what can fit in the default
+ * 8 bit descriptors.
+ */
+PB_STATIC_ASSERT((pb_membersize(grpc_gcp_AltsContext, peer_rpc_versions) < 256), YOU_MUST_DEFINE_PB_FIELD_16BIT_FOR_MESSAGES_grpc_gcp_AltsContext)
+#endif
+
+
+/* @@protoc_insertion_point(eof) */
diff --git a/src/core/tsi/alts/handshaker/altscontext.pb.h b/src/core/tsi/alts/handshaker/altscontext.pb.h
new file mode 100644
index 0000000000..3e72d7f678
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/altscontext.pb.h
@@ -0,0 +1,64 @@
+/* Automatically generated nanopb header */
+/* Generated by nanopb-0.3.7-dev */
+
+#ifndef PB_GRPC_GCP_ALTSCONTEXT_PB_H_INCLUDED
+#define PB_GRPC_GCP_ALTSCONTEXT_PB_H_INCLUDED
+#include "third_party/nanopb/pb.h"
+#include "src/core/tsi/alts/handshaker/transport_security_common.pb.h"
+
+/* @@protoc_insertion_point(includes) */
+#if PB_PROTO_HEADER_VERSION != 30
+#error Regenerate this file with the current version of nanopb generator.
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Struct definitions */
+typedef struct _grpc_gcp_AltsContext {
+ pb_callback_t application_protocol;
+ pb_callback_t record_protocol;
+ bool has_security_level;
+ grpc_gcp_SecurityLevel security_level;
+ pb_callback_t peer_service_account;
+ pb_callback_t local_service_account;
+ bool has_peer_rpc_versions;
+ grpc_gcp_RpcProtocolVersions peer_rpc_versions;
+/* @@protoc_insertion_point(struct:grpc_gcp_AltsContext) */
+} grpc_gcp_AltsContext;
+
+/* Default values for struct fields */
+
+/* Initializer values for message structs */
+#define grpc_gcp_AltsContext_init_default {{{NULL}, NULL}, {{NULL}, NULL}, false, (grpc_gcp_SecurityLevel)0, {{NULL}, NULL}, {{NULL}, NULL}, false, grpc_gcp_RpcProtocolVersions_init_default}
+#define grpc_gcp_AltsContext_init_zero {{{NULL}, NULL}, {{NULL}, NULL}, false, (grpc_gcp_SecurityLevel)0, {{NULL}, NULL}, {{NULL}, NULL}, false, grpc_gcp_RpcProtocolVersions_init_zero}
+
+/* Field tags (for use in manual encoding/decoding) */
+#define grpc_gcp_AltsContext_application_protocol_tag 1
+#define grpc_gcp_AltsContext_record_protocol_tag 2
+#define grpc_gcp_AltsContext_security_level_tag 3
+#define grpc_gcp_AltsContext_peer_service_account_tag 4
+#define grpc_gcp_AltsContext_local_service_account_tag 5
+#define grpc_gcp_AltsContext_peer_rpc_versions_tag 6
+
+/* Struct field encoding specification for nanopb */
+extern const pb_field_t grpc_gcp_AltsContext_fields[7];
+
+/* Maximum encoded size of messages (where known) */
+/* grpc_gcp_AltsContext_size depends on runtime parameters */
+
+/* Message IDs (where set with "msgid" option) */
+#ifdef PB_MSGID
+
+#define ALTSCONTEXT_MESSAGES \
+
+
+#endif
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+/* @@protoc_insertion_point(eof) */
+
+#endif
diff --git a/src/core/tsi/alts/handshaker/handshaker.pb.c b/src/core/tsi/alts/handshaker/handshaker.pb.c
new file mode 100644
index 0000000000..bd992dfa4a
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/handshaker.pb.c
@@ -0,0 +1,123 @@
+/* Automatically generated nanopb constant definitions */
+/* Generated by nanopb-0.3.7-dev */
+
+#include "src/core/tsi/alts/handshaker/handshaker.pb.h"
+
+/* @@protoc_insertion_point(includes) */
+#if PB_PROTO_HEADER_VERSION != 30
+#error Regenerate this file with the current version of nanopb generator.
+#endif
+
+
+
+const pb_field_t grpc_gcp_Endpoint_fields[4] = {
+ PB_FIELD( 1, STRING , OPTIONAL, CALLBACK, FIRST, grpc_gcp_Endpoint, ip_address, ip_address, 0),
+ PB_FIELD( 2, INT32 , OPTIONAL, STATIC , OTHER, grpc_gcp_Endpoint, port, ip_address, 0),
+ PB_FIELD( 3, UENUM , OPTIONAL, STATIC , OTHER, grpc_gcp_Endpoint, protocol, port, 0),
+ PB_LAST_FIELD
+};
+
+const pb_field_t grpc_gcp_Identity_fields[3] = {
+ PB_FIELD( 1, STRING , OPTIONAL, CALLBACK, FIRST, grpc_gcp_Identity, service_account, service_account, 0),
+ PB_FIELD( 2, STRING , OPTIONAL, CALLBACK, OTHER, grpc_gcp_Identity, hostname, service_account, 0),
+ PB_LAST_FIELD
+};
+
+const pb_field_t grpc_gcp_StartClientHandshakeReq_fields[10] = {
+ PB_FIELD( 1, UENUM , OPTIONAL, STATIC , FIRST, grpc_gcp_StartClientHandshakeReq, handshake_security_protocol, handshake_security_protocol, 0),
+ PB_FIELD( 2, STRING , REPEATED, CALLBACK, OTHER, grpc_gcp_StartClientHandshakeReq, application_protocols, handshake_security_protocol, 0),
+ PB_FIELD( 3, STRING , REPEATED, CALLBACK, OTHER, grpc_gcp_StartClientHandshakeReq, record_protocols, application_protocols, 0),
+ PB_FIELD( 4, MESSAGE , REPEATED, CALLBACK, OTHER, grpc_gcp_StartClientHandshakeReq, target_identities, record_protocols, &grpc_gcp_Identity_fields),
+ PB_FIELD( 5, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_StartClientHandshakeReq, local_identity, target_identities, &grpc_gcp_Identity_fields),
+ PB_FIELD( 6, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_StartClientHandshakeReq, local_endpoint, local_identity, &grpc_gcp_Endpoint_fields),
+ PB_FIELD( 7, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_StartClientHandshakeReq, remote_endpoint, local_endpoint, &grpc_gcp_Endpoint_fields),
+ PB_FIELD( 8, STRING , OPTIONAL, CALLBACK, OTHER, grpc_gcp_StartClientHandshakeReq, target_name, remote_endpoint, 0),
+ PB_FIELD( 9, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_StartClientHandshakeReq, rpc_versions, target_name, &grpc_gcp_RpcProtocolVersions_fields),
+ PB_LAST_FIELD
+};
+
+const pb_field_t grpc_gcp_ServerHandshakeParameters_fields[3] = {
+ PB_FIELD( 1, STRING , REPEATED, CALLBACK, FIRST, grpc_gcp_ServerHandshakeParameters, record_protocols, record_protocols, 0),
+ PB_FIELD( 2, MESSAGE , REPEATED, CALLBACK, OTHER, grpc_gcp_ServerHandshakeParameters, local_identities, record_protocols, &grpc_gcp_Identity_fields),
+ PB_LAST_FIELD
+};
+
+const pb_field_t grpc_gcp_StartServerHandshakeReq_fields[7] = {
+ PB_FIELD( 1, STRING , REPEATED, CALLBACK, FIRST, grpc_gcp_StartServerHandshakeReq, application_protocols, application_protocols, 0),
+ PB_FIELD( 2, MESSAGE , REPEATED, STATIC , OTHER, grpc_gcp_StartServerHandshakeReq, handshake_parameters, application_protocols, &grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry_fields),
+ PB_FIELD( 3, BYTES , OPTIONAL, CALLBACK, OTHER, grpc_gcp_StartServerHandshakeReq, in_bytes, handshake_parameters, 0),
+ PB_FIELD( 4, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_StartServerHandshakeReq, local_endpoint, in_bytes, &grpc_gcp_Endpoint_fields),
+ PB_FIELD( 5, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_StartServerHandshakeReq, remote_endpoint, local_endpoint, &grpc_gcp_Endpoint_fields),
+ PB_FIELD( 6, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_StartServerHandshakeReq, rpc_versions, remote_endpoint, &grpc_gcp_RpcProtocolVersions_fields),
+ PB_LAST_FIELD
+};
+
+const pb_field_t grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry_fields[3] = {
+ PB_FIELD( 1, INT32 , OPTIONAL, STATIC , FIRST, grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry, key, key, 0),
+ PB_FIELD( 2, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry, value, key, &grpc_gcp_ServerHandshakeParameters_fields),
+ PB_LAST_FIELD
+};
+
+const pb_field_t grpc_gcp_NextHandshakeMessageReq_fields[2] = {
+ PB_FIELD( 1, BYTES , OPTIONAL, CALLBACK, FIRST, grpc_gcp_NextHandshakeMessageReq, in_bytes, in_bytes, 0),
+ PB_LAST_FIELD
+};
+
+const pb_field_t grpc_gcp_HandshakerReq_fields[4] = {
+ PB_FIELD( 1, MESSAGE , OPTIONAL, STATIC , FIRST, grpc_gcp_HandshakerReq, client_start, client_start, &grpc_gcp_StartClientHandshakeReq_fields),
+ PB_FIELD( 2, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_HandshakerReq, server_start, client_start, &grpc_gcp_StartServerHandshakeReq_fields),
+ PB_FIELD( 3, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_HandshakerReq, next, server_start, &grpc_gcp_NextHandshakeMessageReq_fields),
+ PB_LAST_FIELD
+};
+
+const pb_field_t grpc_gcp_HandshakerResult_fields[8] = {
+ PB_FIELD( 1, STRING , OPTIONAL, CALLBACK, FIRST, grpc_gcp_HandshakerResult, application_protocol, application_protocol, 0),
+ PB_FIELD( 2, STRING , OPTIONAL, CALLBACK, OTHER, grpc_gcp_HandshakerResult, record_protocol, application_protocol, 0),
+ PB_FIELD( 3, BYTES , OPTIONAL, CALLBACK, OTHER, grpc_gcp_HandshakerResult, key_data, record_protocol, 0),
+ PB_FIELD( 4, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_HandshakerResult, peer_identity, key_data, &grpc_gcp_Identity_fields),
+ PB_FIELD( 5, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_HandshakerResult, local_identity, peer_identity, &grpc_gcp_Identity_fields),
+ PB_FIELD( 6, BOOL , OPTIONAL, STATIC , OTHER, grpc_gcp_HandshakerResult, keep_channel_open, local_identity, 0),
+ PB_FIELD( 7, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_HandshakerResult, peer_rpc_versions, keep_channel_open, &grpc_gcp_RpcProtocolVersions_fields),
+ PB_LAST_FIELD
+};
+
+const pb_field_t grpc_gcp_HandshakerStatus_fields[3] = {
+ PB_FIELD( 1, UINT32 , OPTIONAL, STATIC , FIRST, grpc_gcp_HandshakerStatus, code, code, 0),
+ PB_FIELD( 2, STRING , OPTIONAL, CALLBACK, OTHER, grpc_gcp_HandshakerStatus, details, code, 0),
+ PB_LAST_FIELD
+};
+
+const pb_field_t grpc_gcp_HandshakerResp_fields[5] = {
+ PB_FIELD( 1, BYTES , OPTIONAL, CALLBACK, FIRST, grpc_gcp_HandshakerResp, out_frames, out_frames, 0),
+ PB_FIELD( 2, UINT32 , OPTIONAL, STATIC , OTHER, grpc_gcp_HandshakerResp, bytes_consumed, out_frames, 0),
+ PB_FIELD( 3, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_HandshakerResp, result, bytes_consumed, &grpc_gcp_HandshakerResult_fields),
+ PB_FIELD( 4, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_HandshakerResp, status, result, &grpc_gcp_HandshakerStatus_fields),
+ PB_LAST_FIELD
+};
+
+
+/* Check that field information fits in pb_field_t */
+#if !defined(PB_FIELD_32BIT)
+/* If you get an error here, it means that you need to define PB_FIELD_32BIT
+ * compile-time option. You can do that in pb.h or on compiler command line.
+ *
+ * The reason you need to do this is that some of your messages contain tag
+ * numbers or field sizes that are larger than what can fit in 8 or 16 bit
+ * field descriptors.
+ */
+PB_STATIC_ASSERT((pb_membersize(grpc_gcp_StartClientHandshakeReq, target_identities) < 65536 && pb_membersize(grpc_gcp_StartClientHandshakeReq, local_identity) < 65536 && pb_membersize(grpc_gcp_StartClientHandshakeReq, local_endpoint) < 65536 && pb_membersize(grpc_gcp_StartClientHandshakeReq, remote_endpoint) < 65536 && pb_membersize(grpc_gcp_StartClientHandshakeReq, rpc_versions) < 65536 && pb_membersize(grpc_gcp_ServerHandshakeParameters, local_identities) < 65536 && pb_membersize(grpc_gcp_StartServerHandshakeReq, handshake_parameters[0]) < 65536 && pb_membersize(grpc_gcp_StartServerHandshakeReq, local_endpoint) < 65536 && pb_membersize(grpc_gcp_StartServerHandshakeReq, remote_endpoint) < 65536 && pb_membersize(grpc_gcp_StartServerHandshakeReq, rpc_versions) < 65536 && pb_membersize(grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry, value) < 65536 && pb_membersize(grpc_gcp_HandshakerReq, client_start) < 65536 && pb_membersize(grpc_gcp_HandshakerReq, server_start) < 65536 && pb_membersize(grpc_gcp_HandshakerReq, next) < 65536 && pb_membersize(grpc_gcp_HandshakerResult, peer_identity) < 65536 && pb_membersize(grpc_gcp_HandshakerResult, local_identity) < 65536 && pb_membersize(grpc_gcp_HandshakerResult, peer_rpc_versions) < 65536 && pb_membersize(grpc_gcp_HandshakerResp, result) < 65536 && pb_membersize(grpc_gcp_HandshakerResp, status) < 65536), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_grpc_gcp_Endpoint_grpc_gcp_Identity_grpc_gcp_StartClientHandshakeReq_grpc_gcp_ServerHandshakeParameters_grpc_gcp_StartServerHandshakeReq_grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry_grpc_gcp_NextHandshakeMessageReq_grpc_gcp_HandshakerReq_grpc_gcp_HandshakerResult_grpc_gcp_HandshakerStatus_grpc_gcp_HandshakerResp)
+#endif
+
+#if !defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)
+/* If you get an error here, it means that you need to define PB_FIELD_16BIT
+ * compile-time option. You can do that in pb.h or on compiler command line.
+ *
+ * The reason you need to do this is that some of your messages contain tag
+ * numbers or field sizes that are larger than what can fit in the default
+ * 8 bit descriptors.
+ */
+PB_STATIC_ASSERT((pb_membersize(grpc_gcp_StartClientHandshakeReq, target_identities) < 256 && pb_membersize(grpc_gcp_StartClientHandshakeReq, local_identity) < 256 && pb_membersize(grpc_gcp_StartClientHandshakeReq, local_endpoint) < 256 && pb_membersize(grpc_gcp_StartClientHandshakeReq, remote_endpoint) < 256 && pb_membersize(grpc_gcp_StartClientHandshakeReq, rpc_versions) < 256 && pb_membersize(grpc_gcp_ServerHandshakeParameters, local_identities) < 256 && pb_membersize(grpc_gcp_StartServerHandshakeReq, handshake_parameters[0]) < 256 && pb_membersize(grpc_gcp_StartServerHandshakeReq, local_endpoint) < 256 && pb_membersize(grpc_gcp_StartServerHandshakeReq, remote_endpoint) < 256 && pb_membersize(grpc_gcp_StartServerHandshakeReq, rpc_versions) < 256 && pb_membersize(grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry, value) < 256 && pb_membersize(grpc_gcp_HandshakerReq, client_start) < 256 && pb_membersize(grpc_gcp_HandshakerReq, server_start) < 256 && pb_membersize(grpc_gcp_HandshakerReq, next) < 256 && pb_membersize(grpc_gcp_HandshakerResult, peer_identity) < 256 && pb_membersize(grpc_gcp_HandshakerResult, local_identity) < 256 && pb_membersize(grpc_gcp_HandshakerResult, peer_rpc_versions) < 256 && pb_membersize(grpc_gcp_HandshakerResp, result) < 256 && pb_membersize(grpc_gcp_HandshakerResp, status) < 256), YOU_MUST_DEFINE_PB_FIELD_16BIT_FOR_MESSAGES_grpc_gcp_Endpoint_grpc_gcp_Identity_grpc_gcp_StartClientHandshakeReq_grpc_gcp_ServerHandshakeParameters_grpc_gcp_StartServerHandshakeReq_grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry_grpc_gcp_NextHandshakeMessageReq_grpc_gcp_HandshakerReq_grpc_gcp_HandshakerResult_grpc_gcp_HandshakerStatus_grpc_gcp_HandshakerResp)
+#endif
+
+
+/* @@protoc_insertion_point(eof) */
diff --git a/src/core/tsi/alts/handshaker/handshaker.pb.h b/src/core/tsi/alts/handshaker/handshaker.pb.h
new file mode 100644
index 0000000000..0805a144de
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/handshaker.pb.h
@@ -0,0 +1,255 @@
+/* Automatically generated nanopb header */
+/* Generated by nanopb-0.3.7-dev */
+
+#ifndef PB_GRPC_GCP_HANDSHAKER_PB_H_INCLUDED
+#define PB_GRPC_GCP_HANDSHAKER_PB_H_INCLUDED
+#include "third_party/nanopb/pb.h"
+#include "src/core/tsi/alts/handshaker/transport_security_common.pb.h"
+
+/* @@protoc_insertion_point(includes) */
+#if PB_PROTO_HEADER_VERSION != 30
+#error Regenerate this file with the current version of nanopb generator.
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Enum definitions */
+typedef enum _grpc_gcp_HandshakeProtocol {
+ grpc_gcp_HandshakeProtocol_HANDSHAKE_PROTOCOL_UNSPECIFIED = 0,
+ grpc_gcp_HandshakeProtocol_TLS = 1,
+ grpc_gcp_HandshakeProtocol_ALTS = 2
+} grpc_gcp_HandshakeProtocol;
+#define _grpc_gcp_HandshakeProtocol_MIN grpc_gcp_HandshakeProtocol_HANDSHAKE_PROTOCOL_UNSPECIFIED
+#define _grpc_gcp_HandshakeProtocol_MAX grpc_gcp_HandshakeProtocol_ALTS
+#define _grpc_gcp_HandshakeProtocol_ARRAYSIZE ((grpc_gcp_HandshakeProtocol)(grpc_gcp_HandshakeProtocol_ALTS+1))
+
+typedef enum _grpc_gcp_NetworkProtocol {
+ grpc_gcp_NetworkProtocol_NETWORK_PROTOCOL_UNSPECIFIED = 0,
+ grpc_gcp_NetworkProtocol_TCP = 1,
+ grpc_gcp_NetworkProtocol_UDP = 2
+} grpc_gcp_NetworkProtocol;
+#define _grpc_gcp_NetworkProtocol_MIN grpc_gcp_NetworkProtocol_NETWORK_PROTOCOL_UNSPECIFIED
+#define _grpc_gcp_NetworkProtocol_MAX grpc_gcp_NetworkProtocol_UDP
+#define _grpc_gcp_NetworkProtocol_ARRAYSIZE ((grpc_gcp_NetworkProtocol)(grpc_gcp_NetworkProtocol_UDP+1))
+
+/* Struct definitions */
+typedef struct _grpc_gcp_Identity {
+ pb_callback_t service_account;
+ pb_callback_t hostname;
+/* @@protoc_insertion_point(struct:grpc_gcp_Identity) */
+} grpc_gcp_Identity;
+
+typedef struct _grpc_gcp_NextHandshakeMessageReq {
+ pb_callback_t in_bytes;
+/* @@protoc_insertion_point(struct:grpc_gcp_NextHandshakeMessageReq) */
+} grpc_gcp_NextHandshakeMessageReq;
+
+typedef struct _grpc_gcp_ServerHandshakeParameters {
+ pb_callback_t record_protocols;
+ pb_callback_t local_identities;
+/* @@protoc_insertion_point(struct:grpc_gcp_ServerHandshakeParameters) */
+} grpc_gcp_ServerHandshakeParameters;
+
+typedef struct _grpc_gcp_Endpoint {
+ pb_callback_t ip_address;
+ bool has_port;
+ int32_t port;
+ bool has_protocol;
+ grpc_gcp_NetworkProtocol protocol;
+/* @@protoc_insertion_point(struct:grpc_gcp_Endpoint) */
+} grpc_gcp_Endpoint;
+
+typedef struct _grpc_gcp_HandshakerResult {
+ pb_callback_t application_protocol;
+ pb_callback_t record_protocol;
+ pb_callback_t key_data;
+ bool has_peer_identity;
+ grpc_gcp_Identity peer_identity;
+ bool has_local_identity;
+ grpc_gcp_Identity local_identity;
+ bool has_keep_channel_open;
+ bool keep_channel_open;
+ bool has_peer_rpc_versions;
+ grpc_gcp_RpcProtocolVersions peer_rpc_versions;
+/* @@protoc_insertion_point(struct:grpc_gcp_HandshakerResult) */
+} grpc_gcp_HandshakerResult;
+
+typedef struct _grpc_gcp_HandshakerStatus {
+ bool has_code;
+ uint32_t code;
+ pb_callback_t details;
+/* @@protoc_insertion_point(struct:grpc_gcp_HandshakerStatus) */
+} grpc_gcp_HandshakerStatus;
+
+typedef struct _grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry {
+ bool has_key;
+ int32_t key;
+ bool has_value;
+ grpc_gcp_ServerHandshakeParameters value;
+/* @@protoc_insertion_point(struct:grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry) */
+} grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry;
+
+typedef struct _grpc_gcp_HandshakerResp {
+ pb_callback_t out_frames;
+ bool has_bytes_consumed;
+ uint32_t bytes_consumed;
+ bool has_result;
+ grpc_gcp_HandshakerResult result;
+ bool has_status;
+ grpc_gcp_HandshakerStatus status;
+/* @@protoc_insertion_point(struct:grpc_gcp_HandshakerResp) */
+} grpc_gcp_HandshakerResp;
+
+typedef struct _grpc_gcp_StartClientHandshakeReq {
+ bool has_handshake_security_protocol;
+ grpc_gcp_HandshakeProtocol handshake_security_protocol;
+ pb_callback_t application_protocols;
+ pb_callback_t record_protocols;
+ pb_callback_t target_identities;
+ bool has_local_identity;
+ grpc_gcp_Identity local_identity;
+ bool has_local_endpoint;
+ grpc_gcp_Endpoint local_endpoint;
+ bool has_remote_endpoint;
+ grpc_gcp_Endpoint remote_endpoint;
+ pb_callback_t target_name;
+ bool has_rpc_versions;
+ grpc_gcp_RpcProtocolVersions rpc_versions;
+/* @@protoc_insertion_point(struct:grpc_gcp_StartClientHandshakeReq) */
+} grpc_gcp_StartClientHandshakeReq;
+
+typedef struct _grpc_gcp_StartServerHandshakeReq {
+ pb_callback_t application_protocols;
+ pb_size_t handshake_parameters_count;
+ grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry handshake_parameters[3];
+ pb_callback_t in_bytes;
+ bool has_local_endpoint;
+ grpc_gcp_Endpoint local_endpoint;
+ bool has_remote_endpoint;
+ grpc_gcp_Endpoint remote_endpoint;
+ bool has_rpc_versions;
+ grpc_gcp_RpcProtocolVersions rpc_versions;
+/* @@protoc_insertion_point(struct:grpc_gcp_StartServerHandshakeReq) */
+} grpc_gcp_StartServerHandshakeReq;
+
+typedef struct _grpc_gcp_HandshakerReq {
+ bool has_client_start;
+ grpc_gcp_StartClientHandshakeReq client_start;
+ bool has_server_start;
+ grpc_gcp_StartServerHandshakeReq server_start;
+ bool has_next;
+ grpc_gcp_NextHandshakeMessageReq next;
+/* @@protoc_insertion_point(struct:grpc_gcp_HandshakerReq) */
+} grpc_gcp_HandshakerReq;
+
+/* Default values for struct fields */
+
+/* Initializer values for message structs */
+#define grpc_gcp_Endpoint_init_default {{{NULL}, NULL}, false, 0, false, (grpc_gcp_NetworkProtocol)0}
+#define grpc_gcp_Identity_init_default {{{NULL}, NULL}, {{NULL}, NULL}}
+#define grpc_gcp_StartClientHandshakeReq_init_default {false, (grpc_gcp_HandshakeProtocol)0, {{NULL}, NULL}, {{NULL}, NULL}, {{NULL}, NULL}, false, grpc_gcp_Identity_init_default, false, grpc_gcp_Endpoint_init_default, false, grpc_gcp_Endpoint_init_default, {{NULL}, NULL}, false, grpc_gcp_RpcProtocolVersions_init_default}
+#define grpc_gcp_ServerHandshakeParameters_init_default {{{NULL}, NULL}, {{NULL}, NULL}}
+#define grpc_gcp_StartServerHandshakeReq_init_default {{{NULL}, NULL}, 0, {grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry_init_default, grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry_init_default, grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry_init_default}, {{NULL}, NULL}, false, grpc_gcp_Endpoint_init_default, false, grpc_gcp_Endpoint_init_default, false, grpc_gcp_RpcProtocolVersions_init_default}
+#define grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry_init_default {false, 0, false, grpc_gcp_ServerHandshakeParameters_init_default}
+#define grpc_gcp_NextHandshakeMessageReq_init_default {{{NULL}, NULL}}
+#define grpc_gcp_HandshakerReq_init_default {false, grpc_gcp_StartClientHandshakeReq_init_default, false, grpc_gcp_StartServerHandshakeReq_init_default, false, grpc_gcp_NextHandshakeMessageReq_init_default}
+#define grpc_gcp_HandshakerResult_init_default {{{NULL}, NULL}, {{NULL}, NULL}, {{NULL}, NULL}, false, grpc_gcp_Identity_init_default, false, grpc_gcp_Identity_init_default, false, 0, false, grpc_gcp_RpcProtocolVersions_init_default}
+#define grpc_gcp_HandshakerStatus_init_default {false, 0, {{NULL}, NULL}}
+#define grpc_gcp_HandshakerResp_init_default {{{NULL}, NULL}, false, 0, false, grpc_gcp_HandshakerResult_init_default, false, grpc_gcp_HandshakerStatus_init_default}
+#define grpc_gcp_Endpoint_init_zero {{{NULL}, NULL}, false, 0, false, (grpc_gcp_NetworkProtocol)0}
+#define grpc_gcp_Identity_init_zero {{{NULL}, NULL}, {{NULL}, NULL}}
+#define grpc_gcp_StartClientHandshakeReq_init_zero {false, (grpc_gcp_HandshakeProtocol)0, {{NULL}, NULL}, {{NULL}, NULL}, {{NULL}, NULL}, false, grpc_gcp_Identity_init_zero, false, grpc_gcp_Endpoint_init_zero, false, grpc_gcp_Endpoint_init_zero, {{NULL}, NULL}, false, grpc_gcp_RpcProtocolVersions_init_zero}
+#define grpc_gcp_ServerHandshakeParameters_init_zero {{{NULL}, NULL}, {{NULL}, NULL}}
+#define grpc_gcp_StartServerHandshakeReq_init_zero {{{NULL}, NULL}, 0, {grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry_init_zero, grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry_init_zero, grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry_init_zero}, {{NULL}, NULL}, false, grpc_gcp_Endpoint_init_zero, false, grpc_gcp_Endpoint_init_zero, false, grpc_gcp_RpcProtocolVersions_init_zero}
+#define grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry_init_zero {false, 0, false, grpc_gcp_ServerHandshakeParameters_init_zero}
+#define grpc_gcp_NextHandshakeMessageReq_init_zero {{{NULL}, NULL}}
+#define grpc_gcp_HandshakerReq_init_zero {false, grpc_gcp_StartClientHandshakeReq_init_zero, false, grpc_gcp_StartServerHandshakeReq_init_zero, false, grpc_gcp_NextHandshakeMessageReq_init_zero}
+#define grpc_gcp_HandshakerResult_init_zero {{{NULL}, NULL}, {{NULL}, NULL}, {{NULL}, NULL}, false, grpc_gcp_Identity_init_zero, false, grpc_gcp_Identity_init_zero, false, 0, false, grpc_gcp_RpcProtocolVersions_init_zero}
+#define grpc_gcp_HandshakerStatus_init_zero {false, 0, {{NULL}, NULL}}
+#define grpc_gcp_HandshakerResp_init_zero {{{NULL}, NULL}, false, 0, false, grpc_gcp_HandshakerResult_init_zero, false, grpc_gcp_HandshakerStatus_init_zero}
+
+/* Field tags (for use in manual encoding/decoding) */
+#define grpc_gcp_Identity_service_account_tag 1
+#define grpc_gcp_Identity_hostname_tag 2
+#define grpc_gcp_NextHandshakeMessageReq_in_bytes_tag 1
+#define grpc_gcp_ServerHandshakeParameters_record_protocols_tag 1
+#define grpc_gcp_ServerHandshakeParameters_local_identities_tag 2
+#define grpc_gcp_Endpoint_ip_address_tag 1
+#define grpc_gcp_Endpoint_port_tag 2
+#define grpc_gcp_Endpoint_protocol_tag 3
+#define grpc_gcp_HandshakerResult_application_protocol_tag 1
+#define grpc_gcp_HandshakerResult_record_protocol_tag 2
+#define grpc_gcp_HandshakerResult_key_data_tag 3
+#define grpc_gcp_HandshakerResult_peer_identity_tag 4
+#define grpc_gcp_HandshakerResult_local_identity_tag 5
+#define grpc_gcp_HandshakerResult_keep_channel_open_tag 6
+#define grpc_gcp_HandshakerResult_peer_rpc_versions_tag 7
+#define grpc_gcp_HandshakerStatus_code_tag 1
+#define grpc_gcp_HandshakerStatus_details_tag 2
+#define grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry_key_tag 1
+#define grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry_value_tag 2
+#define grpc_gcp_HandshakerResp_out_frames_tag 1
+#define grpc_gcp_HandshakerResp_bytes_consumed_tag 2
+#define grpc_gcp_HandshakerResp_result_tag 3
+#define grpc_gcp_HandshakerResp_status_tag 4
+#define grpc_gcp_StartClientHandshakeReq_handshake_security_protocol_tag 1
+#define grpc_gcp_StartClientHandshakeReq_application_protocols_tag 2
+#define grpc_gcp_StartClientHandshakeReq_record_protocols_tag 3
+#define grpc_gcp_StartClientHandshakeReq_target_identities_tag 4
+#define grpc_gcp_StartClientHandshakeReq_local_identity_tag 5
+#define grpc_gcp_StartClientHandshakeReq_local_endpoint_tag 6
+#define grpc_gcp_StartClientHandshakeReq_remote_endpoint_tag 7
+#define grpc_gcp_StartClientHandshakeReq_target_name_tag 8
+#define grpc_gcp_StartClientHandshakeReq_rpc_versions_tag 9
+#define grpc_gcp_StartServerHandshakeReq_application_protocols_tag 1
+#define grpc_gcp_StartServerHandshakeReq_handshake_parameters_tag 2
+#define grpc_gcp_StartServerHandshakeReq_in_bytes_tag 3
+#define grpc_gcp_StartServerHandshakeReq_local_endpoint_tag 4
+#define grpc_gcp_StartServerHandshakeReq_remote_endpoint_tag 5
+#define grpc_gcp_StartServerHandshakeReq_rpc_versions_tag 6
+#define grpc_gcp_HandshakerReq_client_start_tag 1
+#define grpc_gcp_HandshakerReq_server_start_tag 2
+#define grpc_gcp_HandshakerReq_next_tag 3
+
+/* Struct field encoding specification for nanopb */
+extern const pb_field_t grpc_gcp_Endpoint_fields[4];
+extern const pb_field_t grpc_gcp_Identity_fields[3];
+extern const pb_field_t grpc_gcp_StartClientHandshakeReq_fields[10];
+extern const pb_field_t grpc_gcp_ServerHandshakeParameters_fields[3];
+extern const pb_field_t grpc_gcp_StartServerHandshakeReq_fields[7];
+extern const pb_field_t grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry_fields[3];
+extern const pb_field_t grpc_gcp_NextHandshakeMessageReq_fields[2];
+extern const pb_field_t grpc_gcp_HandshakerReq_fields[4];
+extern const pb_field_t grpc_gcp_HandshakerResult_fields[8];
+extern const pb_field_t grpc_gcp_HandshakerStatus_fields[3];
+extern const pb_field_t grpc_gcp_HandshakerResp_fields[5];
+
+/* Maximum encoded size of messages (where known) */
+/* grpc_gcp_Endpoint_size depends on runtime parameters */
+/* grpc_gcp_Identity_size depends on runtime parameters */
+/* grpc_gcp_StartClientHandshakeReq_size depends on runtime parameters */
+/* grpc_gcp_ServerHandshakeParameters_size depends on runtime parameters */
+/* grpc_gcp_StartServerHandshakeReq_size depends on runtime parameters */
+#define grpc_gcp_StartServerHandshakeReq_HandshakeParametersEntry_size (17 + grpc_gcp_ServerHandshakeParameters_size)
+/* grpc_gcp_NextHandshakeMessageReq_size depends on runtime parameters */
+#define grpc_gcp_HandshakerReq_size (18 + grpc_gcp_StartClientHandshakeReq_size + grpc_gcp_StartServerHandshakeReq_size + grpc_gcp_NextHandshakeMessageReq_size)
+/* grpc_gcp_HandshakerResult_size depends on runtime parameters */
+/* grpc_gcp_HandshakerStatus_size depends on runtime parameters */
+/* grpc_gcp_HandshakerResp_size depends on runtime parameters */
+
+/* Message IDs (where set with "msgid" option) */
+#ifdef PB_MSGID
+
+#define HANDSHAKER_MESSAGES \
+
+
+#endif
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+/* @@protoc_insertion_point(eof) */
+
+#endif
diff --git a/src/core/tsi/alts/handshaker/proto/altscontext.proto b/src/core/tsi/alts/handshaker/proto/altscontext.proto
new file mode 100644
index 0000000000..d195b37e08
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/proto/altscontext.proto
@@ -0,0 +1,41 @@
+// Copyright 2018 gRPC authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+syntax = "proto3";
+
+import "transport_security_common.proto";
+
+package grpc.gcp;
+
+option java_package = "io.grpc.alts";
+
+message AltsContext {
+ // The application protocol negotiated for this connection.
+ string application_protocol = 1;
+
+ // The record protocol negotiated for this connection.
+ string record_protocol = 2;
+
+ // The security level of the created secure channel.
+ SecurityLevel security_level = 3;
+
+ // The peer service account.
+ string peer_service_account = 4;
+
+ // The local service account.
+ string local_service_account = 5;
+
+ // The RPC protocol versions supported by the peer.
+ RpcProtocolVersions peer_rpc_versions = 6;
+}
diff --git a/src/core/tsi/alts/handshaker/proto/handshaker.options b/src/core/tsi/alts/handshaker/proto/handshaker.options
new file mode 100644
index 0000000000..702ba3802a
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/proto/handshaker.options
@@ -0,0 +1,2 @@
+handshaker.proto no_unions:true
+grpc.gcp.StartServerHandshakeReq.handshake_parameters max_count:3
diff --git a/src/core/tsi/alts/handshaker/proto/handshaker.proto b/src/core/tsi/alts/handshaker/proto/handshaker.proto
new file mode 100644
index 0000000000..46b8b09eb0
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/proto/handshaker.proto
@@ -0,0 +1,220 @@
+// Copyright 2018 gRPC authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+syntax = "proto3";
+
+import "transport_security_common.proto";
+
+package grpc.gcp;
+
+option java_package = "io.grpc.alts";
+
+enum HandshakeProtocol {
+ // Default value.
+ HANDSHAKE_PROTOCOL_UNSPECIFIED = 0;
+
+ // TLS handshake protocol.
+ TLS = 1;
+
+ // Application Layer Transport Security handshake protocol.
+ ALTS = 2;
+}
+
+enum NetworkProtocol {
+ NETWORK_PROTOCOL_UNSPECIFIED = 0;
+ TCP = 1;
+ UDP = 2;
+}
+
+message Endpoint {
+ // IP address. It should contain an IPv4 or IPv6 string literal, e.g.
+ // "192.168.0.1" or "2001:db8::1".
+ string ip_address = 1;
+
+ // Port number.
+ int32 port = 2;
+
+ // Network protocol (e.g., TCP, UDP) associated with this endpoint.
+ NetworkProtocol protocol = 3;
+}
+
+message Identity {
+ oneof identity_oneof {
+ // Service account of a connection endpoint.
+ string service_account = 1;
+
+ // Hostname of a connection endpoint.
+ string hostname = 2;
+ }
+}
+
+message StartClientHandshakeReq {
+ // Handshake security protocol requested by the client.
+ HandshakeProtocol handshake_security_protocol = 1;
+
+ // The application protocols supported by the client, e.g., "h2" (for http2),
+ // "grpc".
+ repeated string application_protocols = 2;
+
+ // The record protocols supported by the client, e.g.,
+ // "ALTSRP_GCM_AES128".
+ repeated string record_protocols = 3;
+
+ // (Optional) Describes which server identities are acceptable by the client.
+ // If target identities are provided and none of them matches the peer
+ // identity of the server, handshake will fail.
+ repeated Identity target_identities = 4;
+
+ // (Optional) Application may specify a local identity. Otherwise, the
+ // handshaker chooses a default local identity.
+ Identity local_identity = 5;
+
+ // (Optional) Local endpoint information of the connection to the server,
+ // such as local IP address, port number, and network protocol.
+ Endpoint local_endpoint = 6;
+
+ // (Optional) Endpoint information of the remote server, such as IP address,
+ // port number, and network protocool.
+ Endpoint remote_endpoint = 7;
+
+ // (Optional) If target name is provided, a secure naming check is performed
+ // to verify that the peer authenticated identity is indeed authorized to run
+ // the target name.
+ string target_name = 8;
+
+ // (Optional) RPC protocol versions supported by the client.
+ RpcProtocolVersions rpc_versions = 9;
+}
+
+message ServerHandshakeParameters {
+ // The record protocols supported by the server, e.g.,
+ // "ALTSRP_GCM_AES128".
+ repeated string record_protocols = 1;
+
+ // (Optional) A list of local identities supported by the server, if
+ // specified. Otherwise, the handshaker chooses a default local identity.
+ repeated Identity local_identities = 2;
+}
+
+message StartServerHandshakeReq {
+ // The application protocols supported by the server, e.g., "h2" (for http2),
+ // "grpc".
+ repeated string application_protocols = 1;
+
+ // Handshake parameters (record protocols and local identities supported by
+ // the server) mapped by the handshake protocol. Each handshake security
+ // protocol (e.g., TLS or ALTS) has its own set of record protocols and local
+ // identities. Since protobuf does not support enum as key to the map, the key
+ // to handshake_parameters is the integer value of HandshakeProtocol enum.
+ map<int32, ServerHandshakeParameters> handshake_parameters = 2;
+
+ // Bytes in out_frames returned from the peer's HandshakerResp. It is possible
+ // that the peer's out_frames are split into multiple HandshakReq messages.
+ bytes in_bytes = 3;
+
+ // (Optional) Local endpoint information of the connection to the client,
+ // such as local IP address, port number, and network protocol.
+ Endpoint local_endpoint = 4;
+
+ // (Optional) Endpoint information of the remote client, such as IP address,
+ // port number, and network protocool.
+ Endpoint remote_endpoint = 5;
+
+ // (Optional) RPC protocol versions supported by the server.
+ RpcProtocolVersions rpc_versions = 6;
+}
+
+message NextHandshakeMessageReq {
+ // Bytes in out_frames returned from the peer's HandshakerResp. It is possible
+ // that the peer's out_frames are split into multiple NextHandshakerMessageReq
+ // messages.
+ bytes in_bytes = 1;
+}
+
+message HandshakerReq {
+ oneof req_oneof {
+ // The start client handshake request message.
+ StartClientHandshakeReq client_start = 1;
+
+ // The start server handshake request message.
+ StartServerHandshakeReq server_start = 2;
+
+ // The next handshake request message.
+ NextHandshakeMessageReq next = 3;
+ }
+}
+
+message HandshakerResult {
+ // The application protocol negotiated for this connection.
+ string application_protocol = 1;
+
+ // The record protocol negotiated for this connection.
+ string record_protocol = 2;
+
+ // Cryptographic key data. The key data may be more than the key length
+ // required for the record protocol, thus the client of the handshaker
+ // service needs to truncate the key data into the right key length.
+ bytes key_data = 3;
+
+ // The authenticated identity of the peer.
+ Identity peer_identity = 4;
+
+ // The local identity used in the handshake.
+ Identity local_identity = 5;
+
+ // Indicate whether the handshaker service client should keep the channel
+ // between the handshaker service open, e.g., in order to handle
+ // post-handshake messages in the future.
+ bool keep_channel_open = 6;
+
+ // The RPC protocol versions supported by the peer.
+ RpcProtocolVersions peer_rpc_versions = 7;
+}
+
+message HandshakerStatus {
+ // The status code. This could be the gRPC status code.
+ uint32 code = 1;
+
+ // The status details.
+ string details = 2;
+}
+
+message HandshakerResp {
+ // Frames to be given to the peer for the NextHandshakeMessageReq. May be
+ // empty if no out_frames have to be sent to the peer or if in_bytes in the
+ // HandshakerReq are incomplete. All the non-empty out frames must be sent to
+ // the peer even if the handshaker status is not OK as these frames may
+ // contain the alert frames.
+ bytes out_frames = 1;
+
+ // Number of bytes in the in_bytes consumed by the handshaker. It is possible
+ // that part of in_bytes in HandshakerReq was unrelated to the handshake
+ // process.
+ uint32 bytes_consumed = 2;
+
+ // This is set iff the handshake was successful. out_frames may still be set
+ // to frames that needs to be forwarded to the peer.
+ HandshakerResult result = 3;
+
+ // Status of the handshaker.
+ HandshakerStatus status = 4;
+}
+
+service HandshakerService {
+ // Accepts a stream of handshaker request, returning a stream of handshaker
+ // response.
+ rpc DoHandshake(stream HandshakerReq)
+ returns (stream HandshakerResp) {
+ }
+}
diff --git a/src/core/tsi/alts/handshaker/proto/transport_security_common.proto b/src/core/tsi/alts/handshaker/proto/transport_security_common.proto
new file mode 100644
index 0000000000..41983ab9f9
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/proto/transport_security_common.proto
@@ -0,0 +1,40 @@
+// Copyright 2018 gRPC authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+syntax = "proto3";
+
+package grpc.gcp;
+
+option java_package = "io.grpc.alts";
+
+// The security level of the created channel. The list is sorted in increasing
+// level of security. This order must always be maintained.
+enum SecurityLevel {
+ SECURITY_NONE = 0;
+ INTEGRITY_ONLY = 1;
+ INTEGRITY_AND_PRIVACY = 2;
+}
+
+// Max and min supported RPC protocol versions.
+message RpcProtocolVersions {
+ // RPC version contains a major version and a minor version.
+ message Version {
+ uint32 major = 1;
+ uint32 minor = 2;
+ }
+ // Maximum supported RPC version.
+ Version max_rpc_version = 1;
+ // Minimum supported RPC version.
+ Version min_rpc_version = 2;
+}
diff --git a/src/core/tsi/alts/handshaker/transport_security_common.pb.c b/src/core/tsi/alts/handshaker/transport_security_common.pb.c
new file mode 100644
index 0000000000..6063c7625e
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/transport_security_common.pb.c
@@ -0,0 +1,50 @@
+/* Automatically generated nanopb constant definitions */
+/* Generated by nanopb-0.3.7-dev */
+
+#include "src/core/tsi/alts/handshaker/transport_security_common.pb.h"
+
+/* @@protoc_insertion_point(includes) */
+#if PB_PROTO_HEADER_VERSION != 30
+#error Regenerate this file with the current version of nanopb generator.
+#endif
+
+
+
+const pb_field_t grpc_gcp_RpcProtocolVersions_fields[3] = {
+ PB_FIELD( 1, MESSAGE , OPTIONAL, STATIC , FIRST, grpc_gcp_RpcProtocolVersions, max_rpc_version, max_rpc_version, &grpc_gcp_RpcProtocolVersions_Version_fields),
+ PB_FIELD( 2, MESSAGE , OPTIONAL, STATIC , OTHER, grpc_gcp_RpcProtocolVersions, min_rpc_version, max_rpc_version, &grpc_gcp_RpcProtocolVersions_Version_fields),
+ PB_LAST_FIELD
+};
+
+const pb_field_t grpc_gcp_RpcProtocolVersions_Version_fields[3] = {
+ PB_FIELD( 1, UINT32 , OPTIONAL, STATIC , FIRST, grpc_gcp_RpcProtocolVersions_Version, major, major, 0),
+ PB_FIELD( 2, UINT32 , OPTIONAL, STATIC , OTHER, grpc_gcp_RpcProtocolVersions_Version, minor, major, 0),
+ PB_LAST_FIELD
+};
+
+
+/* Check that field information fits in pb_field_t */
+#if !defined(PB_FIELD_32BIT)
+/* If you get an error here, it means that you need to define PB_FIELD_32BIT
+ * compile-time option. You can do that in pb.h or on compiler command line.
+ *
+ * The reason you need to do this is that some of your messages contain tag
+ * numbers or field sizes that are larger than what can fit in 8 or 16 bit
+ * field descriptors.
+ */
+PB_STATIC_ASSERT((pb_membersize(grpc_gcp_RpcProtocolVersions, max_rpc_version) < 65536 && pb_membersize(grpc_gcp_RpcProtocolVersions, min_rpc_version) < 65536), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_grpc_gcp_RpcProtocolVersions_grpc_gcp_RpcProtocolVersions_Version)
+#endif
+
+#if !defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)
+/* If you get an error here, it means that you need to define PB_FIELD_16BIT
+ * compile-time option. You can do that in pb.h or on compiler command line.
+ *
+ * The reason you need to do this is that some of your messages contain tag
+ * numbers or field sizes that are larger than what can fit in the default
+ * 8 bit descriptors.
+ */
+PB_STATIC_ASSERT((pb_membersize(grpc_gcp_RpcProtocolVersions, max_rpc_version) < 256 && pb_membersize(grpc_gcp_RpcProtocolVersions, min_rpc_version) < 256), YOU_MUST_DEFINE_PB_FIELD_16BIT_FOR_MESSAGES_grpc_gcp_RpcProtocolVersions_grpc_gcp_RpcProtocolVersions_Version)
+#endif
+
+
+/* @@protoc_insertion_point(eof) */
diff --git a/src/core/tsi/alts/handshaker/transport_security_common.pb.h b/src/core/tsi/alts/handshaker/transport_security_common.pb.h
new file mode 100644
index 0000000000..49096dffa3
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/transport_security_common.pb.h
@@ -0,0 +1,78 @@
+/* Automatically generated nanopb header */
+/* Generated by nanopb-0.3.7-dev */
+
+#ifndef PB_GRPC_GCP_TRANSPORT_SECURITY_COMMON_PB_H_INCLUDED
+#define PB_GRPC_GCP_TRANSPORT_SECURITY_COMMON_PB_H_INCLUDED
+#include "third_party/nanopb/pb.h"
+/* @@protoc_insertion_point(includes) */
+#if PB_PROTO_HEADER_VERSION != 30
+#error Regenerate this file with the current version of nanopb generator.
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Enum definitions */
+typedef enum _grpc_gcp_SecurityLevel {
+ grpc_gcp_SecurityLevel_SECURITY_NONE = 0,
+ grpc_gcp_SecurityLevel_INTEGRITY_ONLY = 1,
+ grpc_gcp_SecurityLevel_INTEGRITY_AND_PRIVACY = 2
+} grpc_gcp_SecurityLevel;
+#define _grpc_gcp_SecurityLevel_MIN grpc_gcp_SecurityLevel_SECURITY_NONE
+#define _grpc_gcp_SecurityLevel_MAX grpc_gcp_SecurityLevel_INTEGRITY_AND_PRIVACY
+#define _grpc_gcp_SecurityLevel_ARRAYSIZE ((grpc_gcp_SecurityLevel)(grpc_gcp_SecurityLevel_INTEGRITY_AND_PRIVACY+1))
+
+/* Struct definitions */
+typedef struct _grpc_gcp_RpcProtocolVersions_Version {
+ bool has_major;
+ uint32_t major;
+ bool has_minor;
+ uint32_t minor;
+/* @@protoc_insertion_point(struct:grpc_gcp_RpcProtocolVersions_Version) */
+} grpc_gcp_RpcProtocolVersions_Version;
+
+typedef struct _grpc_gcp_RpcProtocolVersions {
+ bool has_max_rpc_version;
+ grpc_gcp_RpcProtocolVersions_Version max_rpc_version;
+ bool has_min_rpc_version;
+ grpc_gcp_RpcProtocolVersions_Version min_rpc_version;
+/* @@protoc_insertion_point(struct:grpc_gcp_RpcProtocolVersions) */
+} grpc_gcp_RpcProtocolVersions;
+
+/* Default values for struct fields */
+
+/* Initializer values for message structs */
+#define grpc_gcp_RpcProtocolVersions_init_default {false, grpc_gcp_RpcProtocolVersions_Version_init_default, false, grpc_gcp_RpcProtocolVersions_Version_init_default}
+#define grpc_gcp_RpcProtocolVersions_Version_init_default {false, 0, false, 0}
+#define grpc_gcp_RpcProtocolVersions_init_zero {false, grpc_gcp_RpcProtocolVersions_Version_init_zero, false, grpc_gcp_RpcProtocolVersions_Version_init_zero}
+#define grpc_gcp_RpcProtocolVersions_Version_init_zero {false, 0, false, 0}
+
+/* Field tags (for use in manual encoding/decoding) */
+#define grpc_gcp_RpcProtocolVersions_Version_major_tag 1
+#define grpc_gcp_RpcProtocolVersions_Version_minor_tag 2
+#define grpc_gcp_RpcProtocolVersions_max_rpc_version_tag 1
+#define grpc_gcp_RpcProtocolVersions_min_rpc_version_tag 2
+
+/* Struct field encoding specification for nanopb */
+extern const pb_field_t grpc_gcp_RpcProtocolVersions_fields[3];
+extern const pb_field_t grpc_gcp_RpcProtocolVersions_Version_fields[3];
+
+/* Maximum encoded size of messages (where known) */
+#define grpc_gcp_RpcProtocolVersions_size 28
+#define grpc_gcp_RpcProtocolVersions_Version_size 12
+
+/* Message IDs (where set with "msgid" option) */
+#ifdef PB_MSGID
+
+#define TRANSPORT_SECURITY_COMMON_MESSAGES \
+
+
+#endif
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+/* @@protoc_insertion_point(eof) */
+
+#endif
diff --git a/src/core/tsi/alts/handshaker/transport_security_common_api.cc b/src/core/tsi/alts/handshaker/transport_security_common_api.cc
new file mode 100644
index 0000000000..8a7edb53d4
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/transport_security_common_api.cc
@@ -0,0 +1,196 @@
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <grpc/support/port_platform.h>
+
+#include "src/core/tsi/alts/handshaker/transport_security_common_api.h"
+
+bool grpc_gcp_rpc_protocol_versions_set_max(
+ grpc_gcp_rpc_protocol_versions* versions, uint32_t max_major,
+ uint32_t max_minor) {
+ if (versions == nullptr) {
+ gpr_log(GPR_ERROR,
+ "versions is nullptr in "
+ "grpc_gcp_rpc_protocol_versions_set_max().");
+ return false;
+ }
+ versions->has_max_rpc_version = true;
+ versions->max_rpc_version.has_major = true;
+ versions->max_rpc_version.has_minor = true;
+ versions->max_rpc_version.major = max_major;
+ versions->max_rpc_version.minor = max_minor;
+ return true;
+}
+
+bool grpc_gcp_rpc_protocol_versions_set_min(
+ grpc_gcp_rpc_protocol_versions* versions, uint32_t min_major,
+ uint32_t min_minor) {
+ if (versions == nullptr) {
+ gpr_log(GPR_ERROR,
+ "versions is nullptr in "
+ "grpc_gcp_rpc_protocol_versions_set_min().");
+ return false;
+ }
+ versions->has_min_rpc_version = true;
+ versions->min_rpc_version.has_major = true;
+ versions->min_rpc_version.has_minor = true;
+ versions->min_rpc_version.major = min_major;
+ versions->min_rpc_version.minor = min_minor;
+ return true;
+}
+
+size_t grpc_gcp_rpc_protocol_versions_encode_length(
+ const grpc_gcp_rpc_protocol_versions* versions) {
+ if (versions == nullptr) {
+ gpr_log(GPR_ERROR,
+ "Invalid nullptr arguments to "
+ "grpc_gcp_rpc_protocol_versions_encode_length().");
+ return 0;
+ }
+ pb_ostream_t size_stream;
+ memset(&size_stream, 0, sizeof(pb_ostream_t));
+ if (!pb_encode(&size_stream, grpc_gcp_RpcProtocolVersions_fields, versions)) {
+ gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&size_stream));
+ return 0;
+ }
+ return size_stream.bytes_written;
+}
+
+bool grpc_gcp_rpc_protocol_versions_encode_to_raw_bytes(
+ const grpc_gcp_rpc_protocol_versions* versions, uint8_t* bytes,
+ size_t bytes_length) {
+ if (versions == nullptr || bytes == nullptr || bytes_length == 0) {
+ gpr_log(GPR_ERROR,
+ "Invalid nullptr arguments to "
+ "grpc_gcp_rpc_protocol_versions_encode_to_raw_bytes().");
+ return false;
+ }
+ pb_ostream_t output_stream = pb_ostream_from_buffer(bytes, bytes_length);
+ if (!pb_encode(&output_stream, grpc_gcp_RpcProtocolVersions_fields,
+ versions)) {
+ gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&output_stream));
+ return false;
+ }
+ return true;
+}
+
+bool grpc_gcp_rpc_protocol_versions_encode(
+ const grpc_gcp_rpc_protocol_versions* versions, grpc_slice* slice) {
+ if (versions == nullptr || slice == nullptr) {
+ gpr_log(GPR_ERROR,
+ "Invalid nullptr arguments to "
+ "grpc_gcp_rpc_protocol_versions_encode().");
+ return false;
+ }
+ size_t encoded_length =
+ grpc_gcp_rpc_protocol_versions_encode_length(versions);
+ if (encoded_length == 0) return false;
+ *slice = grpc_slice_malloc(encoded_length);
+ return grpc_gcp_rpc_protocol_versions_encode_to_raw_bytes(
+ versions, GRPC_SLICE_START_PTR(*slice), encoded_length);
+}
+
+bool grpc_gcp_rpc_protocol_versions_decode(
+ grpc_slice slice, grpc_gcp_rpc_protocol_versions* versions) {
+ if (versions == nullptr) {
+ gpr_log(GPR_ERROR,
+ "version is nullptr in "
+ "grpc_gcp_rpc_protocol_versions_decode().");
+ return false;
+ }
+ pb_istream_t stream = pb_istream_from_buffer(GRPC_SLICE_START_PTR(slice),
+ GRPC_SLICE_LENGTH(slice));
+ if (!pb_decode(&stream, grpc_gcp_RpcProtocolVersions_fields, versions)) {
+ gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream));
+ return false;
+ }
+ return true;
+}
+
+bool grpc_gcp_rpc_protocol_versions_copy(
+ const grpc_gcp_rpc_protocol_versions* src,
+ grpc_gcp_rpc_protocol_versions* dst) {
+ if ((src == nullptr && dst != nullptr) ||
+ (src != nullptr && dst == nullptr)) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to "
+ "grpc_gcp_rpc_protocol_versions_copy().");
+ return false;
+ }
+ if (src == nullptr) {
+ return true;
+ }
+ grpc_gcp_rpc_protocol_versions_set_max(dst, src->max_rpc_version.major,
+ src->max_rpc_version.minor);
+ grpc_gcp_rpc_protocol_versions_set_min(dst, src->min_rpc_version.major,
+ src->min_rpc_version.minor);
+ return true;
+}
+
+namespace grpc_core {
+namespace internal {
+
+int grpc_gcp_rpc_protocol_version_compare(
+ const grpc_gcp_rpc_protocol_versions_version* v1,
+ const grpc_gcp_rpc_protocol_versions_version* v2) {
+ if ((v1->major > v2->major) ||
+ (v1->major == v2->major && v1->minor > v2->minor)) {
+ return 1;
+ }
+ if ((v1->major < v2->major) ||
+ (v1->major == v2->major && v1->minor < v2->minor)) {
+ return -1;
+ }
+ return 0;
+}
+
+} // namespace internal
+} // namespace grpc_core
+
+bool grpc_gcp_rpc_protocol_versions_check(
+ const grpc_gcp_rpc_protocol_versions* local_versions,
+ const grpc_gcp_rpc_protocol_versions* peer_versions,
+ grpc_gcp_rpc_protocol_versions_version* highest_common_version) {
+ if (local_versions == nullptr || peer_versions == nullptr) {
+ gpr_log(GPR_ERROR,
+ "Invalid arguments to "
+ "grpc_gcp_rpc_protocol_versions_check().");
+ return false;
+ }
+ /* max_common_version is MIN(local.max, peer.max) */
+ const grpc_gcp_rpc_protocol_versions_version* max_common_version =
+ grpc_core::internal::grpc_gcp_rpc_protocol_version_compare(
+ &local_versions->max_rpc_version, &peer_versions->max_rpc_version) > 0
+ ? &peer_versions->max_rpc_version
+ : &local_versions->max_rpc_version;
+ /* min_common_version is MAX(local.min, peer.min) */
+ const grpc_gcp_rpc_protocol_versions_version* min_common_version =
+ grpc_core::internal::grpc_gcp_rpc_protocol_version_compare(
+ &local_versions->min_rpc_version, &peer_versions->min_rpc_version) > 0
+ ? &local_versions->min_rpc_version
+ : &peer_versions->min_rpc_version;
+ bool result = grpc_core::internal::grpc_gcp_rpc_protocol_version_compare(
+ max_common_version, min_common_version) >= 0
+ ? true
+ : false;
+ if (result && highest_common_version != nullptr) {
+ memcpy(highest_common_version, max_common_version,
+ sizeof(grpc_gcp_rpc_protocol_versions_version));
+ }
+ return result;
+}
diff --git a/src/core/tsi/alts/handshaker/transport_security_common_api.h b/src/core/tsi/alts/handshaker/transport_security_common_api.h
new file mode 100644
index 0000000000..68228cb3b5
--- /dev/null
+++ b/src/core/tsi/alts/handshaker/transport_security_common_api.h
@@ -0,0 +1,163 @@
+/*
+ *
+ * Copyright 2018 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_TSI_ALTS_HANDSHAKER_TRANSPORT_SECURITY_COMMON_API_H
+#define GRPC_CORE_TSI_ALTS_HANDSHAKER_TRANSPORT_SECURITY_COMMON_API_H
+
+#include <grpc/support/port_platform.h>
+
+#include "third_party/nanopb/pb_decode.h"
+#include "third_party/nanopb/pb_encode.h"
+
+#include <grpc/slice.h>
+#include <grpc/slice_buffer.h>
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+
+#include "src/core/tsi/alts/handshaker/transport_security_common.pb.h"
+
+typedef grpc_gcp_RpcProtocolVersions grpc_gcp_rpc_protocol_versions;
+
+typedef grpc_gcp_RpcProtocolVersions_Version
+ grpc_gcp_rpc_protocol_versions_version;
+
+/**
+ * This method sets the value for max_rpc_versions field of rpc protocol
+ * versions.
+ *
+ * - versions: an rpc protocol version instance.
+ * - max_major: a major version of maximum supported RPC version.
+ * - max_minor: a minor version of maximum supported RPC version.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_rpc_protocol_versions_set_max(
+ grpc_gcp_rpc_protocol_versions* versions, uint32_t max_major,
+ uint32_t max_minor);
+
+/**
+ * This method sets the value for min_rpc_versions field of rpc protocol
+ * versions.
+ *
+ * - versions: an rpc protocol version instance.
+ * - min_major: a major version of minimum supported RPC version.
+ * - min_minor: a minor version of minimum supported RPC version.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_rpc_protocol_versions_set_min(
+ grpc_gcp_rpc_protocol_versions* versions, uint32_t min_major,
+ uint32_t min_minor);
+
+/**
+ * This method computes serialized byte length of rpc protocol versions.
+ *
+ * - versions: an rpc protocol versions instance.
+ *
+ * The method returns serialized byte length. It returns 0 on failure.
+ */
+size_t grpc_gcp_rpc_protocol_versions_encode_length(
+ const grpc_gcp_rpc_protocol_versions* versions);
+
+/**
+ * This method serializes rpc protocol versions and writes the result to
+ * the memory buffer provided by the caller. Caller is responsible for
+ * allocating sufficient memory to store the serialized data.
+ *
+ * - versions: an rpc protocol versions instance.
+ * - bytes: bytes buffer where the result will be written to.
+ * - bytes_length: length of the bytes buffer.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_rpc_protocol_versions_encode_to_raw_bytes(
+ const grpc_gcp_rpc_protocol_versions* versions, uint8_t* bytes,
+ size_t bytes_length);
+
+/**
+ * This method serializes an rpc protocol version and returns serialized rpc
+ * versions in grpc slice.
+ *
+ * - versions: an rpc protocol versions instance.
+ * - slice: grpc slice where the serialized result will be written.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_rpc_protocol_versions_encode(
+ const grpc_gcp_rpc_protocol_versions* versions, grpc_slice* slice);
+
+/**
+ * This method de-serializes input in grpc slice form and stores the result
+ * in rpc protocol versions.
+ *
+ * - slice: a data stream containing a serialized rpc protocol version.
+ * - versions: an rpc protocol version instance used to hold de-serialized
+ * result.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_rpc_protocol_versions_decode(
+ grpc_slice slice, grpc_gcp_rpc_protocol_versions* versions);
+
+/**
+ * This method performs a deep copy operation on rpc protocol versions
+ * instance.
+ *
+ * - src: rpc protocol versions instance that needs to be copied.
+ * - dst: rpc protocol versions instance that stores the copied result.
+ *
+ * The method returns true on success and false otherwise.
+ */
+bool grpc_gcp_rpc_protocol_versions_copy(
+ const grpc_gcp_rpc_protocol_versions* src,
+ grpc_gcp_rpc_protocol_versions* dst);
+
+/**
+ * This method performs a version check between local and peer rpc protocol
+ * versions.
+ *
+ * - local_versions: local rpc protocol versions instance.
+ * - peer_versions: peer rpc protocol versions instance.
+ * - highest_common_version: an output parameter that will store the highest
+ * common rpc protocol version both parties agreed on.
+ *
+ * The method returns true if the check passes which means both parties agreed
+ * on a common rpc protocol to use, and false otherwise.
+ */
+bool grpc_gcp_rpc_protocol_versions_check(
+ const grpc_gcp_rpc_protocol_versions* local_versions,
+ const grpc_gcp_rpc_protocol_versions* peer_versions,
+ grpc_gcp_rpc_protocol_versions_version* highest_common_version);
+
+namespace grpc_core {
+namespace internal {
+
+/**
+ * Exposed for testing only.
+ * The method returns 0 if v1 = v2,
+ * returns 1 if v1 > v2,
+ * returns -1 if v1 < v2.
+ */
+int grpc_gcp_rpc_protocol_version_compare(
+ const grpc_gcp_rpc_protocol_versions_version* v1,
+ const grpc_gcp_rpc_protocol_versions_version* v2);
+
+} // namespace internal
+} // namespace grpc_core
+
+#endif /* GRPC_CORE_TSI_ALTS_HANDSHAKER_TRANSPORT_SECURITY_COMMON_API_H */