write.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. package openpgp
  5. import (
  6. "code.google.com/p/go.crypto/openpgp/armor"
  7. "code.google.com/p/go.crypto/openpgp/errors"
  8. "code.google.com/p/go.crypto/openpgp/packet"
  9. "code.google.com/p/go.crypto/openpgp/s2k"
  10. "crypto"
  11. "crypto/rand"
  12. _ "crypto/sha256"
  13. "hash"
  14. "io"
  15. "strconv"
  16. "time"
  17. )
  18. // DetachSign signs message with the private key from signer (which must
  19. // already have been decrypted) and writes the signature to w.
  20. func DetachSign(w io.Writer, signer *Entity, message io.Reader) error {
  21. return detachSign(w, signer, message, packet.SigTypeBinary)
  22. }
  23. // ArmoredDetachSign signs message with the private key from signer (which
  24. // must already have been decrypted) and writes an armored signature to w.
  25. func ArmoredDetachSign(w io.Writer, signer *Entity, message io.Reader) (err error) {
  26. return armoredDetachSign(w, signer, message, packet.SigTypeBinary)
  27. }
  28. // DetachSignText signs message (after canonicalising the line endings) with
  29. // the private key from signer (which must already have been decrypted) and
  30. // writes the signature to w.
  31. func DetachSignText(w io.Writer, signer *Entity, message io.Reader) error {
  32. return detachSign(w, signer, message, packet.SigTypeText)
  33. }
  34. // ArmoredDetachSignText signs message (after canonicalising the line endings)
  35. // with the private key from signer (which must already have been decrypted)
  36. // and writes an armored signature to w.
  37. func ArmoredDetachSignText(w io.Writer, signer *Entity, message io.Reader) error {
  38. return armoredDetachSign(w, signer, message, packet.SigTypeText)
  39. }
  40. func armoredDetachSign(w io.Writer, signer *Entity, message io.Reader, sigType packet.SignatureType) (err error) {
  41. out, err := armor.Encode(w, SignatureType, nil)
  42. if err != nil {
  43. return
  44. }
  45. err = detachSign(out, signer, message, sigType)
  46. if err != nil {
  47. return
  48. }
  49. return out.Close()
  50. }
  51. func detachSign(w io.Writer, signer *Entity, message io.Reader, sigType packet.SignatureType) (err error) {
  52. if signer.PrivateKey == nil {
  53. return errors.InvalidArgumentError("signing key doesn't have a private key")
  54. }
  55. if signer.PrivateKey.Encrypted {
  56. return errors.InvalidArgumentError("signing key is encrypted")
  57. }
  58. sig := new(packet.Signature)
  59. sig.SigType = sigType
  60. sig.PubKeyAlgo = signer.PrivateKey.PubKeyAlgo
  61. sig.Hash = crypto.SHA256
  62. sig.CreationTime = time.Now()
  63. sig.IssuerKeyId = &signer.PrivateKey.KeyId
  64. h, wrappedHash, err := hashForSignature(sig.Hash, sig.SigType)
  65. if err != nil {
  66. return
  67. }
  68. io.Copy(wrappedHash, message)
  69. err = sig.Sign(rand.Reader, h, signer.PrivateKey)
  70. if err != nil {
  71. return
  72. }
  73. return sig.Serialize(w)
  74. }
  75. // FileHints contains metadata about encrypted files. This metadata is, itself,
  76. // encrypted.
  77. type FileHints struct {
  78. // IsBinary can be set to hint that the contents are binary data.
  79. IsBinary bool
  80. // FileName hints at the name of the file that should be written. It's
  81. // truncated to 255 bytes if longer. It may be empty to suggest that the
  82. // file should not be written to disk. It may be equal to "_CONSOLE" to
  83. // suggest the data should not be written to disk.
  84. FileName string
  85. // ModTime contains the modification time of the file, or the zero time if not applicable.
  86. ModTime time.Time
  87. }
  88. // SymmetricallyEncrypt acts like gpg -c: it encrypts a file with a passphrase.
  89. // The resulting WriteCloser must be closed after the contents of the file have
  90. // been written.
  91. func SymmetricallyEncrypt(ciphertext io.Writer, passphrase []byte, hints *FileHints) (plaintext io.WriteCloser, err error) {
  92. if hints == nil {
  93. hints = &FileHints{}
  94. }
  95. key, err := packet.SerializeSymmetricKeyEncrypted(ciphertext, rand.Reader, passphrase, packet.CipherAES128)
  96. if err != nil {
  97. return
  98. }
  99. w, err := packet.SerializeSymmetricallyEncrypted(ciphertext, rand.Reader, packet.CipherAES128, key)
  100. if err != nil {
  101. return
  102. }
  103. var epochSeconds uint32
  104. if !hints.ModTime.IsZero() {
  105. epochSeconds = uint32(hints.ModTime.Unix())
  106. }
  107. return packet.SerializeLiteral(w, hints.IsBinary, hints.FileName, epochSeconds)
  108. }
  109. // intersectPreferences mutates and returns a prefix of a that contains only
  110. // the values in the intersection of a and b. The order of a is preserved.
  111. func intersectPreferences(a []uint8, b []uint8) (intersection []uint8) {
  112. var j int
  113. for _, v := range a {
  114. for _, v2 := range b {
  115. if v == v2 {
  116. a[j] = v
  117. j++
  118. break
  119. }
  120. }
  121. }
  122. return a[:j]
  123. }
  124. func hashToHashId(h crypto.Hash) uint8 {
  125. v, ok := s2k.HashToHashId(h)
  126. if !ok {
  127. panic("tried to convert unknown hash")
  128. }
  129. return v
  130. }
  131. // Encrypt encrypts a message to a number of recipients and, optionally, signs
  132. // it. hints contains optional information, that is also encrypted, that aids
  133. // the recipients in processing the message. The resulting WriteCloser must
  134. // be closed after the contents of the file have been written.
  135. func Encrypt(ciphertext io.Writer, to []*Entity, signed *Entity, hints *FileHints) (plaintext io.WriteCloser, err error) {
  136. var signer *packet.PrivateKey
  137. if signed != nil {
  138. signer = signed.signingKey().PrivateKey
  139. if signer == nil || signer.Encrypted {
  140. return nil, errors.InvalidArgumentError("signing key must be decrypted")
  141. }
  142. }
  143. // These are the possible ciphers that we'll use for the message.
  144. candidateCiphers := []uint8{
  145. uint8(packet.CipherAES128),
  146. uint8(packet.CipherAES256),
  147. uint8(packet.CipherCAST5),
  148. }
  149. // These are the possible hash functions that we'll use for the signature.
  150. candidateHashes := []uint8{
  151. hashToHashId(crypto.SHA256),
  152. hashToHashId(crypto.SHA512),
  153. hashToHashId(crypto.SHA1),
  154. hashToHashId(crypto.RIPEMD160),
  155. }
  156. // In the event that a recipient doesn't specify any supported ciphers
  157. // or hash functions, these are the ones that we assume that every
  158. // implementation supports.
  159. defaultCiphers := candidateCiphers[len(candidateCiphers)-1:]
  160. defaultHashes := candidateHashes[len(candidateHashes)-1:]
  161. encryptKeys := make([]Key, len(to))
  162. for i := range to {
  163. encryptKeys[i] = to[i].encryptionKey()
  164. if encryptKeys[i].PublicKey == nil {
  165. return nil, errors.InvalidArgumentError("cannot encrypt a message to key id " + strconv.FormatUint(to[i].PrimaryKey.KeyId, 16) + " because it has no encryption keys")
  166. }
  167. sig := to[i].primaryIdentity().SelfSignature
  168. preferredSymmetric := sig.PreferredSymmetric
  169. if len(preferredSymmetric) == 0 {
  170. preferredSymmetric = defaultCiphers
  171. }
  172. preferredHashes := sig.PreferredHash
  173. if len(preferredHashes) == 0 {
  174. preferredHashes = defaultHashes
  175. }
  176. candidateCiphers = intersectPreferences(candidateCiphers, preferredSymmetric)
  177. candidateHashes = intersectPreferences(candidateHashes, preferredHashes)
  178. }
  179. if len(candidateCiphers) == 0 || len(candidateHashes) == 0 {
  180. return nil, errors.InvalidArgumentError("cannot encrypt because recipient set shares no common algorithms")
  181. }
  182. cipher := packet.CipherFunction(candidateCiphers[0])
  183. hash, _ := s2k.HashIdToHash(candidateHashes[0])
  184. symKey := make([]byte, cipher.KeySize())
  185. if _, err := io.ReadFull(rand.Reader, symKey); err != nil {
  186. return nil, err
  187. }
  188. for _, key := range encryptKeys {
  189. if err := packet.SerializeEncryptedKey(ciphertext, rand.Reader, key.PublicKey, cipher, symKey); err != nil {
  190. return nil, err
  191. }
  192. }
  193. encryptedData, err := packet.SerializeSymmetricallyEncrypted(ciphertext, rand.Reader, cipher, symKey)
  194. if err != nil {
  195. return
  196. }
  197. if signer != nil {
  198. ops := &packet.OnePassSignature{
  199. SigType: packet.SigTypeBinary,
  200. Hash: hash,
  201. PubKeyAlgo: signer.PubKeyAlgo,
  202. KeyId: signer.KeyId,
  203. IsLast: true,
  204. }
  205. if err := ops.Serialize(encryptedData); err != nil {
  206. return nil, err
  207. }
  208. }
  209. if hints == nil {
  210. hints = &FileHints{}
  211. }
  212. w := encryptedData
  213. if signer != nil {
  214. // If we need to write a signature packet after the literal
  215. // data then we need to stop literalData from closing
  216. // encryptedData.
  217. w = noOpCloser{encryptedData}
  218. }
  219. var epochSeconds uint32
  220. if !hints.ModTime.IsZero() {
  221. epochSeconds = uint32(hints.ModTime.Unix())
  222. }
  223. literalData, err := packet.SerializeLiteral(w, hints.IsBinary, hints.FileName, epochSeconds)
  224. if err != nil {
  225. return nil, err
  226. }
  227. if signer != nil {
  228. return signatureWriter{encryptedData, literalData, hash, hash.New(), signer}, nil
  229. }
  230. return literalData, nil
  231. }
  232. // signatureWriter hashes the contents of a message while passing it along to
  233. // literalData. When closed, it closes literalData, writes a signature packet
  234. // to encryptedData and then also closes encryptedData.
  235. type signatureWriter struct {
  236. encryptedData io.WriteCloser
  237. literalData io.WriteCloser
  238. hashType crypto.Hash
  239. h hash.Hash
  240. signer *packet.PrivateKey
  241. }
  242. func (s signatureWriter) Write(data []byte) (int, error) {
  243. s.h.Write(data)
  244. return s.literalData.Write(data)
  245. }
  246. func (s signatureWriter) Close() error {
  247. sig := &packet.Signature{
  248. SigType: packet.SigTypeBinary,
  249. PubKeyAlgo: s.signer.PubKeyAlgo,
  250. Hash: s.hashType,
  251. CreationTime: time.Now(),
  252. IssuerKeyId: &s.signer.KeyId,
  253. }
  254. if err := sig.Sign(rand.Reader, s.h, s.signer); err != nil {
  255. return err
  256. }
  257. if err := s.literalData.Close(); err != nil {
  258. return err
  259. }
  260. if err := sig.Serialize(s.encryptedData); err != nil {
  261. return err
  262. }
  263. return s.encryptedData.Close()
  264. }
  265. // noOpCloser is like an ioutil.NopCloser, but for an io.Writer.
  266. // TODO: we have two of these in OpenPGP packages alone. This probably needs
  267. // to be promoted somewhere more common.
  268. type noOpCloser struct {
  269. w io.Writer
  270. }
  271. func (c noOpCloser) Write(data []byte) (n int, err error) {
  272. return c.w.Write(data)
  273. }
  274. func (c noOpCloser) Close() error {
  275. return nil
  276. }