[System.Runtime.Serialization] Static writer fix.
[mono.git] / mcs / class / corlib / System.Security.Cryptography.X509Certificates / X509CertificateImplMono.cs
1 //
2 // X509CertificateImplMono.cs: X.509 implementation using Mono.Security.X509.
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //      Martin Baulig  <martin.baulig@xamarin.com>
7 //
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
10 // Copyright (C) 2015 Xamarin, Inc. (http://www.xamarin.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 using System;
32 using System.Text;
33 using MX = Mono.Security.X509;
34
35 namespace System.Security.Cryptography.X509Certificates
36 {
37         class X509CertificateImplMono : X509CertificateImpl
38         {
39                 MX.X509Certificate x509;
40
41                 public X509CertificateImplMono (MX.X509Certificate x509)
42                 {
43                         this.x509 = x509;
44                 }
45
46                 public override bool IsValid {
47                         get { return x509 != null; }
48                 }
49
50                 public override IntPtr Handle {
51                         get { return IntPtr.Zero; }
52                 }
53
54                 public override X509CertificateImpl Clone ()
55                 {
56                         ThrowIfContextInvalid ();
57                         return new X509CertificateImplMono (x509);
58                 }
59
60                 public override string GetIssuerName (bool legacyV1Mode)
61                 {
62                         ThrowIfContextInvalid ();
63                         if (legacyV1Mode)
64                                 return x509.IssuerName;
65                         else
66                                 return MX.X501.ToString (x509.GetIssuerName (), true, ", ", true);
67                 }
68
69                 public override string GetSubjectSummary ()
70                 {
71                         ThrowIfContextInvalid ();
72                         return x509.SubjectName;
73                 }
74
75                 public override string GetSubjectName (bool legacyV1Mode)
76                 {
77                         ThrowIfContextInvalid ();
78                         if (legacyV1Mode)
79                                 return x509.SubjectName;
80                         else
81                                 return MX.X501.ToString (x509.GetSubjectName (), true, ", ", true);
82                 }
83
84                 public override byte[] GetRawCertData ()
85                 {
86                         ThrowIfContextInvalid ();
87                         return x509.RawData;
88                 }
89
90                 protected override byte[] GetCertHash (bool lazy)
91                 {
92                         ThrowIfContextInvalid ();
93                         SHA1 sha = SHA1.Create ();
94                         return sha.ComputeHash (x509.RawData);
95                 }
96
97                 public override DateTime GetEffectiveDateString ()
98                 {
99                         ThrowIfContextInvalid ();
100                         return x509.ValidFrom.ToLocalTime ();
101                 }
102
103                 public override DateTime GetExpirationDateString ()
104                 {
105                         ThrowIfContextInvalid ();
106                         return x509.ValidUntil.ToLocalTime ();
107                 }
108
109                 public override bool Equals (X509CertificateImpl other, out bool result)
110                 {
111                         // Use default implementation
112                         result = false;
113                         return false;
114                 }
115
116                 public override string GetKeyAlgorithm () 
117                 {
118                         ThrowIfContextInvalid ();
119                         return x509.KeyAlgorithm;
120                 }
121
122                 public override byte[] GetKeyAlgorithmParameters () 
123                 {
124                         ThrowIfContextInvalid ();
125                         return x509.KeyAlgorithmParameters;
126                 }
127
128                 public override byte[] GetPublicKey ()
129                 {
130                         ThrowIfContextInvalid ();
131                         return x509.PublicKey;
132                 }
133
134                 public override byte[] GetSerialNumber ()
135                 {
136                         ThrowIfContextInvalid ();
137                         return x509.SerialNumber;
138                 }
139
140                 public override byte[] Export (X509ContentType contentType, byte[] password)
141                 {
142                         ThrowIfContextInvalid ();
143
144                         switch (contentType) {
145                         case X509ContentType.Cert:
146                                 return GetRawCertData ();
147                         case X509ContentType.Pfx: // this includes Pkcs12
148                                 // TODO
149                                 throw new NotSupportedException ();
150                         case X509ContentType.SerializedCert:
151                                 // TODO
152                                 throw new NotSupportedException ();
153                         default:
154                                 string msg = Locale.GetText ("This certificate format '{0}' cannot be exported.", contentType);
155                                 throw new CryptographicException (msg);
156                         }
157                 }
158
159                 public override string ToString (bool full)
160                 {
161                         ThrowIfContextInvalid ();
162
163                         string nl = Environment.NewLine;
164                         StringBuilder sb = new StringBuilder ();
165                         sb.AppendFormat ("[Subject]{0}  {1}{0}{0}", nl, GetSubjectName (false));
166                         sb.AppendFormat ("[Issuer]{0}  {1}{0}{0}", nl, GetIssuerName (false));
167                         sb.AppendFormat ("[Not Before]{0}  {1}{0}{0}", nl, GetEffectiveDateString ());
168                         sb.AppendFormat ("[Not After]{0}  {1}{0}{0}", nl, GetExpirationDateString ());
169                         sb.AppendFormat ("[Thumbprint]{0}  {1}{0}", nl, X509Helper.ToHexString (GetCertHash ()));
170                         sb.Append (nl);
171                         return sb.ToString ();
172                 }
173
174                 protected override void Dispose (bool disposing)
175                 {
176                         x509 = null;
177                 }
178         }
179 }