encrypted_key.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 packet
  5. import (
  6. "code.google.com/p/go.crypto/openpgp/elgamal"
  7. "code.google.com/p/go.crypto/openpgp/errors"
  8. "crypto/rand"
  9. "crypto/rsa"
  10. "encoding/binary"
  11. "io"
  12. "math/big"
  13. "strconv"
  14. )
  15. const encryptedKeyVersion = 3
  16. // EncryptedKey represents a public-key encrypted session key. See RFC 4880,
  17. // section 5.1.
  18. type EncryptedKey struct {
  19. KeyId uint64
  20. Algo PublicKeyAlgorithm
  21. CipherFunc CipherFunction // only valid after a successful Decrypt
  22. Key []byte // only valid after a successful Decrypt
  23. encryptedMPI1, encryptedMPI2 []byte
  24. }
  25. func (e *EncryptedKey) parse(r io.Reader) (err error) {
  26. var buf [10]byte
  27. _, err = readFull(r, buf[:])
  28. if err != nil {
  29. return
  30. }
  31. if buf[0] != encryptedKeyVersion {
  32. return errors.UnsupportedError("unknown EncryptedKey version " + strconv.Itoa(int(buf[0])))
  33. }
  34. e.KeyId = binary.BigEndian.Uint64(buf[1:9])
  35. e.Algo = PublicKeyAlgorithm(buf[9])
  36. switch e.Algo {
  37. case PubKeyAlgoRSA, PubKeyAlgoRSAEncryptOnly:
  38. e.encryptedMPI1, _, err = readMPI(r)
  39. case PubKeyAlgoElGamal:
  40. e.encryptedMPI1, _, err = readMPI(r)
  41. if err != nil {
  42. return
  43. }
  44. e.encryptedMPI2, _, err = readMPI(r)
  45. }
  46. _, err = consumeAll(r)
  47. return
  48. }
  49. func checksumKeyMaterial(key []byte) uint16 {
  50. var checksum uint16
  51. for _, v := range key {
  52. checksum += uint16(v)
  53. }
  54. return checksum
  55. }
  56. // Decrypt decrypts an encrypted session key with the given private key. The
  57. // private key must have been decrypted first.
  58. func (e *EncryptedKey) Decrypt(priv *PrivateKey) error {
  59. var err error
  60. var b []byte
  61. // TODO(agl): use session key decryption routines here to avoid
  62. // padding oracle attacks.
  63. switch priv.PubKeyAlgo {
  64. case PubKeyAlgoRSA, PubKeyAlgoRSAEncryptOnly:
  65. b, err = rsa.DecryptPKCS1v15(rand.Reader, priv.PrivateKey.(*rsa.PrivateKey), e.encryptedMPI1)
  66. case PubKeyAlgoElGamal:
  67. c1 := new(big.Int).SetBytes(e.encryptedMPI1)
  68. c2 := new(big.Int).SetBytes(e.encryptedMPI2)
  69. b, err = elgamal.Decrypt(priv.PrivateKey.(*elgamal.PrivateKey), c1, c2)
  70. default:
  71. err = errors.InvalidArgumentError("cannot decrypted encrypted session key with private key of type " + strconv.Itoa(int(priv.PubKeyAlgo)))
  72. }
  73. if err != nil {
  74. return err
  75. }
  76. e.CipherFunc = CipherFunction(b[0])
  77. e.Key = b[1 : len(b)-2]
  78. expectedChecksum := uint16(b[len(b)-2])<<8 | uint16(b[len(b)-1])
  79. checksum := checksumKeyMaterial(e.Key)
  80. if checksum != expectedChecksum {
  81. return errors.StructuralError("EncryptedKey checksum incorrect")
  82. }
  83. return nil
  84. }
  85. // SerializeEncryptedKey serializes an encrypted key packet to w that contains
  86. // key, encrypted to pub.
  87. func SerializeEncryptedKey(w io.Writer, rand io.Reader, pub *PublicKey, cipherFunc CipherFunction, key []byte) error {
  88. var buf [10]byte
  89. buf[0] = encryptedKeyVersion
  90. binary.BigEndian.PutUint64(buf[1:9], pub.KeyId)
  91. buf[9] = byte(pub.PubKeyAlgo)
  92. keyBlock := make([]byte, 1 /* cipher type */ +len(key)+2 /* checksum */ )
  93. keyBlock[0] = byte(cipherFunc)
  94. copy(keyBlock[1:], key)
  95. checksum := checksumKeyMaterial(key)
  96. keyBlock[1+len(key)] = byte(checksum >> 8)
  97. keyBlock[1+len(key)+1] = byte(checksum)
  98. switch pub.PubKeyAlgo {
  99. case PubKeyAlgoRSA, PubKeyAlgoRSAEncryptOnly:
  100. return serializeEncryptedKeyRSA(w, rand, buf, pub.PublicKey.(*rsa.PublicKey), keyBlock)
  101. case PubKeyAlgoElGamal:
  102. return serializeEncryptedKeyElGamal(w, rand, buf, pub.PublicKey.(*elgamal.PublicKey), keyBlock)
  103. case PubKeyAlgoDSA, PubKeyAlgoRSASignOnly:
  104. return errors.InvalidArgumentError("cannot encrypt to public key of type " + strconv.Itoa(int(pub.PubKeyAlgo)))
  105. }
  106. return errors.UnsupportedError("encrypting a key to public key of type " + strconv.Itoa(int(pub.PubKeyAlgo)))
  107. }
  108. func serializeEncryptedKeyRSA(w io.Writer, rand io.Reader, header [10]byte, pub *rsa.PublicKey, keyBlock []byte) error {
  109. cipherText, err := rsa.EncryptPKCS1v15(rand, pub, keyBlock)
  110. if err != nil {
  111. return errors.InvalidArgumentError("RSA encryption failed: " + err.Error())
  112. }
  113. packetLen := 10 /* header length */ + 2 /* mpi size */ + len(cipherText)
  114. err = serializeHeader(w, packetTypeEncryptedKey, packetLen)
  115. if err != nil {
  116. return err
  117. }
  118. _, err = w.Write(header[:])
  119. if err != nil {
  120. return err
  121. }
  122. return writeMPI(w, 8*uint16(len(cipherText)), cipherText)
  123. }
  124. func serializeEncryptedKeyElGamal(w io.Writer, rand io.Reader, header [10]byte, pub *elgamal.PublicKey, keyBlock []byte) error {
  125. c1, c2, err := elgamal.Encrypt(rand, pub, keyBlock)
  126. if err != nil {
  127. return errors.InvalidArgumentError("ElGamal encryption failed: " + err.Error())
  128. }
  129. packetLen := 10 /* header length */
  130. packetLen += 2 /* mpi size */ + (c1.BitLen()+7)/8
  131. packetLen += 2 /* mpi size */ + (c2.BitLen()+7)/8
  132. err = serializeHeader(w, packetTypeEncryptedKey, packetLen)
  133. if err != nil {
  134. return err
  135. }
  136. _, err = w.Write(header[:])
  137. if err != nil {
  138. return err
  139. }
  140. err = writeBig(w, c1)
  141. if err != nil {
  142. return err
  143. }
  144. return writeBig(w, c2)
  145. }