src/keymanagement.h
author Volker Birk <vb@pep.foundation>
Wed, 29 Aug 2018 17:10:23 +0200
branchsync
changeset 2899 63b619aef131
parent 2834 7a35e7ea6138
child 2955 3b2500a15709
child 2981 39a510d16cdd
permissions -rw-r--r--
somewhat bigger
vb@1517
     1
// This file is under GNU General Public License 3.0
vb@1517
     2
// see LICENSE.txt
vb@1517
     3
vb@39
     4
#pragma once
vb@39
     5
vb@217
     6
#include "pEpEngine.h"
vb@217
     7
vb@0
     8
#ifdef __cplusplus
vb@0
     9
extern "C" {
vb@0
    10
#endif
vb@0
    11
vb@0
    12
// update_identity() - update identity information
vb@0
    13
//
vb@0
    14
//  parameters:
vb@0
    15
//      session (in)        session to use
vb@0
    16
//      identity (inout)    identity information of communication partner
krista@2461
    17
//                          (identity->fpr is OUT ONLY), and at least
krista@2461
    18
//                          .address must be set. 
krista@2461
    19
//                          If .username is set, it will be used to set or patch
krista@2461
    20
//                          the username record for this identity.                         
edouard@1850
    21
//  return value:
edouard@1850
    22
//      PEP_STATUS_OK if identity could be updated,
krista@2461
    23
//      PEP_ILLEGAL_VALUE if called with illegal inputs, including an identity
krista@2461
    24
//                        with .me set or with an own user_id specified in the
krista@2461
    25
//                        *input* (see caveats) 
krista@2461
    26
//      PEP_KEY_UNSUITABLE if a default key was found for this identity, no
krista@2461
    27
//                         other acceptable keys were found; if this is returned,
krista@2461
    28
//                         the reason for rejecting the first default key found
krista@2461
    29
//                         may be found in the comm_type
edouard@1850
    30
//      any other value on error
edouard@1850
    31
//
vb@0
    32
//  caveat:
krista@2461
    33
//      at least identity->address must be a non-empty UTF-8 string as input
krista@2461
    34
//      update_identity() never writes flags; use set_identity_flags() for
krista@2461
    35
//      writing
krista@2461
    36
//      this function NEVER reads the incoming fpr, only writes to it.
krista@2461
    37
//      this function will fail if called on an identity which, with its input
krista@2461
    38
//      values, *explicitly* indicates it is an own identity (i.e. .me is set
krista@2461
    39
//      to true on input, or a user_id is given AND it is a known own user_id).
krista@2461
    40
//      however, it can RETURN an own identity if this is not indicated a
krista@2461
    41
//      priori, and in fact will do so with prejudice when not faced with a
krista@2461
    42
//      matching default (i.e. it is forced to search by address only).
krista@2461
    43
//      if the identity is known to be an own identity (or the caller wishes
krista@2461
    44
//      to make it one), call myself() on the identity instead.
krista@2461
    45
//
krista@2461
    46
//      FIXME: is this next point accurate?
vb@10
    47
//      if this function returns PEP_ct_unknown or PEP_ct_key_expired in
vb@10
    48
//      identity->comm_type, the caller must insert the identity into the
vb@10
    49
//      asynchronous management implementation, so retrieve_next_identity()
vb@10
    50
//      will return this identity later
krista@2461
    51
//      END FIXME
vb@0
    52
vb@1387
    53
DYNAMIC_API PEP_STATUS update_identity(
vb@0
    54
        PEP_SESSION session, pEp_identity * identity
vb@0
    55
    );
vb@0
    56
vb@2535
    57
// TODO: remove
krista@2461
    58
// initialise_own_identities () - ensures that an own identity is complete
krista@2461
    59
//
krista@2461
    60
//  parameters:
krista@2461
    61
//      session (in)        session to use
krista@2461
    62
//      my_idents (inout)   identities of local user to quick-set
krista@2461
    63
//                          For these, at least .address must be set.
krista@2461
    64
//                          if no .user_id is set, AND the DB doesn't contain
krista@2461
    65
//                          a default user_id, PEP_OWN_USERID will be used and
krista@2461
    66
//                          become the perennial default for the DB.
krista@2461
    67
//
krista@2461
    68
//  return value:
krista@2461
    69
//      PEP_STATUS_OK if identity could be set,
krista@2461
    70
//      any other value on error
krista@2461
    71
//
krista@2461
    72
//  caveat:
krista@2461
    73
//      this function does NOT generate keypairs. It is intended to
krista@2461
    74
//      precede running of the engine on actual messages. It effectively
krista@2461
    75
//      behaves like myself(), but when there would normally be key generation
krista@2461
    76
//      (when there is no valid key, for example),
krista@2461
    77
//      it instead stores an identity without keys.
krista@2461
    78
//
krista@2461
    79
//      N.B. to adapter devs - this function is likely unnecessary, so please
krista@2461
    80
//      do not put work into exposing it yet. Tickets will be filed if need be.
vb@0
    81
vb@2535
    82
// DYNAMIC_API PEP_STATUS initialise_own_identities(PEP_SESSION session,
vb@2535
    83
//                                                  identity_list* my_idents);
krista@2461
    84
krista@2461
    85
// myself() - ensures that an own identity is complete
vb@0
    86
//
vb@0
    87
//  parameters:
vb@0
    88
//      session (in)        session to use
vb@0
    89
//      identity (inout)    identity of local user
krista@2461
    90
//                          both .address and .user_id must be set.
krista@2461
    91
//                          if .fpr is set, an attempt will be made to make
krista@2461
    92
//                          that the default key for this identity after key
krista@2461
    93
//                          validation
krista@2461
    94
//                          if .fpr is not set, key retrieval is performed
krista@2461
    95
//                          If .username is set, it will be used to set or patch
krista@2461
    96
//                          the username record for this identity.                         
vb@0
    97
//
vb@0
    98
//  return value:
vb@0
    99
//      PEP_STATUS_OK if identity could be completed or was already complete,
vb@0
   100
//      any other value on error
vb@0
   101
//  caveat:
krista@2461
   102
//      If an fpr was entered and is not a valid key, the reason for failure
krista@2461
   103
//      is immediately returned in the status and, possibly, identity->comm_type
krista@2461
   104
//      If a default own user_id exists in the database, an alias will 
krista@2461
   105
//      be created for the default for the input user_id. The ENGINE'S default
krista@2461
   106
//      user_id is always returned in the .user_id field
krista@2461
   107
//      myself() NEVER elects keys from the keyring; it will only choose keys
krista@2461
   108
//      which have been set up explicitly via myself(), or which were imported
krista@2461
   109
//      during a first time DB setup from an OpenPGP keyring (compatibility only) 
vb@0
   110
//      this function generates a keypair on demand; because it's synchronous
vb@0
   111
//      it can need a decent amount of time to return
vb@0
   112
//      if you need to do this asynchronous, you need to return an identity
vb@0
   113
//      with retrieve_next_identity() where pEp_identity.me is true
vb@0
   114
vb@0
   115
DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity);
vb@0
   116
