Files
forge/models/asymkey/ssh_key_verify.go
zeekayandHanzo Dev aa87442fdd Record upstream authorship centrally in NOTICE instead of in every file header
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the
Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in
NOTICE, alongside LICENSE. The header lint accepts any copyright line above the
SPDX line, so it no longer demands an upstream one.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-10 17:50:35 -07:00

53 lines
1.6 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// SPDX-License-Identifier: MIT
package asymkey
import (
"context"
"strings"
"github.com/hanzoai/git/models/db"
"github.com/hanzoai/git/modules/log"
"github.com/42wim/sshsig"
)
// SSHSignatureNamespace is the ssh-keygen "-n" namespace for key-ownership proofs.
// The settings page renders it into the command it tells the user to run, so both
// sides of the proof always agree on one value.
const SSHSignatureNamespace = "hanzo-git"
// VerifySSHKey marks a SSH key as verified
func VerifySSHKey(ctx context.Context, ownerID int64, fingerprint, token, signature string) (string, error) {
return db.WithTx2(ctx, func(ctx context.Context) (string, error) {
key := new(PublicKey)
has, err := db.GetEngine(ctx).Where("owner_id = ? AND fingerprint = ?", ownerID, fingerprint).Get(key)
if err != nil {
return "", err
} else if !has {
return "", ErrKeyNotExist{}
}
err = sshsig.Verify(strings.NewReader(token), []byte(signature), []byte(key.Content), SSHSignatureNamespace)
if err != nil {
// edge case for Windows based shells that will add CR LF if piped to ssh-keygen command
// see https://github.com/PowerShell/PowerShell/issues/5974
if sshsig.Verify(strings.NewReader(token+"\r\n"), []byte(signature), []byte(key.Content), SSHSignatureNamespace) != nil {
log.Debug("VerifySSHKey sshsig.Verify failed: %v", err)
return "", ErrSSHInvalidTokenSignature{
Fingerprint: key.Fingerprint,
}
}
}
key.Verified = true
if _, err := db.GetEngine(ctx).ID(key.ID).Cols("verified").Update(key); err != nil {
return "", err
}
return key.Fingerprint, nil
})
}