-
Notifications
You must be signed in to change notification settings - Fork 0
/
cert.go
140 lines (120 loc) · 3.98 KB
/
cert.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
// Copyright (c) 2024 homuler
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
package mitm
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"time"
)
var serialNumberLimit = new(big.Int).Lsh(big.NewInt(1), 128)
// ForgeCertificate creates a new [tls.Certificate] that looks like the original certificate but signed by the specified root.
func ForgeCertificate(root *tls.Certificate, orig *x509.Certificate) (tls.Certificate, error) {
tmpl, err := createTemplate(orig)
if err != nil {
return tls.Certificate{}, err
}
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return tls.Certificate{}, fmt.Errorf("failed to generate an RSA key: %w", err)
}
if root.Leaf == nil {
root.Leaf, err = x509.ParseCertificate(root.Certificate[0])
if err != nil {
return tls.Certificate{}, fmt.Errorf("failed to parse root certificate: %w", err)
}
}
certDer, err := x509.CreateCertificate(rand.Reader, tmpl, root.Leaf, &key.PublicKey, root.PrivateKey)
if err != nil {
return tls.Certificate{}, fmt.Errorf("failed to create a certificate of %s: %w", orig.Subject.CommonName, err)
}
return tls.Certificate{
Certificate: [][]byte{certDer, root.Certificate[0]},
PrivateKey: key,
}, nil
}
func createTemplate(orig *x509.Certificate) (*x509.Certificate, error) {
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, fmt.Errorf("failed to generate the serial number: %w", err)
}
tmpl := x509.Certificate{
SerialNumber: serialNumber,
Subject: orig.Subject,
SignatureAlgorithm: x509.SHA256WithRSA,
NotBefore: orig.NotBefore,
NotAfter: orig.NotAfter,
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IPAddresses: orig.IPAddresses,
DNSNames: orig.DNSNames,
}
return &tmpl, nil
}
// LoadCertificate loads a certificate from the specified files.
// The returned certificate has the Leaf field set.
func LoadCertificate(certPath, keyPath string) (cert tls.Certificate, err error) {
cert, err = tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return
}
err = loadLeaf(&cert)
return
}
func loadLeaf(cert *tls.Certificate) (err error) {
cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0])
return
}
func CreateCACertPEM(subject pkix.Name, duration time.Duration) (*bytes.Buffer, *bytes.Buffer, error) {
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, nil, fmt.Errorf("failed to generate the serial number: %w", err)
}
notBefore := time.Now()
caCert := &x509.Certificate{
SerialNumber: serialNumber,
Subject: subject,
NotBefore: notBefore,
NotAfter: notBefore.Add(duration),
IsCA: true,
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
BasicConstraintsValid: true,
}
caPrivKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return nil, nil, fmt.Errorf("faild to generate the private key: %w", err)
}
certDer, err := x509.CreateCertificate(rand.Reader, caCert, caCert, &caPrivKey.PublicKey, caPrivKey)
if err != nil {
return nil, nil, err
}
// pem encode
caPEM := new(bytes.Buffer)
pem.Encode(caPEM, &pem.Block{
Type: "CERTIFICATE",
Bytes: certDer,
})
caPrivKeyPEM := new(bytes.Buffer)
pem.Encode(caPrivKeyPEM, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(caPrivKey),
})
return caPEM, caPrivKeyPEM, nil
}
func CreateCACert(subject pkix.Name, duration time.Duration) (cert tls.Certificate, err error) {
caPEM, caPrivKeyPEM, err := CreateCACertPEM(subject, duration)
if err != nil {
return tls.Certificate{}, err
}
return tls.X509KeyPair(caPEM.Bytes(), caPrivKeyPEM.Bytes())
}