Update vendor (whatsapp)
parent
1b9877fda4
commit
aefa70891c
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,163 @@
|
||||
// Copyright (c) 2022 Tulir Asokan
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
package whatsmeow
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
waBinary "go.mau.fi/whatsmeow/binary"
|
||||
waProto "go.mau.fi/whatsmeow/binary/proto"
|
||||
"go.mau.fi/whatsmeow/types"
|
||||
"go.mau.fi/whatsmeow/types/events"
|
||||
"go.mau.fi/whatsmeow/util/hkdfutil"
|
||||
)
|
||||
|
||||
func getMediaRetryKey(mediaKey []byte) (cipherKey []byte) {
|
||||
return hkdfutil.SHA256(mediaKey, nil, []byte("WhatsApp Media Retry Notification"), 32)
|
||||
}
|
||||
|
||||
func prepareMediaRetryGCM(mediaKey []byte) (cipher.AEAD, error) {
|
||||
block, err := aes.NewCipher(getMediaRetryKey(mediaKey))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to initialize AES cipher: %w", err)
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to initialize GCM: %w", err)
|
||||
}
|
||||
return gcm, nil
|
||||
}
|
||||
|
||||
func encryptMediaRetryReceipt(messageID types.MessageID, mediaKey []byte) (ciphertext, iv []byte, err error) {
|
||||
receipt := &waProto.ServerErrorReceipt{
|
||||
StanzaId: proto.String(messageID),
|
||||
}
|
||||
var plaintext []byte
|
||||
plaintext, err = proto.Marshal(receipt)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("failed to marshal payload: %w", err)
|
||||
return
|
||||
}
|
||||
var gcm cipher.AEAD
|
||||
gcm, err = prepareMediaRetryGCM(mediaKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
iv = make([]byte, 12)
|
||||
_, err = rand.Read(iv)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ciphertext = gcm.Seal(plaintext[:0], iv, plaintext, []byte(messageID))
|
||||
return
|
||||
}
|
||||
|
||||
// SendMediaRetryReceipt sends a request to the phone to re-upload the media in a message.
|
||||
//
|
||||
// The response will come as an *events.MediaRetry. The response will then have to be decrypted
|
||||
// using DecryptMediaRetryNotification and the same media key passed here.
|
||||
func (cli *Client) SendMediaRetryReceipt(message *types.MessageInfo, mediaKey []byte) error {
|
||||
ciphertext, iv, err := encryptMediaRetryReceipt(message.ID, mediaKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to prepare encrypted retry receipt: %w", err)
|
||||
}
|
||||
|
||||
rmrAttrs := waBinary.Attrs{
|
||||
"jid": message.Chat,
|
||||
"from_me": message.IsFromMe,
|
||||
}
|
||||
if message.IsGroup {
|
||||
rmrAttrs["participant"] = message.Sender
|
||||
}
|
||||
|
||||
encryptedRequest := []waBinary.Node{
|
||||
{Tag: "enc_p", Content: ciphertext},
|
||||
{Tag: "enc_iv", Content: iv},
|
||||
}
|
||||
|
||||
err = cli.sendNode(waBinary.Node{
|
||||
Tag: "receipt",
|
||||
Attrs: waBinary.Attrs{
|
||||
"id": message.ID,
|
||||
"to": cli.Store.ID.ToNonAD(),
|
||||
"type": "server-error",
|
||||
},
|
||||
Content: []waBinary.Node{
|
||||
{Tag: "encrypt", Content: encryptedRequest},
|
||||
{Tag: "rmr", Attrs: rmrAttrs},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecryptMediaRetryNotification decrypts a media retry notification using the media key.
|
||||
func DecryptMediaRetryNotification(evt *events.MediaRetry, mediaKey []byte) (*waProto.MediaRetryNotification, error) {
|
||||
gcm, err := prepareMediaRetryGCM(mediaKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
plaintext, err := gcm.Open(nil, evt.IV, evt.Ciphertext, []byte(evt.MessageID))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decrypt notification: %w", err)
|
||||
}
|
||||
var notif waProto.MediaRetryNotification
|
||||
err = proto.Unmarshal(plaintext, ¬if)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal notification (invalid encryption key?): %w", err)
|
||||
}
|
||||
return ¬if, nil
|
||||
}
|
||||
|
||||
func parseMediaRetryNotification(node *waBinary.Node) (*events.MediaRetry, error) {
|
||||
ag := node.AttrGetter()
|
||||
var evt events.MediaRetry
|
||||
evt.Timestamp = time.Unix(ag.Int64("t"), 0)
|
||||
evt.MessageID = types.MessageID(ag.String("id"))
|
||||
if !ag.OK() {
|
||||
return nil, ag.Error()
|
||||
}
|
||||
rmr, ok := node.GetOptionalChildByTag("rmr")
|
||||
if !ok {
|
||||
return nil, &ElementMissingError{Tag: "rmr", In: "retry notification"}
|
||||
}
|
||||
rmrAG := rmr.AttrGetter()
|
||||
evt.ChatID = rmrAG.JID("jid")
|
||||
evt.FromMe = rmrAG.Bool("from_me")
|
||||
evt.SenderID = rmrAG.OptionalJIDOrEmpty("participant")
|
||||
if !rmrAG.OK() {
|
||||
return nil, fmt.Errorf("missing attributes in <rmr> tag: %w", rmrAG.Error())
|
||||
}
|
||||
|
||||
evt.Ciphertext, ok = node.GetChildByTag("encrypt", "enc_p").Content.([]byte)
|
||||
if !ok {
|
||||
return nil, &ElementMissingError{Tag: "enc_p", In: fmt.Sprintf("retry notification %s", evt.MessageID)}
|
||||
}
|
||||
evt.IV, ok = node.GetChildByTag("encrypt", "enc_iv").Content.([]byte)
|
||||
if !ok {
|
||||
return nil, &ElementMissingError{Tag: "enc_iv", In: fmt.Sprintf("retry notification %s", evt.MessageID)}
|
||||
}
|
||||
return &evt, nil
|
||||
}
|
||||
|
||||
func (cli *Client) handleMediaRetryNotification(node *waBinary.Node) {
|
||||
// TODO handle errors (e.g. <error code="2"/>)
|
||||
evt, err := parseMediaRetryNotification(node)
|
||||
if err != nil {
|
||||
cli.Log.Warnf("Failed to parse media retry notification: %v", err)
|
||||
return
|
||||
}
|
||||
cli.dispatchEvent(evt)
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
// Copyright (c) 2022 Tulir Asokan
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
package whatsmeow
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"go.mau.fi/whatsmeow/socket"
|
||||
"go.mau.fi/whatsmeow/store"
|
||||
)
|
||||
|
||||
// CheckUpdateResponse is the data returned by CheckUpdate.
|
||||
type CheckUpdateResponse struct {
|
||||
IsBroken bool
|
||||
IsBelowSoft bool
|
||||
IsBelowHard bool
|
||||
CurrentVersion string
|
||||
|
||||
ParsedVersion store.WAVersionContainer `json:"-"`
|
||||
}
|
||||
|
||||
// CheckUpdateURL is the base URL to check for WhatsApp web updates.
|
||||
const CheckUpdateURL = "https://web.whatsapp.com/check-update"
|
||||
|
||||
// CheckUpdate asks the WhatsApp servers if there is an update available
|
||||
// (using the HTTP client and proxy settings of this whatsmeow Client instance).
|
||||
func (cli *Client) CheckUpdate() (respData CheckUpdateResponse, err error) {
|
||||
return CheckUpdate(http.DefaultClient)
|
||||
}
|
||||
|
||||
// CheckUpdate asks the WhatsApp servers if there is an update available.
|
||||
func CheckUpdate(httpClient *http.Client) (respData CheckUpdateResponse, err error) {
|
||||
var reqURL *url.URL
|
||||
reqURL, err = url.Parse(CheckUpdateURL)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("failed to parse check update URL: %w", err)
|
||||
return
|
||||
}
|
||||
q := reqURL.Query()
|
||||
q.Set("version", store.GetWAVersion().String())
|
||||
q.Set("platform", "web")
|
||||
reqURL.RawQuery = q.Encode()
|
||||
var req *http.Request
|
||||
req, err = http.NewRequest(http.MethodGet, reqURL.String(), nil)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("failed to prepare request: %w", err)
|
||||
return
|
||||
}
|
||||
req.Header.Set("Origin", socket.Origin)
|
||||
req.Header.Set("Referer", socket.Origin+"/")
|
||||
var resp *http.Response
|
||||
resp, err = httpClient.Do(req)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("failed to send request: %w", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
err = fmt.Errorf("unexpected response with status %d: %s", resp.StatusCode, body)
|
||||
return
|
||||
}
|
||||
err = json.NewDecoder(resp.Body).Decode(&respData)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("failed to decode response body (status %d): %w", resp.StatusCode, err)
|
||||
return
|
||||
}
|
||||
respData.ParsedVersion, err = store.ParseVersion(respData.CurrentVersion)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("failed to parse version string: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
// Copyright 2019 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build go1.13
|
||||
// +build go1.13
|
||||
|
||||
// Package ed25519 implements the Ed25519 signature algorithm. See
|
||||
// https://ed25519.cr.yp.to/.
|
||||
//
|
||||
// These functions are also compatible with the “Ed25519” function defined in
|
||||
// RFC 8032. However, unlike RFC 8032's formulation, this package's private key
|
||||
// representation includes a public key suffix to make multiple signing
|
||||
// operations with the same key more efficient. This package refers to the RFC
|
||||
// 8032 private key as the “seed”.
|
||||
//
|
||||
// Beginning with Go 1.13, the functionality of this package was moved to the
|
||||
// standard library as crypto/ed25519. This package only acts as a compatibility
|
||||
// wrapper.
|
||||
package ed25519
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"io"
|
||||
)
|
||||
|
||||
const (
|
||||
// PublicKeySize is the size, in bytes, of public keys as used in this package.
|
||||
PublicKeySize = 32
|
||||
// PrivateKeySize is the size, in bytes, of private keys as used in this package.
|
||||
PrivateKeySize = 64
|
||||
// SignatureSize is the size, in bytes, of signatures generated and verified by this package.
|
||||
SignatureSize = 64
|
||||
// SeedSize is the size, in bytes, of private key seeds. These are the private key representations used by RFC 8032.
|
||||
SeedSize = 32
|
||||
)
|
||||
|
||||
// PublicKey is the type of Ed25519 public keys.
|
||||
//
|
||||
// This type is an alias for crypto/ed25519's PublicKey type.
|
||||
// See the crypto/ed25519 package for the methods on this type.
|
||||
type PublicKey = ed25519.PublicKey
|
||||
|
||||
// PrivateKey is the type of Ed25519 private keys. It implements crypto.Signer.
|
||||
//
|
||||
// This type is an alias for crypto/ed25519's PrivateKey type.
|
||||
// See the crypto/ed25519 package for the methods on this type.
|
||||
type PrivateKey = ed25519.PrivateKey
|
||||
|
||||
// GenerateKey generates a public/private key pair using entropy from rand.
|
||||
// If rand is nil, crypto/rand.Reader will be used.
|
||||
func GenerateKey(rand io.Reader) (PublicKey, PrivateKey, error) {
|
||||
return ed25519.GenerateKey(rand)
|
||||
}
|
||||
|
||||
// NewKeyFromSeed calculates a private key from a seed. It will panic if
|
||||
// len(seed) is not SeedSize. This function is provided for interoperability
|
||||
// with RFC 8032. RFC 8032's private keys correspond to seeds in this
|
||||
// package.
|
||||
func NewKeyFromSeed(seed []byte) PrivateKey {
|
||||
return ed25519.NewKeyFromSeed(seed)
|
||||
}
|
||||
|
||||
// Sign signs the message with privateKey and returns a signature. It will
|
||||
// panic if len(privateKey) is not PrivateKeySize.
|
||||
func Sign(privateKey PrivateKey, message []byte) []byte {
|
||||
return ed25519.Sign(privateKey, message)
|
||||
}
|
||||
|
||||
// Verify reports whether sig is a valid signature of message by publicKey. It
|
||||
// will panic if len(publicKey) is not PublicKeySize.
|
||||
func Verify(publicKey PublicKey, message, sig []byte) bool {
|
||||
return ed25519.Verify(publicKey, message, sig)
|
||||
}
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue