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 #include <unistd.h>
31 #include <exception>
32 
33 #include "consts.h"
34 #include "fileerror.hh"
35 #include "logger.hh"
36 #include "yapet20file.hh"
37 
38 using namespace yapet;
39 
40 constexpr std::uint8_t Yapet20File::RECOGNITION_STRING[];
41 
Yapet20File(const std::string & filename,bool create,bool secure)42 Yapet20File::Yapet20File(const std::string& filename, bool create, bool secure)
43     : Yapet10File{filename, create, secure} {}
44 
Yapet20File(Yapet20File && other)45 Yapet20File::Yapet20File(Yapet20File&& other) : Yapet10File{std::move(other)} {}
46 
operator =(Yapet20File && other)47 Yapet20File& Yapet20File::operator=(Yapet20File&& other) {
48     if (&other == this) {
49         return *this;
50     }
51 
52     YapetFile::operator=(std::move(other));
53 
54     return *this;
55 }
56 
~Yapet20File()57 Yapet20File::~Yapet20File() {}
58 
readUnencryptedMetaData()59 SecureArray Yapet20File::readUnencryptedMetaData() {
60     RawFile& rawFile{getRawFile()};
61     // Skip the recognition string.
62     rawFile.seekAbsolute(recognitionStringSize());
63 
64     auto resultPair{rawFile.read()};
65     if (resultPair.second == false) {
66         char msg[YAPET::Consts::EXCEPTION_MESSAGE_BUFFER_SIZE];
67         std::snprintf(msg, YAPET::Consts::EXCEPTION_MESSAGE_BUFFER_SIZE,
68                       _("Cannot read unencrypted meta data from file '%s'"),
69                       rawFile.filename().c_str());
70         throw FileFormatError{msg};
71     }
72 
73     LOG_MESSAGE(std::string{__func__} + ": " + getRawFile().filename());
74     return resultPair.first;
75 }
76 
writeUnencryptedMetaData(const SecureArray & metaData)77 void Yapet20File::writeUnencryptedMetaData(const SecureArray& metaData) {
78     RawFile& rawFile{getRawFile()};
79 
80     rawFile.seekAbsolute(recognitionStringSize());
81 
82     rawFile.write(metaData);
83     rawFile.flush();
84     LOG_MESSAGE(std::string{__func__} + ": " + getRawFile().filename());
85 }
86 
recognitionString() const87 const std::uint8_t* Yapet20File::recognitionString() const {
88     return Yapet20File::RECOGNITION_STRING;
89 }
90 
recognitionStringSize() const91 int Yapet20File::recognitionStringSize() const {
92     return Yapet20File::RECOGNITION_STRING_SIZE;
93 }