1 /*****************************************************************************
2  * Author:   Valient Gough <vgough@pobox.com>
3  *
4  *****************************************************************************
5  * Copyright (c) 2010 Valient Gough
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef _FSConfig_incl_
22 #define _FSConfig_incl_
23 
24 #include <memory>
25 #include <string>
26 #include <vector>
27 
28 #include "CipherKey.h"
29 #include "Interface.h"
30 #include "encfs.h"
31 
32 namespace encfs {
33 
34 enum ConfigType {
35   Config_None = 0,
36   Config_Prehistoric,
37   Config_V3,
38   Config_V4,
39   Config_V5,
40   Config_V6
41 };
42 
43 struct EncFS_Opts;
44 class Cipher;
45 class NameIO;
46 
47 /**
48  * Persistent configuration (stored in config file .encfs6.xml)
49  */
50 struct EncFSConfig {
51   ConfigType cfgType;
52 
53   std::string creator;
54   int subVersion;
55 
56   // interface of cipher
57   Interface cipherIface;
58   // interface used for file name coding
59   Interface nameIface;
60 
61   int keySize;    // reported in bits
62   int blockSize;  // reported in bytes
63 
64   std::vector<unsigned char> keyData;
65   std::vector<unsigned char> salt;
66 
67   int kdfIterations;
68   long desiredKDFDuration;
69 
70   bool plainData;         // do not encrypt file content
71 
72   int blockMACBytes;      // MAC headers on blocks..
73   int blockMACRandBytes;  // number of random bytes in the block header
74 
75   bool uniqueIV;            // per-file Initialization Vector
76   bool externalIVChaining;  // IV seeding by filename IV chaining
77 
78   bool chainedNameIV;  // filename IV chaining
79   bool allowHoles;     // allow holes in files (implicit zero blocks)
80 
EncFSConfigEncFSConfig81   EncFSConfig() : keyData(), salt() {
82     cfgType = Config_None;
83     subVersion = 0;
84     plainData = false;
85     blockMACBytes = 0;
86     blockMACRandBytes = 0;
87     uniqueIV = false;
88     externalIVChaining = false;
89     chainedNameIV = false;
90     allowHoles = false;
91 
92     kdfIterations = 0;
93     desiredKDFDuration = 500;
94   }
95 
96   CipherKey getUserKey(bool useStdin);
97   CipherKey getUserKey(const std::string &passwordProgram,
98                        const std::string &rootDir);
99   CipherKey getNewUserKey();
100 
101   std::shared_ptr<Cipher> getCipher() const;
102 
103   // deprecated
104   void assignKeyData(const std::string &in);
105   void assignKeyData(unsigned char *data, int length);
106   void assignSaltData(unsigned char *data, int length);
107 
108   unsigned char *getKeyData() const;
109   unsigned char *getSaltData() const;
110 
111  private:
112   CipherKey makeKey(const char *password, int passwdLen);
113 };
114 
115 // helpers for serializing to/from a stream
116 std::ostream &operator<<(std::ostream &os, const EncFSConfig &cfg);
117 std::istream &operator>>(std::istream &os, EncFSConfig &cfg);
118 
119 struct FSConfig {
120   std::shared_ptr<EncFSConfig> config;
121   std::shared_ptr<EncFS_Opts> opts;
122 
123   std::shared_ptr<Cipher> cipher;
124   CipherKey key;
125   std::shared_ptr<NameIO> nameCoding;
126 
127   bool forceDecode;        // force decode on MAC block failures
128   bool reverseEncryption;  // reverse encryption operation
129 
130   bool idleTracking;  // turn on idle monitoring of filesystem
131 
FSConfigFSConfig132   FSConfig()
133       : forceDecode(false), reverseEncryption(false), idleTracking(false) {}
134 };
135 
136 using FSConfigPtr = std::shared_ptr<FSConfig>;
137 
138 }  // namespace encfs
139 
140 #endif
141