copying the latest Sys.Web.Services from trunk.
[mono.git] / mcs / class / corlib / System.Security.Policy / StrongNameMembershipCondition.cs
1 //
2 // System.Security.Policy.StrongNameMembershipCondition.cs
3 //
4 // Author:
5 //      Duncan Mak (duncan@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2003 Duncan Mak, Ximian Inc.
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Globalization;
32 using System.Security.Permissions;
33
34 namespace System.Security.Policy {
35
36         [Serializable]
37         public sealed class StrongNameMembershipCondition : IMembershipCondition, IConstantMembershipCondition {
38
39                 private readonly int version = 1;
40
41                 private StrongNamePublicKeyBlob blob;
42                 private string name;
43                 private Version assemblyVersion;
44                 
45                 public StrongNameMembershipCondition (StrongNamePublicKeyBlob blob, string name, Version version)
46                 {
47                         if (blob == null)
48                                 throw new ArgumentNullException ("blob");
49
50                         this.blob = blob;
51                         this.name = name;
52                         assemblyVersion = version;
53                 }
54
55                 // for PolicyLevel (to avoid validation duplication)
56                 internal StrongNameMembershipCondition (SecurityElement e)
57                 {
58                         FromXml (e);
59                 }
60
61                 // so System.Activator.CreateInstance can create an instance...
62                 internal StrongNameMembershipCondition ()
63                 {
64                 }
65
66                 // properties
67
68                 public string Name {
69                         get { return name; }
70                         set { name = value; }
71                 }
72
73                 public Version Version {
74                         get { return assemblyVersion; }
75                         set { assemblyVersion = value; }
76                 }
77
78                 public StrongNamePublicKeyBlob PublicKey {
79                         get { return blob; }
80                         set {
81                                 if (value == null)
82                                         throw new ArgumentNullException (
83                                                 Locale.GetText ("The argument is null."));
84                                 blob = value;
85                         }
86                 }
87
88                 public bool Check (Evidence evidence)
89                 {
90                         if (evidence == null)
91                                 return false;
92
93                         foreach (object o in evidence) {
94                                 if (o is StrongName) {
95                                         StrongName sn = (o as StrongName);
96                                         /* ??? partial match ??? */
97                                         if (sn.PublicKey.Equals (blob) && (sn.Name == name) && (sn.Version.Equals (assemblyVersion)))
98                                                 return true;
99                                 }
100                         }
101                         return false;
102                 }
103
104                 public IMembershipCondition Copy ()
105                 {
106                         return new StrongNameMembershipCondition (blob, name, assemblyVersion);
107                 }
108
109                 public override bool Equals (object o)
110                 {        
111                         if (o is StrongNameMembershipCondition == false)
112                                 return false;
113                         else {
114                                 StrongNameMembershipCondition snmc = (StrongNameMembershipCondition) o;
115                                 return (snmc.Name == Name && snmc.Version == Version && snmc.PublicKey == PublicKey);
116                         }
117                 }
118
119                 public override int GetHashCode ()
120                 {
121                         return blob.GetHashCode ();
122                 }
123
124                 public void FromXml (SecurityElement e)
125                 {
126                         FromXml (e, null);
127                 }
128
129                 public void FromXml (SecurityElement e, PolicyLevel level)
130                 {
131                         MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
132
133                         blob = StrongNamePublicKeyBlob.FromString (e.Attribute ("PublicKeyBlob"));
134                         name = e.Attribute ("Name");
135                         string v = (string) e.Attribute ("AssemblyVersion");
136                         if (v == null)
137                                 assemblyVersion = new Version ();
138                         else
139                                 assemblyVersion = new Version (v);
140                 }
141
142                 public override string ToString ()
143                 {
144                         // ??? missing informations ???
145                         return String.Format ( "Strong Name - {0} name = {1} version {2}",
146                                         blob, name, assemblyVersion);
147                 }
148
149                 public SecurityElement ToXml ()
150                 {
151                         return ToXml (null);
152                 }
153
154                 public SecurityElement ToXml (PolicyLevel level)
155                 {
156                         // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
157                         SecurityElement se = MembershipConditionHelper.Element (typeof (StrongNameMembershipCondition), version);
158
159                         if (blob != null)
160                                 se.AddAttribute ("PublicKeyBlob", blob.ToString ());
161                         if (name != null)
162                                 se.AddAttribute ("Name", name);
163                         if (assemblyVersion != null) {
164                                 string v = assemblyVersion.ToString ();
165                                 if (v != "0.0")
166                                         se.AddAttribute ("AssemblyVersion", v);
167                         }
168                         return se;
169                 }
170         }
171 }