1 /* Copyright (c) 2021, Google Inc. 2 * 3 * Permission to use, copy, modify, and/or distribute this software for any 4 * purpose with or without fee is hereby granted, provided that the above 5 * copyright notice and this permission notice appear in all copies. 6 * 7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ 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 = 8; 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 // ReplyCallback is the type of a callback that writes a reply to an ACVP 53 // request. 54 typedef std::function<bool(const std::vector<Span<const uint8_t>> &)> 55 ReplyCallback; 56 57 // Handler is the type of a function that handles a specific ACVP request. If 58 // successful it will call |write_reply| with the response arguments and return 59 // |write_reply|'s return value. Otherwise it will return false. The given args 60 // must not include the name at the beginning. 61 typedef bool (*Handler)(const Span<const uint8_t> args[], 62 ReplyCallback write_reply); 63 64 // FindHandler returns a |Handler| that can process the given arguments, or logs 65 // a reason and returns |nullptr| if none is found. 66 Handler FindHandler(Span<const Span<const uint8_t>> args); 67 68 } // namespace acvp 69 } // namespace bssl 70