src/message_api.h
author Volker Birk <vb@pep.foundation>
Tue, 20 Mar 2018 15:49:13 +0100
branchpreview_rating
changeset 2578 73da15bd0c14
parent 2477 45e8ed722fec
permissions -rw-r--r--
first try
     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 
    16 bool import_attached_keys(
    17         PEP_SESSION session, 
    18         const message *msg,
    19         identity_list **private_idents
    20     );
    21 
    22 void attach_own_key(PEP_SESSION session, message *msg);
    23 
    24 PEP_cryptotech determine_encryption_format(message *msg);
    25 
    26 void add_opt_field(message *msg, const char *name, const char *value);
    27 
    28 typedef enum _PEP_encrypt_flags {
    29     // "default" means whatever the default behaviour for the function is.
    30     PEP_encrypt_flag_default = 0x0,
    31     PEP_encrypt_flag_force_encryption = 0x1,
    32 
    33     // This flag is for special use cases and should not be used
    34     // by normal pEp clients!
    35     PEP_encrypt_flag_force_unsigned = 0x2,
    36     PEP_encrypt_flag_force_no_attached_key = 0x4,
    37     
    38     // This is used for outer messages (used to wrap the real message)
    39     // This is only used internally and (eventually) by transport functions
    40     PEP_encrypt_flag_inner_message = 0x8
    41 } PEP_encrypt_flags; 
    42 
    43 typedef unsigned int PEP_encrypt_flags_t;
    44 
    45 
    46 // encrypt_message() - encrypt message in memory
    47 //
    48 //  parameters:
    49 //      session (in)        session handle
    50 //      src (in)            message to encrypt
    51 //      extra (in)          extra keys for encryption
    52 //      dst (out)           pointer to new encrypted message or NULL if no
    53 //                          encryption could take place
    54 //      enc_format (in)     encrypted format
    55 //      flags (in)          flags to set special encryption features
    56 //
    57 //  return value:
    58 //      PEP_STATUS_OK                   on success
    59 //      PEP_KEY_HAS_AMBIG_NAME          at least one of the receipient keys has
    60 //                                      an ambiguous name
    61 //      PEP_UNENCRYPTED                 on demand or no recipients with usable
    62 //                                      key, is left unencrypted, and key is
    63 //                                      attached to it
    64 //
    65 //  caveat:
    66 //      the ownershop of src remains with the caller
    67 //      the ownership of dst goes to the caller
    68 DYNAMIC_API PEP_STATUS encrypt_message(
    69         PEP_SESSION session,
    70         message *src,
    71         stringlist_t *extra,
    72         message **dst,
    73         PEP_enc_format enc_format,
    74         PEP_encrypt_flags_t flags
    75     );
    76 
    77 
    78 // encrypt_message_for_self() - encrypt message in memory for user's identity only,
    79 //                              ignoring recipients and other identities from
    80 //                              the message
    81 //  parameters:
    82 //      session (in)        session handle
    83 //      target_id (in)      self identity this message should be encrypted for
    84 //      src (in)            message to encrypt
    85 //      dst (out)           pointer to new encrypted message or NULL on failure
    86 //      enc_format (in)     encrypted format
    87 //      flags (in)          flags to set special encryption features
    88 //
    89 //  return value:       (FIXME: This may not be correct or complete)
    90 //      PEP_STATUS_OK            on success
    91 //      PEP_KEY_NOT_FOUND        at least one of the receipient keys
    92 //                               could not be found
    93 //      PEP_KEY_HAS_AMBIG_NAME   at least one of the receipient keys has
    94 //                               an ambiguous name
    95 //      PEP_GET_KEY_FAILED       cannot retrieve key
    96 //
    97 //  caveat:
    98 //      the ownership of src remains with the caller
    99 //      the ownership of target_id remains w/ caller            
   100 //      the ownership of dst goes to the caller
   101 //      message is NOT encrypted for identities other than the target_id (and then,
   102 //      only if the target_id refers to self!)
   103 DYNAMIC_API PEP_STATUS encrypt_message_for_self(
   104         PEP_SESSION session,
   105         pEp_identity* target_id,
   106         message *src,
   107         message **dst,
   108         PEP_enc_format enc_format,
   109         PEP_encrypt_flags_t flags
   110     );
   111 
   112 
   113 // MIME_encrypt_message() - encrypt a MIME message, with MIME output
   114 //
   115 //  parameters:
   116 //      session (in)            session handle
   117 //      mimetext (in)           MIME encoded text to encrypt
   118 //      size (in)               size of input mime text
   119 //      extra (in)              extra keys for encryption
   120 //      mime_ciphertext (out)   encrypted, encoded message
   121 //      enc_format (in)         encrypted format
   122 //      flags (in)              flags to set special encryption features
   123 //
   124 //  return value:
   125 //      PEP_STATUS_OK           if everything worked
   126 //      PEP_BUFFER_TOO_SMALL    if encoded message size is too big to handle
   127 //      PEP_CANNOT_CREATE_TEMP_FILE
   128 //                              if there are issues with temp files; in
   129 //                              this case errno will contain the underlying
   130 //                              error
   131 //      PEP_OUT_OF_MEMORY       if not enough memory could be allocated
   132 //
   133 //  caveat:
   134 //      the encrypted, encoded mime text will go to the ownership of the caller; mimetext
   135 //      will remain in the ownership of the caller
   136 DYNAMIC_API PEP_STATUS MIME_encrypt_message(
   137     PEP_SESSION session,
   138     const char *mimetext,
   139     size_t size,
   140     stringlist_t* extra,
   141     char** mime_ciphertext,
   142     PEP_enc_format enc_format,
   143     PEP_encrypt_flags_t flags
   144 );
   145 
   146 
   147 // MIME_encrypt_message_for_self() - encrypt MIME message for user's identity only,
   148 //                              ignoring recipients and other identities from
   149 //                              the message, with MIME output
   150 //  parameters:
   151 //      session (in)            session handle
   152 //      target_id (in)          self identity this message should be encrypted for
   153 //      mimetext (in)           MIME encoded text to encrypt
   154 //      size (in)               size of input mime text
   155 //      mime_ciphertext (out)   encrypted, encoded message
   156 //      enc_format (in)         encrypted format
   157 //      flags (in)              flags to set special encryption features
   158 //
   159 //  return value:
   160 //      PEP_STATUS_OK           if everything worked
   161 //      PEP_BUFFER_TOO_SMALL    if encoded message size is too big to handle
   162 //      PEP_CANNOT_CREATE_TEMP_FILE
   163 //                              if there are issues with temp files; in
   164 //                              this case errno will contain the underlying
   165 //                              error
   166 //      PEP_OUT_OF_MEMORY       if not enough memory could be allocated
   167 //
   168 //  caveat:
   169 //      the encrypted, encoded mime text will go to the ownership of the caller; mimetext
   170 //      will remain in the ownership of the caller
   171 DYNAMIC_API PEP_STATUS MIME_encrypt_message_for_self(
   172     PEP_SESSION session,
   173     pEp_identity* target_id,
   174     const char *mimetext,
   175     size_t size,
   176     char** mime_ciphertext,
   177     PEP_enc_format enc_format,
   178     PEP_encrypt_flags_t flags
   179 );
   180 
   181 typedef enum _PEP_color {
   182     PEP_color_no_color = 0,
   183     PEP_color_yellow,
   184     PEP_color_green,
   185     PEP_color_red = -1,
   186 } PEP_color;
   187 
   188 
   189 // color_from_rating - calculate color from rating
   190 //
   191 //  parameters:
   192 //      rating (in)         rating
   193 //
   194 //  return value:           color representing that rating
   195 DYNAMIC_API PEP_color color_from_rating(PEP_rating rating);
   196 
   197 typedef enum _PEP_decrypt_flags {
   198     PEP_decrypt_flag_own_private_key = 0x1,
   199     PEP_decrypt_flag_consume = 0x2,
   200     PEP_decrypt_flag_ignore = 0x4
   201 } PEP_decrypt_flags; 
   202 
   203 typedef unsigned int PEP_decrypt_flags_t;
   204 
   205 
   206 // decrypt_message() - decrypt message in memory
   207 //
   208 //  parameters:
   209 //      session (in)        session handle
   210 //      src (in)            message to decrypt
   211 //      dst (out)           pointer to new decrypted message or NULL on failure
   212 //      keylist (out)       stringlist with keyids
   213 //      rating (out)        rating for the message
   214 //      flags (out)         flags to signal special decryption features
   215 //
   216 //  return value:
   217 //      error status 
   218 //      or PEP_DECRYPTED if message decrypted but not verified
   219 //      or PEP_STATUS_OK on success
   220 //
   221 // caveat:
   222 //      the ownership of src remains with the caller
   223 //      the ownership of dst goes to the caller
   224 //      the ownership of keylist goes to the caller
   225 //      if src is unencrypted this function returns PEP_UNENCRYPTED and sets
   226 //      dst to NULL
   227 DYNAMIC_API PEP_STATUS decrypt_message(
   228         PEP_SESSION session,
   229         message *src,
   230         message **dst,
   231         stringlist_t **keylist,
   232         PEP_rating *rating,
   233         PEP_decrypt_flags_t *flags
   234 );
   235 
   236 
   237 // MIME_decrypt_message() - decrypt a MIME message, with MIME output
   238 //
   239 //  parameters:
   240 //      session (in)            session handle
   241 //      mimetext (in)           MIME encoded text to decrypt
   242 //      size (in)               size of mime text to decode (in order to decrypt)
   243 //      mime_plaintext (out)    decrypted, encoded message
   244 //      keylist (out)           stringlist with keyids
   245 //      rating (out)            rating for the message
   246 //      flags (out)             flags to signal special decryption features
   247 //
   248 //  return value:
   249 //      decrypt status          if everything worked with MIME encode/decode, 
   250 //                              the status of the decryption is returned 
   251 //                              (PEP_STATUS_OK or decryption error status)
   252 //      PEP_BUFFER_TOO_SMALL    if encoded message size is too big to handle
   253 //      PEP_CANNOT_CREATE_TEMP_FILE
   254 //                              if there are issues with temp files; in
   255 //                              this case errno will contain the underlying
   256 //                              error
   257 //      PEP_OUT_OF_MEMORY       if not enough memory could be allocated
   258 //
   259 //  caveat:
   260 //      the decrypted, encoded mime text will go to the ownership of the caller; mimetext
   261 //      will remain in the ownership of the caller
   262 DYNAMIC_API PEP_STATUS MIME_decrypt_message(
   263     PEP_SESSION session,
   264     const char *mimetext,
   265     size_t size,
   266     char** mime_plaintext,
   267     stringlist_t **keylist,
   268     PEP_rating *rating,
   269     PEP_decrypt_flags_t *flags
   270 );
   271 
   272 
   273 // own_message_private_key_details() - details on own key in own message
   274 //
   275 //  parameters:
   276 //      session (in)        session handle
   277 //      msg (in)            message to decrypt
   278 //      ident (out)         identity containing uid, address and fpr of key
   279 //
   280 //  note:
   281 //      In order to obtain details about key to be possibly imported
   282 //      as a replacement of key currently used as own identity, 
   283 //      application passes message that have been previously flagged by 
   284 //      decrypt_message() as own message containing own key to this function
   285 //
   286 //  return value:
   287 //      error status or PEP_STATUS_OK on success
   288 //
   289 //  caveat:
   290 //      the ownership of msg remains with the caller
   291 //      the ownership of ident goes to the caller
   292 //      msg MUST be encrypted so that this function can check own signature
   293 DYNAMIC_API PEP_STATUS own_message_private_key_details(
   294         PEP_SESSION session,
   295         message *msg,
   296         pEp_identity **ident 
   297 );
   298 
   299 
   300 // outgoing_message_rating() - get rating for an outgoing message
   301 //
   302 //  parameters:
   303 //      session (in)        session handle
   304 //      msg (in)            message to get the rating for
   305 //      rating (out)        rating for the message
   306 //
   307 //  return value:
   308 //      error status or PEP_STATUS_OK on success
   309 //
   310 //  caveat:
   311 //      msg->from must point to a valid pEp_identity
   312 //      msg->dir must be PEP_dir_outgoing
   313 //      the ownership of msg remains with the caller
   314 DYNAMIC_API PEP_STATUS outgoing_message_rating(
   315         PEP_SESSION session,
   316         message *msg,
   317         PEP_rating *rating
   318     );
   319 
   320 
   321 // identity_rating() - get rating for a single identity
   322 //
   323 //  parameters:
   324 //      session (in)        session handle
   325 //      ident (in)          identity to get the rating for
   326 //      rating (out)        rating for the identity
   327 //
   328 //  return value:
   329 //      error status or PEP_STATUS_OK on success
   330 //
   331 //  caveat:
   332 //      the ownership of ident remains with the caller
   333 DYNAMIC_API PEP_STATUS identity_rating(
   334         PEP_SESSION session,
   335         pEp_identity *ident,
   336         PEP_rating *rating
   337     );
   338 
   339 
   340 // get_binary_path() - retrieve path of cryptotech binary if available
   341 //
   342 //  parameters:
   343 //      tech (in)           cryptotech to get the binary for
   344 //      path (out)          path to cryptotech binary or NULL if not available
   345 //                          **path is owned by the library, do not change it!
   346 DYNAMIC_API PEP_STATUS get_binary_path(PEP_cryptotech tech, const char **path);
   347 
   348 
   349 // get_trustwords() - get full trustwords string for a *pair* of identities
   350 //
   351 //    parameters:
   352 //        session (in)        session handle
   353 //        id1 (in)            identity of first party in communication - fpr can't be NULL  
   354 //        id2 (in)            identity of second party in communication - fpr can't be NULL
   355 //        lang (in)           C string with ISO 639-1 language code
   356 //        words (out)         pointer to C string with all trustwords UTF-8 encoded,
   357 //                            separated by a blank each
   358 //                            NULL if language is not supported or trustword
   359 //                            wordlist is damaged or unavailable
   360 //        wsize (out)         length of full trustwords string
   361 //        full (in)           if true, generate ALL trustwords for these identities.
   362 //                            else, generate a fixed-size subset. (TODO: fixed-minimum-entropy
   363 //                            subset in next version)
   364 //
   365 //    return value:
   366 //        PEP_STATUS_OK            trustwords retrieved
   367 //        PEP_OUT_OF_MEMORY        out of memory
   368 //        PEP_TRUSTWORD_NOT_FOUND  at least one trustword not found
   369 //
   370 //    caveat:
   371 //        the word pointer goes to the ownership of the caller
   372 //        the caller is responsible to free() it (on Windoze use pEp_free())
   373 //
   374 DYNAMIC_API PEP_STATUS get_trustwords(
   375     PEP_SESSION session, const pEp_identity* id1, const pEp_identity* id2,
   376     const char* lang, char **words, size_t *wsize, bool full
   377 );
   378 
   379 
   380 // get_message_trustwords() - get full trustwords string for message sender and reciever identities 
   381 //
   382 //    parameters:
   383 //        session (in)        session handle
   384 //        msg (in)            message to get sender identity from
   385 //        keylist (in)        NULL if message to be decrypted,
   386 //                            keylist returned by decrypt_message() otherwise
   387 //        received_by (in)    identity for account receiving message can't be NULL
   388 //        lang (in)           C string with ISO 639-1 language code
   389 //        words (out)         pointer to C string with all trustwords UTF-8 encoded,
   390 //                            separated by a blank each
   391 //                            NULL if language is not supported or trustword
   392 //                            wordlist is damaged or unavailable
   393 //        full (in)           if true, generate ALL trustwords for these identities.
   394 //                            else, generate a fixed-size subset. (TODO: fixed-minimum-entropy
   395 //                            subset in next version)
   396 //
   397 //    return value:
   398 //        PEP_STATUS_OK            trustwords retrieved
   399 //        PEP_OUT_OF_MEMORY        out of memory
   400 //        PEP_TRUSTWORD_NOT_FOUND  at least one trustword not found
   401 //        error status of decrypt_message() if decryption fails.
   402 //
   403 //    caveat:
   404 //        the word pointer goes to the ownership of the caller
   405 //        the caller is responsible to free() it (on Windoze use pEp_free())
   406 //
   407 DYNAMIC_API PEP_STATUS get_message_trustwords(
   408     PEP_SESSION session, 
   409     message *msg,
   410     stringlist_t *keylist,
   411     pEp_identity* received_by,
   412     const char* lang, char **words, bool full
   413 );
   414 
   415 // re_evaluate_message_rating() - re-evaluate already decrypted message rating
   416 //
   417 //  parameters:
   418 //      session (in)            session handle
   419 //      msg (in)                message to get the rating for
   420 //      x_keylist (in)          decrypted message recipients keys fpr
   421 //      x_enc_status (in)       original rating for the decrypted message
   422 //      rating (out)            rating for the message
   423 //
   424 //  return value:
   425 //      PEP_ILLEGAL_VALUE       if decrypted message doesn't contain 
   426 //                              X-EncStatus optional field and x_enc_status is 
   427 //                              pEp_rating_udefined
   428 //                              or if decrypted message doesn't contain 
   429 //                              X-Keylist optional field and x_keylist is NULL
   430 //      PEP_OUT_OF_MEMORY       if not enough memory could be allocated
   431 //
   432 //  caveat:
   433 //      msg->from must point to a valid pEp_identity
   434 //      the ownership of msg remains with the caller
   435 //	    the ownership of x_keylist remains with to the caller
   436 
   437 DYNAMIC_API PEP_STATUS re_evaluate_message_rating(
   438     PEP_SESSION session,
   439     message *msg,
   440     stringlist_t *x_keylist,
   441     PEP_rating x_enc_status,
   442     PEP_rating *rating
   443 );
   444 #ifdef __cplusplus
   445 }
   446 #endif