コード例 #1
0
        public static List <Service> GetServicesListDecrypted()
        {
            var services         = new List <Service>();
            var eccService       = new EccKeyServiceProvider();
            var masterKeyService = new KeyDerivationServiceProvider();
            var crypto           = new SymmetricCryptographyServiceProvider();
            var userServices     = UserData.ApiClient.ApiEcccredentialsGetAsync(null, null).ConfigureAwait(false).GetAwaiter().GetResult();

            foreach (var service in userServices)
            {
                var derivedKey = eccService.EcdhDervieKey(
                    new EccKeyPairBlob(
                        service.EccDerivationBlob.Curve,
                        service.EccDerivationBlob.PublicKey,
                        null
                        ),
                    new EccKeyPairBlob(
                        service.EccDerivationBlob.Curve,
                        null,
                        UserData.PrivateKeyDecrypted
                        ),
                    HashAlgorithmName.SHA256
                    );

                var masterKey = masterKeyService.DeriveKeyFromBlob(
                    derivedKey,
                    new KeyDerivationBlob(
                        service.SymmetricCiphertextBlob.DerivationDescription,
                        service.SymmetricCiphertextBlob.DerivationSalt,
                        null
                        )
                    );

                var decryptedService = crypto.DecryptFromSymmetricCipthertextBlob(
                    masterKey.MasterKey,
                    new SymmetricCipthertextBlob(
                        service.SymmetricCiphertextBlob.CipherDescription,
                        service.SymmetricCiphertextBlob.InitializationVector,
                        service.SymmetricCiphertextBlob.Ciphertext,
                        service.SymmetricCiphertextBlob.AuthenticationTag
                        )
                    );

                Service tempService = JsonConvert.DeserializeObject <Service>(Encoding.UTF8.GetString(decryptedService));
                tempService.Id = service.Id;
                services.Add(tempService);
            }

            return(services);
        }
コード例 #2
0
        private async void EditServiceButton_Click(object sender, RoutedEventArgs e)
        {
            var service = (Service)((Button)e.Source).DataContext;
            var dialog  = new EditServiceDialog(service, Notifier);
            var result  = await DialogHost.Show(dialog, "root");

            if (result == null)
            {
                return;
            }

            try
            {
                if ((bool)result)
                {
                    var eccService     = new EccKeyServiceProvider();
                    var ServiceKeyPair = eccService.CreateNew_secp256r1_ECKeyPair();

                    var userKeyPair      = UserData.eccKeyPairs[0];
                    var masterKeyService = new KeyDerivationServiceProvider();
                    var crypto           = new SymmetricCryptographyServiceProvider();

                    var derivedKey = eccService.EcdhDervieKey(
                        new EccKeyPairBlob(userKeyPair.PublicKey.Curve, userKeyPair.PublicKey.PublicKey, null),
                        ServiceKeyPair,
                        HashAlgorithmName.SHA256);

                    var masterKey = masterKeyService.Pbkdf2Sha256DeriveKeyFromPassword(derivedKey, 16, 16);

                    var encrypted = crypto.Aes128GcmEncrypt(masterKey.MasterKey, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(service)));

                    var putModel = new EccCredentialPutModel()
                    {
                        EccDerivationBlob = new EccDerivationBlobModel()
                        {
                            Curve     = ServiceKeyPair.Curve,
                            PublicKey = ServiceKeyPair.PublicKey
                        },
                        EccKeyPairId            = userKeyPair.Id,
                        SymmetricCiphertextBlob = new SymmetricCiphertextBlobModel()
                        {
                            AuthenticationTag     = encrypted.AuthenticationTag,
                            CipherDescription     = encrypted.CipherDescription,
                            Ciphertext            = encrypted.Cipthertext,
                            InitializationVector  = encrypted.InitializationVector,
                            DerivationDescription = masterKey.DerivationDescription,
                            DerivationSalt        = masterKey.DerivationSalt
                        },
                    };

                    await UserData.ApiClient.ApiEcccredentialsPutAsync(service.Id, putModel);
                }
            }
            catch (ApiException <ProblemDetails> exc)
            {
                foreach (var error in ApiErrorsBuilder.GetErrorList(exc.Result.Errors))
                {
                    Notifier.ShowError(error);
                }
            }
            catch (Exception)
            {
                Notifier.ShowError("Unknown error");
            }
        }