@@ -4,14 +4,12 @@ import (
44 "bytes"
55 "crypto/rand"
66 "crypto/tls"
7- "crypto/x509"
87 "encoding/base64"
98 "encoding/binary"
109 "encoding/hex"
1110 "errors"
1211 "flag"
1312 "fmt"
14- "io/ioutil"
1513 "log"
1614 "net"
1715 "net/http"
@@ -37,6 +35,7 @@ import (
3735 "github.com/SenseUnit/dumbproxy/forward"
3836 "github.com/SenseUnit/dumbproxy/handler"
3937 clog "github.com/SenseUnit/dumbproxy/log"
38+ "github.com/SenseUnit/dumbproxy/tlsutil"
4039 proxyproto "github.com/pires/go-proxyproto"
4140
4241 _ "golang.org/x/crypto/x509roots/fallback"
@@ -118,61 +117,16 @@ func (l *PrefixList) Value() []netip.Prefix {
118117type TLSVersionArg uint16
119118
120119func (a * TLSVersionArg ) Set (s string ) error {
121- var ver uint16
122- switch strings .ToUpper (s ) {
123- case "TLS10" :
124- ver = tls .VersionTLS10
125- case "TLS11" :
126- ver = tls .VersionTLS11
127- case "TLS12" :
128- ver = tls .VersionTLS12
129- case "TLS13" :
130- ver = tls .VersionTLS13
131- case "TLS1.0" :
132- ver = tls .VersionTLS10
133- case "TLS1.1" :
134- ver = tls .VersionTLS11
135- case "TLS1.2" :
136- ver = tls .VersionTLS12
137- case "TLS1.3" :
138- ver = tls .VersionTLS13
139- case "10" :
140- ver = tls .VersionTLS10
141- case "11" :
142- ver = tls .VersionTLS11
143- case "12" :
144- ver = tls .VersionTLS12
145- case "13" :
146- ver = tls .VersionTLS13
147- case "1.0" :
148- ver = tls .VersionTLS10
149- case "1.1" :
150- ver = tls .VersionTLS11
151- case "1.2" :
152- ver = tls .VersionTLS12
153- case "1.3" :
154- ver = tls .VersionTLS13
155- case "" :
156- default :
157- return fmt .Errorf ("unknown TLS version %q" , s )
120+ ver , err := tlsutil .ParseVersion (s )
121+ if err != nil {
122+ return err
158123 }
159124 * a = TLSVersionArg (ver )
160125 return nil
161126}
162127
163128func (a * TLSVersionArg ) String () string {
164- switch * a {
165- case tls .VersionTLS10 :
166- return "TLS10"
167- case tls .VersionTLS11 :
168- return "TLS11"
169- case tls .VersionTLS12 :
170- return "TLS12"
171- case tls .VersionTLS13 :
172- return "TLS13"
173- default :
174- return fmt .Sprintf ("%#04x" , * a )
175- }
129+ return tlsutil .FormatVersion (uint16 (* a ))
176130}
177131
178132type proxyArg struct {
@@ -224,7 +178,9 @@ type CLIArgs struct {
224178 verbosity int
225179 cert , key , cafile string
226180 list_ciphers bool
181+ list_curves bool
227182 ciphers string
183+ curves string
228184 disableHTTP2 bool
229185 showVersion bool
230186 autocert bool
@@ -292,7 +248,9 @@ func parse_args() CLIArgs {
292248 flag .StringVar (& args .key , "key" , "" , "key for TLS certificate" )
293249 flag .StringVar (& args .cafile , "cafile" , "" , "CA file to authenticate clients with certificates" )
294250 flag .BoolVar (& args .list_ciphers , "list-ciphers" , false , "list ciphersuites" )
251+ flag .BoolVar (& args .list_curves , "list-curves" , false , "list key exchange curves" )
295252 flag .StringVar (& args .ciphers , "ciphers" , "" , "colon-separated list of enabled ciphers" )
253+ flag .StringVar (& args .curves , "curves" , "" , "colon-separated list of enabled key exchange curves" )
296254 flag .BoolVar (& args .disableHTTP2 , "disable-http2" , false , "disable HTTP2" )
297255 flag .BoolVar (& args .showVersion , "version" , false , "show program version and exit" )
298256 flag .BoolVar (& args .autocert , "autocert" , false , "issue TLS certificates automatically" )
@@ -374,6 +332,11 @@ func run() int {
374332 return 0
375333 }
376334
335+ if args .list_curves {
336+ list_curves ()
337+ return 0
338+ }
339+
377340 if args .passwd != "" {
378341 if err := passwd (args .passwd , args .passwdCost , args .positionalArgs ... ); err != nil {
379342 log .Fatalf ("can't set password: %v" , err )
@@ -570,7 +533,8 @@ func run() int {
570533
571534 if args .cert != "" {
572535 cfg , err1 := makeServerTLSConfig (args .cert , args .key , args .cafile ,
573- args .ciphers , uint16 (args .minTLSVersion ), uint16 (args .maxTLSVersion ), ! args .disableHTTP2 )
536+ args .ciphers , args .curves ,
537+ uint16 (args .minTLSVersion ), uint16 (args .maxTLSVersion ), ! args .disableHTTP2 )
574538 if err1 != nil {
575539 mainLogger .Critical ("TLS config construction failed: %v" , err1 )
576540 return 3
@@ -629,7 +593,7 @@ func run() int {
629593 }()
630594 }
631595 cfg := m .TLSConfig ()
632- cfg , err = updateServerTLSConfig (cfg , args .cafile , args .ciphers ,
596+ cfg , err = updateServerTLSConfig (cfg , args .cafile , args .ciphers , args . curves ,
633597 uint16 (args .minTLSVersion ), uint16 (args .maxTLSVersion ), ! args .disableHTTP2 )
634598 if err != nil {
635599 mainLogger .Critical ("TLS config construction failed: %v" , err )
@@ -657,7 +621,7 @@ func run() int {
657621 return 0
658622}
659623
660- func makeServerTLSConfig (certfile , keyfile , cafile , ciphers string , minVer , maxVer uint16 , h2 bool ) (* tls.Config , error ) {
624+ func makeServerTLSConfig (certfile , keyfile , cafile , ciphers , curves string , minVer , maxVer uint16 , h2 bool ) (* tls.Config , error ) {
661625 cfg := tls.Config {
662626 MinVersion : minVer ,
663627 MaxVersion : maxVer ,
@@ -668,18 +632,21 @@ func makeServerTLSConfig(certfile, keyfile, cafile, ciphers string, minVer, maxV
668632 }
669633 cfg .Certificates = []tls.Certificate {cert }
670634 if cafile != "" {
671- roots := x509 .NewCertPool ()
672- certs , err := ioutil .ReadFile (cafile )
635+ roots , err := tlsutil .LoadCAfile (cafile )
673636 if err != nil {
674637 return nil , err
675638 }
676- if ok := roots .AppendCertsFromPEM (certs ); ! ok {
677- return nil , errors .New ("Failed to load CA certificates" )
678- }
679639 cfg .ClientCAs = roots
680640 cfg .ClientAuth = tls .VerifyClientCertIfGiven
681641 }
682- cfg .CipherSuites = makeCipherList (ciphers )
642+ cfg .CipherSuites , err = tlsutil .ParseCipherList (ciphers )
643+ if err != nil {
644+ return nil , err
645+ }
646+ cfg .CurvePreferences , err = tlsutil .ParseCurveList (curves )
647+ if err != nil {
648+ return nil , err
649+ }
683650 if h2 {
684651 cfg .NextProtos = []string {"h2" , "http/1.1" }
685652 } else {
@@ -688,20 +655,24 @@ func makeServerTLSConfig(certfile, keyfile, cafile, ciphers string, minVer, maxV
688655 return & cfg , nil
689656}
690657
691- func updateServerTLSConfig (cfg * tls.Config , cafile , ciphers string , minVer , maxVer uint16 , h2 bool ) (* tls.Config , error ) {
658+ func updateServerTLSConfig (cfg * tls.Config , cafile , ciphers , curves string , minVer , maxVer uint16 , h2 bool ) (* tls.Config , error ) {
692659 if cafile != "" {
693- roots := x509 .NewCertPool ()
694- certs , err := ioutil .ReadFile (cafile )
660+ roots , err := tlsutil .LoadCAfile (cafile )
695661 if err != nil {
696662 return nil , err
697663 }
698- if ok := roots .AppendCertsFromPEM (certs ); ! ok {
699- return nil , errors .New ("Failed to load CA certificates" )
700- }
701664 cfg .ClientCAs = roots
702665 cfg .ClientAuth = tls .VerifyClientCertIfGiven
703666 }
704- cfg .CipherSuites = makeCipherList (ciphers )
667+ var err error
668+ cfg .CipherSuites , err = tlsutil .ParseCipherList (ciphers )
669+ if err != nil {
670+ return nil , err
671+ }
672+ cfg .CurvePreferences , err = tlsutil .ParseCurveList (curves )
673+ if err != nil {
674+ return nil , err
675+ }
705676 if h2 {
706677 cfg .NextProtos = []string {"h2" , "http/1.1" , "acme-tls/1" }
707678 } else {
@@ -712,33 +683,15 @@ func updateServerTLSConfig(cfg *tls.Config, cafile, ciphers string, minVer, maxV
712683 return cfg , nil
713684}
714685
715- func makeCipherList (ciphers string ) []uint16 {
716- if ciphers == "" {
717- return nil
718- }
719-
720- cipherIDs := make (map [string ]uint16 )
686+ func list_ciphers () {
721687 for _ , cipher := range tls .CipherSuites () {
722- cipherIDs [cipher .Name ] = cipher .ID
723- }
724-
725- cipherNameList := strings .Split (ciphers , ":" )
726- cipherIDList := make ([]uint16 , 0 , len (cipherNameList ))
727-
728- for _ , name := range cipherNameList {
729- id , ok := cipherIDs [name ]
730- if ! ok {
731- log .Printf ("WARNING: Unknown cipher \" %s\" " , name )
732- }
733- cipherIDList = append (cipherIDList , id )
688+ fmt .Println (cipher .Name )
734689 }
735-
736- return cipherIDList
737690}
738691
739- func list_ciphers () {
740- for _ , cipher := range tls . CipherSuites () {
741- fmt .Println (cipher . Name )
692+ func list_curves () {
693+ for _ , curve := range tlsutil . Curves () {
694+ fmt .Println (curve . String () )
742695 }
743696}
744697
0 commit comments