]> Cypherpunks.ru repositories - ucspi.git/blob - cmd/tlss/main.go
c0f7a09f443da23151236219e1ed39da006d3f80
[ucspi.git] / cmd / tlss / main.go
1 /*
2 ucspi/cmd/tlsc -- UCSPI TLS server
3 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package main
19
20 import (
21         "crypto/tls"
22         "crypto/x509"
23         "flag"
24         "fmt"
25         "log"
26         "os"
27         "os/exec"
28
29         "go.cypherpunks.ru/ucspi"
30 )
31
32 func main() {
33         crtPath := flag.String("cert", "cert.pem", "Path to server X.509 certificate")
34         prvPath := flag.String("key", "prv.pem", "Path to server PKCS#8 private key")
35         casPath := flag.String("client-ca", "", "Require client authentication, path to CA certificates file")
36         flag.Usage = func() {
37                 fmt.Fprintf(os.Stderr, `Usage: tcpserver host port tlss [-client-ca CAs.pem]
38         -cert cert.pem -key prv.pem program [args...]
39
40 `)
41                 flag.PrintDefaults()
42         }
43         flag.Parse()
44         log.SetFlags(log.Lshortfile)
45
46         crtRaw, _, err := ucspi.CertificateFromFile(*crtPath)
47         if err != nil {
48                 log.Fatalln(err)
49         }
50         prv, err := ucspi.PrivateKeyFromFile(*prvPath)
51         if err != nil {
52                 log.Fatalln(err)
53         }
54         var cas *x509.CertPool
55         if *casPath != "" {
56                 cas, err = ucspi.CertPoolFromFile(*casPath)
57                 if err != nil {
58                         log.Fatalln(err)
59                 }
60         }
61
62         cfg := &tls.Config{
63                 Certificates: []tls.Certificate{{
64                         Certificate: [][]byte{crtRaw},
65                         PrivateKey:  prv,
66                 }},
67                 ClientCAs: cas,
68         }
69         if *casPath != "" {
70                 cfg.ClientAuth = tls.RequireAndVerifyClientCert
71         }
72
73         conn := &ucspi.Conn{R: os.Stdin, W: os.Stdout}
74         tlsConn := tls.Server(conn, cfg)
75         if err = tlsConn.Handshake(); err != nil {
76                 log.Fatalln(err)
77         }
78         var dn string
79         if *casPath != "" {
80                 dn = tlsConn.ConnectionState().PeerCertificates[0].Subject.String()
81         }
82
83         args := flag.Args()
84         cmd := exec.Command(args[0], args[1:]...)
85         cmd.Stdin = tlsConn
86         cmd.Stdout = tlsConn
87         cmd.Stderr = os.Stderr
88         cmd.Env = append(os.Environ(), "PROTO=TLS")
89         if dn != "" {
90                 cmd.Env = append(cmd.Env, "TLSREMOTEDN="+dn)
91         }
92
93         if err = cmd.Start(); err != nil {
94                 log.Fatalln(err)
95         }
96         if _, err = cmd.Process.Wait(); err != nil {
97                 log.Fatalln(err)
98         }
99 }