-
Notifications
You must be signed in to change notification settings - Fork 0
/
x509.go
63 lines (53 loc) · 1.8 KB
/
x509.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
package certificate
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"math/big"
"net"
"time"
)
const (
x509CertificatePrivateKeyBits = 2048
x509CertificateExpiryInYears = 2
x509CertificateSerialNumber = 1
x509CertificateOrganization = "GitLab Runner"
)
type X509Generator struct{}
func (c X509Generator) Generate(host string) (tls.Certificate, []byte, error) {
priv, err := rsa.GenerateKey(rand.Reader, x509CertificatePrivateKeyBits)
if err != nil {
return tls.Certificate{}, []byte{}, err
}
template := x509.Certificate{
SerialNumber: big.NewInt(x509CertificateSerialNumber),
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(x509CertificateExpiryInYears, 0, 0),
Subject: pkix.Name{
Organization: []string{x509CertificateOrganization},
},
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageDataEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
if ip := net.ParseIP(host); ip != nil {
template.IPAddresses = append(template.IPAddresses, ip)
} else {
template.DNSNames = append(template.DNSNames, host)
}
publicKeyBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, priv.Public(), priv)
if err != nil {
return tls.Certificate{}, []byte{}, errors.New("failed to create certificate")
}
publicKeyPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: publicKeyBytes})
privateKeyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
parsedCertificate, err := tls.X509KeyPair(publicKeyPEM, privateKeyPEM)
if err != nil {
return tls.Certificate{}, []byte{}, err
}
return parsedCertificate, publicKeyPEM, nil
}