forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
u2f_login.go
158 lines (144 loc) · 5.32 KB
/
u2f_login.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
// Copyright 2021 Gravitational, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package webauthncli
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/duo-labs/webauthn/protocol"
"github.com/flynn/u2f/u2ftoken"
"github.com/gravitational/trace"
"github.com/gravitational/teleport/api/client/proto"
wanlib "github.com/gravitational/teleport/lib/auth/webauthn"
)
// U2FLogin implements Login for U2F/CTAP1 devices.
// The implementation is backed exclusively by Go code, making it useful in
// scenarios where libfido2 is unavailable.
func U2FLogin(ctx context.Context, origin string, assertion *wanlib.CredentialAssertion) (*proto.MFAAuthenticateResponse, error) {
switch {
case origin == "":
return nil, trace.BadParameter("origin required")
case assertion == nil:
return nil, trace.BadParameter("assertion required")
case len(assertion.Response.AllowedCredentials) == 0 &&
assertion.Response.UserVerification == protocol.VerificationRequired:
return nil, trace.BadParameter("Passwordless not supported in U2F mode. Please install a recent version of tsh.")
case len(assertion.Response.Challenge) == 0:
return nil, trace.BadParameter("assertion challenge required")
case assertion.Response.RelyingPartyID == "":
return nil, trace.BadParameter("assertion RPID required")
case len(assertion.Response.AllowedCredentials) == 0:
return nil, trace.BadParameter("assertion has no allowed credentials")
case assertion.Response.UserVerification == protocol.VerificationRequired:
return nil, trace.BadParameter(
"assertion required user verification, but it cannot be guaranteed under CTAP1")
}
// References:
// https://fidoalliance.org/specs/fido-v2.1-ps-20210615/fido-client-to-authenticator-protocol-v2.1-ps-20210615.html#u2f-authenticatorGetAssertion-interoperability
// https://www.w3.org/TR/webauthn-2/#sctn-op-get-assertion
ccdJSON, err := json.Marshal(&CollectedClientData{
Type: string(protocol.AssertCeremony),
Challenge: base64.RawURLEncoding.EncodeToString(assertion.Response.Challenge),
Origin: origin,
})
if err != nil {
return nil, trace.Wrap(err)
}
ccdHash := sha256.Sum256(ccdJSON)
rpID := assertion.Response.RelyingPartyID
rpIDHash := sha256.Sum256([]byte(rpID))
// Did we get the App ID extension?
var appID string
var appIDHash [32]byte
if value, ok := assertion.Response.Extensions[wanlib.AppIDExtension]; ok {
appID = fmt.Sprint(value)
appIDHash = sha256.Sum256([]byte(appID))
}
// Variables below are filled by the callback on success.
var authCred protocol.CredentialDescriptor
var authResp *u2ftoken.AuthenticateResponse
var usedAppID bool
makeAuthU2F := func(cred protocol.CredentialDescriptor, req u2ftoken.AuthenticateRequest, appID bool) func(Token) error {
return func(token Token) error {
if err := token.CheckAuthenticate(req); err != nil {
return err // don't wrap, inspected by RunOnU2FDevices
}
resp, err := token.Authenticate(req)
if err != nil {
return err // don't wrap, inspected by RunOnU2FDevices
}
authCred = cred
authResp = resp
usedAppID = appID
return nil
}
}
// Assemble credential+RPID pairs to attempt.
var fns []func(Token) error
for _, cred := range assertion.Response.AllowedCredentials {
req := u2ftoken.AuthenticateRequest{
Challenge: ccdHash[:],
Application: rpIDHash[:],
KeyHandle: cred.CredentialID,
}
fns = append(fns, makeAuthU2F(cred, req, false /* appID */))
if appID != "" {
req.Application = appIDHash[:]
fns = append(fns, makeAuthU2F(cred, req, true /* appID */))
}
}
// Run!
if err := RunOnU2FDevices(ctx, fns...); err != nil {
return nil, trace.Wrap(err)
}
// Assemble extensions.
var exts *wanlib.AuthenticationExtensionsClientOutputs
if usedAppID {
exts = &wanlib.AuthenticationExtensionsClientOutputs{AppID: true}
}
// Assemble authenticator data.
// RPID (32 bytes) + User Presence (0x01, 1 byte) + Counter (4 bytes)
authData := &bytes.Buffer{}
if usedAppID {
authData.Write(appIDHash[:])
} else {
authData.Write(rpIDHash[:])
}
authData.Write(authResp.RawResponse[:5]) // User Presence (1) + Counter (4)
resp := &wanlib.CredentialAssertionResponse{
PublicKeyCredential: wanlib.PublicKeyCredential{
Credential: wanlib.Credential{
ID: base64.RawURLEncoding.EncodeToString(authCred.CredentialID),
Type: string(protocol.PublicKeyCredentialType),
},
RawID: authCred.CredentialID,
Extensions: exts,
},
AssertionResponse: wanlib.AuthenticatorAssertionResponse{
AuthenticatorResponse: wanlib.AuthenticatorResponse{
ClientDataJSON: ccdJSON,
},
AuthenticatorData: authData.Bytes(),
Signature: authResp.Signature,
},
}
return &proto.MFAAuthenticateResponse{
Response: &proto.MFAAuthenticateResponse_Webauthn{
Webauthn: wanlib.CredentialAssertionResponseToProto(resp),
},
}, nil
}