| 1 |
// Copyright 2026 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
//go:build go1.26 |
| 15 |
|
| 16 |
package server |
| 17 |
|
| 18 |
import ( |
| 19 |
"crypto/fips140" |
| 20 |
"crypto/sha1" |
| 21 |
"encoding/base64" |
| 22 |
) |
| 23 |
|
| 24 |
func wsAllowedFIPS() bool { |
| 25 |
// As SHA-1 is only used for Sec-WebSocket-Key and Sec-WebSocket-Accept, we |
| 26 |
// can continue to allow it in FIPS builds as long as they are built with |
| 27 |
// Go 1.26 or later only. |
| 28 |
return true |
| 29 |
} |
| 30 |
|
| 31 |
// Concatenate the key sent by the client with the GUID, then computes the SHA1 hash |
| 32 |
// and returns it as a based64 encoded string. |
| 33 |
func wsAcceptKey(key string) string { |
| 34 |
var r []byte |
| 35 |
fips140.WithoutEnforcement(func() { |
| 36 |
h := sha1.New() |
| 37 |
h.Write([]byte(key)) |
| 38 |
h.Write(wsGUID) |
| 39 |
r = h.Sum(nil) |
| 40 |
}) |
| 41 |
return base64.StdEncoding.EncodeToString(r) |
| 42 |
} |