2004-01-26 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / corlib / System.Security.Policy / PublisherMembershipCondition.cs
1 //
2 // PublisherMembershipCondition.cs: Publisher Membership Condition
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using System;
11 using System.Collections;
12 using System.Security.Cryptography.X509Certificates;
13 using System.Text;
14
15 namespace System.Security.Policy {
16
17         [Serializable]
18         public sealed class PublisherMembershipCondition
19                 : IConstantMembershipCondition, IMembershipCondition, ISecurityEncodable, ISecurityPolicyEncodable {
20         
21                 private X509Certificate x509;
22         
23                 // LAMESPEC: Undocumented ArgumentNullException exception
24                 public PublisherMembershipCondition (X509Certificate certificate) 
25                 {
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");
31                         x509 = certificate;
32                 }
33         
34                 // LAMESPEC: Undocumented ArgumentNullException exception
35                 public X509Certificate Certificate {
36                         get { return x509; }
37                         set { 
38                                 if (value == null)
39                                         throw new ArgumentNullException ("value");
40                                 x509 = value; 
41                         }
42                 }
43         
44                 public bool Check (Evidence evidence) 
45                 {
46                         IEnumerator e = evidence.GetHostEnumerator ();
47                         while (e.MoveNext ()) {
48                                 if (e.Current is Publisher) {
49                                         if (x509.Equals ((e.Current as Publisher).Certificate))
50                                                 return true;
51                                 }
52                         }
53                         return false;
54                 }
55         
56                 public IMembershipCondition Copy () 
57                 {
58                         return new PublisherMembershipCondition (x509);
59                 }
60         
61                 public override bool Equals (object o) 
62                 {
63                         if (!(o is PublisherMembershipCondition))
64                                 throw new ArgumentException ("not a PublisherMembershipCondition");
65                         return x509.Equals ((o as PublisherMembershipCondition).Certificate);
66                 }
67         
68                 public void FromXml (SecurityElement e) 
69                 {
70                         FromXml (e, null);
71                 }
72         
73                 private byte FromHexChar (char c) 
74                 {
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");
80                 }
81
82                 public void FromXml (SecurityElement e, PolicyLevel level) 
83                 {
84                         if (e == null)
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");
90                         if (cert != null) {
91                                 byte[] rawcert = new byte [cert.Length >> 1];
92                                 int n = 0;
93                                 int i = 0;
94                                 while (n < rawcert.Length) {
95                                         rawcert [n] = (byte) (FromHexChar (cert[i++]) << 4);
96                                         rawcert [n++] += FromHexChar (cert[i++]);
97                                 }
98                                 x509 = new X509Certificate (rawcert);
99                         }
100                 }
101         
102                 public override int GetHashCode () 
103                 {
104                         return x509.GetHashCode ();
105                 }
106         
107                 public override string ToString () 
108                 {
109                         return "Publisher - " + x509.GetPublicKeyString ();
110                 }
111
112                 // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
113                 internal SecurityElement Element (object o, int version) 
114                 {
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 ());
121                         return se;
122                 }
123         
124                 public SecurityElement ToXml () 
125                 {
126                         return ToXml (null);
127                 }
128         
129                 public SecurityElement ToXml (PolicyLevel level) 
130                 {
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 ());
134                         return se;
135                 }
136         }
137 }