-
Notifications
You must be signed in to change notification settings - Fork 12
/
util.go
179 lines (152 loc) · 5.31 KB
/
util.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
176
177
178
179
package certificateutil
import (
"bufio"
"crypto/x509"
"encoding/pem"
"fmt"
"regexp"
"strings"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/pkcs12"
"github.com/pkg/errors"
)
func commandError(printableCmd string, cmdOut string, cmdErr error) error {
return errors.Wrapf(cmdErr, "%s failed, out: %s", printableCmd, cmdOut)
}
// CertificatesFromPKCS12Content returns an array of CertificateInfoModel
// Used to parse p12 file containing multiple codesign identities (exported from macOS Keychain)
func CertificatesFromPKCS12Content(content []byte, password string) ([]CertificateInfoModel, error) {
certificates, privateKeys, err := pkcs12.DecodeAll(content, password)
if err != nil {
return nil, err
}
if len(certificates) != len(privateKeys) {
return nil, errors.New("pkcs12: different number of certificates and private keys found")
}
if len(certificates) == 0 {
return nil, errors.New("pkcs12: no certificate and private key pair found")
}
infos := []CertificateInfoModel{}
for i, certificate := range certificates {
if certificate != nil {
infos = append(infos, NewCertificateInfo(*certificate, privateKeys[i]))
}
}
return infos, nil
}
// CertificatesFromPKCS12File ...
func CertificatesFromPKCS12File(pkcs12Pth, password string) ([]CertificateInfoModel, error) {
content, err := fileutil.ReadBytesFromFile(pkcs12Pth)
if err != nil {
return nil, err
}
return CertificatesFromPKCS12Content(content, password)
}
// CertificateFromDERContent ...
func CertificateFromDERContent(content []byte) (*x509.Certificate, error) {
return x509.ParseCertificate(content)
}
// CeritifcateFromPemContent ...
func CeritifcateFromPemContent(content []byte) (*x509.Certificate, error) {
block, _ := pem.Decode(content)
if block == nil || block.Bytes == nil || len(block.Bytes) == 0 {
return nil, fmt.Errorf("failed to parse profile from: %s", string(content))
}
return CertificateFromDERContent(block.Bytes)
}
func installedCodesigningCertificateNamesFromOutput(out string) ([]string, error) {
pettern := `^[0-9]+\) (?P<hash>.*) "(?P<name>.*)"`
re := regexp.MustCompile(pettern)
certificateNameMap := map[string]bool{}
scanner := bufio.NewScanner(strings.NewReader(out))
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if matches := re.FindStringSubmatch(line); len(matches) == 3 {
name := matches[2]
certificateNameMap[name] = true
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
names := []string{}
for name := range certificateNameMap {
names = append(names, name)
}
return names, nil
}
// InstalledCodesigningCertificateNames ...
func InstalledCodesigningCertificateNames() ([]string, error) {
cmd := command.New("security", "find-identity", "-v", "-p", "codesigning")
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
if err != nil {
return nil, commandError(cmd.PrintableCommandArgs(), out, err)
}
return installedCodesigningCertificateNamesFromOutput(out)
}
// InstalledMacAppStoreCertificateNames ...
func InstalledMacAppStoreCertificateNames() ([]string, error) {
cmd := command.New("security", "find-identity", "-v", "-p", "macappstore")
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
if err != nil {
return nil, commandError(cmd.PrintableCommandArgs(), out, err)
}
return installedCodesigningCertificateNamesFromOutput(out)
}
func normalizeFindCertificateOut(out string) ([]string, error) {
certificateContents := []string{}
pattern := `(?s)(-----BEGIN CERTIFICATE-----.*?-----END CERTIFICATE-----)`
matches := regexp.MustCompile(pattern).FindAllString(out, -1)
if len(matches) == 0 {
return nil, fmt.Errorf("no certificates found in: %s", out)
}
for _, certificateContent := range matches {
if !strings.HasPrefix(certificateContent, "\n") {
certificateContent = "\n" + certificateContent
}
if !strings.HasSuffix(certificateContent, "\n") {
certificateContent = certificateContent + "\n"
}
certificateContents = append(certificateContents, certificateContent)
}
return certificateContents, nil
}
// InstalledCodesigningCertificates ...
func InstalledCodesigningCertificates() ([]*x509.Certificate, error) {
certificateNames, err := InstalledCodesigningCertificateNames()
if err != nil {
return nil, err
}
return getInstalledCertificatesByNameSlice(certificateNames)
}
// InstalledMacAppStoreCertificates ...
func InstalledMacAppStoreCertificates() ([]*x509.Certificate, error) {
certificateNames, err := InstalledMacAppStoreCertificateNames()
if err != nil {
return nil, err
}
return getInstalledCertificatesByNameSlice(certificateNames)
}
func getInstalledCertificatesByNameSlice(certificateNames []string) ([]*x509.Certificate, error) {
certificates := []*x509.Certificate{}
for _, name := range certificateNames {
cmd := command.New("security", "find-certificate", "-c", name, "-p", "-a")
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
if err != nil {
return nil, commandError(cmd.PrintableCommandArgs(), out, err)
}
normalizedOuts, err := normalizeFindCertificateOut(out)
if err != nil {
return nil, err
}
for _, normalizedOut := range normalizedOuts {
certificate, err := CeritifcateFromPemContent([]byte(normalizedOut))
if err != nil {
return nil, err
}
certificates = append(certificates, certificate)
}
}
return certificates, nil
}