2020-05-27 11:19:24 +02:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-06-16 14:11:20 +02:00
|
|
|
"crypto/ed25519"
|
2020-05-27 11:19:24 +02:00
|
|
|
"fmt"
|
2020-06-04 16:40:23 +02:00
|
|
|
"time"
|
2020-05-27 11:19:24 +02:00
|
|
|
|
2020-12-02 18:41:00 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-10-07 17:23:18 +02:00
|
|
|
"github.com/matrix-org/dendrite/signingkeyserver/api"
|
2020-05-27 11:19:24 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-06-16 14:11:20 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-05-27 11:19:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type ServerKeyAPI struct {
|
2020-10-07 17:23:18 +02:00
|
|
|
api.SigningKeyServerAPI
|
2020-05-27 11:19:24 +02:00
|
|
|
|
2020-06-16 14:11:20 +02:00
|
|
|
ServerName gomatrixserverlib.ServerName
|
|
|
|
ServerPublicKey ed25519.PublicKey
|
|
|
|
ServerKeyID gomatrixserverlib.KeyID
|
|
|
|
ServerKeyValidity time.Duration
|
2020-09-30 20:52:36 +02:00
|
|
|
OldServerKeys []config.OldVerifyKeys
|
2020-06-16 14:11:20 +02:00
|
|
|
|
2020-06-04 17:26:35 +02:00
|
|
|
OurKeyRing gomatrixserverlib.KeyRing
|
2020-09-22 15:40:54 +02:00
|
|
|
FedClient gomatrixserverlib.KeyClient
|
2020-05-27 11:19:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerKeyAPI) KeyRing() *gomatrixserverlib.KeyRing {
|
2020-06-10 12:02:03 +02:00
|
|
|
// Return a keyring that forces requests to be proxied through the
|
|
|
|
// below functions. That way we can enforce things like validity
|
|
|
|
// and keeping the cache up-to-date.
|
|
|
|
return &gomatrixserverlib.KeyRing{
|
|
|
|
KeyDatabase: s,
|
2020-06-12 12:07:26 +02:00
|
|
|
KeyFetchers: []gomatrixserverlib.KeyFetcher{},
|
2020-06-10 12:02:03 +02:00
|
|
|
}
|
2020-05-27 11:19:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerKeyAPI) StoreKeys(
|
2020-06-04 16:40:23 +02:00
|
|
|
_ context.Context,
|
2020-05-27 11:19:24 +02:00
|
|
|
results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult,
|
|
|
|
) error {
|
2020-06-04 16:40:23 +02:00
|
|
|
// Run in a background context - we don't want to stop this work just
|
|
|
|
// because the caller gives up waiting.
|
|
|
|
ctx := context.Background()
|
2020-06-16 14:11:20 +02:00
|
|
|
|
2020-05-27 11:19:24 +02:00
|
|
|
// Store any keys that we were given in our database.
|
|
|
|
return s.OurKeyRing.KeyDatabase.StoreKeys(ctx, results)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerKeyAPI) FetchKeys(
|
2020-06-04 16:40:23 +02:00
|
|
|
_ context.Context,
|
2020-05-27 11:19:24 +02:00
|
|
|
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
|
|
|
|
) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) {
|
2020-06-04 16:40:23 +02:00
|
|
|
// Run in a background context - we don't want to stop this work just
|
|
|
|
// because the caller gives up waiting.
|
|
|
|
ctx := context.Background()
|
2020-06-12 12:07:26 +02:00
|
|
|
now := gomatrixserverlib.AsTimestamp(time.Now())
|
2020-06-16 14:11:20 +02:00
|
|
|
results := map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult{}
|
|
|
|
origRequests := map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp{}
|
|
|
|
for k, v := range requests {
|
|
|
|
origRequests[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, check if any of these key checks are for our own keys. If
|
|
|
|
// they are then we will satisfy them directly.
|
|
|
|
s.handleLocalKeys(ctx, requests, results)
|
|
|
|
|
|
|
|
// Then consult our local database and see if we have the requested
|
2020-05-27 11:19:24 +02:00
|
|
|
// keys. These might come from a cache, depending on the database
|
|
|
|
// implementation used.
|
2020-06-16 14:11:20 +02:00
|
|
|
if err := s.handleDatabaseKeys(ctx, now, requests, results); err != nil {
|
|
|
|
return nil, err
|
2020-05-27 11:19:24 +02:00
|
|
|
}
|
2020-06-16 14:11:20 +02:00
|
|
|
|
2020-05-27 11:19:24 +02:00
|
|
|
// For any key requests that we still have outstanding, next try to
|
|
|
|
// fetch them directly. We'll go through each of the key fetchers to
|
2020-06-16 14:11:20 +02:00
|
|
|
// ask for the remaining keys
|
2020-05-27 11:19:24 +02:00
|
|
|
for _, fetcher := range s.OurKeyRing.KeyFetchers {
|
2020-06-16 14:11:20 +02:00
|
|
|
// If there are no more keys to look up then stop.
|
2020-05-27 11:19:24 +02:00
|
|
|
if len(requests) == 0 {
|
|
|
|
break
|
|
|
|
}
|
2020-06-16 14:11:20 +02:00
|
|
|
|
|
|
|
// Ask the fetcher to look up our keys.
|
|
|
|
if err := s.handleFetcherKeys(ctx, now, fetcher, requests, results); err != nil {
|
|
|
|
logrus.WithError(err).WithFields(logrus.Fields{
|
|
|
|
"fetcher_name": fetcher.FetcherName(),
|
|
|
|
}).Errorf("Failed to retrieve %d key(s)", len(requests))
|
|
|
|
continue
|
2020-05-27 11:19:24 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-16 14:11:20 +02:00
|
|
|
|
|
|
|
// Check that we've actually satisfied all of the key requests that we
|
|
|
|
// were given. We should report an error if we didn't.
|
|
|
|
for req := range origRequests {
|
|
|
|
if _, ok := results[req]; !ok {
|
|
|
|
// The results don't contain anything for this specific request, so
|
|
|
|
// we've failed to satisfy it from local keys, database keys or from
|
|
|
|
// all of the fetchers. Report an error.
|
|
|
|
logrus.Warnf("Failed to retrieve key %q for server %q", req.KeyID, req.ServerName)
|
|
|
|
}
|
2020-05-27 11:19:24 +02:00
|
|
|
}
|
2020-06-16 14:11:20 +02:00
|
|
|
|
2020-05-27 11:19:24 +02:00
|
|
|
// Return the keys.
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerKeyAPI) FetcherName() string {
|
2020-06-10 12:02:03 +02:00
|
|
|
return fmt.Sprintf("ServerKeyAPI (wrapping %q)", s.OurKeyRing.KeyDatabase.FetcherName())
|
2020-05-27 11:19:24 +02:00
|
|
|
}
|
2020-06-16 14:11:20 +02:00
|
|
|
|
|
|
|
// handleLocalKeys handles cases where the key request contains
|
2020-09-30 20:52:36 +02:00
|
|
|
// a request for our own server keys, either current or old.
|
2020-06-16 14:11:20 +02:00
|
|
|
func (s *ServerKeyAPI) handleLocalKeys(
|
|
|
|
_ context.Context,
|
|
|
|
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
|
|
|
|
results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult,
|
|
|
|
) {
|
|
|
|
for req := range requests {
|
2020-09-30 20:52:36 +02:00
|
|
|
if req.ServerName != s.ServerName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if req.KeyID == s.ServerKeyID {
|
2020-06-16 14:11:20 +02:00
|
|
|
// We found a key request that is supposed to be for our own
|
|
|
|
// keys. Remove it from the request list so we don't hit the
|
|
|
|
// database or the fetchers for it.
|
|
|
|
delete(requests, req)
|
|
|
|
|
|
|
|
// Insert our own key into the response.
|
|
|
|
results[req] = gomatrixserverlib.PublicKeyLookupResult{
|
|
|
|
VerifyKey: gomatrixserverlib.VerifyKey{
|
|
|
|
Key: gomatrixserverlib.Base64Bytes(s.ServerPublicKey),
|
|
|
|
},
|
|
|
|
ExpiredTS: gomatrixserverlib.PublicKeyNotExpired,
|
|
|
|
ValidUntilTS: gomatrixserverlib.AsTimestamp(time.Now().Add(s.ServerKeyValidity)),
|
|
|
|
}
|
2020-09-30 20:52:36 +02:00
|
|
|
} else {
|
|
|
|
// The key request doesn't match our current key. Let's see
|
|
|
|
// if it matches any of our old verify keys.
|
|
|
|
for _, oldVerifyKey := range s.OldServerKeys {
|
|
|
|
if req.KeyID == oldVerifyKey.KeyID {
|
|
|
|
// We found a key request that is supposed to be an expired
|
|
|
|
// key.
|
|
|
|
delete(requests, req)
|
|
|
|
|
|
|
|
// Insert our own key into the response.
|
|
|
|
results[req] = gomatrixserverlib.PublicKeyLookupResult{
|
|
|
|
VerifyKey: gomatrixserverlib.VerifyKey{
|
|
|
|
Key: gomatrixserverlib.Base64Bytes(oldVerifyKey.PrivateKey.Public().(ed25519.PublicKey)),
|
|
|
|
},
|
|
|
|
ExpiredTS: oldVerifyKey.ExpiredAt,
|
|
|
|
ValidUntilTS: gomatrixserverlib.PublicKeyNotValid,
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need to look at the other keys.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-06-16 14:11:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleDatabaseKeys handles cases where the key requests can be
|
|
|
|
// satisfied from our local database/cache.
|
|
|
|
func (s *ServerKeyAPI) handleDatabaseKeys(
|
|
|
|
ctx context.Context,
|
|
|
|
now gomatrixserverlib.Timestamp,
|
|
|
|
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
|
|
|
|
results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult,
|
|
|
|
) error {
|
|
|
|
// Ask the database/cache for the keys.
|
|
|
|
dbResults, err := s.OurKeyRing.KeyDatabase.FetchKeys(ctx, requests)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We successfully got some keys. Add them to the results.
|
|
|
|
for req, res := range dbResults {
|
|
|
|
// The key we've retrieved from the database/cache might
|
|
|
|
// have passed its validity period, but right now, it's
|
|
|
|
// the best thing we've got, and it might be sufficient to
|
|
|
|
// verify a past event.
|
|
|
|
results[req] = res
|
|
|
|
|
|
|
|
// If the key is valid right now then we can also remove it
|
|
|
|
// from the request list as we don't need to fetch it again
|
|
|
|
// in that case. If the key isn't valid right now, then by
|
|
|
|
// leaving it in the 'requests' map, we'll try to update the
|
|
|
|
// key using the fetchers in handleFetcherKeys.
|
|
|
|
if res.WasValidAt(now, true) {
|
|
|
|
delete(requests, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleFetcherKeys handles cases where a fetcher can satisfy
|
|
|
|
// the remaining requests.
|
|
|
|
func (s *ServerKeyAPI) handleFetcherKeys(
|
|
|
|
ctx context.Context,
|
2020-09-30 20:52:36 +02:00
|
|
|
_ gomatrixserverlib.Timestamp,
|
2020-06-16 14:11:20 +02:00
|
|
|
fetcher gomatrixserverlib.KeyFetcher,
|
|
|
|
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
|
|
|
|
results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult,
|
|
|
|
) error {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"fetcher_name": fetcher.FetcherName(),
|
|
|
|
}).Infof("Fetching %d key(s)", len(requests))
|
|
|
|
|
|
|
|
// Create a context that limits our requests to 30 seconds.
|
|
|
|
fetcherCtx, fetcherCancel := context.WithTimeout(ctx, time.Second*30)
|
|
|
|
defer fetcherCancel()
|
|
|
|
|
|
|
|
// Try to fetch the keys.
|
|
|
|
fetcherResults, err := fetcher.FetchKeys(fetcherCtx, requests)
|
|
|
|
if err != nil {
|
2020-09-29 18:08:18 +02:00
|
|
|
return fmt.Errorf("fetcher.FetchKeys: %w", err)
|
2020-06-16 14:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build a map of the results that we want to commit to the
|
|
|
|
// database. We do this in a separate map because otherwise we
|
|
|
|
// might end up trying to rewrite database entries.
|
|
|
|
storeResults := map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult{}
|
|
|
|
|
|
|
|
// Now let's look at the results that we got from this fetcher.
|
|
|
|
for req, res := range fetcherResults {
|
|
|
|
if prev, ok := results[req]; ok {
|
|
|
|
// We've already got a previous entry for this request
|
|
|
|
// so let's see if the newly retrieved one contains a more
|
|
|
|
// up-to-date validity period.
|
|
|
|
if res.ValidUntilTS > prev.ValidUntilTS {
|
|
|
|
// This key is newer than the one we had so let's store
|
|
|
|
// it in the database.
|
2020-09-29 18:34:48 +02:00
|
|
|
storeResults[req] = res
|
2020-06-16 14:11:20 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We didn't already have a previous entry for this request
|
|
|
|
// so store it in the database anyway for now.
|
2020-09-29 18:34:48 +02:00
|
|
|
storeResults[req] = res
|
2020-06-16 14:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the results map with this new result. If nothing
|
|
|
|
// else, we can try verifying against this key.
|
|
|
|
results[req] = res
|
|
|
|
|
2020-09-29 18:34:48 +02:00
|
|
|
// Remove it from the request list so we won't re-fetch it.
|
|
|
|
delete(requests, req)
|
2020-06-16 14:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store the keys from our store map.
|
2020-09-29 18:34:48 +02:00
|
|
|
if err = s.OurKeyRing.KeyDatabase.StoreKeys(context.Background(), storeResults); err != nil {
|
2020-06-16 14:11:20 +02:00
|
|
|
logrus.WithError(err).WithFields(logrus.Fields{
|
|
|
|
"fetcher_name": fetcher.FetcherName(),
|
|
|
|
"database_name": s.OurKeyRing.KeyDatabase.FetcherName(),
|
|
|
|
}).Errorf("Failed to store keys in the database")
|
|
|
|
return fmt.Errorf("server key API failed to store retrieved keys: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(storeResults) > 0 {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"fetcher_name": fetcher.FetcherName(),
|
2020-09-29 18:34:48 +02:00
|
|
|
}).Infof("Updated %d of %d key(s) in database (%d keys remaining)", len(storeResults), len(results), len(requests))
|
2020-06-16 14:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|