attempting changes for keygen w/ 2.1 gnupg-2.1
authorKrista Bennett <krista@pep-project.org>
Wed, 12 Jul 2017 14:01:26 +0200
branchgnupg-2.1
changeset 1914e40bb1f761fe
parent 1913 57585bd12bd1
child 1920 c7e8570c9d25
attempting changes for keygen w/ 2.1
src/pEpEngine.h
src/pgp_gpg.c
src/pgp_gpg_internal.h
     1.1 --- a/src/pEpEngine.h	Mon Jul 10 18:28:01 2017 +0200
     1.2 +++ b/src/pEpEngine.h	Wed Jul 12 14:01:26 2017 +0200
     1.3 @@ -105,7 +105,9 @@
     1.4      PEP_ILLEGAL_VALUE                               = -4,
     1.5      PEP_BUFFER_TOO_SMALL                            = -3,
     1.6      PEP_OUT_OF_MEMORY                               = -2,
     1.7 -    PEP_UNKNOWN_ERROR                               = -1
     1.8 +    PEP_UNKNOWN_ERROR                               = -1,
     1.9 +    
    1.10 +    PEP_VERSION_MISMATCH                            = -7,
    1.11  } PEP_STATUS;
    1.12  
    1.13  
     2.1 --- a/src/pgp_gpg.c	Mon Jul 10 18:28:01 2017 +0200
     2.2 +++ b/src/pgp_gpg.c	Wed Jul 12 14:01:26 2017 +0200
     2.3 @@ -1060,62 +1060,102 @@
     2.4          psize, ctext, csize, true);
     2.5  }
     2.6  
     2.7 +static PEP_STATUS _pgp_createkey(PEP_SESSION session, pEp_identity *identity) {
     2.8 +    PEP_STATUS status = PEP_VERSION_MISMATCH;
     2.9 +
    2.10 +    if (identity && identity->address) {
    2.11 +    
    2.12 +#ifdef GPGME_VERSION_NUMBER 
    2.13 +#if (GPGME_VERSION_NUMBER >= 0x010700)
    2.14 +    gpgme_error_t gpgme_error;
    2.15 +    gpgme_error = gpg.gpgme_op_createkey(session->ctx, identity->address, "RSA", 
    2.16 +                                         0, 31536000, NULL, GPGME_CREATE_NOPASSWD);
    2.17 +    gpgme_error = _GPGERR(gpgme_error);
    2.18 +    if (gpgme_error != GPG_ERR_NOT_SUPPORTED) {
    2.19 +        switch (gpgme_error) {
    2.20 +        case GPG_ERR_NO_ERROR:
    2.21 +            break;
    2.22 +        case GPG_ERR_INV_VALUE:
    2.23 +            return PEP_ILLEGAL_VALUE;
    2.24 +        case GPG_ERR_GENERAL:
    2.25 +            return PEP_CANNOT_CREATE_KEY;
    2.26 +        default:
    2.27 +            assert(0);
    2.28 +            return PEP_UNKNOWN_ERROR;
    2.29 +        }        
    2.30 +    }
    2.31 +#endif
    2.32 +#endif
    2.33 +
    2.34 +    }
    2.35 +    
    2.36 +    return status;
    2.37 +}
    2.38 +
    2.39  PEP_STATUS pgp_generate_keypair(
    2.40      PEP_SESSION session, pEp_identity *identity
    2.41      )
    2.42  {
    2.43 -    gpgme_error_t gpgme_error;
    2.44 -    char *parms;
    2.45 -    const char *template =
    2.46 -        "<GnupgKeyParms format=\"internal\">\n"
    2.47 -        "Key-Type: RSA\n"
    2.48 -        "Key-Length: 4096\n"
    2.49 -        "Subkey-Type: RSA\n"
    2.50 -        "Subkey-Length: 4096\n"
    2.51 -        "Name-Real: %s\n"
    2.52 -        "Name-Email: %s\n"
    2.53 -        /* "Passphrase: %s\n" */
    2.54 -        "Expire-Date: 1y\n"
    2.55 -        "</GnupgKeyParms>\n";
    2.56 -    int result;
    2.57 -    gpgme_genkey_result_t gpgme_genkey_result;
    2.58 -
    2.59      assert(session);
    2.60      assert(identity);
    2.61      assert(identity->address);
    2.62      assert(identity->fpr == NULL || identity->fpr[0] == 0);
    2.63      assert(identity->username);
    2.64  
    2.65 -    parms = calloc(1, PARMS_MAX);
    2.66 -    assert(parms);
    2.67 -    if (parms == NULL)
    2.68 -        return PEP_OUT_OF_MEMORY;
    2.69 +    PEP_STATUS status = _pgp_createkey(session, identity);
    2.70 +    
    2.71 +    if (status != PEP_STATUS_OK || 
    2.72 +        status != PEP_VERSION_MISMATCH)
    2.73 +        return status;
    2.74  
    2.75 -    result = snprintf(parms, PARMS_MAX, template, identity->username,
    2.76 -        identity->address); // , session->passphrase);
    2.77 -    assert(result < PARMS_MAX);
    2.78 -    if (result >= PARMS_MAX) {
    2.79 +    if (status == PEP_VERSION_MISMATCH) {
    2.80 +        gpgme_error_t gpgme_error;
    2.81 +        char *parms;
    2.82 +        const char *template =
    2.83 +            "<GnupgKeyParms format=\"internal\">\n"
    2.84 +            "Key-Type: RSA\n"
    2.85 +            "Key-Length: 4096\n"
    2.86 +            "Subkey-Type: RSA\n"
    2.87 +            "Subkey-Length: 4096\n"
    2.88 +            "Name-Real: %s\n"
    2.89 +            "Name-Email: %s\n"
    2.90 +            /* "Passphrase: %s\n" */
    2.91 +            "Expire-Date: 1y\n"
    2.92 +            "</GnupgKeyParms>\n";
    2.93 +        int result;
    2.94 +    
    2.95 +        parms = calloc(1, PARMS_MAX);
    2.96 +        assert(parms);
    2.97 +        if (parms == NULL)
    2.98 +            return PEP_OUT_OF_MEMORY;
    2.99 +
   2.100 +        result = snprintf(parms, PARMS_MAX, template, identity->username,
   2.101 +            identity->address); // , session->passphrase);
   2.102 +        assert(result < PARMS_MAX);
   2.103 +        if (result >= PARMS_MAX) {
   2.104 +            free(parms);
   2.105 +            return PEP_BUFFER_TOO_SMALL;
   2.106 +        }
   2.107 +
   2.108 +        gpgme_error = gpg.gpgme_op_genkey(session->ctx, parms, NULL, NULL);
   2.109 +        gpgme_error = _GPGERR(gpgme_error);
   2.110          free(parms);
   2.111 -        return PEP_BUFFER_TOO_SMALL;
   2.112 +
   2.113 +        switch (gpgme_error) {
   2.114 +        case GPG_ERR_NO_ERROR:
   2.115 +            break;
   2.116 +        case GPG_ERR_INV_VALUE:
   2.117 +            return PEP_ILLEGAL_VALUE;
   2.118 +        case GPG_ERR_GENERAL:
   2.119 +            return PEP_CANNOT_CREATE_KEY;
   2.120 +        default:
   2.121 +            assert(0);
   2.122 +            return PEP_UNKNOWN_ERROR;
   2.123 +        }
   2.124      }
   2.125  
   2.126 -    gpgme_error = gpg.gpgme_op_genkey(session->ctx, parms, NULL, NULL);
   2.127 -    gpgme_error = _GPGERR(gpgme_error);
   2.128 -    free(parms);
   2.129 -
   2.130 -    switch (gpgme_error) {
   2.131 -    case GPG_ERR_NO_ERROR:
   2.132 -        break;
   2.133 -    case GPG_ERR_INV_VALUE:
   2.134 -        return PEP_ILLEGAL_VALUE;
   2.135 -    case GPG_ERR_GENERAL:
   2.136 -        return PEP_CANNOT_CREATE_KEY;
   2.137 -    default:
   2.138 -        assert(0);
   2.139 -        return PEP_UNKNOWN_ERROR;
   2.140 -    }
   2.141 -
   2.142 -    gpgme_genkey_result = gpg.gpgme_op_genkey_result(session->ctx);
   2.143 +    /* This is the same regardless of whether we got it from genkey or createkey */
   2.144 +    gpgme_genkey_result_t gpgme_genkey_result = gpg.gpgme_op_genkey_result(session->ctx);
   2.145      assert(gpgme_genkey_result);
   2.146      assert(gpgme_genkey_result->fpr);
   2.147  
     3.1 --- a/src/pgp_gpg_internal.h	Mon Jul 10 18:28:01 2017 +0200
     3.2 +++ b/src/pgp_gpg_internal.h	Wed Jul 12 14:01:26 2017 +0200
     3.3 @@ -82,6 +82,15 @@
     3.4  typedef gpgme_ssize_t (*gpgme_io_write_t)(int fd, const void *buffer,
     3.5          size_t count);
     3.6  
     3.7 +#ifdef GPGME_VERSION_NUMBER 
     3.8 +#if (GPGME_VERSION_NUMBER >= 0x010700)
     3.9 +typedef gpgme_error_t(*gpgme_op_createkey_t)(gpgme_ctx_t CTX, 
    3.10 +    const char *USERID, const char *ALGO, unsigned long RESERVED, 
    3.11 +    unsigned long EXPIRES, gpgme_key_t EXTRAKEY, unsigned int FLAGS);
    3.12 +#endif
    3.13 +#endif
    3.14 +
    3.15 +
    3.16  struct gpg_s {
    3.17      const char * version;
    3.18      gpgme_check_version_t gpgme_check;
    3.19 @@ -113,6 +122,11 @@
    3.20      gpgme_get_key_t gpgme_get_key;
    3.21      gpgme_op_genkey_t gpgme_op_genkey;
    3.22      gpgme_op_genkey_result_t gpgme_op_genkey_result;
    3.23 +#ifdef GPGME_VERSION_NUMBER 
    3.24 +#if (GPGME_VERSION_NUMBER >= 0x010700)    
    3.25 +    gpgme_op_createkey_t gpgme_op_createkey;
    3.26 +#endif
    3.27 +#endif    
    3.28      gpgme_op_delete_t gpgme_op_delete;
    3.29      gpgme_op_import_t gpgme_op_import;
    3.30      gpgme_op_import_result_t gpgme_op_import_result;