Php 如何向Transunion API发送pkcs12(p12)证书?

Php 如何向Transunion API发送pkcs12(p12)证书?,php,api,curl,certificate,Php,Api,Curl,Certificate,我在使用cURL通过php连接TransUnion的测试API时遇到问题。如果有人已经这样做了,请告诉我。我已经准备好将XML文件发送给他们,我只是不知道问题出在哪里,因为我从他们那里收到了一个包含证书和密钥的.p12文件,但它仍然不允许我连接。我尝试了以下方法: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_set

我在使用cURL通过php连接TransUnion的测试API时遇到问题。如果有人已经这样做了,请告诉我。我已经准备好将XML文件发送给他们,我只是不知道问题出在哪里,因为我从他们那里收到了一个包含证书和密钥的.p12文件,但它仍然不允许我连接。我尝试了以下方法:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_SSLCERT, getcwd().'/certs/cert.pem');
    curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'test_pass');
    curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
    curl_setopt($ch, CURLOPT_SSLKEY, getcwd().'/certs/key.pem');
    curl_setopt($ch, CURLOPT_SSLKEYPASSWD, 'test_pass');
然后我尝试通过mac上的终端连接,使用:

curl -cert /Users/temp_user/cert.pem   -key /Users/temp_user/key.pem https://netaccess-test.transunion.com

有人能告诉我我做错了什么吗。谢谢。

请确保从p12文件中正确提取证书,如下所示:

要提取CA证书,请执行以下操作:

openssl pkcs12 -in NAME_OF_P12_FILE.p12 -cacerts -nokeys -out NAME_OF_PEM_FILE_TO_CREATE.pem
openssl pkcs12 -in NAME_OF_P12_FILE.p12 -clcerts -nokeys -out NAME_OF_PEM_FILE_TO_CREATE.pem
要提取个人证书,请执行以下操作:

openssl pkcs12 -in NAME_OF_P12_FILE.p12 -cacerts -nokeys -out NAME_OF_PEM_FILE_TO_CREATE.pem
openssl pkcs12 -in NAME_OF_P12_FILE.p12 -clcerts -nokeys -out NAME_OF_PEM_FILE_TO_CREATE.pem
要提取私钥,请执行以下操作:

密码:
openssl pkcs12-in NAME\u OF_P12\u FILE.P12-clcerts-nocerts-out NAME\u OF_PEM\u OF_PEM\u FILE\u TO_CREATE.PEM


没有密码:
openssl pkcs12-in NAME_OF_P12_FILE.P12-clcerts-nocerts-nodes-out NAME_OF_PEM_FILE_TO_CREATE.PEM

我知道这是一篇较老的帖子,但因为我在试图找出与TransUnion的连接问题时遇到了它,我想我应该把我所做的事情发布出来,以防其他人还需要帮助

我与TransUnion支持团队合作,将我所拥有的和他们所拥有的信息结合起来,我能够得到一个有效的解决方案

我发现最大的问题是到处都有关于如何转换证书的说明

使用以下命令转换证书,以获得连接所需的部件。是的,你需要3个,大多数答案都说只能得到2个,但你需要全部3个:

将证书转换为客户端的三个不同证书、私钥和证书颁发机构证书

openssl pkcs12-in-client_systemID.p12-out ca.pem-cacerts-nokeys//将.p12文件中的ca证书输出到ca.pem

openssl pkcs12-in-client_systemID.p12-out-client.pem-clcerts-nokeys//将客户端证书从.p12文件输出到client.pem

openssl pkcs12-in client_systemID.p12-out key.pem-nocerts-nodes//将私钥从.p12输出到key.pem

然后,您可以开始设置代码:

$keyFile = "key.pem";
$caFile = "ca.pem";
$certFile = "client.pem";
$certPass = $_ENV['TUNASSLPass']; //I am storing the passphrase in an Env variable
$URL = "https://netaccess-test.transunion.com";
$data = "<tuna-request-data>"; //need to set this to append to the URL
$xml = "<?xml version='1.0' encoding='UTF-8'?><creditBureau xmlns='http://www.transunion.com/namespace' xsi:schemaLocation='http://www.transunion.com/namespace creditBureau.xsd' xmlns:xsi='http://www.w3.org/3001/XMLSchema-instance'>{The rest of your XML}</creditBureau>";

// Initialise cURL
$ch = curl_init($actualUrl);

