[corlib] New lazily initialized X509Certificate.
[mono.git] / mcs / class / corlib / System.Security.Cryptography.X509Certificates / X509Helper.Apple.cs
1 using System;
2 using System.Runtime.InteropServices;
3 using MX = Mono.Security.X509;
4 using XamMac.CoreFoundation;
5
6 namespace System.Security.Cryptography.X509Certificates
7 {
8         static partial class X509Helper
9         {
10                 public static X509CertificateImpl InitFromHandle (IntPtr handle)
11                 {
12                         return new X509CertificateImplApple (handle, false);
13                 }
14
15                 public static X509CertificateImpl Import (byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
16                 {
17                         MX.X509Certificate x509;
18                         IntPtr handle;
19                         if (password == null) {
20                                 handle = CFHelpers.CreateCertificateFromData (rawData);
21                                 if (handle != IntPtr.Zero)
22                                         return new X509CertificateImplApple (handle, true);
23
24                                 try {
25                                         x509 = new MX.X509Certificate (rawData);
26                                 } catch (Exception e) {
27                                         try {
28                                                 x509 = X509Helper.ImportPkcs12 (rawData, null);
29                                         } catch {
30                                                 string msg = Locale.GetText ("Unable to decode certificate.");
31                                                 // inner exception is the original (not second) exception
32                                                 throw new CryptographicException (msg, e);
33                                         }
34                                 }
35                         } else {
36                                 // try PKCS#12
37                                 try {
38                                         x509 = X509Helper.ImportPkcs12 (rawData, password);
39                                 }
40                                 catch {
41                                         // it's possible to supply a (unrequired/unusued) password
42                                         // fix bug #79028
43                                         x509 = new MX.X509Certificate (rawData);
44                                 }
45                         }
46
47                         return new X509CertificateImplMono (x509);
48                 }
49         }
50 }