mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-23 14:53:10 -06:00
Enforce valid key IDs
This commit is contained in:
parent
f908f8baab
commit
0cf8e9166c
|
|
@ -36,6 +36,9 @@ import (
|
||||||
jaegermetrics "github.com/uber/jaeger-lib/metrics"
|
jaegermetrics "github.com/uber/jaeger-lib/metrics"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// keyIDRegexp defines allowable characters in Key IDs.
|
||||||
|
var keyIDRegexp = regexp.MustCompile("^ed25519:[a-zA-Z0-9_]+$")
|
||||||
|
|
||||||
// Version is the current version of the config format.
|
// Version is the current version of the config format.
|
||||||
// This will change whenever we make breaking changes to the config format.
|
// This will change whenever we make breaking changes to the config format.
|
||||||
const Version = 1
|
const Version = 1
|
||||||
|
|
@ -459,6 +462,9 @@ func readKeyPEM(path string, data []byte) (gomatrixserverlib.KeyID, ed25519.Priv
|
||||||
if !strings.HasPrefix(keyID, "ed25519:") {
|
if !strings.HasPrefix(keyID, "ed25519:") {
|
||||||
return "", nil, fmt.Errorf("key ID %q doesn't start with \"ed25519:\" in %q", keyID, path)
|
return "", nil, fmt.Errorf("key ID %q doesn't start with \"ed25519:\" in %q", keyID, path)
|
||||||
}
|
}
|
||||||
|
if !keyIDRegexp.MatchString(keyID) {
|
||||||
|
return "", nil, fmt.Errorf("key ID %q in %q contains illegal characters (use a-z, A-Z, 0-9 and _ only)", keyID, path)
|
||||||
|
}
|
||||||
_, privKey, err := ed25519.GenerateKey(bytes.NewReader(keyBlock.Bytes))
|
_, privKey, err := ed25519.GenerateKey(bytes.NewReader(keyBlock.Bytes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ import (
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal/config"
|
"github.com/matrix-org/dendrite/internal/config"
|
||||||
|
|
@ -146,10 +147,14 @@ func NewMatrixKey(matrixKeyPath string) (err error) {
|
||||||
err = keyOut.Close()
|
err = keyOut.Close()
|
||||||
})()
|
})()
|
||||||
|
|
||||||
|
keyID := base64.RawURLEncoding.EncodeToString(data[:])
|
||||||
|
keyID = strings.ReplaceAll(keyID, "-", "")
|
||||||
|
keyID = strings.ReplaceAll(keyID, "_", "")
|
||||||
|
|
||||||
err = pem.Encode(keyOut, &pem.Block{
|
err = pem.Encode(keyOut, &pem.Block{
|
||||||
Type: "MATRIX PRIVATE KEY",
|
Type: "MATRIX PRIVATE KEY",
|
||||||
Headers: map[string]string{
|
Headers: map[string]string{
|
||||||
"Key-ID": "ed25519:" + base64.RawStdEncoding.EncodeToString(data[:3]),
|
"Key-ID": fmt.Sprintf("ed25519:%s", keyID[:6]),
|
||||||
},
|
},
|
||||||
Bytes: data[3:],
|
Bytes: data[3:],
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue