1 /*
2  * Copyright (C) 2018 Rafael Ostertag
3  *
4  * This file is part of YAPET.
5  *
6  * YAPET is free software: you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation, either version 3 of the License, or (at your option) any later
9  * version.
10  *
11  * YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * YAPET.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Additional permission under GNU GPL version 3 section 7
20  *
21  * If you modify this program, or any covered work, by linking or combining it
22  * with the OpenSSL project's OpenSSL library (or a modified version of that
23  * library), containing parts covered by the terms of the OpenSSL or SSLeay
24  * licenses, Rafael Ostertag grants you additional permission to convey the
25  * resulting work.  Corresponding Source for a non-source form of such a
26  * combination shall include the source code for the parts of OpenSSL used as
27  * well as that of the covered work.
28  */
29 
30 #ifndef _SECUREARRAY_HH
31 #define _SECUREARRAY_HH
32 
33 #include <cassert>
34 #include <cstdint>
35 #include <string>
36 
37 namespace yapet {
38 
39 /**
40  * @brief Guarantee zeroing out of array
41  *
42  * Allocate an array of \c std::uint8_t on the heap and guarantee zeroing out
43  * the contents of the array upon destruction
44  */
45 class SecureArray {
46    public:
47     // Using int since SecureArray is most likely used in conjunction with
48     // OpenSSL routines which use int
49     using size_type = int;
50 
51    private:
52     size_type _size;
53     std::uint8_t* _array;
54 
55     void clearMemory();
56     void freeMemory();
57     void indexInRangeOrThrow(size_type index) const;
58 
59    public:
60     SecureArray(size_type size = 0);
61     ~SecureArray();
62 
63     SecureArray(const SecureArray&);
64     SecureArray& operator=(const SecureArray&);
65 
66     SecureArray(SecureArray&& other);
67     SecureArray& operator=(SecureArray&&);
68 
69     const std::uint8_t* operator*() const;
70     std::uint8_t* operator*();
71     std::uint8_t operator[](size_type index) const;
72     std::uint8_t& operator[](size_type index);
73 
74     bool operator==(const SecureArray& other) const;
75 
operator !=(const SecureArray & other) const76     inline bool operator!=(const SecureArray& other) const {
77         return !this->operator==(other);
78     };
79 
80     /**
81      * Copy content of \c other to this.
82      *
83      * If this SecureArray is smaller than the source SecureArray, then only as
84      * much data as fitting into this SecureArray are copied from the source
85      * SecureArray.
86      *
87      * If the source SecureArray is smaller than this SecureArray, this
88      * SecureArray is shrunk to the size of the source SecureArray.
89      *
90      * Copying from or to an empty SecureArray will have no effect.
91      */
92     SecureArray& operator<<(const SecureArray& source);
93 
size() const94     size_type size() const { return _size; }
95 };
96 
97 SecureArray operator+(const SecureArray& a, const SecureArray& b);
98 
99 SecureArray toSecureArray(const char* str);
100 SecureArray toSecureArray(const std::string& str);
101 SecureArray toSecureArray(const std::uint8_t* ptr, SecureArray::size_type size);
102 
103 }  // namespace yapet
104 #endif