Unicode C
Unicode C
eHealth.gov.be RequestSecurityToken
Request a security token for use with the eHealth.gov.be SOAP web services.Chilkat Unicode C Downloads
#include <C_CkCertW.h>
#include <C_CkXmlW.h>
#include <C_CkBinDataW.h>
#include <C_CkDateTimeW.h>
#include <C_CkXmlDSigGenW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
void ChilkatSample(void)
{
BOOL success;
HCkCertW cert;
HCkXmlW xmlToSign;
HCkBinDataW bdCert;
HCkDateTimeW dt;
HCkXmlDSigGenW gen;
HCkXmlW xmlCustomKeyInfo;
HCkStringBuilderW sbXml;
HCkHttpW http;
const wchar_t *url;
HCkHttpResponseW resp;
int responseStatus;
HCkBinDataW bdSecToken;
success = FALSE;
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Provide a certificate + private key.
// Note: If your certificate + private key is located on a hardware token or smartcard, you can call a different function to load from smartcard..
cert = CkCertW_Create();
success = CkCertW_LoadPfxFile(cert,L"SSIN=12345678.acc.p12",L"p12_password");
if (success == FALSE) {
wprintf(L"%s\n",CkCertW_lastErrorText(cert));
CkCertW_Dispose(cert);
return;
}
// Create the following XML to be signed..
// <?xml version="1.0" encoding="UTF-8"?>
// <soapenv:Envelope xmlns:ns="http://docs.oasis-open.org/ws-sx/ws-trust/200512" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
// <soapenv:Header>
// <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
// xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
// <wsse:BinarySecurityToken EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"
// ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"
// wsu:Id="X509-4A13D668E59AAC4F3816750824965588">{organization certificate}</wsse:BinarySecurityToken>
// <wsu:Timestamp wsu:Id="TS-4A13D668E59AAC4F3816750824965567">
// <wsu:Created>2023-02-01T12:42:11.156Z</wsu:Created>
// <wsu:Expires>2023-02-01T12:58:51.156Z</wsu:Expires>
// </wsu:Timestamp>
// </wsse:Security>
// </soapenv:Header>
// <soapenv:Body wsu:Id="id-4A13D668E59AAC4F38167508249655911" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
// <wst:RequestSecurityToken Context="RC-302613de-a809-46b5-931a-0a55bfca5937"
// xmlns:auth="http://docs.oasis-open.org/wsfed/authorization/200706"
// xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
// xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
// xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
// xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
// xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512">
// <wst:TokenType>http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1</wst:TokenType>
// <wst:RequestType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue</wst:RequestType>
// <wst:Claims Dialect="http://docs.oasis-open.org/wsfed/authorization/200706/authclaims">
// <auth:ClaimType Uri="urn:be:fgov:kbo-bce:organization:cbe-number">
// <auth:Value>{cbenumber}</auth:Value>
// </auth:ClaimType>
// <auth:ClaimType Uri="urn:be:fgov:ehealth:1.0:certificateholder:enterprise:cbe-number">
// <auth:Value>{cbenumber}</auth:Value>
// </auth:ClaimType>
// </wst:Claims>
// <wst:Lifetime>
// <wsu:Created>2023-02-01T08:30:10+02:00</wsu:Created>
// <wsu:Expires>2023-02-01T09:30:10+02:00</wsu:Expires>
// </wst:Lifetime>
// <wst:KeyType>http://docs.oasis-open.org/ws-sx/wstrust/200512/PublicKey</wst:KeyType>
// </wst:RequestSecurityToken>
// </soapenv:Body>
// </soapenv:Envelope>
xmlToSign = CkXmlW_Create();
CkXmlW_putTag(xmlToSign,L"soapenv:Envelope");
CkXmlW_AddAttribute(xmlToSign,L"xmlns:ns",L"http://docs.oasis-open.org/ws-sx/ws-trust/200512");
CkXmlW_AddAttribute(xmlToSign,L"xmlns:soapenv",L"http://schemas.xmlsoap.org/soap/envelope/");
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Header|wsse:Security",TRUE,L"xmlns:wsse",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Header|wsse:Security",TRUE,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"EncodingType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"ValueType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"wsu:Id",L"X509-4A13D668E59AAC4F3816750824965588");
bdCert = CkBinDataW_Create();
CkCertW_ExportCertDerBd(cert,bdCert);
CkXmlW_UpdateChildContent(xmlToSign,L"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",CkBinDataW_getEncoded(bdCert,L"base64"));
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Header|wsse:Security|wsu:Timestamp",TRUE,L"wsu:Id",L"TS-4A13D668E59AAC4F3816750824965567");
dt = CkDateTimeW_Create();
CkDateTimeW_SetFromCurrentSystemTime(dt);
CkXmlW_UpdateChildContent(xmlToSign,L"soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created",CkDateTimeW_getAsTimestamp(dt,FALSE));
CkDateTimeW_AddSeconds(dt,300);
CkXmlW_UpdateChildContent(xmlToSign,L"soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires",CkDateTimeW_getAsTimestamp(dt,FALSE));
CkDateTimeW_AddSeconds(dt,-300);
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Body",TRUE,L"wsu:Id",L"id-4A13D668E59AAC4F38167508249655911");
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Body",TRUE,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Body|wst:RequestSecurityToken",TRUE,L"Context",L"RC-302613de-a809-46b5-931a-0a55bfca5937");
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Body|wst:RequestSecurityToken",TRUE,L"xmlns:auth",L"http://docs.oasis-open.org/wsfed/authorization/200706");
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Body|wst:RequestSecurityToken",TRUE,L"xmlns:ds",L"http://www.w3.org/2000/09/xmldsig#");
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Body|wst:RequestSecurityToken",TRUE,L"xmlns:wsa",L"http://schemas.xmlsoap.org/ws/2004/08/addressing");
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Body|wst:RequestSecurityToken",TRUE,L"xmlns:wsp",L"http://schemas.xmlsoap.org/ws/2004/09/policy");
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Body|wst:RequestSecurityToken",TRUE,L"xmlns:wsse",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Body|wst:RequestSecurityToken",TRUE,L"xmlns:wst",L"http://docs.oasis-open.org/ws-sx/ws-trust/200512");
CkXmlW_UpdateChildContent(xmlToSign,L"soapenv:Body|wst:RequestSecurityToken|wst:TokenType",L"http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1");
CkXmlW_UpdateChildContent(xmlToSign,L"soapenv:Body|wst:RequestSecurityToken|wst:RequestType",L"http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue");
success = CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Body|wst:RequestSecurityToken|wst:Claims",TRUE,L"Dialect",L"http://docs.oasis-open.org/wsfed/authorization/200706/authclaims");
success = CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Body|wst:RequestSecurityToken|wst:Claims|auth:ClaimType[1]",TRUE,L"Uri",L"urn:be:fgov:ehealth:1.0:certificateholder:person:ssin");
CkXmlW_UpdateChildContent(xmlToSign,L"soapenv:Body|wst:RequestSecurityToken|wst:KeyType",L"http://docs.oasis-open.org/ws-sx/wstrust/200512/PublicKey");
gen = CkXmlDSigGenW_Create();
CkXmlDSigGenW_putSigLocation(gen,L"soapenv:Envelope|soapenv:Header|wsse:Security|wsse:BinarySecurityToken");
CkXmlDSigGenW_putSigLocationMod(gen,1);
CkXmlDSigGenW_putSigId(gen,L"SIG-4A13D668E59AAC4F38167508249656212");
CkXmlDSigGenW_putSigNamespacePrefix(gen,L"ds");
CkXmlDSigGenW_putSigNamespaceUri(gen,L"http://www.w3.org/2000/09/xmldsig#");
CkXmlDSigGenW_putSignedInfoPrefixList(gen,L"soapenv urn urn1");
CkXmlDSigGenW_putIncNamespacePrefix(gen,L"ec");
CkXmlDSigGenW_putIncNamespaceUri(gen,L"http://www.w3.org/2001/10/xml-exc-c14n#");
CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"EXCL_C14N");
CkXmlDSigGenW_putSignedInfoDigestMethod(gen,L"sha256");
// Set the KeyInfoId before adding references..
CkXmlDSigGenW_putKeyInfoId(gen,L"KI-4A13D668E59AAC4F3816750824965589");
// -------- Reference 1 --------
CkXmlDSigGenW_AddSameDocRef(gen,L"TS-4A13D668E59AAC4F3816750824965567",L"sha256",L"EXCL_C14N",L"wsse soapenv urn urn1",L"");
// -------- Reference 2 --------
CkXmlDSigGenW_AddSameDocRef(gen,L"id-4A13D668E59AAC4F38167508249655911",L"sha256",L"EXCL_C14N",L"urn urn1",L"");
// -------- Reference 3 --------
CkXmlDSigGenW_AddSameDocRef(gen,L"X509-4A13D668E59AAC4F3816750824965588",L"sha256",L"EXCL_C14N",L"_EMPTY_",L"");
CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE);
CkXmlDSigGenW_putKeyInfoType(gen,L"Custom");
// Create the custom KeyInfo XML..
xmlCustomKeyInfo = CkXmlW_Create();
CkXmlW_putTag(xmlCustomKeyInfo,L"wsse:SecurityTokenReference");
CkXmlW_AddAttribute(xmlCustomKeyInfo,L"wsu:Id",L"STR-4A13D668E59AAC4F38167508249655810");
CkXmlW_UpdateAttrAt(xmlCustomKeyInfo,L"wsse:Reference",TRUE,L"URI",L"#X509-4A13D668E59AAC4F3816750824965588");
CkXmlW_UpdateAttrAt(xmlCustomKeyInfo,L"wsse:Reference",TRUE,L"ValueType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
CkXmlW_putEmitXmlDecl(xmlCustomKeyInfo,FALSE);
CkXmlDSigGenW_putCustomKeyInfoXml(gen,CkXmlW_getXml(xmlCustomKeyInfo));
// Load XML to be signed...
sbXml = CkStringBuilderW_Create();
CkXmlW_GetXmlSb(xmlToSign,sbXml);
CkXmlDSigGenW_putBehaviors(gen,L"IndentedSignature");
// Sign the XML...
success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml);
if (success == FALSE) {
wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
CkCertW_Dispose(cert);
CkXmlW_Dispose(xmlToSign);
CkBinDataW_Dispose(bdCert);
CkDateTimeW_Dispose(dt);
CkXmlDSigGenW_Dispose(gen);
CkXmlW_Dispose(xmlCustomKeyInfo);
CkStringBuilderW_Dispose(sbXml);
return;
}
// The sbXml is sent as the HTTP request body below..
wprintf(L"%s\n",CkStringBuilderW_getAsString(sbXml));
// -----------------------------------------------------------------------------------------------------------
// Send the SOAP requet to ask the server to issue a security token, which can then be used to access other SOAP services..
http = CkHttpW_Create();
success = CkHttpW_SetSslClientCert(http,cert);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkCertW_Dispose(cert);
CkXmlW_Dispose(xmlToSign);
CkBinDataW_Dispose(bdCert);
CkDateTimeW_Dispose(dt);
CkXmlDSigGenW_Dispose(gen);
CkXmlW_Dispose(xmlCustomKeyInfo);
CkStringBuilderW_Dispose(sbXml);
CkHttpW_Dispose(http);
return;
}
CkHttpW_SetRequestHeader(http,L"Content-Type",L"text/xml");
url = L"https://services-acpt.ehealth.fgov.be/IAM/SecurityTokenService/v1/RequestSecurityToken";
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpSb(http,L"POST",url,sbXml,L"utf-8",L"application/xml",resp);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkCertW_Dispose(cert);
CkXmlW_Dispose(xmlToSign);
CkBinDataW_Dispose(bdCert);
CkDateTimeW_Dispose(dt);
CkXmlDSigGenW_Dispose(gen);
CkXmlW_Dispose(xmlCustomKeyInfo);
CkStringBuilderW_Dispose(sbXml);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
return;
}
responseStatus = CkHttpResponseW_getStatusCode(resp);
wprintf(L"Response Status Code = %d\n",responseStatus);
// You'll want to check to see if the response status code = 200.
// If not, then the response body contains error information instead of a security token.
// This example will assume we received 200 status code.
wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
// The response body contains XML like this:
// <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
// <SOAP-ENV:Header/>
// <SOAP-ENV:Body>
// <wst:RequestSecurityTokenResponse xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512" Context="RC-302613de-a809-46b5-931a-0a55bfca5937">
// <wst:RequestedSecurityToken>
// <Assertion xmlns="urn:oasis:names:tc:SAML:1.0:assertion" AssertionID="_3e8ea5c951b2167c274974750ace9b5d"
// ...
// </Assertion>
// </wst:RequestedSecurityToken>
// </wst:RequestSecurityTokenResponse>
// </SOAP-ENV:Body>
// </SOAP-ENV:Envelope>
// The portion of the response from <Assertion ..> ... </Assertion> is the SAML security token to be included
// in a subsesquent SOAP request. It is extremely important to not modify the contents of the security token in any way, including not changing
// whitespace or any formatting. Therefore, we get the response body exactly as-is, to be used in a SOAP request.
// Copy the response body to a Chilkat BinData object.
bdSecToken = CkBinDataW_Create();
CkHttpResponseW_GetBodyBd(resp,bdSecToken);
// Let's save the bdSecToken to a file, and pick it up in the next example where it is used
// in a SOAP request, such as in this example: AddressBook Search for Professionals
CkBinDataW_WriteFile(bdSecToken,L"qa_data/tokens/ehealth-fgov-be-sectoken.xml");
wprintf(L"OK\n");
CkCertW_Dispose(cert);
CkXmlW_Dispose(xmlToSign);
CkBinDataW_Dispose(bdCert);
CkDateTimeW_Dispose(dt);
CkXmlDSigGenW_Dispose(gen);
CkXmlW_Dispose(xmlCustomKeyInfo);
CkStringBuilderW_Dispose(sbXml);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkBinDataW_Dispose(bdSecToken);
}