2014-07-13 00:45:33 +02:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
|
|
|
|
// All rights reserved. Use of this source code is governed by an MIT-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-06-01 22:50:14 +02:00
|
|
|
|
2014-03-02 23:58:14 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2014-09-11 20:18:08 +01:00
|
|
|
"bytes"
|
2014-03-02 23:58:14 +01:00
|
|
|
"crypto/rand"
|
|
|
|
|
"crypto/rsa"
|
|
|
|
|
"crypto/sha256"
|
|
|
|
|
"crypto/tls"
|
|
|
|
|
"crypto/x509"
|
|
|
|
|
"crypto/x509/pkix"
|
2014-04-18 14:09:54 +02:00
|
|
|
"encoding/binary"
|
2014-03-02 23:58:14 +01:00
|
|
|
"encoding/pem"
|
2014-09-11 20:18:08 +01:00
|
|
|
"io"
|
2014-03-02 23:58:14 +01:00
|
|
|
"math/big"
|
2014-05-23 14:43:17 +02:00
|
|
|
mr "math/rand"
|
2014-09-11 20:18:08 +01:00
|
|
|
"net"
|
2014-03-02 23:58:14 +01:00
|
|
|
"os"
|
2014-03-28 14:36:57 +01:00
|
|
|
"path/filepath"
|
2014-03-02 23:58:14 +01:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
tlsRSABits = 3072
|
|
|
|
|
tlsName = "syncthing"
|
|
|
|
|
)
|
|
|
|
|
|
2014-05-21 14:04:16 +02:00
|
|
|
func loadCert(dir string, prefix string) (tls.Certificate, error) {
|
2014-06-30 01:42:03 +02:00
|
|
|
cf := filepath.Join(dir, prefix+"cert.pem")
|
|
|
|
|
kf := filepath.Join(dir, prefix+"key.pem")
|
|
|
|
|
return tls.LoadX509KeyPair(cf, kf)
|
2014-03-02 23:58:14 +01:00
|
|
|
}
|
|
|
|
|
|
2014-04-18 14:09:54 +02: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 14:04:16 +02:00
|
|
|
func newCertificate(dir string, prefix string) {
|
2014-08-03 09:41:08 +02:00
|
|
|
l.Infoln("Generating RSA key and certificate...")
|
2014-03-02 23:58:14 +01:00
|
|
|
|
|
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, tlsRSABits)
|
2014-05-14 21:08:56 -03:00
|
|
|
l.FatalErr(err)
|
2014-03-02 23:58:14 +01:00
|
|
|
|
|
|
|
|
notBefore := time.Now()
|
|
|
|
|
notAfter := time.Date(2049, 12, 31, 23, 59, 59, 0, time.UTC)
|
|
|
|
|
|
|
|
|
|
template := x509.Certificate{
|
2014-05-23 14:43:17 +02:00
|
|
|
SerialNumber: new(big.Int).SetInt64(mr.Int63()),
|
2014-03-02 23:58:14 +01: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-05-14 21:08:56 -03:00
|
|
|
l.FatalErr(err)
|
2014-03-02 23:58:14 +01:00
|
|
|
|
2014-05-21 14:04:16 +02:00
|
|
|
certOut, err := os.Create(filepath.Join(dir, prefix+"cert.pem"))
|
2014-05-14 21:08:56 -03:00
|
|
|
l.FatalErr(err)
|
2014-03-02 23:58:14 +01:00
|
|
|
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
|
|
|
|
certOut.Close()
|
|
|
|
|
|
2014-05-21 14:04:16 +02:00
|
|
|
keyOut, err := os.OpenFile(filepath.Join(dir, prefix+"key.pem"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
2014-05-14 21:08:56 -03:00
|
|
|
l.FatalErr(err)
|
2014-03-02 23:58:14 +01:00
|
|
|
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
|
|
|
|
|
keyOut.Close()
|
|
|
|
|
}
|
2014-09-11 20:18:08 +01:00
|
|
|
|
|
|
|
|
type DowngradingListener struct {
|
|
|
|
|
net.Listener
|
|
|
|
|
TLSConfig *tls.Config
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type WrappedConnection struct {
|
|
|
|
|
io.Reader
|
|
|
|
|
net.Conn
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewDowngradingListener(address string, config *tls.Config) (net.Listener, error) {
|
|
|
|
|
listener, err := net.Listen("tcp", address)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &DowngradingListener{listener, config}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (listener *DowngradingListener) Accept() (net.Conn, error) {
|
|
|
|
|
connection, err := listener.Listener.Accept()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var peek [1]byte
|
|
|
|
|
_, err = io.ReadFull(connection, peek[:])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jointReader := io.MultiReader(bytes.NewReader(peek[:]), connection)
|
|
|
|
|
wrapper := &WrappedConnection{jointReader, connection}
|
|
|
|
|
|
|
|
|
|
// TLS handshake starts with ASCII SYN
|
|
|
|
|
if peek[0] == 22 {
|
|
|
|
|
return tls.Server(wrapper, listener.TLSConfig), nil
|
|
|
|
|
}
|
|
|
|
|
return wrapper, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *WrappedConnection) Read(b []byte) (n int, err error) {
|
|
|
|
|
return c.Reader.Read(b)
|
|
|
|
|
}
|