// The -d option is equivalent to CURLOPT_POSTFIELDS. But...
// PHP's libcurl interface does not implement the -G flag - instead you would
// append $data to $url like this:
$actualUrl = $URL.'?'.$data;
curl_setopt($ch, CURLOPT_URL, $actualUrl);

// The -v flag only makes sense at the command line, but it can be enabled
// with CURLOPT_VERBOSE - in this case the information will be written to
// STDERR, or the file specified by CURLOPT_STDERR. I will ignore this for
// now, but if you would like a demonstration let me know.

// The --key option - If your key file has a password, you will need to set
// this with CURLOPT_SSLKEYPASSWD
curl_setopt($ch, CURLOPT_SSLKEY, $keyFile);

// The --cacert option
curl_setopt($ch, CURLOPT_CAINFO, $caFile);

// The --cert option
curl_setopt($ch, CURLOPT_SSLCERT, $certFile);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $certPass);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "xml=" . $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
try
{
    $result = curl_exec($ch);
}
catch (Exception $e) 
{
    echo 'There was an issue querying TransUnion.  Here is the returned exception info: ',  $e->getMessage(), "\n";
}

if (curl_errno($ch) > 0)
{
    $result = array('errocurl' => curl_errno($ch), 'msgcurl' => curl_error($ch));
    echo "There was an error calling Trans Union.  Here is the error info: <br>" . curl_error($ch);
}
curl_close($ch);
$keyFile=“key.pem”;
$caFile=“ca.pem”;
$certFile=“client.pem”;
$certPass=$_ENV['TUNASSLPass']//我将密码短语存储在一个Env变量中
$URL=”https://netaccess-test.transunion.com";
$data=“”//需要将其设置为附加到URL
$xml=“{xml的其余部分}”;
//初始化卷曲
$ch=curl_init($actualUrl);
//-d选项相当于CURLOPT_POSTFIELDS。但是
//PHP的libcurl接口并没有实现-G标志——相反,您可以
//将$data附加到$url,如下所示:
$actualUrl=$URL.“?”.$data;
curl_setopt($ch,CURLOPT_URL,$actualur);
//-v标志仅在命令行中有意义,但可以启用它
//使用CURLOPT_VERBOSE-在这种情况下,信息将写入
//STDERR,或curloptu STDERR指定的文件。我将忽略这一点
//现在,如果你想演示,请告诉我。
//--key选项—如果密钥文件有密码,则需要设置
//使用CURLOPT_SSLKEYPASSWD执行此操作
curl_setopt($ch,CURLOPT_SSLKEY,$keyFile);
//--cacert选项
curl_setopt($ch,CURLOPT_CAINFO,$caFile);
//--cert选项
curl_setopt($ch,CURLOPT_SSLCERT,$certFile);
curl_setopt($ch,CURLOPT_SSLCERTPASSWD,$certPass);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type:application/x-www-form-urlencoded');
curl_setopt($ch,CURLOPT_POSTFIELDS,“xml=”.$xml);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
尝试
{
$result=curl\u exec($ch);
}
捕获(例外$e)
{
echo“查询TransUnion时出现问题。以下是返回的异常信息:”,$e->getMessage(),“\n”;
}
如果(旋度误差($ch)>0)
{
$result=array('errocurl'=>curl\u errno($ch),'msgcurl'=>curl\u error($ch));
echo“调用Trans Union时出错。以下是错误信息:
”。curl\u error($ch); } 卷曲关闭($ch);
您返回了哪个错误?使用curl,尝试使用verbose开关(-v在命令行中,以及使用curl_setopt的PHP中的相关选项),以了解更多关于幕后发生的事情。我忘了提到我在命令行上使用OpenSSL从.p12文件创建证书(cert.pem)和密钥(key.pem)文件。但是,当我在命令行中添加--verbose时,我收到了以下消息:40数字证书无效。请确保您拥有正确的数字证书或下载新的数字证书,然后重新提交交易。如果情况仍然存在,请致电800-813-5604通知TransUnion服务台。*关闭连接#0*SSLv3,TLS警报,客户端你好(1):检查是否正确转换。出于测试的原因,我认为您可以从这些文件中删除密码。我不知道怎么做才好。即使.p12是一个密钥库,也可以通过cURL发送这种类型的文件吗?您最终找到解决方案了吗?我也遇到了同样的问题,如果您能分享解决方案,我将不胜感激(