Ejemplo n.º 1
0
        public void ProviderInstallerCheck(Context context)
        {
            try
            {
                // Checking if the ProviderInstaller is installed and updated
                ProviderInstaller.InstallIfNeeded(context);
                Toast.MakeText(context, "Provider installed and updated!", ToastLength.Long).Show();
            }
            catch (GooglePlayServicesRepairableException e)
            {
                /* If the ProviderInstaller is installed but not updated
                 * A popup asks the user to do a manual update of the Google Play Services
                 */
#pragma warning disable CS0618 // Type or member is obsolete
                GooglePlayServicesUtil.ShowErrorNotification(e.ConnectionStatusCode, context);
#pragma warning restore CS0618 // Type or member is obsolete
                Toast.MakeText(context, "Provider it outdated. Please update your Google Play Service", ToastLength.Long).Show();
            }
            catch (GooglePlayServicesNotAvailableException e)
            {
                /* If the ProviderInstaller is not installed but not updated
                 * A popup redirects the user to the Google Play Services page on the Google PlayStore
                 * and let the user download them.
                 */
#pragma warning disable CS0618 // Type or member is obsolete
                Dialog dialog = GooglePlayServicesUtil.GetErrorDialog(e.ErrorCode, this, REQUEST_GOOGLE_PLAY_SERVICES_DOWNLOAD);
#pragma warning restore CS0618 // Type or member is obsolete

                dialog.SetCancelable(false);
                dialog.Show();
            }
        }
Ejemplo n.º 2
0
        public override void OnCreate()
        {
            base.OnCreate();
            CrossCurrentActivity.Current.Init(this);

            if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
            {
                // Support TLS1.2 on Android versions before Lollipop
                ProviderInstaller.InstallIfNeeded(Application.Context);
            }
        }
Ejemplo n.º 3
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
            {
                // Support TLS1.2 on Android versions before Lollipop
                ProviderInstaller.InstallIfNeeded(Application.Context);
            }

            var vm = new MainViewModel();

            Button button = FindViewById <Button>(Resource.Id.myButton);

            button.Click += async delegate {
                // The easiest way to pin a host is turn on pinning with a broken configuration and read the expected configuration when the connection fails.
                // Be sure to do this on a trusted network, and without man -in-the - middle tools like Charles or Fiddler.

                /*var hostname = "gorest.co.in";
                 *
                 * var certificatePinner = new Square.OkHttp3.CertificatePinner.Builder()
                 *  .Add(hostname, "sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
                 *  .Build();
                 *
                 * var client = new OkHttpClient.Builder()
                 *  .CertificatePinner(certificatePinner)
                 *  .Build();
                 *
                 * var request = new Request.Builder()
                 *  .Url("https://" + hostname)
                 *  .Build();
                 *
                 * var call = client.NewCall(request);
                 *
                 * var response = await call.ExecuteAsync();*/

                // As expected, this fails with a certificate pinning exception:

                /*Certificate pinning failure!
                 * Peer certificate chain:
                 *  sha256/CZEvkurQ3diX6pndH4Z5/dUNzK1Gm6+n8Hdx/DQgjO0=: CN=sni96286.cloudflaressl.com,OU=PositiveSSL Multi-Domain,OU=Domain Control Validated
                 *  sha256/x9SZw6TwIqfmvrLZ/kz1o0Ossjmn728BnBKpUFqGNVM=: CN=COMODO ECC Domain Validation Secure Server CA 2,O=COMODO CA Limited,L=Salford,ST=Greater Manchester,C=GB
                 *  sha256/58qRu/uxh4gFezqAcERupSkRYBlBAvfcw7mEjGPLnNU=: CN=COMODO ECC Certification Authority,O=COMODO CA Limited,L=Salford,ST=Greater Manchester,C=GB
                 * Pinned certificates for reqres.in:
                 *  sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=*/

                // Follow up by pasting the public key hashes from the exception into the NativeMessageHandler certificate pinner's configuration.

                await vm.Get();
            };
        }
