| 1 |
package mint |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/hex" |
| 6 |
"fmt" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
func VersionNegotiation(offered, supported []uint16) (bool, uint16) { |
| 11 |
for _, offeredVersion := range offered { |
| 12 |
for _, tls13Version := range supported { |
| 13 |
logf(logTypeHandshake, "[server] version offered by client [%04x] <> [%04x]", offeredVersion, tls13Version) |
| 14 |
if offeredVersion == tls13Version { |
| 15 |
|
| 16 |
|
| 17 |
return true, offeredVersion |
| 18 |
} |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
return false, 0 |
| 23 |
} |
| 24 |
|
| 25 |
func DHNegotiation(keyShares []KeyShareEntry, groups []NamedGroup) (bool, NamedGroup, []byte, []byte) { |
| 26 |
for _, share := range keyShares { |
| 27 |
for _, group := range groups { |
| 28 |
if group != share.Group { |
| 29 |
continue |
| 30 |
} |
| 31 |
|
| 32 |
pub, priv, err := newKeyShare(share.Group) |
| 33 |
if err != nil { |
| 34 |
|
| 35 |
continue |
| 36 |
} |
| 37 |
|
| 38 |
dhSecret, err := keyAgreement(share.Group, share.KeyExchange, priv) |
| 39 |
if err != nil { |
| 40 |
|
| 41 |
continue |
| 42 |
} |
| 43 |
|
| 44 |
return true, group, pub, dhSecret |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return false, 0, nil, nil |
| 49 |
} |
| 50 |
|
| 51 |
const ( |
| 52 |
ticketAgeTolerance uint32 = 5 * 1000 |
| 53 |
) |
| 54 |
|
| 55 |
func PSKNegotiation(identities []PSKIdentity, binders []PSKBinderEntry, context []byte, psks PreSharedKeyCache) (bool, int, *PreSharedKey, CipherSuiteParams, error) { |
| 56 |
logf(logTypeNegotiation, "Negotiating PSK offered=[%d] supported=[%d]", len(identities), psks.Size()) |
| 57 |
for i, id := range identities { |
| 58 |
identityHex := hex.EncodeToString(id.Identity) |
| 59 |
|
| 60 |
psk, ok := psks.Get(identityHex) |
| 61 |
if !ok { |
| 62 |
logf(logTypeNegotiation, "No PSK for identity %x", identityHex) |
| 63 |
continue |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
if psk.IsResumption { |
| 68 |
extTicketAge := id.ObfuscatedTicketAge - psk.TicketAgeAdd |
| 69 |
knownTicketAge := uint32(time.Since(psk.ReceivedAt) / time.Millisecond) |
| 70 |
ticketAgeDelta := knownTicketAge - extTicketAge |
| 71 |
if knownTicketAge < extTicketAge { |
| 72 |
ticketAgeDelta = extTicketAge - knownTicketAge |
| 73 |
} |
| 74 |
if ticketAgeDelta > ticketAgeTolerance { |
| 75 |
logf(logTypeNegotiation, "WARNING potential replay [%x]", psk.Identity) |
| 76 |
logf(logTypeNegotiation, "Ticket age exceeds tolerance |%d - %d| = [%d] > [%d]", |
| 77 |
extTicketAge, knownTicketAge, ticketAgeDelta, ticketAgeTolerance) |
| 78 |
return false, 0, nil, CipherSuiteParams{}, fmt.Errorf("WARNING Potential replay for identity %x", psk.Identity) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
params, ok := cipherSuiteMap[psk.CipherSuite] |
| 83 |
if !ok { |
| 84 |
err := fmt.Errorf("tls.cryptoinit: Unsupported ciphersuite from PSK [%04x]", psk.CipherSuite) |
| 85 |
return false, 0, nil, CipherSuiteParams{}, err |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
binderLabel := labelExternalBinder |
| 90 |
if psk.IsResumption { |
| 91 |
binderLabel = labelResumptionBinder |
| 92 |
} |
| 93 |
|
| 94 |
h0 := params.Hash.New().Sum(nil) |
| 95 |
zero := bytes.Repeat([]byte{0}, params.Hash.Size()) |
| 96 |
earlySecret := HkdfExtract(params.Hash, zero, psk.Key) |
| 97 |
binderKey := deriveSecret(params, earlySecret, binderLabel, h0) |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
ctxHash := params.Hash.New() |
| 102 |
ctxHash.Write(context) |
| 103 |
|
| 104 |
binder := computeFinishedData(params, binderKey, ctxHash.Sum(nil)) |
| 105 |
if !bytes.Equal(binder, binders[i].Binder) { |
| 106 |
logf(logTypeNegotiation, "Binder check failed for identity %x; [%x] != [%x]", psk.Identity, binder, binders[i].Binder) |
| 107 |
return false, 0, nil, CipherSuiteParams{}, fmt.Errorf("Binder check failed identity %x", psk.Identity) |
| 108 |
} |
| 109 |
|
| 110 |
logf(logTypeNegotiation, "Using PSK with identity %x", psk.Identity) |
| 111 |
return true, i, &psk, params, nil |
| 112 |
} |
| 113 |
|
| 114 |
logf(logTypeNegotiation, "Failed to find a usable PSK") |
| 115 |
return false, 0, nil, CipherSuiteParams{}, nil |
| 116 |
} |
| 117 |
|
| 118 |
func PSKModeNegotiation(canDoDH, canDoPSK bool, modes []PSKKeyExchangeMode) (bool, bool) { |
| 119 |
logf(logTypeNegotiation, "Negotiating PSK modes [%v] [%v] [%+v]", canDoDH, canDoPSK, modes) |
| 120 |
dhAllowed := false |
| 121 |
dhRequired := true |
| 122 |
for _, mode := range modes { |
| 123 |
dhAllowed = dhAllowed || (mode == PSKModeDHEKE) |
| 124 |
dhRequired = dhRequired && (mode == PSKModeDHEKE) |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
usingPSK := canDoPSK && (!dhRequired || canDoDH) && (len(modes) > 0) |
| 129 |
|
| 130 |
|
| 131 |
usingDH := canDoDH && (dhAllowed || !usingPSK) |
| 132 |
|
| 133 |
logf(logTypeNegotiation, "Results of PSK mode negotiation: usingDH=[%v] usingPSK=[%v]", usingDH, usingPSK) |
| 134 |
return usingDH, usingPSK |
| 135 |
} |
| 136 |
|
| 137 |
func CertificateSelection(serverName *string, signatureSchemes []SignatureScheme, certs []*Certificate) (*Certificate, SignatureScheme, error) { |
| 138 |
|
| 139 |
candidates := certs |
| 140 |
if serverName != nil { |
| 141 |
candidatesByName := []*Certificate{} |
| 142 |
for _, cert := range certs { |
| 143 |
for _, name := range cert.Chain[0].DNSNames { |
| 144 |
if len(*serverName) > 0 && name == *serverName { |
| 145 |
candidatesByName = append(candidatesByName, cert) |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if len(candidatesByName) == 0 { |
| 151 |
return nil, 0, fmt.Errorf("No certificates available for server name: %s", *serverName) |
| 152 |
} |
| 153 |
|
| 154 |
candidates = candidatesByName |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
for _, cert := range candidates { |
| 159 |
for _, scheme := range signatureSchemes { |
| 160 |
if !schemeValidForKey(scheme, cert.PrivateKey) { |
| 161 |
continue |
| 162 |
} |
| 163 |
|
| 164 |
return cert, scheme, nil |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return nil, 0, fmt.Errorf("No certificates compatible with signature schemes") |
| 169 |
} |
| 170 |
|
| 171 |
func EarlyDataNegotiation(usingPSK, gotEarlyData, allowEarlyData bool) (using bool, rejected bool) { |
| 172 |
using = gotEarlyData && usingPSK && allowEarlyData |
| 173 |
rejected = gotEarlyData && !using |
| 174 |
logf(logTypeNegotiation, "Early data negotiation (%v, %v, %v) => %v, %v", usingPSK, gotEarlyData, allowEarlyData, using, rejected) |
| 175 |
return |
| 176 |
} |
| 177 |
|
| 178 |
func CipherSuiteNegotiation(psk *PreSharedKey, offered, supported []CipherSuite) (CipherSuite, error) { |
| 179 |
for _, s1 := range offered { |
| 180 |
if psk != nil { |
| 181 |
if s1 == psk.CipherSuite { |
| 182 |
return s1, nil |
| 183 |
} |
| 184 |
continue |
| 185 |
} |
| 186 |
|
| 187 |
for _, s2 := range supported { |
| 188 |
if s1 == s2 { |
| 189 |
return s1, nil |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return 0, fmt.Errorf("No overlap between offered and supproted ciphersuites (psk? [%v])", psk != nil) |
| 195 |
} |
| 196 |
|
| 197 |
func ALPNNegotiation(psk *PreSharedKey, offered, supported []string) (string, error) { |
| 198 |
for _, p1 := range offered { |
| 199 |
if psk != nil { |
| 200 |
if p1 != psk.NextProto { |
| 201 |
continue |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
for _, p2 := range supported { |
| 206 |
if p1 == p2 { |
| 207 |
return p1, nil |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
var err error |
| 214 |
if psk != nil && psk.IsResumption && (len(offered) > 0) { |
| 215 |
err = fmt.Errorf("ALPN for PSK not provided") |
| 216 |
} |
| 217 |
return "", err |
| 218 |
} |