This repository has been archived by the owner on Nov 28, 2024. It is now read-only.
forked from bitrise-steplib/steps-sign-apk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apksigner.go
175 lines (145 loc) · 4.26 KB
/
apksigner.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"bytes"
"errors"
"fmt"
"io"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/errorutil"
"github.com/bitrise-io/go-utils/log"
)
func createSignerSchemeCmd(signerScheme string) string {
switch signerScheme {
case "automatic":
return ""
case "v2":
return "--v2-signing-enabled"
case "v3":
return "--v3-signing-enabled"
case "v4":
return "--v4-signing-enabled"
default:
return ""
}
}
func createKeystoreCmdSlice(configuration *KeystoreSignatureConfiguration) ([]string, error) {
if configuration == nil {
return []string{}, errors.New("Invalid Keystore Configuration")
}
cmdSlice := []string{
"--ks",
configuration.keystorePth,
"--ks-pass",
"pass:" + configuration.keystorePassword,
"--ks-key-alias",
configuration.alias,
}
if configuration.aliasPassword != "" {
cmdSlice = append(cmdSlice, "--key-pass", "pass:"+configuration.aliasPassword)
}
return cmdSlice, nil
}
func (configuration SignatureConfiguration) createSignCmd(buildArtifactPth string, destBuildArtifactPth string) ([]string, error) {
var signatureSlice []string
var err error
switch configuration.signatureType {
case KeystoreSignatureType:
signatureSlice, err = createKeystoreCmdSlice(configuration.keystoreConfiguration)
default:
err = fmt.Errorf("invalid signature type: %s", configuration.signatureType)
}
if err != nil {
return nil, err
}
cmdSlice := []string{
configuration.apkSigner,
"sign",
"--in",
buildArtifactPth,
"--out",
destBuildArtifactPth,
"--debuggable-apk-permitted",
configuration.debuggablePermitted,
}
scheme := createSignerSchemeCmd(configuration.signerScheme)
if scheme != "" {
cmdSlice = append(cmdSlice, scheme)
}
cmdSlice = append(cmdSlice, signatureSlice...)
return cmdSlice, nil
}
// SignBuildArtifact buildArtifactPth
// This signs the provided APK, stripping out any pre-existing signatures. Signing
// is performed using one or more signers, each represented by an asymmetric key
// pair and a corresponding certificate.
//
// - buildArtifactPth: The path to the unsigned APK
// - destBuildArtifactPth: Path were the signed APK will be stored
func (configuration SignatureConfiguration) SignBuildArtifact(buildArtifactPth string, destBuildArtifactPth string) error {
cmdSlice, err := configuration.createSignCmd(buildArtifactPth, destBuildArtifactPth)
if err != nil {
return fmt.Errorf("failed to create signing command from signing configuration: %v", err)
}
prinatableCmd := command.PrintableCommandArgs(false, secureSignCmd(cmdSlice))
log.Printf("=> %s", prinatableCmd)
out, err := executeForOutput(cmdSlice)
if err != nil {
return properError(err, out)
}
return err
}
// VerifyBuildArtifact buildArtifactPth
// This checks whether the provided APK will verify on Android. By default, this
// checks whether the APK will verify on all Android platform versions supported
// by the APK (as declared using minSdkVersion in AndroidManifest.xml).
//
// - buildArtifactPth: The path of the signed APK
func (configuration SignatureConfiguration) VerifyBuildArtifact(buildArtifactPth string) error {
cmdSlice := []string{
configuration.apkSigner,
"verify",
"--verbose",
"--in",
buildArtifactPth,
}
prinatableCmd := command.PrintableCommandArgs(false, cmdSlice)
log.Printf("=> %s", prinatableCmd)
out, err := executeForOutput(cmdSlice)
if err != nil {
return properError(err, out)
}
return nil
}
func executeForOutput(cmdSlice []string) (string, error) {
cmd, err := command.NewFromSlice(cmdSlice)
if err != nil {
return "", fmt.Errorf("Failed to create command, error: %s", err)
}
var outputBuf bytes.Buffer
writer := io.MultiWriter(&outputBuf)
cmd.SetStderr(writer)
cmd.SetStdout(writer)
err = cmd.Run()
if err != nil {
err = fmt.Errorf("%s\n%s", outputBuf.String(), err)
}
return outputBuf.String(), err
}
func properError(err error, out string) error {
if errorutil.IsExitStatusError(err) {
return errors.New(out)
}
return err
}
func secureSignCmd(cmdSlice []string) []string {
securedCmdSlice := []string{}
secureNextParam := false
for _, param := range cmdSlice {
if secureNextParam {
param = "***"
}
secureNextParam = (param == "--ks-pass" || param == "--key-pass")
securedCmdSlice = append(securedCmdSlice, param)
}
return securedCmdSlice
}