1 /*
2 *
3 * Copyright 2018 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include <grpc/support/alloc.h>
22
23 #include "src/core/tsi/alts/frame_protector/alts_counter.h"
24 #include "src/core/tsi/alts/frame_protector/alts_crypter.h"
25 #include "src/core/tsi/alts/frame_protector/alts_record_protocol_crypter_common.h"
26
maybe_copy_error_msg(const char * src,char ** dst)27 static void maybe_copy_error_msg(const char* src, char** dst) {
28 if (dst != nullptr && src != nullptr) {
29 *dst = static_cast<char*>(gpr_malloc(strlen(src) + 1));
30 memcpy(*dst, src, strlen(src) + 1);
31 }
32 }
33
34 /* Perform input santity check. */
unseal_check(alts_crypter * c,const unsigned char * data,size_t data_allocated_size,size_t data_size,size_t * output_size,char ** error_details)35 static grpc_status_code unseal_check(alts_crypter* c, const unsigned char* data,
36 size_t data_allocated_size,
37 size_t data_size, size_t* output_size,
38 char** error_details) {
39 /* Do common input sanity check. */
40 grpc_status_code status = input_sanity_check(
41 reinterpret_cast<const alts_record_protocol_crypter*>(c), data,
42 output_size, error_details);
43 if (status != GRPC_STATUS_OK) {
44 return status;
45 }
46 /* Do unseal-specific input check. */
47 size_t num_overhead_bytes =
48 alts_crypter_num_overhead_bytes(reinterpret_cast<const alts_crypter*>(c));
49 if (num_overhead_bytes > data_size) {
50 const char error_msg[] = "data_size is smaller than num_overhead_bytes.";
51 maybe_copy_error_msg(error_msg, error_details);
52 return GRPC_STATUS_INVALID_ARGUMENT;
53 }
54 return GRPC_STATUS_OK;
55 }
56
alts_unseal_crypter_process_in_place(alts_crypter * c,unsigned char * data,size_t data_allocated_size,size_t data_size,size_t * output_size,char ** error_details)57 static grpc_status_code alts_unseal_crypter_process_in_place(
58 alts_crypter* c, unsigned char* data, size_t data_allocated_size,
59 size_t data_size, size_t* output_size, char** error_details) {
60 grpc_status_code status = unseal_check(c, data, data_allocated_size,
61 data_size, output_size, error_details);
62 if (status != GRPC_STATUS_OK) {
63 return status;
64 }
65 /* Do AEAD decryption. */
66 alts_record_protocol_crypter* rp_crypter =
67 reinterpret_cast<alts_record_protocol_crypter*>(c);
68 status = gsec_aead_crypter_decrypt(
69 rp_crypter->crypter, alts_counter_get_counter(rp_crypter->ctr),
70 alts_counter_get_size(rp_crypter->ctr), nullptr /* aad */,
71 0 /* aad_length */, data, data_size, data, data_allocated_size,
72 output_size, error_details);
73 if (status != GRPC_STATUS_OK) {
74 return status;
75 }
76 /* Increment the crypter counter. */
77 return increment_counter(rp_crypter, error_details);
78 }
79
80 static const alts_crypter_vtable vtable = {
81 alts_record_protocol_crypter_num_overhead_bytes,
82 alts_unseal_crypter_process_in_place,
83 alts_record_protocol_crypter_destruct};
84
alts_unseal_crypter_create(gsec_aead_crypter * gc,bool is_client,size_t overflow_size,alts_crypter ** crypter,char ** error_details)85 grpc_status_code alts_unseal_crypter_create(gsec_aead_crypter* gc,
86 bool is_client,
87 size_t overflow_size,
88 alts_crypter** crypter,
89 char** error_details) {
90 if (crypter == nullptr) {
91 const char error_msg[] = "crypter is nullptr.";
92 maybe_copy_error_msg(error_msg, error_details);
93 return GRPC_STATUS_FAILED_PRECONDITION;
94 }
95 alts_record_protocol_crypter* rp_crypter =
96 alts_crypter_create_common(gc, is_client, overflow_size, error_details);
97 if (rp_crypter == nullptr) {
98 return GRPC_STATUS_FAILED_PRECONDITION;
99 }
100 rp_crypter->base.vtable = &vtable;
101 *crypter = &rp_crypter->base;
102 return GRPC_STATUS_OK;
103 }
104