Merge branch 'master' into code-cleanup

This commit is contained in:
Martin Dosch 2022-04-24 00:33:35 +02:00
commit 5d19a6ce12
5 changed files with 36 additions and 49 deletions

View File

@ -4,6 +4,9 @@
### Added
- Experimental support for Ox (OpenPGP for XMPP) encryption.
### Changed
- Use shorter random ID for resource.
## [v0.3.0] 2022-03-21
### Added
- Added support for joining password protected MUCs.

View File

@ -49,3 +49,13 @@ func getID() string {
id := fmt.Sprintf("%x-%x-%x", b[0:4], b[4:8], b[8:])
return id
}
func getShortID() string {
b := make([]byte, 4)
_, err := rand.Read(b)
if err != nil {
log.Fatal(err)
}
id := fmt.Sprintf("%x", b[0:4])
return id
}

View File

@ -30,28 +30,6 @@ type OxMessageElement struct {
}
// Created with https://github.com/miku/zek
type OxCryptElement struct {
XMLName xml.Name `xml:"signcrypt"`
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
To struct {
Text string `xml:",chardata"`
Jid string `xml:"jid,attr"`
} `xml:"to"`
Time struct {
Text string `xml:",chardata"`
Stamp string `xml:"stamp,attr"`
} `xml:"time"`
Rpad string `xml:"rpad"`
Payload struct {
Text string `xml:",chardata"`
Body struct {
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
} `xml:"body"`
} `xml:"payload"`
}
// Created with https://github.com/miku/zek
type OxPublicKeysList struct {
XMLName xml.Name `xml:"pubsub"`
@ -76,18 +54,6 @@ type OxPublicKeysList struct {
} `xml:"items"`
}
// Created with https://github.com/miku/zek
type IQPubsubRequest 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"`
MaxItems string `xml:"max_items,attr"`
} `xml:"items"`
}
// Created with https://github.com/miku/zek
type IQDiscoItemsType struct {
XMLName xml.Name `xml:"query"`

View File

@ -183,7 +183,7 @@ func main() {
resource = *flagResource
} else if resource == "" {
// Use "go-sendxmpp" plus a random string if no other resource is specified
resource = "go-sendxmpp." + getID()
resource = "go-sendxmpp." + getShortID()
}
if (*flagHttpUpload != "") && (*flagInteractive || (*flagMessageFile != "")) {

36
ox.go
View File

@ -373,21 +373,23 @@ func oxRecvPublicKeys(client *xmpp.Client, recipient string,
}
func oxGetPublicKeyRing(client *xmpp.Client, recipient string) (*crypto.KeyRing, error) {
var oxPublicKeyListRequest IQPubsubRequest
var oxPublicKeyListXML OxPublicKeysList
publicKeyRing, err := crypto.NewKeyRing(nil)
if err != nil {
return nil, err
}
oxPublicKeyListRequest.Xmlns = nsPubsub
oxPublicKeyListRequest.Items.Node = nsOxPubKeys
oxPublicKeyListRequest.Items.MaxItems = "1"
opkl, err := xml.Marshal(oxPublicKeyListRequest)
oxPubKeyListReq := etree.NewDocument()
oxPubKeyListReqPs := oxPubKeyListReq.CreateElement("pubsub")
oxPubKeyListReqPs.CreateAttr("xmlns", nsPubsub)
oxPubKeyListReqPsItems := oxPubKeyListReqPs.CreateElement("items")
oxPubKeyListReqPsItems.CreateAttr("node", nsOxPubKeys)
oxPubKeyListReqPsItems.CreateAttr("max_items", "1")
opkl, err := oxPubKeyListReq.WriteToString()
if err != nil {
log.Fatal(err)
}
oxPublicKeyList, err := sendIQ(client, recipient, "get", string(opkl))
oxPublicKeyList, err := sendIQ(client, recipient, "get", opkl)
if err != nil {
log.Fatal(err)
}
@ -487,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 oxCryptMessage OxCryptElement
var oxMessage OxMessageElement
privKeyRing, err := crypto.NewKeyRing(oxPrivKey)
if err != nil {
@ -501,13 +502,20 @@ func oxEncrypt(client *xmpp.Client, oxPrivKey *crypto.Key, recipient string,
_ = keyRing.AddKey(ownKey)
}
}
oxCryptMessage.Xmlns = nsOx
oxCryptMessage.To.Jid = recipient
oxCryptMessage.Time.Stamp = time.Now().UTC().Format("2006-01-02T15:04:05Z")
oxCryptMessage.Rpad = getRpad()
oxCryptMessage.Payload.Body.Xmlns = nsJabberClient
oxCryptMessage.Payload.Body.Text = message
ocm, err := xml.Marshal(oxCryptMessage)
oxCryptMessage := etree.NewDocument()
oxCryptMessageSc := oxCryptMessage.CreateElement("signcrypt")
oxCryptMessageSc.CreateAttr("xmlns", nsOx)
oxCryptMessageScTo := oxCryptMessageSc.CreateElement("to")
oxCryptMessageScTo.CreateAttr("jid", recipient)
oxCryptMessageScTime := oxCryptMessageSc.CreateElement("time")
oxCryptMessageScTime.CreateAttr("stamp", time.Now().UTC().Format("2006-01-02T15:04:05Z"))
oxCryptMessageScRpad := oxCryptMessageSc.CreateElement("rpad")
oxCryptMessageScRpad.CreateText(getRpad())
oxCryptMessageScPayload := oxCryptMessageSc.CreateElement("payload")
oxCryptMessageScPayloadBody := oxCryptMessageScPayload.CreateElement("body")
oxCryptMessageScPayloadBody.CreateAttr("xmlns", nsJabberClient)
oxCryptMessageScPayloadBody.CreateText(message)
ocm, err := oxCryptMessage.WriteToString()
if err != nil {
return "error", err
}