Merged in ENGINE-440
authorKrista Bennett <krista@pep-project.org>
Tue, 26 Jun 2018 12:06:24 +0200
changeset 2767e6746b18e6d5
parent 2759 3e2762e73975
parent 2766 966e18e80c8e
child 2769 e9eb2387f9d1
child 2770 08d7cf271f57
Merged in ENGINE-440
     1.1 --- a/src/message_api.c	Fri Jun 22 13:58:07 2018 +0200
     1.2 +++ b/src/message_api.c	Tue Jun 26 12:06:24 2018 +0200
     1.3 @@ -148,6 +148,43 @@
     1.4      }
     1.5  }
     1.6  
     1.7 +bool _memnmemn(const char* needle, 
     1.8 +                size_t needle_size,
     1.9 +                const char* haystack, 
    1.10 +                size_t haystack_size) 
    1.11 +{
    1.12 +    if (needle_size > haystack_size) {
    1.13 +        return false;
    1.14 +    }
    1.15 +    else if (needle_size == 0) {
    1.16 +        return true;
    1.17 +    }
    1.18 +                        
    1.19 +    bool found = true;
    1.20 +    const char* haystack_ptr = haystack;
    1.21 +    unsigned int i = 0;
    1.22 +    size_t remaining_hay = haystack_size;
    1.23 +    for (i = 0; i < haystack_size && (remaining_hay >= needle_size); i++, haystack_ptr++) {
    1.24 +        found = false;
    1.25 +        const char* needle_ptr = needle;
    1.26 +        if (*haystack_ptr == *needle) {
    1.27 +            const char* haystack_tmp = haystack_ptr;
    1.28 +            unsigned int j;
    1.29 +            found = true;
    1.30 +            for (j = 0; j < needle_size; j++) {
    1.31 +                if (*needle_ptr++ != *haystack_tmp++) {
    1.32 +                    found = false;
    1.33 +                    break;
    1.34 +                }
    1.35 +            }
    1.36 +            if (found)
    1.37 +                break;
    1.38 +        }
    1.39 +        remaining_hay--;
    1.40 +    }
    1.41 +    return found;
    1.42 +}
    1.43 +
    1.44  void add_opt_field(message *msg, const char *name, const char *value)
    1.45  {
    1.46      assert(msg && name && value);
    1.47 @@ -1103,18 +1140,18 @@
    1.48          return false;
    1.49  
    1.50      if (strcmp(blob->mime_type, "application/octet-stream") == 0) {
    1.51 -        if (strcmp(ext, ".pgp") == 0 || strcmp(ext, ".gpg") == 0 ||
    1.52 -            strcmp(ext, ".asc") == 0)
    1.53 +        if (strcmp(ext, ".pgp") == 0 || strcmp(ext, ".gpg") == 0)
    1.54              return true;
    1.55      }
    1.56 -    else if (strcmp(blob->mime_type, "text/plain") == 0) {
    1.57 -        if (strcmp(ext, ".asc") == 0) {
    1.58 -            // NOTE: if this ends up being too expensive, we can implement
    1.59 -            // strnstr...
    1.60 -            if (strstr(blob->value, "BEGIN PGP PUBLIC KEY") == NULL &&
    1.61 -                strstr(blob->value, "BEGIN PGP PRIVATE KEY") == NULL)
    1.62 -                return true;
    1.63 -        }
    1.64 +    if (strcmp(ext, ".asc") == 0 && blob->size > 0) {            
    1.65 +        const char* pubk_needle = "BEGIN PGP PUBLIC KEY";
    1.66 +        size_t pubk_needle_size = strlen(pubk_needle);
    1.67 +        const char* privk_needle = "BEGIN PGP PRIVATE KEY";
    1.68 +        size_t privk_needle_size = strlen(privk_needle);
    1.69 +
    1.70 +        if (!(_memnmemn(pubk_needle, pubk_needle_size, blob->value, blob->size)) &&
    1.71 +            !(_memnmemn(privk_needle, privk_needle_size, blob->value, blob->size)))
    1.72 +            return true;
    1.73      }
    1.74  
    1.75      return false;
    1.76 @@ -1365,9 +1402,7 @@
    1.77              size_t blob_size = bl->size;
    1.78              bool free_blobval = false;
    1.79              
    1.80 -            if (is_encrypted_attachment(bl) &&
    1.81 -                    strstr(blob_value, "BEGIN PGP PUBLIC KEY") == NULL &&
    1.82 -                    strstr(blob_value, "BEGIN PGP PRIVATE KEY") == NULL) {
    1.83 +            if (is_encrypted_attachment(bl)) {
    1.84                      
    1.85                  char* bl_ptext = NULL;
    1.86                  size_t bl_psize = 0;