messages.go 17 KB

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