src/keymanagement.h
author Volker Birk <vb@pep.foundation>
Sat, 24 Feb 2018 16:51:38 +0100
changeset 2535 76edc488ccdd
parent 2494 56c577c84996
child 2548 3bfda8f2136e
permissions -rw-r--r--
SEMANTIC CHANGE in myself(): .fpr is never used; to set fpr now use set_own_key()
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@0
   167
//      retrieve_next_identity  pointer to retrieve_next_identity() callback
vb@0
   168
//                              which returns at least a valid address field in
vb@0
   169
//                              the identity struct
vb@0
   170
//      management              management data to give to keymanagement
vb@0
   171
//                              (implementation defined)
vb@0
   172
//
vb@0
   173
//  return value:
vb@0
   174
//      PEP_STATUS_OK if thread has to terminate successfully or any other
vb@0
   175
//      value on failure
vb@0
   176
//
vb@0
   177
//  caveat:
vb@0
   178
//      to ensure proper working of this library, a thread has to be started
vb@0
   179
//      with this function immediately after initialization
vb@0
   180
//      do_keymanagement() calls retrieve_next_identity(management)
vb@0
   181
vb@0
   182
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   183
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   184
        void *management
vb@0
   185
    );
vb@0
   186
vb@215
   187
krista@2129
   188
// key_mistrusted() - mark key as being compromised
vb@215
   189
//
vb@215
   190
//  parameters:
vb@215
   191
//      session (in)        session to use
krista@2129
   192
//      ident (in)          person and key which was compromised
krista@2461
   193
//  caveat:
krista@2461
   194
//      ident is INPUT ONLY. If you want updated trust on the identity, you'll have
krista@2461
   195
//      to call update_identity or myself respectively after this.
krista@2461
   196
//      N.B. If you are calling this on a key that is the identity or user default,
krista@2461
   197
//      it will be removed as the default key for ANY identity and user for which
krista@2461
   198
//      it is the default. Please keep in mind that the undo in undo_last_mistrust
krista@2461
   199
//      will only undo the current identity's / it's user's default, not any
krista@2461
   200
//      other identities which may be impacted (this will not affect most use
krista@2461
   201
//      cases)
vb@215
   202
krista@1213
   203
DYNAMIC_API PEP_STATUS key_mistrusted(
vb@357
   204
        PEP_SESSION session,
vb@357
   205
        pEp_identity *ident
vb@357
   206
    );
vb@215
   207
krista@2129
   208
// undo_last_mistrust() - reset identity and trust status for the last
krista@2129
   209
//                        identity in this session marked as mistrusted
krista@2129
   210
//                        to their cached values from the time of mistrust
krista@2129
   211
//  parameters:
krista@2129
   212
//      session (in)        session to use
krista@2129
   213
//
krista@2129
   214
//  return value:
krista@2129
   215
//      PEP_STATUS_OK if identity and trust were successfully restored.
krista@2129
   216
//      Otherwise, error status from attempts to set.
krista@2129
   217
//
krista@2129
   218
//  caveat:
krista@2129
   219
//      only works for this session, and only once. cache is invalidated
krista@2129
   220
//      upon use.
krista@2129
   221
//
krista@2129
   222
//      WILL NOT WORK ON MISTRUSTED OWN KEY
krista@2129
   223
krista@2129
   224
DYNAMIC_API PEP_STATUS undo_last_mistrust(PEP_SESSION session);
krista@2129
   225
vb@215
   226
krista@2461
   227
// trust_personal_key() - mark a key as trusted for a user
vb@354
   228
//
vb@354
   229
//  parameters:
vb@354
   230
//      session (in)        session to use
vb@354
   231
//      ident (in)          person and key to trust in
vb@354
   232
//
vb@354
   233
//  caveat:
vb@354
   234
//      the fields user_id, address and fpr must be supplied
krista@2494
   235
//      for non-own users, this will 1) set the trust bit on its comm type in the DB,
krista@2461
   236
//      2) set this key as the identity default if the current identity default
krista@2461
   237
//      is not trusted, and 3) set this key as the user default if the current
krista@2461
   238
//      user default is not trusted.
krista@2461
   239
//      For an own user, this is simply a call to myself().
vb@354
   240
vb@354
   241
DYNAMIC_API PEP_STATUS trust_personal_key(
vb@354
   242
        PEP_SESSION session,
vb@354
   243
        pEp_identity *ident
vb@354
   244
    );
vb@354
   245
vb@354
   246
krista@2461
   247
// key_reset_trust() - reset trust bit or explicitly mistrusted status for an identity and
krista@2461
   248
//                     its accompanying key/user_id pair.
Edouard@410
   249
//  parameters:
Edouard@410
   250
//      session (in)        session to use
krista@2461
   251
//      ident (in)          identity for person and key whose trust status is to be reset
krista@2461
   252
//
krista@2461
   253
//  caveat:
krista@2461
   254
//      ident is INPUT ONLY. If you want updated trust on the identity, you'll have
krista@2461
   255
//      to call update_identity or myself respectively after this.
krista@2461
   256
//      N.B. If you are calling this on a key that is the identity or user default,
krista@2461
   257
//      it will be removed as the default key for the identity and user (but is still
krista@2461
   258
//      available for key election, it is just not the cached default anymore)
Edouard@410
   259
Edouard@410
   260
DYNAMIC_API PEP_STATUS key_reset_trust(
Edouard@410
   261
        PEP_SESSION session,
Edouard@410
   262
        pEp_identity *ident
Edouard@410
   263
    );
Edouard@410
   264
Edouard@584
   265
