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