-
Notifications
You must be signed in to change notification settings - Fork 0
/
cert.go
127 lines (107 loc) · 3.5 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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"os"
"time"
)
const (
keyFileName = "jetbra.key"
certificateFile = "jetbra.pem"
rootCertFile = "root.pem"
powerFile = "power.txt"
)
func generateKeyPair() (*rsa.PrivateKey, *rsa.PublicKey, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return nil, nil, fmt.Errorf("error generating private key: %v", err)
}
return privateKey, &privateKey.PublicKey, nil
}
func createCertificate(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey) ([]byte, error) {
serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 80))
if err != nil {
return nil, fmt.Errorf("failed to generate serial: %v", err)
}
parent := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{CommonName: "JetProfile CA"},
NotBefore: time.Now().Add(-24 * time.Hour),
NotAfter: time.Now().Add(10 * 365 * 24 * time.Hour),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
}
template := parent
template.Subject = pkix.Name{CommonName: "Smallsun"}
return x509.CreateCertificate(rand.Reader, &template, &parent, publicKey, privateKey)
}
func parseCertificate(fileName string) (*x509.Certificate, error) {
certPem, err := os.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("error reading certificate file %s: %v", fileName, err)
}
certBlock, _ := pem.Decode(certPem)
if certBlock == nil {
return nil, fmt.Errorf("failed to decode PEM block from %s", fileName)
}
return x509.ParseCertificate(certBlock.Bytes)
}
func GenerateJetCA() error {
var saveFile = func(fileName, fileType string, bytes []byte) error {
file, err := os.Create(fileName)
defer file.Close()
if err != nil {
return fmt.Errorf("error creating file %s: %v", fileName, err)
}
pemBlock := &pem.Block{
Type: fileType,
Bytes: bytes,
}
if err := pem.Encode(file, pemBlock); err != nil {
return fmt.Errorf("error encoding PEM file %s: %v", fileName, err)
}
return nil
}
privateKey, publicKey, err := generateKeyPair()
if err != nil {
return fmt.Errorf("failed to generate key pair: %v", err)
}
certificate, err := createCertificate(privateKey, publicKey)
if err != nil {
return fmt.Errorf("failed to generate certificate: %v", err)
}
if err := saveFile(keyFileName, "RSA PRIVATE KEY", x509.MarshalPKCS1PrivateKey(privateKey)); err != nil {
return fmt.Errorf("failed to save private key: %v", err)
}
if err := saveFile(certificateFile, "CERTIFICATE", certificate); err != nil {
return fmt.Errorf("failed to save certificate: %v", err)
}
fmt.Println("Certificate and private key generated successfully!")
return nil
}
func GeneratePowerResult() error {
rootCert, err := parseCertificate(rootCertFile)
if err != nil {
return err
}
cert, err := parseCertificate(certificateFile)
if err != nil {
return err
}
x := new(big.Int).SetBytes(cert.Signature) // 证书的签名密文
y := rootCert.PublicKey.(*rsa.PublicKey).E // 证书指数
z := rootCert.PublicKey.(*rsa.PublicKey).N // 内置根证书的公钥
r := new(big.Int)
r.Exp(x, big.NewInt(int64(y)), cert.PublicKey.(*rsa.PublicKey).N)
output := fmt.Sprintf("EQUAL,%d,%d,%d->%d", x, y, z, r)
if err := os.WriteFile(powerFile, []byte(output), 0644); err != nil {
return fmt.Errorf("failed to save power: %v", err)
}
fmt.Println("Power generated successfully!")
return nil
}