aboutsummaryrefslogtreecommitdiffstats
path: root/key/keygen.go
blob: 9857b76cf086f13134fd64d55c9c8dae74409fdd (plain) (blame)
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
package key

import (
	crypto_rand "crypto/rand"
	"errors"
	"fmt"
	"os"
	"path/filepath"

	"github.com/adrg/xdg"
	"golang.org/x/crypto/nacl/box"
)

var (
	pubKeyFile                 = filepath.Join(xdg.DataHome, "hose", "pubkey")
	pubKeyFileMode os.FileMode = 0644

	privKeyFile                 = filepath.Join(xdg.DataHome, "hose", "privkey")
	privKeyFileMode os.FileMode = 0600
)

// Generate generates a new public/private keypair. It stores the private key in the
// private key file and the public key in the public key file.  If either of the key
// files already exist, they will not be overwritten; instead an error will be returned.
func Generate() error {
	// Create public key file.
	pubFile, err := createFile(pubKeyFile, pubKeyFileMode)
	if err != nil {
		return err
	}
	defer pubFile.Close()

	// Create private key file.
	privFile, err := createFile(privKeyFile, privKeyFileMode)
	if err != nil {
		pubFile.Close()
		_ = os.Remove(pubKeyFile)
		return err
	}
	defer privFile.Close()

	// Generate keypair.
	pubkey, privkey, err := box.GenerateKey(crypto_rand.Reader)
	if err != nil {
		return err
	}

	// Write keypair to files.
	if _, err := pubFile.Write((*pubkey)[:]); err != nil {
		return err
	}
	if _, err := privFile.Write((*privkey)[:]); err != nil {
		return err
	}

	return nil
}

// createFile creates a file with the specified permissions and returns it for writing.
// It does not truncate an existing file. If the file already exists, an error is returned.
func createFile(name string, mode os.FileMode) (*os.File, error) {
	exists, err := fileExists(name)
	if err != nil {
		return nil, err // unexpected error.
	} else if exists {
		return nil, errFileExists(name) // file exists; do not overwrite.
	}
	// Does not exist; continue;

	f, err := os.Create(name)
	if err != nil {
		return nil, err
	}

	if err := f.Chmod(mode); err != nil {
		f.Close()
		_ = os.Remove(name)
		return nil, err
	}

	return f, nil
}

// fileExists returns a nil error and true/false if a file does/doesn't exist.
// If an error is encountered, a non-nil error is returned.
func fileExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if errors.Is(err, os.ErrNotExist) {
		return false, nil // file doesn't exist.
	} else if err != nil {
		return false, err // unexpected error.
	}
	return true, nil // file exists.
}

// errFileExists constructs a 'file already exists' error message.
func errFileExists(path string) error {
	return fmt.Errorf("%s: %s", os.ErrExist, path)
}