Don't return key after generation

ox
Martin Dosch 2 years ago
parent 0d6b36e500
commit 635e681b21

@ -16,9 +16,9 @@ import (
"strings" "strings"
"time" "time"
"github.com/ProtonMail/gopenpgp/v2/crypto" // MIT License _ "github.com/ProtonMail/gopenpgp/v2/crypto" // MIT License
"github.com/mattn/go-xmpp" // BSD-3-Clause "github.com/mattn/go-xmpp" // BSD-3-Clause
"github.com/pborman/getopt/v2" // BSD-3-Clause "github.com/pborman/getopt/v2" // BSD-3-Clause
) )
type configuration struct { type configuration struct {
@ -258,18 +258,14 @@ func main() {
} }
if *flagOxGenPrivKey != "" { if *flagOxGenPrivKey != "" {
var oxPrivKey *crypto.Key
validatedOwnJid, err := MarshalJID(*flagOxGenPrivKey) validatedOwnJid, err := MarshalJID(*flagOxGenPrivKey)
if err != nil { if err != nil {
// log.Fatal("Invalid JID:", *flagOxGenPrivKey)
log.Fatal(err) log.Fatal(err)
} }
oxPrivKey, err = oxGenPrivKey(validatedOwnJid, client) err = oxGenPrivKey(validatedOwnJid, client)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
// Print oxPrivKey to be able to compile as it is not yet used.
println(oxPrivKey)
os.Exit(0) os.Exit(0)
} }

27
ox.go

@ -72,25 +72,24 @@ func oxStorePrivKey(jid string, privKey string) error {
return nil return nil
} }
func oxGenPrivKey(jid string, client *xmpp.Client) (*crypto.Key, error) { func oxGenPrivKey(jid string, client *xmpp.Client) error {
var iqOxPublishKey IQoxPublishKeyType var iqOxPublishKey IQoxPublishKeyType
var iqOxPublishKeyList IQoxPublishKeyListType var iqOxPublishKeyList IQoxPublishKeyListType
xmppUri := "xmpp:" + jid xmppUri := "xmpp:" + jid
key, err := crypto.GenerateKey(xmppUri, xmppUri, "x25519", 0) key, err := crypto.GenerateKey(xmppUri, xmppUri, "x25519", 0)
if err != nil { if err != nil {
return nil, err return err
} }
keySerialized, _ := key.Serialize() keySerialized, _ := key.Serialize()
pubKey, err := key.GetPublicKey() pubKey, err := key.GetPublicKey()
if err != nil { if err != nil {
return nil, err return err
} }
pubKeyBase64 := base64.StdEncoding.EncodeToString(pubKey) pubKeyBase64 := base64.StdEncoding.EncodeToString(pubKey)
err = oxStorePrivKey(jid, err = oxStorePrivKey(jid,
base64.StdEncoding.EncodeToString(keySerialized)) base64.StdEncoding.EncodeToString(keySerialized))
if err != nil { if err != nil {
// return nil, errors.New("Couldn't store private key:")
log.Fatal(err) log.Fatal(err)
} }
fingerprint := key.GetFingerprint() fingerprint := key.GetFingerprint()
@ -109,14 +108,14 @@ func oxGenPrivKey(jid string, client *xmpp.Client) (*crypto.Key, error) {
opk, err := xml.Marshal(iqOxPublishKey) opk, err := xml.Marshal(iqOxPublishKey)
if err != nil { if err != nil {
return nil, err return err
} }
iqReply, err := sendIQ(client, jid, "set", string(opk)) iqReply, err := sendIQ(client, jid, "set", string(opk))
if err != nil { if err != nil {
return nil, err return err
} }
if iqReply.Type != "result" { if iqReply.Type != "result" {
return nil, errors.New("Error whil publishing public key.") return errors.New("Error whil publishing public key.")
} }
// TODO: Split GetPublicKey in GetPublicKeyList and GetPublicKey // TODO: Split GetPublicKey in GetPublicKeyList and GetPublicKey
@ -125,14 +124,14 @@ func oxGenPrivKey(jid string, client *xmpp.Client) (*crypto.Key, error) {
// upload. // upload.
ownPubKeyFromPubsub, err := oxRecvPublicKey(client, jid, fingerprint) ownPubKeyFromPubsub, err := oxRecvPublicKey(client, jid, fingerprint)
if err != nil { if err != nil {
return nil, errors.New("Couldn't successfully verify public key upload.") return errors.New("Couldn't successfully verify public key upload.")
} }
ownPubKeyFromPubsubSerialized, err := ownPubKeyFromPubsub.Serialize() ownPubKeyFromPubsubSerialized, err := ownPubKeyFromPubsub.Serialize()
if err != nil { if err != nil {
return nil, errors.New("Couldn't successfully verify public key upload.") return errors.New("Couldn't successfully verify public key upload.")
} }
if pubKeyBase64 != base64.StdEncoding.EncodeToString(ownPubKeyFromPubsubSerialized) { if pubKeyBase64 != base64.StdEncoding.EncodeToString(ownPubKeyFromPubsubSerialized) {
return nil, errors.New("Couldn't successfully verify public key upload.") return errors.New("Couldn't successfully verify public key upload.")
} }
iqOxPublishKeyList.Xmlns = nsPubsub iqOxPublishKeyList.Xmlns = nsPubsub
@ -150,17 +149,17 @@ func oxGenPrivKey(jid string, client *xmpp.Client) (*crypto.Key, error) {
opkl, err := xml.Marshal(iqOxPublishKeyList) opkl, err := xml.Marshal(iqOxPublishKeyList)
if err != nil { if err != nil {
return nil, err return err
} }
iqReply, err = sendIQ(client, jid, "set", string(opkl)) iqReply, err = sendIQ(client, jid, "set", string(opkl))
if err != nil { if err != nil {
return nil, err return err
} }
if iqReply.Type != "result" { if iqReply.Type != "result" {
return nil, errors.New("Error while publishing public key list.") return errors.New("Error while publishing public key list.")
} }
return key, nil return nil
} }
func oxRecvPublicKey(client *xmpp.Client, recipient string, fingerprint string) (*crypto.Key, error) { func oxRecvPublicKey(client *xmpp.Client, recipient string, fingerprint string) (*crypto.Key, error) {

Loading…
Cancel
Save