messages.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ssh
  5. import (
  6. "bytes"
  7. "encoding/binary"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "math/big"
  12. "reflect"
  13. "strconv"
  14. )
  15. // These are SSH message type numbers. They are scattered around several
  16. // documents but many were taken from [SSH-PARAMETERS].
  17. const (
  18. msgIgnore = 2
  19. msgUnimplemented = 3
  20. msgDebug = 4
  21. msgNewKeys = 21
  22. // Standard authentication messages
  23. msgUserAuthSuccess = 52
  24. msgUserAuthBanner = 53
  25. )
  26. // SSH messages:
  27. //
  28. // These structures mirror the wire format of the corresponding SSH messages.
  29. // They are marshaled using reflection with the marshal and unmarshal functions
  30. // in this file. The only wrinkle is that a final member of type []byte with a
  31. // ssh tag of "rest" receives the remainder of a packet when unmarshaling.
  32. // See RFC 4253, section 11.1.
  33. const msgDisconnect = 1
  34. // disconnectMsg is the message that signals a disconnect. It is also
  35. // the error type returned from mux.Wait()
  36. type disconnectMsg struct {
  37. Reason uint32 `sshtype:"1"`
  38. Message string
  39. Language string
  40. }
  41. func (d *disconnectMsg) Error() string {
  42. return fmt.Sprintf("ssh: disconnect, reason %d: %s", d.Reason, d.Message)
  43. }
  44. // See RFC 4253, section 7.1.
  45. const msgKexInit = 20
  46. type kexInitMsg struct {
  47. Cookie [16]byte `sshtype:"20"`
  48. KexAlgos []string
  49. ServerHostKeyAlgos []string
  50. CiphersClientServer []string
  51. CiphersServerClient []string
  52. MACsClientServer []string
  53. MACsServerClient []string
  54. CompressionClientServer []string
  55. CompressionServerClient []string
  56. LanguagesClientServer []string
  57. LanguagesServerClient []string
  58. FirstKexFollows bool
  59. Reserved uint32
  60. }
  61. // See RFC 4253, section 8.
  62. // Diffie-Helman
  63. const msgKexDHInit = 30
  64. type kexDHInitMsg struct {
  65. X *big.Int `sshtype:"30"`
  66. }
  67. const msgKexECDHInit = 30
  68. type kexECDHInitMsg struct {
  69. ClientPubKey []byte `sshtype:"30"`
  70. }
  71. const msgKexECDHReply = 31
  72. type kexECDHReplyMsg struct {
  73. HostKey []byte `sshtype:"31"`
  74. EphemeralPubKey []byte
  75. Signature []byte
  76. }
  77. const msgKexDHReply = 31
  78. type kexDHReplyMsg struct {
  79. HostKey []byte `sshtype:"31"`
  80. Y *big.Int
  81. Signature []byte
  82. }
  83. // See RFC 4253, section 10.
  84. const msgServiceRequest = 5
  85. type serviceRequestMsg struct {
  86. Service string `sshtype:"5"`
  87. }
  88. // See RFC 4253, section 10.
  89. const msgServiceAccept = 6
  90. type serviceAcceptMsg struct {
  91. Service string `sshtype:"6"`
  92. }
  93. // See RFC 4252, section 5.
  94. const msgUserAuthRequest = 50
  95. type userAuthRequestMsg struct {
  96. User string `sshtype:"50"`
  97. Service string
  98. Method string
  99. Payload []byte `ssh:"rest"`
  100. }
  101. // Used for debug printouts of packets.
  102. type userAuthSuccessMsg struct {
  103. }
  104. // See RFC 4252, section 5.1
  105. const msgUserAuthFailure = 51
  106. type userAuthFailureMsg struct {
  107. Methods []string `sshtype:"51"`
  108. PartialSuccess bool
  109. }
  110. // See RFC 4256, section 3.2
  111. const msgUserAuthInfoRequest = 60
  112. const msgUserAuthInfoResponse = 61
  113. type userAuthInfoRequestMsg struct {
  114. User string `sshtype:"60"`
  115. Instruction string
  116. DeprecatedLanguage string
  117. NumPrompts uint32
  118. Prompts []byte `ssh:"rest"`
  119. }
  120. // See RFC 4254, section 5.1.
  121. const msgChannelOpen = 90
  122. type channelOpenMsg struct {
  123. ChanType string `sshtype:"90"`
  124. PeersId uint32
  125. PeersWindow uint32
  126. MaxPacketSize uint32
  127. TypeSpecificData []byte `ssh:"rest"`
  128. }
  129. const msgChannelExtendedData = 95
  130. const msgChannelData = 94
  131. // Used for debug print outs of packets.
  132. type channelDataMsg struct {
  133. PeersId uint32 `sshtype:"94"`
  134. Length uint32
  135. Rest []byte `ssh:"rest"`
  136. }
  137. // See RFC 4254, section 5.1.
  138. const msgChannelOpenConfirm = 91
  139. type channelOpenConfirmMsg struct {
  140. PeersId uint32 `sshtype:"91"`
  141. MyId uint32
  142. MyWindow uint32
  143. MaxPacketSize uint32
  144. TypeSpecificData []byte `ssh:"rest"`
  145. }
  146. // See RFC 4254, section 5.1.
  147. const msgChannelOpenFailure = 92
  148. type channelOpenFailureMsg struct {
  149. PeersId uint32 `sshtype:"92"`
  150. Reason RejectionReason
  151. Message string
  152. Language string
  153. }
  154. const msgChannelRequest = 98
  155. type channelRequestMsg struct {
  156. PeersId uint32 `sshtype:"98"`
  157. Request string
  158. WantReply bool
  159. RequestSpecificData []byte `ssh:"rest"`
  160. }
  161. // See RFC 4254, section 5.4.
  162. const msgChannelSuccess = 99
  163. type channelRequestSuccessMsg struct {
  164. PeersId uint32 `sshtype:"99"`
  165. }
  166. // See RFC 4254, section 5.4.
  167. const msgChannelFailure = 100
  168. type channelRequestFailureMsg struct {
  169. PeersId uint32 `sshtype:"100"`
  170. }
  171. // See RFC 4254, section 5.3
  172. const msgChannelClose = 97
  173. type channelCloseMsg struct {
  174. PeersId uint32 `sshtype:"97"`
  175. }
  176. // See RFC 4254, section 5.3
  177. const msgChannelEOF = 96
  178. type channelEOFMsg struct {
  179. PeersId uint32 `sshtype:"96"`
  180. }
  181. // See RFC 4254, section 4
  182. const msgGlobalRequest = 80
  183. type globalRequestMsg struct {
  184. Type string `sshtype:"80"`
  185. WantReply bool
  186. Data []byte `ssh:"rest"`
  187. }
  188. // See RFC 4254, section 4
  189. const msgRequestSuccess = 81
  190. type globalRequestSuccessMsg struct {
  191. Data []byte `ssh:"rest" sshtype:"81"`
  192. }
  193. // See RFC 4254, section 4
  194. const msgRequestFailure = 82
  195. type globalRequestFailureMsg struct {
  196. Data []byte `ssh:"rest" sshtype:"82"`
  197. }
  198. // See RFC 4254, section 5.2
  199. const msgChannelWindowAdjust = 93
  200. type windowAdjustMsg struct {
  201. PeersId uint32 `sshtype:"93"`
  202. AdditionalBytes uint32
  203. }
  204. // See RFC 4252, section 7
  205. const msgUserAuthPubKeyOk = 60
  206. type userAuthPubKeyOkMsg struct {
  207. Algo string `sshtype:"60"`
  208. PubKey []byte
  209. }
  210. // typeTag returns the type byte for the given type. The type should
  211. // be struct.
  212. func typeTag(structType reflect.Type) byte {
  213. var tag byte
  214. var tagStr string
  215. tagStr = structType.Field(0).Tag.Get("sshtype")
  216. i, err := strconv.Atoi(tagStr)
  217. if err == nil {
  218. tag = byte(i)
  219. }
  220. return tag
  221. }
  222. func fieldError(t reflect.Type, field int, problem string) error {
  223. if problem != "" {
  224. problem = ": " + problem
  225. }
  226. return fmt.Errorf("ssh: unmarshal error for field %s of type %s%s", t.Field(field).Name, t.Name(), problem)
  227. }
  228. var errShortRead = errors.New("ssh: short read")
  229. // Unmarshal parses data in SSH wire format into a structure. The out
  230. // argument should be a pointer to struct. If the first member of the
  231. // struct has the "sshtype" tag set to a number in decimal, the packet
  232. // must start that number. In case of error, Unmarshal returns a
  233. // ParseError or UnexpectedMessageError.
  234. func Unmarshal(data []byte, out interface{}) error {
  235. v := reflect.ValueOf(out).Elem()
  236. structType := v.Type()
  237. expectedType := typeTag(structType)
  238. if len(data) == 0 {
  239. return parseError(expectedType)
  240. }
  241. if expectedType > 0 {
  242. if data[0] != expectedType {
  243. return unexpectedMessageError(expectedType, data[0])
  244. }
  245. data = data[1:]
  246. }
  247. var ok bool
  248. for i := 0; i < v.NumField(); i++ {
  249. field := v.Field(i)
  250. t := field.Type()
  251. switch t.Kind() {
  252. case reflect.Bool:
  253. if len(data) < 1 {
  254. return errShortRead
  255. }
  256. field.SetBool(data[0] != 0)
  257. data = data[1:]
  258. case reflect.Array:
  259. if t.Elem().Kind() != reflect.Uint8 {
  260. return fieldError(structType, i, "array of unsupported type")
  261. }
  262. if len(data) < t.Len() {
  263. return errShortRead
  264. }
  265. for j, n := 0, t.Len(); j < n; j++ {
  266. field.Index(j).Set(reflect.ValueOf(data[j]))
  267. }
  268. data = data[t.Len():]
  269. case reflect.Uint64:
  270. var u64 uint64
  271. if u64, data, ok = parseUint64(data); !ok {
  272. return errShortRead
  273. }
  274. field.SetUint(u64)
  275. case reflect.Uint32:
  276. var u32 uint32
  277. if u32, data, ok = parseUint32(data); !ok {
  278. return errShortRead
  279. }
  280. field.SetUint(uint64(u32))
  281. case reflect.Uint8:
  282. if len(data) < 1 {
  283. return errShortRead
  284. }
  285. field.SetUint(uint64(data[0]))
  286. data = data[1:]
  287. case reflect.String:
  288. var s []byte
  289. if s, data, ok = parseString(data); !ok {
  290. return fieldError(structType, i, "")
  291. }
  292. field.SetString(string(s))
  293. case reflect.Slice:
  294. switch t.Elem().Kind() {
  295. case reflect.Uint8:
  296. if structType.Field(i).Tag.Get("ssh") == "rest" {
  297. field.Set(reflect.ValueOf(data))
  298. data = nil
  299. } else {
  300. var s []byte
  301. if s, data, ok = parseString(data); !ok {
  302. return errShortRead
  303. }
  304. field.Set(reflect.ValueOf(s))
  305. }
  306. case reflect.String:
  307. var nl []string
  308. if nl, data, ok = parseNameList(data); !ok {
  309. return errShortRead
  310. }
  311. field.Set(reflect.ValueOf(nl))
  312. default:
  313. return fieldError(structType, i, "slice of unsupported type")
  314. }
  315. case reflect.Ptr:
  316. if t == bigIntType {
  317. var n *big.Int
  318. if n, data, ok = parseInt(data); !ok {
  319. return errShortRead
  320. }
  321. field.Set(reflect.ValueOf(n))
  322. } else {
  323. return fieldError(structType, i, "pointer to unsupported type")
  324. }
  325. default:
  326. return fieldError(structType, i, "unsupported type")
  327. }
  328. }
  329. if len(data) != 0 {
  330. return parseError(expectedType)
  331. }
  332. return nil
  333. }
  334. // Marshal serializes the message in msg to SSH wire format. The msg
  335. // argument should be a struct or pointer to struct. If the first
  336. // member has the "sshtype" tag set to a number in decimal, that
  337. // number is prepended to the result. If the last of member has the
  338. // "ssh" tag set to "rest", its contents are appended to the output.
  339. func Marshal(msg interface{}) []byte {
  340. out := make([]byte, 0, 64)
  341. return marshalStruct(out, msg)
  342. }
  343. func marshalStruct(out []byte, msg interface{}) []byte {
  344. v := reflect.Indirect(reflect.ValueOf(msg))
  345. msgType := typeTag(v.Type())
  346. if msgType > 0 {
  347. out = append(out, msgType)
  348. }
  349. for i, n := 0, v.NumField(); i < n; i++ {
  350. field := v.Field(i)
  351. switch t := field.Type(); t.Kind() {
  352. case reflect.Bool:
  353. var v uint8
  354. if field.Bool() {
  355. v = 1
  356. }
  357. out = append(out, v)
  358. case reflect.Array:
  359. if t.Elem().Kind() != reflect.Uint8 {
  360. panic(fmt.Sprintf("array of non-uint8 in field %d: %T", i, field.Interface()))
  361. }
  362. for j, l := 0, t.Len(); j < l; j++ {
  363. out = append(out, uint8(field.Index(j).Uint()))
  364. }
  365. case reflect.Uint32:
  366. out = appendU32(out, uint32(field.Uint()))
  367. case reflect.Uint64:
  368. out = appendU64(out, uint64(field.Uint()))
  369. case reflect.Uint8:
  370. out = append(out, uint8(field.Uint()))
  371. case reflect.String:
  372. s := field.String()
  373. out = appendInt(out, len(s))
  374. out = append(out, s...)
  375. case reflect.Slice:
  376. switch t.Elem().Kind() {
  377. case reflect.Uint8:
  378. if v.Type().Field(i).Tag.Get("ssh") != "rest" {
  379. out = appendInt(out, field.Len())
  380. }
  381. out = append(out, field.Bytes()...)
  382. case reflect.String:
  383. offset := len(out)
  384. out = appendU32(out, 0)
  385. if n := field.Len(); n > 0 {
  386. for j := 0; j < n; j++ {
  387. f := field.Index(j)
  388. if j != 0 {
  389. out = append(out, ',')
  390. }
  391. out = append(out, f.String()...)
  392. }
  393. // overwrite length value
  394. binary.BigEndian.PutUint32(out[offset:], uint32(len(out)-offset-4))
  395. }
  396. default:
  397. panic(fmt.Sprintf("slice of unknown type in field %d: %T", i, field.Interface()))
  398. }
  399. case reflect.Ptr:
  400. if t == bigIntType {
  401. var n *big.Int
  402. nValue := reflect.ValueOf(&n)
  403. nValue.Elem().Set(field)
  404. needed := intLength(n)
  405. oldLength := len(out)
  406. if cap(out)-len(out) < needed {
  407. newOut := make([]byte, len(out), 2*(len(out)+needed))
  408. copy(newOut, out)
  409. out = newOut
  410. }
  411. out = out[:oldLength+needed]
  412. marshalInt(out[oldLength:], n)
  413. } else {
  414. panic(fmt.Sprintf("pointer to unknown type in field %d: %T", i, field.Interface()))
  415. }
  416. }
  417. }
  418. return out
  419. }
  420. var bigOne = big.NewInt(1)
  421. func parseString(in []byte) (out, rest []byte, ok bool) {
  422. if len(in) < 4 {
  423. return
  424. }
  425. length := binary.BigEndian.Uint32(in)
  426. in = in[4:]
  427. if uint32(len(in)) < length {
  428. return
  429. }
  430. out = in[:length]
  431. rest = in[length:]
  432. ok = true
  433. return
  434. }
  435. var (
  436. comma = []byte{','}
  437. emptyNameList = []string{}
  438. )
  439. func parseNameList(in []byte) (out []string, rest []byte, ok bool) {
  440. contents, rest, ok := parseString(in)
  441. if !ok {
  442. return
  443. }
  444. if len(contents) == 0 {
  445. out = emptyNameList
  446. return
  447. }
  448. parts := bytes.Split(contents, comma)
  449. out = make([]string, len(parts))
  450. for i, part := range parts {
  451. out[i] = string(part)
  452. }
  453. return
  454. }
  455. func parseInt(in []byte) (out *big.Int, rest []byte, ok bool) {
  456. contents, rest, ok := parseString(in)
  457. if !ok {
  458. return
  459. }
  460. out = new(big.Int)
  461. if len(contents) > 0 && contents[0]&0x80 == 0x80 {
  462. // This is a negative number
  463. notBytes := make([]byte, len(contents))
  464. for i := range notBytes {
  465. notBytes[i] = ^contents[i]
  466. }
  467. out.SetBytes(notBytes)
  468. out.Add(out, bigOne)
  469. out.Neg(out)
  470. } else {
  471. // Positive number
  472. out.SetBytes(contents)
  473. }
  474. ok = true
  475. return
  476. }
  477. func parseUint32(in []byte) (uint32, []byte, bool) {
  478. if len(in) < 4 {
  479. return 0, nil, false
  480. }
  481. return binary.BigEndian.Uint32(in), in[4:], true
  482. }
  483. func parseUint64(in []byte) (uint64, []byte, bool) {
  484. if len(in) < 8 {
  485. return 0, nil, false
  486. }
  487. return binary.BigEndian.Uint64(in), in[8:], true
  488. }
  489. func intLength(n *big.Int) int {
  490. length := 4 /* length bytes */
  491. if n.Sign() < 0 {
  492. nMinus1 := new(big.Int).Neg(n)
  493. nMinus1.Sub(nMinus1, bigOne)
  494. bitLen := nMinus1.BitLen()
  495. if bitLen%8 == 0 {
  496. // The number will need 0xff padding
  497. length++
  498. }
  499. length += (bitLen + 7) / 8
  500. } else if n.Sign() == 0 {
  501. // A zero is the zero length string
  502. } else {
  503. bitLen := n.BitLen()
  504. if bitLen%8 == 0 {
  505. // The number will need 0x00 padding
  506. length++
  507. }
  508. length += (bitLen + 7) / 8
  509. }
  510. return length
  511. }
  512. func marshalUint32(to []byte, n uint32) []byte {
  513. binary.BigEndian.PutUint32(to, n)
  514. return to[4:]
  515. }
  516. func marshalUint64(to []byte, n uint64) []byte {
  517. binary.BigEndian.PutUint64(to, n)
  518. return to[8:]
  519. }
  520. func marshalInt(to []byte, n *big.Int) []byte {
  521. lengthBytes := to
  522. to = to[4:]
  523. length := 0
  524. if n.Sign() < 0 {
  525. // A negative number has to be converted to two's-complement
  526. // form. So we'll subtract 1 and invert. If the
  527. // most-significant-bit isn't set then we'll need to pad the
  528. // beginning with 0xff in order to keep the number negative.
  529. nMinus1 := new(big.Int).Neg(n)
  530. nMinus1.Sub(nMinus1, bigOne)
  531. bytes := nMinus1.Bytes()
  532. for i := range bytes {
  533. bytes[i] ^= 0xff
  534. }
  535. if len(bytes) == 0 || bytes[0]&0x80 == 0 {
  536. to[0] = 0xff
  537. to = to[1:]
  538. length++
  539. }
  540. nBytes := copy(to, bytes)
  541. to = to[nBytes:]
  542. length += nBytes
  543. } else if n.Sign() == 0 {
  544. // A zero is the zero length string
  545. } else {
  546. bytes := n.Bytes()
  547. if len(bytes) > 0 && bytes[0]&0x80 != 0 {
  548. // We'll have to pad this with a 0x00 in order to
  549. // stop it looking like a negative number.
  550. to[0] = 0
  551. to = to[1:]
  552. length++
  553. }
  554. nBytes := copy(to, bytes)
  555. to = to[nBytes:]
  556. length += nBytes
  557. }
  558. lengthBytes[0] = byte(length >> 24)
  559. lengthBytes[1] = byte(length >> 16)
  560. lengthBytes[2] = byte(length >> 8)
  561. lengthBytes[3] = byte(length)
  562. return to
  563. }
  564. func writeInt(w io.Writer, n *big.Int) {
  565. length := intLength(n)
  566. buf := make([]byte, length)
  567. marshalInt(buf, n)
  568. w.Write(buf)
  569. }
  570. func writeString(w io.Writer, s []byte) {
  571. var lengthBytes [4]byte
  572. lengthBytes[0] = byte(len(s) >> 24)
  573. lengthBytes[1] = byte(len(s) >> 16)
  574. lengthBytes[2] = byte(len(s) >> 8)
  575. lengthBytes[3] = byte(len(s))
  576. w.Write(lengthBytes[:])
  577. w.Write(s)
  578. }
  579. func stringLength(n int) int {
  580. return 4 + n
  581. }
  582. func marshalString(to []byte, s []byte) []byte {
  583. to[0] = byte(len(s) >> 24)
  584. to[1] = byte(len(s) >> 16)
  585. to[2] = byte(len(s) >> 8)
  586. to[3] = byte(len(s))
  587. to = to[4:]
  588. copy(to, s)
  589. return to[len(s):]
  590. }
  591. var bigIntType = reflect.TypeOf((*big.Int)(nil))
  592. // Decode a packet into its corresponding message.
  593. func decode(packet []byte) (interface{}, error) {
  594. var msg interface{}
  595. switch packet[0] {
  596. case msgDisconnect:
  597. msg = new(disconnectMsg)
  598. case msgServiceRequest:
  599. msg = new(serviceRequestMsg)
  600. case msgServiceAccept:
  601. msg = new(serviceAcceptMsg)
  602. case msgKexInit:
  603. msg = new(kexInitMsg)
  604. case msgKexDHInit:
  605. msg = new(kexDHInitMsg)
  606. case msgKexDHReply:
  607. msg = new(kexDHReplyMsg)
  608. case msgUserAuthRequest:
  609. msg = new(userAuthRequestMsg)
  610. case msgUserAuthSuccess:
  611. return new(userAuthSuccessMsg), nil
  612. case msgUserAuthFailure:
  613. msg = new(userAuthFailureMsg)
  614. case msgUserAuthPubKeyOk:
  615. msg = new(userAuthPubKeyOkMsg)
  616. case msgGlobalRequest:
  617. msg = new(globalRequestMsg)
  618. case msgRequestSuccess:
  619. msg = new(globalRequestSuccessMsg)
  620. case msgRequestFailure:
  621. msg = new(globalRequestFailureMsg)
  622. case msgChannelOpen:
  623. msg = new(channelOpenMsg)
  624. case msgChannelData:
  625. msg = new(channelDataMsg)
  626. case msgChannelOpenConfirm:
  627. msg = new(channelOpenConfirmMsg)
  628. case msgChannelOpenFailure:
  629. msg = new(channelOpenFailureMsg)
  630. case msgChannelWindowAdjust:
  631. msg = new(windowAdjustMsg)
  632. case msgChannelEOF:
  633. msg = new(channelEOFMsg)
  634. case msgChannelClose:
  635. msg = new(channelCloseMsg)
  636. case msgChannelRequest:
  637. msg = new(channelRequestMsg)
  638. case msgChannelSuccess:
  639. msg = new(channelRequestSuccessMsg)
  640. case msgChannelFailure:
  641. msg = new(channelRequestFailureMsg)
  642. default:
  643. return nil, unexpectedMessageError(0, packet[0])
  644. }
  645. if err := Unmarshal(packet, msg); err != nil {
  646. return nil, err
  647. }
  648. return msg, nil
  649. }