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 "cryptofactoryhelper.hh"
31 #include "aes256factory.hh"
32 #include "blowfishfactory.hh"
33 #include "fileerror.hh"
34 #include "filehelper.hh"
35 #include "intl.h"
36
37 using namespace yapet;
38
39 namespace {
isYapet10File(const std::string & filename)40 bool isYapet10File(const std::string& filename) {
41 SecureArray expectedIdentifier{toSecureArray(
42 Yapet10File::RECOGNITION_STRING, Yapet10File::RECOGNITION_STRING_SIZE)};
43
44 return isFileType(expectedIdentifier, filename);
45 }
46
isYapet20File(const std::string & filename)47 bool isYapet20File(const std::string& filename) {
48 SecureArray expectedIdentifier{toSecureArray(
49 Yapet20File::RECOGNITION_STRING, Yapet20File::RECOGNITION_STRING_SIZE)};
50
51 return isFileType(expectedIdentifier, filename);
52 }
53 } // namespace
54
getCryptoFactoryForFile(const std::string & filename,const SecureArray & password)55 std::shared_ptr<AbstractCryptoFactory> yapet::getCryptoFactoryForFile(
56 const std::string& filename, const SecureArray& password) {
57 try {
58 if (isYapet10File(filename)) {
59 return std::shared_ptr<AbstractCryptoFactory>{
60 new BlowfishFactory{password, MetaData{}}};
61 }
62 } catch (std::exception&) {
63 // Ok, not a Yapet file
64 }
65
66 try {
67 if (isYapet20File(filename)) {
68 auto metaData{readMetaData(filename, false)};
69 return std::shared_ptr<AbstractCryptoFactory>{
70 new Aes256Factory{password, metaData}};
71 }
72 } catch (std::exception&) {
73 // Ok, not a Yapet file
74 }
75
76 return std::shared_ptr<AbstractCryptoFactory>{};
77 }