11package main
22
33import (
4+ "crypto/tls"
5+ "crypto/x509"
46 ocpp16 "github.com/lorenzodonini/ocpp-go/ocpp1.6"
57 "github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
68 "github.com/lorenzodonini/ocpp-go/ocpp1.6/firmware"
@@ -10,6 +12,7 @@ import (
1012 "github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
1113 "github.com/lorenzodonini/ocpp-go/ws"
1214 log "github.com/sirupsen/logrus"
15+ "io/ioutil"
1316 "os"
1417 "strconv"
1518 "time"
@@ -20,6 +23,7 @@ const (
2023 defaultHeartbeatInterval = 600
2124 envVarServerPort = "SERVER_LISTEN_PORT"
2225 envVarTls = "TLS_ENABLED"
26+ envVarCaCertificate = "CA_CERTIFICATE_PATH"
2327 envVarServerCertificate = "SERVER_CERTIFICATE_PATH"
2428 envVarServerCertificateKey = "SERVER_CERTIFICATE_KEY_PATH"
2529)
@@ -31,6 +35,27 @@ func setupCentralSystem() ocpp16.CentralSystem {
3135}
3236
3337func setupTlsCentralSystem () ocpp16.CentralSystem {
38+ var certPool * x509.CertPool
39+ // Load CA certificates
40+ caCertificate , ok := os .LookupEnv (envVarCaCertificate )
41+ if ! ok {
42+ log .Infof ("no %v found, using system CA pool" , envVarCaCertificate )
43+ systemPool , err := x509 .SystemCertPool ()
44+ if err != nil {
45+ log .Fatalf ("couldn't get system CA pool: %v" , err )
46+ }
47+ certPool = systemPool
48+ } else {
49+ certPool = x509 .NewCertPool ()
50+ data , err := ioutil .ReadFile (caCertificate )
51+ if err != nil {
52+ log .Fatalf ("couldn't read CA certificate from %v: %v" , caCertificate , err )
53+ }
54+ ok = certPool .AppendCertsFromPEM (data )
55+ if ! ok {
56+ log .Fatalf ("couldn't read CA certificate from %v" , caCertificate )
57+ }
58+ }
3459 certificate , ok := os .LookupEnv (envVarServerCertificate )
3560 if ! ok {
3661 log .Fatalf ("no required %v found" , envVarServerCertificate )
@@ -39,7 +64,10 @@ func setupTlsCentralSystem() ocpp16.CentralSystem {
3964 if ! ok {
4065 log .Fatalf ("no required %v found" , envVarServerCertificateKey )
4166 }
42- server := ws .NewTLSServer (certificate , key )
67+ server := ws .NewTLSServer (certificate , key , & tls.Config {
68+ ClientAuth : tls .RequireAndVerifyClientCert ,
69+ ClientCAs : certPool ,
70+ })
4371 return ocpp16 .NewCentralSystem (nil , server )
4472}
4573
@@ -57,7 +85,7 @@ func exampleRoutine(chargePointID string, handler *CentralSystemHandler) {
5785 logDefault (chargePointID , confirmation .GetFeatureName ()).Warn (err )
5886 } else if confirmation .Status == reservation .ReservationStatusAccepted {
5987 logDefault (chargePointID , confirmation .GetFeatureName ()).Infof ("connector %v reserved for client %v until %v (reservation ID %d)" , connectorID , clientIdTag , expiryDate .FormatTimestamp (), reservationID )
60- } else {
88+ } else {
6189 logDefault (chargePointID , confirmation .GetFeatureName ()).Infof ("couldn't reserve connector %v: %v" , connectorID , confirmation .Status )
6290 }
6391 }
@@ -74,7 +102,7 @@ func exampleRoutine(chargePointID string, handler *CentralSystemHandler) {
74102 logDefault (chargePointID , confirmation .GetFeatureName ()).Warn (err )
75103 } else if confirmation .Status == reservation .CancelReservationStatusAccepted {
76104 logDefault (chargePointID , confirmation .GetFeatureName ()).Infof ("reservation %v canceled successfully" , reservationID )
77- } else {
105+ } else {
78106 logDefault (chargePointID , confirmation .GetFeatureName ()).Infof ("couldn't cancel reservation %v" , reservationID )
79107 }
80108 }
0 commit comments