This is an automated email from the ASF dual-hosted git repository.

lahirujayathilake pushed a commit to branch ssh-certs
in repository https://gitbox.apache.org/repos/asf/airavata-custos.git

commit 1d39f61375b5e560bb86cd84f83ea2330c47884f
Author: lahiruj <[email protected]>
AuthorDate: Thu Nov 12 18:05:24 2026 -0500

    Ensure the stored CA public key matches the private key
---
 .../SSH-Certificate-Signer/internal/cert/ssh_key.go     | 17 +++++++++++++++++
 .../internal/cert/ssh_key_test.go                       | 16 ++++++++++++++++
 extensions/SSH-Certificate-Signer/main.go               |  3 +++
 3 files changed, 36 insertions(+)

diff --git a/extensions/SSH-Certificate-Signer/internal/cert/ssh_key.go 
b/extensions/SSH-Certificate-Signer/internal/cert/ssh_key.go
index 0f742b566..89465be60 100644
--- a/extensions/SSH-Certificate-Signer/internal/cert/ssh_key.go
+++ b/extensions/SSH-Certificate-Signer/internal/cert/ssh_key.go
@@ -17,6 +17,7 @@
 package cert
 
 import (
+       "bytes"
        "encoding/pem"
        "fmt"
        "strings"
@@ -81,6 +82,22 @@ func ParseCAPublicKey(data []byte) (ssh.PublicKey, error) {
        return pub, nil
 }
 
+// CheckCAKeysMatch returns an error unless publicKey belongs to privateKey.
+func CheckCAKeysMatch(privateKey, publicKey []byte) error {
+       signer, err := ParseCAPrivateKey(privateKey)
+       if err != nil {
+               return err
+       }
+       pub, err := ParseCAPublicKey(publicKey)
+       if err != nil {
+               return err
+       }
+       if !bytes.Equal(signer.PublicKey().Marshal(), pub.Marshal()) {
+               return fmt.Errorf("CA public key does not match the private 
key")
+       }
+       return nil
+}
+
 func CAFingerprint(publicKey []byte) (string, error) {
        pub, err := ParseCAPublicKey(publicKey)
        if err != nil {
diff --git a/extensions/SSH-Certificate-Signer/internal/cert/ssh_key_test.go 
b/extensions/SSH-Certificate-Signer/internal/cert/ssh_key_test.go
index fe3fdce92..38b94dc45 100644
--- a/extensions/SSH-Certificate-Signer/internal/cert/ssh_key_test.go
+++ b/extensions/SSH-Certificate-Signer/internal/cert/ssh_key_test.go
@@ -164,3 +164,19 @@ func TestParseCAPublicKey_NeedsEd25519Line(t *testing.T) {
                t.Error("rsa line: accepted")
        }
 }
+
+// Make sure the stored CA public key must belong to the private key.
+func TestCheckCAKeysMatch(t *testing.T) {
+       pub, priv, _ := ed25519.GenerateKey(rand.Reader)
+       sshPub, _ := ssh.NewPublicKey(pub)
+       otherPub, _, _ := ed25519.GenerateKey(rand.Reader)
+       otherSSHPub, _ := ssh.NewPublicKey(otherPub)
+       privateKey := openSSHPrivateKey(t, priv)
+
+       if err := CheckCAKeysMatch(privateKey, 
ssh.MarshalAuthorizedKey(sshPub)); err != nil {
+               t.Errorf("matching keys: %v", err)
+       }
+       if err := CheckCAKeysMatch(privateKey, 
ssh.MarshalAuthorizedKey(otherSSHPub)); err == nil {
+               t.Error("keys of different CAs: accepted")
+       }
+}
diff --git a/extensions/SSH-Certificate-Signer/main.go 
b/extensions/SSH-Certificate-Signer/main.go
index 60405f626..d5951d69c 100644
--- a/extensions/SSH-Certificate-Signer/main.go
+++ b/extensions/SSH-Certificate-Signer/main.go
@@ -216,6 +216,9 @@ func loadCA(ctx context.Context, vaultClient *vault.Client, 
createIfMissing bool
        if err != nil {
                return "", err
        }
+       if err := cert.CheckCAKeysMatch(kp.PrivateKey, kp.PublicKey); err != 
nil {
+               return "", err
+       }
        return cert.CAFingerprint(kp.PublicKey)
 }
 

Reply via email to