NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
crypt_gpgme.h File Reference

Wrapper for PGP/SMIME calls to GPGME. More...

#include <gpgme.h>
#include <stdbool.h>
#include <stdio.h>
#include "lib.h"
+ Include dependency graph for crypt_gpgme.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  CryptKeyInfo
 A stored PGP key. More...
 

Enumerations

enum  KeyInfo {
  KIP_NAME = 0, KIP_AKA, KIP_VALID_FROM, KIP_VALID_TO,
  KIP_KEY_TYPE, KIP_KEY_USAGE, KIP_FINGERPRINT, KIP_SERIAL_NO,
  KIP_ISSUED_BY, KIP_SUBKEY, KIP_MAX
}
 PGP Key info. More...
 
enum  KeyCap { KEY_CAP_CAN_ENCRYPT, KEY_CAP_CAN_SIGN, KEY_CAP_CAN_CERTIFY }
 PGP/SMIME Key Capabilities. More...
 

Functions

void pgp_gpgme_set_sender (const char *sender)
 Implements CryptModuleSpecs::set_sender() -. More...
 
int pgp_gpgme_application_handler (struct Body *m, struct State *s)
 Implements CryptModuleSpecs::application_handler() -. More...
 
bool pgp_gpgme_check_traditional (FILE *fp, struct Body *b, bool just_one)
 Implements CryptModuleSpecs::pgp_check_traditional() -. More...
 
int pgp_gpgme_decrypt_mime (FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **cur)
 Implements CryptModuleSpecs::decrypt_mime() -. More...
 
int pgp_gpgme_encrypted_handler (struct Body *a, struct State *s)
 Implements CryptModuleSpecs::encrypted_handler() -This handler is passed the application/octet-stream directly. More...
 
struct Bodypgp_gpgme_encrypt_message (struct Body *a, char *keylist, bool sign, const struct AddressList *from)
 Implements CryptModuleSpecs::pgp_encrypt_message() -. More...
 
char * pgp_gpgme_find_keys (struct AddressList *addrlist, bool oppenc_mode)
 Implements CryptModuleSpecs::find_keys() -. More...
 
void pgp_gpgme_invoke_import (const char *fname)
 Implements CryptModuleSpecs::pgp_invoke_import() -. More...
 
struct Bodypgp_gpgme_make_key_attachment (void)
 Implements CryptModuleSpecs::pgp_make_key_attachment() -. More...
 
SecurityFlags pgp_gpgme_send_menu (struct Mailbox *m, struct Email *e)
 Implements CryptModuleSpecs::send_menu() -. More...
 
struct Bodypgp_gpgme_sign_message (struct Body *a, const struct AddressList *from)
 Implements CryptModuleSpecs::sign_message() -. More...
 
int pgp_gpgme_verify_one (struct Body *sigbdy, struct State *s, const char *tempfile)
 Implements CryptModuleSpecs::verify_one() -. More...
 
int smime_gpgme_application_handler (struct Body *a, struct State *s)
 Implements CryptModuleSpecs::application_handler() -. More...
 
struct Bodysmime_gpgme_build_smime_entity (struct Body *a, char *keylist)
 Implements CryptModuleSpecs::smime_build_smime_entity() -. More...
 
int smime_gpgme_decrypt_mime (FILE *fp_in, FILE **fp_out, struct Body *b, struct Body **cur)
 Implements CryptModuleSpecs::decrypt_mime() -. More...
 
char * smime_gpgme_find_keys (struct AddressList *addrlist, bool oppenc_mode)
 Implements CryptModuleSpecs::find_keys() -. More...
 
void smime_gpgme_init (void)
 Implements CryptModuleSpecs::init() -. More...
 
SecurityFlags smime_gpgme_send_menu (struct Mailbox *m, struct Email *e)
 Implements CryptModuleSpecs::send_menu() -. More...
 
struct Bodysmime_gpgme_sign_message (struct Body *a, const struct AddressList *from)
 Implements CryptModuleSpecs::sign_message() -. More...
 
