2017-06-09 12:07:34 -05: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.
|
|
|
|
|
|
|
|
package keydb
|
|
|
|
|
|
|
|
import (
|
2017-09-21 10:16:02 -05:00
|
|
|
"context"
|
2017-06-09 12:07:34 -05:00
|
|
|
"database/sql"
|
2017-08-07 05:51:46 -05:00
|
|
|
|
2017-06-09 12:07:34 -05:00
|
|
|
"github.com/lib/pq"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
const serverKeysSchema = `
|
2017-11-15 11:46:16 -06:00
|
|
|
-- A cache of signing keys downloaded from remote servers.
|
2017-08-07 05:51:46 -05:00
|
|
|
CREATE TABLE IF NOT EXISTS keydb_server_keys (
|
2017-06-09 12:07:34 -05:00
|
|
|
-- The name of the matrix server the key is for.
|
|
|
|
server_name TEXT NOT NULL,
|
|
|
|
-- The ID of the server key.
|
|
|
|
server_key_id TEXT NOT NULL,
|
|
|
|
-- Combined server name and key ID separated by the ASCII unit separator
|
|
|
|
-- to make it easier to run bulk queries.
|
|
|
|
server_name_and_key_id TEXT NOT NULL,
|
2017-11-15 11:46:16 -06:00
|
|
|
-- When the key is valid until as a millisecond timestamp.
|
|
|
|
-- 0 if this is an expired key (in which case expired_ts will be non-zero)
|
2017-06-09 12:07:34 -05:00
|
|
|
valid_until_ts BIGINT NOT NULL,
|
2017-11-15 11:46:16 -06:00
|
|
|
-- When the key expired as a millisecond timestamp.
|
|
|
|
-- 0 if this is an active key (in which case valid_until_ts will be non-zero)
|
|
|
|
expired_ts BIGINT NOT NULL,
|
|
|
|
-- The base64-encoded public key.
|
|
|
|
server_key TEXT NOT NULL,
|
2017-08-07 05:51:46 -05:00
|
|
|
CONSTRAINT keydb_server_keys_unique UNIQUE (server_name, server_key_id)
|
2017-06-09 12:07:34 -05:00
|
|
|
);
|
|
|
|
|
2017-08-07 05:51:46 -05:00
|
|
|
CREATE INDEX IF NOT EXISTS keydb_server_name_and_key_id ON keydb_server_keys (server_name_and_key_id);
|
2017-06-09 12:07:34 -05:00
|
|
|
`
|
|
|
|
|
|
|
|
const bulkSelectServerKeysSQL = "" +
|
2017-11-15 11:46:16 -06:00
|
|
|
"SELECT server_name, server_key_id, valid_until_ts, expired_ts, " +
|
|
|
|
" server_key FROM keydb_server_keys" +
|
2017-06-09 12:07:34 -05:00
|
|
|
" WHERE server_name_and_key_id = ANY($1)"
|
|
|
|
|
|
|
|
const upsertServerKeysSQL = "" +
|
2017-08-07 05:51:46 -05:00
|
|
|
"INSERT INTO keydb_server_keys (server_name, server_key_id," +
|
2017-11-15 11:46:16 -06:00
|
|
|
" server_name_and_key_id, valid_until_ts, expired_ts, server_key)" +
|
|
|
|
" VALUES ($1, $2, $3, $4, $5, $6)" +
|
2017-08-07 05:51:46 -05:00
|
|
|
" ON CONFLICT ON CONSTRAINT keydb_server_keys_unique" +
|
2017-11-15 11:46:16 -06:00
|
|
|
" DO UPDATE SET valid_until_ts = $4, expired_ts = $5, server_key = $6"
|
2017-06-09 12:07:34 -05:00
|
|
|
|
|
|
|
type serverKeyStatements struct {
|
|
|
|
bulkSelectServerKeysStmt *sql.Stmt
|
|
|
|
upsertServerKeysStmt *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serverKeyStatements) prepare(db *sql.DB) (err error) {
|
|
|
|
_, err = db.Exec(serverKeysSchema)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.bulkSelectServerKeysStmt, err = db.Prepare(bulkSelectServerKeysSQL); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.upsertServerKeysStmt, err = db.Prepare(upsertServerKeysSQL); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serverKeyStatements) bulkSelectServerKeys(
|
2017-09-21 10:16:02 -05:00
|
|
|
ctx context.Context,
|
2018-06-01 11:42:55 -05:00
|
|
|
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
|
|
|
|
) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) {
|
2017-06-09 12:07:34 -05:00
|
|
|
var nameAndKeyIDs []string
|
|
|
|
for request := range requests {
|
|
|
|
nameAndKeyIDs = append(nameAndKeyIDs, nameAndKeyID(request))
|
|
|
|
}
|
2017-09-21 10:16:02 -05:00
|
|
|
stmt := s.bulkSelectServerKeysStmt
|
|
|
|
rows, err := stmt.QueryContext(ctx, pq.StringArray(nameAndKeyIDs))
|
2017-06-09 12:07:34 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-20 04:59:19 -05:00
|
|
|
defer rows.Close() // nolint: errcheck
|
2018-06-01 11:42:55 -05:00
|
|
|
results := map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult{}
|
2017-06-09 12:07:34 -05:00
|
|
|
for rows.Next() {
|
|
|
|
var serverName string
|
|
|
|
var keyID string
|
2017-11-15 11:46:16 -06:00
|
|
|
var key string
|
|
|
|
var validUntilTS int64
|
|
|
|
var expiredTS int64
|
|
|
|
if err = rows.Scan(&serverName, &keyID, &validUntilTS, &expiredTS, &key); err != nil {
|
2017-06-09 12:07:34 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-01 11:42:55 -05:00
|
|
|
r := gomatrixserverlib.PublicKeyLookupRequest{
|
2017-09-20 07:40:22 -05:00
|
|
|
ServerName: gomatrixserverlib.ServerName(serverName),
|
|
|
|
KeyID: gomatrixserverlib.KeyID(keyID),
|
2017-06-09 12:07:34 -05:00
|
|
|
}
|
2017-11-15 11:46:16 -06:00
|
|
|
vk := gomatrixserverlib.VerifyKey{}
|
|
|
|
err = vk.Key.Decode(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
results[r] = gomatrixserverlib.PublicKeyLookupResult{
|
|
|
|
VerifyKey: vk,
|
|
|
|
ValidUntilTS: gomatrixserverlib.Timestamp(validUntilTS),
|
|
|
|
ExpiredTS: gomatrixserverlib.Timestamp(expiredTS),
|
|
|
|
}
|
2017-06-09 12:07:34 -05:00
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serverKeyStatements) upsertServerKeys(
|
2017-09-21 10:16:02 -05:00
|
|
|
ctx context.Context,
|
2018-06-01 11:42:55 -05:00
|
|
|
request gomatrixserverlib.PublicKeyLookupRequest,
|
2017-11-15 11:46:16 -06:00
|
|
|
key gomatrixserverlib.PublicKeyLookupResult,
|
2017-06-09 12:07:34 -05:00
|
|
|
) error {
|
2017-11-15 11:46:16 -06:00
|
|
|
_, err := s.upsertServerKeysStmt.ExecContext(
|
2017-09-21 10:16:02 -05:00
|
|
|
ctx,
|
|
|
|
string(request.ServerName),
|
|
|
|
string(request.KeyID),
|
|
|
|
nameAndKeyID(request),
|
2017-11-15 11:46:16 -06:00
|
|
|
key.ValidUntilTS,
|
|
|
|
key.ExpiredTS,
|
|
|
|
key.Key.Encode(),
|
2017-06-09 12:07:34 -05:00
|
|
|
)
|
2017-09-20 08:15:38 -05:00
|
|
|
return err
|
2017-06-09 12:07:34 -05:00
|
|
|
}
|
|
|
|
|
2018-06-01 11:42:55 -05:00
|
|
|
func nameAndKeyID(request gomatrixserverlib.PublicKeyLookupRequest) string {
|
2017-06-09 12:07:34 -05:00
|
|
|
return string(request.ServerName) + "\x1F" + string(request.KeyID)
|
|
|
|
}
|