src/pEpEngine.h
author Volker Birk <vb@pep.foundation>
Sat, 03 Jun 2017 11:08:14 +0200
changeset 1819 b3e57d741b15
parent 1810 0beaab16a32c
child 1864 9ce429048fd7
permissions -rw-r--r--
extending logging features
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #pragma once
     5 
     6 #ifdef __cplusplus
     7 extern "C" {
     8 #endif
     9 
    10 #include <stddef.h>
    11 #include <stdint.h>
    12 #include <stdbool.h>
    13 
    14 #include "dynamic_api.h"
    15 #include "stringlist.h"
    16 #include "stringpair.h"    
    17 #include "timestamp.h"
    18 
    19 #define PEP_VERSION "1.0" // protocol version
    20 
    21 #define PEP_OWN_USERID "pEp_own_userId"
    22     
    23 // pEp Engine API
    24 
    25 //  caveat:
    26 //      Unicode data has to be normalized to NFC before calling
    27 //      UTF-8 strings are UTF-8 encoded C strings (zero terminated)
    28 
    29 
    30 struct _pEpSession;
    31 typedef struct _pEpSession * PEP_SESSION;
    32 
    33 typedef enum {
    34     PEP_STATUS_OK                                   = 0,
    35 
    36     PEP_INIT_CANNOT_LOAD_GPGME                      = 0x0110,
    37     PEP_INIT_GPGME_INIT_FAILED                      = 0x0111,
    38     PEP_INIT_NO_GPG_HOME                            = 0x0112,
    39     PEP_INIT_NETPGP_INIT_FAILED                     = 0x0113,
    40 
    41     PEP_INIT_SQLITE3_WITHOUT_MUTEX                  = 0x0120,
    42     PEP_INIT_CANNOT_OPEN_DB                         = 0x0121,
    43     PEP_INIT_CANNOT_OPEN_SYSTEM_DB                  = 0x0122,
    44     
    45     PEP_KEY_NOT_FOUND                               = 0x0201,
    46     PEP_KEY_HAS_AMBIG_NAME                          = 0x0202,
    47     PEP_GET_KEY_FAILED                              = 0x0203,
    48     PEP_CANNOT_EXPORT_KEY                           = 0x0204,
    49     PEP_CANNOT_EDIT_KEY                             = 0x0205,
    50     
    51     PEP_CANNOT_FIND_IDENTITY                        = 0x0301,
    52     PEP_CANNOT_SET_PERSON                           = 0x0381,
    53     PEP_CANNOT_SET_PGP_KEYPAIR                      = 0x0382,
    54     PEP_CANNOT_SET_IDENTITY                         = 0x0383,
    55     PEP_CANNOT_SET_TRUST                            = 0x0384,
    56     PEP_KEY_BLACKLISTED                             = 0x0385,
    57     
    58     PEP_UNENCRYPTED                                 = 0x0400,
    59     PEP_VERIFIED                                    = 0x0401,
    60     PEP_DECRYPTED                                   = 0x0402,
    61     PEP_DECRYPTED_AND_VERIFIED                      = 0x0403,
    62     PEP_DECRYPT_WRONG_FORMAT                        = 0x0404,
    63     PEP_DECRYPT_NO_KEY                              = 0x0405,
    64     PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH            = 0x0406,
    65     PEP_VERIFY_NO_KEY                               = 0x0407,
    66     PEP_VERIFIED_AND_TRUSTED                        = 0x0408,
    67     PEP_CANNOT_DECRYPT_UNKNOWN                      = 0x04ff,
    68 
    69     PEP_TRUSTWORD_NOT_FOUND                         = 0x0501,
    70     PEP_TRUSTWORDS_FPR_WRONG_LENGTH                 = 0x0502,
    71 
    72     PEP_CANNOT_CREATE_KEY                           = 0x0601,
    73     PEP_CANNOT_SEND_KEY                             = 0x0602,
    74 
    75     PEP_PHRASE_NOT_FOUND                            = 0x0701,
    76 
    77     PEP_SEND_FUNCTION_NOT_REGISTERED                = 0x0801,
    78     PEP_CONTRAINTS_VIOLATED                         = 0x0802,
    79     PEP_CANNOT_ENCODE                               = 0x0803,
    80 
    81     PEP_SYNC_NO_NOTIFY_CALLBACK                     = 0x0901,
    82     PEP_SYNC_ILLEGAL_MESSAGE                        = 0x0902,
    83     PEP_SYNC_NO_INJECT_CALLBACK                     = 0x0903,
    84 
    85     PEP_SEQUENCE_VIOLATED                           = 0x0970,
    86     PEP_CANNOT_INCREASE_SEQUENCE                    = 0x0971,
    87     PEP_CANNOT_SET_SEQUENCE_VALUE                   = 0x0972,
    88     PEP_OWN_SEQUENCE                                = 0x097f,
    89 
    90     PEP_SYNC_STATEMACHINE_ERROR                     = 0x0980,
    91     PEP_SYNC_NO_TRUST                               = 0x0981,
    92     PEP_STATEMACHINE_INVALID_STATE                  = 0x0982,
    93     PEP_STATEMACHINE_INVALID_EVENT                  = 0x0983,
    94     PEP_STATEMACHINE_INVALID_CONDITION              = 0x0984,
    95     PEP_STATEMACHINE_INVALID_ACTION                 = 0x0985,
    96     PEP_STATEMACHINE_INHIBITED_EVENT                = 0x0986,
    97 
    98     PEP_COMMIT_FAILED                               = 0xff01,
    99     PEP_MESSAGE_CONSUME                             = 0xff02,
   100     PEP_MESSAGE_IGNORE                              = 0xff03,
   101 
   102     PEP_RECORD_NOT_FOUND                            = -6,
   103     PEP_CANNOT_CREATE_TEMP_FILE                     = -5,
   104     PEP_ILLEGAL_VALUE                               = -4,
   105     PEP_BUFFER_TOO_SMALL                            = -3,
   106     PEP_OUT_OF_MEMORY                               = -2,
   107     PEP_UNKNOWN_ERROR                               = -1
   108 } PEP_STATUS;
   109 
   110 
   111 // INIT_STATUS init() - initialize pEpEngine for a thread
   112 //
   113 //  parameters:
   114 //        session (out)   init() allocates session memory and returns a pointer
   115 //                        as a handle
   116 //
   117 //  return value:
   118 //        PEP_STATUS_OK = 0                 if init() succeeds
   119 //        PEP_INIT_SQLITE3_WITHOUT_MUTEX    if SQLite3 was compiled with
   120 //                                            SQLITE_THREADSAFE 0
   121 //        PEP_INIT_CANNOT_LOAD_GPGME        if libgpgme.dll cannot be found
   122 //        PEP_INIT_GPGME_INIT_FAILED        if GPGME init fails
   123 //        PEP_INIT_CANNOT_OPEN_DB           if user's management db cannot be
   124 //                                            opened
   125 //        PEP_INIT_CANNOT_OPEN_SYSTEM_DB    if system's management db cannot be
   126 //                                            opened
   127 //
   128 //  caveat:
   129 //      the pointer is valid only if the return value is PEP_STATUS_OK
   130 //      in other case a NULL pointer will be returned; a valid handle must
   131 //      be released using release() when it's no longer needed
   132 //
   133 //      the caller has to guarantee that the first call to this function
   134 //      will succeed before further calls can be done
   135 
   136 DYNAMIC_API PEP_STATUS init(PEP_SESSION *session);
   137 
   138 
   139 // void release() - release thread session handle
   140 //
   141 //  parameters:
   142 //        session (in)    session handle to release
   143 //
   144 //    caveat:
   145 //        the last release() can be called only when all other release() calls
   146 //        are done
   147 
   148 DYNAMIC_API void release(PEP_SESSION session);
   149 
   150 
   151 // const stringlist_t* get_errorstack(PEP_SESSION) - get the error stack for that session, if any
   152 //
   153 //  parameters:
   154 //        session (in)    session handle
   155 //
   156 //    caveat:
   157 //        To get a useful error stack you have to compile with -DDEBUG_ERRORSTACK
   158 //        The error stack belongs to the session. Do no not change it!
   159 DYNAMIC_API const stringlist_t* get_errorstack(PEP_SESSION session);
   160 
   161 
   162 // void clear_errorstack(PEP_SESSION) - clear the error stack for that session, if any
   163 //
   164 //  parameters:
   165 //        session (in)    session handle
   166 //
   167 DYNAMIC_API void clear_errorstack(PEP_SESSION session);
   168 
   169 
   170 // config_passive_mode() - enable passive mode
   171 //
   172 //  parameters:
   173 //      session (in)    session handle
   174 //      enable (in)     flag if enabled or disabled
   175 
   176 DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable);
   177 
   178 
   179 // config_unencrypted_subject() - disable subject encryption
   180 //
   181 //  parameters:
   182 //      session (in)    session handle
   183 //      enable (in)     flag if enabled or disabled
   184 
   185 DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable);
   186 
   187 
   188 // config_use_only_own_private_keys() - enable passive mode
   189 //
   190 //  parameters:
   191 //      session (in)    session handle
   192 //      enable (in)     flag if enabled or disabled
   193 
   194 DYNAMIC_API void config_use_only_own_private_keys(PEP_SESSION session, bool enable);
   195 
   196 
   197 // config_keep_sync_msg() - do not remove sync messages (for debugging purposes)
   198 //
   199 //      session (in)    session handle
   200 //      enable (in)     flag if enabled or disabled
   201 
   202 DYNAMIC_API void config_keep_sync_msg(PEP_SESSION session, bool enable);
   203 
   204 
   205 // config_service_log() - log more for service purposes
   206 //
   207 //      session (in)    session handle
   208 //      enable (in)     flag if enabled or disabled
   209 
   210 DYNAMIC_API void config_service_log(PEP_SESSION session, bool enable);
   211 
   212 
   213 // decrypt_and_verify() - decrypt and/or verify a message
   214 //
   215 //    parameters:
   216 //        session (in)    session handle
   217 //        ctext (in)      cipher text to decrypt and/or verify
   218 //        csize (in)      size of cipher text
   219 //        dsigtext (in)   if extant, *detached* signature text for this
   220 //                        message (or NULL if not)
   221 //        dsize (in)      size of *detached* signature text for this
   222 //                        message (0, if no detached sig exists)
   223 //        ptext (out)     pointer to internal buffer with plain text
   224 //        psize (out)     size of plain text
   225 //        keylist (out)   list of key ids which where used to encrypt
   226 //
   227 //    return value:
   228 //        PEP_UNENCRYPTED               message was unencrypted and not signed
   229 //        PEP_VERIFIED                  message was unencrypted, signature matches
   230 //        PEP_DECRYPTED                 message is decrypted now, no signature
   231 //        PEP_DECRYPTED_AND_VERIFIED    message is decrypted now and verified
   232 //        PEP_DECRYPT_WRONG_FORMAT      message has wrong format to handle
   233 //        PEP_DECRYPT_NO_KEY            key not available to decrypt and/or verify
   234 //        PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH    wrong signature
   235 //
   236 //    caveat:
   237 //        the ownerships of ptext as well as keylist are going to the caller
   238 //        the caller must use free() (or an Windoze pEp_free()) and
   239 //        free_stringlist() to free them
   240 //
   241 //      if this function failes an error message may be the first element of
   242 //      keylist and the other elements may be the keys used for encryption
   243 
   244 DYNAMIC_API PEP_STATUS decrypt_and_verify(
   245         PEP_SESSION session, const char *ctext, size_t csize,
   246         const char *dsigtext, size_t dsigsize,
   247         char **ptext, size_t *psize, stringlist_t **keylist
   248     );
   249 
   250 
   251 // verify_text() - verfy plain text with a digital signature
   252 //
   253 //  parameters:
   254 //      session (in)    session handle
   255 //      text (in)       text to verify
   256 //      size (in)       size of text
   257 //      signature (in)  signature text
   258 //      sig_size (in)   size of signature
   259 //      keylist (out)   list of key ids which where used to encrypt or NULL on
   260 //                        error
   261 //
   262 //  return value:
   263 //        PEP_VERIFIED                message was unencrypted, signature matches
   264 //        PEP_DECRYPT_NO_KEY          key not available to decrypt and/or verify
   265 //        PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH    wrong signature
   266 
   267 DYNAMIC_API PEP_STATUS verify_text(
   268         PEP_SESSION session, const char *text, size_t size,
   269         const char *signature, size_t sig_size, stringlist_t **keylist
   270     );
   271 
   272 
   273 // encrypt_and_sign() - encrypt and sign a message
   274 //
   275 //    parameters:
   276 //        session (in)    session handle
   277 //        keylist (in)    list of key ids to encrypt with as C strings
   278 //        ptext (in)      plain text to decrypt and/or verify
   279 //        psize (in)      size of plain text
   280 //        ctext (out)     pointer to internal buffer with cipher text
   281 //        csize (out)     size of cipher text
   282 //
   283 //    return value:
   284 //        PEP_STATUS_OK = 0            encryption and signing succeeded
   285 //        PEP_KEY_NOT_FOUND            at least one of the receipient keys
   286 //                                     could not be found
   287 //        PEP_KEY_HAS_AMBIG_NAME       at least one of the receipient keys has
   288 //                                     an ambiguous name
   289 //        PEP_GET_KEY_FAILED           cannot retrieve key
   290 //
   291 //    caveat:
   292 //      the ownership of ctext is going to the caller
   293 //      the caller is responsible to free() it (on Windoze use pEp_free())
   294 //      the first key in keylist is being used to sign the message
   295 //      this implies there has to be a private key for that keypair
   296 
   297 DYNAMIC_API PEP_STATUS encrypt_and_sign(
   298         PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
   299         size_t psize, char **ctext, size_t *csize
   300     );
   301 
   302 
   303 // log_event() - log a user defined event defined by UTF-8 encoded strings into
   304 // management log
   305 //
   306 //    parameters:
   307 //        session (in)        session handle
   308 //        title (in)          C string with event name
   309 //        entity (in)         C string with name of entity which is logging
   310 //        description (in)    C string with long description for event or NULL if
   311 //                            omitted
   312 //        comment (in)        C string with user defined comment or NULL if
   313 //                            omitted
   314 //
   315 //    return value:
   316 //        PEP_STATUS_OK       log entry created
   317 
   318 DYNAMIC_API PEP_STATUS log_event(
   319         PEP_SESSION session,
   320         const char *title,
   321         const char *entity,
   322         const char *description,
   323         const char *comment
   324     );
   325 
   326 
   327 DYNAMIC_API PEP_STATUS log_service(PEP_SESSION session, const char *title,
   328         const char *entity, const char *description, const char *comment);
   329 
   330 #define _STR_(x) #x
   331 #define _D_STR_(x) _STR_(x)
   332 #define S_LINE _D_STR_(__LINE__)
   333 
   334 #define SERVICE_LOG(session, title, entity, desc) \
   335     log_service((session), (title), (entity), (desc), "service " __FILE__ ":" S_LINE)
   336 
   337 
   338 // trustword() - get the corresponding trustword for a 16 bit value
   339 //
   340 //    parameters:
   341 //        session (in)            session handle
   342 //        value (in)              value to find a trustword for
   343 //        lang (in)               C string with ISO 639-1 language code
   344 //        word (out)              pointer to C string with trustword UTF-8 encoded
   345 //                                NULL if language is not supported or trustword
   346 //                                wordlist is damaged or unavailable
   347 //        wsize (out)             length of trustword
   348 //
   349 //    return value:
   350 //        PEP_STATUS_OK            trustword retrieved
   351 //        PEP_TRUSTWORD_NOT_FOUND  trustword not found
   352 //
   353 //    caveat:
   354 //        the word pointer goes to the ownership of the caller
   355 //      the caller is responsible to free() it (on Windoze use pEp_free())
   356 
   357 DYNAMIC_API PEP_STATUS trustword(
   358             PEP_SESSION session, uint16_t value, const char *lang,
   359             char **word, size_t *wsize
   360         );
   361 
   362 
   363 // trustwords() - get trustwords for a string of hex values of a fingerprint
   364 //
   365 //    parameters:
   366 //        session (in)        session handle
   367 //        fingerprint (in)    C string with hex values to find trustwords for
   368 //        lang (in)           C string with ISO 639-1 language code
   369 //        words (out)         pointer to C string with trustwords UTF-8 encoded,
   370 //                            separated by a blank each
   371 //                            NULL if language is not supported or trustword
   372 //                            wordlist is damaged or unavailable
   373 //        wsize (out)         length of trustwords string
   374 //        max_words (in)      only generate a string with max_words;
   375 //                            if max_words == 0 there is no such limit
   376 //
   377 //    return value:
   378 //        PEP_STATUS_OK            trustwords retrieved
   379 //        PEP_OUT_OF_MEMORY        out of memory
   380 //        PEP_TRUSTWORD_NOT_FOUND  at least one trustword not found
   381 //
   382 //    caveat:
   383 //        the word pointer goes to the ownership of the caller
   384 //      the caller is responsible to free() it (on Windoze use pEp_free())
   385 //
   386 //  DON'T USE THIS FUNCTION FROM HIGH LEVEL LANGUAGES!
   387 //
   388 //  Better implement a simple one in the adapter yourself using trustword(), and
   389 //  return a list of trustwords.
   390 //  This function is provided for being used by C and C++ programs only.
   391 
   392 DYNAMIC_API PEP_STATUS trustwords(
   393         PEP_SESSION session, const char *fingerprint, const char *lang,
   394         char **words, size_t *wsize, int max_words
   395     );
   396 
   397 
   398 // TODO: increase versions in pEp.asn1 if rating changes
   399 
   400 typedef enum _PEP_comm_type {
   401     PEP_ct_unknown = 0,
   402 
   403     // range 0x01 to 0x09: no encryption, 0x0a to 0x0e: nothing reasonable
   404 
   405     PEP_ct_no_encryption = 0x01,                // generic
   406     PEP_ct_no_encrypted_channel = 0x02,
   407     PEP_ct_key_not_found = 0x03,
   408     PEP_ct_key_expired = 0x04,
   409     PEP_ct_key_revoked = 0x05,
   410     PEP_ct_key_b0rken = 0x06,
   411     PEP_ct_my_key_not_included = 0x09,
   412 
   413     PEP_ct_security_by_obscurity = 0x0a,
   414     PEP_ct_b0rken_crypto = 0x0b,
   415     PEP_ct_key_too_short = 0x0c,
   416 
   417     PEP_ct_compromized = 0x0e,                  // known compromized connection
   418     PEP_ct_mistrusted = 0x0f,                   // known mistrusted key
   419 
   420     // range 0x10 to 0x3f: unconfirmed encryption
   421 
   422     PEP_ct_unconfirmed_encryption = 0x10,       // generic
   423     PEP_ct_OpenPGP_weak_unconfirmed = 0x11,     // RSA 1024 is weak
   424 
   425     PEP_ct_to_be_checked = 0x20,                // generic
   426     PEP_ct_SMIME_unconfirmed = 0x21,
   427     PEP_ct_CMS_unconfirmed = 0x22,
   428 
   429     PEP_ct_strong_but_unconfirmed = 0x30,       // generic
   430     PEP_ct_OpenPGP_unconfirmed = 0x38,          // key at least 2048 bit RSA or EC
   431     PEP_ct_OTR_unconfirmed = 0x3a,
   432 
   433     // range 0x40 to 0x7f: unconfirmed encryption and anonymization
   434 
   435     PEP_ct_unconfirmed_enc_anon = 0x40,         // generic
   436     PEP_ct_pEp_unconfirmed = 0x7f,
   437 
   438     PEP_ct_confirmed = 0x80,                    // this bit decides if trust is confirmed
   439 
   440     // range 0x81 to 0x8f: reserved
   441     // range 0x90 to 0xbf: confirmed encryption
   442 
   443     PEP_ct_confirmed_encryption = 0x90,         // generic
   444     PEP_ct_OpenPGP_weak = 0x91,                 // RSA 1024 is weak (unused)
   445 
   446     PEP_ct_to_be_checked_confirmed = 0xa0,      //generic
   447     PEP_ct_SMIME = 0xa1,
   448     PEP_ct_CMS = 0xa2,
   449 
   450     PEP_ct_strong_encryption = 0xb0,            // generic
   451     PEP_ct_OpenPGP = 0xb8,                      // key at least 2048 bit RSA or EC
   452     PEP_ct_OTR = 0xba,
   453 
   454     // range 0xc0 to 0xff: confirmed encryption and anonymization
   455 
   456     PEP_ct_confirmed_enc_anon = 0xc0,           // generic
   457     PEP_ct_pEp = 0xff
   458 } PEP_comm_type;
   459 
   460 typedef enum _identity_flags {
   461     // the first octet flags are app defined settings
   462     PEP_idf_not_for_sync = 0x0001,   // don't use this identity for sync
   463     PEP_idf_list = 0x0002,           // identity of list of persons
   464 
   465     // the second octet flags are calculated
   466     PEP_idf_devicegroup = 0x0100     // identity of a device group member
   467 } identity_flags;
   468 
   469 typedef unsigned int identity_flags_t;
   470 
   471 // typedef enum _keypair_flags {
   472 // } keypair_flags;
   473 
   474 typedef unsigned int keypair_flags_t;
   475 
   476 typedef struct _pEp_identity {
   477     char *address;              // C string with address UTF-8 encoded
   478     char *fpr;                  // C string with fingerprint UTF-8 encoded
   479     char *user_id;              // C string with user ID UTF-8 encoded
   480     char *username;             // C string with user name UTF-8 encoded
   481     PEP_comm_type comm_type;    // type of communication with this ID
   482     char lang[3];               // language of conversation
   483                                 // ISO 639-1 ALPHA-2, last byte is 0
   484     bool me;                    // if this is the local user herself/himself
   485     identity_flags_t flags;     // identity_flag1 | identity_flag2 | ...
   486 } pEp_identity;
   487 
   488 typedef struct _identity_list {
   489     pEp_identity *ident;
   490     struct _identity_list *next;
   491 } identity_list;
   492 
   493 
   494 // new_identity() - allocate memory and set the string and size fields
   495 //
   496 //  parameters:
   497 //      address (in)        UTF-8 string or NULL 
   498 //      fpr (in)            UTF-8 string or NULL 
   499 //      user_id (in)        UTF-8 string or NULL 
   500 //      username (in)       UTF-8 string or NULL 
   501 //
   502 //  return value:
   503 //      pEp_identity struct or NULL if out of memory
   504 //
   505 //  caveat:
   506 //      the strings are copied; the original strings are still being owned by
   507 //      the caller
   508 
   509 DYNAMIC_API pEp_identity *new_identity(
   510         const char *address, const char *fpr, const char *user_id,
   511         const char *username
   512     );
   513 
   514 
   515 // identity_dup() - allocate memory and duplicate
   516 //
   517 //  parameters:
   518 //      src (in)            identity to duplicate
   519 //
   520 //  return value:
   521 //      pEp_identity struct or NULL if out of memory
   522 //
   523 //  caveat:
   524 //      the strings are copied; the original strings are still being owned by
   525 //      the caller
   526 
   527 DYNAMIC_API pEp_identity *identity_dup(const pEp_identity *src);
   528 
   529 
   530 // free_identity() - free all memory being occupied by a pEp_identity struct
   531 //
   532 //  parameters:
   533 //      identity (in)       struct to release
   534 //
   535 //  caveat:
   536 //      not only the struct but also all string memory referenced by the
   537 //      struct is being freed; all pointers inside are invalid afterwards
   538 
   539 DYNAMIC_API void free_identity(pEp_identity *identity);
   540 
   541 
   542 // get_identity() - get identity information
   543 //
   544 //    parameters:
   545 //        session (in)        session handle
   546 //        address (in)        C string with communication address, UTF-8 encoded
   547 //        user_id (in)        unique C string to identify person that identity
   548 //                            is refering to
   549 //        identity (out)      pointer to pEp_identity structure with results or
   550 //                            NULL if failure
   551 //
   552 //    caveat:
   553 //        address and user_id are being copied; the original strings remains in
   554 //        the ownership of the caller
   555 //        the resulting pEp_identity structure goes to the ownership of the
   556 //        caller and has to be freed with free_identity() when not in use any
   557 //        more
   558 
   559 DYNAMIC_API PEP_STATUS get_identity(
   560         PEP_SESSION session,
   561         const char *address,
   562         const char *user_id,
   563         pEp_identity **identity
   564     );
   565 
   566 PEP_STATUS replace_identities_fpr(PEP_SESSION session, 
   567                                  const char* old_fpr, 
   568                                  const char* new_fpr); 
   569 
   570 
   571 // set_identity() - set identity information
   572 //
   573 //    parameters:
   574 //        session (in)        session handle
   575 //        identity (in)       pointer to pEp_identity structure
   576 //
   577 //    return value:
   578 //        PEP_STATUS_OK = 0             encryption and signing succeeded
   579 //        PEP_CANNOT_SET_PERSON         writing to table person failed
   580 //        PEP_CANNOT_SET_PGP_KEYPAIR    writing to table pgp_keypair failed
   581 //        PEP_CANNOT_SET_IDENTITY       writing to table identity failed
   582 //        PEP_COMMIT_FAILED             SQL commit failed
   583 //        PEP_KEY_BLACKLISTED           Key blacklisted, cannot set identity
   584 //
   585 //    caveat:
   586 //        address, fpr, user_id and username must be given
   587 
   588 DYNAMIC_API PEP_STATUS set_identity(
   589         PEP_SESSION session, const pEp_identity *identity
   590     );
   591 
   592 // set_device_group() - update own person's device group
   593 //
   594 //    parameters:
   595 //        session (in)        session handle
   596 //        group_name (in)     new group name
   597 //
   598 //    return value:
   599 //        PEP_STATUS_OK = 0             device group was updated
   600 //        PEP_CANNOT_SET_PERSON         update failed
   601 
   602 DYNAMIC_API PEP_STATUS set_device_group(
   603         PEP_SESSION session,
   604         const char *group_name
   605     );
   606 
   607 // get_device_group() - get own person's device group
   608 //
   609 //    parameters:
   610 //        session (in)        session handle
   611 //        group_name (in)     new group name
   612 //
   613 //    return value:
   614 //        PEP_STATUS_OK = 0             couldn't get device group
   615 //        PEP_RECORD_NOT_FOUND          update failed
   616 //
   617 //    caveat:
   618 //        the ownerships of group_name is going to the caller
   619 
   620 DYNAMIC_API PEP_STATUS get_device_group(
   621         PEP_SESSION session, 
   622         char **group_name
   623     );
   624 
   625 // set_identity_flags() - update identity flags on existing identity
   626 //
   627 //    parameters:
   628 //        session (in)        session handle
   629 //        identity (in,out)   pointer to pEp_identity structure
   630 //        flags (in)          new value for flags
   631 //
   632 //    return value:
   633 //        PEP_STATUS_OK = 0             encryption and signing succeeded
   634 //        PEP_CANNOT_SET_IDENTITY       update of identity failed
   635 //
   636 //    caveat:
   637 //        address and user_id must be given in identity
   638 
   639 DYNAMIC_API PEP_STATUS set_identity_flags(
   640         PEP_SESSION session,
   641         pEp_identity *identity,
   642         identity_flags_t flags
   643     );
   644 
   645 // unset_identity_flags() - update identity flags on existing identity
   646 //
   647 //    parameters:
   648 //        session (in)        session handle
   649 //        identity (in,out)   pointer to pEp_identity structure
   650 //        flags (in)          new value for flags
   651 //
   652 //    return value:
   653 //        PEP_STATUS_OK = 0             encryption and signing succeeded
   654 //        PEP_CANNOT_SET_IDENTITY       update of identity failed
   655 //
   656 //    caveat:
   657 //        address and user_id must be given in identity
   658 
   659 DYNAMIC_API PEP_STATUS unset_identity_flags(
   660         PEP_SESSION session,
   661         pEp_identity *identity,
   662         identity_flags_t flags
   663     );
   664 
   665 // mark_as_compromized() - mark key in trust db as compromized
   666 //
   667 //    parameters:
   668 //        session (in)        session handle
   669 //        fpr (in)            fingerprint of key to mark
   670 
   671 DYNAMIC_API PEP_STATUS mark_as_compromized(
   672         PEP_SESSION session,
   673         const char *fpr
   674     );
   675 
   676 
   677 // generate_keypair() - generate a new key pair and add it to the key ring
   678 //
   679 //  parameters:
   680 //      session (in)            session handle
   681 //        identity (inout)      pointer to pEp_identity structure
   682 //
   683 //    return value:
   684 //        PEP_STATUS_OK = 0       encryption and signing succeeded
   685 //        PEP_ILLEGAL_VALUE       illegal values for identity fields given
   686 //        PEP_CANNOT_CREATE_KEY   key engine is on strike
   687 //
   688 //  caveat:
   689 //      address and username fields must be set to UTF-8 strings
   690 //      the fpr field must be set to NULL
   691 //
   692 //      this function allocates a string and sets set fpr field of identity
   693 //      the caller is responsible to call free() for that string or use
   694 //      free_identity() on the struct
   695 
   696 DYNAMIC_API PEP_STATUS generate_keypair(
   697         PEP_SESSION session, pEp_identity *identity
   698     );
   699 
   700 
   701 // delete_keypair() - delete a public key or a key pair from the key ring
   702 //
   703 //  parameters:
   704 //      session (in)            session handle
   705 //      fpr (in)                C string with key id or fingerprint of the
   706 //                              public key
   707 //
   708 //  return value:
   709 //      PEP_STATUS_OK = 0       key was successfully deleted
   710 //      PEP_KEY_NOT_FOUND       key not found
   711 //      PEP_ILLEGAL_VALUE       not a valid key id or fingerprint
   712 //      PEP_KEY_HAS_AMBIG_NAME  fpr does not uniquely identify a key
   713 //      PEP_OUT_OF_MEMORY       out of memory
   714 
   715 DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr);
   716 
   717 
   718 // import_key() - import key from data
   719 //
   720 //  parameters:
   721 //      session (in)            session handle
   722 //      key_data (in)           key data, i.e. ASCII armored OpenPGP key
   723 //      size (in)               amount of data to handle
   724 //      private_keys (out)      list of private keys that have been imported
   725 //
   726 //  return value:
   727 //      PEP_STATUS_OK = 0       key was successfully imported
   728 //      PEP_OUT_OF_MEMORY       out of memory
   729 //      PEP_ILLEGAL_VALUE       there is no key data to import
   730 //
   731 //  caveat:
   732 //      private_keys goes to the ownership of the caller
   733 //      private_keys can be left NULL, it is then ignored
   734 
   735 DYNAMIC_API PEP_STATUS import_key(
   736         PEP_SESSION session,
   737         const char *key_data,
   738         size_t size,
   739         identity_list **private_keys
   740     );
   741 
   742 
   743 // export_key() - export ascii armored key
   744 //
   745 //  parameters:
   746 //      session (in)            session handle
   747 //      fpr (in)                key id or fingerprint of key
   748 //      key_data (out)          ASCII armored OpenPGP key
   749 //      size (out)              amount of data to handle
   750 //
   751 //  return value:
   752 //      PEP_STATUS_OK = 0       key was successfully exported
   753 //      PEP_OUT_OF_MEMORY       out of memory
   754 //      PEP_KEY_NOT_FOUND       key not found
   755 //
   756 //  caveat:
   757 //      the key_data goes to the ownership of the caller
   758 //      the caller is responsible to free() it (on Windoze use pEp_free())
   759 
   760 DYNAMIC_API PEP_STATUS export_key(
   761         PEP_SESSION session, const char *fpr, char **key_data, size_t *size
   762     );
   763 
   764 
   765 // export_secret_key() - export secret key ascii armored
   766 //
   767 //  parameters:
   768 //      session (in)            session handle
   769 //      fpr (in)                fingerprint of key, at least 16 hex digits
   770 //      key_data (out)          ASCII armored OpenPGP secret key
   771 //      size (out)              amount of data to handle
   772 //
   773 //  return value:
   774 //      PEP_STATUS_OK = 0       key was successfully exported
   775 //      PEP_OUT_OF_MEMORY       out of memory
   776 //      PEP_KEY_NOT_FOUND       key not found
   777 //      PEP_CANNOT_EXPORT_KEY   cannot export secret key (i.e. it's on an HKS)
   778 //
   779 //  caveat:
   780 //      the key_data goes to the ownership of the caller
   781 //      the caller is responsible to free() it (on Windoze use pEp_free())
   782 //      beware of leaking secret key data - overwrite it in memory after use
   783 
   784 DYNAMIC_API PEP_STATUS export_secrect_key(
   785         PEP_SESSION session, const char *fpr, char **key_data, size_t *size
   786     );
   787 
   788 
   789 // recv_key() - update key(s) from keyserver
   790 //
   791 //  parameters:
   792 //      session (in)            session handle
   793 //      pattern (in)            key id, user id or address to search for as
   794 //                              UTF-8 string
   795 
   796 DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern);
   797 
   798 
   799 // find_keys() - find keys in keyring
   800 //
   801 //  parameters:
   802 //      session (in)            session handle
   803 //      pattern (in)            key id, user id or address to search for as
   804 //                              UTF-8 string
   805 //      keylist (out)           list of fingerprints found or NULL on error
   806 //
   807 //  caveat:
   808 //        the ownerships of keylist isgoing to the caller
   809 //        the caller must use free_stringlist() to free it
   810 
   811 
   812 DYNAMIC_API PEP_STATUS find_keys(
   813         PEP_SESSION session, const char *pattern, stringlist_t **keylist
   814     );
   815 
   816 // send_key() - send key(s) to keyserver
   817 //
   818 //  parameters:
   819 //      session (in)            session handle
   820 //      pattern (in)            key id, user id or address to search for as
   821 //                              UTF-8 string
   822 
   823 DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern);
   824 
   825 
   826 // pEp_free() - free memory allocated by pEp engine
   827 //
   828 //  parameters:
   829 //      p (in)                  pointer to free
   830 //
   831 //  The reason for this function is that heap management can be a pretty
   832 //  complex task with Windoze. This free() version calls the free()
   833 //  implementation of the C runtime library which was used to build pEp engine,
   834 //  so you're using the correct heap. For more information, see:
   835 //  <http://msdn.microsoft.com/en-us/library/windows/desktop/aa366711(v=vs.85).aspx>
   836 
   837 DYNAMIC_API void pEp_free(void *p);
   838 
   839 
   840 // get_trust() - get the trust level a key has for a person
   841 //
   842 //  parameters:
   843 //      session (in)            session handle
   844 //      identity (inout)        user_id and fpr to check as UTF-8 strings (in)
   845 //                              user_id and comm_type as result (out)
   846 //
   847 //  this function modifies the given identity struct; the struct remains in
   848 //  the ownership of the caller
   849 //  if the trust level cannot be determined identity->comm_type is set
   850 //  to PEP_ct_unknown
   851 
   852 DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity);
   853 
   854 PEP_STATUS set_trust(PEP_SESSION session, 
   855                             const char* user_id,
   856                             const char* fpr, 
   857                             PEP_comm_type comm_type);
   858                             
   859 PEP_STATUS update_trust_for_fpr(PEP_SESSION session, 
   860                                 const char* fpr, 
   861                                 PEP_comm_type comm_type);
   862 
   863 // least_trust() - get the least known trust level for a key in the database
   864 //
   865 //  parameters:
   866 //      session (in)            session handle
   867 //      fpr (in)                fingerprint of key to check
   868 //      comm_type (out)         least comm_type as result (out)
   869 //
   870 //  if the trust level cannot be determined comm_type is set to PEP_ct_unknown
   871 
   872 DYNAMIC_API PEP_STATUS least_trust(
   873         PEP_SESSION session,
   874         const char *fpr,
   875         PEP_comm_type *comm_type
   876     );
   877 
   878 
   879 // get_key_rating() - get the rating a bare key has
   880 //
   881 //  parameters:
   882 //      session (in)            session handle
   883 //      fpr (in)                unique identifyer for key as UTF-8 string
   884 //      comm_type (out)         key rating
   885 //
   886 //  if an error occurs, *comm_type is set to PEP_ct_unknown and an error
   887 //  is returned
   888 
   889 DYNAMIC_API PEP_STATUS get_key_rating(
   890         PEP_SESSION session,
   891         const char *fpr,
   892         PEP_comm_type *comm_type
   893     );
   894 
   895 
   896 // renew_key() - renew an expired key
   897 //
   898 //  parameters:
   899 //      session (in)            session handle
   900 //      fpr (in)                ID of key to renew as UTF-8 string
   901 //      ts (in)                 timestamp when key should expire or NULL for
   902 //                              default
   903 
   904 DYNAMIC_API PEP_STATUS renew_key(
   905         PEP_SESSION session,
   906         const char *fpr,
   907         const timestamp *ts
   908     );
   909 
   910 
   911 // revoke_key() - revoke a key
   912 //
   913 //  parameters:
   914 //      session (in)            session handle
   915 //      fpr (in)                ID of key to revoke as UTF-8 string
   916 //      reason (in)             text with reason for revoke as UTF-8 string
   917 //                              or NULL if reason unknown
   918 //
   919 //  caveat:
   920 //      reason text must not include empty lines
   921 //      this function is meant for internal use only; better use
   922 //      key_mistrusted() of keymanagement API
   923 
   924 DYNAMIC_API PEP_STATUS revoke_key(
   925         PEP_SESSION session,
   926         const char *fpr,
   927         const char *reason
   928     );
   929 
   930 
   931 // key_expired() - flags if a key is already expired
   932 //
   933 //  parameters:
   934 //      session (in)            session handle
   935 //      fpr (in)                ID of key to check as UTF-8 string
   936 //      when (in)               UTC time of when should expiry be considered
   937 //      expired (out)           flag if key expired
   938 
   939 DYNAMIC_API PEP_STATUS key_expired(
   940         PEP_SESSION session,
   941         const char *fpr,
   942         const time_t when,
   943         bool *expired
   944     );
   945 
   946     
   947 // key_revoked() - flags if a key is already revoked
   948 //
   949 //  parameters:
   950 //      session (in)            session handle
   951 //      fpr (in)                ID of key to check as UTF-8 string
   952 //      revoked (out)           flag if key revoked
   953 
   954 DYNAMIC_API PEP_STATUS key_revoked(
   955         PEP_SESSION session,
   956         const char *fpr,
   957         bool *revoked
   958     );
   959 
   960 PEP_STATUS get_key_userids(
   961         PEP_SESSION session,
   962         const char* fpr,
   963         stringlist_t** keylist
   964     );
   965 
   966 
   967 // get_crashdump_log() - get the last log messages out
   968 //
   969 //  parameters:
   970 //      session (in)            session handle
   971 //      maxlines (in)           maximum number of lines (0 for default)
   972 //      logdata (out)           logdata as string in double quoted CSV format
   973 //                              column1 is title
   974 //                              column2 is entity
   975 //                              column3 is description
   976 //                              column4 is comment
   977 //
   978 //  caveat:
   979 //      the ownership of logdata goes to the caller
   980 
   981 DYNAMIC_API PEP_STATUS get_crashdump_log(
   982         PEP_SESSION session,
   983         int maxlines,
   984         char **logdata
   985     );
   986 
   987 
   988 // get_languagelist() - get the list of languages
   989 //
   990 //  parameters:
   991 //      session (in)            session handle
   992 //      languages (out)         languages as string in double quoted CSV format
   993 //                              column 1 is the ISO 639-1 language code
   994 //                              column 2 is the name of the language
   995 //
   996 //  caveat:
   997 //      the ownership of languages goes to the caller
   998 
   999 DYNAMIC_API PEP_STATUS get_languagelist(
  1000         PEP_SESSION session,
  1001         char **languages
  1002     );
  1003 
  1004 
  1005 // get_phrase() - get phrase in a dedicated language through i18n
  1006 //
  1007 //  parameters:
  1008 //      session (in)            session handle
  1009 //      lang (in)               C string with ISO 639-1 language code
  1010 //      phrase_id (in)          id of phrase in i18n
  1011 //      phrase (out)            phrase as UTF-8 string
  1012 //
  1013 //  caveat:
  1014 //      the ownership of phrase goes to the caller
  1015 
  1016 DYNAMIC_API PEP_STATUS get_phrase(
  1017         PEP_SESSION session,
  1018         const char *lang,
  1019         int phrase_id,
  1020         char **phrase
  1021     );
  1022 
  1023 
  1024 // sequence_value() - raise the value of a named sequence and retrieve it
  1025 //
  1026 //  parameters:
  1027 //      session (in)            session handle
  1028 //      name (inout)            name of sequence or char[37] set to {0, }
  1029 //                              for new own sequence named as UUID
  1030 //      value (inout)           value of sequence value to test or 0 for
  1031 //                              getting next value
  1032 //
  1033 //  returns:
  1034 //      PEP_STATUS_OK                   no error, not own sequence
  1035 //      PEP_SEQUENCE_VIOLATED           if sequence violated
  1036 //      PEP_CANNOT_INCREASE_SEQUENCE    if sequence cannot be increased
  1037 //      PEP_OWN_SEQUENCE                if own sequence
  1038 
  1039 DYNAMIC_API PEP_STATUS sequence_value(
  1040         PEP_SESSION session,
  1041         char *name,
  1042         int32_t *value
  1043     );
  1044 
  1045 
  1046 // set_revoked() - records relation between a revoked key and its replacement
  1047 //
  1048 //  parameters:
  1049 //      session (in)            session handle
  1050 //      revoked_fpr (in)        revoked fingerprint
  1051 //      replacement_fpr (in)    replacement key fingerprint
  1052 //      revocation_date (in)    revocation date
  1053 
  1054 DYNAMIC_API PEP_STATUS set_revoked(
  1055        PEP_SESSION session,
  1056        const char *revoked_fpr,
  1057        const char *replacement_fpr,
  1058        const uint64_t revocation_date
  1059     );
  1060 
  1061 
  1062 // get_revoked() - find revoked key that may have been replaced by given key, if any
  1063 //
  1064 //  parameters:
  1065 //      session (in)            session handle
  1066 //      fpr (in)                given fingerprint
  1067 //      revoked_fpr (out)       revoked fingerprint
  1068 //      revocation_date (out)   revocation date
  1069     
  1070 DYNAMIC_API PEP_STATUS get_revoked(
  1071         PEP_SESSION session,
  1072         const char *fpr,
  1073         char **revoked_fpr,
  1074         uint64_t *revocation_date
  1075     );
  1076 
  1077 
  1078 // key_created() - get creation date of a key
  1079 //
  1080 //  parameters:
  1081 //      session (in)            session handle
  1082 //      fpr (in)                fingerprint of key
  1083 //      created (out)           date of creation
  1084 
  1085 PEP_STATUS key_created(
  1086         PEP_SESSION session,
  1087         const char *fpr,
  1088         time_t *created
  1089     );
  1090 
  1091 
  1092 // find_private_keys() - find keys in keyring
  1093 //
  1094 //  parameters:
  1095 //      session (in)            session handle
  1096 //      pattern (in)            key id, user id or address to search for as
  1097 //                              UTF-8 string
  1098 //      keylist (out)           list of fingerprints found or NULL on error
  1099 //
  1100 //  caveat:
  1101 //        the ownerships of keylist isgoing to the caller
  1102 //        the caller must use free_stringlist() to free it
  1103 PEP_STATUS find_private_keys(PEP_SESSION session, const char* pattern,
  1104                              stringlist_t **keylist);
  1105 
  1106 // get_engine_version() - returns the current version of pEpEngine (this is different
  1107 //                        from the pEp protocol version!)
  1108 //
  1109 //  parameters: none
  1110 //
  1111 //  return_value: const char* to the engine version string constant
  1112 //
  1113 DYNAMIC_API const char* get_engine_version();
  1114 
  1115 
  1116 DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session);
  1117 
  1118 #ifdef __cplusplus
  1119 }
  1120 #endif