2018-10-15 00:59:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"crypto/rand"
|
|
|
|
libpeer "cwtch.im/cwtch/peer"
|
2018-10-06 03:50:55 +00:00
|
|
|
storage2 "cwtch.im/cwtch/storage"
|
2018-10-15 00:59:53 +00:00
|
|
|
"fmt"
|
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/utils"
|
2018-10-06 03:50:55 +00:00
|
|
|
|
2018-12-04 02:52:11 +00:00
|
|
|
"errors"
|
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
2018-10-15 00:59:53 +00:00
|
|
|
"golang.org/x/crypto/ed25519"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-12-04 02:52:11 +00:00
|
|
|
func convertCwtchFile(filename string, password string) error {
|
2018-10-06 03:50:55 +00:00
|
|
|
fileStore := storage2.CreateFileProfileStore(filename, password)
|
|
|
|
peer, err := fileStore.Load()
|
2018-10-15 00:59:53 +00:00
|
|
|
if err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
return err
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b := []byte("== ed25519v1-secret: type0 ==")
|
|
|
|
b = append(b, peer.GetProfile().Ed25519PrivateKey...)
|
|
|
|
err = ioutil.WriteFile("hs_ed25519_secret_key", b, 0600)
|
|
|
|
if err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
return err
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b = []byte("== ed25519v1-public: type0 ==")
|
|
|
|
b = append(b, peer.GetProfile().Ed25519PublicKey...)
|
|
|
|
err = ioutil.WriteFile("hs_ed25519_public_key", b, 0600)
|
|
|
|
if err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
return err
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b = []byte(peer.GetProfile().Onion + ".onion\n")
|
|
|
|
err = ioutil.WriteFile("hostname", b, 0600)
|
|
|
|
if err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
return err
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Infoln("success!")
|
|
|
|
return nil
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 02:52:11 +00:00
|
|
|
func convertTorFile(filename string, password string) error {
|
|
|
|
return errors.New("this code doesn't work and can never work :( it's a math thing")
|
2018-10-15 00:59:53 +00:00
|
|
|
|
2018-12-04 02:52:11 +00:00
|
|
|
/*name, _ := diceware.Generate(2)
|
2018-10-15 00:59:53 +00:00
|
|
|
sk, err := ioutil.ReadFile("hs_ed25519_secret_key")
|
|
|
|
if err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
return err
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
sk = sk[32:]
|
|
|
|
|
|
|
|
pk, err := ioutil.ReadFile("hs_ed25519_public_key")
|
|
|
|
if err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
return err
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
pk = pk[32:]
|
|
|
|
|
|
|
|
onion, err := ioutil.ReadFile("hostname")
|
|
|
|
if err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
return err
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
onion = onion[:56]
|
|
|
|
|
2018-10-06 03:50:55 +00:00
|
|
|
peer := libpeer.NewCwtchPeer(strings.Join(name, "-"))
|
2018-10-15 00:59:53 +00:00
|
|
|
|
|
|
|
fmt.Printf("%d %d %s\n", len(peer.GetProfile().Ed25519PublicKey), len(peer.GetProfile().Ed25519PrivateKey), peer.GetProfile().Onion)
|
|
|
|
peer.GetProfile().Ed25519PrivateKey = sk
|
|
|
|
peer.GetProfile().Ed25519PublicKey = pk
|
|
|
|
peer.GetProfile().Onion = string(onion)
|
2018-10-06 03:50:55 +00:00
|
|
|
fileStore := storage2.CreateFileProfileStore(filename, password)
|
|
|
|
err = fileStore.Save(peer)
|
|
|
|
if err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
return err
|
2018-10-06 03:50:55 +00:00
|
|
|
}
|
2018-10-15 00:59:53 +00:00
|
|
|
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Infof("success! loaded %d byte pk and %d byte sk for %s.onion\n", len(pk), len(sk), onion)
|
|
|
|
return nil*/
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 02:52:11 +00:00
|
|
|
func vanity() error {
|
2018-10-15 00:59:53 +00:00
|
|
|
for {
|
|
|
|
pk, sk, err := ed25519.GenerateKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
onion := utils.GetTorV3Hostname(pk)
|
|
|
|
for i := 4; i < len(os.Args); i++ {
|
|
|
|
if strings.HasPrefix(onion, os.Args[i]) {
|
2018-10-06 03:50:55 +00:00
|
|
|
peer := libpeer.NewCwtchPeer(os.Args[i])
|
2018-10-15 00:59:53 +00:00
|
|
|
peer.GetProfile().Ed25519PrivateKey = sk
|
|
|
|
peer.GetProfile().Ed25519PublicKey = pk
|
|
|
|
peer.GetProfile().Onion = onion
|
2018-10-06 03:50:55 +00:00
|
|
|
fileStore := storage2.CreateFileProfileStore(os.Args[3], onion+".cwtch")
|
|
|
|
err := fileStore.Save(peer)
|
|
|
|
if err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
return err
|
2018-10-06 03:50:55 +00:00
|
|
|
}
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Infof("found %s.onion\n", onion)
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func printHelp() {
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Infoln("usage: cwtchutil {help, convert-cwtch-file, convert-tor-file, changepw, vanity}")
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2018-12-04 02:52:11 +00:00
|
|
|
log.SetLevel(log.LevelInfo)
|
2018-10-15 00:59:53 +00:00
|
|
|
if len(os.Args) < 2 {
|
|
|
|
printHelp()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch os.Args[1] {
|
|
|
|
default:
|
|
|
|
printHelp()
|
|
|
|
case "help":
|
|
|
|
printHelp()
|
|
|
|
case "convert-cwtch-file":
|
|
|
|
if len(os.Args) != 4 {
|
2018-12-04 02:52:11 +00:00
|
|
|
fmt.Println("example: cwtchutil convert-cwtch-file ~/.cwtch/profiles/11ddd78a9918c064e742d5e36a8b8fd4 passw0rd")
|
2018-10-15 00:59:53 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-12-04 02:52:11 +00:00
|
|
|
err := convertCwtchFile(os.Args[2], os.Args[3])
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
2018-10-15 00:59:53 +00:00
|
|
|
case "convert-tor-file":
|
|
|
|
if len(os.Args) != 4 {
|
2018-12-04 02:52:11 +00:00
|
|
|
fmt.Println("example: cwtchutil convert-tor-file /var/lib/tor/hs1 passw0rd")
|
2018-10-15 00:59:53 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-12-04 02:52:11 +00:00
|
|
|
err := convertTorFile(os.Args[2], os.Args[3])
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
2018-10-15 00:59:53 +00:00
|
|
|
case "vanity":
|
|
|
|
if len(os.Args) < 5 {
|
2018-12-04 02:52:11 +00:00
|
|
|
fmt.Println("example: cwtchutil vanity 4 passw0rd erinn openpriv")
|
2018-10-15 00:59:53 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
goroutines, err := strconv.Atoi(os.Args[2])
|
|
|
|
if err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Errorf("first parameter after vanity should be a number\n")
|
2018-10-15 00:59:53 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Infoln("searching. press ctrl+c to stop")
|
2018-10-15 00:59:53 +00:00
|
|
|
for i := 0; i < goroutines; i++ {
|
|
|
|
go vanity()
|
|
|
|
}
|
|
|
|
|
|
|
|
for { // run until ctrl+c
|
|
|
|
time.Sleep(time.Hour * 24)
|
|
|
|
}
|
|
|
|
case "changepw":
|
|
|
|
if len(os.Args) != 3 {
|
2018-12-04 02:52:11 +00:00
|
|
|
fmt.Println("example: cwtch changepw ~/.cwtch/profiles/XXX")
|
|
|
|
os.Exit(1)
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("old password: ")
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
pw, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Errorln(err)
|
|
|
|
os.Exit(1)
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
pw = pw[:len(pw)-1]
|
|
|
|
|
2018-10-06 03:50:55 +00:00
|
|
|
fileStore := storage2.CreateFileProfileStore(os.Args[2], pw)
|
|
|
|
|
|
|
|
peer, err := fileStore.Load()
|
2018-10-15 00:59:53 +00:00
|
|
|
if err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Errorln(err)
|
|
|
|
os.Exit(1)
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("new password: ")
|
|
|
|
newpw1, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Errorln(err)
|
|
|
|
os.Exit(1)
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
newpw1 = newpw1[:len(newpw1)-1] // fuck go with this linebreak shit ^ea
|
|
|
|
|
2018-10-06 03:50:55 +00:00
|
|
|
fileStore2 := storage2.CreateFileProfileStore(os.Args[2], newpw1)
|
|
|
|
err = fileStore2.Save(peer)
|
2018-10-15 00:59:53 +00:00
|
|
|
if err != nil {
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Errorln(err)
|
|
|
|
os.Exit(1)
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Infoln("success!")
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
}
|