| 1 |
package handshake |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"math" |
| 6 |
|
| 7 |
"google.golang.org/protobuf/encoding/protowire" |
| 8 |
) |
| 9 |
|
| 10 |
var ( |
| 11 |
errInvalidHandshakeMessage = errors.New("invalid handshake message") |
| 12 |
errInvalidHandshakeDetails = errors.New("invalid handshake details") |
| 13 |
) |
| 14 |
|
| 15 |
// Payload represents the decoded fields of a handshake message. |
| 16 |
// Wire format is protobuf-compatible with NebulaHandshake{Details: NebulaHandshakeDetails{...}}. |
| 17 |
type Payload struct { |
| 18 |
Cert []byte |
| 19 |
InitiatorIndex uint32 |
| 20 |
ResponderIndex uint32 |
| 21 |
Time uint64 |
| 22 |
CertVersion uint32 |
| 23 |
} |
| 24 |
|
| 25 |
// Proto field numbers for NebulaHandshakeDetails |
| 26 |
const ( |
| 27 |
fieldCert = 1 // bytes |
| 28 |
fieldInitiatorIndex = 2 // uint32 |
| 29 |
fieldResponderIndex = 3 // uint32 |
| 30 |
fieldTime = 5 // uint64 |
| 31 |
fieldCertVersion = 8 // uint32 |
| 32 |
) |
| 33 |
|
| 34 |
// MarshalPayload encodes a handshake payload in protobuf wire format compatible |
| 35 |
// with NebulaHandshake{Details: NebulaHandshakeDetails{...}}. |
| 36 |
// Returns out (which may be nil), with the marshalled Payload appended to it. |
| 37 |
func MarshalPayload(out []byte, p Payload) []byte { |
| 38 |
var details []byte |
| 39 |
|
| 40 |
if len(p.Cert) > 0 { |
| 41 |
details = protowire.AppendTag(details, fieldCert, protowire.BytesType) |
| 42 |
details = protowire.AppendBytes(details, p.Cert) |
| 43 |
} |
| 44 |
if p.InitiatorIndex != 0 { |
| 45 |
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.VarintType) |
| 46 |
details = protowire.AppendVarint(details, uint64(p.InitiatorIndex)) |
| 47 |
} |
| 48 |
if p.ResponderIndex != 0 { |
| 49 |
details = protowire.AppendTag(details, fieldResponderIndex, protowire.VarintType) |
| 50 |
details = protowire.AppendVarint(details, uint64(p.ResponderIndex)) |
| 51 |
} |
| 52 |
if p.Time != 0 { |
| 53 |
details = protowire.AppendTag(details, fieldTime, protowire.VarintType) |
| 54 |
details = protowire.AppendVarint(details, p.Time) |
| 55 |
} |
| 56 |
if p.CertVersion != 0 { |
| 57 |
details = protowire.AppendTag(details, fieldCertVersion, protowire.VarintType) |
| 58 |
details = protowire.AppendVarint(details, uint64(p.CertVersion)) |
| 59 |
} |
| 60 |
|
| 61 |
out = protowire.AppendTag(out, 1, protowire.BytesType) |
| 62 |
out = protowire.AppendBytes(out, details) |
| 63 |
|
| 64 |
return out |
| 65 |
} |
| 66 |
|
| 67 |
// UnmarshalPayload decodes a protobuf-encoded NebulaHandshake message. |
| 68 |
func UnmarshalPayload(b []byte) (Payload, error) { |
| 69 |
var p Payload |
| 70 |
|
| 71 |
for len(b) > 0 { |
| 72 |
num, typ, n := protowire.ConsumeTag(b) |
| 73 |
if n < 0 { |
| 74 |
return p, errInvalidHandshakeMessage |
| 75 |
} |
| 76 |
b = b[n:] |
| 77 |
|
| 78 |
switch { |
| 79 |
case num == 1 && typ == protowire.BytesType: |
| 80 |
details, n := protowire.ConsumeBytes(b) |
| 81 |
if n < 0 { |
| 82 |
return p, errInvalidHandshakeMessage |
| 83 |
} |
| 84 |
b = b[n:] |
| 85 |
if err := unmarshalPayloadDetails(&p, details); err != nil { |
| 86 |
return p, err |
| 87 |
} |
| 88 |
default: |
| 89 |
n := protowire.ConsumeFieldValue(num, typ, b) |
| 90 |
if n < 0 { |
| 91 |
return p, errInvalidHandshakeMessage |
| 92 |
} |
| 93 |
b = b[n:] |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return p, nil |
| 98 |
} |
| 99 |
|
| 100 |
func unmarshalPayloadDetails(p *Payload, b []byte) error { |
| 101 |
for len(b) > 0 { |
| 102 |
num, typ, n := protowire.ConsumeTag(b) |
| 103 |
if n < 0 { |
| 104 |
return errInvalidHandshakeDetails |
| 105 |
} |
| 106 |
b = b[n:] |
| 107 |
|
| 108 |
// For known field numbers, reject any non-matching wire type as a |
| 109 |
// hard error rather than silently skipping. The caller will catch |
| 110 |
// missing-field cases downstream, but a wire-type mismatch on a tag |
| 111 |
// we know is a peer protocol violation worth flagging here. |
| 112 |
// Repeated occurrences of a singular field follow proto3 last-wins. |
| 113 |
switch num { |
| 114 |
case fieldCert: |
| 115 |
if typ != protowire.BytesType { |
| 116 |
return errInvalidHandshakeDetails |
| 117 |
} |
| 118 |
v, n := protowire.ConsumeBytes(b) |
| 119 |
if n < 0 { |
| 120 |
return errInvalidHandshakeDetails |
| 121 |
} |
| 122 |
p.Cert = append([]byte(nil), v...) |
| 123 |
b = b[n:] |
| 124 |
case fieldInitiatorIndex: |
| 125 |
if typ != protowire.VarintType { |
| 126 |
return errInvalidHandshakeDetails |
| 127 |
} |
| 128 |
v, n := protowire.ConsumeVarint(b) |
| 129 |
if n < 0 || v > math.MaxUint32 { |
| 130 |
return errInvalidHandshakeDetails |
| 131 |
} |
| 132 |
p.InitiatorIndex = uint32(v) |
| 133 |
b = b[n:] |
| 134 |
case fieldResponderIndex: |
| 135 |
if typ != protowire.VarintType { |
| 136 |
return errInvalidHandshakeDetails |
| 137 |
} |
| 138 |
v, n := protowire.ConsumeVarint(b) |
| 139 |
if n < 0 || v > math.MaxUint32 { |
| 140 |
return errInvalidHandshakeDetails |
| 141 |
} |
| 142 |
p.ResponderIndex = uint32(v) |
| 143 |
b = b[n:] |
| 144 |
case fieldTime: |
| 145 |
if typ != protowire.VarintType { |
| 146 |
return errInvalidHandshakeDetails |
| 147 |
} |
| 148 |
v, n := protowire.ConsumeVarint(b) |
| 149 |
if n < 0 { |
| 150 |
return errInvalidHandshakeDetails |
| 151 |
} |
| 152 |
p.Time = v |
| 153 |
b = b[n:] |
| 154 |
case fieldCertVersion: |
| 155 |
if typ != protowire.VarintType { |
| 156 |
return errInvalidHandshakeDetails |
| 157 |
} |
| 158 |
v, n := protowire.ConsumeVarint(b) |
| 159 |
if n < 0 || v > math.MaxUint32 { |
| 160 |
return errInvalidHandshakeDetails |
| 161 |
} |
| 162 |
p.CertVersion = uint32(v) |
| 163 |
b = b[n:] |
| 164 |
default: |
| 165 |
n := protowire.ConsumeFieldValue(num, typ, b) |
| 166 |
if n < 0 { |
| 167 |
return errInvalidHandshakeDetails |
| 168 |
} |
| 169 |
b = b[n:] |
| 170 |
} |
| 171 |
} |
| 172 |
return nil |
| 173 |
} |