xref: /aosp_15_r20/external/libbrillo/brillo/streams/tls_stream.h (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright 2015 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li 
5*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_STREAMS_TLS_STREAM_H_
6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_STREAMS_TLS_STREAM_H_
7*1a96fba6SXin Li 
8*1a96fba6SXin Li #include <memory>
9*1a96fba6SXin Li #include <string>
10*1a96fba6SXin Li 
11*1a96fba6SXin Li #include <base/macros.h>
12*1a96fba6SXin Li #include <brillo/brillo_export.h>
13*1a96fba6SXin Li #include <brillo/errors/error.h>
14*1a96fba6SXin Li #include <brillo/streams/stream.h>
15*1a96fba6SXin Li 
16*1a96fba6SXin Li namespace brillo {
17*1a96fba6SXin Li 
18*1a96fba6SXin Li // This class provides client-side TLS stream that performs handshake with the
19*1a96fba6SXin Li // server and established a secure communication channel which can be used
20*1a96fba6SXin Li // by performing read/write operations on this stream. Both synchronous and
21*1a96fba6SXin Li // asynchronous I/O is supported.
22*1a96fba6SXin Li // The underlying socket stream must already be created and connected to the
23*1a96fba6SXin Li // destination server and passed in TlsStream::Connect() method as |socket|.
24*1a96fba6SXin Li class BRILLO_EXPORT TlsStream : public Stream {
25*1a96fba6SXin Li  public:
26*1a96fba6SXin Li   ~TlsStream() override;
27*1a96fba6SXin Li 
28*1a96fba6SXin Li   // Perform a TLS handshake and establish secure connection over |socket|.
29*1a96fba6SXin Li   // Calls |callback| when successful and passes the instance of TlsStream
30*1a96fba6SXin Li   // as an argument. In case of an error, |error_callback| is called.
31*1a96fba6SXin Li   // |host| must specify the expected remote host (server) name.
32*1a96fba6SXin Li   static void Connect(
33*1a96fba6SXin Li       StreamPtr socket,
34*1a96fba6SXin Li       const std::string& host,
35*1a96fba6SXin Li       const base::Callback<void(StreamPtr)>& success_callback,
36*1a96fba6SXin Li       const Stream::ErrorCallback& error_callback);
37*1a96fba6SXin Li 
38*1a96fba6SXin Li   // Overrides from Stream:
39*1a96fba6SXin Li   bool IsOpen() const override;
CanRead()40*1a96fba6SXin Li   bool CanRead() const override { return true; }
CanWrite()41*1a96fba6SXin Li   bool CanWrite() const override { return true; }
CanSeek()42*1a96fba6SXin Li   bool CanSeek() const override { return false; }
CanGetSize()43*1a96fba6SXin Li   bool CanGetSize() const override { return false; }
GetSize()44*1a96fba6SXin Li   uint64_t GetSize() const override { return 0; }
45*1a96fba6SXin Li   bool SetSizeBlocking(uint64_t size, ErrorPtr* error) override;
GetRemainingSize()46*1a96fba6SXin Li   uint64_t GetRemainingSize() const override { return 0; }
GetPosition()47*1a96fba6SXin Li   uint64_t GetPosition() const override { return 0; }
48*1a96fba6SXin Li   bool Seek(int64_t offset,
49*1a96fba6SXin Li             Whence whence,
50*1a96fba6SXin Li             uint64_t* new_position,
51*1a96fba6SXin Li             ErrorPtr* error) override;
52*1a96fba6SXin Li   bool ReadNonBlocking(void* buffer,
53*1a96fba6SXin Li                        size_t size_to_read,
54*1a96fba6SXin Li                        size_t* size_read,
55*1a96fba6SXin Li                        bool* end_of_stream,
56*1a96fba6SXin Li                        ErrorPtr* error) override;
57*1a96fba6SXin Li   bool WriteNonBlocking(const void* buffer,
58*1a96fba6SXin Li                         size_t size_to_write,
59*1a96fba6SXin Li                         size_t* size_written,
60*1a96fba6SXin Li                         ErrorPtr* error) override;
61*1a96fba6SXin Li   bool FlushBlocking(ErrorPtr* error) override;
62*1a96fba6SXin Li   bool CloseBlocking(ErrorPtr* error) override;
63*1a96fba6SXin Li   bool WaitForData(AccessMode mode,
64*1a96fba6SXin Li                    const base::Callback<void(AccessMode)>& callback,
65*1a96fba6SXin Li                    ErrorPtr* error) override;
66*1a96fba6SXin Li   bool WaitForDataBlocking(AccessMode in_mode,
67*1a96fba6SXin Li                            base::TimeDelta timeout,
68*1a96fba6SXin Li                            AccessMode* out_mode,
69*1a96fba6SXin Li                            ErrorPtr* error) override;
70*1a96fba6SXin Li   void CancelPendingAsyncOperations() override;
71*1a96fba6SXin Li 
72*1a96fba6SXin Li  private:
73*1a96fba6SXin Li   class TlsStreamImpl;
74*1a96fba6SXin Li 
75*1a96fba6SXin Li   // Private constructor called from TlsStream::Connect() factory method.
76*1a96fba6SXin Li   explicit TlsStream(std::unique_ptr<TlsStreamImpl> impl);
77*1a96fba6SXin Li 
78*1a96fba6SXin Li   std::unique_ptr<TlsStreamImpl> impl_;
79*1a96fba6SXin Li   DISALLOW_COPY_AND_ASSIGN(TlsStream);
80*1a96fba6SXin Li };
81*1a96fba6SXin Li 
82*1a96fba6SXin Li }  // namespace brillo
83*1a96fba6SXin Li 
84*1a96fba6SXin Li #endif  // LIBBRILLO_BRILLO_STREAMS_TLS_STREAM_H_
85