1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "google/cloud/storage/well_known_headers.h"
16 #include "google/cloud/storage/internal/openssl_util.h"
17 #include "google/cloud/storage/internal/sha256_hash.h"
18 #include <algorithm>
19 #include <iomanip>
20 #include <iostream>
21 
22 namespace google {
23 namespace cloud {
24 namespace storage {
25 inline namespace STORAGE_CLIENT_NS {
operator <<(std::ostream & os,CustomHeader const & rhs)26 std::ostream& operator<<(std::ostream& os, CustomHeader const& rhs) {
27   if (!rhs.has_value()) {
28     return os;
29   }
30   return os << rhs.custom_header_name() << ": " << rhs.value();
31 }
32 
EncryptionDataFromBinaryKey(std::string const & key)33 EncryptionKeyData EncryptionDataFromBinaryKey(std::string const& key) {
34   return EncryptionKeyData{"AES256", internal::Base64Encode(key),
35                            internal::Base64Encode(internal::Sha256Hash(key))};
36 }
37 
EncryptionDataFromBase64Key(std::string const & key)38 EncryptionKeyData EncryptionDataFromBase64Key(std::string const& key) {
39   auto binary_key = internal::Base64Decode(key);
40   return EncryptionKeyData{
41       "AES256", key, internal::Base64Encode(internal::Sha256Hash(binary_key))};
42 }
43 
FromBinaryKey(std::string const & key)44 EncryptionKey EncryptionKey::FromBinaryKey(std::string const& key) {
45   return EncryptionKey(EncryptionDataFromBinaryKey(key));
46 }
47 
FromBase64Key(std::string const & key)48 EncryptionKey EncryptionKey::FromBase64Key(std::string const& key) {
49   return EncryptionKey(EncryptionDataFromBase64Key(key));
50 }
51 
operator <<(std::ostream & os,EncryptionKey const & rhs)52 std::ostream& operator<<(std::ostream& os, EncryptionKey const& rhs) {
53   char const* prefix = EncryptionKey::prefix();
54   if (rhs.has_value()) {
55     return os << prefix << "algorithm: " << rhs.value().algorithm << "\n"
56               << prefix << "key: " << rhs.value().key << "\n"
57               << prefix << "key-sha256: " << rhs.value().sha256;
58   }
59   return os << prefix << "*: <not set>";
60 }
61 
FromBinaryKey(std::string const & key)62 SourceEncryptionKey SourceEncryptionKey::FromBinaryKey(std::string const& key) {
63   return SourceEncryptionKey(EncryptionDataFromBinaryKey(key));
64 }
65 
FromBase64Key(std::string const & key)66 SourceEncryptionKey SourceEncryptionKey::FromBase64Key(std::string const& key) {
67   return SourceEncryptionKey(EncryptionDataFromBase64Key(key));
68 }
69 
operator <<(std::ostream & os,SourceEncryptionKey const & rhs)70 std::ostream& operator<<(std::ostream& os, SourceEncryptionKey const& rhs) {
71   char const* prefix = SourceEncryptionKey::prefix();
72   if (rhs.has_value()) {
73     return os << prefix << "algorithm: " << rhs.value().algorithm << "\n"
74               << prefix << "key: " << rhs.value().key << "\n"
75               << prefix << "key-sha256: " << rhs.value().sha256;
76   }
77   return os << prefix << "*: <not set>";
78 }
79 
80 }  // namespace STORAGE_CLIENT_NS
81 }  // namespace storage
82 }  // namespace cloud
83 }  // namespace google
84