-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.go
82 lines (70 loc) · 1.82 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
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
package worker
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"time"
)
func (m *Worker) getTLSCertificate() (_ tls.Certificate, e error) {
// TODO
// if gCli.String("swarm-master-custom-ca") != ""
// get key, get pub, return
var cbytes, kbytes []byte
if cbytes, kbytes, e = m.createPublicPrivatePair(); e != nil {
return
}
return tls.X509KeyPair(cbytes, kbytes)
}
func (*Worker) createPublicPrivatePair() (_, _ []byte, e error) {
cert := &x509.Certificate{
SerialNumber: big.NewInt(2019),
Subject: pkix.Name{
Organization: []string{"Company, INC."},
Country: []string{"US"},
Province: []string{""},
Locality: []string{"San Francisco"},
StreetAddress: []string{"Golden Gate Bridge"},
PostalCode: []string{"94016"},
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
IsCA: true,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
}
priv, e := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if e != nil {
return
}
certBytes, e := x509.CreateCertificate(rand.Reader, cert, cert, &priv.PublicKey, priv)
if e != nil {
return
}
var cbuf = new(bytes.Buffer)
pem.Encode(cbuf, &pem.Block{
Type: "CERTIFICATE",
Bytes: certBytes,
})
var p []byte
if p, e = x509.MarshalECPrivateKey(priv); e != nil {
return
}
var pbuf = new(bytes.Buffer)
pem.Encode(pbuf, &pem.Block{
Type: "EC PRIVATE KEY",
Bytes: p,
})
if gCli.Bool("http-debug") {
fmt.Println("\n" + cbuf.String())
fmt.Println("\n" + pbuf.String())
}
return cbuf.Bytes(), pbuf.Bytes(), nil
}