2021-09-09 20:25:25 +08:00
|
|
|
//go:build !plan9
|
2019-05-11 02:00:18 +08:00
|
|
|
|
|
|
|
package sftp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-11-05 23:18:51 +08:00
|
|
|
"context"
|
2021-10-09 00:14:35 +08:00
|
|
|
"crypto/ecdsa"
|
2021-10-10 20:18:51 +08:00
|
|
|
"crypto/ed25519"
|
2021-10-09 00:14:35 +08:00
|
|
|
"crypto/elliptic"
|
2019-05-11 02:00:18 +08:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/subtle"
|
|
|
|
"crypto/x509"
|
2019-12-25 01:34:35 +08:00
|
|
|
"encoding/base64"
|
2019-05-11 02:00:18 +08:00
|
|
|
"encoding/pem"
|
2021-11-04 18:12:57 +08:00
|
|
|
"errors"
|
2019-05-11 02:00:18 +08:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2019-08-01 05:20:46 +08:00
|
|
|
"github.com/rclone/rclone/cmd/serve/proxy"
|
|
|
|
"github.com/rclone/rclone/cmd/serve/proxy/proxyflags"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config"
|
|
|
|
"github.com/rclone/rclone/lib/env"
|
2021-06-11 06:46:36 +08:00
|
|
|
"github.com/rclone/rclone/lib/file"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/vfs"
|
|
|
|
"github.com/rclone/rclone/vfs/vfsflags"
|
2019-05-11 02:00:18 +08:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
|
|
|
// server contains everything to run the server
|
|
|
|
type server struct {
|
|
|
|
f fs.Fs
|
|
|
|
opt Options
|
|
|
|
vfs *vfs.VFS
|
2020-11-05 23:18:51 +08:00
|
|
|
ctx context.Context // for global config
|
2019-05-11 02:00:18 +08:00
|
|
|
config *ssh.ServerConfig
|
|
|
|
listener net.Listener
|
|
|
|
waitChan chan struct{} // for waiting on the listener to close
|
2019-08-01 05:20:46 +08:00
|
|
|
proxy *proxy.Proxy
|
2019-05-11 02:00:18 +08:00
|
|
|
}
|
|
|
|
|
2020-11-05 23:18:51 +08:00
|
|
|
func newServer(ctx context.Context, f fs.Fs, opt *Options) *server {
|
2019-05-11 02:00:18 +08:00
|
|
|
s := &server{
|
|
|
|
f: f,
|
2020-11-05 23:18:51 +08:00
|
|
|
ctx: ctx,
|
2019-05-11 02:00:18 +08:00
|
|
|
opt: *opt,
|
|
|
|
waitChan: make(chan struct{}),
|
|
|
|
}
|
2019-08-01 05:20:46 +08:00
|
|
|
if proxyflags.Opt.AuthProxy != "" {
|
2020-11-05 23:18:51 +08:00
|
|
|
s.proxy = proxy.New(ctx, &proxyflags.Opt)
|
2019-08-01 05:20:46 +08:00
|
|
|
} else {
|
|
|
|
s.vfs = vfs.New(f, &vfsflags.Opt)
|
|
|
|
}
|
2019-05-11 02:00:18 +08:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-08-01 05:20:46 +08:00
|
|
|
// getVFS gets the vfs from s or the proxy
|
|
|
|
func (s *server) getVFS(what string, sshConn *ssh.ServerConn) (VFS *vfs.VFS) {
|
|
|
|
if s.proxy == nil {
|
|
|
|
return s.vfs
|
|
|
|
}
|
|
|
|
if sshConn.Permissions == nil && sshConn.Permissions.Extensions == nil {
|
|
|
|
fs.Infof(what, "SSH Permissions Extensions not found")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
key := sshConn.Permissions.Extensions["_vfsKey"]
|
|
|
|
if key == "" {
|
|
|
|
fs.Infof(what, "VFS key not found")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
VFS = s.proxy.Get(key)
|
|
|
|
if VFS == nil {
|
|
|
|
fs.Infof(what, "failed to read VFS from cache")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return VFS
|
|
|
|
}
|
|
|
|
|
2021-01-06 01:14:40 +08:00
|
|
|
// Accept a single connection - run in a go routine as the ssh
|
|
|
|
// authentication can block
|
|
|
|
func (s *server) acceptConnection(nConn net.Conn) {
|
|
|
|
what := describeConn(nConn)
|
|
|
|
|
|
|
|
// Before use, a handshake must be performed on the incoming net.Conn.
|
|
|
|
sshConn, chans, reqs, err := ssh.NewServerConn(nConn, s.config)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(what, "SSH login failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.Infof(what, "SSH login from %s using %s", sshConn.User(), sshConn.ClientVersion())
|
|
|
|
|
|
|
|
// Discard all global out-of-band Requests
|
|
|
|
go ssh.DiscardRequests(reqs)
|
|
|
|
|
|
|
|
c := &conn{
|
|
|
|
what: what,
|
|
|
|
vfs: s.getVFS(what, sshConn),
|
|
|
|
}
|
|
|
|
if c.vfs == nil {
|
|
|
|
fs.Infof(what, "Closing unauthenticated connection (couldn't find VFS)")
|
|
|
|
_ = nConn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.handlers = newVFSHandler(c.vfs)
|
|
|
|
|
|
|
|
// Accept all channels
|
|
|
|
go c.handleChannels(chans)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept connections and call them in a go routine
|
2019-05-11 02:00:18 +08:00
|
|
|
func (s *server) acceptConnections() {
|
|
|
|
for {
|
|
|
|
nConn, err := s.listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "use of closed network connection") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fs.Errorf(nil, "Failed to accept incoming connection: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2021-01-06 01:14:40 +08:00
|
|
|
go s.acceptConnection(nConn)
|
2019-05-11 02:00:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Based on example server code from golang.org/x/crypto/ssh and server_standalone
|
|
|
|
func (s *server) serve() (err error) {
|
|
|
|
var authorizedKeysMap map[string]struct{}
|
|
|
|
|
2019-12-25 01:34:35 +08:00
|
|
|
// ensure the user isn't trying to use conflicting flags
|
|
|
|
if proxyflags.Opt.AuthProxy != "" && s.opt.AuthorizedKeys != "" && s.opt.AuthorizedKeys != DefaultOpt.AuthorizedKeys {
|
|
|
|
return errors.New("--auth-proxy and --authorized-keys cannot be used at the same time")
|
|
|
|
}
|
|
|
|
|
2019-05-11 02:00:18 +08:00
|
|
|
// Load the authorized keys
|
2019-12-25 01:34:35 +08:00
|
|
|
if s.opt.AuthorizedKeys != "" && proxyflags.Opt.AuthProxy == "" {
|
2019-05-11 02:00:18 +08:00
|
|
|
authKeysFile := env.ShellExpand(s.opt.AuthorizedKeys)
|
|
|
|
authorizedKeysMap, err = loadAuthorizedKeys(authKeysFile)
|
|
|
|
// If user set the flag away from the default then report an error
|
|
|
|
if err != nil && s.opt.AuthorizedKeys != DefaultOpt.AuthorizedKeys {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fs.Logf(nil, "Loaded %d authorized keys from %q", len(authorizedKeysMap), authKeysFile)
|
|
|
|
}
|
|
|
|
|
2019-08-07 23:36:17 +08:00
|
|
|
if !s.opt.NoAuth && len(authorizedKeysMap) == 0 && s.opt.User == "" && s.opt.Pass == "" && s.proxy == nil {
|
|
|
|
return errors.New("no authorization found, use --user/--pass or --authorized-keys or --no-auth or --auth-proxy")
|
2019-05-11 02:00:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// An SSH server is represented by a ServerConfig, which holds
|
|
|
|
// certificate details and handles authentication of ServerConns.
|
|
|
|
s.config = &ssh.ServerConfig{
|
2020-11-05 19:33:32 +08:00
|
|
|
ServerVersion: "SSH-2.0-" + fs.GetConfig(s.ctx).UserAgent,
|
2019-05-11 02:00:18 +08:00
|
|
|
PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
|
|
|
|
fs.Debugf(describeConn(c), "Password login attempt for %s", c.User())
|
2019-08-01 05:20:46 +08:00
|
|
|
if s.proxy != nil {
|
|
|
|
// query the proxy for the config
|
2019-12-25 01:34:35 +08:00
|
|
|
_, vfsKey, err := s.proxy.Call(c.User(), string(pass), false)
|
2019-08-01 05:20:46 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// just return the Key so we can get it back from the cache
|
|
|
|
return &ssh.Permissions{
|
|
|
|
Extensions: map[string]string{
|
|
|
|
"_vfsKey": vfsKey,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
} else if s.opt.User != "" && s.opt.Pass != "" {
|
2019-05-11 02:00:18 +08:00
|
|
|
userOK := subtle.ConstantTimeCompare([]byte(c.User()), []byte(s.opt.User))
|
|
|
|
passOK := subtle.ConstantTimeCompare(pass, []byte(s.opt.Pass))
|
|
|
|
if (userOK & passOK) == 1 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("password rejected for %q", c.User())
|
|
|
|
},
|
|
|
|
PublicKeyCallback: func(c ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) {
|
|
|
|
fs.Debugf(describeConn(c), "Public key login attempt for %s", c.User())
|
2019-08-01 05:20:46 +08:00
|
|
|
if s.proxy != nil {
|
2019-12-25 01:34:35 +08:00
|
|
|
//query the proxy for the config
|
|
|
|
_, vfsKey, err := s.proxy.Call(
|
|
|
|
c.User(),
|
|
|
|
base64.StdEncoding.EncodeToString(pubKey.Marshal()),
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// just return the Key so we can get it back from the cache
|
|
|
|
return &ssh.Permissions{
|
|
|
|
Extensions: map[string]string{
|
|
|
|
"_vfsKey": vfsKey,
|
|
|
|
},
|
|
|
|
}, nil
|
2019-08-01 05:20:46 +08:00
|
|
|
}
|
2019-05-11 02:00:18 +08:00
|
|
|
if _, ok := authorizedKeysMap[string(pubKey.Marshal())]; ok {
|
|
|
|
return &ssh.Permissions{
|
|
|
|
// Record the public key used for authentication.
|
|
|
|
Extensions: map[string]string{
|
|
|
|
"pubkey-fp": ssh.FingerprintSHA256(pubKey),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown public key for %q", c.User())
|
|
|
|
},
|
|
|
|
AuthLogCallback: func(conn ssh.ConnMetadata, method string, err error) {
|
|
|
|
status := "OK"
|
|
|
|
if err != nil {
|
|
|
|
status = err.Error()
|
|
|
|
}
|
|
|
|
fs.Debugf(describeConn(conn), "ssh auth %q from %q: %s", method, conn.ClientVersion(), status)
|
|
|
|
},
|
|
|
|
NoClientAuth: s.opt.NoAuth,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the private key, from the cache if not explicitly configured
|
2020-05-09 21:43:17 +08:00
|
|
|
keyPaths := s.opt.HostKeys
|
2021-09-10 21:35:53 +08:00
|
|
|
cachePath := filepath.Join(config.GetCacheDir(), "serve-sftp")
|
2020-05-09 21:43:17 +08:00
|
|
|
if len(keyPaths) == 0 {
|
2021-10-09 00:14:35 +08:00
|
|
|
keyPaths = []string{
|
|
|
|
filepath.Join(cachePath, "id_rsa"),
|
|
|
|
filepath.Join(cachePath, "id_ecdsa"),
|
2021-10-10 20:18:51 +08:00
|
|
|
filepath.Join(cachePath, "id_ed25519"),
|
2021-10-09 00:14:35 +08:00
|
|
|
}
|
2019-05-11 02:00:18 +08:00
|
|
|
}
|
2020-05-09 21:43:17 +08:00
|
|
|
for _, keyPath := range keyPaths {
|
|
|
|
private, err := loadPrivateKey(keyPath)
|
|
|
|
if err != nil && len(s.opt.HostKeys) == 0 {
|
|
|
|
fs.Debugf(nil, "Failed to load %q: %v", keyPath, err)
|
|
|
|
// If loading a cached key failed, make the keys and retry
|
2021-06-11 06:46:36 +08:00
|
|
|
err = file.MkdirAll(cachePath, 0700)
|
2020-05-09 21:43:17 +08:00
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("failed to create cache path: %w", err)
|
2020-05-09 21:43:17 +08:00
|
|
|
}
|
2021-10-10 20:35:57 +08:00
|
|
|
if strings.HasSuffix(keyPath, string(os.PathSeparator)+"id_rsa") {
|
2021-10-09 00:14:35 +08:00
|
|
|
const bits = 2048
|
|
|
|
fs.Logf(nil, "Generating %d bit key pair at %q", bits, keyPath)
|
|
|
|
err = makeRSASSHKeyPair(bits, keyPath+".pub", keyPath)
|
2021-10-10 20:35:57 +08:00
|
|
|
} else if strings.HasSuffix(keyPath, string(os.PathSeparator)+"id_ecdsa") {
|
2021-10-09 00:14:35 +08:00
|
|
|
fs.Logf(nil, "Generating ECDSA p256 key pair at %q", keyPath)
|
|
|
|
err = makeECDSASSHKeyPair(keyPath+".pub", keyPath)
|
2021-10-10 20:35:57 +08:00
|
|
|
} else if strings.HasSuffix(keyPath, string(os.PathSeparator)+"id_ed25519") {
|
2021-10-10 20:18:51 +08:00
|
|
|
fs.Logf(nil, "Generating Ed25519 key pair at %q", keyPath)
|
|
|
|
err = makeEd25519SSHKeyPair(keyPath+".pub", keyPath)
|
2021-10-09 00:14:35 +08:00
|
|
|
} else {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("don't know how to generate key pair %q", keyPath)
|
2021-10-09 00:14:35 +08:00
|
|
|
}
|
2020-05-09 21:43:17 +08:00
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("failed to create SSH key pair: %w", err)
|
2020-05-09 21:43:17 +08:00
|
|
|
}
|
2021-10-09 00:14:35 +08:00
|
|
|
// reload the new key
|
2020-05-09 21:43:17 +08:00
|
|
|
private, err = loadPrivateKey(keyPath)
|
2019-05-11 02:00:18 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2020-05-09 21:43:17 +08:00
|
|
|
return err
|
2019-05-11 02:00:18 +08:00
|
|
|
}
|
2020-05-09 21:43:17 +08:00
|
|
|
fs.Debugf(nil, "Loaded private key from %q", keyPath)
|
2019-05-11 02:00:18 +08:00
|
|
|
|
2020-05-09 21:43:17 +08:00
|
|
|
s.config.AddHostKey(private)
|
|
|
|
}
|
2019-05-11 02:00:18 +08:00
|
|
|
|
|
|
|
// Once a ServerConfig has been configured, connections can be
|
|
|
|
// accepted.
|
|
|
|
s.listener, err = net.Listen("tcp", s.opt.ListenAddr)
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("failed to listen for connection: %w", err)
|
2019-05-11 02:00:18 +08:00
|
|
|
}
|
|
|
|
fs.Logf(nil, "SFTP server listening on %v\n", s.listener.Addr())
|
|
|
|
|
|
|
|
go s.acceptConnections()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the address the server is listening on
|
|
|
|
func (s *server) Addr() string {
|
|
|
|
return s.listener.Addr().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve runs the sftp server in the background.
|
|
|
|
//
|
|
|
|
// Use s.Close() and s.Wait() to shutdown server
|
|
|
|
func (s *server) Serve() error {
|
|
|
|
err := s.serve()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait blocks while the listener is open.
|
|
|
|
func (s *server) Wait() {
|
|
|
|
<-s.waitChan
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close shuts the running server down
|
|
|
|
func (s *server) Close() {
|
|
|
|
err := s.listener.Close()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Error on closing SFTP server: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
close(s.waitChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadPrivateKey(keyPath string) (ssh.Signer, error) {
|
2022-08-20 22:38:02 +08:00
|
|
|
privateBytes, err := os.ReadFile(keyPath)
|
2019-05-11 02:00:18 +08:00
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, fmt.Errorf("failed to load private key: %w", err)
|
2019-05-11 02:00:18 +08:00
|
|
|
}
|
|
|
|
private, err := ssh.ParsePrivateKey(privateBytes)
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, fmt.Errorf("failed to parse private key: %w", err)
|
2019-05-11 02:00:18 +08:00
|
|
|
}
|
|
|
|
return private, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Public key authentication is done by comparing
|
|
|
|
// the public key of a received connection
|
|
|
|
// with the entries in the authorized_keys file.
|
|
|
|
func loadAuthorizedKeys(authorizedKeysPath string) (authorizedKeysMap map[string]struct{}, err error) {
|
2022-08-20 22:38:02 +08:00
|
|
|
authorizedKeysBytes, err := os.ReadFile(authorizedKeysPath)
|
2019-05-11 02:00:18 +08:00
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, fmt.Errorf("failed to load authorized keys: %w", err)
|
2019-05-11 02:00:18 +08:00
|
|
|
}
|
|
|
|
authorizedKeysMap = make(map[string]struct{})
|
|
|
|
for len(authorizedKeysBytes) > 0 {
|
|
|
|
pubKey, _, _, rest, err := ssh.ParseAuthorizedKey(authorizedKeysBytes)
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return nil, fmt.Errorf("failed to parse authorized keys: %w", err)
|
2019-05-11 02:00:18 +08:00
|
|
|
}
|
|
|
|
authorizedKeysMap[string(pubKey.Marshal())] = struct{}{}
|
|
|
|
authorizedKeysBytes = bytes.TrimSpace(rest)
|
|
|
|
}
|
|
|
|
return authorizedKeysMap, nil
|
|
|
|
}
|
|
|
|
|
2021-10-09 00:14:35 +08:00
|
|
|
// makeRSASSHKeyPair make a pair of public and private keys for SSH access.
|
2019-05-11 02:00:18 +08:00
|
|
|
// Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file.
|
|
|
|
// Private Key generated is PEM encoded
|
|
|
|
//
|
|
|
|
// Originally from: https://stackoverflow.com/a/34347463/164234
|
2021-10-09 00:14:35 +08:00
|
|
|
func makeRSASSHKeyPair(bits int, pubKeyPath, privateKeyPath string) (err error) {
|
2019-05-11 02:00:18 +08:00
|
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate and write private key as PEM
|
|
|
|
privateKeyFile, err := os.OpenFile(privateKeyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fs.CheckClose(privateKeyFile, &err)
|
|
|
|
privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
|
|
|
|
if err := pem.Encode(privateKeyFile, privateKeyPEM); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate and write public key
|
|
|
|
pub, err := ssh.NewPublicKey(&privateKey.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-20 22:38:02 +08:00
|
|
|
return os.WriteFile(pubKeyPath, ssh.MarshalAuthorizedKey(pub), 0644)
|
2019-05-11 02:00:18 +08:00
|
|
|
}
|
2021-10-09 00:14:35 +08:00
|
|
|
|
|
|
|
// makeECDSASSHKeyPair make a pair of public and private keys for ECDSA SSH access.
|
|
|
|
// Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file.
|
|
|
|
// Private Key generated is PEM encoded
|
|
|
|
func makeECDSASSHKeyPair(pubKeyPath, privateKeyPath string) (err error) {
|
|
|
|
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate and write private key as PEM
|
|
|
|
privateKeyFile, err := os.OpenFile(privateKeyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fs.CheckClose(privateKeyFile, &err)
|
|
|
|
buf, err := x509.MarshalECPrivateKey(privateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
privateKeyPEM := &pem.Block{Type: "EC PRIVATE KEY", Bytes: buf}
|
|
|
|
if err := pem.Encode(privateKeyFile, privateKeyPEM); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate and write public key
|
|
|
|
pub, err := ssh.NewPublicKey(&privateKey.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-20 22:38:02 +08:00
|
|
|
return os.WriteFile(pubKeyPath, ssh.MarshalAuthorizedKey(pub), 0644)
|
2021-10-09 00:14:35 +08:00
|
|
|
}
|
2021-10-10 20:18:51 +08:00
|
|
|
|
|
|
|
// makeEd25519SSHKeyPair make a pair of public and private keys for Ed25519 SSH access.
|
|
|
|
// Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file.
|
|
|
|
// Private Key generated is PEM encoded
|
|
|
|
func makeEd25519SSHKeyPair(pubKeyPath, privateKeyPath string) (err error) {
|
|
|
|
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate and write private key as PEM
|
|
|
|
privateKeyFile, err := os.OpenFile(privateKeyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fs.CheckClose(privateKeyFile, &err)
|
|
|
|
buf, err := x509.MarshalPKCS8PrivateKey(privateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
privateKeyPEM := &pem.Block{Type: "PRIVATE KEY", Bytes: buf}
|
|
|
|
if err := pem.Encode(privateKeyFile, privateKeyPEM); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate and write public key
|
|
|
|
pub, err := ssh.NewPublicKey(publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-20 22:38:02 +08:00
|
|
|
return os.WriteFile(pubKeyPath, ssh.MarshalAuthorizedKey(pub), 0644)
|
2021-10-10 20:18:51 +08:00
|
|
|
}
|