[System]: New X509Helper2 APIs for BTLS.
authorMartin Baulig <martin.baulig@xamarin.com>
Fri, 30 Sep 2016 10:55:31 +0000 (12:55 +0200)
committerMartin Baulig <martin.baulig@xamarin.com>
Fri, 30 Sep 2016 11:03:42 +0000 (13:03 +0200)
The managed HAVE_BTLS conditionals will be removed once BTLS itself has landed.

mcs/class/System/System.Security.Cryptography.X509Certificates/X509ChainPolicy.cs
mcs/class/System/System.Security.Cryptography.X509Certificates/X509Helper2.cs

index 1dc234575b86a3cb6806c7b471f8d0ba8ec9f694..2fcdd982f33366e8b1b163a1aa178947232fbd74 100644 (file)
@@ -91,6 +91,9 @@ namespace System.Security.Cryptography.X509Certificates {
                                }
                                return store2;
                        }
+                       internal set {
+                               store2 = value;
+                       }
                }
 
                public X509RevocationFlag RevocationFlag {
index 2a9163a346f89ae143e7477b9215c67659f5e875..92709e04fa8a77a2c599fd7b7121d55e6a86b45b 100644 (file)
@@ -37,10 +37,49 @@ using MonoSecurity::Mono.Security.Interface;
 using Mono.Security.Interface;
 #endif
 
+#if HAVE_BTLS
+using Mono.Btls;
+#endif
+#endif
+
+using System.IO;
+using System.Text;
+
 namespace System.Security.Cryptography.X509Certificates
 {
        internal static class X509Helper2
        {
+               internal static long GetSubjectNameHash (X509Certificate certificate)
+               {
+                       return GetSubjectNameHash (certificate.Impl);
+               }
+
+               internal static long GetSubjectNameHash (X509CertificateImpl impl)
+               {
+#if SECURITY_DEP && HAVE_BTLS
+                       using (var x509 = GetNativeInstance (impl))
+                               return GetSubjectNameHash (x509);
+#else
+                       throw new NotSupportedException ();
+#endif
+               }
+
+               internal static void ExportAsPEM (X509Certificate certificate, Stream stream, bool includeHumanReadableForm)
+               {
+                       ExportAsPEM (certificate.Impl, stream, includeHumanReadableForm);
+               }
+
+               internal static void ExportAsPEM (X509CertificateImpl impl, Stream stream, bool includeHumanReadableForm)
+               {
+#if SECURITY_DEP && HAVE_BTLS
+                       using (var x509 = GetNativeInstance (impl))
+                               ExportAsPEM (x509, stream, includeHumanReadableForm);
+#else
+                       throw new NotSupportedException ();
+#endif
+               }
+
+#if SECURITY_DEP
                internal static void Initialize ()
                {
                        X509Helper.InstallNativeHelper (new MyNativeHelper ());
@@ -51,6 +90,31 @@ namespace System.Security.Cryptography.X509Certificates
                        X509Helper.ThrowIfContextInvalid (impl);
                }
 
+#if HAVE_BTLS
+               static MonoBtlsX509 GetNativeInstance (X509CertificateImpl impl)
+               {
+                       ThrowIfContextInvalid (impl);
+                       var btlsImpl = impl as X509CertificateImplBtls;
+                       if (btlsImpl != null)
+                               return btlsImpl.X509.Copy ();
+                       else
+                               return MonoBtlsX509.LoadFromData (impl.GetRawCertData (), MonoBtlsX509Format.DER);
+               }
+
+               internal static long GetSubjectNameHash (MonoBtlsX509 x509)
+               {
+                       using (var subject = x509.GetSubjectName ())
+                               return subject.GetHash ();
+               }
+
+               internal static void ExportAsPEM (MonoBtlsX509 x509, Stream stream, bool includeHumanReadableForm)
+               {
+                       using (var bio = MonoBtlsBio.CreateMonoStream (stream)) {
+                               x509.ExportAsPEM (bio, includeHumanReadableForm);
+                       }
+               }
+#endif
+
                internal static X509Certificate2Impl Import (byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)
                {
                        var provider = MonoTlsProviderFactory.GetProvider ();
@@ -111,6 +175,6 @@ namespace System.Security.Cryptography.X509Certificates
                                return X509Helper2.Import (cert);
                        }
                }
+#endif
        }
 }
-#endif