edouard@1406
   117
PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags);
vb@0
   118
vb@0
   119
// retrieve_next_identity() - callback being called by do_keymanagement()
vb@0
   120
//
vb@0
   121
//  parameters:
vb@0
   122
//      management (in)     data structure to deliver (implementation defined)
vb@0
   123
//
vb@0
   124
//  return value:
vb@0
   125
//      identity to check or NULL to terminate do_keymanagement()
vb@0
   126
//      if given identity must be created with new_identity()
vb@0
   127
//      the identity struct is going to the ownership of this library
vb@0
   128
//      it must not be freed by the callee
vb@0
   129
//
vb@0
   130
//  caveat:
vb@0
   131
//      this callback has to block until an identity or NULL can be returned
vb@0
   132
//      an implementation is not provided by this library; instead it has to be
vb@0
   133
//      implemented by the user of this library
vb@0
   134
vb@0
   135
typedef pEp_identity *(*retrieve_next_identity_t)(void *management);
vb@0
   136
vb@0
   137
vb@292
   138
// examine_identity() - callback for appending to queue
vb@292
   139
//
vb@292
   140
//  parameters:
vb@292
   141
//      ident (in)          identity to examine
vb@292
   142
//      management (in)     data structure to deliver (implementation defined)
vb@292
   143
//
vb@292
   144
//  return value:
vb@292
   145
//      0 if identity was added successfully to queue or nonzero otherwise
vb@292
   146
vb@296
   147
