@@ -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 }
0 commit comments