merging
authorVolker Birk <vb@pep-project.org>
Tue, 10 May 2016 12:15:14 +0200
changeset 5862eac603617fe
parent 585 8ec129931074
parent 584 3f175e8668b3
child 587 ed8aa5e5b02c
merging
     1.1 --- a/src/keymanagement.c	Tue May 10 12:03:45 2016 +0200
     1.2 +++ b/src/keymanagement.c	Tue May 10 12:15:14 2016 +0200
     1.3 @@ -541,3 +541,128 @@
     1.4      return status;
     1.5  }
     1.6  
     1.7 +DYNAMIC_API PEP_STATUS own_key_add(PEP_SESSION session, const char *fpr)
     1.8 +{
     1.9 +    PEP_STATUS status = PEP_STATUS_OK;
    1.10 +    
    1.11 +    assert(session && fpr && fpr[0]);
    1.12 +    
    1.13 +    if (!(session && fpr && fpr[0]))
    1.14 +        return PEP_ILLEGAL_VALUE;
    1.15 +    
    1.16 +    sqlite3_reset(session->own_key_add);
    1.17 +    sqlite3_bind_text(session->own_key_add, 1, fpr, -1, SQLITE_STATIC);
    1.18 +    
    1.19 +    int result;
    1.20 +    
    1.21 +    result = sqlite3_step(session->own_key_add);
    1.22 +    switch (result) {
    1.23 +        case SQLITE_DONE:
    1.24 +            status = PEP_STATUS_OK;
    1.25 +            break;
    1.26 +            
    1.27 +        default:
    1.28 +            status = PEP_UNKNOWN_ERROR;
    1.29 +    }
    1.30 +    
    1.31 +    sqlite3_reset(session->own_key_add);
    1.32 +    return status;
    1.33 +}
    1.34 +
    1.35 +DYNAMIC_API PEP_STATUS own_key_is_listed(
    1.36 +                                           PEP_SESSION session,
    1.37 +                                           const char *fpr,
    1.38 +                                           bool *listed
    1.39 +                                           )
    1.40 +{
    1.41 +    PEP_STATUS status = PEP_STATUS_OK;
    1.42 +    int count;
    1.43 +    
    1.44 +    assert(session && fpr && fpr[0] && listed);
    1.45 +    
    1.46 +    if (!(session && fpr && fpr[0] && listed))
    1.47 +        return PEP_ILLEGAL_VALUE;
    1.48 +    
    1.49 +    *listed = false;
    1.50 +    
    1.51 +    sqlite3_reset(session->own_key_is_listed);
    1.52 +    sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
    1.53 +    
    1.54 +    int result;
    1.55 +    
    1.56 +    result = sqlite3_step(session->own_key_is_listed);
    1.57 +    switch (result) {
    1.58 +        case SQLITE_ROW:
    1.59 +            count = sqlite3_column_int(session->own_key_is_listed, 0);
    1.60 +            *listed = count > 0;
    1.61 +            status = PEP_STATUS_OK;
    1.62 +            break;
    1.63 +            
    1.64 +        default:
    1.65 +            status = PEP_UNKNOWN_ERROR;
    1.66 +    }
    1.67 +    
    1.68 +    sqlite3_reset(session->own_key_is_listed);
    1.69 +    return status;
    1.70 +}
    1.71 +
    1.72 +DYNAMIC_API PEP_STATUS own_key_retrieve(
    1.73 +                                          PEP_SESSION session,
    1.74 +                                          stringlist_t **own_key
    1.75 +                                          )
    1.76 +{
    1.77 +    PEP_STATUS status = PEP_STATUS_OK;
    1.78 +    
    1.79 +    assert(session);
    1.80 +    assert(own_key);
    1.81 +    
    1.82 +    if (!(session && own_key))
    1.83 +        return PEP_ILLEGAL_VALUE;
    1.84 +    
    1.85 +    *own_key = NULL;
    1.86 +    stringlist_t *_own_key = new_stringlist(NULL);
    1.87 +    if (_own_key == NULL)
    1.88 +        goto enomem;
    1.89 +    
    1.90 +    sqlite3_reset(session->own_key_retrieve);
    1.91 +    
    1.92 +    int result;
    1.93 +    const char *fpr = NULL;
    1.94 +    
    1.95 +    stringlist_t *_bl = _own_key;
    1.96 +    do {
    1.97 +        result = sqlite3_step(session->own_key_retrieve);
    1.98 +        switch (result) {
    1.99 +            case SQLITE_ROW:
   1.100 +                fpr = (const char *) sqlite3_column_text(session->own_key_retrieve, 0);
   1.101 +                
   1.102 +                _bl = stringlist_add(_bl, fpr);
   1.103 +                if (_bl == NULL)
   1.104 +                    goto enomem;
   1.105 +                
   1.106 +                break;
   1.107 +                
   1.108 +            case SQLITE_DONE:
   1.109 +                break;
   1.110 +                
   1.111 +            default:
   1.112 +                status = PEP_UNKNOWN_ERROR;
   1.113 +                result = SQLITE_DONE;
   1.114 +        }
   1.115 +    } while (result != SQLITE_DONE);
   1.116 +    
   1.117 +    sqlite3_reset(session->own_key_retrieve);
   1.118 +    if (status == PEP_STATUS_OK)
   1.119 +        *own_key = _own_key;
   1.120 +    else
   1.121 +        free_stringlist(_own_key);
   1.122 +    
   1.123 +    goto the_end;
   1.124 +    
   1.125 +enomem:
   1.126 +    free_stringlist(_own_key);
   1.127 +    status = PEP_OUT_OF_MEMORY;
   1.128 +    
   1.129 +the_end:
   1.130 +    return status;
   1.131 +}
     2.1 --- a/src/keymanagement.h	Tue May 10 12:03:45 2016 +0200
     2.2 +++ b/src/keymanagement.h	Tue May 10 12:15:14 2016 +0200
     2.3 @@ -152,6 +152,43 @@
     2.4          pEp_identity *ident
     2.5      );
     2.6  
     2.7 +    
     2.8 +// own_key_add() - add to own keys
     2.9 +//
    2.10 +//  parameters:
    2.11 +//      session (in)        session to use
    2.12 +//      fpr (in)            fingerprint of owned key
    2.13 +
    2.14 +DYNAMIC_API PEP_STATUS own_key_add(PEP_SESSION session, const char *fpr);
    2.15 +
    2.16 +
    2.17 +// own_key_is_listed() - returns true id key is listed as own key
    2.18 +//
    2.19 +//  parameters:
    2.20 +//      session (in)        session to use
    2.21 +//      fpr (in)            fingerprint of key to test
    2.22 +//      bool (out)          flags if key is own
    2.23 +
    2.24 +DYNAMIC_API PEP_STATUS own_key_is_listed(
    2.25 +        PEP_SESSION session,
    2.26 +        const char *fpr,
    2.27 +        bool *listed
    2.28 +    );
    2.29 +
    2.30 +
    2.31 +// own_key_retrieve() - retrieve all own keys fingerprints
    2.32 +//
    2.33 +//  parameters:
    2.34 +//      session (in)        session to use
    2.35 +//      own_key (out)     copy of own_key
    2.36 +//
    2.37 +//  caveat:
    2.38 +//      the ownership of the copy of own_key goes to the caller
    2.39 +
    2.40 +DYNAMIC_API PEP_STATUS own_key_retrieve(
    2.41 +        PEP_SESSION session,
    2.42 +        stringlist_t **own_key
    2.43 +    );
    2.44  
    2.45  #ifdef __cplusplus
    2.46  }
     3.1 --- a/src/pEpEngine.c	Tue May 10 12:03:45 2016 +0200
     3.2 +++ b/src/pEpEngine.c	Tue May 10 12:15:14 2016 +0200
     3.3 @@ -30,6 +30,11 @@
     3.4      static const char *sql_blacklist_is_listed;
     3.5      static const char *sql_blacklist_retrieve;
     3.6  
     3.7 +    // Own keys
     3.8 +    static const char *sql_own_key_add;
     3.9 +    static const char *sql_own_key_is_listed;
    3.10 +    static const char *sql_own_key_retrieve;
    3.11 +
    3.12      bool in_first = false;
    3.13  
    3.14      assert(sqlite3_threadsafe());
    3.15 @@ -168,6 +173,12 @@
    3.16                  "create table if not exists blacklist_keys (\n"
    3.17                  "   fpr text primary key\n"
    3.18                  ");\n"
    3.19 +                // Own keys
    3.20 +                "create table if not exists own_keys (\n"
    3.21 +                "   fpr text primary key\n"
    3.22 +                "       references pgp_keypair (fpr)\n"
    3.23 +                "       on delete cascade,\n"
    3.24 +                ");\n"
    3.25                  ,
    3.26              NULL,
    3.27              NULL,
    3.28 @@ -187,7 +198,7 @@
    3.29          sql_log = "insert into log (title, entity, description, comment)"
    3.30                    "values (?1, ?2, ?3, ?4);";
    3.31  
    3.32 -        sql_get_identity =    "select fpr, username, comm_type, lang"
    3.33 +        sql_get_identity =  "select fpr, username, comm_type, lang"
    3.34                              "   from identity"
    3.35                              "   join person on id = identity.user_id"
    3.36                              "   join pgp_keypair on fpr = identity.main_key_id"
    3.37 @@ -236,6 +247,16 @@
    3.38          sql_blacklist_is_listed = "select count(*) from blacklist_keys where fpr = upper(replace(?1,' ','')) ;";
    3.39  
    3.40          sql_blacklist_retrieve = "select * from blacklist_keys ;";
    3.41 +        
    3.42 +        // Own keys
    3.43 +        
    3.44 +        sql_own_key_add = "insert or replace into own_keys (fpr) "
    3.45 +                          "    values (upper(replace(?1,' ',''))) ;";
    3.46 +        
    3.47 +        sql_own_key_is_listed = "select count(*) from own_keys where fpr = upper(replace(?1,' ','')) ;";
    3.48 +
    3.49 +        sql_own_key_retrieve = "select * from own_keys ;";
    3.50 +        
    3.51      }
    3.52  
    3.53      int_result = sqlite3_prepare_v2(_session->db, sql_log, (int)strlen(sql_log),
    3.54 @@ -308,6 +329,20 @@
    3.55              (int)strlen(sql_blacklist_retrieve), &_session->blacklist_retrieve, NULL);
    3.56      assert(int_result == SQLITE_OK);
    3.57  
    3.58 +    // Own keys
    3.59 +    
    3.60 +    int_result = sqlite3_prepare_v2(_session->db, sql_own_key_add,
    3.61 +                                    (int)strlen(sql_own_key_add), &_session->own_key_add, NULL);
    3.62 +    assert(int_result == SQLITE_OK);
    3.63 +    
    3.64 +    int_result = sqlite3_prepare_v2(_session->db, sql_own_key_is_listed,
    3.65 +                                    (int)strlen(sql_own_key_is_listed), &_session->own_key_is_listed, NULL);
    3.66 +    assert(int_result == SQLITE_OK);
    3.67 +    
    3.68 +    int_result = sqlite3_prepare_v2(_session->db, sql_own_key_retrieve,
    3.69 +                                    (int)strlen(sql_own_key_retrieve), &_session->own_key_retrieve, NULL);
    3.70 +    assert(int_result == SQLITE_OK);
    3.71 +    
    3.72      status = init_cryptotech(_session, in_first);
    3.73      if (status != PEP_STATUS_OK)
    3.74          goto pep_error;
    3.75 @@ -807,7 +842,7 @@
    3.76      sqlite3_reset(session->set_trust);
    3.77      if (result != SQLITE_DONE) {
    3.78          sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
    3.79 -        return PEP_CANNOT_SET_IDENTITY;
    3.80 +        return PEP_CANNOT_SET_TRUST;
    3.81      }
    3.82  
    3.83      result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
    3.84 @@ -837,7 +872,7 @@
    3.85      sqlite3_reset(session->mark_compromized);
    3.86  
    3.87      if (result != SQLITE_DONE)
    3.88 -        return PEP_CANNOT_SET_IDENTITY;
    3.89 +        return PEP_CANNOT_SET_TRUST;
    3.90  
    3.91      return PEP_STATUS_OK;
    3.92  }
     4.1 --- a/src/pEp_internal.h	Tue May 10 12:03:45 2016 +0200
     4.2 +++ b/src/pEp_internal.h	Tue May 10 12:15:14 2016 +0200
     4.3 @@ -105,7 +105,13 @@
     4.4      sqlite3_stmt *blacklist_delete;
     4.5      sqlite3_stmt *blacklist_is_listed;
     4.6      sqlite3_stmt *blacklist_retrieve;
     4.7 +    
     4.8 +    // Own keys
     4.9 +    sqlite3_stmt *own_key_add;
    4.10 +    sqlite3_stmt *own_key_is_listed;
    4.11 +    sqlite3_stmt *own_key_retrieve;
    4.12  
    4.13 +    
    4.14      examine_identity_t examine_identity;
    4.15      void *examine_management;
    4.16