This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
util.go
91 lines (79 loc) · 2.46 KB
/
util.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
/* Copyright (c) Edgeless Systems GmbH
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
package db
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"net"
"time"
"github.com/edgelesssys/edgelessdb/edb/util"
)
func splitHostPort(address, defaultPort string) (host, port string) {
if address == "" {
return "0.0.0.0", defaultPort
}
host, port, err := net.SplitHostPort(address)
if err != nil {
return address, defaultPort
}
if host == "" {
host = "0.0.0.0"
}
if port == "" {
port = defaultPort
}
return
}
func toPEM(cert []byte, key crypto.PrivateKey) (pemCert, pemKey []byte, err error) {
pemCert = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert})
if len(pemCert) <= 0 {
return nil, nil, errors.New("pem.EncodeToMemory failed")
}
keyBytes, err := x509.MarshalPKCS8PrivateKey(key)
if err != nil {
return nil, nil, err
}
pemKey = pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyBytes})
if len(pemKey) <= 0 {
return nil, nil, errors.New("pem.EncodeToMemory failed")
}
return
}
func createCertificate(dnsName string) ([]byte, crypto.PrivateKey, error) {
serialNumber, err := util.GenerateCertificateSerialNumber()
if err != nil {
return nil, nil, err
}
template := &x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{Organization: []string{"EDB root"}, CommonName: dnsName},
NotAfter: time.Now().AddDate(10, 0, 0),
DNSNames: []string{dnsName},
BasicConstraintsValid: true,
IsCA: true,
}
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, nil, err
}
cert, err := x509.CreateCertificate(rand.Reader, template, template, &priv.PublicKey, priv)
if err != nil {
return nil, nil, err
}
return cert, priv, nil
}