This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.go
56 lines (51 loc) · 1.39 KB
/
tls.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
package main
import (
"crypto/ecdsa"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"fmt"
"math/big"
"net"
"time"
)
const certValidityPeriod = 100 * 365 * 24 * time.Hour // ~100 years
func makeCertificate(key *ecdsa.PrivateKey, addrs []string) (tls.Certificate, error) {
var ips []net.IP
for _, addr := range addrs {
ip, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
return tls.Certificate{}, fmt.Errorf("resolving address from %s: %w", addr, err)
}
ips = append(ips, ip.IP)
}
sn, err := rand.Int(rand.Reader, big.NewInt(1<<62))
if err != nil {
return tls.Certificate{}, err
}
tmpl := &x509.Certificate{
// should always generate the same cert for the same private key
SerialNumber: sn,
Subject: pkix.Name{
Organization: []string{"IPFS"},
CommonName: "127.0.0.1",
},
BasicConstraintsValid: true,
NotBefore: time.Time{},
NotAfter: time.Now().Add(certValidityPeriod),
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
DNSNames: []string{"localhost"},
IPAddresses: ips,
}
certDER, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, key.Public(), key)
if err != nil {
logger.Error(err)
return tls.Certificate{}, err
}
return tls.Certificate{
Certificate: [][]byte{certDER},
PrivateKey: key,
}, nil
}