-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
115 lines (99 loc) · 2.44 KB
/
main.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
// Copyright © 2020 https://github.com/Leso-KN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"io/ioutil"
"path"
"os"
"playground/android/apksign"
"playground/android"
)
//
func loadFile(name string) ([]byte, error) {
var f *os.File
var err error
var b []byte
if f, err = os.Open(name); err != nil {
return nil, err
}
defer f.Close()
if b, err = ioutil.ReadAll(f); err != nil {
return nil, err
}
return b, err
}
func saveFile(name string, b []byte) error {
var f *os.File
var err error
if f, err = os.Create(name); err != nil {
return err
}
defer f.Close()
if _, err = f.Write(b); err != nil {
return err
}
return nil
}
func main() {
if len(os.Args) < 4 {
print("Usage: " + path.Base(os.Args[0]) + " <unsigned.apk> <private.key> <public.crt>\n")
return
}
var filename = os.Args[1]
var keyfile = os.Args[2]
var crtfile = os.Args[3]
//
var keys []*android.SigningCert = []*android.SigningCert{
{SigningKey: android.SigningKey{
KeyPath: keyfile,
Type: android.RSA,
Hash: android.SHA256,
},
CertPath: crtfile,
},
}
var z *apksign.Zip
var unsapk []byte
var err error
if unsapk, err = loadFile(filename); err != nil {
print("error reading apk.\n")
return
}
if z, err = apksign.NewZip(unsapk); err != nil {
print("error parsing apk: ", err, "\n")
return
}
// Sign
if z, err = z.Sign(keys); err != nil {
print("error signing apk. ", err, "\n")
return
}
if err = z.VerifyV1(); err == nil {
print("v2-signature apk passed v1 verification.\n")
return
}
if err = z.VerifyV2(); err != nil {
print("v2-signature verification failed. (1) ", err, "\n")
return
}
if err = z.Verify(); err != nil {
print("v2-signature verification failed. (2) ", err, "\n")
return
}
if !z.IsAPK || !z.IsV1Signed || !z.IsV2Signed {
print("signed apk was incorrectly characterized. ", z.IsAPK, z.IsV1Signed, z.IsV2Signed, "\n")
return
}
saveFile(filename, z.Bytes())
}