public static int Main(string[] args) { if (string.IsNullOrWhiteSpace(s_idScope) && (args.Length > 0)) { s_idScope = args[0]; } if (string.IsNullOrWhiteSpace(s_idScope)) { Console.WriteLine("ProvisioningDeviceClientTpm <IDScope>"); return(1); } // Remove if a real TPM is being used. Console.WriteLine("Starting TPM simulator."); SecurityProviderTpmSimulator.StartSimulatorProcess(); // Replace the following type with SecurityProviderTpmHsm() to use a real TPM2.0 device. using (var security = new SecurityProviderTpmSimulator(s_registrationID)) // Select one of the available transports: // To optimize for size, reference only the protocols used by your application. using (var transport = new ProvisioningTransportHandlerHttp()) // using (var transport = new ProvisioningTransportHandlerAmqp(TransportFallbackType.TcpOnly)) // using (var transport = new ProvisioningTransportHandlerAmqp(TransportFallbackType.WebSocketOnly)) { // Note that the TPM simulator will create an NVChip file containing the simulated TPM state. Console.WriteLine("Extracting endorsement key."); string base64EK = Convert.ToBase64String(security.GetEndorsementKey()); Console.WriteLine( "In your Azure Device Provisioning Service please go to 'Manage enrollments' and select " + "'Individual Enrollments'. Select 'Add' then fill in the following:"); Console.WriteLine("\tMechanism: TPM"); Console.WriteLine($"\tEndorsement key: {base64EK}"); Console.WriteLine($"\tRegistration ID: {s_registrationID}"); Console.WriteLine($"\tDevice ID: {s_registrationID} (or any other valid DeviceID)"); Console.WriteLine(); Console.WriteLine("Press ENTER once enrollment has been created."); Console.ReadLine(); ProvisioningDeviceClient provClient = ProvisioningDeviceClient.Create(GlobalDeviceEndpoint, s_idScope, security, transport); var sample = new ProvisioningDeviceClientSample(provClient, security); sample.RunSampleAsync().GetAwaiter().GetResult(); } // Remove if a real TPM is being used. Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Stopping TPM simulator."); SecurityProviderTpmSimulator.StopSimulatorProcess(); return(0); }
public static async Task <int> Main(string[] args) { // Parse application parameters Parameters parameters = null; ParserResult <Parameters> result = Parser.Default.ParseArguments <Parameters>(args) .WithParsed(parsedParams => { parameters = parsedParams; }) .WithNotParsed(errors => { Environment.Exit(1); }); // This sample provides a way to get the endorsement key (EK) required in creation of the individual enrollment if (parameters.GetTpmEndorsementKey) { if (parameters.UseTpmSimulator) { Console.WriteLine("Starting TPM simulator..."); SecurityProviderTpmSimulator.StartSimulatorProcess(); } using var security = new SecurityProviderTpmHsm(null); Console.WriteLine($"Your EK is {Convert.ToBase64String(security.GetEndorsementKey())}"); if (parameters.UseTpmSimulator) { SecurityProviderTpmSimulator.StopSimulatorProcess(); } return(0); } // For a normal run of this sample, IdScope and RegistrationId are required if (string.IsNullOrWhiteSpace(parameters.IdScope) || string.IsNullOrWhiteSpace(parameters.RegistrationId)) { Console.WriteLine(CommandLine.Text.HelpText.AutoBuild(result, null, null)); Environment.Exit(1); } var sample = new ProvisioningDeviceClientSample(parameters); await sample.RunSampleAsync(); return(0); }
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."); }