Publisher.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // Publisher.cs: Publisher Policy using X509 Certificate
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using System;
  10. using System.Security.Cryptography.X509Certificates;
  11. using System.Security.Permissions;
  12. using System.Text;
  13. namespace System.Security.Policy {
  14. [Serializable]
  15. public sealed class Publisher : IIdentityPermissionFactory {
  16. private X509Certificate x509;
  17. public Publisher (X509Certificate cert)
  18. {
  19. if (cert == null)
  20. throw new ArgumentNullException ("cert");
  21. x509 = cert;
  22. }
  23. ~Publisher ()
  24. {
  25. // X509Certificate doesn't have a Dispose
  26. // (bad design as it deal with unmanaged code in Windows)
  27. // not really needed but corcompare will be happier
  28. }
  29. public X509Certificate Certificate {
  30. get {
  31. // needed to match MS implementation
  32. if (x509.GetRawCertData() == null)
  33. throw new NullReferenceException ("x509");
  34. return x509;
  35. }
  36. }
  37. public object Copy ()
  38. {
  39. return (object) new Publisher (x509);
  40. }
  41. [MonoTODO("What should we do with the evidence ? nothing?")]
  42. public IPermission CreateIdentityPermission (Evidence evidence)
  43. {
  44. return new PublisherIdentityPermission (x509);
  45. }
  46. public override bool Equals (object o)
  47. {
  48. if (!(o is Publisher))
  49. throw new ArgumentException ("not a Publisher");
  50. return x509.Equals ((o as Publisher).Certificate);
  51. }
  52. public override int GetHashCode ()
  53. {
  54. return x509.GetHashCode ();
  55. }
  56. public override string ToString ()
  57. {
  58. StringBuilder sb = new StringBuilder ();
  59. sb.Append ("<System.Security.Policy.Publisher version=\"1\">\r\n <X509v3Certificate");
  60. string cert = x509.GetRawCertDataString ();
  61. if (cert == null)
  62. sb.Append ("/>\r\n");
  63. else {
  64. sb.Append (">");
  65. sb.Append (cert);
  66. sb.Append ("</X509v3Certificate>\r\n");
  67. }
  68. sb.Append ("</System.Security.Policy.Publisher>\r\n");
  69. return sb.ToString ();
  70. }
  71. }
  72. }