Skip to content

Commit 89961b9

Browse files
committed
feat: add self_signed_certificates for client net test
Signed-off-by: Minh Huy Tran <[email protected]>
1 parent 99867bb commit 89961b9

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

Diff for: client/test/setup.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func MakeNetRoleSetups(t *testing.T, rng *rand.Rand, s *ethctest.Setup, names []
7979
setups := make([]clienttest.RoleSetup, len(names))
8080
commonName := "127.0.0.1"
8181
sans := []string{"127.0.0.1", "localhost"}
82-
tlsConfigs, err := simple.GenerateSelfSignedCertConfigs(commonName, sans, len(names))
82+
tlsConfigs, err := GenerateSelfSignedCertConfigs(commonName, sans, len(names))
8383
if err != nil {
8484
panic("Error generating TLS configs: " + err.Error())
8585
}

Diff for: client/test/tls.go

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright 2022 - See NOTICE file for copyright holders.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package test
16+
17+
import (
18+
"crypto/rand"
19+
"crypto/rsa"
20+
"crypto/tls"
21+
"crypto/x509"
22+
"crypto/x509/pkix"
23+
"encoding/pem"
24+
"fmt"
25+
"math/big"
26+
"net"
27+
"time"
28+
)
29+
30+
// GenerateSelfSignedCertConfigs generates self-signed certificates and returns
31+
// a list of TLS configurations for n clients.
32+
func GenerateSelfSignedCertConfigs(commonName string, sans []string, numClients int) ([]*tls.Config, error) {
33+
keySize := 2048
34+
configs := make([]*tls.Config, numClients)
35+
certPEMs := make([][]byte, numClients)
36+
tlsCerts := make([]tls.Certificate, numClients)
37+
38+
for i := 0; i < numClients; i++ {
39+
// Private key for the client
40+
privateKey, err := rsa.GenerateKey(rand.Reader, keySize)
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
// Create a certificate template
46+
template := x509.Certificate{
47+
SerialNumber: big.NewInt(int64(i) + 1),
48+
Subject: pkix.Name{
49+
Organization: []string{"Perun Network"},
50+
CommonName: fmt.Sprintf("%s-client-%d", commonName, i+1),
51+
},
52+
NotBefore: time.Now(),
53+
NotAfter: time.Now().Add(24 * time.Hour),
54+
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
55+
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
56+
BasicConstraintsValid: true,
57+
}
58+
59+
// Add SANs to the server certificate template
60+
for _, san := range sans {
61+
if ip := net.ParseIP(san); ip != nil {
62+
template.IPAddresses = append(template.IPAddresses, ip)
63+
} else {
64+
template.DNSNames = append(template.DNSNames, san)
65+
}
66+
}
67+
68+
// Generate a self-signed server certificate
69+
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey)
70+
if err != nil {
71+
return nil, err
72+
}
73+
74+
// Encode the server certificate to PEM format
75+
certPEMs[i] = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
76+
77+
// Encode the server private key to PEM format
78+
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)})
79+
80+
// Create a tls.Certificate object for the server
81+
tlsCerts[i], err = tls.X509KeyPair(certPEMs[i], keyPEM)
82+
if err != nil {
83+
return nil, err
84+
}
85+
}
86+
87+
for i := 0; i < numClients; i++ {
88+
certPool := x509.NewCertPool()
89+
for j := 0; j < numClients; j++ {
90+
ok := certPool.AppendCertsFromPEM(certPEMs[j])
91+
if !ok {
92+
return nil, fmt.Errorf("failed to parse root certificate")
93+
}
94+
}
95+
96+
// Create the server-side TLS configuration
97+
configs[i] = &tls.Config{
98+
RootCAs: certPool,
99+
ClientCAs: certPool,
100+
Certificates: []tls.Certificate{tlsCerts[i]},
101+
ClientAuth: tls.RequireAndVerifyClientCert,
102+
MinVersion: tls.VersionTLS12, // Set minimum TLS version to TLS 1.2
103+
}
104+
}
105+
106+
return configs, nil
107+
}

0 commit comments

Comments
 (0)