fixed password handling

git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-OpenPGP@5127 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2012-04-29 11:57:03 +00:00
parent b7dcbf3ef2
commit ce5e6d3949
7 changed files with 73 additions and 68 deletions

View File

@ -12,8 +12,10 @@ extern "C" {
#include <openpgpsdk/keyring.h>
#include <openpgpsdk/readerwriter.h>
#include <openpgpsdk/validate.h>
#include <openpgpsdk/../../src/parse_local.h>
}
#include "pgphandler.h"
#include "retroshare/rsiface.h" // For rsicontrol.
std::string PGPIdType::toStdString() const
{
@ -206,7 +208,7 @@ void PGPHandler::initCertificateInfo(PGPCertificateInfo& cert,const ops_keydata_
std::string namestring( (char *)keydata->uids[0].user_id ) ;
cert._name = "" ;
int i=0;
uint32_t i=0;
while(i < namestring.length() && namestring[i] != '(' && namestring[i] != '<') { cert._name += namestring[i] ; ++i ;}
std::string& next = (namestring[i] == '(')?cert._comment:cert._email ;
@ -308,22 +310,30 @@ bool PGPHandler::availableGPGCertificatesWithPrivateKeys(std::list<PGPIdType>& i
return true ;
}
static ops_parse_cb_return_t cb_get_passphrase(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo __attribute__((unused)))
static ops_parse_cb_return_t cb_get_passphrase(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo)// __attribute__((unused)))
{
const ops_parser_content_union_t *content=&content_->content;
// validate_key_cb_arg_t *arg=ops_parse_cb_get_arg(cbinfo);
// ops_error_t **errors=ops_parse_cb_get_errors(cbinfo);
bool prev_was_bad = false ;
switch(content_->tag)
{
case OPS_PARSER_CMD_GET_SK_PASSPHRASE_PREV_WAS_BAD: prev_was_bad = true ;
case OPS_PARSER_CMD_GET_SK_PASSPHRASE:
{
std::string passwd = getpass("Please enter passwd:") ;
*(content->secret_key_passphrase.passphrase)= (char *)ops_mallocz(passwd.length()+1) ;
memcpy(*(content->secret_key_passphrase.passphrase),passwd.c_str(),passwd.length()) ;
return OPS_KEEP_MEMORY;
}
break;
{
std::string passwd;
std::string uid_hint = std::string((const char *)cbinfo->cryptinfo.keydata->uids[0].user_id) + "(" + PGPIdType(cbinfo->cryptinfo.keydata->key_id).toStdString()+")" ;
if (rsicontrol->getNotify().askForPassword(uid_hint, prev_was_bad, passwd) == false)
return OPS_RELEASE_MEMORY;
*(content->secret_key_passphrase.passphrase)= (char *)ops_mallocz(passwd.length()+1) ;
memcpy(*(content->secret_key_passphrase.passphrase),passwd.c_str(),passwd.length()) ;
return OPS_KEEP_MEMORY;
}
break;
default:
break;
@ -701,10 +711,12 @@ void PGPHandler::setAcceptConnexion(const PGPIdType& id,bool b)
std::map<std::string,PGPCertificateInfo>::iterator res = _public_keyring_map.find(id.toStdString()) ;
if(res != _public_keyring_map.end())
{
if(b)
res->second._flags |= PGPCertificateInfo::PGP_CERTIFICATE_FLAG_ACCEPT_CONNEXION ;
else
res->second._flags &= ~PGPCertificateInfo::PGP_CERTIFICATE_FLAG_ACCEPT_CONNEXION ;
}
}
bool PGPHandler::getGPGFilteredList(std::list<PGPIdType>& list,bool (*filter)(const PGPCertificateInfo&)) const

View File

@ -84,27 +84,6 @@ std::string pgp_pwd_callback(void * /*hook*/, const char *uid_hint, const char *
rsicontrol->getNotify().askForPassword(uid_hint, prev_was_bad, password) ;
return password ;
// #ifdef GPG_DEBUG2
// std::cerr << "pgp_pwd_callback() got GPG passwd from gui." << std::endl;
// #endif
//
// if((void*)fd != NULL)
// {
// #ifndef WINDOWS_SYS
// write(fd, password.c_str(), password.size());
// write(fd, "\n", 1); /* needs a new line? */
// #else
// DWORD written = 0;
// HANDLE winFd = (HANDLE) fd;
// WriteFile(winFd, password.c_str(), password.size(), &written, NULL);
// WriteFile(winFd, "\n", 1, &written, NULL);
// #endif
// }
//
// #ifdef GPG_DEBUG2
// fprintf(stderr, "pgp_pwd_callback() password setted\n");
// #endif
}
void AuthGPG::init(const std::string& path_to_public_keyring,const std::string& path_to_secret_keyring)

View File

@ -152,6 +152,7 @@ enum ops_content_tag_t
/* commands to the callback */
OPS_PARSER_CMD_GET_SK_PASSPHRASE =0x400,
OPS_PARSER_CMD_GET_SECRET_KEY =0x400+1,
OPS_PARSER_CMD_GET_SK_PASSPHRASE_PREV_WAS_BAD =0x400+2,
/* Errors */

View File

@ -506,6 +506,7 @@ callback_write_parsed(const ops_parser_content_t *content_,
break;
case OPS_PARSER_CMD_GET_SK_PASSPHRASE:
case OPS_PARSER_CMD_GET_SK_PASSPHRASE_PREV_WAS_BAD:
// return callback_cmd_get_secret_key_passphrase(content_,cbinfo);
return cbinfo->cryptinfo.cb_get_passphrase(content_, cbinfo);
break;

View File

@ -921,6 +921,7 @@ void ops_parser_content_free(ops_parser_content_t *c)
break;
case OPS_PARSER_CMD_GET_SK_PASSPHRASE:
case OPS_PARSER_CMD_GET_SK_PASSPHRASE_PREV_WAS_BAD:
ops_cmd_get_passphrase_free(&c->content.secret_key_passphrase);
break;

View File

@ -348,51 +348,61 @@ callback_pk_session_key(const ops_parser_content_t *content_,ops_parse_cb_info_t
ops_parse_cb_return_t
callback_cmd_get_secret_key(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo)
{
ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content;
const ops_secret_key_t *secret;
ops_parser_content_t pc;
{
ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content;
const ops_secret_key_t *secret;
ops_parser_content_t pc;
OPS_USED(cbinfo);
OPS_USED(cbinfo);
// ops_print_packet(content_);
// ops_print_packet(content_);
switch(content_->tag)
switch(content_->tag)
{
case OPS_PARSER_CMD_GET_SECRET_KEY:
cbinfo->cryptinfo.keydata=ops_keyring_find_key_by_id(cbinfo->cryptinfo.keyring,content->get_secret_key.pk_session_key->key_id);
if (!cbinfo->cryptinfo.keydata || !ops_is_key_secret(cbinfo->cryptinfo.keydata))
return 0;
case OPS_PARSER_CMD_GET_SECRET_KEY:
cbinfo->cryptinfo.keydata=ops_keyring_find_key_by_id(cbinfo->cryptinfo.keyring,content->get_secret_key.pk_session_key->key_id);
if (!cbinfo->cryptinfo.keydata || !ops_is_key_secret(cbinfo->cryptinfo.keydata))
return 0;
/* now get the key from the data */
secret=ops_get_secret_key_from_data(cbinfo->cryptinfo.keydata);
while(!secret)
{
if (!cbinfo->cryptinfo.passphrase)
{
memset(&pc,'\0',sizeof pc);
pc.content.secret_key_passphrase.passphrase=&cbinfo->cryptinfo.passphrase;
CB(cbinfo,OPS_PARSER_CMD_GET_SK_PASSPHRASE,&pc);
if (!cbinfo->cryptinfo.passphrase)
{
fprintf(stderr,"can't get passphrase\n");
assert(0);
}
}
/* then it must be encrypted */
secret=ops_decrypt_secret_key_from_data(cbinfo->cryptinfo.keydata,cbinfo->cryptinfo.passphrase);
}
*content->get_secret_key.secret_key=secret;
break;
/* now get the key from the data */
secret=ops_get_secret_key_from_data(cbinfo->cryptinfo.keydata);
int tag_to_use = OPS_PARSER_CMD_GET_SK_PASSPHRASE ;
int nbtries = 0 ;
default:
// return callback_general(content_,cbinfo);
break;
while( (!secret) && nbtries++ < 3)
{
if (!cbinfo->cryptinfo.passphrase)
{
memset(&pc,'\0',sizeof pc);
pc.content.secret_key_passphrase.passphrase=&cbinfo->cryptinfo.passphrase;
CB(cbinfo,tag_to_use,&pc);
if (!cbinfo->cryptinfo.passphrase)
{
fprintf(stderr,"can't get passphrase\n");
assert(0);
}
}
/* then it must be encrypted */
secret=ops_decrypt_secret_key_from_data(cbinfo->cryptinfo.keydata,cbinfo->cryptinfo.passphrase);
free(cbinfo->cryptinfo.passphrase) ;
cbinfo->cryptinfo.passphrase = NULL ;
tag_to_use = OPS_PARSER_CMD_GET_SK_PASSPHRASE_PREV_WAS_BAD ;
}
if(!secret)
return 0 ;
*content->get_secret_key.secret_key=secret;
break;
default:
// return callback_general(content_,cbinfo);
break;
}
return OPS_RELEASE_MEMORY;
}
return OPS_RELEASE_MEMORY;
}
char *ops_get_passphrase(void)
{

View File

@ -35,6 +35,7 @@ linux-* {
QMAKE_CXXFLAGS *= -D_FILE_OFFSET_BITS=64
PRE_TARGETDEPS *= ../../libretroshare/src/lib/libretroshare.a
PRE_TARGETDEPS *= ../../openpgpsdk/lib/libops.a
LIBS += ../../libretroshare/src/lib/libretroshare.a
LIBS += ../../openpgpsdk/lib/libops.a -lbz2