blob: ab1bf19a5d1911146d763f1685f836a6c55f3454 [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 Zhaob8b22612021-06-21 15:16:35 -0700573. Date time
58-------------
59TLS needs a trust-worthy source of wall clock time in order to check
60expiration. Provisioning of time source for TLS communication is very specific
61to the TLS library in use. However, common TLS libraires, such as BoringSSL
62and MbedTLS, support the use of C APIs ``time()`` and ``getimtofday()`` for
63obtaining date time. To accomodate the use of these libraries, a facade target
64``pw_tls_client:time`` is added that wraps these APIs. For gn build,
65specify the backend target with variable ``pw_tls_client_C_TIME_BACKEND``.
66
67If downstream project chooses to use other TLS libraires that handle time source
68differently, then it needs to be investigated separately.
69
Yecheng Zhao08dd6a52021-05-10 15:50:22 -070070Setup
71=====
72This module requires the following setup:
73
74 1. Choose a ``pw_tls_client`` backend, or write one yourself.
75 2. If using GN build, Specify the ``pw_tls_client_BACKEND`` GN build arg to
Yecheng Zhaofb666552021-06-22 10:02:43 -070076 point the library that provides a ``pw_tls_client`` backend. To use the
77 MbedTLS backend, set variable ``pw_tls_client_BACKEND`` to
78 ``//pw_tls_client_mbedtls``. To use the BoringSSL backend, set it to
79 ``//pw_tls_client_boringssl``.
Yecheng Zhaoe5dbfc02021-06-07 16:38:48 -070080 3. Provide a `pw_tls_client:entropy` backend. If using GN build, specify the
81 backend with variable ``pw_tls_client_ENTROPY_BACKEND``.
Yecheng Zhao08dd6a52021-05-10 15:50:22 -070082
83Module usage
84============
85For GN build, add ``//pw_tls_client`` to the dependency list.
86
87The following gives an example code for using the module on host platform.
88The example uses a Pigweed socket stream as the transport and performs TLS
89connection to www.google.com:
90
91.. code-block:: cpp
92
93 // Host domain name
94 constexpr char kHost[] = "www.google.com";
95
96 constexpr int kPort = 443;
97
98 // Server Name Indication.
99 constexpr const char* kServerNameIndication = kHost;
100
101 // An example message to send.
102 constexpr char kHTTPRequest[] = "GET / HTTP/1.1\r\n\r\n";
103
104 // pw::stream::SocketStream doesn't accept host domain name as input. Thus we
105 // introduce this helper function for getting the IP address
106 pw::Status GetIPAddrFromHostName(std::string_view host, std::span<char> ip) {
107 char null_terminated_host_name[256] = {0};
108 auto host_copy_status = pw::string::Copy(host, null_terminated_host_name);
109 if (!host_copy_status.ok()) {
110 return host_copy_status.status();
111 }
112
113 struct hostent* ent = gethostbyname(null_terminated_host_name);
114 if (ent == NULL) {
115 return PW_STATUS_INTERNAL;
116 }
117
118 in_addr** addr_list = reinterpret_cast<in_addr**>(ent->h_addr_list);
119 if (addr_list[0] == nullptr) {
120 return PW_STATUS_INTERNAL;
121 }
122
123 auto ip_copy_status = pw::string::Copy(inet_ntoa(*addr_list[0]), ip);
124 if (!ip_copy_status.ok()) {
125 return ip_copy_status.status();
126 }
127
128 return pw::OkStatus();
129 }
130
131 int main() {
132 // Get the IP address of the target host.
133 char ip_address[64] = {0};
134 auto get_ip_status = GetIPAddrFromHostName(kHost, ip_address);
135 if (!get_ip_status.ok()) {
136 return 1;
137 }
138
139 // Use a socket stream as the transport.
140 pw::stream::SocketStream socket_stream;
141
142 // Connect the socket to the remote host.
143 auto socket_connect_status = socket_stream.Connect(ip_address, kPort);
144 if (!socket_connect_status.ok()) {
145 return 1;
146 }
147
148 // Create a TLS session. Register the transport.
149 auto options = pw::tls_client::SessionOptions()
150 .set_server_name(kServerNameIndication)
151 .set_transport(socket_stream);
152 auto tls_conn = pw::tls_client::Session::Create(options);
153 if (!tls_conn.ok()) {
154 // Handle errors.
155 return 1;
156 }
157
158 auto open_status = tls_conn.value()->Open();
159 if (!open_status.ok()) {
160 // Inspect/handle error with open_status.code() and
161 // tls_conn.value()->GetLastTLSStatus().
162 return 1;
163 }
164
165 auto write_status = tls_conn.value()->Write(std::as_bytes(std::span{kHTTPRequest}));
166 if (!write_status.ok()) {
167 // Inspect/handle error with write_status.code() and
168 // tls_conn.value()->GetLastTLSStatus().
169 return 0;
170 }
171
172 // Listen for incoming data.
173 std::array<std::byte, 4096> buffer;
174 while (true) {
175 auto res = tls_conn.value()->Read(buffer);
176 if (!res.ok()) {
177 // Inspect/handle error with res.status().code() and
178 // tls_conn.value()->GetLastTLSStatus().
179 return 1;
180 }
181
182 // Process data in |buffer|. res.value() gives the span of read bytes.
183 // The following simply print to console.
184 if (res.value().size()) {
185 auto print_status = pw::sys_io::WriteBytes(res.value());
186 if (!print_status.ok()) {
187 return 1;
188 }
189 }
190
191 }
192 }
193
194A list of other demos will be provided in ``//pw_tls_client/examples/``
195
196Warning
197============
198
199Open()/Read() APIs are synchronous for now. Support for
200non-blocking/asynchronous usage will be added in the future.