changing signature of renew_key, adding param for timestamp for expiry
authorVolker Birk <vb@pep-project.org>
Mon, 20 Apr 2015 17:54:57 +0200
changeset 20179bb3ac04b41
parent 200 06655f7634a6
child 202 4de298352d87
changing signature of renew_key, adding param for timestamp for expiry
src/cryptotech.h
src/pEpEngine.c
src/pEpEngine.h
src/pgp_gpg.c
src/pgp_gpg.h
     1.1 --- a/src/cryptotech.h	Mon Apr 20 15:59:13 2015 +0200
     1.2 +++ b/src/cryptotech.h	Mon Apr 20 17:54:57 2015 +0200
     1.3 @@ -47,13 +47,15 @@
     1.4          PEP_comm_type *comm_type
     1.5      );
     1.6  
     1.7 -typedef PEP_STATUS (*import_key_t)(PEP_SESSION session, const char *key_data, size_t size);
     1.8 +typedef PEP_STATUS (*import_key_t)(PEP_SESSION session, const char *key_data,
     1.9 +        size_t size);
    1.10  
    1.11  typedef PEP_STATUS (*recv_key_t)(PEP_SESSION session, const char *pattern);
    1.12  
    1.13  typedef PEP_STATUS (*send_key_t)(PEP_SESSION session, const char *pattern);
    1.14  
    1.15 -typedef PEP_STATUS (*renew_key_t)(PEP_SESSION session, const char *key_id);
    1.16 +typedef PEP_STATUS (*renew_key_t)(PEP_SESSION session, const char *key_id,
    1.17 +        const timestamp *ts);
    1.18  
    1.19  typedef PEP_STATUS (*revoke_key_t)(PEP_SESSION session, const char *key_id);
    1.20  
     2.1 --- a/src/pEpEngine.c	Mon Apr 20 15:59:13 2015 +0200
     2.2 +++ b/src/pEpEngine.c	Mon Apr 20 17:54:57 2015 +0200
     2.3 @@ -2,6 +2,7 @@
     2.4  #include "dynamic_api.h"
     2.5  #include "cryptotech.h"
     2.6  #include "transport.h"
     2.7 +#include "timestamp.h"
     2.8  
     2.9  int init_count = -1;
    2.10  
    2.11 @@ -898,7 +899,11 @@
    2.12      return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
    2.13  }
    2.14  
    2.15 -DYNAMIC_API PEP_STATUS renew_key(PEP_SESSION session, const char *fpr)
    2.16 +DYNAMIC_API PEP_STATUS renew_key(
    2.17 +        PEP_SESSION session,
    2.18 +        const char *fpr,
    2.19 +        const timestamp *ts
    2.20 +    )
    2.21  {
    2.22      assert(session);
    2.23      assert(fpr);
    2.24 @@ -906,7 +911,7 @@
    2.25      if (!(session && fpr))
    2.26          return PEP_ILLEGAL_VALUE;
    2.27  
    2.28 -    return session->cryptotech[PEP_crypt_OpenPGP].renew_key(session, fpr);
    2.29 +    return session->cryptotech[PEP_crypt_OpenPGP].renew_key(session, fpr, ts);
    2.30  }
    2.31  
    2.32  DYNAMIC_API PEP_STATUS revoke_key(PEP_SESSION session, const char *fpr)
     3.1 --- a/src/pEpEngine.h	Mon Apr 20 15:59:13 2015 +0200
     3.2 +++ b/src/pEpEngine.h	Mon Apr 20 17:54:57 2015 +0200
     3.3 @@ -10,6 +10,7 @@
     3.4  
     3.5  #include "dynamic_api.h"
     3.6  #include "stringlist.h"
     3.7 +#include "timestamp.h"
     3.8  
     3.9  #define PEP_VERSION "1.0"
    3.10  
    3.11 @@ -611,8 +612,14 @@
    3.12  //  parameters:
    3.13  //      session (in)            session handle
    3.14  //      key_id (in)             ID of key to renew as UTF-8 string
    3.15 +//      ts (in)                 timestamp when key should expire or NULL for
    3.16 +//                              default
    3.17  
    3.18 -DYNAMIC_API PEP_STATUS renew_key(PEP_SESSION session, const char *fpr);
    3.19 +DYNAMIC_API PEP_STATUS renew_key(
    3.20 +        PEP_SESSION session,
    3.21 +        const char *fpr,
    3.22 +        const timestamp *ts
    3.23 +    );
    3.24  
    3.25  
    3.26  // revoke_key() - revoke an expired key
     4.1 --- a/src/pgp_gpg.c	Mon Apr 20 15:59:13 2015 +0200
     4.2 +++ b/src/pgp_gpg.c	Mon Apr 20 17:54:57 2015 +0200
     4.3 @@ -4,6 +4,7 @@
     4.4  #include <limits.h>
     4.5  
     4.6  #include "wrappers.h"
     4.7 +#include "timestamp.h"
     4.8  
     4.9  #define _GPGERR(X) ((X) & 0xffffL)
    4.10  
    4.11 @@ -1322,7 +1323,7 @@
    4.12      return PEP_STATUS_OK;
    4.13  }
    4.14  
    4.15 -typedef struct _renew_data {
    4.16 +typedef struct _renew_state {
    4.17      enum state_t {
    4.18          renew_command = 0,
    4.19          renew_date,
    4.20 @@ -1335,20 +1336,26 @@
    4.21          renew_error = -1
    4.22      } state;
    4.23      const char *fpr_ref;
    4.24 -} renew_data;
    4.25 +    const char *date_ref;
    4.26 +} renew_state;
    4.27  
    4.28 -static gpgme_error_t renew_player(
    4.29 +static gpgme_error_t renew_fsm(
    4.30          void *_handle,
    4.31          gpgme_status_code_t statuscode,
    4.32          const char *args,
    4.33          int fd
    4.34      )
    4.35  {
    4.36 -    renew_data *handle = _handle;
    4.37 +    renew_state *handle = _handle;
    4.38  
    4.39      switch (handle->state) {
    4.40          case renew_command:
    4.41              if (statuscode == GPGME_STATUS_GET_LINE) {
    4.42 +                assert(strcmp(args, "keyedit.prompt") == 0);
    4.43 +                if (strcmp(args, "keyedit.prompt")) {
    4.44 +                    handle->state = renew_error;
    4.45 +                    return GPG_ERR_GENERAL;
    4.46 +                }
    4.47                  write(fd, "expire\n", 7);
    4.48                  handle->state = renew_date;
    4.49              }
    4.50 @@ -1356,6 +1363,11 @@
    4.51  
    4.52          case renew_date:
    4.53              if (statuscode == GPGME_STATUS_GET_LINE) {
    4.54 +                assert(strcmp(args, "keygen.valid") == 0);
    4.55 +                if (strcmp(args, "keygen.valid")) {
    4.56 +                    handle->state = renew_error;
    4.57 +                    return GPG_ERR_GENERAL;
    4.58 +                }
    4.59                  write(fd, "2015-12-31\n", 11);
    4.60                  handle->state = renew_secret_key;
    4.61              }
    4.62 @@ -1363,6 +1375,11 @@
    4.63  
    4.64          case renew_secret_key:
    4.65              if (statuscode == GPGME_STATUS_GET_LINE) {
    4.66 +                assert(strcmp(args, "keyedit.prompt") == 0);
    4.67 +                if (strcmp(args, "keyedit.prompt")) {
    4.68 +                    handle->state = renew_error;
    4.69 +                    return GPG_ERR_GENERAL;
    4.70 +                }
    4.71                  write(fd, "key 1\n", 6);
    4.72                  handle->state = renew_command2;
    4.73              }
    4.74 @@ -1370,6 +1387,11 @@
    4.75  
    4.76          case renew_command2:
    4.77              if (statuscode == GPGME_STATUS_GET_LINE) {
    4.78 +                assert(strcmp(args, "keyedit.prompt") == 0);
    4.79 +                if (strcmp(args, "keyedit.prompt")) {
    4.80 +                    handle->state = renew_error;
    4.81 +                    return GPG_ERR_GENERAL;
    4.82 +                }
    4.83                  write(fd, "expire\n", 7);
    4.84                  handle->state = renew_date2;
    4.85              }
    4.86 @@ -1377,6 +1399,11 @@
    4.87  
    4.88          case renew_date2:
    4.89              if (statuscode == GPGME_STATUS_GET_LINE) {
    4.90 +                assert(strcmp(args, "keygen.valid") == 0);
    4.91 +                if (strcmp(args, "keygen.valid")) {
    4.92 +                    handle->state = renew_error;
    4.93 +                    return GPG_ERR_GENERAL;
    4.94 +                }
    4.95                  write(fd, "2015-12-31\n", 11);
    4.96                  handle->state = renew_quit;
    4.97              }
    4.98 @@ -1384,6 +1411,11 @@
    4.99  
   4.100          case renew_quit:
   4.101              if (statuscode == GPGME_STATUS_GET_LINE) {
   4.102 +                assert(strcmp(args, "keyedit.prompt") == 0);
   4.103 +                if (strcmp(args, "keyedit.prompt")) {
   4.104 +                    handle->state = renew_error;
   4.105 +                    return GPG_ERR_GENERAL;
   4.106 +                }
   4.107                  write(fd, "quit\n", 5);
   4.108                  handle->state = renew_save;
   4.109              }
   4.110 @@ -1391,6 +1423,11 @@
   4.111  
   4.112          case renew_save:
   4.113              if (statuscode == GPGME_STATUS_GET_BOOL) {
   4.114 +                assert(strcmp(args, "keyedit.save.okay") == 0);
   4.115 +                if (strcmp(args, "keyedit.save.okay")) {
   4.116 +                    handle->state = renew_error;
   4.117 +                    return GPG_ERR_GENERAL;
   4.118 +                }
   4.119                  write(fd, "Y\n", 2);
   4.120                  handle->state = renew_exit;
   4.121              }
   4.122 @@ -1415,19 +1452,27 @@
   4.123      return size;
   4.124  }
   4.125  
   4.126 -PEP_STATUS pgp_renew_key(PEP_SESSION session, const char *fpr)
   4.127 +PEP_STATUS pgp_renew_key(
   4.128 +        PEP_SESSION session,
   4.129 +        const char *fpr,
   4.130 +        const timestamp *ts
   4.131 +    )
   4.132  {
   4.133      PEP_STATUS status = PEP_STATUS_OK;
   4.134      gpgme_error_t gpgme_error;
   4.135      gpgme_key_t key;
   4.136      gpgme_data_t output;
   4.137 -    renew_data handle;
   4.138 +    renew_state handle;
   4.139 +    char date_text[11];
   4.140  
   4.141      assert(session);
   4.142      assert(fpr);
   4.143  
   4.144 -    memset(&handle, 0, sizeof(renew_data));
   4.145 +    memset(&handle, 0, sizeof(renew_state));
   4.146      handle.fpr_ref = fpr;
   4.147 +    snprintf(date_text, 11, "%.4d-%.2d-%.2d", ts->tm_year + 1900,
   4.148 +            ts->tm_mon + 1, ts->tm_mday);
   4.149 +    handle.date_ref = date_text;
   4.150  
   4.151      status = find_single_key(session, fpr, &key);
   4.152      if (status != PEP_STATUS_OK)
   4.153 @@ -1438,7 +1483,7 @@
   4.154      data_cbs.write = _nullwriter;
   4.155      gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
   4.156  
   4.157 -    gpgme_error = gpgme_op_edit(session->ctx, key, renew_player, &handle,
   4.158 +    gpgme_error = gpgme_op_edit(session->ctx, key, renew_fsm, &handle,
   4.159              output);
   4.160      assert(gpgme_error == GPG_ERR_NO_ERROR);
   4.161  
     5.1 --- a/src/pgp_gpg.h	Mon Apr 20 15:59:13 2015 +0200
     5.2 +++ b/src/pgp_gpg.h	Mon Apr 20 17:54:57 2015 +0200
     5.3 @@ -45,6 +45,12 @@
     5.4  
     5.5  PEP_STATUS pgp_recv_key(PEP_SESSION session, const char *pattern);
     5.6  PEP_STATUS pgp_send_key(PEP_SESSION session, const char *pattern);
     5.7 -PEP_STATUS pgp_renew_key(PEP_SESSION session, const char *fpr);
     5.8 +
     5.9 +PEP_STATUS pgp_renew_key(
    5.10 +        PEP_SESSION session,
    5.11 +        const char *fpr,
    5.12 +        const timestamp *ts
    5.13 +    );
    5.14 +
    5.15  PEP_STATUS pgp_revoke_key(PEP_SESSION session, const char *fpr);
    5.16