2020-05-27 04:19:24 -05:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-06-15 08:05:57 -05:00
|
|
|
"crypto/ed25519"
|
|
|
|
"encoding/json"
|
2020-05-27 04:19:24 -05:00
|
|
|
"fmt"
|
2020-06-04 09:40:23 -05:00
|
|
|
"time"
|
2020-05-27 04:19:24 -05:00
|
|
|
|
2020-06-15 08:05:57 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/config"
|
2020-05-27 04:19:24 -05:00
|
|
|
"github.com/matrix-org/dendrite/serverkeyapi/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ServerKeyAPI struct {
|
|
|
|
api.ServerKeyInternalAPI
|
|
|
|
|
2020-06-15 08:05:57 -05:00
|
|
|
Cfg *config.Dendrite
|
2020-06-04 10:26:35 -05:00
|
|
|
OurKeyRing gomatrixserverlib.KeyRing
|
|
|
|
FedClient *gomatrixserverlib.FederationClient
|
2020-05-27 04:19:24 -05:00
|
|
|
}
|
|
|
|
|
2020-06-15 08:05:57 -05:00
|
|
|
func (s *ServerKeyAPI) QueryLocalKeys(ctx context.Context, request *api.QueryLocalKeysRequest, response *api.QueryLocalKeysResponse) error {
|
|
|
|
response.ServerKeys.ServerName = s.Cfg.Matrix.ServerName
|
|
|
|
|
|
|
|
publicKey := s.Cfg.Matrix.PrivateKey.Public().(ed25519.PublicKey)
|
|
|
|
response.ServerKeys.VerifyKeys = map[gomatrixserverlib.KeyID]gomatrixserverlib.VerifyKey{
|
|
|
|
s.Cfg.Matrix.KeyID: {
|
|
|
|
Key: gomatrixserverlib.Base64Bytes(publicKey),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
response.ServerKeys.TLSFingerprints = s.Cfg.Matrix.TLSFingerPrints
|
|
|
|
response.ServerKeys.OldVerifyKeys = map[gomatrixserverlib.KeyID]gomatrixserverlib.OldVerifyKey{}
|
|
|
|
response.ServerKeys.ValidUntilTS = gomatrixserverlib.AsTimestamp(time.Now().Add(s.Cfg.Matrix.KeyValidityPeriod))
|
|
|
|
|
|
|
|
toSign, err := json.Marshal(response.ServerKeys.ServerKeyFields)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.ServerKeys.Raw, err = gomatrixserverlib.SignJSON(
|
|
|
|
string(s.Cfg.Matrix.ServerName), s.Cfg.Matrix.KeyID, s.Cfg.Matrix.PrivateKey, toSign,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-27 04:19:24 -05:00
|
|
|
func (s *ServerKeyAPI) KeyRing() *gomatrixserverlib.KeyRing {
|
2020-06-10 05:02:03 -05: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-11 04:50:48 -05:00
|
|
|
KeyFetchers: []gomatrixserverlib.KeyFetcher{},
|
2020-06-10 05:02:03 -05:00
|
|
|
}
|
2020-05-27 04:19:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerKeyAPI) StoreKeys(
|
2020-06-04 09:40:23 -05:00
|
|
|
_ context.Context,
|
2020-05-27 04:19:24 -05:00
|
|
|
results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult,
|
|
|
|
) error {
|
2020-06-04 09:40:23 -05: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-05-27 04:19:24 -05: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 09:40:23 -05:00
|
|
|
_ context.Context,
|
2020-05-27 04:19:24 -05:00
|
|
|
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
|
|
|
|
) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) {
|
2020-06-04 09:40:23 -05: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-05-27 04:19:24 -05:00
|
|
|
results := map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult{}
|
2020-06-11 04:50:48 -05:00
|
|
|
now := gomatrixserverlib.AsTimestamp(time.Now())
|
2020-05-27 04:19:24 -05:00
|
|
|
// First consult our local database and see if we have the requested
|
|
|
|
// keys. These might come from a cache, depending on the database
|
|
|
|
// implementation used.
|
|
|
|
if dbResults, err := s.OurKeyRing.KeyDatabase.FetchKeys(ctx, requests); err == nil {
|
2020-06-15 08:05:57 -05:00
|
|
|
// We successfully got some keys. Add them to the results.
|
2020-05-27 04:19:24 -05:00
|
|
|
for req, res := range dbResults {
|
|
|
|
results[req] = res
|
2020-06-15 08:05:57 -05:00
|
|
|
// 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 res.WasValidAt(now, true) {
|
|
|
|
delete(requests, req)
|
|
|
|
}
|
2020-05-27 04:19:24 -05: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
|
|
|
|
// ask for the remaining keys.
|
|
|
|
for _, fetcher := range s.OurKeyRing.KeyFetchers {
|
|
|
|
if len(requests) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if fetcherResults, err := fetcher.FetchKeys(ctx, requests); err == nil {
|
2020-06-15 08:05:57 -05: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.
|
2020-05-27 04:19:24 -05:00
|
|
|
for req, res := range fetcherResults {
|
2020-06-15 08:05:57 -05:00
|
|
|
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.
|
|
|
|
storeResults[req] = res
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We didn't already have a previous entry for this request
|
|
|
|
// so store it in the database anyway for now.
|
|
|
|
storeResults[req] = res
|
2020-06-11 04:50:48 -05:00
|
|
|
}
|
2020-06-15 08:05:57 -05:00
|
|
|
// Update the results map with this new result. If nothing
|
|
|
|
// else, we can try verifying against this key.
|
2020-05-27 04:19:24 -05:00
|
|
|
results[req] = res
|
2020-06-15 08:05:57 -05:00
|
|
|
// If the key is valid right now then we can remove it from the
|
|
|
|
// request list as we won't need to re-fetch it.
|
|
|
|
if res.WasValidAt(now, true) {
|
|
|
|
delete(requests, req)
|
|
|
|
}
|
2020-05-27 04:19:24 -05:00
|
|
|
}
|
2020-06-15 08:05:57 -05:00
|
|
|
// Store the keys from our store map.
|
|
|
|
if err = s.OurKeyRing.KeyDatabase.StoreKeys(ctx, storeResults); err != nil {
|
2020-06-04 09:40:23 -05:00
|
|
|
return nil, fmt.Errorf("server key API failed to store retrieved keys: %w", err)
|
|
|
|
}
|
2020-05-27 04:19:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we failed to fetch any keys then we should report an error.
|
|
|
|
if len(requests) > 0 {
|
|
|
|
return results, fmt.Errorf("server key API failed to fetch %d keys", len(requests))
|
|
|
|
}
|
|
|
|
// Return the keys.
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerKeyAPI) FetcherName() string {
|
2020-06-10 05:02:03 -05:00
|
|
|
return fmt.Sprintf("ServerKeyAPI (wrapping %q)", s.OurKeyRing.KeyDatabase.FetcherName())
|
2020-05-27 04:19:24 -05:00
|
|
|
}
|