2004-01-05 Sebastien Pouliot <spouliot@videotron.ca>
[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 using System;
11 using System.Globalization;
12 using System.Security.Permissions;
13
14 namespace System.Security.Policy {
15
16         public sealed class StrongNameMembershipCondition
17                 : IMembershipCondition, ISecurityEncodable, ISecurityPolicyEncodable, IConstantMembershipCondition
18         {
19                 StrongNamePublicKeyBlob blob;
20                 string name;
21                 Version version;
22                 
23                 public StrongNameMembershipCondition (StrongNamePublicKeyBlob blob, string name, Version version)
24                 {
25                         if (blob == null)
26                                 throw new ArgumentNullException ("blob");
27
28                         this.blob = blob;
29                         this.name = name;
30                         this.version = version;
31                 }
32
33                 // for PolicyLevel (to avoid validation duplication)
34                 internal StrongNameMembershipCondition (SecurityElement e)
35                 {
36                         FromXml (e);
37                 }
38
39                 // properties
40
41                 public string Name {
42
43                         get { return name; }
44
45                         set { name = value; }
46                 }
47
48                 public Version Version {
49
50                         get { return version; }
51
52                         set { version = value; }
53                 }
54
55                 public StrongNamePublicKeyBlob PublicKey {
56
57                         get { return blob; }
58
59                         set {
60                                 if (value == null)
61                                         throw new ArgumentNullException (
62                                                 Locale.GetText ("The argument is null."));
63
64                                 blob = value;
65                         }
66                 }
67
68                 public bool Check (Evidence evidence)
69                 {
70                         if (evidence == null)
71                                 return false;
72
73                         foreach (object o in evidence) {
74                                 if (o is StrongName) {
75                                         StrongName sn = (o as StrongName);
76                                         if (sn.PublicKey.Equals (blob) && (sn.Name == name) && (sn.Version.Equals (version)))
77                                                 return true;
78                                 }
79                         }
80                         return false;
81                 }
82
83                 public IMembershipCondition Copy ()
84                 {
85                         return new StrongNameMembershipCondition (blob, name, version);
86                 }
87
88                 public override bool Equals (object o)
89                 {        
90                         if (o is StrongNameMembershipCondition == false)
91                                 return false;
92                         else {
93                                 StrongNameMembershipCondition snmc = (StrongNameMembershipCondition) o;
94                                 return (snmc.Name == Name && snmc.Version == Version && snmc.PublicKey == PublicKey);
95                         }
96                 }
97
98                 public override int GetHashCode ()
99                 {
100                         return blob.GetHashCode ();
101                 }
102
103                 public void FromXml (SecurityElement e)
104                 {
105                         FromXml (e, null);
106                 }
107
108                 public void FromXml (SecurityElement e, PolicyLevel level)
109                 {
110                         if (e == null)
111                                 throw new ArgumentNullException ("e");
112
113                         if (e.Attribute ("class").IndexOf (GetType ().Name) < 0)
114                                 throw new ArgumentException (Locale.GetText ("Invalid class"));
115
116                         if (e.Attribute ("version") != "1")
117                                 throw new ArgumentException (Locale.GetText ("Invalid version"));
118
119                         blob = StrongNamePublicKeyBlob.FromString (e.Attribute ("PublicKeyBlob"));
120                         name = e.Attribute ("Name");
121                         string v = (string) e.Attribute ("AssemblyVersion");
122                         if (v == null)
123                                 version = new Version ();
124                         else
125                                 version = new Version (v);
126                 }
127
128                 public override string ToString ()
129                 {
130                         return String.Format ( "Strong Name - {0} name = {1} version {2}",
131                                         blob, name, version);
132                 }
133
134                 public SecurityElement ToXml ()
135                 {
136                         return ToXml (null);
137                 }
138
139                 public SecurityElement ToXml (PolicyLevel level)
140                 {
141                         SecurityElement element = new SecurityElement ("IMembershipCondition");
142                         element.AddAttribute ("class", this.GetType ().AssemblyQualifiedName);
143                         element.AddAttribute ("version", "1");
144
145                         element.AddAttribute ("PublicKeyBlob", blob.ToString ());
146                         element.AddAttribute ("Name", name);
147                         string v = version.ToString ();
148                         if (v != "0.0")
149                                 element.AddAttribute ("AssemblyVersion", version.ToString ());
150
151                         return element;
152                 }
153         }
154 }