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 5508a04da419723f1ba267a31532092d3b473b9b Author: lahiruj <[email protected]> AuthorDate: Thu Nov 12 17:29:08 2026 -0500 Use only ed25519 CA keys in OpenSSH format --- extensions/SSH-Certificate-Signer/README.md | 2 +- .../internal/cert/rsa_ecdsa_signing_test.go | 172 --------------------- .../SSH-Certificate-Signer/internal/cert/signer.go | 2 +- .../internal/cert/signer_test.go | 41 +++-- .../internal/cert/ssh_key.go | 115 +++----------- .../internal/cert/ssh_key_test.go | 152 ++++-------------- .../internal/handler/ca_public_key.go | 16 +- .../internal/vault/client.go | 17 +- 8 files changed, 100 insertions(+), 417 deletions(-) diff --git a/extensions/SSH-Certificate-Signer/README.md b/extensions/SSH-Certificate-Signer/README.md index 275fbcc85..bd0dc5735 100644 --- a/extensions/SSH-Certificate-Signer/README.md +++ b/extensions/SSH-Certificate-Signer/README.md @@ -58,7 +58,7 @@ The certificate names who it is for. The CA signature covers all of it. A request can also set `source_address`, a comma-separated list of IP addresses or CIDR blocks. It goes in the certificate's `source-address` critical option, and sshd then accepts the certificate only from those addresses. -A deployment has one Ed25519 CA, stored in Vault. The signer reads it at startup and refuses to start without it, +A deployment has one Ed25519 CA, stored in Vault in OpenSSH format, the same format `ssh-keygen -t ed25519` writes. The signer reads it at startup and refuses to start without it, unless `signer.ca.create_if_missing` is set, in which case it creates one and writes a `ca_created` audit log entry. ## Prerequisites diff --git a/extensions/SSH-Certificate-Signer/internal/cert/rsa_ecdsa_signing_test.go b/extensions/SSH-Certificate-Signer/internal/cert/rsa_ecdsa_signing_test.go deleted file mode 100644 index 1c1a6f258..000000000 --- a/extensions/SSH-Certificate-Signer/internal/cert/rsa_ecdsa_signing_test.go +++ /dev/null @@ -1,172 +0,0 @@ -// Licensed to the Apache Software Foundation (ASF) under one or more -// contributor license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright ownership. -// The ASF licenses this file to You under the Apache License, Version 2.0 -// (the "License"); you may not use this file except in compliance with -// the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package cert - -import ( - "crypto/ecdsa" - "crypto/ed25519" - "crypto/elliptic" - "crypto/rand" - "crypto/rsa" - "crypto/x509" - "encoding/pem" - "testing" - - "golang.org/x/crypto/ssh" -) - -func generateRSACA(t *testing.T) []byte { - t.Helper() - key, err := rsa.GenerateKey(rand.Reader, 2048) - if err != nil { - t.Fatal(err) - } - privBytes, err := x509.MarshalPKCS8PrivateKey(key) - if err != nil { - t.Fatal(err) - } - return pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}) -} - -func generateECDSACA(t *testing.T) []byte { - t.Helper() - key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - if err != nil { - t.Fatal(err) - } - privBytes, err := x509.MarshalPKCS8PrivateKey(key) - if err != nil { - t.Fatal(err) - } - return pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}) -} - -func generateRSAUserKey(t *testing.T) ssh.PublicKey { - t.Helper() - key, _ := rsa.GenerateKey(rand.Reader, 2048) - sshPub, _ := ssh.NewPublicKey(&key.PublicKey) - return sshPub -} - -func generateECDSAUserKey(t *testing.T) ssh.PublicKey { - t.Helper() - key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - sshPub, _ := ssh.NewPublicKey(&key.PublicKey) - return sshPub -} - -func TestSignCertificate_RSAKeyWithRSACA(t *testing.T) { - caPEM := generateRSACA(t) - userKey := generateRSAUserKey(t) - - result, err := SignCertificate(&SignRequest{ - PublicKey: userKey, - CAPrivateKeyPEM: caPEM, - Serial: 10, - Principal: "rsauser", - UserEmail: "[email protected]", - TTLSeconds: 3600, - }) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - - parsedKey, err := ssh.ParsePublicKey(result.CertBytes) - if err != nil { - t.Fatalf("failed to parse cert: %v", err) - } - cert := parsedKey.(*ssh.Certificate) - if cert.Serial != 10 { - t.Errorf("expected serial 10, got %d", cert.Serial) - } -} - -func TestSignCertificate_ECDSAKeyWithECDSACA(t *testing.T) { - caPEM := generateECDSACA(t) - userKey := generateECDSAUserKey(t) - - result, err := SignCertificate(&SignRequest{ - PublicKey: userKey, - CAPrivateKeyPEM: caPEM, - Serial: 20, - Principal: "ecuser", - UserEmail: "[email protected]", - TTLSeconds: 3600, - }) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - - parsedKey, err := ssh.ParsePublicKey(result.CertBytes) - if err != nil { - t.Fatalf("failed to parse cert: %v", err) - } - cert := parsedKey.(*ssh.Certificate) - if cert.Serial != 20 { - t.Errorf("expected serial 20, got %d", cert.Serial) - } -} - -// Sign an Ed25519 user key with an RSA CA -func TestSignCertificate_Ed25519KeyWithRSACA(t *testing.T) { - caPEM := generateRSACA(t) - pub, _, _ := ed25519.GenerateKey(rand.Reader) - sshPub, _ := ssh.NewPublicKey(pub) - - result, err := SignCertificate(&SignRequest{ - PublicKey: sshPub, - CAPrivateKeyPEM: caPEM, - Serial: 30, - Principal: "crossuser", - UserEmail: "[email protected]", - TTLSeconds: 3600, - }) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - - parsedKey, err := ssh.ParsePublicKey(result.CertBytes) - if err != nil { - t.Fatalf("failed to parse cert: %v", err) - } - cert := parsedKey.(*ssh.Certificate) - if cert.Serial != 30 { - t.Errorf("expected serial 30, got %d", cert.Serial) - } -} - -// Sign an Ed25519 user key with ECDSA CA -func TestSignCertificate_Ed25519KeyWithECDSACA(t *testing.T) { - caPEM := generateECDSACA(t) - pub, _, _ := ed25519.GenerateKey(rand.Reader) - sshPub, _ := ssh.NewPublicKey(pub) - - result, err := SignCertificate(&SignRequest{ - PublicKey: sshPub, - CAPrivateKeyPEM: caPEM, - Serial: 40, - Principal: "ecdsacauser", - UserEmail: "[email protected]", - TTLSeconds: 3600, - }) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - - if result.Serial != 40 { - t.Errorf("expected serial 40, got %d", result.Serial) - } -} diff --git a/extensions/SSH-Certificate-Signer/internal/cert/signer.go b/extensions/SSH-Certificate-Signer/internal/cert/signer.go index 45ac3b838..2576aa54a 100644 --- a/extensions/SSH-Certificate-Signer/internal/cert/signer.go +++ b/extensions/SSH-Certificate-Signer/internal/cert/signer.go @@ -56,7 +56,7 @@ type SignResult struct { } func SignCertificate(req *SignRequest) (*SignResult, error) { - _, sshSigner, err := ParseCAPrivateKey(req.CAPrivateKeyPEM) + sshSigner, err := ParseCAPrivateKey(req.CAPrivateKeyPEM) if err != nil { return nil, fmt.Errorf("parsing CA key: %w", err) } diff --git a/extensions/SSH-Certificate-Signer/internal/cert/signer_test.go b/extensions/SSH-Certificate-Signer/internal/cert/signer_test.go index 92df5b87f..653fbf679 100644 --- a/extensions/SSH-Certificate-Signer/internal/cert/signer_test.go +++ b/extensions/SSH-Certificate-Signer/internal/cert/signer_test.go @@ -16,10 +16,11 @@ package cert import ( + "crypto/ecdsa" "crypto/ed25519" + "crypto/elliptic" "crypto/rand" - "crypto/x509" - "encoding/pem" + "crypto/rsa" "testing" "time" @@ -32,17 +33,11 @@ func generateTestEd25519CA(t *testing.T) ([]byte, ssh.PublicKey) { if err != nil { t.Fatal(err) } - privBytes, err := x509.MarshalPKCS8PrivateKey(priv) - if err != nil { - t.Fatal(err) - } - privPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}) - sshPub, err := ssh.NewPublicKey(pub) if err != nil { t.Fatal(err) } - return privPEM, sshPub + return openSSHPrivateKey(t, priv), sshPub } func generateTestEd25519UserKey(t *testing.T) ssh.PublicKey { @@ -193,3 +188,31 @@ func TestSignCertificate_WithCriticalOptions(t *testing.T) { t.Errorf("expected critical option source-address=10.0.0.0/8, got %v", cert.Permissions.CriticalOptions) } } + +// Make sure the ed25519 CA still signs RSA and ECDSA user keys, since users may bring either. +func TestSignCertificate_SignsRSAAndECDSAUserKeys(t *testing.T) { + caPrivPEM, _ := generateTestEd25519CA(t) + rsaKey, _ := rsa.GenerateKey(rand.Reader, 2048) + p256, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + p384, _ := ecdsa.GenerateKey(elliptic.P384(), rand.Reader) + p521, _ := ecdsa.GenerateKey(elliptic.P521(), rand.Reader) + + for name, key := range map[string]any{ + "rsa": &rsaKey.PublicKey, + "ecdsa-p256": &p256.PublicKey, + "ecdsa-p384": &p384.PublicKey, + "ecdsa-p521": &p521.PublicKey, + } { + userKey, _ := ssh.NewPublicKey(key) + if _, err := SignCertificate(&SignRequest{ + PublicKey: userKey, + CAPrivateKeyPEM: caPrivPEM, + Serial: 10, + Principal: "jdoe", + UserEmail: "[email protected]", + TTLSeconds: 3600, + }); err != nil { + t.Errorf("%s user key: %v", name, err) + } + } +} diff --git a/extensions/SSH-Certificate-Signer/internal/cert/ssh_key.go b/extensions/SSH-Certificate-Signer/internal/cert/ssh_key.go index 7fe163f0a..0f742b566 100644 --- a/extensions/SSH-Certificate-Signer/internal/cert/ssh_key.go +++ b/extensions/SSH-Certificate-Signer/internal/cert/ssh_key.go @@ -17,12 +17,8 @@ package cert import ( - "crypto" - "crypto/rsa" - "crypto/x509" "encoding/pem" "fmt" - "io" "strings" "golang.org/x/crypto/ssh" @@ -58,108 +54,37 @@ func SSHPublicKeyFingerprint(key ssh.PublicKey) string { return ssh.FingerprintSHA256(key) } -func ParseCAPrivateKey(pemData []byte) (crypto.Signer, ssh.Signer, error) { - block, _ := pem.Decode(pemData) - if block == nil { - return nil, nil, fmt.Errorf("failed to decode PEM block") +// ParseCAPrivateKey reads the CA private key. Only ed25519 keys in OpenSSH format are accepted. +func ParseCAPrivateKey(data []byte) (ssh.Signer, error) { + if block, _ := pem.Decode(data); block == nil || block.Type != "OPENSSH PRIVATE KEY" { + return nil, fmt.Errorf("CA private key must be in OpenSSH format") } - - var signer crypto.Signer - switch block.Type { - case "PRIVATE KEY": - key, err := x509.ParsePKCS8PrivateKey(block.Bytes) - if err != nil { - return nil, nil, fmt.Errorf("parsing PKCS8 private key: %w", err) - } - var ok bool - signer, ok = key.(crypto.Signer) - if !ok { - return nil, nil, fmt.Errorf("private key does not implement crypto.Signer") - } - case "EC PRIVATE KEY": - key, err := x509.ParseECPrivateKey(block.Bytes) - if err != nil { - return nil, nil, fmt.Errorf("parsing EC private key: %w", err) - } - signer = key - case "RSA PRIVATE KEY": - key, err := x509.ParsePKCS1PrivateKey(block.Bytes) - if err != nil { - return nil, nil, fmt.Errorf("parsing RSA private key: %w", err) - } - signer = key - default: - return nil, nil, fmt.Errorf("unsupported PEM block type: %s", block.Type) - } - - var sshSigner ssh.Signer - var err error - - // For RSA keys, use SHA-256 signer - if _, ok := signer.(*rsa.PrivateKey); ok { - sshSigner, err = ssh.NewSignerFromKey(signer) - if err != nil { - return nil, nil, fmt.Errorf("creating SSH signer: %w", err) - } - // Wrap to use rsa-sha2-256 algorithm - algorithmSigner, ok := sshSigner.(ssh.AlgorithmSigner) - if ok { - sshSigner = &rsaSHA256Signer{inner: algorithmSigner} - } - } else { - sshSigner, err = ssh.NewSignerFromKey(signer) - if err != nil { - return nil, nil, fmt.Errorf("creating SSH signer: %w", err) - } - } - - return signer, sshSigner, nil -} - -// rsaSHA256Signer wraps an SSH AlgorithmSigner to always use rsa-sha2-256. -type rsaSHA256Signer struct { - inner ssh.AlgorithmSigner -} - -func (s *rsaSHA256Signer) PublicKey() ssh.PublicKey { - return s.inner.PublicKey() -} - -func (s *rsaSHA256Signer) Sign(rand io.Reader, data []byte) (*ssh.Signature, error) { - return s.inner.SignWithAlgorithm(rand, data, ssh.KeyAlgoRSASHA256) -} - -func ParseCAPublicKey(pemData []byte) (ssh.PublicKey, error) { - block, _ := pem.Decode(pemData) - if block == nil { - return nil, fmt.Errorf("failed to decode PEM block") - } - - pubKey, err := x509.ParsePKIXPublicKey(block.Bytes) + signer, err := ssh.ParsePrivateKey(data) if err != nil { - return nil, fmt.Errorf("parsing public key: %w", err) + return nil, fmt.Errorf("parsing CA private key: %w", err) } - - sshPub, err := ssh.NewPublicKey(pubKey) - if err != nil { - return nil, fmt.Errorf("converting to SSH public key: %w", err) + if signer.PublicKey().Type() != ssh.KeyAlgoED25519 { + return nil, fmt.Errorf("CA key must be ed25519, got %s", signer.PublicKey().Type()) } - return sshPub, nil + return signer, nil } -// PEMToOpenSSH converts a PEM public key to OpenSSH format, suitable for trusted-user-ca-keys files. -func PEMToOpenSSH(publicKeyPEM []byte) (string, error) { - sshPub, err := ParseCAPublicKey(publicKeyPEM) +// ParseCAPublicKey reads the CA public key in OpenSSH format. +func ParseCAPublicKey(data []byte) (ssh.PublicKey, error) { + pub, err := ParseSSHPublicKey(string(data)) if err != nil { - return "", err + return nil, fmt.Errorf("parsing CA public key: %w", err) + } + if pub.Type() != ssh.KeyAlgoED25519 { + return nil, fmt.Errorf("CA key must be ed25519, got %s", pub.Type()) } - return strings.TrimSpace(string(ssh.MarshalAuthorizedKey(sshPub))), nil + return pub, nil } -func CAFingerprint(publicKeyPEM []byte) (string, error) { - sshPub, err := ParseCAPublicKey(publicKeyPEM) +func CAFingerprint(publicKey []byte) (string, error) { + pub, err := ParseCAPublicKey(publicKey) if err != nil { return "", err } - return ssh.FingerprintSHA256(sshPub), nil + return ssh.FingerprintSHA256(pub), 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 e42ccf4c6..fe3fdce92 100644 --- a/extensions/SSH-Certificate-Signer/internal/cert/ssh_key_test.go +++ b/extensions/SSH-Certificate-Signer/internal/cert/ssh_key_test.go @@ -23,7 +23,6 @@ import ( "crypto/rsa" "crypto/x509" "encoding/pem" - "strings" "testing" "golang.org/x/crypto/ssh" @@ -116,141 +115,52 @@ func TestSSHPublicKeyFingerprint(t *testing.T) { } } -func TestParseCAPrivateKey_Ed25519(t *testing.T) { - _, priv, _ := ed25519.GenerateKey(rand.Reader) - privBytes, _ := x509.MarshalPKCS8PrivateKey(priv) - privPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}) - - signer, sshSigner, err := ParseCAPrivateKey(privPEM) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if signer == nil || sshSigner == nil { - t.Fatal("expected non-nil signers") - } -} - -func TestParseCAPrivateKey_RSA(t *testing.T) { - rsaKey, _ := rsa.GenerateKey(rand.Reader, 2048) - privBytes, _ := x509.MarshalPKCS8PrivateKey(rsaKey) - privPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}) - - _, sshSigner, err := ParseCAPrivateKey(privPEM) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if sshSigner == nil { - t.Fatal("expected non-nil SSH signer") - } -} - -func TestParseCAPrivateKey_ECDSA(t *testing.T) { - ecKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - privBytes, _ := x509.MarshalPKCS8PrivateKey(ecKey) - privPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}) - - _, sshSigner, err := ParseCAPrivateKey(privPEM) +// openSSHPrivateKey encodes key the way ssh-keygen writes it. +func openSSHPrivateKey(t *testing.T, key any) []byte { + t.Helper() + block, err := ssh.MarshalPrivateKey(key, "") if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if sshSigner == nil { - t.Fatal("expected non-nil SSH signer") + t.Fatal(err) } + return pem.EncodeToMemory(block) } -func TestParseCAPrivateKey_InvalidPEM(t *testing.T) { - _, _, err := ParseCAPrivateKey([]byte("not a PEM")) - if err == nil { - t.Error("expected error for invalid PEM") - } -} - -func TestCAFingerprint(t *testing.T) { - pub, _, _ := ed25519.GenerateKey(rand.Reader) - pubBytes, _ := x509.MarshalPKIXPublicKey(pub) - pubPEM := pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: pubBytes}) - - fp, err := CAFingerprint(pubPEM) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if fp == "" || fp[:7] != "SHA256:" { - t.Errorf("expected SHA256: fingerprint, got %s", fp) - } -} - -func TestPEMToOpenSSH_Ed25519(t *testing.T) { - pub, _, _ := ed25519.GenerateKey(rand.Reader) - pubBytes, _ := x509.MarshalPKIXPublicKey(pub) - pubPEM := pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: pubBytes}) - - openssh, err := PEMToOpenSSH(pubPEM) - if err != nil { +// Make sure an ed25519 CA key in OpenSSH format, as ssh-keygen writes it, is accepted. +func TestParseCAPrivateKey_AcceptsOpenSSHEd25519(t *testing.T) { + _, priv, _ := ed25519.GenerateKey(rand.Reader) + if _, err := ParseCAPrivateKey(openSSHPrivateKey(t, priv)); err != nil { t.Fatalf("unexpected error: %v", err) } - - if !strings.HasPrefix(openssh, "ssh-ed25519 ") { - t.Errorf("expected ssh-ed25519 prefix, got %q", openssh) - } - - // Should be parseable back - parsed, err := ParseSSHPublicKey(openssh) - if err != nil { - t.Fatalf("failed to re-parse OpenSSH key: %v", err) - } - if parsed.Type() != "ssh-ed25519" { - t.Errorf("expected ssh-ed25519, got %s", parsed.Type()) - } } -func TestPEMToOpenSSH_RSA(t *testing.T) { +// Make sure a CA key in PEM format or of a type other than ed25519 is refused. +func TestParseCAPrivateKey_RefusesOtherFormatsAndTypes(t *testing.T) { + _, ed25519Key, _ := ed25519.GenerateKey(rand.Reader) + pkcs8, _ := x509.MarshalPKCS8PrivateKey(ed25519Key) rsaKey, _ := rsa.GenerateKey(rand.Reader, 2048) - pubBytes, _ := x509.MarshalPKIXPublicKey(&rsaKey.PublicKey) - pubPEM := pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: pubBytes}) - - openssh, err := PEMToOpenSSH(pubPEM) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - - if !strings.HasPrefix(openssh, "ssh-rsa ") { - t.Errorf("expected ssh-rsa prefix, got %q", openssh) - } -} -func TestPEMToOpenSSH_ECDSA(t *testing.T) { - ecKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - pubBytes, _ := x509.MarshalPKIXPublicKey(&ecKey.PublicKey) - pubPEM := pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: pubBytes}) - - openssh, err := PEMToOpenSSH(pubPEM) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - - if !strings.HasPrefix(openssh, "ecdsa-sha2-nistp256 ") { - t.Errorf("expected ecdsa-sha2-nistp256 prefix, got %q", openssh) - } -} - -func TestPEMToOpenSSH_InvalidPEM(t *testing.T) { - _, err := PEMToOpenSSH([]byte("not valid PEM")) - if err == nil { - t.Error("expected error for invalid PEM") + for name, key := range map[string][]byte{ + "ed25519 in PEM": pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: pkcs8}), + "rsa in OpenSSH": openSSHPrivateKey(t, rsaKey), + "not a key at all": []byte("not a key"), + } { + if _, err := ParseCAPrivateKey(key); err == nil { + t.Errorf("%s: accepted", name) + } } } -func TestPEMToOpenSSH_NoTrailingNewline(t *testing.T) { +// Make sure the CA public key is read as an ed25519 OpenSSH line and a key of another type is refused. +func TestParseCAPublicKey_NeedsEd25519Line(t *testing.T) { pub, _, _ := ed25519.GenerateKey(rand.Reader) - pubBytes, _ := x509.MarshalPKIXPublicKey(pub) - pubPEM := pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: pubBytes}) - - openssh, err := PEMToOpenSSH(pubPEM) - if err != nil { - t.Fatalf("unexpected error: %v", err) + sshPub, _ := ssh.NewPublicKey(pub) + if _, err := ParseCAPublicKey(ssh.MarshalAuthorizedKey(sshPub)); err != nil { + t.Errorf("ed25519 line: %v", err) } - if strings.HasSuffix(openssh, "\n") { - t.Error("PEMToOpenSSH should not include trailing newline") + rsaKey, _ := rsa.GenerateKey(rand.Reader, 2048) + rsaPub, _ := ssh.NewPublicKey(&rsaKey.PublicKey) + if _, err := ParseCAPublicKey(ssh.MarshalAuthorizedKey(rsaPub)); err == nil { + t.Error("rsa line: accepted") } } diff --git a/extensions/SSH-Certificate-Signer/internal/handler/ca_public_key.go b/extensions/SSH-Certificate-Signer/internal/handler/ca_public_key.go index e4f24e65f..bde472ded 100644 --- a/extensions/SSH-Certificate-Signer/internal/handler/ca_public_key.go +++ b/extensions/SSH-Certificate-Signer/internal/handler/ca_public_key.go @@ -21,6 +21,8 @@ import ( "net/http" "strings" + "golang.org/x/crypto/ssh" + "github.com/apache/airavata-custos/signer/internal/cert" vaultpkg "github.com/apache/airavata-custos/signer/internal/vault" ) @@ -51,23 +53,21 @@ func (h *CAPublicKeyHandler) Handle(w http.ResponseWriter, r *http.Request) { return } - opensshKey, err := cert.PEMToOpenSSH(currentKey.PublicKey) + caPublicKey, err := cert.ParseCAPublicKey(currentKey.PublicKey) if err != nil { - h.logger.Error("failed to convert CA key to OpenSSH format", "error", err) - writeError(w, http.StatusInternalServerError, "internal_error", "Failed to convert CA key") + h.logger.Error("failed to parse CA public key", "error", err) + writeError(w, http.StatusInternalServerError, "internal_error", "Failed to read CA key") return } + opensshKey := strings.TrimSpace(string(ssh.MarshalAuthorizedKey(caPublicKey))) // If the client accepts JSON, return structured response accept := r.Header.Get("Accept") if strings.Contains(accept, "application/json") { - fingerprint, _ := cert.CAFingerprint(currentKey.PublicKey) - algorithm := cert.NormalizeKeyType(strings.SplitN(opensshKey, " ", 2)[0]) - resp := CAPublicKeyJSONResponse{ PublicKey: opensshKey, - Fingerprint: fingerprint, - Algorithm: algorithm, + Fingerprint: ssh.FingerprintSHA256(caPublicKey), + Algorithm: cert.NormalizeKeyType(caPublicKey.Type()), } w.Header().Set("Content-Type", "application/json") diff --git a/extensions/SSH-Certificate-Signer/internal/vault/client.go b/extensions/SSH-Certificate-Signer/internal/vault/client.go index b51c1d0d6..249d9f944 100644 --- a/extensions/SSH-Certificate-Signer/internal/vault/client.go +++ b/extensions/SSH-Certificate-Signer/internal/vault/client.go @@ -20,7 +20,6 @@ import ( "context" "crypto/ed25519" "crypto/rand" - "crypto/x509" "encoding/json" "encoding/pem" "errors" @@ -30,6 +29,7 @@ import ( "time" vaultapi "github.com/openbao/openbao/api/v2" + "golang.org/x/crypto/ssh" "github.com/apache/airavata-custos/signer/internal/config" ) @@ -53,8 +53,8 @@ func isCASMismatch(err error) bool { } type CAKeyPair struct { - PrivateKey []byte // PEM-encoded private key - PublicKey []byte // PEM-encoded public key + PrivateKey []byte // OpenSSH private key + PublicKey []byte // OpenSSH public key line, as in authorized_keys Algorithm string // e.g., "ed25519" CreatedAt string } @@ -329,21 +329,18 @@ func generateEd25519CAKey() (*CAKeyPair, error) { return nil, fmt.Errorf("generating ed25519 key: %w", err) } - privBytes, err := x509.MarshalPKCS8PrivateKey(priv) + privBlock, err := ssh.MarshalPrivateKey(priv, "") if err != nil { return nil, fmt.Errorf("marshaling private key: %w", err) } - privPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}) - - pubBytes, err := x509.MarshalPKIXPublicKey(pub) + sshPub, err := ssh.NewPublicKey(pub) if err != nil { return nil, fmt.Errorf("marshaling public key: %w", err) } - pubPEM := pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: pubBytes}) return &CAKeyPair{ - PrivateKey: privPEM, - PublicKey: pubPEM, + PrivateKey: pem.EncodeToMemory(privBlock), + PublicKey: ssh.MarshalAuthorizedKey(sshPub), Algorithm: "ed25519", CreatedAt: time.Now().UTC().Format(time.RFC3339), }, nil
