noiseExplorerClient uses the Noise Explorer implementation of Noise IK to handshake as a Noise client on conn, transmit payload, and read+return a payload from the peer.
(conn net.Conn, controlKey key.MachinePublic, machineKey key.MachinePrivate, payload []byte)
| 115 | // IK to handshake as a Noise client on conn, transmit payload, and |
| 116 | // read+return a payload from the peer. |
| 117 | func noiseExplorerClient(conn net.Conn, controlKey key.MachinePublic, machineKey key.MachinePrivate, payload []byte) ([]byte, error) { |
| 118 | var mk keypair |
| 119 | copy(mk.private_key[:], machineKey.UntypedBytes()) |
| 120 | copy(mk.public_key[:], machineKey.Public().UntypedBytes()) |
| 121 | var peerKey [32]byte |
| 122 | copy(peerKey[:], controlKey.UntypedBytes()) |
| 123 | session := InitSession(true, protocolVersionPrologue(testProtocolVersion), mk, peerKey) |
| 124 | |
| 125 | _, msg1 := SendMessage(&session, nil) |
| 126 | var hdr [initiationHeaderLen]byte |
| 127 | binary.BigEndian.PutUint16(hdr[:2], testProtocolVersion) |
| 128 | hdr[2] = msgTypeInitiation |
| 129 | binary.BigEndian.PutUint16(hdr[3:5], 96) |
| 130 | if _, err := conn.Write(hdr[:]); err != nil { |
| 131 | return nil, err |
| 132 | } |
| 133 | if _, err := conn.Write(msg1.ne[:]); err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | if _, err := conn.Write(msg1.ns); err != nil { |
| 137 | return nil, err |
| 138 | } |
| 139 | if _, err := conn.Write(msg1.ciphertext); err != nil { |
| 140 | return nil, err |
| 141 | } |
| 142 | |
| 143 | var buf [1024]byte |
| 144 | if _, err := io.ReadFull(conn, buf[:51]); err != nil { |
| 145 | return nil, err |
| 146 | } |
| 147 | // ignore the header for this test, we're only checking the noise |
| 148 | // implementation. |
| 149 | msg2 := messagebuffer{ |
| 150 | ciphertext: buf[35:51], |
| 151 | } |
| 152 | copy(msg2.ne[:], buf[3:35]) |
| 153 | _, p, valid := RecvMessage(&session, &msg2) |
| 154 | if !valid { |
| 155 | return nil, errors.New("handshake failed") |
| 156 | } |
| 157 | if len(p) != 0 { |
| 158 | return nil, errors.New("non-empty payload") |
| 159 | } |
| 160 | |
| 161 | _, msg3 := SendMessage(&session, payload) |
| 162 | hdr[0] = msgTypeRecord |
| 163 | binary.BigEndian.PutUint16(hdr[1:3], uint16(len(msg3.ciphertext))) |
| 164 | if _, err := conn.Write(hdr[:3]); err != nil { |
| 165 | return nil, err |
| 166 | } |
| 167 | if _, err := conn.Write(msg3.ciphertext); err != nil { |
| 168 | return nil, err |
| 169 | } |
| 170 | |
| 171 | if _, err := io.ReadFull(conn, buf[:3]); err != nil { |
| 172 | return nil, err |
| 173 | } |
| 174 | // Ignore all of the header except the payload length |
no test coverage detected
searching dependent graphs…