zuluCrypt  5.7.1
About: zuluCrypt is a simple but feature rich solution for hard drives encryption. It can manage PLAIN dm-crypt, LUKS, TrueCrypt and VeraCrypt encrypted volumes.
  Fossies Dox: zuluCrypt-5.7.1.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

open_tcrypt.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2012-2015
4  * name : Francis Banyikwa
5  * email: mhogomchungu@gmail.com
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "includes.h"
21 #include <sys/syscall.h>
22 #include <libcryptsetup.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <sys/mount.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <string.h>
29 
30 #include "zuluplay_support.h"
31 #include "check_tcrypt.h"
32 
33 #include "veracrypt_pim.h"
34 
35 static void _chown( const char * x,uid_t y,gid_t z )
36 {
37  if( chown( x,y,z ) ){}
38 }
39 static void _chmod( const char * x,mode_t y )
40 {
41  if( chmod( x,y ) ){}
42 }
43 static void _write( int x,const void * y,size_t z )
44 {
45  if( write( x,y,z ) ){}
46 }
47 static void _close( int x )
48 {
49  if( close( x ) ){}
50 }
51 
53 {
54 #ifdef CRYPT_TCRYPT
55  return 0 ;
56 #else
57  return 1 ;
58 #endif
59 }
60 
62 {
63 #ifdef CRYPT_TCRYPT_VERA_MODES
64  return 0 ;
65 #else
66  return 1 ;
67 #endif
68 }
69 
71 {
72 #ifdef CRYPT_TCRYPT
73  return CRYPT_TCRYPT ;
74 #else
75  return "" ;
76 #endif
77 }
78 
80 {
81 #ifdef CRYPT_TCRYPT
82 
83  struct crypt_params_tcrypt * m = malloc( sizeof( struct crypt_params_tcrypt ) ) ;
84 
85  memset( m,'\0',sizeof( struct crypt_params_tcrypt ) ) ;
86 
87  m->passphrase = opt->key ;
88  m->passphrase_size = opt->key_len ;
89  m->keyfiles = ( const char ** ) opt->tcrypt_keyfiles ;
90  m->keyfiles_count = ( unsigned int ) opt->tcrypt_keyfiles_count ;
91 
92  m->flags = CRYPT_TCRYPT_LEGACY_MODES ;
93 
94  if( opt->system_volume ){
95 
96  m->flags |= CRYPT_TCRYPT_SYSTEM_HEADER ;
97  }
98  if( opt->use_backup_header ){
99 
100  m->flags |= CRYPT_TCRYPT_BACKUP_HEADER ;
101  }
102  if( opt->use_hidden_header ){
103 
104  m->flags |= CRYPT_TCRYPT_HIDDEN_HEADER ;
105  }
106 
107 #if SUPPORT_VERACRYPT_PIM
108 
109  m->veracrypt_pim = ( unsigned int ) opt->iteration_count ;
110 #endif
111 
112 #ifdef CRYPT_TCRYPT_VERA_MODES
113 
114  if( opt->veraCrypt_volume ){
115 
116  m->flags |= CRYPT_TCRYPT_VERA_MODES ;
117  }
118 #endif
119  return m ;
120 #else
121  return NULL ;
122 #endif
123 }
124 
125 string_t zuluCryptCreateKeyFile( const char * key,size_t key_len,const char * fileName )
126 {
127  string_t st = StringVoid ;
128  int fd ;
129  const char * file ;
130 
131  struct stat statstr ;
132 
133  if( key == NULL || key_len == 0 || fileName == NULL ){
134  return StringVoid ;
135  }
136 
137  #define path_does_not_exist( x ) stat( x,&statstr ) != 0
138 
139  if( path_does_not_exist( "/run" ) ){
140 
141  mkdir( "/run",S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH | S_IROTH ) ;
142  _chown( "/run",0,0 ) ;
143  }
144  if( path_does_not_exist( "/run/zuluCrypt" ) ){
145 
146  mkdir( "/run/zuluCrypt",S_IRWXU ) ;
147  _chown( "/run/zuluCrypt",0,0 ) ;
148  }
149 
150  st = String_1( "/run/zuluCrypt/",fileName,NULL ) ;
151  file = StringAppendInt( st,syscall( SYS_gettid ) ) ;
152  fd = open( file,O_WRONLY | O_CREAT,S_IRUSR | S_IWUSR | S_IRGRP |S_IROTH ) ;
153 
154  if( fd == -1 ){
155 
156  StringDelete( &st ) ;
157  }else{
158  _write( fd,key,key_len ) ;
159  _close( fd ) ;
160  _chown( file,0,0 ) ;
161  _chmod( file,S_IRWXU ) ;
162  }
163 
164  return st ;
165 }
166 
167 string_t zuluCryptCreateKeyFile_1( string_t st,const char * fileName )
168 {
169  return zuluCryptCreateKeyFile( StringContent( st ),StringLength( st ),fileName ) ;
170 }
171 
172 static int zuluExit( int st,struct crypt_device * cd, void * m )
173 {
174  free( m ) ;
175  crypt_free( cd ) ;
176  return st ;
177 }
178 
179 static int _open_tcrypt_volume_zuluplay( const char * device,const open_struct_t * opts )
180 {
181  tc_api_task task ;
182  int r = !TC_OK ;
183 
184  size_t i ;
185  size_t k ;
186 
187  const char * const * z ;
188 
189  const char * e ;
190 
191  string_t st = StringVoid ;
192 
193  if( opts->use_hidden_header ){
194 
195  /*
196  * We return with an error here because zuluplay automatically check
197  * for this option.
198  */
199  return r ;
200  }
201 
202  if( tc_api_initialize() ){
203 
204  if( tc_api_task_initialize( &task,"map" ) ){
205 
206  tc_api_task_set( task,"use_backup_header",opts->use_backup_header ) ;
207  tc_api_task_set( task,"veracrypt_mode",opts->veraCrypt_volume ) ;
208  tc_api_task_set( task,"map_name",opts->mapper_name ) ;
209  tc_api_task_set( task,"read_only",StringHasComponent( opts->m_opts,"ro" ) ) ;
210 
211  tc_api_task_set( task,"dev",device ) ;
212 
213  if( opts->system_volume ){
214 
215  /*
216  * zuluCryptSystemVeraCryptPIM() is defined in create_tcrypt.c
217  */
218  tc_api_task_set( task,"iteration_count",
220 
221  if( StringAtLeastOnePrefixMatch( device,"/dev/sd","/dev/hd",NULL ) ){
222 
223  st = String( device ) ;
224  e = StringRemoveDigits( st ) ;
225  tc_api_task_set( task,"sys",e ) ;
226  }else{
227  tc_api_task_set( task,"sys",device ) ;
228  }
229  }else{
230  /*
231  * zuluCryptVeraCryptPIM() is defined in create_tcrypt.c
232  */
233  tc_api_task_set( task,"iteration_count",
235  }
236 
237  tc_api_task_set( task,"passphrase",opts->key ) ;
238 
239  k = opts->tcrypt_keyfiles_count ;
240  z = opts->tcrypt_keyfiles ;
241 
242  for( i = 0 ; i < k ; i++ ){
243 
244  tc_api_task_set( task,"keyfiles",*( z + i ) ) ;
245  }
246 
247  r = tc_api_task_do( task ) ;
248 
249  tc_api_task_uninit( task ) ;
250 
251  StringDelete( &st ) ;
252  }
253 
254  tc_api_uninit() ;
255  }
256 
257  return r ;
258 }
259 
260 static int _open_tcrypt_volume_cryptsetup( const char * device,const open_struct_t * opt )
261 {
262  struct crypt_device * cd ;
263  uint32_t flags ;
264  int st ;
265 
266  void * m ;
267 
268  if( crypt_init( &cd,device ) != 0 ){
269 
270  return 2 ;
271  }
272 
274 
275  if( crypt_load( cd,zuluCryptCryptsetupTCRYPTType(),m ) != 0 ){
276 
277  return zuluExit( 2,cd,m ) ;
278  }
279  if( StringHasComponent( opt->m_opts,"ro" ) ){
280 
281  flags = CRYPT_ACTIVATE_READONLY ;
282  }else{
283  flags = CRYPT_ACTIVATE_ALLOW_DISCARDS ;
284  }
285 
286  st = crypt_activate_by_volume_key( cd,opt->mapper_name,NULL,0,flags ) ;
287 
288  if( st == 0 ){
289 
290  return zuluExit( 0,cd,m ) ;
291  }else{
292  return zuluExit( 1,cd,m ) ;
293  }
294 }
295 
296 static int _open_tcrypt_volume( const char * device,const open_struct_t * opt )
297 {
298  if( opt->veraCrypt_volume ){
299 
301 
302  return _open_tcrypt_volume_zuluplay( device,opt ) ;
303  }else{
304  return _open_tcrypt_volume_cryptsetup( device,opt ) ;
305  }
306  }else{
308 
309  return _open_tcrypt_volume_zuluplay( device,opt ) ;
310  }else{
311  return _open_tcrypt_volume_cryptsetup( device,opt ) ;
312  }
313  }
314 }
315 
316 static int _open_tcrypt_volume_1( const char * device,const resolve_path_t * opt )
317 {
318  int r ;
319 
320  open_struct_t opts ;
321 
322  memcpy( &opts,opt->args,sizeof( opts ) ) ;
323 
324  if( opts.trueCrypt_volume ){
325 
326  r = _open_tcrypt_volume( device,&opts ) ;
327 
328  if( r == 0 ){
329 
330  return 0 ;
331  }
332 
333  opts.use_backup_header = 1 ;
334 
335  r = _open_tcrypt_volume( device,&opts ) ;
336 
337  if( r == 0 ){
338 
339  return 0 ;
340  }
341 
342  opts.use_backup_header = 0 ;
343  opts.use_hidden_header = 1 ;
344 
345  r = _open_tcrypt_volume( device,&opts ) ;
346 
347  if( r == 0 ){
348 
349  return 0 ;
350  }
351 
352  opts.use_backup_header = 0 ;
353  opts.use_hidden_header = 0 ;
354  opts.system_volume = 1 ;
355 
356  return _open_tcrypt_volume( device,&opts ) ;
357  }else{
358  if( opts.system_volume || opts.use_hidden_header ){
359 
360  return _open_tcrypt_volume( device,&opts ) ;
361  }else{
362  if( _open_tcrypt_volume( device,&opts ) == 0 ){
363 
364  return 0 ;
365  }else{
366  opts.use_hidden_header = 1 ;
367 
368  return _open_tcrypt_volume( device,&opts ) ;
369  }
370  }
371  }
372 }
373 
374 static int _open_tcrypt_0( const open_struct_t * opt )
375 {
376  /*
377  * zuluCryptResolveDevicePath_0() is defined in resolve_path.c
378  */
380 }
381 
382 int zuluCryptOpenTcrypt( const char * device,const char * mapper,const char * key,size_t key_len,
383  int key_source,int volume_type,const char * m_point,
384  uid_t uid,unsigned long m_flags,const char * fs_opts )
385 {
386  open_struct_t opts ;
387  string_t st ;
388  int r ;
389  const char * keyfile ;
390 
391  memset( &opts,'\0',sizeof( open_struct_t ) ) ;
392 
393  opts.device = device ;
394  opts.mapper_name = mapper ;
395  opts.volume_type = volume_type ;
396  opts.m_point = m_point ;
397  opts.uid = uid ;
398  opts.m_flags = m_flags ;
399  opts.fs_opts = fs_opts ;
400 
401  if( m_flags & MS_RDONLY ){
402  opts.m_opts = "ro" ;
403  }else{
404  opts.m_opts = "rw" ;
405  }
406 
407  if( key_source == TCRYPT_KEYFILE ){
408  st = zuluCryptCreateKeyFile( key,key_len,"open_tcrypt-" ) ;
409  if( st != StringVoid ){
410 
411  keyfile = StringContent( st ) ;
412 
413  opts.tcrypt_keyfiles_count = 1 ;
414  opts.tcrypt_keyfiles = &keyfile ;
415 
416  r = zuluCryptOpenTcrypt_1( &opts ) ;
417  /*
418  * zuluCryptDeleteFile() is defined in open_path_security.c
419  */
420  zuluCryptDeleteFile( keyfile ) ;
421  StringDelete( &st ) ;
422  }else{
423  r = 1 ;
424  }
425  }else if( key_source == TCRYPT_KEYFILE_FILE ){
426 
427  opts.tcrypt_keyfiles_count = 1 ;
428  opts.tcrypt_keyfiles = &key ;
429 
430  r = zuluCryptOpenTcrypt_1( &opts ) ;
431  }else{
432  opts.key_len = key_len ;
433  opts.key = key ;
434  r = zuluCryptOpenTcrypt_1( &opts ) ;
435  }
436 
437  return r ;
438 }
439 
441 {
442  /*
443  * zuluCryptOpenVolume_0() is defined in open_volume.c
444  */
445  return zuluCryptOpenVolume_0( _open_tcrypt_0,opts ) ;
446 }
447 
448 /*
449  * 1 is returned if a volume is a truecrypt volume.
450  * 0 is returned if a volume is not a truecrypt volume or functionality is not supported
451  */
452 int zuluCryptVolumeIsTcrypt( const char * device,const char * key,int key_source )
453 {
454  void * m ;
455 
456  open_struct_t s ;
457 
458  struct crypt_device * cd = NULL;
459 
460  memset( &s,'\0',sizeof( open_struct_t ) ) ;
461 
462  if( key_source ){}
463 
464  if( crypt_init( &cd,device ) < 0 ){
465 
466  return 0 ;
467  }else{
468  s.key = key ;
469  s.key_len = StringSize( key ) ;
470 
472 
473  if( m == NULL ){
474 
475  return 0 ;
476  }
477 
478  if( crypt_load( cd,zuluCryptCryptsetupTCRYPTType(),m ) == 0 ){
479 
480  return zuluExit( 1,cd,m ) ;
481  }else{
482  return zuluExit( 0,cd,m ) ;
483  }
484  }
485 }
open_struct_t::fs_opts
const char * fs_opts
Definition: includes.h:75
StringAppendInt
const char * StringAppendInt(string_t st, u_int64_t z)
Definition: String.c:1410
zuluCryptOpenTcrypt
int zuluCryptOpenTcrypt(const char *device, const char *mapper, const char *key, size_t key_len, int key_source, int volume_type, const char *m_point, uid_t uid, unsigned long m_flags, const char *fs_opts)
Definition: open_tcrypt.c:382
tc_api_task_initialize
static int tc_api_task_initialize(tc_api_task *task, const char *op)
Definition: zuluplay_api.h:103
open_struct_t::mapper_name
const char * mapper_name
Definition: includes.h:71
zuluCryptUseZuluPlayVCRYPT
int zuluCryptUseZuluPlayVCRYPT()
Definition: open_tcrypt.c:61
tc_api_initialize
static int tc_api_initialize(void)
Definition: zuluplay_api.h:98
open_struct_t
Definition: includes.h:68
open_struct_t::uid
uid_t uid
Definition: includes.h:80
StringLength
size_t StringLength(string_t st)
Definition: String.c:678
zuluCryptVolumeIsTcrypt
int zuluCryptVolumeIsTcrypt(const char *device, const char *key, int key_source)
Definition: open_tcrypt.c:452
open_struct_t::tcrypt_keyfiles_count
int tcrypt_keyfiles_count
Definition: includes.h:84
open_struct_t::m_point
const char * m_point
Definition: includes.h:74
_open_tcrypt_volume_1
static int _open_tcrypt_volume_1(const char *device, const resolve_path_t *opt)
Definition: open_tcrypt.c:316
zuluCryptDeleteFile
void zuluCryptDeleteFile(const char *file)
Definition: file_path_security.c:57
_chown
static void _chown(const char *x, uid_t y, gid_t z)
Definition: open_tcrypt.c:35
resolve_path_t::args
const void * args
Definition: includes.h:119
open_struct_t::use_hidden_header
int use_hidden_header
Definition: includes.h:93
open_struct_t::use_backup_header
int use_backup_header
Definition: includes.h:92
zuluCryptVeraCryptPIM
int zuluCryptVeraCryptPIM(int pim)
Definition: create_tcrypt.c:284
_write
static void _write(int x, const void *y, size_t z)
Definition: open_tcrypt.c:43
_open_tcrypt_volume_cryptsetup
static int _open_tcrypt_volume_cryptsetup(const char *device, const open_struct_t *opt)
Definition: open_tcrypt.c:260
open_struct_t::system_volume
int system_volume
Definition: includes.h:88
StringSize
static __inline__ size_t StringSize(const char *str)
Definition: String.h:846
zuluCryptUseZuluPlayTCRYPT
int zuluCryptUseZuluPlayTCRYPT()
Definition: open_tcrypt.c:52
StringContent
static const __inline__ char * StringContent(string_t st)
Definition: String.h:1011
open_struct_t::device
const char * device
Definition: includes.h:69
zuluCryptCreateKeyFile_1
string_t zuluCryptCreateKeyFile_1(string_t st, const char *fileName)
Definition: open_tcrypt.c:167
StringType
Definition: String.c:49
zuluCryptResolveDevicePath_0
int zuluCryptResolveDevicePath_0(int(*)(const char *, const resolve_path_t *), const open_struct_t *, int)
Definition: resolve_paths.c:395
open_struct_t::trueCrypt_volume
int trueCrypt_volume
Definition: includes.h:86
open_struct_t::volume_type
int volume_type
Definition: includes.h:81
TC_OK
#define TC_OK
Definition: zuluplay_api.h:36
open_struct_t::iteration_count
int iteration_count
Definition: includes.h:83
zuluCryptOpenVolume_0
int zuluCryptOpenVolume_0(int(*function)(const open_struct_t *), const open_struct_t *)
Definition: open_volume.c:48
String
string_t String(const char *cstring)
Definition: String.c:318
String_1
string_t String_1(const char *cstring,...)
Definition: String.c:363
_open_tcrypt_volume_zuluplay
static int _open_tcrypt_volume_zuluplay(const char *device, const open_struct_t *opts)
Definition: open_tcrypt.c:179
open_struct_t::key
const char * key
Definition: includes.h:73
_chmod
static void _chmod(const char *x, mode_t y)
Definition: open_tcrypt.c:39
StringHasComponent
static __inline__ int StringHasComponent(const char *x, const char *y)
Definition: String.h:964
zuluExit
static int zuluExit(int st, struct crypt_device *cd, void *m)
Definition: open_tcrypt.c:172
zuluCryptCryptsetupTCryptVCrypt
void * zuluCryptCryptsetupTCryptVCrypt(const open_struct_t *opt)
Definition: open_tcrypt.c:79
_open_tcrypt_volume
static int _open_tcrypt_volume(const char *device, const open_struct_t *opt)
Definition: open_tcrypt.c:296
zuluCryptSystemVeraCryptPIM
int zuluCryptSystemVeraCryptPIM(int pim)
Definition: create_tcrypt.c:293
open_struct_t::tcrypt_keyfiles
const char *const * tcrypt_keyfiles
Definition: includes.h:90
e
static QString e
Definition: about.cpp:31
TCRYPT_KEYFILE_FILE
#define TCRYPT_KEYFILE_FILE
Definition: libzuluCrypt.h:307
path_does_not_exist
#define path_does_not_exist(x)
TCRYPT_KEYFILE
#define TCRYPT_KEYFILE
Definition: libzuluCrypt.h:306
open_struct_t::veraCrypt_volume
int veraCrypt_volume
Definition: includes.h:85
StringAtLeastOnePrefixMatch
int StringAtLeastOnePrefixMatch(const char *x,...)
Definition: String.c:1578
tc_api_task_uninit
int tc_api_task_uninit(tc_api_task task)
Definition: tcplay_api.c:187
includes.h
StringDelete
void StringDelete(string_t *st)
Definition: String.c:162
zuluCryptCreateKeyFile
string_t zuluCryptCreateKeyFile(const char *key, size_t key_len, const char *fileName)
Definition: open_tcrypt.c:125
_tc_api_task
Definition: tcplay_api_internal.h:46
zuluCryptCryptsetupTCRYPTType
const char * zuluCryptCryptsetupTCRYPTType()
Definition: open_tcrypt.c:70
_close
static void _close(int x)
Definition: open_tcrypt.c:47
open_struct_t::key_len
size_t key_len
Definition: includes.h:79
StringRemoveDigits
const char * StringRemoveDigits(string_t st)
Definition: String.c:587
tc_api_uninit
int tc_api_uninit(void)
Definition: tcplay_api.c:57
open_struct_t::m_flags
unsigned long m_flags
Definition: includes.h:91
tc_api_task_do
int tc_api_task_do(tc_api_task task)
Definition: tcplay_api.c:778
tc_api_task_set
int tc_api_task_set(tc_api_task task, const char *key,...)
Definition: tcplay_api.c:312
resolve_path_t
Definition: includes.h:114
_open_tcrypt_0
static int _open_tcrypt_0(const open_struct_t *opt)
Definition: open_tcrypt.c:374
open_struct_t::m_opts
const char * m_opts
Definition: includes.h:76
zuluCryptOpenTcrypt_1
int zuluCryptOpenTcrypt_1(const open_struct_t *opts)
Definition: open_tcrypt.c:440
StringVoid
#define StringVoid
Definition: String.h:47