Code Monkey home page Code Monkey logo

applepay's Introduction

applepay

Update, November 2023: This repo is no longer maintained.

GoDoc

applepay is a Go package for processing Apple Pay transactions easily. It is aimed at merchants or PSPs that are looking to manage their own Apple Pay flow, as opposed to letting a third-party (such as Stripe) do it end-to-end. You will need a paid Apple Developer account to use this.

Note: we have included the Apple root CA in this repository. For production use-cases, you should always download the certificates directly from Apple.

Running tests

go test

You may need to change your PKG_CONFIG_PATH to include OpenSSL. For example, on my Mac I use PKG_CONFIG_PATH=$(brew --prefix openssl)/lib/pkgconfig go test.

Getting up and running with the example

Requirements:

  • An account in the Apple Developer Program
  • A recent version of Go (tested with v1.9.2)
  • cfssl
  • OpenSSL/libssl-dev
  • make
  1. Log into your developer account. In the developer console, create a Merchant ID

  2. Verify your domain by serving the apple-developer-merchantid-domain-association file at https://yourdomain.com/.well-known/apple-developer-merchantid-domain-association (the example app will do it for you if you put it in example/static/.well-known). Note: Be careful to support one of the supported cipher suites for HTTPS!

  3. Edit the JSON files in the certs/ directory of this repo with your merchant ID, your domain and your email address.

  4. Generate a Payment Processing Certificate request by running make cert-processing.certSigningRequest in the certs/ directory.

  5. Upload the certificate request to the developer console. Select your merchant ID and click "Create Certificate" in the "Payment Processing Certificate" section

  6. Download the signed certificate to certs/cert-processing.cer, run make cert-processing.crt to convert it to the proper format

  7. Repeat steps 4-6 for the Merchant Identity Certificate, by running make cert-merchant.certSigningRequest and, with the certificate, make cert-merchant.crt

  8. Move the directory certs/ to example/certs/

  9. Deploy the application under your domain. We have included a Dockerfile your your convenience; you should be able to run docker build, docker push and docker run.

  10. Go to the running application and try to pay with Apple Pay. If you don't see an Apple Pay button, you are probably visiting from an unsupported browser or device. You will not be charged.

Picking a Payment Service Provider (PSP) that supports Apple Pay

Some PSPs, such as Braintree or Stripe, will accept Apple Pay payments on your behalf. That means that you don't have to use this package and just need to verify your domain with them. If you really want to, you can probably use this package and pass the tokens that decrypt yourself. Check with your processor for more info on how to know how to do that.

Other payment services may not do the integration for you, but will let you pass Apple Pay-related values (such as the payment cryptogram). This package is made for you!

If you can't charge network tokens with your payment processor, unfortunately you will not be able to accept Apple Pay. Contact your provider for more details.

applepay's People

Contributors

andrea-vasapollo-cko avatar krystian-ogierman-cko avatar louis-paul avatar mateusz-walesiak-cko avatar pierre-lafanechaire-cko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

applepay's Issues

"intermediate cert is not trusted by root" when cert issued by a CA other than AppleRootCA-G3

Hello,

Apple issued us a certificate from the AppleRootCA-G2. I am able to overwrite this CA file contents using the following:

AppleRootCertificatePath = "AppleRootCA-G3.crt"

but because there is no way to override the following OIDs:

leafCertificateOID = mustParseASN1ObjectIdentifier("1.2.840.113635.100.6.29")
interCertificateOID = mustParseASN1ObjectIdentifier("1.2.840.113635.100.6.2.14")

I receive the following error:

return errors.Wrap(err, "intermediate cert is not trusted by root")

Bug in ECDHE Implementation

I understand that this library is now deprecated, but perhaps this information will be useful for those who intend to fork this library.

Background

We run an internal fork of this library and noticed around a small percentage of applepay token decryptions were failing with a cipher: message authentication failed error in production. We tried to replicate the issue locally and managed to narrow it down to the ECDHE and KDF functions.

Issue

The combined use of the ecdheSharedSecret and deriveEncryptionKey functions do not correctly implement P256 ECDHE correctly. Notably, they fail to pad the X coordinate of the point to 32 bytes as specified in SEC 1, Version 2.0, Section 2.3.5.

Replication

This snippet (playground link) illustrates the difference between the ScalarMult and Bytes implementation with the correct crypto/ecdh implementation:

package main

import (
	"crypto/ecdh"
	"crypto/ecdsa"
	"crypto/elliptic"
	"fmt"
	"math/big"
)

func main() {
	k1 := []byte{42, 154, 0, 223, 84, 8, 55, 162, 117, 24, 142, 136, 128, 33, 143, 69, 194, 175, 251, 116, 183, 49, 190, 0, 228, 232, 21, 229, 203, 187, 226, 96}
	k2 := []byte{195, 5, 124, 117, 229, 83, 2, 62, 251, 203, 46, 195, 218, 215, 152, 7, 226, 64, 180, 63, 19, 183, 198, 37, 234, 194, 188, 222, 158, 85, 230, 191}

	privECDH, _ := ecdh.P256().NewPrivateKey(k1)
	privECDH2, _ := ecdh.P256().NewPrivateKey(k2)
	secretECDHE, _ := privECDH.ECDH(privECDH2.PublicKey())

	privECDSA := ecdhToECDSAPrivKey(privECDH)
	pubECDSA := ecdhToECDSAPublicKey(privECDH2.PublicKey())
	x, _ := privECDSA.Curve.ScalarMult(pubECDSA.X, pubECDSA.Y, privECDSA.D.Bytes())
	secretScalarMult := x.Bytes()
	secretMultFillBytes := make([]byte, 32)
	secretMultFillBytes = x.FillBytes(secretMultFillBytes)

	fmt.Printf("len:%d %x\n", len(secretECDHE), secretECDHE)                 // len:32 005363f5f75762476c04d7f32385629501368593df7e89aea4cdaebe4ae6c217
	fmt.Printf("len:%d %x\n", len(secretScalarMult), secretScalarMult)       // len:31 5363f5f75762476c04d7f32385629501368593df7e89aea4cdaebe4ae6c217
	fmt.Printf("len:%d %x\n", len(secretMultFillBytes), secretMultFillBytes) // len:32 005363f5f75762476c04d7f32385629501368593df7e89aea4cdaebe4ae6c217
}

func ecdhToECDSAPrivKey(key *ecdh.PrivateKey) *ecdsa.PrivateKey {
	return &ecdsa.PrivateKey{
		PublicKey: *ecdhToECDSAPublicKey(key.PublicKey()),
		D:         big.NewInt(0).SetBytes(key.Bytes()),
	}
}

func ecdhToECDSAPublicKey(key *ecdh.PublicKey) *ecdsa.PublicKey {
	rawKey := key.Bytes()
	return &ecdsa.PublicKey{
		Curve: elliptic.P256(),
		X:     big.NewInt(0).SetBytes(rawKey[1:33]),
		Y:     big.NewInt(0).SetBytes(rawKey[33:]),
	}
}

Fix

If you are using go1.20 or newer, I would recommend that you refactor the code to use crypto/ecdh instead. As at go1.20, (elliptic.Curve).ScalarMult contained a note advising against its use while recommending the then newly added crypto/ecdh package which correctly implements ECDHE. From go1.21 onwards, (elliptic.Curve).ScalarMult is marked as deprecated.

For versions less than go1.20, replacing the (*big.Int).Bytes() call in deriveEncryptionKey with (*big.Int).SetBytes on a 32 length byte slice would be an alternative.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.