func (u *gtkUI) chooseKeyToImport(keys map[string][]byte) ([]byte, bool) { result := make(chan int) parsedKeys := make(map[string]otr3.PrivateKey) for v, vv := range keys { _, ok, parsedKey := otr3.ParsePrivateKey(vv) if ok { parsedKeys[v] = parsedKey } } sortedKeys := sortKeys(parsedKeys) doInUIThread(func() { builder := newBuilder("ChooseKeyToImport") d := builder.getObj("dialog").(gtki.Dialog) d.SetTransientFor(u.window) keyBox := builder.getObj("keys").(gtki.ComboBoxText) for _, s := range sortedKeys { kval := parsedKeys[s] keyBox.AppendText(fmt.Sprintf("%s -- %s", s, config.FormatFingerprint(kval.PublicKey().Fingerprint()))) } keyBox.SetActive(0) builder.ConnectSignals(map[string]interface{}{ "on_import_signal": func() { ix := keyBox.GetActive() if ix != -1 { result <- ix close(result) d.Destroy() } }, "on_cancel_signal": func() { close(result) d.Destroy() }, }) d.ShowAll() }) res, ok := <-result if ok { return keys[sortedKeys[res]], true } return nil, false }
func parseFromConfig(cu *config.Account) []otr3.PrivateKey { var result []otr3.PrivateKey allKeys := cu.AllPrivateKeys() log.Printf("Loading %d configured keys", len(allKeys)) for _, pp := range allKeys { _, ok, parsedKey := otr3.ParsePrivateKey(pp) if ok { result = append(result, parsedKey) log.Printf("Loaded key: %s", config.FormatFingerprint(parsedKey.PublicKey().Fingerprint())) } } return result }
func (u *gtkUI) exportKeysFor(account *config.Account, file string) bool { var result []*otr3.Account allKeys := account.AllPrivateKeys() for _, pp := range allKeys { _, ok, parsedKey := otr3.ParsePrivateKey(pp) if ok { result = append(result, &otr3.Account{ Name: account.Account, Protocol: "prpl-jabber", Key: parsedKey, }) } } err := otr3.ExportKeysToFile(result, file) return err == nil }
// This test requires libotr_test_helper.c to be built as /tmp/a.out. func TestAgainstLibOTR(t *testing.T) { limit, err := strconv.Atoi(numIterations) if limit == 0 || err != nil { t.Skip(disabledMessage) } alicePrivateKey, _ := hex.DecodeString(alicePrivateKeyHex) alice := &otr3.Conversation{} alice.Policies.AllowV2() _, _, k := otr3.ParsePrivateKey(alicePrivateKey) alice.SetOurKeys([]otr3.PrivateKey{k}) cmd := exec.Command("/tmp/a.out") cmd.Stderr = os.Stderr out, err := cmd.StdinPipe() if err != nil { t.Fatal(err) } defer out.Close() stdout, err := cmd.StdoutPipe() if err != nil { t.Fatal(err) } defer stdout.Close() in := bufio.NewReader(stdout) if err := cmd.Start(); err != nil { t.Fatal(err) } out.Write([]byte("?OTRv23?")) out.Write([]byte("\n")) var expectedText = []byte("test message") h := &securityEventHandler{} alice.SetSecurityEventHandler(h) for i := 0; i < limit; i++ { line, isPrefix, err := in.ReadLine() if isPrefix { t.Fatal("line from subprocess too long") } if err != nil { t.Fatal(err) } text, alicesMessage, err := alice.Receive(line) if err != nil { t.Fatal(err) } for _, msg := range alicesMessage { out.Write(msg) out.Write([]byte("\n")) } if h.newKeys { h.newKeys = false alicesMessage, err := alice.Send([]byte("Go -> libotr test message")) if err != nil { t.Errorf("error sending message: %s", err.Error()) } else { for _, msg := range alicesMessage { out.Write(msg) out.Write([]byte("\n")) } } } if len(text) > 0 { if !bytes.Equal(text, expectedText) { t.Errorf("expected %x, but got %x", expectedText, text) } if !alice.IsEncrypted() { t.Error("message wasn't encrypted") } } } }