From 79bf060c2f403703dafa06ffbbf794d099cf251f Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Tue, 6 Sep 2022 12:41:52 -0700 Subject: [PATCH] Change ioutil -> os APIs --- app/app.go | 3 +-- app/plugins/networkCheck.go | 1 - model/group.go | 11 ++++++----- model/message.go | 2 +- peer/cwtch_peer.go | 7 ++++--- peer/storage.go | 7 +++---- protocol/files/manifest.go | 5 ++--- protocol/files/manifest_test.go | 4 ++-- storage/v1/file_enc.go | 8 ++++---- storage/v1/file_store.go | 3 +-- storage/v1/profile_store.go | 6 +++--- storage/v1/stream_store.go | 3 +-- testing/cwtch_peer_server_integration_test.go | 3 +-- .../encrypted_storage_integration_test.go | 3 +-- .../filesharing/file_sharing_integration_test.go | 14 ++++++-------- 15 files changed, 36 insertions(+), 44 deletions(-) diff --git a/app/app.go b/app/app.go index b61ae78..9a9aa4f 100644 --- a/app/app.go +++ b/app/app.go @@ -11,7 +11,6 @@ import ( "cwtch.im/cwtch/storage" "git.openprivacy.ca/openprivacy/connectivity" "git.openprivacy.ca/openprivacy/log" - "io/ioutil" "os" path "path/filepath" "strconv" @@ -140,7 +139,7 @@ func (app *application) LoadProfiles(password string) { count := 0 migrating := false - files, err := ioutil.ReadDir(path.Join(app.directory, "profiles")) + files, err := os.ReadDir(path.Join(app.directory, "profiles")) if err != nil { log.Errorf("error: cannot read profiles directory: %v", err) return diff --git a/app/plugins/networkCheck.go b/app/plugins/networkCheck.go index 9b38eb0..eec11a5 100644 --- a/app/plugins/networkCheck.go +++ b/app/plugins/networkCheck.go @@ -121,7 +121,6 @@ func (nc *networkCheck) selfTest() { go nc.checkConnection(nc.onion) } -// func (nc *networkCheck) checkConnection(onion string) { prog, _ := nc.acn.GetBootstrapStatus() if prog != 100 { diff --git a/model/group.go b/model/group.go index cecf2a9..eeacc8e 100644 --- a/model/group.go +++ b/model/group.go @@ -91,7 +91,7 @@ func (g *Group) Invite() (string, error) { return serializedInvite, err } -//EncryptMessage takes a message and encrypts the message under the group key. +// EncryptMessage takes a message and encrypts the message under the group key. func (g *Group) EncryptMessage(message *groups.DecryptedGroupMessage) ([]byte, error) { var nonce [24]byte if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil { @@ -210,11 +210,12 @@ func (g *Group) AttemptDecryption(ciphertext []byte, signature []byte) (bool, *g // VerifyGroupMessage confirms the authenticity of a message given an sender onion, message and signature. // The goal of this function is 2-fold: -// 1. We confirm that the sender referenced in the group text is the actual sender of the message (or at least +// 1. We confirm that the sender referenced in the group text is the actual sender of the message (or at least // knows the senders private key) -// 2. Secondly, we confirm that the sender sent the message to a particular group id on a specific server (it doesn't -// matter if we actually received this message from the server or from a hybrid protocol, all that matters is -// that the sender and receivers agree that this message was intended for the group +// 2. Secondly, we confirm that the sender sent the message to a particular group id on a specific server (it doesn't +// matter if we actually received this message from the server or from a hybrid protocol, all that matters is +// that the sender and receivers agree that this message was intended for the group +// // The 2nd point is important as it prevents an attack documented in the original Cwtch paper (and later at // https://docs.openprivacy.ca/cwtch-security-handbook/groups.html) in which a malicious profile sets up 2 groups // on two different servers with the same key and then forwards messages between them to convince the parties in diff --git a/model/message.go b/model/message.go index e622a54..65a66a9 100644 --- a/model/message.go +++ b/model/message.go @@ -99,7 +99,7 @@ func (t *Timeline) SetMessages(messages []Message) { // GetMessagesByHash attempts to find messages that match the given // content hash in the timeline. If successful it returns a list of messages as well as their local index -//, on failure it returns an error. +// , on failure it returns an error. // We return a list of messages because content hashes are not guaranteed to be unique from a given Peer. This allows // us to do things like: ensure that reply-to and quotes reference the last seen message from the message they are quoted // in or detect duplicate messages from a peer. diff --git a/peer/cwtch_peer.go b/peer/cwtch_peer.go index f38a1ff..03b45fb 100644 --- a/peer/cwtch_peer.go +++ b/peer/cwtch_peer.go @@ -13,8 +13,8 @@ import ( "git.openprivacy.ca/openprivacy/connectivity" "git.openprivacy.ca/openprivacy/connectivity/tor" "golang.org/x/crypto/ed25519" - "io/ioutil" "math/bits" + "os" path "path/filepath" "runtime" "strconv" @@ -40,7 +40,8 @@ var autoHandleableEvents = map[event.Type]bool{event.EncryptedGroupMessage: true event.ManifestSizeReceived: true, event.ManifestReceived: true, event.FileDownloaded: true} // DefaultEventsToHandle specifies which events will be subscribed to -// when a peer has its Init() function called +// +// when a peer has its Init() function called var DefaultEventsToHandle = []event.Type{ event.EncryptedGroupMessage, event.NewMessageFromPeerEngine, @@ -108,7 +109,7 @@ func (cp *cwtchPeer) ChangePassword(password string, newpassword string, newpass } cps.Close() - salt, err := ioutil.ReadFile(path.Join(cp.storage.ProfileDirectory, saltFile)) + salt, err := os.ReadFile(path.Join(cp.storage.ProfileDirectory, saltFile)) if err != nil { return err } diff --git a/peer/storage.go b/peer/storage.go index af87888..c69b8e1 100644 --- a/peer/storage.go +++ b/peer/storage.go @@ -12,7 +12,6 @@ import ( "golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/sha3" "io" - "io/ioutil" "os" "path" "path/filepath" @@ -56,12 +55,12 @@ func initV2Directory(directory, password string) ([32]byte, [128]byte, error) { return [32]byte{}, [128]byte{}, err } - if err = ioutil.WriteFile(path.Join(directory, versionFile), []byte(version), 0600); err != nil { + if err = os.WriteFile(path.Join(directory, versionFile), []byte(version), 0600); err != nil { log.Errorf("Could not write version file: %v", err) return [32]byte{}, [128]byte{}, err } - if err = ioutil.WriteFile(path.Join(directory, saltFile), salt[:], 0600); err != nil { + if err = os.WriteFile(path.Join(directory, saltFile), salt[:], 0600); err != nil { log.Errorf("Could not write salt file: %v", err) return [32]byte{}, [128]byte{}, err } @@ -70,7 +69,7 @@ func initV2Directory(directory, password string) ([32]byte, [128]byte, error) { } func openEncryptedDatabase(profileDirectory string, password string, createIfNotExists bool) (*sql.DB, error) { - salt, err := ioutil.ReadFile(path.Join(profileDirectory, saltFile)) + salt, err := os.ReadFile(path.Join(profileDirectory, saltFile)) if err != nil { return nil, err } diff --git a/protocol/files/manifest.go b/protocol/files/manifest.go index 4df5ae7..5478211 100644 --- a/protocol/files/manifest.go +++ b/protocol/files/manifest.go @@ -9,7 +9,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "sync" ) @@ -122,7 +121,7 @@ func (m *Manifest) GetChunkBytes(id uint64) ([]byte, error) { // LoadManifest reads in a json serialized Manifest from a file func LoadManifest(filename string) (*Manifest, error) { - bytes, err := ioutil.ReadFile(filename) + bytes, err := os.ReadFile(filename) if err != nil { return nil, err } @@ -319,7 +318,7 @@ func (m *Manifest) Close() { // Save writes a JSON encoded byte array version of the manifest to path func (m *Manifest) Save(path string) error { - return ioutil.WriteFile(path, m.Serialize(), 0600) + return os.WriteFile(path, m.Serialize(), 0600) } // Serialize returns the manifest as a JSON encoded byte array diff --git a/protocol/files/manifest_test.go b/protocol/files/manifest_test.go index caa14ad..b876139 100644 --- a/protocol/files/manifest_test.go +++ b/protocol/files/manifest_test.go @@ -3,8 +3,8 @@ package files import ( "encoding/hex" "encoding/json" - "io/ioutil" "math" + "os" "testing" ) @@ -77,7 +77,7 @@ func TestManifestLarge(t *testing.T) { t.Logf("%v %s", len(json), json) // Pretend we downloaded the manifest - ioutil.WriteFile("testdata/cwtch.png.manifest", json, 0600) + os.WriteFile("testdata/cwtch.png.manifest", json, 0600) // Load the manifest from a file cwtchPngManifest, err := LoadManifest("testdata/cwtch.png.manifest") diff --git a/storage/v1/file_enc.go b/storage/v1/file_enc.go index b7da95a..728679b 100644 --- a/storage/v1/file_enc.go +++ b/storage/v1/file_enc.go @@ -8,7 +8,7 @@ import ( "golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/sha3" "io" - "io/ioutil" + "os" path "path/filepath" ) @@ -35,7 +35,7 @@ func CreateKey(password string, salt []byte) [32]byte { return dkr } -//EncryptFileData encrypts the data with the supplied key +// EncryptFileData encrypts the data with the supplied key func EncryptFileData(data []byte, key [32]byte) ([]byte, error) { var nonce [24]byte @@ -48,7 +48,7 @@ func EncryptFileData(data []byte, key [32]byte) ([]byte, error) { return encrypted, nil } -//DecryptFile decrypts the passed ciphertext with the supplied key. +// DecryptFile decrypts the passed ciphertext with the supplied key. func DecryptFile(ciphertext []byte, key [32]byte) ([]byte, error) { var decryptNonce [24]byte copy(decryptNonce[:], ciphertext[:24]) @@ -61,7 +61,7 @@ func DecryptFile(ciphertext []byte, key [32]byte) ([]byte, error) { // ReadEncryptedFile reads data from an encrypted file in directory with key func ReadEncryptedFile(directory, filename string, key [32]byte) ([]byte, error) { - encryptedbytes, err := ioutil.ReadFile(path.Join(directory, filename)) + encryptedbytes, err := os.ReadFile(path.Join(directory, filename)) if err == nil { return DecryptFile(encryptedbytes, key) } diff --git a/storage/v1/file_store.go b/storage/v1/file_store.go index 4351f7e..c0ef9e7 100644 --- a/storage/v1/file_store.go +++ b/storage/v1/file_store.go @@ -2,7 +2,6 @@ package v1 import ( "git.openprivacy.ca/openprivacy/log" - "io/ioutil" "os" "path" ) @@ -38,7 +37,7 @@ func (fps *fileStore) Write(data []byte) error { return err } - err = ioutil.WriteFile(path.Join(fps.directory, fps.filename), encryptedbytes, 0600) + err = os.WriteFile(path.Join(fps.directory, fps.filename), encryptedbytes, 0600) return err } diff --git a/storage/v1/profile_store.go b/storage/v1/profile_store.go index f8122b2..32110ba 100644 --- a/storage/v1/profile_store.go +++ b/storage/v1/profile_store.go @@ -5,14 +5,14 @@ import ( "cwtch.im/cwtch/model" "encoding/json" "git.openprivacy.ca/openprivacy/log" - "io/ioutil" + "os" "path" ) const profileFilename = "profile" const saltFile = "SALT" -//ProfileStoreV1 storage for profiles and message streams that uses in memory key and fs stored salt instead of in memory password +// ProfileStoreV1 storage for profiles and message streams that uses in memory key and fs stored salt instead of in memory password type ProfileStoreV1 struct { fs FileStore directory string @@ -24,7 +24,7 @@ type ProfileStoreV1 struct { // LoadProfileWriterStore loads a profile store from filestore listening for events and saving them // directory should be $appDir/profiles/$rand func LoadProfileWriterStore(directory, password string) (*ProfileStoreV1, error) { - salt, err := ioutil.ReadFile(path.Join(directory, saltFile)) + salt, err := os.ReadFile(path.Join(directory, saltFile)) if err != nil { return nil, err } diff --git a/storage/v1/stream_store.go b/storage/v1/stream_store.go index 75ec293..c90c9ee 100644 --- a/storage/v1/stream_store.go +++ b/storage/v1/stream_store.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "git.openprivacy.ca/openprivacy/log" - "io/ioutil" "math" "os" "path" @@ -93,7 +92,7 @@ func (ss *streamStore) updateFile() error { return err } - ioutil.WriteFile(path.Join(ss.storeDirectory, fmt.Sprintf("%s.%d", ss.filenameBase, 0)), encryptedMsgs, 0600) + os.WriteFile(path.Join(ss.storeDirectory, fmt.Sprintf("%s.%d", ss.filenameBase, 0)), encryptedMsgs, 0600) return nil } diff --git a/testing/cwtch_peer_server_integration_test.go b/testing/cwtch_peer_server_integration_test.go index 14fa8fb..06bd589 100644 --- a/testing/cwtch_peer_server_integration_test.go +++ b/testing/cwtch_peer_server_integration_test.go @@ -15,7 +15,6 @@ import ( "git.openprivacy.ca/openprivacy/connectivity/tor" "git.openprivacy.ca/openprivacy/log" _ "github.com/mutecomm/go-sqlcipher/v4" - "io/ioutil" mrand "math/rand" "os" "os/user" @@ -80,7 +79,7 @@ func TestCwtchPeerIntegration(t *testing.T) { } torDataDir := "" - if torDataDir, err = ioutil.TempDir(dataDir, "data-dir-"); err != nil { + if torDataDir, err = os.MkdirTemp(dataDir, "data-dir-"); err != nil { t.Fatalf("could not create data dir") } diff --git a/testing/encryptedstorage/encrypted_storage_integration_test.go b/testing/encryptedstorage/encrypted_storage_integration_test.go index 3e40d5d..27bf862 100644 --- a/testing/encryptedstorage/encrypted_storage_integration_test.go +++ b/testing/encryptedstorage/encrypted_storage_integration_test.go @@ -12,7 +12,6 @@ import ( "git.openprivacy.ca/openprivacy/connectivity/tor" "git.openprivacy.ca/openprivacy/log" _ "github.com/mutecomm/go-sqlcipher/v4" - "io/ioutil" mrand "math/rand" "os" "path" @@ -42,7 +41,7 @@ func TestEncryptedStorage(t *testing.T) { } torDataDir := "" - if torDataDir, err = ioutil.TempDir(dataDir, "data-dir-"); err != nil { + if torDataDir, err = os.MkdirTemp(dataDir, "data-dir-"); err != nil { t.Fatalf("could not create data dir") } diff --git a/testing/filesharing/file_sharing_integration_test.go b/testing/filesharing/file_sharing_integration_test.go index e0570cc..82b7fcf 100644 --- a/testing/filesharing/file_sharing_integration_test.go +++ b/testing/filesharing/file_sharing_integration_test.go @@ -2,13 +2,6 @@ package filesharing import ( "crypto/rand" - utils2 "cwtch.im/cwtch/utils" - "encoding/base64" - "encoding/hex" - "encoding/json" - "fmt" - "io/ioutil" - app2 "cwtch.im/cwtch/app" "cwtch.im/cwtch/event" "cwtch.im/cwtch/functionality/filesharing" @@ -18,6 +11,11 @@ import ( "cwtch.im/cwtch/peer" "cwtch.im/cwtch/protocol/connections" "cwtch.im/cwtch/protocol/files" + utils2 "cwtch.im/cwtch/utils" + "encoding/base64" + "encoding/hex" + "encoding/json" + "fmt" "git.openprivacy.ca/openprivacy/connectivity/tor" "git.openprivacy.ca/openprivacy/log" @@ -77,7 +75,7 @@ func TestFileSharing(t *testing.T) { } torDataDir := "" - if torDataDir, err = ioutil.TempDir(dataDir, "data-dir-"); err != nil { + if torDataDir, err = os.MkdirTemp(dataDir, "data-dir-"); err != nil { t.Fatalf("could not create data dir") }