private void change_click(object sender, EventArgs e) { try { string certs = ""; X509Certificate2Collection tempcollection = new X509Certificate2Collection(); foreach (X509Certificate2 applied_cert in cert_mgmt.applied_certs) { foreach (ListViewItem item in listView1.Items) { if (item.Checked && item.Text == applied_cert.SubjectName.Name) { tempcollection.Add(applied_cert); certs += applied_cert.SubjectName.Name + "\n"; } } } DialogResult priv_deci = new DialogResult(); bool priv_key = cert_mgmt.checkIfPrivKeyExists(tempcollection); if (!priv_key) { priv_deci = MessageBox.Show("No private key found in certificate collection!\nClick OK to proceed! But after that you can't open the file anymore!!\nClick Cancel to stop! No changes will made!!", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Stop); } if (priv_deci == DialogResult.OK || priv_key) { cert_mgmt.applied_certs = tempcollection; DialogResult decision = MessageBox.Show("The following certificates will be used for encryption:\n\n" + certs, "Encryption", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2); if (decision == DialogResult.Yes) { if (string.IsNullOrEmpty(key_path)) { DialogResult save_dialog = saveFileDialog1.ShowDialog(); if (save_dialog == DialogResult.OK) { key_path = saveFileDialog1.FileName; } } if (fileBytes.Length > 1) { File.WriteAllBytes(key_path, cert_mgmt.EncryptMsg(cert_mgmt.DecryptMsg(fileBytes), cert_mgmt.applied_certs)); MessageBox.Show("New key saved!", "Key saved", MessageBoxButtons.OK, MessageBoxIcon.Information); cert_mgmt.keyring_certs = cert_mgmt.applied_certs; safekeyring(); } } } } catch (CryptographicException key_ex) { MessageBox.Show("Certificate can't be used for encryption or you don't have a private key for decryption (bad key)!\n" + key_ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception some_ex) { MessageBox.Show("A general error occurred - please check the file and certificates!\nPlease open an issue on GitHub :-) ." + some_ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Get a key for an existing database. First, the key file is located, either because its location /// and filename are the same as the database path (with the exception of the extension), or the user /// is asked. Then, the key file is decrypted using a private key. /// </summary> /// <param name="strPath">Full filename of the database file.</param> /// <returns>A byte array with the key, or null if an error occurs. If an error occurs, user is /// notified of the error.</returns> byte[] GetExistingKey(IOConnectionInfo ioc) { Stream stream = null; try { string newpath = UrlUtil.StripExtension(ioc.Path) + "." + CertProtKeyFileExtension; IOConnectionInfo keyIoc = ioc.CloneDeep(); keyIoc.Path = newpath; stream = IOConnection.OpenRead(keyIoc); } catch (Exception e) { // strPath may be a URL (even if IsLocalFile returns true?), // whatever the reason, fall through and the user can pick a // local file as the key file } if (stream == null || !stream.CanRead) { // fall back on opening a local file // FUTURE ENHANCEMENT: allow user to enter a URL and name/pwd as well OpenFileDialog ofd = UIUtil.CreateOpenFileDialog("KeePassX509Provider", UIUtil.CreateFileTypeFilter(CertProtKeyFileExtension, "x05KeyFile", true), 1, CertProtKeyFileExtension, false /* multi-select */, true); if (ofd.ShowDialog() != DialogResult.OK) { return(null); } stream = IOConnection.OpenRead(IOConnectionInfo.FromPath(ofd.FileName)); } try { BinaryReader reader = new BinaryReader(stream); byte[] p7m = reader.ReadBytes(MAX_KEY_FILE_LENGTH); // URL streams don't support seeking, and so Position doesn't work //bool tooBig = stream.Position >= MAX_KEY_FILE_LENGTH; bool tooBig = p7m.Length >= MAX_KEY_FILE_LENGTH; reader.Close(); if (tooBig) { MessageBox.Show("Kes File ist to big"); return(null); } Certmanager cert_mgr = new Certmanager(); return(cert_mgr.DecryptMsg(p7m)); } catch (SystemException ex) // covers IOException and CryptographicException { MessageBox.Show("Error at encryption or IO error!\nIf you used a smart card for encryption, please provide/plugin fist!"); return(null); } }