-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathca.go
More file actions
187 lines (155 loc) · 4.43 KB
/
Copy pathca.go
File metadata and controls
187 lines (155 loc) · 4.43 KB
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
180
181
182
183
184
185
186
187
package groxy
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"os"
"time"
)
const (
defaultCACommonName = "Groxy Local CA"
defaultCAValidFor = 365 * 24 * time.Hour
)
// CA is a certificate authority used to sign per-host certificates for HTTPS
// inspection.
type CA struct {
cert *x509.Certificate
key *rsa.PrivateKey
pem []byte
}
// CAConfig configures local CA generation.
type CAConfig struct {
// CommonName is the certificate common name. If empty, Groxy uses a default.
CommonName string
// ValidFor is how long the generated CA certificate is valid. If zero, Groxy
// uses a default validity period.
ValidFor time.Duration
}
// NewCA creates a new local certificate authority for HTTPS inspection.
func NewCA(config CAConfig) (*CA, error) {
commonName := config.CommonName
if commonName == "" {
commonName = defaultCACommonName
}
validFor := config.ValidFor
if validFor == 0 {
validFor = defaultCAValidFor
}
if validFor < 0 {
return nil, fmt.Errorf("CA validity duration cannot be negative")
}
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, fmt.Errorf("generate CA private key: %w", err)
}
serial, err := randomSerialNumber()
if err != nil {
return nil, err
}
now := time.Now()
template := &x509.Certificate{
SerialNumber: serial,
Subject: pkix.Name{
CommonName: commonName,
},
NotBefore: now.Add(-time.Minute),
NotAfter: now.Add(validFor),
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
BasicConstraintsValid: true,
IsCA: true,
MaxPathLenZero: true,
}
der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
if err != nil {
return nil, fmt.Errorf("create CA certificate: %w", err)
}
cert, err := x509.ParseCertificate(der)
if err != nil {
return nil, fmt.Errorf("parse generated CA certificate: %w", err)
}
return &CA{
cert: cert,
key: key,
pem: pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}),
}, nil
}
// LoadCAFiles loads a CA certificate and RSA private key from PEM files.
func LoadCAFiles(certFile, keyFile string) (*CA, error) {
certPEM, err := os.ReadFile(certFile)
if err != nil {
return nil, fmt.Errorf("read CA certificate file: %w", err)
}
keyPEM, err := os.ReadFile(keyFile)
if err != nil {
return nil, fmt.Errorf("read CA key file: %w", err)
}
cert, err := parseCACertificate(certPEM)
if err != nil {
return nil, err
}
key, err := parseRSAPrivateKey(keyPEM)
if err != nil {
return nil, err
}
return &CA{cert: cert, key: key, pem: certPEM}, nil
}
// WriteFiles writes the CA certificate and private key to PEM files.
func (ca *CA) WriteFiles(certFile, keyFile string) error {
if ca == nil || ca.cert == nil || ca.key == nil {
return fmt.Errorf("CA is not initialized")
}
if err := os.WriteFile(certFile, ca.pem, 0644); err != nil {
return fmt.Errorf("write CA certificate file: %w", err)
}
keyDER := x509.MarshalPKCS1PrivateKey(ca.key)
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: keyDER})
if err := os.WriteFile(keyFile, keyPEM, 0600); err != nil {
return fmt.Errorf("write CA key file: %w", err)
}
return nil
}
func parseCACertificate(certPEM []byte) (*x509.Certificate, error) {
block, _ := pem.Decode(certPEM)
if block == nil || block.Type != "CERTIFICATE" {
return nil, fmt.Errorf("CA certificate PEM block not found")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf("parse CA certificate: %w", err)
}
if !cert.IsCA {
return nil, fmt.Errorf("certificate is not a CA")
}
return cert, nil
}
func parseRSAPrivateKey(keyPEM []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(keyPEM)
if block == nil {
return nil, fmt.Errorf("CA key PEM block not found")
}
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err == nil {
return key, nil
}
parsed, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("parse CA private key: %w", err)
}
rsaKey, ok := parsed.(*rsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("CA private key must be RSA")
}
return rsaKey, nil
}
func randomSerialNumber() (*big.Int, error) {
limit := new(big.Int).Lsh(big.NewInt(1), 128)
serial, err := rand.Int(rand.Reader, limit)
if err != nil {
return nil, fmt.Errorf("generate certificate serial number: %w", err)
}
return serial, nil
}