sequoia: Quote the name in and more carefully extract URIs from User ID sync
authorNeal H. Walfield <neal@pep.foundation>
Fri, 31 May 2019 23:35:59 +0200
branchsync
changeset 3796442937f4fabb
parent 3794 e7f94d975542
child 3797 657dc02b1cc3
sequoia: Quote the name in and more carefully extract URIs from User ID

- When creating a new key, quote the name part of the User ID. This
is essential to support names with @ characters and commas, which
are often used by pEp.

- More carefully extract URIs from User IDs.

- Don't forget to extract them in email_cmp.
src/pgp_sequoia.c
     1.1 --- a/src/pgp_sequoia.c	Fri May 31 12:27:39 2019 +0200
     1.2 +++ b/src/pgp_sequoia.c	Fri May 31 23:35:59 2019 +0200
     1.3 @@ -141,9 +141,13 @@
     1.4  
     1.5      char *a_address = NULL;
     1.6      pgp_user_id_address_normalized(NULL, a_userid, &a_address);
     1.7 +    if (!a_address)
     1.8 +        pgp_user_id_other(NULL, a_userid, &a_address);
     1.9  
    1.10      char *b_address = NULL;
    1.11      pgp_user_id_address_normalized(NULL, b_userid, &b_address);
    1.12 +    if (!b_address)
    1.13 +        pgp_user_id_other(NULL, b_userid, &b_address);
    1.14  
    1.15      pgp_packet_free(a_userid);
    1.16      pgp_packet_free(b_userid);
    1.17 @@ -793,54 +797,7 @@
    1.18  
    1.19          pgp_packet_t userid = pgp_user_id_new (user_id_value);
    1.20          pgp_user_id_name(NULL, userid, &name);
    1.21 -        pgp_user_id_address(NULL, userid, &email);
    1.22 -                
    1.23 -        if (!email || email[0] == '\0') {
    1.24 -            size_t uid_value_len;
    1.25 -            const char* uid_value = (const char*)pgp_user_id_value(userid, &uid_value_len);
    1.26 -            if (!uid_value) {
    1.27 -                // We need some kind of an error here, maybe?
    1.28 -                 
    1.29 -            }
    1.30 -            else {
    1.31 -                // Ok, asan gets really pissed at us using this string directly, SO...
    1.32 -                char* uid_copy = calloc(uid_value_len + 1, 1);
    1.33 -                strlcpy(uid_copy, uid_value, uid_value_len);
    1.34 -                const char* split = strstr(uid_copy, "<");
    1.35 -                if (split != uid_copy) {       
    1.36 -                    while (split) {
    1.37 -                        if (isspace(*(split - 1)))
    1.38 -                            break;
    1.39 -                        split = strstr(split + 1, "<");
    1.40 -                    }
    1.41 -                }
    1.42 -                if (split) {
    1.43 -                    char* stopchr = strrchr(split, '>');
    1.44 -                    if (stopchr) {
    1.45 -                        int email_len = stopchr - split - 1;
    1.46 -                        email = calloc(email_len + 1, 1); 
    1.47 -                        strlcpy(email, split + 1, email_len + 1);
    1.48 -                        const char* last = NULL;
    1.49 -                        if (split != uid_value) {
    1.50 -                            for (last = split - 1; last > uid_value; last--) {
    1.51 -                                if (!isspace(*last))
    1.52 -                                    break;
    1.53 -                            }
    1.54 -                            int name_len = (last - uid_value) + 1;
    1.55 -                            name = calloc(name_len + 1, 1);
    1.56 -                            strlcpy(name, uid_value, name_len + 1);
    1.57 -                        }
    1.58 -                    }
    1.59 -                    else  
    1.60 -                        split = NULL;
    1.61 -                }
    1.62 -                if (split == NULL)
    1.63 -                    email = uid_copy;
    1.64 -                else 
    1.65 -                    free(uid_copy);
    1.66 -            }
    1.67 -        }
    1.68 -        
    1.69 +        pgp_user_id_address_or_other(NULL, userid, &email);
    1.70          pgp_packet_free(userid);
    1.71          free(user_id_value);
    1.72  
    1.73 @@ -1819,6 +1776,7 @@
    1.74  {
    1.75      PEP_STATUS status = PEP_STATUS_OK;
    1.76      pgp_error_t err = NULL;
    1.77 +    pgp_packet_t userid_packet = NULL;
    1.78      char *userid = NULL;
    1.79      pgp_tpk_t tpk = NULL;
    1.80      pgp_fingerprint_t pgp_fpr = NULL;
    1.81 @@ -1830,16 +1788,22 @@
    1.82      assert(identity->fpr == NULL || identity->fpr[0] == 0);
    1.83      assert(identity->username);
    1.84  
    1.85 -    size_t userid_size = strlen(identity->username)+strlen(identity->address)+3+1;
    1.86 -    userid = (char *) calloc(1, userid_size);
    1.87 -    assert(userid);
    1.88 +    userid_packet = pgp_user_id_from_unchecked_address(&err,
    1.89 +                                                       identity->username, NULL,
    1.90 +                                                       identity->address);
    1.91 +    if (!userid_packet)
    1.92 +        ERROR_OUT(err, PEP_UNKNOWN_ERROR, "pgp_user_id_from_other_address");
    1.93 +
    1.94 +    size_t userid_len = 0;
    1.95 +    const uint8_t *raw = pgp_user_id_value(userid_packet, &userid_len);
    1.96 +
    1.97 +    // Null terminate it.
    1.98 +    userid = malloc(userid_len + 1);
    1.99      if (!userid)
   1.100          ERROR_OUT(NULL, PEP_OUT_OF_MEMORY, "out of memory");
   1.101  
   1.102 -    int r = snprintf(userid, userid_size, "%s <%s>", identity->username, identity->address);
   1.103 -    assert(r >= 0 && r < userid_size);
   1.104 -    if (r < 0)
   1.105 -        ERROR_OUT(NULL, PEP_UNKNOWN_ERROR, "snprintf");
   1.106 +    memcpy(userid, raw, userid_len);
   1.107 +    userid[userid_len] = 0;
   1.108  
   1.109      T("(%s)", userid);
   1.110  
   1.111 @@ -1873,6 +1837,8 @@
   1.112      if (tpk)
   1.113          pgp_tpk_free(tpk);
   1.114      free(userid);
   1.115 +    if (userid_packet)
   1.116 +        pgp_packet_free(userid_packet);
   1.117  
   1.118      T("-> %s", pEp_status_to_string(status));
   1.119      return status;