CpEpEngine.cpp
branchCOM-87
changeset 285 9a19b5dbb271
parent 282 3b92433f8da7
parent 284 83ad8d765099
child 289 71e95ca19464
     1.1 --- a/CpEpEngine.cpp	Thu Apr 26 15:30:43 2018 +0200
     1.2 +++ b/CpEpEngine.cpp	Mon Apr 30 11:52:00 2018 +0200
     1.3 @@ -3,6 +3,7 @@
     1.4  #include "stdafx.h"
     1.5  #include "CpEpEngine.h"
     1.6  #include <mutex>
     1.7 +#include "GateKeeper.h"
     1.8  
     1.9  using namespace std;
    1.10  using namespace pEp::utility;
    1.11 @@ -917,6 +918,57 @@
    1.12      return S_OK;
    1.13  }
    1.14  
    1.15 +STDMETHODIMP CpEpEngine::EncryptMessageAndAddPrivKey(TextMessage * src, TextMessage * dst, BSTR to_fpr, pEpEncryptFlags flags, pEpEncFormat encFormat)
    1.16 +{
    1.17 +    assert(src);
    1.18 +    assert(dst);
    1.19 +    assert(to_fpr);
    1.20 +
    1.21 +    if (!(src && dst))
    1.22 +        return E_INVALIDARG;
    1.23 +
    1.24 +    ::message *_src = text_message_to_C(src);
    1.25 +
    1.26 +    _PEP_enc_format _encFormat = (_PEP_enc_format)encFormat;
    1.27 +
    1.28 +    // COM-19: Initialize msg_dst to NULL, or we end up calling
    1.29 +    // free_message() below with a pointer to random garbage in
    1.30 +    // case of an error in encrypt_message().
    1.31 +    ::message *msg_dst = NULL;
    1.32 +
    1.33 +    string _to_fpr = utf8_string(to_fpr);
    1.34 +                                                    // _PEP_enc_format used to be intentionally hardcoded to PEP_enc_PEP:
    1.35 +                                                    // Since COM-74, this has been changed to an explicit parameter, to allow the engine to attach
    1.36 +                                                    // the keys and headers to outgoing, unencrypted messages.
    1.37 +    PEP_encrypt_flags_t engineFlags = (PEP_encrypt_flags_t)flags;
    1.38 +    PEP_STATUS status = ::encrypt_message_and_add_priv_key(get_session(), _src, &msg_dst, _to_fpr.c_str(), _encFormat, engineFlags);
    1.39 +
    1.40 +    if (status == PEP_STATUS_OK)
    1.41 +        text_message_from_C(dst, msg_dst);
    1.42 +    else
    1.43 +        text_message_from_C(dst, _src);
    1.44 +
    1.45 +    ::free_message(msg_dst);
    1.46 +    ::free_message(_src);
    1.47 +
    1.48 +    if (status == PEP_OUT_OF_MEMORY)
    1.49 +        return E_OUTOFMEMORY;
    1.50 +
    1.51 +    // COM-41: Enhanced PEP status handling
    1.52 +    if ((status != PEP_STATUS_OK) && (status < PEP_UNENCRYPTED || status >= PEP_TRUSTWORD_NOT_FOUND))
    1.53 +        return FAIL("Failure to encrypt message", status);
    1.54 +
    1.55 +    // Statii like PEP_UNENCRYPTED due to no private key
    1.56 +    // should not be a catastrophic failure here. Using S_FALSE
    1.57 +    // still allows clients to differentiate with S_OK,
    1.58 +    // although this does not work out of the box with
    1.59 +    // the standard .NET mapping of COM.
    1.60 +    if (status != PEP_STATUS_OK)
    1.61 +        return S_FALSE;
    1.62 +
    1.63 +    return S_OK;
    1.64 +}
    1.65 +
    1.66  STDMETHODIMP CpEpEngine::EncryptMessageForSelf(pEpIdentity * targetId, TextMessage * src,
    1.67      /* [in] */ SAFEARRAY *extra, TextMessage * dst, pEpEncryptFlags flags)
    1.68  {
    1.69 @@ -1424,6 +1476,20 @@
    1.70      return msg;
    1.71  }
    1.72  
    1.73 +// Force an update check now
    1.74 +STDMETHODIMP CpEpEngine::UpdateNow()
    1.75 +{
    1.76 +    try
    1.77 +    {
    1.78 +        ::pEp::GateKeeper::update_now();
    1.79 +    }
    1.80 +    catch (bad_alloc&) {
    1.81 +        return E_OUTOFMEMORY;
    1.82 +    }
    1.83 +    catch (exception& ex) {
    1.84 +        return FAIL(ex.what());;
    1.85 +    }
    1.86 +}
    1.87  
    1.88  // Event callbacks
    1.89