forked from fiam/macapptool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign.go
120 lines (109 loc) · 2.65 KB
/
sign.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/google/subcommands"
)
type signCmd struct {
Identity string
Entitlements string
}
func (*signCmd) Name() string {
return "sign"
}
func (*signCmd) Synopsis() string {
return "Sign an app bundle"
}
func (*signCmd) Usage() string {
return `sign [-i identity][-e entitlements] some.app`
}
func (c *signCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
for _, arg := range f.Args() {
if err := c.signApp(arg); err != nil {
errPrintf("error signing %s: %v\n", arg, err)
return subcommands.ExitFailure
}
}
return subcommands.ExitSuccess
}
func (c *signCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&c.Identity, "i", "Developer ID", "Identity to sign the app")
f.StringVar(&c.Entitlements, "e", "", "Custom entitlements to use")
}
func (c *signCmd) signPath(root, p string) error {
st, err := os.Stat(p)
if err != nil {
return err
}
ext := filepath.Ext(p)
var shouldSign bool
if st.IsDir() {
shouldSign = ext == ".app" || ext == ".framework" || ext == ".xpc"
// Inner bundles need to be signed before outer ones
entries, err := ioutil.ReadDir(p)
if err != nil {
return err
}
for _, v := range entries {
if err := c.signPath(root, filepath.Join(p, v.Name())); err != nil {
return err
}
}
} else {
shouldSign = ext == ".dylib" || filepath.Base(filepath.Dir(p)) == "Helpers" || isExecutable(st)
}
if shouldSign {
return c.signEntry(root, p)
}
return nil
}
func (c *signCmd) signApp(p string) error {
// If the argument is foo.app/,
// filepath.Ext() will return an empty
// string. Make sure we don't skip it
if strings.HasSuffix(p, "/") {
p = p[:len(p)-1]
}
if err := c.signPath(p, p); err != nil {
return err
}
// Verify signature
ext := strings.ToLower(filepath.Ext(p))
if ext == ".app" || ext == ".framework" {
//return verifySignature(p)
return nil
}
return nil
}
func (c *signCmd) signEntry(root, p string) error {
rel, _ := filepath.Rel(root, p)
name := rel
if name == "" {
name = root
}
verbosePrintf(1, "signing %s\n", name)
var args []string
if *verbose > 0 {
args = append(args, "--verbose")
}
args = append(args, "--force", "--options=runtime", "--timestamp")
if c.Entitlements != "" {
args = append(args, "--entitlements", c.Entitlements)
}
args = append(args, "--sign", c.Identity, p)
cmd := exec.Command("codesign", args...)
if *dryRun {
fmt.Printf("%s\n", strings.Join(cmd.Args, " "))
return nil
}
verbosePrintf(2, "%s\n", strings.Join(cmd.Args, " "))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}