typedef int (*examine_identity_t)(pEp_identity *ident, void *management);
vb@292
   148
vb@292
   149
vb@292
   150
// register_examine_function() - register examine_identity() callback
vb@292
   151
//
vb@292
   152
//  parameters:
vb@292
   153
//      session (in)            session to use
vb@292
   154
//      examine_identity (in)   examine_identity() function to register
vb@292
   155
//      management (in)     data structure to deliver (implementation defined)
vb@292
   156
vb@296
   157
DYNAMIC_API PEP_STATUS register_examine_function(
vb@292
   158
        PEP_SESSION session, 
vb@292
   159
        examine_identity_t examine_identity,
vb@292
   160
        void *management
vb@292
   161
    );
vb@292
   162
vb@292
   163
vb@0
   164
// do_keymanagement() - function to be run on an extra thread
vb@0
   165
//
vb@0
   166
//  parameters:
vb@2834
   167
//      session (in)                session to use
vb@2833
   168
//      retrieve_next_identity (in) pointer to retrieve_next_identity()
vb@2833
   169
//                                  callback which returns at least a valid
vb@2833
   170
//                                  address field in the identity struct
vb@2833
   171
//      messageToSend (in)          callback for sending message by the
vb@2833
   172
//                                  application
vb@2833
   173
//      management (in)             management data to give to keymanagement
vb@2833
   174
//                                  (implementation defined)
vb@0
   175
//
vb@0
   176
//  return value:
vb@0
   177
//      PEP_STATUS_OK if thread has to terminate successfully or any other
vb@0
   178
//      value on failure
vb@0
   179
//
vb@0
   180
//  caveat:
vb@0
   181
//      to ensure proper working of this library, a thread has to be started
vb@0
   182
//      with this function immediately after initialization
vb@2833
   183
//
vb@0
   184
//      do_keymanagement() calls retrieve_next_identity(management)
vb@2833
   185
//
vb@2833
   186
//      messageToSend can only be null if no transport is application based
vb@2833
   187
//      if transport system is not used it must not be NULL
vb@0
   188
vb@0
   189
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@2834
   190
        PEP_SESSION session,
vb@0
   191
        retrieve_next_identity_t retrieve_next_identity,
vb@2833
   192
        messageToSend_t messageToSend,
vb@0
   193
        void *management
vb@0
   194
    );
vb@0
   195
vb@215
   196
krista@2129
   197
// key_mistrusted() - mark key as being compromised
vb@215
   198
//
vb@215
   199
//  parameters:
vb@215
   200
//      session (in)        session to use
krista@2129
   201
//      ident (in)          person and key which was compromised
krista@2461
   202
//  caveat:
krista@2461
   203
//      ident is INPUT ONLY. If you want updated trust on the identity, you'll have
krista@2461
   204
//      to call update_identity or myself respectively after this.
krista@2461
   205
//      N.B. If you are calling this on a key that is the identity or user default,
krista@2461
   206
//      it will be removed as the default key for ANY identity and user for which
krista@2461
   207
//      it is the default. Please keep in mind that the undo in undo_last_mistrust
krista@2461
   208
//      will only undo the current identity's / it's user's default, not any
krista@2461
   209
//      other identities which may be impacted (this will not affect most use
krista@2461
   210
//      cases)
vb@215
   211
krista@1213
   212
DYNAMIC_API PEP_STATUS key_mistrusted(
vb@357
   213
        PEP_SESSION session,
vb@357
   214
        pEp_identity *ident
vb@357
   215
    );
vb@215
   216
krista@2129
   217
// undo_last_mistrust() - reset identity and trust status for the last
krista@2129
   218
//                        identity in this session marked as mistrusted
krista@2129
   219
//                        to their cached values from the time of mistrust
krista@2129
   220
//  parameters:
krista@2129
   221
//      session (in)        session to use
krista@2129
   222
//
krista@2129
   223
//  return value:
krista@2129
   224
//      PEP_STATUS_OK if identity and trust were successfully restored.
krista@2129
   225
//      Otherwise, error status from attempts to set.
krista@2129
   226
//
krista@2129
   227
//  caveat:
krista@2129
   228
//      only works for this session, and only once. cache is invalidated
krista@2129
   229
//      upon use.
krista@2129
   230
//
krista@2129
   231
//      WILL NOT WORK ON MISTRUSTED OWN KEY
krista@2129
   232
