Skip to content

Commit

Permalink
Support plain Ed25519 key as returned by Nextcloud / PHP.
Browse files Browse the repository at this point in the history
  • Loading branch information
fancycode committed Jul 13, 2022
1 parent 4b2556c commit 91ac84b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
22 changes: 22 additions & 0 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
package signaling

import (
"bytes"
"context"
"crypto/ed25519"
"crypto/hmac"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"hash/fnv"
Expand Down Expand Up @@ -1030,6 +1034,24 @@ func (h *Hub) processHelloV2(client *Client, message *ClientMessage) (*Backend,
}
case *jwt.SigningMethodEd25519:
loadKeyFunc = func(data []byte) (interface{}, error) {
if !bytes.HasPrefix(data, []byte("-----BEGIN ")) {
// Nextcloud sends the Ed25519 key as base64-encoded public key data.
decoded, err := base64.StdEncoding.DecodeString(string(data))
if err != nil {
return nil, err
}

key := ed25519.PublicKey(decoded)
data, err = x509.MarshalPKIXPublicKey(key)
if err != nil {
return nil, err
}

data = pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: data,
})
}
return jwt.ParseEdPublicKeyFromPEM(data)
}
default:
Expand Down
9 changes: 8 additions & 1 deletion hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ var (
"RSA",
"ECDSA",
"Ed25519",
"Ed25519_Nextcloud",
}
)

Expand Down Expand Up @@ -715,7 +716,13 @@ func registerBackendHandlerUrl(t *testing.T, router *mux.Router, url string) {
Type: pemType,
Bytes: public,
})
signaling[ConfigKeyHelloV2TokenKey] = string(public)
if strings.Contains(t.Name(), "Ed25519_Nextcloud") {
// Simulate Nextcloud which returns the Ed25519 key as base64-encoded data.
encoded := base64.StdEncoding.EncodeToString(key.(ed25519.PublicKey))
signaling[ConfigKeyHelloV2TokenKey] = encoded
} else {
signaling[ConfigKeyHelloV2TokenKey] = string(public)
}
}
spreedCapa, _ := json.Marshal(map[string]interface{}{
"features": features,
Expand Down

0 comments on commit 91ac84b

Please sign in to comment.