-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add sub package tlsversion: manage tls version to stringer / crypto tls constant, with marshaller & unmarshaller
- Loading branch information
Showing
4 changed files
with
348 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/*********************************************************************************************************************** | ||
* | ||
* MIT License | ||
* | ||
* Copyright (c) 2022 Nicolas JUHEL | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
* | ||
**********************************************************************************************************************/ | ||
|
||
package tlsversion | ||
|
||
import ( | ||
"fmt" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func (v *Version) unmarshall(val []byte) error { | ||
*v = parseBytes(val) | ||
return nil | ||
} | ||
|
||
func (v Version) MarshalJSON() ([]byte, error) { | ||
t := v.String() | ||
b := make([]byte, 0, len(t)+2) | ||
b = append(b, '"') | ||
b = append(b, []byte(t)...) | ||
b = append(b, '"') | ||
return b, nil | ||
} | ||
|
||
func (v *Version) UnmarshalJSON(bytes []byte) error { | ||
return v.unmarshall(bytes) | ||
} | ||
|
||
func (v Version) MarshalYAML() (interface{}, error) { | ||
return []byte(v.String()), nil | ||
} | ||
|
||
func (v *Version) UnmarshalYAML(value *yaml.Node) error { | ||
return v.unmarshall([]byte(value.Value)) | ||
} | ||
|
||
func (v Version) MarshalTOML() ([]byte, error) { | ||
return []byte(v.String()), nil | ||
} | ||
|
||
func (v *Version) UnmarshalTOML(i interface{}) error { | ||
if p, k := i.([]byte); k { | ||
return v.unmarshall(p) | ||
} | ||
if p, k := i.(string); k { | ||
return v.unmarshall([]byte(p)) | ||
} | ||
return fmt.Errorf("size: value not in valid format") | ||
} | ||
|
||
func (v Version) MarshalText() ([]byte, error) { | ||
return []byte(v.String()), nil | ||
} | ||
|
||
func (v *Version) UnmarshalText(bytes []byte) error { | ||
return v.unmarshall(bytes) | ||
} | ||
|
||
func (v Version) MarshalCBOR() ([]byte, error) { | ||
return []byte(v.String()), nil | ||
} | ||
|
||
func (v *Version) UnmarshalCBOR(bytes []byte) error { | ||
return v.unmarshall(bytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/*********************************************************************************************************************** | ||
* | ||
* MIT License | ||
* | ||
* Copyright (c) 2022 Nicolas JUHEL | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
* | ||
**********************************************************************************************************************/ | ||
|
||
package tlsversion | ||
|
||
import ( | ||
"crypto/tls" | ||
"strings" | ||
) | ||
|
||
func (v Version) String() string { | ||
switch v { | ||
case VersionTLS10: | ||
return "TLS 1.0" | ||
case VersionTLS11: | ||
return "TLS 1.1" | ||
case VersionTLS12: | ||
return "TLS 1.2" | ||
case VersionTLS13: | ||
return "TLS 1.3" | ||
default: | ||
return "" | ||
} | ||
} | ||
|
||
func (v Version) Code() string { | ||
s := strings.ToLower(v.String()) | ||
s = strings.Replace(s, " ", "_", -1) | ||
return s | ||
} | ||
|
||
func (v Version) Uint16() uint16 { | ||
switch v { | ||
case VersionTLS10: | ||
return tls.VersionTLS10 | ||
case VersionTLS11: | ||
return tls.VersionTLS11 | ||
case VersionTLS12: | ||
return tls.VersionTLS12 | ||
case VersionTLS13: | ||
return tls.VersionTLS13 | ||
default: | ||
return 0 | ||
} | ||
} | ||
|
||
func (v Version) Uint() uint { | ||
return uint(v.Uint16()) | ||
} | ||
|
||
func (v Version) Uint32() uint32 { | ||
return uint32(v.Uint16()) | ||
} | ||
|
||
func (v Version) Uint64() uint64 { | ||
return uint64(v.Uint16()) | ||
} | ||
|
||
func (v Version) Int() int { | ||
return int(v.Uint16()) | ||
} | ||
|
||
func (v Version) Int32() int32 { | ||
return int32(v.Uint16()) | ||
} | ||
|
||
func (v Version) Int64() int64 { | ||
return int64(v.Uint16()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/*********************************************************************************************************************** | ||
* | ||
* MIT License | ||
* | ||
* Copyright (c) 2022 Nicolas JUHEL | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
* | ||
**********************************************************************************************************************/ | ||
|
||
package tlsversion | ||
|
||
import ( | ||
"crypto/tls" | ||
"strings" | ||
) | ||
|
||
type Version int | ||
|
||
const ( | ||
VersionUnknown Version = iota | ||
VersionTLS10 = Version(tls.VersionTLS10) | ||
VersionTLS11 = Version(tls.VersionTLS11) | ||
VersionTLS12 = Version(tls.VersionTLS12) | ||
VersionTLS13 = Version(tls.VersionTLS13) | ||
) | ||
|
||
func List() []Version { | ||
return []Version{ | ||
VersionTLS13, | ||
VersionTLS12, | ||
VersionTLS11, | ||
VersionTLS10, | ||
} | ||
} | ||
|
||
func ListHigh() []Version { | ||
return []Version{ | ||
VersionTLS13, | ||
VersionTLS12, | ||
} | ||
} | ||
|
||
func Parse(s string) Version { | ||
s = strings.ToLower(s) | ||
s = strings.Replace(s, "tls", "", -1) | ||
s = strings.Replace(s, "ssl", "", -1) | ||
s = strings.Replace(s, ".", "", -1) | ||
s = strings.Replace(s, " ", "", -1) | ||
s = strings.TrimSpace(s) | ||
|
||
switch { | ||
case strings.EqualFold(s, "1"): | ||
return VersionTLS10 | ||
case strings.EqualFold(s, "10"): | ||
return VersionTLS10 | ||
case strings.EqualFold(s, "11"): | ||
return VersionTLS11 | ||
case strings.EqualFold(s, "12"): | ||
return VersionTLS12 | ||
case strings.EqualFold(s, "13"): | ||
return VersionTLS13 | ||
default: | ||
return VersionUnknown | ||
} | ||
} | ||
|
||
func ParseInt(d int) Version { | ||
switch d { | ||
case tls.VersionTLS10: | ||
return VersionTLS10 | ||
case tls.VersionTLS11: | ||
return VersionTLS11 | ||
case tls.VersionTLS12: | ||
return VersionTLS12 | ||
case tls.VersionTLS13: | ||
return VersionTLS13 | ||
default: | ||
return VersionUnknown | ||
} | ||
} | ||
|
||
func parseBytes(p []byte) Version { | ||
return Parse(string(p)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/*********************************************************************************************************************** | ||
* | ||
* MIT License | ||
* | ||
* Copyright (c) 2022 Nicolas JUHEL | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
* | ||
**********************************************************************************************************************/ | ||
|
||
package tlsversion | ||
|
||
import ( | ||
"reflect" | ||
|
||
libmap "github.com/mitchellh/mapstructure" | ||
) | ||
|
||
func ViperDecoderHook() libmap.DecodeHookFuncType { | ||
return func(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) { | ||
var ( | ||
z = Version(0) | ||
t string | ||
k bool | ||
) | ||
|
||
// Check if the data type matches the expected one | ||
if from.Kind() != reflect.String { | ||
return data, nil | ||
} else if t, k = data.(string); !k { | ||
return data, nil | ||
} | ||
|
||
// Check if the target type matches the expected one | ||
if to != reflect.TypeOf(z) { | ||
return data, nil | ||
} | ||
|
||
// Format/decode/parse the data and return the new value | ||
if e := z.unmarshall([]byte(t)); e != nil { | ||
return nil, e | ||
} else { | ||
return z, nil | ||
} | ||
} | ||
} |