Beispiel #1
0
        public async Task ProvisioningDeviceClient_InvalidGlobalAddress_Register_Fail(
            string transportType,
            string securityType,
            X509EnrollmentType?x509EnrollmentType,
            TransportFallbackType?transportFallback)
        {
            if (!ConfigurationFound())
            {
                _log.WriteLine("Provisioning test configuration not found. Result inconclusive.");
                return;
            }

            using (ProvisioningTransportHandler transport = CreateTransportHandlerFromName(transportType, transportFallback))
                using (SecurityProvider security = CreateSecurityProviderFromName(securityType, x509EnrollmentType))
                {
                    ProvisioningDeviceClient provClient = ProvisioningDeviceClient.Create(
                        InvalidGlobalAddress,
                        Configuration.Provisioning.IdScope,
                        security,
                        transport);

                    var cts = new CancellationTokenSource(FailingTimeoutMiliseconds);

                    _log.WriteLine("ProvisioningClient RegisterAsync . . . ");
                    var exception = await Assert.ThrowsExceptionAsync <ProvisioningTransportException>(
                        () => provClient.RegisterAsync(cts.Token)).ConfigureAwait(false);

                    _log.WriteLine($"Exception: {exception}");
                }
        }
Beispiel #2
0
        public async Task ProvisioningDeviceClient_ValidRegistrationId_Register_Ok(
            string transportType,
            string securityType,
            X509EnrollmentType?x509EnrollmentType,
            TransportFallbackType?transportFallback,
            bool setCustomProxy,
            string proxyServerAddress = null)
        {
            using (ProvisioningTransportHandler transport = CreateTransportHandlerFromName(transportType, transportFallback))
                using (SecurityProvider security = await CreateSecurityProviderFromName(securityType, x509EnrollmentType).ConfigureAwait(false))
                {
                    _verboseLog.WriteLine("Creating device");

                    if (ImplementsWebProxy(transportType, transportFallback) && setCustomProxy)
                    {
                        transport.Proxy = (proxyServerAddress != null) ? new WebProxy(proxyServerAddress) : null;
                    }

                    ProvisioningDeviceClient provClient = ProvisioningDeviceClient.Create(
                        s_globalDeviceEndpoint,
                        Configuration.Provisioning.IdScope,
                        security,
                        transport);

                    var cts = new CancellationTokenSource(PassingTimeoutMiliseconds);

                    _log.WriteLine("ProvisioningDeviceClient RegisterAsync . . . ");
                    DeviceRegistrationResult result = await provClient.RegisterAsync(cts.Token).ConfigureAwait(false);

                    Assert.IsNotNull(result);
                    _log.WriteLine($"{result.Status} (Error Code: {result.ErrorCode}; Error Message: {result.ErrorMessage})");
                    _log.WriteLine($"ProvisioningDeviceClient AssignedHub: {result.AssignedHub}; DeviceID: {result.DeviceId}");

                    Assert.AreEqual(ProvisioningRegistrationStatusType.Assigned, result.Status);
                    Assert.IsNotNull(result.AssignedHub);
                    Assert.IsNotNull(result.DeviceId);

                    Client.IAuthenticationMethod auth = CreateAuthenticationMethodFromSecurityProvider(security, result.DeviceId);

                    // TODO: #591 - ProvisioningClient and DeviceClient should use the same protocol.
                    using (DeviceClient iotClient = DeviceClient.Create(result.AssignedHub, auth, Client.TransportType.Http1))
                    {
                        _log.WriteLine("DeviceClient OpenAsync.");
                        await iotClient.OpenAsync().ConfigureAwait(false);

                        _log.WriteLine("DeviceClient SendEventAsync.");
                        await iotClient.SendEventAsync(
                            new Client.Message(Encoding.UTF8.GetBytes("TestMessage"))).ConfigureAwait(false);

                        _log.WriteLine("DeviceClient CloseAsync.");
                        await iotClient.CloseAsync().ConfigureAwait(false);
                    }
                }
        }
