Sequoia fix - we can now import multiple concatenated armoured keys. Returns PEP_SOME_KEYS_IMPORTED if only some keyfiles work out. sync
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Fri, 28 Jun 2019 17:41:02 +0200
branchsync
changeset 388920e17006f286
parent 3887 a3f479737dc4
child 3890 6f269a58707c
child 3898 af2d664f4eb6
Sequoia fix - we can now import multiple concatenated armoured keys. Returns PEP_SOME_KEYS_IMPORTED if only some keyfiles work out.
src/identity_list.c
src/identity_list.h
src/keymanagement.c
src/pEpEngine.h
src/pgp_sequoia.c
     1.1 --- a/src/identity_list.c	Thu Jun 27 15:14:20 2019 +0200
     1.2 +++ b/src/identity_list.c	Fri Jun 28 17:41:02 2019 +0200
     1.3 @@ -108,6 +108,23 @@
     1.4      return list_curr->next;
     1.5  }
     1.6  
     1.7 +// returns *head* of list
     1.8 +DYNAMIC_API identity_list* identity_list_join(identity_list *first_list, identity_list *second_list) {
     1.9 +    if (!first_list) {
    1.10 +        if (!second_list)
    1.11 +            return NULL;
    1.12 +        return second_list;
    1.13 +    }
    1.14 +    if (second_list) {
    1.15 +        identity_list* list_curr = first_list;
    1.16 +        while (list_curr->next)
    1.17 +            list_curr = list_curr->next;    
    1.18 +            
    1.19 +        list_curr->next = second_list;
    1.20 +    }        
    1.21 +    return first_list;    
    1.22 +}
    1.23 +
    1.24  DYNAMIC_API int identity_list_length(const identity_list *id_list)
    1.25  {
    1.26      int len = 0;
     2.1 --- a/src/identity_list.h	Thu Jun 27 15:14:20 2019 +0200
     2.2 +++ b/src/identity_list.h	Fri Jun 28 17:41:02 2019 +0200
     2.3 @@ -63,6 +63,16 @@
     2.4  
     2.5  DYNAMIC_API identity_list *identity_list_add(identity_list *id_list, pEp_identity *ident);
     2.6  
     2.7 +// identity_list_add - join second identity_list to the first.
     2.8 +//
     2.9 +//  parameters:
    2.10 +//      first_list (in)             identity_list to add to
    2.11 +//      second_list (in)            identity list to add
    2.12 +//
    2.13 +//  return value:
    2.14 +//      pointer to the HEAD of the new list, or NULL if both lists are empty.
    2.15 +//
    2.16 +DYNAMIC_API identity_list *identity_list_join(identity_list *first_list, identity_list* second_list);
    2.17  
    2.18  // identity_list_length() - get length of identity_list
    2.19  //
    2.20 @@ -73,9 +83,7 @@
    2.21  //      length of identity_list in number of elements
    2.22  
    2.23  DYNAMIC_API int identity_list_length(const identity_list *id_list);
    2.24 -
    2.25 -
    2.26 +    
    2.27  #ifdef __cplusplus
    2.28  }
    2.29  #endif
    2.30 -
     3.1 --- a/src/keymanagement.c	Thu Jun 27 15:14:20 2019 +0200
     3.2 +++ b/src/keymanagement.c	Fri Jun 28 17:41:02 2019 +0200
     3.3 @@ -495,7 +495,7 @@
     3.4          stored_ident->fpr = NULL;
     3.5          stored_ident->comm_type = PEP_ct_key_not_found;        
     3.6      }
     3.7 -    else {
     3.8 +    else { // no key returned, but status ok?
     3.9          if (stored_ident->comm_type == PEP_ct_unknown)
    3.10              stored_ident->comm_type = PEP_ct_key_not_found;
    3.11      }
     4.1 --- a/src/pEpEngine.h	Thu Jun 27 15:14:20 2019 +0200
     4.2 +++ b/src/pEpEngine.h	Fri Jun 28 17:41:02 2019 +0200
     4.3 @@ -60,6 +60,7 @@
     4.4      PEP_KEY_IMPORTED                                = 0x0220,
     4.5      PEP_NO_KEY_IMPORTED                             = 0x0221,
     4.6      PEP_KEY_IMPORT_STATUS_UNKNOWN                   = 0x0222,
     4.7 +    PEP_SOME_KEYS_IMPORTED                          = 0x0223,
     4.8      
     4.9      PEP_CANNOT_FIND_IDENTITY                        = 0x0301,
    4.10      PEP_CANNOT_SET_PERSON                           = 0x0381,
     5.1 --- a/src/pgp_sequoia.c	Thu Jun 27 15:14:20 2019 +0200
     5.2 +++ b/src/pgp_sequoia.c	Fri Jun 28 17:41:02 2019 +0200
     5.3 @@ -1854,7 +1854,22 @@
     5.4      return status;
     5.5  }
     5.6  
     5.7 -PEP_STATUS pgp_import_keydata(PEP_SESSION session, const char *key_data,
     5.8 +static unsigned int count_keydata_parts(const char* key_data) {
     5.9 +    unsigned int retval = 0;
    5.10 +    
    5.11 +    const char* pgp_begin = "-----BEGIN PGP";
    5.12 +    size_t prefix_len = strlen(pgp_begin);
    5.13 +    while (key_data) {
    5.14 +        key_data = strstr(key_data, pgp_begin);
    5.15 +        if (key_data) {
    5.16 +            retval++;
    5.17 +            key_data += prefix_len;
    5.18 +        }
    5.19 +    }
    5.20 +    return retval;
    5.21 + }
    5.22 +
    5.23 +PEP_STATUS _pgp_import_keydata(PEP_SESSION session, const char *key_data,
    5.24                                size_t size, identity_list **private_idents)
    5.25  {
    5.26      PEP_STATUS status = PEP_NO_KEY_IMPORTED;
    5.27 @@ -1986,6 +2001,71 @@
    5.28      return status;
    5.29  }
    5.30  
    5.31 +PEP_STATUS pgp_import_keydata(PEP_SESSION session, const char *key_data,
    5.32 +                              size_t size, identity_list **private_idents)
    5.33 +{
    5.34 +    unsigned int keycount = count_keydata_parts(key_data);
    5.35 +    if (keycount < 2)
    5.36 +        return(_pgp_import_keydata(session, key_data, size, private_idents));
    5.37 +
    5.38 +    const char* pgp_begin = "-----BEGIN PGP";
    5.39 +    size_t prefix_len = strlen(pgp_begin);
    5.40 +        
    5.41 +    unsigned int i;
    5.42 +    const char* curr_begin;
    5.43 +    size_t curr_size;
    5.44 +    
    5.45 +    identity_list* collected_idents = NULL;        
    5.46 +    
    5.47 +    PEP_STATUS retval = PEP_KEY_IMPORTED;
    5.48 +    
    5.49 +    for (i = 0, curr_begin = key_data; i < keycount; i++) {
    5.50 +        const char* next_begin = strstr(curr_begin + prefix_len, pgp_begin);
    5.51 +        if (next_begin)
    5.52 +            curr_size = next_begin - curr_begin;
    5.53 +        else
    5.54 +            curr_size = (key_data + size) - curr_begin;
    5.55 +        
    5.56 +        PEP_STATUS curr_status = _pgp_import_keydata(session, curr_begin, curr_size, private_idents);
    5.57 +        if (private_idents && *private_idents) {
    5.58 +            if (!collected_idents)
    5.59 +                collected_idents = *private_idents;
    5.60 +            else 
    5.61 +                identity_list_join(collected_idents, *private_idents);
    5.62 +            *private_idents = NULL;    
    5.63 +        }
    5.64 +        
    5.65 +        if (curr_status != retval) {
    5.66 +            switch (curr_status) {
    5.67 +                case PEP_NO_KEY_IMPORTED:
    5.68 +                case PEP_KEY_NOT_FOUND:
    5.69 +                case PEP_UNKNOWN_ERROR:
    5.70 +                    switch (retval) {
    5.71 +                        case PEP_KEY_IMPORTED:
    5.72 +                            retval = PEP_SOME_KEYS_IMPORTED;
    5.73 +                            break;
    5.74 +                        case PEP_UNKNOWN_ERROR:
    5.75 +                            retval = curr_status;
    5.76 +                            break;
    5.77 +                        default:
    5.78 +                            break;
    5.79 +                    }
    5.80 +                    break;
    5.81 +                case PEP_KEY_IMPORTED:
    5.82 +                    retval = PEP_SOME_KEYS_IMPORTED;
    5.83 +                default:
    5.84 +                    break;
    5.85 +            }        
    5.86 +        }        
    5.87 +        curr_begin = next_begin;     
    5.88 +    }
    5.89 +    
    5.90 +    if (private_idents)
    5.91 +        *private_idents = collected_idents;
    5.92 +    
    5.93 +    return retval;    
    5.94 +}
    5.95 +
    5.96  PEP_STATUS pgp_export_keydata(
    5.97          PEP_SESSION session, const char *fpr, char **key_data, size_t *size,
    5.98          bool secret)