blob: f40702f2696fe65fcef7cbdecd9779c03bc8f807 [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
32Setup
33=====
34This module requires the following setup:
35
36 1. Choose a ``pw_tls_client`` backend, or write one yourself.
37 2. If using GN build, Specify the ``pw_tls_client_BACKEND`` GN build arg to
38 point the library that provides a ``pw_tls_client`` backend.
39
40Module usage
41============
42For GN build, add ``//pw_tls_client`` to the dependency list.
43
44The following gives an example code for using the module on host platform.
45The example uses a Pigweed socket stream as the transport and performs TLS
46connection to www.google.com:
47
48.. code-block:: cpp
49
50 // Host domain name
51 constexpr char kHost[] = "www.google.com";
52
53 constexpr int kPort = 443;
54
55 // Server Name Indication.
56 constexpr const char* kServerNameIndication = kHost;
57
58 // An example message to send.
59 constexpr char kHTTPRequest[] = "GET / HTTP/1.1\r\n\r\n";
60
61 // pw::stream::SocketStream doesn't accept host domain name as input. Thus we
62 // introduce this helper function for getting the IP address
63 pw::Status GetIPAddrFromHostName(std::string_view host, std::span<char> ip) {
64 char null_terminated_host_name[256] = {0};
65 auto host_copy_status = pw::string::Copy(host, null_terminated_host_name);
66 if (!host_copy_status.ok()) {
67 return host_copy_status.status();
68 }
69
70 struct hostent* ent = gethostbyname(null_terminated_host_name);
71 if (ent == NULL) {
72 return PW_STATUS_INTERNAL;
73 }
74
75 in_addr** addr_list = reinterpret_cast<in_addr**>(ent->h_addr_list);
76 if (addr_list[0] == nullptr) {
77 return PW_STATUS_INTERNAL;
78 }
79
80 auto ip_copy_status = pw::string::Copy(inet_ntoa(*addr_list[0]), ip);
81 if (!ip_copy_status.ok()) {
82 return ip_copy_status.status();
83 }
84
85 return pw::OkStatus();
86 }
87
88 int main() {
89 // Get the IP address of the target host.
90 char ip_address[64] = {0};
91 auto get_ip_status = GetIPAddrFromHostName(kHost, ip_address);
92 if (!get_ip_status.ok()) {
93 return 1;
94 }
95
96 // Use a socket stream as the transport.
97 pw::stream::SocketStream socket_stream;
98
99 // Connect the socket to the remote host.
100 auto socket_connect_status = socket_stream.Connect(ip_address, kPort);
101 if (!socket_connect_status.ok()) {
102 return 1;
103 }
104
105 // Create a TLS session. Register the transport.
106 auto options = pw::tls_client::SessionOptions()
107 .set_server_name(kServerNameIndication)
108 .set_transport(socket_stream);
109 auto tls_conn = pw::tls_client::Session::Create(options);
110 if (!tls_conn.ok()) {
111 // Handle errors.
112 return 1;
113 }
114
115 auto open_status = tls_conn.value()->Open();
116 if (!open_status.ok()) {
117 // Inspect/handle error with open_status.code() and
118 // tls_conn.value()->GetLastTLSStatus().
119 return 1;
120 }
121
122 auto write_status = tls_conn.value()->Write(std::as_bytes(std::span{kHTTPRequest}));
123 if (!write_status.ok()) {
124 // Inspect/handle error with write_status.code() and
125 // tls_conn.value()->GetLastTLSStatus().
126 return 0;
127 }
128
129 // Listen for incoming data.
130 std::array<std::byte, 4096> buffer;
131 while (true) {
132 auto res = tls_conn.value()->Read(buffer);
133 if (!res.ok()) {
134 // Inspect/handle error with res.status().code() and
135 // tls_conn.value()->GetLastTLSStatus().
136 return 1;
137 }
138
139 // Process data in |buffer|. res.value() gives the span of read bytes.
140 // The following simply print to console.
141 if (res.value().size()) {
142 auto print_status = pw::sys_io::WriteBytes(res.value());
143 if (!print_status.ok()) {
144 return 1;
145 }
146 }
147
148 }
149 }
150
151A list of other demos will be provided in ``//pw_tls_client/examples/``
152
153Warning
154============
155
156Open()/Read() APIs are synchronous for now. Support for
157non-blocking/asynchronous usage will be added in the future.