AWS SDK for C++  0.12.9
AWS SDK for C++
CryptoBuf.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010-2016 Amazon.com, Inc. or its affiliates. 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  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 #pragma once
16 
18 #include <aws/core/Core_EXPORTS.h>
19 #include <ios>
20 
21 namespace Aws
22 {
23  namespace Utils
24  {
25  namespace Crypto
26  {
27  typedef std::mbstate_t FPOS_TYPE;
28  static const size_t DEFAULT_BUF_SIZE = 1024;
29  static const size_t PUT_BACK_SIZE = 1;
30 
34  enum class CipherMode
35  {
36  Encrypt,
37  Decrypt
38  };
39 
43  class AWS_CORE_API CryptoBuf : public std::streambuf
44  {
45  public:
46  CryptoBuf() = default;
47  virtual ~CryptoBuf() = default;
48  CryptoBuf(const CryptoBuf&) = delete;
49  CryptoBuf(CryptoBuf&& rhs) = delete;
53  virtual void Finalize() {}
54  };
55 
65  {
66  public:
73  SymmetricCryptoBufSrc(Aws::IStream& stream, SymmetricCipher& cipher, CipherMode cipherMode, size_t bufferSize = DEFAULT_BUF_SIZE);
74 
77 
78  SymmetricCryptoBufSrc& operator=(const SymmetricCryptoBufSrc&) = delete;
79  SymmetricCryptoBufSrc& operator=(SymmetricCryptoBufSrc&&) = delete;
80 
81  virtual ~SymmetricCryptoBufSrc() { FinalizeCipher(); }
82 
88  void Finalize() override { FinalizeCipher(); }
89 
90  protected:
91  pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out ) override;
92  pos_type seekpos(pos_type pos, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out ) override;
93 
94  private:
95  int_type underflow() override;
96  off_type ComputeAbsSeekPosition(off_type, std::ios_base::seekdir, std::fpos<FPOS_TYPE>);
97  void FinalizeCipher();
98 
99  CryptoBuffer m_isBuf;
100  SymmetricCipher& m_cipher;
101  Aws::IStream& m_stream;
102  CipherMode m_cipherMode;
103  bool m_isFinalized;
104  size_t m_bufferSize;
105  size_t m_putBack;
106  };
107 
116  {
117  public:
124  SymmetricCryptoBufSink(Aws::OStream& stream, SymmetricCipher& cipher, CipherMode cipherMode, size_t bufferSize = DEFAULT_BUF_SIZE);
127 
128  SymmetricCryptoBufSink& operator=(const SymmetricCryptoBufSink&) = delete;
132  SymmetricCryptoBufSink& operator=(SymmetricCryptoBufSink&&) = delete;
133 
134  virtual ~SymmetricCryptoBufSink();
135 
139  void FinalizeCiphersAndFlushSink();
143  void Finalize() override { FinalizeCiphersAndFlushSink(); }
144 
145  private:
146  int_type overflow(int_type ch) override;
147  int sync() override;
148  bool writeOutput(bool finalize);
149 
150  CryptoBuffer m_osBuf;
151  SymmetricCipher& m_cipher;
152  Aws::OStream& m_stream;
153  CipherMode m_cipherMode;
154  bool m_isFinalized;
155  size_t m_bufferSize;
156  };
157  }
158  }
159 }
std::mbstate_t FPOS_TYPE
Definition: CryptoBuf.h:27
std::basic_istream< char, std::char_traits< char > > IStream
Definition: AWSStreamFwd.h:30
static const size_t PUT_BACK_SIZE
Definition: CryptoBuf.h:29
std::basic_ostream< char, std::char_traits< char > > OStream
Definition: AWSStreamFwd.h:31
static const size_t DEFAULT_BUF_SIZE
Definition: CryptoBuf.h:28
#define AWS_CORE_API
Definition: Core_EXPORTS.h:30
JSON (JavaScript Object Notation).