krista@2129
   233
DYNAMIC_API PEP_STATUS undo_last_mistrust(PEP_SESSION session);
krista@2129
   234
vb@215
   235
krista@2461
   236
// trust_personal_key() - mark a key as trusted for a user
vb@354
   237
//
vb@354
   238
//  parameters:
vb@354
   239
//      session (in)        session to use
vb@354
   240
//      ident (in)          person and key to trust in
vb@354
   241
//
vb@354
   242
//  caveat:
vb@354
   243
//      the fields user_id, address and fpr must be supplied
krista@2494
   244
//      for non-own users, this will 1) set the trust bit on its comm type in the DB,
krista@2461
   245
//      2) set this key as the identity default if the current identity default
krista@2461
   246
//      is not trusted, and 3) set this key as the user default if the current
krista@2461
   247
//      user default is not trusted.
krista@2461
   248
//      For an own user, this is simply a call to myself().
vb@354
   249
vb@354
   250
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
   251
        PEP_SESSION session,
vb@354
   252
        pEp_identity *ident
vb@354
   253
    );
vb@354
   254
vb@354
   255
krista@2461
   256
// key_reset_trust() - reset trust bit or explicitly mistrusted status for an identity and
krista@2461
   257
//                     its accompanying key/user_id pair.
Edouard@410
   258
//  parameters:
Edouard@410
   259
//      session (in)        session to use
krista@2461
   260
//      ident (in)          identity for person and key whose trust status is to be reset
krista@2461
   261
//
krista@2461
   262
//  caveat:
krista@2461
   263
//      ident is INPUT ONLY. If you want updated trust on the identity, you'll have
krista@2461
   264
//      to call update_identity or myself respectively after this.
krista@2461
   265
//      N.B. If you are calling this on a key that is the identity or user default,
krista@2461
   266
//      it will be removed as the default key for the identity and user (but is still
krista@2461
   267
//      available for key election, it is just not the cached default anymore)
Edouard@410
   268
Edouard@410
   269
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
   270
        PEP_SESSION session,
Edouard@410
   271
        pEp_identity *ident
Edouard@410
   272
    );
Edouard@410
   273
Edouard@584
   274
// own_key_is_listed() - returns true id key is listed as own key
Edouard@584
   275
//
Edouard@584
   276
//  parameters:
Edouard@584
   277
//      session (in)        session to use
Edouard@584
   278
//      fpr (in)            fingerprint of key to test
roker@2060
   279
//      listed (out)        flags if key is own
Edouard@584
   280
Edouard@584
   281
DYNAMIC_API PEP_STATUS own_key_is_listed(
Edouard@584
   282
        PEP_SESSION session,
Edouard@584
   283
        const char *fpr,
Edouard@584
   284
        bool *listed
Edouard@584
   285
    );
Edouard@584
   286
Edouard@584
   287
edouard@1412
   288
// _own_identities_retrieve() - retrieve all own identities
edouard@1412
   289
//
edouard@1412
   290
//  parameters:
edouard@1412
   291
//      session (in)            session to use
edouard@1412
   292
//      own_identities (out)    list of own identities
edouard@1412
   293
//      excluded_flags (int)    flags to exclude from results
edouard@1412
   294
//
edouard@1412
   295
//  caveat:
edouard@1412
   296
//      the ownership of the copy of own_identities goes to the caller
edouard@1412
   297
edouard@1412
   298
DYNAMIC_API PEP_STATUS _own_identities_retrieve(
edouard@1412
   299
        PEP_SESSION session,
edouard@1412
   300
        identity_list **own_identities,
edouard@1412
   301
        identity_flags_t excluded_flags
edouard@1412
   302
    );
edouard@1412
   303
vb@955
   304
// own_identities_retrieve() - retrieve all own identities
Edouard@584
   305
//
Edouard@584
   306
//  parameters:
vb@955
   307
//      session (in)            session to use
vb@955
   308
//      own_identities (out)    list of own identities
Edouard@584
   309
//
Edouard@584
   310
//  caveat:
vb@1133
   311
//      the ownership of the copy of own_identities goes to the caller
Edouard@584
   312
vb@955
   313
DYNAMIC_API PEP_STATUS own_identities_retrieve(
Edouard@584
   314
        PEP_SESSION session,
vb@955
   315
        identity_list **own_identities
Edouard@584
   316
    );
