Repository navigation
Expand file tree
/
Copy pathLuks.cpp
More file actions
185 lines (151 loc) · 3.01 KB
/
Copy pathLuks.cpp
File metadata and controls
185 lines (151 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "Luks.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libutils/Exceptions.h>
#include <libutils/FileUtils.h>
#include <libudev.h>
#include <iostream>
using namespace std;
using namespace Utils;
namespace OPI
{
Luks::Luks(const string &path): path(path), open(false)
{
if( crypt_init( &this->cryptdevice, path.c_str() ) < 0 )
{
throw Utils::ErrnoException("Failed to init cryptsetup");
}
}
bool Luks::isLuks(const string &device)
{
struct udev *udev=udev_new();
if ( !udev ) {
return false;
}
struct udev_device *dev = udev_device_new_from_subsystem_sysname( udev, "block", device.c_str() );
if( ! dev )
{
// Lets try device number instead
try
{
string rname = File::RealPath(device);
struct stat sbuf = {};
if( stat(rname.c_str(), &sbuf) != 0 )
{
udev_unref(udev);
return false;
}
dev = udev_device_new_from_devnum(udev, 'b', sbuf.st_rdev );
if ( ! dev )
{
udev_unref(udev);
return false;
}
}
catch( Utils::ErrnoException& err )
{
udev_unref(udev);
return false;
}
}
const char* c_type = udev_device_get_property_value(dev, "ID_FS_TYPE");
if( ! c_type )
{
udev_device_unref( dev );
udev_unref(udev);
return false;
}
string type(c_type);
bool ret = type == "crypto_LUKS";
udev_device_unref( dev );
udev_unref(udev);
return ret;
}
void Luks::Format(const string &password)
{
struct crypt_params_luks1 params = {};
params.hash = "sha1";
params.data_alignment = 0;
params.data_device = nullptr;
int r = crypt_format(
this->cryptdevice,
CRYPT_LUKS1,
"aes",
"xts-plain64",
nullptr,
nullptr,
256/8,
¶ms
);
if( r < 0 )
{
throw Utils::ErrnoException("Failed to LUKS format device");
}
r = crypt_keyslot_add_by_volume_key(
this->cryptdevice,
CRYPT_ANY_SLOT,
nullptr,
0,
password.c_str(),
password.length()
);
if( r < 0 )
{
throw Utils::ErrnoException("Failed to add key to LUKS volume");
}
}
bool Luks::Open(const string &name, const string &password, bool discard)
{
int r = crypt_load(
this->cryptdevice,
CRYPT_LUKS1,
nullptr
);
if( r < 0 )
{
throw Utils::ErrnoException("Failed to load context");
}
r = crypt_activate_by_passphrase(
this->cryptdevice,
name.c_str(),
CRYPT_ANY_SLOT,
password.c_str(),
password.length(),
discard?CRYPT_ACTIVATE_ALLOW_DISCARDS:0
);
if( r < 0 )
{
return false;
}
this->name = name;
this->open = true;
return true;
}
bool Luks::Active(const string &name)
{
crypt_status_info info = crypt_status( this->cryptdevice, name.c_str() );
return info == CRYPT_ACTIVE || info == CRYPT_BUSY;
}
void Luks::Close(const string& mname)
{
string mappername;
if( mname != "" )
{
mappername = mname;
}
else
{
mappername = this->name;
}
if( crypt_deactivate( this->cryptdevice, mappername.c_str() ) < 0 )
{
throw Utils::ErrnoException("Failed to deactivate device");
}
this->open = false;
}
Luks::~Luks()
{
crypt_free( this->cryptdevice );
}
} // End NS