Skip to content

Commit 5ad5666

Browse files
committed
fix: support more key formats
1 parent fd1d6f8 commit 5ad5666

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

pkg/liqo-controller-manager/authentication/csr.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,24 @@ func checkCSR(csr, publicKey []byte, checkPublicKey bool, commonName, organizati
170170
return fmt.Errorf("invalid public key")
171171
}
172172

173-
// Marshal CSR public key to PKIX DER and compare with provided PKIX public key bytes
173+
// Parse the provided public key (supports PEM, PKIX DER, or raw Ed25519)
174+
parsedPubKey, err := parsePublicKey(publicKey)
175+
if err != nil {
176+
return fmt.Errorf("failed to parse provided public key: %w", err)
177+
}
178+
179+
// Marshal both keys to PKIX DER for comparison
174180
csrPubDER, err := x509.MarshalPKIXPublicKey(x509Csr.PublicKey)
175181
if err != nil {
176182
return fmt.Errorf("failed to marshal CSR public key: %w", err)
177183
}
178184

179-
if !bytes.Equal(csrPubDER, publicKey) {
185+
parsedPubDER, err := x509.MarshalPKIXPublicKey(parsedPubKey)
186+
if err != nil {
187+
return fmt.Errorf("failed to marshal provided public key: %w", err)
188+
}
189+
190+
if !bytes.Equal(csrPubDER, parsedPubDER) {
180191
return fmt.Errorf("invalid public key")
181192
}
182193
}

pkg/liqo-controller-manager/authentication/keys.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,37 @@ func SignNonce(priv crypto.PrivateKey, nonce []byte) ([]byte, error) {
109109
}
110110
}
111111

112-
// VerifyNonce verifies the signature of a nonce using the PKIX-encoded public key bytes of the cluster.
112+
// parsePublicKey parses a public key from various formats:
113+
// - PKIX DER-encoded bytes
114+
// - PEM-encoded public key
115+
// - Raw Ed25519 public key bytes (32 bytes)
116+
func parsePublicKey(pubKeyBytes []byte) (crypto.PublicKey, error) {
117+
// Try PEM decoding first
118+
if block, _ := pem.Decode(pubKeyBytes); block != nil {
119+
pubKeyBytes = block.Bytes
120+
}
121+
122+
// Try PKIX parsing
123+
if pub, err := x509.ParsePKIXPublicKey(pubKeyBytes); err == nil {
124+
return pub, nil
125+
}
126+
127+
// Try raw Ed25519 public key (32 bytes)
128+
if len(pubKeyBytes) == ed25519.PublicKeySize {
129+
return ed25519.PublicKey(pubKeyBytes), nil
130+
}
131+
132+
return nil, fmt.Errorf("unable to parse public key: unrecognized format (length: %d)", len(pubKeyBytes))
133+
}
134+
135+
// VerifyNonce verifies the signature of a nonce using the public key bytes of the cluster.
113136
// The public key can be Ed25519, RSA, or ECDSA.
114-
func VerifyNonce(pubKeyPKIX, nonce, signature []byte) (bool, error) {
115-
pub, err := x509.ParsePKIXPublicKey(pubKeyPKIX)
137+
// It accepts the public key in multiple formats:
138+
// - PKIX DER-encoded bytes
139+
// - PEM-encoded public key (will be decoded first)
140+
// - Raw Ed25519 public key bytes (32 bytes)
141+
func VerifyNonce(pubKeyBytes, nonce, signature []byte) (bool, error) {
142+
pub, err := parsePublicKey(pubKeyBytes)
116143
if err != nil {
117144
return false, fmt.Errorf("failed to parse public key: %w", err)
118145
}

tmp.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: apps/v1
2+
kind: DaemonSet
3+
metadata:
4+
name: nginx-daemonset
5+
namespace: test
6+
labels:
7+
app: nginx
8+
spec:
9+
selector:
10+
matchLabels:
11+
app: nginx
12+
template:
13+
metadata:
14+
labels:
15+
app: nginx
16+
spec:
17+
containers:
18+
- name: nginx
19+
image: nginx:latest
20+
ports:
21+
- containerPort: 80

0 commit comments

Comments
 (0)