Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

PowerBuilder Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(PowerBuilder) Decrypt a SAML Response

Demonstrates how to decrypt a SAML response.

Note: This example requires Chilkat v9.5.0.76 or greater.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

integer li_rc
oleobject loo_Http
oleobject loo_SbSamlResponse
oleobject loo_SbPrivateKeyPem
integer li_Success
oleobject loo_Xml
oleobject loo_Privkey
oleobject loo_Rsa
string ls_EncryptedAesKey
oleobject loo_BdAesKey
oleobject loo_SbRsaAlg
string ls_Encrypted64
oleobject loo_BdEncrypted
oleobject loo_Crypt
oleobject loo_SbAlg
string ls_DecryptedXml
oleobject loo_XmlAssertion
oleobject loo_XmlEncryptedAssertion

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

//  This example decrypts this SAML response:

//  <?xml version="1.0" encoding="UTF-8" ?>
//  <saml2p:Response Destination="https://deskflow-asp2.com/ubc/ubcdfe.dll/cwlacs" ID="_e4585eaeedbcaf7c24dff7f1ee2499f5" IssueInstant="2018-10-11T17:46:20.727Z" Version="2.0" xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol">
//      <saml2:Issuer xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">https://authentication.stg.id.ubc.ca</saml2:Issuer>
//      <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
//          <ds:SignedInfo>
//              <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
//              <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
//              <ds:Reference URI="#_e4585eaeedbcaf7c24dff7f1ee2499f5">
//                  <ds:Transforms>
//                      <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
//                      <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
//                  </ds:Transforms>
//                  <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
//                  <ds:DigestValue>1ui22tqFyYEOoWI19CMwz4n+ynxNjLDGdTeRMdi60EU=</ds:DigestValue>
//              </ds:Reference>
//          </ds:SignedInfo>
//          <ds:SignatureValue>ROg7FXV6vsp8socVhdo76/i7cRHGGKIveAiScKdujZT0QrHVqIvvbZ/RnwvEMJ9H9i/kJFAQA171
//  		Eo2kDjSdvNFQ/YcKaJUwMtAwT05yVatGV42RZKEf7ME+vpcCTR1LWZdrhat1FWCg1MNQwNWB0EL5
//  		fEP2a4jAcSTB8tFbjTAHsv7IWC39E5RVv99mACYXLa7iGZLtORANZxgYu5qQgmH6pUkI6Z1cpmf+
//  		m9mIjKM6LF0EvLfWOBWL6udZ+GsHPOLjVTJg+1S0xb9FQCYDVW1QhbjSS0icKHKTNNbrsaxllVDY
//  		m4q27YQjRh+XxugPgvsZ61Pxlto8Jbg+6jUlMQ==</ds:SignatureValue>
//          <ds:KeyInfo>
//              <ds:X509Data>
//                  <ds:X509Certificate>MIIDTTCCAjWgAwIBAgIVAJccYyIV6wly8XyddumpgnHMJ2JLMA0GCSqGSIb3DQEBCwUAMCcxJTAj
//  			BgNVBAMMHGF1dGhlbnRpY2F0aW9uLnN0Zy5pZC51YmMuY2EwHhcNMTcwMzAxMTk1NDM0WhcNMzcw
//  			...
//  			xUuh6HuHKIwQqHBz7udxbH3Zbb6jXGDJjiDHt1LRJ8xbVisFIcDlIwsGQQi0HeEJfx4P</ds:X509Certificate>
//              </ds:X509Data>
//          </ds:KeyInfo>
//      </ds:Signature>
//      <saml2p:Status>
//          <saml2p:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
//      </saml2p:Status>
//      <saml2:EncryptedAssertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">
//          <xenc:EncryptedData Id="_314d80b9cf02d8eda8d686a6ffd626cf" Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
//              <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"/>
//              <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
//                  <xenc:EncryptedKey Id="_d7b6da6fb59a627ebb4a96928441ab79" Recipient="https://ubcdfe.deskflow-asp2.com" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
//                      <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
//                          <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"/>
//                      </xenc:EncryptionMethod>
//                      <ds:KeyInfo>
//                          <ds:X509Data>
//                              <ds:X509Certificate>MIICuzCCAiQCCQD3bpigRnKMSzANBgkqhkiG9w0BAQsFADCBoTELMAkGA1UEBhMCQ0ExEDAOBgNV
//  				BAgMB09udGFyaW8xEDAOBgNVBAcMB1Rvcm9udG8xJjAkBgNVBAoMHVRhY3RpY2FsIEJ1c2luZXNz
//  				...
//  				kVRcHd1UK3q7G8FoykWjdQz/0EoMTfEZ+Md56mLOe48eMUZV2ONZuL1kDCEKw1UwkaDQI4Pf8pzx
//  				82b9rgw9wBDtvu5eFPlUGEGIBw==</ds:X509Certificate>
//                          </ds:X509Data>
//                      </ds:KeyInfo>
//                      <xenc:CipherData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
//                          <xenc:CipherValue>BNHfUOpgdPE5BgpN2VIZIDthMAv1rxk91qVnWyCZOG9bmUKChJtTUqMpndot7VJwYuyKFshkAdnT
//  				D79KGdlSA1xHKcVeZXXzDWglqSyYjzhDCsyOhPaI4HelMFgCLwyFz89uEpUpqlvfl8ol3Am/XnzQ
//  				Vp7V7oS76hocjUI51Qs=</xenc:CipherValue>
//                      </xenc:CipherData>
//                  </xenc:EncryptedKey>
//              </ds:KeyInfo>
//              <xenc:CipherData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
//                  <xenc:CipherValue>R6l7tmbnXrOfBgB8lA3KnwLYsLH5ZO5omQ7Hp5K05atzw2o55xmCXVMYhNneFxMtxUh6raEyHeZX
//  			PTZNgWrvdqc4GYND/R7MhRrJzk9OAq1WyoOXwbtRpwNDwWA4N2IuprPQJbvjVxaw/PesZMZwZqlp
//  			...
//  			zm9zAxahyu8Ooe8M4r3HN2cY0JxxxkZtDiulbnyA+rRtXfBRJtangvFQ4iFAnzM/Yg9hMyW9jcu0
//  			S7FzuRB9ONMxi+nh0IFWgqp+</xenc:CipherValue>
//              </xenc:CipherData>
//          </xenc:EncryptedData>
//      </saml2:EncryptedAssertion>
//  </saml2p:Response>

