Debian Codegraph @

openpgp/armor/armor.go

raw
1 // Copyright 2010 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 armor implements OpenPGP ASCII Armor, see RFC 4880. OpenPGP Armor is
6 // very similar to PEM except that it has an additional CRC checksum.
7 package armor // import "github.com/ProtonMail/go-crypto/openpgp/armor"
8
9 import (
10 "bufio"
11 "bytes"
12 "encoding/base64"
13 "io"
14
15 "github.com/ProtonMail/go-crypto/openpgp/errors"
16 )
17
18 // A Block represents an OpenPGP armored structure.
19 //
20 // The encoded form is:
21 //
22 // -----BEGIN Type-----
23 // Headers
24 //
25 // base64-encoded Bytes
26 // '=' base64 encoded checksum (optional) not checked anymore
27 // -----END Type-----
28 //
29 // where Headers is a possibly empty sequence of Key: Value lines.
30 //
31 // Since the armored data can be very large, this package presents a streaming
32 // interface.
33 type Block struct {
34 Type string // The type, taken from the preamble (i.e. "PGP SIGNATURE").
35 Header map[string]string // Optional headers.
36 Body io.Reader // A Reader from which the contents can be read
37 lReader lineReader
38 oReader openpgpReader
39 }
40
41 var ArmorCorrupt error = errors.StructuralError("armor invalid")
42
43 var armorStart = []byte("-----BEGIN ")
44 var armorEnd = []byte("-----END ")
45 var armorEndOfLine = []byte("-----")
46
47 // lineReader wraps a line based reader. It watches for the end of an armor block
48 type lineReader struct {
49 in *bufio.Reader
50 buf []byte
51 eof bool
52 }
53
54 func (l *lineReader) Read(p []byte) (n int, err error) {
55 if l.eof {
56 return 0, io.EOF
57 }
58
59 if len(l.buf) > 0 {
60 n = copy(p, l.buf)
61 l.buf = l.buf[n:]
62 return
63 }
64
65 line, isPrefix, err := l.in.ReadLine()
66 if err != nil {
67 return
68 }
69 if isPrefix {
70 return 0, ArmorCorrupt
71 }
72 // Trim the line to remove any whitespace
73 line = bytes.TrimSpace(line)
74
75 if bytes.HasPrefix(line, armorEnd) {
76 l.eof = true
77 return 0, io.EOF
78 }
79
80 if len(line) == 5 && line[0] == '=' {
81 // This is the checksum line
82 // Don't check the checksum
83
84 l.eof = true
85 return 0, io.EOF
86 }
87
88 if len(line) > 96 {
89 return 0, ArmorCorrupt
90 }
91
92 n = copy(p, line)
93 bytesToSave := len(line) - n
94 if bytesToSave > 0 {
95 if cap(l.buf) < bytesToSave {
96 l.buf = make([]byte, 0, bytesToSave)
97 }
98 l.buf = l.buf[0:bytesToSave]
99 copy(l.buf, line[n:])
100 }
101
102 return
103 }
104
105 // openpgpReader passes Read calls to the underlying base64 decoder.
106 type openpgpReader struct {
107 lReader *lineReader
108 b64Reader io.Reader
109 }
110
111 func (r *openpgpReader) Read(p []byte) (n int, err error) {
112 n, err = r.b64Reader.Read(p)
113 return
114 }
115
116 // Decode reads a PGP armored block from the given Reader. It will ignore
117 // leading garbage. If it doesn't find a block, it will return nil, io.EOF. The
118 // given Reader is not usable after calling this function: an arbitrary amount
119 // of data may have been read past the end of the block.
120 func Decode(in io.Reader) (p *Block, err error) {
121 r := bufio.NewReaderSize(in, 100)
122 var line []byte
123 ignoreNext := false
124
125 TryNextBlock:
126 p = nil
127
128 // Skip leading garbage
129 for {
130 ignoreThis := ignoreNext
131 line, ignoreNext, err = r.ReadLine()
132 if err != nil {
133 return
134 }
135 if ignoreNext || ignoreThis {
136 continue
137 }
138 line = bytes.TrimSpace(line)
139 if len(line) > len(armorStart)+len(armorEndOfLine) && bytes.HasPrefix(line, armorStart) {
140 break
141 }
142 }
143
144 p = new(Block)
145 p.Type = string(line[len(armorStart) : len(line)-len(armorEndOfLine)])
146 p.Header = make(map[string]string)
147 nextIsContinuation := false
148 var lastKey string
149
150 // Read headers
151 for {
152 isContinuation := nextIsContinuation
153 line, nextIsContinuation, err = r.ReadLine()
154 if err != nil {
155 p = nil
156 return
157 }
158 if isContinuation {
159 p.Header[lastKey] += string(line)
160 continue
161 }
162 line = bytes.TrimSpace(line)
163 if len(line) == 0 {
164 break
165 }
166
167 i := bytes.Index(line, []byte(":"))
168 if i == -1 {
169 goto TryNextBlock
170 }
171 lastKey = string(line[:i])
172 var value string
173 if len(line) > i+2 {
174 value = string(line[i+2:])
175 }
176 p.Header[lastKey] = value
177 }
178
179 p.lReader.in = r
180 p.oReader.lReader = &p.lReader
181 p.oReader.b64Reader = base64.NewDecoder(base64.StdEncoding, &p.lReader)
182 p.Body = &p.oReader
183
184 return
185 }