Ejemplo n.º 4
0
        protected override void OnCreate(Bundle bundle)
        {
            try
            {
                base.OnCreate(bundle);
                InputDialog.DisplayDialog += OnDisplayDialog;
                AndroidEnvironment.UnhandledExceptionRaiser += HandleUnhandledException;

                global::Xamarin.Forms.Forms.Init(this, bundle);

                context = ApplicationContext; // or activity.getApplicationContext()
                PackageManager packageManager = context.PackageManager;
                string         packageName    = context.PackageName;
                AppData.Version = new OnSiteVersion(string.Format("{0}", packageManager.GetPackageInfo(packageName, 0).VersionName));

                LoadApplication(new App());

                //AppContext.AppContext.ShowInput = () =>
                //{
                //    InputMethodManager showinput = (InputMethodManager)GetSystemService(InputMethodService);
                //    showinput.ToggleSoftInput(ShowFlags.Forced, 0);
                //};


                if (Build.VERSION.SdkInt <= BuildVersionCodes.Kitkat)
                {
                    ProviderInstaller.InstallIfNeeded(ApplicationContext);
                }
                SSLContext sslContext = SSLContext.GetInstance("TLSv1.2");
                sslContext.Init(null, null, null);
                SSLEngine engine = sslContext.CreateSSLEngine();
            }
            catch (Exception ex)
            {
                LogTracking.LogTrace(ex.ToString());
            }
        }
        public NativeMessageHandler(bool throwOnCaptiveNetwork, TLSConfig tLSConfig, NativeCookieHandler cookieHandler = null, IWebProxy proxy = null)
        {
            this.throwOnCaptiveNetwork = throwOnCaptiveNetwork;

            var clientBuilder = client.NewBuilder();

            this.TLSConfig = tLSConfig;

            var tlsSpecBuilder = new ConnectionSpec.Builder(ConnectionSpec.ModernTls).TlsVersions(new[] { TlsVersion.Tls12, TlsVersion.Tls13 });
            var tlsSpec        = tlsSpecBuilder.Build();

            var specs = new List <ConnectionSpec>()
            {
                tlsSpec
            };

            if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop || NetworkSecurityPolicy.Instance.IsCleartextTrafficPermitted)
            {
                specs.Add(ConnectionSpec.Cleartext);
            }

            clientBuilder.ConnectionSpecs(specs);
            clientBuilder.Protocols(new[] { Protocol.Http11 }); // Required to avoid stream was reset: PROTOCOL_ERROR

            // Add Certificate Pins
            if (!TLSConfig.DangerousAcceptAnyServerCertificateValidator &&
                TLSConfig.Pins != null &&
                TLSConfig.Pins.Count > 0 &&
                TLSConfig.Pins.FirstOrDefault(p => p.PublicKeys.Count() > 0) != null)
            {
                this.PinningMode = "PublicKeysOnly";

                this.CertificatePinner = new CertificatePinner();

                foreach (var pin in TLSConfig.Pins)
                {
                    this.CertificatePinner.AddPins(pin.Hostname, pin.PublicKeys);
                }

                clientBuilder.CertificatePinner(CertificatePinner.Build());
            }

            // Set client credentials
            SetClientCertificate(TLSConfig.ClientCertificate);

            if (cookieHandler != null)
            {
                clientBuilder.CookieJar(cookieHandler);
            }

            // Adding proxy support
            if (proxy != null && proxy is WebProxy)
            {
                var webProxy = proxy as WebProxy;

                var type    = Java.Net.Proxy.Type.Http;
                var address = new InetSocketAddress(webProxy.Address.Host, webProxy.Address.Port);
                var jProxy  = new Proxy(type, address);
                clientBuilder.Proxy(jProxy);

                if (webProxy.Credentials != null)
                {
                    var credentials = (NetworkCredential)webProxy.Credentials;
                    clientBuilder.ProxyAuthenticator(new ProxyAuthenticator(credentials.UserName, credentials.Password));
                }
            }

            var sslContext = SSLContext.GetInstance("TLS");

            // Support self-signed certificates
            if (TLSConfig.DangerousAcceptAnyServerCertificateValidator)
            {
                // Install the all-trusting trust manager
                var trustManager = new CustomX509TrustManager();
                sslContext.Init(KeyManagers, new ITrustManager[] { trustManager }, new SecureRandom());
                // Create an ssl socket factory with our all-trusting manager
                var sslSocketFactory = sslContext.SocketFactory;
                clientBuilder.SslSocketFactory(sslSocketFactory, trustManager);
            }
            else
            {
                // Set SslSocketFactory
                if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
                {
                    // Support TLS1.2 on Android versions before Lollipop
                    ProviderInstaller.InstallIfNeeded(Application.Context); // To enable TLS
                    clientBuilder.SslSocketFactory(new TlsSslSocketFactory(), TlsSslSocketFactory.GetSystemDefaultTrustManager());
                }
                else
                {
                    sslContext.Init(KeyManagers, null, null);
                    clientBuilder.SslSocketFactory(sslContext.SocketFactory, TlsSslSocketFactory.GetSystemDefaultTrustManager());
                }
            }

            clientBuilder.HostnameVerifier(new HostnameVerifier(this));
            client = clientBuilder.Build();
        }