forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
156 lines (133 loc) · 3.35 KB
/
utils.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
package crypto
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/binary"
"encoding/hex"
"encoding/pem"
"fmt"
errors "github.com/pkg/errors"
"www.velocidex.com/golang/velociraptor/config"
)
func parseRsaPrivateKeyFromPemStr(pem_str []byte) (*rsa.PrivateKey, error) {
for {
block, rest := pem.Decode(pem_str)
if block == nil {
return nil, errors.New("failed to parse PEM block containing the key")
}
if block.Type == "RSA PRIVATE KEY" {
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, errors.WithStack(err)
}
return priv, nil
}
pem_str = rest
}
}
func parseX509CertFromPemStr(pem_str []byte) (*x509.Certificate, error) {
for {
block, rest := pem.Decode(pem_str)
if block == nil {
return nil, errors.New("Unable to parse PEM")
}
if block.Type == "CERTIFICATE" {
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, errors.WithStack(err)
}
return cert, nil
}
pem_str = rest
}
}
func parseX509CSRFromPemStr(pem_str []byte) (*x509.CertificateRequest, error) {
for {
block, rest := pem.Decode(pem_str)
if block == nil {
return nil, errors.New("Unable to parse PEM")
}
if block.Type == "CERTIFICATE REQUEST" {
csr, err := x509.ParseCertificateRequest(block.Bytes)
if err != nil {
return nil, errors.WithStack(err)
}
return csr, nil
}
pem_str = rest
}
}
/* GRR Client IDs are derived from the public key of the client.
This makes it impossible to impersonate a client unless one has the
client's corresponding private key.
*/
func ClientIDFromPublicKey(public_key *rsa.PublicKey) string {
raw_n := public_key.N.Bytes()
result := make([]byte, 4+1+len(raw_n))
binary.BigEndian.PutUint32(result[0:], uint32(len(raw_n)+1))
copy(result[5:], raw_n)
hashed := sha256.Sum256(result)
dst := make([]byte, hex.EncodedLen(8))
hex.Encode(dst, hashed[:8])
return "C." + string(dst)
}
func GeneratePrivateKey() ([]byte, error) {
reader := rand.Reader
bitSize := 2048
key, err := rsa.GenerateKey(reader, bitSize)
if err != nil {
return nil, errors.WithStack(err)
}
pemdata := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
},
)
return pemdata, nil
}
func PublicKeyToPem(key *rsa.PublicKey) []byte {
return pem.EncodeToMemory(
&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: x509.MarshalPKCS1PublicKey(key),
},
)
}
func PemToPublicKey(pem_str []byte) (*rsa.PublicKey, error) {
for {
block, rest := pem.Decode(pem_str)
if block == nil {
return nil, errors.New("failed to parse PEM block containing the key")
}
if block.Type == "RSA PUBLIC KEY" {
pub, err := x509.ParsePKCS1PublicKey(block.Bytes)
if err != nil {
return nil, errors.WithStack(err)
}
return pub, nil
}
pem_str = rest
}
}
// Verify the configuration, possibly updating default settings.
func VerifyConfig(config_obj *config.Config) error {
if len(config_obj.Client.ServerUrls) == 0 {
return errors.New("No server URLs configured!")
}
if config_obj.Writeback.PrivateKey == "" {
fmt.Println("Genering new private key....")
pem, err := GeneratePrivateKey()
if err != nil {
return errors.WithStack(err)
}
config_obj.Writeback.PrivateKey = string(pem)
err = config.UpdateWriteback(config_obj)
if err != nil {
return err
}
}
return nil
}