Wednesday, 15 March 2017

Empty client certificates with nodejs https in ionic

I try to performa GET request with client certificates in ionic framework. I read ionic can't do this, so i use nodeJs https-modul.

testConnectWithCert() {
  Observable.forkJoin([
    this.getLocalText('assets/data/client1-key.pem'),
    this.getLocalText('assets/data/client1-crt.pem'),
    this.getLocalText('assets/data/ca-crt.pem')]
  ).subscribe(data => {
    var options = {
      hostname: 'xxx.xxx.xxx.xxx',
      port: 4433,
      path: '/',
      method: 'GET',
      key: data[0],
      cert: data[1],
      ca: data[2]
    };

    var https = require('https');

    var req = https.request(options, (res) => {
        res.setEncoding('utf8');
        res.on('data', function(d) {
          console.log(d);
        });
    });

    req.end();

    req.on('error', function(e) {
        console.log(e);
    });
  });
}

For testing purposes, a simple nodeJs server is the counterpart.

var fs = require('fs');
var https = require('https');

var options = {
    key: fs.readFileSync('server-key.pem'),
    cert: fs.readFileSync('server-crt.pem'),
    ca: fs.readFileSync('ca-crt.pem'),

    requestCert: true,
    rejectUnauthorized: true
};

https.createServer(options, (req, res) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Request-Method', '*');
  res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
  res.setHeader('Access-Control-Allow-Headers', '*');

  var output = new Date() + ' ';
  output += req.connection.remoteAddress + ' ';
  output += req.socket.getPeerCertificate().subject.CN + ' ';
  output += req.method + ' ';
  output += req.url;
  console.log(output);

  res.writeHead(200);
  res.end("hello world\n");
}).listen(4433);

The problem: In web view (localhost:8100) and as build app on android phone, the send certificates are overwritten by the webview-browser. If i add the certificate to chrome and got to localhost:8100, everything works. But not on the mobile.

So, anyone got an idea how i can bypass the overwriting of my certificates or use the android certificate store in the app?



via hisMajesty

No comments:

Post a Comment