Skip to content

Commit 3ea7af7

Browse files
committed
Implemented better certificate generation practices.
1 parent c3f2b14 commit 3ea7af7

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

server/genCert.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@ import (
55
"crypto/ecdsa"
66
"crypto/elliptic"
77
"crypto/rand"
8+
"crypto/sha1"
89
"crypto/tls"
910
"crypto/x509"
1011
"crypto/x509/pkix"
1112
"encoding/pem"
1213
"math/big"
14+
"net"
1315
"time"
1416
)
1517

1618
// Generate a self-signed certificate as long as the server is running.
1719
func serial_number() *big.Int {
18-
serial_num, serial_err := rand.Int(rand.Reader, big.NewInt(9223372036854775807))
20+
serialNumLimit := new(big.Int).Lsh(big.NewInt(1), 128)
21+
serial_num, serial_err := rand.Int(rand.Reader, serialNumLimit)
1922
if serial_err != nil {
2023
return big.NewInt(time.Now().UnixNano())
2124
}
@@ -30,6 +33,8 @@ func gen_cert() (*tls.Config, error) {
3033
Organization: []string{"NVDARemote Server"},
3134
CommonName: "Root CA",
3235
},
36+
DNSNames: []string{"localhost"},
37+
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
3338
NotBefore: time.Now().Add(-10 * time.Second),
3439
NotAfter: time.Now().AddDate(10, 0, 0),
3540
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign | x509.KeyUsageDigitalSignature,
@@ -41,6 +46,10 @@ func gen_cert() (*tls.Config, error) {
4146
if err != nil {
4247
return nil, err
4348
}
49+
pubKeyBytes, _ := x509.MarshalPKIXPublicKey(&priv.PublicKey)
50+
keyID := sha1.Sum(pubKeyBytes)
51+
ca.SubjectKeyId = keyID[:]
52+
ca.AuthorityKeyId = keyID[:]
4453
caBytes, cerr := x509.CreateCertificate(rand.Reader, ca, ca, &priv.PublicKey, priv)
4554
if cerr != nil {
4655
return nil, cerr
@@ -62,7 +71,7 @@ func gen_cert() (*tls.Config, error) {
6271

6372
certPrivKeyPEM := new(bytes.Buffer)
6473
err = pem.Encode(certPrivKeyPEM, &pem.Block{
65-
Type: "EC PRIVATE KEY",
74+
Type: "PRIVATE KEY",
6675
Bytes: mpk,
6776
})
6877
if err != nil {

0 commit comments

Comments
 (0)