-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcert.go
50 lines (45 loc) · 1.12 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
package godot_web
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"fmt"
"math/big"
"time"
)
func generateSelfSignedCertificate(commonName string) (tls.Certificate, error) {
now := time.Now()
cert := &x509.Certificate{
SerialNumber: big.NewInt(now.Unix()),
Subject: pkix.Name{
CommonName: commonName,
Organization: []string{"godot_web"},
},
NotBefore: now,
NotAfter: now.AddDate(1, 0, 0), // +1 year
BasicConstraintsValid: true,
IsCA: true,
ExtKeyUsage: []x509.ExtKeyUsage{
x509.ExtKeyUsageServerAuth,
},
KeyUsage: x509.KeyUsageCertSign |
x509.KeyUsageDigitalSignature |
x509.KeyUsageKeyEncipherment,
}
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return tls.Certificate{}, fmt.Errorf("error generating RSA key: %v", err)
}
signed, err := x509.CreateCertificate(rand.Reader, cert, cert, key.Public(), key)
if err != nil {
return tls.Certificate{}, fmt.Errorf("error signing certificate: %v", err)
}
return tls.Certificate{
Certificate: [][]byte{
signed,
},
PrivateKey: key,
}, nil
}