Code Monkey home page Code Monkey logo

xamarin.sslpinning's Introduction

Xamarin.SSLPinning

Test project to setup SSL pinning with Xamarin

Looking for a workaround for blog post here

Basically I did the following to get this to work:

  • Dumped Xamarin's source for NSUrlSessionHandler.cs and its code in HttpClientEx.cs
  • Figured out a quick command to export certificate
  • Wrote some C# code ported from Obj-C here

My code:

var serverCertChain = challenge.ProtectionSpace.ServerSecTrust;
var first = serverCertChain[0].DerData;
var cert = NSData.FromFile("httpbin.cer");
if (first.IsEqual(cert))
{
    completionHandler(NSUrlSessionAuthChallengeDisposition.PerformDefaultHandling, challenge.ProposedCredential);
}
else
{
    completionHandler(NSUrlSessionAuthChallengeDisposition.RejectProtectionSpace, null);
}

NOTE: you may want to do some checks for empty array & reuse NSData for better performance

MORE NOTE: I would not ship the public key as a flat file in your app, place it somewhere safe inside an assembly so an attacker will not easily replace it with their own

If you need to get the cert for your own site, run the command:

openssl s_client -connect httpbin.org:443 | openssl x509 -outform DER > httpbin.cer

Replace httpbin with your domain.

If Xamarin could somehow expose DidReceiveChallenge that would be awesome!

xamarin.sslpinning's People

Contributors

jonathanpeppers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

xamarin.sslpinning's Issues

POST method fails with 404 (Bad Request)

Thanks Jonathan for posting a workaround for iOS certificate pinning till HttpClient supports ServerCertificateValidationCallback with NSUrlSession.
I copied your example with NSUrlSessionHandler and certificate pinning works great. However, all the POST (or PostAsync) calls fails with 404 (Bad Request).
I checked server logs and found that the JSON post data is corrupted.

Did you observe the same? How do we fix it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.