clearsign.go 9.2 KB

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