2018-01-16 12:48:25 -05:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018, Joyent, Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
//
|
|
|
|
|
2017-04-26 15:12:57 -04:00
|
|
|
package authentication
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
2017-10-31 11:02:15 -04:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
2017-04-26 15:12:57 -04:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
2017-12-29 08:00:47 -05:00
|
|
|
"path"
|
2017-04-26 15:12:57 -04:00
|
|
|
"strings"
|
|
|
|
|
2018-01-16 12:48:25 -05:00
|
|
|
pkgerrors "github.com/pkg/errors"
|
2017-04-26 15:12:57 -04:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
"golang.org/x/crypto/ssh/agent"
|
|
|
|
)
|
|
|
|
|
2017-12-29 08:00:47 -05:00
|
|
|
var (
|
2018-01-16 12:48:25 -05:00
|
|
|
ErrUnsetEnvVar = pkgerrors.New("environment variable SSH_AUTH_SOCK not set")
|
2017-12-29 08:00:47 -05:00
|
|
|
)
|
|
|
|
|
2017-04-26 15:12:57 -04:00
|
|
|
type SSHAgentSigner struct {
|
|
|
|
formattedKeyFingerprint string
|
|
|
|
keyFingerprint string
|
2017-10-31 11:02:15 -04:00
|
|
|
algorithm string
|
2017-04-26 15:12:57 -04:00
|
|
|
accountName string
|
2017-12-29 08:00:47 -05:00
|
|
|
userName string
|
2017-04-26 15:12:57 -04:00
|
|
|
keyIdentifier string
|
|
|
|
|
|
|
|
agent agent.Agent
|
|
|
|
key ssh.PublicKey
|
|
|
|
}
|
|
|
|
|
2017-12-29 08:00:47 -05:00
|
|
|
type SSHAgentSignerInput struct {
|
|
|
|
KeyID string
|
|
|
|
AccountName string
|
|
|
|
Username string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSSHAgentSigner(input SSHAgentSignerInput) (*SSHAgentSigner, error) {
|
|
|
|
sshAgentAddress, agentOk := os.LookupEnv("SSH_AUTH_SOCK")
|
|
|
|
if !agentOk {
|
|
|
|
return nil, ErrUnsetEnvVar
|
2017-04-26 15:12:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := net.Dial("unix", sshAgentAddress)
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, pkgerrors.Wrap(err, "unable to dial SSH agent")
|
2017-04-26 15:12:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ag := agent.NewClient(conn)
|
|
|
|
|
2017-12-29 08:00:47 -05:00
|
|
|
signer := &SSHAgentSigner{
|
|
|
|
keyFingerprint: input.KeyID,
|
|
|
|
accountName: input.AccountName,
|
|
|
|
agent: ag,
|
|
|
|
}
|
|
|
|
|
|
|
|
matchingKey, err := signer.MatchKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
signer.key = matchingKey
|
|
|
|
signer.formattedKeyFingerprint = formatPublicKeyFingerprint(signer.key, true)
|
|
|
|
if input.Username != "" {
|
|
|
|
signer.userName = input.Username
|
|
|
|
signer.keyIdentifier = path.Join("/", signer.accountName, "users", input.Username, "keys", signer.formattedKeyFingerprint)
|
|
|
|
} else {
|
|
|
|
signer.keyIdentifier = path.Join("/", signer.accountName, "keys", signer.formattedKeyFingerprint)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, algorithm, err := signer.SignRaw("HelloWorld")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Cannot sign using ssh agent: %s", err)
|
|
|
|
}
|
|
|
|
signer.algorithm = algorithm
|
|
|
|
|
|
|
|
return signer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SSHAgentSigner) MatchKey() (ssh.PublicKey, error) {
|
|
|
|
keys, err := s.agent.List()
|
2017-04-26 15:12:57 -04:00
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, pkgerrors.Wrap(err, "unable to list keys in SSH Agent")
|
2017-04-26 15:12:57 -04:00
|
|
|
}
|
|
|
|
|
2017-12-29 08:00:47 -05:00
|
|
|
keyFingerprintStripped := strings.TrimPrefix(s.keyFingerprint, "MD5:")
|
2017-10-31 11:02:15 -04:00
|
|
|
keyFingerprintStripped = strings.TrimPrefix(keyFingerprintStripped, "SHA256:")
|
|
|
|
keyFingerprintStripped = strings.Replace(keyFingerprintStripped, ":", "", -1)
|
2017-04-26 15:12:57 -04:00
|
|
|
|
|
|
|
var matchingKey ssh.PublicKey
|
|
|
|
for _, key := range keys {
|
2017-10-31 11:02:15 -04:00
|
|
|
keyMD5 := md5.New()
|
|
|
|
keyMD5.Write(key.Marshal())
|
|
|
|
finalizedMD5 := fmt.Sprintf("%x", keyMD5.Sum(nil))
|
2017-04-26 15:12:57 -04:00
|
|
|
|
2017-10-31 11:02:15 -04:00
|
|
|
keySHA256 := sha256.New()
|
|
|
|
keySHA256.Write(key.Marshal())
|
|
|
|
finalizedSHA256 := base64.RawStdEncoding.EncodeToString(keySHA256.Sum(nil))
|
|
|
|
|
|
|
|
if keyFingerprintStripped == finalizedMD5 || keyFingerprintStripped == finalizedSHA256 {
|
2017-04-26 15:12:57 -04:00
|
|
|
matchingKey = key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if matchingKey == nil {
|
2017-12-29 08:00:47 -05:00
|
|
|
return nil, fmt.Errorf("No key in the SSH Agent matches fingerprint: %s", s.keyFingerprint)
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
2017-12-29 08:00:47 -05:00
|
|
|
return matchingKey, nil
|
2017-04-26 15:12:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SSHAgentSigner) Sign(dateHeader string) (string, error) {
|
|
|
|
const headerName = "date"
|
|
|
|
|
|
|
|
signature, err := s.agent.Sign(s.key, []byte(fmt.Sprintf("%s: %s", headerName, dateHeader)))
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return "", pkgerrors.Wrap(err, "unable to sign date header")
|
2017-04-26 15:12:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
keyFormat, err := keyFormatToKeyType(signature.Format)
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return "", pkgerrors.Wrap(err, "unable to format signature")
|
2017-04-26 15:12:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var authSignature httpAuthSignature
|
|
|
|
switch keyFormat {
|
|
|
|
case "rsa":
|
|
|
|
authSignature, err = newRSASignature(signature.Blob)
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return "", pkgerrors.Wrap(err, "unable to read RSA signature")
|
2017-04-26 15:12:57 -04:00
|
|
|
}
|
|
|
|
case "ecdsa":
|
|
|
|
authSignature, err = newECDSASignature(signature.Blob)
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return "", pkgerrors.Wrap(err, "unable to read ECDSA signature")
|
2017-04-26 15:12:57 -04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("Unsupported algorithm from SSH agent: %s", signature.Format)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(authorizationHeaderFormat, s.keyIdentifier,
|
|
|
|
authSignature.SignatureType(), headerName, authSignature.String()), nil
|
|
|
|
}
|
2017-10-31 11:02:15 -04:00
|
|
|
|
|
|
|
func (s *SSHAgentSigner) SignRaw(toSign string) (string, string, error) {
|
|
|
|
signature, err := s.agent.Sign(s.key, []byte(toSign))
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return "", "", pkgerrors.Wrap(err, "unable to sign string")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
keyFormat, err := keyFormatToKeyType(signature.Format)
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return "", "", pkgerrors.Wrap(err, "unable to format key")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var authSignature httpAuthSignature
|
|
|
|
switch keyFormat {
|
|
|
|
case "rsa":
|
|
|
|
authSignature, err = newRSASignature(signature.Blob)
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return "", "", pkgerrors.Wrap(err, "unable to read RSA signature")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
case "ecdsa":
|
|
|
|
authSignature, err = newECDSASignature(signature.Blob)
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return "", "", pkgerrors.Wrap(err, "unable to read ECDSA signature")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return "", "", fmt.Errorf("Unsupported algorithm from SSH agent: %s", signature.Format)
|
|
|
|
}
|
|
|
|
|
|
|
|
return authSignature.String(), authSignature.SignatureType(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SSHAgentSigner) KeyFingerprint() string {
|
|
|
|
return s.formattedKeyFingerprint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SSHAgentSigner) DefaultAlgorithm() string {
|
|
|
|
return s.algorithm
|
|
|
|
}
|