예제 #1
0
// Loads and parses a private key.
func loadKey(path string) *otr.PrivateKey {
	establishDir(false)
	base64Key, err := ioutil.ReadFile(path)
	if os.IsNotExist(err) {
		exitPrintf("The private key (%s) does not exist.  Please use genkey.\n",
			path)
	}
	if err != nil {
		exitError(err)
	}

	rawKey := make([]byte, base64.StdEncoding.DecodedLen(len(base64Key)))
	n, err := base64.StdEncoding.Decode(rawKey, base64Key)
	if err != nil {
		exitError(err)
	}

	key := new(otr.PrivateKey)
	if _, ok := key.Parse(rawKey[:n]); !ok {
		exitPrintf("Invalid or corrupted private key (%s).\n", path)
	}

	return key
}
예제 #2
0
func main() {
	flag.Parse()
	log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)

	prv := otr.PrivateKey{}
	if *genKey {
		prv.Generate(rand.Reader)
		fmt.Println(base64.RawStdEncoding.EncodeToString(prv.Serialize(nil)))
		return
	}

	if *prvDataEnc == "" {
		log.Fatalln("You have to provide OTR key")
	}
	prvData, err := base64.RawStdEncoding.DecodeString(*prvDataEnc)
	if err != nil {
		log.Fatalln("Invalid private key's Base64 encoding:", err)
	}
	if _, ok := prv.Parse(prvData); !ok {
		log.Fatalln("Unable to parse private key")
	}
	log.Println("Our fingerprint:", hex.EncodeToString(prv.Fingerprint()))

	var wg sync.WaitGroup
	var ctin *os.File
	var ctout *os.File
	var ptin *os.File
	var ptout *os.File
	opener := func(fdd **os.File, path string, flag int) {
		fd, err := os.OpenFile(path, flag, os.FileMode(0600))
		if err != nil {
			log.Fatalln("Unable to open", path, "file:", err)
		}
		*fdd = fd
		wg.Done()
	}
	wg.Add(1)
	go opener(&ctin, *ctInPath, os.O_RDONLY)
	wg.Add(1)
	go opener(&ptin, *ptInPath, os.O_RDONLY)
	wg.Add(1)
	go opener(&ctout, *ctOutPath, os.O_WRONLY)
	wg.Add(1)
	go opener(&ptout, *ptOutPath, os.O_WRONLY)

	wg.Wait()
	conv := otr.Conversation{PrivateKey: &prv, FragmentSize: MaxMsgSize}
	exited := make(chan struct{})

	go func() {
		scanner := bufio.NewScanner(ptin)
		var err error
		var msg []byte
		var msgs [][]byte
		var t string
		for scanner.Scan() {
			t = scanner.Text()
			if strings.HasPrefix(t, OTRStart) {
				ctout.Write(append([]byte(otr.QueryMessage), '\n'))
				continue
			} else if t == OTRStop {
				msgs = conv.End()
			} else if strings.HasPrefix(t, OTRSMP) {
				ss := strings.SplitN(t, " ", 3)
				var question string
				if len(ss) == 1 {
					log.Println("Invalid SMP usage")
					continue
				} else if len(ss) == 3 {
					question = ss[2]
				}
				msgs, err = conv.Authenticate(question, []byte(ss[1]))
				if err != nil {
					log.Println("Error during authentication:", err)
					continue
				}
			} else {
				msgs, err = conv.Send(scanner.Bytes())
				if err != nil {
					log.Println("Error during sending:", err)
					continue
				}
			}
			for _, msg = range msgs {
				ctout.Write(append(msg, '\n'))
			}
		}
		exited <- struct{}{}
	}()

	go func() {
		scanner := bufio.NewScanner(ctin)
		var got []byte
		var msg []byte
		var enc bool
		var change otr.SecurityChange
		var toSend [][]byte
		var err error
		for scanner.Scan() {
			got, enc, change, toSend, err = conv.Receive(scanner.Bytes())
			if err != nil {
				log.Println("Error during receiving:", err)
				continue
			}
			for _, msg = range toSend {
				ctout.Write(append(msg, '\n'))
			}
			switch change {
			case otr.NewKeys:
				log.Println(
					"OTR established, remote fingerprint:",
					hex.EncodeToString(conv.TheirPublicKey.Fingerprint()),
				)
			case otr.ConversationEnded:
				log.Println("OTR terminated")
			case otr.SMPSecretNeeded:
				log.Println("SMP requested:", conv.SMPQuestion())
			case otr.SMPComplete:
				log.Println("SMP succeeded")
			case otr.SMPFailed:
				log.Println("SMP failed")
			}
			if len(got) > 0 {
				if !enc {
					got = append([]byte("Unencrypted:"), got...)
				}
				ptout.Write(append(got, '\n'))
			}
		}
		exited <- struct{}{}
	}()

	killed := make(chan os.Signal)
	signal.Notify(killed, os.Interrupt, os.Kill)
	select {
	case <-exited:
	case <-killed:
	}
	if conv.IsEncrypted() {
		for _, msg := range conv.End() {
			ctout.Write(append(msg, '\n'))
		}
	}
	ctout.Close()
	ctin.Close()
	ptout.Close()
	ptin.Close()
}