added error detection to signing and encrypting methodes: this has to be improved furtheron

This commit is contained in:
Karl-Heinz Zimmer 2001-11-29 12:29:55 +00:00
parent 4dcc18b102
commit ee33d9f146

View File

@ -701,9 +701,9 @@ bool signMessage( const char* cleartext,
GpgmeCtx ctx; GpgmeCtx ctx;
GpgmeError err; GpgmeError err;
GpgmeData data, sig; GpgmeData data, sig;
size_t rSLen; size_t rSLen = 0;
char* rSig = 0; char* rSig = 0;
bool bOk = false; bool bOk = false;
@ -754,21 +754,27 @@ bool signMessage( const char* cleartext,
gpgme_data_new_from_mem (&data, cleartext, gpgme_data_new_from_mem (&data, cleartext,
1+strlen( cleartext ), 1 ); 1+strlen( cleartext ), 1 );
gpgme_data_new ( &sig ); gpgme_data_new ( &sig );
gpgme_op_sign (ctx, data, sig, GPGME_SIG_MODE_DETACH ); err = gpgme_op_sign (ctx, data, sig, GPGME_SIG_MODE_DETACH );
rSig = gpgme_data_release_and_get_mem( sig, &rSLen ); if (!err) {
gpgme_data_release( data ); rSig = gpgme_data_release_and_get_mem( sig, &rSLen );
*ciphertext = malloc( rSLen + 1 );
*ciphertext = malloc( rSLen + 1 ); if( *ciphertext ) {
if( *ciphertext ) { if( rSLen ) {
if( rSLen ) { bOk = true;
bOk = true; strncpy((char*)*ciphertext, rSig, rSLen );
strncpy((char*)*ciphertext, rSig, rSLen ); }
((char*)(*ciphertext))[rSLen] = '\0';
} }
((char*)(*ciphertext))[rSLen] = '\0'; free( rSig );
} }
else {
free( rSig ); gpgme_data_release( sig );
*ciphertext = 0;
// hier fehlt eine Fehlerbehandlung, falls das
// Signieren schiefging
}
gpgme_data_release( data );
gpgme_release (ctx); gpgme_release (ctx);
return bOk; return bOk;
@ -838,12 +844,10 @@ bool checkMessageSignature( const char* ciphertext,
/* Provide information in the sigmeta struct */ /* Provide information in the sigmeta struct */
/* the status string */ /* the status string */
statusStr = sig_status_to_string( status ); statusStr = sig_status_to_string( status );
// PENDING(kalle) Handle out of memory
sigmeta->status = malloc( strlen( statusStr ) + 1 ); sigmeta->status = malloc( strlen( statusStr ) + 1 );
if( sigmeta->status ) { strcpy( sigmeta->status, statusStr );
strcpy( sigmeta->status, statusStr ); sigmeta->status[strlen( statusStr )] = '\0';
sigmeta->status[strlen( statusStr )] = '\0';
} else
; // nothing to do, is already 0
// Extended information for any number of signatures. // Extended information for any number of signatures.
fpr = gpgme_get_sig_status( ctx, sig_idx, &status, &created ); fpr = gpgme_get_sig_status( ctx, sig_idx, &status, &created );
@ -852,36 +856,28 @@ bool checkMessageSignature( const char* ciphertext,
struct tm* ctime_val; struct tm* ctime_val;
const char* sig_status; const char* sig_status;
void* realloc_return = realloc( sigmeta->extended_info, // PENDING(kalle) Handle out of memory
sizeof( struct SignatureMetaDataExtendedInfo ) * ( sig_idx + 1 ) ); sigmeta->extended_info = realloc( sigmeta->extended_info,
if( realloc_return ) { sizeof( struct SignatureMetaDataExtendedInfo ) * ( sig_idx + 1 ) );
sigmeta->extended_info = realloc_return; // the creation time
// the creation time // PENDING(kalle) Handle out of memory
sigmeta->extended_info[sig_idx].creation_time = malloc( sizeof( struct tm ) ); sigmeta->extended_info[sig_idx].creation_time = malloc( sizeof( struct tm ) );
if( sigmeta->extended_info[sig_idx].creation_time ) { ctime_val = localtime( &created );
ctime_val = localtime( &created ); memcpy( sigmeta->extended_info[sig_idx].creation_time,
memcpy( sigmeta->extended_info[sig_idx].creation_time, ctime_val, sizeof( struct tm ) );
ctime_val, sizeof( struct tm ) );
}
err = gpgme_get_sig_key (ctx, sig_idx, &key); err = gpgme_get_sig_key (ctx, sig_idx, &key);
sig_status = sig_status_to_string( status ); sig_status = sig_status_to_string( status );
sigmeta->extended_info[sig_idx].status_text = malloc( strlen( sig_status ) + 1 ); // PENDING(kalle) Handle out of memory
if( sigmeta->extended_info[sig_idx].status_text ) { sigmeta->extended_info[sig_idx].status_text = malloc( strlen( sig_status ) + 1 );
strcpy( sigmeta->extended_info[sig_idx].status_text, strcpy( sigmeta->extended_info[sig_idx].status_text,
sig_status ); sig_status );
sigmeta->extended_info[sig_idx].status_text[strlen( sig_status )] = '\0'; sigmeta->extended_info[sig_idx].status_text[strlen( sig_status )] = '\0';
} // PENDING(kalle) Handle out of memory
sigmeta->extended_info[sig_idx].fingerprint = malloc( strlen( fpr ) + 1 );
strcpy( sigmeta->extended_info[sig_idx].fingerprint, fpr );
sigmeta->extended_info[sig_idx].fingerprint[strlen( fpr )] = '\0';
sigmeta->extended_info[sig_idx].fingerprint = malloc( strlen( fpr ) + 1 );
if( sigmeta->extended_info[sig_idx].fingerprint ) {
strcpy( sigmeta->extended_info[sig_idx].fingerprint, fpr );
sigmeta->extended_info[sig_idx].fingerprint[strlen( fpr )] = '\0';
}
} else
break; // if allocation fails once, it isn't likely to
// succeed the next time either
fpr = gpgme_get_sig_status (ctx, ++sig_idx, &status, &created); fpr = gpgme_get_sig_status (ctx, ++sig_idx, &status, &created);
} }
sigmeta->extended_info_count = sig_idx; sigmeta->extended_info_count = sig_idx;
@ -904,7 +900,7 @@ bool encryptMessage( const char* cleartext,
GpgmeError err; GpgmeError err;
GpgmeData gCiphertext, gPlaintext; GpgmeData gCiphertext, gPlaintext;
GpgmeRecipients rset; GpgmeRecipients rset;
size_t rCLen; size_t rCLen = 0;
char* rCiph = 0; char* rCiph = 0;
bool bOk = false; bool bOk = false;
@ -921,22 +917,30 @@ bool encryptMessage( const char* cleartext,
gpgme_recipients_new (&rset); gpgme_recipients_new (&rset);
gpgme_recipients_add_name (rset, addressee); gpgme_recipients_add_name (rset, addressee);
gpgme_op_encrypt (ctx, rset, gPlaintext, gCiphertext ); err = gpgme_op_encrypt (ctx, rset, gPlaintext, gCiphertext );
gpgme_data_release (gPlaintext);
gpgme_recipients_release (rset); gpgme_recipients_release (rset);
gpgme_data_release (gPlaintext);
rCiph = gpgme_data_release_and_get_mem( gCiphertext, &rCLen ); if( !err ) {
rCiph = gpgme_data_release_and_get_mem( gCiphertext, &rCLen );
*ciphertext = malloc( rCLen + 1 ); *ciphertext = malloc( rCLen + 1 );
if( *ciphertext ) { if( *ciphertext ) {
if( rCLen ) { if( rCLen ) {
bOk = true; bOk = true;
strncpy((char*)*ciphertext, rCiph, rCLen ); strncpy((char*)*ciphertext, rCiph, rCLen );
}
((char*)(*ciphertext))[rCLen] = 0;
} }
((char*)(*ciphertext))[rCLen] = 0; free( rCiph );
}
else {
gpgme_data_release ( gCiphertext );
*ciphertext = 0;
// hier fehlt eine Fehlerbehandlung: fuer einen Recipient nur ein
// untrusted key (oder gar keiner) gefunden wurde, verweigert gpg
// das signieren.
} }
free( rCiph );
gpgme_release (ctx); gpgme_release (ctx);
return bOk; return bOk;