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