2020-11-09 14:33:32 +00:00
|
|
|
// Copyright (C) 2019 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base32"
|
2021-02-02 19:15:14 +00:00
|
|
|
"encoding/binary"
|
2020-11-09 14:33:32 +00:00
|
|
|
"errors"
|
2021-02-02 19:15:14 +00:00
|
|
|
"fmt"
|
2020-11-09 14:33:32 +00:00
|
|
|
"io"
|
|
|
|
"strings"
|
2021-03-22 20:50:19 +00:00
|
|
|
"sync"
|
2020-11-09 14:33:32 +00:00
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
2023-03-12 19:06:59 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru/v2"
|
2020-11-09 14:33:32 +00:00
|
|
|
"github.com/miscreant/miscreant.go"
|
2021-03-02 18:17:20 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2021-03-07 17:44:21 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sha256"
|
2020-11-09 14:33:32 +00:00
|
|
|
"golang.org/x/crypto/chacha20poly1305"
|
|
|
|
"golang.org/x/crypto/hkdf"
|
|
|
|
"golang.org/x/crypto/scrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
nonceSize = 24 // chacha20poly1305.NonceSizeX
|
|
|
|
tagSize = 16 // chacha20poly1305.Overhead()
|
|
|
|
keySize = 32 // fits both chacha20poly1305 and AES-SIV
|
|
|
|
minPaddedSize = 1024 // smallest block we'll allow
|
|
|
|
blockOverhead = tagSize + nonceSize
|
|
|
|
maxPathComponent = 200 // characters
|
|
|
|
encryptedDirExtension = ".syncthing-enc" // for top level dirs
|
|
|
|
miscreantAlgo = "AES-SIV"
|
2023-03-12 19:06:59 +00:00
|
|
|
folderKeyCacheEntries = 1000
|
|
|
|
fileKeyCacheEntries = 5000
|
2020-11-09 14:33:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// The encryptedModel sits between the encrypted device and the model. It
|
|
|
|
// receives encrypted metadata and requests from the untrusted device, so it
|
|
|
|
// must decrypt those and answer requests by encrypting the data.
|
|
|
|
type encryptedModel struct {
|
2023-09-06 10:52:01 +00:00
|
|
|
model rawModel
|
2021-03-22 20:50:19 +00:00
|
|
|
folderKeys *folderKeyRegistry
|
2023-03-12 19:06:59 +00:00
|
|
|
keyGen *KeyGenerator
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:52:01 +00:00
|
|
|
func newEncryptedModel(model rawModel, folderKeys *folderKeyRegistry, keyGen *KeyGenerator) encryptedModel {
|
2023-03-12 19:06:59 +00:00
|
|
|
return encryptedModel{
|
|
|
|
model: model,
|
|
|
|
folderKeys: folderKeys,
|
|
|
|
keyGen: keyGen,
|
|
|
|
}
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
func (e encryptedModel) Index(idx *Index) error {
|
|
|
|
if folderKey, ok := e.folderKeys.get(idx.Folder); ok {
|
2020-11-09 14:33:32 +00:00
|
|
|
// incoming index data to be decrypted
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
if err := decryptFileInfos(e.keyGen, idx.Files, folderKey); err != nil {
|
2020-11-09 14:33:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
return e.model.Index(idx)
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
func (e encryptedModel) IndexUpdate(idxUp *IndexUpdate) error {
|
|
|
|
if folderKey, ok := e.folderKeys.get(idxUp.Folder); ok {
|
2020-11-09 14:33:32 +00:00
|
|
|
// incoming index data to be decrypted
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
if err := decryptFileInfos(e.keyGen, idxUp.Files, folderKey); err != nil {
|
2020-11-09 14:33:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
return e.model.IndexUpdate(idxUp)
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
func (e encryptedModel) Request(req *Request) (RequestResponse, error) {
|
|
|
|
folderKey, ok := e.folderKeys.get(req.Folder)
|
2020-11-09 14:33:32 +00:00
|
|
|
if !ok {
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
return e.model.Request(req)
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out the real file name, offset and size from the encrypted /
|
|
|
|
// tweaked values.
|
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
realName, err := decryptName(req.Name, folderKey)
|
2020-11-09 14:33:32 +00:00
|
|
|
if err != nil {
|
2021-02-02 19:15:14 +00:00
|
|
|
return nil, fmt.Errorf("decrypting name: %w", err)
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
realSize := req.Size - blockOverhead
|
|
|
|
realOffset := req.Offset - int64(req.BlockNo*blockOverhead)
|
2020-11-09 14:33:32 +00:00
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
if req.Size < minPaddedSize {
|
2020-11-09 14:33:32 +00:00
|
|
|
return nil, errors.New("short request")
|
|
|
|
}
|
|
|
|
|
2023-03-18 09:22:18 +00:00
|
|
|
// Attempt to decrypt the block hash; it may be nil depending on what
|
|
|
|
// type of device the request comes from. Trusted devices with
|
|
|
|
// encryption enabled know the hash but don't bother to encrypt & send
|
|
|
|
// it to us. Untrusted devices have the hash from the encrypted index
|
|
|
|
// data and do send it. The model knows to only verify the hash if it
|
|
|
|
// actually gets one.
|
|
|
|
|
|
|
|
var realHash []byte
|
2023-03-12 19:06:59 +00:00
|
|
|
fileKey := e.keyGen.FileKey(realName, folderKey)
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
if len(req.Hash) > 0 {
|
2023-03-18 09:22:18 +00:00
|
|
|
var additional [8]byte
|
|
|
|
binary.BigEndian.PutUint64(additional[:], uint64(realOffset))
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
realHash, err = decryptDeterministic(req.Hash, fileKey, additional[:])
|
2023-03-18 09:22:18 +00:00
|
|
|
if err != nil {
|
|
|
|
// "Legacy", no offset additional data?
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
realHash, err = decryptDeterministic(req.Hash, fileKey, nil)
|
2023-03-18 09:22:18 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("decrypting block hash: %w", err)
|
|
|
|
}
|
2021-02-02 19:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Perform that request and grab the data.
|
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
req.Name = realName
|
|
|
|
req.Size = realSize
|
|
|
|
req.Offset = realOffset
|
|
|
|
req.Hash = realHash
|
|
|
|
resp, err := e.model.Request(req)
|
2020-11-09 14:33:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypt the response. Blocks smaller than minPaddedSize are padded
|
|
|
|
// with random data.
|
|
|
|
|
|
|
|
data := resp.Data()
|
|
|
|
if len(data) < minPaddedSize {
|
|
|
|
nd := make([]byte, minPaddedSize)
|
|
|
|
copy(nd, data)
|
|
|
|
if _, err := rand.Read(nd[len(data):]); err != nil {
|
|
|
|
panic("catastrophic randomness failure")
|
|
|
|
}
|
|
|
|
data = nd
|
|
|
|
}
|
|
|
|
enc := encryptBytes(data, fileKey)
|
|
|
|
resp.Close()
|
|
|
|
return rawResponse{enc}, nil
|
|
|
|
}
|
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
func (e encryptedModel) DownloadProgress(p *DownloadProgress) error {
|
|
|
|
if _, ok := e.folderKeys.get(p.Folder); !ok {
|
|
|
|
return e.model.DownloadProgress(p)
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypted devices shouldn't send these - ignore them.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
func (e encryptedModel) ClusterConfig(config *ClusterConfig) error {
|
2023-07-29 08:24:44 +00:00
|
|
|
return e.model.ClusterConfig(config)
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 08:24:44 +00:00
|
|
|
func (e encryptedModel) Closed(err error) {
|
|
|
|
e.model.Closed(err)
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The encryptedConnection sits between the model and the encrypted device. It
|
|
|
|
// encrypts outgoing metadata and decrypts incoming responses.
|
|
|
|
type encryptedConnection struct {
|
2020-12-21 10:40:51 +00:00
|
|
|
ConnectionInfo
|
2021-03-22 20:50:19 +00:00
|
|
|
conn *rawConnection
|
|
|
|
folderKeys *folderKeyRegistry
|
2023-03-12 19:06:59 +00:00
|
|
|
keyGen *KeyGenerator
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEncryptedConnection(ci ConnectionInfo, conn *rawConnection, folderKeys *folderKeyRegistry, keyGen *KeyGenerator) encryptedConnection {
|
|
|
|
return encryptedConnection{
|
|
|
|
ConnectionInfo: ci,
|
|
|
|
conn: conn,
|
|
|
|
folderKeys: folderKeys,
|
|
|
|
keyGen: keyGen,
|
|
|
|
}
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e encryptedConnection) Start() {
|
|
|
|
e.conn.Start()
|
|
|
|
}
|
|
|
|
|
2021-03-22 20:50:19 +00:00
|
|
|
func (e encryptedConnection) SetFolderPasswords(passwords map[string]string) {
|
|
|
|
e.folderKeys.setPasswords(passwords)
|
|
|
|
}
|
|
|
|
|
2023-07-29 08:24:44 +00:00
|
|
|
func (e encryptedConnection) DeviceID() DeviceID {
|
|
|
|
return e.conn.DeviceID()
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e encryptedConnection) Index(ctx context.Context, folder string, files []FileInfo) error {
|
2021-03-22 20:50:19 +00:00
|
|
|
if folderKey, ok := e.folderKeys.get(folder); ok {
|
2023-03-12 19:06:59 +00:00
|
|
|
encryptFileInfos(e.keyGen, files, folderKey)
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
return e.conn.Index(ctx, folder, files)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e encryptedConnection) IndexUpdate(ctx context.Context, folder string, files []FileInfo) error {
|
2021-03-22 20:50:19 +00:00
|
|
|
if folderKey, ok := e.folderKeys.get(folder); ok {
|
2023-03-12 19:06:59 +00:00
|
|
|
encryptFileInfos(e.keyGen, files, folderKey)
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
return e.conn.IndexUpdate(ctx, folder, files)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e encryptedConnection) Request(ctx context.Context, folder string, name string, blockNo int, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error) {
|
2021-03-22 20:50:19 +00:00
|
|
|
folderKey, ok := e.folderKeys.get(folder)
|
2020-11-09 14:33:32 +00:00
|
|
|
if !ok {
|
|
|
|
return e.conn.Request(ctx, folder, name, blockNo, offset, size, hash, weakHash, fromTemporary)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypt / adjust the request parameters.
|
|
|
|
|
|
|
|
origSize := size
|
|
|
|
if size < minPaddedSize {
|
|
|
|
// Make a request for minPaddedSize data instead of the smaller
|
|
|
|
// block. We'll chop of the extra data later.
|
|
|
|
size = minPaddedSize
|
|
|
|
}
|
|
|
|
encName := encryptName(name, folderKey)
|
|
|
|
encOffset := offset + int64(blockNo*blockOverhead)
|
|
|
|
encSize := size + blockOverhead
|
|
|
|
|
|
|
|
// Perform that request, getting back and encrypted block.
|
|
|
|
|
|
|
|
bs, err := e.conn.Request(ctx, folder, encName, blockNo, encOffset, encSize, nil, 0, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the decrypted block (or an error if it fails decryption)
|
|
|
|
|
2023-03-12 19:06:59 +00:00
|
|
|
fileKey := e.keyGen.FileKey(name, folderKey)
|
2020-11-09 14:33:32 +00:00
|
|
|
bs, err = DecryptBytes(bs, fileKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bs[:origSize], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e encryptedConnection) DownloadProgress(ctx context.Context, folder string, updates []FileDownloadProgressUpdate) {
|
2021-03-22 20:50:19 +00:00
|
|
|
if _, ok := e.folderKeys.get(folder); !ok {
|
2020-11-09 14:33:32 +00:00
|
|
|
e.conn.DownloadProgress(ctx, folder, updates)
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need to send these
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e encryptedConnection) ClusterConfig(config ClusterConfig) {
|
|
|
|
e.conn.ClusterConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e encryptedConnection) Close(err error) {
|
|
|
|
e.conn.Close(err)
|
|
|
|
}
|
|
|
|
|
2021-06-17 11:57:44 +00:00
|
|
|
func (e encryptedConnection) Closed() <-chan struct{} {
|
2020-11-09 14:33:32 +00:00
|
|
|
return e.conn.Closed()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e encryptedConnection) Statistics() Statistics {
|
|
|
|
return e.conn.Statistics()
|
|
|
|
}
|
|
|
|
|
2023-03-12 19:06:59 +00:00
|
|
|
func encryptFileInfos(keyGen *KeyGenerator, files []FileInfo, folderKey *[keySize]byte) {
|
2020-11-09 14:33:32 +00:00
|
|
|
for i, fi := range files {
|
2023-03-12 19:06:59 +00:00
|
|
|
files[i] = encryptFileInfo(keyGen, fi, folderKey)
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// encryptFileInfo encrypts a FileInfo and wraps it into a new fake FileInfo
|
|
|
|
// with an encrypted name.
|
2023-03-12 19:06:59 +00:00
|
|
|
func encryptFileInfo(keyGen *KeyGenerator, fi FileInfo, folderKey *[keySize]byte) FileInfo {
|
|
|
|
fileKey := keyGen.FileKey(fi.Name, folderKey)
|
2020-11-09 14:33:32 +00:00
|
|
|
|
|
|
|
// The entire FileInfo is encrypted with a random nonce, and concatenated
|
|
|
|
// with that nonce.
|
|
|
|
|
|
|
|
bs, err := proto.Marshal(&fi)
|
|
|
|
if err != nil {
|
|
|
|
panic("impossible serialization mishap: " + err.Error())
|
|
|
|
}
|
|
|
|
encryptedFI := encryptBytes(bs, fileKey)
|
|
|
|
|
|
|
|
// The vector is set to something that is higher than any other version sent
|
2021-03-31 06:59:15 +00:00
|
|
|
// previously. We do this because
|
2020-11-09 14:33:32 +00:00
|
|
|
// there is no way for the insecure device on the other end to do proper
|
|
|
|
// conflict resolution, so they will simply accept and keep whatever is the
|
|
|
|
// latest version they see. The secure devices will decrypt the real
|
|
|
|
// FileInfo, see the real Version, and act appropriately regardless of what
|
|
|
|
// this fake version happens to be.
|
2021-03-31 06:59:15 +00:00
|
|
|
// The vector also needs to be deterministic/the same among all trusted
|
|
|
|
// devices with the same vector, such that the pulling/remote completion
|
|
|
|
// works correctly on the untrusted device(s).
|
2020-11-09 14:33:32 +00:00
|
|
|
|
|
|
|
version := Vector{
|
|
|
|
Counters: []Counter{
|
|
|
|
{
|
2021-03-31 06:59:15 +00:00
|
|
|
ID: 1,
|
2020-11-09 14:33:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-03-31 06:59:15 +00:00
|
|
|
for _, counter := range fi.Version.Counters {
|
|
|
|
version.Counters[0].Value += counter.Value
|
|
|
|
}
|
2020-11-09 14:33:32 +00:00
|
|
|
|
|
|
|
// Construct the fake block list. Each block will be blockOverhead bytes
|
|
|
|
// larger than the corresponding real one and have an encrypted hash.
|
|
|
|
// Very small blocks will be padded upwards to minPaddedSize.
|
|
|
|
//
|
|
|
|
// The encrypted hash becomes just a "token" for the data -- it doesn't
|
|
|
|
// help verifying it, but it lets the encrypted device do block level
|
|
|
|
// diffs and data reuse properly when it gets a new version of a file.
|
|
|
|
|
|
|
|
var offset int64
|
|
|
|
blocks := make([]BlockInfo, len(fi.Blocks))
|
|
|
|
for i, b := range fi.Blocks {
|
|
|
|
if b.Size < minPaddedSize {
|
|
|
|
b.Size = minPaddedSize
|
|
|
|
}
|
|
|
|
size := b.Size + blockOverhead
|
2021-02-02 19:15:14 +00:00
|
|
|
|
|
|
|
// The offset goes into the encrypted block hash as additional data,
|
|
|
|
// essentially mixing in with the nonce. This means a block hash
|
|
|
|
// remains stable for the same data at the same offset, but doesn't
|
|
|
|
// reveal the existence of identical data blocks at other offsets.
|
|
|
|
var additional [8]byte
|
|
|
|
binary.BigEndian.PutUint64(additional[:], uint64(b.Offset))
|
|
|
|
hash := encryptDeterministic(b.Hash, fileKey, additional[:])
|
|
|
|
|
2020-11-09 14:33:32 +00:00
|
|
|
blocks[i] = BlockInfo{
|
2021-02-02 19:15:14 +00:00
|
|
|
Hash: hash,
|
2020-11-09 14:33:32 +00:00
|
|
|
Offset: offset,
|
|
|
|
Size: size,
|
|
|
|
}
|
|
|
|
offset += int64(size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the fake FileInfo. This is mostly just a wrapper around the
|
|
|
|
// encrypted FileInfo and fake block list. We'll represent symlinks as
|
|
|
|
// directories, because they need some sort of on disk representation
|
|
|
|
// but have no data outside of the metadata. Deletion and sequence
|
|
|
|
// numbering are handled as usual.
|
|
|
|
|
|
|
|
typ := FileInfoTypeFile
|
|
|
|
if fi.Type != FileInfoTypeFile {
|
|
|
|
typ = FileInfoTypeDirectory
|
|
|
|
}
|
|
|
|
enc := FileInfo{
|
2021-08-04 21:12:01 +00:00
|
|
|
Name: encryptName(fi.Name, folderKey),
|
|
|
|
Type: typ,
|
2023-03-12 19:06:59 +00:00
|
|
|
Permissions: 0o644,
|
2021-08-04 21:12:01 +00:00
|
|
|
ModifiedS: 1234567890, // Sat Feb 14 00:31:30 CET 2009
|
|
|
|
Deleted: fi.Deleted,
|
|
|
|
RawInvalid: fi.IsInvalid(),
|
|
|
|
Version: version,
|
|
|
|
Sequence: fi.Sequence,
|
|
|
|
Encrypted: encryptedFI,
|
|
|
|
}
|
|
|
|
if typ == FileInfoTypeFile {
|
|
|
|
enc.Size = offset // new total file size
|
|
|
|
enc.Blocks = blocks
|
|
|
|
enc.RawBlockSize = fi.BlockSize() + blockOverhead
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return enc
|
|
|
|
}
|
|
|
|
|
2023-03-12 19:06:59 +00:00
|
|
|
func decryptFileInfos(keyGen *KeyGenerator, files []FileInfo, folderKey *[keySize]byte) error {
|
2020-11-09 14:33:32 +00:00
|
|
|
for i, fi := range files {
|
2023-03-12 19:06:59 +00:00
|
|
|
decFI, err := DecryptFileInfo(keyGen, fi, folderKey)
|
2020-11-09 14:33:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
files[i] = decFI
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptFileInfo extracts the encrypted portion of a FileInfo, decrypts it
|
|
|
|
// and returns that.
|
2023-03-12 19:06:59 +00:00
|
|
|
func DecryptFileInfo(keyGen *KeyGenerator, fi FileInfo, folderKey *[keySize]byte) (FileInfo, error) {
|
2020-11-09 14:33:32 +00:00
|
|
|
realName, err := decryptName(fi.Name, folderKey)
|
|
|
|
if err != nil {
|
|
|
|
return FileInfo{}, err
|
|
|
|
}
|
|
|
|
|
2023-03-12 19:06:59 +00:00
|
|
|
fileKey := keyGen.FileKey(realName, folderKey)
|
2020-11-09 14:33:32 +00:00
|
|
|
dec, err := DecryptBytes(fi.Encrypted, fileKey)
|
|
|
|
if err != nil {
|
|
|
|
return FileInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var decFI FileInfo
|
|
|
|
if err := proto.Unmarshal(dec, &decFI); err != nil {
|
|
|
|
return FileInfo{}, err
|
|
|
|
}
|
2021-10-06 08:26:54 +00:00
|
|
|
|
|
|
|
// Preserve sequence, which is legitimately controlled by the untrusted device
|
|
|
|
decFI.Sequence = fi.Sequence
|
|
|
|
|
2020-11-09 14:33:32 +00:00
|
|
|
return decFI, nil
|
|
|
|
}
|
|
|
|
|
2021-02-27 07:57:12 +00:00
|
|
|
var base32Hex = base32.HexEncoding.WithPadding(base32.NoPadding)
|
|
|
|
|
2020-11-09 14:33:32 +00:00
|
|
|
// encryptName encrypts the given string in a deterministic manner (the
|
|
|
|
// result is always the same for any given string) and encodes it in a
|
|
|
|
// filesystem-friendly manner.
|
|
|
|
func encryptName(name string, key *[keySize]byte) string {
|
2021-02-02 19:15:14 +00:00
|
|
|
enc := encryptDeterministic([]byte(name), key, nil)
|
2021-02-27 07:57:12 +00:00
|
|
|
return slashify(base32Hex.EncodeToString(enc))
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// decryptName decrypts a string from encryptName
|
|
|
|
func decryptName(name string, key *[keySize]byte) (string, error) {
|
2021-02-27 07:57:12 +00:00
|
|
|
name, err := deslashify(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
bs, err := base32Hex.DecodeString(name)
|
2020-11-09 14:33:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-02-02 19:15:14 +00:00
|
|
|
dec, err := decryptDeterministic(bs, key, nil)
|
2020-11-09 14:33:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(dec), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// encryptBytes encrypts bytes with a random nonce
|
|
|
|
func encryptBytes(data []byte, key *[keySize]byte) []byte {
|
|
|
|
nonce := randomNonce()
|
|
|
|
return encrypt(data, nonce, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// encryptDeterministic encrypts bytes using AES-SIV
|
2021-02-02 19:15:14 +00:00
|
|
|
func encryptDeterministic(data []byte, key *[keySize]byte, additionalData []byte) []byte {
|
2020-11-09 14:33:32 +00:00
|
|
|
aead, err := miscreant.NewAEAD(miscreantAlgo, key[:], 0)
|
|
|
|
if err != nil {
|
|
|
|
panic("cipher failure: " + err.Error())
|
|
|
|
}
|
2021-02-02 19:15:14 +00:00
|
|
|
return aead.Seal(nil, nil, data, additionalData)
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// decryptDeterministic decrypts bytes using AES-SIV
|
2021-02-02 19:15:14 +00:00
|
|
|
func decryptDeterministic(data []byte, key *[keySize]byte, additionalData []byte) ([]byte, error) {
|
2020-11-09 14:33:32 +00:00
|
|
|
aead, err := miscreant.NewAEAD(miscreantAlgo, key[:], 0)
|
|
|
|
if err != nil {
|
|
|
|
panic("cipher failure: " + err.Error())
|
|
|
|
}
|
2021-02-02 19:15:14 +00:00
|
|
|
return aead.Open(nil, nil, data, additionalData)
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func encrypt(data []byte, nonce *[nonceSize]byte, key *[keySize]byte) []byte {
|
|
|
|
aead, err := chacha20poly1305.NewX(key[:])
|
|
|
|
if err != nil {
|
|
|
|
// Can only fail if the key is the wrong length
|
|
|
|
panic("cipher failure: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if aead.NonceSize() != nonceSize || aead.Overhead() != tagSize {
|
|
|
|
// We want these values to be constant for our type declarations so
|
|
|
|
// we don't use the values returned by the GCM, but we verify them
|
|
|
|
// here.
|
|
|
|
panic("crypto parameter mismatch")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data is appended to the nonce
|
|
|
|
return aead.Seal(nonce[:], nonce[:], data, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptBytes returns the decrypted bytes, or an error if decryption
|
|
|
|
// failed.
|
|
|
|
func DecryptBytes(data []byte, key *[keySize]byte) ([]byte, error) {
|
|
|
|
if len(data) < blockOverhead {
|
|
|
|
return nil, errors.New("data too short")
|
|
|
|
}
|
|
|
|
|
|
|
|
aead, err := chacha20poly1305.NewX(key[:])
|
|
|
|
if err != nil {
|
|
|
|
// Can only fail if the key is the wrong length
|
|
|
|
panic("cipher failure: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if aead.NonceSize() != nonceSize || aead.Overhead() != tagSize {
|
|
|
|
// We want these values to be constant for our type declarations so
|
|
|
|
// we don't use the values returned by the GCM, but we verify them
|
|
|
|
// here.
|
|
|
|
panic("crypto parameter mismatch")
|
|
|
|
}
|
|
|
|
|
|
|
|
return aead.Open(nil, data[:nonceSize], data[nonceSize:], nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// randomNonce is a normal, cryptographically random nonce
|
|
|
|
func randomNonce() *[nonceSize]byte {
|
|
|
|
var nonce [nonceSize]byte
|
|
|
|
if _, err := rand.Read(nonce[:]); err != nil {
|
|
|
|
panic("catastrophic randomness failure: " + err.Error())
|
|
|
|
}
|
|
|
|
return &nonce
|
|
|
|
}
|
|
|
|
|
|
|
|
// keysFromPasswords converts a set of folder ID to password into a set of
|
|
|
|
// folder ID to encryption key, using our key derivation function.
|
2023-03-12 19:06:59 +00:00
|
|
|
func keysFromPasswords(keyGen *KeyGenerator, passwords map[string]string) map[string]*[keySize]byte {
|
2020-11-09 14:33:32 +00:00
|
|
|
res := make(map[string]*[keySize]byte, len(passwords))
|
|
|
|
for folder, password := range passwords {
|
2023-03-12 19:06:59 +00:00
|
|
|
res[folder] = keyGen.KeyFromPassword(folder, password)
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func knownBytes(folderID string) []byte {
|
|
|
|
return []byte("syncthing" + folderID)
|
|
|
|
}
|
|
|
|
|
2023-03-12 19:06:59 +00:00
|
|
|
type KeyGenerator struct {
|
|
|
|
mut sync.Mutex
|
|
|
|
folderKeys *lru.TwoQueueCache[folderKeyCacheKey, *[keySize]byte]
|
|
|
|
fileKeys *lru.TwoQueueCache[fileKeyCacheKey, *[keySize]byte]
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewKeyGenerator() *KeyGenerator {
|
|
|
|
folderKeys, _ := lru.New2Q[folderKeyCacheKey, *[keySize]byte](folderKeyCacheEntries)
|
|
|
|
fileKeys, _ := lru.New2Q[fileKeyCacheKey, *[keySize]byte](fileKeyCacheEntries)
|
|
|
|
return &KeyGenerator{
|
|
|
|
folderKeys: folderKeys,
|
|
|
|
fileKeys: fileKeys,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type folderKeyCacheKey struct {
|
|
|
|
folderID string
|
|
|
|
password string
|
|
|
|
}
|
|
|
|
|
2020-11-09 14:33:32 +00:00
|
|
|
// KeyFromPassword uses key derivation to generate a stronger key from a
|
|
|
|
// probably weak password.
|
2023-03-12 19:06:59 +00:00
|
|
|
func (g *KeyGenerator) KeyFromPassword(folderID, password string) *[keySize]byte {
|
|
|
|
cacheKey := folderKeyCacheKey{folderID, password}
|
|
|
|
g.mut.Lock()
|
|
|
|
defer g.mut.Unlock()
|
|
|
|
if key, ok := g.folderKeys.Get(cacheKey); ok {
|
|
|
|
return key
|
|
|
|
}
|
2020-11-09 14:33:32 +00:00
|
|
|
bs, err := scrypt.Key([]byte(password), knownBytes(folderID), 32768, 8, 1, keySize)
|
|
|
|
if err != nil {
|
|
|
|
panic("key derivation failure: " + err.Error())
|
|
|
|
}
|
|
|
|
if len(bs) != keySize {
|
|
|
|
panic("key derivation failure: wrong number of bytes")
|
|
|
|
}
|
|
|
|
var key [keySize]byte
|
|
|
|
copy(key[:], bs)
|
2023-03-12 19:06:59 +00:00
|
|
|
g.folderKeys.Add(cacheKey, &key)
|
2020-11-09 14:33:32 +00:00
|
|
|
return &key
|
|
|
|
}
|
|
|
|
|
2021-03-07 17:44:21 +00:00
|
|
|
var hkdfSalt = []byte("syncthing")
|
|
|
|
|
2023-03-12 19:06:59 +00:00
|
|
|
type fileKeyCacheKey struct {
|
|
|
|
file string
|
|
|
|
key [keySize]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *KeyGenerator) FileKey(filename string, folderKey *[keySize]byte) *[keySize]byte {
|
|
|
|
g.mut.Lock()
|
|
|
|
defer g.mut.Unlock()
|
|
|
|
cacheKey := fileKeyCacheKey{filename, *folderKey}
|
|
|
|
if key, ok := g.fileKeys.Get(cacheKey); ok {
|
|
|
|
return key
|
|
|
|
}
|
2021-03-07 17:44:21 +00:00
|
|
|
kdf := hkdf.New(sha256.New, append(folderKey[:], filename...), hkdfSalt, nil)
|
2020-11-09 14:33:32 +00:00
|
|
|
var fileKey [keySize]byte
|
|
|
|
n, err := io.ReadFull(kdf, fileKey[:])
|
|
|
|
if err != nil || n != keySize {
|
|
|
|
panic("hkdf failure")
|
|
|
|
}
|
2023-03-12 19:06:59 +00:00
|
|
|
g.fileKeys.Add(cacheKey, &fileKey)
|
2020-11-09 14:33:32 +00:00
|
|
|
return &fileKey
|
|
|
|
}
|
|
|
|
|
2023-03-12 19:06:59 +00:00
|
|
|
func PasswordToken(keyGen *KeyGenerator, folderID, password string) []byte {
|
|
|
|
return encryptDeterministic(knownBytes(folderID), keyGen.KeyFromPassword(folderID, password), nil)
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// slashify inserts slashes (and file extension) in the string to create an
|
|
|
|
// appropriate tree. ABCDEFGH... => A.syncthing-enc/BC/DEFGH... We can use
|
|
|
|
// forward slashes here because we're on the outside of native path formats,
|
|
|
|
// the slash is the wire format.
|
|
|
|
func slashify(s string) string {
|
|
|
|
// We somewhat sloppily assume bytes == characters here, but the only
|
|
|
|
// file names we should deal with are those that come from our base32
|
|
|
|
// encoding.
|
|
|
|
|
|
|
|
comps := make([]string, 0, len(s)/maxPathComponent+3)
|
|
|
|
comps = append(comps, s[:1]+encryptedDirExtension)
|
|
|
|
s = s[1:]
|
|
|
|
comps = append(comps, s[:2])
|
|
|
|
s = s[2:]
|
|
|
|
|
|
|
|
for len(s) > maxPathComponent {
|
|
|
|
comps = append(comps, s[:maxPathComponent])
|
|
|
|
s = s[maxPathComponent:]
|
|
|
|
}
|
|
|
|
if len(s) > 0 {
|
|
|
|
comps = append(comps, s)
|
|
|
|
}
|
|
|
|
return strings.Join(comps, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
// deslashify removes slashes and encrypted file extensions from the string.
|
|
|
|
// This is the inverse of slashify().
|
2021-02-27 07:57:12 +00:00
|
|
|
func deslashify(s string) (string, error) {
|
2022-07-28 14:51:03 +00:00
|
|
|
if s == "" || !strings.HasPrefix(s[1:], encryptedDirExtension) {
|
2021-02-27 07:57:12 +00:00
|
|
|
return "", fmt.Errorf("invalid encrypted path: %q", s)
|
|
|
|
}
|
|
|
|
s = s[:1] + s[1+len(encryptedDirExtension):]
|
|
|
|
return strings.ReplaceAll(s, "/", ""), nil
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type rawResponse struct {
|
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r rawResponse) Data() []byte {
|
|
|
|
return r.data
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:32:45 +00:00
|
|
|
func (rawResponse) Close() {}
|
|
|
|
func (rawResponse) Wait() {}
|
2020-11-09 14:33:32 +00:00
|
|
|
|
|
|
|
// IsEncryptedParent returns true if the path points at a parent directory of
|
|
|
|
// encrypted data, i.e. is not a "real" directory. This is determined by
|
|
|
|
// checking for a sentinel string in the path.
|
2021-04-11 13:29:43 +00:00
|
|
|
func IsEncryptedParent(pathComponents []string) bool {
|
2020-12-09 17:16:14 +00:00
|
|
|
l := len(pathComponents)
|
|
|
|
if l == 2 && len(pathComponents[1]) != 2 {
|
2020-11-09 14:33:32 +00:00
|
|
|
return false
|
2020-11-23 17:37:27 +00:00
|
|
|
} else if l == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2022-07-28 14:51:03 +00:00
|
|
|
if pathComponents[0] == "" {
|
2020-11-23 17:37:27 +00:00
|
|
|
return false
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
2020-12-09 17:16:14 +00:00
|
|
|
if pathComponents[0][1:] != encryptedDirExtension {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if l < 2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, comp := range pathComponents[2:] {
|
|
|
|
if len(comp) != maxPathComponent {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
2021-03-22 20:50:19 +00:00
|
|
|
|
|
|
|
type folderKeyRegistry struct {
|
2023-03-12 19:06:59 +00:00
|
|
|
keyGen *KeyGenerator
|
|
|
|
keys map[string]*[keySize]byte // folder ID -> key
|
|
|
|
mut sync.RWMutex
|
2021-03-22 20:50:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-12 19:06:59 +00:00
|
|
|
func newFolderKeyRegistry(keyGen *KeyGenerator, passwords map[string]string) *folderKeyRegistry {
|
2021-03-22 20:50:19 +00:00
|
|
|
return &folderKeyRegistry{
|
2023-03-12 19:06:59 +00:00
|
|
|
keyGen: keyGen,
|
|
|
|
keys: keysFromPasswords(keyGen, passwords),
|
2021-03-22 20:50:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *folderKeyRegistry) get(folder string) (*[keySize]byte, bool) {
|
|
|
|
r.mut.RLock()
|
|
|
|
key, ok := r.keys[folder]
|
|
|
|
r.mut.RUnlock()
|
|
|
|
return key, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *folderKeyRegistry) setPasswords(passwords map[string]string) {
|
|
|
|
r.mut.Lock()
|
2023-03-12 19:06:59 +00:00
|
|
|
r.keys = keysFromPasswords(r.keyGen, passwords)
|
2021-03-22 20:50:19 +00:00
|
|
|
r.mut.Unlock()
|
|
|
|
}
|