Skip to content

x/crypto/ssh: ParseDSAPrivateKey lacks validation for DSA parameters #80418

Closed
golang/crypto
#365

Description

@rolandshoemaker

ParseDSAPrivateKey parses PEM/DER-encoded DSA private keys without validating the DSA parameters (P , Q , G , Y ). This allows structurally valid ASN.1 encoded keys with malformed or out-of-range parameters (such as an extremely large Q ) to be parsed successfully. Subsequent operations on these keys, such as dsa.Sign or dsa.Verify , can consume excessive CPU.

This is inconsistent with parseDSA and parseDSAKey , which validate parameters via checkDSAParams and range checks on Y.

Expected Behavior

ParseDSAPrivateKey should validate that:

• Q and P bit lengths are correct.
• G and Y are within the valid group range.

Actual Behavior

ParseDSAPrivateKey passes the parsed ASN.1 values directly to the dsa.PrivateKey structure without validation.

Minimal Example

package main

import (
    "crypto/dsa"
    "encoding/asn1"
    "encoding/pem"
    "fmt"
    "math/big"
    "time"
    "golang.org/x/crypto/ssh"
)

func main() {
    hugeQ := new(big.Int).Lsh(big.NewInt(1), 20000)
    p     := new(big.Int).Lsh(big.NewInt(1), 1023)
    g     := big.NewInt(2)
    y     := big.NewInt(5)
    x     := big.NewInt(7)

    type dsaKeyASN1 struct {
        Version         int
        P, Q, G, Pub, Priv *big.Int
    }
    der, _ := asn1.Marshal(dsaKeyASN1{0, p, hugeQ, g, y, x})
    pemBlock := pem.EncodeToMemory(&pem.Block{
        Type:  "DSA PRIVATE KEY",
        Bytes: der,
    })

    key, err := ssh.ParseRawPrivateKey(pemBlock)
    if err != nil {
        fmt.Printf("Parse error: %v\n", err)
        return
    }

    if priv, ok := key.(*dsa.PrivateKey); ok {
        start := time.Now()
        _, _ = dsa.Sign(nil, priv, make([]byte, 20))
        fmt.Printf("Sign took: %v\n", time.Since(start))
    }
}

Output:

Parse error: <nil>
Sign took: [very long]

This was originally reported by Prasanna Dabi (@prasanna8585).

cc @drakkan

