简体   繁体   中英

UNABLE_TO_VERIFY_LEAF_SIGNATURE from request with Firebase functions node.js with Certificate (pfx)

I am trying to make a request from my Firebase function to a custom server that requires a certficate (.pfx). Based on this answer:

Upload TLS client certificate to Firebase cloud functions

My code is as follows:

const functions = require('firebase-functions');
const request = require('request');

var fs = require('fs');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();

exports.postBankId = functions.https.onRequest(async (req, res) => {
  console.log('PostBankId');
  const ipAddress = req.query.ipAddress;
  const requestBody = '{ "endUserIp": "' + ipAddress +'" }';
  console.log('requestBody:', requestBody); 
  const options = {
      url: 'https://appapi2.test.bankid.com/rp/v5/auth',
      json: true,
      pfx: fs.readFileSync('bankidtest.pfx'),
      passphrase: 'myPassPhraseHere',
      body: requestBody
  }

  request.post(options, (err, response) => {
      if (err) {
            console.log('bankid creation error: ' + JSON.stringify(err))
            res.status(500).send('Failed with error: ' + JSON.stringify(err));
      }
      if (response) {
          res.status(200).send('Success');
          console.log('Succes body: ' + response.body)
      }
  });
});

The answer I get:

{"code":"UNABLE_TO_VERIFY_LEAF_SIGNATURE"}

I place the bankidtest.pfx in the same folder as index.js. And it seems to be uploaded, because if removing it produces another error:

Error: could not handle the request

Edit1:

placing the path to the cert in agentOptions does not work either. Gives same UNABLE_TO_VERIFY_LEAF_SIGNATURE error.

var options = {
    url: 'https://appapi2.test.bankid.com/rp/v5/auth',
    headers: {
        "content-type": "application/json",
    },
    agentOptions: {
        pfx: fs.readFileSync('bankidtest.pfx'),
        passphrase: '********'
    }
};

Edit2: Got it semi-working. Setting the request-parameter "rejectUnauthorized" to "false", makes the request work. But according to BankId, this is not a safe or recommended way. The semi-working code is:

  request({
    url: "https://appapi2.test.bankid.com/rp/v5/auth",
    host: "appapi2.test.bankid.com",
    rejectUnauthorized: false, // This like makes it work
    requestCert: true,
    method: "POST",
    headers: {
        "content-type": "application/json", 
        'Connection': "Keep-Alive"
    },

    body: requestBody,
    agentOptions: {
        pfx: fs.readFileSync('bankidtest.pfx'),
        passphrase: '*****'
    },

Edit3: Tried npm install ssl-root-cas and then added this to the top of my index.js:

var sslRootCAs = require('ssl-root-cas/latest')
sslRootCAs.inject()

But then I got this error:

Error: EROFS: read-only file system, open '/srv/node_modules/ssl-root-cas/pems/mozilla-certdata.txt'
   at Object.fs.openSync (fs.js:646:18)
   at Object.fs.writeFileSync (fs.js:1299:33)
   at /srv/node_modules/ssl-root-cas/ca-store-generator.js:219:10
   at IncomingMessage.<anonymous> 
     (/srv/node_modules/@coolaj86/urequest/index.js:154:9)

Edit4: Tried these instead for depricated inject() but without success. No read-only-error this time, but still UNABLE_TO_VERIFY_LEAF_SIGNATURE :

var rootCas = require('ssl-root-cas/latest').create();

//rootCas.addFile(__dirname + '/mycerts.crt');
rootCas.addFile('mycerts.cer');
rootCas.addFile('mycerts.crt');
rootCas.addFile('bankidtest.pfx'); // Also tried with __dirname

require('https').globalAgent.options.ca = rootCas;

// Also tried this:
//require('https').globalAgent.options.ca = require('ssl-root-cas').rootCas

Edit5 Solved it

Seemed as a CA was needed that was not derived from the pfx file.

Bank-ID provided a CA as a text in their documentation. Starting with "-----BEGIN CERTIFICATE"... I copied the text into a pem file and referenced it from my index.js-file like this:

agentOptions: {
    pfx: fs.readFileSync('bankidtest.pfx'),
    passphrase: '*****',
    ca: fs.readFileSync('certificate.pem')
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM