server-side.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2018 MinIO, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package encrypt
  18. import (
  19. "crypto/md5"
  20. "encoding/base64"
  21. "errors"
  22. "net/http"
  23. jsoniter "github.com/json-iterator/go"
  24. "golang.org/x/crypto/argon2"
  25. )
  26. const (
  27. // sseGenericHeader is the AWS SSE header used for SSE-S3 and SSE-KMS.
  28. sseGenericHeader = "X-Amz-Server-Side-Encryption"
  29. // sseKmsKeyID is the AWS SSE-KMS key id.
  30. sseKmsKeyID = sseGenericHeader + "-Aws-Kms-Key-Id"
  31. // sseEncryptionContext is the AWS SSE-KMS Encryption Context data.
  32. sseEncryptionContext = sseGenericHeader + "-Context"
  33. // sseCustomerAlgorithm is the AWS SSE-C algorithm HTTP header key.
  34. sseCustomerAlgorithm = sseGenericHeader + "-Customer-Algorithm"
  35. // sseCustomerKey is the AWS SSE-C encryption key HTTP header key.
  36. sseCustomerKey = sseGenericHeader + "-Customer-Key"
  37. // sseCustomerKeyMD5 is the AWS SSE-C encryption key MD5 HTTP header key.
  38. sseCustomerKeyMD5 = sseGenericHeader + "-Customer-Key-MD5"
  39. // sseCopyCustomerAlgorithm is the AWS SSE-C algorithm HTTP header key for CopyObject API.
  40. sseCopyCustomerAlgorithm = "X-Amz-Copy-Source-Server-Side-Encryption-Customer-Algorithm"
  41. // sseCopyCustomerKey is the AWS SSE-C encryption key HTTP header key for CopyObject API.
  42. sseCopyCustomerKey = "X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key"
  43. // sseCopyCustomerKeyMD5 is the AWS SSE-C encryption key MD5 HTTP header key for CopyObject API.
  44. sseCopyCustomerKeyMD5 = "X-Amz-Copy-Source-Server-Side-Encryption-Customer-Key-MD5"
  45. )
  46. // PBKDF creates a SSE-C key from the provided password and salt.
  47. // PBKDF is a password-based key derivation function
  48. // which can be used to derive a high-entropy cryptographic
  49. // key from a low-entropy password and a salt.
  50. type PBKDF func(password, salt []byte) ServerSide
  51. // DefaultPBKDF is the default PBKDF. It uses Argon2id with the
  52. // recommended parameters from the RFC draft (1 pass, 64 MB memory, 4 threads).
  53. var DefaultPBKDF PBKDF = func(password, salt []byte) ServerSide {
  54. sse := ssec{}
  55. copy(sse[:], argon2.IDKey(password, salt, 1, 64*1024, 4, 32))
  56. return sse
  57. }
  58. // Type is the server-side-encryption method. It represents one of
  59. // the following encryption methods:
  60. // - SSE-C: server-side-encryption with customer provided keys
  61. // - KMS: server-side-encryption with managed keys
  62. // - S3: server-side-encryption using S3 storage encryption
  63. type Type string
  64. const (
  65. // SSEC represents server-side-encryption with customer provided keys
  66. SSEC Type = "SSE-C"
  67. // KMS represents server-side-encryption with managed keys
  68. KMS Type = "KMS"
  69. // S3 represents server-side-encryption using S3 storage encryption
  70. S3 Type = "S3"
  71. )
  72. // ServerSide is a form of S3 server-side-encryption.
  73. type ServerSide interface {
  74. // Type returns the server-side-encryption method.
  75. Type() Type
  76. // Marshal adds encryption headers to the provided HTTP headers.
  77. // It marks an HTTP request as server-side-encryption request
  78. // and inserts the required data into the headers.
  79. Marshal(h http.Header)
  80. }
  81. // NewSSE returns a server-side-encryption using S3 storage encryption.
  82. // Using SSE-S3 the server will encrypt the object with server-managed keys.
  83. func NewSSE() ServerSide { return s3{} }
  84. // NewSSEKMS returns a new server-side-encryption using SSE-KMS and the provided Key Id and context.
  85. func NewSSEKMS(keyID string, context interface{}) (ServerSide, error) {
  86. if context == nil {
  87. return kms{key: keyID, hasContext: false}, nil
  88. }
  89. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  90. serializedContext, err := json.Marshal(context)
  91. if err != nil {
  92. return nil, err
  93. }
  94. return kms{key: keyID, context: serializedContext, hasContext: true}, nil
  95. }
  96. // NewSSEC returns a new server-side-encryption using SSE-C and the provided key.
  97. // The key must be 32 bytes long.
  98. func NewSSEC(key []byte) (ServerSide, error) {
  99. if len(key) != 32 {
  100. return nil, errors.New("encrypt: SSE-C key must be 256 bit long")
  101. }
  102. sse := ssec{}
  103. copy(sse[:], key)
  104. return sse, nil
  105. }
  106. // SSE transforms a SSE-C copy encryption into a SSE-C encryption.
  107. // It is the inverse of SSECopy(...).
  108. //
  109. // If the provided sse is no SSE-C copy encryption SSE returns
  110. // sse unmodified.
  111. func SSE(sse ServerSide) ServerSide {
  112. if sse == nil || sse.Type() != SSEC {
  113. return sse
  114. }
  115. if sse, ok := sse.(ssecCopy); ok {
  116. return ssec(sse)
  117. }
  118. return sse
  119. }
  120. // SSECopy transforms a SSE-C encryption into a SSE-C copy
  121. // encryption. This is required for SSE-C key rotation or a SSE-C
  122. // copy where the source and the destination should be encrypted.
  123. //
  124. // If the provided sse is no SSE-C encryption SSECopy returns
  125. // sse unmodified.
  126. func SSECopy(sse ServerSide) ServerSide {
  127. if sse == nil || sse.Type() != SSEC {
  128. return sse
  129. }
  130. if sse, ok := sse.(ssec); ok {
  131. return ssecCopy(sse)
  132. }
  133. return sse
  134. }
  135. type ssec [32]byte
  136. func (s ssec) Type() Type { return SSEC }
  137. func (s ssec) Marshal(h http.Header) {
  138. keyMD5 := md5.Sum(s[:])
  139. h.Set(sseCustomerAlgorithm, "AES256")
  140. h.Set(sseCustomerKey, base64.StdEncoding.EncodeToString(s[:]))
  141. h.Set(sseCustomerKeyMD5, base64.StdEncoding.EncodeToString(keyMD5[:]))
  142. }
  143. type ssecCopy [32]byte
  144. func (s ssecCopy) Type() Type { return SSEC }
  145. func (s ssecCopy) Marshal(h http.Header) {
  146. keyMD5 := md5.Sum(s[:])
  147. h.Set(sseCopyCustomerAlgorithm, "AES256")
  148. h.Set(sseCopyCustomerKey, base64.StdEncoding.EncodeToString(s[:]))
  149. h.Set(sseCopyCustomerKeyMD5, base64.StdEncoding.EncodeToString(keyMD5[:]))
  150. }
  151. type s3 struct{}
  152. func (s s3) Type() Type { return S3 }
  153. func (s s3) Marshal(h http.Header) { h.Set(sseGenericHeader, "AES256") }
  154. type kms struct {
  155. key string
  156. context []byte
  157. hasContext bool
  158. }
  159. func (s kms) Type() Type { return KMS }
  160. func (s kms) Marshal(h http.Header) {
  161. h.Set(sseGenericHeader, "aws:kms")
  162. if s.key != "" {
  163. h.Set(sseKmsKeyID, s.key)
  164. }
  165. if s.hasContext {
  166. h.Set(sseEncryptionContext, base64.StdEncoding.EncodeToString(s.context))
  167. }
  168. }