2014-07-12 15:45:33 -07:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
// more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-06-01 13:50:14 -07:00
|
|
|
|
2014-03-02 15:58:14 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-09-14 15:18:05 -07:00
|
|
|
"bufio"
|
2014-03-02 15:58:14 -07:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
2014-04-18 05:09:54 -07:00
|
|
|
"encoding/binary"
|
2014-03-02 15:58:14 -07:00
|
|
|
"encoding/pem"
|
2014-09-11 12:18:08 -07:00
|
|
|
"io"
|
2014-03-02 15:58:14 -07:00
|
|
|
"math/big"
|
2014-05-23 05:43:17 -07:00
|
|
|
mr "math/rand"
|
2014-09-11 12:18:08 -07:00
|
|
|
"net"
|
2014-03-02 15:58:14 -07:00
|
|
|
"os"
|
2014-03-28 06:36:57 -07:00
|
|
|
"path/filepath"
|
2014-03-02 15:58:14 -07:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tlsRSABits = 3072
|
|
|
|
tlsName = "syncthing"
|
|
|
|
)
|
|
|
|
|
2014-05-21 05:04:16 -07:00
|
|
|
func loadCert(dir string, prefix string) (tls.Certificate, error) {
|
2014-06-29 16:42:03 -07:00
|
|
|
cf := filepath.Join(dir, prefix+"cert.pem")
|
|
|
|
kf := filepath.Join(dir, prefix+"key.pem")
|
|
|
|
return tls.LoadX509KeyPair(cf, kf)
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
2014-04-18 05:09:54 -07:00
|
|
|
func certSeed(bs []byte) int64 {
|
|
|
|
hf := sha256.New()
|
|
|
|
hf.Write(bs)
|
|
|
|
id := hf.Sum(nil)
|
|
|
|
return int64(binary.BigEndian.Uint64(id))
|
|
|
|
}
|
|
|
|
|
2014-05-21 05:04:16 -07:00
|
|
|
func newCertificate(dir string, prefix string) {
|
2014-08-03 00:41:08 -07:00
|
|
|
l.Infoln("Generating RSA key and certificate...")
|
2014-03-02 15:58:14 -07:00
|
|
|
|
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, tlsRSABits)
|
2014-09-20 06:42:20 -07:00
|
|
|
if err != nil {
|
|
|
|
l.Fatalln("generate key:", err)
|
|
|
|
}
|
2014-03-02 15:58:14 -07:00
|
|
|
|
|
|
|
notBefore := time.Now()
|
|
|
|
notAfter := time.Date(2049, 12, 31, 23, 59, 59, 0, time.UTC)
|
|
|
|
|
|
|
|
template := x509.Certificate{
|
2014-05-23 05:43:17 -07:00
|
|
|
SerialNumber: new(big.Int).SetInt64(mr.Int63()),
|
2014-03-02 15:58:14 -07:00
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: tlsName,
|
|
|
|
},
|
|
|
|
NotBefore: notBefore,
|
|
|
|
NotAfter: notAfter,
|
|
|
|
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
2014-09-20 06:42:20 -07:00
|
|
|
if err != nil {
|
|
|
|
l.Fatalln("create cert:", err)
|
|
|
|
}
|
2014-03-02 15:58:14 -07:00
|
|
|
|
2014-05-21 05:04:16 -07:00
|
|
|
certOut, err := os.Create(filepath.Join(dir, prefix+"cert.pem"))
|
2014-09-20 06:42:20 -07:00
|
|
|
if err != nil {
|
|
|
|
l.Fatalln("save cert:", err)
|
|
|
|
}
|
2014-09-20 06:31:15 -07:00
|
|
|
err = pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
2014-09-20 06:42:20 -07:00
|
|
|
if err != nil {
|
|
|
|
l.Fatalln("save cert:", err)
|
|
|
|
}
|
2014-09-20 06:31:15 -07:00
|
|
|
err = certOut.Close()
|
2014-09-20 06:42:20 -07:00
|
|
|
if err != nil {
|
|
|
|
l.Fatalln("save cert:", err)
|
|
|
|
}
|
2014-03-02 15:58:14 -07:00
|
|
|
|
2014-05-21 05:04:16 -07:00
|
|
|
keyOut, err := os.OpenFile(filepath.Join(dir, prefix+"key.pem"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
2014-09-20 06:42:20 -07:00
|
|
|
if err != nil {
|
|
|
|
l.Fatalln("save key:", err)
|
|
|
|
}
|
2014-09-20 06:31:15 -07:00
|
|
|
err = pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
|
2014-09-20 06:42:20 -07:00
|
|
|
if err != nil {
|
|
|
|
l.Fatalln("save key:", err)
|
|
|
|
}
|
2014-09-20 06:31:15 -07:00
|
|
|
err = keyOut.Close()
|
2014-09-20 06:42:20 -07:00
|
|
|
if err != nil {
|
|
|
|
l.Fatalln("save key:", err)
|
|
|
|
}
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
2014-09-11 12:18:08 -07:00
|
|
|
|
|
|
|
type DowngradingListener struct {
|
|
|
|
net.Listener
|
|
|
|
TLSConfig *tls.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
type WrappedConnection struct {
|
|
|
|
io.Reader
|
|
|
|
net.Conn
|
|
|
|
}
|
|
|
|
|
2014-09-14 15:18:05 -07:00
|
|
|
func (l *DowngradingListener) Accept() (net.Conn, error) {
|
|
|
|
conn, err := l.Listener.Accept()
|
2014-09-11 12:18:08 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-09-14 15:18:05 -07:00
|
|
|
br := bufio.NewReader(conn)
|
|
|
|
bs, err := br.Peek(1)
|
2014-09-11 12:18:08 -07:00
|
|
|
if err != nil {
|
2014-09-18 02:45:48 -07:00
|
|
|
// We hit a read error here, but the Accept() call succeeded so we must not return an error.
|
|
|
|
// We return the connection as is and let whoever tries to use it deal with the error.
|
|
|
|
return conn, nil
|
2014-09-11 12:18:08 -07:00
|
|
|
}
|
|
|
|
|
2014-09-14 15:18:05 -07:00
|
|
|
wrapper := &WrappedConnection{br, conn}
|
2014-09-11 12:18:08 -07:00
|
|
|
|
2014-09-14 15:18:05 -07:00
|
|
|
// 0x16 is the first byte of a TLS handshake
|
|
|
|
if bs[0] == 0x16 {
|
|
|
|
return tls.Server(wrapper, l.TLSConfig), nil
|
2014-09-11 12:18:08 -07:00
|
|
|
}
|
2014-09-14 15:18:05 -07:00
|
|
|
|
2014-09-11 12:18:08 -07:00
|
|
|
return wrapper, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *WrappedConnection) Read(b []byte) (n int, err error) {
|
|
|
|
return c.Reader.Read(b)
|
|
|
|
}
|