Activity

  1. added this to the Unreleased milestone on Jul 15, 2026
  2. gopherbot commented on Jul 16, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/801440 mentions this issue: ssh: validate DSA parameters in ParseDSAPrivateKey

  3. prattmic commented on Jul 17, 2026

    @prattmic
    Member

    cc @golang/security

  4. added
    NeedsFix
    NeedsDecision
    and removed
    NeedsFix
    on Jul 17, 2026
  5. gopherbot commented on Jul 19, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/802860 mentions this issue: ssh/agent: validate DSA private value X in parseDSAKey and parseDSACert

  6. drakkan commented on Jul 27, 2026

    @drakkan
    Member

    We have an accepted proposal to remove DSA support from x/crypto/ssh:

    #40337

    For context on how far behind upstream we are: OpenSSH disabled ssh-dss host and user keys by default at run time in 7.0(2015-08-11), stopped compiling DSA in by default in 9.8 (2024-07-01), and removed the code entirely in 10.0 (2025-04-09).

    On Red Hat systems DSA has not been usable with OpenSSH clients since RHEL 8: DSA host keys are not accepted even when the system-wide cryptographic policy is set to LEGACY.

    We still lists ssh-dss and ssh-dss-cert-v01@openssh.com in defaultHostKeyAlgos and defaultPubKeyAuthAlgos, so DSA is enabled by default outside FIPS mode.

    I reviewed the linked CL and left a few comments. That said, I would seriously consider implementing the proposal and removing DSA support altogether instead of merging this change.

  7. rolandshoemaker commented on Jul 27, 2026

    @rolandshoemaker
    MemberAuthor

    Strongly in favor of just removing DSA if we can without hurting ecosystem compatibility too much (which it sounds like we can).

  8. drakkan commented on Jul 28, 2026

    @drakkan
    Member

    @rolandshoemaker I would prefer to remove DSA entirely, and I say that fully aware that some of my users still rely on it and that I'll have to add it back to my fork.

    If we want to be more conservative, we may consider a different approach.

    Currently:

    • ParsePrivateKey, ParseRawPrivateKey and ParseRawPrivateKeyWithPassphrase transparently accept a DSA key.
    • ssh-dss and ssh-dss-cert-v01@openssh.com are in defaultHostKeyAlgos, so a client accepts DSA host keys out of the box.
    • Server side, ssh-dss is in defaultPubKeyAuthAlgos and is advertised in the server-sig-algs extension.
    • The agent client and server accept DSA keys.

    Note that crypto/dsa itself is already marked Deprecated in the standard library.

    Signing can be pushed to the user, but verification cannot: reading an ssh-dss key off the wire and verifying a DSA signature are inherently internal, so that code stays implemented and simply becomes opt-in.

    We may start by changing the defaults:

    • Drop InsecureCertAlgoDSAv01 and InsecureKeyAlgoDSA from defaultHostKeyAlgos.
    • Drop InsecureKeyAlgoDSA from defaultPubKeyAuthAlgos.
    • Leave insecureHostKeyAlgos and insecurePubKeyAuthAlgos unchanged; NewServerConn validates PublicKeyAuthAlgorithms against SupportedAlgorithms() plus InsecureAlgorithms(), so removing them there would break the opt-in itself.

    Opt-in is then ClientConfig.HostKeyAlgorithms on the client and ServerConfig.PublicKeyAuthAlgorithms on the server.

    Loading a DSA key should be explicit:

    • In the "DSA PRIVATE KEY" case in ParseRawPrivateKey and ParseRawPrivateKeyWithPassphrase, return an explicit error instead of parsing a DSA key.
    • Keep ParseDSAPrivateKey exported, marked // Deprecated.
    • Keep the *dsa.PrivateKey case in NewSignerFromKey and the *dsa.PublicKey case in NewPublicKey: a caller needs a way to build an ssh.PublicKey of type ssh-dss for a custom signer. Document DSA support in both as deprecated.

    For the agent I would remove DSA entirely. One thing this does not cover: agent.Client.List and Signers would still enumerate ssh-dss identities held by a remote agent, because that we read the format string off the wire.

    We should still fix this issue: ParseDSAPrivateKey stays exported and becomes the only entry point for a DSA private key, so it still has to validate its input.

    Breaking changes:

    • ssh.ParsePrivateKey on a DSA PEM starts failing. The migration path is pem.Decode(pemBytes) + ParseDSAPrivateKey + NewSignerFromKey.
    • Servers stop accepting DSA client authentication unless PublicKeyAuthAlgorithms is set explicitly.
    • Clients stop accepting DSA host keys unless HostKeyAlgorithms is set explicitly.
    • ssh-add of a DSA key against a Go agent fails.
  9. rolandshoemaker commented on Jul 28, 2026

    @rolandshoemaker
    MemberAuthor

    OpenSSH is (in my understanding) generally quite conservative. If they have dropped DSA support, I would assume the user base is small enough that we can also remove it. If we think there are still some reasonable number of users we still need to support, then the conservative path seems reasonable, but if its really quite small I'd suggest just dropping it outright at this point. All that said, my handle on the SSH ecosystem is quite weak, so I'd defer to you here.

    @FiloSottile do you have opinions on this?

  10. gopherbot commented on Oct 11, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/848607 mentions this issue: ssh: remove DSA support

  11. added a commit that references this issue on Nov 6, 2026
    a1058eb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsDecision

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions