Sample code for 30+ languages & platforms
Objective-C

HTTP request for a SOAP web service using WS-Security 1.0 with a digital certificate for authentication

See more HTTP Examples

Demonstrates how to build and send an HTTP request for a SOAP web service using WS-Security 1.0 with a digital certificate for authentication.

Chilkat Objective-C Downloads

Objective-C
#import <CkoCert.h>
#import <CkoXml.h>
#import <CkoXmlDSigGen.h>
#import <CkoStringBuilder.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>

BOOL success = NO;

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// See HTTP request for a SOAP web service using WS-Security 1.0 with a digital certificate for authentication

// ------------------------------------
// Step 1: Load the signing certificate
// ------------------------------------
CkoCert *cert = [[CkoCert alloc] init];
success = [cert LoadFromSmartcard: @""];
if (success == NO) {
    NSLog(@"%@",cert.LastErrorText);
    return;
}

// ---------------------------------------
// Step 2: Build the SOAP XML to be signed
// ---------------------------------------

CkoXml *xml = [[CkoXml alloc] init];
xml.Tag = @"SOAP-ENV:Envelope";
[xml AddAttribute: @"xmlns:SOAP-ENV" value: @"http://schemas.xmlsoap.org/soap/envelope/"];
[xml AddAttribute: @"xmlns:web" value: @"http://www.example.com/webservice/"];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security" autoCreate: YES attrName: @"xmlns:ds" attrValue: @"http://www.w3.org/2000/09/xmldsig#"];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security" autoCreate: YES attrName: @"xmlns:wsse" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security" autoCreate: YES attrName: @"xmlns:wsu" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security" autoCreate: YES attrName: @"xmlns:xenc" attrValue: @"http://www.w3.org/2001/04/xmlenc#"];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security" autoCreate: YES attrName: @"SOAP-ENV:mustUnderstand" attrValue: @"1"];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken" autoCreate: YES attrName: @"A1" attrValue: @""];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken" autoCreate: YES attrName: @"EncodingType" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken" autoCreate: YES attrName: @"ValueType" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509"];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken" autoCreate: YES attrName: @"wsu:Id" attrValue: @"x509cert00"];
[xml UpdateChildContent: @"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken" value: [cert GetEncoded]];
[xml UpdateAttrAt: @"SOAP-ENV:Body" autoCreate: YES attrName: @"xmlns:wsu" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"];
[xml UpdateAttrAt: @"SOAP-ENV:Body" autoCreate: YES attrName: @"wsu:Id" attrValue: @"TheBody"];
[xml UpdateChildContent: @"SOAP-ENV:Body|web:MyRequest|web:Parameter" value: @"MyValue"];

// ---------------------------------------
// Step 3: Sign the XML
// ---------------------------------------

CkoXmlDSigGen *gen = [[CkoXmlDSigGen alloc] init];

gen.SigLocation = @"SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security";
gen.SigLocationMod = [NSNumber numberWithInt:0];
gen.SigNamespacePrefix = @"ds";
gen.SigNamespaceUri = @"http://www.w3.org/2000/09/xmldsig#";
gen.SignedInfoPrefixList = @"ds wsu xenc SOAP-ENV ";
gen.IncNamespacePrefix = @"c14n";
gen.IncNamespaceUri = @"http://www.w3.org/2001/10/xml-exc-c14n#";
gen.SignedInfoCanonAlg = @"EXCL_C14N";
gen.SignedInfoDigestMethod = @"sha1";

// -------- Reference 1 --------
[gen AddSameDocRef: @"TheBody" digestMethod: @"sha1" canonMethod: @"EXCL_C14N" prefixList: @"wsu SOAP-ENV" refType: @""];

[gen SetX509Cert: cert usePrivateKey: YES];

gen.KeyInfoType = @"Custom";

// Create the custom KeyInfo XML..
CkoXml *xmlCustomKeyInfo = [[CkoXml alloc] init];
xmlCustomKeyInfo.Tag = @"wsse:SecurityTokenReference";
xmlCustomKeyInfo.Content = @"5";
[xmlCustomKeyInfo UpdateAttrAt: @"wsse:Reference" autoCreate: YES attrName: @"URI" attrValue: @"#x509cert00"];
[xmlCustomKeyInfo UpdateAttrAt: @"wsse:Reference" autoCreate: YES attrName: @"ValueType" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509"];

xmlCustomKeyInfo.EmitXmlDecl = NO;
gen.CustomKeyInfoXml = [xmlCustomKeyInfo GetXml];

// Load XML to be signed...
CkoStringBuilder *sbXml = [[CkoStringBuilder alloc] init];
xml.EmitXmlDecl = NO;
[xml GetXmlSb: sbXml];

gen.Behaviors = @"IndentedSignature";

// Sign the XML...
success = [gen CreateXmlDSigSb: sbXml];
if (success == NO) {
    NSLog(@"%@",gen.LastErrorText);
    return;
}

// ---------------------------------------------------------
// Step 4: Send the HTTP POST containing the Signed SOAP XML
// ---------------------------------------------------------

CkoHttp *http = [[CkoHttp alloc] init];

[http SetRequestHeader: @"SOAPAction" value: @"\"http://www.example.com/MyWebService\""];
[http SetRequestHeader: @"Host" value: @"www.example.com"];
[http SetRequestHeader: @"Content-Type" value: @"text/xml; charset=utf-8"];

CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
success = [http HttpSb: @"POST" url: @"https://example.com/MyWebService" sb: sbXml charset: @"utf-8" contentType: @"text/xml; charset=utf-8" response: resp];
if (success == NO) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

NSLog(@"%d",[resp.StatusCode intValue]);
NSLog(@"%@",resp.BodyStr);

NSLog(@"%@",@"Finished.");