From 32134e3a434bb72e8e781c5e371cd8226498ef98 Mon Sep 17 00:00:00 2001 From: Gusted Date: Fri, 5 Apr 2024 14:00:36 +0200 Subject: [PATCH] [CHORE] Remove u2f dependency - It was only used to parse old U2F data to webauthn credentials. We only used the public key and keyhandle. This functiontionality was reworked to `parseU2FRegistration`. - Tests are already present, `Test_RemigrateU2FCredentials`. --- assets/go-licenses.json | 5 ---- go.mod | 1 - go.sum | 2 -- models/migrations/v1_16/v210.go | 37 ++++++++++++++++++++++------ models/migrations/v1_16/v210_test.go | 14 +++++++++++ 5 files changed, 43 insertions(+), 16 deletions(-) diff --git a/assets/go-licenses.json b/assets/go-licenses.json index d1062d747b..ef4c7b3c00 100644 --- a/assets/go-licenses.json +++ b/assets/go-licenses.json @@ -954,11 +954,6 @@ "path": "github.com/syndtr/goleveldb/leveldb/LICENSE", "licenseText": "Copyright 2012 Suryandaru Triandana \u003csyndtr@gmail.com\u003e\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are\nmet:\n\n * Redistributions of source code must retain the above copyright\nnotice, this list of conditions and the following disclaimer.\n * Redistributions in binary form must reproduce the above copyright\nnotice, this list of conditions and the following disclaimer in the\ndocumentation and/or other materials provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\nLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\nA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\nHOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\nSPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\nLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\nDATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\nTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n" }, - { - "name": "github.com/tstranex/u2f", - "path": "github.com/tstranex/u2f/LICENSE", - "licenseText": "The MIT License (MIT)\n\nCopyright (c) 2015 The Go FIDO U2F Library Authors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n" - }, { "name": "github.com/ulikunitz/xz", "path": "github.com/ulikunitz/xz/LICENSE", diff --git a/go.mod b/go.mod index 669ba8ced3..7e404372fc 100644 --- a/go.mod +++ b/go.mod @@ -92,7 +92,6 @@ require ( github.com/shurcooL/vfsgen v0.0.0-20230704071429-0000e147ea92 github.com/stretchr/testify v1.8.4 github.com/syndtr/goleveldb v1.0.0 - github.com/tstranex/u2f v1.0.0 github.com/ulikunitz/xz v0.5.11 github.com/urfave/cli/v2 v2.27.1 github.com/xanzy/go-gitlab v0.96.0 diff --git a/go.sum b/go.sum index a1f35bd1e0..5bd7ea67ba 100644 --- a/go.sum +++ b/go.sum @@ -801,8 +801,6 @@ github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpP github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/toqueteos/webbrowser v1.2.0 h1:tVP/gpK69Fx+qMJKsLE7TD8LuGWPnEV71wBN9rrstGQ= github.com/toqueteos/webbrowser v1.2.0/go.mod h1:XWoZq4cyp9WeUeak7w7LXRUQf1F1ATJMir8RTqb4ayM= -github.com/tstranex/u2f v1.0.0 h1:HhJkSzDDlVSVIVt7pDJwCHQj67k7A5EeBgPmeD+pVsQ= -github.com/tstranex/u2f v1.0.0/go.mod h1:eahSLaqAS0zsIEv80+vXT7WanXs7MQQDg3j3wGBSayo= github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8= diff --git a/models/migrations/v1_16/v210.go b/models/migrations/v1_16/v210.go index 533bb4bf80..d1a22ee87a 100644 --- a/models/migrations/v1_16/v210.go +++ b/models/migrations/v1_16/v210.go @@ -4,18 +4,44 @@ package v1_16 //nolint import ( + "crypto/ecdh" "encoding/base32" + "errors" "fmt" "strings" "code.gitea.io/gitea/models/migrations/base" "code.gitea.io/gitea/modules/timeutil" - "github.com/tstranex/u2f" "xorm.io/xorm" "xorm.io/xorm/schemas" ) +func parseU2FRegistration(raw []byte) (pubKey *ecdh.PublicKey, keyHandle []byte, err error) { + if len(raw) < 69 { + return nil, nil, errors.New("data is too short") + } + if raw[0] != 0x05 { + return nil, nil, errors.New("invalid reserved byte") + } + raw = raw[1:] + + pubKey, err = ecdh.P256().NewPublicKey(raw[:65]) + if err != nil { + return nil, nil, err + } + raw = raw[65:] + + khLen := int(raw[0]) + if len(raw) < khLen { + return nil, nil, errors.New("invalid key handle") + } + raw = raw[1:] + keyHandle = raw[:khLen] + + return pubKey, keyHandle, nil +} + // v208 migration was completely broken func RemigrateU2FCredentials(x *xorm.Engine) error { // Create webauthnCredential table @@ -117,12 +143,7 @@ func RemigrateU2FCredentials(x *xorm.Engine) error { } } for _, reg := range regs { - parsed := new(u2f.Registration) - err = parsed.UnmarshalBinary(reg.Raw) - if err != nil { - continue - } - pubKey, err := parsed.PubKey.ECDH() + pubKey, keyHandle, err := parseU2FRegistration(reg.Raw) if err != nil { continue } @@ -131,7 +152,7 @@ func RemigrateU2FCredentials(x *xorm.Engine) error { Name: reg.Name, LowerName: strings.ToLower(reg.Name), UserID: reg.UserID, - CredentialID: base32.HexEncoding.EncodeToString(parsed.KeyHandle), + CredentialID: base32.HexEncoding.EncodeToString(keyHandle), PublicKey: pubKey.Bytes(), AttestationType: "fido-u2f", AAGUID: []byte{}, diff --git a/models/migrations/v1_16/v210_test.go b/models/migrations/v1_16/v210_test.go index d43fb03106..b39646545a 100644 --- a/models/migrations/v1_16/v210_test.go +++ b/models/migrations/v1_16/v210_test.go @@ -4,6 +4,7 @@ package v1_16 //nolint import ( + "encoding/hex" "testing" "code.gitea.io/gitea/models/migrations/base" @@ -13,6 +14,19 @@ import ( "xorm.io/xorm/schemas" ) +func TestParseU2FRegistration(t *testing.T) { + // test vectors from https://github.com/tstranex/u2f/blob/d21a03e0b1d9fc1df59ff54e7a513655c1748b0c/register_test.go#L15 + + const testRegRespHex = "0504b174bc49c7ca254b70d2e5c207cee9cf174820ebd77ea3c65508c26da51b657c1cc6b952f8621697936482da0a6d3d3826a59095daf6cd7c03e2e60385d2f6d9402a552dfdb7477ed65fd84133f86196010b2215b57da75d315b7b9e8fe2e3925a6019551bab61d16591659cbaf00b4950f7abfe6660e2e006f76868b772d70c253082013c3081e4a003020102020a47901280001155957352300a06082a8648ce3d0403023017311530130603550403130c476e756262792050696c6f74301e170d3132303831343138323933325a170d3133303831343138323933325a3031312f302d0603550403132650696c6f74476e756262792d302e342e312d34373930313238303030313135353935373335323059301306072a8648ce3d020106082a8648ce3d030107034200048d617e65c9508e64bcc5673ac82a6799da3c1446682c258c463fffdf58dfd2fa3e6c378b53d795c4a4dffb4199edd7862f23abaf0203b4b8911ba0569994e101300a06082a8648ce3d0403020347003044022060cdb6061e9c22262d1aac1d96d8c70829b2366531dda268832cb836bcd30dfa0220631b1459f09e6330055722c8d89b7f48883b9089b88d60d1d9795902b30410df304502201471899bcc3987e62e8202c9b39c33c19033f7340352dba80fcab017db9230e402210082677d673d891933ade6f617e5dbde2e247e70423fd5ad7804a6d3d3961ef871" + + regResp, err := hex.DecodeString(testRegRespHex) + assert.NoError(t, err) + pubKey, keyHandle, err := parseU2FRegistration(regResp) + assert.NoError(t, err) + assert.Equal(t, "04b174bc49c7ca254b70d2e5c207cee9cf174820ebd77ea3c65508c26da51b657c1cc6b952f8621697936482da0a6d3d3826a59095daf6cd7c03e2e60385d2f6d9", hex.EncodeToString(pubKey.Bytes())) + assert.Equal(t, "2a552dfdb7477ed65fd84133f86196010b2215b57da75d315b7b9e8fe2e3925a6019551bab61d16591659cbaf00b4950f7abfe6660e2e006f76868b772d70c25", hex.EncodeToString(keyHandle)) +} + func Test_RemigrateU2FCredentials(t *testing.T) { // Create webauthnCredential table type WebauthnCredential struct {