forked from cwtch.im/cwtch
Cwtch_peer.go
Added missing imports and corrected NewCwtchPeer to implement the CwtchPeerInterface Corrected formatting Main.go Corrected formatting App.go Included missing arguments Cwtch_peer_test.go Included password arguments into tests Cwtch_peer_server_intergration_test.go Included password arguments into tests
This commit is contained in:
parent
26043e8b7a
commit
ad996d4fd0
|
@ -34,7 +34,7 @@ func (app *Application) NewProfile(name string, filename string, password string
|
|||
}
|
||||
|
||||
// SetProfile loads an existing profile from the given filename.
|
||||
func (app *Application) SetProfile(filename string) error {
|
||||
func (app *Application) SetProfile(filename string, password string) error {
|
||||
profile, err := peer.LoadCwtchPeer(filename, password)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -9,8 +9,8 @@ import (
|
|||
"time"
|
||||
|
||||
"bytes"
|
||||
"syscall"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
var app app2.Application
|
||||
|
@ -187,7 +187,7 @@ func main() {
|
|||
fmt.Print("** WARNING: PASSWORDS CANNOT BE RECOVERED! **\n")
|
||||
|
||||
password := ""
|
||||
failcount := 0;
|
||||
failcount := 0
|
||||
for ; failcount < 3; failcount++ {
|
||||
fmt.Print("Enter a password to encrypt the profile: ")
|
||||
bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin))
|
||||
|
@ -197,17 +197,17 @@ func main() {
|
|||
}
|
||||
fmt.Print("\nRe-enter password: ")
|
||||
bytePassword2, _ := terminal.ReadPassword(int(syscall.Stdin))
|
||||
if(bytes.Equal(bytePassword, bytePassword2)){
|
||||
if bytes.Equal(bytePassword, bytePassword2) {
|
||||
password = string(bytePassword)
|
||||
break
|
||||
}else{
|
||||
} else {
|
||||
fmt.Print("\nPASSWORDS DIDN'T MATCH! Try again.\n")
|
||||
}
|
||||
}
|
||||
|
||||
if(failcount >= 3){
|
||||
if failcount >= 3 {
|
||||
fmt.Printf("Error creating profile for %v: Your password entries must match!\n", commands[1])
|
||||
}else{
|
||||
} else {
|
||||
err := app.NewProfile(commands[1], commands[2], password)
|
||||
profilefile = commands[2]
|
||||
if err == nil {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package peer
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"cwtch.im/cwtch/model"
|
||||
"cwtch.im/cwtch/peer/connections"
|
||||
|
@ -9,11 +10,17 @@ import (
|
|||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"fmt"
|
||||
"git.openprivacy.ca/openprivacy/libricochet-go/application"
|
||||
"git.openprivacy.ca/openprivacy/libricochet-go/channels"
|
||||
"git.openprivacy.ca/openprivacy/libricochet-go/connection"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/ulule/deepcopier"
|
||||
"golang.org/x/crypto/ed25519"
|
||||
"golang.org/x/crypto/nacl/secretbox"
|
||||
"golang.org/x/crypto/pbkdf2"
|
||||
"golang.org/x/crypto/sha3"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"strings"
|
||||
|
@ -67,18 +74,17 @@ type CwtchPeerInterface interface {
|
|||
Shutdown()
|
||||
}
|
||||
|
||||
// CreateHash hashes the user's password to a length suitable for file encryption
|
||||
// CreateKey derives a key and salt for use in encrypting the Profile
|
||||
func CreateKey(key string) ([32]byte, [128]byte) {
|
||||
var salt [128]byte
|
||||
if _, err := io.ReadFull(rand.Reader, salt[:]);
|
||||
err != nil {
|
||||
var salt [128]byte
|
||||
if _, err := io.ReadFull(rand.Reader, salt[:]); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
dk := pbkdf2.Key([]byte(key), salt[:], 4096, 32, sha3.New512)
|
||||
}
|
||||
dk := pbkdf2.Key([]byte(key), salt[:], 4096, 32, sha3.New512)
|
||||
|
||||
var dkr [32]byte
|
||||
copy(dkr[:], dk)
|
||||
return dkr, salt
|
||||
var dkr [32]byte
|
||||
copy(dkr[:], dk)
|
||||
return dkr, salt
|
||||
}
|
||||
|
||||
//EncryptMessage takes a message and encrypts the message under the group key.
|
||||
|
@ -101,7 +107,7 @@ func EncryptProfile(p *cwtchPeer, password [32]byte) []byte {
|
|||
}
|
||||
|
||||
//EncryptMessage takes a message and encrypts the message under the group key.
|
||||
func DecryptProfile(ciphertext []byte, password [32]byte) (error, *cwtchPeer){
|
||||
func DecryptProfile(ciphertext []byte, password [32]byte) (error, *cwtchPeer) {
|
||||
|
||||
var decryptNonce [24]byte
|
||||
copy(decryptNonce[:], ciphertext[:24])
|
||||
|
@ -139,7 +145,7 @@ func (cp *cwtchPeer) setup() {
|
|||
}
|
||||
|
||||
// NewCwtchPeer creates and returns a new CwtchPeer with the given name.
|
||||
func NewCwtchPeer(name string, password string) *cwtchPeer {
|
||||
func NewCwtchPeer(name string, password string) CwtchPeerInterface {
|
||||
cp := new(cwtchPeer)
|
||||
cp.Profile = model.GenerateNewProfile(name)
|
||||
cp.setup()
|
||||
|
@ -153,7 +159,7 @@ func NewCwtchPeer(name string, password string) *cwtchPeer {
|
|||
func (cp *cwtchPeer) Save(profilefile string) error {
|
||||
cp.mutex.Lock()
|
||||
encryptedbytes := EncryptProfile(cp, cp.password)
|
||||
encryptedbytes = append(cp.salt[:],encryptedbytes...)
|
||||
encryptedbytes = append(cp.salt[:], encryptedbytes...)
|
||||
err := ioutil.WriteFile(profilefile, encryptedbytes, 0600)
|
||||
cp.profilefile = profilefile
|
||||
cp.mutex.Unlock()
|
||||
|
|
|
@ -6,10 +6,10 @@ import (
|
|||
|
||||
func TestCwtchPeerGenerate(t *testing.T) {
|
||||
|
||||
alice := NewCwtchPeer("alice")
|
||||
alice := NewCwtchPeer("alice","testpass")
|
||||
alice.Save("./test_profile")
|
||||
|
||||
aliceLoaded, err := LoadCwtchPeer("./test_profile")
|
||||
aliceLoaded, err := LoadCwtchPeer("./test_profile","testpass")
|
||||
if err != nil || aliceLoaded.GetProfile().Name != "alice" {
|
||||
t.Errorf("something went wrong saving and loading profiles %v %v", err, aliceLoaded)
|
||||
}
|
||||
|
|
|
@ -143,17 +143,17 @@ func TestCwtchPeerIntegration(t *testing.T) {
|
|||
// ***** Peer setup *****
|
||||
|
||||
fmt.Println("Creating Alice...")
|
||||
alice := peer.NewCwtchPeer("Alice")
|
||||
alice := peer.NewCwtchPeer("Alice","alicepass")
|
||||
go alice.Listen()
|
||||
fmt.Println("Alice created:", alice.GetProfile().Onion)
|
||||
|
||||
fmt.Println("Creating Bob...")
|
||||
bob := peer.NewCwtchPeer("Bob")
|
||||
bob := peer.NewCwtchPeer("Bob","bobpass")
|
||||
go bob.Listen()
|
||||
fmt.Println("Bob created:", bob.GetProfile().Onion)
|
||||
|
||||
fmt.Println("Creating Carol...")
|
||||
carol := peer.NewCwtchPeer("Carol")
|
||||
carol := peer.NewCwtchPeer("Carol","carolpass")
|
||||
go carol.Listen()
|
||||
fmt.Println("Carol created:", carol.GetProfile().Onion)
|
||||
|
||||
|
|
Loading…
Reference in New Issue