2020-10-07 17:23:18 +02:00
|
|
|
package signingkeyserver
|
2020-05-27 11:19:24 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ed25519"
|
|
|
|
"encoding/base64"
|
|
|
|
|
2020-06-08 16:51:07 +02:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
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"
|
|
|
|
"github.com/matrix-org/dendrite/signingkeyserver/internal"
|
|
|
|
"github.com/matrix-org/dendrite/signingkeyserver/inthttp"
|
|
|
|
"github.com/matrix-org/dendrite/signingkeyserver/storage"
|
|
|
|
"github.com/matrix-org/dendrite/signingkeyserver/storage/cache"
|
2020-05-27 11:19:24 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-06-08 16:51:07 +02:00
|
|
|
// AddInternalRoutes registers HTTP handlers for the internal API. Invokes functions
|
|
|
|
// on the given input API.
|
2020-10-07 17:23:18 +02:00
|
|
|
func AddInternalRoutes(router *mux.Router, intAPI api.SigningKeyServerAPI, caches *caching.Caches) {
|
2020-06-08 16:51:07 +02:00
|
|
|
inthttp.AddRoutes(intAPI, router, caches)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewInternalAPI returns a concerete implementation of the internal API. Callers
|
|
|
|
// can call functions directly on the returned API or via an HTTP interface using AddInternalRoutes.
|
|
|
|
func NewInternalAPI(
|
2020-10-07 17:23:18 +02:00
|
|
|
cfg *config.SigningKeyServer,
|
2020-09-22 15:40:54 +02:00
|
|
|
fedClient gomatrixserverlib.KeyClient,
|
2020-06-08 16:51:07 +02:00
|
|
|
caches *caching.Caches,
|
2020-10-07 17:23:18 +02:00
|
|
|
) api.SigningKeyServerAPI {
|
2020-05-27 11:19:24 +02:00
|
|
|
innerDB, err := storage.NewDatabase(
|
2020-08-10 15:18:04 +02:00
|
|
|
&cfg.Database,
|
2020-06-08 16:51:07 +02:00
|
|
|
cfg.Matrix.ServerName,
|
|
|
|
cfg.Matrix.PrivateKey.Public().(ed25519.PublicKey),
|
|
|
|
cfg.Matrix.KeyID,
|
2020-05-27 11:19:24 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panicf("failed to connect to server key database")
|
|
|
|
}
|
|
|
|
|
2020-06-08 16:51:07 +02:00
|
|
|
serverKeyDB, err := cache.NewKeyDatabase(innerDB, caches)
|
2020-05-27 11:19:24 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panicf("failed to set up caching wrapper for server key database")
|
|
|
|
}
|
|
|
|
|
|
|
|
internalAPI := internal.ServerKeyAPI{
|
2020-06-16 14:11:20 +02:00
|
|
|
ServerName: cfg.Matrix.ServerName,
|
|
|
|
ServerPublicKey: cfg.Matrix.PrivateKey.Public().(ed25519.PublicKey),
|
|
|
|
ServerKeyID: cfg.Matrix.KeyID,
|
|
|
|
ServerKeyValidity: cfg.Matrix.KeyValidityPeriod,
|
2020-09-30 20:52:36 +02:00
|
|
|
OldServerKeys: cfg.Matrix.OldVerifyKeys,
|
2020-06-16 14:11:20 +02:00
|
|
|
FedClient: fedClient,
|
2020-05-27 11:19:24 +02:00
|
|
|
OurKeyRing: gomatrixserverlib.KeyRing{
|
2020-09-29 18:08:18 +02:00
|
|
|
KeyFetchers: []gomatrixserverlib.KeyFetcher{},
|
2020-05-27 11:19:24 +02:00
|
|
|
KeyDatabase: serverKeyDB,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-09-29 18:08:18 +02:00
|
|
|
addDirectFetcher := func() {
|
|
|
|
internalAPI.OurKeyRing.KeyFetchers = append(
|
|
|
|
internalAPI.OurKeyRing.KeyFetchers,
|
|
|
|
&gomatrixserverlib.DirectKeyFetcher{
|
|
|
|
Client: fedClient,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.PreferDirectFetch {
|
|
|
|
addDirectFetcher()
|
|
|
|
} else {
|
|
|
|
defer addDirectFetcher()
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:19:24 +02:00
|
|
|
var b64e = base64.StdEncoding.WithPadding(base64.NoPadding)
|
2020-08-10 15:18:04 +02:00
|
|
|
for _, ps := range cfg.KeyPerspectives {
|
2020-05-27 11:19:24 +02:00
|
|
|
perspective := &gomatrixserverlib.PerspectiveKeyFetcher{
|
|
|
|
PerspectiveServerName: ps.ServerName,
|
|
|
|
PerspectiveServerKeys: map[gomatrixserverlib.KeyID]ed25519.PublicKey{},
|
2020-09-22 15:40:54 +02:00
|
|
|
Client: fedClient,
|
2020-05-27 11:19:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range ps.Keys {
|
|
|
|
rawkey, err := b64e.DecodeString(key.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).WithFields(logrus.Fields{
|
|
|
|
"server_name": ps.ServerName,
|
|
|
|
"public_key": key.PublicKey,
|
|
|
|
}).Warn("Couldn't parse perspective key")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
perspective.PerspectiveServerKeys[key.KeyID] = rawkey
|
|
|
|
}
|
|
|
|
|
|
|
|
internalAPI.OurKeyRing.KeyFetchers = append(
|
|
|
|
internalAPI.OurKeyRing.KeyFetchers,
|
|
|
|
perspective,
|
|
|
|
)
|
|
|
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"server_name": ps.ServerName,
|
|
|
|
"num_public_keys": len(ps.Keys),
|
|
|
|
}).Info("Enabled perspective key fetcher")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &internalAPI
|
|
|
|
}
|