Bob Beck | bc97b7a | 2023-04-18 08:35:15 -0600 | [diff] [blame^] | 1 | // Copyright 2015 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BSSL_PKI_CERT_NET_FETCHER_H_ |
| 6 | #define BSSL_PKI_CERT_NET_FETCHER_H_ |
| 7 | |
| 8 | #include "webutil/url/url.h" |
| 9 | #include "fillins/openssl_util.h" |
| 10 | #include <stdint.h> |
| 11 | |
| 12 | #include <memory> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include <memory> |
| 16 | #include "fillins/net_errors.h" |
| 17 | |
| 18 | |
| 19 | |
| 20 | class URL; |
| 21 | |
| 22 | namespace bssl { |
| 23 | |
| 24 | // CertNetFetcher is a synchronous interface for fetching AIA URLs and CRL |
| 25 | // URLs. It is shared between a caller thread (which starts and waits for |
| 26 | // fetches), and a network thread (which does the actual fetches). It can be |
| 27 | // shutdown from the network thread to cancel outstanding requests. |
| 28 | // |
| 29 | // A Request object is returned when starting a fetch. The consumer can |
| 30 | // use this as a handle for aborting the request (by freeing it), or reading |
| 31 | // the result of the request (WaitForResult) |
| 32 | class OPENSSL_EXPORT CertNetFetcher |
| 33 | { |
| 34 | public: |
| 35 | class Request { |
| 36 | public: |
| 37 | virtual ~Request() = default; |
| 38 | |
| 39 | // WaitForResult() can be called at most once. |
| 40 | // |
| 41 | // It will block and wait for the (network) request to complete, and |
| 42 | // then write the result into the provided out-parameters. |
| 43 | virtual void WaitForResult(Error* error, std::vector<uint8_t>* bytes) = 0; |
| 44 | }; |
| 45 | |
| 46 | // This value can be used in place of timeout or max size limits. |
| 47 | enum { DEFAULT = -1 }; |
| 48 | |
| 49 | CertNetFetcher() = default; |
| 50 | |
| 51 | CertNetFetcher(const CertNetFetcher&) = delete; |
| 52 | CertNetFetcher& operator=(const CertNetFetcher&) = delete; |
| 53 | |
| 54 | // Shuts down the CertNetFetcher and cancels outstanding network requests. It |
| 55 | // is not guaranteed that any outstanding or subsequent |
| 56 | // Request::WaitForResult() calls will be completed. Shutdown() must be called |
| 57 | // from the network thread. It can be called more than once, but must be |
| 58 | // called before the CertNetFetcher is destroyed. |
| 59 | virtual void Shutdown() = 0; |
| 60 | |
| 61 | // The Fetch*() methods start a request which can be cancelled by |
| 62 | // deleting the returned Request. Here is the meaning of the common |
| 63 | // parameters: |
| 64 | // |
| 65 | // * url -- The http:// URL to fetch. |
| 66 | // * timeout_seconds -- The maximum allowed duration for the fetch job. If |
| 67 | // this delay is exceeded then the request will fail. To use a default |
| 68 | // timeout pass DEFAULT. |
| 69 | // * max_response_bytes -- The maximum size of the response body. If this |
| 70 | // size is exceeded then the request will fail. To use a default timeout |
| 71 | // pass DEFAULT. |
| 72 | |
| 73 | [[nodiscard]] virtual std::unique_ptr<Request> FetchCaIssuers( |
| 74 | const URL& url, |
| 75 | int timeout_milliseconds, |
| 76 | int max_response_bytes) = 0; |
| 77 | |
| 78 | [[nodiscard]] virtual std::unique_ptr<Request> FetchCrl( |
| 79 | const URL& url, |
| 80 | int timeout_milliseconds, |
| 81 | int max_response_bytes) = 0; |
| 82 | |
| 83 | [[nodiscard]] virtual std::unique_ptr<Request> FetchOcsp( |
| 84 | const URL& url, |
| 85 | int timeout_milliseconds, |
| 86 | int max_response_bytes) = 0; |
| 87 | |
| 88 | protected: |
| 89 | virtual ~CertNetFetcher() = default; |
| 90 | |
| 91 | private: |
| 92 | |
| 93 | }; |
| 94 | |
| 95 | } // namespace net |
| 96 | |
| 97 | #endif // BSSL_PKI_CERT_NET_FETCHER_H_ |