ENGINE_275: returned subject replacement for non-pEp recipients ENGINE-275
authorKrista Bennett <krista@pep-project.org>
Fri, 13 Oct 2017 13:14:33 +0200
branchENGINE-275
changeset 2157bb39881db930
parent 2156 0df79faabcc7
child 2159 b417e92107ab
ENGINE_275: returned subject replacement for non-pEp recipients
src/message_api.c
     1.1 --- a/src/message_api.c	Fri Oct 13 11:02:02 2017 +0200
     1.2 +++ b/src/message_api.c	Fri Oct 13 13:14:33 2017 +0200
     1.3 @@ -266,6 +266,65 @@
     1.4      return ptext;
     1.5  }
     1.6  
     1.7 +static char * combine_short_and_long(const char *shortmsg, const char *longmsg)
     1.8 +{
     1.9 +    assert(shortmsg);
    1.10 +    
    1.11 +    unsigned char pepstr[] = PEP_SUBJ_STRING;
    1.12 +    assert(strcmp(shortmsg, "pEp") != 0 && _unsigned_signed_strcmp(pepstr, shortmsg, PEP_SUBJ_BYTELEN) != 0); 
    1.13 +    
    1.14 +    if (!shortmsg || strcmp(shortmsg, "pEp") == 0 || 
    1.15 +                     _unsigned_signed_strcmp(pepstr, shortmsg, PEP_SUBJ_BYTELEN) == 0) {
    1.16 +        if (!longmsg) {
    1.17 +            return NULL;
    1.18 +        }
    1.19 +        else {
    1.20 +            char *result = strdup(longmsg);
    1.21 +            assert(result);
    1.22 +            return result;
    1.23 +        }
    1.24 +    }
    1.25 +
    1.26 +    if (longmsg == NULL)
    1.27 +        longmsg = "";
    1.28 +
    1.29 +    const char * const newlines = "\n\n";
    1.30 +    const size_t NL_LEN = 2;
    1.31 +
    1.32 +    const size_t bufsize = PEP_SUBJ_KEY_LEN + strlen(shortmsg) + NL_LEN + strlen(longmsg) + 1;
    1.33 +    char * ptext = calloc(1, bufsize);
    1.34 +    assert(ptext);
    1.35 +    if (ptext == NULL)
    1.36 +        return NULL;
    1.37 +
    1.38 +    strlcpy(ptext, PEP_SUBJ_KEY, bufsize);
    1.39 +    strlcat(ptext, shortmsg, bufsize);
    1.40 +    strlcat(ptext, newlines, bufsize);
    1.41 +    strlcat(ptext, longmsg, bufsize);
    1.42 +
    1.43 +    return ptext;
    1.44 +}
    1.45 +
    1.46 +static PEP_STATUS replace_subject(message* msg) {
    1.47 +    unsigned char pepstr[] = PEP_SUBJ_STRING;
    1.48 +    if (msg->shortmsg && *(msg->shortmsg) != '\0') {
    1.49 +        char* longmsg = combine_short_and_long(msg->shortmsg, msg->longmsg);
    1.50 +        if (!longmsg)
    1.51 +            return PEP_OUT_OF_MEMORY;
    1.52 +        else {
    1.53 +            free(msg->longmsg);
    1.54 +            msg->longmsg = longmsg;
    1.55 +        }
    1.56 +    }
    1.57 +    free(msg->shortmsg);
    1.58 +    msg->shortmsg = strdup((char*)pepstr);
    1.59 +    
    1.60 +    if (!msg->shortmsg)
    1.61 +        return PEP_OUT_OF_MEMORY;
    1.62 +    
    1.63 +    return PEP_STATUS_OK;
    1.64 +}
    1.65 +
    1.66  /* 
    1.67     WARNING: For the moment, this only works for the first line of decrypted
    1.68     plaintext because we don't need more. IF WE DO, THIS MUST BE EXPANDED, or
    1.69 @@ -1226,6 +1285,12 @@
    1.70          if ((max_comm_type | PEP_ct_confirmed) == PEP_ct_pEp) {
    1.71              _src = wrap_message_as_attachment(NULL, src);
    1.72          }
    1.73 +        else {
    1.74 +            // hide subject
    1.75 +            status = replace_subject(_src);
    1.76 +            if (status == PEP_OUT_OF_MEMORY)
    1.77 +                goto enomem;
    1.78 +        }
    1.79          if (!(flags & PEP_encrypt_flag_force_no_attached_key))
    1.80              attach_own_key(session, _src);
    1.81