src/message_api.c
branchENGINE-199
changeset 1753 01faac16cec6
parent 1734 6d5ea8793614
parent 1748 58ae886d5079
child 1754 a9e54ca6c043
     1.1 --- a/src/message_api.c	Fri Apr 21 21:39:42 2017 +0200
     1.2 +++ b/src/message_api.c	Mon May 08 08:18:47 2017 +0200
     1.3 @@ -794,6 +794,7 @@
     1.4          return PEP_rating_unencrypted;
     1.5  
     1.6      case PEP_DECRYPTED:
     1.7 +    case PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH:
     1.8          return PEP_rating_unreliable;
     1.9  
    1.10      case PEP_DECRYPTED_AND_VERIFIED:
    1.11 @@ -2251,88 +2252,6 @@
    1.12      return PEP_color_no_color;
    1.13  }
    1.14  
    1.15 -static bool _is_valid_hex(const char* hexstr) {
    1.16 -    if (!hexstr)
    1.17 -        return false;
    1.18 -
    1.19 -    const char* curr = hexstr;
    1.20 -    char currchar;
    1.21 -
    1.22 -    for (currchar = *curr; currchar != '\0'; currchar = *(++curr)) {
    1.23 -        if ((currchar >= '0' && currchar <= '9') ||
    1.24 -            (currchar >= 'a' && currchar <= 'f') ||
    1.25 -            (currchar >= 'A' && currchar <= 'F'))
    1.26 -        {
    1.27 -            continue;
    1.28 -        }
    1.29 -        return false;
    1.30 -    }
    1.31 -    return true;
    1.32 -}
    1.33 -
    1.34 -// Returns, in comparison: 1 if fpr1 > fpr2, 0 if equal, -1 if fpr1 < fpr2
    1.35 -static PEP_STATUS _compare_fprs(const char* fpr1, const char* fpr2, int* comparison) {
    1.36 -
    1.37 -    const int _FULL_FINGERPRINT_LENGTH = 40;
    1.38 -    const int _ASCII_LOWERCASE_OFFSET = 32;
    1.39 -
    1.40 -    size_t fpr1_len = strlen(fpr1);
    1.41 -    size_t fpr2_len = strlen(fpr2);
    1.42 -
    1.43 -    if (fpr1_len != _FULL_FINGERPRINT_LENGTH || fpr2_len != _FULL_FINGERPRINT_LENGTH)
    1.44 -        return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
    1.45 -
    1.46 -    if (!_is_valid_hex(fpr1) || !_is_valid_hex(fpr2))
    1.47 -        return PEP_ILLEGAL_VALUE;
    1.48 -
    1.49 -    const char* fpr1_curr = fpr1;
    1.50 -    const char* fpr2_curr = fpr2;
    1.51 -
    1.52 -    char current;
    1.53 -
    1.54 -    // Advance past leading zeros.
    1.55 -    for (current = *fpr1_curr; current != '0' && current != '\0'; current = *(++fpr1_curr), fpr1_len--);
    1.56 -    for (current = *fpr2_curr; current != '0' && current != '\0'; current = *(++fpr2_curr), fpr2_len--);
    1.57 -
    1.58 -    if (fpr1_len == fpr2_len) {
    1.59 -        char digit1;
    1.60 -        char digit2;
    1.61 -
    1.62 -        while (fpr1_curr && *fpr1_curr != '\0') {
    1.63 -            digit1 = *fpr1_curr++;
    1.64 -            digit2 = *fpr2_curr++;
    1.65 -
    1.66 -            // Adjust for case-insensitive compare
    1.67 -            if (digit1 >= 'a' && digit1 <= 'f')
    1.68 -                digit1 -= _ASCII_LOWERCASE_OFFSET;
    1.69 -            if (digit2 >= 'a' && digit2 <= 'f')
    1.70 -                digit2 -= _ASCII_LOWERCASE_OFFSET;
    1.71 -
    1.72 -            // We take advantage of the fact that 'a'-'f' are larger
    1.73 -            // integer values in the ASCII table than '0'-'9'.
    1.74 -            // This allows us to compare digits directly.
    1.75 -            if (digit1 > digit2) {
    1.76 -                *comparison = 1;
    1.77 -                return PEP_STATUS_OK;
    1.78 -            } else if (digit1 < digit2) {
    1.79 -                *comparison = -1;
    1.80 -                return PEP_STATUS_OK;
    1.81 -            }
    1.82 -
    1.83 -            // pointers already advanced above. Keep going.
    1.84 -        }
    1.85 -        *comparison = 0;
    1.86 -        return PEP_STATUS_OK;
    1.87 -    }
    1.88 -    else if (fpr1_len > fpr2_len) {
    1.89 -        *comparison = 1;
    1.90 -        return PEP_STATUS_OK;
    1.91 -    }
    1.92 -    // Otherwise, fpr1_len < fpr2_len
    1.93 -    *comparison = -1;
    1.94 -    return PEP_STATUS_OK;
    1.95 -}
    1.96 -
    1.97  DYNAMIC_API PEP_STATUS get_trustwords(
    1.98      PEP_SESSION session, const pEp_identity* id1, const pEp_identity* id2,
    1.99      const char* lang, char **words, size_t *wsize, bool full
   1.100 @@ -2368,7 +2287,7 @@
   1.101      size_t second_wsize = 0;
   1.102  
   1.103      int fpr_comparison = -255;
   1.104 -    PEP_STATUS status = _compare_fprs(source1, source2, &fpr_comparison);
   1.105 +    PEP_STATUS status = _compare_fprs(source1, strlen(source1), source2, strlen(source2), &fpr_comparison);
   1.106      if (status != PEP_STATUS_OK)
   1.107          return status;
   1.108  
   1.109 @@ -2556,6 +2475,12 @@
   1.110      PEP_decrypt_flags_t *flags
   1.111  )
   1.112  {
   1.113 +    assert(mimetext);
   1.114 +    assert(mime_plaintext);
   1.115 +    assert(keylist);
   1.116 +    assert(rating);
   1.117 +    assert(flags);
   1.118 +
   1.119      PEP_STATUS status = PEP_STATUS_OK;
   1.120      message* tmp_msg = NULL;
   1.121      message* dec_msg = NULL;
   1.122 @@ -2570,12 +2495,24 @@
   1.123                                                  keylist,
   1.124                                                  rating,
   1.125                                                  flags);
   1.126 +                                                
   1.127 +    if (!dec_msg && (decrypt_status == PEP_UNENCRYPTED || decrypt_status == PEP_VERIFIED)) {
   1.128 +        dec_msg = message_dup(tmp_msg);
   1.129 +    }
   1.130 +        
   1.131      if (decrypt_status > PEP_CANNOT_DECRYPT_UNKNOWN)
   1.132      {
   1.133          status = decrypt_status;
   1.134          goto pep_error;
   1.135      }
   1.136  
   1.137 +    assert(dec_msg);
   1.138 +    
   1.139 +    if (!dec_msg) {
   1.140 +        status = PEP_UNKNOWN_ERROR;
   1.141 +        goto pep_error;
   1.142 +    }
   1.143 +
   1.144      status = mime_encode_message(dec_msg, false, mime_plaintext);
   1.145  
   1.146      if (status == PEP_STATUS_OK)
   1.147 @@ -2621,6 +2558,12 @@
   1.148      if (status != PEP_STATUS_OK)
   1.149          GOTO(pep_error);
   1.150  
   1.151 +
   1.152 +    if (!enc_msg) {
   1.153 +        status = PEP_UNKNOWN_ERROR;
   1.154 +        goto pep_error;
   1.155 +    }
   1.156 +
   1.157      status = mime_encode_message(enc_msg, false, mime_ciphertext);
   1.158  
   1.159  pep_error:
   1.160 @@ -2659,6 +2602,11 @@
   1.161                                        flags);
   1.162      if (status != PEP_STATUS_OK)
   1.163          goto pep_error;
   1.164 + 
   1.165 +    if (!enc_msg) {
   1.166 +        status = PEP_UNKNOWN_ERROR;
   1.167 +        goto pep_error;
   1.168 +    }
   1.169  
   1.170      status = mime_encode_message(enc_msg, false, mime_ciphertext);
   1.171