This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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                 // properties
64
65                 public string Name {
66
67                         get { return name; }
68
69                         set { name = value; }
70                 }
71
72                 public Version Version {
73
74                         get { return version; }
75
76                         set { version = value; }
77                 }
78
79                 public StrongNamePublicKeyBlob PublicKey {
80
81                         get { return blob; }
82
83                         set {
84                                 if (value == null)
85                                         throw new ArgumentNullException (
86                                                 Locale.GetText ("The argument is null."));
87
88                                 blob = value;
89                         }
90                 }
91
92                 public bool Check (Evidence evidence)
93                 {
94                         if (evidence == null)
95                                 return false;
96
97                         foreach (object o in evidence) {
98                                 if (o is StrongName) {
99                                         StrongName sn = (o as StrongName);
100                                         if (sn.PublicKey.Equals (blob) && (sn.Name == name) && (sn.Version.Equals (version)))
101                                                 return true;
102                                 }
103                         }
104                         return false;
105                 }
106
107                 public IMembershipCondition Copy ()
108                 {
109                         return new StrongNameMembershipCondition (blob, name, version);
110                 }
111
112                 public override bool Equals (object o)
113                 {        
114                         if (o is StrongNameMembershipCondition == false)
115                                 return false;
116                         else {
117                                 StrongNameMembershipCondition snmc = (StrongNameMembershipCondition) o;
118                                 return (snmc.Name == Name && snmc.Version == Version && snmc.PublicKey == PublicKey);
119                         }
120                 }
121
122                 public override int GetHashCode ()
123                 {
124                         return blob.GetHashCode ();
125                 }
126
127                 public void FromXml (SecurityElement e)
128                 {
129                         FromXml (e, null);
130                 }
131
132                 public void FromXml (SecurityElement e, PolicyLevel level)
133                 {
134                         if (e == null)
135                                 throw new ArgumentNullException ("e");
136
137                         if (e.Attribute ("class").IndexOf (GetType ().Name) < 0)
138                                 throw new ArgumentException (Locale.GetText ("Invalid class"));
139
140                         if (e.Attribute ("version") != "1")
141                                 throw new ArgumentException (Locale.GetText ("Invalid version"));
142
143                         blob = StrongNamePublicKeyBlob.FromString (e.Attribute ("PublicKeyBlob"));
144                         name = e.Attribute ("Name");
145                         string v = (string) e.Attribute ("AssemblyVersion");
146                         if (v == null)
147                                 version = new Version ();
148                         else
149                                 version = new Version (v);
150                 }
151
152                 public override string ToString ()
153                 {
154                         return String.Format ( "Strong Name - {0} name = {1} version {2}",
155                                         blob, name, version);
156                 }
157
158                 public SecurityElement ToXml ()
159                 {
160                         return ToXml (null);
161                 }
162
163                 public SecurityElement ToXml (PolicyLevel level)
164                 {
165                         SecurityElement element = new SecurityElement ("IMembershipCondition");
166                         element.AddAttribute ("class", this.GetType ().AssemblyQualifiedName);
167                         element.AddAttribute ("version", "1");
168
169                         element.AddAttribute ("PublicKeyBlob", blob.ToString ());
170                         element.AddAttribute ("Name", name);
171                         string v = version.ToString ();
172                         if (v != "0.0")
173                                 element.AddAttribute ("AssemblyVersion", version.ToString ());
174
175                         return element;
176                 }
177         }
178 }