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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
/*
*
* Copyright (c) 2014-2015
* name : Francis Banyikwa
* email: mhogomchungu@gmail.com
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QApplication>
#include "../mainwindow.h"
#include <QDebug>
#include <QByteArray>
#include <QString>
#include <QFile>
#include <QDir>
#include <QObject>
#include "network_key.h"
#define DEBUG 0
static void _debug( const char * msg )
{
#if DEBUG
puts( msg ) ;
#endif
}
#define _cast( x ) ( const char * ) x
static result_t _network_key_get_key( const arguments_t * e )
{
zuluKey_t key ;
zuluValue_t * value ;
socket_t s ;
result_t result ;
crypt_buffer_ctx ctx ;
crypt_buffer_result r ;
const char * encryption_key = e->encryption_key ;
size_t encryption_key_key_length = strlen( encryption_key ) ;
const char * wallet_name = "zuluCrypt" ;
const char * wallet_key = e->wallet_key ;
size_t n ;
int k = 0 ;
char * buffer ;
memset( &result,'\0',sizeof( result ) ) ;
memset( &key,'\0',sizeof( zuluKey_t ) ) ;
key.key_0_length = strlen( e->path ) + 1 ;
if( key.key_0_length > sizeof( key.key_0 ) ){
_debug( "error: key length buffer overflow" ) ;
return result ;
}else{
memcpy( key.key_0,e->path,key.key_0_length ) ;
}
key.key_1_length = strlen( e->uuid ) + 1 ;
if( key.key_1_length > sizeof( key.key_1 ) ){
_debug( "error: key length buffer overflow" ) ;
return result ;
}else{
memcpy( key.key_1,e->uuid,key.key_1_length ) ;
}
key.wallet_key_length = strlen( wallet_key ) ;
if( key.wallet_key_length > sizeof( key.wallet_key ) ){
_debug( "error: wallet key length buffer overflow" ) ;
return result ;
}else{
memcpy( key.wallet_key,wallet_key,key.wallet_key_length ) ;
}
n = strlen( wallet_name ) ;
if( n > sizeof( key.wallet_name ) ){
_debug( "error: buffer overflow" ) ;
return result ;
}else{
memcpy( key.wallet_name,wallet_name,n + 1 ) ;
memcpy( key.application_name,wallet_name,n + 1 ) ;
}
while( 1 ){
_debug( "client connecting ..." ) ;
s = SocketNet( e->network_address,e->port_number ) ;
if( SocketConnect( &s ) ){
_debug( "client connected" ) ;
break ;
}else{
if( k == 10 ){
_debug( "failed to connect to server" ) ;
return result ;
}else{
sleep( 1 ) ;
k++ ;
}
}
}
if( crypt_buffer_init( &ctx,encryption_key,encryption_key_key_length ) ){
if( crypt_buffer_encrypt( ctx,_cast( &key ),sizeof( zuluKey_t ),&r ) ){
SocketSendData( s,_cast( r.buffer ),r.length ) ;
buffer = nullptr ;
n = SocketGetData( s,&buffer ) ;
if( buffer ){
if( crypt_buffer_decrypt( ctx,buffer,n,&r ) ){
if( r.length == sizeof( zuluValue_t ) ){
value = ( zuluValue_t * )r.buffer ;
if( value->key_found ){
if( value->value_length <= sizeof( result.key ) ){
result.key_len = value->value_length ;
memcpy( result.key,value->value,value->value_length ) ;
result.got_key = 1 ;
}
}else{
_debug( "key not found" ) ;
}
}
}
free( buffer ) ;
}
}
crypt_buffer_uninit( &ctx ) ;
}
SocketClose( &s ) ;
return result ;
}
int main( int argc,char * argv[] )
{
QApplication a( argc,argv ) ;
MainWindow w ;
w.setToken( *( argv + 3 ) ) ;
w.setApplicationName( "network key" ) ;
w.setkeyLabel( QObject::tr( "Enter zuluCrypt Wallet Key Below" ) ) ;
w.setkeyFileLabel( QObject::tr( "Enter Network Key Below" ) ) ;
w.setKeyFileAsKey() ;
auto e = [ = ]( const QVector<QString>& exe,const QString& network_key,const QString& password ){
Q_UNUSED( exe ) ;
arguments_t e ;
QByteArray k = password.toLatin1() ;
QByteArray n = network_key.toLatin1() ;
QByteArray z = QString( "UUID=\"%1\"" ).arg( QString( *( argv + 2 ) ) ).toLatin1() ;
e.path = *( argv + 1 ) ;
e.uuid = z.constData() ;
e.wallet_key = k.constData() ;
e.encryption_key = n.constData() ;
e.network_address = NETWORK_ADDRESS ;
e.port_number = PORT_NUMBER ;
QFile file( QDir::homePath() + "/.zuluCrypt/network_key" ) ;
QByteArray networkAddress ;
if( file.open( QIODevice::ReadOnly ) ){
QStringList l = QString( file.readAll() ).split( "\n" ) ;
if( l.size() >= 2 ){
networkAddress = l.first().toLatin1() ;
e.network_address = networkAddress.constData() ;
e.port_number = l.at( 1 ).toInt() ;
}
}
auto r = _network_key_get_key( &e ) ;
if( r.got_key ){
return QByteArray( r.key,r.key_len ) ;
}else{
return QByteArray() ;
}
} ;
w.setKeyFunction( e ) ;
w.Show() ;
return a.exec() ;
}
|