//  The sample encrypted SAML response and RSA private key are available online:
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat_9_5_0.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_SbSamlResponse = create oleobject
li_rc = loo_SbSamlResponse.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

loo_SbPrivateKeyPem = create oleobject
li_rc = loo_SbPrivateKeyPem.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

li_Success = loo_Http.QuickGetSb("https://chilkatdownload.com/data/samlresponse.xml",loo_SbSamlResponse)
if li_Success = 1 then
    li_Success = loo_Http.QuickGetSb("https://chilkatdownload.com/data/samlresponse_privkey.pem",loo_SbPrivateKeyPem)
end if

if li_Success <> 1 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_SbSamlResponse
    destroy loo_SbPrivateKeyPem
    return
end if

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat_9_5_0.Xml")

loo_Xml.LoadSb(loo_SbSamlResponse,1)

//  Load the RSA private key..
loo_Privkey = create oleobject
li_rc = loo_Privkey.ConnectToNewObject("Chilkat_9_5_0.PrivateKey")

li_Success = loo_Privkey.LoadPem(loo_SbPrivateKeyPem.GetAsString())
if li_Success <> 1 then
    Write-Debug loo_Privkey.LastErrorText
    destroy loo_Http
    destroy loo_SbSamlResponse
    destroy loo_SbPrivateKeyPem
    destroy loo_Xml
    destroy loo_Privkey
    return
end if

//  Prepare an RSA object w/ the private key...
loo_Rsa = create oleobject
li_rc = loo_Rsa.ConnectToNewObject("Chilkat_9_5_0.Rsa")

li_Success = loo_Rsa.ImportPrivateKeyObj(loo_Privkey)
if li_Success <> 1 then
    Write-Debug loo_Rsa.LastErrorText
    destroy loo_Http
    destroy loo_SbSamlResponse
    destroy loo_SbPrivateKeyPem
    destroy loo_Xml
    destroy loo_Privkey
    destroy loo_Rsa
    return
end if

//  RSA will be used to decrypt the xenc:EncryptedKey
//  The bytes to be decrypted are in xenc:CipherValue (in base64 format)
ls_EncryptedAesKey = loo_Xml.GetChildContent("saml2:EncryptedAssertion|xenc:EncryptedData|ds:KeyInfo|xenc:EncryptedKey|xenc:CipherData|xenc:CipherValue")
if loo_Xml.LastMethodSuccess <> 1 then
    Write-Debug "Encrypted AES key not found."
    destroy loo_Http
    destroy loo_SbSamlResponse
    destroy loo_SbPrivateKeyPem
    destroy loo_Xml
    destroy loo_Privkey
    destroy loo_Rsa
    return
end if

Write-Debug "Encrypted AES key (base64) = " + ls_EncryptedAesKey

loo_BdAesKey = create oleobject
li_rc = loo_BdAesKey.ConnectToNewObject("Chilkat_9_5_0.BinData")

loo_BdAesKey.AppendEncoded(ls_EncryptedAesKey,"base64")

loo_SbRsaAlg = create oleobject
li_rc = loo_SbRsaAlg.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

loo_SbRsaAlg.Append(loo_Xml.ChilkatPath("saml2:EncryptedAssertion|xenc:EncryptedData|ds:KeyInfo|xenc:EncryptedKey|xenc:EncryptionMethod|(Algorithm)"))
Write-Debug "sbRsaAlg contains: " + loo_SbRsaAlg.GetAsString()
if loo_SbRsaAlg.Contains("rsa-oaep",1) = 1 then
    loo_Rsa.OaepPadding = 1
