blob: 348ad44b75bfba14c12ee88ea2965c0cedf45b35 [file] [log] [blame]
Yecheng Zhao08dd6a52021-05-10 15:50:22 -07001.. _module-pw_tls_client:
2
3--------------
4pw_tls_client
5--------------
6
7This module provides a facade that defines the public APIs for establishing TLS
8sessions over arbitrary transports. Two options of backends,
9pw_tls_client_mbedtls and pw_tls_client_boringssl, which are based on BoringSSL
10and MbedTLS libraries, are under construction.
11
12The facade provides a class ``pw::tls_client::Session`` with Open(), Read(),
13Write() and Close() methods for TLS communication. An instance is created by
14``pw::tls_client::Session::Create`` method. The method takes a
15``pw::tls_client::SessionOptions`` object, which is used to configure TLS
16connection options. The list of supported configurations currently include:
17
181. Host name of the target server. This will be used as the Server Name
19Indication(SNI) extension during TLS handshake.
20
212. User-implemented transport. The underlying transport for the TLS
22communication. It is an object that implements the interface of
23``pw::stream::ReaderWriter``.
24
25The module will also provide mechanisms/APIs for users to specify sources of
26trust anchors, time and entropy. These are under construction.
27
28.. warning::
29 This module is under construction, not ready for use, and the documentation
30 is incomplete.
31
Yecheng Zhao4ee81d72021-06-16 22:19:38 -070032Prerequisites
33=============
Yecheng Zhaoe5dbfc02021-06-07 16:38:48 -070034This module requires the following dependencies:
35
361. Entropy
Yecheng Zhao4ee81d72021-06-16 22:19:38 -070037-----------
Yecheng Zhaoe5dbfc02021-06-07 16:38:48 -070038TLS requires an entropy source for generating random bytes. Users of this
39module should provide one by implementing a backend to the
40``pw_tls_client:entropy`` facade.
41
Yecheng Zhao4ee81d72021-06-16 22:19:38 -0700422. Chromium Verifier
43---------------------
44BoringSSL backend uses chromium verifier for certication verification. If the
45downstream project uses BoringSSL as the backend, the sources of the verifier,
46which is part of the chorimum sources, needs to be downloaded in order for
47``//third_party/chromium_verifier`` to build. It is recommended to use our
48support in pw_package for downloading compatible and tested version:
49
50.. code-block:: sh
51
52 pw package install chromium_verifier
53
54Then follow instruction for setting ``dir_pw_third_party_chromium_verifier`` to
55the path of the downloaded repo.
56
Yecheng Zhao08dd6a52021-05-10 15:50:22 -070057Setup
58=====
59This module requires the following setup:
60
61 1. Choose a ``pw_tls_client`` backend, or write one yourself.
62 2. If using GN build, Specify the ``pw_tls_client_BACKEND`` GN build arg to
63 point the library that provides a ``pw_tls_client`` backend.
Yecheng Zhaoe5dbfc02021-06-07 16:38:48 -070064 3. Provide a `pw_tls_client:entropy` backend. If using GN build, specify the
65 backend with variable ``pw_tls_client_ENTROPY_BACKEND``.
Yecheng Zhao08dd6a52021-05-10 15:50:22 -070066
67Module usage
68============
69For GN build, add ``//pw_tls_client`` to the dependency list.
70
71The following gives an example code for using the module on host platform.
72The example uses a Pigweed socket stream as the transport and performs TLS
73connection to www.google.com:
74
75.. code-block:: cpp
76
77 // Host domain name
78 constexpr char kHost[] = "www.google.com";
79
80 constexpr int kPort = 443;
81
82 // Server Name Indication.
83 constexpr const char* kServerNameIndication = kHost;
84
85 // An example message to send.
86 constexpr char kHTTPRequest[] = "GET / HTTP/1.1\r\n\r\n";
87
88 // pw::stream::SocketStream doesn't accept host domain name as input. Thus we
89 // introduce this helper function for getting the IP address
90 pw::Status GetIPAddrFromHostName(std::string_view host, std::span<char> ip) {
91 char null_terminated_host_name[256] = {0};
92 auto host_copy_status = pw::string::Copy(host, null_terminated_host_name);
93 if (!host_copy_status.ok()) {
94 return host_copy_status.status();
95 }
96
97 struct hostent* ent = gethostbyname(null_terminated_host_name);
98 if (ent == NULL) {
99 return PW_STATUS_INTERNAL;
100 }
101
102 in_addr** addr_list = reinterpret_cast<in_addr**>(ent->h_addr_list);
103 if (addr_list[0] == nullptr) {
104 return PW_STATUS_INTERNAL;
105 }
106
107 auto ip_copy_status = pw::string::Copy(inet_ntoa(*addr_list[0]), ip);
108 if (!ip_copy_status.ok()) {
109 return ip_copy_status.status();
110 }
111
112 return pw::OkStatus();
113 }
114
115 int main() {
116 // Get the IP address of the target host.
117 char ip_address[64] = {0};
118 auto get_ip_status = GetIPAddrFromHostName(kHost, ip_address);
119 if (!get_ip_status.ok()) {
120 return 1;
121 }
122
123 // Use a socket stream as the transport.
124 pw::stream::SocketStream socket_stream;
125
126 // Connect the socket to the remote host.
127 auto socket_connect_status = socket_stream.Connect(ip_address, kPort);
128 if (!socket_connect_status.ok()) {
129 return 1;
130 }
131
132 // Create a TLS session. Register the transport.
133 auto options = pw::tls_client::SessionOptions()
134 .set_server_name(kServerNameIndication)
135 .set_transport(socket_stream);
136 auto tls_conn = pw::tls_client::Session::Create(options);
137 if (!tls_conn.ok()) {
138 // Handle errors.
139 return 1;
140 }
141
142 auto open_status = tls_conn.value()->Open();
143 if (!open_status.ok()) {
144 // Inspect/handle error with open_status.code() and
145 // tls_conn.value()->GetLastTLSStatus().
146 return 1;
147 }
148
149 auto write_status = tls_conn.value()->Write(std::as_bytes(std::span{kHTTPRequest}));
150 if (!write_status.ok()) {
151 // Inspect/handle error with write_status.code() and
152 // tls_conn.value()->GetLastTLSStatus().
153 return 0;
154 }
155
156 // Listen for incoming data.
157 std::array<std::byte, 4096> buffer;
158 while (true) {
159 auto res = tls_conn.value()->Read(buffer);
160 if (!res.ok()) {
161 // Inspect/handle error with res.status().code() and
162 // tls_conn.value()->GetLastTLSStatus().
163 return 1;
164 }
165
166 // Process data in |buffer|. res.value() gives the span of read bytes.
167 // The following simply print to console.
168 if (res.value().size()) {
169 auto print_status = pw::sys_io::WriteBytes(res.value());
170 if (!print_status.ok()) {
171 return 1;
172 }
173 }
174
175 }
176 }
177
178A list of other demos will be provided in ``//pw_tls_client/examples/``
179
180Warning
181============
182
183Open()/Read() APIs are synchronous for now. Support for
184non-blocking/asynchronous usage will be added in the future.