1 // Copyright 2021 The BoringSSL Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include <openssl/base.h> 16 17 #include <functional> 18 #include <memory> 19 #include <vector> 20 21 #include <openssl/span.h> 22 23 24 namespace bssl { 25 namespace acvp { 26 27 // kMaxArgs is the maximum number of arguments (including the function name) 28 // that an ACVP request can contain. 29 constexpr size_t kMaxArgs = 9; 30 // kMaxNameLength is the maximum length of a function name in an ACVP request. 31 constexpr size_t kMaxNameLength = 30; 32 33 // RequestBuffer holds various buffers needed for parsing an ACVP request. It 34 // can be reused between requests. 35 class RequestBuffer { 36 public: 37 virtual ~RequestBuffer(); 38 39 static std::unique_ptr<RequestBuffer> New(); 40 }; 41 42 // ParseArgsFromFd returns a span of arguments, the first of which is the name 43 // of the requested function, from |fd|. The return values point into |buffer| 44 // and so must not be used after |buffer| has been freed or reused for a 45 // subsequent call. It returns an empty span on error, because std::optional 46 // is still too new. 47 Span<const Span<const uint8_t>> ParseArgsFromFd(int fd, RequestBuffer *buffer); 48 49 // WriteReplyToFd writes a reply to the given file descriptor. 50 bool WriteReplyToFd(int fd, const std::vector<Span<const uint8_t>> &spans); 51 52 // WriteReplyToBuffer writes a reply to an internal buffer that may be flushed 53 // with |FlushBuffer|. 54 bool WriteReplyToBuffer(const std::vector<Span<const uint8_t>> &spans); 55 56 // FlushBuffer writes the buffer that |WriteReplyToBuffer| fills, to |fd|. 57 bool FlushBuffer(int fd); 58 59 // ReplyCallback is the type of a callback that writes a reply to an ACVP 60 // request. 61 typedef std::function<bool(const std::vector<Span<const uint8_t>> &)> 62 ReplyCallback; 63 64 // Handler is the type of a function that handles a specific ACVP request. If 65 // successful it will call |write_reply| with the response arguments and return 66 // |write_reply|'s return value. Otherwise it will return false. The given args 67 // must not include the name at the beginning. 68 typedef bool (*Handler)(const Span<const uint8_t> args[], 69 ReplyCallback write_reply); 70 71 // FindHandler returns a |Handler| that can process the given arguments, or logs 72 // a reason and returns |nullptr| if none is found. 73 Handler FindHandler(Span<const Span<const uint8_t>> args); 74 75 } // namespace acvp 76 } // namespace bssl 77