-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
59 lines (52 loc) · 2.25 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Provisioning.Security;
using Tpm2Lib;
namespace GatewayTest
{
class Program
{
static async Task Main(string[] args)
{
var invalidIotHubAddress = "invalid.azure.devices.com";
var validGatewayAddress = "localhost";
var deviceId = "valid-device-id";
Console.WriteLine("Connecting to the TPM simulator...");
var tpmDevice = ConnectToTpmSimulator();
var securityProvider = new SecurityProviderTpmHsm(deviceId, tpmDevice);
var authenticationMethod = new DeviceAuthenticationWithTpm(deviceId, securityProvider);
Console.WriteLine("Connecting to invalid IoT Hub address directly...");
using (var deviceClient = DeviceClient.Create(invalidIotHubAddress, authenticationMethod))
{
var cts = new CancellationTokenSource(1000);
try { await deviceClient.OpenAsync(cts.Token); }
catch (Exception ex) { Console.WriteLine($"Swallowed expected exception '{ex.Message}'."); }
finally
{
await deviceClient.CloseAsync();
}
}
Console.WriteLine("Connecting to invalid IoT Hub address via valid gateway...");
using (var deviceClient = DeviceClient.Create(invalidIotHubAddress, validGatewayAddress, authenticationMethod))
{
var cts = new CancellationTokenSource(1000);
await deviceClient.OpenAsync(cts.Token); // This is the call that ultimately triggers the NullReferenceException.
await deviceClient.CloseAsync();
}
}
private static Tpm2Device ConnectToTpmSimulator(string simulatorHost = "127.0.0.1", int simulatorPort = 2321)
{
var tpmDevice = new TcpTpmDevice(simulatorHost, simulatorPort);
tpmDevice.Connect();
tpmDevice.SetSocketTimeout(10);
tpmDevice.PowerCycle();
using (var tpm2 = new Tpm2(tpmDevice))
{
tpm2.Startup(Su.Clear);
}
return tpmDevice;
}
}
}