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)
}
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
type KeyError struct {

View file

@ -437,32 +437,29 @@ func (a *KeyInternalAPI) crossSigningKeys(
return fmt.Errorf("a.DB.CrossSigningKeysForUser (%q): %w", userID, err)
}
for keyType, keysByType := range keys {
for keyID, keyData := range keysByType {
key := gomatrixserverlib.CrossSigningKey{
UserID: userID,
Usage: []gomatrixserverlib.CrossSigningKeyPurpose{
keyType,
},
Keys: map[gomatrixserverlib.KeyID]gomatrixserverlib.Base64Bytes{
keyID: keyData,
},
}
for keyType, keyData := range keys {
b64 := keyData.Encode()
key := gomatrixserverlib.CrossSigningKey{
UserID: userID,
Usage: []gomatrixserverlib.CrossSigningKeyPurpose{
keyType,
},
Keys: map[gomatrixserverlib.KeyID]gomatrixserverlib.Base64Bytes{
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.CrossSigningKeyPurposeMaster:
res.MasterKeys[userID] = key
case gomatrixserverlib.CrossSigningKeyPurposeSelfSigning:
res.SelfSigningKeys[userID] = key
case gomatrixserverlib.CrossSigningKeyPurposeSelfSigning:
res.SelfSigningKeys[userID] = key
case gomatrixserverlib.CrossSigningKeyPurposeUserSigning:
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 (
user_id TEXT NOT NULL,
key_type TEXT NOT NULL,
key_id TEXT NOT NULL,
key_data TEXT 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 = "" +
"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" +
" ORDER BY user_id, key_type, stream_id DESC"
@ -71,15 +70,11 @@ func (s *crossSigningKeysStatements) SelectCrossSigningKeysForUser(
r = api.CrossSigningKeyMap{}
for rows.Next() {
var keyType gomatrixserverlib.CrossSigningKeyPurpose
var keyID gomatrixserverlib.KeyID
var keyData gomatrixserverlib.Base64Bytes
if err := rows.Scan(&keyType, &keyID, &keyData); err != nil {
if err := rows.Scan(&keyType, &keyData); err != nil {
return nil, err
}
if _, ok := r[keyType]; !ok {
r[keyType] = map[gomatrixserverlib.KeyID]gomatrixserverlib.Base64Bytes{}
}
r[keyType][keyID] = keyData
r[keyType] = keyData
}
return
}

View file

@ -28,7 +28,6 @@ var crossSigningKeysSchema = `
CREATE TABLE IF NOT EXISTS keyserver_cross_signing_keys (
user_id TEXT NOT NULL,
key_type TEXT NOT NULL,
key_id TEXT NOT NULL,
key_data TEXT 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 = "" +
"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)" +
" GROUP BY user_id, key_type"
@ -71,15 +70,11 @@ func (s *crossSigningKeysStatements) SelectCrossSigningKeysForUser(
r = api.CrossSigningKeyMap{}
for rows.Next() {
var keyType gomatrixserverlib.CrossSigningKeyPurpose
var keyID gomatrixserverlib.KeyID
var keyData gomatrixserverlib.Base64Bytes
if err := rows.Scan(&keyType, &keyID, &keyData); err != nil {
if err := rows.Scan(&keyType, &keyData); err != nil {
return nil, err
}
if _, ok := r[keyType]; !ok {
r[keyType] = map[gomatrixserverlib.KeyID]gomatrixserverlib.Base64Bytes{}
}
r[keyType][keyID] = keyData
r[keyType] = keyData
}
return
}