int smime_gpgme_verify_one (struct Body *sigbdy, struct State *s, const char *tempfile)
 Implements CryptModuleSpecs::verify_one() -. More...
 
int smime_gpgme_verify_sender (struct Mailbox *m, struct Email *e, struct Message *msg)
 Implements CryptModuleSpecs::smime_verify_sender() -. More...
 
bool crypt_id_is_strong (struct CryptKeyInfo *key)
 Is the key strong. More...
 
int digit (const char *s)
 
const char * crypt_fpr_or_lkeyid (struct CryptKeyInfo *k)
 Find the fingerprint of a key. More...
 
unsigned int key_check_cap (gpgme_key_t key, enum KeyCap cap)
 Check the capabilities of a key. More...
 
gpgme_ctx_t create_gpgme_context (bool for_smime)
 Create a new GPGME context. More...
 
const char * crypt_keyid (struct CryptKeyInfo *k)
 Find the ID for the key. More...
 
int crypt_id_is_valid (struct CryptKeyInfo *key)
 Is key ID valid. More...
 
struct CryptKeyInfocrypt_copy_key (struct CryptKeyInfo *key)
 Return a copy of KEY. More...
 

Detailed Description

Wrapper for PGP/SMIME calls to GPGME.

Authors
  • g10 Code GmbH
  • Pietro Cerutti

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file crypt_gpgme.h.

Enumeration Type Documentation

◆ KeyInfo

enum KeyInfo

PGP Key info.

Enumerator
KIP_NAME 

PGP Key field: Name.

KIP_AKA 

PGP Key field: aka (Also Known As)

KIP_VALID_FROM 

PGP Key field: Valid From date.

KIP_VALID_TO 

PGP Key field: Valid To date.

KIP_KEY_TYPE 

PGP Key field: Key Type.

KIP_KEY_USAGE 

PGP Key field: Key Usage.

KIP_FINGERPRINT 

PGP Key field: Fingerprint.

KIP_SERIAL_NO 

PGP Key field: Serial number.

KIP_ISSUED_BY 

PGP Key field: Issued By.

KIP_SUBKEY 

PGP Key field: Subkey.

KIP_MAX 

Definition at line 57 of file crypt_gpgme.h.

58 {
59  KIP_NAME = 0,
60  KIP_AKA,
62  KIP_VALID_TO,
63  KIP_KEY_TYPE,
68  KIP_SUBKEY,
69  KIP_MAX,
70 };
PGP Key field: Valid To date.
Definition: crypt_gpgme.h:62
PGP Key field: Serial number.
Definition: crypt_gpgme.h:66
PGP Key field: Key Usage.
Definition: crypt_gpgme.h:64
PGP Key field: Key Type.
Definition: crypt_gpgme.h:63
PGP Key field: Valid From date.
Definition: crypt_gpgme.h:61
PGP Key field: Name.
Definition: crypt_gpgme.h:59
PGP Key field: Fingerprint.
Definition: crypt_gpgme.h:65
PGP Key field: aka (Also Known As)
Definition: crypt_gpgme.h:60
PGP Key field: Issued By.
Definition: crypt_gpgme.h:67
PGP Key field: Subkey.
Definition: crypt_gpgme.h:68

◆ KeyCap

enum KeyCap

PGP/SMIME Key Capabilities.

Enumerator
KEY_CAP_CAN_ENCRYPT 

Key can be used for encryption.

KEY_CAP_CAN_SIGN 

Key can be used for signing.

KEY_CAP_CAN_CERTIFY 

Key can be used to certify.

Definition at line 75 of file crypt_gpgme.h.

76 {
80 };
Key can be used for encryption.
Definition: crypt_gpgme.h:77
Key can be used for signing.
Definition: crypt_gpgme.h:78
Key can be used to certify.
Definition: crypt_gpgme.h:79

Function Documentation

◆ crypt_id_is_strong()

bool crypt_id_is_strong ( struct CryptKeyInfo key)

Is the key strong.

Parameters
keyKey to test
Return values
trueValidity of key is sufficient

Definition at line 484 of file crypt_gpgme.c.

