Validating Certificates, Extracting Public Keys, and Verifying JWS
CREATED: 2024 / 03 / 02 Sat
UPDATED: 2024 / 03 / 03 Sun
Validating Certificates and Extracting the Public Key
When receiving notifications from third-party services, a common pattern involves validating a certificate.
I had AppStore server notifications primarily in mind here, but this pattern applies more broadly, and I felt there was a lot to learn from it — so I wanted to document the approach.
Assuming the root certificate is publicly available and can be obtained from official documentation: extract the certificate chain from a JWS header received separately, and validate it against the published root certificate.
If validation succeeds, extract the public key from the first certificate in the JWS certificate chain.
Use the public key and hash algorithm to hash the payload, then verify that it matches the JWS signature — detecting any tampering.
Validating the Certificate Chain in JWS and Extracting the Public Key
Speaking in terms of the AppStore server notification V2 spec: when a purchase is made in an iOS app, a request containing a string built according to the JWS spec is sent to a pre-configured endpoint.
Inspecting the request body at the endpoint reveals an element called signedPayload.
This is the JWS-formatted string, split into three parts by .:
the header, the payload, and the signature.
Here’s the order of operations for validating it:
1. Extract the certificate chain from the header
2. Validate the certificate chain using the published root certificate
3. Extract the public key
First, let’s extract the certificate chain from the header. We’ll use Ruby.
Start by pointing your server at the AppStore server notification endpoint, then make a test purchase via TestFlight or similar.
Apple will send a request to your server. Extract signedPayload from it.
This is the JWS-formatted string, so split it by ..
The result should be an array of three elements: header, payload, and signature. The certificate chain is in the first element.
Each JWS part is Base64-encoded, so you need to decode it to inspect the contents.
The code below also JSON-parses the decoded header for convenience:
jws = params['signedPayload']
jws_parts = jws.split('.')
headers = JSON.parse(Base64.urlsafe_decode64(jws_parts[0]))
The headers roughly contains the following:
{
"alg": "ES256",
"x5c": [
"MIIEMDCCA7agAwIBAgIQfTlfd0fNvFWvzC1YIANsXjAKBggqhkjOPQQDAzB1MUQwQgYDVQQDDDtBcHBsZSBXb3JsZHdpZGUgRGV2ZWxvcGVyIFJlbGF0aW9ucyBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTELMAkGA1UECwwCRzYxEzARBgNVBAoMCkFwcGxlIEluYy4xCzAJBgNVBAYTAlVTMB4XDTIzMDkx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XhSq5IyKogMCPtw490BaB677CaEGJXufQB/EqZGd6CSjiCtOnuMTbXVXmxxcxfkCMQDTSPxarZXvNrkxU3TkUMI33yzvFVVRT4wxWJC994OsdcZ4+RGNsYDyR5gmdr0nDGg=",
"MIICQzCCAcmgAwIBAgIILcX8iNLFS5UwCgYIKoZIzj0EAwMwZzEbMBkGA1UEAwwSQXBwbGUgUm9vdCBDQSAtIEczMSYwJAYDVQQLDB1BcHBsZSBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTETMBEGA1UECgwKQXBwbGUgSW5jLjELMAkGA1UEBhMCVVMwHhcNMTQwNDMwMTgxOTA2WhcNMzkwNDMwMTgxOTA2WjBnMRswGQYDVQQDDBJBcHBsZSBSb290IENBIC0gRzMxJjAkBgNVBAsMHUFwcGxlIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MRMwEQYDVQQKDApBcHBsZSBJbmMuMQswCQYDVQQGEwJVUzB2MBAGByqGSM49AgEGBSuBBAAiA2IABJjpLz1AcqTtkyJygRMc3RCV8cWjTnHcFBbZDuWmBSp3ZHtfTjjTuxxEtX/1H7YyYl3J6YRbTzBPEVoA/VhYDKX1DyxNB0cTddqXl5dvMVztK517IDvYuVTZXpmkOlEKMaNCMEAwHQYDVR0OBBYEFLuw3qFYM4iapIqZ3r6966/ayySrMA8GA1UdEwEB/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgEGMAoGCCqGSM49BAMDA2gAMGUCMQCD6cHEFl4aXTQY2e3v9GwOAEZLuN+yRhHFD/3meoyhpmvOwgPUnPWTxnS4at+qIxUCMG1mihDK1A3UT82NQz60imOlM27jbdoXt2QfyFMm+YhidDkLF1vLUagM6BgD56KyKA=="
]
}
alg specifies the JWS hash algorithm — AppStore server notifications use ES256.
ES256 is ECDSA using P-256 and SHA-256. To validate the certificate chain in x5c, you need a root certificate signed with the same algorithm.
Among the root certificates provided by Apple PKI, only G3 (Apple Root CA - G3 Root) supports ECDSA, so you need to download and use that one (you can confirm by running openssl x509 -in AppleRootCA-G3.cer -inform DER -text -noout and checking the Algorithm — only G3 Root uses ECDSA).
The x5c field specification in JWS headers can be found here:
Each string in the array is a base64-encoded (Section 4 of [RFC4648] -- not base64url-encoded) DER [ITU.X690.2008] PKIX certificate value. The certificate containing the public key corresponding to the key used to digitally sign the JWS MUST be the first certificate.
As stated, the first certificate in x5c contains the public key used for the digital signature.
PKIX refers to the Internet PKI (Public Key Infrastructure) based on X.509, as defined in RFC 5280.
Here’s how to validate the certificate chain in Ruby and extract the public key from the first certificate:
# Extract x5c from the headers above
certificate, *chain = headers['x5c'].map { |c| OpenSSL::X509::Certificate.new(Base64.strict_decode64(c)) }
root_certificate = OpenSSL::X509::Certificate.new(File.read('/path/to/AppleRootCA-G3.cer'))
store = OpenSSL::X509::Store.new
store.add_cert(root_certificate)
if store.verify(certificate, chain)
certificate.public_key # Extract the public key
end
The certificates from x5c are converted into OpenSSL::X509::Certificate instances. The first certificate goes into certificate, the rest into chain.
The local AppleRootCA-G3.cer is used to validate the chain.
OpenSSL::X509::Store manages a list of trusted certificates. add_cert adds AppleRootCA-G3.cer, and verify uses it to validate certificate and chain.
If validation succeeds, extract public_key and use it to verify the JWS signature.
Curious about what’s inside the x5c certificates?
According to the spec, x5c contains an array of Base64-encoded DER-format strings.
Let’s extract one and inspect it locally with openssl:
$ echo MIIEMDCCA7agAwIBAgIQfTlfd0fNvFWvzC1YIANsXjAKBggqhkjOPQQDAzB1MUQwQgYDVQQDDDtBcHBsZSBXb3JsZHdpZGUgRGV2ZWxvcGVyIFJlbGF0aW9ucyBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTELMAkGA1UECwwCRzYxEzARBgNVBAoMCkFwcGxlIEluYy4xCzAJBgNVBAYTAlVTMB4XDTIzMDkxMjE5NTE1M1oXDTI1MTAxMTE5NTE1MlowgZIxQDA+BgNVBAMMN1Byb2QgRUNDIE1hYyBBcHAgU3RvcmUgYW5kIGlUdW5lcyBTdG9yZSBSZWNlaXB0IFNpZ25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VTamRYL4aSs5k= | base64 --decode >> first.der
$ openssl x509 -in first.der -inform DER -text -noout
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
7d:39:5f:77:47:cd:bc:55:af:cc:2d:58:20:03:6c:5e
Signature Algorithm: ecdsa-with-SHA384
Issuer: CN=Apple Worldwide Developer Relations Certification Authority, OU=G6, O=Apple Inc., C=US
Validity
Not Before: Sep 12 19:51:53 2023 GMT
Not After : Oct 11 19:51:52 2025 GMT
Subject: CN=Prod ECC Mac App Store and iTunes Store Receipt Signing, OU=Apple Worldwide Developer Relations, O=Apple Inc., C=US
# omitted...
This is the first element of x5c, so we extract the public key from this certificate to verify the JWS signature.
Let’s also look at the other certificates:
$ openssl x509 -in second.der -inform DER -text -noout
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
22:c1:a1:47:0a:74:73:69:ef:53:86:12:c9:c6:9f:3d:38:f3:6c:d7
Signature Algorithm: ecdsa-with-SHA384
Issuer: CN=Apple Root CA - G3, OU=Apple Certification Authority, O=Apple Inc., C=US
Validity
Not Before: Mar 17 20:37:10 2021 GMT
Not After : Mar 19 00:00:00 2036 GMT
Subject: CN=Apple Worldwide Developer Relations Certification Authority, OU=G6, O=Apple Inc., C=US
# omitted...
$ openssl x509 -in third.der -inform DER -text -noout
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 3298319966700653461 (0x2dc5fc88d2c54b95)
Signature Algorithm: ecdsa-with-SHA384
Issuer: CN=Apple Root CA - G3, OU=Apple Certification Authority, O=Apple Inc., C=US
Validity
Not Before: Apr 30 18:19:06 2014 GMT
Not After : Apr 30 18:19:06 2039 GMT
Subject: CN=Apple Root CA - G3, OU=Apple Certification Authority, O=Apple Inc., C=US
# omitted...
It’s a proper chain.
third.der has matching Issuer and Subject; second.der’s Issuer matches third.der’s Subject; and first.der’s Issuer matches second.der’s Subject.
The third certificate is the root, with the second and first following as children.
You can also confirm that the root certificate downloaded from Apple PKI matches the third certificate.
Extracting the Public Key from the Certificate and Verifying the JWS
JWS signature verification uses the extracted public key. In Ruby, it’s simply:
# Using ruby-jwt (https://github.com/jwt/ruby-jwt)
# Decode (https://github.com/jwt/ruby-jwt/blob/main/lib/jwt/decode.rb)
# 1st argument: JWS
# 2nd argument: public key
# 3rd argument: whether to verify
# 4th argument: supported algorithm
JWT.decode jws, public_key, true, { algorithm: 'ES256' }
If verification succeeds, an array is returned, and the payload can be found in the first element. With AppStore server notifications, you can then perform whatever server-side processing the payload dictates.
Appendix
Nothing else to add.