2 // System.Security.Policy.StrongNameMembershipCondition.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2003 Duncan Mak, Ximian Inc.
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System
.Collections
;
32 using System
.Globalization
;
33 using System
.Security
.Permissions
;
34 using System
.Runtime
.InteropServices
;
37 namespace System
.Security
.Policy
{
41 public sealed class StrongNameMembershipCondition
: IMembershipCondition
, IConstantMembershipCondition
{
43 private readonly int version
= 1;
45 private StrongNamePublicKeyBlob blob
;
47 private Version assemblyVersion
;
49 public StrongNameMembershipCondition (StrongNamePublicKeyBlob blob
, string name
, Version version
)
52 throw new ArgumentNullException ("blob");
57 assemblyVersion
= (Version
) version
.Clone ();
60 // for PolicyLevel (to avoid validation duplication)
61 internal StrongNameMembershipCondition (SecurityElement e
)
66 // so System.Activator.CreateInstance can create an instance...
67 internal StrongNameMembershipCondition ()
78 public Version Version
{
79 get { return assemblyVersion; }
80 set { assemblyVersion = value; }
83 public StrongNamePublicKeyBlob PublicKey
{
87 throw new ArgumentNullException ("PublicKey");
92 public bool Check (Evidence evidence
)
97 IEnumerator e
= evidence
.GetHostEnumerator ();
98 while (e
.MoveNext ()) {
99 StrongName sn
= (e
.Current
as StrongName
);
101 if (!sn
.PublicKey
.Equals (blob
))
103 if ((name
!= null) && (name
!= sn
.Name
))
105 if ((assemblyVersion
!= null) && !assemblyVersion
.Equals (sn
.Version
))
113 public IMembershipCondition
Copy ()
115 return new StrongNameMembershipCondition (blob
, name
, assemblyVersion
);
118 public override bool Equals (object o
)
120 StrongNameMembershipCondition snmc
= (o
as StrongNameMembershipCondition
);
123 if (!snmc
.PublicKey
.Equals (PublicKey
))
125 if (name
!= snmc
.Name
)
127 if (assemblyVersion
!= null)
128 return assemblyVersion
.Equals (snmc
.Version
);
129 return (snmc
.Version
== null);
132 public override int GetHashCode ()
134 // name and version aren't part of the calculation
135 return blob
.GetHashCode ();
138 public void FromXml (SecurityElement e
)
143 public void FromXml (SecurityElement e
, PolicyLevel level
)
145 MembershipConditionHelper
.CheckSecurityElement (e
, "e", version
, version
);
147 blob
= StrongNamePublicKeyBlob
.FromString (e
.Attribute ("PublicKeyBlob"));
148 name
= e
.Attribute ("Name");
149 string v
= (string) e
.Attribute ("AssemblyVersion");
151 assemblyVersion
= null;
153 assemblyVersion
= new Version (v
);
156 public override string ToString ()
158 StringBuilder sb
= new StringBuilder ("StrongName - ");
161 sb
.AppendFormat (" name = {0}", name
);
162 if (assemblyVersion
!= null)
163 sb
.AppendFormat (" version = {0}", assemblyVersion
);
164 return sb
.ToString ();
167 public SecurityElement
ToXml ()
172 public SecurityElement
ToXml (PolicyLevel level
)
174 // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
175 SecurityElement se
= MembershipConditionHelper
.Element (typeof (StrongNameMembershipCondition
), version
);
178 se
.AddAttribute ("PublicKeyBlob", blob
.ToString ());
180 se
.AddAttribute ("Name", name
);
181 if (assemblyVersion
!= null) {
182 string v
= assemblyVersion
.ToString ();
184 se
.AddAttribute ("AssemblyVersion", v
);