1 #if MONO_FEATURE_APPLETLS || MONO_FEATURE_APPLE_X509
4 using System.Runtime.InteropServices;
5 using XamMac.CoreFoundation;
6 using MX = Mono.Security.X509;
8 namespace System.Security.Cryptography.X509Certificates
10 class X509CertificateImplApple : X509CertificateImpl
13 X509CertificateImpl fallback;
15 public X509CertificateImplApple (IntPtr handle, bool owns)
19 CFHelpers.CFRetain (handle);
22 public override bool IsValid {
23 get { return handle != IntPtr.Zero; }
26 public override IntPtr Handle {
27 get { return handle; }
30 public override IntPtr GetNativeAppleCertificate ()
32 ThrowIfContextInvalid ();
36 public override X509CertificateImpl Clone ()
38 ThrowIfContextInvalid ();
39 return new X509CertificateImplApple (handle, false);
42 [DllImport (CFHelpers.SecurityLibrary)]
43 extern static IntPtr SecCertificateCopySubjectSummary (IntPtr cert);
45 [DllImport (CFHelpers.SecurityLibrary)]
46 extern static IntPtr SecCertificateCopyData (IntPtr cert);
48 public override byte[] GetRawCertData ()
50 ThrowIfContextInvalid ();
51 var data = SecCertificateCopyData (handle);
52 if (data == IntPtr.Zero)
53 throw new ArgumentException ("Not a valid certificate");
56 return CFHelpers.FetchDataBuffer (data);
58 CFHelpers.CFRelease (data);
62 public string GetSubjectSummary ()
64 ThrowIfContextInvalid ();
65 IntPtr cfstr = SecCertificateCopySubjectSummary (handle);
66 string ret = CFHelpers.FetchString (cfstr);
67 CFHelpers.CFRelease (cfstr);
71 protected override byte[] GetCertHash (bool lazy)
73 // FIXME: might just return 'null' when 'lazy' is true.
74 ThrowIfContextInvalid ();
75 SHA1 sha = SHA1.Create ();
76 return sha.ComputeHash (GetRawCertData ());
79 public override bool Equals (X509CertificateImpl other, out bool result)
81 var otherAppleImpl = other as X509CertificateImplApple;
82 if (otherAppleImpl != null && otherAppleImpl.handle == handle) {
93 ThrowIfContextInvalid ();
96 var mxCert = new MX.X509Certificate (GetRawCertData ());
97 fallback = new X509CertificateImplMono (mxCert);
100 public X509CertificateImpl FallbackImpl {
107 public override string GetSubjectName (bool legacyV1Mode)
109 return FallbackImpl.GetSubjectName (legacyV1Mode);
112 public override string GetIssuerName (bool legacyV1Mode)
114 return FallbackImpl.GetIssuerName (legacyV1Mode);
117 public override DateTime GetValidFrom ()
119 return FallbackImpl.GetValidFrom ();
122 public override DateTime GetValidUntil ()
124 return FallbackImpl.GetValidUntil ();
127 public override string GetKeyAlgorithm ()
129 return FallbackImpl.GetKeyAlgorithm ();
132 public override byte[] GetKeyAlgorithmParameters ()
134 return FallbackImpl.GetKeyAlgorithmParameters ();
137 public override byte[] GetPublicKey ()
139 return FallbackImpl.GetPublicKey ();
142 public override byte[] GetSerialNumber ()
144 return FallbackImpl.GetSerialNumber ();
147 public override byte[] Export (X509ContentType contentType, byte[] password)
149 ThrowIfContextInvalid ();
151 switch (contentType) {
152 case X509ContentType.Cert:
153 return GetRawCertData ();
154 case X509ContentType.Pfx: // this includes Pkcs12
156 throw new NotSupportedException ();
157 case X509ContentType.SerializedCert:
159 throw new NotSupportedException ();
161 string msg = Locale.GetText ("This certificate format '{0}' cannot be exported.", contentType);
162 throw new CryptographicException (msg);
166 public override string ToString (bool full)
168 ThrowIfContextInvalid ();
170 if (!full || fallback == null) {
171 var summary = GetSubjectSummary ();
172 return string.Format ("[X509Certificate: {0}]", summary);
175 string nl = Environment.NewLine;
176 StringBuilder sb = new StringBuilder ();
177 sb.AppendFormat ("[Subject]{0} {1}{0}{0}", nl, GetSubjectName (false));
179 sb.AppendFormat ("[Issuer]{0} {1}{0}{0}", nl, GetIssuerName (false));
180 sb.AppendFormat ("[Not Before]{0} {1}{0}{0}", nl, GetValidFrom ().ToLocalTime ());
181 sb.AppendFormat ("[Not After]{0} {1}{0}{0}", nl, GetValidUntil ().ToLocalTime ());
182 sb.AppendFormat ("[Thumbprint]{0} {1}{0}", nl, X509Helper.ToHexString (GetCertHash ()));
185 return sb.ToString ();
188 protected override void Dispose (bool disposing)
190 if (handle != IntPtr.Zero){
191 CFHelpers.CFRelease (handle);
192 handle = IntPtr.Zero;
194 if (fallback != null) {