forked from skeeto/passphrase2pgp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.go
196 lines (170 loc) · 3.54 KB
/
ssh.go
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
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/binary"
"io"
)
type pem struct {
buf bytes.Buffer
b64 io.WriteCloser
stack []io.Writer
bound bool
}
func newPEM(bound bool) *pem {
p := new(pem)
if bound {
p.buf.WriteString("-----BEGIN OPENSSH PRIVATE KEY-----\n")
}
encoding := base64.RawStdEncoding.WithPadding('=')
wrap := &wrapper{&p.buf, 70, 0}
p.b64 = base64.NewEncoder(encoding, wrap)
p.stack = append(p.stack, p.b64)
p.bound = bound
return p
}
func (p *pem) Close() error {
if len(p.stack) != 1 {
panic("PEM Push()/Pop() mismatch")
}
p.b64.Close()
if p.bound {
p.buf.WriteString("\n-----END OPENSSH PRIVATE KEY-----\n")
}
return nil
}
func (p *pem) Push() {
var buf bytes.Buffer
p.stack = append(p.stack, &buf)
}
func (p *pem) Pop() {
top := p.stack[len(p.stack)-1].(*bytes.Buffer)
p.stack[len(p.stack)-1] = nil
p.stack = p.stack[:len(p.stack)-1]
p.Bytes(top.Bytes())
}
func (p *pem) Top() *bytes.Buffer {
return p.stack[len(p.stack)-1].(*bytes.Buffer)
}
func (p *pem) Output() []byte {
return p.buf.Bytes()
}
func (p *pem) Write(b []byte) (int, error) {
return p.stack[len(p.stack)-1].Write(b)
}
func (p *pem) Uint32(v uint32) {
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], v)
p.Write(buf[:])
}
func (p *pem) Bytes(b []byte) {
p.Uint32(uint32(len(b)))
p.Write(b)
}
func (p *pem) String(str string) {
p.Bytes([]byte(str))
}
func secSSH(pub, sec, uid, password []byte, rounds uint32) []byte {
pem := newPEM(true)
pem.Write([]byte("openssh-key-v1\x00")) // magic
var pad int
var check uint32
var stream cipher.Stream
if rounds == 0 {
pem.String("none") // ciphername
pem.String("none") // kdfname
pem.Bytes([]byte{}) // kdfoptions
pad = 8
} else {
pem.String("aes256-ctr")
pem.String("bcrypt")
var buf [20]byte
if _, err := rand.Read(buf[:]); err != nil {
panic(err)
}
salt := buf[:16]
check = binary.LittleEndian.Uint32(buf[16:20])
pem.Push()
pem.Bytes(salt)
pem.Uint32(rounds)
pem.Pop()
keyiv := bcryptPBKDF(password, salt, 32+16, rounds)
key := keyiv[:32]
iv := keyiv[32:]
block, _ := aes.NewCipher(key)
stream = cipher.NewCTR(block, iv)
pad = 16
}
pem.Uint32(1) // number of keys
// Public key
pem.Push()
pem.String("ssh-ed25519")
pem.Bytes(pub)
pem.Pop()
// Private key
pem.Push()
pem.Uint32(check)
pem.Uint32(check)
pem.String("ssh-ed25519")
pem.Bytes(pub)
pem.Bytes(append(sec[0:32:32], pub...))
pem.Bytes(uid)
top := pem.Top()
for i := 1; top.Len()%pad != 0; i++ {
// Pad to block size (despite CTR)
pem.Write([]byte{byte(i)})
}
if rounds > 0 {
// Encrypt
buf := top.Bytes()
stream.XORKeyStream(buf, buf)
}
pem.Pop()
pem.Close()
return pem.Output()
}
func pubSSH(pub, uid []byte) []byte {
pem := newPEM(false)
pem.String("ssh-ed25519")
pem.Bytes(pub)
pem.Close()
out := bytes.NewBufferString("ssh-ed25519 ")
out.Write(pem.Output())
out.WriteByte(0x20)
out.Write(uid)
out.WriteByte(0x0a)
return out.Bytes()
}
// wrapper is an io.Writer filter that inserts regular hard line breaks.
type wrapper struct {
w io.Writer
max int
count int
}
func (w *wrapper) Write(p []byte) (int, error) {
for len(p) > 0 {
if w.count == w.max {
if _, err := w.w.Write([]byte{10}); err != nil {
return 0, err
}
w.count = 0
}
left := w.max - w.count
var line []byte
if len(p) > left {
line = p[:left]
} else {
line = p
}
p = p[len(line):]
w.count += len(line)
_, err := w.w.Write(line)
if err != nil {
return 0, err
}
}
return len(p), nil
}