485 {
486  if (!key)
487  return false;
488 
489  bool is_strong = false;
490 
491  if ((key->flags & KEYFLAG_ISX509))
492  return true;
493 
494  switch (key->validity)
495  {
496  case GPGME_VALIDITY_MARGINAL:
497  case GPGME_VALIDITY_NEVER:
498  case GPGME_VALIDITY_UNDEFINED:
499  case GPGME_VALIDITY_UNKNOWN:
500  is_strong = false;
501  break;
502 
503  case GPGME_VALIDITY_FULL:
504  case GPGME_VALIDITY_ULTIMATE:
505  is_strong = true;
506  break;
507  }
508 
509  return is_strong;
510 }
#define KEYFLAG_ISX509
Key is an X.509 key.
Definition: lib.h:126
KeyFlags flags
global and per uid flags (for convenience)
Definition: crypt_gpgme.h:50
gpgme_validity_t validity
uid validity (cached for convenience)
Definition: crypt_gpgme.h:51
+ Here is the caller graph for this function:

◆ digit()

int digit ( const char *  s)

◆ crypt_fpr_or_lkeyid()

const char* crypt_fpr_or_lkeyid ( struct CryptKeyInfo k)

Find the fingerprint of a key.

Parameters
kKey to examine
Return values
ptrFingerprint if available, otherwise the long keyid

Definition at line 423 of file crypt_gpgme.c.

424 {
425  const char *s = "????????????????";
426 
427  if (k->kobj && k->kobj->subkeys)
428  {
429  if (k->kobj->subkeys->fpr)
430  s = k->kobj->subkeys->fpr;
431  else
432  s = k->kobj->subkeys->keyid;
433  }
434 
435  return s;
436 }
gpgme_key_t kobj
Definition: crypt_gpgme.h:47
+ Here is the caller graph for this function:

◆ key_check_cap()

unsigned int key_check_cap ( gpgme_key_t  key,
enum KeyCap  cap 
)

Check the capabilities of a key.

Parameters
keyGPGME key
capFlags, e.g. KEY_CAP_CAN_ENCRYPT
Return values
>0Key has the capabilities

Definition at line 3256 of file crypt_gpgme.c.

3257 {
3258  unsigned int ret = 0;
3259 
3260  switch (cap)
3261  {
3262  case KEY_CAP_CAN_ENCRYPT:
3263  ret = key->can_encrypt;
3264  if (ret == 0)
3265  {
3266  for (gpgme_subkey_t subkey = key->subkeys; subkey; subkey = subkey->next)
3267  {
3268  ret = subkey->can_encrypt;
3269  if (ret != 0)
3270  break;
3271  }
3272  }
3273  break;
3274  case KEY_CAP_CAN_SIGN:
3275  ret = key->can_sign;
3276  if (ret == 0)
3277  {
3278  for (gpgme_subkey_t subkey = key->subkeys; subkey; subkey = subkey->next)
3279  {
3280  ret = subkey->can_sign;
3281  if (ret != 0)
3282  break;
3283  }
3284  }
3285  break;
3286  case KEY_CAP_CAN_CERTIFY:
3287  ret = key->can_certify;
3288  if (ret == 0)
3289  {
3290  for (gpgme_subkey_t subkey = key->subkeys; subkey; subkey = subkey->next)
3291  {
3292  ret = subkey->can_certify;
3293  if (ret != 0)
3294  break;
3295  }
3296  }
3297  break;
3298  }
3299 
3300  return ret;
3301 }
Key can be used for encryption.
Definition: crypt_gpgme.h:77
Key can be used for signing.
Definition: crypt_gpgme.h:78
Key can be used to certify.
Definition: crypt_gpgme.h:79
+ Here is the caller graph for this function:

◆ create_gpgme_context()

gpgme_ctx_t create_gpgme_context ( bool  for_smime)

Create a new GPGME context.

Parameters
for_smimeIf true, protocol of the context is set to CMS
Return values
ptrNew GPGME context

Definition at line 571 of file crypt_gpgme.c.

