1 /*
2  * Copyright 2009-2017 Alibaba Cloud All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 #include <alibabacloud/oss/encryption/CryptoConfiguration.h>
19 #include <alibabacloud/oss/encryption/EncryptionMaterials.h>
20 #include <alibabacloud/oss/encryption/Cipher.h>
21 #include <alibabacloud/oss/OssFwd.h>
22 #include <alibabacloud/oss/model/MultipartUploadCryptoContext.h>
23 #include "../OssClientImpl.h"
24 
25 namespace AlibabaCloud
26 {
27 namespace OSS
28 {
29     class CryptoModule
30     {
31     public:
32         virtual ~CryptoModule();
33 
34         PutObjectOutcome PutObjectSecurely(const std::shared_ptr<OssClientImpl>& client, const PutObjectRequest& request);
35         GetObjectOutcome GetObjectSecurely(const std::shared_ptr<OssClientImpl>& client, const GetObjectRequest& request, const ObjectMetaData& meta);
36 
37         InitiateMultipartUploadOutcome InitiateMultipartUploadSecurely(const std::shared_ptr<OssClientImpl>& client,
38             const InitiateMultipartUploadRequest& request, MultipartUploadCryptoContext& ctx);
39         PutObjectOutcome UploadPartSecurely(const std::shared_ptr<OssClientImpl>& client, const UploadPartRequest& request,
40             const MultipartUploadCryptoContext& ctx);
41     public:
42         static std::shared_ptr<CryptoModule> CreateCryptoModule(const std::shared_ptr<EncryptionMaterials>& encryptionMaterials,
43             const CryptoConfiguration& cryptoConfig);
44 
45     protected:
46         CryptoModule(const std::shared_ptr<EncryptionMaterials>& encryptionMaterials, const CryptoConfiguration& cryptoConfig);
47         void addMetaData(const ContentCryptoMaterial& content, ObjectMetaData& meta);
48         void addMetaDataMultipart(const MultipartUploadCryptoContext& ctx, ObjectMetaData& meta);
49         void readMetaData(ContentCryptoMaterial& content, const ObjectMetaData& meta);
50         void addUserAgent(ObjectMetaData& meta, const std::string& prefix);
51 
52         virtual void initEncryptionCipher(ContentCryptoMaterial& content) = 0;
53         virtual void generateKeyIV(ContentCryptoMaterial& content) = 0;
54         virtual void initDecryptionCipher(ContentCryptoMaterial& content) = 0;
55         virtual bool checkUserParameter(const MultipartUploadCryptoContext& ctx, std::string& errMsg) = 0;
56 
57     protected:
58         std::shared_ptr<EncryptionMaterials> encryptionMaterials_;
59         CryptoConfiguration cryptoConfig_;
60         std::shared_ptr<SymmetricCipher> cipher_;
61     };
62 
63     class CryptoModuleAESCTR :public CryptoModule
64     {
65     public:
66         CryptoModuleAESCTR(const std::shared_ptr<EncryptionMaterials>& encryptionMaterials, const CryptoConfiguration& cryptoConfig);
67         ~CryptoModuleAESCTR();
68     protected:
69         virtual void initEncryptionCipher(ContentCryptoMaterial& content);
70         virtual void generateKeyIV(ContentCryptoMaterial& content);
71         virtual void initDecryptionCipher(ContentCryptoMaterial& content);
72         virtual bool checkUserParameter(const MultipartUploadCryptoContext& ctx, std::string& errMsg);
73     private:
74 
75     };
76 
77 }
78 }
79