Merge pull request #2323 from esdrubal/servicemodel
[mono.git] / mcs / class / corlib / System.Security.Cryptography.X509Certificates / X509Certificate20.cs
1 //
2 // X509Certificate20.cs: Partial class to handle new 2.0-only stuff
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2006,2008 Novell, Inc (http://www.novell.com)
9 // Copyright 2013 Xamarin Inc.
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.IO;
32 using System.Runtime.InteropServices;
33 using System.Security.Permissions;
34 using System.Text;
35
36 using Mono.Security;
37 using Mono.Security.X509;
38
39 using System.Runtime.Serialization;
40
41 namespace System.Security.Cryptography.X509Certificates {
42
43         [ComVisible (true)]
44         [MonoTODO ("X509ContentType.SerializedCert isn't supported (anywhere in the class)")]
45         public partial class X509Certificate : IDeserializationCallback, ISerializable, IDisposable {
46                 private string issuer_name;
47                 private string subject_name;
48
49
50                 public X509Certificate ()
51                 {
52                         // this allows an empty certificate to exists
53                 }
54
55                 public X509Certificate (byte[] rawData, string password)
56                 {
57                         Import (rawData, password, X509KeyStorageFlags.DefaultKeySet);
58                 }
59
60                 [MonoTODO ("SecureString support is incomplete")]
61                 public X509Certificate (byte[] rawData, SecureString password)
62                 {
63                         Import (rawData, password, X509KeyStorageFlags.DefaultKeySet);
64                 }
65
66                 public X509Certificate (byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
67                 {
68                         Import (rawData, password, keyStorageFlags);
69                 }
70
71                 [MonoTODO ("SecureString support is incomplete")]
72                 public X509Certificate (byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
73                 {
74                         Import (rawData, password, keyStorageFlags);
75                 }
76
77                 public X509Certificate (string fileName)
78                 {
79                         Import (fileName, (string)null, X509KeyStorageFlags.DefaultKeySet);
80                 }
81
82                 public X509Certificate (string fileName, string password)
83                 {
84                         Import (fileName, password, X509KeyStorageFlags.DefaultKeySet);
85                 }
86
87                 [MonoTODO ("SecureString support is incomplete")]
88                 public X509Certificate (string fileName, SecureString password)
89                 {
90                         Import (fileName, password, X509KeyStorageFlags.DefaultKeySet);
91                 }
92
93                 public X509Certificate (string fileName, string password, X509KeyStorageFlags keyStorageFlags)
94                 {
95                         Import (fileName, password, keyStorageFlags);
96                 }
97
98                 [MonoTODO ("SecureString support is incomplete")]
99                 public X509Certificate (string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
100                 {
101                         Import (fileName, password, keyStorageFlags);
102                 }
103
104                 public X509Certificate (SerializationInfo info, StreamingContext context)
105                 {
106                         byte[] raw = (byte[]) info.GetValue ("RawData", typeof (byte[]));
107                         Import (raw, (string)null, X509KeyStorageFlags.DefaultKeySet);
108                 }
109
110
111                 public string Issuer {
112                         get {
113                                 X509Helper.ThrowIfContextInvalid (impl);
114
115                                 if (issuer_name == null)
116                                         issuer_name = impl.GetIssuerName (false);
117                                 return issuer_name;
118                         }
119                 }
120
121                 public string Subject {
122                         get {
123                                 X509Helper.ThrowIfContextInvalid (impl);
124
125                                 if (subject_name == null)
126                                         subject_name = impl.GetSubjectName (false);
127                                 return subject_name;
128                         }
129                 }
130
131                 [ComVisible (false)]
132                 public IntPtr Handle {
133                         get {
134                                 if (X509Helper.IsValid (impl))
135                                         return impl.Handle;
136                                 return IntPtr.Zero;
137                         }
138                 }
139
140
141                 [ComVisible (false)]
142                 public override bool Equals (object obj) 
143                 {
144                         X509Certificate x = (obj as X509Certificate);
145                         if (x != null)
146                                 return this.Equals (x);
147                         return false;
148                 }
149
150                 [MonoTODO ("X509ContentType.Pfx/Pkcs12 and SerializedCert are not supported")]
151                 [ComVisible (false)]
152                 public virtual byte[] Export (X509ContentType contentType)
153                 {
154                         return Export (contentType, (byte[])null);
155                 }
156
157                 [MonoTODO ("X509ContentType.Pfx/Pkcs12 and SerializedCert are not supported")]
158                 [ComVisible (false)]
159                 public virtual byte[] Export (X509ContentType contentType, string password)
160                 {
161                         byte[] pwd = (password == null) ? null : Encoding.UTF8.GetBytes (password);
162                         return Export (contentType, pwd);
163                 }
164
165                 [MonoTODO ("X509ContentType.Pfx/Pkcs12 and SerializedCert are not supported. SecureString support is incomplete.")]
166                 public virtual byte[] Export (X509ContentType contentType, SecureString password)
167                 {
168                         byte[] pwd = (password == null) ? null : password.GetBuffer ();
169                         return Export (contentType, pwd);
170                 }
171
172                 internal byte[] Export (X509ContentType contentType, byte[] password)
173                 {
174                         try {
175                                 X509Helper.ThrowIfContextInvalid (impl);
176                                 return impl.Export (contentType, password);
177                         } finally {
178                                 // protect password
179                                 if (password != null)
180                                         Array.Clear (password, 0, password.Length);
181                         }
182                 }
183
184                 [ComVisible (false)]
185                 public virtual void Import (byte[] rawData)
186                 {
187                         Import (rawData, (string)null, X509KeyStorageFlags.DefaultKeySet);
188                 }
189
190                 [MonoTODO ("missing KeyStorageFlags support")]
191                 [ComVisible (false)]
192                 public virtual void Import (byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
193                 {
194                         Reset ();
195                         impl = X509Helper.Import (rawData, password, keyStorageFlags);
196                 }
197
198                 [MonoTODO ("SecureString support is incomplete")]
199                 public virtual void Import (byte[] rawData, SecureString password, X509KeyStorageFlags keyStorageFlags)
200                 {
201                         Import (rawData, (string)null, keyStorageFlags);
202                 }
203
204                 [ComVisible (false)]
205                 public virtual void Import (string fileName)
206                 {
207                         byte[] rawData = File.ReadAllBytes (fileName);
208                         Import (rawData, (string)null, X509KeyStorageFlags.DefaultKeySet);
209                 }
210
211                 [MonoTODO ("missing KeyStorageFlags support")]
212                 [ComVisible (false)]
213                 public virtual void Import (string fileName, string password, X509KeyStorageFlags keyStorageFlags)
214                 {
215                         byte[] rawData = File.ReadAllBytes (fileName);
216                         Import (rawData, password, keyStorageFlags);
217                 }
218
219                 [MonoTODO ("SecureString support is incomplete, missing KeyStorageFlags support")]
220                 public virtual void Import (string fileName, SecureString password, X509KeyStorageFlags keyStorageFlags)
221                 {
222                         byte[] rawData = File.ReadAllBytes (fileName);
223                         Import (rawData, (string)null, keyStorageFlags);
224                 }
225
226                 void IDeserializationCallback.OnDeserialization (object sender)
227                 {
228                 }
229
230                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
231                 {
232                         if (!X509Helper.IsValid (impl))
233                                 throw new NullReferenceException ();
234                         // will throw a NRE if info is null (just like MS implementation)
235                         info.AddValue ("RawData", impl.GetRawCertData ());
236                 }
237
238                 public void Dispose ()
239                 {
240                         Dispose (true);
241                 }
242
243                 protected virtual void Dispose (bool disposing)
244                 {
245                         if (disposing)
246                                 Reset ();
247                 }
248
249                 [ComVisible (false)]
250                 public virtual void Reset ()
251                 {
252                         if (impl != null) {
253                                 impl.Dispose ();
254                                 impl = null;
255                         }
256
257                         issuer_name = null;
258                         subject_name = null;
259                         hideDates = false;
260                 }
261         }
262 }