【问题标题】:Is my key really ignored in my PHP CURL SSL call?我的密钥在我的 PHP CURL SSL 调用中真的被忽略了吗?
【发布时间】:2016-08-18 21:23:47
【问题描述】:

我正在使用 TestApi 使用 PEM 和 KEY 文件检查 SSL 连接。 但是,如果我编辑我的私钥,似乎无论如何我都可以获得标题。 它真的应该这样吗?我从来没有收到关于密钥的错误。但是,当认证不正确时,我确实会出错。

我总是得到一个

HTTP/1.1 200 OK 内容类型:text/xml;charset=UTF-8 内容长度:5983 日期:2016 年 8 月 18 日星期四 21:10:33 GMT 服务器:TheServer TheServer BUS RP 接口版本:4.0。 0

接着是来自htmlentities 的大量文本。

$url = "https://mylink?wsdl";

// cert file/pass (same as pfx above but converted to pem and key)
$cert_file = "myPemfile.pem";
$cert_password = "myKeyfile.key"; // I've intentionally set the wrong key... but God knows why it still works

// server cert which we trust (this is needed when using VERIFYPEER below)
$cert_server = "serverCA.pem";

$ch = curl_init();

$options = array( 
    CURLOPT_RETURNTRANSFER => true, //return output
    CURLOPT_HEADER         => true, // just to see header response
    CURLOPT_FOLLOWLOCATION => true, // cant figure this out yet (probably means internal redirect within $url)
    CURLOPT_SSL_VERIFYHOST => 2, //ok
    CURLOPT_SSL_VERIFYPEER => true, // yes
    CURLOPT_CAINFO => $cert_server, // set it
    CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)', // ? seems this is needed
    CURLOPT_VERBOSE        => false, // change to true/false if you want/(dont want) verbose
    CURLOPT_URL => $url , //set
    CURLOPT_SSLCERT => $cert_file,  //set
    //CURLOPT_SSLCERTTYPE => 'PEM', // well.. default is PEM anyway
    CURLOPT_SSLKEY => $cert_password, //set
    );
curl_setopt_array($ch , $options);
$output = curl_exec($ch);

if(!$output)
{
    echo "Curl Error : " . curl_error($ch);
}
else
{
    echo "<br /> db: output unparsed :<br />" . $output . "<br /> db: DONE <br /><br />";

    echo htmlentities($output);

}

【问题讨论】:

  • 我一直对此有点模糊,但发现不需要设置 certs\keys 但发现 curl 会自己处理它。
  • 在这种情况下,我什至不需要指出密钥文件就可以了。我不知道这是否只是因为它是一个 testApi ..也许?但是你所说的“curl 自己照顾它”听起来并不令人信服,也没有任何意义......但是......我不知道你可能是对的
  • 你的浏览器会自己处理它,对吧? curl 有一个证书包,就像浏览器一样。但是就像我说的有点朦胧,我确实使用 curl 通过 https 来命中 api 端点,而没有添加任何特殊的东西,因为它是 https 与 http。
  • 好吧..这更有意义。但是忽略私钥?真的吗?我更倾向于我在这里做了一些事情。无论是代码还是思想
  • 我刚刚看到 PEM 文件包含私钥...为什么我需要一个单独的 KEY 文件?...呵呵...

标签: php ssl curl


【解决方案1】:

有几种可能符合您的描述:

  • 服务器根本不需要客户端证书。在这种情况下,您提供什么客户端证书和密钥并不重要,因为它无论如何都不会被使用。
  • 真正的密钥已经包含在您为证书提供的 PEM 文件中

【讨论】:

  • 是的,我想了很多关于 PEM 文件中的密钥。我将对此进行试验并回复您。我应该能够从 PEM 文件中删除私钥部分吧?
  • @niCkcAMel:是的,您可以在文本编辑器中简单地编辑文件并删除-----BEGIN PRIVATE KEY----- .... -----END PRIVATE KEY-----之间的部分(包括-----BEGIN PRIVATE KEY----- .... -----END PRIVATE KEY-----)。
  • 哦,好吧,那么前缀和后缀PRIVATE KEY 需要存在吗?
  • @niCkcAMel:这个前缀和后缀属于key的PEM编码,应该和key一起去掉
  • 我错过了你的“……包括……”。对不起,我的坏事
猜你喜欢
  • 2011-12-18
  • 2015-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-14
  • 2018-07-11
  • 2023-03-18
相关资源
最近更新 更多