~egtann/shh

ref: v1.9.0 shh/user.go -rw-r--r-- 7.4 KiB
53855162 — Evan Tann update install instructions, add man page 4 months ago
                                                                                
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"path/filepath"
	"strings"

	"golang.org/x/crypto/ssh/terminal"
)

const defaultPasswordPrompt = "password"

type user struct {
	Username username
	Password []byte
	Port     int
	Keys     *keys
}

type username string

type keys struct {
	PublicKey       *rsa.PublicKey
	PrivateKey      *rsa.PrivateKey
	PublicKeyBlock  *pem.Block
	PrivateKeyBlock *pem.Block
}

// getUser from the ~/.config/shh/config file. If the user already exists in
// the project's shh key, this returns nil User and nil error.
func getUser(config *config) (*user, error) {
	keys, err := getPublicKey(config.path)
	if err != nil {
		return nil, fmt.Errorf("get public key: %w", err)
	}
	u := &user{
		Username: config.Username,
		Port:     config.Port,
		Keys:     keys,
	}
	if u.Port == 0 {
		u.Port = 4210
	}
	return u, nil
}

func createUser(globalPath string) (*user, error) {
	fmt.Print("username (usually email): ")
	var uname string
	_, err := fmt.Scan(&uname)
	if err != nil {
		return nil, err
	}
	if uname == "" {
		return nil, errors.New("empty username")
	}

	password, err := requestPasswordAndConfirm(defaultPasswordPrompt)
	if err != nil {
		return nil, fmt.Errorf("request password: %w", err)
	}
	user := &user{
		Username: username(uname),
		Password: password,
	}

	// Create ~/.config/shh folder
	err = os.MkdirAll(globalPath, 0700)
	if err != nil {
		return nil, err
	}

	// Create public and private keys
	user.Keys, err = createKeys(globalPath, user.Password)
	if err != nil {
		return nil, fmt.Errorf("create keys: %w", err)
	}

	// Create initial config file (644) specifying username
	content := []byte(fmt.Sprintf("username=%s", user.Username))
	err = ioutil.WriteFile(filepath.Join(globalPath, "config"), content,
		0644)
	if err != nil {
		return nil, err
	}
	return user, nil
}

// requestPasswordFromServer and report an error if no password can be
// retrieved.
func requestPasswordFromServer(port int, resetTimer bool) ([]byte, error) {
	url := fmt.Sprint("http://127.0.0.1:", port)
	if err := pingServer(url); err != nil {
		return nil, err
	}
	if resetTimer {
		url += "/reset-timer"
	}
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	password, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("read all: %w", err)
	}
	if len(password) == 0 {
		return nil, errors.New("cached password not available. run `shh login`")
	}
	return password, nil
}

// requestPassword from user using the CLI. If prompt is empty, the default is
// used. This attempts to retrieve the password from the server if configured.
func requestPassword(port int, prompt string) ([]byte, error) {
	// Attempt to use the password from the server, if running. If any
	// error, just ask for the password.
	if port > 0 {
		password, err := requestPasswordFromServer(port, false)
		if err == nil {
			return password, nil
		}
	}
	fmt.Print(prompt + ": ")
	password, err := terminal.ReadPassword(int(os.Stdin.Fd()))
	if err != nil {
		return nil, err
	}
	fmt.Print("\n")
	if len(string(password)) < 24 {
		// The goal is to make manual entry so inconvenient that it's
		// never used. Use a password manager and a randomly generated
		// password instead.
		return nil, errors.New("password must be >= 24 chars")
	}
	return password, nil
}

func requestPasswordAndConfirm(prompt string) ([]byte, error) {
	fmt.Print(prompt + ": ")
	password, err := terminal.ReadPassword(int(os.Stdin.Fd()))
	if err != nil {
		return nil, err
	}
	fmt.Print("\n")
	if len(string(password)) < 24 {
		// The goal is to make manual entry so inconvenient that it's
		// never used. Use a password manager and a randomly generated
		// password instead.
		return nil, errors.New("password must be >= 24 chars")
	}
	fmt.Print("confirm password: ")
	password2, err := terminal.ReadPassword(int(os.Stdin.Fd()))
	if err != nil {
		return nil, err
	}
	if string(password) != string(password2) {
		return nil, errors.New("passwords do not match")
	}
	fmt.Print("\n")
	return password, nil
}

