2022-04-17 15:16:29 +00:00
|
|
|
// Copyright 2022 Martin Dosch.
|
|
|
|
// Use of this source code is governed by the BSD-2-clause
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ProtonMail/gopenpgp/v2/crypto" // MIT License
|
|
|
|
"github.com/beevik/etree" // BSD-2-clause
|
|
|
|
"github.com/mattn/go-xmpp" // BSD-3-Clause
|
|
|
|
)
|
|
|
|
|
2022-05-04 09:18:24 +00:00
|
|
|
func oxDeleteNodes(jid string, client *xmpp.Client,
|
|
|
|
iqc chan xmpp.IQ) error {
|
|
|
|
nodeListRequest := etree.NewDocument()
|
2023-05-09 19:23:29 +00:00
|
|
|
nodeListRequest.WriteSettings.AttrSingleQuote = true
|
2022-05-04 09:18:24 +00:00
|
|
|
query := nodeListRequest.CreateElement("query")
|
|
|
|
query.CreateAttr("xmlns", nsDiscoItems)
|
|
|
|
nlr, err := nodeListRequest.WriteToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
iqReply, err := sendIQ(client, iqc, jid, "get", nlr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nodeListReply := etree.NewDocument()
|
|
|
|
err = nodeListReply.ReadFromBytes(iqReply.Query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
query = nodeListReply.SelectElement("query")
|
2022-05-04 10:12:10 +00:00
|
|
|
if query == nil {
|
|
|
|
return errors.New("error parsing iq reply")
|
|
|
|
}
|
2022-05-04 09:18:24 +00:00
|
|
|
items := query.SelectElements("item")
|
2022-05-04 10:12:10 +00:00
|
|
|
if items == nil {
|
|
|
|
return errors.New("error parsing iq reply")
|
|
|
|
}
|
2022-05-04 09:18:24 +00:00
|
|
|
for _, item := range items {
|
|
|
|
node := item.SelectAttr("node")
|
2022-05-04 10:12:10 +00:00
|
|
|
if node == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-05-04 09:18:24 +00:00
|
|
|
if !strings.Contains(node.Value, nsOx) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
deleteNodeRequest := etree.NewDocument()
|
2023-05-09 19:23:29 +00:00
|
|
|
deleteNodeRequest.WriteSettings.AttrSingleQuote = true
|
2022-05-04 09:18:24 +00:00
|
|
|
pubsub := deleteNodeRequest.CreateElement("pubsub")
|
|
|
|
pubsub.CreateAttr("xmlns", nsPubsubOwner)
|
|
|
|
delete := pubsub.CreateElement("delete")
|
|
|
|
delete.CreateAttr("node", node.Value)
|
|
|
|
dnr, err := deleteNodeRequest.WriteToString()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err = sendIQ(client, iqc, jid, "set", dnr)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-03 10:25:43 +00:00
|
|
|
func oxDecrypt(m xmpp.Chat, client *xmpp.Client, iqc chan xmpp.IQ,
|
|
|
|
user string, oxPrivKey *crypto.Key) (string, time.Time, error) {
|
2022-04-30 11:06:04 +00:00
|
|
|
var cryptMsgByte []byte
|
|
|
|
var err error
|
|
|
|
sender := strings.Split(m.Remote, "/")[0]
|
|
|
|
for _, r := range m.OtherElem {
|
|
|
|
if r.XMLName.Space == nsOx {
|
|
|
|
cryptMsgByte, err =
|
|
|
|
base64.StdEncoding.DecodeString(r.InnerXML)
|
|
|
|
if err != nil {
|
|
|
|
return "error", time.Now(), err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
oxMsg := crypto.NewPGPMessage(cryptMsgByte)
|
|
|
|
keyRing, err := crypto.NewKeyRing(oxPrivKey)
|
|
|
|
if err != nil {
|
|
|
|
return "error", time.Now(), err
|
|
|
|
}
|
2022-05-03 10:25:43 +00:00
|
|
|
senderKeyRing, err := oxGetPublicKeyRing(client, iqc, sender)
|
2022-04-30 11:06:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return "error", time.Now(), err
|
|
|
|
}
|
|
|
|
decryptMsg, err := keyRing.Decrypt(oxMsg, senderKeyRing, crypto.GetUnixTime())
|
|
|
|
if err != nil {
|
|
|
|
return "error", time.Now(), err
|
|
|
|
}
|
2022-04-30 11:27:44 +00:00
|
|
|
// Remove invalid code points.
|
2022-11-05 11:40:40 +00:00
|
|
|
message := validUTF8(string(decryptMsg.Data))
|
2022-04-30 11:06:04 +00:00
|
|
|
doc := etree.NewDocument()
|
2022-04-30 11:27:44 +00:00
|
|
|
err = doc.ReadFromString(message)
|
2022-04-30 11:06:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return "error", time.Now(), err
|
|
|
|
}
|
|
|
|
signcrypt := doc.SelectElement("signcrypt")
|
|
|
|
if signcrypt == nil {
|
2022-05-03 12:20:32 +00:00
|
|
|
return "error", time.Now(), errors.New("ox: no signcrypt element")
|
2022-04-30 11:06:04 +00:00
|
|
|
}
|
|
|
|
to := signcrypt.SelectElement("to")
|
|
|
|
if to == nil {
|
2022-05-03 12:20:32 +00:00
|
|
|
return "error", time.Now(), errors.New("ox: no to element")
|
2022-04-30 11:06:04 +00:00
|
|
|
}
|
|
|
|
jid := to.SelectAttr("jid")
|
|
|
|
if jid == nil {
|
2022-05-03 12:20:32 +00:00
|
|
|
return "error", time.Now(), errors.New("ox: no jid attribute")
|
2022-04-30 11:06:04 +00:00
|
|
|
}
|
|
|
|
if strings.Split(jid.Value, "/")[0] != user {
|
2022-05-03 12:20:32 +00:00
|
|
|
return "error", time.Now(), errors.New("ox: encrypted for wrong user")
|
2022-04-30 11:06:04 +00:00
|
|
|
}
|
|
|
|
timestamp := signcrypt.SelectElement("time")
|
|
|
|
if timestamp == nil {
|
2022-05-03 12:20:32 +00:00
|
|
|
return "error", time.Now(), errors.New("ox: no time element")
|
2022-04-30 11:06:04 +00:00
|
|
|
}
|
|
|
|
stamp := timestamp.SelectAttr("stamp")
|
|
|
|
if stamp == nil {
|
2022-05-03 12:20:32 +00:00
|
|
|
return "error", time.Now(), errors.New("ox: no stamp attribute")
|
2022-04-30 11:06:04 +00:00
|
|
|
}
|
2022-05-02 14:35:25 +00:00
|
|
|
msgStamp, err := time.Parse("2006-01-02T15:04:05Z0700", stamp.Value)
|
2022-04-30 11:06:04 +00:00
|
|
|
if err != nil {
|
2022-05-02 14:35:25 +00:00
|
|
|
return "error", time.Now(), err
|
2022-04-30 11:06:04 +00:00
|
|
|
}
|
|
|
|
payload := signcrypt.SelectElement("payload")
|
|
|
|
if payload == nil {
|
2022-05-03 12:20:32 +00:00
|
|
|
return "error", time.Now(), errors.New("ox: no payload element")
|
2022-04-30 11:06:04 +00:00
|
|
|
}
|
|
|
|
body := payload.SelectElement("body")
|
|
|
|
if body == nil {
|
|
|
|
return "", time.Now(), nil
|
|
|
|
}
|
|
|
|
return body.Text(), msgStamp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isOxMsg(m xmpp.Chat) bool {
|
|
|
|
for _, r := range m.OtherElem {
|
|
|
|
if r.XMLName.Space == nsOx {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-03 10:25:43 +00:00
|
|
|
func oxImportPrivKey(jid string, privKeyLocation string, client *xmpp.Client,
|
|
|
|
iqc chan xmpp.IQ) error {
|
2022-04-25 19:29:14 +00:00
|
|
|
xmppURI := "xmpp:" + jid
|
2022-04-18 09:16:41 +00:00
|
|
|
buffer, err := readFile(privKeyLocation)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key, err := crypto.NewKey(buffer.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
key, err = crypto.NewKeyFromArmored(buffer.String())
|
|
|
|
if err != nil {
|
2022-05-04 09:42:42 +00:00
|
|
|
keyDecoded, err := base64.StdEncoding.DecodeString(buffer.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key, err = crypto.NewKey(keyDecoded)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-18 09:16:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
entity := key.GetEntity()
|
2022-04-25 19:29:14 +00:00
|
|
|
if entity.Identities[xmppURI] == nil {
|
|
|
|
return errors.New("Key identity is not " + xmppURI)
|
2022-04-18 09:16:41 +00:00
|
|
|
}
|
|
|
|
pk, err := key.GetPublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pubKey, err := crypto.NewKey(pk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fingerprint := strings.ToUpper(pubKey.GetFingerprint())
|
2022-05-03 10:25:43 +00:00
|
|
|
_, err = oxRecvPublicKeys(client, iqc, jid, fingerprint)
|
2022-04-18 09:16:41 +00:00
|
|
|
if err != nil {
|
2022-05-04 09:42:42 +00:00
|
|
|
err = oxPublishPubKey(jid, client, iqc, pubKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-18 09:16:41 +00:00
|
|
|
}
|
|
|
|
location, err := oxGetPrivKeyLoc(jid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
keySerialized, err := key.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-18 15:14:03 +00:00
|
|
|
err = oxStoreKey(location,
|
2022-04-18 09:16:41 +00:00
|
|
|
base64.StdEncoding.EncodeToString(keySerialized))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-05-03 10:25:43 +00:00
|
|
|
pubKeyRing, err := oxGetPublicKeyRing(client, iqc, jid)
|
2022-04-28 09:35:14 +00:00
|
|
|
if err == nil {
|
|
|
|
pubKeys := pubKeyRing.GetKeys()
|
|
|
|
for _, r := range pubKeys {
|
|
|
|
if strings.ToUpper(r.GetFingerprint()) == fingerprint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-03 10:25:43 +00:00
|
|
|
err = oxPublishPubKey(jid, client, iqc, pubKey)
|
2022-04-28 09:35:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-03 10:25:43 +00:00
|
|
|
func oxPublishPubKey(jid string, client *xmpp.Client, iqc chan xmpp.IQ,
|
|
|
|
pubKey *crypto.Key) error {
|
2022-04-28 09:35:14 +00:00
|
|
|
keyCreated := time.Now().UTC().Format("2006-01-02T15:04:05Z")
|
|
|
|
fingerprint := strings.ToUpper(pubKey.GetFingerprint())
|
|
|
|
keySerialized, err := pubKey.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pubKeyBase64 := base64.StdEncoding.EncodeToString(keySerialized)
|
|
|
|
root := etree.NewDocument()
|
2023-05-09 19:23:29 +00:00
|
|
|
root.WriteSettings.AttrSingleQuote = true
|
2022-04-28 09:35:14 +00:00
|
|
|
pubsub := root.CreateElement("pubsub")
|
|
|
|
pubsub.CreateAttr("xmlns", nsPubsub)
|
|
|
|
publish := pubsub.CreateElement("publish")
|
|
|
|
publish.CreateAttr("node", nsOxPubKeys+":"+fingerprint)
|
|
|
|
item := publish.CreateElement("item")
|
|
|
|
item.CreateAttr("id", keyCreated)
|
|
|
|
pubkey := item.CreateElement("pubkey")
|
|
|
|
pubkey.CreateAttr("xmlns", nsOx)
|
|
|
|
data := pubkey.CreateElement("data")
|
|
|
|
data.CreateText(pubKeyBase64)
|
|
|
|
publishoptions := pubsub.CreateElement("publish-options")
|
|
|
|
x := publishoptions.CreateElement("x")
|
|
|
|
x.CreateAttr("xmlns", nsJabberData)
|
|
|
|
x.CreateAttr("type", "submit")
|
|
|
|
field := x.CreateElement("field")
|
|
|
|
field.CreateAttr("var", "FORM_TYPE")
|
|
|
|
field.CreateAttr("type", "hidden")
|
|
|
|
value := field.CreateElement("value")
|
|
|
|
value.CreateText(pubsubPubOptions)
|
|
|
|
field = x.CreateElement("field")
|
|
|
|
field.CreateAttr("var", "pubsub#access_model")
|
|
|
|
value = field.CreateElement("value")
|
|
|
|
value.CreateText("open")
|
|
|
|
xmlstring, err := root.WriteToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-03 10:25:43 +00:00
|
|
|
iqReply, err := sendIQ(client, iqc, jid, "set", xmlstring)
|
2022-04-28 09:35:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if iqReply.Type != "result" {
|
2022-05-03 12:20:32 +00:00
|
|
|
return errors.New("error while publishing public key")
|
2022-04-28 09:35:14 +00:00
|
|
|
}
|
2022-05-03 10:25:43 +00:00
|
|
|
ownPubKeyRingFromPubsub, err := oxRecvPublicKeys(client, iqc, jid, fingerprint)
|
2022-04-28 09:35:14 +00:00
|
|
|
if err != nil {
|
2022-05-03 12:20:32 +00:00
|
|
|
return errors.New("couldn't successfully verify public key upload")
|
2022-04-28 09:35:14 +00:00
|
|
|
}
|
|
|
|
ownPubKeyFromPubsub := ownPubKeyRingFromPubsub.GetKeys()[0]
|
|
|
|
ownPubKeyFromPubsubSerialized, err := ownPubKeyFromPubsub.Serialize()
|
|
|
|
if err != nil {
|
2022-05-03 12:20:32 +00:00
|
|
|
return errors.New("couldn't successfully verify public key upload")
|
2022-04-28 09:35:14 +00:00
|
|
|
}
|
|
|
|
if pubKeyBase64 != base64.StdEncoding.EncodeToString(ownPubKeyFromPubsubSerialized) {
|
2022-05-03 12:20:32 +00:00
|
|
|
return errors.New("couldn't successfully verify public key upload")
|
2022-04-28 09:35:14 +00:00
|
|
|
}
|
|
|
|
root = etree.NewDocument()
|
2023-05-09 19:23:29 +00:00
|
|
|
root.WriteSettings.AttrSingleQuote = true
|
2022-04-28 09:35:14 +00:00
|
|
|
pubsub = root.CreateElement("pubsub")
|
|
|
|
pubsub.CreateAttr("xmlns", nsPubsub)
|
|
|
|
publish = pubsub.CreateElement("publish")
|
|
|
|
publish.CreateAttr("node", nsOxPubKeys)
|
|
|
|
item = publish.CreateElement("item")
|
|
|
|
pubkeyslist := item.CreateElement("public-keys-list")
|
|
|
|
pubkeyslist.CreateAttr("xmlns", nsOx)
|
|
|
|
pubkeymeta := pubkeyslist.CreateElement("pubkey-metadata")
|
|
|
|
pubkeymeta.CreateAttr("v4-fingerprint", fingerprint)
|
|
|
|
pubkeymeta.CreateAttr("date", keyCreated)
|
|
|
|
publishoptions = pubsub.CreateElement("publish-options")
|
|
|
|
x = publishoptions.CreateElement("x")
|
|
|
|
x.CreateAttr("xmlns", nsJabberData)
|
|
|
|
x.CreateAttr("type", "submit")
|
|
|
|
field = x.CreateElement("field")
|
|
|
|
field.CreateAttr("var", "FORM_TYPE")
|
|
|
|
field.CreateAttr("type", "hidden")
|
|
|
|
value = field.CreateElement("value")
|
|
|
|
value.CreateText(pubsubPubOptions)
|
|
|
|
field = x.CreateElement("field")
|
|
|
|
field.CreateAttr("var", "pubsub#access_model")
|
|
|
|
value = field.CreateElement("value")
|
|
|
|
value.CreateText("open")
|
|
|
|
xmlstring, err = root.WriteToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-03 10:25:43 +00:00
|
|
|
iqReply, err = sendIQ(client, iqc, jid, "set", xmlstring)
|
2022-04-28 09:35:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if iqReply.Type != "result" {
|
2022-05-03 12:20:32 +00:00
|
|
|
return errors.New("couldn't publish public key list")
|
2022-04-28 09:35:14 +00:00
|
|
|
}
|
2022-04-18 09:16:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-17 15:16:29 +00:00
|
|
|
func oxGetPrivKeyLoc(jid string) (string, error) {
|
|
|
|
var err error
|
|
|
|
var homeDir, dataDir string
|
|
|
|
switch {
|
|
|
|
case os.Getenv("$XDG_DATA_HOME") != "":
|
|
|
|
dataDir = os.Getenv("$XDG_DATA_HOME")
|
|
|
|
case os.Getenv("$XDG_HOME") != "":
|
|
|
|
homeDir = os.Getenv("$XDG_HOME")
|
|
|
|
dataDir = homeDir + "/.local/share"
|
|
|
|
case os.Getenv("$HOME") != "":
|
|
|
|
homeDir = os.Getenv("$HOME")
|
|
|
|
dataDir = homeDir + "/.local/share"
|
|
|
|
default:
|
|
|
|
homeDir, err = os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return "error", err
|
|
|
|
}
|
|
|
|
if homeDir == "" {
|
|
|
|
return "error", err
|
|
|
|
}
|
|
|
|
dataDir = homeDir + "/.local/share"
|
|
|
|
}
|
|
|
|
dataDir = dataDir + "/go-sendxmpp/oxprivkeys/"
|
|
|
|
if _, err = os.Stat(dataDir); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(dataDir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return "error", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dataFile := dataDir + base64.StdEncoding.EncodeToString([]byte(jid))
|
|
|
|
return dataFile, nil
|
|
|
|
}
|
|
|
|
|
2022-04-18 15:14:03 +00:00
|
|
|
func oxGetPubKeyLoc(fingerprint string) (string, error) {
|
|
|
|
var err error
|
|
|
|
var homeDir, dataDir string
|
|
|
|
switch {
|
|
|
|
case os.Getenv("$XDG_DATA_HOME") != "":
|
|
|
|
dataDir = os.Getenv("$XDG_DATA_HOME")
|
|
|
|
case os.Getenv("$XDG_HOME") != "":
|
|
|
|
homeDir = os.Getenv("$XDG_HOME")
|
|
|
|
dataDir = homeDir + "/.local/share"
|
|
|
|
case os.Getenv("$HOME") != "":
|
|
|
|
homeDir = os.Getenv("$HOME")
|
|
|
|
dataDir = homeDir + "/.local/share"
|
|
|
|
default:
|
|
|
|
homeDir, err = os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return "error", err
|
|
|
|
}
|
|
|
|
if homeDir == "" {
|
|
|
|
return "error", err
|
|
|
|
}
|
|
|
|
dataDir = homeDir + "/.local/share"
|
|
|
|
}
|
|
|
|
dataDir = dataDir + "/go-sendxmpp/oxpubkeys/"
|
|
|
|
if _, err = os.Stat(dataDir); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(dataDir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return "error", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dataFile := dataDir + fingerprint
|
|
|
|
return dataFile, nil
|
|
|
|
}
|
|
|
|
|
2022-04-17 15:16:29 +00:00
|
|
|
func oxGetPrivKey(jid string, passphrase string) (*crypto.Key, error) {
|
|
|
|
dataFile, err := oxGetPrivKeyLoc(jid)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-04-18 08:16:00 +00:00
|
|
|
keyBuffer, err := readFile(dataFile)
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
2022-04-18 08:16:00 +00:00
|
|
|
log.Fatal(err)
|
2022-04-17 15:16:29 +00:00
|
|
|
}
|
|
|
|
keyString := keyBuffer.String()
|
|
|
|
decodedPrivKey, err := base64.StdEncoding.DecodeString(keyString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
key, err := crypto.NewKey(decodedPrivKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if passphrase != "" {
|
|
|
|
key, err = key.Unlock([]byte(passphrase))
|
|
|
|
if err != nil {
|
2022-05-05 13:56:53 +00:00
|
|
|
log.Fatal("Ox: couldn't unlock private key.")
|
2022-04-17 15:16:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
isLocked, err := key.IsLocked()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if isLocked {
|
2022-05-05 13:56:53 +00:00
|
|
|
log.Fatal("Ox: private key is locked.")
|
2022-04-17 15:16:29 +00:00
|
|
|
}
|
|
|
|
if key.IsExpired() {
|
2022-05-05 13:56:53 +00:00
|
|
|
return nil, errors.New("Ox: private key is expired: " + key.GetFingerprint())
|
2022-04-17 15:16:29 +00:00
|
|
|
}
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
2022-04-18 15:14:03 +00:00
|
|
|
func oxStoreKey(location string, key string) error {
|
2022-04-17 15:16:29 +00:00
|
|
|
var file *os.File
|
2022-04-25 20:50:31 +00:00
|
|
|
file, err := os.Create(location)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-04-17 15:16:29 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
_ = file.Chmod(os.FileMode(0600))
|
|
|
|
} else {
|
|
|
|
_ = file.Chmod(os.FileMode(0200))
|
|
|
|
}
|
2022-04-25 20:50:31 +00:00
|
|
|
_, err = file.Write([]byte(key))
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-03 10:25:43 +00:00
|
|
|
func oxGenPrivKey(jid string, client *xmpp.Client, iqc chan xmpp.IQ,
|
|
|
|
passphrase string, keyType string) error {
|
2022-04-25 19:29:14 +00:00
|
|
|
xmppURI := "xmpp:" + jid
|
2022-04-26 17:47:22 +00:00
|
|
|
key, err := crypto.GenerateKey(xmppURI, "", keyType, 4096)
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if passphrase != "" {
|
|
|
|
key, err = key.Lock([]byte(passphrase))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
keySerialized, err := key.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-18 09:16:41 +00:00
|
|
|
location, err := oxGetPrivKeyLoc(jid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-18 15:14:03 +00:00
|
|
|
err = oxStoreKey(location,
|
2022-04-17 15:16:29 +00:00
|
|
|
base64.StdEncoding.EncodeToString(keySerialized))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-04-28 09:35:14 +00:00
|
|
|
decodedPubKey, err := key.GetPublicKey()
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-28 09:35:14 +00:00
|
|
|
pubKey, err := crypto.NewKey(decodedPubKey)
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-03 10:25:43 +00:00
|
|
|
err = oxPublishPubKey(jid, client, iqc, pubKey)
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-03 10:25:43 +00:00
|
|
|
func oxRecvPublicKeys(client *xmpp.Client, iqc chan xmpp.IQ, recipient string,
|
2022-04-21 15:28:59 +00:00
|
|
|
fingerprint string) (*crypto.KeyRing, error) {
|
2022-04-23 21:34:03 +00:00
|
|
|
opkr := etree.NewDocument()
|
2023-05-09 19:23:29 +00:00
|
|
|
opkr.WriteSettings.AttrSingleQuote = true
|
2022-04-23 21:34:03 +00:00
|
|
|
opkrPs := opkr.CreateElement("pubsub")
|
|
|
|
opkrPs.CreateAttr("xmlns", nsPubsub)
|
|
|
|
opkrPsItems := opkrPs.CreateElement("items")
|
|
|
|
opkrPsItems.CreateAttr("node", nsOxPubKeys+":"+fingerprint)
|
|
|
|
opkrPsItems.CreateAttr("max_items", "1")
|
|
|
|
opkrString, err := opkr.WriteToString()
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-03 10:25:43 +00:00
|
|
|
oxPublicKey, err := sendIQ(client, iqc, recipient, "get", opkrString)
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if oxPublicKey.Type != "result" {
|
2023-04-09 12:56:22 +00:00
|
|
|
return nil, errors.New("error while requesting public key for " +
|
2022-04-17 15:34:28 +00:00
|
|
|
recipient)
|
2022-04-17 15:16:29 +00:00
|
|
|
}
|
2022-04-23 21:34:03 +00:00
|
|
|
oxPublicKeyXML := etree.NewDocument()
|
|
|
|
err = oxPublicKeyXML.ReadFromBytes(oxPublicKey.Query)
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-21 15:28:59 +00:00
|
|
|
keyring, err := crypto.NewKeyRing(nil)
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-23 21:34:03 +00:00
|
|
|
oxPublicKeyXMLPubsub := oxPublicKeyXML.SelectElement("pubsub")
|
2022-09-01 20:13:13 +00:00
|
|
|
if oxPublicKeyXMLPubsub == nil {
|
|
|
|
return nil, errors.New("ox: no pubsub element in reply to public " +
|
|
|
|
"key request")
|
|
|
|
}
|
2022-04-23 21:34:03 +00:00
|
|
|
oxPublicKeyXMLItems := oxPublicKeyXMLPubsub.SelectElement("items")
|
2022-09-01 20:13:13 +00:00
|
|
|
if oxPublicKeyXMLItems == nil {
|
|
|
|
return nil, errors.New("ox: no items element in reply to public " +
|
|
|
|
"key request")
|
|
|
|
}
|
2022-04-23 21:34:03 +00:00
|
|
|
oxPublicKeyXMLItem := oxPublicKeyXMLItems.SelectElement("item")
|
2022-09-01 20:13:13 +00:00
|
|
|
if oxPublicKeyXMLItem == nil {
|
|
|
|
return nil, errors.New("ox: no item element in reply to public " +
|
|
|
|
"key request")
|
|
|
|
}
|
2022-04-23 21:34:03 +00:00
|
|
|
oxPublicKeyXMLPubkeys := oxPublicKeyXMLItem.SelectElements("pubkey")
|
|
|
|
for _, r := range oxPublicKeyXMLPubkeys {
|
|
|
|
data := r.SelectElement("data")
|
2022-09-01 20:13:13 +00:00
|
|
|
if data == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-04-23 21:34:03 +00:00
|
|
|
decodedPubKey, err := base64.StdEncoding.DecodeString(data.Text())
|
2022-04-21 15:28:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
key, err := crypto.NewKey(decodedPubKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if key.IsExpired() {
|
|
|
|
return nil, errors.New("Key is expired: " + fingerprint)
|
|
|
|
}
|
|
|
|
err = keyring.AddKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-17 15:16:29 +00:00
|
|
|
}
|
2022-04-21 15:28:59 +00:00
|
|
|
return keyring, nil
|
2022-04-17 15:16:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-03 10:25:43 +00:00
|
|
|
func oxGetPublicKeyRing(client *xmpp.Client, iqc chan xmpp.IQ,
|
|
|
|
recipient string) (*crypto.KeyRing, error) {
|
2022-04-21 15:28:59 +00:00
|
|
|
publicKeyRing, err := crypto.NewKeyRing(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-18 15:14:03 +00:00
|
|
|
|
2022-04-23 21:59:19 +00:00
|
|
|
oxPubKeyListReq := etree.NewDocument()
|
2023-05-09 19:23:29 +00:00
|
|
|
oxPubKeyListReq.WriteSettings.AttrSingleQuote = true
|
2022-04-23 21:59:19 +00:00
|
|
|
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()
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-05-03 10:25:43 +00:00
|
|
|
oxPublicKeyList, err := sendIQ(client, iqc, recipient, "get", opkl)
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if oxPublicKeyList.Type != "result" {
|
2023-04-09 12:56:22 +00:00
|
|
|
return nil, errors.New("error while requesting public openpgp keys for " +
|
2022-04-17 15:16:29 +00:00
|
|
|
recipient)
|
|
|
|
}
|
2022-04-23 23:20:39 +00:00
|
|
|
oxPubKeyListXML := etree.NewDocument()
|
|
|
|
err = oxPubKeyListXML.ReadFromBytes(oxPublicKeyList.Query)
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-25 19:29:14 +00:00
|
|
|
pubKeyRingID := "none"
|
2022-04-21 15:28:59 +00:00
|
|
|
newestKey, err := time.Parse(time.RFC3339, "1900-01-01T00:00:00Z")
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-23 23:20:39 +00:00
|
|
|
|
|
|
|
oxPubKeyListXMLPubsub := oxPubKeyListXML.SelectElement("pubsub")
|
2022-09-01 20:13:13 +00:00
|
|
|
if oxPubKeyListXMLPubsub == nil {
|
|
|
|
return nil, errors.New("ox: no pubsub element in public key list")
|
|
|
|
}
|
2022-04-23 23:20:39 +00:00
|
|
|
oxPubKeyListXMLPubsubItems := oxPubKeyListXMLPubsub.SelectElement("items")
|
2022-09-01 20:13:13 +00:00
|
|
|
if oxPubKeyListXMLPubsubItems == nil {
|
|
|
|
return nil, errors.New("ox: no items element in public key list")
|
|
|
|
}
|
2022-04-23 23:20:39 +00:00
|
|
|
oxPubKeyListXMLPubsubItemsItem := oxPubKeyListXMLPubsubItems.SelectElement("item")
|
2022-09-01 20:13:13 +00:00
|
|
|
if oxPubKeyListXMLPubsubItemsItem == nil {
|
|
|
|
return nil, errors.New("ox: no item element in public key list")
|
|
|
|
}
|
2022-04-23 23:20:39 +00:00
|
|
|
oxPubKeyListXMLPubsubItemsItemPkl := oxPubKeyListXMLPubsubItemsItem.SelectElement("public-keys-list")
|
2022-09-01 20:13:13 +00:00
|
|
|
if oxPubKeyListXMLPubsubItemsItemPkl == nil {
|
|
|
|
return nil, errors.New("ox: no keblic-keys-list element")
|
|
|
|
}
|
2022-04-23 23:20:39 +00:00
|
|
|
oxPubKeyListXMLPubsubItemsItemPklPm :=
|
|
|
|
oxPubKeyListXMLPubsubItemsItemPkl.SelectElements("pubkey-metadata")
|
|
|
|
for _, r := range oxPubKeyListXMLPubsubItemsItemPklPm {
|
|
|
|
date := r.SelectAttr("date")
|
2022-09-01 20:13:13 +00:00
|
|
|
if date == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-04-23 23:20:39 +00:00
|
|
|
fingerprint := r.SelectAttr("v4-fingerprint")
|
2022-09-01 20:13:13 +00:00
|
|
|
if fingerprint == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-04-23 23:20:39 +00:00
|
|
|
keyDate, err := time.Parse(time.RFC3339, date.Value)
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if keyDate.After(newestKey) {
|
|
|
|
newestKey = keyDate
|
2022-04-25 19:29:14 +00:00
|
|
|
pubKeyRingID = fingerprint.Value
|
2022-04-17 15:16:29 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-25 19:29:14 +00:00
|
|
|
if pubKeyRingID == "none" {
|
2022-04-17 15:16:29 +00:00
|
|
|
return nil, errors.New("server didn't provide public key fingerprints for " + recipient)
|
|
|
|
}
|
|
|
|
|
2022-04-25 19:29:14 +00:00
|
|
|
pubKeyRingLocation, err := oxGetPubKeyLoc(pubKeyRingID)
|
2022-04-18 15:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-21 15:28:59 +00:00
|
|
|
|
2022-04-23 11:19:42 +00:00
|
|
|
pubKeyReadXML := etree.NewDocument()
|
|
|
|
err = pubKeyReadXML.ReadFromFile(pubKeyRingLocation)
|
2022-04-18 15:14:03 +00:00
|
|
|
if err == nil {
|
2022-04-23 11:19:42 +00:00
|
|
|
date := pubKeyReadXML.SelectElement("date")
|
2022-04-21 15:28:59 +00:00
|
|
|
if date != nil {
|
|
|
|
savedKeysDate, err := time.Parse(time.RFC3339, date.Text())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !savedKeysDate.Before(newestKey) {
|
2022-04-23 11:19:42 +00:00
|
|
|
pubKeys := pubKeyReadXML.SelectElements("pubkey")
|
2022-04-21 15:28:59 +00:00
|
|
|
if pubKeys == nil {
|
2022-05-03 12:20:32 +00:00
|
|
|
return nil, errors.New("couldn't read public keys from cache")
|
2022-04-21 15:28:59 +00:00
|
|
|
}
|
|
|
|
for _, r := range pubKeys {
|
|
|
|
keyByte, err := base64.StdEncoding.DecodeString(r.Text())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
key, err := crypto.NewKey(keyByte)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !key.IsExpired() {
|
|
|
|
err = publicKeyRing.AddKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if publicKeyRing.CanEncrypt() {
|
|
|
|
return publicKeyRing, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-03 10:25:43 +00:00
|
|
|
pubKeyRing, err := oxRecvPublicKeys(client, iqc, recipient, pubKeyRingID)
|
2022-04-21 15:28:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-23 11:19:42 +00:00
|
|
|
pubKeySaveXML := etree.NewDocument()
|
|
|
|
date := pubKeySaveXML.CreateElement("date")
|
2022-04-21 15:28:59 +00:00
|
|
|
date.SetText(newestKey.Format(time.RFC3339))
|
|
|
|
for _, key := range pubKeyRing.GetKeys() {
|
|
|
|
keySerialized, err := key.Serialize()
|
2022-04-18 15:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-23 11:19:42 +00:00
|
|
|
saveKey := pubKeySaveXML.CreateElement("pubkey")
|
2022-04-21 15:28:59 +00:00
|
|
|
saveKey.SetText(base64.StdEncoding.EncodeToString(keySerialized))
|
2022-04-18 15:14:03 +00:00
|
|
|
}
|
2022-04-23 11:19:42 +00:00
|
|
|
err = pubKeySaveXML.WriteToFile(pubKeyRingLocation)
|
2022-04-18 15:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-21 15:28:59 +00:00
|
|
|
return pubKeyRing, nil
|
2022-04-17 15:16:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-03 10:25:43 +00:00
|
|
|
func oxEncrypt(client *xmpp.Client, iqc chan xmpp.IQ, oxPrivKey *crypto.Key,
|
|
|
|
recipient string, keyRing *crypto.KeyRing, message string) (string, error) {
|
2022-07-04 14:20:17 +00:00
|
|
|
if message == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
2022-04-17 15:16:29 +00:00
|
|
|
privKeyRing, err := crypto.NewKeyRing(oxPrivKey)
|
|
|
|
if err != nil {
|
|
|
|
return "error", err
|
|
|
|
}
|
|
|
|
ownJid := strings.Split(client.JID(), "/")[0]
|
|
|
|
if recipient != ownJid {
|
|
|
|
opk, err := oxPrivKey.GetPublicKey()
|
|
|
|
if err == nil {
|
|
|
|
ownKey, _ := crypto.NewKey(opk)
|
|
|
|
_ = keyRing.AddKey(ownKey)
|
|
|
|
}
|
|
|
|
}
|
2022-04-23 21:59:19 +00:00
|
|
|
oxCryptMessage := etree.NewDocument()
|
2023-05-09 19:23:29 +00:00
|
|
|
oxCryptMessage.WriteSettings.AttrSingleQuote = true
|
2022-04-23 22:33:17 +00:00
|
|
|
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")
|
2022-04-29 09:36:41 +00:00
|
|
|
oxCryptMessageScRpad.CreateText(getRpad(len(message)))
|
2022-04-23 22:33:17 +00:00
|
|
|
oxCryptMessageScPayload := oxCryptMessageSc.CreateElement("payload")
|
|
|
|
oxCryptMessageScPayloadBody := oxCryptMessageScPayload.CreateElement("body")
|
|
|
|
oxCryptMessageScPayloadBody.CreateAttr("xmlns", nsJabberClient)
|
|
|
|
oxCryptMessageScPayloadBody.CreateText(message)
|
2022-04-23 21:59:19 +00:00
|
|
|
ocm, err := oxCryptMessage.WriteToString()
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return "error", err
|
|
|
|
}
|
|
|
|
plainMessage := crypto.NewPlainMessage([]byte(ocm))
|
|
|
|
pgpMessage, err := keyRing.Encrypt(plainMessage, privKeyRing)
|
|
|
|
if err != nil {
|
|
|
|
return "error", err
|
|
|
|
}
|
2022-04-23 22:56:44 +00:00
|
|
|
om := etree.NewDocument()
|
2023-05-09 19:23:29 +00:00
|
|
|
om.WriteSettings.AttrSingleQuote = true
|
2022-04-23 22:56:44 +00:00
|
|
|
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()
|
2022-04-17 15:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return "error", err
|
|
|
|
}
|
|
|
|
|
2022-04-23 22:56:44 +00:00
|
|
|
return oms, nil
|
2022-04-17 15:16:29 +00:00
|
|
|
}
|