2 // PublisherMembershipCondition.cs: Publisher Membership Condition
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
11 using System.Collections;
12 using System.Security.Cryptography.X509Certificates;
15 namespace System.Security.Policy {
18 public sealed class PublisherMembershipCondition
19 : IConstantMembershipCondition, IMembershipCondition, ISecurityEncodable, ISecurityPolicyEncodable {
21 private X509Certificate x509;
23 // LAMESPEC: Undocumented ArgumentNullException exception
24 public PublisherMembershipCondition (X509Certificate certificate)
26 if (certificate == null)
27 throw new ArgumentNullException ("certificate");
28 // needed to match MS implementation
29 if (certificate.GetRawCertData() == null)
30 throw new NullReferenceException ("certificate");
34 // LAMESPEC: Undocumented ArgumentNullException exception
35 public X509Certificate Certificate {
39 throw new ArgumentNullException ("value");
44 public bool Check (Evidence evidence)
46 IEnumerator e = evidence.GetHostEnumerator ();
47 while (e.MoveNext ()) {
48 if (e.Current is Publisher) {
49 if (x509.Equals ((e.Current as Publisher).Certificate))
56 public IMembershipCondition Copy ()
58 return new PublisherMembershipCondition (x509);
61 public override bool Equals (object o)
63 if (!(o is PublisherMembershipCondition))
64 throw new ArgumentException ("not a PublisherMembershipCondition");
65 return x509.Equals ((o as PublisherMembershipCondition).Certificate);
68 public void FromXml (SecurityElement e)
73 private byte FromHexChar (char c)
75 if ((c >= 'A') && (c <= 'F'))
76 return (byte) (c - 'A' + 10);
77 if ((c >= '0') && (c <= '9'))
78 return (byte) (c - '0');
79 throw new ArgumentException ("invalid hex char");
82 public void FromXml (SecurityElement e, PolicyLevel level)
85 throw new ArgumentNullException ("e");
86 if (e.Tag != "IMembershipCondition")
87 throw new ArgumentException ("Not IMembershipCondition", "e");
88 // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
89 string cert = e.Attribute ("X509Certificate");
91 byte[] rawcert = new byte [cert.Length >> 1];
94 while (n < rawcert.Length) {
95 rawcert [n] = (byte) (FromHexChar (cert[i++]) << 4);
96 rawcert [n++] += FromHexChar (cert[i++]);
98 x509 = new X509Certificate (rawcert);
102 public override int GetHashCode ()
104 return x509.GetHashCode ();
107 public override string ToString ()
109 return "Publisher - " + x509.GetPublicKeyString ();
112 // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
113 internal SecurityElement Element (object o, int version)
115 SecurityElement se = new SecurityElement ("IMembershipCondition");
116 Type type = this.GetType ();
117 StringBuilder asmName = new StringBuilder (type.Assembly.ToString ());
118 asmName.Replace ('\"', '\'');
119 se.AddAttribute ("class", type.FullName + ", " + asmName);
120 se.AddAttribute ("version", version.ToString ());
124 public SecurityElement ToXml ()
129 public SecurityElement ToXml (PolicyLevel level)
131 // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
132 SecurityElement se = Element (this, 1);
133 se.AddAttribute ("X509Certificate", x509.GetRawCertDataString ());