Remove own_keys table, added PEP_OWN_USERID, unique person userId for any own identities and keys.
authorEdouard Tisserant
Wed, 18 May 2016 15:11:39 +0200
changeset 658244734164566
parent 651 3543ea5e8b02
child 659 37bdde68b914
Remove own_keys table, added PEP_OWN_USERID, unique person userId for any own identities and keys.
src/keymanagement.c
src/keymanagement.h
src/pEpEngine.c
src/pEpEngine.h
src/pEp_internal.h
     1.1 --- a/src/keymanagement.c	Wed May 18 08:46:40 2016 +0200
     1.2 +++ b/src/keymanagement.c	Wed May 18 15:11:39 2016 +0200
     1.3 @@ -249,17 +249,29 @@
     1.4      assert(identity);
     1.5      assert(identity->address);
     1.6      assert(identity->username);
     1.7 -    assert(identity->user_id);
     1.8 +    assert(EMPTYSTR(identity->user_id) ||
     1.9 +           strcmp(identity->user_id, PEP_OWN_USERID) == 0);
    1.10  
    1.11      if (!(session && identity && identity->address && identity->username &&
    1.12 -                identity->user_id))
    1.13 +          (EMPTYSTR(identity->user_id) ||
    1.14 +           strcmp(identity->user_id, PEP_OWN_USERID) == 0)))
    1.15          return PEP_ILLEGAL_VALUE;
    1.16  
    1.17      identity->comm_type = PEP_ct_pEp;
    1.18      identity->me = true;
    1.19 +    
    1.20 +    if(EMPTYSTR(identity->user_id))
    1.21 +    {
    1.22 +        free(identity->user_id);
    1.23 +        identity->user_id = strdup(PEP_OWN_USERID);
    1.24 +        assert(identity->user_id);
    1.25 +        if (identity->user_id == NULL)
    1.26 +        {
    1.27 +            return PEP_OUT_OF_MEMORY;
    1.28 +        }
    1.29 +    }
    1.30  
    1.31      DEBUG_LOG("myself", "debug", identity->address);
    1.32 -
    1.33      
    1.34      status = get_identity(session,
    1.35                            identity->address,
    1.36 @@ -280,22 +292,13 @@
    1.37                  return PEP_OUT_OF_MEMORY;
    1.38              }
    1.39          }
    1.40 -
    1.41 -        // Backward compatibility, not check that stored key is indeed own key
    1.42 -
    1.43      }
    1.44      else if (!EMPTYSTR(identity->fpr))
    1.45      {
    1.46          // App must have a good reason to give fpr, such as explicit
    1.47          // import of private key, or similar.
    1.48  
    1.49 -        // Take given fpr as-is, and consider it as own-key.
    1.50 -
    1.51 -        status = own_key_add(session, identity->fpr);
    1.52 -        assert(status == PEP_STATUS_OK);
    1.53 -        if (status != PEP_STATUS_OK) {
    1.54 -            return status;
    1.55 -        }
    1.56 +        // Take given fpr as-is.
    1.57      }
    1.58      else
    1.59      {
    1.60 @@ -392,14 +395,6 @@
    1.61              free_stringlist(keylist);
    1.62              return PEP_UNKNOWN_ERROR;
    1.63          }
    1.64 -        
    1.65 -        // Consider generated keys as own keys.
    1.66 -        status = own_key_add(session, identity->fpr);
    1.67 -        assert(status == PEP_STATUS_OK);
    1.68 -        if (status != PEP_STATUS_OK) {
    1.69 -            free_stringlist(keylist);
    1.70 -            return status;
    1.71 -        }
    1.72      }
    1.73      else
    1.74      {
    1.75 @@ -582,34 +577,6 @@
    1.76      return status;
    1.77  }
    1.78  
    1.79 -DYNAMIC_API PEP_STATUS own_key_add(PEP_SESSION session, const char *fpr)
    1.80 -{
    1.81 -    PEP_STATUS status = PEP_STATUS_OK;
    1.82 -    
    1.83 -    assert(session && fpr && fpr[0]);
    1.84 -    
    1.85 -    if (!(session && fpr && fpr[0]))
    1.86 -        return PEP_ILLEGAL_VALUE;
    1.87 -    
    1.88 -    sqlite3_reset(session->own_key_add);
    1.89 -    sqlite3_bind_text(session->own_key_add, 1, fpr, -1, SQLITE_STATIC);
    1.90 -    
    1.91 -    int result;
    1.92 -    
    1.93 -    result = sqlite3_step(session->own_key_add);
    1.94 -    switch (result) {
    1.95 -        case SQLITE_DONE:
    1.96 -            status = PEP_STATUS_OK;
    1.97 -            break;
    1.98 -            
    1.99 -        default:
   1.100 -            status = PEP_UNKNOWN_ERROR;
   1.101 -    }
   1.102 -    
   1.103 -    sqlite3_reset(session->own_key_add);
   1.104 -    return status;
   1.105 -}
   1.106 -
   1.107  DYNAMIC_API PEP_STATUS own_key_is_listed(
   1.108                                             PEP_SESSION session,
   1.109                                             const char *fpr,
     2.1 --- a/src/keymanagement.h	Wed May 18 08:46:40 2016 +0200
     2.2 +++ b/src/keymanagement.h	Wed May 18 15:11:39 2016 +0200
     2.3 @@ -152,15 +152,6 @@
     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  //
     3.1 --- a/src/pEpEngine.c	Wed May 18 08:46:40 2016 +0200
     3.2 +++ b/src/pEpEngine.c	Wed May 18 15:11:39 2016 +0200
     3.3 @@ -31,7 +31,6 @@
     3.4      static const char *sql_blacklist_retrieve;
     3.5  
     3.6      // Own keys
     3.7 -    static const char *sql_own_key_add;
     3.8      static const char *sql_own_key_is_listed;
     3.9      static const char *sql_own_key_retrieve;
    3.10  
    3.11 @@ -176,12 +175,6 @@
    3.12                  "create table if not exists blacklist_keys (\n"
    3.13                  "   fpr text primary key\n"
    3.14                  ");\n"
    3.15 -                // Own keys
    3.16 -                "create table if not exists own_keys (\n"
    3.17 -                "   fpr text primary key\n"
    3.18 -                "       references pgp_keypair (fpr)\n"
    3.19 -                "       on delete cascade\n"
    3.20 -                ");\n"
    3.21                  // sequences
    3.22                  "create table if not exists sequences(\n"
    3.23                  "   name text primary key,\n"
    3.24 @@ -262,13 +255,24 @@
    3.25          
    3.26          // Own keys
    3.27          
    3.28 -        sql_own_key_add = "insert or replace into own_keys (fpr) "
    3.29 -                          "    values (upper(replace(?1,' ',''))) ;";
    3.30 +        sql_own_key_is_listed =
    3.31 +                                "select count(*) from ("
    3.32 +                                " select main_key_id from person "
    3.33 +                                "   where main_key_id = upper(replace(?1,' ',''))"
    3.34 +                                "    and id = '" PEP_OWN_USERID "' "
    3.35 +                                " union "
    3.36 +                                "  select main_key_id from identity "
    3.37 +                                "   where main_key_id = upper(replace(?1,' ',''))"
    3.38 +                                "    and user_id = '" PEP_OWN_USERID "' );";
    3.39 +
    3.40 +        sql_own_key_retrieve = "select main_key_id from person "
    3.41 +                               "  where main_key_id is not null"
    3.42 +                               "   and id = '" PEP_OWN_USERID "' "
    3.43 +                               "union "
    3.44 +                               " select main_key_id from identity "
    3.45 +                               "  where main_key_id is not null"
    3.46 +                               "   and user_id = '" PEP_OWN_USERID "' ;";
    3.47          
    3.48 -        sql_own_key_is_listed = "select count(*) from own_keys where fpr = upper(replace(?1,' ','')) ;";
    3.49 -
    3.50 -        sql_own_key_retrieve = "select * from own_keys ;";
    3.51 - 
    3.52          sql_sequence_value1 = "insert or replace into sequences (name, value) "
    3.53                                "values (?1, "
    3.54                                "(select coalesce((select value + 1 from sequences "
    3.55 @@ -348,10 +352,6 @@
    3.56  
    3.57      // Own keys
    3.58      
    3.59 -    int_result = sqlite3_prepare_v2(_session->db, sql_own_key_add,
    3.60 -            (int)strlen(sql_own_key_add), &_session->own_key_add, NULL);
    3.61 -    assert(int_result == SQLITE_OK);
    3.62 -    
    3.63      int_result = sqlite3_prepare_v2(_session->db, sql_own_key_is_listed,
    3.64              (int)strlen(sql_own_key_is_listed), &_session->own_key_is_listed, NULL);
    3.65      assert(int_result == SQLITE_OK);
     4.1 --- a/src/pEpEngine.h	Wed May 18 08:46:40 2016 +0200
     4.2 +++ b/src/pEpEngine.h	Wed May 18 15:11:39 2016 +0200
     4.3 @@ -14,6 +14,8 @@
     4.4  
     4.5  #define PEP_VERSION "1.0"
     4.6  
     4.7 +#define PEP_OWN_USERID "pEp_own_userId"
     4.8 +    
     4.9  // pEp Engine API
    4.10  
    4.11  //  caveat:
     5.1 --- a/src/pEp_internal.h	Wed May 18 08:46:40 2016 +0200
     5.2 +++ b/src/pEp_internal.h	Wed May 18 15:11:39 2016 +0200
     5.3 @@ -108,7 +108,6 @@
     5.4      sqlite3_stmt *blacklist_retrieve;
     5.5      
     5.6      // Own keys
     5.7 -    sqlite3_stmt *own_key_add;
     5.8      sqlite3_stmt *own_key_is_listed;
     5.9      sqlite3_stmt *own_key_retrieve;
    5.10