Beispiel #3
0
        public async Task ProvisioningDeviceClient_ValidRegistrationId_Register_Ok(
            string transportType,
            string securityType,
            X509EnrollmentType?x509EnrollmentType,
            TransportFallbackType?transportFallback)
        {
            if (!ConfigurationFound())
            {
                _log.WriteLine("Provisioning test configuration not found. Result inconclusive.");
                return;
            }

            using (ProvisioningTransportHandler transport = CreateTransportHandlerFromName(transportType, transportFallback))
                using (SecurityProvider security = CreateSecurityProviderFromName(securityType, x509EnrollmentType))
                {
                    _verboseLog.WriteLine("Creating device");

                    ProvisioningDeviceClient provClient = ProvisioningDeviceClient.Create(
                        s_globalDeviceEndpoint,
                        Configuration.Provisioning.IdScope,
                        security,
                        transport);

                    var cts = new CancellationTokenSource(PassingTimeoutMiliseconds);

                    _log.WriteLine("ProvisioningClient RegisterAsync . . . ");
                    DeviceRegistrationResult result = await provClient.RegisterAsync(cts.Token).ConfigureAwait(false);

                    Assert.IsNotNull(result);
                    _log.WriteLine($"{result.Status} (Error Code: {result.ErrorCode}; Error Message: {result.ErrorMessage})");
                    _log.WriteLine($"ProvisioningClient AssignedHub: {result.AssignedHub}; DeviceID: {result.DeviceId}");

                    Assert.AreEqual(ProvisioningRegistrationStatusType.Assigned, result.Status);
                    Assert.IsNotNull(result.AssignedHub);
                    Assert.IsNotNull(result.DeviceId);

                    Client.IAuthenticationMethod auth = CreateAuthenticationMethodFromSecurityProvider(security, result.DeviceId);

                    using (DeviceClient iotClient = DeviceClient.Create(result.AssignedHub, auth, Client.TransportType.Mqtt_Tcp_Only))
                    {
                        _log.WriteLine("DeviceClient OpenAsync.");
                        await iotClient.OpenAsync().ConfigureAwait(false);

                        _log.WriteLine("DeviceClient SendEventAsync.");
                        await iotClient.SendEventAsync(
                            new Client.Message(Encoding.UTF8.GetBytes("TestMessage"))).ConfigureAwait(false);

                        _log.WriteLine("DeviceClient CloseAsync.");
                        await iotClient.CloseAsync().ConfigureAwait(false);
                    }
                }
        }
Beispiel #4
0
        private SecurityProvider CreateSecurityProviderFromName(string name, X509EnrollmentType?x509Type)
        {
            _verboseLog.WriteLine($"{nameof(CreateSecurityProviderFromName)}({name})");

            switch (name)
            {
            case nameof(SecurityProviderTpmHsm):
                var tpmSim = new SecurityProviderTpmSimulator(Configuration.Provisioning.TpmDeviceRegistrationId);
                SecurityProviderTpmSimulator.StartSimulatorProcess();

                _log.WriteLine(
                    $"RegistrationID = {Configuration.Provisioning.TpmDeviceRegistrationId} " +
                    $"EK = '{Convert.ToBase64String(tpmSim.GetEndorsementKey())}'");

                return(tpmSim);

            case nameof(SecurityProviderX509Certificate):

                X509Certificate2           certificate = null;
                X509Certificate2Collection collection  = null;
                switch (x509Type)
                {
                case X509EnrollmentType.Individual:
                    certificate = Configuration.Provisioning.GetIndividualEnrollmentCertificate();
                    break;

                case X509EnrollmentType.Group:
                    certificate = Configuration.Provisioning.GetGroupEnrollmentCertificate();
                    collection  = Configuration.Provisioning.GetGroupEnrollmentChain();
                    break;

                default:
                    throw new NotSupportedException($"Unknown X509 type: '{x509Type}'");
                }

                return(new SecurityProviderX509Certificate(certificate, collection));
            }

            throw new NotSupportedException($"Unknown security type: '{name}'.");
        }
