From 8764f03eeca1901fa9b8b2201a943c93c9d8f605 Mon Sep 17 00:00:00 2001 From: Martin Dosch Date: Sun, 24 Apr 2022 00:56:44 +0200 Subject: [PATCH 1/2] Change from encoding/xml to etree. --- iqstructs.go | 22 ---------------------- ox.go | 27 ++++++++++++++++----------- 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/iqstructs.go b/iqstructs.go index c9196dd..fcfda21 100644 --- a/iqstructs.go +++ b/iqstructs.go @@ -7,28 +7,6 @@ package main import "encoding/xml" // Created with https://github.com/miku/zek -type OxMessageElement struct { - XMLName xml.Name `xml:"message"` - Text string `xml:",chardata"` - To string `xml:"to,attr"` - Id string `xml:"id,attr"` - Store struct { - Text string `xml:",chardata"` - Xmlns string `xml:"xmlns,attr"` - } `xml:"store"` - Openpgp struct { - Text string `xml:",chardata"` - Xmlns string `xml:"xmlns,attr"` - } `xml:"openpgp"` - Encryption struct { - XMLName xml.Name `xml:"encryption"` - Text string `xml:",chardata"` - Xmlns string `xml:"xmlns,attr"` - Namespace string `xml:"namespace,attr"` - } - Body string `xml:"body"` -} - // Created with https://github.com/miku/zek // Created with https://github.com/miku/zek type OxPublicKeysList struct { diff --git a/ox.go b/ox.go index 88dbcc8..5fdc4f5 100644 --- a/ox.go +++ b/ox.go @@ -489,7 +489,6 @@ func oxGetPublicKeyRing(client *xmpp.Client, recipient string) (*crypto.KeyRing, func oxEncrypt(client *xmpp.Client, oxPrivKey *crypto.Key, recipient string, keyRing *crypto.KeyRing, message string) (string, error) { - var oxMessage OxMessageElement privKeyRing, err := crypto.NewKeyRing(oxPrivKey) if err != nil { return "error", err @@ -524,18 +523,24 @@ func oxEncrypt(client *xmpp.Client, oxPrivKey *crypto.Key, recipient string, if err != nil { return "error", err } - oxMessage.To = recipient - oxMessage.Id = getID() - oxMessage.Store.Xmlns = nsHints - oxMessage.Openpgp.Text = base64.StdEncoding.EncodeToString(pgpMessage.Data) - oxMessage.Openpgp.Xmlns = nsOx - oxMessage.Encryption.Xmlns = nsEme - oxMessage.Encryption.Namespace = nsOx - oxMessage.Body = oxAltBody - om, err := xml.Marshal(oxMessage) + om := etree.NewDocument() + omMessage := om.CreateElement("message") + omMessage.CreateAttr("to", recipient) + omMessage.CreateAttr("id", getID()) + omMessageStore := omMessage.CreateElement("store") + omMessageStore.CreateAttr("xmlns", nsHints) + omMessageEme := omMessage.CreateElement("encryption") + omMessageEme.CreateAttr("xmlns", nsEme) + omMessageEme.CreateAttr("namespace", nsOx) + omMessageOpgp := omMessage.CreateElement("openpgp") + omMessageOpgp.CreateAttr("xmlns", nsOx) + omMessageOpgp.CreateText(base64.StdEncoding.EncodeToString(pgpMessage.Data)) + omMessageBody := omMessage.CreateElement("body") + omMessageBody.CreateText(oxAltBody) + oms, err := om.WriteToString() if err != nil { return "error", err } - return string(om), nil + return oms, nil } From 8aab6810d29c807b17d3db6c9f280171c7bef637 Mon Sep 17 00:00:00 2001 From: Martin Dosch Date: Sun, 24 Apr 2022 01:20:39 +0200 Subject: [PATCH 2/2] Change from encoding/xml to etree. --- iqstructs.go | 26 -------------------------- ox.go | 20 ++++++++++++++------ 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/iqstructs.go b/iqstructs.go index fcfda21..4e4bbf7 100644 --- a/iqstructs.go +++ b/iqstructs.go @@ -6,32 +6,6 @@ package main import "encoding/xml" -// Created with https://github.com/miku/zek -// Created with https://github.com/miku/zek -// Created with https://github.com/miku/zek -type OxPublicKeysList struct { - XMLName xml.Name `xml:"pubsub"` - Text string `xml:",chardata"` - Xmlns string `xml:"xmlns,attr"` - Items struct { - Text string `xml:",chardata"` - Node string `xml:"node,attr"` - Item struct { - Text string `xml:",chardata"` - ID string `xml:"id,attr"` - PublicKeysList struct { - Text string `xml:",chardata"` - Xmlns string `xml:"xmlns,attr"` - PubkeyMetadata []struct { - Text string `xml:",chardata"` - Date string `xml:"date,attr"` - V4Fingerprint string `xml:"v4-fingerprint,attr"` - } `xml:"pubkey-metadata"` - } `xml:"public-keys-list"` - } `xml:"item"` - } `xml:"items"` -} - // Created with https://github.com/miku/zek type IQDiscoItemsType struct { XMLName xml.Name `xml:"query"` diff --git a/ox.go b/ox.go index 5fdc4f5..0d208c4 100644 --- a/ox.go +++ b/ox.go @@ -6,7 +6,6 @@ package main import ( "encoding/base64" - "encoding/xml" "errors" "log" "os" @@ -373,7 +372,6 @@ func oxRecvPublicKeys(client *xmpp.Client, recipient string, } func oxGetPublicKeyRing(client *xmpp.Client, recipient string) (*crypto.KeyRing, error) { - var oxPublicKeyListXML OxPublicKeysList publicKeyRing, err := crypto.NewKeyRing(nil) if err != nil { return nil, err @@ -397,7 +395,8 @@ func oxGetPublicKeyRing(client *xmpp.Client, recipient string) (*crypto.KeyRing, return nil, errors.New("Error while requesting public openpgp keys for " + recipient) } - err = xml.Unmarshal(oxPublicKeyList.Query, &oxPublicKeyListXML) + oxPubKeyListXML := etree.NewDocument() + err = oxPubKeyListXML.ReadFromBytes(oxPublicKeyList.Query) if err != nil { return nil, err } @@ -407,14 +406,23 @@ func oxGetPublicKeyRing(client *xmpp.Client, recipient string) (*crypto.KeyRing, if err != nil { return nil, err } - for _, r := range oxPublicKeyListXML.Items.Item.PublicKeysList.PubkeyMetadata { - keyDate, err := time.Parse(time.RFC3339, r.Date) + + oxPubKeyListXMLPubsub := oxPubKeyListXML.SelectElement("pubsub") + oxPubKeyListXMLPubsubItems := oxPubKeyListXMLPubsub.SelectElement("items") + oxPubKeyListXMLPubsubItemsItem := oxPubKeyListXMLPubsubItems.SelectElement("item") + oxPubKeyListXMLPubsubItemsItemPkl := oxPubKeyListXMLPubsubItemsItem.SelectElement("public-keys-list") + oxPubKeyListXMLPubsubItemsItemPklPm := + oxPubKeyListXMLPubsubItemsItemPkl.SelectElements("pubkey-metadata") + for _, r := range oxPubKeyListXMLPubsubItemsItemPklPm { + date := r.SelectAttr("date") + fingerprint := r.SelectAttr("v4-fingerprint") + keyDate, err := time.Parse(time.RFC3339, date.Value) if err != nil { return nil, err } if keyDate.After(newestKey) { newestKey = keyDate - pubKeyRingId = r.V4Fingerprint + pubKeyRingId = fingerprint.Value } } if pubKeyRingId == "none" {