2017-05-19 15:06:41 +00:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-10-25 10:27:44 +00:00
|
|
|
package routing
|
2017-05-19 15:06:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-03-13 15:55:45 +00:00
|
|
|
"net/http"
|
2017-09-07 11:50:39 +00:00
|
|
|
"time"
|
|
|
|
|
2020-05-21 13:40:13 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/config"
|
2017-05-19 15:06:41 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
"golang.org/x/crypto/ed25519"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LocalKeys returns the local keys for the server.
|
|
|
|
// See https://matrix.org/docs/spec/server_server/unstable.html#publishing-keys
|
2020-02-11 11:18:12 +00:00
|
|
|
func LocalKeys(cfg *config.Dendrite) util.JSONResponse {
|
2017-06-19 14:21:04 +00:00
|
|
|
keys, err := localKeys(cfg, time.Now().Add(cfg.Matrix.KeyValidityPeriod))
|
2017-05-19 15:06:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2018-03-13 15:55:45 +00:00
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: keys}
|
2017-05-19 15:06:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 11:18:12 +00:00
|
|
|
func localKeys(cfg *config.Dendrite, validUntil time.Time) (*gomatrixserverlib.ServerKeys, error) {
|
2017-05-19 15:06:41 +00:00
|
|
|
var keys gomatrixserverlib.ServerKeys
|
|
|
|
|
2017-06-19 14:21:04 +00:00
|
|
|
keys.ServerName = cfg.Matrix.ServerName
|
2017-05-19 15:06:41 +00:00
|
|
|
|
2017-06-19 14:21:04 +00:00
|
|
|
publicKey := cfg.Matrix.PrivateKey.Public().(ed25519.PublicKey)
|
2017-05-19 15:06:41 +00:00
|
|
|
|
|
|
|
keys.VerifyKeys = map[gomatrixserverlib.KeyID]gomatrixserverlib.VerifyKey{
|
2017-06-19 14:21:04 +00:00
|
|
|
cfg.Matrix.KeyID: {
|
2020-06-04 10:50:57 +00:00
|
|
|
Key: gomatrixserverlib.Base64Bytes(publicKey),
|
2017-05-19 15:06:41 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-06-19 14:21:04 +00:00
|
|
|
keys.TLSFingerprints = cfg.Matrix.TLSFingerPrints
|
2017-05-19 15:06:41 +00:00
|
|
|
keys.OldVerifyKeys = map[gomatrixserverlib.KeyID]gomatrixserverlib.OldVerifyKey{}
|
|
|
|
keys.ValidUntilTS = gomatrixserverlib.AsTimestamp(validUntil)
|
|
|
|
|
|
|
|
toSign, err := json.Marshal(keys.ServerKeyFields)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-19 14:21:04 +00:00
|
|
|
keys.Raw, err = gomatrixserverlib.SignJSON(
|
|
|
|
string(cfg.Matrix.ServerName), cfg.Matrix.KeyID, cfg.Matrix.PrivateKey, toSign,
|
|
|
|
)
|
2017-05-19 15:06:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &keys, nil
|
|
|
|
}
|