2020-07-13 16:02:35 +01:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
2020-07-14 12:59:07 +01:00
|
|
|
import (
|
2022-02-22 15:43:17 +00:00
|
|
|
"bytes"
|
2020-07-14 12:59:07 +01:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2020-07-15 12:02:34 +01:00
|
|
|
"strings"
|
2020-07-15 18:40:41 +01:00
|
|
|
"time"
|
2020-07-30 18:00:56 +01:00
|
|
|
|
2021-08-06 10:13:35 +01:00
|
|
|
"github.com/matrix-org/dendrite/keyserver/types"
|
2020-07-30 18:00:56 +01:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2020-08-05 13:41:16 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-07-14 12:59:07 +01:00
|
|
|
)
|
2020-07-13 16:02:35 +01:00
|
|
|
|
|
|
|
type KeyInternalAPI interface {
|
2020-07-30 18:00:56 +01:00
|
|
|
// SetUserAPI assigns a user API to query when extracting device names.
|
|
|
|
SetUserAPI(i userapi.UserInternalAPI)
|
2020-08-05 13:41:16 +01:00
|
|
|
// InputDeviceListUpdate from a federated server EDU
|
|
|
|
InputDeviceListUpdate(ctx context.Context, req *InputDeviceListUpdateRequest, res *InputDeviceListUpdateResponse)
|
2020-07-13 16:02:35 +01:00
|
|
|
PerformUploadKeys(ctx context.Context, req *PerformUploadKeysRequest, res *PerformUploadKeysResponse)
|
2020-07-21 14:47:53 +01:00
|
|
|
// PerformClaimKeys claims one-time keys for use in pre-key messages
|
2020-07-13 16:02:35 +01:00
|
|
|
PerformClaimKeys(ctx context.Context, req *PerformClaimKeysRequest, res *PerformClaimKeysResponse)
|
2021-08-18 12:07:09 +01:00
|
|
|
PerformDeleteKeys(ctx context.Context, req *PerformDeleteKeysRequest, res *PerformDeleteKeysResponse)
|
2021-08-04 17:56:29 +01:00
|
|
|
PerformUploadDeviceKeys(ctx context.Context, req *PerformUploadDeviceKeysRequest, res *PerformUploadDeviceKeysResponse)
|
|
|
|
PerformUploadDeviceSignatures(ctx context.Context, req *PerformUploadDeviceSignaturesRequest, res *PerformUploadDeviceSignaturesResponse)
|
2020-07-13 16:02:35 +01:00
|
|
|
QueryKeys(ctx context.Context, req *QueryKeysRequest, res *QueryKeysResponse)
|
2020-07-28 18:25:16 +01:00
|
|
|
QueryKeyChanges(ctx context.Context, req *QueryKeyChangesRequest, res *QueryKeyChangesResponse)
|
2020-08-03 12:29:58 +01:00
|
|
|
QueryOneTimeKeys(ctx context.Context, req *QueryOneTimeKeysRequest, res *QueryOneTimeKeysResponse)
|
2020-08-04 11:32:14 +01:00
|
|
|
QueryDeviceMessages(ctx context.Context, req *QueryDeviceMessagesRequest, res *QueryDeviceMessagesResponse)
|
2021-08-06 10:13:35 +01:00
|
|
|
QuerySignatures(ctx context.Context, req *QuerySignaturesRequest, res *QuerySignaturesResponse)
|
2020-07-13 16:02:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// KeyError is returned if there was a problem performing/querying the server
|
|
|
|
type KeyError struct {
|
2021-08-04 17:56:29 +01:00
|
|
|
Err string `json:"error"`
|
|
|
|
IsInvalidSignature bool `json:"is_invalid_signature,omitempty"` // M_INVALID_SIGNATURE
|
|
|
|
IsMissingParam bool `json:"is_missing_param,omitempty"` // M_MISSING_PARAM
|
2021-08-17 13:44:30 +01:00
|
|
|
IsInvalidParam bool `json:"is_invalid_param,omitempty"` // M_INVALID_PARAM
|
2020-07-15 12:02:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k *KeyError) Error() string {
|
|
|
|
return k.Err
|
2020-07-13 16:02:35 +01:00
|
|
|
}
|
|
|
|
|
2021-08-11 09:44:14 +01:00
|
|
|
type DeviceMessageType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
TypeDeviceKeyUpdate DeviceMessageType = iota
|
|
|
|
TypeCrossSigningUpdate
|
|
|
|
)
|
|
|
|
|
2020-08-03 17:07:06 +01:00
|
|
|
// DeviceMessage represents the message produced into Kafka by the key server.
|
|
|
|
type DeviceMessage struct {
|
2022-03-29 13:14:35 +01:00
|
|
|
Type DeviceMessageType `json:"Type,omitempty"`
|
|
|
|
*DeviceKeys `json:"DeviceKeys,omitempty"`
|
|
|
|
*OutputCrossSigningKeyUpdate `json:"CrossSigningKeyUpdate,omitempty"`
|
2020-08-03 17:07:06 +01:00
|
|
|
// A monotonically increasing number which represents device changes for this user.
|
2022-03-10 13:17:28 +00:00
|
|
|
StreamID int64
|
2022-01-21 09:56:06 +00:00
|
|
|
DeviceChangeID int64
|
2020-08-03 17:07:06 +01:00
|
|
|
}
|
|
|
|
|
2022-03-29 13:14:35 +01:00
|
|
|
// OutputCrossSigningKeyUpdate is an entry in the signing key update output kafka log
|
|
|
|
type OutputCrossSigningKeyUpdate struct {
|
|
|
|
CrossSigningKeyUpdate `json:"signing_keys"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CrossSigningKeyUpdate struct {
|
|
|
|
MasterKey *gomatrixserverlib.CrossSigningKey `json:"master_key,omitempty"`
|
|
|
|
SelfSigningKey *gomatrixserverlib.CrossSigningKey `json:"self_signing_key,omitempty"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
}
|
|
|
|
|
2022-02-22 15:43:17 +00:00
|
|
|
// DeviceKeysEqual returns true if the device keys updates contain the
|
|
|
|
// same display name and key JSON. This will return false if either of
|
|
|
|
// the updates is not a device keys update, or if the user ID/device ID
|
|
|
|
// differ between the two.
|
|
|
|
func (m1 *DeviceMessage) DeviceKeysEqual(m2 *DeviceMessage) bool {
|
|
|
|
if m1.DeviceKeys == nil || m2.DeviceKeys == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if m1.UserID != m2.UserID || m1.DeviceID != m2.DeviceID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if m1.DisplayName != m2.DisplayName {
|
|
|
|
return false // different display names
|
|
|
|
}
|
|
|
|
if len(m1.KeyJSON) == 0 || len(m2.KeyJSON) == 0 {
|
|
|
|
return false // either is empty
|
|
|
|
}
|
|
|
|
return bytes.Equal(m1.KeyJSON, m2.KeyJSON)
|
|
|
|
}
|
|
|
|
|
2020-07-14 12:59:07 +01:00
|
|
|
// DeviceKeys represents a set of device keys for a single device
|
|
|
|
// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-keys-upload
|
|
|
|
type DeviceKeys struct {
|
|
|
|
// The user who owns this device
|
|
|
|
UserID string
|
|
|
|
// The device ID of this device
|
|
|
|
DeviceID string
|
2020-08-03 17:07:06 +01:00
|
|
|
// The device display name
|
|
|
|
DisplayName string
|
2020-07-14 12:59:07 +01:00
|
|
|
// The raw device key JSON
|
|
|
|
KeyJSON []byte
|
|
|
|
}
|
|
|
|
|
2020-08-03 17:07:06 +01:00
|
|
|
// WithStreamID returns a copy of this device message with the given stream ID
|
2022-03-10 13:17:28 +00:00
|
|
|
func (k *DeviceKeys) WithStreamID(streamID int64) DeviceMessage {
|
2020-08-03 17:07:06 +01:00
|
|
|
return DeviceMessage{
|
2021-08-11 09:44:14 +01:00
|
|
|
DeviceKeys: k,
|
2020-08-03 17:07:06 +01:00
|
|
|
StreamID: streamID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 12:59:07 +01:00
|
|
|
// OneTimeKeys represents a set of one-time keys for a single device
|
|
|
|
// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-keys-upload
|
|
|
|
type OneTimeKeys struct {
|
|
|
|
// The user who owns this device
|
|
|
|
UserID string
|
|
|
|
// The device ID of this device
|
|
|
|
DeviceID string
|
|
|
|
// A map of algorithm:key_id => key JSON
|
|
|
|
KeyJSON map[string]json.RawMessage
|
|
|
|
}
|
|
|
|
|
2020-07-15 12:02:34 +01:00
|
|
|
// Split a key in KeyJSON into algorithm and key ID
|
|
|
|
func (k *OneTimeKeys) Split(keyIDWithAlgo string) (algo string, keyID string) {
|
|
|
|
segments := strings.Split(keyIDWithAlgo, ":")
|
|
|
|
return segments[0], segments[1]
|
|
|
|
}
|
|
|
|
|
2020-07-14 12:59:07 +01:00
|
|
|
// OneTimeKeysCount represents the counts of one-time keys for a single device
|
|
|
|
type OneTimeKeysCount struct {
|
|
|
|
// The user who owns this device
|
|
|
|
UserID string
|
|
|
|
// The device ID of this device
|
|
|
|
DeviceID string
|
|
|
|
// algorithm to count e.g:
|
|
|
|
// {
|
|
|
|
// "curve25519": 10,
|
|
|
|
// "signed_curve25519": 20
|
|
|
|
// }
|
|
|
|
KeyCount map[string]int
|
|
|
|
}
|
|
|
|
|
|
|
|
// PerformUploadKeysRequest is the request to PerformUploadKeys
|
2020-07-13 16:02:35 +01:00
|
|
|
type PerformUploadKeysRequest struct {
|
2021-03-02 11:40:20 +00:00
|
|
|
UserID string // Required - User performing the request
|
|
|
|
DeviceID string // Optional - Device performing the request, for fetching OTK count
|
2020-07-14 12:59:07 +01:00
|
|
|
DeviceKeys []DeviceKeys
|
|
|
|
OneTimeKeys []OneTimeKeys
|
2020-08-12 22:43:02 +01:00
|
|
|
// OnlyDisplayNameUpdates should be `true` if ALL the DeviceKeys are present to update
|
|
|
|
// the display name for their respective device, and NOT to modify the keys. The key
|
|
|
|
// itself doesn't change but it's easier to pretend upload new keys and reuse the same code paths.
|
|
|
|
// Without this flag, requests to modify device display names would delete device keys.
|
|
|
|
OnlyDisplayNameUpdates bool
|
2020-07-13 16:02:35 +01:00
|
|
|
}
|
|
|
|
|
2020-07-14 12:59:07 +01:00
|
|
|
// PerformUploadKeysResponse is the response to PerformUploadKeys
|
2020-07-13 16:02:35 +01:00
|
|
|
type PerformUploadKeysResponse struct {
|
2020-07-15 12:02:34 +01:00
|
|
|
// A fatal error when processing e.g database failures
|
2020-07-13 16:02:35 +01:00
|
|
|
Error *KeyError
|
2020-07-14 12:59:07 +01:00
|
|
|
// A map of user_id -> device_id -> Error for tracking failures.
|
|
|
|
KeyErrors map[string]map[string]*KeyError
|
|
|
|
OneTimeKeyCounts []OneTimeKeysCount
|
|
|
|
}
|
|
|
|
|
2021-08-18 12:07:09 +01:00
|
|
|
// PerformDeleteKeysRequest asks the keyserver to forget about certain
|
|
|
|
// keys, and signatures related to those keys.
|
|
|
|
type PerformDeleteKeysRequest struct {
|
|
|
|
UserID string
|
|
|
|
KeyIDs []gomatrixserverlib.KeyID
|
|
|
|
}
|
|
|
|
|
|
|
|
// PerformDeleteKeysResponse is the response to PerformDeleteKeysRequest.
|
|
|
|
type PerformDeleteKeysResponse struct {
|
|
|
|
Error *KeyError
|
|
|
|
}
|
|
|
|
|
2020-07-14 12:59:07 +01:00
|
|
|
// KeyError sets a key error field on KeyErrors
|
|
|
|
func (r *PerformUploadKeysResponse) KeyError(userID, deviceID string, err *KeyError) {
|
|
|
|
if r.KeyErrors[userID] == nil {
|
|
|
|
r.KeyErrors[userID] = make(map[string]*KeyError)
|
|
|
|
}
|
|
|
|
r.KeyErrors[userID][deviceID] = err
|
2020-07-13 16:02:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type PerformClaimKeysRequest struct {
|
2020-07-21 14:47:53 +01:00
|
|
|
// Map of user_id to device_id to algorithm name
|
|
|
|
OneTimeKeys map[string]map[string]string
|
|
|
|
Timeout time.Duration
|
2020-07-13 16:02:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type PerformClaimKeysResponse struct {
|
2020-07-21 14:47:53 +01:00
|
|
|
// Map of user_id to device_id to algorithm:key_id to key JSON
|
|
|
|
OneTimeKeys map[string]map[string]map[string]json.RawMessage
|
|
|
|
// Map of remote server domain to error JSON
|
|
|
|
Failures map[string]interface{}
|
|
|
|
// Set if there was a fatal error processing this action
|
2020-07-13 16:02:35 +01:00
|
|
|
Error *KeyError
|
|
|
|
}
|
|
|
|
|
2021-08-04 17:56:29 +01:00
|
|
|
type PerformUploadDeviceKeysRequest struct {
|
|
|
|
gomatrixserverlib.CrossSigningKeys
|
|
|
|
// The user that uploaded the key, should be populated by the clientapi.
|
2021-08-09 14:35:24 +01:00
|
|
|
UserID string
|
2021-08-04 17:56:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type PerformUploadDeviceKeysResponse struct {
|
|
|
|
Error *KeyError
|
|
|
|
}
|
|
|
|
|
|
|
|
type PerformUploadDeviceSignaturesRequest struct {
|
|
|
|
Signatures map[string]map[gomatrixserverlib.KeyID]gomatrixserverlib.CrossSigningForKeyOrDevice
|
|
|
|
// The user that uploaded the sig, should be populated by the clientapi.
|
2021-08-09 14:35:24 +01:00
|
|
|
UserID string
|
2021-08-04 17:56:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type PerformUploadDeviceSignaturesResponse struct {
|
|
|
|
Error *KeyError
|
|
|
|
}
|
|
|
|
|
2020-07-13 16:02:35 +01:00
|
|
|
type QueryKeysRequest struct {
|
2021-08-04 17:56:29 +01:00
|
|
|
// The user ID asking for the keys, e.g. if from a client API request.
|
|
|
|
// Will not be populated if the key request came from federation.
|
|
|
|
UserID string
|
2020-07-15 18:40:41 +01:00
|
|
|
// Maps user IDs to a list of devices
|
|
|
|
UserToDevices map[string][]string
|
|
|
|
Timeout time.Duration
|
2020-07-13 16:02:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type QueryKeysResponse struct {
|
2020-07-15 18:40:41 +01:00
|
|
|
// Map of remote server domain to error JSON
|
|
|
|
Failures map[string]interface{}
|
|
|
|
// Map of user_id to device_id to device_key
|
|
|
|
DeviceKeys map[string]map[string]json.RawMessage
|
2021-08-04 17:56:29 +01:00
|
|
|
// Maps of user_id to cross signing key
|
|
|
|
MasterKeys map[string]gomatrixserverlib.CrossSigningKey
|
|
|
|
SelfSigningKeys map[string]gomatrixserverlib.CrossSigningKey
|
|
|
|
UserSigningKeys map[string]gomatrixserverlib.CrossSigningKey
|
2020-07-15 18:40:41 +01:00
|
|
|
// Set if there was a fatal error processing this query
|
2020-07-13 16:02:35 +01:00
|
|
|
Error *KeyError
|
|
|
|
}
|
2020-07-28 18:25:16 +01:00
|
|
|
|
|
|
|
type QueryKeyChangesRequest struct {
|
|
|
|
// The offset of the last received key event, or sarama.OffsetOldest if this is from the beginning
|
|
|
|
Offset int64
|
2020-07-30 14:52:21 +01:00
|
|
|
// The inclusive offset where to track key changes up to. Messages with this offset are included in the response.
|
2022-02-04 13:08:13 +00:00
|
|
|
// Use types.OffsetNewest if the offset is unknown (then check the response Offset to avoid racing).
|
2020-07-30 14:52:21 +01:00
|
|
|
ToOffset int64
|
2020-07-28 18:25:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type QueryKeyChangesResponse struct {
|
|
|
|
// The set of users who have had their keys change.
|
|
|
|
UserIDs []string
|
|
|
|
// The latest offset represented in this response.
|
|
|
|
Offset int64
|
|
|
|
// Set if there was a problem handling the request.
|
|
|
|
Error *KeyError
|
|
|
|
}
|
2020-08-03 12:29:58 +01:00
|
|
|
|
|
|
|
type QueryOneTimeKeysRequest struct {
|
|
|
|
// The local user to query OTK counts for
|
|
|
|
UserID string
|
|
|
|
// The device to query OTK counts for
|
|
|
|
DeviceID string
|
|
|
|
}
|
|
|
|
|
|
|
|
type QueryOneTimeKeysResponse struct {
|
|
|
|
// OTK key counts, in the extended /sync form described by https://matrix.org/docs/spec/client_server/r0.6.1#id84
|
|
|
|
Count OneTimeKeysCount
|
|
|
|
Error *KeyError
|
|
|
|
}
|
2020-08-04 11:32:14 +01:00
|
|
|
|
|
|
|
type QueryDeviceMessagesRequest struct {
|
|
|
|
UserID string
|
|
|
|
}
|
|
|
|
|
|
|
|
type QueryDeviceMessagesResponse struct {
|
|
|
|
// The latest stream ID
|
2022-03-10 13:17:28 +00:00
|
|
|
StreamID int64
|
2020-08-04 11:32:14 +01:00
|
|
|
Devices []DeviceMessage
|
|
|
|
Error *KeyError
|
|
|
|
}
|
2020-08-05 13:41:16 +01:00
|
|
|
|
2021-08-06 10:13:35 +01:00
|
|
|
type QuerySignaturesRequest struct {
|
|
|
|
// A map of target user ID -> target key/device IDs to retrieve signatures for
|
|
|
|
TargetIDs map[string][]gomatrixserverlib.KeyID `json:"target_ids"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type QuerySignaturesResponse struct {
|
|
|
|
// A map of target user ID -> target key/device ID -> origin user ID -> origin key/device ID -> signatures
|
|
|
|
Signatures map[string]map[gomatrixserverlib.KeyID]types.CrossSigningSigMap
|
|
|
|
// A map of target user ID -> cross-signing master key
|
|
|
|
MasterKeys map[string]gomatrixserverlib.CrossSigningKey
|
|
|
|
// A map of target user ID -> cross-signing self-signing key
|
|
|
|
SelfSigningKeys map[string]gomatrixserverlib.CrossSigningKey
|
|
|
|
// A map of target user ID -> cross-signing user-signing key
|
|
|
|
UserSigningKeys map[string]gomatrixserverlib.CrossSigningKey
|
|
|
|
// The request error, if any
|
|
|
|
Error *KeyError
|
|
|
|
}
|
|
|
|
|
2020-08-05 13:41:16 +01:00
|
|
|
type InputDeviceListUpdateRequest struct {
|
|
|
|
Event gomatrixserverlib.DeviceListUpdateEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
type InputDeviceListUpdateResponse struct {
|
|
|
|
Error *KeyError
|
|
|
|
}
|