buffet: Add TLS support to XMPP connection

Properly handle <starttls> XMPP request from the server by initiating
TLS handshake.

BUG=brillo:191
TEST=`FEATURES=test emerge-link buffet`
     Manually test on LINK DUT and inspecting logs to make sure TLS
     connection to XMPP server is established successfully.

Change-Id: I94d8b5eb9e29402fc3d662afcfdbf2e0c5ec1a02
Reviewed-on: https://chromium-review.googlesource.com/272263
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/notification/xmpp_channel.h b/buffet/notification/xmpp_channel.h
index d6b1550..4cf540e 100644
--- a/buffet/notification/xmpp_channel.h
+++ b/buffet/notification/xmpp_channel.h
@@ -43,6 +43,8 @@
   enum class XmppState {
     kNotStarted,
     kStarted,
+    kTlsStarted,
+    kTlsCompleted,
     kAuthenticationStarted,
     kAuthenticationFailed,
     kStreamRestartedPostAuthentication,
@@ -71,6 +73,10 @@
   void HandleStanza(std::unique_ptr<XmlNode> stanza);
   void RestartXmppStream();
 
+  void StartTlsHandshake();
+  void OnTlsHandshakeComplete(chromeos::StreamPtr tls_stream);
+  void OnTlsError(const chromeos::Error* error);
+
   void SendMessage(const std::string& message);
   void WaitForMessage();
 
@@ -88,6 +94,7 @@
   std::string access_token_;
 
   chromeos::StreamPtr raw_socket_;
+  chromeos::StreamPtr tls_stream_;
 
   // Read buffer for incoming message packets.
   std::vector<char> read_socket_data_;
@@ -95,6 +102,10 @@
   std::string write_socket_data_;
   std::string queued_write_data_;
 
+  // XMPP server name and port used for connection.
+  std::string host_;
+  uint16_t port_{0};
+
   chromeos::BackoffEntry backoff_entry_;
   NotificationDelegate* delegate_{nullptr};
   scoped_refptr<base::TaskRunner> task_runner_;