利用WSE 加密SOAP報文(6
2024-07-21 02:21:39
供稿:網友
加密對外發送的報文
我已經修改了前面的getxmldocument方法,讓它可以使用由wse實現的基于x.509非對稱加密技術。加密回應報文,findcertificatebysubjectstring方法可以用來接收客戶端證書的公開備份,一個來自本地機器賬號的個人儲存室給的客戶端證書。這個證書然后被用來創建一個新的 x.509安全token,這個token將被加入到響應報文的soapcontext的安全token集合里。另外,在對稱加密例子中引用的命名空間,你應該再加一個using指示附來引用一個microsoft.webservices.security.x509命名空間。getxmldocument方法代碼如下:
//創建一個用于返回的簡單xml文檔
xmldocument mydoc = new xmldocument();
mydoc.innerxml =
"<encryptedresponse>this is sensitive data.</encryptedresponse>";
"<encryptedresponse>這里是敏感數據.</encryptedresponse>";
//得到響應報文的soapcontext
soapcontext mycontext = httpsoapcontext.responsecontext;
//打開并讀取本地機器帳號的個人證書儲存室
x509certificatestore mystore =
x509certificatestore.localmachinestore(
x509certificatestore.mystore);
mystore.openread();
//查找所有名為”我的證書”的證書,然后將所有匹配的證書添加到證書集合中
x509certificatecollection mycerts =
mystore.findcertificatebysubjectstring("my certificate");
x509certificate mycert = null;
//查找在集合中中的第一個證書
if (mycerts.count > 0)
{
mycert = mycerts[0];
}
//確定我們有一個可以用于加密的證書
if (mycert == null || !mycert.supportsdataencryption)
{
throw new applicationexception("service is not able to
encrypt the response");
return null;
}
else
{
//使用有效的證書來創建一個安全token
x509securitytoken mytoken = new x509securitytoken(mycert);
//wse將使用這個標記來加密報文正文的
//wse產生一個keyinfo元素,用來請求客戶端上曾用于給報文解密的證書
encrypteddata myencdata = new encrypteddata(mytoken);
//將已加密數據元素添加到響應報文的soapcontext上
mycontext.security.elements.add(myencdata);
return mydoc;
}
基于前面的方法,wse管道產生了下面的有相應security頭、密文和密鑰信息的元素:
<?xml version="1.0" encoding="utf-8"?>
<soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xmlns:xsd="http://www.w3.org/2001/xmlschema">
<soap:header>
<wsu:timestamp
xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility">
<wsu:created>2003-02-11t01:34:01z</wsu:created>
<wsu:expires>2003-02-11t01:39:01z</wsu:expires>
</wsu:timestamp>
<wsse:security soap:mustunderstand="1"
xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">
<xenc:encryptedkey
type="http://www.w3.org/2001/04/xmlenc#encryptedkey"
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:encryptionmethod
algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
<keyinfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<wsse:securitytokenreference>
<wsse:keyidentifier valuetype="wsse:x509v3">
ymlkvwxyd8vuguyliuiydeaqqpw=
</wsse:keyidentifier>
</wsse:securitytokenreference>
</keyinfo>
<xenc:cipherdata>
<xenc:ciphervalue>uj64addf3fd59xsaq=ã‚â…</xenc:ciphervalue>
</xenc:cipherdata>
<xenc:referencelist>
<xenc:datareference uri=
"#encryptedcontent-608eef8b-4104-4469-95b6-7cb4703cfa03" />
</xenc:referencelist>
</xenc:encryptedkey>
</wsse:security>
</soap:header>
<soap:body xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility"
wsu:id="id-70179c5b-4975-4932-9ecd-a58feb34b0d3">
<xenc:encrypteddata
id="encryptedcontent-608eef8b-4104-4469-95b6-7cb4703cfa03"
type="http://www.w3.org/2001/04/xmlenc#content"
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:encryptionmethod
algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
<xenc:cipherdata>
<xenc:ciphervalue>
4o1b4befwbju6tzuaygfraax0ugtaykcw2klibuzpjli...z8i2yphn4+w==
</xenc:ciphervalue>
</xenc:cipherdata>
</xenc:encrypteddata>
</soap:body>
</soap:envelope>
注意在這個已加密的報文里面,由非對稱加密過的encryptedkey元素包含了用于給報文正文加密的對稱加密密鑰。referencelist元素引用了報文正文的encrypteddata元素的id屬性。雖然我在我的例子中沒這樣做,標記這個消息以便能讓容器驗證發送者其實是個不錯的想法。關于使用wse來標記報文的詳細信息,看ws-security authentication and digital signatures with web services enhancements