This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / System.Security.Policy / PublisherMembershipCondition.cs
1 //
2 // PublisherMembershipCondition.cs: Publisher Membership Condition
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Collections;
36 using System.Security.Cryptography.X509Certificates;
37 using System.Text;
38
39 using Mono.Security.Cryptography;
40
41 namespace System.Security.Policy {
42
43         [Serializable]
44         public sealed class PublisherMembershipCondition
45                 : IConstantMembershipCondition, IMembershipCondition, ISecurityEncodable, ISecurityPolicyEncodable {
46         
47                 private X509Certificate x509;
48         
49                 // LAMESPEC: Undocumented ArgumentNullException exception
50                 public PublisherMembershipCondition (X509Certificate certificate) 
51                 {
52                         if (certificate == null)
53                                 throw new ArgumentNullException ("certificate");
54                         // needed to match MS implementation
55                         if (certificate.GetRawCertData () == null)
56                                 throw new NullReferenceException ("certificate");
57                         x509 = certificate;
58                 }
59         
60                 // LAMESPEC: Undocumented ArgumentNullException exception
61                 public X509Certificate Certificate {
62                         get { return x509; }
63                         set { 
64                                 if (value == null)
65                                         throw new ArgumentNullException ("value");
66                                 x509 = value; 
67                         }
68                 }
69         
70                 public bool Check (Evidence evidence) 
71                 {
72                         IEnumerator e = evidence.GetHostEnumerator ();
73                         while (e.MoveNext ()) {
74                                 if (e.Current is Publisher) {
75                                         if (x509.Equals ((e.Current as Publisher).Certificate))
76                                                 return true;
77                                 }
78                         }
79                         return false;
80                 }
81         
82                 public IMembershipCondition Copy () 
83                 {
84                         return new PublisherMembershipCondition (x509);
85                 }
86         
87                 public override bool Equals (object o) 
88                 {
89                         if (!(o is PublisherMembershipCondition))
90                                 throw new ArgumentException ("not a PublisherMembershipCondition");
91                         return x509.Equals ((o as PublisherMembershipCondition).Certificate);
92                 }
93         
94                 public void FromXml (SecurityElement e) 
95                 {
96                         FromXml (e, null);
97                 }
98         
99                 public void FromXml (SecurityElement e, PolicyLevel level) 
100                 {
101                         if (e == null)
102                                 throw new ArgumentNullException ("e");
103                         if (e.Tag != "IMembershipCondition")
104                                 throw new ArgumentException ("Not IMembershipCondition", "e");
105                         // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
106                         string cert = e.Attribute ("X509Certificate");
107                         if (cert != null) {
108                                 byte[] rawcert = CryptoConvert.FromHex (cert);
109                                 x509 = new X509Certificate (rawcert);
110                         }
111                 }
112         
113                 public override int GetHashCode () 
114                 {
115                         return x509.GetHashCode ();
116                 }
117         
118                 public override string ToString () 
119                 {
120                         return "Publisher - " + x509.GetPublicKeyString ();
121                 }
122
123                 // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
124                 internal SecurityElement Element (object o, int version) 
125                 {
126                         SecurityElement se = new SecurityElement ("IMembershipCondition");
127                         Type type = this.GetType ();
128                         StringBuilder asmName = new StringBuilder (type.Assembly.ToString ());
129                         asmName.Replace ('\"', '\'');
130                         se.AddAttribute ("class", type.FullName + ", " + asmName);
131                         se.AddAttribute ("version", version.ToString ());
132                         return se;
133                 }
134         
135                 public SecurityElement ToXml () 
136                 {
137                         return ToXml (null);
138                 }
139         
140                 public SecurityElement ToXml (PolicyLevel level) 
141                 {
142                         // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
143                         SecurityElement se = Element (this, 1);
144                         se.AddAttribute ("X509Certificate", x509.GetRawCertDataString ());
145                         return se;
146                 }
147         }
148 }