ENGINE-109: moved get_trustwords, added bool for long (all) vs. short trustword lists
authorKrista Grothoff <krista@pep-project.org>
Tue, 18 Oct 2016 17:21:51 +0200
changeset 1307f4a8b0035541
parent 1301 16c1adcfaed3
child 1308 df40e76a86fc
ENGINE-109: moved get_trustwords, added bool for long (all) vs. short trustword lists
src/message_api.c
src/message_api.h
src/pEpEngine.c
src/pEpEngine.h
test/trustwords_test.cc
     1.1 --- a/src/message_api.c	Tue Oct 18 11:55:17 2016 +0200
     1.2 +++ b/src/message_api.c	Tue Oct 18 17:21:51 2016 +0200
     1.3 @@ -1907,3 +1907,85 @@
     1.4      assert(false);
     1.5  }
     1.6  
     1.7 +DYNAMIC_API PEP_STATUS get_trustwords(
     1.8 +    PEP_SESSION session, pEp_identity* id1, pEp_identity* id2,
     1.9 +    const char* lang, char **words, size_t *wsize, bool full
    1.10 +)
    1.11 +{
    1.12 +    assert(session);
    1.13 +    assert(id1);
    1.14 +    assert(id2);
    1.15 +    assert(id1->fpr);
    1.16 +    assert(id2->fpr);
    1.17 +    assert(words);
    1.18 +    assert(wsize);
    1.19 +    
    1.20 +    if (!(session && id1 && id2 && words && wsize) ||
    1.21 +        !(id1->fpr) || (!id2->fpr))
    1.22 +        return PEP_ILLEGAL_VALUE;
    1.23 +    
    1.24 +    const char *source1 = id1->fpr;
    1.25 +    const char *source2 = id2->fpr;
    1.26 +    
    1.27 +    *words = NULL;
    1.28 +    *wsize = 0;
    1.29 +
    1.30 +    const size_t SHORT_NUM_TWORDS = 5;
    1.31 +    
    1.32 +    // N.B. THIS will have to be changed once we start checking trustword entropy.
    1.33 +    // For now, full is ALL, and otherwise it's 5-per-id.
    1.34 +    size_t max_words_per_id = (full ? 0 : SHORT_NUM_TWORDS);
    1.35 +    
    1.36 +    char* first_set = NULL;
    1.37 +    char* second_set = NULL;
    1.38 +    size_t first_wsize = 0;
    1.39 +    size_t second_wsize = 0;
    1.40 +    PEP_STATUS status = PEP_UNKNOWN_ERROR;
    1.41 +    
    1.42 +    char* _retstr = NULL;
    1.43 +    
    1.44 +    if (source1 > source2) {
    1.45 +        status = trustwords(session, source2, lang, &first_set, &first_wsize, max_words_per_id);
    1.46 +        if (status != PEP_STATUS_OK)
    1.47 +            goto error_release;
    1.48 +        status = trustwords(session, source1, lang, &second_set, &second_wsize, max_words_per_id); 
    1.49 +        if (status != PEP_STATUS_OK)
    1.50 +            goto error_release;
    1.51 +    }
    1.52 +    else {
    1.53 +        status = trustwords(session, source1, lang, &first_set, &first_wsize, max_words_per_id);
    1.54 +        if (status != PEP_STATUS_OK)
    1.55 +            goto error_release;
    1.56 +        status = trustwords(session, source2, lang, &second_set, &second_wsize, max_words_per_id); 
    1.57 +        if (status != PEP_STATUS_OK)
    1.58 +            goto error_release;
    1.59 +    }
    1.60 +    size_t _wsize = first_wsize + second_wsize;
    1.61 +    
    1.62 +    _retstr = calloc(1, _wsize + 1);
    1.63 +    
    1.64 +    size_t len = strlcpy(_retstr, first_set, _wsize);
    1.65 +    if (len >= _wsize) {
    1.66 +        status = PEP_UNKNOWN_ERROR;
    1.67 +        goto error_release;
    1.68 +    }
    1.69 +    strlcat(_retstr, second_set, _wsize);
    1.70 +    if (len >= _wsize){
    1.71 +        status = PEP_UNKNOWN_ERROR;
    1.72 +        goto error_release;
    1.73 +    }
    1.74 +    
    1.75 +    *words = _retstr;
    1.76 +    *wsize = _wsize;
    1.77 +    status = PEP_STATUS_OK;
    1.78 +    
    1.79 +    goto the_end;
    1.80 +    
    1.81 +    error_release:
    1.82 +    free(_retstr);
    1.83 +    
    1.84 +    the_end:
    1.85 +    free(first_set);
    1.86 +    free(second_set);
    1.87 +    return status;
    1.88 +}
    1.89 \ No newline at end of file
     2.1 --- a/src/message_api.h	Tue Oct 18 11:55:17 2016 +0200
     2.2 +++ b/src/message_api.h	Tue Oct 18 17:21:51 2016 +0200
     2.3 @@ -234,6 +234,36 @@
     2.4  //                          **path is owned by the library, do not change it!
     2.5  DYNAMIC_API PEP_STATUS get_binary_path(PEP_cryptotech tech, const char **path);
     2.6  
     2.7 +// get_trustwords() - get full trustwords string for a *pair* of identities
     2.8 +//
     2.9 +//    parameters:
    2.10 +//        session (in)        session handle
    2.11 +//        id1 (in)            identity of first party in communication - fpr can't be NULL  
    2.12 +//        id2 (in)            identity of second party in communication - fpr can't be NULL
    2.13 +//        lang (in)           C string with ISO 639-1 language code
    2.14 +//        words (out)         pointer to C string with all trustwords UTF-8 encoded,
    2.15 +//                            separated by a blank each
    2.16 +//                            NULL if language is not supported or trustword
    2.17 +//                            wordlist is damaged or unavailable
    2.18 +//        wsize (out)         length of full trustwords string
    2.19 +//        full (in)           if true, generate ALL trustwords for these identities.
    2.20 +//                            else, generate a fixed-size subset. (TODO: fixed-minimum-entropy
    2.21 +//                            subset in next version)
    2.22 +//
    2.23 +//    return value:
    2.24 +//        PEP_STATUS_OK            trustwords retrieved
    2.25 +//        PEP_OUT_OF_MEMORY        out of memory
    2.26 +//        PEP_TRUSTWORD_NOT_FOUND  at least one trustword not found
    2.27 +//
    2.28 +//    caveat:
    2.29 +//        the word pointer goes to the ownership of the caller
    2.30 +//        the caller is responsible to free() it (on Windoze use pEp_free())
    2.31 +//
    2.32 +
    2.33 +DYNAMIC_API PEP_STATUS get_trustwords(
    2.34 +    PEP_SESSION session, pEp_identity* id1, pEp_identity* id2,
    2.35 +    const char* lang, char **words, size_t *wsize, bool full
    2.36 +);
    2.37  
    2.38  #ifdef __cplusplus
    2.39  }
     3.1 --- a/src/pEpEngine.c	Tue Oct 18 11:55:17 2016 +0200
     3.2 +++ b/src/pEpEngine.c	Tue Oct 18 17:21:51 2016 +0200
     3.3 @@ -877,85 +877,6 @@
     3.4      return PEP_STATUS_OK;
     3.5  }
     3.6  
     3.7 -DYNAMIC_API PEP_STATUS get_trustwords(
     3.8 -    PEP_SESSION session, pEp_identity* id1, pEp_identity* id2,
     3.9 -    const char* lang, char **words, size_t *wsize, int max_words_per_id
    3.10 -)
    3.11 -{
    3.12 -    assert(session);
    3.13 -    assert(id1);
    3.14 -    assert(id2);
    3.15 -    assert(id1->fpr);
    3.16 -    assert(id2->fpr);
    3.17 -    assert(words);
    3.18 -    assert(wsize);
    3.19 -    assert(max_words_per_id >= 0);
    3.20 -    
    3.21 -    if (!(session && id1 && id2 && words && wsize && max_words_per_id >= 0) ||
    3.22 -        !(id1->fpr) || (!id2->fpr))
    3.23 -        return PEP_ILLEGAL_VALUE;
    3.24 -    
    3.25 -    const char *source1 = id1->fpr;
    3.26 -    const char *source2 = id2->fpr;
    3.27 -    
    3.28 -    *words = NULL;
    3.29 -    *wsize = 0;
    3.30 -
    3.31 -    char* first_set = NULL;
    3.32 -    char* second_set = NULL;
    3.33 -    size_t first_wsize = 0;
    3.34 -    size_t second_wsize = 0;
    3.35 -    PEP_STATUS status = PEP_UNKNOWN_ERROR;
    3.36 -
    3.37 -    char* _retstr = NULL;
    3.38 -    
    3.39 -    if (source1 > source2) {
    3.40 -        status = trustwords(session, source2, lang, &first_set, &first_wsize, max_words_per_id);
    3.41 -        if (status != PEP_STATUS_OK)
    3.42 -            goto error_release;
    3.43 -        status = trustwords(session, source1, lang, &second_set, &second_wsize, max_words_per_id); 
    3.44 -        if (status != PEP_STATUS_OK)
    3.45 -            goto error_release;
    3.46 -    }
    3.47 -    else {
    3.48 -        status = trustwords(session, source1, lang, &first_set, &first_wsize, max_words_per_id);
    3.49 -        if (status != PEP_STATUS_OK)
    3.50 -            goto error_release;
    3.51 -        status = trustwords(session, source2, lang, &second_set, &second_wsize, max_words_per_id); 
    3.52 -        if (status != PEP_STATUS_OK)
    3.53 -            goto error_release;
    3.54 -    }
    3.55 -    size_t _wsize = first_wsize + second_wsize;
    3.56 -    
    3.57 -    _retstr = calloc(1, _wsize + 1);
    3.58 -
    3.59 -    size_t len = strlcpy(_retstr, first_set, _wsize);
    3.60 -    if (len >= _wsize) {
    3.61 -        status = PEP_UNKNOWN_ERROR;
    3.62 -        goto error_release;
    3.63 -    }
    3.64 -    strlcat(_retstr, second_set, _wsize);
    3.65 -    if (len >= _wsize){
    3.66 -        status = PEP_UNKNOWN_ERROR;
    3.67 -        goto error_release;
    3.68 -    }
    3.69 -    
    3.70 -    *words = _retstr;
    3.71 -    *wsize = _wsize;
    3.72 -    status = PEP_STATUS_OK;
    3.73 -    
    3.74 -    goto the_end;
    3.75 -    
    3.76 -error_release:
    3.77 -    free(_retstr);
    3.78 -    
    3.79 -the_end:
    3.80 -    free(first_set);
    3.81 -    free(second_set);
    3.82 -    return status;
    3.83 -}
    3.84 -
    3.85 -
    3.86  pEp_identity *new_identity(
    3.87          const char *address, const char *fpr, const char *user_id,
    3.88          const char *username
     4.1 --- a/src/pEpEngine.h	Tue Oct 18 11:55:17 2016 +0200
     4.2 +++ b/src/pEpEngine.h	Tue Oct 18 17:21:51 2016 +0200
     4.3 @@ -530,44 +530,6 @@
     4.4          PEP_SESSION session, const pEp_identity *identity
     4.5      );
     4.6  
     4.7 -// get_trustwords() - get full trustwords string for a *pair* of identities
     4.8 -//
     4.9 -//    parameters:
    4.10 -//        session (in)        session handle
    4.11 -//        id1 (in)            identity of first party in communication - fpr can't be NULL  
    4.12 -//        id2 (in)            identity of second party in communication - fpr can't be NULL
    4.13 -//        lang (in)           C string with ISO 639-1 language code
    4.14 -//        words (out)         pointer to C string with all trustwords UTF-8 encoded,
    4.15 -//                            separated by a blank each
    4.16 -//                            NULL if language is not supported or trustword
    4.17 -//                            wordlist is damaged or unavailable
    4.18 -//        wsize (out)         length of full trustwords string
    4.19 -//        max_words_per_id (in) generate at most max_words_per_id *per input identity* (output
    4.20 -//                              string can have 2 times this value)
    4.21 -//                              if max_words_per_id == 0 there is no such limit
    4.22 -//
    4.23 -//    return value:
    4.24 -//        PEP_STATUS_OK            trustwords retrieved
    4.25 -//        PEP_OUT_OF_MEMORY        out of memory
    4.26 -//        PEP_TRUSTWORD_NOT_FOUND  at least one trustword not found
    4.27 -//
    4.28 -//    caveat:
    4.29 -//        the word pointer goes to the ownership of the caller
    4.30 -//        the caller is responsible to free() it (on Windoze use pEp_free())
    4.31 -//
    4.32 -//  Warning from trustwords(), as this function uses trustwords() as an underlying function:
    4.33 -//  DON'T USE THIS FUNCTION FROM HIGH LEVEL LANGUAGES!
    4.34 -//
    4.35 -//  Better implement a simple one in the adapter yourself using trustword(), and
    4.36 -//  return a list of trustwords.
    4.37 -//  This function is provided for being used by C and C++ programs only.
    4.38 -
    4.39 -DYNAMIC_API PEP_STATUS get_trustwords(
    4.40 -    PEP_SESSION session, pEp_identity* id1, pEp_identity* id2,
    4.41 -    const char* lang, char **words, size_t *wsize, int max_words_per_id
    4.42 -);
    4.43 -
    4.44 -
    4.45  // set_device_group() - update own person's device group
    4.46  //
    4.47  //    parameters:
    4.48 @@ -1044,6 +1006,7 @@
    4.49  //
    4.50  DYNAMIC_API const char* get_engine_version();
    4.51  
    4.52 +
    4.53  DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session);
    4.54  
    4.55  #ifdef __cplusplus
     5.1 --- a/test/trustwords_test.cc	Tue Oct 18 11:55:17 2016 +0200
     5.2 +++ b/test/trustwords_test.cc	Tue Oct 18 17:21:51 2016 +0200
     5.3 @@ -2,6 +2,7 @@
     5.4  #include <string>
     5.5  #include <assert.h>
     5.6  #include "pEpEngine.h"
     5.7 +#include "message_api.h"
     5.8  
     5.9  using namespace std;
    5.10  
    5.11 @@ -48,7 +49,7 @@
    5.12      cout << words2 << "\n";
    5.13  
    5.14      cout << "\nfinding German trustwords for " << identity1->address << " and " << identity2->address << "...\n";
    5.15 -    get_trustwords(session, identity1, identity2, "de", &full_wordlist, &wsize_full, 5);
    5.16 +    get_trustwords(session, identity1, identity2, "de", &full_wordlist, &wsize_full, false);
    5.17      assert(full_wordlist);
    5.18      cout << full_wordlist << "\n";
    5.19