EaseFilter Demo Project
ShareFileForm.cs
Go to the documentation of this file.
1 //
3 // (C) Copyright 2012 EaseFilter Technologies Inc.
4 // All Rights Reserved
5 //
6 // This software is part of a licensed software product and may
7 // only be used or copied in accordance with the terms of that license.
8 //
10 
11 using System;
12 using System.Collections;
13 using System.Collections.Generic;
14 using System.ComponentModel;
15 using System.Data;
16 using System.Drawing;
17 using System.Linq;
18 using System.Text;
19 using System.IO;
20 using System.Windows.Forms;
21 using System.Runtime.InteropServices;
22 
24 
25 namespace SecureAgent
26 {
27  public partial class ShareFileForm : Form
28  {
29 
31  public bool isNewFileAddedToServer = false;
32  bool isNewFile = false;
33 
34  public ShareFileForm(DRPolicy _selectedDRPolicy)
35  {
36  InitializeComponent();
37 
38  this.selectedDRPolicy = _selectedDRPolicy;
39  InitializeDRSetting();
40 
41  }
42 
43  public ShareFileForm()
44  {
45  isNewFile = true;
46 
47  InitializeComponent();
48  InitializeDRSetting();
49  }
50 
51  private void InitializeDRSetting()
52  {
53  textBox_FileName.Text = string.Empty; ;
54  textBox_OutputFilePath.Text = selectedDRPolicy.FileName;
55 
56  if (!isNewFile)
57  {
58  try
59  {
60  textBox_FileName.Enabled = false;
61  textBox_ExcludeProcessNames.Text = selectedDRPolicy.ExcludeProcessNames;
62  dateTimePicker_ExpireDate.Value = DateTime.FromFileTime(selectedDRPolicy.ExpireTime);
63  dateTimePicker_ExpireTime.Value = DateTime.FromFileTime(selectedDRPolicy.ExpireTime);
64  textBox_IncludeProcessNames.Text = selectedDRPolicy.IncludeProcessNames;
65  textBox_IncludeUserNames.Text = selectedDRPolicy.IncludeUserNames;
66  textBox_ExcludeUserNames.Text = selectedDRPolicy.ExcludeUserNames;
67  textBox_IncludeComputerIds.Text = selectedDRPolicy.IncludeComputerIds;
68  textBox_UserPassword.Text = selectedDRPolicy.UserPassword;
69  checkBox_RevokeControl.Checked = true;
70  checkBox_RevokeControl.Enabled = false;
71  }
72  catch
73  {
74  }
75  }
76  else
77  {
78  textBox_FileName.Enabled = true;
79  checkBox_RevokeControl.Enabled = true;
80 
81  if (AccountForm.isGuest)
82  {
83  checkBox_RevokeControl.Enabled = false;
84  checkBox_RevokeControl.Checked = false;
85  }
86  else
87  {
88  checkBox_RevokeControl.Checked = true;
89  }
90 
91  dateTimePicker_ExpireDate.Value = DateTime.Now.AddDays(1);
92  textBox_ExcludeProcessNames.Text = "explorer.exe;";
93  }
94 
95 
96  }
97 
98  private DRPolicy GetDRSetting()
99  {
100  DRPolicy drPolicy = new DRPolicy();
101 
102  try
103  {
104  drPolicy.IncludeProcessNames = textBox_IncludeProcessNames.Text.Trim().ToLower();
105  drPolicy.ExcludeProcessNames = textBox_ExcludeProcessNames.Text.Trim().ToLower();
106  drPolicy.IncludeUserNames = textBox_IncludeUserNames.Text.Trim().ToLower();
107  drPolicy.ExcludeUserNames = textBox_ExcludeUserNames.Text.Trim().ToLower();
108  drPolicy.IncludeComputerIds = textBox_IncludeComputerIds.Text.Trim();
109  DateTime expireDate = dateTimePicker_ExpireDate.Value.Date + dateTimePicker_ExpireTime.Value.TimeOfDay;
110  drPolicy.ExpireTime = expireDate.ToUniversalTime().ToFileTime();
111  drPolicy.FileName = Path.GetFileName(textBox_OutputFilePath.Text);
112  drPolicy.UserPassword = textBox_UserPassword.Text.Trim();
113  }
114  catch (Exception ex)
115  {
116  throw new Exception("Apply digital right failed with error:" + ex.Message);
117  }
118 
119  return drPolicy;
120  }
121 
122  private bool AddNewFileDRInfoToServer(ref string iv, ref string key, ref long creationTime)
123  {
124  bool retVal = false;
125  string lastError = string.Empty;
126 
127  try
128  {
129 
130  iv = string.Empty;
131  key = string.Empty;
132  creationTime = 0;
133 
134  if (AccountForm.accountName.Length == 0 || AccountForm.password.Length == 0)
135  {
136  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
137  MessageBox.Show("Can't enable revoke access feature for guest user!", "Create share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error);
138  return retVal;
139  }
140 
141  selectedDRPolicy = GetDRSetting();
142 
143  string encryptedDRPolicy = DigitalRightControl.EncryptObjectToStr<DRPolicy>(selectedDRPolicy);
144 
145  //add new shared file meta data to remote server.
146  retVal = false;// AddNewFile(AccountForm.accountName, AccountForm.password, encryptedDRPolicy, ref creationTime, ref key, ref iv, ref lastError);
147  if (!retVal)
148  {
149  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
150  MessageBox.Show("Create share encrypted file failed with error:" + lastError, "Process share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error);
151  return retVal;
152  }
153  else
154  {
155  selectedDRPolicy.CreationTime = creationTime;
156  isNewFileAddedToServer = true;
157  }
158  }
159  catch (Exception ex)
160  {
161  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
162  MessageBox.Show("Get encryption key info failed with error:" + ex.Message, "GetEncryptionKeyAndIVFromServer", MessageBoxButtons.OK, MessageBoxIcon.Error);
163  }
164 
165  return retVal;
166  }
167 
168  private DRPolicyData GetDRPolicyData()
169  {
170  DRPolicyData policyData = new DRPolicyData();
171 
172  policyData.AESVerificationKey = FilterAPI.AES_TAG_KEY;
173 
174  policyData.AccessFlags = FilterAPI.ALLOW_MAX_RIGHT_ACCESS;
175 
176 
177  if (textBox_IncludeProcessNames.Text.Trim().Length > 0 || textBox_ExcludeProcessNames.Text.Trim().Length > 0)
178  {
179  policyData.AESFlags |= AESFlags.Flags_Enabled_Check_ProcessName;
180  }
181 
182  if (textBox_IncludeUserNames.Text.Trim().Length > 0 || textBox_ExcludeUserNames.Text.Trim().Length > 0)
183  {
184  policyData.AESFlags |= AESFlags.Flags_Enabled_Check_UserName;
185  }
186 
187  if (textBox_IncludeComputerIds.Text.Trim().Length > 0)
188  {
189  policyData.AESFlags |= AESFlags.Flags_Enabled_Check_Computer_Id;
190  }
191 
192  if (textBox_UserPassword.Text.Trim().Length > 0)
193  {
194  policyData.AESFlags |= AESFlags.Flags_Enabled_Check_User_Permit;
195  }
196 
197  DateTime expireDateTime = dateTimePicker_ExpireDate.Value.Date + dateTimePicker_ExpireTime.Value.TimeOfDay;
198  policyData.ExpireTime = expireDateTime.ToFileTime();
199 
200  if (checkBox_RevokeControl.Checked )
201  {
202  //get encryption key and iv from server.
203  policyData.AESFlags |= AESFlags.Flags_Enabled_Revoke_Access_Control;
204  }
205  else
206  {
207  policyData.IncludeProcessNames = textBox_IncludeProcessNames.Text.Trim();
208  policyData.LengthOfIncludeProcessNames = (uint)textBox_IncludeProcessNames.Text.Length * 2;
209  policyData.ExcludeProcessNames = textBox_ExcludeProcessNames.Text.Trim();
210  policyData.LengthOfExcludeProcessNames = (uint)textBox_ExcludeProcessNames.Text.Length * 2;
211  policyData.IncludeUserNames = textBox_IncludeUserNames.Text.Trim();
212  policyData.LengthOfIncludeUserNames = (uint)textBox_IncludeUserNames.Text.Length * 2;
213  policyData.ExcludeUserNames = textBox_ExcludeUserNames.Text.Trim();
214  policyData.LengthOfExcludeUserNames = (uint)textBox_ExcludeUserNames.Text.Length * 2;
215  policyData.ComputerIds = textBox_IncludeComputerIds.Text.Trim();
216  policyData.LengthOfComputerIds = (uint)policyData.ComputerIds.Length * 2;
217  policyData.CreationTime = DateTime.Now.ToFileTime();
218  policyData.UserPassword = textBox_UserPassword.Text.Trim();
219  policyData.LengthOfUserPassword = (uint)policyData.UserPassword.Length * 2;
220 
221  //notify the filter driver to check AccessFlags for permission, if the file was expired and get encryption key here.
222  policyData.AESFlags |= AESFlags.Flags_Enabled_Check_AccessFlags | AESFlags.Flags_Enabled_Expire_Time | AESFlags.Flags_AES_Key_Was_Embedded;
223  }
224 
225  if (textBox_UserPassword.Text.Trim().Length > 0)
226  {
227  policyData.AESFlags |= AESFlags.Flags_Enabled_Check_User_Password;
228  }
229 
230  policyData.AccountName = AccountForm.accountName;
231  policyData.LengthOfAccountName = (uint)policyData.AccountName.Length * 2;
232 
233  return policyData;
234 
235  }
236 
237 
238  private bool CreateShareEncryptFile()
239  {
240  string lastError = string.Empty;
241  string key = string.Empty;
242  string iv = string.Empty;
243  long creationTime = DateTime.Now.ToFileTime();
244 
245  try
246  {
247  if (textBox_FileName.Text.Trim().Length == 0)
248  {
249  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
250  MessageBox.Show("The file name can't be empty.", "Create share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error);
251  return false;
252  }
253 
254  DateTime expireDateTime = dateTimePicker_ExpireDate.Value.Date + dateTimePicker_ExpireTime.Value.TimeOfDay;
255  if (expireDateTime <= DateTime.Now)
256  {
257  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
258  MessageBox.Show("The expire time can't be less than current time.", "Create share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error);
259  return false;
260  }
261 
262  DRPolicyData policyData = GetDRPolicyData();
263 
264  if (((uint)policyData.AESFlags & (uint)AESFlags.Flags_Enabled_Revoke_Access_Control) == (uint)AESFlags.Flags_Enabled_Revoke_Access_Control)
265  {
266  if (!AddNewFileDRInfoToServer(ref iv, ref key, ref creationTime))
267  {
268  return false;
269  }
270 
271  policyData.CreationTime = creationTime;
272  }
273 
274  byte[] encryptIV = null;
275  byte[] encryptKey = null;
276 
277  if (iv.Length > 0 && key.Length > 0)
278  {
279  encryptIV = Utils.ConvertHexStrToByteArray(iv);
280  encryptKey = Utils.ConvertHexStrToByteArray(key);
281  }
282  else
283  {
284  encryptIV = Utils.GetRandomIV();
285  encryptKey = Utils.GetRandomKey();
286 
287  policyData.IV = encryptIV;
288  policyData.IVLength = (uint)encryptIV.Length;
289  policyData.EncryptionKey = encryptKey;
290  policyData.EncryptionKeyLength = (uint)encryptKey.Length;
291  }
292 
293 
294  if (!DigitalRightControl.EncryptFileWithEmbeddedDRPolicy(textBox_FileName.Text, textBox_OutputFilePath.Text, encryptIV, encryptKey, policyData, out lastError))
295  {
296  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
297  MessageBox.Show("Create share encrypted file " + textBox_FileName.Text + " failed with error:" + lastError, "Process share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error);
298 
299  return false;
300  }
301  else
302  {
303  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
304  MessageBox.Show("Create encrypted file " + textBox_OutputFilePath.Text + " succeeded.", "Process share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Information);
305 
306  selectedDRPolicy.CreationTime = creationTime;
307  selectedDRPolicy.FileName = Path.GetFileName(textBox_OutputFilePath.Text);
308 
309  isNewFileAddedToServer = true;
310 
311  return true;
312 
313  }
314  }
315  catch (Exception ex)
316  {
317  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
318  MessageBox.Show("Create share file failed with error " + ex.Message, "Create share encrypted file", MessageBoxButtons.OK, MessageBoxIcon.Error);
319 
320  return false;
321  }
322  }
323 
324 
325  private void button_OpenFile_Click(object sender, EventArgs e)
326  {
327  OpenFileDialog openFileDialog = new OpenFileDialog();
328 
329  if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
330  {
331  textBox_FileName.Text = openFileDialog.FileName;
332  textBox_OutputFilePath.Text = openFileDialog.FileName + DigitalRightControl.SECURE_SHARE_FILE_EXTENSION;
333  }
334  }
335 
336  private void button_GetComputerId_Click(object sender, EventArgs e)
337  {
338  bool retVal = false;
339  string myComputerId = string.Empty;
340  string lastError = string.Empty;
341 
342  retVal = FilterAPI.GetUniqueComputerId(ref myComputerId, ref lastError);
343 
344  if (retVal)
345  {
346  textBox_IncludeComputerIds.Text = myComputerId;
347  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
348  MessageBox.Show("This is your local computer unique id:\r\n\r\n" + myComputerId, "Get computer info", MessageBoxButtons.OK, MessageBoxIcon.Information);
349  }
350  else
351  {
352  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
353  MessageBox.Show(lastError, "Get computer info", MessageBoxButtons.OK, MessageBoxIcon.Error);
354  }
355 
356  return;
357  }
358 
359  private void textBox_FileName_TextChanged(object sender, EventArgs e)
360  {
361  textBox_OutputFilePath.Text = textBox_FileName.Text + DigitalRightControl.SECURE_SHARE_FILE_EXTENSION;
362  checkBox_RevokeControl.Enabled = true;
363  }
364 
365  private void button_Apply_Click(object sender, EventArgs e)
366  {
367  button_Apply.Enabled = false;
368 
369  try
370  {
371  string lastError = string.Empty;
372 
373  if (isNewFile)
374  {
375  if (CreateShareEncryptFile())
376  {
377  this.Close();
378  }
379  }
380  else
381  {
382  DRPolicy newDRPolicy = GetDRSetting();
383  newDRPolicy.CreationTime = selectedDRPolicy.CreationTime;
384  string encryptedDRPolicy = string.Empty;
385 
386  encryptedDRPolicy = DigitalRightControl.EncryptObjectToStr<DRPolicy>(newDRPolicy);
387 
388  //apply the share file meta data change in remote server.
389  if(true)// (!WebFormServices.SetFileDRInfo(AccountForm.accountName, AccountForm.password, encryptedDRPolicy, ref lastError))
390  {
391  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
392  MessageBox.Show("Apply digital right information for file " + selectedDRPolicy.FileName + " failed with error " + lastError, "Apply", MessageBoxButtons.OK, MessageBoxIcon.Error);
393  return;
394  }
395  else
396  {
397  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
398  MessageBox.Show("Apply digital right information for file " + selectedDRPolicy.FileName + " succeeded.", "Apply", MessageBoxButtons.OK, MessageBoxIcon.Information);
399 
400  this.Close();
401 
402  return;
403  }
404  }
405 
406 
407  }
408  catch (Exception ex)
409  {
410  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
411  MessageBox.Show("Apply digital right information for file " + selectedDRPolicy.FileName + " failed with error " + ex.Message, "Apply", MessageBoxButtons.OK, MessageBoxIcon.Error);
412  }
413  finally
414  {
415  button_Apply.Enabled = true;
416  }
417 
418  }
419 
420  private void checkBox_RevokeControk_Click(object sender, EventArgs e)
421  {
422  if (string.IsNullOrEmpty(AccountForm.accountName) || string.IsNullOrEmpty(AccountForm.password))
423  {
424  //this is a guest user.
425  if (checkBox_RevokeControl.Checked)
426  {
427  checkBox_RevokeControl.Checked = false;
428 
429  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
430  MessageBox.Show("The guest user can't enable the revoke access control feature.", "revoke", MessageBoxButtons.OK, MessageBoxIcon.Error);
431  }
432  }
433  }
434 
435 
436  }
437 }
static byte [] GetRandomIV()
Definition: Utils.cs:229
unsigned char key[]
string ExcludeUserNames
If it is not empty, all the useres in the user name list can not access the files.
static string accountName
Definition: AccountForm.cs:26
static string password
Definition: AccountForm.cs:27
long ExpireTime
The file will be expired after the expire time, and it can&#39;t be accessed.
static byte [] ConvertHexStrToByteArray(string hexString)
Definition: Utils.cs:185
string IncludeUserNames
If it is not empty, only the users in the user name list can access the files.
string IncludeProcessNames
If it is not empty, only the processes in the process list can access the files.
string FileName
the file name which was applied with policy.
ShareFileForm(DRPolicy _selectedDRPolicy)
This is the DR info meta data which will be stored in server if revoke access control is enabled...
unsigned char iv[]
long CreationTime
The time of the encrypted file was created.
This the DR data which will be embedded to the encyrpted file if the revoke access control flag is no...
string ExcludeProcessNames
If it is not empty, all the processes in the process list can not access the files.
static bool EncryptFileWithEmbeddedDRPolicy(string sourceFileName, string destFileName, byte[] encryptIV, byte[] encryptKey, DRPolicyData policy, out string lastError)
Create an encrypted file with embedded digital right policy, distribute the encrypted file via intern...
string UserPassword
the password of the shared file.
static byte [] GetRandomKey()
Definition: Utils.cs:220
string IncludeComputerIds
If it is not empty, only the computer in the computer id list can access the files.

Social Network


Services Overview

Architect, implement and test file system filter drivers for a wide range of functionality. We can offer several levels of assistance to meet your specific.

Contact Us

You are welcome to contact us for salse or partnership.

Sales: sales@easefilter.com
Support: support@easefilter.com
Info: info@easefilter.com