Skip to content

Commit 3d4788c

Browse files
authored
security: crypto/rand ShuffleChromeTLSExtensions (#286)
`math/rand` might not be randomly seeded as documented on some platforms, including wasm. Signed-off-by: Gaukas Wang <[email protected]>
1 parent d2768e4 commit 3d4788c

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

u_parrots.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ package tls
66

77
import (
88
"crypto/ecdh"
9+
crand "crypto/rand"
910
"crypto/sha256"
1011
"encoding/binary"
1112
"errors"
1213
"fmt"
1314
"io"
15+
"math"
16+
"math/big"
1417
"math/rand"
1518
"sort"
1619
"strconv"
@@ -2558,12 +2561,24 @@ func ShuffleChromeTLSExtensions(exts []TLSExtension) []TLSExtension {
25582561
}
25592562

25602563
// Shuffle other extensions
2561-
rand.Shuffle(len(exts), func(i, j int) {
2562-
if skipShuf(i, exts) || skipShuf(j, exts) {
2563-
return // do not shuffle some of the extensions
2564-
}
2565-
exts[i], exts[j] = exts[j], exts[i]
2566-
})
2564+
randInt64, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
2565+
if err != nil {
2566+
// warning: random could be deterministic
2567+
rand.Shuffle(len(exts), func(i, j int) {
2568+
if skipShuf(i, exts) || skipShuf(j, exts) {
2569+
return // do not shuffle some of the extensions
2570+
}
2571+
exts[i], exts[j] = exts[j], exts[i]
2572+
})
2573+
fmt.Println("Warning: failed to use a cryptographically secure random number generator. The shuffle can be deterministic.")
2574+
} else {
2575+
rand.New(rand.NewSource(randInt64.Int64())).Shuffle(len(exts), func(i, j int) {
2576+
if skipShuf(i, exts) || skipShuf(j, exts) {
2577+
return // do not shuffle some of the extensions
2578+
}
2579+
exts[i], exts[j] = exts[j], exts[i]
2580+
})
2581+
}
25672582

25682583
return exts
25692584
}

0 commit comments

Comments
 (0)