Edouard@410
   317
krista@1357
   318
PEP_STATUS contains_priv_key(PEP_SESSION session, const char *fpr,
krista@1357
   319
                             bool *has_private);
krista@1357
   320
edouard@1412
   321
// _own_keys_retrieve() - retrieve all flagged keypair fingerprints 
edouard@1412
   322
//
edouard@1412
   323
//  parameters:
edouard@1412
   324
//      session (in)            session to use
edouard@1412
   325
//      keylist (out)           list of fingerprints
edouard@1412
   326
//      excluded_flags (int)    flags to exclude from results
edouard@1412
   327
//
edouard@1412
   328
//  caveat:
edouard@1412
   329
//      the ownership of the list goes to the caller
edouard@1412
   330
DYNAMIC_API PEP_STATUS _own_keys_retrieve(
edouard@1412
   331
        PEP_SESSION session,
edouard@1412
   332
        stringlist_t **keylist,
edouard@1412
   333
        identity_flags_t excluded_flags
edouard@1412
   334
      );
edouard@1412
   335
edouard@1394
   336
// own_keys_retrieve() - retrieve all flagged keypair fingerprints 
edouard@1370
   337
//
edouard@1370
   338
//  parameters:
edouard@1370
   339
//      session (in)            session to use
edouard@1370
   340
//      keylist (out)           list of fingerprints
edouard@1370
   341
//
edouard@1370
   342
//  caveat:
edouard@1370
   343
//      the ownership of the list goes to the caller
edouard@1394
   344
DYNAMIC_API PEP_STATUS own_keys_retrieve(
edouard@1370
   345
        PEP_SESSION session,
edouard@1370
   346
        stringlist_t **keylist
edouard@1370
   347
      );
edouard@1370
   348
vb@2535
   349
// set_own_key() - mark a key as own key
vb@2535
   350
//
vb@2535
   351
//  parameters:
vb@2535
   352
//      session (in)            session to use
vb@2535
   353
//      me (inout)              own identity this key is used for
vb@2535
   354
//      fpr (in)                fingerprint of the key to mark as own key
vb@2535
   355
//
vb@2535
   356
//  caveat:
vb@2535
   357
//      the key has to be in the key ring already
vb@2535
   358
//      me->address, me->user_id and me->username must be set to valid data
vb@2535
   359
//      myself() is called by set_own_key() without key generation
vb@2535
   360
//      me->flags are ignored
vb@2535
   361
//      me->address must not be an alias
vb@2535
   362
//      me->fpr will be ignored and replaced by fpr
vb@2535
   363
edouard@1752
   364
DYNAMIC_API PEP_STATUS set_own_key(
edouard@1752
   365
       PEP_SESSION session,
vb@2535
   366
       pEp_identity *me,
edouard@1752
   367
       const char *fpr
edouard@1752
   368
    );
edouard@1752
   369
krista@2461
   370
PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags);
krista@2461
   371
krista@2471
   372
PEP_STATUS add_mistrusted_key(PEP_SESSION session, const char* fpr);
krista@2471
   373
PEP_STATUS delete_mistrusted_key(PEP_SESSION session, const char* fpr);
krista@2471
   374
PEP_STATUS is_mistrusted_key(PEP_SESSION session, const char* fpr, bool* mistrusted);
krista@2615
   375
PEP_STATUS get_user_default_key(PEP_SESSION session, const char* user_id,
krista@2615
   376
                                char** default_key);
krista@2615
   377
krista@2615
   378
krista@2615
   379
krista@2471
   380
krista@2548
   381
// Only call on retrieval of previously stored identity!
krista@2548
   382
// Also, we presume that if the stored_identity was sent in
krista@2548
   383
// without an fpr, there wasn't one in the trust DB for this
krista@2548
   384
// identity.
krista@2548
   385
PEP_STATUS get_valid_pubkey(PEP_SESSION session,
krista@2548
   386
                            pEp_identity* stored_identity,
krista@2548
   387
                            bool* is_identity_default,
krista@2548
   388
                            bool* is_user_default,
krista@2548
   389
                            bool* is_address_default,
krista@2548
   390
                            bool check_blacklist);
krista@2471
   391
vb@0
   392
#ifdef __cplusplus
vb@0
   393
}
vb@0
   394
#endif