First draft of a keysync implementation (not tested yet). keysync
authorMarkus Schaber <markus@pep-security.net>
Sat, 01 Oct 2016 01:08:29 +0200
branchkeysync
changeset 16577dfd1defafa
parent 164 1971112a0cc0
child 166 1955a9c2609f
First draft of a keysync implementation (not tested yet).
CpEpEngine.cpp
CpEpEngine.h
pEpCOMServerAdapter.idl
     1.1 --- a/CpEpEngine.cpp	Fri Sep 30 22:21:09 2016 +0200
     1.2 +++ b/CpEpEngine.cpp	Sat Oct 01 01:08:29 2016 +0200
     1.3 @@ -10,14 +10,14 @@
     1.4  
     1.5  STDMETHODIMP CpEpEngine::InterfaceSupportsErrorInfo(REFIID riid)
     1.6  {
     1.7 -	static const IID* const arr[] = 
     1.8 +	static const IID* const arr[] =
     1.9  	{
    1.10  		&IID_IpEpEngine
    1.11  	};
    1.12  
    1.13 -	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
    1.14 +	for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
    1.15  	{
    1.16 -		if (InlineIsEqualGUID(*arr[i],riid))
    1.17 +		if (InlineIsEqualGUID(*arr[i], riid))
    1.18  			return S_OK;
    1.19  	}
    1.20  	return S_FALSE;
    1.21 @@ -27,806 +27,806 @@
    1.22  
    1.23  STDMETHODIMP CpEpEngine::verbose_logging(VARIANT_BOOL enable)
    1.24  {
    1.25 -    verbose_mode = enable != VARIANT_FALSE;
    1.26 -    return S_OK;
    1.27 +	verbose_mode = enable != VARIANT_FALSE;
    1.28 +	return S_OK;
    1.29  }
    1.30  
    1.31  STDMETHODIMP CpEpEngine::passive_mode(VARIANT_BOOL enable)
    1.32  {
    1.33 -    ::config_passive_mode(get_session(), enable != VARIANT_FALSE);
    1.34 -    return S_OK;
    1.35 +	::config_passive_mode(get_session(), enable != VARIANT_FALSE);
    1.36 +	return S_OK;
    1.37  }
    1.38  
    1.39  STDMETHODIMP CpEpEngine::unencrypted_subject(VARIANT_BOOL enable)
    1.40  {
    1.41 -    ::config_unencrypted_subject(get_session(), enable != VARIANT_FALSE);
    1.42 -    return S_OK;
    1.43 +	::config_unencrypted_subject(get_session(), enable != VARIANT_FALSE);
    1.44 +	return S_OK;
    1.45  }
    1.46  
    1.47  STDMETHODIMP CpEpEngine::log(BSTR title, BSTR entity, BSTR description, BSTR comment)
    1.48  {
    1.49 -    string _title;
    1.50 -    string _entity;
    1.51 -    string _description;
    1.52 -    string _comment;
    1.53 -    HRESULT result = S_OK;
    1.54 +	string _title;
    1.55 +	string _entity;
    1.56 +	string _description;
    1.57 +	string _comment;
    1.58 +	HRESULT result = S_OK;
    1.59  
    1.60 -    assert(title);
    1.61 -    if (title)
    1.62 -        _title = utf8_string(title);
    1.63 -    else
    1.64 -        result = E_INVALIDARG;
    1.65 +	assert(title);
    1.66 +	if (title)
    1.67 +		_title = utf8_string(title);
    1.68 +	else
    1.69 +		result = E_INVALIDARG;
    1.70  
    1.71 -    assert(entity);
    1.72 -    if (entity)
    1.73 -        _entity = utf8_string(entity);
    1.74 -    else
    1.75 -        result = E_INVALIDARG;
    1.76 +	assert(entity);
    1.77 +	if (entity)
    1.78 +		_entity = utf8_string(entity);
    1.79 +	else
    1.80 +		result = E_INVALIDARG;
    1.81  
    1.82 -    if (description)
    1.83 -        _description = utf8_string(description);
    1.84 +	if (description)
    1.85 +		_description = utf8_string(description);
    1.86  
    1.87 -    if (comment)
    1.88 -        _comment = utf8_string(comment);
    1.89 +	if (comment)
    1.90 +		_comment = utf8_string(comment);
    1.91  
    1.92 -    if (result != S_OK)
    1.93 -        return result;
    1.94 +	if (result != S_OK)
    1.95 +		return result;
    1.96  
    1.97 -    PEP_STATUS _status = ::log_event(get_session(), _title.c_str(), _entity.c_str(), _description.c_str(), _comment.c_str());
    1.98 -    assert(_status == PEP_STATUS_OK);
    1.99 -    if (_status != PEP_STATUS_OK)
   1.100 -        return FAIL(L"log_event");
   1.101 -    else
   1.102 -        return S_OK;
   1.103 +	PEP_STATUS _status = ::log_event(get_session(), _title.c_str(), _entity.c_str(), _description.c_str(), _comment.c_str());
   1.104 +	assert(_status == PEP_STATUS_OK);
   1.105 +	if (_status != PEP_STATUS_OK)
   1.106 +		return FAIL(L"log_event");
   1.107 +	else
   1.108 +		return S_OK;
   1.109  }
   1.110  
   1.111  
   1.112  STDMETHODIMP CpEpEngine::decrypt(BSTR ctext, BSTR * ptext, LPSAFEARRAY * key_list, pEp_STATUS * status)
   1.113  {
   1.114 -    assert(ctext);
   1.115 -    assert(ptext);
   1.116 -    assert(key_list);
   1.117 -    assert(status);
   1.118 +	assert(ctext);
   1.119 +	assert(ptext);
   1.120 +	assert(key_list);
   1.121 +	assert(status);
   1.122  
   1.123 -    if (ctext == NULL || ptext == NULL || key_list == NULL || status == NULL) {
   1.124 -        if (ptext)
   1.125 -            *ptext = NULL;
   1.126 -        if (key_list)
   1.127 -            *key_list = NULL;
   1.128 -        if (status)
   1.129 -            *status = pEp_UNENCRYPTED;
   1.130 -        return E_INVALIDARG;
   1.131 -    }
   1.132 +	if (ctext == NULL || ptext == NULL || key_list == NULL || status == NULL) {
   1.133 +		if (ptext)
   1.134 +			*ptext = NULL;
   1.135 +		if (key_list)
   1.136 +			*key_list = NULL;
   1.137 +		if (status)
   1.138 +			*status = pEp_UNENCRYPTED;
   1.139 +		return E_INVALIDARG;
   1.140 +	}
   1.141  
   1.142 -    string _ctext = utf8_string(ctext);
   1.143 +	string _ctext = utf8_string(ctext);
   1.144  
   1.145 -    char *_ptext = NULL;
   1.146 -    size_t _psize = 0;
   1.147 -    ::stringlist_t *_keylist = NULL;
   1.148 -    PEP_STATUS _status;
   1.149 +	char *_ptext = NULL;
   1.150 +	size_t _psize = 0;
   1.151 +	::stringlist_t *_keylist = NULL;
   1.152 +	PEP_STATUS _status;
   1.153  
   1.154 -    _status = ::decrypt_and_verify(get_session(), _ctext.c_str(), _ctext.size(), &_ptext, &_psize, &_keylist);
   1.155 -    assert(_status != PEP_OUT_OF_MEMORY);
   1.156 -    if (_status == PEP_OUT_OF_MEMORY)
   1.157 -        return E_OUTOFMEMORY;
   1.158 +	_status = ::decrypt_and_verify(get_session(), _ctext.c_str(), _ctext.size(), &_ptext, &_psize, &_keylist);
   1.159 +	assert(_status != PEP_OUT_OF_MEMORY);
   1.160 +	if (_status == PEP_OUT_OF_MEMORY)
   1.161 +		return E_OUTOFMEMORY;
   1.162  
   1.163 -    *status = (pEp_STATUS) _status;
   1.164 -    if (_ptext == NULL) {
   1.165 -        if (_keylist) {
   1.166 -            string msg;
   1.167 -            if (_keylist->value[0] != 0) {
   1.168 -                msg = _keylist->value;
   1.169 -            }
   1.170 -            else {
   1.171 -                for (::stringlist_t *s = _keylist->next; s != NULL; s = s->next) {
   1.172 -                    if (s->value) {
   1.173 -                        msg += s->value;
   1.174 -                        msg += ";";
   1.175 -                    }
   1.176 -                }
   1.177 -            }
   1.178 -            ::free_stringlist(_keylist);
   1.179 +	*status = (pEp_STATUS)_status;
   1.180 +	if (_ptext == NULL) {
   1.181 +		if (_keylist) {
   1.182 +			string msg;
   1.183 +			if (_keylist->value[0] != 0) {
   1.184 +				msg = _keylist->value;
   1.185 +			}
   1.186 +			else {
   1.187 +				for (::stringlist_t *s = _keylist->next; s != NULL; s = s->next) {
   1.188 +					if (s->value) {
   1.189 +						msg += s->value;
   1.190 +						msg += ";";
   1.191 +					}
   1.192 +				}
   1.193 +			}
   1.194 +			::free_stringlist(_keylist);
   1.195  
   1.196 -            return FAIL(utf16_bstr(msg));
   1.197 -        }
   1.198 -        else
   1.199 -            return FAIL(L"cannot decrypt");
   1.200 -    }
   1.201 +			return FAIL(utf16_bstr(msg));
   1.202 +		}
   1.203 +		else
   1.204 +			return FAIL(L"cannot decrypt");
   1.205 +	}
   1.206  
   1.207 -    *ptext = utf16_bstr(_ptext);
   1.208 -    pEp_free(_ptext);
   1.209 +	*ptext = utf16_bstr(_ptext);
   1.210 +	pEp_free(_ptext);
   1.211  
   1.212 -    if (_keylist && _keylist->value)
   1.213 -        *key_list = string_array(_keylist);
   1.214 -    else
   1.215 -        *key_list = NULL;
   1.216 -    ::free_stringlist(_keylist);
   1.217 +	if (_keylist && _keylist->value)
   1.218 +		*key_list = string_array(_keylist);
   1.219 +	else
   1.220 +		*key_list = NULL;
   1.221 +	::free_stringlist(_keylist);
   1.222  
   1.223 -    return S_OK;
   1.224 +	return S_OK;
   1.225  }
   1.226  
   1.227  STDMETHODIMP CpEpEngine::decrypt_b(BSTR ctext, LPSAFEARRAY * ptext, LPSAFEARRAY * key_list, pEp_STATUS * status)
   1.228  {
   1.229 -    assert(ctext);
   1.230 -    assert(ptext);
   1.231 -    assert(key_list);
   1.232 -    assert(status);
   1.233 +	assert(ctext);
   1.234 +	assert(ptext);
   1.235 +	assert(key_list);
   1.236 +	assert(status);
   1.237  
   1.238 -    if (ctext == NULL || ptext == NULL || key_list == NULL || status == NULL) {
   1.239 -        if (ptext)
   1.240 -            *ptext = NULL;
   1.241 -        if (key_list)
   1.242 -            *key_list = NULL;
   1.243 -        if (status)
   1.244 -            *status = pEp_UNENCRYPTED;
   1.245 -        return E_INVALIDARG;
   1.246 -    }
   1.247 +	if (ctext == NULL || ptext == NULL || key_list == NULL || status == NULL) {
   1.248 +		if (ptext)
   1.249 +			*ptext = NULL;
   1.250 +		if (key_list)
   1.251 +			*key_list = NULL;
   1.252 +		if (status)
   1.253 +			*status = pEp_UNENCRYPTED;
   1.254 +		return E_INVALIDARG;
   1.255 +	}
   1.256  
   1.257 -    // Welcome to Windoze string hell!
   1.258 +	// Welcome to Windoze string hell!
   1.259  
   1.260 -    char *_ctext = NULL;
   1.261 -    _bstr_t bstr_ctext(ctext, true);
   1.262 -    int w_csize = bstr_ctext.length() + 1;
   1.263 -    int _csize = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, (wchar_t *) bstr_ctext, w_csize, NULL, 0, NULL, NULL);
   1.264 -    if (_csize) {
   1.265 -        _ctext = new char[_csize];
   1.266 -        WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, (wchar_t *) bstr_ctext, w_csize, _ctext, _csize, NULL, NULL);
   1.267 -    }
   1.268 +	char *_ctext = NULL;
   1.269 +	_bstr_t bstr_ctext(ctext, true);
   1.270 +	int w_csize = bstr_ctext.length() + 1;
   1.271 +	int _csize = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, (wchar_t *)bstr_ctext, w_csize, NULL, 0, NULL, NULL);
   1.272 +	if (_csize) {
   1.273 +		_ctext = new char[_csize];
   1.274 +		WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, (wchar_t *)bstr_ctext, w_csize, _ctext, _csize, NULL, NULL);
   1.275 +	}
   1.276  
   1.277 -    char *_ptext = NULL;
   1.278 -    size_t _psize = 0;
   1.279 -    ::stringlist_t *_keylist = NULL;
   1.280 -    PEP_STATUS _status;
   1.281 +	char *_ptext = NULL;
   1.282 +	size_t _psize = 0;
   1.283 +	::stringlist_t *_keylist = NULL;
   1.284 +	PEP_STATUS _status;
   1.285  
   1.286 -    _status = ::decrypt_and_verify(get_session(), _ctext, _csize, &_ptext, &_psize, &_keylist);
   1.287 -    assert(_status != PEP_OUT_OF_MEMORY);
   1.288 -    delete[] _ctext;
   1.289 -    if (_status == PEP_OUT_OF_MEMORY) {
   1.290 -        ::free_stringlist(_keylist);
   1.291 -        return E_OUTOFMEMORY;
   1.292 -    }
   1.293 -    *status = (pEp_STATUS) _status;
   1.294 +	_status = ::decrypt_and_verify(get_session(), _ctext, _csize, &_ptext, &_psize, &_keylist);
   1.295 +	assert(_status != PEP_OUT_OF_MEMORY);
   1.296 +	delete[] _ctext;
   1.297 +	if (_status == PEP_OUT_OF_MEMORY) {
   1.298 +		::free_stringlist(_keylist);
   1.299 +		return E_OUTOFMEMORY;
   1.300 +	}
   1.301 +	*status = (pEp_STATUS)_status;
   1.302  
   1.303 -    if (_ptext == NULL) {
   1.304 -        ::free_stringlist(_keylist);
   1.305 -        return FAIL(L"decrypt_and_verify");
   1.306 -    }
   1.307 +	if (_ptext == NULL) {
   1.308 +		::free_stringlist(_keylist);
   1.309 +		return FAIL(L"decrypt_and_verify");
   1.310 +	}
   1.311  
   1.312 -    CComSafeArray<BYTE> sa_ptext;
   1.313 +	CComSafeArray<BYTE> sa_ptext;
   1.314  
   1.315 -    HRESULT _result = sa_ptext.Create(_psize, 0);
   1.316 -    assert(_result == S_OK);
   1.317 -    if (_result == E_OUTOFMEMORY) {
   1.318 -        pEp_free(_ptext);
   1.319 -        ::free_stringlist(_keylist);
   1.320 -        return E_OUTOFMEMORY;
   1.321 -    }
   1.322 -    else if (_result != S_OK) {
   1.323 -        pEp_free(_ptext);
   1.324 -        ::free_stringlist(_keylist);
   1.325 -        return FAIL(L"CComSafeArray<BYTE>::Create");
   1.326 -    }
   1.327 +	HRESULT _result = sa_ptext.Create(_psize, 0);
   1.328 +	assert(_result == S_OK);
   1.329 +	if (_result == E_OUTOFMEMORY) {
   1.330 +		pEp_free(_ptext);
   1.331 +		::free_stringlist(_keylist);
   1.332 +		return E_OUTOFMEMORY;
   1.333 +	}
   1.334 +	else if (_result != S_OK) {
   1.335 +		pEp_free(_ptext);
   1.336 +		::free_stringlist(_keylist);
   1.337 +		return FAIL(L"CComSafeArray<BYTE>::Create");
   1.338 +	}
   1.339  
   1.340 -    memcpy(sa_ptext.m_psa->pvData, _ptext, _psize);
   1.341 -    *ptext = sa_ptext.Detach();
   1.342 -    ::pEp_free(_ptext);
   1.343 +	memcpy(sa_ptext.m_psa->pvData, _ptext, _psize);
   1.344 +	*ptext = sa_ptext.Detach();
   1.345 +	::pEp_free(_ptext);
   1.346  
   1.347 -    if (_keylist && _keylist->value)
   1.348 -        *key_list = string_array(_keylist);
   1.349 -    else
   1.350 -        *key_list = NULL;
   1.351 -    ::free_stringlist(_keylist);
   1.352 +	if (_keylist && _keylist->value)
   1.353 +		*key_list = string_array(_keylist);
   1.354 +	else
   1.355 +		*key_list = NULL;
   1.356 +	::free_stringlist(_keylist);
   1.357  
   1.358 -    return S_OK;
   1.359 +	return S_OK;
   1.360  }
   1.361  
   1.362  STDMETHODIMP CpEpEngine::verify(BSTR text, BSTR signature, LPSAFEARRAY * key_list, pEp_STATUS * verify_status)
   1.363  {
   1.364 -    assert(text);
   1.365 -    assert(signature);
   1.366 -    assert(key_list);
   1.367 +	assert(text);
   1.368 +	assert(signature);
   1.369 +	assert(key_list);
   1.370  
   1.371 -    if (text == NULL || signature == NULL || key_list == NULL)
   1.372 -        return E_INVALIDARG;
   1.373 +	if (text == NULL || signature == NULL || key_list == NULL)
   1.374 +		return E_INVALIDARG;
   1.375  
   1.376 -    string _text = utf8_string(text);
   1.377 -    string _signature = utf8_string(signature);
   1.378 +	string _text = utf8_string(text);
   1.379 +	string _signature = utf8_string(signature);
   1.380  
   1.381 -    ::stringlist_t *_keylist = NULL;
   1.382 -    PEP_STATUS _status;
   1.383 -    _status = ::verify_text(get_session(), _text.c_str(), _text.size(), _signature.c_str(), _signature.size(), &_keylist);
   1.384 -    assert(_status != PEP_OUT_OF_MEMORY);
   1.385 -    if (_status == PEP_OUT_OF_MEMORY)
   1.386 -        return E_OUTOFMEMORY;
   1.387 -    if (_status == PEP_DECRYPT_WRONG_FORMAT || _status == PEP_UNKNOWN_ERROR)
   1.388 -        return FAIL(L"verify_text");
   1.389 +	::stringlist_t *_keylist = NULL;
   1.390 +	PEP_STATUS _status;
   1.391 +	_status = ::verify_text(get_session(), _text.c_str(), _text.size(), _signature.c_str(), _signature.size(), &_keylist);
   1.392 +	assert(_status != PEP_OUT_OF_MEMORY);
   1.393 +	if (_status == PEP_OUT_OF_MEMORY)
   1.394 +		return E_OUTOFMEMORY;
   1.395 +	if (_status == PEP_DECRYPT_WRONG_FORMAT || _status == PEP_UNKNOWN_ERROR)
   1.396 +		return FAIL(L"verify_text");
   1.397  
   1.398 -    *verify_status = (pEp_STATUS) _status;
   1.399 +	*verify_status = (pEp_STATUS)_status;
   1.400  
   1.401 -    if (_keylist && _keylist->value)
   1.402 -        *key_list = string_array(_keylist);
   1.403 -    else
   1.404 -        *key_list = NULL;
   1.405 -    ::free_stringlist(_keylist);
   1.406 +	if (_keylist && _keylist->value)
   1.407 +		*key_list = string_array(_keylist);
   1.408 +	else
   1.409 +		*key_list = NULL;
   1.410 +	::free_stringlist(_keylist);
   1.411  
   1.412 -    return S_OK;
   1.413 +	return S_OK;
   1.414  }
   1.415  
   1.416  STDMETHODIMP CpEpEngine::encrypt(SAFEARRAY * key_list, BSTR ptext, BSTR * ctext, pEp_STATUS * status)
   1.417  {
   1.418 -    assert(key_list);
   1.419 -    assert(ptext);
   1.420 -    assert(ctext);
   1.421 -    assert(status);
   1.422 +	assert(key_list);
   1.423 +	assert(ptext);
   1.424 +	assert(ctext);
   1.425 +	assert(status);
   1.426  
   1.427 -    if (ctext == NULL || ptext == NULL || key_list == NULL || status == NULL) {
   1.428 -        if (ctext)
   1.429 -            *ctext = NULL;
   1.430 -        if (status)
   1.431 -            *status = pEp_UNKNOWN_ERROR;
   1.432 -        return E_INVALIDARG;
   1.433 -    }
   1.434 +	if (ctext == NULL || ptext == NULL || key_list == NULL || status == NULL) {
   1.435 +		if (ctext)
   1.436 +			*ctext = NULL;
   1.437 +		if (status)
   1.438 +			*status = pEp_UNKNOWN_ERROR;
   1.439 +		return E_INVALIDARG;
   1.440 +	}
   1.441  
   1.442 -    HRESULT result = S_OK;
   1.443 +	HRESULT result = S_OK;
   1.444  
   1.445 -    ::stringlist_t *_keylist = new_stringlist(key_list);
   1.446 -    string _ptext = utf8_string(ptext);
   1.447 +	::stringlist_t *_keylist = new_stringlist(key_list);
   1.448 +	string _ptext = utf8_string(ptext);
   1.449  
   1.450 -    char *_ctext = NULL;
   1.451 -    size_t _csize = 0;
   1.452 -    PEP_STATUS _status;
   1.453 +	char *_ctext = NULL;
   1.454 +	size_t _csize = 0;
   1.455 +	PEP_STATUS _status;
   1.456  
   1.457 -    _status = ::encrypt_and_sign(get_session(), _keylist, _ptext.c_str(), _ptext.size(), &_ctext, &_csize);
   1.458 +	_status = ::encrypt_and_sign(get_session(), _keylist, _ptext.c_str(), _ptext.size(), &_ctext, &_csize);
   1.459  
   1.460 -    assert(_status != PEP_OUT_OF_MEMORY);
   1.461 -    ::free_stringlist(_keylist);
   1.462 -    if (_status == PEP_OUT_OF_MEMORY)
   1.463 -        return E_OUTOFMEMORY;
   1.464 -    *status = (pEp_STATUS) _status;
   1.465 +	assert(_status != PEP_OUT_OF_MEMORY);
   1.466 +	::free_stringlist(_keylist);
   1.467 +	if (_status == PEP_OUT_OF_MEMORY)
   1.468 +		return E_OUTOFMEMORY;
   1.469 +	*status = (pEp_STATUS)_status;
   1.470  
   1.471 -    if (_ctext == NULL)
   1.472 -        return FAIL(L"encrypt_and_sign");
   1.473 +	if (_ctext == NULL)
   1.474 +		return FAIL(L"encrypt_and_sign");
   1.475  
   1.476 -    *ctext = utf16_bstr(_ctext);
   1.477 -    pEp_free(_ctext);
   1.478 +	*ctext = utf16_bstr(_ctext);
   1.479 +	pEp_free(_ctext);
   1.480  
   1.481 -    return S_OK;
   1.482 +	return S_OK;
   1.483  }
   1.484  
   1.485  STDMETHODIMP CpEpEngine::encrypt_b(SAFEARRAY * key_list, SAFEARRAY * ptext, BSTR * ctext, pEp_STATUS * status)
   1.486  {
   1.487 -    assert(key_list);
   1.488 -    assert(ptext);
   1.489 -    assert(ctext);
   1.490 -    assert(status);
   1.491 +	assert(key_list);
   1.492 +	assert(ptext);
   1.493 +	assert(ctext);
   1.494 +	assert(status);
   1.495  
   1.496 -    if (ctext == NULL || ptext == NULL || key_list == NULL || status == NULL) {
   1.497 -        if (ctext)
   1.498 -            *ctext = NULL;
   1.499 -        if (status)
   1.500 -            *status = pEp_UNKNOWN_ERROR;
   1.501 -        return E_INVALIDARG;
   1.502 -    }
   1.503 +	if (ctext == NULL || ptext == NULL || key_list == NULL || status == NULL) {
   1.504 +		if (ctext)
   1.505 +			*ctext = NULL;
   1.506 +		if (status)
   1.507 +			*status = pEp_UNKNOWN_ERROR;
   1.508 +		return E_INVALIDARG;
   1.509 +	}
   1.510  
   1.511 -    HRESULT result = S_OK;
   1.512 +	HRESULT result = S_OK;
   1.513  
   1.514 -    ::stringlist_t *_keylist = new_stringlist(key_list);
   1.515 +	::stringlist_t *_keylist = new_stringlist(key_list);
   1.516  
   1.517 -    char *_ctext = NULL;
   1.518 -    size_t _csize = 0;
   1.519 -    ::PEP_STATUS _status;
   1.520 +	char *_ctext = NULL;
   1.521 +	size_t _csize = 0;
   1.522 +	::PEP_STATUS _status;
   1.523  
   1.524 -    _status = ::encrypt_and_sign(get_session(), _keylist, (const char *) ptext->pvData, ptext->rgsabound[0].cElements, &_ctext, &_csize);
   1.525 -    assert(_status != PEP_OUT_OF_MEMORY);
   1.526 -    ::free_stringlist(_keylist);
   1.527 -    if (_status == PEP_OUT_OF_MEMORY)
   1.528 -        return E_OUTOFMEMORY;
   1.529 -    *status = (pEp_STATUS) _status;
   1.530 +	_status = ::encrypt_and_sign(get_session(), _keylist, (const char *)ptext->pvData, ptext->rgsabound[0].cElements, &_ctext, &_csize);
   1.531 +	assert(_status != PEP_OUT_OF_MEMORY);
   1.532 +	::free_stringlist(_keylist);
   1.533 +	if (_status == PEP_OUT_OF_MEMORY)
   1.534 +		return E_OUTOFMEMORY;
   1.535 +	*status = (pEp_STATUS)_status;
   1.536  
   1.537 -    if (_ctext == NULL)
   1.538 -        return FAIL(L"encrypt_and_sign");
   1.539 +	if (_ctext == NULL)
   1.540 +		return FAIL(L"encrypt_and_sign");
   1.541  
   1.542 -    *status = (pEp_STATUS) _status;
   1.543 -    wchar_t *w_ctext = NULL;
   1.544 -    int w_csize = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, _ctext, _csize, NULL, 0);
   1.545 -    if (w_csize) {
   1.546 -        w_ctext = new wchar_t[w_csize + 1];
   1.547 -        MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, _ctext, _csize, w_ctext, w_csize);
   1.548 -        w_ctext[w_csize] = 0; // this is for debugging; Visual Studio will crash without that if you're unlucky
   1.549 -    }
   1.550 -    *ctext = ::SysAllocStringLen(w_ctext, w_csize);
   1.551 -    assert(ctext);
   1.552 -    delete[] w_ctext;
   1.553 -    pEp_free(_ctext);
   1.554 -    if (ctext == NULL)
   1.555 -        return E_OUTOFMEMORY;
   1.556 +	*status = (pEp_STATUS)_status;
   1.557 +	wchar_t *w_ctext = NULL;
   1.558 +	int w_csize = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, _ctext, _csize, NULL, 0);
   1.559 +	if (w_csize) {
   1.560 +		w_ctext = new wchar_t[w_csize + 1];
   1.561 +		MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, _ctext, _csize, w_ctext, w_csize);
   1.562 +		w_ctext[w_csize] = 0; // this is for debugging; Visual Studio will crash without that if you're unlucky
   1.563 +	}
   1.564 +	*ctext = ::SysAllocStringLen(w_ctext, w_csize);
   1.565 +	assert(ctext);
   1.566 +	delete[] w_ctext;
   1.567 +	pEp_free(_ctext);
   1.568 +	if (ctext == NULL)
   1.569 +		return E_OUTOFMEMORY;
   1.570  
   1.571 -    return S_OK;
   1.572 +	return S_OK;
   1.573  }
   1.574  
   1.575  STDMETHODIMP CpEpEngine::trustword(LONG value, BSTR lang, BSTR * word)
   1.576  {
   1.577 -    assert(value >= 0 && value <= 65535);
   1.578 -    assert(word);
   1.579 +	assert(value >= 0 && value <= 65535);
   1.580 +	assert(word);
   1.581  
   1.582 -    HRESULT result = S_OK;
   1.583 +	HRESULT result = S_OK;
   1.584  
   1.585 -    uint16_t _value = 0;
   1.586 -    if (value < 0 || value > 65535)
   1.587 -        result = E_INVALIDARG;
   1.588 -    else
   1.589 -        _value = (uint16_t) value;
   1.590 +	uint16_t _value = 0;
   1.591 +	if (value < 0 || value > 65535)
   1.592 +		result = E_INVALIDARG;
   1.593 +	else
   1.594 +		_value = (uint16_t)value;
   1.595  
   1.596 -    string _lang = "en";
   1.597 -    if (lang) {
   1.598 -        _lang = utf8_string(lang);
   1.599 -        if (_lang.length() != 2)
   1.600 -            result = E_INVALIDARG;
   1.601 -    }
   1.602 +	string _lang = "en";
   1.603 +	if (lang) {
   1.604 +		_lang = utf8_string(lang);
   1.605 +		if (_lang.length() != 2)
   1.606 +			result = E_INVALIDARG;
   1.607 +	}
   1.608  
   1.609 -    if (word == NULL)
   1.610 -        result = E_INVALIDARG;
   1.611 +	if (word == NULL)
   1.612 +		result = E_INVALIDARG;
   1.613  
   1.614 -    if (result != S_OK)
   1.615 -        return result;
   1.616 +	if (result != S_OK)
   1.617 +		return result;
   1.618  
   1.619 -    char *_word = NULL;
   1.620 -    size_t _wsize = 0;
   1.621 +	char *_word = NULL;
   1.622 +	size_t _wsize = 0;
   1.623  
   1.624 -    PEP_STATUS status = ::trustword(get_session(), _value, _lang.c_str(), &_word, &_wsize);
   1.625 -    assert(status != PEP_OUT_OF_MEMORY);
   1.626 -    if (status == PEP_OUT_OF_MEMORY)
   1.627 -        return E_OUTOFMEMORY;
   1.628 +	PEP_STATUS status = ::trustword(get_session(), _value, _lang.c_str(), &_word, &_wsize);
   1.629 +	assert(status != PEP_OUT_OF_MEMORY);
   1.630 +	if (status == PEP_OUT_OF_MEMORY)
   1.631 +		return E_OUTOFMEMORY;
   1.632  
   1.633 -    if (_word == NULL) {
   1.634 -        *word = NULL;
   1.635 -        return FAIL(L"trustword");
   1.636 -    }
   1.637 -    else {
   1.638 -        *word = utf16_bstr(_word);
   1.639 -        pEp_free(_word);
   1.640 -        return S_OK;
   1.641 -    }
   1.642 +	if (_word == NULL) {
   1.643 +		*word = NULL;
   1.644 +		return FAIL(L"trustword");
   1.645 +	}
   1.646 +	else {
   1.647 +		*word = utf16_bstr(_word);
   1.648 +		pEp_free(_word);
   1.649 +		return S_OK;
   1.650 +	}
   1.651  }
   1.652  
   1.653  STDMETHODIMP CpEpEngine::trustwords(BSTR fpr, BSTR lang, LONG max_words, BSTR * words)
   1.654  {
   1.655 -    assert(fpr);
   1.656 -    assert(max_words >= 0);
   1.657 -    assert(words);
   1.658 +	assert(fpr);
   1.659 +	assert(max_words >= 0);
   1.660 +	assert(words);
   1.661  
   1.662 -    HRESULT result = S_OK;
   1.663 +	HRESULT result = S_OK;
   1.664  
   1.665 -    string _fpr;
   1.666 -    if (fpr)
   1.667 -        _fpr = utf8_string(fpr);
   1.668 -    else
   1.669 -        result = E_INVALIDARG;
   1.670 +	string _fpr;
   1.671 +	if (fpr)
   1.672 +		_fpr = utf8_string(fpr);
   1.673 +	else
   1.674 +		result = E_INVALIDARG;
   1.675  
   1.676 -    string _lang;
   1.677 -    if (lang) {
   1.678 -        _lang = utf8_string(lang);
   1.679 -        if (_lang.length()) {
   1.680 -            if (_lang.length() != 2)
   1.681 -                result = E_INVALIDARG;
   1.682 -        }
   1.683 -        else
   1.684 -            _lang = "en";
   1.685 -    }
   1.686 -    else
   1.687 -        _lang = "en";
   1.688 +	string _lang;
   1.689 +	if (lang) {
   1.690 +		_lang = utf8_string(lang);
   1.691 +		if (_lang.length()) {
   1.692 +			if (_lang.length() != 2)
   1.693 +				result = E_INVALIDARG;
   1.694 +		}
   1.695 +		else
   1.696 +			_lang = "en";
   1.697 +	}
   1.698 +	else
   1.699 +		_lang = "en";
   1.700  
   1.701 -    if (max_words < 0)
   1.702 -        result = E_INVALIDARG;
   1.703 +	if (max_words < 0)
   1.704 +		result = E_INVALIDARG;
   1.705  
   1.706 -    if (words == NULL)
   1.707 -        result = E_INVALIDARG;
   1.708 +	if (words == NULL)
   1.709 +		result = E_INVALIDARG;
   1.710  
   1.711 -    if (result != S_OK)
   1.712 -        return result;
   1.713 +	if (result != S_OK)
   1.714 +		return result;
   1.715  
   1.716 -    char *_words = NULL;
   1.717 -    size_t _wsize = 0;
   1.718 +	char *_words = NULL;
   1.719 +	size_t _wsize = 0;
   1.720  
   1.721 -    PEP_STATUS status = ::trustwords(get_session(), _fpr.c_str(), _lang.c_str(), &_words, &_wsize, max_words);
   1.722 -    assert(status != PEP_OUT_OF_MEMORY);
   1.723 -    if (status == PEP_OUT_OF_MEMORY)
   1.724 -        return E_OUTOFMEMORY;
   1.725 +	PEP_STATUS status = ::trustwords(get_session(), _fpr.c_str(), _lang.c_str(), &_words, &_wsize, max_words);
   1.726 +	assert(status != PEP_OUT_OF_MEMORY);
   1.727 +	if (status == PEP_OUT_OF_MEMORY)
   1.728 +		return E_OUTOFMEMORY;
   1.729  
   1.730 -    if (_words == NULL) {
   1.731 -        *words = NULL;
   1.732 -        return FAIL(L"trustwords");
   1.733 -    }
   1.734 -    else {
   1.735 -        *words = utf16_bstr(_words);
   1.736 -        pEp_free(_words);
   1.737 -        return S_OK;
   1.738 -    }
   1.739 +	if (_words == NULL) {
   1.740 +		*words = NULL;
   1.741 +		return FAIL(L"trustwords");
   1.742 +	}
   1.743 +	else {
   1.744 +		*words = utf16_bstr(_words);
   1.745 +		pEp_free(_words);
   1.746 +		return S_OK;
   1.747 +	}
   1.748  }
   1.749  
   1.750  STDMETHODIMP CpEpEngine::get_crashdump_log(LONG maxlines, BSTR * log)
   1.751  {
   1.752 -    assert(maxlines >= 0);
   1.753 -    assert(log);
   1.754 +	assert(maxlines >= 0);
   1.755 +	assert(log);
   1.756  
   1.757 -    if (!(maxlines >= 0 && log))
   1.758 -        return E_INVALIDARG;
   1.759 +	if (!(maxlines >= 0 && log))
   1.760 +		return E_INVALIDARG;
   1.761  
   1.762 -    char *_log;
   1.763 -    PEP_STATUS status = ::get_crashdump_log(get_session(), (int) maxlines, &_log);
   1.764 -    assert(status == PEP_STATUS_OK);
   1.765 -    if (status == PEP_OUT_OF_MEMORY)
   1.766 -        return E_OUTOFMEMORY;
   1.767 -    if (status != PEP_STATUS_OK || _log == NULL)
   1.768 -        return FAIL(L"get_crashdump_log");
   1.769 +	char *_log;
   1.770 +	PEP_STATUS status = ::get_crashdump_log(get_session(), (int)maxlines, &_log);
   1.771 +	assert(status == PEP_STATUS_OK);
   1.772 +	if (status == PEP_OUT_OF_MEMORY)
   1.773 +		return E_OUTOFMEMORY;
   1.774 +	if (status != PEP_STATUS_OK || _log == NULL)
   1.775 +		return FAIL(L"get_crashdump_log");
   1.776  
   1.777 -    *log = utf16_bstr(_log);
   1.778 -    pEp_free(_log);
   1.779 -    return S_OK;
   1.780 +	*log = utf16_bstr(_log);
   1.781 +	pEp_free(_log);
   1.782 +	return S_OK;
   1.783  }
   1.784  
   1.785  STDMETHODIMP CpEpEngine::get_engine_version(BSTR * engine_version)
   1.786  {
   1.787 -    assert(engine_version);
   1.788 +	assert(engine_version);
   1.789  
   1.790 -    if (!engine_version)
   1.791 -        return E_INVALIDARG;
   1.792 +	if (!engine_version)
   1.793 +		return E_INVALIDARG;
   1.794  
   1.795 -    const char *_enginge_version = ::get_engine_version();
   1.796 +	const char *_enginge_version = ::get_engine_version();
   1.797  
   1.798 -    if (_enginge_version == NULL)
   1.799 -        return FAIL(L"get_engine_version");
   1.800 +	if (_enginge_version == NULL)
   1.801 +		return FAIL(L"get_engine_version");
   1.802  
   1.803 -    *engine_version = utf16_bstr(_enginge_version);
   1.804 +	*engine_version = utf16_bstr(_enginge_version);
   1.805  
   1.806 -    return S_OK;
   1.807 +	return S_OK;
   1.808  }
   1.809  
   1.810  STDMETHODIMP CpEpEngine::get_languagelist(BSTR * languages)
   1.811  {
   1.812 -    assert(languages);
   1.813 +	assert(languages);
   1.814  
   1.815 -    if (!languages)
   1.816 -        return E_INVALIDARG;
   1.817 +	if (!languages)
   1.818 +		return E_INVALIDARG;
   1.819  
   1.820 -    char *_languages;
   1.821 -    PEP_STATUS status = ::get_languagelist(get_session(), &_languages);
   1.822 -    assert(status == PEP_STATUS_OK);
   1.823 -    if (status == PEP_OUT_OF_MEMORY)
   1.824 -        return E_OUTOFMEMORY;
   1.825 -    if (status != PEP_STATUS_OK || _languages == NULL)
   1.826 -        return FAIL(L"get_languagelist");
   1.827 +	char *_languages;
   1.828 +	PEP_STATUS status = ::get_languagelist(get_session(), &_languages);
   1.829 +	assert(status == PEP_STATUS_OK);
   1.830 +	if (status == PEP_OUT_OF_MEMORY)
   1.831 +		return E_OUTOFMEMORY;
   1.832 +	if (status != PEP_STATUS_OK || _languages == NULL)
   1.833 +		return FAIL(L"get_languagelist");
   1.834  
   1.835 -    *languages = utf16_bstr(_languages);
   1.836 -    pEp_free(_languages);
   1.837 -    return S_OK;
   1.838 +	*languages = utf16_bstr(_languages);
   1.839 +	pEp_free(_languages);
   1.840 +	return S_OK;
   1.841  }
   1.842  
   1.843  STDMETHODIMP CpEpEngine::get_phrase(BSTR lang, LONG phrase_id, BSTR * phrase)
   1.844  {
   1.845 -    assert(lang && phrase_id >= 0 && phrase);
   1.846 +	assert(lang && phrase_id >= 0 && phrase);
   1.847  
   1.848 -    if (!(lang && phrase_id >= 0 && phrase))
   1.849 -        return E_INVALIDARG;
   1.850 +	if (!(lang && phrase_id >= 0 && phrase))
   1.851 +		return E_INVALIDARG;
   1.852  
   1.853 -    string _lang = utf8_string(lang);
   1.854 -    assert(_lang.length() == 2);
   1.855 -    if (_lang.length() != 2)
   1.856 -        return E_INVALIDARG;
   1.857 +	string _lang = utf8_string(lang);
   1.858 +	assert(_lang.length() == 2);
   1.859 +	if (_lang.length() != 2)
   1.860 +		return E_INVALIDARG;
   1.861  
   1.862 -    char *_phrase;
   1.863 -    PEP_STATUS status = ::get_phrase(get_session(), _lang.c_str(), (int) phrase_id, &_phrase);
   1.864 -    assert(status == PEP_STATUS_OK);
   1.865 -    if (status == PEP_OUT_OF_MEMORY)
   1.866 -        return E_OUTOFMEMORY;
   1.867 -    if (status != PEP_STATUS_OK || _phrase == NULL)
   1.868 -        return FAIL(L"get_phrase");
   1.869 +	char *_phrase;
   1.870 +	PEP_STATUS status = ::get_phrase(get_session(), _lang.c_str(), (int)phrase_id, &_phrase);
   1.871 +	assert(status == PEP_STATUS_OK);
   1.872 +	if (status == PEP_OUT_OF_MEMORY)
   1.873 +		return E_OUTOFMEMORY;
   1.874 +	if (status != PEP_STATUS_OK || _phrase == NULL)
   1.875 +		return FAIL(L"get_phrase");
   1.876  
   1.877 -    *phrase = utf16_bstr(_phrase);
   1.878 -    pEp_free(_phrase);
   1.879 -    return S_OK;
   1.880 +	*phrase = utf16_bstr(_phrase);
   1.881 +	pEp_free(_phrase);
   1.882 +	return S_OK;
   1.883  }
   1.884  
   1.885  STDMETHODIMP CpEpEngine::get_identity(BSTR address, BSTR user_id, pEp_identity_s * ident)
   1.886  {
   1.887 -    assert(address);
   1.888 -    assert(user_id);
   1.889 -    assert(ident);
   1.890 +	assert(address);
   1.891 +	assert(user_id);
   1.892 +	assert(ident);
   1.893  
   1.894 -    if (address == NULL)
   1.895 -        return E_INVALIDARG;
   1.896 -    if (user_id == NULL)
   1.897 -        return E_INVALIDARG;
   1.898 -    if (ident == NULL)
   1.899 -        return E_INVALIDARG;
   1.900 +	if (address == NULL)
   1.901 +		return E_INVALIDARG;
   1.902 +	if (user_id == NULL)
   1.903 +		return E_INVALIDARG;
   1.904 +	if (ident == NULL)
   1.905 +		return E_INVALIDARG;
   1.906  
   1.907 -    string _address = utf8_string(address);
   1.908 -    string _user_id = utf8_string(user_id);
   1.909 +	string _address = utf8_string(address);
   1.910 +	string _user_id = utf8_string(user_id);
   1.911  
   1.912 -    ::pEp_identity *_ident = NULL;
   1.913 -    PEP_STATUS status = ::get_identity(get_session(), _address.c_str(), _user_id.c_str(), &_ident);
   1.914 -    assert(status != PEP_OUT_OF_MEMORY);
   1.915 -    if (status == PEP_OUT_OF_MEMORY)
   1.916 -        return E_OUTOFMEMORY;
   1.917 +	::pEp_identity *_ident = NULL;
   1.918 +	PEP_STATUS status = ::get_identity(get_session(), _address.c_str(), _user_id.c_str(), &_ident);
   1.919 +	assert(status != PEP_OUT_OF_MEMORY);
   1.920 +	if (status == PEP_OUT_OF_MEMORY)
   1.921 +		return E_OUTOFMEMORY;
   1.922  
   1.923 -    if (_ident == NULL) {
   1.924 -        return FAIL(L"get_identity");
   1.925 -    }
   1.926 +	if (_ident == NULL) {
   1.927 +		return FAIL(L"get_identity");
   1.928 +	}
   1.929  
   1.930 -    copy_identity(ident, _ident);
   1.931 -    ::free_identity(_ident);
   1.932 +	copy_identity(ident, _ident);
   1.933 +	::free_identity(_ident);
   1.934  
   1.935 -    return S_OK;
   1.936 +	return S_OK;
   1.937  }
   1.938  
   1.939  STDMETHODIMP CpEpEngine::set_identity(pEp_identity_s * ident)
   1.940  {
   1.941 -    assert(ident);
   1.942 -    assert(ident->address);
   1.943 -    assert(ident->fpr);
   1.944 -    assert(ident->username);
   1.945 -    assert(ident->user_id);
   1.946 +	assert(ident);
   1.947 +	assert(ident->address);
   1.948 +	assert(ident->fpr);
   1.949 +	assert(ident->username);
   1.950 +	assert(ident->user_id);
   1.951  
   1.952 -    if (ident == NULL || ident->address == NULL || ident->fpr == NULL
   1.953 -        || ident->username == NULL || ident->user_id == NULL)
   1.954 -        return E_INVALIDARG;
   1.955 +	if (ident == NULL || ident->address == NULL || ident->fpr == NULL
   1.956 +		|| ident->username == NULL || ident->user_id == NULL)
   1.957 +		return E_INVALIDARG;
   1.958  
   1.959 -    ::pEp_identity *_ident = new_identity(ident);
   1.960 -    ::PEP_STATUS status = ::set_identity(get_session(), _ident);
   1.961 -    ::free_identity(_ident);
   1.962 +	::pEp_identity *_ident = new_identity(ident);
   1.963 +	::PEP_STATUS status = ::set_identity(get_session(), _ident);
   1.964 +	::free_identity(_ident);
   1.965  
   1.966 -    if (status != ::PEP_STATUS_OK)
   1.967 -        return FAIL(L"set_identity");
   1.968 -    else
   1.969 -        return S_OK;
   1.970 +	if (status != ::PEP_STATUS_OK)
   1.971 +		return FAIL(L"set_identity");
   1.972 +	else
   1.973 +		return S_OK;
   1.974  }
   1.975  
   1.976  STDMETHODIMP CpEpEngine::generate_keypair(pEp_identity_s * ident, BSTR * fpr)
   1.977  {
   1.978 -    assert(ident);
   1.979 -    assert(ident->address);
   1.980 -    assert(ident->username);
   1.981 -    assert(fpr);
   1.982 +	assert(ident);
   1.983 +	assert(ident->address);
   1.984 +	assert(ident->username);
   1.985 +	assert(fpr);
   1.986  
   1.987 -    if (ident == NULL || ident->address == NULL || ident->username == NULL || fpr == NULL)
   1.988 -        return E_INVALIDARG;
   1.989 +	if (ident == NULL || ident->address == NULL || ident->username == NULL || fpr == NULL)
   1.990 +		return E_INVALIDARG;
   1.991  
   1.992 -    ::pEp_identity *_ident = new_identity(ident);
   1.993 -    ::pEp_free(_ident->fpr);
   1.994 -    _ident->fpr = NULL;
   1.995 +	::pEp_identity *_ident = new_identity(ident);
   1.996 +	::pEp_free(_ident->fpr);
   1.997 +	_ident->fpr = NULL;
   1.998  
   1.999 -    ::PEP_STATUS status = ::generate_keypair(get_session(), _ident);
  1.1000 -    assert(status != ::PEP_OUT_OF_MEMORY);
  1.1001 -    if (status == ::PEP_OUT_OF_MEMORY) {
  1.1002 -        ::free_identity(_ident);
  1.1003 -        return E_OUTOFMEMORY;
  1.1004 -    }
  1.1005 +	::PEP_STATUS status = ::generate_keypair(get_session(), _ident);
  1.1006 +	assert(status != ::PEP_OUT_OF_MEMORY);
  1.1007 +	if (status == ::PEP_OUT_OF_MEMORY) {
  1.1008 +		::free_identity(_ident);
  1.1009 +		return E_OUTOFMEMORY;
  1.1010 +	}
  1.1011  
  1.1012 -    if (_ident->fpr)
  1.1013 -        *fpr = utf16_bstr(_ident->fpr);
  1.1014 +	if (_ident->fpr)
  1.1015 +		*fpr = utf16_bstr(_ident->fpr);
  1.1016  
  1.1017 -    ::free_identity(_ident);
  1.1018 +	::free_identity(_ident);
  1.1019  
  1.1020 -    if (status != ::PEP_STATUS_OK)
  1.1021 -        return FAIL(L"generate_keypair");
  1.1022 +	if (status != ::PEP_STATUS_OK)
  1.1023 +		return FAIL(L"generate_keypair");
  1.1024  
  1.1025 -    return S_OK;
  1.1026 +	return S_OK;
  1.1027  }
  1.1028  
  1.1029  STDMETHODIMP CpEpEngine::delete_keypair(BSTR fpr)
  1.1030  {
  1.1031 -    assert(fpr);
  1.1032 +	assert(fpr);
  1.1033  
  1.1034 -    if (fpr == NULL)
  1.1035 -        return E_INVALIDARG;
  1.1036 +	if (fpr == NULL)
  1.1037 +		return E_INVALIDARG;
  1.1038  
  1.1039 -    string _fpr = utf8_string(fpr);
  1.1040 +	string _fpr = utf8_string(fpr);
  1.1041  
  1.1042 -    ::PEP_STATUS status = ::delete_keypair(get_session(), _fpr.c_str());
  1.1043 -    assert(status != PEP_OUT_OF_MEMORY);
  1.1044 -    if (status == PEP_OUT_OF_MEMORY)
  1.1045 -        return E_OUTOFMEMORY;
  1.1046 +	::PEP_STATUS status = ::delete_keypair(get_session(), _fpr.c_str());
  1.1047 +	assert(status != PEP_OUT_OF_MEMORY);
  1.1048 +	if (status == PEP_OUT_OF_MEMORY)
  1.1049 +		return E_OUTOFMEMORY;
  1.1050  
  1.1051 -    if (status != ::PEP_STATUS_OK)
  1.1052 -        return FAIL(L"delete_keypair");
  1.1053 -    else
  1.1054 -        return S_OK;
  1.1055 +	if (status != ::PEP_STATUS_OK)
  1.1056 +		return FAIL(L"delete_keypair");
  1.1057 +	else
  1.1058 +		return S_OK;
  1.1059  }
  1.1060  
  1.1061  STDMETHODIMP CpEpEngine::import_key(BSTR key_data)
  1.1062  {
  1.1063 -    assert(key_data);
  1.1064 +	assert(key_data);
  1.1065  
  1.1066 -    if (key_data == NULL)
  1.1067 -        return E_INVALIDARG;
  1.1068 +	if (key_data == NULL)
  1.1069 +		return E_INVALIDARG;
  1.1070  
  1.1071 -    string _key_data = utf8_string(key_data);
  1.1072 +	string _key_data = utf8_string(key_data);
  1.1073  
  1.1074 -    PEP_STATUS status = ::import_key(get_session(), _key_data.c_str(), _key_data.length(), NULL);
  1.1075 -    assert(status != PEP_OUT_OF_MEMORY);
  1.1076 -    if (status == PEP_OUT_OF_MEMORY)
  1.1077 -        return E_OUTOFMEMORY;
  1.1078 +	PEP_STATUS status = ::import_key(get_session(), _key_data.c_str(), _key_data.length(), NULL);
  1.1079 +	assert(status != PEP_OUT_OF_MEMORY);
  1.1080 +	if (status == PEP_OUT_OF_MEMORY)
  1.1081 +		return E_OUTOFMEMORY;
  1.1082  
  1.1083 -    if (status != pEp_STATUS_OK)
  1.1084 -        return FAIL(L"import_key");
  1.1085 -    else
  1.1086 -        return S_OK;
  1.1087 +	if (status != pEp_STATUS_OK)
  1.1088 +		return FAIL(L"import_key");
  1.1089 +	else
  1.1090 +		return S_OK;
  1.1091  }
  1.1092  
  1.1093  STDMETHODIMP CpEpEngine::import_key_b(SAFEARRAY * key_data)
  1.1094  {
  1.1095 -    assert(key_data);
  1.1096 +	assert(key_data);
  1.1097  
  1.1098 -    if (key_data == NULL)
  1.1099 -        return E_INVALIDARG;
  1.1100 +	if (key_data == NULL)
  1.1101 +		return E_INVALIDARG;
  1.1102  
  1.1103 -    ::PEP_STATUS status = ::import_key(get_session(), (const char *) key_data->pvData, key_data->rgsabound[0].cElements, NULL);
  1.1104 -    assert(status != ::PEP_OUT_OF_MEMORY);
  1.1105 -    if (status == ::PEP_OUT_OF_MEMORY)
  1.1106 -        return E_OUTOFMEMORY;
  1.1107 +	::PEP_STATUS status = ::import_key(get_session(), (const char *)key_data->pvData, key_data->rgsabound[0].cElements, NULL);
  1.1108 +	assert(status != ::PEP_OUT_OF_MEMORY);
  1.1109 +	if (status == ::PEP_OUT_OF_MEMORY)
  1.1110 +		return E_OUTOFMEMORY;
  1.1111  
  1.1112 -    if (status != ::PEP_STATUS_OK)
  1.1113 -        return FAIL(L"import_key");
  1.1114 -    else
  1.1115 -        return S_OK;
  1.1116 +	if (status != ::PEP_STATUS_OK)
  1.1117 +		return FAIL(L"import_key");
  1.1118 +	else
  1.1119 +		return S_OK;
  1.1120  }
  1.1121  
  1.1122  STDMETHODIMP CpEpEngine::export_key(BSTR fpr, BSTR * key_data)
  1.1123  {
  1.1124 -    assert(fpr);
  1.1125 -    assert(key_data);
  1.1126 +	assert(fpr);
  1.1127 +	assert(key_data);
  1.1128  
  1.1129 -    if (fpr == NULL || key_data == NULL)
  1.1130 -        return E_INVALIDARG;
  1.1131 +	if (fpr == NULL || key_data == NULL)
  1.1132 +		return E_INVALIDARG;
  1.1133  
  1.1134 -    string _fpr = utf8_string(fpr);
  1.1135 -    char *_key_data = NULL;
  1.1136 -    size_t _size = 0;
  1.1137 +	string _fpr = utf8_string(fpr);
  1.1138 +	char *_key_data = NULL;
  1.1139 +	size_t _size = 0;
  1.1140  
  1.1141 -    ::PEP_STATUS status = ::export_key(get_session(), _fpr.c_str(), &_key_data, &_size);
  1.1142 -    assert(status != ::PEP_OUT_OF_MEMORY);
  1.1143 -    if (status == ::PEP_OUT_OF_MEMORY)
  1.1144 -        return E_OUTOFMEMORY;
  1.1145 +	::PEP_STATUS status = ::export_key(get_session(), _fpr.c_str(), &_key_data, &_size);
  1.1146 +	assert(status != ::PEP_OUT_OF_MEMORY);
  1.1147 +	if (status == ::PEP_OUT_OF_MEMORY)
  1.1148 +		return E_OUTOFMEMORY;
  1.1149  
  1.1150 -    if (status != ::PEP_STATUS_OK)
  1.1151 -        return FAIL(L"export_key");
  1.1152 +	if (status != ::PEP_STATUS_OK)
  1.1153 +		return FAIL(L"export_key");
  1.1154  
  1.1155 -    _bstr_t b_key_data(utf16_string(_key_data).c_str());
  1.1156 -    pEp_free(_key_data);
  1.1157 -    *key_data = b_key_data.Detach();
  1.1158 +	_bstr_t b_key_data(utf16_string(_key_data).c_str());
  1.1159 +	pEp_free(_key_data);
  1.1160 +	*key_data = b_key_data.Detach();
  1.1161  
  1.1162 -    return S_OK;
  1.1163 +	return S_OK;
  1.1164  }
  1.1165  
  1.1166  STDMETHODIMP CpEpEngine::recv_key(BSTR pattern)
  1.1167  {
  1.1168 -    assert(pattern);
  1.1169 +	assert(pattern);
  1.1170  
  1.1171 -    if (pattern == NULL)
  1.1172 -        return E_INVALIDARG;
  1.1173 +	if (pattern == NULL)
  1.1174 +		return E_INVALIDARG;
  1.1175  
  1.1176 -    string _pattern = utf8_string(pattern);
  1.1177 +	string _pattern = utf8_string(pattern);
  1.1178  
  1.1179 -    PEP_STATUS status = ::recv_key(get_session(), _pattern.c_str());
  1.1180 -    assert(status != PEP_OUT_OF_MEMORY);
  1.1181 -    if (status == PEP_OUT_OF_MEMORY)
  1.1182 -        return E_OUTOFMEMORY;
  1.1183 +	PEP_STATUS status = ::recv_key(get_session(), _pattern.c_str());
  1.1184 +	assert(status != PEP_OUT_OF_MEMORY);
  1.1185 +	if (status == PEP_OUT_OF_MEMORY)
  1.1186 +		return E_OUTOFMEMORY;
  1.1187  
  1.1188 -    if (status != ::PEP_STATUS_OK)
  1.1189 -        return FAIL(L"recv_key");
  1.1190 -    else
  1.1191 -        return S_OK;
  1.1192 +	if (status != ::PEP_STATUS_OK)
  1.1193 +		return FAIL(L"recv_key");
  1.1194 +	else
  1.1195 +		return S_OK;
  1.1196  }
  1.1197  
  1.1198  STDMETHODIMP CpEpEngine::find_keys(BSTR pattern, LPSAFEARRAY * key_list)
  1.1199  {
  1.1200 -    assert(pattern);
  1.1201 -    assert(key_list);
  1.1202 +	assert(pattern);
  1.1203 +	assert(key_list);
  1.1204  
  1.1205 -    if (pattern == NULL || key_list == NULL)
  1.1206 -        return E_INVALIDARG;
  1.1207 +	if (pattern == NULL || key_list == NULL)
  1.1208 +		return E_INVALIDARG;
  1.1209  
  1.1210 -    string _pattern = utf8_string(pattern);
  1.1211 -    ::stringlist_t *_keylist = NULL;
  1.1212 +	string _pattern = utf8_string(pattern);
  1.1213 +	::stringlist_t *_keylist = NULL;
  1.1214  
  1.1215 -    PEP_STATUS status = ::find_keys(get_session(), _pattern.c_str(), &_keylist);
  1.1216 -    assert(status != PEP_OUT_OF_MEMORY);
  1.1217 -    if (status == PEP_OUT_OF_MEMORY)
  1.1218 -        return E_OUTOFMEMORY;
  1.1219 +	PEP_STATUS status = ::find_keys(get_session(), _pattern.c_str(), &_keylist);
  1.1220 +	assert(status != PEP_OUT_OF_MEMORY);
  1.1221 +	if (status == PEP_OUT_OF_MEMORY)
  1.1222 +		return E_OUTOFMEMORY;
  1.1223  
  1.1224 -    if (status != ::PEP_STATUS_OK)
  1.1225 -        return FAIL(L"find_keys");
  1.1226 +	if (status != ::PEP_STATUS_OK)
  1.1227 +		return FAIL(L"find_keys");
  1.1228  
  1.1229 -    if (_keylist && _keylist->value) {
  1.1230 -        *key_list = string_array(_keylist);
  1.1231 -    }
  1.1232 -    else {
  1.1233 -        ::free_stringlist(_keylist);
  1.1234 -        return FAIL(L"find_keys: no keys found");
  1.1235 -    }
  1.1236 +	if (_keylist && _keylist->value) {
  1.1237 +		*key_list = string_array(_keylist);
  1.1238 +	}
  1.1239 +	else {
  1.1240 +		::free_stringlist(_keylist);
  1.1241 +		return FAIL(L"find_keys: no keys found");
  1.1242 +	}
  1.1243  
  1.1244 -    ::free_stringlist(_keylist);
  1.1245 -    return S_OK;
  1.1246 +	::free_stringlist(_keylist);
  1.1247 +	return S_OK;
  1.1248  }
  1.1249  
  1.1250  STDMETHODIMP CpEpEngine::send_key(BSTR pattern)
  1.1251  {
  1.1252 -    assert(pattern);
  1.1253 +	assert(pattern);
  1.1254  
  1.1255 -    if (pattern == NULL)
  1.1256 -        return E_INVALIDARG;
  1.1257 +	if (pattern == NULL)
  1.1258 +		return E_INVALIDARG;
  1.1259  
  1.1260 -    string _pattern = utf8_string(pattern);
  1.1261 +	string _pattern = utf8_string(pattern);
  1.1262  
  1.1263 -    ::PEP_STATUS status = ::send_key(get_session(), _pattern.c_str());
  1.1264 +	::PEP_STATUS status = ::send_key(get_session(), _pattern.c_str());
  1.1265  
  1.1266 -    if (status != ::PEP_STATUS_OK)
  1.1267 -        return FAIL(L"send_key");
  1.1268 -    else
  1.1269 -        return S_OK;
  1.1270 +	if (status != ::PEP_STATUS_OK)
  1.1271 +		return FAIL(L"send_key");
  1.1272 +	else
  1.1273 +		return S_OK;
  1.1274  }
  1.1275  
  1.1276  STDMETHODIMP CpEpEngine::start_keyserver_lookup()
  1.1277  {
  1.1278 -    if (identity_queue.load())
  1.1279 -        return S_OK;
  1.1280 +	if (identity_queue.load())
  1.1281 +		return S_OK;
  1.1282  
  1.1283 -    identity_queue.store(new identity_queue_t());
  1.1284 -    keymanagement_thread = new thread(::do_keymanagement, retrieve_next_identity, (void *) identity_queue.load());
  1.1285 -    
  1.1286 -    return S_OK;
  1.1287 +	identity_queue.store(new identity_queue_t());
  1.1288 +	keymanagement_thread = new thread(::do_keymanagement, retrieve_next_identity, (void *)identity_queue.load());
  1.1289 +
  1.1290 +	return S_OK;
  1.1291  }
  1.1292  
  1.1293  STDMETHODIMP CpEpEngine::stop_keyserver_lookup()
  1.1294  {
  1.1295 -    if (identity_queue.load() == NULL)
  1.1296 -        return S_OK;
  1.1297 +	if (identity_queue.load() == NULL)
  1.1298 +		return S_OK;
  1.1299  
  1.1300 -    identity_queue_t *_iq = identity_queue.load();
  1.1301 -    identity_queue.store(NULL);
  1.1302 +	identity_queue_t *_iq = identity_queue.load();
  1.1303 +	identity_queue.store(NULL);
  1.1304  
  1.1305 -    pEp_identity_cpp shutdown;
  1.1306 -    _iq->push_front(shutdown);
  1.1307 +	pEp_identity_cpp shutdown;
  1.1308 +	_iq->push_front(shutdown);
  1.1309  
  1.1310 -    keymanagement_thread->join();
  1.1311 -    delete keymanagement_thread;
  1.1312 -    keymanagement_thread = NULL;
  1.1313 +	keymanagement_thread->join();
  1.1314 +	delete keymanagement_thread;
  1.1315 +	keymanagement_thread = NULL;
  1.1316  
  1.1317 -    delete _iq;
  1.1318 +	delete _iq;
  1.1319  
  1.1320 -    return S_OK;
  1.1321 +	return S_OK;
  1.1322  }
  1.1323  
  1.1324  STDMETHODIMP CpEpEngine::examine_identity(pEp_identity_s * ident)
  1.1325  {
  1.1326 -    assert(ident);
  1.1327 -    if (ident == NULL)
  1.1328 -        return E_INVALIDARG;
  1.1329 +	assert(ident);
  1.1330 +	if (ident == NULL)
  1.1331 +		return E_INVALIDARG;
  1.1332  
  1.1333 -    if (identity_queue.load() == NULL) {
  1.1334 -        try {
  1.1335 -            identity_queue.load()->push_back(ident);
  1.1336 -        }
  1.1337 -        catch (bad_alloc) {
  1.1338 -            return E_OUTOFMEMORY;
  1.1339 -        }
  1.1340 -    }
  1.1341 +	if (identity_queue.load() == NULL) {
  1.1342 +		try {
  1.1343 +			identity_queue.load()->push_back(ident);
  1.1344 +		}
  1.1345 +		catch (bad_alloc) {
  1.1346 +			return E_OUTOFMEMORY;
  1.1347 +		}
  1.1348 +	}
  1.1349  
  1.1350 -    return S_OK;
  1.1351 +	return S_OK;
  1.1352  }
  1.1353  
  1.1354  STDMETHODIMP CpEpEngine::myself(struct pEp_identity_s *ident, struct pEp_identity_s *result)
  1.1355  {
  1.1356 -    assert(ident);
  1.1357 -    assert(result);
  1.1358 +	assert(ident);
  1.1359 +	assert(result);
  1.1360  
  1.1361 -    if (ident == NULL || result == NULL)
  1.1362 -        return E_INVALIDARG;
  1.1363 +	if (ident == NULL || result == NULL)
  1.1364 +		return E_INVALIDARG;
  1.1365  
  1.1366 -    ::pEp_identity *_ident = new_identity(ident);
  1.1367 -    assert(_ident);
  1.1368 -    if (_ident == NULL)
  1.1369 -        return E_OUTOFMEMORY;
  1.1370 +	::pEp_identity *_ident = new_identity(ident);
  1.1371 +	assert(_ident);
  1.1372 +	if (_ident == NULL)
  1.1373 +		return E_OUTOFMEMORY;
  1.1374  
  1.1375  	// DEBUG CODE - REMOVE BEFORE RELEASE!
  1.1376  	// sync_handshake_result_s handshakeResult;
  1.1377 @@ -835,395 +835,395 @@
  1.1378  	// 
  1.1379  	// HRESULT res2 = Fire_TestEvent(15, _bstr_t( "hallo"));
  1.1380  
  1.1381 -    PEP_STATUS status = ::myself(get_session(), _ident);
  1.1382 +	PEP_STATUS status = ::myself(get_session(), _ident);
  1.1383  
  1.1384 -    if (status == PEP_STATUS_OK) {
  1.1385 -        assert(_ident->fpr);
  1.1386 -        copy_identity(result, _ident);
  1.1387 -        ::free_identity(_ident);
  1.1388 -        return S_OK;
  1.1389 -    }
  1.1390 -    else {
  1.1391 -        ::free_identity(_ident);
  1.1392 -        if (status == PEP_OUT_OF_MEMORY)
  1.1393 -            return E_OUTOFMEMORY;
  1.1394 -        else
  1.1395 -            return FAIL(L"myself");
  1.1396 -    }
  1.1397 +	if (status == PEP_STATUS_OK) {
  1.1398 +		assert(_ident->fpr);
  1.1399 +		copy_identity(result, _ident);
  1.1400 +		::free_identity(_ident);
  1.1401 +		return S_OK;
  1.1402 +	}
  1.1403 +	else {
  1.1404 +		::free_identity(_ident);
  1.1405 +		if (status == PEP_OUT_OF_MEMORY)
  1.1406 +			return E_OUTOFMEMORY;
  1.1407 +		else
  1.1408 +			return FAIL(L"myself");
  1.1409 +	}
  1.1410  }
  1.1411  
  1.1412  STDMETHODIMP CpEpEngine::update_identity(struct pEp_identity_s *ident, struct pEp_identity_s *result)
  1.1413  {
  1.1414 -    assert(ident);
  1.1415 -    assert(result);
  1.1416 +	assert(ident);
  1.1417 +	assert(result);
  1.1418  
  1.1419 -    if (ident == NULL || result == NULL)
  1.1420 -        return E_INVALIDARG;
  1.1421 +	if (ident == NULL || result == NULL)
  1.1422 +		return E_INVALIDARG;
  1.1423  
  1.1424 -    ::pEp_identity *_ident = new_identity(ident);
  1.1425 -    assert(_ident);
  1.1426 -    if (_ident == NULL)
  1.1427 -        return E_OUTOFMEMORY;
  1.1428 +	::pEp_identity *_ident = new_identity(ident);
  1.1429 +	assert(_ident);
  1.1430 +	if (_ident == NULL)
  1.1431 +		return E_OUTOFMEMORY;
  1.1432  
  1.1433 -    PEP_STATUS status = ::update_identity(get_session(), _ident);
  1.1434 +	PEP_STATUS status = ::update_identity(get_session(), _ident);
  1.1435  
  1.1436 -    if (status == PEP_STATUS_OK) {
  1.1437 -        assert(_ident->fpr);
  1.1438 -        copy_identity(result, _ident);
  1.1439 -        ::free_identity(_ident);
  1.1440 -        return S_OK;
  1.1441 -    }
  1.1442 -    else {
  1.1443 -        ::free_identity(_ident);
  1.1444 -        if (status == PEP_OUT_OF_MEMORY)
  1.1445 -            return E_OUTOFMEMORY;
  1.1446 -        else
  1.1447 -            return FAIL(L"update_identity");
  1.1448 -    }
  1.1449 +	if (status == PEP_STATUS_OK) {
  1.1450 +		assert(_ident->fpr);
  1.1451 +		copy_identity(result, _ident);
  1.1452 +		::free_identity(_ident);
  1.1453 +		return S_OK;
  1.1454 +	}
  1.1455 +	else {
  1.1456 +		::free_identity(_ident);
  1.1457 +		if (status == PEP_OUT_OF_MEMORY)
  1.1458 +			return E_OUTOFMEMORY;
  1.1459 +		else
  1.1460 +			return FAIL(L"update_identity");
  1.1461 +	}
  1.1462  }
  1.1463  
  1.1464  STDMETHODIMP CpEpEngine::key_mistrusted(struct pEp_identity_s *ident)
  1.1465  {
  1.1466 -    ::pEp_identity *_ident;
  1.1467 +	::pEp_identity *_ident;
  1.1468  
  1.1469 -    assert(ident);
  1.1470 +	assert(ident);
  1.1471  
  1.1472 -    try {
  1.1473 -        _ident = new_identity(ident);
  1.1474 -    }
  1.1475 -    catch (bad_alloc&) {
  1.1476 -        return E_OUTOFMEMORY;
  1.1477 -    }
  1.1478 -    catch (exception&) {
  1.1479 -        return E_FAIL;
  1.1480 -    }
  1.1481 +	try {
  1.1482 +		_ident = new_identity(ident);
  1.1483 +	}
  1.1484 +	catch (bad_alloc&) {
  1.1485 +		return E_OUTOFMEMORY;
  1.1486 +	}
  1.1487 +	catch (exception&) {
  1.1488 +		return E_FAIL;
  1.1489 +	}
  1.1490  
  1.1491 -    PEP_STATUS status = ::key_mistrusted(get_session(), _ident);
  1.1492 -    free_identity(_ident);
  1.1493 +	PEP_STATUS status = ::key_mistrusted(get_session(), _ident);
  1.1494 +	free_identity(_ident);
  1.1495  
  1.1496 -    if (status == PEP_OUT_OF_MEMORY)
  1.1497 -        return E_OUTOFMEMORY;
  1.1498 +	if (status == PEP_OUT_OF_MEMORY)
  1.1499 +		return E_OUTOFMEMORY;
  1.1500  
  1.1501 -    if (status == PEP_KEY_NOT_FOUND)
  1.1502 -        return FAIL(L"key not found");
  1.1503 +	if (status == PEP_KEY_NOT_FOUND)
  1.1504 +		return FAIL(L"key not found");
  1.1505  
  1.1506 -    if (status != ::PEP_STATUS_OK)
  1.1507 -        return FAIL(L"cannot revoke compromized key");
  1.1508 +	if (status != ::PEP_STATUS_OK)
  1.1509 +		return FAIL(L"cannot revoke compromized key");
  1.1510  
  1.1511 -    return S_OK;
  1.1512 +	return S_OK;
  1.1513  }
  1.1514  
  1.1515  STDMETHODIMP CpEpEngine::key_reset_trust(struct pEp_identity_s *ident)
  1.1516  {
  1.1517 -    ::pEp_identity *_ident;
  1.1518 +	::pEp_identity *_ident;
  1.1519  
  1.1520 -    assert(ident);
  1.1521 +	assert(ident);
  1.1522  
  1.1523 -    try {
  1.1524 -        _ident = new_identity(ident);
  1.1525 -    }
  1.1526 -    catch (bad_alloc&) {
  1.1527 -        return E_OUTOFMEMORY;
  1.1528 -    }
  1.1529 -    catch (exception&) {
  1.1530 -        return E_FAIL;
  1.1531 -    }
  1.1532 +	try {
  1.1533 +		_ident = new_identity(ident);
  1.1534 +	}
  1.1535 +	catch (bad_alloc&) {
  1.1536 +		return E_OUTOFMEMORY;
  1.1537 +	}
  1.1538 +	catch (exception&) {
  1.1539 +		return E_FAIL;
  1.1540 +	}
  1.1541  
  1.1542 -    PEP_STATUS status = ::key_reset_trust(get_session(), _ident);
  1.1543 -    free_identity(_ident);
  1.1544 +	PEP_STATUS status = ::key_reset_trust(get_session(), _ident);
  1.1545 +	free_identity(_ident);
  1.1546  
  1.1547 -    if (status == PEP_OUT_OF_MEMORY)
  1.1548 -        return E_OUTOFMEMORY;
  1.1549 +	if (status == PEP_OUT_OF_MEMORY)
  1.1550 +		return E_OUTOFMEMORY;
  1.1551  
  1.1552 -    if (status == PEP_KEY_NOT_FOUND)
  1.1553 -        return FAIL(L"key not found");
  1.1554 +	if (status == PEP_KEY_NOT_FOUND)
  1.1555 +		return FAIL(L"key not found");
  1.1556  
  1.1557 -    if (status != ::PEP_STATUS_OK)
  1.1558 -        return FAIL(L"cannot reset trust");
  1.1559 +	if (status != ::PEP_STATUS_OK)
  1.1560 +		return FAIL(L"cannot reset trust");
  1.1561  
  1.1562 -    return S_OK;
  1.1563 +	return S_OK;
  1.1564  }
  1.1565  
  1.1566  int CpEpEngine::examine_identity(pEp_identity *ident, void *management)
  1.1567  {
  1.1568 -    assert(ident);
  1.1569 -    assert(management);
  1.1570 -    if (!(ident && management))
  1.1571 -        return -1;
  1.1572 +	assert(ident);
  1.1573 +	assert(management);
  1.1574 +	if (!(ident && management))
  1.1575 +		return -1;
  1.1576  
  1.1577 -    CpEpEngine *me = (CpEpEngine *) management;
  1.1578 +	CpEpEngine *me = (CpEpEngine *)management;
  1.1579  
  1.1580 -    if (me->identity_queue.load() == NULL)
  1.1581 -        return 0;
  1.1582 +	if (me->identity_queue.load() == NULL)
  1.1583 +		return 0;
  1.1584  
  1.1585 -    try {
  1.1586 -        me->identity_queue.load()->push_back(ident);
  1.1587 -    }
  1.1588 -    catch (exception&) {
  1.1589 -        return -1;
  1.1590 -    }
  1.1591 +	try {
  1.1592 +		me->identity_queue.load()->push_back(ident);
  1.1593 +	}
  1.1594 +	catch (exception&) {
  1.1595 +		return -1;
  1.1596 +	}
  1.1597  
  1.1598 -    return 0;
  1.1599 +	return 0;
  1.1600  }
  1.1601  
  1.1602  ::pEp_identity * CpEpEngine::retrieve_next_identity(void *management)
  1.1603  {
  1.1604 -    assert(management);
  1.1605 -    identity_queue_t *iq = (identity_queue_t *) management;
  1.1606 +	assert(management);
  1.1607 +	identity_queue_t *iq = (identity_queue_t *)management;
  1.1608  
  1.1609 -    do /* poll queue */ {
  1.1610 -        if (iq->size())
  1.1611 -            break;
  1.1612 -        ::Sleep(100);
  1.1613 -    } while (true);
  1.1614 +	do /* poll queue */ {
  1.1615 +		if (iq->size())
  1.1616 +			break;
  1.1617 +		::Sleep(100);
  1.1618 +	} while (true);
  1.1619  
  1.1620 -    ::pEp_identity *_ident;
  1.1621 -    pEp_identity_cpp& ident = iq->front();
  1.1622 +	::pEp_identity *_ident;
  1.1623 +	pEp_identity_cpp& ident = iq->front();
  1.1624  
  1.1625 -    if (ident.address.size() == 0)
  1.1626 -        return NULL;
  1.1627 +	if (ident.address.size() == 0)
  1.1628 +		return NULL;
  1.1629  
  1.1630 -    _ident = ident.to_pEp_identity();
  1.1631 -    iq->pop_front();
  1.1632 +	_ident = ident.to_pEp_identity();
  1.1633 +	iq->pop_front();
  1.1634  
  1.1635 -    return _ident;
  1.1636 +	return _ident;
  1.1637  }
  1.1638  
  1.1639  PEP_STATUS CpEpEngine::messageToSend(void * obj, message *msg)
  1.1640  {
  1.1641 -    assert(msg);
  1.1642 -    if (msg == NULL)
  1.1643 -        return PEP_ILLEGAL_VALUE;
  1.1644 +	assert(msg);
  1.1645 +	if (msg == NULL)
  1.1646 +		return PEP_ILLEGAL_VALUE;
  1.1647  
  1.1648 -    text_message _msg;
  1.1649 -    memset(&_msg, 0, sizeof(text_message));
  1.1650 +	text_message _msg;
  1.1651 +	memset(&_msg, 0, sizeof(text_message));
  1.1652  
  1.1653 -    text_message_from_C(&_msg, msg);
  1.1654 -    CpEpEngine *me = (CpEpEngine *) obj;
  1.1655 -    HRESULT r = me->Fire_MessageToSend(&_msg);
  1.1656 -    assert(r == S_OK);
  1.1657 -    clear_text_message(&_msg);
  1.1658 -    if (r == E_OUTOFMEMORY)
  1.1659 -        return PEP_OUT_OF_MEMORY;
  1.1660 -    if (r != S_OK)
  1.1661 -        return PEP_UNKNOWN_ERROR;
  1.1662 +	text_message_from_C(&_msg, msg);
  1.1663 +	CpEpEngine *me = (CpEpEngine *)obj;
  1.1664 +	HRESULT r = me->Fire_MessageToSend(&_msg);
  1.1665 +	assert(r == S_OK);
  1.1666 +	clear_text_message(&_msg);
  1.1667 +	if (r == E_OUTOFMEMORY)
  1.1668 +		return PEP_OUT_OF_MEMORY;
  1.1669 +	if (r != S_OK)
  1.1670 +		return PEP_UNKNOWN_ERROR;
  1.1671  
  1.1672 -    return PEP_STATUS_OK;
  1.1673 +	return PEP_STATUS_OK;
  1.1674  }
  1.1675  
  1.1676  PEP_STATUS CpEpEngine::showHandshake(void * obj, pEp_identity *self, pEp_identity *partner)
  1.1677  {
  1.1678 -    assert(self && partner);
  1.1679 -    if (!(self && partner))
  1.1680 -        return PEP_ILLEGAL_VALUE;
  1.1681 +	assert(self && partner);
  1.1682 +	if (!(self && partner))
  1.1683 +		return PEP_ILLEGAL_VALUE;
  1.1684  
  1.1685 -    pEp_identity_s _self;
  1.1686 -    copy_identity(&_self, self);
  1.1687 -    pEp_identity_s _partner;
  1.1688 -    copy_identity(&_partner, partner);
  1.1689 -    CpEpEngine *me = (CpEpEngine *) obj;
  1.1690 -    sync_handshake_result_s _result;
  1.1691 -    HRESULT r = me->Fire_ShowHandshake(&_self, &_partner, &_result);
  1.1692 -    assert(r == S_OK);
  1.1693 -    clear_identity_s(_self);
  1.1694 -    clear_identity_s(_partner);
  1.1695 -    if (r == E_OUTOFMEMORY)
  1.1696 -        return PEP_OUT_OF_MEMORY;
  1.1697 -    if (r != S_OK)
  1.1698 -        return PEP_UNKNOWN_ERROR;
  1.1699 +	pEp_identity_s _self;
  1.1700 +	copy_identity(&_self, self);
  1.1701 +	pEp_identity_s _partner;
  1.1702 +	copy_identity(&_partner, partner);
  1.1703 +	CpEpEngine *me = (CpEpEngine *)obj;
  1.1704 +	sync_handshake_result_s _result;
  1.1705 +	HRESULT r = me->Fire_ShowHandshake(&_self, &_partner, &_result);
  1.1706 +	assert(r == S_OK);
  1.1707 +	clear_identity_s(_self);
  1.1708 +	clear_identity_s(_partner);
  1.1709 +	if (r == E_OUTOFMEMORY)
  1.1710 +		return PEP_OUT_OF_MEMORY;
  1.1711 +	if (r != S_OK)
  1.1712 +		return PEP_UNKNOWN_ERROR;
  1.1713  
  1.1714 -    PEP_STATUS status = deliverHandshakeResult(me->get_session(), partner, (sync_handshake_result) (int) _result);
  1.1715 -    return status;
  1.1716 +	PEP_STATUS status = deliverHandshakeResult(me->get_session(), partner, (sync_handshake_result)(int)_result);
  1.1717 +	return status;
  1.1718  }
  1.1719  
  1.1720  STDMETHODIMP CpEpEngine::blacklist_add(BSTR fpr)
  1.1721  {
  1.1722 -    assert(fpr);
  1.1723 +	assert(fpr);
  1.1724  
  1.1725 -    string _fpr = utf8_string(fpr);
  1.1726 -    PEP_STATUS status = ::blacklist_add(get_session(), _fpr.c_str());
  1.1727 -    assert(status == PEP_STATUS_OK);
  1.1728 -    if (status != PEP_STATUS_OK)
  1.1729 -        return FAIL(L"blacklist_add failed in pEp engine");
  1.1730 +	string _fpr = utf8_string(fpr);
  1.1731 +	PEP_STATUS status = ::blacklist_add(get_session(), _fpr.c_str());
  1.1732 +	assert(status == PEP_STATUS_OK);
  1.1733 +	if (status != PEP_STATUS_OK)
  1.1734 +		return FAIL(L"blacklist_add failed in pEp engine");
  1.1735  
  1.1736 -    return S_OK;
  1.1737 +	return S_OK;
  1.1738  }
  1.1739  
  1.1740  STDMETHODIMP CpEpEngine::blacklist_delete(BSTR fpr)
  1.1741  {
  1.1742 -    assert(fpr);
  1.1743 +	assert(fpr);
  1.1744  
  1.1745 -    string _fpr = utf8_string(fpr);
  1.1746 -    PEP_STATUS status = ::blacklist_delete(get_session(), _fpr.c_str());
  1.1747 -    assert(status == PEP_STATUS_OK);
  1.1748 -    if (status != PEP_STATUS_OK)
  1.1749 -        return FAIL(L"blacklist_delete failed in pEp engine");
  1.1750 +	string _fpr = utf8_string(fpr);
  1.1751 +	PEP_STATUS status = ::blacklist_delete(get_session(), _fpr.c_str());
  1.1752 +	assert(status == PEP_STATUS_OK);
  1.1753 +	if (status != PEP_STATUS_OK)
  1.1754 +		return FAIL(L"blacklist_delete failed in pEp engine");
  1.1755  
  1.1756 -    return S_OK;
  1.1757 +	return S_OK;
  1.1758  }
  1.1759  
  1.1760  STDMETHODIMP CpEpEngine::blacklist_is_listed(BSTR fpr, VARIANT_BOOL *listed)
  1.1761  {
  1.1762 -    assert(fpr);
  1.1763 -    assert(listed);
  1.1764 +	assert(fpr);
  1.1765 +	assert(listed);
  1.1766  
  1.1767 -    string _fpr = utf8_string(fpr);
  1.1768 -    bool result;
  1.1769 -    PEP_STATUS status = ::blacklist_is_listed(get_session(), _fpr.c_str(), &result);
  1.1770 -    assert(status == PEP_STATUS_OK);
  1.1771 -    if (status != PEP_STATUS_OK)
  1.1772 -        return FAIL(L"blacklist_is_listed failed in pEp engine");
  1.1773 +	string _fpr = utf8_string(fpr);
  1.1774 +	bool result;
  1.1775 +	PEP_STATUS status = ::blacklist_is_listed(get_session(), _fpr.c_str(), &result);
  1.1776 +	assert(status == PEP_STATUS_OK);
  1.1777 +	if (status != PEP_STATUS_OK)
  1.1778 +		return FAIL(L"blacklist_is_listed failed in pEp engine");
  1.1779  
  1.1780 -    *listed = result ? VARIANT_TRUE : VARIANT_FALSE;
  1.1781 -    return S_OK;
  1.1782 +	*listed = result ? VARIANT_TRUE : VARIANT_FALSE;
  1.1783 +	return S_OK;
  1.1784  }
  1.1785  
  1.1786  STDMETHODIMP CpEpEngine::blacklist_retrieve(SAFEARRAY **blacklist)
  1.1787  {
  1.1788 -    assert(blacklist);
  1.1789 +	assert(blacklist);
  1.1790  
  1.1791 -    ::stringlist_t *_blacklist = NULL;
  1.1792 -    PEP_STATUS status = ::blacklist_retrieve(get_session(), &_blacklist);
  1.1793 -    assert(status == PEP_STATUS_OK);
  1.1794 -    if (status != PEP_STATUS_OK)
  1.1795 -        return FAIL(L"blacklist_retrieve failed in pEp engine");
  1.1796 -    assert(_blacklist);
  1.1797 +	::stringlist_t *_blacklist = NULL;
  1.1798 +	PEP_STATUS status = ::blacklist_retrieve(get_session(), &_blacklist);
  1.1799 +	assert(status == PEP_STATUS_OK);
  1.1800 +	if (status != PEP_STATUS_OK)
  1.1801 +		return FAIL(L"blacklist_retrieve failed in pEp engine");
  1.1802 +	assert(_blacklist);
  1.1803  
  1.1804 -    *blacklist = string_array(_blacklist);
  1.1805 -    return S_OK;
  1.1806 +	*blacklist = string_array(_blacklist);
  1.1807 +	return S_OK;
  1.1808  }
  1.1809  
  1.1810  HRESULT CpEpEngine::error(_bstr_t msg)
  1.1811  {
  1.1812 -    _bstr_t helpFile = L"";
  1.1813 -    _bstr_t source = L"pEp COM Adapter";
  1.1814 +	_bstr_t helpFile = L"";
  1.1815 +	_bstr_t source = L"pEp COM Adapter";
  1.1816  
  1.1817 -    ICreateErrorInfo *cei;
  1.1818 -    if (SUCCEEDED(CreateErrorInfo(&cei))) {
  1.1819 -        cei->SetDescription(msg);
  1.1820 -        cei->SetGUID(__uuidof(IpEpEngine));
  1.1821 -        cei->SetHelpContext(0);
  1.1822 -        cei->SetHelpFile(helpFile);
  1.1823 -        cei->SetSource(source);
  1.1824 +	ICreateErrorInfo *cei;
  1.1825 +	if (SUCCEEDED(CreateErrorInfo(&cei))) {
  1.1826 +		cei->SetDescription(msg);
  1.1827 +		cei->SetGUID(__uuidof(IpEpEngine));
  1.1828 +		cei->SetHelpContext(0);
  1.1829 +		cei->SetHelpFile(helpFile);
  1.1830 +		cei->SetSource(source);
  1.1831  
  1.1832 -        IErrorInfo *errinfo;
  1.1833 -        if (SUCCEEDED(cei->QueryInterface(IID_IErrorInfo, (LPVOID FAR*) &errinfo))) {
  1.1834 -            SetErrorInfo(0, errinfo);
  1.1835 -            errinfo->Release();
  1.1836 -        }
  1.1837 -        cei->Release();
  1.1838 -    }
  1.1839 -    return E_FAIL;
  1.1840 +		IErrorInfo *errinfo;
  1.1841 +		if (SUCCEEDED(cei->QueryInterface(IID_IErrorInfo, (LPVOID FAR*) &errinfo))) {
  1.1842 +			SetErrorInfo(0, errinfo);
  1.1843 +			errinfo->Release();
  1.1844 +		}
  1.1845 +		cei->Release();
  1.1846 +	}
  1.1847 +	return E_FAIL;
  1.1848  }
  1.1849  
  1.1850  STDMETHODIMP CpEpEngine::encrypt_message(text_message * src, text_message * dst, SAFEARRAY * extra)
  1.1851  {
  1.1852 -    assert(src);
  1.1853 -    assert(dst);
  1.1854 +	assert(src);
  1.1855 +	assert(dst);
  1.1856  
  1.1857 -    ::message *_src = text_message_to_C(src);
  1.1858 -    ::message *msg_dst;
  1.1859 -    ::stringlist_t *_extra = new_stringlist(extra);
  1.1860 +	::message *_src = text_message_to_C(src);
  1.1861 +	::message *msg_dst;
  1.1862 +	::stringlist_t *_extra = new_stringlist(extra);
  1.1863  
  1.1864  	// TODO: Check whether we should pass any of the encryption flags as last parameter, or expose
  1.1865  	// it to the client. (_PEP_enc_format is intentionally hidden for now...)
  1.1866  	PEP_encrypt_flags_t flags = 0;
  1.1867 -    PEP_STATUS status = ::encrypt_message(get_session(), _src, _extra, &msg_dst, PEP_enc_PGP_MIME, flags);
  1.1868 -    ::free_stringlist(_extra);
  1.1869 +	PEP_STATUS status = ::encrypt_message(get_session(), _src, _extra, &msg_dst, PEP_enc_PGP_MIME, flags);
  1.1870 +	::free_stringlist(_extra);
  1.1871  
  1.1872 -    if (status == PEP_STATUS_OK)
  1.1873 -        text_message_from_C(dst, msg_dst);
  1.1874 -    else
  1.1875 -        text_message_from_C(dst, _src);
  1.1876 +	if (status == PEP_STATUS_OK)
  1.1877 +		text_message_from_C(dst, msg_dst);
  1.1878 +	else
  1.1879 +		text_message_from_C(dst, _src);
  1.1880  
  1.1881 -    ::free_message(msg_dst);
  1.1882 -    ::free_message(_src);
  1.1883 +	::free_message(msg_dst);
  1.1884 +	::free_message(_src);
  1.1885  
  1.1886 -    if (status == PEP_OUT_OF_MEMORY)
  1.1887 -        return E_OUTOFMEMORY;
  1.1888 +	if (status == PEP_OUT_OF_MEMORY)
  1.1889 +		return E_OUTOFMEMORY;
  1.1890  
  1.1891 -    return S_OK;
  1.1892 +	return S_OK;
  1.1893  }
  1.1894  
  1.1895  STDMETHODIMP CpEpEngine::decrypt_message(text_message * src, text_message * dst, SAFEARRAY ** keylist, pEp_rating *rating)
  1.1896  {
  1.1897 -    assert(src);
  1.1898 -    assert(dst);
  1.1899 -    assert(keylist);
  1.1900 -    assert(rating);
  1.1901 +	assert(src);
  1.1902 +	assert(dst);
  1.1903 +	assert(keylist);
  1.1904 +	assert(rating);
  1.1905  
  1.1906 -    *keylist = NULL;
  1.1907 -    *rating = pEp_rating_undefined;
  1.1908 +	*keylist = NULL;
  1.1909 +	*rating = pEp_rating_undefined;
  1.1910  
  1.1911 -    ::message *_src = text_message_to_C(src);
  1.1912 -    ::message *msg_dst = NULL;
  1.1913 -    ::stringlist_t *_keylist;
  1.1914 -    ::PEP_rating _rating;
  1.1915 +	::message *_src = text_message_to_C(src);
  1.1916 +	::message *msg_dst = NULL;
  1.1917 +	::stringlist_t *_keylist;
  1.1918 +	::PEP_rating _rating;
  1.1919  
  1.1920 -    PEP_decrypt_flags_t flags = 0; 
  1.1921 -    PEP_STATUS status = ::decrypt_message(get_session(), _src, &msg_dst, &_keylist, &_rating, &flags);
  1.1922 -    // TODO : output decrypt flags.
  1.1923 +	PEP_decrypt_flags_t flags = 0;
  1.1924 +	PEP_STATUS status = ::decrypt_message(get_session(), _src, &msg_dst, &_keylist, &_rating, &flags);
  1.1925 +	// TODO : output decrypt flags.
  1.1926  
  1.1927 -    if (msg_dst)
  1.1928 -        text_message_from_C(dst, msg_dst);
  1.1929 +	if (msg_dst)
  1.1930 +		text_message_from_C(dst, msg_dst);
  1.1931  
  1.1932 -    ::free_message(_src);
  1.1933 -    ::free_message(msg_dst);
  1.1934 +	::free_message(_src);
  1.1935 +	::free_message(msg_dst);
  1.1936  
  1.1937 -    if (_keylist) {
  1.1938 -        *keylist = string_array(_keylist);
  1.1939 -        free_stringlist(_keylist);
  1.1940 -    }
  1.1941 +	if (_keylist) {
  1.1942 +		*keylist = string_array(_keylist);
  1.1943 +		free_stringlist(_keylist);
  1.1944 +	}
  1.1945  
  1.1946 -    *rating = (pEp_rating) _rating;
  1.1947 +	*rating = (pEp_rating)_rating;
  1.1948  
  1.1949 -    return S_OK;
  1.1950 +	return S_OK;
  1.1951  }
  1.1952  
  1.1953  STDMETHODIMP CpEpEngine::outgoing_message_rating(text_message *msg, pEp_rating * pVal)
  1.1954  {
  1.1955 -    assert(msg);
  1.1956 -    assert(pVal);
  1.1957 +	assert(msg);
  1.1958 +	assert(pVal);
  1.1959  
  1.1960 -    ::message *_msg = text_message_to_C(msg);
  1.1961 +	::message *_msg = text_message_to_C(msg);
  1.1962  
  1.1963 -    PEP_rating _rating;
  1.1964 -    PEP_STATUS status = ::outgoing_message_rating(get_session(), _msg, &_rating);
  1.1965 -    if (status != PEP_STATUS_OK)
  1.1966 -        return FAIL(L"cannot get message rating");
  1.1967 +	PEP_rating _rating;
  1.1968 +	PEP_STATUS status = ::outgoing_message_rating(get_session(), _msg, &_rating);
  1.1969 +	if (status != PEP_STATUS_OK)
  1.1970 +		return FAIL(L"cannot get message rating");
  1.1971  
  1.1972 -    *pVal = (pEp_rating) _rating;
  1.1973 -    return S_OK;
  1.1974 +	*pVal = (pEp_rating)_rating;
  1.1975 +	return S_OK;
  1.1976  }
  1.1977  
  1.1978  STDMETHODIMP CpEpEngine::identity_rating(struct pEp_identity_s *ident, pEp_rating * pVal)
  1.1979  {
  1.1980 -    ::pEp_identity *_ident;
  1.1981 +	::pEp_identity *_ident;
  1.1982  
  1.1983 -    assert(ident);
  1.1984 -    assert(pVal);
  1.1985 +	assert(ident);
  1.1986 +	assert(pVal);
  1.1987  
  1.1988 -    try {
  1.1989 -        _ident = new_identity(ident);
  1.1990 -    }
  1.1991 -    catch (bad_alloc&) {
  1.1992 -        return E_OUTOFMEMORY;
  1.1993 -    }
  1.1994 -    catch (exception&) {
  1.1995 -        return E_FAIL;
  1.1996 -    }
  1.1997 +	try {
  1.1998 +		_ident = new_identity(ident);
  1.1999 +	}
  1.2000 +	catch (bad_alloc&) {
  1.2001 +		return E_OUTOFMEMORY;
  1.2002 +	}
  1.2003 +	catch (exception&) {
  1.2004 +		return E_FAIL;
  1.2005 +	}
  1.2006  
  1.2007 -    PEP_rating _rating;
  1.2008 -    PEP_STATUS status = ::identity_rating(get_session(), _ident, &_rating);
  1.2009 -    free_identity(_ident);
  1.2010 -    if (status != PEP_STATUS_OK)
  1.2011 -        return FAIL(L"cannot get message color");
  1.2012 +	PEP_rating _rating;
  1.2013 +	PEP_STATUS status = ::identity_rating(get_session(), _ident, &_rating);
  1.2014 +	free_identity(_ident);
  1.2015 +	if (status != PEP_STATUS_OK)
  1.2016 +		return FAIL(L"cannot get message color");
  1.2017  
  1.2018 -    *pVal = (pEp_rating) _rating;
  1.2019 -    return S_OK;
  1.2020 +	*pVal = (pEp_rating)_rating;
  1.2021 +	return S_OK;
  1.2022  }
  1.2023  
  1.2024 -STDMETHODIMP CpEpEngine::color_from_rating(pEp_rating rating, pEp_color * pVal) 
  1.2025 +STDMETHODIMP CpEpEngine::color_from_rating(pEp_rating rating, pEp_color * pVal)
  1.2026  {
  1.2027  	assert(pVal);
  1.2028  
  1.2029 @@ -1237,59 +1237,186 @@
  1.2030  
  1.2031  STDMETHODIMP CpEpEngine::trust_personal_key(struct pEp_identity_s *ident, struct pEp_identity_s *result)
  1.2032  {
  1.2033 -    ::pEp_identity *_ident;
  1.2034 +	::pEp_identity *_ident;
  1.2035  
  1.2036 -    assert(ident);
  1.2037 -    assert(result);
  1.2038 +	assert(ident);
  1.2039 +	assert(result);
  1.2040  
  1.2041 -    try {
  1.2042 -        _ident = new_identity(ident);
  1.2043 -    }
  1.2044 -    catch (bad_alloc&) {
  1.2045 -        return E_OUTOFMEMORY;
  1.2046 -    }
  1.2047 -    catch (exception&) {
  1.2048 -        return E_FAIL;
  1.2049 -    }
  1.2050 +	try {
  1.2051 +		_ident = new_identity(ident);
  1.2052 +	}
  1.2053 +	catch (bad_alloc&) {
  1.2054 +		return E_OUTOFMEMORY;
  1.2055 +	}
  1.2056 +	catch (exception&) {
  1.2057 +		return E_FAIL;
  1.2058 +	}
  1.2059  
  1.2060 -    if (verbose_mode) {
  1.2061 -        stringstream ss;
  1.2062 -        ss << "trust_personal_key called with ";
  1.2063 -        ss << utf8_string(ident->address);
  1.2064 -        ss << L": ";
  1.2065 -        ss << ident->comm_type;
  1.2066 -        verbose(ss.str());
  1.2067 -    }
  1.2068 +	if (verbose_mode) {
  1.2069 +		stringstream ss;
  1.2070 +		ss << "trust_personal_key called with ";
  1.2071 +		ss << utf8_string(ident->address);
  1.2072 +		ss << L": ";
  1.2073 +		ss << ident->comm_type;
  1.2074 +		verbose(ss.str());
  1.2075 +	}
  1.2076  
  1.2077 -    PEP_STATUS status = ::trust_personal_key(get_session(), _ident);
  1.2078 +	PEP_STATUS status = ::trust_personal_key(get_session(), _ident);
  1.2079  
  1.2080 -    if (verbose_mode) {
  1.2081 -        stringstream ss;
  1.2082 -        ss << "result ";
  1.2083 -        ss << status;
  1.2084 -        ss << " for ";
  1.2085 -        ss << _ident->address;
  1.2086 -        ss << L": ";
  1.2087 -        ss << _ident->comm_type;
  1.2088 -        verbose(ss.str());
  1.2089 -    }
  1.2090 +	if (verbose_mode) {
  1.2091 +		stringstream ss;
  1.2092 +		ss << "result ";
  1.2093 +		ss << status;
  1.2094 +		ss << " for ";
  1.2095 +		ss << _ident->address;
  1.2096 +		ss << L": ";
  1.2097 +		ss << _ident->comm_type;
  1.2098 +		verbose(ss.str());
  1.2099 +	}
  1.2100  
  1.2101 -    if (status == PEP_STATUS_OK)
  1.2102 -        copy_identity(result, _ident);
  1.2103 +	if (status == PEP_STATUS_OK)
  1.2104 +		copy_identity(result, _ident);
  1.2105  
  1.2106 -    free_identity(_ident);
  1.2107 -    if (status == PEP_OUT_OF_MEMORY)
  1.2108 -        return E_OUTOFMEMORY;
  1.2109 -    else if (status != PEP_STATUS_OK)
  1.2110 -        return FAIL(L"failure while executing trust_personal_key()");
  1.2111 +	free_identity(_ident);
  1.2112 +	if (status == PEP_OUT_OF_MEMORY)
  1.2113 +		return E_OUTOFMEMORY;
  1.2114 +	else if (status != PEP_STATUS_OK)
  1.2115 +		return FAIL(L"failure while executing trust_personal_key()");
  1.2116  
  1.2117 -    return S_OK;
  1.2118 +	return S_OK;
  1.2119 +}
  1.2120 +
  1.2121 +// keysync api
  1.2122 +
  1.2123 +std::mutex CpEpEngine::keysync_mutex;
  1.2124 +std::condition_variable CpEpEngine::keysync_condition;
  1.2125 +std::thread *CpEpEngine::keysync_thread = NULL;
  1.2126 +std::queue<void*> CpEpEngine::keysync_queue;
  1.2127 +bool CpEpEngine::keysync_thread_running = FALSE;
  1.2128 +bool CpEpEngine::keysync_abort_requested = FALSE;
  1.2129 +PEP_SESSION CpEpEngine::keysync_session;
  1.2130 +
  1.2131 +STDMETHODIMP CpEpEngine::start_keysync()
  1.2132 +{
  1.2133 +	// acquire the lock
  1.2134 +	std::unique_lock<std::mutex> lock(keysync_mutex);
  1.2135 +
  1.2136 +	// Check if we're already running.
  1.2137 +	if (keysync_thread_running)
  1.2138 +		return S_OK;
  1.2139 +
  1.2140 +	// Check if a concurrent abort was requested and is in progress
  1.2141 +	if (keysync_abort_requested)
  1.2142 +		return S_FALSE;
  1.2143 +
  1.2144 +	PEP_STATUS status = ::init(&keysync_session);
  1.2145 +	assert(status = PEP_STATUS_OK);
  1.2146 +
  1.2147 +	// we pass the address of our mutex as management pointer, to do safety checks.
  1.2148 +	keysync_thread = new thread(::do_sync_protocol, keysync_session, &keysync_mutex);
  1.2149 +
  1.2150 +	// flag to signal we're running
  1.2151 +	keysync_thread_running = true;
  1.2152 +
  1.2153 +	return S_OK;
  1.2154 +}
  1.2155 +
  1.2156 +STDMETHODIMP CpEpEngine::stop_keysync()
  1.2157 +{
  1.2158 +	// acquire the lock
  1.2159 +	std::unique_lock<std::mutex> lock(keysync_mutex);
  1.2160 +
  1.2161 +	// check whether we're actually running, or there's a concurrent abort
  1.2162 +	if (keysync_abort_requested || !keysync_thread_running)
  1.2163 +		return S_OK;
  1.2164 +
  1.2165 +	// signal that we're gonna abort
  1.2166 +	keysync_abort_requested = true;
  1.2167 +
  1.2168 +	// Notify the keysync thread
  1.2169 +	keysync_condition.notify_all();
  1.2170 +
  1.2171 +	// Wait for the other thread to finish and clean up
  1.2172 +	while (keysync_thread_running)
  1.2173 +		keysync_condition.wait(lock);
  1.2174 +
  1.2175 +	// wait for the thread to end
  1.2176 +	keysync_thread->join();
  1.2177 +
  1.2178 +	// clean up
  1.2179 +	delete keysync_thread;
  1.2180 +	keysync_thread = NULL;
  1.2181 +	release(keysync_session);
  1.2182 +	keysync_abort_requested = false;
  1.2183 +}
  1.2184 +
  1.2185 +int CpEpEngine::inject_sync_msg(void * msg, void * management)
  1.2186 +{
  1.2187 +	// check argument
  1.2188 +	if (!msg)
  1.2189 +		return E_INVALIDARG;
  1.2190 +
  1.2191 +	// sanity check
  1.2192 +	if (management != &keysync_mutex)
  1.2193 +		return ERROR_INVALID_HANDLE;
  1.2194 +
  1.2195 +	// acquire the lock
  1.2196 +	std::unique_lock<std::mutex> lock(keysync_mutex);
  1.2197 +
  1.2198 +	// check whether we're running:
  1.2199 +	if (keysync_abort_requested || !keysync_thread_running)
  1.2200 +		return E_ASYNC_OPERATION_NOT_STARTED;
  1.2201 +
  1.2202 +	// queue the message
  1.2203 +	keysync_queue.push(msg);
  1.2204 +
  1.2205 +	// notify the receivers
  1.2206 +	keysync_condition.notify_all();
  1.2207 +}
  1.2208 +
  1.2209 +void * CpEpEngine::retreive_next_sync_msg(void * management)
  1.2210 +{
  1.2211 +	// sanity check
  1.2212 +	assert(management == &keysync_mutex);
  1.2213 +
  1.2214 +	// acquire the lock
  1.2215 +	std::unique_lock<std::mutex> lock(keysync_mutex);
  1.2216 +
  1.2217 +	// as long as we're supposed to be active 
  1.2218 +	// (we won't wait for the queue to empty currently...)
  1.2219 +	while (!keysync_abort_requested)
  1.2220 +	{
  1.2221 +		// If the queue is empty, wait for a signal, and try again.
  1.2222 +		if (keysync_queue.empty())
  1.2223 +		{
  1.2224 +			keysync_condition.wait(lock);
  1.2225 +			continue;
  1.2226 +		}
  1.2227 +
  1.2228 +		// Pop the message and return it.
  1.2229 +		void* msg = keysync_queue.front();
  1.2230 +		assert(msg);
  1.2231 +
  1.2232 +		keysync_queue.pop();
  1.2233 +		
  1.2234 +		return msg;
  1.2235 +	}
  1.2236 +
  1.2237 +	// we acknowledge that we're quitting...
  1.2238 +	keysync_thread_running == false;
  1.2239 +
  1.2240 +	// We signal the main thread that we got his signal
  1.2241 +	// so it can gain the mutex again and call join() on us.
  1.2242 +	keysync_condition.notify_all();
  1.2243 +
  1.2244 +	// and tell the pep engine we're done.
  1.2245 +	return NULL;
  1.2246  }
  1.2247  
  1.2248  
  1.2249  // Event callbacks
  1.2250  
  1.2251 -STDMETHODIMP CpEpEngine::register_callbacks(IpEpEngineCallbacks* new_callbacks) 
  1.2252 +STDMETHODIMP CpEpEngine::register_callbacks(IpEpEngineCallbacks* new_callbacks)
  1.2253  {
  1.2254  	callbacks cbs = get_callbacks();
  1.2255  	vector<IpEpEngineCallbacks*>& vec = cbs;
  1.2256 @@ -1300,7 +1427,7 @@
  1.2257  	return S_OK;
  1.2258  }
  1.2259  
  1.2260 -STDMETHODIMP CpEpEngine::unregister_callbacks(IpEpEngineCallbacks* obsolete_callbacks) 
  1.2261 +STDMETHODIMP CpEpEngine::unregister_callbacks(IpEpEngineCallbacks* obsolete_callbacks)
  1.2262  {
  1.2263  	callbacks cbs = get_callbacks();
  1.2264  	vector<IpEpEngineCallbacks*>& vec = cbs;
  1.2265 @@ -1316,35 +1443,35 @@
  1.2266  }
  1.2267  
  1.2268  STDMETHODIMP CpEpEngine::OpenPGP_list_keyinfo(BSTR search_pattern, LPSAFEARRAY* keyinfo_list) {
  1.2269 -    assert(keyinfo_list);
  1.2270 -    
  1.2271 -    if (keyinfo_list == NULL)
  1.2272 -        return E_INVALIDARG;
  1.2273 +	assert(keyinfo_list);
  1.2274  
  1.2275 -    string _pattern = "";
  1.2276 -    if (search_pattern)
  1.2277 -        _pattern = utf8_string(search_pattern);
  1.2278 -    ::stringpair_list_t* _keyinfo_list = NULL;
  1.2279 +	if (keyinfo_list == NULL)
  1.2280 +		return E_INVALIDARG;
  1.2281  
  1.2282 -    PEP_STATUS status = ::OpenPGP_list_keyinfo(get_session(), _pattern.c_str(), &_keyinfo_list);
  1.2283 -    assert(status != PEP_OUT_OF_MEMORY);
  1.2284 -    if (status == PEP_OUT_OF_MEMORY)
  1.2285 -        return E_OUTOFMEMORY;
  1.2286 +	string _pattern = "";
  1.2287 +	if (search_pattern)
  1.2288 +		_pattern = utf8_string(search_pattern);
  1.2289 +	::stringpair_list_t* _keyinfo_list = NULL;
  1.2290  
  1.2291 -    if (status != ::PEP_STATUS_OK)
  1.2292 -        return FAIL(L"OpenPGP_list_keyinfo");
  1.2293 +	PEP_STATUS status = ::OpenPGP_list_keyinfo(get_session(), _pattern.c_str(), &_keyinfo_list);
  1.2294 +	assert(status != PEP_OUT_OF_MEMORY);
  1.2295 +	if (status == PEP_OUT_OF_MEMORY)
  1.2296 +		return E_OUTOFMEMORY;
  1.2297  
  1.2298 -    if (_keyinfo_list && _keyinfo_list->value) {
  1.2299 -        ::opt_field_array_from_C(_keyinfo_list, keyinfo_list);
  1.2300 -    }
  1.2301 -    else {
  1.2302 -        ::free_stringpair_list(_keyinfo_list);
  1.2303 -        return FAIL(L"OpenPGP_list_keyinfo: no keys found");
  1.2304 -    }
  1.2305 +	if (status != ::PEP_STATUS_OK)
  1.2306 +		return FAIL(L"OpenPGP_list_keyinfo");
  1.2307  
  1.2308 -    ::free_stringpair_list(_keyinfo_list);
  1.2309 -    return S_OK;
  1.2310 -    
  1.2311 +	if (_keyinfo_list && _keyinfo_list->value) {
  1.2312 +		::opt_field_array_from_C(_keyinfo_list, keyinfo_list);
  1.2313 +	}
  1.2314 +	else {
  1.2315 +		::free_stringpair_list(_keyinfo_list);
  1.2316 +		return FAIL(L"OpenPGP_list_keyinfo: no keys found");
  1.2317 +	}
  1.2318 +
  1.2319 +	::free_stringpair_list(_keyinfo_list);
  1.2320 +	return S_OK;
  1.2321 +
  1.2322  }
  1.2323  
  1.2324  HRESULT CpEpEngine::Fire_MessageToSend(text_message * msg)
  1.2325 @@ -1354,7 +1481,7 @@
  1.2326  
  1.2327  	assert(msg);
  1.2328  
  1.2329 -	for (auto it = vec.begin(); it != vec.end(); ++it) 
  1.2330 +	for (auto it = vec.begin(); it != vec.end(); ++it)
  1.2331  	{
  1.2332  		auto res = (*it)->MessageToSend(msg);
  1.2333  		if (res != S_OK)
     2.1 --- a/CpEpEngine.h	Fri Sep 30 22:21:09 2016 +0200
     2.2 +++ b/CpEpEngine.h	Sat Oct 01 01:08:29 2016 +0200
     2.3 @@ -8,6 +8,7 @@
     2.4  #include "locked_queue.hh"
     2.5  #include "utf8_helper.h"
     2.6  #include "pEp_utility.h"
     2.7 +#include <queue>
     2.8  
     2.9  
    2.10  #if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
    2.11 @@ -37,7 +38,7 @@
    2.12          PEP_STATUS status = ::init(&m_session);
    2.13          assert(status == PEP_STATUS_OK);
    2.14          ::register_examine_function(m_session, CpEpEngine::examine_identity, (void *)this);
    2.15 -        ::register_sync_callbacks(m_session, (void*)this, messageToSend, showHandshake, NULL, NULL);
    2.16 +        ::register_sync_callbacks(m_session, (void*)this, messageToSend, showHandshake, inject_sync_msg, retreive_next_sync_msg);
    2.17          ::log_event(m_session, "Startup", "pEp COM Adapter", NULL, NULL);
    2.18      }
    2.19  
    2.20 @@ -137,9 +138,12 @@
    2.21  
    2.22      typedef locked_queue<pEp_identity_cpp> identity_queue_t;
    2.23      static ::pEp_identity * retrieve_next_identity(void *management);
    2.24 -    static PEP_STATUS messageToSend(void *obj, message *msg);
    2.25 +    static PEP_STATUS messageToSend(void * obj, message *msg);
    2.26      static PEP_STATUS showHandshake(void * obj, pEp_identity *self, pEp_identity *partner);
    2.27  
    2.28 +	static int inject_sync_msg(void *msg, void* management);
    2.29 +	static void* retreive_next_sync_msg(void* management);
    2.30 +
    2.31      HRESULT error(_bstr_t msg);
    2.32  
    2.33      void verbose(string text)
    2.34 @@ -161,6 +165,15 @@
    2.35  	mutex callback_mutex;
    2.36  	vector<IpEpEngineCallbacks*> callback_vector;
    2.37  
    2.38 +	// Keysync members
    2.39 +	static std::mutex keysync_mutex;
    2.40 +	static std::condition_variable keysync_condition;
    2.41 +	static std::thread *keysync_thread;
    2.42 +	static std::queue<void*> keysync_queue;
    2.43 +	static bool keysync_thread_running;
    2.44 +	static bool keysync_abort_requested;
    2.45 +	static PEP_SESSION keysync_session;
    2.46 +
    2.47  public:
    2.48      // runtime config of the adapter
    2.49  
    2.50 @@ -208,6 +221,10 @@
    2.51      STDMETHOD(key_reset_trust)(struct pEp_identity_s *ident);
    2.52      STDMETHOD(trust_personal_key)(struct pEp_identity_s *ident, struct pEp_identity_s *result);
    2.53  
    2.54 +	// keysync API
    2.55 +	STDMETHOD(start_keysync)();
    2.56 +	STDMETHOD(stop_keysync)();
    2.57 +
    2.58      // Blacklist API
    2.59  
    2.60      STDMETHOD(blacklist_add)(BSTR fpr);
     3.1 --- a/pEpCOMServerAdapter.idl	Fri Sep 30 22:21:09 2016 +0200
     3.2 +++ b/pEpCOMServerAdapter.idl	Sat Oct 01 01:08:29 2016 +0200
     3.3 @@ -190,6 +190,8 @@
     3.4  
     3.5      HRESULT start_keyserver_lookup();
     3.6      HRESULT stop_keyserver_lookup();
     3.7 +	HRESULT start_keysync();
     3.8 +	HRESULT stop_keysync();
     3.9  
    3.10      HRESULT examine_identity([in] struct pEp_identity_s * ident);
    3.11      HRESULT myself([in] struct pEp_identity_s *ident, [out, retval] struct pEp_identity_s *result);