1 //
2 // Copyright 2024 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "src/core/ext/transport/chttp2/transport/frame_security.h"
18
19 #include <cstddef>
20 #include <cstdint>
21
22 #include "absl/status/status.h"
23 #include "src/core/ext/transport/chttp2/transport/internal.h"
24 #include "src/core/ext/transport/chttp2/transport/legacy_frame.h"
25 #include "src/core/lib/iomgr/event_engine_shims/endpoint.h"
26 #include "src/core/lib/slice/slice.h"
27 #include "src/core/lib/slice/slice_buffer.h"
28 #include "src/core/lib/transport/transport_framing_endpoint_extension.h"
29
grpc_chttp2_security_frame_parser_parse(void * parser,grpc_chttp2_transport * t,grpc_chttp2_stream *,const grpc_slice & slice,int is_last)30 absl::Status grpc_chttp2_security_frame_parser_parse(void* parser,
31 grpc_chttp2_transport* t,
32 grpc_chttp2_stream* /*s*/,
33 const grpc_slice& slice,
34 int is_last) {
35 // Ignore frames from non-EventEngine endpoints.
36 if (t->transport_framing_endpoint_extension == nullptr) {
37 return absl::OkStatus();
38 }
39
40 grpc_chttp2_security_frame_parser* p =
41 static_cast<grpc_chttp2_security_frame_parser*>(parser);
42 p->payload.Append(grpc_core::Slice(grpc_core::CSliceRef(slice)));
43
44 if (is_last) {
45 // Send security frame payload to endpoint.
46 t->transport_framing_endpoint_extension->ReceiveFrame(
47 std::move(p->payload));
48 }
49
50 return absl::OkStatus();
51 }
52
grpc_chttp2_security_frame_parser_begin_frame(grpc_chttp2_security_frame_parser * parser)53 absl::Status grpc_chttp2_security_frame_parser_begin_frame(
54 grpc_chttp2_security_frame_parser* parser) {
55 parser->payload.Clear();
56 return absl::OkStatus();
57 }
58
grpc_chttp2_security_frame_create(grpc_slice_buffer * payload,uint32_t length,grpc_slice_buffer * frame)59 void grpc_chttp2_security_frame_create(grpc_slice_buffer* payload,
60 uint32_t length,
61 grpc_slice_buffer* frame) {
62 grpc_slice hdr;
63 uint8_t* p;
64 static const size_t header_size = 9;
65
66 hdr = GRPC_SLICE_MALLOC(header_size);
67 p = GRPC_SLICE_START_PTR(hdr);
68 *p++ = static_cast<uint8_t>(length >> 16);
69 *p++ = static_cast<uint8_t>(length >> 8);
70 *p++ = static_cast<uint8_t>(length);
71 *p++ = GRPC_CHTTP2_FRAME_SECURITY;
72 *p++ = 0; // no flags
73 *p++ = 0;
74 *p++ = 0;
75 *p++ = 0;
76 *p++ = 0;
77
78 grpc_slice_buffer_add(frame, hdr);
79 grpc_slice_buffer_move_first_no_ref(payload, payload->length, frame);
80 }
81