How to implement SSL Key Pinning in iOS in Ionic 3?

2.3k views Asked by At

I did not have much knowledge in iOS classes. I am building an iOS app(Ionic 3) which required the SSL pinning. Most of the google example based on swift. May I know the steps or could anybody provide some links regarding iOS SSL pinning?

PS: I already have a certificate in my server. Also, I have done the SSL pinning with Network security configuration for Android. Ref https://developer.android.com/training/articles/security-config. Its working fine.

Thanks in advance.

1

There are 1 answers

0
Dušan Argaláš On

Ionic 5.4.15 version solution.

To enable SSL pinning in ionic create a directory inside your root folder e.g. "certificates" and put all your certificates inside this folder. IMPORTANT: all certificates inside this folder must have suffix .cer!!!

After that modify angular.json in root project directory append this part to all occurencies of "assets" array.

{   
"glob": "**/*", 
"input": "certificates",    
"output": "certificates"
}

then delete your www directory in root project and run "ionic build", it will generate new subdirectory "certificates" in your www folder

USE of certificates in Typescript:

I am using ionic-native http and cordova-advanced-http-plugin

Install:

ionic cordova plugin add cordova-plugin-advanced-http
npm install @ionic-native/http

Import in your root, xyz.module.ts file:

import { HTTP } from '@ionic-native/http/ngx';

Append it to providers:

 providers: [
    StatusBar,
    SplashScreen,
    **HTTP**,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],

Import in your page/component, xyz.ts file:

import { HTTP } from '@ionic-native/http/ngx';

Declare in constructor:

constructor(private http: HTTP) {}

Pinning the certificates before any request made:

  async ngOnInit() {
    await this.platform.ready();
    this.advHttp.setServerTrustMode('pinned').then((res: any) => {
    }, (error) => {
      this.helpers.showError(error);
    });
    this.advHttp.setRequestTimeout(5);
  }

Now youre all set and may use https requests! Docs: https://ionicframework.com/docs/native/http