php - 检查 WS* 服务器实现中的签名

标签 php web-services soap wsse

我想在用 php 实现的 soap 服务器上验证 soap 请求的签名。

服务器代码:

$Server = new SoapServer();

$d = new DOMDocument();
$d->load('php://input');

$s = new WSSESoapServer($d);
try {
    if($s->process()) {
        // Valid signature
        $Server->handle($s->saveXML());
    } else {
        throw new Exception('Invalid signature');
    }
} catch (Exception $e) {
    echo "server exception: " . $e;
}

错误:

exception 'Exception' with message 'Error loading key to handle Signature' in /<path>/wse/src/WSSESoapServer.php:146

我已经实现了一个客户端来使用这个库签署 SOAP 请求:https://github.com/robrichards/wse-php .没有实例说明如何实现服务器...

如何加载公钥来校验签名?

[编辑] 我现在可以使用

加载提供的 key
    $key = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, array('type' => 'public'));
    $key->loadKey(CERT, true);

我在验证签名时不再收到错误消息:

$x = new XMLSecurityDSig();
$d = $x->locateSignature($soapDoc);
$valid = $x->verify($key);

但是,$valid 始终为 false。我不知道是因为 key 加载错误还是实际上无效。我找不到关于使用 PHP 实现 SOAP 服务器的信息,也找不到关于实现依赖于检查签名请求的 SOAP 服务器的信息。

澄清

  1. 我的客户与远程网络服务对话并获得确认。

  2. 然后远程服务器需要一些时间来处理我的请求。

  3. 一个远程客户端(我无法控制)然后向 我的服务。

最后一步是我无法验证签名的地方

最佳答案

无论如何,我认为您的第一种方法很好,我的服务器具有相同的结构。不幸的是,WSSESoapServer 没有继承自 SoapServer,因此不是真正的 SoapServer,而是一个SoapSignatureValidator,应该这样称呼。很容易纠正这种行为,不需要单独的 SoapServer 实例(应该是透明的和自动的)。

<?php
require 'soap-server-wsse.php';

try {
    // get soap message
    $xmlSoap = DOMDocument::load('php://input');

    // validate signature
    $validateSignature = new WSSESoapServer($xmlSoap);
    if(!$validateSignature->process())
        file_put_contents("log.txt", "soapserver: SIGNATURE VALIDATION ERROR - CONTINUING WITHOUT SIGNATURE\n", FILE_APPEND);
        //throw new Exception('Invalid Signature'); # this would cancel the execution and not send an answer

    $sServer = new SoapServer($wsdl);
    // actually process the soap message and create & send answer
    //$sServer->setClass() or setObject() or set setFunction()
    $sServer->handle($validateSignature->saveXML());
} catch (Exception $fault) {
    file_put_contents("log.txt", "soapserver soapfault: ".print_r($fault, true), FILE_APPEND);
}
?>

关于php - 检查 WS* 服务器实现中的签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33272115/

相关文章:

php - 像 stackoverflow 一样打开 id 或单点登录

java - 动态加载 Jar 并实例化已加载类的对象

java - WSO2 BPS/SOAP - 如何使用 HumanTaskClientAPI 获取人工任务的结果?

c# - FedEx express 服务

javascript - 通过 HTTPS 发出 SOAP 请求 - Javascript, Phonegap

php - 如何根据时间在 td 中定位 div 看起来像谷歌日历

php - Python 3.6 Bittrex API 无效签名

PHP 语句 : while($row = mysql_fetch_array($query))

java - 如何将 jax-ws 服务部署到 eclipse 或 tomcat?

c# - 如何在不重新编译的情况下在 .NET 中动态切换 Web 服务地址?