Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to importing libotr file containing multiple private keys #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/url"
"strconv"
"strings"

"github.com/agl/xmpp-client/xmpp"
"github.com/juniorz/otr-keychain"
"golang.org/x/crypto/otr"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/net/proxy"
Expand Down Expand Up @@ -152,26 +154,51 @@ func enroll(config *Config, term *terminal.Terminal) bool {
config.UseTor = true
}

term.SetPrompt("File to import libotr private key from (enter to generate): ")

var priv otr.PrivateKey
for {
term.SetPrompt("File to import libotr private key from (enter to generate): ")
importFile, err := term.ReadLine()
if err != nil {
return false
}

if len(importFile) > 0 {
privKeyBytes, err := ioutil.ReadFile(importFile)
if err != nil {
alert(term, "Failed to open private key file: "+err.Error())
continue
}

if !priv.Import(privKeyBytes) {
privKeys := keychain.ImportFromLibOTR(privKeyBytes)
numKeys := len(privKeys)
if numKeys == 0 {
alert(term, "Failed to parse libotr private key file (the parser is pretty simple I'm afraid)")
continue
}
break

if numKeys == 1 {
priv = privKeys[0]
break
}

info(term, fmt.Sprintf("%d keys found:", numKeys))
for i, k := range privKeys {
info(term, fmt.Sprintf("(%d) fingerprint: %s", i+1, hex.EncodeToString(k.Fingerprint())))
}

term.SetPrompt("Choose one to import (enter to choose another file): ")

if otrKeyQuery, err := term.ReadLine(); err != nil || len(otrKeyQuery) == 0 || otrKeyQuery[0] == '0' {
continue
} else {
if i, err := strconv.Atoi(otrKeyQuery); err != nil || i > numKeys || i < 1 {
continue
} else {
priv = privKeys[i-1]
info(term, fmt.Sprintf("Importing private key with fingerprint %s", hex.EncodeToString(priv.Fingerprint())))
break
}
}
} else {
info(term, "Generating private key...")
priv.Generate(rand.Reader)
Expand Down