-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrypto.go
67 lines (60 loc) · 1.84 KB
/
crypto.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
package chat
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"log"
"math/big"
"time"
)
func makeSelfSignedCert() (*tls.Certificate, error) {
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, err
}
cert := &x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{Organization: []string{"Chat Self-Signed Dev Cert"}},
NotBefore: time.Now(),
NotAfter: time.Now().Add(24 * time.Hour * 31), // a month-ish
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
}
// ecdsa256 has a constant-time assembly implementation to prevent timing attacks
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
derBytes, err := x509.CreateCertificate(rand.Reader, cert, cert, &privateKey.PublicKey, privateKey)
return &tls.Certificate{
Certificate: [][]byte{derBytes},
PrivateKey: privateKey,
Leaf: cert,
}, err
}
// DefaultTLSConfig is the default config used for serving content with TLS,
// such as in the HTTPS server.
func DefaultTLSConfig() *tls.Config {
cert, err := makeSelfSignedCert()
if err != nil {
// You'll and obvious error if the nil config is returned, so for simplicity
// sake, just return nil here. In a real app, this would be a horrible idea.
log.Printf("Unable to generate a self signed cert: %s\n", err.Error())
return nil
}
pool := x509.NewCertPool()
pool.AddCert(cert.Leaf)
return &tls.Config{
MinVersion: tls.VersionTLS12,
PreferServerCipherSuites: true,
CurvePreferences: []tls.CurveID{
tls.CurveP256,
},
Certificates: []tls.Certificate{*cert},
ClientCAs: pool,
}
}