merged in ENGINE-293 ENGINE-289
authorKrista Bennett <krista@pep-project.org>
Sat, 09 Dec 2017 13:06:29 +0100
branchENGINE-289
changeset 2310f0aeda5e1ce9
parent 2309 b4c78872e2c1
parent 2308 b7ef1c9005ae
child 2311 688b925c3e73
merged in ENGINE-293
     1.1 --- a/src/keymanagement.c	Sat Dec 09 13:06:02 2017 +0100
     1.2 +++ b/src/keymanagement.c	Sat Dec 09 13:06:29 2017 +0100
     1.3 @@ -104,13 +104,18 @@
     1.4  
     1.5      if (_no_user_id)
     1.6      {
     1.7 -        status = get_identity(session, identity->address, PEP_OWN_USERID,
     1.8 -                &stored_identity);
     1.9 -        if (status == PEP_STATUS_OK) {
    1.10 -            free_identity(stored_identity);
    1.11 -            return _myself(session, identity, false, true);
    1.12 +        char* own_id = NULL;
    1.13 +        status = get_own_userid(session, &own_id);
    1.14 +        if (status == PEP_STATUS_OK && own_id) {
    1.15 +            status = get_identity(session, identity->address, own_id,
    1.16 +                    &stored_identity);
    1.17 +            if (status == PEP_STATUS_OK) {
    1.18 +                free_identity(stored_identity);
    1.19 +                identity->user_id = own_id;
    1.20 +                return _myself(session, identity, false, true);
    1.21 +            }
    1.22          }
    1.23 -
    1.24 +        
    1.25          free(identity->user_id);
    1.26  
    1.27          identity->user_id = calloc(1, strlen(identity->address) + 6);
    1.28 @@ -429,14 +434,25 @@
    1.29      assert(identity);
    1.30      assert(!EMPTYSTR(identity->address));
    1.31  
    1.32 +    char* own_id = NULL;
    1.33 +    status = get_own_userid(session, &own_id);
    1.34 +
    1.35 +
    1.36      assert(EMPTYSTR(identity->user_id) ||
    1.37 -           strcmp(identity->user_id, PEP_OWN_USERID) == 0);
    1.38 +           (own_id && strcmp(identity->user_id, own_id) == 0) ||
    1.39 +           !own_id);
    1.40  
    1.41      if (!(session && identity && !EMPTYSTR(identity->address) &&
    1.42              (EMPTYSTR(identity->user_id) ||
    1.43 -            strcmp(identity->user_id, PEP_OWN_USERID) == 0)))
    1.44 +            (own_id && strcmp(identity->user_id, own_id) == 0) ||
    1.45 +             !own_id)))
    1.46          return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
    1.47  
    1.48 +    if (!own_id) {
    1.49 +        // check to see if we have ANY identity for this address... could have
    1.50 +        // happened due to race condition
    1.51 +    }
    1.52 +    
    1.53      identity->comm_type = PEP_ct_pEp;
    1.54      identity->me = true;
    1.55      if(ignore_flags)
    1.56 @@ -445,7 +461,7 @@
    1.57      if (EMPTYSTR(identity->user_id))
    1.58      {
    1.59          free(identity->user_id);
    1.60 -        identity->user_id = strdup(PEP_OWN_USERID);
    1.61 +        identity->user_id = (own_id ? own_id : strdup(PEP_OWN_USERID));
    1.62          assert(identity->user_id);
    1.63          if (identity->user_id == NULL)
    1.64              return PEP_OUT_OF_MEMORY;
    1.65 @@ -454,7 +470,7 @@
    1.66      if (EMPTYSTR(identity->username))
    1.67      {
    1.68          free(identity->username);
    1.69 -        identity->username = strdup("anonymous");
    1.70 +        identity->username = strdup("Anonymous");
    1.71          assert(identity->username);
    1.72          if (identity->username == NULL)
    1.73              return PEP_OUT_OF_MEMORY;
    1.74 @@ -625,6 +641,45 @@
    1.75      return ADD_TO_LOG(PEP_STATUS_OK);
    1.76  }
    1.77  
    1.78 +DYNAMIC_API PEP_STATUS initialise_own_identities(PEP_SESSION session,
    1.79 +                                                 identity_list* my_idents) {
    1.80 +    PEP_STATUS status = PEP_STATUS_OK;
    1.81 +    if (!session)
    1.82 +        return PEP_ILLEGAL_VALUE;
    1.83 +        
    1.84 +    char* stored_own_userid = NULL;
    1.85 +    get_own_userid(session, &stored_own_userid);
    1.86 +    
    1.87 +    identity_list* ident_curr = my_idents;
    1.88 +    while (ident_curr) {
    1.89 +        pEp_identity* ident = ident_curr->ident;
    1.90 +        if (!ident)
    1.91 +            return PEP_ILLEGAL_VALUE;
    1.92 +            
    1.93 +        if (stored_own_userid) {
    1.94 +            if (!ident->user_id) 
    1.95 +                ident->user_id = strdup(stored_own_userid);
    1.96 +            else if (strcmp(stored_own_userid, ident->user_id) != 0)
    1.97 +                return PEP_ILLEGAL_VALUE;
    1.98 +        }
    1.99 +        else if (!ident->user_id) {
   1.100 +            stored_own_userid = PEP_OWN_USERID;
   1.101 +            ident->user_id = strdup(PEP_OWN_USERID);
   1.102 +        }
   1.103 +        
   1.104 +        ident->me = true; // Just in case.
   1.105 +        
   1.106 +        // Ok, do it...
   1.107 +        status = set_identity(session, ident);
   1.108 +        if (status != PEP_STATUS_OK)
   1.109 +            return status;
   1.110 +        
   1.111 +        ident_curr = ident_curr->next;
   1.112 +    }
   1.113 +    
   1.114 +    return status;
   1.115 +}
   1.116 +
   1.117  DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
   1.118  {
   1.119      return ADD_TO_LOG(_myself(session, identity, true, false));
   1.120 @@ -1069,7 +1124,7 @@
   1.121              status = get_identity(session, my_user_id, address, &my_id);
   1.122              
   1.123              if (status == PEP_STATUS_OK && my_id) {
   1.124 -                if (my_id->fpr && strcasecmp(my_id->fpr, fpr) == 0)) {
   1.125 +                if (my_id->fpr && strcasecmp(my_id->fpr, fpr) == 0) {
   1.126                      // We're done. It was already here.
   1.127                      // FIXME: Do we check trust/revocation/?
   1.128                      goto pep_free;
   1.129 @@ -1083,22 +1138,20 @@
   1.130              if (my_id) {
   1.131                  free(my_id->fpr);
   1.132                  my_id->fpr = my_user_id;
   1.133 -                my_user_id->comm_type = PEP_ct_pEp;
   1.134 +                my_id->comm_type = PEP_ct_pEp;
   1.135 +                my_id->me = true; // just in case? 
   1.136              }
   1.137              else { // Else, we need a new identity
   1.138 -                status = new_identity(session, address, fpr, my_user_id, NULL, &my_id); 
   1.139 +                my_id = new_identity(address, fpr, my_user_id, NULL); 
   1.140                  if (status != PEP_STATUS_OK)
   1.141                      goto pep_free; 
   1.142 -                my_user_id->me = true;
   1.143 -                my_user_id->comm_type = PEP_ct_pEp;
   1.144 +                my_id->me = true;
   1.145 +                my_id->comm_type = PEP_ct_pEp;
   1.146              }
   1.147          }
   1.148          else {
   1.149              // I think the prerequisite should be that at least one own identity
   1.150              // already in the DB, so REALLY look at this.
   1.151 -            // status = new_identity(session, address, fpr, "PEP_OWN_USERID", NULL); 
   1.152 -            // my_user_id->me = true;
   1.153 -            // my_user_id->comm_type = PEP_ct_pEp;
   1.154              return PEP_CANNOT_FIND_IDENTITY;
   1.155          }
   1.156          
     2.1 --- a/src/keymanagement.h	Sat Dec 09 13:06:02 2017 +0100
     2.2 +++ b/src/keymanagement.h	Sat Dec 09 13:06:29 2017 +0100
     2.3 @@ -34,13 +34,19 @@
     2.4          PEP_SESSION session, pEp_identity * identity
     2.5      );
     2.6  
     2.7 +DYNAMIC_API PEP_STATUS initialise_own_identities(PEP_SESSION session,
     2.8 +                                                 identity_list* my_idents);
     2.9  
    2.10 -// myself() - ensures that the own identity is being complete
    2.11 +// myself() - ensures that an own identity is complete
    2.12  //
    2.13  //  parameters:
    2.14  //      session (in)        session to use
    2.15  //      identity (inout)    identity of local user
    2.16 -//                          at least .address, .username, .user_id must be set
    2.17 +//                          at least .address must be set.
    2.18 +//                          if no .user_id is set, AND the DB doesn't contain
    2.19 +//                          a user_id, PEP_OWN_USERID will be used.
    2.20 +//                          if no .username is set and none is in the DB,
    2.21 +//                          username will be set to "Anonymous"
    2.22  //
    2.23  //  return value:
    2.24  //      PEP_STATUS_OK if identity could be completed or was already complete,
     3.1 --- a/src/message.h	Sat Dec 09 13:06:02 2017 +0100
     3.2 +++ b/src/message.h	Sat Dec 09 13:06:29 2017 +0100
     3.3 @@ -42,6 +42,7 @@
     3.4  typedef struct _message {
     3.5      PEP_msg_direction dir;
     3.6      char *id;                               // UTF-8 string of message ID
     3.7 +    bool me;                                // true if own identity
     3.8      char *shortmsg;                         // UTF-8 string of short message
     3.9      char *longmsg;                          // UTF-8 string of long message
    3.10                                              // (plain)
     4.1 --- a/src/message_api.c	Sat Dec 09 13:06:02 2017 +0100
     4.2 +++ b/src/message_api.c	Sat Dec 09 13:06:29 2017 +0100
     4.3 @@ -2577,7 +2577,7 @@
     4.4      // 1. Check to see if this message is to us and contains an own key imported 
     4.5      // from own trusted message 
     4.6      if (msg && *rating >= PEP_rating_trusted && imported_private_key_address &&
     4.7 -        msg->to && _identity_me(msg->to) {
     4.8 +        msg->to && msg->to->ident && _identity_me(msg->to->ident)) {
     4.9  
    4.10          // flag it as such
    4.11          *flags |= PEP_decrypt_flag_own_private_key;
     5.1 --- a/src/pEpEngine.c	Sat Dec 09 13:06:02 2017 +0100
     5.2 +++ b/src/pEpEngine.c	Sat Dec 09 13:06:29 2017 +0100
     5.3 @@ -47,10 +47,10 @@
     5.4      "select id, word from wordlist where lang = lower(?1) "
     5.5      "and id = ?2 ;";
     5.6  
     5.7 -
     5.8  static const char *sql_get_identity =  
     5.9      "select fpr, username, comm_type, lang,"
    5.10 -    "   identity.flags | pgp_keypair.flags"
    5.11 +    "   identity.flags | pgp_keypair.flags,"
    5.12 +    "   is_own"
    5.13      "   from identity"
    5.14      "   join person on id = identity.user_id"
    5.15      "   join pgp_keypair on fpr = identity.main_key_id"
    5.16 @@ -63,6 +63,18 @@
    5.17      "          end) = 1"
    5.18      "   and identity.user_id = ?2;";
    5.19  
    5.20 +static const char *sql_get_identity_without_fpr =  
    5.21 +    "select main_key_id, username, comm_type, lang,"
    5.22 +    "   identity.flags, is_own"
    5.23 +    "   from identity"
    5.24 +    "   join person on id = identity.user_id"
    5.25 +    "   where (case when (address = ?1) then (1)"
    5.26 +    "               when (lower(address) = lower(?1)) then (1)"
    5.27 +    "               when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
    5.28 +    "               else 0"
    5.29 +    "          end) = 1"
    5.30 +    "   and identity.user_id = ?2;";
    5.31 +
    5.32  static const char *sql_replace_identities_fpr =  
    5.33      "update identity"
    5.34      "   set main_key_id = ?1 "
    5.35 @@ -77,15 +89,13 @@
    5.36      "      where id = ?1), upper(replace(?4,' ','')))),"
    5.37      "    (select device_group from person where id = ?1)) ;";
    5.38  
    5.39 -// FIXME: PEP_OWN_USERID
    5.40  static const char *sql_set_device_group = 
    5.41      "update person set device_group = ?1 "
    5.42 -    "where id = '" PEP_OWN_USERID "';";
    5.43 -
    5.44 -// FIXME: PEP_OWN_USERID
    5.45 +    "where id = ?2;";
    5.46 +
    5.47  static const char *sql_get_device_group = 
    5.48      "select device_group from person "
    5.49 -    "where id = '" PEP_OWN_USERID "';";
    5.50 +    "where id = ?1;";
    5.51  
    5.52  static const char *sql_set_pgp_keypair = 
    5.53      "insert or replace into pgp_keypair (fpr) "
    5.54 @@ -94,7 +104,7 @@
    5.55  static const char *sql_set_identity = 
    5.56      "insert or replace into identity ("
    5.57      " address, main_key_id, "
    5.58 -    " user_id, flags"
    5.59 +    " user_id, flags, is_own"
    5.60      ") values ("
    5.61      " ?1,"
    5.62      " upper(replace(?2,' ','')),"
    5.63 @@ -107,7 +117,8 @@
    5.64      // "    0)"
    5.65      // " ) | (?4 & 255)"
    5.66      /* set_identity ignores previous flags, and doesn't filter machine flags */
    5.67 -    " ?4"
    5.68 +    " ?4,"
    5.69 +    " ?5"
    5.70      ");";
    5.71          
    5.72  static const char *sql_set_identity_flags = 
    5.73 @@ -177,14 +188,14 @@
    5.74  // We only care if it's 0 or non-zero
    5.75  static const char *sql_own_key_is_listed = 
    5.76      "select count(*) from ("
    5.77 -    "   select fpr from trust"
    5.78 -    "      join identity on id = identity.user_id"
    5.79 -    "      where fpr = upper(replace(?1,' ',''))"
    5.80 -    "           and identity.is_own = 1;"
    5.81 +    "   select pgp_keypair_fpr from trust"
    5.82 +    "      join identity on trust.user_id = identity.user_id"
    5.83 +    "      where pgp_keypair_fpr = upper(replace(?1,' ',''))"
    5.84 +    "           and identity.is_own = 1"
    5.85      ");";
    5.86  
    5.87  static const char *sql_own_identities_retrieve =  
    5.88 -    "select address, fpr, username, user_id, "
    5.89 +    "select address, fpr, username, identity.user_id, "
    5.90      "   lang, identity.flags | pgp_keypair.flags"
    5.91      "   from identity"
    5.92      "   join person on id = identity.user_id"
    5.93 @@ -194,15 +205,15 @@
    5.94      "   where identity.is_own = 1"
    5.95      "       and (identity.flags & ?1) = 0;";
    5.96  
    5.97 -static const char *sql_own_keys_retrieve =  
    5.98 -    "select fpr from trust"
    5.99 +static const char *sql_own_keys_retrieve = 
   5.100 +    "select pgp_keypair_fpr from trust"
   5.101 +    "   join identity on trust.user_id = identity.user_id"
   5.102 +    "   where identity.is_own = 1";
   5.103 +
   5.104 +static const char* sql_get_own_userid =
   5.105 +    "select id from person"
   5.106      "   join identity on id = identity.user_id"
   5.107 -    "   where identity.is_own = 1"
   5.108 -
   5.109 -// FIXME: PEP_OWN_USERID
   5.110 -static const char *sql_set_own_key = 
   5.111 -    "insert or replace into own_keys (address, user_id, fpr)"
   5.112 -    " values (?1, '" PEP_OWN_USERID "', upper(replace(?2,' ','')));";
   5.113 +    "   where identity.is_own = 1";
   5.114  
   5.115  // Sequence
   5.116  static const char *sql_sequence_value1 = 
   5.117 @@ -248,6 +259,56 @@
   5.118      return 0;
   5.119  }
   5.120  
   5.121 +static int table_contains_column(PEP_SESSION session, const char* table_name,
   5.122 +                                                      const char* col_name) {
   5.123 +
   5.124 +
   5.125 +    if (!session || !table_name || !col_name)
   5.126 +        return -1;
   5.127 +        
   5.128 +    // Table names can't be SQL parameters, so we do it this way.
   5.129 +    
   5.130 +    // these two must be the same number.
   5.131 +    char sql_buf[500];
   5.132 +    const size_t max_q_len = 500;
   5.133 +    
   5.134 +    size_t t_size, c_size, q_size;
   5.135 +    
   5.136 +    const char* q1 = "SELECT "; // 7
   5.137 +    const char* q2 = " from "; // 6
   5.138 +    const char* q3 = ";";       // 1
   5.139 +    
   5.140 +    q_size = 14;
   5.141 +    t_size = strlen(table_name);
   5.142 +    c_size = strlen(col_name);
   5.143 +    
   5.144 +    size_t query_len = q_size + c_size + t_size + 1;
   5.145 +    
   5.146 +    if (query_len > max_q_len)
   5.147 +        return -1;
   5.148 +
   5.149 +    strlcpy(sql_buf, q1, max_q_len);
   5.150 +    strlcat(sql_buf, col_name, max_q_len);
   5.151 +    strlcat(sql_buf, q2, max_q_len);
   5.152 +    strlcat(sql_buf, table_name, max_q_len);
   5.153 +    strlcat(sql_buf, q3, max_q_len);
   5.154 +
   5.155 +    sqlite3_stmt *stmt; 
   5.156 +
   5.157 +    sqlite3_prepare_v2(session->db, sql_buf, -1, &stmt, NULL);
   5.158 +
   5.159 +    int retval = 0;
   5.160 +
   5.161 +    int rc = sqlite3_step(stmt);  
   5.162 +    if (rc == SQLITE_DONE || rc == SQLITE_OK || rc == SQLITE_ROW) {
   5.163 +        retval = 1;
   5.164 +    }
   5.165 +
   5.166 +    sqlite3_finalize(stmt);      
   5.167 +        
   5.168 +    return retval;
   5.169 +}
   5.170 +
   5.171  DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
   5.172  {
   5.173      PEP_STATUS status = PEP_STATUS_OK;
   5.174 @@ -355,7 +416,7 @@
   5.175      sqlite3_busy_timeout(_session->system_db, 1000);
   5.176  
   5.177  // increment this when patching DDL
   5.178 -#define _DDL_USER_VERSION "5"
   5.179 +#define _DDL_USER_VERSION "6"
   5.180  
   5.181      if (in_first) {
   5.182  
   5.183 @@ -413,8 +474,8 @@
   5.184                  "       references pgp_keypair (fpr)\n"
   5.185                  "       on delete set null,\n"
   5.186                  "   comment text,\n"
   5.187 -                "   flags integer default 0,"
   5.188 -                "   is_own integer default 0,"
   5.189 +                "   flags integer default 0,\n"
   5.190 +                "   is_own integer default 0,\n"
   5.191                  "   primary key (address, user_id)\n"
   5.192                  ");\n"
   5.193                  "create table if not exists trust (\n"
   5.194 @@ -445,18 +506,6 @@
   5.195                  "       on delete cascade,\n"
   5.196                  "   revocation_date integer\n"
   5.197                  ");\n"
   5.198 -                "create table if not exists own_keys (\n"
   5.199 -                "   address text,\n"
   5.200 -                "   user_id text,\n"
   5.201 -                "   fpr text not null\n"
   5.202 -                "       references pgp_keypair (fpr)\n"
   5.203 -                "       on delete cascade,\n"
   5.204 -                "   foreign key (address, user_id)\n"
   5.205 -                "       references identity\n"
   5.206 -                "       on delete cascade\n"
   5.207 -//                "   check (user_id = '" PEP_OWN_USERID "')\n"
   5.208 -                "   primary key (address, fpr)\n"
   5.209 -                ");\n" 
   5.210                  ,
   5.211              NULL,
   5.212              NULL,
   5.213 @@ -488,6 +537,48 @@
   5.214              NULL,
   5.215              NULL);
   5.216          assert(int_result == SQLITE_OK);
   5.217 +        
   5.218 +        // Sometimes the user_version wasn't set correctly. Check to see if this
   5.219 +        // is really necessary...
   5.220 +        if (version == 1) {
   5.221 +            bool version_changed = true;
   5.222 +            
   5.223 +            if (table_contains_column(_session, "identity", "is_own") > 0) {
   5.224 +                version = 6;
   5.225 +            }
   5.226 +            else if (table_contains_column(_session, "sequences", "own") > 0) {
   5.227 +                version = 3;
   5.228 +            }
   5.229 +            else if (table_contains_column(_session, "pgp_keypair", "flags") > 0) {
   5.230 +                version = 2;
   5.231 +            }
   5.232 +            else {
   5.233 +                version_changed = false;
   5.234 +            }
   5.235 +            
   5.236 +            if (version_changed) {
   5.237 +                // set it in the DB, finally. Yeesh.
   5.238 +                char verbuf[21]; // enough digits for a max-sized 64 bit int, cmon. 
   5.239 +                sprintf(verbuf,"%d",version);
   5.240 +                
   5.241 +                size_t query_size = strlen(verbuf) + 25;
   5.242 +                char* query = calloc(query_size, 1);
   5.243 +                
   5.244 +                strlcpy(query, "pragma user_version = ", query_size);
   5.245 +                strlcat(query, verbuf, query_size);
   5.246 +                strlcat(query, ";", query_size);
   5.247 +                
   5.248 +                int_result = sqlite3_exec(
   5.249 +                    _session->db,
   5.250 +                    query,
   5.251 +                    user_version,
   5.252 +                    &version,
   5.253 +                    NULL
   5.254 +                );
   5.255 +                free(query);
   5.256 +            }
   5.257 +        }
   5.258 +
   5.259  
   5.260          if(version != 0) { 
   5.261              // Version has been already set
   5.262 @@ -508,7 +599,7 @@
   5.263              //     );
   5.264              //     assert(int_result == SQLITE_OK);
   5.265              // }
   5.266 -
   5.267 +            
   5.268              if (version < 2) {
   5.269                  int_result = sqlite3_exec(
   5.270                      _session->db,
   5.271 @@ -562,7 +653,18 @@
   5.272                      NULL,
   5.273                      NULL,
   5.274                      NULL
   5.275 -                );                
   5.276 +                );
   5.277 +                assert(int_result == SQLITE_OK);                
   5.278 +                int_result = sqlite3_exec(
   5.279 +                    _session->db,
   5.280 +                    "update identity\n"
   5.281 +                    "   set is_own = 1\n"
   5.282 +                    "   where (user_id = '" PEP_OWN_USERID "');\n",
   5.283 +                    NULL,
   5.284 +                    NULL,
   5.285 +                    NULL
   5.286 +                );
   5.287 +                assert(int_result == SQLITE_OK);                
   5.288              }
   5.289          }
   5.290          else { 
   5.291 @@ -601,6 +703,15 @@
   5.292              (int)strlen(sql_get_identity), &_session->get_identity, NULL);
   5.293      assert(int_result == SQLITE_OK);
   5.294  
   5.295 +    int_result = sqlite3_prepare_v2(_session->db, sql_get_identity_without_fpr,
   5.296 +            (int)strlen(sql_get_identity_without_fpr), 
   5.297 +            &_session->get_identity_without_fpr, NULL);
   5.298 +    assert(int_result == SQLITE_OK);
   5.299 +
   5.300 +    int_result = sqlite3_prepare_v2(_session->db, sql_get_own_userid,
   5.301 +            (int)strlen(sql_get_own_userid), &_session->get_own_userid, NULL);
   5.302 +    assert(int_result == SQLITE_OK);
   5.303 +
   5.304      int_result = sqlite3_prepare_v2(_session->db, sql_replace_identities_fpr,
   5.305              (int)strlen(sql_replace_identities_fpr), 
   5.306              &_session->replace_identities_fpr, NULL);
   5.307 @@ -708,10 +819,10 @@
   5.308              &_session->own_keys_retrieve, NULL);
   5.309      assert(int_result == SQLITE_OK);
   5.310   
   5.311 -    int_result = sqlite3_prepare_v2(_session->db, sql_set_own_key,
   5.312 -            (int)strlen(sql_set_own_key),
   5.313 -            &_session->set_own_key, NULL);
   5.314 -    assert(int_result == SQLITE_OK);
   5.315 +    // int_result = sqlite3_prepare_v2(_session->db, sql_set_own_key,
   5.316 +    //         (int)strlen(sql_set_own_key),
   5.317 +    //         &_session->set_own_key, NULL);
   5.318 +    // assert(int_result == SQLITE_OK);
   5.319   
   5.320      // Sequence
   5.321  
   5.322 @@ -842,6 +953,10 @@
   5.323                  sqlite3_finalize(session->trustword);
   5.324              if (session->get_identity)
   5.325                  sqlite3_finalize(session->get_identity);
   5.326 +            if (session->get_identity_without_fpr)
   5.327 +                sqlite3_finalize(session->get_identity_without_fpr);    
   5.328 +            if (session->get_own_userid)
   5.329 +                sqlite3_finalize(session->get_own_userid);
   5.330              if (session->replace_identities_fpr)
   5.331                  sqlite3_finalize(session->replace_identities_fpr);        
   5.332              if (session->set_person)
   5.333 @@ -888,8 +1003,8 @@
   5.334                  sqlite3_finalize(session->own_identities_retrieve);
   5.335              if (session->own_keys_retrieve)
   5.336                  sqlite3_finalize(session->own_keys_retrieve);
   5.337 -            if (session->set_own_key)
   5.338 -                sqlite3_finalize(session->set_own_key);
   5.339 +            // if (session->set_own_key)
   5.340 +            //     sqlite3_finalize(session->set_own_key);
   5.341              if (session->sequence_value1)
   5.342                  sqlite3_finalize(session->sequence_value1);
   5.343              if (session->sequence_value2)
   5.344 @@ -1208,11 +1323,58 @@
   5.345      }
   5.346  }
   5.347  
   5.348 -DYNAMIC_API PEP_STATUS get_identity(
   5.349 +DYNAMIC_API PEP_STATUS get_own_userid(
   5.350 +        PEP_SESSION session, 
   5.351 +        char** userid
   5.352 +    )
   5.353 +{
   5.354 +    assert(session);
   5.355 +    assert(userid);
   5.356 +    
   5.357 +    if (!session || !userid)
   5.358 +        return PEP_ILLEGAL_VALUE;
   5.359 +        
   5.360 +    PEP_STATUS status = PEP_STATUS_OK;
   5.361 +    char* retval = NULL;
   5.362 +    
   5.363 +    sqlite3_reset(session->get_own_userid);
   5.364 +
   5.365 +    const int result = sqlite3_step(session->get_own_userid);
   5.366 +    const char* id;
   5.367 +    
   5.368 +    switch (result) {
   5.369 +        case SQLITE_ROW:
   5.370 +            id = (const char *) sqlite3_column_text(session->get_own_userid, 0);
   5.371 +            if (!id) {
   5.372 +                // Shouldn't happen.
   5.373 +                status = PEP_UNKNOWN_ERROR;
   5.374 +            }
   5.375 +            else {
   5.376 +                retval = strdup(id);
   5.377 +                if (!retval)
   5.378 +                    status = PEP_OUT_OF_MEMORY;
   5.379 +            }
   5.380 +            break;
   5.381 +        default:
   5.382 +            // Technically true, given how we find it, but FIXME we need a more descriptive error
   5.383 +            status = PEP_CANNOT_FIND_IDENTITY;
   5.384 +            *userid = NULL;
   5.385 +    }
   5.386 +
   5.387 +    *userid = retval;
   5.388 +
   5.389 +    sqlite3_reset(session->get_own_userid);
   5.390 +    
   5.391 +    return status;
   5.392 +}
   5.393 +
   5.394 +
   5.395 +static PEP_STATUS _get_identity_internal(
   5.396          PEP_SESSION session,
   5.397          const char *address,
   5.398          const char *user_id,
   5.399 -        pEp_identity **identity
   5.400 +        pEp_identity **identity,
   5.401 +        sqlite3_stmt* get_id_stmt
   5.402      )
   5.403  {
   5.404      PEP_STATUS status = PEP_STATUS_OK;
   5.405 @@ -1228,27 +1390,27 @@
   5.406  
   5.407      *identity = NULL;
   5.408  
   5.409 -    sqlite3_reset(session->get_identity);
   5.410 -    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
   5.411 -    sqlite3_bind_text(session->get_identity, 2, user_id, -1, SQLITE_STATIC);
   5.412 -
   5.413 -    const int result = sqlite3_step(session->get_identity);
   5.414 +    sqlite3_reset(get_id_stmt);
   5.415 +    sqlite3_bind_text(get_id_stmt, 1, address, -1, SQLITE_STATIC);
   5.416 +    sqlite3_bind_text(get_id_stmt, 2, user_id, -1, SQLITE_STATIC);
   5.417 +
   5.418 +    const int result = sqlite3_step(get_id_stmt);
   5.419      switch (result) {
   5.420      case SQLITE_ROW:
   5.421          _identity = new_identity(
   5.422                  address,
   5.423 -                (const char *) sqlite3_column_text(session->get_identity, 0),
   5.424 +                (const char *) sqlite3_column_text(get_id_stmt, 0),
   5.425                  user_id,
   5.426 -                (const char *) sqlite3_column_text(session->get_identity, 1)
   5.427 +                (const char *) sqlite3_column_text(get_id_stmt, 1)
   5.428                  );
   5.429          assert(_identity);
   5.430          if (_identity == NULL)
   5.431              return PEP_OUT_OF_MEMORY;
   5.432  
   5.433          _identity->comm_type = (PEP_comm_type)
   5.434 -            sqlite3_column_int(session->get_identity, 2);
   5.435 +            sqlite3_column_int(get_id_stmt, 2);
   5.436          const char* const _lang = (const char *)
   5.437 -            sqlite3_column_text(session->get_identity, 3);
   5.438 +            sqlite3_column_text(get_id_stmt, 3);
   5.439          if (_lang && _lang[0]) {
   5.440              assert(_lang[0] >= 'a' && _lang[0] <= 'z');
   5.441              assert(_lang[1] >= 'a' && _lang[1] <= 'z');
   5.442 @@ -1258,7 +1420,10 @@
   5.443              _identity->lang[2] = 0;
   5.444          }
   5.445          _identity->flags = (unsigned int)
   5.446 -            sqlite3_column_int(session->get_identity, 4);
   5.447 +            sqlite3_column_int(get_id_stmt, 4);
   5.448 +        _identity->me = (unsigned int)
   5.449 +            sqlite3_column_int(get_id_stmt, 5);
   5.450 +    
   5.451          *identity = _identity;
   5.452          break;
   5.453      default:
   5.454 @@ -1266,10 +1431,33 @@
   5.455          *identity = NULL;
   5.456      }
   5.457  
   5.458 -    sqlite3_reset(session->get_identity);
   5.459 +    sqlite3_reset(get_id_stmt);
   5.460      return status;
   5.461  }
   5.462  
   5.463 +DYNAMIC_API PEP_STATUS get_identity(
   5.464 +        PEP_SESSION session,
   5.465 +        const char *address,
   5.466 +        const char *user_id,
   5.467 +        pEp_identity **identity
   5.468 +    )
   5.469 +{
   5.470 +    return _get_identity_internal(session, address, user_id, identity,
   5.471 +                                  session->get_identity);
   5.472 +}
   5.473 +
   5.474 +PEP_STATUS get_identity_without_fpr(
   5.475 +        PEP_SESSION session,
   5.476 +        const char *address,
   5.477 +        const char *user_id,
   5.478 +        pEp_identity **identity
   5.479 +    )
   5.480 +{
   5.481 +    return _get_identity_internal(session, address, user_id, identity,
   5.482 +                                  session->get_identity_without_fpr);
   5.483 +}
   5.484 +
   5.485 +
   5.486  DYNAMIC_API PEP_STATUS set_identity(
   5.487          PEP_SESSION session, const pEp_identity *identity
   5.488      )
   5.489 @@ -1350,6 +1538,7 @@
   5.490      sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
   5.491              SQLITE_STATIC);
   5.492      sqlite3_bind_int(session->set_identity, 4, identity->flags);
   5.493 +    sqlite3_bind_int(session->set_identity, 5, identity->me);
   5.494      result = sqlite3_step(session->set_identity);
   5.495      sqlite3_reset(session->set_identity);
   5.496      if (result != SQLITE_DONE) {
   5.497 @@ -1358,22 +1547,6 @@
   5.498      }
   5.499  
   5.500      if (has_fpr) {
   5.501 -        if(_identity_me(identity)) {
   5.502 -            sqlite3_reset(session->set_own_key);
   5.503 -            sqlite3_bind_text(session->set_own_key, 1, identity->address, -1,
   5.504 -                SQLITE_STATIC);
   5.505 -            sqlite3_bind_text(session->set_own_key, 2, identity->fpr, -1,
   5.506 -                SQLITE_STATIC);
   5.507 -            result = sqlite3_step(session->set_own_key);
   5.508 -            sqlite3_reset(session->set_own_key);
   5.509 -            if (result != SQLITE_DONE) {
   5.510 -                sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   5.511 -                return PEP_CANNOT_SET_PGP_KEYPAIR;
   5.512 -            }
   5.513 -        }
   5.514 -
   5.515 -        // status = set_trust(session, identity->user_id, identity->fpr,
   5.516 -        //                    identity->comm_type)
   5.517          sqlite3_reset(session->set_trust);
   5.518          sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
   5.519                  SQLITE_STATIC);
   5.520 @@ -1453,6 +1626,15 @@
   5.521      if (!(session && group_name))
   5.522          return PEP_ILLEGAL_VALUE;
   5.523  
   5.524 +    // 1. Get own user_id
   5.525 +    char* user_id = NULL;
   5.526 +    PEP_STATUS status = get_own_userid(session, &user_id);
   5.527 +    
   5.528 +    // No user_id is returned in this case, no need to free;
   5.529 +    if (status != PEP_STATUS_OK)
   5.530 +        return status;
   5.531 +        
   5.532 +    // 2. Set device group
   5.533      sqlite3_reset(session->set_device_group);
   5.534      if(group_name){
   5.535          sqlite3_bind_text(session->set_device_group, 1, group_name, -1,
   5.536 @@ -1460,9 +1642,15 @@
   5.537      } else {
   5.538          sqlite3_bind_null(session->set_device_group, 1);
   5.539      }
   5.540 +    
   5.541 +    sqlite3_bind_text(session->set_device_group, 2, user_id, -1,
   5.542 +            SQLITE_STATIC);
   5.543  
   5.544      result = sqlite3_step(session->set_device_group);
   5.545      sqlite3_reset(session->set_device_group);
   5.546 +    
   5.547 +    free(user_id);
   5.548 +    
   5.549      if (result != SQLITE_DONE)
   5.550          return PEP_CANNOT_SET_PERSON;
   5.551  
   5.552 @@ -1480,7 +1668,18 @@
   5.553      if (!(session && group_name))
   5.554          return PEP_ILLEGAL_VALUE;
   5.555  
   5.556 +    // 1. Get own user_id
   5.557 +    char* user_id = NULL;
   5.558 +    status = get_own_userid(session, &user_id);
   5.559 +    
   5.560 +    // No user_id is returned in this case, no need to free;
   5.561 +    if (status != PEP_STATUS_OK)
   5.562 +        return status;
   5.563 +
   5.564 +    // 2. get device group
   5.565      sqlite3_reset(session->get_device_group);
   5.566 +    sqlite3_bind_text(session->get_device_group, 1, user_id, -1,
   5.567 +            SQLITE_STATIC);
   5.568  
   5.569      result = sqlite3_step(session->get_device_group);
   5.570      switch (result) {
   5.571 @@ -1498,6 +1697,7 @@
   5.572          status = PEP_RECORD_NOT_FOUND;
   5.573      }
   5.574  
   5.575 +    free(user_id);
   5.576      sqlite3_reset(session->get_device_group);
   5.577      return status;
   5.578  }
     6.1 --- a/src/pEpEngine.h	Sat Dec 09 13:06:02 2017 +0100
     6.2 +++ b/src/pEpEngine.h	Sat Dec 09 13:06:29 2017 +0100
     6.3 @@ -470,7 +470,6 @@
     6.4      // the first octet flags are app defined settings
     6.5      PEP_idf_not_for_sync = 0x0001,   // don't use this identity for sync
     6.6      PEP_idf_list = 0x0002,           // identity of list of persons
     6.7 -
     6.8      // the second octet flags are calculated
     6.9      PEP_idf_devicegroup = 0x0100     // identity of a device group member
    6.10  } identity_flags;
    6.11 @@ -602,6 +601,27 @@
    6.12          PEP_SESSION session, const pEp_identity *identity
    6.13      );
    6.14  
    6.15 +// get_own_userid() - get the user_id of the own user
    6.16 +//
    6.17 +//    parameters:
    6.18 +//        session (in)        session handle
    6.19 +//        userid  (out)       own user id (if it exists)
    6.20 +//
    6.21 +//    return value:
    6.22 +//        PEP_STATUS_OK = 0             userid was found
    6.23 +//        PEP_CANNOT_FIND_IDENTITY      no own_user found in the DB
    6.24 +//        PEP_UNKNOWN_ERROR             results were returned, but no ID
    6.25 +//                                      found (no reason this should ever occur)
    6.26 +//    caveat:
    6.27 +//        userid will be NULL if not found; otherwise, returned string
    6.28 +//        belongs to the caller.
    6.29 +
    6.30 +DYNAMIC_API PEP_STATUS get_own_userid(
    6.31 +        PEP_SESSION session, 
    6.32 +        char** userid
    6.33 +    );
    6.34 +
    6.35 +
    6.36  // set_device_group() - update own person's device group
    6.37  //
    6.38  //    parameters:
    6.39 @@ -1129,6 +1149,16 @@
    6.40  
    6.41  DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session);
    6.42  
    6.43 +// This is used internally when there is a temporary identity to be retrieved
    6.44 +// that may not yet have an FPR attached. See get_identity() for functionality,
    6.45 +// params and caveats.
    6.46 +PEP_STATUS get_identity_without_fpr(
    6.47 +        PEP_SESSION session,
    6.48 +        const char *address,
    6.49 +        const char *user_id,
    6.50 +        pEp_identity **identity
    6.51 +    );
    6.52 +    
    6.53  #ifdef __cplusplus
    6.54  }
    6.55  #endif
     7.1 --- a/src/pEp_internal.h	Sat Dec 09 13:06:02 2017 +0100
     7.2 +++ b/src/pEp_internal.h	Sat Dec 09 13:06:29 2017 +0100
     7.3 @@ -124,6 +124,7 @@
     7.4      sqlite3_stmt *log;
     7.5      sqlite3_stmt *trustword;
     7.6      sqlite3_stmt *get_identity;
     7.7 +    sqlite3_stmt *get_identity_without_fpr;    
     7.8      sqlite3_stmt *replace_identities_fpr;
     7.9      sqlite3_stmt *set_person;
    7.10      sqlite3_stmt *set_device_group;
    7.11 @@ -151,9 +152,10 @@
    7.12      // Own keys
    7.13      sqlite3_stmt *own_key_is_listed;
    7.14      sqlite3_stmt *own_identities_retrieve;
    7.15 -    sqlite3_stmt *own_userid_by_address;
    7.16      sqlite3_stmt *own_keys_retrieve;
    7.17 -    sqlite3_stmt *set_own_key;
    7.18 +    sqlite3_stmt *get_own_userid;
    7.19 +
    7.20 +//    sqlite3_stmt *set_own_key;
    7.21  
    7.22      // sequence value
    7.23      sqlite3_stmt *sequence_value1;
     8.1 --- a/src/sync_actions.c	Sat Dec 09 13:06:02 2017 +0100
     8.2 +++ b/src/sync_actions.c	Sat Dec 09 13:06:29 2017 +0100
     8.3 @@ -56,8 +56,14 @@
     8.4      // key created first wins
     8.5  
     8.6      Identity me = NULL;
     8.7 -    PEP_STATUS status = get_identity(session, partner->address, PEP_OWN_USERID,
     8.8 -            &me);
     8.9 +    
    8.10 +    char* own_id = NULL;
    8.11 +    PEP_STATUS status = get_own_userid(session, &own_id);
    8.12 +    if (own_id) {
    8.13 +        status = get_identity(session, partner->address, own_id,
    8.14 +                              &me);
    8.15 +        free(own_id);
    8.16 +    }
    8.17      if (status == PEP_OUT_OF_MEMORY)
    8.18          return invalid_out_of_memory;
    8.19      if (status != PEP_STATUS_OK)
    8.20 @@ -142,9 +148,15 @@
    8.21      if (!session->notifyHandshake)
    8.22          return PEP_SYNC_NO_NOTIFY_CALLBACK;
    8.23  
    8.24 +    char* own_id = NULL;
    8.25 +    status = get_own_userid(session, &own_id);
    8.26 +        
    8.27      // notifyHandshake take ownership of given identities
    8.28      pEp_identity *me = NULL;
    8.29 -    status = get_identity(session, partner->address, PEP_OWN_USERID, &me);
    8.30 +    if (own_id) {
    8.31 +        status = get_identity(session, partner->address, own_id, &me);
    8.32 +        free(own_id);
    8.33 +    }
    8.34      if (status != PEP_STATUS_OK)
    8.35          goto error;
    8.36      
    8.37 @@ -235,16 +247,23 @@
    8.38      )
    8.39  {
    8.40      PEP_STATUS status = PEP_STATUS_OK;
    8.41 -
    8.42 +    
    8.43 +    char* own_id = NULL;
    8.44 +    status = get_own_userid(session, &own_id);
    8.45 +    
    8.46 +    // FIXME: Is this where and what we wanna do with this?
    8.47 +    if (status != PEP_STATUS_OK)
    8.48 +        return status;
    8.49 +        
    8.50      for (identity_list *il = group_keys; il && il->ident; il = il->next) {
    8.51  
    8.52 -        if (strcmp(il->ident->user_id, PEP_OWN_USERID)!=0) {
    8.53 +        if (strcmp(il->ident->user_id, own_id)!=0) {
    8.54              assert(0);
    8.55              continue;
    8.56          }
    8.57          // Check that identity isn't excluded from sync.
    8.58          pEp_identity *stored_identity = NULL;
    8.59 -        status = get_identity(session, il->ident->address, PEP_OWN_USERID,
    8.60 +        status = get_identity(session, il->ident->address, own_id,
    8.61                  &stored_identity);
    8.62          if (status == PEP_STATUS_OK) {
    8.63              if(stored_identity->flags & PEP_idf_not_for_sync){
    8.64 @@ -258,7 +277,8 @@
    8.65          if (status != PEP_STATUS_OK)
    8.66              break;
    8.67      }
    8.68 -
    8.69 +    
    8.70 +    free(own_id);
    8.71      return status;
    8.72  }
    8.73      
     9.1 --- a/src/sync_impl.c	Sat Dec 09 13:06:02 2017 +0100
     9.2 +++ b/src/sync_impl.c	Sat Dec 09 13:06:29 2017 +0100
     9.3 @@ -275,16 +275,25 @@
     9.4      // partner identity must be explicitely added DB to later
     9.5      // be able to communicate securely with it.
     9.6      if(partner){
     9.7 +        
     9.8 +        char* own_id = NULL;
     9.9 +        status = get_own_userid(session, &own_id);
    9.10 +        
    9.11 +        if (!own_id)
    9.12 +            own_id = strdup(PEP_OWN_USERID);
    9.13 +            
    9.14          // protect virtual user IDs 
    9.15          if((strncmp("TOFU_", partner->user_id, 6) == 0 &&
    9.16             strlen(partner->user_id) == strlen(partner->address) + 6 &&
    9.17             strcmp(partner->user_id + 6, partner->address)) ||
    9.18          // protect own ID 
    9.19 -           (strcmp(PEP_OWN_USERID, partner->user_id) == 0)){
    9.20 +           (strcmp(own_id, partner->user_id) == 0)){
    9.21              status = PEP_SYNC_ILLEGAL_MESSAGE;
    9.22 +            free(own_id);
    9.23              goto error;
    9.24          }
    9.25  
    9.26 +        free(own_id);
    9.27          // partner IDs are UUIDs bound to session lifespan
    9.28          // and therefore partner identities are not supposed
    9.29          // to mutate over time, but just not be used anymore.
    9.30 @@ -426,6 +435,9 @@
    9.31      bool discard = false;
    9.32      bool force_keep_msg = false;
    9.33  
    9.34 +    char* own_id = NULL;
    9.35 +    PEP_STATUS own_id_status = get_own_userid(session, &own_id);
    9.36 +
    9.37      for (bloblist_t *bl = src->attachments; bl && bl->value; bl = bl->next) {
    9.38          if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp.sync") == 0
    9.39                  && bl->size) {
    9.40 @@ -451,11 +463,18 @@
    9.41                              msg->header.me.address->size);
    9.42  
    9.43                  if(null_terminated_address){
    9.44 -                    status = get_identity(session, 
    9.45 -                                          null_terminated_address, 
    9.46 -                                          PEP_OWN_USERID, 
    9.47 -                                          &check_me);
    9.48 -                    free(null_terminated_address);
    9.49 +                    
    9.50 +                    if (own_id) {                        
    9.51 +                        status = get_identity(session, 
    9.52 +                                              null_terminated_address, 
    9.53 +                                              own_id, 
    9.54 +                                              &check_me);
    9.55 +                        free(null_terminated_address);
    9.56 +
    9.57 +                    }
    9.58 +                    else {
    9.59 +                        status = own_id_status;
    9.60 +                    }
    9.61                  } 
    9.62                  else
    9.63                      status = PEP_OUT_OF_MEMORY;
    9.64 @@ -608,10 +627,23 @@
    9.65                              // GroupUpdate and UpdateRequests come from group.
    9.66                              // check trust relation in between signer key and 
    9.67                              // own id to be sure.
    9.68 -                            pEp_identity *_from = new_identity(NULL, 
    9.69 -                                                               keylist->value,
    9.70 -                                                               PEP_OWN_USERID,
    9.71 -                                                               NULL);
    9.72 +                            
    9.73 +                            if (status != PEP_STATUS_OK)
    9.74 +                                goto free_all;
    9.75 +                            
    9.76 +                            pEp_identity* _from = NULL;
    9.77 +                            
    9.78 +                            if (own_id) {    
    9.79 +                                _from = new_identity(NULL, 
    9.80 +                                                     keylist->value,
    9.81 +                                                     own_id,
    9.82 +                                                     NULL);
    9.83 +                            }
    9.84 +                            else {
    9.85 +                                status = own_id_status;
    9.86 +                                goto free_all;
    9.87 +                            }
    9.88 +                            
    9.89                              if (_from == NULL){
    9.90                                  status = PEP_OUT_OF_MEMORY;
    9.91                                  goto free_all;
    9.92 @@ -658,7 +690,7 @@
    9.93                  ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    9.94              free_userid:
    9.95                  free(user_id);
    9.96 -
    9.97 +                free(own_id);
    9.98                  if (status != PEP_STATUS_OK)
    9.99                      return status;
   9.100              }
   9.101 @@ -765,10 +797,15 @@
   9.102          goto error;
   9.103      }
   9.104  
   9.105 +    char* own_id = NULL;
   9.106 +    status = get_own_userid(session, &own_id);
   9.107 +    if (status != PEP_STATUS_OK)
   9.108 +        goto error;
   9.109 +
   9.110      msg->header.version.major = SYNC_VERSION_MAJOR;
   9.111      msg->header.version.minor = SYNC_VERSION_MINOR;
   9.112  
   9.113 -    status = get_identity(session, partner->address, PEP_OWN_USERID, &me);
   9.114 +    status = get_identity(session, partner->address, own_id, &me);
   9.115      if (status != PEP_STATUS_OK)
   9.116          goto error;
   9.117      
   9.118 @@ -889,6 +926,7 @@
   9.119      free(payload);
   9.120      free_message(_message);
   9.121      free_identity(me);
   9.122 +    free(own_id);
   9.123      return status;
   9.124  }
   9.125  
    10.1 --- a/test/case_and_dot_address_test.cc	Sat Dec 09 13:06:02 2017 +0100
    10.2 +++ b/test/case_and_dot_address_test.cc	Sat Dec 09 13:06:29 2017 +0100
    10.3 @@ -39,36 +39,42 @@
    10.4      assert(statuspub == PEP_STATUS_OK);
    10.5      assert(statuspriv == PEP_STATUS_OK);
    10.6  
    10.7 -    pEp_identity * alice_id = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
    10.8 -    status = update_identity(session, alice_id);
    10.9 -    assert(alice_id->fpr);
   10.10 -    assert(strcmp(alice_id->fpr, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97") == 0);
   10.11 +    pEp_identity * alice_id = new_identity("pep.test.alice@pep-project.org", "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", PEP_OWN_USERID, "Alice Test");
   10.12 +    identity_list* own_id = new_identity_list(alice_id);
   10.13 +    status = initialise_own_identities(session, own_id);
   10.14 +    status = trust_personal_key(session, alice_id);
   10.15 +    pEp_identity * new_alice_id = new_identity("pep.test.alice@pep-project.org", "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", PEP_OWN_USERID, "Alice Test");
   10.16 +    status = update_identity(session, new_alice_id);
   10.17 +    assert(new_alice_id->fpr);
   10.18 +    assert(strcmp(new_alice_id->fpr, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97") == 0);
   10.19 +    free_identity(new_alice_id);
   10.20      free_identity(alice_id);
   10.21      alice_id = NULL;
   10.22 +    new_alice_id = NULL;
   10.23  
   10.24      alice_id = new_identity(alice_email_case, NULL, PEP_OWN_USERID, "Alice Test");
   10.25 -    status = update_identity(session, alice_id);
   10.26 +    status = myself(session, alice_id);
   10.27      assert(alice_id->fpr);
   10.28      assert(strcmp(alice_id->fpr, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97") == 0);
   10.29      free_identity(alice_id);
   10.30      alice_id = NULL;
   10.31  
   10.32      alice_id = new_identity(alice_email_dot, NULL, PEP_OWN_USERID, "Alice Test");
   10.33 -    status = update_identity(session, alice_id);
   10.34 +    status = myself(session, alice_id);
   10.35      assert(alice_id->fpr);
   10.36      assert(strcmp(alice_id->fpr, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97") == 0);
   10.37      free_identity(alice_id);
   10.38      alice_id = NULL;
   10.39  
   10.40      alice_id = new_identity(alice_email_dotless, NULL, PEP_OWN_USERID, "Alice Test");
   10.41 -    status = update_identity(session, alice_id);
   10.42 +    status = myself(session, alice_id);
   10.43      assert(alice_id->fpr);
   10.44      assert(strcmp(alice_id->fpr, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97") == 0);
   10.45      free_identity(alice_id);
   10.46      alice_id = NULL;
   10.47  
   10.48      alice_id = new_identity(alice_email_case_and_dot, NULL, PEP_OWN_USERID, "Alice Test");
   10.49 -    status = update_identity(session, alice_id);
   10.50 +    status = myself(session, alice_id);
   10.51      assert(alice_id->fpr);
   10.52      assert(strcmp(alice_id->fpr, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97") == 0);
   10.53      free_identity(alice_id);