⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wsse2api.c

📁 一款开源的soap库
💻 C
📖 第 1 页 / 共 5 页
字号:
/*wsse2api.cWS-Security plugingSOAP XML Web services toolsCopyright (C) 2000-2005, Robert van Engelen, Genivia Inc., All Rights Reserved.This part of the software is released under one of the following licenses:GPL, the gSOAP public license, or Genivia's license for commercial use.--------------------------------------------------------------------------------gSOAP public license.The contents of this file are subject to the gSOAP Public License Version 1.3(the "License"); you may not use this file except in compliance with theLicense. You may obtain a copy of the License athttp://www.cs.fsu.edu/~engelen/soaplicense.htmlSoftware distributed under the License is distributed on an "AS IS" basis,WITHOUT WARRANTY OF ANY KIND, either express or implied. See the Licensefor the specific language governing rights and limitations under the License.The Initial Developer of the Original Code is Robert A. van Engelen.Copyright (C) 2000-2005, Robert van Engelen, Genivia Inc., All Rights Reserved.--------------------------------------------------------------------------------GPL license.This program is free software; you can redistribute it and/or modify it underthe terms of the GNU General Public License as published by the Free SoftwareFoundation; either version 2 of the License, or (at your option) any laterversion.This program is distributed in the hope that it will be useful, but WITHOUT ANYWARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR APARTICULAR PURPOSE. See the GNU General Public License for more details.You should have received a copy of the GNU General Public License along withthis program; if not, write to the Free Software Foundation, Inc., 59 TemplePlace, Suite 330, Boston, MA 02111-1307 USAAuthor contact information:engelen@genivia.com / engelen@acm.org--------------------------------------------------------------------------------A commercial use license is available from Genivia, Inc., contact@genivia.com--------------------------------------------------------------------------------*//**@mainpage- @ref wsse documents the wsse plugin for WS-Security 1.0 support.- @ref smdevp documents the smdevp engine used by the wsse plugin.*//**@page wsse The wsse plugin@section wsse_5 Security HeaderThe material in this section relates to the WS-Security specification section 5.To use the wsse plugin:-# Run wsdl2h -t typemap.dat on a WSDL of a service that requires WS-Security   headers. The typemap.dat file is used to recognize and translate Security   header blocks.-# Run soapcpp2 on the header file produced by wsdl2h.-# (Re-)compile stdsoap2.c/pp, dom.c/pp, smdevp.c, wsseapi.c and the generated   source files with the -DWITH_DOM and -DWITH_OPENSSL compile flags set. The   smdevp.c and wssapi.c files are located in the 'plugin' directory.-# Use the wsse plugin API functions described below to add and verify   Security headers.An example wsse client/server application can be found in samples/wsse.The Security header block was generated from the WS-Security schema with thewsdl2h tool and WS/WS-typemap.dat:@code    $ wsdl2h -cegxy -o wsse.h -t WS/WS-typemap.dat WS/wsse.xsd@endcodeThe same process was used to generate the header file ds.h from the XML digitalsignatures core schema.The import/wsse.h file has the following definition for the Security headerblock:@codetypedef struct _wsse2__Security{       struct _wsu__Timestamp*                 wsu__Timestamp;        struct _wsse2__UsernameToken*            UsernameToken;        struct _wsse2__BinarySecurityToken*      BinarySecurityToken;        struct ds__SignatureType*               ds__Signature;        @char*                                  SOAP_ENV__actor;        @char*                                  SOAP_ENV__role;} _wsse2__Security;@endcodeTo add an empty Security header block to the SOAP header, use:@code    soap_wsse_add_Security(soap);@endcodeTo delete a Security header, use:@code    soap_wsse_delete_Security(soap);@endcodeAdding an empty Security header block is not very useful. We will mainly makeuse of the higher-level functions of the wsse plugin to populate and verifySecurity header content.Note: The soap context includes an actor value soap.actor that is populated andrendered as the SOAP-ENV:actor (SOAP 1.1) or SOAP-ENV:role (SOAP 1.2) attributein XML within the generic SOAP Header. The attribute is optional, but should beused to target a recipient such as an intermediate node to process the SOAPheader.  In contrast, actor or role attributes within Security header blockstarget specific recipients to process the Security header block. The gSOAPimplementation does not automate this feature and application should set andcheck the actor/role attribute when necessary. In addition, the currentimplementation supports the inclusion of a single Security header block in theSOAP header.To populate the SOAP-ENV:actor or SOAP-ENV:role attribute within the Securityheader, use:@code    soap_wsse_add_Security_actor(soap, "recipient");@endcodeTo obtain the actor or role value (e.g. after receiving a message), use:@code    _wsse2__Security *security = soap_wsse_Security(soap);    if (security)    { ... = security->SOAP_ENV__actor; // SOAP 1.1      ... = security->SOAP_ENV__role;  // SOAP 1.2@endcodeThe SOAP-ENV:mustUnderstand attribute is automatically added and checked by thegSOAP engine. A gSOAP application compiled without Security support will rejectSecurity headers.Security header blocks are attached to the soap context, which means that theinformation will be automatically kept to support multiple invocations.@section wsse_6 Security TokensThe material in this section relates to the WS-Security specification section 6.@subsection wsse_6_2 User Name TokenTo add a user name token to the Security header block, use:@code    soap_wsse_add_UsernameTokenText(soap, "Id", "username", NULL);@endcodeThe Id value is optional. When non-NULL the user name token is included in thedigital signature to protect its integrity. It is common for the wsse pluginfunctions to accept such Ids, which are serialized as wsu:Id identifiers forcross-referencing XML elements. The signature engine of the wsse plugin isdesigned to automatically sign all wsu:Id attributed elements to simplify thecode you need to write to implement the signing process.To add a user name token with clear text password, use:@code    soap_wsse_add_UsernameTokenText(soap, "Id", "username", "password");@endcodeIt is strongly recommended to use @ref soap_wsse_add_UsernameTokenText only incombination with HTTPS encrypted transmission or not at all. A betteralternative is to use password digests. With password digest authentication,the digest value of a password (with message creation time and a random nonce)is compared on both sides, thus eliminating the need to exchange a passwordover the wire.To add a user name token with password digest, use:@code    soap_wsse_add_UsernameTokenDigest(soap, "Id", "username", "password");@endcodeAlthough the password string is passed to this function, it is not rendered inXML or stored in a message log. It has been argued that this approach adoptedby the WS-Security protocol is still vulnerable since the application retrievesthe password in text form requiring a database to store passwords in cleartext. However, a digest algorithm can be used to hash the passwords and storetheir digests instead, which eliminates the need to store clear-text passwords.Note that this is a common approach adopted by Unix for decades.By setting the Id value to a unique string, the user name token is alsodigitally signed by the signature engine further preventing tampering with itsvalue.You must use @ref soap_wsse_add_UsernameTokenDigest for each message exchangeto refresh the password digest even when the user name and password are notchanged. Otherwise, the receiver might flag the message as a replay attack.Clear-text passwords and password digests are verified with@ref soap_wsse_verify_Password. To verify a password at the receiving side toauthorize a request (e.g. within a Web service operation), use:@code    int ns__myMethod(struct soap *soap, ...)    { const char *username = soap_wsse_get_Username(soap);      const char *password;      if (!username)        return soap->error; // no username: return FailedAuthentication      password = ...; // lookup password of username      if (soap_wsse_verify_Password(soap, password))        return soap->error; // password verification failed: return FailedAuthentication      ... // process request      return SOAP_OK;    }@endcodeNote that the @ref soap_wsse_get_Username functions sets thewsse:FailedAuthentication fault. It is common for the wsse plugin functions toreturn SOAP_OK or a wsse fault that should be passed to the sender by returningsoap->error from service operations. The fault is displayed with the @refsoap_print_fault function.Password digest authentication prevents message replay attacks. The wsse pluginkeeps a database of password digests to thwart replay attacks. This is theonly part in the plugin code that requires mutex provided by threads.h.  Ofcourse, this only works correctly if the server is persistent, such as astand-alone service. Note that CGI-based services do not keep state. Machineclocks must be synchronized and clock skew should not exceed @refSOAP_WSSE_CLKSKEW at the server side.@subsection wsse_6_3 Binary Security TokensX509 certificates are commonly included in Security header blocks as binarysecurity tokens. A certificate is used to verify the digital signature of adigitally signed message using the public key embedded within the certificate.The certificate itself is signed by a certificate authority (CA) that vouchesfor the authenticity of the certificate, i.e. to prove the identify of themessage originator. This verification process is important, because digitalsignatures are useless without verification: an attacker could simply replacethe message, sign it, and replace the certificate.Certificates are automatically verified by the wsse plugin signature enginewhen received and accessed, which means that the certificates of the CAs mustbe made accessible to the wsse plugin as follows:@code    soap->cafile = "cacerts.pem";  // use this    soap->capath = "dir/to/certs"; // and/or point to CA certs    soap->crlfile = "revoked.pem"; // use CRL (optional)@endcodeThe @ref soap_wsse_verify_X509 function checks the validity of a certificate.The check is automatically performed. The check is also performed whenretrieving the certificate from a Security header block, either automaticallyby the wsse plugin's signature verification engine or manually as follows:@code    X509 *cert = soap_wsse_get_BinarySecurityTokenX509(soap, "Id");@endcodewhere Id is the identification string of the binary security token or NULL.The verification is an expensive process that will be optimized in futurereleases by caching the certificate chain.To attach a binary security token stored in a PEM file to a Security headerblock for transmission, use:@code    soap_wsse_add_BinarySecurityTokenPEM(soap, NULL, "mycert.pem")@endcodeA binary security token can be automatically signed by setting its Idattribute:@code    soap_wsse_add_BinarySecurityTokenPEM(soap, "X509Token", "mycert.pem")@endcodeRepeatedly loading a certificate from a PEM file is inefficient. To reuse acertificate loaded from a PEM file for multiple invocations, use:@code    FILE *fd = fopen("mycert.pem", "r");    X509 *cert = PEM_read_X509(fd, NULL, NULL, NULL);    fclose(fd);    if (soap_wsse_add_BinarySecurityTokenX509(soap, "X509Token", cert))      ... // an error occurred@endcodeOther types of binary security tokens can be added to the Security header block using:@code    soap_wsse_add_BinarySecurityToken(soap, "Id", "valueType", data, datalen);@endcode@section wsse_6_4 XML TokensThe use and processing rules for XML tokens such as SAML assertions is specificto an application.  The wsse plugin does not automate the use of XML tokens.The developer is encouraged to generate code for the SAML schema with wsdl2hand add the necessary assertions to the Security header block:@codetypedef struct _wsse2__Security{       struct _wsu__Timestamp*                 wsu__Timestamp;        struct _wsse2__UsernameToken*            UsernameToken;        struct _wsse2__BinarySecurityToken*      BinarySecurityToken;        struct _saml__Assertion*		saml__Assertion; // added        struct ds__SignatureType*               ds__Signature;        @char*                                  SOAP_ENV__actor;        @char*                                  SOAP_ENV__role;} _wsse2__Security;@endcode

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -