简体   繁体   English

cURL,php和SSL错误

[英]cURL, php and SSL error

I got instructions from an organisation how to connect to their server with a CA, key and cert. 我从一个组织那里获得了有关如何使用CA,密钥和证书连接到其服务器的说明。 Tried in the terminal successfully with the following: 在终端中成功尝试了以下操作:

openssl s_client -connect api-system3.xxxx.com:443 -CAfile teliasonerarootcav1.cer -cert BolagACert.crt -key BolagAKey.key

and a following GET request. 以及以下GET请求。 Seems to return ok: 似乎返回确定:

    CONNECTED(00000003)
    depth=2 O = TeliaSonera, CN = TeliaSonera Root CA v1
    verify return:1
    depth=1 C = FI, O = TeliaSonera, CN = TeliaSonera Server CA v2
    verify return:1
    depth=0 C = SE, L = XXXXX, O = XXXXX, OU = IT, CN = *.XXXX.COM
    verify return:1
    ---
    Certificate chain
     0 s:/C=SE/L=XXXXX/O=XXXXX/OU=IT/CN=*.XXXXXX
       i:/C=FI/O=TeliaSonera/CN=TeliaSonera Server CA v2
     1 s:/C=FI/O=TeliaSonera/CN=TeliaSonera Server CA v2
       i:/O=TeliaSonera/CN=TeliaSonera Root CA v1
    ---
    Server certificate
    -----BEGIN CERTIFICATE-----
   XXXXXX
    -----END CERTIFICATE-----
    subject=/C=XX/L=XXXXX/O=XXXXXX/OU=IT/CN=*.XXXXXX.COM
    issuer=/C=FI/O=TeliaSonera/CN=TeliaSonera Server CA v2
    ---
    No client certificate CA names sent
    Peer signing digest: SHA256
    Server Temp Key: ECDH, P-256, 256 bits
    ---
    SSL handshake has read 3879 bytes and written 441 bytes
    ---
    New, TLSv1/SSLv3, Cipher is XXXXX
    Server public key is 2048 bit
    Secure Renegotiation IS supported
    Compression: NONE
    Expansion: NONE
    No ALPN negotiated
    SSL-Session:
        Protocol  : TLSv1.2
        Cipher    : XXXXX
        Session-ID: XXXXXX
        Session-ID-ctx: 
        Master-Key: XXXXXX
        Key-Arg   : None
        PSK identity: None
        PSK identity hint: None
        SRP username: None
        Start Time: 1517505794
        Timeout   : 300 (sec)
        Verify return code: 0 (ok)
    ---
    GET /XXXXXX/
    depth=2 O = TeliaSonera, CN = TeliaSonera Root CA v1
    verify return:1
    depth=1 C = FI, O = TeliaSonera, CN = TeliaSonera Server CA v2
    verify return:1
    depth=0 C = XX, L = XXXX, O = XXXXX, OU = IT, CN = *.XXXXX.COM
    verify return:1
    read R BLOCK
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>302 Found</title>
    </head><body>
    <h1>Found</h1>
    <p>The document has moved <a href="https://www.xxxxx.com/">here</a>.</p>
    <hr>
    <address>Apache Server at system3-jas123.system3.xxxxx.com Port 443</address>
    </body></html>
    read:errno=0

Trying to implement this into a PHP cURL request but the code below generates the error: The requested URL returned error: 403 Forbidden 尝试将其实现为PHP cURL请求,但以下代码生成错误: The requested URL returned error: 403 Forbidden

Any thoughts what is wrong? 有什么想法怎么了?

Code: 码:

$CAfile = getcwd()."/teliasonerarootcav1.cer";
$pemfile = getcwd()."/BolagACert.crt";
$keyfile = getcwd()."/BolagAKey.key";
$url = "https://xxxx.com/xxxxx";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); 
curl_setopt($ch, CURLOPT_FAILONERROR, 1); 
curl_setopt($ch, CURLOPT_SSLCERT, $pemfile); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
curl_setopt($ch, CURLOPT_CAINFO,  $CAfile);
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM'); 
curl_setopt($ch, CURLOPT_SSLKEY, $keyfile); 
$ret = curl_exec($ch);

//if error
if ($ret === false) {
    $info = curl_error($ch);
    curl_close($ch);
    die('Error: ' . $info);
}


curl_close($ch);

echo "<pre>";
print_r(json_decode($ret,true));
echo "</pre>";

SSL and HTTP response code are two different stories. SSL和HTTP响应代码是两个不同的故事。 First you use SSL to connect to the server, then the server processes the request and returns response code. 首先,您使用SSL连接到服务器,然后服务器处理请求并返回响应代码。 In your openssl dump there's nothing about what HTTP response code is, so it may actually have returned 403. On the other hand the 403 response in PHP means you got the connection set up correctly and the server does process your request. 在您的openssl转储中,没有什么关于HTTP响应代码的信息,因此它实际上已经返回了403。另一方面,PHP中的403响应意味着您已正确设置了连接,服务器也处理了您的请求。 Please check if the API requires some kind of credentials passed along. 请检查API是否需要传递某种凭据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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