@@ -3,6 +3,7 @@ package main
33import (
44 "context"
55 "crypto/tls"
6+ "errors"
67 "log"
78 "net/http"
89 "os"
@@ -13,7 +14,6 @@ import (
1314 "github.com/flashbots/cvm-reverse-proxy/common"
1415 "github.com/flashbots/cvm-reverse-proxy/internal/atls"
1516 "github.com/flashbots/cvm-reverse-proxy/proxy"
16-
1717 "github.com/urfave/cli/v2" // imports as package "cli"
1818)
1919
@@ -38,6 +38,14 @@ var flags []cli.Flag = []cli.Flag{
3838 Value : string (proxy .AttestationNone ),
3939 Usage : "type of attestation to expect and verify (" + proxy .AvailableAttestationTypes + ")" ,
4040 },
41+ & cli.StringFlag {
42+ Name : "tls-certificate" ,
43+ Usage : "Certificate to present (PEM)" ,
44+ },
45+ & cli.StringFlag {
46+ Name : "tls-private-key" ,
47+ Usage : "Private key for the certificate (PEM)" ,
48+ },
4149 & cli.StringFlag {
4250 Name : "client-measurements" ,
4351 Usage : "optional path to JSON measurements enforced on the client" ,
@@ -73,6 +81,10 @@ func runServer(cCtx *cli.Context) error {
7381 clientMeasurements := cCtx .String ("client-measurements" )
7482 logJSON := cCtx .Bool ("log-json" )
7583 logDebug := cCtx .Bool ("log-debug" )
84+ serverAttestationTypeFlag := cCtx .String ("server-attestation-type" )
85+
86+ certFile := cCtx .String ("tls-certificate" )
87+ keyFile := cCtx .String ("tls-private-key" )
7688
7789 log := common .SetupLogger (& common.LoggingOpts {
7890 Debug : logDebug ,
@@ -81,7 +93,18 @@ func runServer(cCtx *cli.Context) error {
8193 Version : common .Version ,
8294 })
8395
84- serverAttestationType , err := proxy .ParseAttestationType (cCtx .String ("server-attestation-type" ))
96+ useRegularTLS := certFile != "" || keyFile != ""
97+ if serverAttestationTypeFlag != "none" && useRegularTLS {
98+ log .Error ("invalid combination of --tls-certificate, --tls-private-key and --server-attestation-type flags passed (only 'none' is allowed)" )
99+ return errors .New ("invalid combination of --tls-certificate, --tls-private-key and --server-attestation-type flags passed (only 'none' is allowed)" )
100+ }
101+
102+ if useRegularTLS && (certFile == "" || keyFile == "" ) {
103+ log .Error ("not all of --tls-certificate and --tls-private-key specified" )
104+ return errors .New ("not all of --tls-certificate and --tls-private-key specified" )
105+ }
106+
107+ serverAttestationType , err := proxy .ParseAttestationType (serverAttestationTypeFlag )
85108 if err != nil {
86109 log .With ("attestation-type" , cCtx .String ("server-attestation-type" )).Error ("invalid server-attestation-type passed, see --help" )
87110 return err
@@ -112,6 +135,29 @@ func runServer(cCtx *cli.Context) error {
112135 panic (err )
113136 }
114137
138+ if useRegularTLS {
139+ cert , err := tls .LoadX509KeyPair (certFile , keyFile )
140+ if err != nil {
141+ log .Error ("could not load tls key pair" , "err" , err )
142+ return err
143+ }
144+
145+ originalGetConfigForClient := confTLS .GetConfigForClient
146+ confTLS .GetConfigForClient = func (clientHello * tls.ClientHelloInfo ) (* tls.Config , error ) {
147+ ogClientConfig , err := originalGetConfigForClient (clientHello )
148+ if err != nil {
149+ return ogClientConfig , err
150+ }
151+
152+ // Note: we don't have to copy the certificate because it's always created per request
153+ ogClientConfig .Certificates = []tls.Certificate {cert }
154+ ogClientConfig .GetClientCertificate = nil
155+ ogClientConfig .ServerName = ""
156+ return ogClientConfig , nil
157+ }
158+
159+ }
160+
115161 // Create an HTTP server
116162 server := & http.Server {
117163 Addr : listenAddr ,
0 commit comments