-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh_cert_exchange.go
176 lines (154 loc) · 4.87 KB
/
ssh_cert_exchange.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (C) 2023 Shanhu Tech Inc.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
// for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package authgate
import (
"crypto/rsa"
"encoding/json"
"time"
"golang.org/x/crypto/ssh"
"shanhu.io/g/aries"
"shanhu.io/g/errcode"
"shanhu.io/g/rsautil"
"shanhu.io/g/signer"
"shanhu.io/g/signin"
"shanhu.io/g/signin/signinapi"
"shanhu.io/g/timeutil"
)
// SSHCertExchangeConfig is the configuration to create an SSH certificate
// signin stub.
type SSHCertExchangeConfig struct {
CAPublicKey []byte `json:",omitempty"`
CAPublicKeyFunc func() ([]byte, error) `json:",omitempty"`
CAPublicKeyFile string `json:",omitempty"`
ChallengeKey []byte
// Time function for checking certificate. It is not used for
// token generation.
Now func() time.Time
}
// SSHCertExchange is a service stub that provides session tokens if the
// user signs a challenge and the SSH certificate of it.
type SSHCertExchange struct {
tokener signin.Tokener
caPublicKey *rsa.PublicKey
ch *Challenger
now func() time.Time
}
func caPublicKeyFromConfig(conf *SSHCertExchangeConfig) (
*rsa.PublicKey, error,
) {
if conf.CAPublicKeyFile != "" {
return rsautil.ReadPublicKey(conf.CAPublicKeyFile)
}
var keyBytes = conf.CAPublicKey
if keyBytes == nil && conf.CAPublicKeyFunc != nil {
bs, err := conf.CAPublicKeyFunc()
if err != nil {
return nil, errcode.Annotate(err, "fetch CA public key")
}
keyBytes = bs
}
k, err := rsautil.ParsePublicKey(keyBytes)
if err != nil {
return nil, errcode.Annotate(err, "parse CA public key")
}
return k, nil
}
// NewSSHCertExchange creates a new SSH certificate exchange that exchanges
// signed challenges for session tokens.
func NewSSHCertExchange(tok signin.Tokener, conf *SSHCertExchangeConfig) (
*SSHCertExchange, error,
) {
caPubKey, err := caPublicKeyFromConfig(conf)
if err != nil {
return nil, errcode.Annotate(err, "read CA public key")
}
signer := signer.New(conf.ChallengeKey)
ch := NewChallenger(&ChallengerConfig{
Signer: signer,
Now: conf.Now,
})
return &SSHCertExchange{
tokener: tok,
caPublicKey: caPubKey,
ch: ch,
now: timeutil.NowFunc(conf.Now),
}, nil
}
func (s *SSHCertExchange) apiSignIn(
c *aries.C, req *signinapi.SSHSignInRequest,
) (*signinapi.Creds, error) {
record := new(signinapi.SSHSignInRecord)
if err := json.Unmarshal(req.RecordBytes, record); err != nil {
return nil, errcode.Annotate(err, "parse signin record")
}
user := record.User
if user == "" {
return nil, errcode.InvalidArgf("user name is empty")
}
if _, err := s.ch.Check(record.Challenge); err != nil {
return nil, errcode.Annotate(err, "challenge check failed")
}
// Parse Certificate.
certKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(req.Certificate))
if err != nil {
return nil, errcode.Annotate(err, "parse certificate")
}
cert, ok := certKey.(*ssh.Certificate)
if !ok {
return nil, errcode.InvalidArgf("invalid certificate")
}
// Check if it is a user certificate.
if cert.CertType != ssh.UserCert {
return nil, errcode.InvalidArgf("not a user certificate")
}
// Check CA key.
cryptoPubKey, ok := cert.SignatureKey.(ssh.CryptoPublicKey)
if !ok {
return nil, errcode.InvalidArgf("not a crypto public key")
}
if !s.caPublicKey.Equal(cryptoPubKey.CryptoPublicKey()) {
return nil, errcode.Unauthorizedf("unrecognized CA")
}
// Check the time and the certificate.
checker := &ssh.CertChecker{Clock: s.now}
if err := checker.CheckCert(user, cert); err != nil {
return nil, errcode.Annotate(err, "check certificate failed")
}
// Check the signature.
sig := &ssh.Signature{
Format: req.Sig.Format,
Blob: req.Sig.Blob,
Rest: req.Sig.Rest,
}
if err := cert.Verify(req.RecordBytes, sig); err != nil {
return nil, errcode.Annotate(err, "check signature")
}
// Get a token.
ttl := timeutil.TimeDuration(record.TTL)
token := s.tokener.Token(user, ttl)
return signin.TokenCreds(user, token), nil
}
// API returns the API router stub for signing in with SSH certificate
// credentials.
func (s *SSHCertExchange) API() *aries.Router {
r := aries.NewRouter()
r.Call("challenge", s.ch.Serve)
r.Call("signin", s.apiSignIn)
return r
}
// AddAPI adds the API to under /ssh .
func (s *SSHCertExchange) AddAPI(r *aries.Router) {
r.DirService("ssh", s.API())
}