public bool GetSharedFileList() { string lastError = string.Empty; string encryptFileList = string.Empty; sharedFileList.Clear(); bool retVal = WebAPIServices.GetFileList(ref encryptFileList, ref lastError); if (!retVal) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show(lastError, "GetFileList", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } else { if (encryptFileList.Length > 0) { List <DRPolicy> decrypFileList = DigitalRightControl.DecryptStrToObject <List <DRPolicy> >(encryptFileList); sharedFileList.Clear(); foreach (DRPolicy drPolicy in decrypFileList) { sharedFileList.Add(drPolicy.EncryptionIV, drPolicy); } } InitShareFileListView(); } return(true); }
private bool AddNewFileDRInfoToServer(ref string iv, ref string key, ref long creationTime) { bool retVal = false; string lastError = string.Empty; try { iv = string.Empty; key = string.Empty; creationTime = 0; DRPolicy drPolicy = GetDRSetting(); string encryptedDRPolicy = DigitalRightControl.EncryptObjectToStr <DRPolicy>(drPolicy); retVal = WebAPIServices.AddShareFile(encryptedDRPolicy, ref creationTime, ref key, ref iv, ref lastError); if (!retVal) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Create share encrypted file failed with error:" + lastError, "Process share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error); return(retVal); } } catch (Exception ex) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Get encryption key info failed with error:" + ex.Message, "GetEncryptionKeyAndIVFromServer", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(retVal); }
private void toolStripButton_ClearMessage_Click(object sender, EventArgs e) { string lastError = string.Empty; bool retVal = WebAPIServices.ClearAccessLog(ref lastError); if (retVal) { accessLogStr = string.Empty; InitListView(); } }
private void toolStripButton_GetAccessLog_Click(object sender, EventArgs e) { string logStr = string.Empty; string lastError = string.Empty; bool retVal = WebAPIServices.GetAccessLog(ref logStr, ref lastError); if (retVal) { accessLogStr = logStr; InitListView(); } }
private void toolStripButton_RemoveShareFile_Click(object sender, EventArgs e) { if (listView_SharedFiles.SelectedItems.Count != 1) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Please select a file.", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } DRPolicy drPolicy = (DRPolicy)listView_SharedFiles.SelectedItems[0].Tag; string lastError = string.Empty; if (!WebAPIServices.DeleteShareFile(drPolicy.EncryptionIV, ref lastError)) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Delete shared file " + selectedDRPolicy.FileName + " failed with error:" + lastError, "DeleteSharedFile", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } GetSharedFileList(); }
private bool CreateOrModifyShareEncryptFile() { string lastError = string.Empty; string authorizedProcessNames = textBox_authorizedProcessNames.Text.Trim(); string unauthorizedProcessNames = textBox_UnauthorizedProcessNames.Text.Trim(); string authorizedUserNames = textBox_AuthorizedUserNames.Text.Trim(); string unauthorizedUserNames = textBox_UnauthorizedUserNames.Text.Trim(); string fileName = textBox_FileName.Text.Trim(); string targetFileName = textBox_TargetName.Text; try { if (fileName.Length == 0) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("The file name can't be empty.", "Create share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } DateTime expireDateTime = dateTimePicker_ExpireDate.Value.Date + dateTimePicker_ExpireTime.Value.TimeOfDay; if (expireDateTime <= DateTime.Now) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("The expire time can't be less than current time.", "Create share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } if (selectedDRPolicy != null) { DRPolicy drPolicy = GetDRSetting(); string encryptedDRPolicy = DigitalRightControl.EncryptObjectToStr <DRPolicy>(drPolicy); if (WebAPIServices.ModifySharedFileDRInfo(encryptedDRPolicy, ref lastError)) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Modify shared file " + textBox_FileName.Text + " policy succeeded.", "Modify shared file", MessageBoxButtons.OK, MessageBoxIcon.Information); return(true); } else { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Modify shared file " + textBox_FileName.Text + " policy failed with error:" + lastError, "Modify shared file", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } //here we generate the random unique IV and key, you can use your own key and iv byte[] encryptionIV = Utils.GetRandomIV(); byte[] encryptionKey = Utils.GetRandomKey(); string keyStr = string.Empty; string ivStr = string.Empty; if (GlobalConfig.StoreSharedFileMetaDataInServer) { long creationTime = DateTime.Now.ToFileTime(); //send the encrypted file digital right information to the server and get back the iv and key. if (!AddNewFileDRInfoToServer(ref ivStr, ref keyStr, ref creationTime)) { return(false); } if (ivStr.Length > 0 && keyStr.Length > 0) { encryptionIV = Utils.ConvertHexStrToByteArray(ivStr); encryptionKey = Utils.ConvertHexStrToByteArray(keyStr); } } //for this example, we add the encryptIV and account name as the tag data to the encrypted file //you can add your own custom tag data to the encyrpted file, so when someone open the encrypted file, you will get the tag data. string tagStr = GlobalConfig.AccountName + ";" + ivStr; byte[] tagData = UnicodeEncoding.Unicode.GetBytes(tagStr); bool retVal = false; if (fileName.Equals(targetFileName, StringComparison.CurrentCulture)) { retVal = FilterAPI.AESEncryptFileWithTag(fileName, (uint)encryptionKey.Length, encryptionKey, (uint)encryptionIV.Length, encryptionIV, (uint)tagData.Length, tagData); } else { retVal = FilterAPI.AESEncryptFileToFileWithTag(fileName, targetFileName, (uint)encryptionKey.Length, encryptionKey, (uint)encryptionIV.Length, encryptionIV, (uint)tagData.Length, tagData); } if (!retVal) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Create encrypted file " + targetFileName + " failed with error:" + FilterAPI.GetLastErrorMessage(), "Create share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error); if (GlobalConfig.StoreSharedFileMetaDataInServer) { WebAPIServices.DeleteShareFile(ivStr, ref lastError); } if (!fileName.Equals(targetFileName, StringComparison.CurrentCulture)) { File.Delete(targetFileName); } return(false); } else { //set this flag to the encrypted file, require to get permission from user mode when the file open if (!FilterAPI.SetHeaderFlags(targetFileName, (uint)AESFlags.Flags_Request_IV_And_Key_From_User, FilterAPI.ALLOW_MAX_RIGHT_ACCESS)) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("SetHeaderFlags for file " + targetFileName + " failed with error:" + FilterAPI.GetLastErrorMessage(), "SetHeaderFlags", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } if (!GlobalConfig.StoreSharedFileMetaDataInServer) { //add the permission meta data to a file and store it in the server, it will be used when the file open. if (!DRServer.AddDRInfoToFile(targetFileName, authorizedProcessNames, unauthorizedProcessNames, authorizedUserNames, unauthorizedUserNames , expireDateTime, encryptionIV, encryptionKey, FilterAPI.ALLOW_MAX_RIGHT_ACCESS.ToString())) { return(false); } } MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); string message = "Create encrypted file " + targetFileName + " succeeded, you can distribute this encrypted file to your client.\r\n\r\nDownload this file to the share file drop folder in the client,"; message += " then start the filter service there, now you can open the encrypted file if the process in client has the permission."; MessageBox.Show(message, "Share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Information); } return(true); } catch (Exception ex) { MessageBoxHelper.PrepToCenterMessageBoxOnForm(this); MessageBox.Show("Create share file failed with error " + ex.Message, "Create share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } }
static private bool GetAccessPermissionFromServer(string fileName, string userName, string processName, string tagStr, ref string encryptKey, ref uint accessFlag, ref string lastError) { Boolean retVal = true; try { CacheUserAccessInfo cacheUserAccessInfo = new CacheUserAccessInfo(); string index = userName + "_" + processName + "_" + tagStr; //cache the same user/process/filename access. lock (userAccessCache) { if (userAccessCache.ContainsKey(index)) { cacheUserAccessInfo = userAccessCache[index]; EventManager.WriteMessage(446, "GetUserPermission", EventLevel.Verbose, "Thread" + Thread.CurrentThread.ManagedThreadId + ",userInfoKey " + index + " exists in the cache table."); } else { cacheUserAccessInfo.isDownloaded = false; cacheUserAccessInfo.index = index; cacheUserAccessInfo.lastAccessTime = DateTime.Now; userAccessCache.Add(index, cacheUserAccessInfo); EventManager.WriteMessage(435, "GetUserPermission", EventLevel.Verbose, "Thread" + Thread.CurrentThread.ManagedThreadId + ",add userInfoKey " + index + " to the cache table."); } } //synchronize the same file access. if (!cacheUserAccessInfo.isDownloaded && !cacheUserAccessInfo.syncEvent.WaitOne(new TimeSpan(0, 0, cacheTimeOutInSeconds))) { string info = "User name: " + userName + ",processname:" + processName + ",file name:" + fileName + " wait for permission timeout."; EventManager.WriteMessage(402, "GetUserPermission", EventLevel.Warning, info); return(false); } TimeSpan timeSpan = DateTime.Now - cacheUserAccessInfo.lastAccessTime; if (cacheUserAccessInfo.isDownloaded && timeSpan.TotalSeconds < cacheTimeOutInSeconds) { //the access was cached, return the last access status. retVal = cacheUserAccessInfo.accessStatus; if (!retVal) { EventManager.WriteMessage(308, "GetAccessPermissionFromServer", EventLevel.Error, cacheUserAccessInfo.lastError); } else { string info = "thread" + Thread.CurrentThread.ManagedThreadId + ", Cached userInfoKey " + index + " in the cache table,return " + retVal; EventManager.WriteMessage(451, "GetUserPermission", EventLevel.Verbose, info); } encryptKey = cacheUserAccessInfo.key; accessFlag = cacheUserAccessInfo.accessFlags; lastError = cacheUserAccessInfo.lastError; cacheUserAccessInfo.syncEvent.Set(); return(retVal); } string encryptionIV = tagStr; retVal = WebAPIServices.GetSharedFilePermission(fileName, processName, userName, tagStr, ref encryptionIV, ref encryptKey, ref accessFlag, ref lastError); cacheUserAccessInfo.accessStatus = retVal; cacheUserAccessInfo.isDownloaded = true; cacheUserAccessInfo.syncEvent.Set(); if (!retVal) { string message = "Get file " + fileName + " permission from server return error:" + lastError; cacheUserAccessInfo.lastError = message; cacheUserAccessInfo.accessStatus = false; EventManager.WriteMessage(293, "GetAccessPermissionFromServer", EventLevel.Error, message); return(retVal); } else { string message = "Get file " + fileName + " permission frome server return succeed."; EventManager.WriteMessage(208, "GetAccessPermissionFromServer", EventLevel.Verbose, message); } cacheUserAccessInfo.key = encryptKey; cacheUserAccessInfo.iv = encryptionIV; cacheUserAccessInfo.accessFlags = accessFlag; } catch (Exception ex) { EventManager.WriteMessage(286, "GetAccessPermissionFromServer", EventLevel.Error, "Get file " + fileName + "permission failed with exception:" + ex.Message); retVal = false; } return(retVal); }