Skip to content

Commit

Permalink
badkeys: improve compatibility of hashing func
Browse files Browse the repository at this point in the history
  • Loading branch information
hdm committed Dec 8, 2024
1 parent 26991b7 commit 54f5d30
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions badkeys/badkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,41 @@ import (
"os"
"path/filepath"

"github.com/runZeroInc/excrypto/crypto/ecdh"
"github.com/runZeroInc/excrypto/crypto/ecdsa"
"github.com/runZeroInc/excrypto/crypto/ed25519"
"github.com/runZeroInc/excrypto/crypto/rsa"
"github.com/runZeroInc/excrypto/crypto/sha256"
"github.com/runZeroInc/excrypto/crypto/x509"
"github.com/runZeroInc/excrypto/x/crypto/ssh"
)

const BadKeysMetaURL = "https://update.badkeys.info/v0/badkeysdata.json"

func PrefixFromPublicKey(pub ssh.PublicKey) ([]byte, error) {
var res []byte
switch pub.Type() {
case ssh.KeyAlgoRSA:
pk, ok := pub.(ssh.RSAPublicKey)
if !ok {
return nil, fmt.Errorf("%s doesn't implement RSAPublicKey", pub.Type())
// PrefixFromPublicKey implements the badkeys `blocklistmaker` hashing method
func PrefixFromPublicKey(pub any) ([]byte, error) {
var rawb []byte
switch pub := pub.(type) {
case ssh.PublicKey:
if cpk, ok := pub.(ssh.CryptoPublicKey); ok {
return PrefixFromPublicKey(cpk.CryptoPublicKey())
}
res = pk.ToRSAPublicKey().N.Bytes()
return nil, fmt.Errorf("unsupported ssh public key: %v", pub.Type())
case *rsa.PublicKey:
rawb = pub.N.Bytes()
case *ecdsa.PublicKey:
rawb = pub.X.Bytes()
case ed25519.PublicKey:
rawb = pub
case x509.X25519PublicKey:
rawb = pub
case *ecdh.PublicKey:
rawb = pub.Bytes()
default:
res = pub.Marshal()
return nil, fmt.Errorf("unsupported key: %T", pub)
}
hash := sha256.Sum256(res)
return hash[0:15], nil
sha256sum := sha256.Sum256(rawb)
return sha256sum[0:15], nil
}

// GetExecutablePath returns the full path to the running binary
Expand Down

0 comments on commit 54f5d30

Please sign in to comment.