ENGINE-178: fixing context problem. However, still can't tell if it would verify a posteo mail. ENGINE-178
authorKrista Grothoff <krista@pep-project.org>
Mon, 20 Feb 2017 15:15:14 +0100
branchENGINE-178
changeset 1591df20d8334b91
parent 1584 d21b3f92542d
child 1592 cc35fb239fce
ENGINE-178: fixing context problem. However, still can't tell if it would verify a posteo mail.
src/pgp_gpg.c
     1.1 --- a/src/pgp_gpg.c	Fri Feb 17 11:18:27 2017 +0100
     1.2 +++ b/src/pgp_gpg.c	Mon Feb 20 15:15:14 2017 +0100
     1.3 @@ -456,7 +456,51 @@
     1.4              case GPG_ERR_NO_ERROR:
     1.5              {
     1.6                  gpgme_decrypt_result = gpg.gpgme_op_decrypt_result(session->ctx);
     1.7 -                
     1.8 +                /* NOW is when we have to process the decrypt_result, period.
     1.9 +                   it is only valid until the next call on the context. */
    1.10 +                   
    1.11 +                gpgme_key_t key;
    1.12 +                memset(&key,0,sizeof(key));
    1.13 +                stringlist_t* recipient_keylist = new_stringlist(NULL);
    1.14 +                if (!recipient_keylist) // FIXME: mem cleanup
    1.15 +                    return PEP_OUT_OF_MEMORY;
    1.16 +               
    1.17 +                if (gpgme_decrypt_result != NULL) {
    1.18 +                    stringlist_t* _keylist = recipient_keylist;
    1.19 +                    for (gpgme_recipient_t r = gpgme_decrypt_result->recipients; r != NULL; r = r->next) {
    1.20 +                        // GPGME may give subkey's fpr instead of primary key's fpr.
    1.21 +                        // Therefore we ask for the primary fingerprint instead
    1.22 +                        // we assume that gpgme_get_key can find key by subkey's fpr
    1.23 +                        gpgme_error = gpg.gpgme_get_key(session->ctx,
    1.24 +                            r->keyid, &key, 0);
    1.25 +                        gpgme_error = _GPGERR(gpgme_error);
    1.26 +                        assert(gpgme_error != GPG_ERR_ENOMEM);
    1.27 +                        if (gpgme_error == GPG_ERR_ENOMEM) {
    1.28 +                            free_stringlist(_keylist);
    1.29 +                            result = PEP_OUT_OF_MEMORY;
    1.30 +                        }
    1.31 +                        // Primary key is given as the first subkey
    1.32 +                        if (gpgme_error == GPG_ERR_NO_ERROR &&
    1.33 +                            key && key->subkeys && key->subkeys->fpr
    1.34 +                            && key->subkeys->fpr[0]) {
    1.35 +                            _keylist = stringlist_add(_keylist, key->subkeys->fpr);
    1.36 + 
    1.37 +                            gpg.gpgme_key_unref(key);
    1.38 + 
    1.39 +                        }
    1.40 +                    }
    1.41 +                    assert(_keylist);
    1.42 +                    if (_keylist == NULL) {
    1.43 +                        free_stringlist(recipient_keylist);
    1.44 +                        if (*keylist)
    1.45 +                            free_stringlist(*keylist);
    1.46 +                        *keylist = NULL;
    1.47 +                        result = PEP_OUT_OF_MEMORY;
    1.48 +                    }
    1.49 +                } /* Ok, so now we have any recipients it was encrypted for
    1.50 +                     in recipient_keylist */
    1.51 +            
    1.52 +                   
    1.53                  gpgme_verify_result_t gpgme_verify_result;
    1.54                  char *_buffer = NULL;
    1.55                  size_t reading;
    1.56 @@ -475,6 +519,8 @@
    1.57                  if (_buffer == NULL) {
    1.58                      gpg.gpgme_data_release(plain);
    1.59                      gpg.gpgme_data_release(cipher);
    1.60 +                    if (recipient_keylist)
    1.61 +                        free_stringlist(recipient_keylist);
    1.62                      return PEP_OUT_OF_MEMORY;
    1.63                  }
    1.64  
    1.65 @@ -517,7 +563,8 @@
    1.66  
    1.67                      result = PEP_DECRYPTED_AND_VERIFIED;
    1.68                      gpg.gpgme_check(NULL);
    1.69 -                    do {
    1.70 +                    do { /* get all signers and put them at the front off
    1.71 +                            the keylist (likely only one) */
    1.72                          switch (_GPGERR(gpgme_signature->status)) {
    1.73                          case GPG_ERR_NO_ERROR:
    1.74                          {
    1.75 @@ -551,6 +598,8 @@
    1.76  
    1.77                                  if (k == NULL) {
    1.78                                      free_stringlist(_keylist);
    1.79 +                                    if (recipient_keylist)
    1.80 +                                        free (recipient_keylist);
    1.81                                      gpg.gpgme_data_release(plain);
    1.82                                      gpg.gpgme_data_release(cipher);
    1.83                                      free(_buffer);
    1.84 @@ -574,6 +623,8 @@
    1.85                              k = stringlist_add(k, gpgme_signature->fpr);
    1.86                              if (k == NULL) {
    1.87                                  free_stringlist(_keylist);
    1.88 +                                if (recipient_keylist)
    1.89 +                                    free_stringlist(recipient_keylist);
    1.90                                  gpg.gpgme_data_release(plain);
    1.91                                  gpg.gpgme_data_release(cipher);
    1.92                                  free(_buffer);
    1.93 @@ -601,9 +652,24 @@
    1.94                      *psize = reading;
    1.95                      (*ptext)[*psize] = 0; // safeguard for naive users
    1.96                      *keylist = _keylist;
    1.97 +                    if (recipient_keylist)
    1.98 +                        if (!_keylist)
    1.99 +                            *keylist = new_stringlist(""); // no sig 
   1.100 +                        if (!(*keylist)) {
   1.101 +                            free_stringlist(_keylist);
   1.102 +                            if (recipient_keylist)
   1.103 +                                free_stringlist(recipient_keylist);
   1.104 +                            gpg.gpgme_data_release(plain);
   1.105 +                            gpg.gpgme_data_release(cipher);
   1.106 +                            free(_buffer);
   1.107 +                            return PEP_OUT_OF_MEMORY;
   1.108 +                        }    
   1.109 +                        stringlist_append(*keylist, recipient_keylist);
   1.110                  }
   1.111                  else {
   1.112                      free_stringlist(_keylist);
   1.113 +                    if (recipient_keylist)
   1.114 +                        free_stringlist(recipient_keylist);
   1.115                      free(_buffer);
   1.116                  }
   1.117                  break;
   1.118 @@ -636,44 +702,44 @@
   1.119          result = PEP_DECRYPT_WRONG_FORMAT;
   1.120      }
   1.121  
   1.122 -    if (result != PEP_DECRYPT_WRONG_FORMAT && result != PEP_OUT_OF_MEMORY) {
   1.123 -        gpgme_key_t key;
   1.124 -        memset(&key,0,sizeof(key));
   1.125 -        
   1.126 -        if (gpgme_decrypt_result != NULL) {
   1.127 -            if (!(*keylist))
   1.128 -                *keylist = new_stringlist(""); // no sig
   1.129 -            stringlist_t* _keylist = *keylist;
   1.130 -            for (gpgme_recipient_t r = gpgme_decrypt_result->recipients; r != NULL; r = r->next) {
   1.131 -                // GPGME may give subkey's fpr instead of primary key's fpr.
   1.132 -                // Therefore we ask for the primary fingerprint instead
   1.133 -                // we assume that gpgme_get_key can find key by subkey's fpr
   1.134 -                gpgme_error = gpg.gpgme_get_key(session->ctx,
   1.135 -                    r->keyid, &key, 0);
   1.136 -                gpgme_error = _GPGERR(gpgme_error);
   1.137 -                assert(gpgme_error != GPG_ERR_ENOMEM);
   1.138 -                if (gpgme_error == GPG_ERR_ENOMEM) {
   1.139 -                    free_stringlist(_keylist);
   1.140 -                    result = PEP_OUT_OF_MEMORY;
   1.141 -                }
   1.142 -                // Primary key is given as the first subkey
   1.143 -                if (gpgme_error == GPG_ERR_NO_ERROR &&
   1.144 -                    key && key->subkeys && key->subkeys->fpr
   1.145 -                    && key->subkeys->fpr[0]) {
   1.146 -                    _keylist = stringlist_add(_keylist, key->subkeys->fpr);
   1.147 -
   1.148 -                    gpg.gpgme_key_unref(key);
   1.149 -
   1.150 -                }
   1.151 -            }
   1.152 -            assert(_keylist);
   1.153 -            if (_keylist == NULL) {
   1.154 -                free_stringlist(*keylist);
   1.155 -                *keylist = NULL;
   1.156 -                result = PEP_OUT_OF_MEMORY;
   1.157 -            }
   1.158 -        }
   1.159 -    }
   1.160 +    // if (result != PEP_DECRYPT_WRONG_FORMAT && result != PEP_OUT_OF_MEMORY) {
   1.161 +    //     gpgme_key_t key;
   1.162 +    //     memset(&key,0,sizeof(key));
   1.163 +    //     
   1.164 +    //     if (gpgme_decrypt_result != NULL) {
   1.165 +    //         if (!(*keylist))
   1.166 +    //             *keylist = new_stringlist(""); // no sig
   1.167 +    //         stringlist_t* _keylist = *keylist;
   1.168 +    //         for (gpgme_recipient_t r = gpgme_decrypt_result->recipients; r != NULL; r = r->next) {
   1.169 +    //             // GPGME may give subkey's fpr instead of primary key's fpr.
   1.170 +    //             // Therefore we ask for the primary fingerprint instead
   1.171 +    //             // we assume that gpgme_get_key can find key by subkey's fpr
   1.172 +    //             gpgme_error = gpg.gpgme_get_key(session->ctx,
   1.173 +    //                 r->keyid, &key, 0);
   1.174 +    //             gpgme_error = _GPGERR(gpgme_error);
   1.175 +    //             assert(gpgme_error != GPG_ERR_ENOMEM);
   1.176 +    //             if (gpgme_error == GPG_ERR_ENOMEM) {
   1.177 +    //                 free_stringlist(_keylist);
   1.178 +    //                 result = PEP_OUT_OF_MEMORY;
   1.179 +    //             }
   1.180 +    //             // Primary key is given as the first subkey
   1.181 +    //             if (gpgme_error == GPG_ERR_NO_ERROR &&
   1.182 +    //                 key && key->subkeys && key->subkeys->fpr
   1.183 +    //                 && key->subkeys->fpr[0]) {
   1.184 +    //                 _keylist = stringlist_add(_keylist, key->subkeys->fpr);
   1.185 +    // 
   1.186 +    //                 gpg.gpgme_key_unref(key);
   1.187 +    // 
   1.188 +    //             }
   1.189 +    //         }
   1.190 +    //         assert(_keylist);
   1.191 +    //         if (_keylist == NULL) {
   1.192 +    //             free_stringlist(*keylist);
   1.193 +    //             *keylist = NULL;
   1.194 +    //             result = PEP_OUT_OF_MEMORY;
   1.195 +    //         }
   1.196 +    //     }
   1.197 +    //}
   1.198  
   1.199      gpg.gpgme_data_release(plain);
   1.200      gpg.gpgme_data_release(cipher);