|
|
@@ -5,13 +5,12 @@ import (
|
|
5
|
5
|
"encoding/base64"
|
|
6
|
6
|
"encoding/hex"
|
|
7
|
7
|
"fmt"
|
|
8
|
|
- "strings"
|
|
9
|
8
|
)
|
|
10
|
9
|
|
|
11
|
10
|
const (
|
|
12
|
11
|
SecretKeyLength = 16
|
|
13
|
12
|
|
|
14
|
|
- secretFakeTLSFirstByte byte = 238
|
|
|
13
|
+ secretFakeTLSFirstByte byte = 0xee
|
|
15
|
14
|
)
|
|
16
|
15
|
|
|
17
|
16
|
var secretEmptyKey [SecretKeyLength]byte
|
|
|
@@ -35,31 +34,26 @@ func (s *Secret) UnmarshalText(data []byte) error {
|
|
35
|
34
|
return ErrSecretEmpty
|
|
36
|
35
|
}
|
|
37
|
36
|
|
|
38
|
|
- var (
|
|
39
|
|
- decoded []byte
|
|
40
|
|
- err error
|
|
41
|
|
- )
|
|
42
|
|
-
|
|
43
|
|
- if strings.HasPrefix(text, "ee") {
|
|
44
|
|
- decoded, err = hex.DecodeString(strings.TrimPrefix(text, "ee"))
|
|
45
|
|
- }
|
|
46
|
|
-
|
|
47
|
|
- if err != nil || len(decoded) <= SecretKeyLength {
|
|
|
37
|
+ decoded, err := hex.DecodeString(text)
|
|
|
38
|
+ if err != nil {
|
|
48
|
39
|
decoded, err = base64.RawURLEncoding.DecodeString(text)
|
|
|
40
|
+ }
|
|
49
|
41
|
|
|
50
|
|
- if err != nil {
|
|
51
|
|
- return fmt.Errorf("incorrect secret format: %w", err)
|
|
52
|
|
- }
|
|
|
42
|
+ if err != nil {
|
|
|
43
|
+ return fmt.Errorf("incorrect secret format: %w", err)
|
|
|
44
|
+ }
|
|
53
|
45
|
|
|
54
|
|
- if len(decoded) <= SecretKeyLength {
|
|
55
|
|
- return fmt.Errorf("secret has incorrect length %d", len(text))
|
|
56
|
|
- }
|
|
|
46
|
+ if len(decoded) < 2 { // nolint: gomnd // we need at least 1 byte here
|
|
|
47
|
+ return fmt.Errorf("secret is truncated, length=%d", len(decoded))
|
|
|
48
|
+ }
|
|
57
|
49
|
|
|
58
|
|
- if decoded[0] != secretFakeTLSFirstByte {
|
|
59
|
|
- return fmt.Errorf("incorrect first byte: %v", decoded[0])
|
|
60
|
|
- }
|
|
|
50
|
+ if decoded[0] != secretFakeTLSFirstByte {
|
|
|
51
|
+ return fmt.Errorf("incorrect first byte of secret: %#x", decoded[0])
|
|
|
52
|
+ }
|
|
61
|
53
|
|
|
62
|
|
- decoded = decoded[1:]
|
|
|
54
|
+ decoded = decoded[1:]
|
|
|
55
|
+ if len(decoded) < SecretKeyLength {
|
|
|
56
|
+ return fmt.Errorf("secret has incorrect length %d", len(decoded))
|
|
63
|
57
|
}
|
|
64
|
58
|
|
|
65
|
59
|
copy(s.Key[:], decoded[:SecretKeyLength])
|