This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from Azure-Samples/drwill/dpsTpm
Revamp DPS TPM sample
- Loading branch information
Showing
12 changed files
with
257 additions
and
190 deletions.
There are no files selected for viewing
79 changes: 0 additions & 79 deletions
79
provisioning/Samples/device/Common/ProvisioningDeviceClientSample.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using CommandLine; | ||
using Microsoft.Azure.Devices.Client; | ||
|
||
namespace Microsoft.Azure.Devices.Provisioning.Client.Samples | ||
{ | ||
/// <summary> | ||
/// Parameters for the application | ||
/// </summary> | ||
internal class Parameters | ||
{ | ||
[Option( | ||
'e', | ||
"GetTpmEndorsementKey", | ||
HelpText = "Gets the TPM endorsement key. Use this option by itself to get the EK needed to create a DPS individual enrollment.")] | ||
public bool GetTpmEndorsementKey { get; set; } | ||
|
||
[Option( | ||
'u', | ||
"UseTpmSimulator", | ||
HelpText = "Runs the TPM simulator - useful when the local device does not have a TPM chip.")] | ||
public bool UseTpmSimulator { get; set; } | ||
|
||
[Option( | ||
's', | ||
"IdScope", | ||
HelpText = "The Id Scope of the DPS instance. For normal runs, this is required.")] | ||
public string IdScope { get; set; } | ||
|
||
[Option( | ||
'r', | ||
"RegistrationId", | ||
HelpText = "The registration Id from the individual enrollment. For normal runs, this is required.")] | ||
public string RegistrationId { get; set; } | ||
|
||
[Option( | ||
'g', | ||
"GlobalDeviceEndpoint", | ||
Default = "global.azure-devices-provisioning.net", | ||
HelpText = "The global endpoint for devices to connect to.")] | ||
public string GlobalDeviceEndpoint { get; set; } | ||
|
||
[Option( | ||
't', | ||
"TransportType", | ||
Default = TransportType.Amqp, | ||
HelpText = "The transport to use to communicate with the device provisioning instance. Possible values include Amqp, Amqp_WebSocket_Only, Amqp_Tcp_only, and Http1.")] | ||
public TransportType TransportType { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
provisioning/Samples/device/TpmSample/ProvisioningDeviceClientSample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.Azure.Devices.Client; | ||
using Microsoft.Azure.Devices.Provisioning.Client.Transport; | ||
using Microsoft.Azure.Devices.Provisioning.Security; | ||
using Microsoft.Azure.Devices.Provisioning.Security.Samples; | ||
using Microsoft.Azure.Devices.Shared; | ||
using System; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.Devices.Provisioning.Client.Samples | ||
{ | ||
/// <summary> | ||
/// Demonstrates how to register a device with the device provisioning service using a certificate, and then | ||
/// use the registration information to authenticate to IoT Hub. | ||
/// </summary> | ||
internal class ProvisioningDeviceClientSample | ||
{ | ||
private readonly Parameters _parameters; | ||
|
||
public ProvisioningDeviceClientSample(Parameters parameters) | ||
{ | ||
_parameters = parameters; | ||
} | ||
|
||
public async Task RunSampleAsync() | ||
{ | ||
SecurityProviderTpm security = null; | ||
|
||
try | ||
{ | ||
if (_parameters.UseTpmSimulator) | ||
{ | ||
Console.WriteLine("Starting TPM simulator..."); | ||
SecurityProviderTpmSimulator.StartSimulatorProcess(); | ||
security = new SecurityProviderTpmSimulator(_parameters.RegistrationId); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Initializing security using the local TPM..."); | ||
security = new SecurityProviderTpmHsm(_parameters.RegistrationId); | ||
} | ||
|
||
Console.WriteLine($"Initializing the device provisioning client..."); | ||
|
||
using var transport = GetTransportHandler(); | ||
ProvisioningDeviceClient provClient = ProvisioningDeviceClient.Create( | ||
_parameters.GlobalDeviceEndpoint, | ||
_parameters.IdScope, | ||
security, | ||
transport); | ||
|
||
Console.WriteLine($"Initialized for registration Id {security.GetRegistrationID()}."); | ||
|
||
Console.WriteLine("Registering with the device provisioning service... "); | ||
DeviceRegistrationResult result = await provClient.RegisterAsync(); | ||
|
||
Console.WriteLine($"Registration status: {result.Status}."); | ||
if (result.Status != ProvisioningRegistrationStatusType.Assigned) | ||
{ | ||
Console.WriteLine($"Registration status did not assign a hub, so exiting this sample."); | ||
return; | ||
} | ||
|
||
Console.WriteLine($"Device {result.DeviceId} registered to {result.AssignedHub}."); | ||
|
||
Console.WriteLine("Creating TPM authentication for IoT Hub..."); | ||
IAuthenticationMethod auth = new DeviceAuthenticationWithTpm(result.DeviceId, security); | ||
|
||
Console.WriteLine($"Testing the provisioned device with IoT Hub..."); | ||
using DeviceClient iotClient = DeviceClient.Create(result.AssignedHub, auth, _parameters.TransportType); | ||
|
||
Console.WriteLine("Sending a telemetry message..."); | ||
using var message = new Message(Encoding.UTF8.GetBytes("TestMessage")); | ||
await iotClient.SendEventAsync(message); | ||
} | ||
finally | ||
{ | ||
if (_parameters.UseTpmSimulator) | ||
{ | ||
SecurityProviderTpmSimulator.StopSimulatorProcess(); | ||
} | ||
|
||
security?.Dispose(); | ||
} | ||
|
||
Console.WriteLine("Finished."); | ||
} | ||
|
||
private ProvisioningTransportHandler GetTransportHandler() | ||
{ | ||
return _parameters.TransportType switch | ||
{ | ||
TransportType.Amqp => new ProvisioningTransportHandlerAmqp(), | ||
TransportType.Amqp_Tcp_Only => new ProvisioningTransportHandlerAmqp(TransportFallbackType.WebSocketOnly), | ||
TransportType.Amqp_WebSocket_Only => new ProvisioningTransportHandlerAmqp(TransportFallbackType.TcpOnly), | ||
TransportType.Http1 => new ProvisioningTransportHandlerHttp(), | ||
TransportType.Mqtt => throw new NotSupportedException("MQTT is not supported for TPM"), | ||
TransportType.Mqtt_Tcp_Only => throw new NotSupportedException("MQTT is not supported for TPM"), | ||
TransportType.Mqtt_WebSocket_Only => throw new NotSupportedException("MQTT is not supported for TPM"), | ||
_ => throw new NotSupportedException($"Unsupported transport type {_parameters.TransportType}"), | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.