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 "src/core/tsi/alts/frame_protector/alts_record_protocol_crypter_common.h" 22 23 #include <grpc/support/alloc.h> 24 25 static void maybe_copy_error_msg(const char* src, char** dst) { 26 if (dst != nullptr && src != nullptr) { 27 *dst = static_cast<char*>(gpr_malloc(strlen(src) + 1)); 28 memcpy(*dst, src, strlen(src) + 1); 29 } 30 } 31 32 grpc_status_code input_sanity_check( 33 const alts_record_protocol_crypter* rp_crypter, const unsigned char* data, 34 size_t* output_size, char** error_details) { 35 if (rp_crypter == nullptr) { 36 maybe_copy_error_msg("alts_crypter instance is nullptr.", error_details); 37 return GRPC_STATUS_INVALID_ARGUMENT; 38 } else if (data == nullptr) { 39 maybe_copy_error_msg("data is nullptr.", error_details); 40 return GRPC_STATUS_INVALID_ARGUMENT; 41 } else if (output_size == nullptr) { 42 maybe_copy_error_msg("output_size is nullptr.", error_details); 43 return GRPC_STATUS_INVALID_ARGUMENT; 44 } 45 return GRPC_STATUS_OK; 46 } 47 48 grpc_status_code increment_counter(alts_record_protocol_crypter* rp_crypter, 49 char** error_details) { 50 bool is_overflow = false; 51 grpc_status_code status = 52 alts_counter_increment(rp_crypter->ctr, &is_overflow, error_details); 53 if (status != GRPC_STATUS_OK) { 54 return status; 55 } 56 if (is_overflow) { 57 const char error_msg[] = 58 "crypter counter is wrapped. The connection" 59 "should be closed and the key should be deleted."; 60 maybe_copy_error_msg(error_msg, error_details); 61 return GRPC_STATUS_INTERNAL; 62 } 63 return GRPC_STATUS_OK; 64 } 65 66 size_t alts_record_protocol_crypter_num_overhead_bytes(const alts_crypter* c) { 67 if (c != nullptr) { 68 size_t num_overhead_bytes = 0; 69 char* error_details = nullptr; 70 const alts_record_protocol_crypter* rp_crypter = 71 reinterpret_cast<const alts_record_protocol_crypter*>(c); 72 grpc_status_code status = gsec_aead_crypter_tag_length( 73 rp_crypter->crypter, &num_overhead_bytes, &error_details); 74 if (status == GRPC_STATUS_OK) { 75 return num_overhead_bytes; 76 } 77 } 78 return 0; 79 } 80 81 void alts_record_protocol_crypter_destruct(alts_crypter* c) { 82 if (c != nullptr) { 83 alts_record_protocol_crypter* rp_crypter = 84 reinterpret_cast<alts_record_protocol_crypter*>(c); 85 alts_counter_destroy(rp_crypter->ctr); 86 gsec_aead_crypter_destroy(rp_crypter->crypter); 87 } 88 } 89 90 alts_record_protocol_crypter* alts_crypter_create_common( 91 gsec_aead_crypter* crypter, bool is_client, size_t overflow_size, 92 char** error_details) { 93 if (crypter != nullptr) { 94 auto* rp_crypter = static_cast<alts_record_protocol_crypter*>( 95 gpr_malloc(sizeof(alts_record_protocol_crypter))); 96 size_t counter_size = 0; 97 grpc_status_code status = 98 gsec_aead_crypter_nonce_length(crypter, &counter_size, error_details); 99 if (status != GRPC_STATUS_OK) { 100 return nullptr; 101 } 102 /* Create a counter. */ 103 status = alts_counter_create(is_client, counter_size, overflow_size, 104 &rp_crypter->ctr, error_details); 105 if (status != GRPC_STATUS_OK) { 106 return nullptr; 107 } 108 rp_crypter->crypter = crypter; 109 return rp_crypter; 110 } 111 const char error_msg[] = "crypter is nullptr."; 112 maybe_copy_error_msg(error_msg, error_details); 113 return nullptr; 114 } 115