forked from open-horizon/edge-sync-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificates.go
127 lines (108 loc) · 3.29 KB
/
certificates.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package base
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"net"
"os"
"time"
"github.com/open-horizon/edge-utilities/logger"
"github.com/open-horizon/edge-utilities/logger/log"
"github.com/open-horizon/edge-sync-service/common"
)
const (
certDir = "sync/certs/"
certName = "cert.pem"
keyName = "key.pem"
rsaBits = 2048
daysValidFor = 500
)
func setupCertificates() error {
if common.Configuration.NodeType == common.ESS && common.ServingAPIs {
_, err := tls.X509KeyPair([]byte(common.Configuration.ServerCertificate), []byte(common.Configuration.ServerKey))
if err == nil {
// common.Configuration.ServerCertificate) and common.Configuration.ServerKey are pem format strings
return nil
}
common.Configuration.ServerCertificate = certDir + certName
common.Configuration.ServerKey = certDir + keyName
certFile := common.Configuration.PersistenceRootPath + certDir + certName
keyFile := common.Configuration.PersistenceRootPath + certDir + keyName
info, err := os.Stat(certFile)
if err == nil {
if info.IsDir() {
return &common.InvalidRequest{Message: fmt.Sprintf("%s is a directory", certFile)}
}
return nil
} else if err != nil && !os.IsNotExist(err) {
return err
}
if err = os.MkdirAll(common.Configuration.PersistenceRootPath+certDir, 0750); err != nil {
return nil
}
notBefore := time.Now()
notAfter := notBefore.Add(daysValidFor * 24 * time.Hour)
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return err
}
priv, err := rsa.GenerateKey(rand.Reader, rsaBits)
if err != nil {
return err
}
template := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"SomeOrg"},
OrganizationalUnit: []string{"Edge Node"},
CommonName: "localhost",
},
NotBefore: notBefore,
NotAfter: notAfter,
IsCA: true,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
DNSNames: []string{"edge-sync-service", "localhost"},
IPAddresses: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("::1")},
}
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
if err != nil {
return err
}
certOut, err := os.Create(certFile)
if err != nil {
return err
}
err = pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
if closeErr := certOut.Close(); closeErr != nil {
return closeErr
}
if err != nil {
// Close succeeded
return err
}
keyOut, err := os.OpenFile(keyFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
err = pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
if closeErr := keyOut.Close(); closeErr != nil {
return closeErr
}
if err != nil {
// Close succeeded
return err
}
if log.IsLogging(logger.INFO) {
log.Info("Created server certificate at %s\n", certFile)
}
}
return nil
}