572 {
573  gpgme_ctx_t ctx = NULL;
574 
575  gpgme_error_t err = gpgme_new(&ctx);
576 
577 #ifdef USE_AUTOCRYPT
578  const char *const c_autocrypt_dir =
579  cs_subset_path(NeoMutt->sub, "autocrypt_dir");
580  if (!err && OptAutocryptGpgme)
581  err = gpgme_ctx_set_engine_info(ctx, GPGME_PROTOCOL_OpenPGP, NULL, c_autocrypt_dir);
582 #endif
583 
584  if (err != 0)
585  {
586  mutt_error(_("error creating GPGME context: %s"), gpgme_strerror(err));
587  mutt_exit(1);
588  }
589 
590  if (for_smime)
591  {
592  err = gpgme_set_protocol(ctx, GPGME_PROTOCOL_CMS);
593  if (err != 0)
594  {
595  mutt_error(_("error enabling CMS protocol: %s"), gpgme_strerror(err));
596  mutt_exit(1);
597  }
598  }
599 
600  return ctx;
601 }
#define mutt_error(...)
Definition: logging.h:88
#define _(a)
Definition: message.h:28
WHERE bool OptAutocryptGpgme
(pseudo) use Autocrypt context inside ncrypt/crypt_gpgme.c
Definition: options.h:33
Container for Accounts, Notifications.
Definition: neomutt.h:36
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:194
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:279
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ crypt_keyid()

const char* crypt_keyid ( struct CryptKeyInfo k)

Find the ID for the key.

Parameters
kKey to use
Return values
ptrID string for the key

Return the keyID for the key K. Note that this string is valid as long as K is valid

Definition at line 347 of file crypt_gpgme.c.

348 {
349  const char *s = "????????";
350 
351  if (k->kobj && k->kobj->subkeys)
352  {
353  s = k->kobj->subkeys->keyid;
354  const bool c_pgp_long_ids = cs_subset_bool(NeoMutt->sub, "pgp_long_ids");
355  if ((!c_pgp_long_ids) && (strlen(s) == 16))
356  {
357  /* Return only the short keyID. */
358  s += 8;
359  }
360  }
361 
362  return s;
363 }
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
Container for Accounts, Notifications.
Definition: neomutt.h:36
gpgme_key_t kobj
Definition: crypt_gpgme.h:47
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ crypt_id_is_valid()

int crypt_id_is_valid ( struct CryptKeyInfo key)

Is key ID valid.

Parameters
keyKey to test
Return values
trueKey is valid

When the key is not marked as unusable

Definition at line 519 of file crypt_gpgme.c.

520 {
521  if (!key)
522  return 0;
523 
524  return !(key->flags & KEYFLAG_CANTUSE);
525 }
#define KEYFLAG_CANTUSE
Definition: lib.h:136
KeyFlags flags
global and per uid flags (for convenience)
Definition: crypt_gpgme.h:50
+ Here is the caller graph for this function:

◆ crypt_copy_key()

struct CryptKeyInfo* crypt_copy_key ( struct CryptKeyInfo key)

Return a copy of KEY.

Parameters
keyKey to copy
Return values
ptrCopy of key

Definition at line 443 of file crypt_gpgme.c.

444 {
445  struct CryptKeyInfo *k = NULL;
446 
447  k = mutt_mem_calloc(1, sizeof(*k));
448  k->kobj = key->kobj;
449  gpgme_key_ref(key->kobj);
450  k->idx = key->idx;
451  k->uid = key->uid;
452  k->flags = key->flags;
453  k->validity = key->validity;
454 
455  return k;
456 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
A stored PGP key.
Definition: crypt_gpgme.h:44
int idx
and the user ID at this index
Definition: crypt_gpgme.h:48
const char * uid
and for convenience point to this user ID
Definition: crypt_gpgme.h:49
gpgme_key_t kobj
Definition: crypt_gpgme.h:47
KeyFlags flags
global and per uid flags (for convenience)
Definition: crypt_gpgme.h:50
gpgme_validity_t validity
uid validity (cached for convenience)
Definition: crypt_gpgme.h:51
+ Here is the call graph for this function:
+ Here is the caller graph for this function: