protected override GenerateKeysStepResult ExecuteInternal() { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048)) { var publicKey = Encoding.UTF8.GetBytes(RSAKeyManagement.ExportPublicKeyToPEM(rsa)); var privateKey = new ProtectedDataBlock(Encoding.UTF8.GetBytes(RSAKeyManagement.ExportPrivateKeyToPEM(rsa))); return(new GenerateKeysStepResult() { PrivateKey = privateKey, PublicKey = publicKey }); } }
internal static void CheckRSA(GenerateKeysStepResult generateKeysStepResult, IPBData db, IPBWebAPI web) { // RIO - Hot path if (db == null || web == null) { return; } UserInfo ui = db.GetUserInfo(db.ActiveUser); if (ui == null) { return; } bool createKeys = true; if (ui.RSAPrivateKey != null) { using (var rsa = RSAKeyManagement.ImportPEMToRSACSP(ui.RSAPrivateKey)) { createKeys = !(rsa != null && rsa.KeySize == 2048); } } if (createKeys) { Func <dynamic, string> extractPublicKeyFromAccountJSON = (dynamic json) => { if (json == null) { logger.Error("CheckRSA: json is null"); } else if (json.error != null) { logger.Error("CheckRSA: {0}", json.error); } else if (json.ToString().Contains("\"public_key\"")) { return((string)json.account.public_key); } return(null); }; //generateKeysStep.Wait(); byte[] publicKeyPem = generateKeysStepResult.PublicKey; ProtectedDataBlock privateKeyPem = generateKeysStepResult.PrivateKey; string public_keyB64 = Convert.ToBase64String(publicKeyPem); string pub_key = extractPublicKeyFromAccountJSON(web.GetAccount(db.ActiveUser + "|" + db.DeviceUUID, new PasswordBoss.WEBApiJSON.AccountRequest { })); if (!string.IsNullOrWhiteSpace(pub_key)) { using (RSACryptoServiceProvider rsaPub = RSAKeyManagement.ImportPEMToRSACSP(Encoding.UTF8.GetString(Convert.FromBase64String(pub_key)))) { if (rsaPub != null && rsaPub.KeySize == 2048) { return; } } } dynamic accountResponse = web.UpdateAccount(db.ActiveUser + "|" + db.DeviceUUID, new PasswordBoss.WEBApiJSON.AccountRequest { public_key = public_keyB64 }); if (accountResponse == null) { logger.Error("CheckRSA: UpdateAccount failed"); } else if (accountResponse.error != null) { logger.Error("CheckRSA: UpdateAccount - {0}", accountResponse.error); } else { pub_key = extractPublicKeyFromAccountJSON(accountResponse); if (pub_key != null && Encoding.UTF8.GetString(Convert.FromBase64String(pub_key)) == Encoding.UTF8.GetString(publicKeyPem)) { ui.RSAPrivateKey = privateKeyPem; ui.PublicKey = publicKeyPem; if (!db.UpdateUserKeys(ui)) { logger.Error("CheckRSA: UpdateUserKeys failed"); } } } } }
private void NextButtonClick(object element) { try { logger.Debug("Started account request and creating rsaKeys"); if (String.IsNullOrEmpty(email) || String.IsNullOrEmpty(masterPassword)) { // To - Do Change style of Dialog MessageBox.Show("Error retreiving account information"); return; } if (this.masterPassword != UserPassword) { // To - Do Change style of Dialog MessageBox.Show("Master password not matching"); return; } UserPassword = string.Empty; //Generate a public/private key pair /*RSA rsaBase = new RSA(); * rsaBase.GenerateKeys(1024, 65537, null, null); * string privateKeyPem = rsaBase.PrivateKeyAsPEM; * string publicKeyPem = rsaBase.PublicKeyAsPEM; * * logger.Debug("Call webAPI.RequestAccount"); * string publicKeyPem = rsaBase.PublicKeyAsPEM;*/ byte[] publicKeyPem = null; ProtectedDataBlock privateKeyPem = null; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048)) { privateKeyPem = new ProtectedDataBlock(Encoding.UTF8.GetBytes(RSAKeyManagement.ExportPrivateKeyToPEM(rsa))); publicKeyPem = Encoding.UTF8.GetBytes(RSAKeyManagement.ExportPublicKeyToPEM(rsa)); } IPBWebAPI webAPI = resolver.GetInstanceOf <IPBWebAPI>(); dynamic accountResponse = webAPI.RequestAccount(new WEBApiJSON.AccountRequest() { email = email, language = "English", installation = pbData.InstallationUUID, public_key = Convert.ToBase64String(publicKeyPem) }); if (accountResponse == null) { // To - Do Change style of Dialog MessageBox.Show("Error in account registration"); return; } else { if (accountResponse.error != null) { //MessageBox.Show(accountResponse.error.details[0].ToString()); if (accountResponse.error.code == "400") { //try to register device dynamic deviceRegistrationResponse = webAPI.RegisterDevice(new WEBApiJSON.DeviceRegistrationRequest() { installation = pbData.InstallationUUID, nickname = Environment.MachineName, software_version = Assembly.GetExecutingAssembly().GetName().Version.ToString() }, email); if (deviceRegistrationResponse == null) { // To - Do Change style of Dialog MessageBox.Show("Error in device registration"); return; } else { if (deviceRegistrationResponse.error != null) { //MessageBox.Show(deviceRegistrationResponse.error.message.ToString()); if (deviceRegistrationResponse.error.code.ToString() == "403") { //send verification code for new device dynamic verificationRequestResponse = webAPI.RequestVerificationCode(email); Application.Current.Dispatcher.Invoke((Action) delegate { var verificationScreen = new VerificationRequired(resolver, email, masterPassword);// resolver.GetInstanceOf<VerificationRequired>(); Navigator.NavigationService.Navigate(verificationScreen); }); } return; } Application.Current.Dispatcher.Invoke((Action) delegate { Login login = resolver.GetInstanceOf <Login>(); login.EmailTextBox.Text = email; Navigator.NavigationService.Navigate(login); }); } } } } logger.Debug("Creating profile"); if (!pbData.CreateProfile(email, masterPassword)) { // To - Do Change style of Dialog MessageBox.Show("Error while creating secure database"); } pbData.AddUserInfo(new DTO.UserInfo() { Email = email, RSAPrivateKey = privateKeyPem, PublicKey = publicKeyPem }); logger.Debug("Performing initial sync"); PerformInitialSync(); SetDefaultSettings(pbData); inAppAnalyitics.Get <Events.AccountCreationFlow, AccountCreationFlowItem>().Log(new AccountCreationFlowItem(3, AccountCreationFlowSteps.ConfirmMP, string.Empty, MarketingActionType.Continue)); // Added dispatcher because background worker can't create new UI elements Application.Current.Dispatcher.Invoke((Action) delegate { var nextScreen = new SetupComplete(resolver);// resolver.GetInstanceOf<SetupComplete>(); Navigator.NavigationService.Navigate(nextScreen); }); } catch (Exception ex) { logger.Error(ex.Message); } }