-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ca.go
149 lines (127 loc) · 4.41 KB
/
ca.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
package certs
/*
Sliver Implant Framework
Copyright (C) 2019 Bishop Fox
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import (
"crypto/ecdsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"path"
"github.com/bishopfox/sliver/server/assets"
)
// -----------------------
// CERTIFICATE AUTHORITY
// -----------------------
// SetupCAs - Creates directories for certs
func SetupCAs() {
GenerateCertificateAuthority(C2ServerCA, "")
GenerateCertificateAuthority(ImplantCA, "")
GenerateCertificateAuthority(OperatorCA, "operators")
GenerateCertificateAuthority(HTTPSCA, "")
}
func getCertDir() string {
rootDir := assets.GetRootAppDir()
certDir := path.Join(rootDir, "certs")
if _, err := os.Stat(certDir); os.IsNotExist(err) {
err := os.MkdirAll(certDir, 0700)
if err != nil {
certsLog.Fatalf("Failed to create cert dir %s", err)
}
}
return certDir
}
// GenerateCertificateAuthority - Creates a new CA cert for a given type
func GenerateCertificateAuthority(caType string, commonName string) (*x509.Certificate, *ecdsa.PrivateKey) {
storageDir := getCertDir()
certFilePath := path.Join(storageDir, fmt.Sprintf("%s-ca-cert.pem", caType))
if _, err := os.Stat(certFilePath); os.IsNotExist(err) {
certsLog.Infof("Generating certificate authority for '%s'", caType)
cert, key := GenerateECCCertificate(caType, commonName, true, false)
SaveCertificateAuthority(caType, cert, key)
}
cert, key, err := GetCertificateAuthority(caType)
if err != nil {
certsLog.Fatalf("Failed to load CA %s", err)
}
return cert, key
}
// GetCertificateAuthority - Get the current CA certificate
func GetCertificateAuthority(caType string) (*x509.Certificate, *ecdsa.PrivateKey, error) {
certPEM, keyPEM, err := GetCertificateAuthorityPEM(caType)
if err != nil {
return nil, nil, err
}
certBlock, _ := pem.Decode(certPEM)
if certBlock == nil {
certsLog.Error("Failed to parse certificate PEM")
return nil, nil, err
}
cert, err := x509.ParseCertificate(certBlock.Bytes)
if err != nil {
certsLog.Error("Failed to parse certificate: " + err.Error())
return nil, nil, err
}
keyBlock, _ := pem.Decode(keyPEM)
if keyBlock == nil {
certsLog.Error("Failed to parse certificate PEM")
return nil, nil, err
}
key, err := x509.ParseECPrivateKey(keyBlock.Bytes)
if err != nil {
certsLog.Error(err)
return nil, nil, err
}
return cert, key, nil
}
// GetCertificateAuthorityPEM - Get PEM encoded CA cert/key
func GetCertificateAuthorityPEM(caType string) ([]byte, []byte, error) {
caType = path.Base(caType)
caCertPath := path.Join(getCertDir(), fmt.Sprintf("%s-ca-cert.pem", caType))
caKeyPath := path.Join(getCertDir(), fmt.Sprintf("%s-ca-key.pem", caType))
certPEM, err := ioutil.ReadFile(caCertPath)
if err != nil {
certsLog.Error(err)
return nil, nil, err
}
keyPEM, err := ioutil.ReadFile(caKeyPath)
if err != nil {
certsLog.Error(err)
return nil, nil, err
}
return certPEM, keyPEM, nil
}
// SaveCertificateAuthority - Save the certificate and the key to the filesystem
// doesn't return an error because errors are fatal. If we can't generate CAs,
// then we can't secure comms and we should die a horrible death.
func SaveCertificateAuthority(caType string, cert []byte, key []byte) {
storageDir := getCertDir()
if _, err := os.Stat(storageDir); os.IsNotExist(err) {
os.MkdirAll(storageDir, 0700)
}
// CAs get written to the filesystem since we control the names and makes them
// easier to move around/backup
certFilePath := path.Join(storageDir, fmt.Sprintf("%s-ca-cert.pem", caType))
keyFilePath := path.Join(storageDir, fmt.Sprintf("%s-ca-key.pem", caType))
err := ioutil.WriteFile(certFilePath, cert, 0600)
if err != nil {
certsLog.Fatalf("Failed write certificate data to: %s", certFilePath)
}
err = ioutil.WriteFile(keyFilePath, key, 0600)
if err != nil {
certsLog.Fatalf("Failed write certificate data to: %s", keyFilePath)
}
}