-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathssh.go
153 lines (120 loc) · 3.14 KB
/
ssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package sshauth
import (
"bytes"
"errors"
"fmt"
"strings"
"time"
"github.com/hashicorp/vault/sdk/logical"
"golang.org/x/crypto/ssh"
)
func validatePubkey(pubkey string, role *sshRole) error {
found := false
pkParsed, _, _, _, err := ssh.ParseAuthorizedKey([]byte(pubkey))
if err != nil {
return fmt.Errorf("key parsing failed: %s", err)
}
pk := strings.TrimRight(string(ssh.MarshalAuthorizedKey(pkParsed)), "\n")
for _, key := range role.PublicKeys {
if key != pk {
continue
}
found = true
break
}
if !found {
return logical.ErrPermissionDenied
}
return nil
}
// Detect the first valid principal from the certificate, if any is present
func detectPrincipal(pubkey ssh.PublicKey, validPrincipals []string) string {
cert, ok := pubkey.(*ssh.Certificate)
if !ok {
// Not a certificate - no principal to detect
return ""
}
if len(cert.ValidPrincipals) == 0 {
// Certificate is not valid for any principal
return ""
}
// Return the first valid principal found in the certificate
for _, p := range validPrincipals {
for _, vp := range cert.ValidPrincipals {
if p == vp || vp == "*" {
return p
}
}
}
// No valid principal was found in the certificate
return ""
}
func validatePrincipal(principal string, validPrincipals []string) error {
if principal == "*" || principal == "" {
return errors.New("invalid principal")
}
for _, vp := range validPrincipals {
if vp == principal || vp == "*" {
return nil
}
}
return errors.New("no matching principal found")
}
func validateCert(pubkey ssh.PublicKey, principal string, config *ConfigEntry) error {
cert, ok := pubkey.(*ssh.Certificate)
if !ok {
return errors.New("not a certificate")
}
if len(cert.ValidPrincipals) == 0 {
return errors.New("refusing certificate without principal list")
}
c := &ssh.CertChecker{
IsUserAuthority: func(auth ssh.PublicKey) bool {
for _, caKey := range config.SSHCAPublicKeys {
pubKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(caKey))
if err != nil {
return false
}
if bytes.Equal(auth.Marshal(), pubKey.Marshal()) {
return true
}
}
return false
},
}
// check the CA of the cert
if !c.IsUserAuthority(cert.SignatureKey) {
return errors.New("CA doesn't match")
}
// check cert validity
if err := c.CheckCert(principal, cert); err != nil {
return fmt.Errorf("certificate validation failed: %s", err)
}
return nil
}
func parsePubkey(pubkey string) (ssh.PublicKey, error) {
certParsed, _, _, _, err := ssh.ParseAuthorizedKey([]byte(pubkey))
if err != nil {
return nil, fmt.Errorf("parsing failed %s", err)
}
parsedPubkey, err := ssh.ParsePublicKey(certParsed.Marshal())
if err != nil {
return nil, fmt.Errorf("pubkey parsing failed %s", err)
}
return parsedPubkey, nil
}
func verifySignature(pubkey ssh.PublicKey, nonce, signature []byte) error {
if cert, ok := pubkey.(*ssh.Certificate); ok {
pubkey = cert.Key
}
sig := &ssh.Signature{
Format: pubkey.Type(),
Blob: signature,
}
return pubkey.Verify(nonce, sig)
}
func validNonceTime(nonce []byte) bool {
t := time.Time{}
t.UnmarshalBinary(nonce)
return time.Since(t) <= time.Second*30
}