Webpay communication protocol

February 1, 2013

This is going to be a bit long and boring, but I promised to write about this so here it comes:

The base

The protocol can be divided in a few basic steps:

  1. The commerce generates a payment token by delivering all the payment information (amount, order id, redirection urls among others) to Webpay's servers through an application/x-www-form-urlencoded HTTP POST request with an encrypted TBK_PARAM attribute.
  2. Webpay's servers responds the request with an encrypted payload that contains an XML document with a TOKEN value.
  3. The user is redirected to Webpay payment flow using the token acquired by the commerce on the second step.
  4. Once the user completes the payment the commerce receives an application/x-www-form-urlencoded HTTP POST request with an encrypted TBK_PARAM attribute directly from Webpay's servers with the payment information (success or failure, amount, authorization code among others).
  5. The commerce responds to Webpay's request with an encrypted acknowledge message
  6. Finally the user is redirected back to the commerce website

Now given the communication flow outlined above there are 2 things to analyze.

  1. The encryption and decryption algorithms.
  2. The 4 messages exchanged between the commerce and Webpay:
    1. Payment details message (from step 1)
    2. Webpay token response (from step 2)
    3. Webpay confirmation details (from step 4)
    4. Commerce acknowledge message (from step 5)

The encryption and decryption algorithms.

This algorithm is based on a pair of asymmetric RSA keys exchanged between the commerce and Transbank.

In order to explain this algorithm I'm going to use some bytes of ruby code. So given a message, the private key of the sender (sender_key) and the public key of the recipient (recipient_key) we will do the following:

# Sign the message using the sender_key with a SHA512 Hash
signature = sender_key.sign(OpenSSL::Digest::SHA512.new, message)

# Generate a random symmetric key of 32 bytes
key = SecureRandom.random_bytes(32)
# Generate a random initialization vector of 16 bytes
iv = SecureRandom.random_bytes(16)

# Encrypt the message and signature with an AES 256 CBC cypher
cipher = OpenSSL::Cipher.new('AES-256-CBC')
# Use the generated key
cipher.key = key
# Use the generated initialization vector with a constant padding
cipher.iv = iv + "\x10\xBB\xFF\xBF\x00\x00\x00\x00\x00\x00\x00\x00\xF4\xBF"

# Encrypt the signature concatenated with the message
encrypted_message = cipher.update(signature + text) + cipher.final

# Encrypt the generated key with the recipient_key using PKCS1 OAEP padding
encrypted_key = recipient_key.public_encrypt(key, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)

# And finally Base64 encode the initialization vector, the encrypted key and
# the encrypted message.
return Base64.encode64( iv + encrypted_key + encrypted_message)

(Check TBK::Webpay::Encryption#webpay_encrypt for a working example.)

And as should be expected the decryption algorithm is basically performing the same actions from the encryption algorithm in reverse order

data = Base64.decode64(encripted_message)

# Initialization vector is on the first 16 bytes
iv = data[0...16]

# the encrypted key comes next
encrypted_key = data[16...(16 + recipient_key_bytes)]

# Decrypt with the recipient_key using PKCS1 OAEP padding
key = recipient_key.private_decrypt(encrypted_key, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)

# Decrypt with an AES 256 CBC cypher
cipher = OpenSSL::Cipher.new('AES-256-CBC')
cipher.key = key
cipher.iv = iv + "\x10\xBB\xFF\xBF\x00\x00\x00\x00\x00\x00\x00\x00\xF4\xBF"
decrypted_text = cipher.update(data[(16 + recipient_key_bytes)..-1]) + cipher.final

# Split signature and message
signature = decrypted_text[0...(sender_key_bytes)]
message = decrypted_text[(sender_key_bytes)..-1]

# Verify the message signature
if sender_key.verify(OpenSSL::Digest::SHA512.new, signature, message)
  return message
  raise "Invalid message signature"

(Check TBK::Webpay::Encryption#webpay_decrypt for a working example)

The messages exchanged between the commerce and Webpay

Now that we know how everything is encrypted all we need to know is what messages get exchanged.

First message

The first message consists on all the payment information and looks like:


Where the key attributes are:

(Check TBK::Webpay::Payment#raw_params for a working example) This message is encrypted and delivered via HTTP POST to the endpoint https://webpay.transbank.cl:443/cgi-bin/bp_validacion.cgi

Second message

This endpoint responds with an encrypted message that looks like:


Once you have the token all you have to do is redirect the user to the url:


Third message

Once the user has completed the payment your server will receive an encrypted message like:


With all the details from the payment

Fourth message

Finally you have to acknowledge this POST request with an encrypted ACK message. Once done, Webpay will capture the payment and redirect the user to the TBK_URL_EXITO url if the value of TBK_RESPUESTA is zero and to TBK_URL_FRACASO otherwise.

Hope this can help anybody trying to implement the protocol on any language and any comment or doubt can be done on the comments..

Discussion, links, and tweets

I'm Co Founder & CTO at Welcu and a Natural born hacker.