Source File
knownhosts.go
Belonging Package
golang.org/x/crypto/ssh/knownhosts
package knownhosts
import (
)
type addr struct{ host, port string }
func ( *addr) () string {
:= .host
if strings.Contains(, ":") {
= "[" + + "]"
}
return + ":" + .port
}
type matcher interface {
match(addr) bool
}
type hostPattern struct {
negate bool
addr addr
}
func ( *hostPattern) () string {
:= ""
if .negate {
= "!"
}
return + .addr.String()
}
type hostPatterns []hostPattern
func ( hostPatterns) ( addr) bool {
:= false
for , := range {
if !.match() {
continue
}
if .negate {
return false
}
= true
}
return
}
func ( []byte, []byte) bool {
for {
if len() == 0 {
return len() == 0
}
if len() == 0 {
return false
}
if [0] == '*' {
if len() == 1 {
return true
}
for := range {
if ([1:], [:]) {
return true
}
}
return false
}
if [0] == '?' || [0] == [0] {
= [1:]
= [1:]
} else {
return false
}
}
}
func ( *hostPattern) ( addr) bool {
return wildcardMatch([]byte(.addr.host), []byte(.host)) && .addr.port == .port
}
type keyDBLine struct {
cert bool
matcher matcher
knownKey KnownKey
}
func ( ssh.PublicKey) string {
return .Type() + " " + base64.StdEncoding.EncodeToString(.Marshal())
}
func ( *keyDBLine) ( addr) bool {
return .matcher.match()
}
func ( *hostKeyDB) ( *ssh.Certificate) bool {
, := .revoked[string(.Marshal())]
return
}
const markerCert = "@cert-authority"
const markerRevoked = "@revoked"
func ( []byte) (string, []byte) {
:= bytes.IndexAny(, "\t ")
if == -1 {
return string(), nil
}
return string([:]), bytes.TrimSpace([:])
}
func ( []byte) (, string, ssh.PublicKey, error) {
if , := nextWord(); == markerCert || == markerRevoked {
=
=
}
, = nextWord()
if len() == 0 {
return "", "", nil, errors.New("knownhosts: missing host pattern")
}
_, = nextWord()
if len() == 0 {
return "", "", nil, errors.New("knownhosts: missing key type pattern")
}
, := nextWord()
, := base64.StdEncoding.DecodeString()
if != nil {
return "", "", nil,
}
, = ssh.ParsePublicKey()
if != nil {
return "", "", nil,
}
return , , , nil
}
func ( *hostKeyDB) ( []byte, string, int) error {
, , , := parseLine()
if != nil {
return
}
if == markerRevoked {
.revoked[string(.Marshal())] = &KnownKey{
Key: ,
Filename: ,
Line: ,
}
return nil
}
:= keyDBLine{
cert: == markerCert,
knownKey: KnownKey{
Filename: ,
Line: ,
Key: ,
},
}
if [0] == '|' {
.matcher, = newHashedHost()
} else {
.matcher, = newHostnameMatcher()
}
if != nil {
return
}
.lines = append(.lines, )
return nil
}
func ( string) (matcher, error) {
var hostPatterns
for , := range strings.Split(, ",") {
if len() == 0 {
continue
}
var addr
var bool
if [0] == '!' {
= true
= [1:]
}
if len() == 0 {
return nil, errors.New("knownhosts: negation without following hostname")
}
var error
if [0] == '[' {
.host, .port, = net.SplitHostPort()
if != nil {
return nil,
}
} else {
.host, .port, = net.SplitHostPort()
if != nil {
.host =
.port = "22"
}
}
= append(, hostPattern{
negate: ,
addr: ,
})
}
return , nil
}
type RevokedError struct {
Revoked KnownKey
}
func ( *RevokedError) () string {
return "knownhosts: key is revoked"
}
if len() == 0 {
return
}
func ( ...string) (ssh.HostKeyCallback, error) {
:= newHostKeyDB()
for , := range {
, := os.Open()
if != nil {
return nil,
}
defer .Close()
if := .Read(, ); != nil {
return nil,
}
}
var ssh.CertChecker
.IsHostAuthority = .IsHostAuthority
.IsRevoked = .IsRevoked
.HostKeyFallback = .check
return .CheckHostKey, nil
}
:= make([]byte, sha1.Size)
, := rand.Read()
if != nil {
panic(fmt.Sprintf("crypto/rand failure %v", ))
}
:= hashHost(, )
return encodeHash(sha1HashType, , )
}
func ( string) ( string, , []byte, error) {
if len() == 0 || [0] != '|' {
= errors.New("knownhosts: hashed host must start with '|'")
return
}
:= strings.Split(, "|")
if len() != 4 {
= fmt.Errorf("knownhosts: got %d components, want 3", len())
return
}
= [1]
if , = base64.StdEncoding.DecodeString([2]); != nil {
return
}
if , = base64.StdEncoding.DecodeString([3]); != nil {
return
}
return
}
func ( string, []byte, []byte) string {
return strings.Join([]string{"",
,
base64.StdEncoding.EncodeToString(),
base64.StdEncoding.EncodeToString(),
}, "|")
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |