forked from gravitational/teleport
/
api.go
286 lines (256 loc) · 8.43 KB
/
api.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Teleport
* Copyright (C) 2023 Gravitational, 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 <http://www.gnu.org/licenses/>.
*/
// Package webauthnwin is wrapper around Windows webauthn API.
// It loads system webauthn.dll and uses its methods.
// It supports API versions 1+.
// API definition: https://github.com/microsoft/webauthn/blob/master/webauthn.h
// As Windows Webauthn device can be used both Windows Hello and FIDO devices.
package webauthnwin
import (
"context"
"fmt"
"io"
"os"
"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/protocol/webauthncose"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
"github.com/gravitational/teleport/api/client/proto"
wantypes "github.com/gravitational/teleport/lib/auth/webauthntypes"
)
// LoginOpts groups non-mandatory options for Login.
type LoginOpts struct {
// AuthenticatorAttachment specifies the desired authenticator attachment.
AuthenticatorAttachment AuthenticatorAttachment
}
type AuthenticatorAttachment int
const (
AttachmentAuto AuthenticatorAttachment = iota
AttachmentCrossPlatform
AttachmentPlatform
)
// nativeWebauthn represents the native windows webauthn interface.
// Implementors must provide a global variable called `native`.
type nativeWebauthn interface {
CheckSupport() CheckSupportResult
GetAssertion(origin string, in *getAssertionRequest) (*wantypes.CredentialAssertionResponse, error)
MakeCredential(origin string, in *makeCredentialRequest) (*wantypes.CredentialCreationResponse, error)
}
type getAssertionRequest struct {
rpID *uint16
clientData *webauthnClientData
jsonEncodedClientData []byte
opts *webauthnAuthenticatorGetAssertionOptions
}
type makeCredentialRequest struct {
rp *webauthnRPEntityInformation
user *webauthnUserEntityInformation
credParameters *webauthnCoseCredentialParameters
clientData *webauthnClientData
jsonEncodedClientData []byte
opts *webauthnAuthenticatorMakeCredentialOptions
}
// Login implements Login for Windows Webauthn API.
func Login(_ context.Context, origin string, assertion *wantypes.CredentialAssertion, loginOpts *LoginOpts) (*proto.MFAAuthenticateResponse, string, error) {
if origin == "" {
return nil, "", trace.BadParameter("origin required")
}
if err := assertion.Validate(); err != nil {
return nil, "", trace.Wrap(err)
}
rpid, err := utf16PtrFromString(assertion.Response.RelyingPartyID)
if err != nil {
return nil, "", trace.Wrap(err)
}
cd, jsonEncodedCD, err := clientDataToCType(assertion.Response.Challenge.String(), origin, string(protocol.AssertCeremony))
if err != nil {
return nil, "", trace.Wrap(err)
}
assertOpts, err := assertOptionsToCType(assertion.Response, loginOpts)
if err != nil {
return nil, "", trace.Wrap(err)
}
promptPlatform()
resp, err := native.GetAssertion(origin, &getAssertionRequest{
rpID: rpid,
clientData: cd,
jsonEncodedClientData: jsonEncodedCD,
opts: assertOpts,
})
if err != nil {
return nil, "", trace.Wrap(err)
}
return &proto.MFAAuthenticateResponse{
Response: &proto.MFAAuthenticateResponse_Webauthn{
Webauthn: wantypes.CredentialAssertionResponseToProto(resp),
},
}, "", nil
}
// Register implements Register for Windows Webauthn API.
func Register(_ context.Context, origin string, cc *wantypes.CredentialCreation) (*proto.MFARegisterResponse, error) {
if origin == "" {
return nil, trace.BadParameter("origin required")
}
if err := cc.Validate(); err != nil {
return nil, trace.Wrap(err)
}
rp, err := rpToCType(cc.Response.RelyingParty)
if err != nil {
return nil, trace.Wrap(err)
}
u, err := userToCType(cc.Response.User)
if err != nil {
return nil, trace.Wrap(err)
}
credParam, err := credParamToCType(cc.Response.Parameters)
if err != nil {
return nil, trace.Wrap(err)
}
cd, jsonEncodedCD, err := clientDataToCType(cc.Response.Challenge.String(), origin, string(protocol.CreateCeremony))
if err != nil {
return nil, trace.Wrap(err)
}
opts, err := makeCredOptionsToCType(cc.Response)
if err != nil {
return nil, trace.Wrap(err)
}
promptPlatform()
resp, err := native.MakeCredential(origin, &makeCredentialRequest{
rp: rp,
user: u,
credParameters: credParam,
clientData: cd,
jsonEncodedClientData: jsonEncodedCD,
opts: opts,
})
if err != nil {
return nil, trace.Wrap(err)
}
return &proto.MFARegisterResponse{
Response: &proto.MFARegisterResponse_Webauthn{
Webauthn: wantypes.CredentialCreationResponseToProto(resp),
},
}, nil
}
const defaultPromptMessage = "Using platform authenticator, follow the OS dialogs"
var (
// PromptPlatformMessage is the message shown before system prompts.
PromptPlatformMessage = defaultPromptMessage
// PromptWriter is the writer used for prompt messages.
PromptWriter io.Writer = os.Stderr
)
// ResetPromptPlatformMessage resets [PromptPlatformMessage] to its original state.
func ResetPromptPlatformMessage() {
PromptPlatformMessage = defaultPromptMessage
}
func promptPlatform() {
if PromptPlatformMessage != "" {
fmt.Fprintln(PromptWriter, PromptPlatformMessage)
}
}
// CheckSupport is the result from a Windows webauthn support check.
type CheckSupportResult struct {
HasCompileSupport bool
IsAvailable bool
HasPlatformUV bool
WebAuthnAPIVersion int
}
// IsAvailable returns true if Windows webauthn library is available in the
// system. Typically, a series of checks is performed in an attempt to avoid
// false positives.
// See CheckSupport.
func IsAvailable() bool {
supports := CheckSupport()
if supports.HasCompileSupport && !supports.IsAvailable {
log.Warn("Webauthn is not supported on this version of Windows, supported from version 1903")
}
return supports.IsAvailable
}
// CheckSupport return information whether Windows Webauthn is supported and
// information about API version.
func CheckSupport() CheckSupportResult {
return native.CheckSupport()
}
type DiagResult struct {
Available bool
RegisterSuccessful, LoginSuccessful bool
}
// Diag runs a few diagnostic commands and returns the result.
// User interaction is required.
func Diag(ctx context.Context) (*DiagResult, error) {
res := &DiagResult{}
if !IsAvailable() {
return res, nil
}
res.Available = true
// Attempt registration.
const origin = "localhost"
cc := &wantypes.CredentialCreation{
Response: wantypes.PublicKeyCredentialCreationOptions{
Challenge: make([]byte, 32),
RelyingParty: wantypes.RelyingPartyEntity{
ID: "localhost",
CredentialEntity: wantypes.CredentialEntity{
Name: "test RP",
},
},
User: wantypes.UserEntity{
CredentialEntity: wantypes.CredentialEntity{
Name: "test",
},
ID: []byte("test"),
DisplayName: "test",
},
Parameters: []wantypes.CredentialParameter{
{
Type: protocol.PublicKeyCredentialType,
Algorithm: webauthncose.AlgES256,
},
{
Type: protocol.PublicKeyCredentialType,
Algorithm: webauthncose.AlgRS256,
},
},
Attestation: protocol.PreferNoAttestation,
},
}
ccr, err := Register(ctx, origin, cc)
if err != nil {
return res, trace.Wrap(err)
}
res.RegisterSuccessful = true
// Attempt login.
assertion := &wantypes.CredentialAssertion{
Response: wantypes.PublicKeyCredentialRequestOptions{
Challenge: make([]byte, 32),
RelyingPartyID: cc.Response.RelyingParty.ID,
AllowedCredentials: []wantypes.CredentialDescriptor{
{
Type: protocol.PublicKeyCredentialType,
CredentialID: ccr.GetWebauthn().GetRawId(),
},
},
UserVerification: protocol.VerificationDiscouraged,
},
}
if _, _, err := Login(ctx, origin, assertion, &LoginOpts{}); err != nil {
return res, trace.Wrap(err)
}
res.LoginSuccessful = true
return res, nil
}