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