/// <summary>
        /// Generate a new set of keys for a particular device, and store it to Azure Key Vault.
        /// The Previous keys will be erased.
        /// </summary>
        /// <param name="deviceId">Device Id</param>
        /// <returns>DeviceSecretKeysModel</returns>
        public async Task <DeviceSecretKeysModel> SetSecretForDevice(Guid deviceId)
        {
            DeviceSecretKeysModel output = new DeviceSecretKeysModel();

            var ssidPassword = await SetSecret($"{deviceId}-ssid", GenerateSecret(10));

            var portalPassword = await SetSecret($"{deviceId}-portal", GenerateSecret(10));

            var encryptionKey = await SetSecret($"{deviceId}-cryptkey", GenerateSecret(64));

            output.AccessPointPassword = !string.IsNullOrEmpty(ssidPassword) ? ssidPassword : _config.DefaultSecrets.AccessPointPassword;
            output.PortalPassword      = !string.IsNullOrEmpty(portalPassword) ? portalPassword : _config.DefaultSecrets.PortalPassword;
            output.EncryptionKey       = !string.IsNullOrEmpty(encryptionKey) ? encryptionKey : _config.DefaultSecrets.EncryptionKey;

            return(output);
        }
        /// <summary>
        /// Retrieve the set of security keys for a particular device
        /// </summary>
        /// <param name="deviceId">Device id</param>
        /// <returns>DeviceSecretKeysModel</returns>
        public async Task <DeviceSecretKeysModel> GetSecretForDevice(Guid deviceId)
        {
            var output = new DeviceSecretKeysModel();

            var ssidName = await GetSecret($"{deviceId}-ssidn");

            var ssidPassword = await GetSecret($"{deviceId}-ssidp");

            var portalPassword = await GetSecret($"{deviceId}-portal");

            var encryptionKey = await GetSecret($"{deviceId}-cryptkey");

            output.SSIDName       = !string.IsNullOrEmpty(ssidName) ? ssidName : _config.DefaultSecrets.SSIDName;
            output.SSIDPassword   = !string.IsNullOrEmpty(ssidPassword) ? ssidPassword : _config.DefaultSecrets.SSIDPassword;
            output.PortalPassword = !string.IsNullOrEmpty(portalPassword) ? portalPassword : _config.DefaultSecrets.PortalPassword;
            output.EncryptionKey  = !string.IsNullOrEmpty(encryptionKey) ? encryptionKey : _config.DefaultSecrets.EncryptionKey;

            return(output);
        }
Esempio n. 3
0
        private async Task <bool> ProvisionWithCloud()
        {
            if (csrResult == null)
            {
                return(await ProvisionDeviceFail());
            }

            // provision device with azure
            SetPairingStatusText("Provisioning device with the mothership...");

            certificateResponse = await deviceProvisioningRestService.GenerateDeviceCertificate(new DeviceCertificateRequestModel
            {
                Csr        = csrResult?.Csr ?? "MIIBbjCB2AIBADAvMS0wKwYDVQQDEyQ4OTJlYWM5YS1iOWFkLTQ0NDgtYWEwYS0wOTI0MDE1YWMwMWEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALeqOH+XoeXXERg8neKzr3IumxTDMKsPzKjZ/kfE1gu/FHmr1ugPuRTtQzP5WFVD5lWqtEKJyX+YDCjNevKeHBSpHTAAdVR8GbpDdvRvij0k6yrmrjTRVohO5bTaE611KNzXOW5K4Y8PhoTHasNnMEydfAh4ysut92lWObmg2CG1AgMBAAGgADANBgkqhkiG9w0BAQsFAAOBgQCg8dbM4gMxChp4MF67B/0ARv5Ezq3423v/Tkj5KOMxFql+NeYtM9JpIWABMw2xlARl+agp9e8eaj503grhHjYeGV0afC2/8AA2o/PyZOrS80QViDK6Z4cY+zUO5hp3darGCEH14fuAHKwrokSQxYReqdBELyT3r4ZnCdbi+NUx7A==",
                DeviceType = deviceSetupService.DeviceTypeAsString,
            });

            if (certificateResponse == null)
            {
                return(await ProvisionDeviceFail());
            }

            generateKeysResponse = await deviceProvisioningRestService.GenerateDeviceKeys(deviceSetupService.CurrentDeviceModel.DeviceId, deviceSetupService.CurrentDeviceModel.SSID);

            if (generateKeysResponse == null)
            {
                return(await ProvisionDeviceFail());
            }

            await Task.Delay(2000);

            SetPairingStatusText("Reconnecting to device and finishing up! Sit tight...");

            State = RegistrationState.ConnectingConnectingToDeviceSecondTime;
            // reconnect to device to set device type
            var reconnectSuccess = await wifiService.ConnectToWifiNetwork(deviceSetupService.CurrentDeviceModel.SSID, deviceSetupService.DefaultPassword);

            if (!reconnectSuccess)
            {
                return(await ProvisionDeviceFail());
            }
            return(true);
        }
        /// <summary>
        /// Generate a new set of keys for a particular device, and store it to Azure Key Vault.
        /// The Previous keys will be erased.
        /// </summary>
        /// <param name="deviceId">Device Id</param>
        /// <returns>DeviceSecretKeysModel</returns>
        public async Task <DeviceSecretKeysModel> SetSecretForDevice(DeviceSecretKeysCreationModel deviceSecretKeysCreationRequest)
        {
            DeviceSecretKeysModel output = new DeviceSecretKeysModel();

            Guid deviceId = deviceSecretKeysCreationRequest.DeviceId;

            var ssidName = await SetSecret($"{deviceId}-ssidn", deviceSecretKeysCreationRequest.SSIDName);

            var ssidPassword = await SetSecret($"{deviceId}-ssidp", GenerateSecret(10));

            var portalPassword = await SetSecret($"{deviceId}-portal", GenerateSecret(10));

            var encryptionKey = await SetSecret($"{deviceId}-cryptkey", GenerateSecret(64));

            output.SSIDName       = !string.IsNullOrEmpty(ssidName) ? ssidName : _config.DefaultSecrets.SSIDName;
            output.SSIDPassword   = !string.IsNullOrEmpty(ssidPassword) ? ssidPassword : _config.DefaultSecrets.SSIDPassword;
            output.PortalPassword = !string.IsNullOrEmpty(portalPassword) ? portalPassword : _config.DefaultSecrets.PortalPassword;
            output.EncryptionKey  = !string.IsNullOrEmpty(encryptionKey) ? encryptionKey : _config.DefaultSecrets.EncryptionKey;

            return(output);
        }