google-pay #
Verification and decryption of Google Pay payment method tokens, protocol
ECv2.
A merchant that takes Google Pay with the tokenization type DIRECT, rather
than through a gateway, receives the payment data as a token it verifies and
decrypts itself, as Google's
Payment data cryptography
page specifies:
- one of the intermediate signing key's signatures verifies under an unexpired
key of Google's root key set (keys.json); - the intermediate key has not expired;
- the message signature verifies under the intermediate key for the recipient
merchant:<merchant id>; - ECIES-KEM on P-256 with the token's ephemeral point, HKDF-SHA256 with no salt
and the infoGoogle, gives an AES-256-CTR key and an HMAC-SHA256 key, and
the tag is checked in constant time before the message is decrypted; - the decrypted message has not expired.
The signatures are ECDSA P-256 with SHA-256 over the fields Google, the
recipient, ECv2 and the signed string, each preceded by its length as 4
bytes little-endian (Google_pay.Signed). The root key set, the merchant id,
the merchant's keys and the current time are arguments: the caller fetches
keys.json from Google_pay.Root_keys.production_url (or test_url), at
least once a year, and lists its keys new first so that a key rotation decrypts
tokens made for either key. ECv1 and ECv2SigningOnly are not supported.
Usage #
A merchant key is a P-256 key, in PKCS #8 DER or in PEM; its public point is
the publicKey of the tokenization specification:
# let key =
Base64.decode_exn
"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgCPSuFr4iSIaQprjjchHPyDu2NXFe0vDBoTpPkYaK9dehRANCAATnaFz/vQKuO90pxsINyVNWojabHfbx9qIJ6uD7Q7ZSxmtyo/Ez3/o2kDT8g0pIdyVIYktCsq65VoQIDWSh2Bdm"
|> Google_pay.Merchant_key.of_der |> Result.get_ok;;
val key : Google_pay.Merchant_key.t = <abstr>
# Google_pay.Merchant_key.public_key key;;
- : string =
"BOdoXP+9Aq473SnGwg3JU1aiNpsd9vH2ognq4PtDtlLGa3Kj8TPf+jaQNPyDSkh3JUhiS0KyrrlWhAgNZKHYF2Y="
Google_pay.decrypt verifies and decrypts a token, and names why it refuses
one:
# let root_keys = Google_pay.Root_keys.of_string {|{"keys":[]}|} |> Result.get_ok;;
val root_keys : Google_pay.Root_keys.t = <abstr>
# let token =
{|{"protocolVersion":"ECv1","signature":"","signedMessage":"",
"intermediateSigningKey":{"signedKey":"","signatures":[]}}|};;
val token : string =
"{\"protocolVersion\":\"ECv1\",\"signature\":\"\",\"signedMessage\":\"\",\n \"intermediateSigningKey\":{\"signedKey\":\"\",\"signatures\":[]}}"
# match
Google_pay.decrypt ~root_keys ~merchant_id:"12345" ~keys:[ key ]
~now:Ptime.epoch token
with
| Ok m -> Fmt.pr "%a@." Google_pay.Message.pp m
| Error e -> Fmt.pr "%a@." Google_pay.pp_error e;;
protocol version "ECv1" is not ECv2
- : unit = ()
Command line #
google-pay public-key KEY prints the publicKey of a merchant key in PEM.
google-pay decrypt verifies and decrypts a token, from a file or standard
input, and prints its message without the card number or the cryptogram; a
refused token exits with its reason:
$ google-pay decrypt --root-keys keys.json --merchant-id 12345 \
--key new.pem --key old.pem token.json
--time checks the expirations at another time than the current one, and
--unsealed prints the decrypted bytes without reading them as a message.
Tests #
The unit cases replay Google's page: its two signed byte strings, whose lengths
count the escapes \u003d the signed strings keep; its sample token, whose
message signature and ciphertext verify and decrypt under the test keys of
Tink's paymentmethodtoken recipient tests; and its merchant key's public
point. Tink's tests hold no fixed ECv2 token, as they seal one when they run,
and the page's intermediate key is signed by a root key neither publishes, so
the cases add a signature by Tink's ECv2 test key beside the page's. A test
sender seals further tokens with those keys to refuse, each by its name, an
expired intermediate key, an expired message, a wrong recipient, a wrong
merchant key and a flipped bit of the tag.
Related work #
- Tink's
apps-paymentmethodtoken(Java) is the library Google recommends,
and the reference for the checks here.