"Fossies" - the Fresh Open Source Software Archive

Member "zuluCrypt-5.7.1/zuluCrypt-cli/lib/open_tcrypt.c" (3 Feb 2020, 10143 Bytes) of package /linux/misc/zuluCrypt-5.7.1.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "open_tcrypt.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.7.0_vs_5.7.1.

    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 
   52 int zuluCryptUseZuluPlayTCRYPT()
   53 {
   54 #ifdef CRYPT_TCRYPT
   55     return 0 ;
   56 #else
   57     return 1 ;
   58 #endif
   59 }
   60 
   61 int zuluCryptUseZuluPlayVCRYPT()
   62 {
   63 #ifdef CRYPT_TCRYPT_VERA_MODES
   64     return 0 ;
   65 #else
   66     return 1 ;
   67 #endif
   68 }
   69 
   70 const char * zuluCryptCryptsetupTCRYPTType()
   71 {
   72 #ifdef CRYPT_TCRYPT
   73     return CRYPT_TCRYPT ;
   74 #else
   75     return "" ;
   76 #endif
   77 }
   78 
   79 void * zuluCryptCryptsetupTCryptVCrypt( const open_struct_t * opt )
   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",
  219                          zuluCryptSystemVeraCryptPIM( opts->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",
  234                          zuluCryptVeraCryptPIM( opts->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 
  273     m = zuluCryptCryptsetupTCryptVCrypt( opt ) ;
  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 
  300         if( zuluCryptUseZuluPlayVCRYPT() ){
  301 
  302             return _open_tcrypt_volume_zuluplay( device,opt ) ;
  303         }else{
  304             return _open_tcrypt_volume_cryptsetup( device,opt ) ;
  305         }
  306     }else{
  307         if( zuluCryptUseZuluPlayTCRYPT() ){
  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      */
  379     return zuluCryptResolveDevicePath_0( _open_tcrypt_volume_1,opt,1 ) ;
  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 
  440 int zuluCryptOpenTcrypt_1( const open_struct_t * opts )
  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 
  471         m = zuluCryptCryptsetupTCryptVCrypt( &s ) ;
  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 }