forked from francoismichel/ssh3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
334 lines (291 loc) · 7.62 KB
/
util.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package util
import (
"context"
"crypto"
"crypto/ed25519"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"math/big"
"net"
"os"
"strings"
"sync"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/crypto/cryptobyte"
cryptobyte_asn1 "golang.org/x/crypto/cryptobyte/asn1"
)
type UnknownSSHPubkeyType struct {
pubkey crypto.PublicKey
}
func (m UnknownSSHPubkeyType) Error() string {
return fmt.Sprintf("unknown signing method: %T", m.pubkey)
}
// copied from "net/http/internal/ascii"
// EqualFold is strings.EqualFold, ASCII only. It reports whether s and t
// are equal, ASCII-case-insensitively.
func EqualFold(s, t string) bool {
if len(s) != len(t) {
return false
}
for i := 0; i < len(s); i++ {
if lower(s[i]) != lower(t[i]) {
return false
}
}
return true
}
// lower returns the ASCII lowercase version of b.
func lower(b byte) byte {
if 'A' <= b && b <= 'Z' {
return b + ('a' - 'A')
}
return b
}
func ConfigureLogger(logLevel string) {
switch strings.ToLower(logLevel) {
case "trace":
zerolog.SetGlobalLevel(zerolog.TraceLevel)
case "debug":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "info":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "warning":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "error":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
default:
zerolog.SetGlobalLevel(zerolog.WarnLevel)
}
}
// Accept queue copied from https://github.com/quic-go/webtransport-go/blob/master/session.go
type AcceptQueue[T any] struct {
mx sync.Mutex
// The channel is used to notify consumers (via Chan) about new incoming items.
// Needs to be buffered to preserve the notification if an item is enqueued
// between a call to Next and to Chan.
c chan struct{}
// Contains all the streams waiting to be accepted.
// There's no explicit limit to the length of the queue, but it is implicitly
// limited by the stream flow control provided by QUIC.
queue []T
}
func NewAcceptQueue[T any]() *AcceptQueue[T] {
return &AcceptQueue[T]{c: make(chan struct{}, 1)}
}
func (q *AcceptQueue[T]) Add(str T) {
q.mx.Lock()
q.queue = append(q.queue, str)
q.mx.Unlock()
select {
case q.c <- struct{}{}:
default:
}
}
func (q *AcceptQueue[T]) Next() T {
q.mx.Lock()
defer q.mx.Unlock()
if len(q.queue) == 0 {
return *new(T)
}
str := q.queue[0]
q.queue = q.queue[1:]
return str
}
func (q *AcceptQueue[T]) Chan() <-chan struct{} { return q.c }
type DatagramsQueue struct {
c chan []byte
}
func NewDatagramsQueue(len uint64) *DatagramsQueue {
return &DatagramsQueue{c: make(chan []byte, len)}
}
// returns true if added, false otherwise
func (q *DatagramsQueue) Add(datagram []byte) bool {
select {
case q.c <- datagram:
return true
default:
return false
}
}
// returns nil if added, the context closing error (context.Cause(ctx)) otherwise
func (q *DatagramsQueue) WaitAdd(ctx context.Context, datagram []byte) error {
select {
case q.c <- datagram:
return nil
case <-ctx.Done():
return context.Cause(ctx)
}
}
func (q *DatagramsQueue) Next() []byte {
select {
case datagram := <-q.c:
return datagram
default:
return nil
}
}
func (q *DatagramsQueue) WaitNext(ctx context.Context) ([]byte, error) {
select {
case datagram := <-q.c:
return datagram, nil
case <-ctx.Done():
return nil, context.Cause(ctx)
}
}
func JWTSigningMethodFromCryptoPubkey(pubkey crypto.PublicKey) (jwt.SigningMethod, error) {
log.Debug().Type("SigningMethodType", pubkey).Msg("fetching singing method from crypto.PublicKey")
switch pubkey.(type) {
case *rsa.PublicKey:
log.
Trace().
Type("SigningMethodType", pubkey).
Str("FoundSigningMethod", "RSA").
Msg("found public key type")
return jwt.SigningMethodRS256, nil
case ed25519.PublicKey:
log.
Trace().
Type("SigningMethodType", pubkey).
Str("FoundSigningMethod", "ED25519").
Msg("found public key type")
return jwt.SigningMethodEdDSA, nil
default:
log.
Error().
Type("SigningMethodType", pubkey).
Str("FoundSigningMethod", "unknown").
Msg("did not find public key type")
return nil, UnknownSSHPubkeyType{pubkey: pubkey}
}
}
func Sha256Fingerprint(in []byte) string {
hash := sha256.Sum256(in)
return base64.StdEncoding.EncodeToString(hash[:])
}
func getSANExtension(cert *x509.Certificate) []byte {
oidExtensionSubjectAltName := []int{2, 5, 29, 17}
for _, e := range cert.Extensions {
if e.Id.Equal(oidExtensionSubjectAltName) {
return e.Value
}
}
return nil
}
func forEachSAN(der cryptobyte.String, callback func(tag int, data []byte) error) error {
if !der.ReadASN1(&der, cryptobyte_asn1.SEQUENCE) {
return errors.New("x509: invalid subject alternative names")
}
for !der.Empty() {
var san cryptobyte.String
var tag cryptobyte_asn1.Tag
if !der.ReadAnyASN1(&san, &tag) {
return errors.New("x509: invalid subject alternative name")
}
if err := callback(int(tag^0x80), san); err != nil {
return err
}
}
return nil
}
// returns true whether the certificat contains a SubjectAltName extension
// with at least one IP address record
func CertHasIPSANs(cert *x509.Certificate) (bool, error) {
SANExtension := getSANExtension(cert)
if SANExtension == nil {
return false, nil
}
nameTypeIP := 7
var ipAddresses []net.IP
err := forEachSAN(SANExtension, func(tag int, data []byte) error {
switch tag {
case nameTypeIP:
switch len(data) {
case net.IPv4len, net.IPv6len:
ipAddresses = append(ipAddresses, data)
default:
return fmt.Errorf("x509: cannot parse IP address of length %d", len(data))
}
default:
}
return nil
})
return len(ipAddresses) > 0, err
}
func GenerateKey() (crypto.PublicKey, crypto.PrivateKey, error) {
return ed25519.GenerateKey(rand.Reader)
}
func GenerateCert(priv crypto.PrivateKey) (*x509.Certificate, error) {
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, err
}
cert := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"SSH3Organization"},
},
NotBefore: time.Now(),
NotAfter: time.Now().Add(time.Hour * 24 * 365 * 10),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
DNSNames: []string{"*", "selfsigned.ssh3"},
IsCA: true,
}
return &cert, nil
}
func DumpCertAndKeyToFiles(cert *x509.Certificate, pubkey crypto.PublicKey, privkey crypto.PrivateKey, certPath, keyPath string) error {
certBytes, err := x509.CreateCertificate(rand.Reader, cert, cert, pubkey, privkey)
if err != nil {
return err
}
certFile, err := os.Create(certPath)
if err != nil {
return err
}
defer certFile.Close()
err = pem.Encode(certFile, &pem.Block{Type: "CERTIFICATE", Bytes: certBytes})
if err != nil {
return err
}
keyFile, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer keyFile.Close()
keyBytes, err := x509.MarshalPKCS8PrivateKey(privkey)
if err != nil {
return err
}
err = pem.Encode(keyFile, &pem.Block{Type: "PRIVATE KEY", Bytes: keyBytes})
if err != nil {
return err
}
return nil
}
type SyncMap[K comparable, V any] struct {
inner sync.Map
}
func NewSyncMap[K comparable, V any]() SyncMap[K, V] {
return SyncMap[K, V]{
inner: sync.Map{},
}
}
func (m *SyncMap[K, V]) Get(key K) (V, bool) {
val, ok := m.inner.Load(key)
return val.(V), ok
}
func (m *SyncMap[K, V]) Insert(key K, val V) {
m.inner.Store(key, val)
}