literal.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 packet
  5. import (
  6. "encoding/binary"
  7. "io"
  8. )
  9. // LiteralData represents an encrypted file. See RFC 4880, section 5.9.
  10. type LiteralData struct {
  11. IsBinary bool
  12. FileName string
  13. Time uint32 // Unix epoch time. Either creation time or modification time. 0 means undefined.
  14. Body io.Reader
  15. }
  16. // ForEyesOnly returns whether the contents of the LiteralData have been marked
  17. // as especially sensitive.
  18. func (l *LiteralData) ForEyesOnly() bool {
  19. return l.FileName == "_CONSOLE"
  20. }
  21. func (l *LiteralData) parse(r io.Reader) (err error) {
  22. var buf [256]byte
  23. _, err = readFull(r, buf[:2])
  24. if err != nil {
  25. return
  26. }
  27. l.IsBinary = buf[0] == 'b'
  28. fileNameLen := int(buf[1])
  29. _, err = readFull(r, buf[:fileNameLen])
  30. if err != nil {
  31. return
  32. }
  33. l.FileName = string(buf[:fileNameLen])
  34. _, err = readFull(r, buf[:4])
  35. if err != nil {
  36. return
  37. }
  38. l.Time = binary.BigEndian.Uint32(buf[:4])
  39. l.Body = r
  40. return
  41. }
  42. // SerializeLiteral serializes a literal data packet to w and returns a
  43. // WriteCloser to which the data itself can be written and which MUST be closed
  44. // on completion. The fileName is truncated to 255 bytes.
  45. func SerializeLiteral(w io.WriteCloser, isBinary bool, fileName string, time uint32) (plaintext io.WriteCloser, err error) {
  46. var buf [4]byte
  47. buf[0] = 't'
  48. if isBinary {
  49. buf[0] = 'b'
  50. }
  51. if len(fileName) > 255 {
  52. fileName = fileName[:255]
  53. }
  54. buf[1] = byte(len(fileName))
  55. inner, err := serializeStreamHeader(w, packetTypeLiteralData)
  56. if err != nil {
  57. return
  58. }
  59. _, err = inner.Write(buf[:2])
  60. if err != nil {
  61. return
  62. }
  63. _, err = inner.Write([]byte(fileName))
  64. if err != nil {
  65. return
  66. }
  67. binary.BigEndian.PutUint32(buf[:], time)
  68. _, err = inner.Write(buf[:])
  69. if err != nil {
  70. return
  71. }
  72. plaintext = inner
  73. return
  74. }