Change ioutil -> os APIs

This commit is contained in:
Sarah Jamie Lewis 2022-09-06 12:41:52 -07:00
parent 5765cfd6c4
commit 79bf060c2f
15 changed files with 36 additions and 44 deletions

View File

@ -11,7 +11,6 @@ import (
"cwtch.im/cwtch/storage" "cwtch.im/cwtch/storage"
"git.openprivacy.ca/openprivacy/connectivity" "git.openprivacy.ca/openprivacy/connectivity"
"git.openprivacy.ca/openprivacy/log" "git.openprivacy.ca/openprivacy/log"
"io/ioutil"
"os" "os"
path "path/filepath" path "path/filepath"
"strconv" "strconv"
@ -140,7 +139,7 @@ func (app *application) LoadProfiles(password string) {
count := 0 count := 0
migrating := false migrating := false
files, err := ioutil.ReadDir(path.Join(app.directory, "profiles")) files, err := os.ReadDir(path.Join(app.directory, "profiles"))
if err != nil { if err != nil {
log.Errorf("error: cannot read profiles directory: %v", err) log.Errorf("error: cannot read profiles directory: %v", err)
return return

View File

@ -121,7 +121,6 @@ func (nc *networkCheck) selfTest() {
go nc.checkConnection(nc.onion) go nc.checkConnection(nc.onion)
} }
//
func (nc *networkCheck) checkConnection(onion string) { func (nc *networkCheck) checkConnection(onion string) {
prog, _ := nc.acn.GetBootstrapStatus() prog, _ := nc.acn.GetBootstrapStatus()
if prog != 100 { if prog != 100 {

View File

@ -91,7 +91,7 @@ func (g *Group) Invite() (string, error) {
return serializedInvite, err 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) { func (g *Group) EncryptMessage(message *groups.DecryptedGroupMessage) ([]byte, error) {
var nonce [24]byte var nonce [24]byte
if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil { 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. // VerifyGroupMessage confirms the authenticity of a message given an sender onion, message and signature.
// The goal of this function is 2-fold: // 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) // 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 // 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 // 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 // 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 // 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 // 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 // on two different servers with the same key and then forwards messages between them to convince the parties in

View File

@ -99,7 +99,7 @@ func (t *Timeline) SetMessages(messages []Message) {
// GetMessagesByHash attempts to find messages that match the given // 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 // 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 // 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 // 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. // in or detect duplicate messages from a peer.

View File

@ -13,8 +13,8 @@ import (
"git.openprivacy.ca/openprivacy/connectivity" "git.openprivacy.ca/openprivacy/connectivity"
"git.openprivacy.ca/openprivacy/connectivity/tor" "git.openprivacy.ca/openprivacy/connectivity/tor"
"golang.org/x/crypto/ed25519" "golang.org/x/crypto/ed25519"
"io/ioutil"
"math/bits" "math/bits"
"os"
path "path/filepath" path "path/filepath"
"runtime" "runtime"
"strconv" "strconv"
@ -40,7 +40,8 @@ var autoHandleableEvents = map[event.Type]bool{event.EncryptedGroupMessage: true
event.ManifestSizeReceived: true, event.ManifestReceived: true, event.FileDownloaded: true} event.ManifestSizeReceived: true, event.ManifestReceived: true, event.FileDownloaded: true}
// DefaultEventsToHandle specifies which events will be subscribed to // 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{ var DefaultEventsToHandle = []event.Type{
event.EncryptedGroupMessage, event.EncryptedGroupMessage,
event.NewMessageFromPeerEngine, event.NewMessageFromPeerEngine,
@ -108,7 +109,7 @@ func (cp *cwtchPeer) ChangePassword(password string, newpassword string, newpass
} }
cps.Close() 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 { if err != nil {
return err return err
} }

View File

@ -12,7 +12,6 @@ import (
"golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
"io" "io"
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -56,12 +55,12 @@ func initV2Directory(directory, password string) ([32]byte, [128]byte, error) {
return [32]byte{}, [128]byte{}, err 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) log.Errorf("Could not write version file: %v", err)
return [32]byte{}, [128]byte{}, 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) log.Errorf("Could not write salt file: %v", err)
return [32]byte{}, [128]byte{}, 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) { 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -9,7 +9,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"sync" "sync"
) )
@ -122,7 +121,7 @@ func (m *Manifest) GetChunkBytes(id uint64) ([]byte, error) {
// LoadManifest reads in a json serialized Manifest from a file // LoadManifest reads in a json serialized Manifest from a file
func LoadManifest(filename string) (*Manifest, error) { func LoadManifest(filename string) (*Manifest, error) {
bytes, err := ioutil.ReadFile(filename) bytes, err := os.ReadFile(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -319,7 +318,7 @@ func (m *Manifest) Close() {
// Save writes a JSON encoded byte array version of the manifest to path // Save writes a JSON encoded byte array version of the manifest to path
func (m *Manifest) Save(path string) error { 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 // Serialize returns the manifest as a JSON encoded byte array

View File

@ -3,8 +3,8 @@ package files
import ( import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"io/ioutil"
"math" "math"
"os"
"testing" "testing"
) )
@ -77,7 +77,7 @@ func TestManifestLarge(t *testing.T) {
t.Logf("%v %s", len(json), json) t.Logf("%v %s", len(json), json)
// Pretend we downloaded the manifest // 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 // Load the manifest from a file
cwtchPngManifest, err := LoadManifest("testdata/cwtch.png.manifest") cwtchPngManifest, err := LoadManifest("testdata/cwtch.png.manifest")

View File

@ -8,7 +8,7 @@ import (
"golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
"io" "io"
"io/ioutil" "os"
path "path/filepath" path "path/filepath"
) )
@ -35,7 +35,7 @@ func CreateKey(password string, salt []byte) [32]byte {
return dkr 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) { func EncryptFileData(data []byte, key [32]byte) ([]byte, error) {
var nonce [24]byte var nonce [24]byte
@ -48,7 +48,7 @@ func EncryptFileData(data []byte, key [32]byte) ([]byte, error) {
return encrypted, nil 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) { func DecryptFile(ciphertext []byte, key [32]byte) ([]byte, error) {
var decryptNonce [24]byte var decryptNonce [24]byte
copy(decryptNonce[:], ciphertext[:24]) 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 // ReadEncryptedFile reads data from an encrypted file in directory with key
func ReadEncryptedFile(directory, filename string, key [32]byte) ([]byte, error) { 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 { if err == nil {
return DecryptFile(encryptedbytes, key) return DecryptFile(encryptedbytes, key)
} }

View File

@ -2,7 +2,6 @@ package v1
import ( import (
"git.openprivacy.ca/openprivacy/log" "git.openprivacy.ca/openprivacy/log"
"io/ioutil"
"os" "os"
"path" "path"
) )
@ -38,7 +37,7 @@ func (fps *fileStore) Write(data []byte) error {
return err 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 return err
} }

View File

@ -5,14 +5,14 @@ import (
"cwtch.im/cwtch/model" "cwtch.im/cwtch/model"
"encoding/json" "encoding/json"
"git.openprivacy.ca/openprivacy/log" "git.openprivacy.ca/openprivacy/log"
"io/ioutil" "os"
"path" "path"
) )
const profileFilename = "profile" const profileFilename = "profile"
const saltFile = "SALT" 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 { type ProfileStoreV1 struct {
fs FileStore fs FileStore
directory string directory string
@ -24,7 +24,7 @@ type ProfileStoreV1 struct {
// LoadProfileWriterStore loads a profile store from filestore listening for events and saving them // LoadProfileWriterStore loads a profile store from filestore listening for events and saving them
// directory should be $appDir/profiles/$rand // directory should be $appDir/profiles/$rand
func LoadProfileWriterStore(directory, password string) (*ProfileStoreV1, error) { 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"git.openprivacy.ca/openprivacy/log" "git.openprivacy.ca/openprivacy/log"
"io/ioutil"
"math" "math"
"os" "os"
"path" "path"
@ -93,7 +92,7 @@ func (ss *streamStore) updateFile() error {
return err 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 return nil
} }

View File

@ -15,7 +15,6 @@ import (
"git.openprivacy.ca/openprivacy/connectivity/tor" "git.openprivacy.ca/openprivacy/connectivity/tor"
"git.openprivacy.ca/openprivacy/log" "git.openprivacy.ca/openprivacy/log"
_ "github.com/mutecomm/go-sqlcipher/v4" _ "github.com/mutecomm/go-sqlcipher/v4"
"io/ioutil"
mrand "math/rand" mrand "math/rand"
"os" "os"
"os/user" "os/user"
@ -80,7 +79,7 @@ func TestCwtchPeerIntegration(t *testing.T) {
} }
torDataDir := "" 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") t.Fatalf("could not create data dir")
} }

View File

@ -12,7 +12,6 @@ import (
"git.openprivacy.ca/openprivacy/connectivity/tor" "git.openprivacy.ca/openprivacy/connectivity/tor"
"git.openprivacy.ca/openprivacy/log" "git.openprivacy.ca/openprivacy/log"
_ "github.com/mutecomm/go-sqlcipher/v4" _ "github.com/mutecomm/go-sqlcipher/v4"
"io/ioutil"
mrand "math/rand" mrand "math/rand"
"os" "os"
"path" "path"
@ -42,7 +41,7 @@ func TestEncryptedStorage(t *testing.T) {
} }
torDataDir := "" 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") t.Fatalf("could not create data dir")
} }

View File

@ -2,13 +2,6 @@ package filesharing
import ( import (
"crypto/rand" "crypto/rand"
utils2 "cwtch.im/cwtch/utils"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
app2 "cwtch.im/cwtch/app" app2 "cwtch.im/cwtch/app"
"cwtch.im/cwtch/event" "cwtch.im/cwtch/event"
"cwtch.im/cwtch/functionality/filesharing" "cwtch.im/cwtch/functionality/filesharing"
@ -18,6 +11,11 @@ import (
"cwtch.im/cwtch/peer" "cwtch.im/cwtch/peer"
"cwtch.im/cwtch/protocol/connections" "cwtch.im/cwtch/protocol/connections"
"cwtch.im/cwtch/protocol/files" "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/connectivity/tor"
"git.openprivacy.ca/openprivacy/log" "git.openprivacy.ca/openprivacy/log"
@ -77,7 +75,7 @@ func TestFileSharing(t *testing.T) {
} }
torDataDir := "" 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") t.Fatalf("could not create data dir")
} }