2004-04-12 David Sheldon <dave-mono@earth.li>
[mono.git] / mcs / class / corlib / System.Security.Policy / Publisher.cs
1 //
2 // Publisher.cs: Publisher Policy using X509 Certificate
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using System;
11 using System.Security;
12 using System.Security.Cryptography.X509Certificates;
13 using System.Security.Permissions;
14 using System.Text;
15
16 namespace System.Security.Policy {
17
18 [Serializable]
19 public sealed class Publisher : IIdentityPermissionFactory, IBuiltInEvidence {
20         
21         private X509Certificate x509;
22
23         public Publisher (X509Certificate cert) 
24         {
25                 if (cert == null)
26                         throw new ArgumentNullException ("cert");
27                 x509 = cert;
28         }
29
30         ~Publisher () 
31         {
32                 // X509Certificate doesn't have a Dispose 
33                 // (bad design as it deal with unmanaged code in Windows)
34                 // not really needed but corcompare will be happier
35         }
36
37         public X509Certificate Certificate { 
38                 get { 
39                         // needed to match MS implementation
40                         if (x509.GetRawCertData () == null)
41                                 throw new NullReferenceException ("x509");
42                         return x509; 
43                 }
44         }
45
46         public object Copy () 
47         {
48                 return (object) new Publisher (x509);
49         }
50
51         public IPermission CreateIdentityPermission (Evidence evidence) 
52         {
53                 return new PublisherIdentityPermission (x509);
54         }
55
56         public override bool Equals (object o) 
57         {
58                 if (!(o is Publisher))
59                         throw new ArgumentException ("not a Publisher");
60                 return x509.Equals ((o as Publisher).Certificate);
61         }
62         
63         public override int GetHashCode () 
64         {
65                 return x509.GetHashCode ();
66         }
67
68         public override string ToString ()
69         {
70                 SecurityElement se = new SecurityElement ("System.Security.Policy.Publisher");
71                 se.AddAttribute ("version", "1");
72                 SecurityElement cert = new SecurityElement ("X509v3Certificate");
73                 string data = x509.GetRawCertDataString ();
74                 if (data != null)
75                         cert.Text = data;
76                 se.AddChild (cert);
77                 return se.ToString ();
78         }
79
80         // interface IBuiltInEvidence
81
82         [MonoTODO]
83         int IBuiltInEvidence.GetRequiredSize (bool verbose) 
84         {
85                 return 0;
86         }
87
88         [MonoTODO]
89         int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position) 
90         {
91                 return 0;
92         }
93
94         [MonoTODO]
95         int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose) 
96         {
97                 return 0;
98         }
99 }
100
101 }