-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode.go
52 lines (43 loc) · 1.44 KB
/
decode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package applereceipt
import (
"crypto/x509"
"encoding/asn1"
"encoding/base64"
"go.mozilla.org/pkcs7"
)
//go:generate go run gen.go
type ReceiptAttribute struct {
Type int
Version int
Value []byte
}
// DecodeBase64 decodes given base64-encoded receipt into AppReceipt type.
// It verifies the payload with given certPool. certPool can be nil to skip verification, but be careful that users can pass a forged receipt.
func DecodeBase64(b64Receipt string, certPool *x509.CertPool) (AppReceipt, error) {
receipt := make([]byte, base64.StdEncoding.DecodedLen(len(b64Receipt)))
_, err := base64.StdEncoding.Decode(receipt, []byte(b64Receipt))
if err != nil {
return AppReceipt{}, err
}
return Decode(receipt, certPool)
}
// Decode decodes given receipt binary into AppReceipt type.
// It verifies the payload with given certPool. certPool can be nil to skip verification, but be careful that users can pass a forged receipt.
func Decode(receipt []byte, certPool *x509.CertPool) (AppReceipt, error) {
p, err := pkcs7.Parse(receipt)
if err != nil {
return AppReceipt{}, err
}
var attrs []ReceiptAttribute
if _, err := asn1.UnmarshalWithParams(p.Content, &attrs, "set"); err != nil {
return AppReceipt{}, err
}
appReceipt, err := newAppReceipt(attrs)
if err != nil {
return AppReceipt{}, err
}
if err := p.VerifyWithChainAtTime(certPool, appReceipt.ReceiptCreationDate); err != nil {
return AppReceipt{}, err
}
return appReceipt, nil
}