// own_key_is_listed() - returns true id key is listed as own key
Edouard@584
   266
//
Edouard@584
   267
//  parameters:
Edouard@584
   268
//      session (in)        session to use
Edouard@584
   269
//      fpr (in)            fingerprint of key to test
roker@2060
   270
//      listed (out)        flags if key is own
Edouard@584
   271
Edouard@584
   272
DYNAMIC_API PEP_STATUS own_key_is_listed(
Edouard@584
   273
        PEP_SESSION session,
Edouard@584
   274
        const char *fpr,
Edouard@584
   275
        bool *listed
Edouard@584
   276
    );
Edouard@584
   277
Edouard@584
   278
edouard@1412
   279
// _own_identities_retrieve() - retrieve all own identities
edouard@1412
   280
//
edouard@1412
   281
//  parameters:
edouard@1412
   282
//      session (in)            session to use
edouard@1412
   283
//      own_identities (out)    list of own identities
edouard@1412
   284
//      excluded_flags (int)    flags to exclude from results
edouard@1412
   285
//
edouard@1412
   286
//  caveat:
edouard@1412
   287
//      the ownership of the copy of own_identities goes to the caller
edouard@1412
   288
edouard@1412
   289
DYNAMIC_API PEP_STATUS _own_identities_retrieve(
edouard@1412
   290
        PEP_SESSION session,
edouard@1412
   291
        identity_list **own_identities,
edouard@1412
   292
        identity_flags_t excluded_flags
edouard@1412
   293
    );
edouard@1412
   294
vb@955
   295
// own_identities_retrieve() - retrieve all own identities
Edouard@584
   296
//
Edouard@584
   297
//  parameters:
vb@955
   298
//      session (in)            session to use
vb@955
   299
//      own_identities (out)    list of own identities
Edouard@584
   300
//
Edouard@584
   301
//  caveat:
vb@1133
   302
//      the ownership of the copy of own_identities goes to the caller
Edouard@584
   303
vb@955
   304
DYNAMIC_API PEP_STATUS own_identities_retrieve(
Edouard@584
   305
        PEP_SESSION session,
vb@955
   306
        identity_list **own_identities
Edouard@584
   307
    );
Edouard@410
   308
krista@1357
   309
PEP_STATUS contains_priv_key(PEP_SESSION session, const char *fpr,
krista@1357
   310
                             bool *has_private);
krista@1357
   311
edouard@1412
   312
// _own_keys_retrieve() - retrieve all flagged keypair fingerprints 
edouard@1412
   313
//
edouard@1412
   314
//  parameters:
edouard@1412
   315
//      session (in)            session to use
edouard@1412
   316
//      keylist (out)           list of fingerprints
edouard@1412
   317
//      excluded_flags (int)    flags to exclude from results
edouard@1412
   318
//
edouard@1412
   319
//  caveat:
edouard@1412
   320
//      the ownership of the list goes to the caller
edouard@1412
   321
DYNAMIC_API PEP_STATUS _own_keys_retrieve(
edouard@1412
   322
        PEP_SESSION session,
edouard@1412
   323
        stringlist_t **keylist,
edouard@1412
   324
        identity_flags_t excluded_flags
edouard@1412
   325
      );
edouard@1412
   326
edouard@1394
   327
// own_keys_retrieve() - retrieve all flagged keypair fingerprints 
edouard@1370
   328
//
edouard@1370
   329
//  parameters:
edouard@1370
   330
//      session (in)            session to use
edouard@1370
   331
//      keylist (out)           list of fingerprints
edouard@1370
   332
//
edouard@1370
   333
//  caveat:
edouard@1370
   334
//      the ownership of the list goes to the caller
edouard@1394
   335
DYNAMIC_API PEP_STATUS own_keys_retrieve(
edouard@1370
   336
        PEP_SESSION session,
edouard@1370
   337
        stringlist_t **keylist
edouard@1370
   338
      );
edouard@1370
   339
vb@2535
   340
// set_own_key() - mark a key as own key
vb@2535
   341
//
vb@2535
   342
//  parameters:
vb@2535
   343
//      session (in)            session to use
vb@2535
   344
//      me (inout)              own identity this key is used for
vb@2535
   345
//      fpr (in)                fingerprint of the key to mark as own key
vb@2535
   346
//
vb@2535
   347
//  caveat:
vb@2535
   348
//      the key has to be in the key ring already
vb@2535
   349
//      me->address, me->user_id and me->username must be set to valid data
vb@2535
   350
//      myself() is called by set_own_key() without key generation
vb@2535
   351
//      me->flags are ignored
vb@2535
   352
//      me->address must not be an alias
vb@2535
   353
//      me->fpr will be ignored and replaced by fpr
vb@2535
   354
edouard@1752
   355
DYNAMIC_API PEP_STATUS set_own_key(
edouard@1752
   356
       PEP_SESSION session,
vb@2535
   357
       pEp_identity *me,
edouard@1752
   358
       const char *fpr
edouard@1752
   359
    );
edouard@1752
   360
krista@2461
   361
PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags);
krista@2461
   362
krista@2471
   363
PEP_STATUS add_mistrusted_key(PEP_SESSION session, const char* fpr);
krista@2471
   364
PEP_STATUS delete_mistrusted_key(PEP_SESSION session, const char* fpr);
krista@2471
   365
PEP_STATUS is_mistrusted_key(PEP_SESSION session, const char* fpr, bool* mistrusted);
krista@2471
   366
krista@2471
   367
vb@0
   368
#ifdef __cplusplus
vb@0
   369
}
vb@0
   370
#endif