如何使用SSL证书对API进行身份验证?

时间:2020-03-27 09:01:36

标签: node.js api ssl certificate pem

我需要访问服务提供商的API(对于我的公司) 因此,他们给了我一个“ doc”和多种形式的SSL证书(.jks,.p12,.pem) 我使用NodeJS,所以我拿了.pem,里面有2个证书和1个加密的私钥。

我将.pem分为3个文件,mycert.crt.pem,mycert.key.pem,mycert2.crt.pem (我在https://www.sslshopper.com/certificate-key-matcher.html上进行了检查,以了解密钥使用了哪种证书)

所以我的NodeJS,我使用了最少的模块来避免模块问题:

const cert = fs.readFileSync(path.resolve('cert', 'mycert.crt.pem'))
const key = fs.readFileSync(path.resolve('cert', 'mycert.key.pem'))

let options = {
    hostname: 'https://serviceproviderurl.com',
    path: 'v1/api/example',
    method: 'POST',
    key: key,
    cert: cert
}

let req = https.request(options, function (res) {
    console.log(res.statusCode)
    res.on('data', function (d) {
      process.stdout.write(d)
    })
})
req.end()

错误消息:

Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
    at Object.createSecureContext (_tls_common.js:151:17)
    at Object.connect (_tls_wrap.js:1407:48)
    at Agent.createConnection (https.js:125:22)
    at Agent.createSocket (_http_agent.js:234:26)
    at Agent.addRequest (_http_agent.js:193:10)
    at new ClientRequest (_http_client.js:276:16)
    at Object.request (https.js:309:10)
    at Object.<anonymous> (/mnt/c/Project/test.js:74:17)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10) {
  opensslErrorStack: [
    'error:0907B00D:PEM routines:PEM_read_bio_PrivateKey:ASN1 lib',
    'error:2306A075:PKCS12 routines:PKCS12_item_decrypt_d2i:pkcs12 pbe crypt error',
    'error:23077074:PKCS12 routines:PKCS12_pbe_crypt:pkcs12 cipherfinal error'
  ],
  library: 'digital envelope routines',
  function: 'EVP_DecryptFinal_ex',
  reason: 'bad decrypt',
  code: 'ERR_OSSL_EVP_BAD_DECRYPT'

我想我错过了3个证书。他们也没有给我密码,这正常吗? 我对SSL证书的使用不了解,希望您能对我有所帮助。

谢谢

0 个答案:

没有答案