end if

//  Note: The DecryptBd method is introduced in Chilkat v9.5.0.76
li_Success = loo_Rsa.DecryptBd(loo_BdAesKey,1)
if li_Success <> 1 then
    Write-Debug loo_Rsa.LastErrorText
    destroy loo_Http
    destroy loo_SbSamlResponse
    destroy loo_SbPrivateKeyPem
    destroy loo_Xml
    destroy loo_Privkey
    destroy loo_Rsa
    destroy loo_BdAesKey
    destroy loo_SbRsaAlg
    return
end if

Write-Debug "Decrypted AES key (hex) = " + loo_BdAesKey.GetEncoded("hex")

//  Get the encrypted XML (in base64) to be decrypted w/ the AES key.
ls_Encrypted64 = loo_Xml.GetChildContent("saml2:EncryptedAssertion|xenc:EncryptedData|xenc:CipherData|xenc:CipherValue")
if loo_Xml.LastMethodSuccess <> 1 then
    Write-Debug "Encrypted data not found."
    destroy loo_Http
    destroy loo_SbSamlResponse
    destroy loo_SbPrivateKeyPem
    destroy loo_Xml
    destroy loo_Privkey
    destroy loo_Rsa
    destroy loo_BdAesKey
    destroy loo_SbRsaAlg
    return
end if

loo_BdEncrypted = create oleobject
li_rc = loo_BdEncrypted.ConnectToNewObject("Chilkat_9_5_0.BinData")

loo_BdEncrypted.AppendEncoded(ls_Encrypted64,"base64")

//  Get the symmetric algorithm:  "http://www.w3.org/2001/04/xmlenc#aes128-cbc"
//  and set the symmetric decrypt properties.
loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat_9_5_0.Crypt2")

loo_SbAlg = create oleobject
li_rc = loo_SbAlg.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

loo_SbAlg.Append(loo_Xml.ChilkatPath("saml2:EncryptedAssertion|xenc:EncryptedData|xenc:EncryptionMethod|(Algorithm)"))
if loo_SbAlg.Contains("aes128-cbc",1) = 1 then
    loo_Crypt.CryptAlgorithm = "aes"
    loo_Crypt.KeyLength = 128
    loo_Crypt.CipherMode = "cbc"
    //  The 1st 16 bytes of the encrypted data are the AES IV.
    loo_Crypt.SetEncodedIV(loo_BdEncrypted.GetEncodedChunk(0,16,"hex"),"hex")
    loo_BdEncrypted.RemoveChunk(0,16)
end if

//  Other algorithms, key lengths, etc, can be supported by checking for different Algorithm attribute values..

loo_Crypt.SetEncodedKey(loo_BdAesKey.GetEncoded("hex"),"hex")

//  AES decrypt...
li_Success = loo_Crypt.DecryptBd(loo_BdEncrypted)
if li_Success <> 1 then
    Write-Debug loo_Crypt.LastErrorText
    destroy loo_Http
    destroy loo_SbSamlResponse
    destroy loo_SbPrivateKeyPem
    destroy loo_Xml
    destroy loo_Privkey
    destroy loo_Rsa
    destroy loo_BdAesKey
    destroy loo_SbRsaAlg
    destroy loo_BdEncrypted
    destroy loo_Crypt
    destroy loo_SbAlg
    return
end if

//  Get the decrypted XML
ls_DecryptedXml = loo_BdEncrypted.GetString("utf-8")
Write-Debug "Decrypted XML:"
Write-Debug ls_DecryptedXml

//  The decrypted XML looks like this:

//  <saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" ID="_226e565c548db7986d165d7d969b48b4" IssueInstant="2018-10-11T17:46:20.727Z" Version="2.0">
//  ...
//  ...
//  ...
//  </saml2:Assertion>

loo_XmlAssertion = create oleobject
li_rc = loo_XmlAssertion.ConnectToNewObject("Chilkat_9_5_0.Xml")

loo_XmlAssertion.LoadXml(ls_DecryptedXml)

//  Replace the saml2:EncryptedAssertion XML subtree with the saml2:Assertion XML.
loo_XmlEncryptedAssertion = loo_Xml.FindChild("saml2:EncryptedAssertion")
loo_XmlEncryptedAssertion.SwapTree(loo_XmlAssertion)
destroy loo_XmlEncryptedAssertion

//  The decrypted XML assertion has now replaced the encrypted XML assertion.
//  Examine the fully decrypted XML document:
Write-Debug "Full XML SAML document with decrypted assertion:"
Write-Debug loo_Xml.GetXml()


destroy loo_Http
destroy loo_SbSamlResponse
destroy loo_SbPrivateKeyPem
destroy loo_Xml
destroy loo_Privkey
destroy loo_Rsa
destroy loo_BdAesKey
destroy loo_SbRsaAlg
destroy loo_BdEncrypted
destroy loo_Crypt
destroy loo_SbAlg
destroy loo_XmlAssertion

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.