src/message_api.h
author us@chu.huenfield.org
Tue, 25 Dec 2018 14:46:45 +0100
branchsync
changeset 3209 c15b4ca2b52a
parent 3184 557a689068aa
child 3239 72d4c848f2d9
permissions -rw-r--r--
Replace use of Sequoia's backend with a custom key store.

- Sequoia's key store doesn't meet pep's needs (in particular, the
ability to search on a key's user id) and trying to shoehorn pep's
needs onto Sequoia's key store abstractions is just introducing
overhead with no appreciable gain in functionality.

- This patch changes the Sequoia backend to use a local sqlite
database to store the public keys.
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #pragma once
     5 
     6 #include "pEpEngine.h"
     7 #include "keymanagement.h"
     8 #include "message.h"
     9 #include "cryptotech.h"
    10 
    11 #ifdef __cplusplus
    12 extern "C" {
    13 #endif
    14 
    15 bool import_attached_keys(
    16         PEP_SESSION session, 
    17         const message *msg,
    18         identity_list **private_idents
    19     );
    20 
    21 void attach_own_key(PEP_SESSION session, message *msg);
    22 
    23 PEP_cryptotech determine_encryption_format(message *msg);
    24 
    25 void add_opt_field(message *msg, const char *name, const char *value);
    26 
    27 typedef enum _PEP_encrypt_flags {
    28     // "default" means whatever the default behaviour for the function is.
    29     PEP_encrypt_flag_default = 0x0,
    30     PEP_encrypt_flag_force_encryption = 0x1,
    31 
    32     // This flag is for special use cases and should not be used
    33     // by normal pEp clients!
    34     PEP_encrypt_flag_force_unsigned = 0x2,
    35     PEP_encrypt_flag_force_no_attached_key = 0x4,
    36     
    37     // This is used for outer messages (used to wrap the real message)
    38     // This is only used internally and (eventually) by transport functions
    39     PEP_encrypt_flag_inner_message = 0x8,
    40     
    41     // This is mainly used by pEp clients to send private keys to 
    42     // their own PGP-only device
    43     PEP_encrypt_flag_force_version_1 = 0x10,
    44     
    45     PEP_encrypt_flag_key_reset_only = 0x20
    46     
    47 } PEP_encrypt_flags; 
    48 
    49 typedef unsigned int PEP_encrypt_flags_t;
    50 
    51 typedef enum _message_wrap_type {
    52     PEP_message_default,    // typical inner/outer message 2.0
    53     PEP_message_transport,  // e.g. for onion layers
    54     PEP_message_key_reset   // for wrapped key reset information
    55 } message_wrap_type;
    56 
    57 // encrypt_message() - encrypt message in memory
    58 //
    59 //  parameters:
    60 //      session (in)        session handle
    61 //      src (inout)         message to encrypt - usually in-only, but can be 
    62 //                          in-out for unencrypted messages; in that case, 
    63 //                          we may attach the key and decorate the message
    64 //      extra (in)          extra keys for encryption
    65 //      dst (out)           pointer to new encrypted message or NULL if no
    66 //                          encryption could take place
    67 //      enc_format (in)     encrypted format
    68 //      flags (in)          flags to set special encryption features
    69 //
    70 //  return value:
    71 //      PEP_STATUS_OK                   on success
    72 //      PEP_KEY_HAS_AMBIG_NAME          at least one of the receipient keys has
    73 //                                      an ambiguous name
    74 //      PEP_UNENCRYPTED                 on demand or no recipients with usable
    75 //                                      key, is left unencrypted, and key is
    76 //                                      attached to it
    77 //
    78 //  caveat:
    79 //      the ownership of src remains with the caller
    80 //      the ownership of dst goes to the caller
    81 DYNAMIC_API PEP_STATUS encrypt_message(
    82         PEP_SESSION session,
    83         message *src,
    84         stringlist_t *extra,
    85         message **dst,
    86         PEP_enc_format enc_format,
    87         PEP_encrypt_flags_t flags
    88     );
    89 
    90 
    91 // encrypt_message_and_add_priv_key() - encrypt message in memory, adding an encrypted private
    92 //                                      key (encrypted separately and sent within the inner message)
    93 //
    94 //  parameters:
    95 //      session (in)        session handle
    96 //      src (in)            message to encrypt
    97 //      dst (out)           pointer to new encrypted message or NULL if no
    98 //                          encryption could take place
    99 //      to_fpr              fingerprint of the recipient key to which the private key
   100 //                          should be encrypted
   101 //      enc_format (in)     encrypted format
   102 //      flags (in)          flags to set special encryption features
   103 //
   104 //  return value:
   105 //      PEP_STATUS_OK                   on success
   106 //      PEP_KEY_HAS_AMBIG_NAME          at least one of the receipient keys has
   107 //                                      an ambiguous name
   108 //      PEP_UNENCRYPTED                 on demand or no recipients with usable
   109 //                                      key, is left unencrypted, and key is
   110 //                                      attached to it
   111 //
   112 //  caveat:
   113 //      the ownershop of src remains with the caller
   114 //      the ownership of dst goes to the caller
   115 DYNAMIC_API PEP_STATUS encrypt_message_and_add_priv_key(
   116         PEP_SESSION session,
   117         message *src,
   118         message **dst,
   119         const char* to_fpr,
   120         PEP_enc_format enc_format,
   121         PEP_encrypt_flags_t flags
   122     );
   123 
   124 
   125 // encrypt_message_for_self() - encrypt message in memory for user's identity only,
   126 //                              ignoring recipients and other identities from
   127 //                              the message
   128 //  parameters:
   129 //      session (in)        session handle
   130 //      target_id (in)      self identity this message should be encrypted for
   131 //      src (in)            message to encrypt
   132 //      extra (in)          extra keys for encryption
   133 //      dst (out)           pointer to new encrypted message or NULL on failure
   134 //      enc_format (in)     encrypted format
   135 //      flags (in)          flags to set special encryption features
   136 //
   137 //  return value:       (FIXME: This may not be correct or complete)
   138 //      PEP_STATUS_OK            on success
   139 //      PEP_KEY_NOT_FOUND        at least one of the receipient keys
   140 //                               could not be found
   141 //      PEP_KEY_HAS_AMBIG_NAME   at least one of the receipient keys has
   142 //                               an ambiguous name
   143 //      PEP_GET_KEY_FAILED       cannot retrieve key
   144 //
   145 //  caveat:
   146 //      the ownership of src remains with the caller
   147 //      the ownership of target_id remains w/ caller            
   148 //      the ownership of dst goes to the caller
   149 //      message is NOT encrypted for identities other than the target_id (and then,
   150 //      only if the target_id refers to self!)
   151 DYNAMIC_API PEP_STATUS encrypt_message_for_self(
   152         PEP_SESSION session,
   153         pEp_identity* target_id,
   154         message *src,
   155         stringlist_t* extra,
   156         message **dst,
   157         PEP_enc_format enc_format,
   158         PEP_encrypt_flags_t flags
   159     );
   160 
   161 
   162 // MIME_encrypt_message() - encrypt a MIME message, with MIME output
   163 //
   164 //  parameters:
   165 //      session (in)            session handle
   166 //      mimetext (in)           MIME encoded text to encrypt
   167 //      size (in)               size of input mime text
   168 //      extra (in)              extra keys for encryption
   169 //      mime_ciphertext (out)   encrypted, encoded message
   170 //      enc_format (in)         encrypted format
   171 //      flags (in)              flags to set special encryption features
   172 //
   173 //  return value:
   174 //      PEP_STATUS_OK           if everything worked
   175 //      PEP_BUFFER_TOO_SMALL    if encoded message size is too big to handle
   176 //      PEP_CANNOT_CREATE_TEMP_FILE
   177 //                              if there are issues with temp files; in
   178 //                              this case errno will contain the underlying
   179 //                              error
   180 //      PEP_OUT_OF_MEMORY       if not enough memory could be allocated
   181 //
   182 //  caveat:
   183 //      the encrypted, encoded mime text will go to the ownership of the caller; mimetext
   184 //      will remain in the ownership of the caller
   185 DYNAMIC_API PEP_STATUS MIME_encrypt_message(
   186     PEP_SESSION session,
   187     const char *mimetext,
   188     size_t size,
   189     stringlist_t* extra,
   190     char** mime_ciphertext,
   191     PEP_enc_format enc_format,
   192     PEP_encrypt_flags_t flags
   193 );
   194 
   195 
   196 // MIME_encrypt_message_for_self() - encrypt MIME message for user's identity only,
   197 //                              ignoring recipients and other identities from
   198 //                              the message, with MIME output
   199 //  parameters:
   200 //      session (in)            session handle
   201 //      target_id (in)          self identity this message should be encrypted for
   202 //      mimetext (in)           MIME encoded text to encrypt
   203 //      size (in)               size of input mime text
   204 //      extra (in)              extra keys for encryption
   205 //      mime_ciphertext (out)   encrypted, encoded message
   206 //      enc_format (in)         encrypted format
   207 //      flags (in)              flags to set special encryption features
   208 //
   209 //  return value:
   210 //      PEP_STATUS_OK           if everything worked
   211 //      PEP_BUFFER_TOO_SMALL    if encoded message size is too big to handle
   212 //      PEP_CANNOT_CREATE_TEMP_FILE
   213 //                              if there are issues with temp files; in
   214 //                              this case errno will contain the underlying
   215 //                              error
   216 //      PEP_OUT_OF_MEMORY       if not enough memory could be allocated
   217 //
   218 //  caveat:
   219 //      the encrypted, encoded mime text will go to the ownership of the caller; mimetext
   220 //      will remain in the ownership of the caller
   221 DYNAMIC_API PEP_STATUS MIME_encrypt_message_for_self(
   222     PEP_SESSION session,
   223     pEp_identity* target_id,
   224     const char *mimetext,
   225     size_t size,
   226     stringlist_t* extra,
   227     char** mime_ciphertext,
   228     PEP_enc_format enc_format,
   229     PEP_encrypt_flags_t flags
   230 );
   231 
   232 
   233 typedef enum _PEP_rating {
   234     PEP_rating_undefined = 0,
   235     PEP_rating_cannot_decrypt,
   236     PEP_rating_have_no_key,
   237     PEP_rating_unencrypted,
   238     PEP_rating_unencrypted_for_some,
   239     PEP_rating_unreliable,
   240     PEP_rating_reliable,
   241     PEP_rating_trusted,
   242     PEP_rating_trusted_and_anonymized,
   243     PEP_rating_fully_anonymous,   
   244 
   245     PEP_rating_mistrust = -1,
   246     PEP_rating_b0rken = -2,
   247     PEP_rating_under_attack = -3
   248 } PEP_rating;
   249 
   250 typedef enum _PEP_color {
   251     PEP_color_no_color = 0,
   252     PEP_color_yellow,
   253     PEP_color_green,
   254     PEP_color_red = -1,
   255 } PEP_color;
   256 
   257 
   258 // color_from_rating - calculate color from rating
   259 //
   260 //  parameters:
   261 //      rating (in)         rating
   262 //
   263 //  return value:           color representing that rating
   264 DYNAMIC_API PEP_color color_from_rating(PEP_rating rating);
   265 
   266 typedef enum _PEP_decrypt_flags {
   267     PEP_decrypt_flag_own_private_key = 0x1,
   268     PEP_decrypt_flag_consume = 0x2,
   269     PEP_decrypt_flag_ignore = 0x4,
   270     PEP_decrypt_flag_src_modified = 0x8,
   271     // input flags    
   272     PEP_decrypt_flag_untrusted_server = 0x100
   273 } PEP_decrypt_flags; 
   274 
   275 typedef unsigned int PEP_decrypt_flags_t;
   276 
   277 
   278 // decrypt_message() - decrypt message in memory
   279 //
   280 //  parameters:
   281 //      session (in)        session handle
   282 //      src (inout)         message to decrypt
   283 //      dst (out)           pointer to new decrypted message or NULL on failure
   284 //      keylist (inout)     in: stringlist with additional keyids for reencryption if needed
   285 //                              (will be freed and replaced with output keylist) 
   286 //                          out: stringlist with keyids used for signing and encryption. first
   287 //                               first key is signer, additional keys are the ones it was encrypted
   288 //                               to. Only signer and whichever of the user's keys was used are 
   289 //                               reliable
   290 //      rating (out)        rating for the message
   291 //      flags (inout)       flags to signal special decryption features
   292 //
   293 //  return value:
   294 //      error status 
   295 //      or PEP_DECRYPTED if message decrypted but not verified
   296 //      or PEP_CANNOT_REENCRYPT if message was decrypted (and possibly
   297 //         verified) but a reencryption operation is expected by the caller
   298 //         and failed
   299 //      or PEP_STATUS_OK on success
   300 //
   301 //  flag values:
   302 //      in:
   303 //          PEP_decrypt_flag_untrusted_server
   304 //              used to signal that decrypt function should engage in behaviour
   305 //              specified for when the server storing the source is untrusted
   306 //      out:
   307 //          PEP_decrypt_flag_own_private_key
   308 //              private key was imported for one of our addresses (NOT trusted
   309 //              or set to be used - handshake/trust is required for that)
   310 //          PEP_decrypt_flag_src_modified
   311 //              indicates that the src object has been modified. At the moment,
   312 //              this is always as a direct result of the behaviour driven
   313 //              by the input flags. This flag is the ONLY value that should be
   314 //              relied upon to see if such changes have taken place.
   315 //          PEP_decrypt_flag_consume
   316 //              used by sync 
   317 //          PEP_decrypt_flag_ignore
   318 //              used by sync 
   319 //
   320 //
   321 // caveat:
   322 //      the ownership of src remains with the caller - however, the contents 
   323 //          might be modified (strings freed and allocated anew or set to NULL,
   324 //          etc) intentionally; when this happens, PEP_decrypt_flag_src_modified
   325 //          is set.
   326 //      the ownership of dst goes to the caller
   327 //      the ownership of keylist goes to the caller
   328 //      if src is unencrypted this function returns PEP_UNENCRYPTED and sets
   329 //         dst to NULL
   330 DYNAMIC_API PEP_STATUS decrypt_message(
   331         PEP_SESSION session,
   332         message *src,
   333         message **dst,
   334         stringlist_t **keylist,
   335         PEP_rating *rating,
   336         PEP_decrypt_flags_t *flags
   337 );
   338 
   339 
   340 // MIME_decrypt_message() - decrypt a MIME message, with MIME output
   341 //
   342 //  parameters:
   343 //      session (in)            session handle
   344 //      mimetext (in)           MIME encoded text to decrypt
   345 //      size (in)               size of mime text to decode (in order to decrypt)
   346 //      mime_plaintext (out)    decrypted, encoded message
   347 //      keylist (inout)         in: stringlist with additional keyids for reencryption if needed
   348 //                                  (will be freed and replaced with output keylist)
   349 //                              out: stringlist with keyids
   350 //      rating (out)            rating for the message
   351 //      flags (inout)           flags to signal special decryption features (see below)
   352 //      modified_src (out)      modified source string, if decrypt had reason to change it
   353 //
   354 //  return value:
   355 //      decrypt status          if everything worked with MIME encode/decode, 
   356 //                              the status of the decryption is returned 
   357 //                              (PEP_STATUS_OK or decryption error status)
   358 //      PEP_BUFFER_TOO_SMALL    if encoded message size is too big to handle
   359 //      PEP_CANNOT_CREATE_TEMP_FILE
   360 //                              if there are issues with temp files; in
   361 //                              this case errno will contain the underlying
   362 //                              error
   363 //      PEP_OUT_OF_MEMORY       if not enough memory could be allocated
   364 //
   365 //  flag values:
   366 //      in:
   367 //          PEP_decrypt_flag_untrusted_server
   368 //              used to signal that decrypt function should engage in behaviour
   369 //              specified for when the server storing the source is untrusted.
   370 //      out:
   371 //          PEP_decrypt_flag_own_private_key
   372 //              private key was imported for one of our addresses (NOT trusted
   373 //              or set to be used - handshake/trust is required for that)
   374 //          PEP_decrypt_flag_src_modified
   375 //              indicates that the modified_src field should contain a modified
   376 //              version of the source, at the moment always as a result of the
   377 //              input flags. 
   378 //          PEP_decrypt_flag_consume
   379 //              used by sync 
   380 //          PEP_decrypt_flag_ignore
   381 //              used by sync 
   382 // 
   383 //  caveat:
   384 //      the decrypted, encoded mime text will go to the ownership of the caller; mimetext
   385 //      will remain in the ownership of the caller
   386 DYNAMIC_API PEP_STATUS MIME_decrypt_message(
   387     PEP_SESSION session,
   388     const char *mimetext,
   389     size_t size,
   390     char** mime_plaintext,
   391     stringlist_t **keylist,
   392     PEP_rating *rating,
   393     PEP_decrypt_flags_t *flags,
   394     char** modified_src
   395 );
   396 
   397 
   398 // own_message_private_key_details() - details on own key in own message
   399 //
   400 //  parameters:
   401 //      session (in)        session handle
   402 //      msg (in)            message to decrypt
   403 //      ident (out)         identity containing uid, address and fpr of key
   404 //
   405 //  note:
   406 //      In order to obtain details about key to be possibly imported
   407 //      as a replacement of key currently used as own identity, 
   408 //      application passes message that have been previously flagged by 
   409 //      decrypt_message() as own message containing own key to this function
   410 //
   411 //  return value:
   412 //      error status or PEP_STATUS_OK on success
   413 //
   414 //  caveat:
   415 //      the ownership of msg remains with the caller
   416 //      the ownership of ident goes to the caller
   417 //      msg MUST be encrypted so that this function can check own signature
   418 DYNAMIC_API PEP_STATUS own_message_private_key_details(
   419         PEP_SESSION session,
   420         message *msg,
   421         pEp_identity **ident 
   422 );
   423 
   424 
   425 // outgoing_message_rating() - get rating for an outgoing message
   426 //
   427 //  parameters:
   428 //      session (in)        session handle
   429 //      msg (in)            message to get the rating for
   430 //      rating (out)        rating for the message
   431 //
   432 //  return value:
   433 //      error status or PEP_STATUS_OK on success
   434 //
   435 //  caveat:
   436 //      msg->from must point to a valid pEp_identity
   437 //      msg->dir must be PEP_dir_outgoing
   438 //      the ownership of msg remains with the caller
   439 DYNAMIC_API PEP_STATUS outgoing_message_rating(
   440         PEP_SESSION session,
   441         message *msg,
   442         PEP_rating *rating
   443     );
   444 
   445 
   446 // outgoing_message_rating_preview() - get rating preview
   447 //
   448 //  parameters:
   449 //      session (in)        session handle
   450 //      msg (in)            message to get the rating for
   451 //      rating (out)        rating preview for the message
   452 //
   453 //  return value:
   454 //      error status or PEP_STATUS_OK on success
   455 //
   456 //  caveat:
   457 //      msg->from must point to a valid pEp_identity
   458 //      msg->dir must be PEP_dir_outgoing
   459 //      the ownership of msg remains with the caller
   460 DYNAMIC_API PEP_STATUS outgoing_message_rating_preview(
   461         PEP_SESSION session,
   462         message *msg,
   463         PEP_rating *rating
   464     );
   465 
   466 // identity_rating() - get rating for a single identity
   467 //
   468 //  parameters:
   469 //      session (in)        session handle
   470 //      ident (in)          identity to get the rating for
   471 //      rating (out)        rating for the identity
   472 //
   473 //  return value:
   474 //      error status or PEP_STATUS_OK on success
   475 //
   476 //  caveat:
   477 //      the ownership of ident remains with the caller
   478 DYNAMIC_API PEP_STATUS identity_rating(
   479         PEP_SESSION session,
   480         pEp_identity *ident,
   481         PEP_rating *rating
   482     );
   483 
   484 
   485 // get_binary_path() - retrieve path of cryptotech binary if available
   486 //
   487 //  parameters:
   488 //      tech (in)           cryptotech to get the binary for
   489 //      path (out)          path to cryptotech binary or NULL if not available
   490 //                          **path is owned by the library, do not change it!
   491 DYNAMIC_API PEP_STATUS get_binary_path(PEP_cryptotech tech, const char **path);
   492 
   493 
   494 // get_trustwords() - get full trustwords string for a *pair* of identities
   495 //
   496 //    parameters:
   497 //        session (in)        session handle
   498 //        id1 (in)            identity of first party in communication - fpr can't be NULL  
   499 //        id2 (in)            identity of second party in communication - fpr can't be NULL
   500 //        lang (in)           C string with ISO 639-1 language code
   501 //        words (out)         pointer to C string with all trustwords UTF-8 encoded,
   502 //                            separated by a blank each
   503 //                            NULL if language is not supported or trustword
   504 //                            wordlist is damaged or unavailable
   505 //        wsize (out)         length of full trustwords string
   506 //        full (in)           if true, generate ALL trustwords for these identities.
   507 //                            else, generate a fixed-size subset. (TODO: fixed-minimum-entropy
   508 //                            subset in next version)
   509 //
   510 //    return value:
   511 //        PEP_STATUS_OK            trustwords retrieved
   512 //        PEP_OUT_OF_MEMORY        out of memory
   513 //        PEP_TRUSTWORD_NOT_FOUND  at least one trustword not found
   514 //
   515 //    caveat:
   516 //        the word pointer goes to the ownership of the caller
   517 //        the caller is responsible to free() it (on Windoze use pEp_free())
   518 //
   519 DYNAMIC_API PEP_STATUS get_trustwords(
   520     PEP_SESSION session, const pEp_identity* id1, const pEp_identity* id2,
   521     const char* lang, char **words, size_t *wsize, bool full
   522 );
   523 
   524 
   525 // get_message_trustwords() - get full trustwords string for message sender and reciever identities 
   526 //
   527 //    parameters:
   528 //        session (in)        session handle
   529 //        msg (in)            message to get sender identity from
   530 //        keylist (in)        NULL if message to be decrypted,
   531 //                            keylist returned by decrypt_message() otherwise
   532 //        received_by (in)    identity for account receiving message can't be NULL
   533 //        lang (in)           C string with ISO 639-1 language code
   534 //        words (out)         pointer to C string with all trustwords UTF-8 encoded,
   535 //                            separated by a blank each
   536 //                            NULL if language is not supported or trustword
   537 //                            wordlist is damaged or unavailable
   538 //        full (in)           if true, generate ALL trustwords for these identities.
   539 //                            else, generate a fixed-size subset. (TODO: fixed-minimum-entropy
   540 //                            subset in next version)
   541 //
   542 //    return value:
   543 //        PEP_STATUS_OK            trustwords retrieved
   544 //        PEP_OUT_OF_MEMORY        out of memory
   545 //        PEP_TRUSTWORD_NOT_FOUND  at least one trustword not found
   546 //        error status of decrypt_message() if decryption fails.
   547 //
   548 //    caveat:
   549 //        the word pointer goes to the ownership of the caller
   550 //        the caller is responsible to free() it (on Windoze use pEp_free())
   551 //
   552 DYNAMIC_API PEP_STATUS get_message_trustwords(
   553     PEP_SESSION session, 
   554     message *msg,
   555     stringlist_t *keylist,
   556     pEp_identity* received_by,
   557     const char* lang, char **words, bool full
   558 );
   559 
   560 // re_evaluate_message_rating() - re-evaluate already decrypted message rating
   561 //
   562 //  parameters:
   563 //      session (in)            session handle
   564 //      msg (in)                message to get the rating for
   565 //      x_keylist (in)          decrypted message recipients keys fpr
   566 //      x_enc_status (in)       original rating for the decrypted message
   567 //      rating (out)            rating for the message
   568 //
   569 //  return value:
   570 //      PEP_ILLEGAL_VALUE       if decrypted message doesn't contain 
   571 //                              X-EncStatus optional field and x_enc_status is 
   572 //                              pEp_rating_udefined
   573 //                              or if decrypted message doesn't contain 
   574 //                              X-Keylist optional field and x_keylist is NULL
   575 //      PEP_OUT_OF_MEMORY       if not enough memory could be allocated
   576 //
   577 //  caveat:
   578 //      msg->from must point to a valid pEp_identity
   579 //      the ownership of msg remains with the caller
   580 //	    the ownership of x_keylist remains with to the caller
   581 
   582 DYNAMIC_API PEP_STATUS re_evaluate_message_rating(
   583     PEP_SESSION session,
   584     message *msg,
   585     stringlist_t *x_keylist,
   586     PEP_rating x_enc_status,
   587     PEP_rating *rating
   588 );
   589 #ifdef __cplusplus
   590 }
   591 #endif