6

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

服务器代码:

$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 = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, array('type' => 'public'));
    $key->loadKey(CERT, true);

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

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

然而,$valid 总是假的。我不知道是因为密钥加载错误还是实际上无效。我几乎找不到关于使用 PHP 实现 SOAP 服务器的信息,也找不到关于实现依赖于检查签名请求的 SOAP 服务器的信息。

澄清

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

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

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

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

4

1 回答 1

0

无论如何,您的第一种方法对我来说看起来不错,我的服务器具有相同的结构。不幸的是,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);
}
?>
于 2015-11-04T11:49:19.527 回答