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