Beispiel #5
0
        //        [DataRow(nameof(ProvisioningTransportHandlerMqtt), nameof(SecurityClientTpm), null, TransportFallbackType.TcpOnly)]
        //        [DataRow(nameof(ProvisioningTransportHandlerMqtt), nameof(SecurityClientX509), X509EnrollmentType.Individual, TransportFallbackType.TcpOnly)]
        //        [DataRow(nameof(ProvisioningTransportHandlerMqtt), nameof(SecurityClientX509), X509EnrollmentType.Group, TransportFallbackType.TcpOnly)]
        //        [DataRow(nameof(ProvisioningTransportHandlerMqtt), nameof(SecurityClientTpm), null, TransportFallbackType.WebSocketOnly)]
        //        [DataRow(nameof(ProvisioningTransportHandlerMqtt), nameof(SecurityClientX509), X509EnrollmentType.Individual, TransportFallbackType.WebSocketOnly)]
        //        [DataRow(nameof(ProvisioningTransportHandlerMqtt), nameof(SecurityClientX509), X509EnrollmentType.Group, TransportFallbackType.WebSocketOnly)]
        public async Task ProvisioningDeviceClient_InvalidIdScope_Register_Fail(
            string transportType,
            string securityType,
            X509EnrollmentType?x509EnrollmentType,
            TransportFallbackType?transportFallback)
        {
            using (ProvisioningTransportHandler transport = CreateTransportHandlerFromName(transportType, transportFallback))
                using (SecurityClient security = CreateSecurityClientFromName(securityType, x509EnrollmentType))
                {
                    ProvisioningDeviceClient provClient = ProvisioningDeviceClient.Create(
                        InvalidIDScope,
                        security,
                        transport);

                    var cts = new CancellationTokenSource(FailingTimeoutMiliseconds);

                    var exception = await Assert.ThrowsExceptionAsync <ProvisioningTransportException>(
                        () => provClient.RegisterAsync(cts.Token)).ConfigureAwait(false);

                    _log.WriteLine($"Exception: {exception}");
                }
        }
Beispiel #6
0
        private async Task <SecurityProvider> CreateSecurityProviderFromName(string name, X509EnrollmentType?x509Type)
        {
            _verboseLog.WriteLine($"{nameof(CreateSecurityProviderFromName)}({name})");

            switch (name)
            {
            case nameof(SecurityProviderTpmHsm):
                var tpmSim = new SecurityProviderTpmSimulator(Configuration.Provisioning.TpmDeviceRegistrationId);

                string base64Ek       = Convert.ToBase64String(tpmSim.GetEndorsementKey());
                string registrationId = Configuration.Provisioning.TpmDeviceRegistrationId;


                var provisioningService = ProvisioningServiceClient.CreateFromConnectionString(Configuration.Provisioning.ConnectionString);

                _log.WriteLine($"Getting enrollment: RegistrationID = {registrationId}");
                IndividualEnrollment enrollment = await provisioningService.GetIndividualEnrollmentAsync(registrationId).ConfigureAwait(false);

                var attestation = new TpmAttestation(base64Ek);
                enrollment.Attestation = attestation;
                _log.WriteLine($"Updating enrollment: RegistrationID = {registrationId} EK = '{base64Ek}'");
                await provisioningService.CreateOrUpdateIndividualEnrollmentAsync(enrollment).ConfigureAwait(false);

                return(tpmSim);

            case nameof(SecurityProviderX509Certificate):

                X509Certificate2           certificate = null;
                X509Certificate2Collection collection  = null;
                switch (x509Type)
                {
                case X509EnrollmentType.Individual:
                    certificate = Configuration.Provisioning.GetIndividualEnrollmentCertificate();
                    break;

                case X509EnrollmentType.Group:
                    certificate = Configuration.Provisioning.GetGroupEnrollmentCertificate();
                    collection  = Configuration.Provisioning.GetGroupEnrollmentChain();
                    break;

                default:
                    throw new NotSupportedException($"Unknown X509 type: '{x509Type}'");
                }

                return(new SecurityProviderX509Certificate(certificate, collection));
            }

            throw new NotSupportedException($"Unknown security type: '{name}'.");
        }