AppKeys.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Reflection;
  3. using System.Runtime.Remoting;
  4. using System.Threading;
  5. namespace Microsoft.Samples.AppUpdater
  6. {
  7. public class AppKeys
  8. {
  9. private const string KEYFILENAME = "AppUpdaterKeys.dll";
  10. private AppDomain AD;
  11. private byte[][] KeyList;
  12. private string[] ExceptionList;
  13. private KeyValidator Validator;
  14. private string AppUrl;
  15. public AppKeys(string appUrl)
  16. {
  17. AppUrl = appUrl;
  18. }
  19. public void InitializeKeyCheck()
  20. {
  21. UnInitializeKeyCheck();
  22. AD = AppDomain.CreateDomain("KeyValidatorDomain");
  23. BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance;
  24. ObjectHandle objectHandle = AD.CreateInstance("AppUpdater", "Microsoft.Samples.AppUpdater.KeyValidator", ignoreCase: false, bindingAttr, null, null, null, null, null);
  25. object obj = objectHandle.Unwrap();
  26. Validator = (KeyValidator)obj;
  27. KeyList = GetKeyList(AppUrl.TrimEnd('/') + "/AppUpdaterKeys.dll");
  28. }
  29. public void UnInitializeKeyCheck()
  30. {
  31. if (AD != null)
  32. {
  33. AppDomain.Unload(AD);
  34. Thread.Sleep(TimeSpan.FromSeconds(2.0));
  35. AD = null;
  36. }
  37. }
  38. public bool ValidateAssembly(string assemblyLocation)
  39. {
  40. return Validator.Validate(assemblyLocation, KeyList, ExceptionList);
  41. }
  42. public byte[][] GetKeyList(string keyFileUrl)
  43. {
  44. byte[][] array = null;
  45. try
  46. {
  47. AssemblyName assemblyName = new AssemblyName();
  48. assemblyName.CodeBase = keyFileUrl;
  49. Assembly assembly = AD.Load(assemblyName);
  50. if (KeyValidator.CompareKeys(assembly.GetName().GetPublicKey(), new byte[1][]
  51. {
  52. Assembly.GetEntryAssembly().GetName().GetPublicKey()
  53. }))
  54. {
  55. Type type = assembly.GetType("Microsoft.Samples.AppUpdater.KeyList");
  56. array = (byte[][])type.GetField("Keys").GetValue(null);
  57. ExceptionList = (string[])type.GetField("ExceptionList").GetValue(null);
  58. }
  59. }
  60. catch (Exception)
  61. {
  62. }
  63. byte[][] array2 = null;
  64. if (array == null)
  65. {
  66. array2 = new byte[1][]
  67. {
  68. Assembly.GetEntryAssembly().GetName().GetPublicKey()
  69. };
  70. }
  71. else
  72. {
  73. array2 = new byte[array.Length + 1][];
  74. array2[0] = Assembly.GetEntryAssembly().GetName().GetPublicKey();
  75. array.CopyTo(array2, 1);
  76. }
  77. return array2;
  78. }
  79. }
  80. }