Skip to content

Commit 48a40df

Browse files
Add a sample that demonstrates TLS 1.3 support (#1224)
* Add sample for TLS 1.3 support * more * note * simplify sample * comments
1 parent f61aca5 commit 48a40df

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
7+
## SCENARIO
8+
9+
Connect to an IoT hub using TLS 1.3 (currently in preview) and then periodically send telemetry messages.
10+
11+
## CONTEXT
12+
13+
### IoT hub support for TLS 1.3
14+
IoT Hub is currently previewing the ability to use TLS 1.3, but accessing that requires some small changes on the client side.
15+
16+
Up until now, connection strings would always follow a format like:
17+
18+
```
19+
<hub name>.azure-devices.<dnsSuffix>
20+
```
21+
22+
and this IoT hub endpoint supports TLS versions 1.0, 1.1, and 1.2
23+
24+
IoT hub is previewing offering TLS version 1.2 + 1.3 support in endpoints with connection strings like
25+
26+
```
27+
device connection string:
28+
<hub name>.device.azure-devices.<dnsSuffix>
29+
```
30+
31+
and
32+
33+
```
34+
service connection string:
35+
<hub name>.service.azure-devices.<dnsSuffix>
36+
```
37+
38+
This sample expects the environment variable "IOTHUB_DEVICE_CONNECTION_STRING" to contain a connection string that follows the device connection string pattern above
39+
40+
### SDK behavior
41+
42+
When you run this sample, the "Client Hello" message in the TLS layer will advertise to IoT hub that this client supports TLS 1.2 and TLS 1.3. In the "Server Hello" response, IoT hub will then choose TLS 1.3 as the version for both client and server to use for this connection.
43+
44+
Note that the SDK behavior here is unchanged as it has always advertised support for TLS 1.2 and TLS 1.3. The only change to make the connection use TLS 1.3 is to connect with the connection string of the host that also supports TLS 1.3
45+
46+
## ADDITIONAL CONSIDERATIONS
47+
48+
This feature is not generally available yet nor is it available in preview for all IoT hubs. As such, the above connection string pattern my yield a "host not found" exception.
49+
50+
Note that this library currently uses the SSL library for all TLS connections. This SSL library has a few feature gaps around TLS 1.3 specifically as documented [here](https://docs.python.org/3/library/ssl.html#tls-1-3)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
7+
import os
8+
from azure.iot.device import IoTHubDeviceClient, Message
9+
10+
# The device connection string to authenticate the device with your IoT hub.
11+
CONNECTION_STRING = os.getenv("IOTHUB_DEVICE_CONNECTION_STRING")
12+
13+
if ".device.azure-devices." not in CONNECTION_STRING:
14+
# classic connection strings that look like '<hub-name>.azure-devices.<dns suffix>' only support up to TLS 1.2
15+
raise ValueError(
16+
"Device connection string must match the format '<hub name>.device.azure-devices.<dns suffix>' in order to use TLS 1.3"
17+
)
18+
19+
# The client object is used to interact with your Azure IoT hub.
20+
device_client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
21+
22+
# Connect the client.
23+
print("opening the connection")
24+
device_client.connect()
25+
26+
print("sending message")
27+
msg = Message("Hello from TLS 1.3 connection!")
28+
device_client.send_message(msg)
29+
30+
# finally, shut down the client
31+
print("closing the connection")
32+
device_client.shutdown()

0 commit comments

Comments
 (0)