Skip to content

Commit

Permalink
feat(decode): Apple 解析数据后存储凭证原始数据,有助于查验结构体数据一致性 (#432)
Browse files Browse the repository at this point in the history
* feat(decode): 解析数据后存储凭证原始数据,有助于查验结构体数据一致性
* Update parser.go
pkg/jwt/parser.go:105:5: S1009: should omit nil check; len() for []encoding/json.RawMessage is defined as zero (gosimple)
空判断去除
  • Loading branch information
admininfon authored Nov 26, 2024
1 parent 2dc12fd commit 6042c74
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 61 deletions.
143 changes: 83 additions & 60 deletions apple/unsign_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,98 +21,121 @@ func ExtractClaims(signedPayload string, tran jwt.Claims) (err error) {
if valueOf.Kind() != reflect.Ptr {
return errors.New("tran must be ptr struct")
}

_, err = jwt.ParseWithClaims(signedPayload, tran, func(token *jwt.Token) (any, error) {
return x5cCertVerify(signedPayload)
tokenStr := signedPayload
rootCertStr, err := extractHeaderByIndex(tokenStr, 2)
if err != nil {
return err
}
intermediaCertStr, err := extractHeaderByIndex(tokenStr, 1)
if err != nil {
return err
}
if err = verifyCert(rootCertStr, intermediaCertStr); err != nil {
return err
}
_, err = jwt.ParseWithClaims(tokenStr, tran, func(token *jwt.Token) (any, error) {
return extractPublicKeyFromToken(tokenStr)
})
if err != nil {
return err
}
return nil
}

type header struct {
Alg string `json:"alg"`
X5c []string `json:"x5c"`
}

// 解析 x5c root intermedia user证书
// Per doc: https://datatracker.ietf.org/doc/html/rfc7515#section-4.1.6
func (h *header) certParse() (*x509.Certificate, *x509.Certificate, *x509.Certificate, error) {
if len(h.X5c) != 3 {
return nil, nil, nil, errors.New("invalid x5c format")
}
var certDecode = func(certStr string) (*x509.Certificate, error) {
certBytes, err := base64.StdEncoding.DecodeString(certStr)
if err != nil {
return nil, fmt.Errorf("failed to parse certificate: %w", err)
}
cert, err := x509.ParseCertificate(certBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse certificate: %w", err)
}
return cert, nil
}
root, err := certDecode(h.X5c[2])
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to decode root certificate: %w", err)
// ExtractClaimsToken 解析jws格式数据
//
// Args:
// - signedPayload:string, jws格式数据
// - tran:jwt.Claims, 指针类型的结构体,用于接收解析后的数据
func ExtractClaimsToken(signedPayload string, tran jwt.Claims) (tk *jwt.Token, err error) {
valueOf := reflect.ValueOf(tran)
if valueOf.Kind() != reflect.Ptr {
return nil, errors.New("tran must be ptr struct")
}
interCert, err := certDecode(h.X5c[1])
tokenStr := signedPayload
rootCertStr, err := extractHeaderByIndex(tokenStr, 2)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to decode intermedia certificate: %w", err)
return nil, err
}
userCert, err := certDecode(h.X5c[0])
intermediaCertStr, err := extractHeaderByIndex(tokenStr, 1)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to decode user certificate: %w", err)
return nil, err
}
return root, interCert, userCert, nil
if err = verifyCert(rootCertStr, intermediaCertStr); err != nil {
return nil, err
}
tk, err = jwt.ParseWithClaims(tokenStr, tran, func(token *jwt.Token) (any, error) {
return extractPublicKeyFromToken(tokenStr)
})
return tk, err
}

func (h *header) x5cCertVerify() (*ecdsa.PublicKey, error) {
_, i, u, err := h.certParse()
// Per doc: https://datatracker.ietf.org/doc/html/rfc7515#section-4.1.6
func extractPublicKeyFromToken(tokenStr string) (*ecdsa.PublicKey, error) {
certStr, err := extractHeaderByIndex(tokenStr, 0)
if err != nil {
return nil, err
}
var iPool = x509.NewCertPool()
iPool.AddCert(i)
opts := x509.VerifyOptions{
Roots: rootCertPool,
Intermediates: iPool,
}
_, err = u.Verify(opts)
cert, err := x509.ParseCertificate(certStr)
if err != nil {
return nil, err
}
switch pk := u.PublicKey.(type) {
switch pk := cert.PublicKey.(type) {
case *ecdsa.PublicKey:
return pk, nil
default:
return nil, errors.New("appstore public key must be of type ecdsa.PublicKey")
}
}

// 苹果根ca证书
var rootCertPool *x509.CertPool

func init() {
rootCertPool = x509.NewCertPool()
rootCertPool.AppendCertsFromPEM([]byte(rootPEM))
}

// 验证x5c证书链是否合法
func x5cCertVerify(tokenStr string) (*ecdsa.PublicKey, error) {
tokenArr := strings.Split(tokenStr, ".")
if len(tokenArr) != 3 {
return nil, errors.New("invalid jwt format")
func extractHeaderByIndex(tokenStr string, index int) ([]byte, error) {
if index > 2 {
return nil, errors.New("invalid index")
}
tokenArr := strings.Split(tokenStr, ".")
headerByte, err := base64.RawStdEncoding.DecodeString(tokenArr[0])
if err != nil {
return nil, err
}
h := &header{}
err = json.Unmarshal(headerByte, h)
type Header struct {
Alg string `json:"alg"`
X5c []string `json:"x5c"`
}
header := &Header{}
err = json.Unmarshal(headerByte, header)
if err != nil {
return nil, err
}
if len(header.X5c) < index {
return nil, fmt.Errorf("index[%d] > header.x5c slice len(%d)", index, len(header.X5c))
}
certByte, err := base64.StdEncoding.DecodeString(header.X5c[index])
if err != nil {
return nil, err
}
return certByte, nil
}

func verifyCert(certByte, intermediaCertStr []byte) error {
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM([]byte(rootPEM))
if !ok {
return errors.New("failed to parse root certificate")
}
interCert, err := x509.ParseCertificate(intermediaCertStr)
if err != nil {
return nil, fmt.Errorf("invalid jwt.header: %w", err)
return errors.New("failed to parse intermedia certificate")
}
intermedia := x509.NewCertPool()
intermedia.AddCert(interCert)
cert, err := x509.ParseCertificate(certByte)
if err != nil {
return err
}
opts := x509.VerifyOptions{
Roots: roots,
Intermediates: intermedia,
}
return h.x5cCertVerify()
_, err = cert.Verify(opts)
return err
}
67 changes: 67 additions & 0 deletions apple/unsign_jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,70 @@ func TestExtractClaims(t *testing.T) {
}
*/
}

func TestExtractClaimsToken(t *testing.T) {
signedPayload := "eyJhbGciOiJFUzI1NiIsIng1YyI6WyJNSUlFTURDQ0E3YWdBd0lCQWdJUWFQb1BsZHZwU29FSDBsQnJqRFB2OWpBS0JnZ3Foa2pPUFFRREF6QjFNVV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XdNREF4TWpZeE1EY3lPVEl4SWl3aWIzSnBaMmx1WVd4VWNtRnVjMkZqZEdsdmJrbGtJam9pTlRFd01EQXhNall4TURjeU9USXhJaXdpWW5WdVpHeGxTV1FpT2lKamIyMHVhbkpxYWk1clpYbHpibk1pTENKd2NtOWtkV04wU1dRaU9pSmpiMjB1YTJWNWMyNXpMa3BTTVRJd01DSXNJbkIxY21Ob1lYTmxSR0YwWlNJNk1UWTROekl6T1RBek5qQXdNQ3dpYjNKcFoybHVZV3hRZFhKamFHRnpaVVJoZEdVaU9qRTJPRGN5TXprd016WXdNREFzSW5GMVlXNTBhWFI1SWpveExDSjBlWEJsSWpvaVEyOXVjM1Z0WVdKc1pTSXNJbWx1UVhCd1QzZHVaWEp6YUdsd1ZIbHdaU0k2SWxCVlVrTklRVk5GUkNJc0luTnBaMjVsWkVSaGRHVWlPakUyT0RjNU5ERTFNVEkxTnpNc0ltVnVkbWx5YjI1dFpXNTBJam9pVUhKdlpIVmpkR2x2YmlJc0luUnlZVzV6WVdOMGFXOXVVbVZoYzI5dUlqb2lVRlZTUTBoQlUwVWlMQ0p6ZEc5eVpXWnliMjUwSWpvaVEwaE9JaXdpYzNSdmNtVm1jbTl1ZEVsa0lqb2lNVFF6TkRZMUluMC5hM09PRTNGcGxPdF9UQV9nQURzNW1rM2JRNWdKUDJubFdWX2hfN2xKRUMtcjNCZDh0d2ItSzFuQm5KR3hGVW9OeVRibWp3YXZOcDhPUHM5bGw4cGhfZyJ9LCJ2ZXJzaW9uIjoiMi4wIiwic2lnbmVkRGF0ZSI6MTY4Nzk0MTUxMjU2MH0.Tz4bvMOb-AT88is_pobjyOBZxwIKDkyugOQ4BqfTbhrlRNgKaZ-upGRTJlydJrUM9aRfdezRc0-1ekmMwcndcA"
np := &NotificationV2Payload{}
tk, err := ExtractClaimsToken(signedPayload, np)
if err != nil {
xlog.Error(err)
return
}
xlog.Infof("np:%+v", np)
xlog.Infof("np.data:%+v", np.Data)

// Print token segments, verify the integrity of original data.
for i := 0; i < len(tk.TokenSegmentRows); i++ {
xlog.Infof("token_raw[%d]:%s\n", i, tk.TokenSegmentRows[i])
}

ti, err := np.DecodeTransactionInfo()
if err != nil {
xlog.Error(err)
return
}
xlog.Infof("ti:%+v", ti.TransactionId)
xlog.Infof("ti_info:%+v\n", ti)

ot, err := ExtractClaimsToken(np.Data.SignedTransactionInfo, &TransactionInfo{})
if err != nil {
xlog.Error(err)
return
}

// 解析数据[ti_info] 与 原始数据[ot_info] 对比, 确保数据结构体可以完全解析, 版本更新后确认数据一致性场景下会有帮助作用.
xlog.Infof("ot_info:%s\n", ot.TokenSegmentRows[1])

/*
{
"transactionId": "510001261072921",
"originalTransactionId": "510001261072921",
"bundleId": "com.jrjj.keysns",
"productId": "com.keysns.JR1200",
"purchaseDate": 1687239036000,
"originalPurchaseDate": 1687239036000,
"quantity": 1,
"type": "Consumable",
"inAppOwnershipType": "PURCHASED",
"signedDate": 1687941512573,
"environment": "Production",
"transactionReason": "PURCHASE",
"storefront": "CHN",
"storefrontId": "143465"
}
*/
}

func ExampleExtractClaimsToken() {
signedPayload := "eyJhbGciOi......upGRTJlydJrUM9aRfdezRc0-1ekmMwcndcA"
np := &NotificationV2Payload{}
tk, err := ExtractClaimsToken(signedPayload, np)
xlog.Errorf("err:%+v\n", err)
xlog.Infof("token:%+v\n", tk)
xlog.Infof("np:%+v\n", np)

// Output:
// - err: error, invalid message.
// - token: *jwt.Token, &jwt.Token{...}
// - np: *apple.NotificationV2Payload, &apple.NotificationV2Payload{...}
}
Loading

0 comments on commit 6042c74

Please sign in to comment.