| 
									
										
										
										
											2021-04-19 03:41:13 +08:00
										 |  |  | // Copyright (c) 2015-2021 MinIO, Inc.
 | 
					
						
							| 
									
										
										
										
											2018-06-29 03:47:42 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2021-04-19 03:41:13 +08:00
										 |  |  | // This file is part of MinIO Object Storage stack
 | 
					
						
							| 
									
										
										
										
											2018-06-29 03:47:42 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2021-04-19 03:41:13 +08:00
										 |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							| 
									
										
										
										
											2018-06-29 03:47:42 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2021-04-19 03:41:13 +08:00
										 |  |  | // This program is distributed in the hope that it will be useful
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2018-06-29 03:47:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | package crypto | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-07-19 01:49:26 +08:00
										 |  |  | 	"bytes" | 
					
						
							|  |  |  | 	"crypto/md5" | 
					
						
							|  |  |  | 	"encoding/base64" | 
					
						
							| 
									
										
										
										
											2018-06-29 03:47:42 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2020-08-11 23:29:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 05:59:40 +08:00
										 |  |  | 	xhttp "github.com/minio/minio/internal/http" | 
					
						
							| 
									
										
										
										
											2018-06-29 03:47:42 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-24 23:32:51 +08:00
										 |  |  | // RemoveSensitiveHeaders removes confidential encryption
 | 
					
						
							|  |  |  | // information - e.g. the SSE-C key - from the HTTP headers.
 | 
					
						
							|  |  |  | // It has the same semantics as RemoveSensitiveEntires.
 | 
					
						
							|  |  |  | func RemoveSensitiveHeaders(h http.Header) { | 
					
						
							| 
									
										
										
										
											2020-12-23 01:19:32 +08:00
										 |  |  | 	h.Del(xhttp.AmzServerSideEncryptionCustomerKey) | 
					
						
							|  |  |  | 	h.Del(xhttp.AmzServerSideEncryptionCopyCustomerKey) | 
					
						
							| 
									
										
										
										
											2020-08-11 23:29:29 +08:00
										 |  |  | 	h.Del(xhttp.AmzMetaUnencryptedContentLength) | 
					
						
							|  |  |  | 	h.Del(xhttp.AmzMetaUnencryptedContentMD5) | 
					
						
							| 
									
										
										
										
											2018-09-24 23:32:51 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-03 01:15:06 +08:00
										 |  |  | // SSECopy represents AWS SSE-C for copy requests. It provides
 | 
					
						
							|  |  |  | // functionality to handle SSE-C copy requests.
 | 
					
						
							|  |  |  | var SSECopy = ssecCopy{} | 
					
						
							| 
									
										
										
										
											2018-07-19 01:49:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type ssecCopy struct{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // IsRequested returns true if the HTTP headers contains
 | 
					
						
							|  |  |  | // at least one SSE-C copy header. Regular SSE-C headers
 | 
					
						
							|  |  |  | // are ignored.
 | 
					
						
							|  |  |  | func (ssecCopy) IsRequested(h http.Header) bool { | 
					
						
							| 
									
										
										
										
											2020-12-23 01:19:32 +08:00
										 |  |  | 	if _, ok := h[xhttp.AmzServerSideEncryptionCopyCustomerAlgorithm]; ok { | 
					
						
							| 
									
										
										
										
											2018-07-19 01:49:26 +08:00
										 |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-12-23 01:19:32 +08:00
										 |  |  | 	if _, ok := h[xhttp.AmzServerSideEncryptionCopyCustomerKey]; ok { | 
					
						
							| 
									
										
										
										
											2018-07-19 01:49:26 +08:00
										 |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-12-23 01:19:32 +08:00
										 |  |  | 	if _, ok := h[xhttp.AmzServerSideEncryptionCopyCustomerKeyMD5]; ok { | 
					
						
							| 
									
										
										
										
											2018-07-19 01:49:26 +08:00
										 |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 04:35:54 +08:00
										 |  |  | // ParseHTTP parses the SSE-C copy headers and returns the SSE-C client key
 | 
					
						
							| 
									
										
										
										
											2018-07-19 01:49:26 +08:00
										 |  |  | // on success. Regular SSE-C headers are ignored.
 | 
					
						
							| 
									
										
										
										
											2018-07-26 04:35:54 +08:00
										 |  |  | func (ssecCopy) ParseHTTP(h http.Header) (key [32]byte, err error) { | 
					
						
							| 
									
										
										
										
											2020-12-23 01:19:32 +08:00
										 |  |  | 	if h.Get(xhttp.AmzServerSideEncryptionCopyCustomerAlgorithm) != xhttp.AmzEncryptionAES { | 
					
						
							| 
									
										
										
										
											2018-07-19 01:49:26 +08:00
										 |  |  | 		return key, ErrInvalidCustomerAlgorithm | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-12-23 01:19:32 +08:00
										 |  |  | 	if h.Get(xhttp.AmzServerSideEncryptionCopyCustomerKey) == "" { | 
					
						
							| 
									
										
										
										
											2018-07-19 01:49:26 +08:00
										 |  |  | 		return key, ErrMissingCustomerKey | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-12-23 01:19:32 +08:00
										 |  |  | 	if h.Get(xhttp.AmzServerSideEncryptionCopyCustomerKeyMD5) == "" { | 
					
						
							| 
									
										
										
										
											2018-07-19 01:49:26 +08:00
										 |  |  | 		return key, ErrMissingCustomerKeyMD5 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 01:19:32 +08:00
										 |  |  | 	clientKey, err := base64.StdEncoding.DecodeString(h.Get(xhttp.AmzServerSideEncryptionCopyCustomerKey)) | 
					
						
							| 
									
										
										
										
											2018-07-19 01:49:26 +08:00
										 |  |  | 	if err != nil || len(clientKey) != 32 { // The client key must be 256 bits long
 | 
					
						
							|  |  |  | 		return key, ErrInvalidCustomerKey | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-12-23 01:19:32 +08:00
										 |  |  | 	keyMD5, err := base64.StdEncoding.DecodeString(h.Get(xhttp.AmzServerSideEncryptionCopyCustomerKeyMD5)) | 
					
						
							| 
									
										
										
										
											2018-07-19 01:49:26 +08:00
										 |  |  | 	if md5Sum := md5.Sum(clientKey); err != nil || !bytes.Equal(md5Sum[:], keyMD5) { | 
					
						
							|  |  |  | 		return key, ErrCustomerKeyMD5Mismatch | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	copy(key[:], clientKey) | 
					
						
							|  |  |  | 	return key, nil | 
					
						
							|  |  |  | } |