2 // PublisherMembershipCondition.cs: Publisher Membership Condition
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections;
36 using System.Security.Cryptography.X509Certificates;
39 using Mono.Security.Cryptography;
41 namespace System.Security.Policy {
44 public sealed class PublisherMembershipCondition
45 : IConstantMembershipCondition, IMembershipCondition, ISecurityEncodable, ISecurityPolicyEncodable {
47 private X509Certificate x509;
49 // LAMESPEC: Undocumented ArgumentNullException exception
50 public PublisherMembershipCondition (X509Certificate certificate)
52 if (certificate == null)
53 throw new ArgumentNullException ("certificate");
54 // needed to match MS implementation
55 if (certificate.GetRawCertData () == null)
56 throw new NullReferenceException ("certificate");
60 // LAMESPEC: Undocumented ArgumentNullException exception
61 public X509Certificate Certificate {
65 throw new ArgumentNullException ("value");
70 public bool Check (Evidence evidence)
72 IEnumerator e = evidence.GetHostEnumerator ();
73 while (e.MoveNext ()) {
74 if (e.Current is Publisher) {
75 if (x509.Equals ((e.Current as Publisher).Certificate))
82 public IMembershipCondition Copy ()
84 return new PublisherMembershipCondition (x509);
87 public override bool Equals (object o)
89 if (!(o is PublisherMembershipCondition))
90 throw new ArgumentException ("not a PublisherMembershipCondition");
91 return x509.Equals ((o as PublisherMembershipCondition).Certificate);
94 public void FromXml (SecurityElement e)
99 public void FromXml (SecurityElement e, PolicyLevel level)
102 throw new ArgumentNullException ("e");
103 if (e.Tag != "IMembershipCondition")
104 throw new ArgumentException ("Not IMembershipCondition", "e");
105 // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
106 string cert = e.Attribute ("X509Certificate");
108 byte[] rawcert = CryptoConvert.FromHex (cert);
109 x509 = new X509Certificate (rawcert);
113 public override int GetHashCode ()
115 return x509.GetHashCode ();
118 public override string ToString ()
120 return "Publisher - " + x509.GetPublicKeyString ();
123 // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
124 internal SecurityElement Element (object o, int version)
126 SecurityElement se = new SecurityElement ("IMembershipCondition");
127 Type type = this.GetType ();
128 StringBuilder asmName = new StringBuilder (type.Assembly.ToString ());
129 asmName.Replace ('\"', '\'');
130 se.AddAttribute ("class", type.FullName + ", " + asmName);
131 se.AddAttribute ("version", version.ToString ());
135 public SecurityElement ToXml ()
140 public SecurityElement ToXml (PolicyLevel level)
142 // PolicyLevel isn't used as there's no need to resolve NamedPermissionSet references
143 SecurityElement se = Element (this, 1);
144 se.AddAttribute ("X509Certificate", x509.GetRawCertDataString ());