encryption.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package encryption
  2. import (
  3. cryptorand "crypto/rand"
  4. "encoding/base64"
  5. "fmt"
  6. "io"
  7. "strings"
  8. "github.com/docker/swarmkit/api"
  9. "github.com/gogo/protobuf/proto"
  10. "github.com/pkg/errors"
  11. )
  12. // This package defines the interfaces and encryption package
  13. const humanReadablePrefix = "SWMKEY-1-"
  14. // ErrCannotDecrypt is the type of error returned when some data cannot be decryptd as plaintext
  15. type ErrCannotDecrypt struct {
  16. msg string
  17. }
  18. func (e ErrCannotDecrypt) Error() string {
  19. return e.msg
  20. }
  21. // A Decrypter can decrypt an encrypted record
  22. type Decrypter interface {
  23. Decrypt(api.MaybeEncryptedRecord) ([]byte, error)
  24. }
  25. // A Encrypter can encrypt some bytes into an encrypted record
  26. type Encrypter interface {
  27. Encrypt(data []byte) (*api.MaybeEncryptedRecord, error)
  28. }
  29. type noopCrypter struct{}
  30. func (n noopCrypter) Decrypt(e api.MaybeEncryptedRecord) ([]byte, error) {
  31. if e.Algorithm != n.Algorithm() {
  32. return nil, fmt.Errorf("record is encrypted")
  33. }
  34. return e.Data, nil
  35. }
  36. func (n noopCrypter) Encrypt(data []byte) (*api.MaybeEncryptedRecord, error) {
  37. return &api.MaybeEncryptedRecord{
  38. Algorithm: n.Algorithm(),
  39. Data: data,
  40. }, nil
  41. }
  42. func (n noopCrypter) Algorithm() api.MaybeEncryptedRecord_Algorithm {
  43. return api.MaybeEncryptedRecord_NotEncrypted
  44. }
  45. // NoopCrypter is just a pass-through crypter - it does not actually encrypt or
  46. // decrypt any data
  47. var NoopCrypter = noopCrypter{}
  48. // Decrypt turns a slice of bytes serialized as an MaybeEncryptedRecord into a slice of plaintext bytes
  49. func Decrypt(encryptd []byte, decrypter Decrypter) ([]byte, error) {
  50. if decrypter == nil {
  51. return nil, ErrCannotDecrypt{msg: "no decrypter specified"}
  52. }
  53. r := api.MaybeEncryptedRecord{}
  54. if err := proto.Unmarshal(encryptd, &r); err != nil {
  55. // nope, this wasn't marshalled as a MaybeEncryptedRecord
  56. return nil, ErrCannotDecrypt{msg: "unable to unmarshal as MaybeEncryptedRecord"}
  57. }
  58. plaintext, err := decrypter.Decrypt(r)
  59. if err != nil {
  60. return nil, ErrCannotDecrypt{msg: err.Error()}
  61. }
  62. return plaintext, nil
  63. }
  64. // Encrypt turns a slice of bytes into a serialized MaybeEncryptedRecord slice of bytes
  65. func Encrypt(plaintext []byte, encrypter Encrypter) ([]byte, error) {
  66. if encrypter == nil {
  67. return nil, fmt.Errorf("no encrypter specified")
  68. }
  69. encryptedRecord, err := encrypter.Encrypt(plaintext)
  70. if err != nil {
  71. return nil, errors.Wrap(err, "unable to encrypt data")
  72. }
  73. data, err := proto.Marshal(encryptedRecord)
  74. if err != nil {
  75. return nil, errors.Wrap(err, "unable to marshal as MaybeEncryptedRecord")
  76. }
  77. return data, nil
  78. }
  79. // Defaults returns a default encrypter and decrypter
  80. func Defaults(key []byte) (Encrypter, Decrypter) {
  81. n := NewNACLSecretbox(key)
  82. return n, n
  83. }
  84. // GenerateSecretKey generates a secret key that can be used for encrypting data
  85. // using this package
  86. func GenerateSecretKey() []byte {
  87. secretData := make([]byte, naclSecretboxKeySize)
  88. if _, err := io.ReadFull(cryptorand.Reader, secretData); err != nil {
  89. // panic if we can't read random data
  90. panic(errors.Wrap(err, "failed to read random bytes"))
  91. }
  92. return secretData
  93. }
  94. // HumanReadableKey displays a secret key in a human readable way
  95. func HumanReadableKey(key []byte) string {
  96. // base64-encode the key
  97. return humanReadablePrefix + base64.RawStdEncoding.EncodeToString(key)
  98. }
  99. // ParseHumanReadableKey returns a key as bytes from recognized serializations of
  100. // said keys
  101. func ParseHumanReadableKey(key string) ([]byte, error) {
  102. if !strings.HasPrefix(key, humanReadablePrefix) {
  103. return nil, fmt.Errorf("invalid key string")
  104. }
  105. keyBytes, err := base64.RawStdEncoding.DecodeString(strings.TrimPrefix(key, humanReadablePrefix))
  106. if err != nil {
  107. return nil, fmt.Errorf("invalid key string")
  108. }
  109. return keyBytes, nil
  110. }