// createKeys at the given path, returning the keys and their pem block for use
// in the .shh file.
func createKeys(pth string, password []byte) (*keys, error) {
	keys := &keys{}
	keyPath := filepath.Join(pth, "id_rsa")

	// Generate id_rsa (600) and id_rsa.pub (644)
	var err error
	keys.PrivateKey, err = rsa.GenerateKey(rand.Reader, 4096)
	if err != nil {
		return nil, err
	}
	flags := os.O_CREATE | os.O_WRONLY | os.O_EXCL
	privKeyFile, err := os.OpenFile(keyPath, flags, 0600)
	if err != nil {
		return nil, err
	}
	defer privKeyFile.Close()

	keys.PrivateKeyBlock = &pem.Block{
		Type:  "RSA PRIVATE KEY",
		Bytes: x509.MarshalPKCS1PrivateKey(keys.PrivateKey),
	}
	keys.PrivateKeyBlock, err = x509.EncryptPEMBlock(
		rand.Reader,
		keys.PrivateKeyBlock.Type,
		keys.PrivateKeyBlock.Bytes,
		password,
		x509.PEMCipherAES256,
	)
	if err != nil {
		return nil, err
	}
	if err = pem.Encode(privKeyFile, keys.PrivateKeyBlock); err != nil {
		return nil, err
	}

	keyPath += ".pub"
	pubKeyFile, err := os.OpenFile(keyPath, flags, 0644)
	if err != nil {
		return nil, err
	}
	defer pubKeyFile.Close()

	keys.PublicKey = keys.PrivateKey.Public().(*rsa.PublicKey)
	keys.PublicKeyBlock = &pem.Block{
		Type:  "RSA PUBLIC KEY",
		Bytes: x509.MarshalPKCS1PublicKey(keys.PublicKey),
	}
	if err = pem.Encode(pubKeyFile, keys.PublicKeyBlock); err != nil {
		return nil, err
	}
	return keys, nil
}

func getPublicKey(pth string) (*keys, error) {
	keyPath := filepath.Join(pth, "id_rsa.pub")
	byt, err := ioutil.ReadFile(keyPath)
	if err != nil {
		return nil, err
	}
	keys := &keys{}
	keys.PublicKeyBlock, _ = pem.Decode(byt)
	if keys.PublicKeyBlock == nil || keys.PublicKeyBlock.Type != "RSA PUBLIC KEY" {
		return nil, errors.New("failed to decode pem block for public key")
	}
	keys.PublicKey, err = x509.ParsePKCS1PublicKey(keys.PublicKeyBlock.Bytes)
	if err != nil {
		return nil, fmt.Errorf("parse public key: %w", err)
	}
	return keys, nil
}

func getKeys(pth string, password []byte) (*keys, error) {
	keyPath := filepath.Join(pth, "id_rsa")

	// Require 600 permission on private key
	fileInfo, err := os.Stat(keyPath)
	if err != nil {
		return nil, err
	}
	if fileInfo.Mode() != 0600 {
		return nil, errors.New("bad private key permission level. id_rsa mode must be set to 0600")
	}

	byt, err := ioutil.ReadFile(keyPath)
	if err != nil {
		return nil, err
	}

	keys := &keys{}
	keys.PrivateKeyBlock, _ = pem.Decode(byt)
	if keys.PrivateKeyBlock == nil || keys.PrivateKeyBlock.Type != "RSA PRIVATE KEY" {
		return nil, errors.New("failed to decode pem block for encrypted private key")
	}
	byt, err = x509.DecryptPEMBlock(keys.PrivateKeyBlock, password)
	if err != nil {
		return nil, fmt.Errorf("decrypt pem: %w", err)
	}
	keys.PrivateKey, err = x509.ParsePKCS1PrivateKey(byt)
	if err != nil {
		return nil, fmt.Errorf("parse private key: %w", err)
	}

	pubkeys, err := getPublicKey(pth)
	if err != nil {
		return nil, fmt.Errorf("get public keys: %w", err)
	}
	keys.PublicKeyBlock = pubkeys.PublicKeyBlock
	keys.PublicKey = pubkeys.PublicKey
	return keys, nil
}

func pingServer(url string) error {
	resp, err := http.Get(url + "/ping")
	if err != nil {
		if strings.HasSuffix(err.Error(), "connection refused") {
			return errors.New("server not running. run `shh serve` first")
		}
		return fmt.Errorf("new request: %w", err)
	}
	_ = resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("bad resp code: %d", resp.StatusCode)
	}
	return nil
}