| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package wire |
| 16 |
| 17 | import ( |
| 18 | "crypto/rand" |
| 19 | "crypto/sha256" |
| 20 | "encoding/binary" |
| 21 | "encoding/json" |
| 22 | "fmt" |
| 23 | "hash" |
| 24 | "runtime" |
| 25 |
| 26 | "golang.org/x/sys/cpu" |
| 27 | ) |
| 28 |
| 29 | const ( |
| 30 | AEADAlgorithmAES256GCM = "aes-256-gcm" |
| 31 | AEADAlgorithmXChaCha20Poly1305 = "xchacha20-poly1305" |
| 32 |
| 33 | CryptoRandomSize = 32 |
| 34 |
| 35 | cryptoTranscriptLabel = "frp wire v2 crypto transcript" |
| 36 | ) |
| 37 |
| 38 | var supportedAEADAlgorithms = []string{ |
| 39 | AEADAlgorithmAES256GCM, |
| 40 | AEADAlgorithmXChaCha20Poly1305, |
| 41 | } |
| 42 |
| 43 | type CryptoContext struct { |
| 44 | Algorithm string |
| 45 | TranscriptHash []byte |
| 46 | } |
| 47 |
| 48 | func NewClientHello(bootstrap BootstrapInfo) (ClientHello, error) { |
| 49 | clientRandom, err := newCryptoRandom() |
| 50 | if err != nil { |
| 51 | return ClientHello{}, err |
| 52 | } |
| 53 | return clientHelloWithCryptoRandom(bootstrap, clientRandom), nil |
| 54 | } |
| 55 |
| 56 | func NewServerHello(clientHello ClientHello) (ServerHello, error) { |
| 57 | if err := ValidateClientHello(clientHello); err != nil { |
| 58 | return ServerHello{}, err |
| 59 | } |
| 60 | algorithm, ok := SelectAEADAlgorithm(clientHello.Capabilities.Crypto.Algorithms) |
| 61 | if !ok { |
| 62 | return ServerHello{}, fmt.Errorf("no supported crypto algorithm") |
| 63 | } |
| 64 | serverRandom, err := newCryptoRandom() |
| 65 | if err != nil { |
| 66 | return ServerHello{}, err |
| 67 | } |
| 68 | return ServerHello{ |
| 69 | Selected: ServerSelection{ |
| 70 | Message: MessageSelection{ |
| 71 | Codec: MessageCodecJSON, |
| 72 | }, |
| 73 | Crypto: CryptoSelection{ |
| 74 | Algorithm: algorithm, |
| 75 | ServerRandom: serverRandom, |
| 76 | }, |
| 77 | }, |
| 78 | }, nil |
| 79 | } |
| 80 |
| 81 | func ValidateCryptoCapabilities(c CryptoCapabilities) error { |
| 82 | if len(c.ClientRandom) != CryptoRandomSize { |
| 83 | return fmt.Errorf("invalid crypto client random length %d, want %d", len(c.ClientRandom), CryptoRandomSize) |
| 84 | } |
| 85 | if _, ok := SelectAEADAlgorithm(c.Algorithms); !ok { |
| 86 | return fmt.Errorf("no supported crypto algorithm") |
| 87 | } |
| 88 | return nil |
| 89 | } |
| 90 |
| 91 | func ValidateServerHelloForClient(clientHello ClientHello, serverHello ServerHello) error { |
| 92 | if serverHello.Selected.Message.Codec != MessageCodecJSON { |
| 93 | return fmt.Errorf("unsupported selected message codec: %s", serverHello.Selected.Message.Codec) |
| 94 | } |
| 95 | cryptoSelection := serverHello.Selected.Crypto |
| 96 | if !IsSupportedAEADAlgorithm(cryptoSelection.Algorithm) { |
| 97 | return fmt.Errorf("unknown selected crypto algorithm: %s", cryptoSelection.Algorithm) |
| 98 | } |
| 99 | if !Supports(clientHello.Capabilities.Crypto.Algorithms, cryptoSelection.Algorithm) { |
| 100 | return fmt.Errorf("selected crypto algorithm was not advertised by client: %s", cryptoSelection.Algorithm) |
| 101 | } |
| 102 | if len(cryptoSelection.ServerRandom) != CryptoRandomSize { |
| 103 | return fmt.Errorf("invalid crypto server random length %d, want %d", len(cryptoSelection.ServerRandom), CryptoRandomSize) |
| 104 | } |
| 105 | return nil |
| 106 | } |
| 107 |
| 108 | func NewCryptoContext(algorithm string, clientHelloPayload, serverHelloPayload []byte) *CryptoContext { |
| 109 | return &CryptoContext{ |
| 110 | Algorithm: algorithm, |
| 111 | TranscriptHash: HashCryptoTranscript(clientHelloPayload, serverHelloPayload), |
| 112 | } |
| 113 | } |
| 114 |
| 115 | func NewClientCryptoContext(clientHelloPayload, serverHelloPayload []byte) (*CryptoContext, error) { |
| 116 | var clientHello ClientHello |
| 117 | if err := json.Unmarshal(clientHelloPayload, &clientHello); err != nil { |
| 118 | return nil, fmt.Errorf("decode ClientHello transcript: %w", err) |
| 119 | } |
| 120 | var serverHello ServerHello |
| 121 | if err := json.Unmarshal(serverHelloPayload, &serverHello); err != nil { |
| 122 | return nil, fmt.Errorf("decode ServerHello transcript: %w", err) |
| 123 | } |
| 124 | if err := ValidateServerHelloForClient(clientHello, serverHello); err != nil { |
| 125 | return nil, err |
| 126 | } |
| 127 |
| 128 | return NewCryptoContext(serverHello.Selected.Crypto.Algorithm, clientHelloPayload, serverHelloPayload), nil |
| 129 | } |
| 130 |
| 131 | func HashCryptoTranscript(clientHelloPayload, serverHelloPayload []byte) []byte { |
| 132 | h := sha256.New() |
| 133 | _, _ = h.Write([]byte(cryptoTranscriptLabel)) |
| 134 | writeCryptoTranscriptPart(h, "client hello", clientHelloPayload) |
| 135 | writeCryptoTranscriptPart(h, "server hello", serverHelloPayload) |
| 136 | return h.Sum(nil) |
| 137 | } |
| 138 |
| 139 | func writeCryptoTranscriptPart(h hash.Hash, label string, payload []byte) { |
| 140 | var length [8]byte |
| 141 | binary.BigEndian.PutUint64(length[:], uint64(len(payload))) |
| 142 | _, _ = h.Write([]byte{0}) |
| 143 | _, _ = h.Write([]byte(label)) |
| 144 | _, _ = h.Write([]byte{0}) |
| 145 | _, _ = h.Write(length[:]) |
| 146 | _, _ = h.Write(payload) |
| 147 | } |
| 148 |
| 149 | func PreferredAEADAlgorithms() []string { |
| 150 | if hasFastAESGCM() { |
| 151 | return []string{AEADAlgorithmAES256GCM, AEADAlgorithmXChaCha20Poly1305} |
| 152 | } |
| 153 | return []string{AEADAlgorithmXChaCha20Poly1305, AEADAlgorithmAES256GCM} |
| 154 | } |
| 155 |
| 156 | func SelectAEADAlgorithm(clientAlgorithms []string) (string, bool) { |
| 157 | for _, algorithm := range clientAlgorithms { |
| 158 | if IsSupportedAEADAlgorithm(algorithm) { |
| 159 | return algorithm, true |
| 160 | } |
| 161 | } |
| 162 | return "", false |
| 163 | } |
| 164 |
| 165 | func IsSupportedAEADAlgorithm(algorithm string) bool { |
| 166 | return Supports(supportedAEADAlgorithms, algorithm) |
| 167 | } |
| 168 |
| 169 | func newCryptoRandom() ([]byte, error) { |
| 170 | b := make([]byte, CryptoRandomSize) |
| 171 | if _, err := rand.Read(b); err != nil { |
| 172 | return nil, fmt.Errorf("generate crypto random: %w", err) |
| 173 | } |
| 174 | return b, nil |
| 175 | } |
| 176 |
| 177 | func hasFastAESGCM() bool { |
| 178 | switch runtime.GOARCH { |
| 179 | case "amd64": |
| 180 | return cpu.X86.HasAES && |
| 181 | cpu.X86.HasPCLMULQDQ && |
| 182 | cpu.X86.HasSSE41 && |
| 183 | cpu.X86.HasSSSE3 |
| 184 | case "arm64": |
| 185 | return cpu.ARM64.HasAES && cpu.ARM64.HasPMULL |
| 186 | case "s390x": |
| 187 | return cpu.S390X.HasAES && |
| 188 | cpu.S390X.HasAESCTR && |
| 189 | cpu.S390X.HasGHASH |
| 190 | case "ppc64", "ppc64le": |
| 191 | // Go's ppc64/ppc64le port targets POWER8+, which has AES instructions; |
| 192 | // x/sys/cpu does not expose a PPC64 AES feature flag. |
| 193 | return true |
| 194 | default: |
| 195 | return false |
| 196 | } |
| 197 | } |