Generate key IDs automatically

This commit is contained in:
Neil Alexander 2021-07-28 16:41:53 +01:00
parent b638b06ec1
commit 701ffdeb36
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
4 changed files with 26 additions and 39 deletions

View file

@ -40,7 +40,7 @@ type KeyInternalAPI interface {
QueryDeviceMessages(ctx context.Context, req *QueryDeviceMessagesRequest, res *QueryDeviceMessagesResponse) QueryDeviceMessages(ctx context.Context, req *QueryDeviceMessagesRequest, res *QueryDeviceMessagesResponse)
} }
type CrossSigningKeyMap map[gomatrixserverlib.CrossSigningKeyPurpose]map[gomatrixserverlib.KeyID]gomatrixserverlib.Base64Bytes type CrossSigningKeyMap map[gomatrixserverlib.CrossSigningKeyPurpose]gomatrixserverlib.Base64Bytes
// KeyError is returned if there was a problem performing/querying the server // KeyError is returned if there was a problem performing/querying the server
type KeyError struct { type KeyError struct {

View file

@ -437,32 +437,29 @@ func (a *KeyInternalAPI) crossSigningKeys(
return fmt.Errorf("a.DB.CrossSigningKeysForUser (%q): %w", userID, err) return fmt.Errorf("a.DB.CrossSigningKeysForUser (%q): %w", userID, err)
} }
for keyType, keysByType := range keys { for keyType, keyData := range keys {
for keyID, keyData := range keysByType { b64 := keyData.Encode()
key := gomatrixserverlib.CrossSigningKey{ key := gomatrixserverlib.CrossSigningKey{
UserID: userID, UserID: userID,
Usage: []gomatrixserverlib.CrossSigningKeyPurpose{ Usage: []gomatrixserverlib.CrossSigningKeyPurpose{
keyType, keyType,
}, },
Keys: map[gomatrixserverlib.KeyID]gomatrixserverlib.Base64Bytes{ Keys: map[gomatrixserverlib.KeyID]gomatrixserverlib.Base64Bytes{
keyID: keyData, gomatrixserverlib.KeyID("ed25519:" + b64): keyData,
}, },
} }
logrus.WithField("key", key).Info("Cross-signing key") // TODO: populate signatures
// TODO: populate signatures switch keyType {
case gomatrixserverlib.CrossSigningKeyPurposeMaster:
res.MasterKeys[userID] = key
switch keyType { case gomatrixserverlib.CrossSigningKeyPurposeSelfSigning:
case gomatrixserverlib.CrossSigningKeyPurposeMaster: res.SelfSigningKeys[userID] = key
res.MasterKeys[userID] = key
case gomatrixserverlib.CrossSigningKeyPurposeSelfSigning: case gomatrixserverlib.CrossSigningKeyPurposeUserSigning:
res.SelfSigningKeys[userID] = key res.UserSigningKeys[userID] = key
case gomatrixserverlib.CrossSigningKeyPurposeUserSigning:
res.UserSigningKeys[userID] = key
}
} }
} }
} }

View file

@ -28,7 +28,6 @@ var crossSigningKeysSchema = `
CREATE TABLE IF NOT EXISTS keyserver_cross_signing_keys ( CREATE TABLE IF NOT EXISTS keyserver_cross_signing_keys (
user_id TEXT NOT NULL, user_id TEXT NOT NULL,
key_type TEXT NOT NULL, key_type TEXT NOT NULL,
key_id TEXT NOT NULL,
key_data TEXT NOT NULL, key_data TEXT NOT NULL,
stream_id BIGINT NOT NULL stream_id BIGINT NOT NULL
); );
@ -37,7 +36,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS keyserver_cross_signing_keys_idx ON keyserver_
` `
const selectCrossSigningKeysForUserSQL = "" + const selectCrossSigningKeysForUserSQL = "" +
"SELECT DISTINCT ON (user_id, key_type) key_type, key_id, key_data FROM keyserver_cross_signing_keys" + "SELECT DISTINCT ON (user_id, key_type) key_type, key_data FROM keyserver_cross_signing_keys" +
" WHERE user_id = $1" + " WHERE user_id = $1" +
" ORDER BY user_id, key_type, stream_id DESC" " ORDER BY user_id, key_type, stream_id DESC"
@ -71,15 +70,11 @@ func (s *crossSigningKeysStatements) SelectCrossSigningKeysForUser(
r = api.CrossSigningKeyMap{} r = api.CrossSigningKeyMap{}
for rows.Next() { for rows.Next() {
var keyType gomatrixserverlib.CrossSigningKeyPurpose var keyType gomatrixserverlib.CrossSigningKeyPurpose
var keyID gomatrixserverlib.KeyID
var keyData gomatrixserverlib.Base64Bytes var keyData gomatrixserverlib.Base64Bytes
if err := rows.Scan(&keyType, &keyID, &keyData); err != nil { if err := rows.Scan(&keyType, &keyData); err != nil {
return nil, err return nil, err
} }
if _, ok := r[keyType]; !ok { r[keyType] = keyData
r[keyType] = map[gomatrixserverlib.KeyID]gomatrixserverlib.Base64Bytes{}
}
r[keyType][keyID] = keyData
} }
return return
} }

View file

@ -28,7 +28,6 @@ var crossSigningKeysSchema = `
CREATE TABLE IF NOT EXISTS keyserver_cross_signing_keys ( CREATE TABLE IF NOT EXISTS keyserver_cross_signing_keys (
user_id TEXT NOT NULL, user_id TEXT NOT NULL,
key_type TEXT NOT NULL, key_type TEXT NOT NULL,
key_id TEXT NOT NULL,
key_data TEXT NOT NULL, key_data TEXT NOT NULL,
stream_id BIGINT NOT NULL stream_id BIGINT NOT NULL
); );
@ -37,7 +36,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS keyserver_cross_signing_keys_idx ON keyserver_
` `
const selectCrossSigningKeysForUserSQL = "" + const selectCrossSigningKeysForUserSQL = "" +
"SELECT key_type, key_id, key_data FROM " + "SELECT key_type, key_data FROM " +
" (SELECT * FROM keyserver_cross_signing_keys WHERE user_id = $1 ORDER BY stream_id DESC)" + " (SELECT * FROM keyserver_cross_signing_keys WHERE user_id = $1 ORDER BY stream_id DESC)" +
" GROUP BY user_id, key_type" " GROUP BY user_id, key_type"
@ -71,15 +70,11 @@ func (s *crossSigningKeysStatements) SelectCrossSigningKeysForUser(
r = api.CrossSigningKeyMap{} r = api.CrossSigningKeyMap{}
for rows.Next() { for rows.Next() {
var keyType gomatrixserverlib.CrossSigningKeyPurpose var keyType gomatrixserverlib.CrossSigningKeyPurpose
var keyID gomatrixserverlib.KeyID
var keyData gomatrixserverlib.Base64Bytes var keyData gomatrixserverlib.Base64Bytes
if err := rows.Scan(&keyType, &keyID, &keyData); err != nil { if err := rows.Scan(&keyType, &keyData); err != nil {
return nil, err return nil, err
} }
if _, ok := r[keyType]; !ok { r[keyType] = keyData
r[keyType] = map[gomatrixserverlib.KeyID]gomatrixserverlib.Base64Bytes{}
}
r[keyType][keyID] = keyData
} }
return return
} }