clearsign.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // Copyright 2012 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 clearsign generates and processes OpenPGP, clear-signed data. See
  5. // RFC 4880, section 7.
  6. //
  7. // Clearsigned messages are cryptographically signed, but the contents of the
  8. // message are kept in plaintext so that it can be read without special tools.
  9. package clearsign // import "golang.org/x/crypto/openpgp/clearsign"
  10. import (
  11. "bufio"
  12. "bytes"
  13. "crypto"
  14. "fmt"
  15. "hash"
  16. "io"
  17. "net/textproto"
  18. "strconv"
  19. "strings"
  20. "golang.org/x/crypto/openpgp/armor"
  21. "golang.org/x/crypto/openpgp/errors"
  22. "golang.org/x/crypto/openpgp/packet"
  23. )
  24. // A Block represents a clearsigned message. A signature on a Block can
  25. // be checked by passing Bytes into openpgp.CheckDetachedSignature.
  26. type Block struct {
  27. Headers textproto.MIMEHeader // Optional unverified Hash headers
  28. Plaintext []byte // The original message text
  29. Bytes []byte // The signed message
  30. ArmoredSignature *armor.Block // The signature block
  31. }
  32. // start is the marker which denotes the beginning of a clearsigned message.
  33. var start = []byte("\n-----BEGIN PGP SIGNED MESSAGE-----")
  34. // dashEscape is prefixed to any lines that begin with a hyphen so that they
  35. // can't be confused with endText.
  36. var dashEscape = []byte("- ")
  37. // endText is a marker which denotes the end of the message and the start of
  38. // an armored signature.
  39. var endText = []byte("-----BEGIN PGP SIGNATURE-----")
  40. // end is a marker which denotes the end of the armored signature.
  41. var end = []byte("\n-----END PGP SIGNATURE-----")
  42. var crlf = []byte("\r\n")
  43. var lf = byte('\n')
  44. // getLine returns the first \r\n or \n delineated line from the given byte
  45. // array. The line does not include the \r\n or \n. The remainder of the byte
  46. // array (also not including the new line bytes) is also returned and this will
  47. // always be smaller than the original argument.
  48. func getLine(data []byte) (line, rest []byte) {
  49. i := bytes.Index(data, []byte{'\n'})
  50. var j int
  51. if i < 0 {
  52. i = len(data)
  53. j = i
  54. } else {
  55. j = i + 1
  56. if i > 0 && data[i-1] == '\r' {
  57. i--
  58. }
  59. }
  60. return data[0:i], data[j:]
  61. }
  62. // Decode finds the first clearsigned message in data and returns it, as well as
  63. // the suffix of data which remains after the message. Any prefix data is
  64. // discarded.
  65. //
  66. // If no message is found, or if the message is invalid, Decode returns nil and
  67. // the whole data slice. The only allowed header type is Hash, and it is not
  68. // verified against the signature hash.
  69. func Decode(data []byte) (b *Block, rest []byte) {
  70. // start begins with a newline. However, at the very beginning of
  71. // the byte array, we'll accept the start string without it.
  72. rest = data
  73. if bytes.HasPrefix(data, start[1:]) {
  74. rest = rest[len(start)-1:]
  75. } else if i := bytes.Index(data, start); i >= 0 {
  76. rest = rest[i+len(start):]
  77. } else {
  78. return nil, data
  79. }
  80. // Consume the start line and check it does not have a suffix.
  81. suffix, rest := getLine(rest)
  82. if len(suffix) != 0 {
  83. return nil, data
  84. }
  85. var line []byte
  86. b = &Block{
  87. Headers: make(textproto.MIMEHeader),
  88. }
  89. // Next come a series of header lines.
  90. for {
  91. // This loop terminates because getLine's second result is
  92. // always smaller than its argument.
  93. if len(rest) == 0 {
  94. return nil, data
  95. }
  96. // An empty line marks the end of the headers.
  97. if line, rest = getLine(rest); len(line) == 0 {
  98. break
  99. }
  100. // Reject headers with control or Unicode characters.
  101. if i := bytes.IndexFunc(line, func(r rune) bool {
  102. return r < 0x20 || r > 0x7e
  103. }); i != -1 {
  104. return nil, data
  105. }
  106. i := bytes.Index(line, []byte{':'})
  107. if i == -1 {
  108. return nil, data
  109. }
  110. key, val := string(line[0:i]), string(line[i+1:])
  111. key = strings.TrimSpace(key)
  112. if key != "Hash" {
  113. return nil, data
  114. }
  115. val = strings.TrimSpace(val)
  116. b.Headers.Add(key, val)
  117. }
  118. firstLine := true
  119. for {
  120. start := rest
  121. line, rest = getLine(rest)
  122. if len(line) == 0 && len(rest) == 0 {
  123. // No armored data was found, so this isn't a complete message.
  124. return nil, data
  125. }
  126. if bytes.Equal(line, endText) {
  127. // Back up to the start of the line because armor expects to see the
  128. // header line.
  129. rest = start
  130. break
  131. }
  132. // The final CRLF isn't included in the hash so we don't write it until
  133. // we've seen the next line.
  134. if firstLine {
  135. firstLine = false
  136. } else {
  137. b.Bytes = append(b.Bytes, crlf...)
  138. }
  139. if bytes.HasPrefix(line, dashEscape) {
  140. line = line[2:]
  141. }
  142. line = bytes.TrimRight(line, " \t")
  143. b.Bytes = append(b.Bytes, line...)
  144. b.Plaintext = append(b.Plaintext, line...)
  145. b.Plaintext = append(b.Plaintext, lf)
  146. }
  147. // We want to find the extent of the armored data (including any newlines at
  148. // the end).
  149. i := bytes.Index(rest, end)
  150. if i == -1 {
  151. return nil, data
  152. }
  153. i += len(end)
  154. for i < len(rest) && (rest[i] == '\r' || rest[i] == '\n') {
  155. i++
  156. }
  157. armored := rest[:i]
  158. rest = rest[i:]
  159. var err error
  160. b.ArmoredSignature, err = armor.Decode(bytes.NewBuffer(armored))
  161. if err != nil {
  162. return nil, data
  163. }
  164. return b, rest
  165. }
  166. // A dashEscaper is an io.WriteCloser which processes the body of a clear-signed
  167. // message. The clear-signed message is written to buffered and a hash, suitable
  168. // for signing, is maintained in h.
  169. //
  170. // When closed, an armored signature is created and written to complete the
  171. // message.
  172. type dashEscaper struct {
  173. buffered *bufio.Writer
  174. hashers []hash.Hash // one per key in privateKeys
  175. hashType crypto.Hash
  176. toHash io.Writer // writes to all the hashes in hashers
  177. atBeginningOfLine bool
  178. isFirstLine bool
  179. whitespace []byte
  180. byteBuf []byte // a one byte buffer to save allocations
  181. privateKeys []*packet.PrivateKey
  182. config *packet.Config
  183. }
  184. func (d *dashEscaper) Write(data []byte) (n int, err error) {
  185. for _, b := range data {
  186. d.byteBuf[0] = b
  187. if d.atBeginningOfLine {
  188. // The final CRLF isn't included in the hash so we have to wait
  189. // until this point (the start of the next line) before writing it.
  190. if !d.isFirstLine {
  191. d.toHash.Write(crlf)
  192. }
  193. d.isFirstLine = false
  194. }
  195. // Any whitespace at the end of the line has to be removed so we
  196. // buffer it until we find out whether there's more on this line.
  197. if b == ' ' || b == '\t' || b == '\r' {
  198. d.whitespace = append(d.whitespace, b)
  199. d.atBeginningOfLine = false
  200. continue
  201. }
  202. if d.atBeginningOfLine {
  203. // At the beginning of a line, hyphens have to be escaped.
  204. if b == '-' {
  205. // The signature isn't calculated over the dash-escaped text so
  206. // the escape is only written to buffered.
  207. if _, err = d.buffered.Write(dashEscape); err != nil {
  208. return
  209. }
  210. d.toHash.Write(d.byteBuf)
  211. d.atBeginningOfLine = false
  212. } else if b == '\n' {
  213. // Nothing to do because we delay writing CRLF to the hash.
  214. } else {
  215. d.toHash.Write(d.byteBuf)
  216. d.atBeginningOfLine = false
  217. }
  218. if err = d.buffered.WriteByte(b); err != nil {
  219. return
  220. }
  221. } else {
  222. if b == '\n' {
  223. // We got a raw \n. Drop any trailing whitespace and write a
  224. // CRLF.
  225. d.whitespace = d.whitespace[:0]
  226. // We delay writing CRLF to the hash until the start of the
  227. // next line.
  228. if err = d.buffered.WriteByte(b); err != nil {
  229. return
  230. }
  231. d.atBeginningOfLine = true
  232. } else {
  233. // Any buffered whitespace wasn't at the end of the line so
  234. // we need to write it out.
  235. if len(d.whitespace) > 0 {
  236. d.toHash.Write(d.whitespace)
  237. if _, err = d.buffered.Write(d.whitespace); err != nil {
  238. return
  239. }
  240. d.whitespace = d.whitespace[:0]
  241. }
  242. d.toHash.Write(d.byteBuf)
  243. if err = d.buffered.WriteByte(b); err != nil {
  244. return
  245. }
  246. }
  247. }
  248. }
  249. n = len(data)
  250. return
  251. }
  252. func (d *dashEscaper) Close() (err error) {
  253. if !d.atBeginningOfLine {
  254. if err = d.buffered.WriteByte(lf); err != nil {
  255. return
  256. }
  257. }
  258. out, err := armor.Encode(d.buffered, "PGP SIGNATURE", nil)
  259. if err != nil {
  260. return
  261. }
  262. t := d.config.Now()
  263. for i, k := range d.privateKeys {
  264. sig := new(packet.Signature)
  265. sig.SigType = packet.SigTypeText
  266. sig.PubKeyAlgo = k.PubKeyAlgo
  267. sig.Hash = d.hashType
  268. sig.CreationTime = t
  269. sig.IssuerKeyId = &k.KeyId
  270. if err = sig.Sign(d.hashers[i], k, d.config); err != nil {
  271. return
  272. }
  273. if err = sig.Serialize(out); err != nil {
  274. return
  275. }
  276. }
  277. if err = out.Close(); err != nil {
  278. return
  279. }
  280. if err = d.buffered.Flush(); err != nil {
  281. return
  282. }
  283. return
  284. }
  285. // Encode returns a WriteCloser which will clear-sign a message with privateKey
  286. // and write it to w. If config is nil, sensible defaults are used.
  287. func Encode(w io.Writer, privateKey *packet.PrivateKey, config *packet.Config) (plaintext io.WriteCloser, err error) {
  288. return EncodeMulti(w, []*packet.PrivateKey{privateKey}, config)
  289. }
  290. // EncodeMulti returns a WriteCloser which will clear-sign a message with all the
  291. // private keys indicated and write it to w. If config is nil, sensible defaults
  292. // are used.
  293. func EncodeMulti(w io.Writer, privateKeys []*packet.PrivateKey, config *packet.Config) (plaintext io.WriteCloser, err error) {
  294. for _, k := range privateKeys {
  295. if k.Encrypted {
  296. return nil, errors.InvalidArgumentError(fmt.Sprintf("signing key %s is encrypted", k.KeyIdString()))
  297. }
  298. }
  299. hashType := config.Hash()
  300. name := nameOfHash(hashType)
  301. if len(name) == 0 {
  302. return nil, errors.UnsupportedError("unknown hash type: " + strconv.Itoa(int(hashType)))
  303. }
  304. if !hashType.Available() {
  305. return nil, errors.UnsupportedError("unsupported hash type: " + strconv.Itoa(int(hashType)))
  306. }
  307. var hashers []hash.Hash
  308. var ws []io.Writer
  309. for range privateKeys {
  310. h := hashType.New()
  311. hashers = append(hashers, h)
  312. ws = append(ws, h)
  313. }
  314. toHash := io.MultiWriter(ws...)
  315. buffered := bufio.NewWriter(w)
  316. // start has a \n at the beginning that we don't want here.
  317. if _, err = buffered.Write(start[1:]); err != nil {
  318. return
  319. }
  320. if err = buffered.WriteByte(lf); err != nil {
  321. return
  322. }
  323. if _, err = buffered.WriteString("Hash: "); err != nil {
  324. return
  325. }
  326. if _, err = buffered.WriteString(name); err != nil {
  327. return
  328. }
  329. if err = buffered.WriteByte(lf); err != nil {
  330. return
  331. }
  332. if err = buffered.WriteByte(lf); err != nil {
  333. return
  334. }
  335. plaintext = &dashEscaper{
  336. buffered: buffered,
  337. hashers: hashers,
  338. hashType: hashType,
  339. toHash: toHash,
  340. atBeginningOfLine: true,
  341. isFirstLine: true,
  342. byteBuf: make([]byte, 1),
  343. privateKeys: privateKeys,
  344. config: config,
  345. }
  346. return
  347. }
  348. // nameOfHash returns the OpenPGP name for the given hash, or the empty string
  349. // if the name isn't known. See RFC 4880, section 9.4.
  350. func nameOfHash(h crypto.Hash) string {
  351. switch h {
  352. case crypto.MD5:
  353. return "MD5"
  354. case crypto.SHA1:
  355. return "SHA1"
  356. case crypto.RIPEMD160:
  357. return "RIPEMD160"
  358. case crypto.SHA224:
  359. return "SHA224"
  360. case crypto.SHA256:
  361. return "SHA256"
  362. case crypto.SHA384:
  363. return "SHA384"
  364. case crypto.SHA512:
  365. return "SHA512"
  366. }
  367. return ""
  368. }