src/pEpEngine.c
changeset 24 50887c6ab78f
parent 20 63ff31d0cfbd
child 26 843e629627bf
     1.1 --- a/src/pEpEngine.c	Mon Sep 01 13:02:23 2014 +0200
     1.2 +++ b/src/pEpEngine.c	Mon Sep 01 17:44:19 2014 +0200
     1.3 @@ -1,216 +1,13 @@
     1.4 -#define PEP_ENGINE_VERSION "0.4.0"
     1.5 -
     1.6 -// this is 20 safewords with 79 chars max
     1.7 -#define MAX_SAFEWORDS_SPACE (20 * 80)
     1.8 -
     1.9 -// XML parameters string
    1.10 -#define PARMS_MAX 32768
    1.11 -
    1.12 -// maximum busy wait time in ms
    1.13 -#define BUSY_WAIT_TIME 5000
    1.14 -
    1.15 -// maximum line length for reading gpg.conf
    1.16 -#define MAX_LINELENGTH 1024
    1.17 -
    1.18 -// default keyserver
    1.19 -#define DEFAULT_KEYSERVER "hkp://keys.gnupg.net"
    1.20 -
    1.21 -#ifdef WIN32
    1.22 -#include "platform_windows.h"
    1.23 -#define LOCAL_DB windoze_local_db()
    1.24 -#define SYSTEM_DB windoze_system_db()
    1.25 -#define LIBGPGME "libgpgme-11.dll"
    1.26 -#else // UNIX
    1.27 -#define _POSIX_C_SOURCE 200809L
    1.28 -#include <dlfcn.h>
    1.29 -#include "platform_unix.h"
    1.30 -#define LOCAL_DB unix_local_db()
    1.31 -#ifndef SYSTEM_DB
    1.32 -#define SYSTEM_DB "/usr/share/pEp/system.db"
    1.33 +#include "pEp_internal.h"
    1.34 +#ifndef NO_GPG
    1.35 +#include "pgp_gpg.h"
    1.36 +#else
    1.37 +#include "pgp_netpgp.h"
    1.38  #endif
    1.39 -#ifndef LIBGPGME
    1.40 -#define LIBGPGME "libgpgme-pthread.so"
    1.41 -#endif
    1.42 -#endif
    1.43 -
    1.44 -#include <locale.h>
    1.45 -#include <stdlib.h>
    1.46 -#include <string.h>
    1.47 -#include <assert.h>
    1.48 -#include <stdio.h>
    1.49 -
    1.50 -#ifndef NDEBUG
    1.51 -#include <stdio.h>
    1.52 -#endif
    1.53 -
    1.54 -#include <gpgme.h>
    1.55 -#include "sqlite3.h"
    1.56 -
    1.57 -#define _EXPORT_PEP_ENGINE_DLL
    1.58 -#include "pEpEngine.h"
    1.59 -
    1.60 -#define NOT_IMPLEMENTED assert(0)
    1.61 -
    1.62 -// init
    1.63 -
    1.64 -typedef const char * (*gpgme_check_version_t)(const char*);
    1.65 -typedef gpgme_error_t (*gpgme_set_locale_t)(gpgme_ctx_t CTX, int CATEGORY,
    1.66 -        const char *VALUE);
    1.67 -typedef gpgme_error_t (*gpgme_new_t)(gpgme_ctx_t *CTX);
    1.68 -typedef void (*gpgme_release_t)(gpgme_ctx_t CTX);
    1.69 -typedef gpgme_error_t (*gpgme_set_protocol_t)(gpgme_ctx_t CTX,
    1.70 -        gpgme_protocol_t PROTO);
    1.71 -typedef void (*gpgme_set_armor_t)(gpgme_ctx_t CTX, int YES);
    1.72 -
    1.73 -// data
    1.74 -
    1.75 -typedef gpgme_error_t (*gpgme_data_new_t)(gpgme_data_t *DH);
    1.76 -typedef gpgme_error_t (*gpgme_data_new_from_mem_t)(gpgme_data_t *DH,
    1.77 -        const char *BUFFER, size_t SIZE, int COPY);
    1.78 -typedef void (*gpgme_data_release_t)(gpgme_data_t DH);
    1.79 -typedef gpgme_data_type_t (*gpgme_data_identify_t)(gpgme_data_t DH);
    1.80 -typedef size_t (*gpgme_data_seek_t)(gpgme_data_t DH, size_t OFFSET,
    1.81 -        int WHENCE);
    1.82 -typedef size_t (*gpgme_data_read_t)(gpgme_data_t DH, void *BUFFER,
    1.83 -        size_t LENGTH);
    1.84 -
    1.85 -// encrypt and decrypt
    1.86 -
    1.87 -typedef gpgme_error_t (*gpgme_op_decrypt_t)(gpgme_ctx_t CTX,
    1.88 -        gpgme_data_t CIPHER, gpgme_data_t PLAIN);
    1.89 -typedef gpgme_error_t (*gpgme_op_verify_t)(gpgme_ctx_t CTX, gpgme_data_t SIG,
    1.90 -        gpgme_data_t SIGNED_TEXT, gpgme_data_t PLAIN);
    1.91 -typedef gpgme_error_t (*gpgme_op_decrypt_verify_t)(gpgme_ctx_t CTX,
    1.92 -        gpgme_data_t CIPHER, gpgme_data_t PLAIN);
    1.93 -typedef gpgme_decrypt_result_t (*gpgme_op_decrypt_result_t)(gpgme_ctx_t CTX);
    1.94 -typedef gpgme_error_t (*gpgme_op_encrypt_sign_t)(gpgme_ctx_t CTX,
    1.95 -        gpgme_key_t RECP[], gpgme_encrypt_flags_t FLAGS, gpgme_data_t PLAIN,
    1.96 -        gpgme_data_t CIPHER);
    1.97 -typedef gpgme_verify_result_t (*gpgme_op_verify_result_t)(gpgme_ctx_t CTX);
    1.98 -typedef void (*gpgme_signers_clear_t)(gpgme_ctx_t CTX);
    1.99 -typedef gpgme_error_t (*gpgme_signers_add_t)(gpgme_ctx_t CTX, const gpgme_key_t KEY);
   1.100 -
   1.101 -// keys
   1.102 -
   1.103 -typedef gpgme_error_t (*gpgme_get_key_t)(gpgme_ctx_t CTX, const char *FPR,
   1.104 -        gpgme_key_t *R_KEY, int SECRET);
   1.105 -typedef gpgme_error_t (*gpgme_op_genkey_t)(gpgme_ctx_t CTX, const char *PARMS,
   1.106 -        gpgme_data_t PUBLIC, gpgme_data_t SECRET);
   1.107 -typedef gpgme_genkey_result_t (*gpgme_op_genkey_result_t)(gpgme_ctx_t CTX);
   1.108 -typedef gpgme_error_t (*gpgme_op_delete_t)(gpgme_ctx_t CTX,
   1.109 -        const gpgme_key_t KEY, int ALLOW_SECRET);
   1.110 -typedef gpgme_error_t (*gpgme_op_import_t)(gpgme_ctx_t CTX,
   1.111 -        gpgme_data_t KEYDATA);
   1.112 -typedef gpgme_error_t (*gpgme_op_export_t)(gpgme_ctx_t CTX,
   1.113 -        const char *PATTERN, gpgme_export_mode_t MODE, gpgme_data_t KEYDATA);
   1.114 -typedef gpgme_error_t (*gpgme_set_keylist_mode_t)(gpgme_ctx_t CTX,
   1.115 -        gpgme_keylist_mode_t MODE);
   1.116 -typedef gpgme_keylist_mode_t (*gpgme_get_keylist_mode_t)(gpgme_ctx_t CTX);
   1.117 -typedef gpgme_error_t (*gpgme_op_keylist_start_t)(gpgme_ctx_t CTX,
   1.118 -        const char *PATTERN, int SECRET_ONLY);
   1.119 -typedef gpgme_error_t (*gpgme_op_keylist_next_t)(gpgme_ctx_t CTX,
   1.120 -        gpgme_key_t *R_KEY);
   1.121 -typedef gpgme_error_t (*gpgme_op_keylist_end_t)(gpgme_ctx_t CTX);
   1.122 -typedef gpgme_error_t (*gpgme_op_import_keys_t)(gpgme_ctx_t CTX,
   1.123 -        gpgme_key_t *KEYS);
   1.124 -typedef void (*gpgme_key_ref_t)(gpgme_key_t KEY);
   1.125 -typedef void (*gpgme_key_unref_t)(gpgme_key_t KEY);
   1.126 -
   1.127 -typedef struct {
   1.128 -	const char *version;
   1.129 -    const char *passphrase;
   1.130 -	void * gpgme;
   1.131 -	gpgme_ctx_t ctx;
   1.132 -
   1.133 -	sqlite3 *db;
   1.134 -	sqlite3 *system_db;
   1.135 -
   1.136 -	sqlite3_stmt *log;
   1.137 -	sqlite3_stmt *safeword;
   1.138 -	sqlite3_stmt *get_identity;
   1.139 -	sqlite3_stmt *set_person;
   1.140 -	sqlite3_stmt *set_pgp_keypair;
   1.141 -	sqlite3_stmt *set_identity;
   1.142 -	sqlite3_stmt *set_trust;
   1.143 -    sqlite3_stmt *get_trust;
   1.144 -
   1.145 -	gpgme_check_version_t gpgme_check;
   1.146 -	gpgme_set_locale_t gpgme_set_locale;
   1.147 -	gpgme_new_t gpgme_new;
   1.148 -	gpgme_release_t gpgme_release;
   1.149 -	gpgme_set_protocol_t gpgme_set_protocol;
   1.150 -	gpgme_set_armor_t gpgme_set_armor;
   1.151 -
   1.152 -	gpgme_data_new_t gpgme_data_new;
   1.153 -	gpgme_data_new_from_mem_t gpgme_data_new_from_mem;
   1.154 -	gpgme_data_release_t gpgme_data_release;
   1.155 -	gpgme_data_identify_t gpgme_data_identify;
   1.156 -	gpgme_data_seek_t gpgme_data_seek;
   1.157 -	gpgme_data_read_t gpgme_data_read;
   1.158 -
   1.159 -	gpgme_op_decrypt_t gpgme_op_decrypt;
   1.160 -	gpgme_op_verify_t gpgme_op_verify;
   1.161 -	gpgme_op_decrypt_verify_t gpgme_op_decrypt_verify;
   1.162 -	gpgme_op_decrypt_result_t gpgme_op_decrypt_result;
   1.163 -	gpgme_op_encrypt_sign_t gpgme_op_encrypt_sign;
   1.164 -	gpgme_op_verify_result_t gpgme_op_verify_result;
   1.165 -    gpgme_signers_clear_t gpgme_signers_clear;
   1.166 -    gpgme_signers_add_t gpgme_signers_add;
   1.167 -
   1.168 -	gpgme_get_key_t gpgme_get_key;
   1.169 -	gpgme_op_genkey_t gpgme_op_genkey;
   1.170 -    gpgme_op_genkey_result_t gpgme_op_genkey_result;
   1.171 -    gpgme_op_delete_t gpgme_op_delete;
   1.172 -    gpgme_op_import_t gpgme_op_import;
   1.173 -    gpgme_op_export_t gpgme_op_export;
   1.174 -    gpgme_set_keylist_mode_t gpgme_set_keylist_mode;
   1.175 -    gpgme_get_keylist_mode_t gpgme_get_keylist_mode;
   1.176 -    gpgme_op_keylist_start_t gpgme_op_keylist_start;
   1.177 -    gpgme_op_keylist_next_t gpgme_op_keylist_next;
   1.178 -    gpgme_op_keylist_end_t gpgme_op_keylist_end;
   1.179 -    gpgme_op_import_keys_t gpgme_op_import_keys;
   1.180 -    gpgme_key_ref_t gpgme_key_ref;
   1.181 -    gpgme_key_unref_t gpgme_key_unref;
   1.182 -} pEpSession;
   1.183 -
   1.184 -static bool ensure_keyserver()
   1.185 -{
   1.186 -    static char buf[MAX_LINELENGTH];
   1.187 -    int n;
   1.188 -    FILE *f = fopen(gpg_conf(), "r");
   1.189 -
   1.190 -    if (f != NULL) {
   1.191 -        while (!feof(f)) {
   1.192 -            char * s = fgets(buf, MAX_LINELENGTH, f);
   1.193 -            if (s && !feof(f)) {
   1.194 -                char * t = strtok(s, " ");
   1.195 -                if (t && strcmp(t, "keyserver") == 0)
   1.196 -                {
   1.197 -                    fclose(f);
   1.198 -                    return true;
   1.199 -                }
   1.200 -            }
   1.201 -        }
   1.202 -        f = freopen(gpg_conf(), "a", f);
   1.203 -    }
   1.204 -    else {
   1.205 -        f = fopen(gpg_conf(), "w");
   1.206 -    }
   1.207 -
   1.208 -    assert(f);
   1.209 -    if (f == NULL)
   1.210 -        return false;
   1.211 -
   1.212 -    n = fprintf(f, "keyserver %s\n", DEFAULT_KEYSERVER);
   1.213 -    assert(n >= 0);
   1.214 -    fclose(f);
   1.215 -
   1.216 -    return true;
   1.217 -}
   1.218  
   1.219  DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
   1.220  {
   1.221 -	gpgme_error_t gpgme_error;
   1.222 +    PEP_STATUS status_result;
   1.223  	int int_result;
   1.224  	const char *sql_log;
   1.225  	const char *sql_safeword;
   1.226 @@ -221,8 +18,6 @@
   1.227  	const char *sql_set_trust;
   1.228      const char *sql_get_trust;
   1.229  
   1.230 -    bool bResult;
   1.231 -
   1.232  	assert(sqlite3_threadsafe());
   1.233  	if (!sqlite3_threadsafe())
   1.234  		return PEP_INIT_SQLITE3_WITHOUT_MUTEX;
   1.235 @@ -237,195 +32,16 @@
   1.236  	
   1.237  	_session->version = PEP_ENGINE_VERSION;
   1.238  
   1.239 -    bResult = ensure_keyserver();
   1.240 -    assert(bResult);
   1.241 -
   1.242 -    // to do: implement something useful
   1.243 -    _session->passphrase = "";
   1.244 -
   1.245 -	_session->gpgme = dlopen(LIBGPGME, RTLD_LAZY);
   1.246 -	if (_session->gpgme == NULL) {
   1.247 -		free(_session);
   1.248 -		return PEP_INIT_CANNOT_LOAD_GPGME;
   1.249 -	}
   1.250 -
   1.251 -	_session->gpgme_set_locale
   1.252 -        = (gpgme_set_locale_t) (intptr_t) dlsym(_session->gpgme,
   1.253 -                "gpgme_set_locale");
   1.254 -	assert(_session->gpgme_set_locale);
   1.255 -
   1.256 -	_session->gpgme_check
   1.257 -        = (gpgme_check_version_t) (intptr_t) dlsym(_session->gpgme,
   1.258 -                "gpgme_check_version");
   1.259 -	assert(_session->gpgme_check);
   1.260 -
   1.261 -	_session->gpgme_new
   1.262 -        = (gpgme_new_t) (intptr_t) dlsym(_session->gpgme, "gpgme_new");
   1.263 -	assert(_session->gpgme_new);
   1.264 -
   1.265 -	_session->gpgme_release
   1.266 -        = (gpgme_release_t) (intptr_t) dlsym(_session->gpgme, "gpgme_release");
   1.267 -	assert(_session->gpgme_release);
   1.268 -
   1.269 -	_session->gpgme_set_protocol
   1.270 -        = (gpgme_set_protocol_t) (intptr_t) dlsym(_session->gpgme,
   1.271 -                "gpgme_set_protocol");
   1.272 -	assert(_session->gpgme_set_protocol);
   1.273 -
   1.274 -	_session->gpgme_set_armor
   1.275 -        = (gpgme_set_armor_t) (intptr_t) dlsym(_session->gpgme,
   1.276 -                "gpgme_set_armor");
   1.277 -	assert(_session->gpgme_set_armor);
   1.278 -
   1.279 -	_session->gpgme_data_new
   1.280 -        = (gpgme_data_new_t) (intptr_t) dlsym(_session->gpgme,
   1.281 -                "gpgme_data_new");
   1.282 -	assert(_session->gpgme_data_new);
   1.283 -
   1.284 -	_session->gpgme_data_new_from_mem
   1.285 -        = (gpgme_data_new_from_mem_t) (intptr_t) dlsym(_session->gpgme,
   1.286 -                "gpgme_data_new_from_mem");
   1.287 -	assert(_session->gpgme_data_new_from_mem);
   1.288 -
   1.289 -	_session->gpgme_data_release
   1.290 -        = (gpgme_data_release_t) (intptr_t) dlsym(_session->gpgme,
   1.291 -                "gpgme_data_release");
   1.292 -	assert(_session->gpgme_data_release);
   1.293 -
   1.294 -	_session->gpgme_data_identify
   1.295 -        = (gpgme_data_identify_t) (intptr_t) dlsym(_session->gpgme,
   1.296 -                "gpgme_data_identify");
   1.297 -	assert(_session->gpgme_data_identify);
   1.298 -
   1.299 -	_session->gpgme_data_seek
   1.300 -        = (gpgme_data_seek_t) (intptr_t) dlsym(_session->gpgme,
   1.301 -                "gpgme_data_seek");
   1.302 -	assert(_session->gpgme_data_seek);
   1.303 -
   1.304 -	_session->gpgme_data_read
   1.305 -        = (gpgme_data_read_t) (intptr_t) dlsym(_session->gpgme,
   1.306 -                "gpgme_data_read");
   1.307 -	assert(_session->gpgme_data_read);
   1.308 -
   1.309 -	_session->gpgme_op_decrypt
   1.310 -        = (gpgme_op_decrypt_t) (intptr_t) dlsym(_session->gpgme,
   1.311 -                "gpgme_op_decrypt");
   1.312 -	assert(_session->gpgme_op_decrypt);
   1.313 -
   1.314 -	_session->gpgme_op_verify
   1.315 -        = (gpgme_op_verify_t) (intptr_t) dlsym(_session->gpgme,
   1.316 -                "gpgme_op_verify");
   1.317 -	assert(_session->gpgme_op_verify);
   1.318 -
   1.319 -	_session->gpgme_op_decrypt_verify
   1.320 -        = (gpgme_op_decrypt_verify_t) (intptr_t) dlsym(_session->gpgme,
   1.321 -                "gpgme_op_decrypt_verify");
   1.322 -	assert(_session->gpgme_op_decrypt_verify);
   1.323 -
   1.324 -	_session->gpgme_op_decrypt_result
   1.325 -        = (gpgme_op_decrypt_result_t) (intptr_t) dlsym(_session->gpgme,
   1.326 -                "gpgme_op_decrypt_result");
   1.327 -	assert(_session->gpgme_op_decrypt_result);
   1.328 -
   1.329 -	_session->gpgme_op_encrypt_sign
   1.330 -        = (gpgme_op_encrypt_sign_t) (intptr_t) dlsym(_session->gpgme,
   1.331 -                "gpgme_op_encrypt_sign");
   1.332 -	assert(_session->gpgme_op_encrypt_sign);
   1.333 -
   1.334 -	_session->gpgme_op_verify_result
   1.335 -        = (gpgme_op_verify_result_t) (intptr_t) dlsym(_session->gpgme,
   1.336 -                "gpgme_op_verify_result");
   1.337 -	assert(_session->gpgme_op_verify_result);
   1.338 -
   1.339 -    _session->gpgme_signers_clear
   1.340 -        = (gpgme_signers_clear_t) (intptr_t) dlsym(_session->gpgme,
   1.341 -        "gpgme_signers_clear");
   1.342 -    assert(_session->gpgme_signers_clear);
   1.343 -
   1.344 -    _session->gpgme_signers_add
   1.345 -        = (gpgme_signers_add_t) (intptr_t) dlsym(_session->gpgme,
   1.346 -        "gpgme_signers_add");
   1.347 -    assert(_session->gpgme_signers_add);
   1.348 -
   1.349 -	_session->gpgme_get_key
   1.350 -        = (gpgme_get_key_t) (intptr_t) dlsym(_session->gpgme, "gpgme_get_key");
   1.351 -	assert(_session->gpgme_get_key);
   1.352 -
   1.353 -	_session->gpgme_op_genkey
   1.354 -        = (gpgme_op_genkey_t) (intptr_t) dlsym(_session->gpgme,
   1.355 -                "gpgme_op_genkey");
   1.356 -	assert(_session->gpgme_op_genkey);
   1.357 -
   1.358 -	_session->gpgme_op_genkey_result
   1.359 -        = (gpgme_op_genkey_result_t) (intptr_t) dlsym(_session->gpgme,
   1.360 -                "gpgme_op_genkey_result");
   1.361 -	assert(_session->gpgme_op_genkey_result);
   1.362 -
   1.363 -    _session->gpgme_op_delete = (gpgme_op_delete_t) (intptr_t)
   1.364 -        dlsym(_session->gpgme, "gpgme_op_delete");
   1.365 -	assert(_session->gpgme_op_delete);
   1.366 -
   1.367 -    _session->gpgme_op_import = (gpgme_op_import_t) (intptr_t)
   1.368 -        dlsym(_session->gpgme, "gpgme_op_import");
   1.369 -	assert(_session->gpgme_op_import);
   1.370 -
   1.371 -    _session->gpgme_op_export = (gpgme_op_export_t) (intptr_t)
   1.372 -        dlsym(_session->gpgme, "gpgme_op_export");
   1.373 -	assert(_session->gpgme_op_export);
   1.374 -
   1.375 -    _session->gpgme_set_keylist_mode = (gpgme_set_keylist_mode_t) (intptr_t)
   1.376 -        dlsym(_session->gpgme, "gpgme_set_keylist_mode");
   1.377 -	assert(_session->gpgme_set_keylist_mode);
   1.378 -
   1.379 -    _session->gpgme_get_keylist_mode = (gpgme_get_keylist_mode_t) (intptr_t)
   1.380 -        dlsym(_session->gpgme, "gpgme_get_keylist_mode");
   1.381 -	assert(_session->gpgme_get_keylist_mode);
   1.382 -
   1.383 -    _session->gpgme_op_keylist_start = (gpgme_op_keylist_start_t) (intptr_t)
   1.384 -        dlsym(_session->gpgme, "gpgme_op_keylist_start");
   1.385 -	assert(_session->gpgme_op_keylist_start);
   1.386 -
   1.387 -    _session->gpgme_op_keylist_next = (gpgme_op_keylist_next_t) (intptr_t)
   1.388 -        dlsym(_session->gpgme, "gpgme_op_keylist_next");
   1.389 -	assert(_session->gpgme_op_keylist_next);
   1.390 -
   1.391 -    _session->gpgme_op_keylist_end = (gpgme_op_keylist_end_t) (intptr_t)
   1.392 -        dlsym(_session->gpgme, "gpgme_op_keylist_end");
   1.393 -	assert(_session->gpgme_op_keylist_end);
   1.394 -
   1.395 -    _session->gpgme_op_import_keys = (gpgme_op_import_keys_t) (intptr_t)
   1.396 -        dlsym(_session->gpgme, "gpgme_op_import_keys");
   1.397 -	assert(_session->gpgme_op_import_keys);
   1.398 -
   1.399 -    _session->gpgme_key_ref = (gpgme_key_ref_t) (intptr_t)
   1.400 -        dlsym(_session->gpgme, "gpgme_key_ref");
   1.401 -	assert(_session->gpgme_key_ref);
   1.402 -
   1.403 -    _session->gpgme_key_unref = (gpgme_key_unref_t) (intptr_t)
   1.404 -        dlsym(_session->gpgme, "gpgme_key_unref");
   1.405 -	assert(_session->gpgme_key_unref);
   1.406 -
   1.407 -	setlocale(LC_ALL, "");
   1.408 -	_session->version = _session->gpgme_check(NULL);
   1.409 -	_session->gpgme_set_locale(NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
   1.410 -
   1.411 -	gpgme_error = _session->gpgme_new(&_session->ctx);
   1.412 -	if (gpgme_error != GPG_ERR_NO_ERROR) {
   1.413 -		dlclose(_session->gpgme);
   1.414 -		free(_session);
   1.415 -		return PEP_INIT_GPGME_INIT_FAILED;
   1.416 -	}
   1.417 -
   1.418 -    gpgme_error = _session->gpgme_set_protocol(_session->ctx,
   1.419 -            GPGME_PROTOCOL_OpenPGP);
   1.420 -	assert(gpgme_error == GPG_ERR_NO_ERROR);
   1.421 -
   1.422 -	_session->gpgme_set_armor(_session->ctx, 1);
   1.423 +    status_result = pgp_init(session);
   1.424 +    assert(status_result == PEP_STATUS_OK);
   1.425 +    if (status_result != PEP_STATUS_OK) {
   1.426 +        free(_session);
   1.427 +        return status_result;
   1.428 +    }
   1.429  
   1.430      assert(LOCAL_DB);
   1.431      if (LOCAL_DB == NULL) {
   1.432 -		_session->gpgme_release(_session->ctx);
   1.433 -		dlclose(_session->gpgme);
   1.434 +        pgp_release(session);
   1.435          free(_session);
   1.436          return PEP_INIT_CANNOT_OPEN_DB;
   1.437      }
   1.438 @@ -442,9 +58,8 @@
   1.439  
   1.440  	if (int_result != SQLITE_OK) {
   1.441  		sqlite3_close_v2(_session->db);
   1.442 -		_session->gpgme_release(_session->ctx);
   1.443 -		dlclose(_session->gpgme);
   1.444 -		free(_session);
   1.445 +        pgp_release(session);
   1.446 +        free(_session);
   1.447  		return PEP_INIT_CANNOT_OPEN_DB;
   1.448  	}
   1.449  
   1.450 @@ -453,9 +68,8 @@
   1.451      assert(SYSTEM_DB);
   1.452      if (SYSTEM_DB == NULL) {
   1.453  		sqlite3_close_v2(_session->db);
   1.454 -		_session->gpgme_release(_session->ctx);
   1.455 -		dlclose(_session->gpgme);
   1.456 -		free(_session);
   1.457 +        pgp_release(session);
   1.458 +        free(_session);
   1.459  		return PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
   1.460      }
   1.461  
   1.462 @@ -470,9 +84,8 @@
   1.463  	if (int_result != SQLITE_OK) {
   1.464  		sqlite3_close_v2(_session->system_db);
   1.465  		sqlite3_close_v2(_session->db);
   1.466 -		_session->gpgme_release(_session->ctx);
   1.467 -		dlclose(_session->gpgme);
   1.468 -		free(_session);
   1.469 +        pgp_release(session);
   1.470 +        free(_session);
   1.471  		return PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
   1.472  	}
   1.473  
   1.474 @@ -634,10 +247,8 @@
   1.475  			sqlite3_close_v2(_session->db);
   1.476  			sqlite3_close_v2(_session->system_db);
   1.477  		}
   1.478 -		if (_session->ctx)
   1.479 -			_session->gpgme_release(_session->ctx);
   1.480 -		dlclose(_session->gpgme);
   1.481 -	}
   1.482 +        pgp_release(session);
   1.483 +    }
   1.484  	free(_session);
   1.485  }
   1.486  
   1.487 @@ -706,471 +317,6 @@
   1.488      }
   1.489  }
   1.490  
   1.491 -DYNAMIC_API PEP_STATUS decrypt_and_verify(
   1.492 -        PEP_SESSION session, const char *ctext, size_t csize,
   1.493 -        char **ptext, size_t *psize, stringlist_t **keylist
   1.494 -    )
   1.495 -{
   1.496 -	pEpSession *_session = (pEpSession *) session;
   1.497 -
   1.498 -	PEP_STATUS result;
   1.499 -	gpgme_error_t gpgme_error;
   1.500 -	gpgme_data_t cipher, plain;
   1.501 -	gpgme_data_type_t dt;
   1.502 -
   1.503 -	stringlist_t *_keylist = NULL;
   1.504 -	int i_key = 0;
   1.505 -
   1.506 -	assert(_session);
   1.507 -	assert(ctext);
   1.508 -	assert(csize);
   1.509 -	assert(ptext);
   1.510 -	assert(psize);
   1.511 -	assert(keylist);
   1.512 -
   1.513 -	*ptext = NULL;
   1.514 -	*psize = 0;
   1.515 -	*keylist = NULL;
   1.516 -
   1.517 -    gpgme_error = _session->gpgme_data_new_from_mem(&cipher, ctext, csize, 0);
   1.518 -	assert(gpgme_error == GPG_ERR_NO_ERROR);
   1.519 -	if (gpgme_error != GPG_ERR_NO_ERROR) {
   1.520 -		if (gpgme_error == GPG_ERR_ENOMEM)
   1.521 -			return PEP_OUT_OF_MEMORY;
   1.522 -		else
   1.523 -			return PEP_UNKNOWN_ERROR;
   1.524 -	}
   1.525 -
   1.526 -	gpgme_error = _session->gpgme_data_new(&plain);
   1.527 -	assert(gpgme_error == GPG_ERR_NO_ERROR);
   1.528 -	if (gpgme_error != GPG_ERR_NO_ERROR) {
   1.529 -		_session->gpgme_data_release(cipher);
   1.530 -		if (gpgme_error == GPG_ERR_ENOMEM)
   1.531 -			return PEP_OUT_OF_MEMORY;
   1.532 -		else
   1.533 -			return PEP_UNKNOWN_ERROR;
   1.534 -	}
   1.535 -
   1.536 -	dt = _session->gpgme_data_identify(cipher);
   1.537 -	switch (dt) {
   1.538 -	case GPGME_DATA_TYPE_PGP_SIGNED:
   1.539 -	case GPGME_DATA_TYPE_PGP_OTHER:
   1.540 -        gpgme_error = _session->gpgme_op_decrypt_verify(_session->ctx, cipher,
   1.541 -                plain);
   1.542 -		assert(gpgme_error != GPG_ERR_INV_VALUE);
   1.543 -		assert(gpgme_error != GPG_ERR_NO_DATA);
   1.544 -
   1.545 -		switch (gpgme_error) {
   1.546 -		case GPG_ERR_NO_ERROR:
   1.547 -			{
   1.548 -                gpgme_verify_result_t gpgme_verify_result;
   1.549 -                char *_buffer = NULL;
   1.550 -				size_t reading;
   1.551 -                size_t length = _session->gpgme_data_seek(plain, 0, SEEK_END);
   1.552 -                gpgme_signature_t gpgme_signature;
   1.553 -
   1.554 -				assert(length != -1);
   1.555 -				_session->gpgme_data_seek(plain, 0, SEEK_SET);
   1.556 -
   1.557 -				// TODO: make things less memory consuming
   1.558 -                // the following algorithm allocates memory for the complete
   1.559 -                // text
   1.560 -
   1.561 -                _buffer = malloc(length + 1);
   1.562 -                assert(_buffer);
   1.563 -                if (_buffer == NULL) {
   1.564 -                    _session->gpgme_data_release(plain);
   1.565 -                    _session->gpgme_data_release(cipher);
   1.566 -                    return PEP_OUT_OF_MEMORY;
   1.567 -                }
   1.568 -
   1.569 -                reading = _session->gpgme_data_read(plain, _buffer, length);
   1.570 -				assert(length == reading);
   1.571 -
   1.572 -                gpgme_verify_result =
   1.573 -                    _session->gpgme_op_verify_result(_session->ctx);
   1.574 -				assert(gpgme_verify_result);
   1.575 -                gpgme_signature = gpgme_verify_result->signatures;
   1.576 -
   1.577 -				if (gpgme_signature) {
   1.578 -                    stringlist_t *k;
   1.579 -                    _keylist = new_stringlist(NULL);
   1.580 -                    assert(_keylist);
   1.581 -                    if (_keylist == NULL) {
   1.582 -						_session->gpgme_data_release(plain);
   1.583 -						_session->gpgme_data_release(cipher);
   1.584 -                        free(_buffer);
   1.585 -                        return PEP_OUT_OF_MEMORY;
   1.586 -                    }
   1.587 -                    k = _keylist;
   1.588 -
   1.589 -                    result = PEP_DECRYPTED_AND_VERIFIED;
   1.590 -					do {
   1.591 -                        switch (gpgme_signature->status) {
   1.592 -                        case GPG_ERR_NO_ERROR:
   1.593 -                            k = stringlist_add(k, gpgme_signature->fpr);
   1.594 -                            break;
   1.595 -                        case GPG_ERR_CERT_REVOKED:
   1.596 -                        case GPG_ERR_BAD_SIGNATURE:
   1.597 -                            result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
   1.598 -                            break;
   1.599 -                        case GPG_ERR_SIG_EXPIRED:
   1.600 -                        case GPG_ERR_KEY_EXPIRED:
   1.601 -                        case GPG_ERR_NO_PUBKEY:
   1.602 -                            k = stringlist_add(k, gpgme_signature->fpr);
   1.603 -                            if (result == PEP_DECRYPTED_AND_VERIFIED)
   1.604 -                                result = PEP_DECRYPTED;
   1.605 -                            break;
   1.606 -                        case GPG_ERR_GENERAL:
   1.607 -                            break;
   1.608 -                        default:
   1.609 -                            if (result == PEP_DECRYPTED_AND_VERIFIED)
   1.610 -                                result = PEP_DECRYPTED;
   1.611 -                            break;
   1.612 -                        }
   1.613 -					} while ((gpgme_signature = gpgme_signature->next));
   1.614 -				} else {
   1.615 -					result = PEP_DECRYPTED;
   1.616 -				}
   1.617 -
   1.618 -				if (result == PEP_DECRYPTED_AND_VERIFIED
   1.619 -                        || result == PEP_DECRYPTED) {
   1.620 -					*ptext = _buffer;
   1.621 -					*psize = reading;
   1.622 -                    (*ptext)[*psize] = 0; // safeguard for naive users
   1.623 -					*keylist = _keylist;
   1.624 -				}
   1.625 -                else {
   1.626 -                    free_stringlist(_keylist);
   1.627 -                    free(_buffer);
   1.628 -	            }
   1.629 -				break;
   1.630 -			}
   1.631 -		case GPG_ERR_DECRYPT_FAILED:
   1.632 -			result = PEP_DECRYPT_WRONG_FORMAT;
   1.633 -			break;
   1.634 -		case GPG_ERR_BAD_PASSPHRASE:
   1.635 -			NOT_IMPLEMENTED;
   1.636 -        default:
   1.637 -            {
   1.638 -                gpgme_decrypt_result_t gpgme_decrypt_result = _session->gpgme_op_decrypt_result(_session->ctx);
   1.639 -                result = PEP_DECRYPT_NO_KEY;
   1.640 -
   1.641 -                if (gpgme_decrypt_result != NULL) {
   1.642 -                    if (gpgme_decrypt_result->unsupported_algorithm)
   1.643 -                        *keylist = new_stringlist(gpgme_decrypt_result->unsupported_algorithm);
   1.644 -                    else
   1.645 -                        *keylist = new_stringlist("");
   1.646 -                    assert(*keylist);
   1.647 -                    if (*keylist == NULL) {
   1.648 -                        result = PEP_OUT_OF_MEMORY;
   1.649 -                        break;
   1.650 -                    }
   1.651 -                    stringlist_t *_keylist = *keylist;
   1.652 -                    for (gpgme_recipient_t r = gpgme_decrypt_result->recipients; r != NULL; r = r->next) {
   1.653 -                        _keylist = stringlist_add(_keylist, r->keyid);
   1.654 -                        assert(_keylist);
   1.655 -                        if (_keylist == NULL) {
   1.656 -                            free_stringlist(*keylist);
   1.657 -                            *keylist = NULL;
   1.658 -                            result = PEP_OUT_OF_MEMORY;
   1.659 -                            break;
   1.660 -                        }
   1.661 -                    }
   1.662 -                    if (result == PEP_OUT_OF_MEMORY)
   1.663 -                        break;
   1.664 -                }
   1.665 -            }
   1.666 -		}
   1.667 -		break;
   1.668 -
   1.669 -	default:
   1.670 -		result = PEP_DECRYPT_WRONG_FORMAT;
   1.671 -	}
   1.672 -
   1.673 -	_session->gpgme_data_release(plain);
   1.674 -	_session->gpgme_data_release(cipher);
   1.675 -	return result;
   1.676 -}
   1.677 -
   1.678 -DYNAMIC_API PEP_STATUS verify_text(
   1.679 -        PEP_SESSION session, const char *text, size_t size,
   1.680 -        const char *signature, size_t sig_size, stringlist_t **keylist
   1.681 -    )
   1.682 -{
   1.683 -	pEpSession *_session = (pEpSession *) session;
   1.684 -
   1.685 -	PEP_STATUS result;
   1.686 -	gpgme_error_t gpgme_error;
   1.687 -	gpgme_data_t d_text, d_sig;
   1.688 -    stringlist_t *_keylist;
   1.689 -
   1.690 -    assert(session);
   1.691 -    assert(text);
   1.692 -    assert(size);
   1.693 -    assert(signature);
   1.694 -    assert(sig_size);
   1.695 -    assert(keylist);
   1.696 -
   1.697 -    *keylist = NULL;
   1.698 -
   1.699 -    gpgme_error = _session->gpgme_data_new_from_mem(&d_text, text, size, 0);
   1.700 -	assert(gpgme_error == GPG_ERR_NO_ERROR);
   1.701 -	if (gpgme_error != GPG_ERR_NO_ERROR) {
   1.702 -		if (gpgme_error == GPG_ERR_ENOMEM)
   1.703 -			return PEP_OUT_OF_MEMORY;
   1.704 -		else
   1.705 -			return PEP_UNKNOWN_ERROR;
   1.706 -	}
   1.707 -
   1.708 -    gpgme_error = _session->gpgme_data_new_from_mem(&d_sig, signature, sig_size, 0);
   1.709 -	assert(gpgme_error == GPG_ERR_NO_ERROR);
   1.710 -	if (gpgme_error != GPG_ERR_NO_ERROR) {
   1.711 -		_session->gpgme_data_release(d_text);
   1.712 -		if (gpgme_error == GPG_ERR_ENOMEM)
   1.713 -			return PEP_OUT_OF_MEMORY;
   1.714 -		else
   1.715 -			return PEP_UNKNOWN_ERROR;
   1.716 -	}
   1.717 -
   1.718 -    gpgme_error = _session->gpgme_op_verify(_session->ctx, d_sig, d_text, NULL);
   1.719 -    assert(gpgme_error != GPG_ERR_INV_VALUE);
   1.720 -
   1.721 -    switch (gpgme_error) {
   1.722 -    case GPG_ERR_NO_ERROR:
   1.723 -        {
   1.724 -            gpgme_verify_result_t gpgme_verify_result;
   1.725 -            gpgme_signature_t gpgme_signature;
   1.726 -
   1.727 -            gpgme_verify_result =
   1.728 -                _session->gpgme_op_verify_result(_session->ctx);
   1.729 -            assert(gpgme_verify_result);
   1.730 -            gpgme_signature = gpgme_verify_result->signatures;
   1.731 -
   1.732 -            if (gpgme_signature) {
   1.733 -                stringlist_t *k;
   1.734 -                _keylist = new_stringlist(NULL);
   1.735 -                assert(_keylist);
   1.736 -                if (_keylist == NULL) {
   1.737 -                    _session->gpgme_data_release(d_text);
   1.738 -                    _session->gpgme_data_release(d_sig);
   1.739 -                    return PEP_OUT_OF_MEMORY;
   1.740 -                }
   1.741 -                k = _keylist;
   1.742 -
   1.743 -                result = PEP_VERIFIED;
   1.744 -                do {
   1.745 -                    k = stringlist_add(k, gpgme_signature->fpr);
   1.746 -                    if (k == NULL) {
   1.747 -                        free_stringlist(_keylist);
   1.748 -                        _session->gpgme_data_release(d_text);
   1.749 -                        _session->gpgme_data_release(d_sig);
   1.750 -                        return PEP_OUT_OF_MEMORY;
   1.751 -                    }
   1.752 -                    if (gpgme_signature->summary & GPGME_SIGSUM_RED) {
   1.753 -                        if (gpgme_signature->summary & GPGME_SIGSUM_KEY_EXPIRED
   1.754 -                                || gpgme_signature->summary & GPGME_SIGSUM_SIG_EXPIRED) {
   1.755 -                            if (result == PEP_VERIFIED
   1.756 -                                    || result == PEP_VERIFIED_AND_TRUSTED)
   1.757 -                                result = PEP_UNENCRYPTED;
   1.758 -                        }
   1.759 -                        else {
   1.760 -                            result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
   1.761 -                            break;
   1.762 -                        }
   1.763 -                    }
   1.764 -                    else {
   1.765 -                        if (gpgme_signature->summary & GPGME_SIGSUM_VALID) {
   1.766 -                            if (result == PEP_VERIFIED)
   1.767 -                                result = PEP_VERIFIED_AND_TRUSTED;
   1.768 -                        }
   1.769 -                        if (gpgme_signature->summary & GPGME_SIGSUM_GREEN) {
   1.770 -                            // good
   1.771 -                        }
   1.772 -                        else if (gpgme_signature->summary & GPGME_SIGSUM_KEY_MISSING) {
   1.773 -                            result = PEP_VERIFY_NO_KEY;
   1.774 -                        }
   1.775 -                        else if (gpgme_signature->summary & GPGME_SIGSUM_SYS_ERROR) {
   1.776 -                            if (result == PEP_VERIFIED
   1.777 -                                    || result == PEP_VERIFIED_AND_TRUSTED)
   1.778 -                                result = PEP_UNENCRYPTED;
   1.779 -                        }
   1.780 -                        else {
   1.781 -                            // do nothing
   1.782 -                        }
   1.783 -                    }
   1.784 -                } while ((gpgme_signature = gpgme_signature->next));
   1.785 -                *keylist = _keylist;
   1.786 -            } else {
   1.787 -                result = PEP_UNENCRYPTED;
   1.788 -            }
   1.789 -            break;
   1.790 -        }
   1.791 -        break;
   1.792 -    case GPG_ERR_NO_DATA:
   1.793 -        result = PEP_DECRYPT_WRONG_FORMAT;
   1.794 -        break;
   1.795 -    case GPG_ERR_INV_VALUE:
   1.796 -    default:
   1.797 -        result = PEP_UNKNOWN_ERROR;
   1.798 -        break;
   1.799 -    }
   1.800 -
   1.801 -    _session->gpgme_data_release(d_text);
   1.802 -    _session->gpgme_data_release(d_sig);
   1.803 -
   1.804 -    return result;
   1.805 -}
   1.806 -
   1.807 -DYNAMIC_API PEP_STATUS encrypt_and_sign(
   1.808 -        PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
   1.809 -        size_t psize, char **ctext, size_t *csize
   1.810 -    )
   1.811 -{
   1.812 -	pEpSession *_session = (pEpSession *) session;
   1.813 -
   1.814 -	PEP_STATUS result;
   1.815 -	gpgme_error_t gpgme_error;
   1.816 -	gpgme_data_t plain, cipher;
   1.817 -	gpgme_key_t *rcpt;
   1.818 -	gpgme_encrypt_flags_t flags;
   1.819 -	const stringlist_t *_keylist;
   1.820 -    int i, j;
   1.821 -
   1.822 -	assert(_session);
   1.823 -	assert(keylist);
   1.824 -	assert(ptext);
   1.825 -	assert(psize);
   1.826 -	assert(ctext);
   1.827 -	assert(csize);
   1.828 -
   1.829 -	*ctext = NULL;
   1.830 -	*csize = 0;
   1.831 -
   1.832 -    gpgme_error = _session->gpgme_data_new_from_mem(&plain, ptext, psize, 0);
   1.833 -	assert(gpgme_error == GPG_ERR_NO_ERROR);
   1.834 -	if (gpgme_error != GPG_ERR_NO_ERROR) {
   1.835 -		if (gpgme_error == GPG_ERR_ENOMEM)
   1.836 -			return PEP_OUT_OF_MEMORY;
   1.837 -		else
   1.838 -			return PEP_UNKNOWN_ERROR;
   1.839 -	}
   1.840 -
   1.841 -	gpgme_error = _session->gpgme_data_new(&cipher);
   1.842 -	assert(gpgme_error == GPG_ERR_NO_ERROR);
   1.843 -	if (gpgme_error != GPG_ERR_NO_ERROR) {
   1.844 -		_session->gpgme_data_release(plain);
   1.845 -		if (gpgme_error == GPG_ERR_ENOMEM)
   1.846 -			return PEP_OUT_OF_MEMORY;
   1.847 -		else
   1.848 -			return PEP_UNKNOWN_ERROR;
   1.849 -	}
   1.850 -
   1.851 -    rcpt = (gpgme_key_t *) calloc(stringlist_length(keylist) + 1,
   1.852 -            sizeof(gpgme_key_t));
   1.853 -	assert(rcpt);
   1.854 -	if (rcpt == NULL) {
   1.855 -		_session->gpgme_data_release(plain);
   1.856 -		_session->gpgme_data_release(cipher);
   1.857 -		return PEP_OUT_OF_MEMORY;
   1.858 -	}
   1.859 -
   1.860 -    _session->gpgme_signers_clear(_session->ctx);
   1.861 -
   1.862 -    for (_keylist=keylist, i=0; _keylist!=NULL; _keylist=_keylist->next, i++) {
   1.863 -		assert(_keylist->value);
   1.864 -        gpgme_error = _session->gpgme_get_key(_session->ctx, _keylist->value,
   1.865 -                &rcpt[i], 0);
   1.866 -		assert(gpgme_error != GPG_ERR_ENOMEM);
   1.867 -
   1.868 -		switch (gpgme_error) {
   1.869 -		case GPG_ERR_ENOMEM:
   1.870 -            for (j=0; j<i; j++)
   1.871 -                _session->gpgme_key_unref(rcpt[j]);
   1.872 -			free(rcpt);
   1.873 -			_session->gpgme_data_release(plain);
   1.874 -			_session->gpgme_data_release(cipher);
   1.875 -			return PEP_OUT_OF_MEMORY;
   1.876 -		case GPG_ERR_NO_ERROR:
   1.877 -            if (i == 0) {
   1.878 -                gpgme_error_t _gpgme_error = _session->gpgme_signers_add(_session->ctx, rcpt[0]);
   1.879 -                assert(_gpgme_error == GPG_ERR_NO_ERROR);
   1.880 -            }
   1.881 -			break;
   1.882 -		case GPG_ERR_EOF:
   1.883 -            for (j=0; j<i; j++)
   1.884 -                _session->gpgme_key_unref(rcpt[j]);
   1.885 -			free(rcpt);
   1.886 -			_session->gpgme_data_release(plain);
   1.887 -			_session->gpgme_data_release(cipher);
   1.888 -			return PEP_KEY_NOT_FOUND;
   1.889 -		case GPG_ERR_AMBIGUOUS_NAME:
   1.890 -            for (j=0; j<i; j++)
   1.891 -                _session->gpgme_key_unref(rcpt[j]);
   1.892 -			free(rcpt);
   1.893 -			_session->gpgme_data_release(plain);
   1.894 -			_session->gpgme_data_release(cipher);
   1.895 -			return PEP_KEY_HAS_AMBIG_NAME;
   1.896 -        default: // GPG_ERR_INV_VALUE if CTX or R_KEY is not a valid pointer or
   1.897 -                 // FPR is not a fingerprint or key ID
   1.898 -            for (j=0; j<i; j++)
   1.899 -                _session->gpgme_key_unref(rcpt[j]);
   1.900 -			free(rcpt);
   1.901 -			_session->gpgme_data_release(plain);
   1.902 -			_session->gpgme_data_release(cipher);
   1.903 -			return PEP_GET_KEY_FAILED;
   1.904 -		}
   1.905 -	}
   1.906 -
   1.907 -	// TODO: remove that and replace with proper key management
   1.908 -	flags  = GPGME_ENCRYPT_ALWAYS_TRUST;
   1.909 -
   1.910 -    gpgme_error = _session->gpgme_op_encrypt_sign(_session->ctx, rcpt, flags,
   1.911 -            plain, cipher);
   1.912 -	switch (gpgme_error) {
   1.913 -	case GPG_ERR_NO_ERROR:
   1.914 -		{
   1.915 -            char *_buffer = NULL;
   1.916 -			size_t reading;
   1.917 -            size_t length = _session->gpgme_data_seek(cipher, 0, SEEK_END);
   1.918 -            assert(length != -1);
   1.919 -			_session->gpgme_data_seek(cipher, 0, SEEK_SET);
   1.920 -
   1.921 -			// TODO: make things less memory consuming
   1.922 -            // the following algorithm allocates a buffer for the complete text
   1.923 -
   1.924 -            _buffer = (char *) malloc(length + 1);
   1.925 -            assert(_buffer);
   1.926 -            if (_buffer == NULL) {
   1.927 -                for (j=0; j<stringlist_length(keylist); j++)
   1.928 -                    _session->gpgme_key_unref(rcpt[j]);
   1.929 -                free(rcpt);
   1.930 -                _session->gpgme_data_release(plain);
   1.931 -                _session->gpgme_data_release(cipher);
   1.932 -                return PEP_OUT_OF_MEMORY;
   1.933 -            }
   1.934 -
   1.935 -            reading = _session->gpgme_data_read(cipher, _buffer, length);
   1.936 -			assert(length == reading);
   1.937 -
   1.938 -			*ctext = _buffer;
   1.939 -			*csize = reading;
   1.940 -			(*ctext)[*csize] = 0; // safeguard for naive users
   1.941 -			result = PEP_STATUS_OK;
   1.942 -			break;
   1.943 -		}
   1.944 -	default:
   1.945 -		result = PEP_UNKNOWN_ERROR;
   1.946 -	}
   1.947 -
   1.948 -    for (j=0; j<stringlist_length(keylist); j++)
   1.949 -        _session->gpgme_key_unref(rcpt[j]);
   1.950 -	free(rcpt);
   1.951 -	_session->gpgme_data_release(plain);
   1.952 -	_session->gpgme_data_release(cipher);
   1.953 -	return result;
   1.954 -}
   1.955 -
   1.956  DYNAMIC_API PEP_STATUS log_event(
   1.957          PEP_SESSION session, const char *title, const char *entity,
   1.958          const char *description, const char *comment
   1.959 @@ -1526,402 +672,6 @@
   1.960  		return PEP_COMMIT_FAILED;
   1.961  }
   1.962  
   1.963 -DYNAMIC_API PEP_STATUS generate_keypair(
   1.964 -        PEP_SESSION session, pEp_identity *identity
   1.965 -    )
   1.966 -{
   1.967 -	pEpSession *_session = (pEpSession *) session;
   1.968 -	gpgme_error_t gpgme_error;
   1.969 -    char *parms;
   1.970 -    const char *template =
   1.971 -        "<GnupgKeyParms format=\"internal\">\n"
   1.972 -        "Key-Type: RSA\n"
   1.973 -        "Key-Length: 4096\n"
   1.974 -        "Name-Real: %s\n"
   1.975 -        "Name-Email: %s\n"
   1.976 -        /* "Passphrase: %s\n" */
   1.977 -        "Expire-Date: 1y\n"
   1.978 -        "</GnupgKeyParms>\n";
   1.979 -    int result;
   1.980 -    gpgme_genkey_result_t gpgme_genkey_result;
   1.981 -
   1.982 -    assert(session);
   1.983 -    assert(identity);
   1.984 -    assert(identity->address);
   1.985 -    assert(identity->fpr == NULL);
   1.986 -    assert(identity->username);
   1.987 -    
   1.988 -    parms = calloc(1, PARMS_MAX);
   1.989 -    assert(parms);
   1.990 -    if (parms == NULL)
   1.991 -        return PEP_OUT_OF_MEMORY;
   1.992 -
   1.993 -    result = snprintf(parms, PARMS_MAX, template, identity->username,
   1.994 -            identity->address); // , _session->passphrase);
   1.995 -    assert(result < PARMS_MAX);
   1.996 -    if (result >= PARMS_MAX) {
   1.997 -        free(parms);
   1.998 -        return PEP_BUFFER_TOO_SMALL;
   1.999 -    }
  1.1000 -
  1.1001 -    gpgme_error = _session->gpgme_op_genkey(_session->ctx, parms, NULL, NULL);
  1.1002 -    free(parms);
  1.1003 -
  1.1004 -    switch (gpgme_error) {
  1.1005 -    case GPG_ERR_NO_ERROR:
  1.1006 -        break;
  1.1007 -    case GPG_ERR_INV_VALUE:
  1.1008 -        return PEP_ILLEGAL_VALUE;
  1.1009 -    case GPG_ERR_GENERAL:
  1.1010 -        return PEP_CANNOT_CREATE_KEY;
  1.1011 -    default:
  1.1012 -        assert(0);
  1.1013 -        return PEP_UNKNOWN_ERROR;
  1.1014 -    }
  1.1015 -
  1.1016 -    gpgme_genkey_result = _session->gpgme_op_genkey_result(_session->ctx);
  1.1017 -    assert(gpgme_genkey_result);
  1.1018 -    assert(gpgme_genkey_result->fpr);
  1.1019 -
  1.1020 -    identity->fpr = strdup(gpgme_genkey_result->fpr);
  1.1021 -
  1.1022 -    return PEP_STATUS_OK;
  1.1023 -}
  1.1024 -
  1.1025 -PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
  1.1026 -{
  1.1027 -	pEpSession *_session = (pEpSession *) session;
  1.1028 -	gpgme_error_t gpgme_error;
  1.1029 -    gpgme_key_t key;
  1.1030 -
  1.1031 -    assert(session);
  1.1032 -    assert(fpr);
  1.1033 -
  1.1034 -    gpgme_error = _session->gpgme_get_key(_session->ctx, fpr, &key, 0);
  1.1035 -    assert(gpgme_error != GPG_ERR_ENOMEM);
  1.1036 -    switch (gpgme_error) {
  1.1037 -    case GPG_ERR_NO_ERROR:
  1.1038 -        break;
  1.1039 -    case GPG_ERR_EOF:
  1.1040 -        return PEP_KEY_NOT_FOUND;
  1.1041 -    case GPG_ERR_INV_VALUE:
  1.1042 -        return PEP_ILLEGAL_VALUE;
  1.1043 -    case GPG_ERR_AMBIGUOUS_NAME:
  1.1044 -        return PEP_KEY_HAS_AMBIG_NAME;
  1.1045 -    case GPG_ERR_ENOMEM:
  1.1046 -        return PEP_OUT_OF_MEMORY;
  1.1047 -    default:
  1.1048 -        assert(0);
  1.1049 -        return PEP_UNKNOWN_ERROR;
  1.1050 -    }
  1.1051 -
  1.1052 -    gpgme_error = _session->gpgme_op_delete(_session->ctx, key, 1);
  1.1053 -    _session->gpgme_key_unref(key);
  1.1054 -    switch (gpgme_error) {
  1.1055 -    case GPG_ERR_NO_ERROR:
  1.1056 -        break;
  1.1057 -    case GPG_ERR_INV_VALUE:
  1.1058 -        assert(0);
  1.1059 -        return PEP_UNKNOWN_ERROR;
  1.1060 -    case GPG_ERR_NO_PUBKEY:
  1.1061 -        assert(0);
  1.1062 -        return PEP_KEY_NOT_FOUND;
  1.1063 -    case GPG_ERR_AMBIGUOUS_NAME:
  1.1064 -        assert(0);
  1.1065 -        return PEP_KEY_HAS_AMBIG_NAME;
  1.1066 -    default:
  1.1067 -        assert(0);
  1.1068 -        return PEP_UNKNOWN_ERROR;
  1.1069 -    }
  1.1070 -
  1.1071 -    return PEP_STATUS_OK;
  1.1072 -}
  1.1073 -
  1.1074 -PEP_STATUS import_key(PEP_SESSION session, const char *key_data, size_t size)
  1.1075 -{
  1.1076 -	pEpSession *_session = (pEpSession *) session;
  1.1077 -	gpgme_error_t gpgme_error;
  1.1078 -    gpgme_data_t dh;
  1.1079 -
  1.1080 -    assert(session);
  1.1081 -    assert(key_data);
  1.1082 -
  1.1083 -    gpgme_error = _session->gpgme_data_new_from_mem(&dh, key_data, size, 0);
  1.1084 -    assert(gpgme_error != GPG_ERR_ENOMEM);
  1.1085 -    switch (gpgme_error) {
  1.1086 -    case GPG_ERR_NO_ERROR:
  1.1087 -        break;
  1.1088 -    case GPG_ERR_ENOMEM:
  1.1089 -        return PEP_OUT_OF_MEMORY;
  1.1090 -    case GPG_ERR_INV_VALUE:
  1.1091 -        assert(0);
  1.1092 -        return PEP_UNKNOWN_ERROR;
  1.1093 -    default:
  1.1094 -        assert(0);
  1.1095 -        return PEP_UNKNOWN_ERROR;
  1.1096 -    }
  1.1097 -
  1.1098 -    gpgme_error = _session->gpgme_op_import(_session->ctx, dh);
  1.1099 -    switch (gpgme_error) {
  1.1100 -    case GPG_ERR_NO_ERROR:
  1.1101 -        break;
  1.1102 -    case GPG_ERR_INV_VALUE:
  1.1103 -        assert(0);
  1.1104 -        _session->gpgme_data_release(dh);
  1.1105 -        return PEP_UNKNOWN_ERROR;
  1.1106 -    case GPG_ERR_NO_DATA:
  1.1107 -        _session->gpgme_data_release(dh);
  1.1108 -        return PEP_ILLEGAL_VALUE;
  1.1109 -    default:
  1.1110 -        assert(0);
  1.1111 -        _session->gpgme_data_release(dh);
  1.1112 -        return PEP_UNKNOWN_ERROR;
  1.1113 -    }
  1.1114 -
  1.1115 -    _session->gpgme_data_release(dh);
  1.1116 -    return PEP_STATUS_OK;
  1.1117 -}
  1.1118 -
  1.1119 -PEP_STATUS export_key(
  1.1120 -        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
  1.1121 -    )
  1.1122 -{
  1.1123 -	pEpSession *_session = (pEpSession *) session;
  1.1124 -	gpgme_error_t gpgme_error;
  1.1125 -    gpgme_data_t dh;
  1.1126 -    size_t _size;
  1.1127 -    char *buffer;
  1.1128 -    int reading;
  1.1129 -
  1.1130 -    assert(session);
  1.1131 -    assert(fpr);
  1.1132 -    assert(key_data);
  1.1133 -    assert(size);
  1.1134 -
  1.1135 -    gpgme_error = _session->gpgme_data_new(&dh);
  1.1136 -    assert(gpgme_error != GPG_ERR_ENOMEM);
  1.1137 -    switch (gpgme_error) {
  1.1138 -    case GPG_ERR_NO_ERROR:
  1.1139 -        break;
  1.1140 -    case GPG_ERR_ENOMEM:
  1.1141 -        return PEP_OUT_OF_MEMORY;
  1.1142 -    case GPG_ERR_INV_VALUE:
  1.1143 -        assert(0);
  1.1144 -        return PEP_UNKNOWN_ERROR;
  1.1145 -    default:
  1.1146 -        assert(0);
  1.1147 -        return PEP_UNKNOWN_ERROR;
  1.1148 -    }
  1.1149 -
  1.1150 -    gpgme_error = _session->gpgme_op_export(_session->ctx, fpr,
  1.1151 -            GPGME_EXPORT_MODE_MINIMAL, dh);
  1.1152 -    switch (gpgme_error) {
  1.1153 -    case GPG_ERR_NO_ERROR:
  1.1154 -        break;
  1.1155 -    case GPG_ERR_EOF:
  1.1156 -        _session->gpgme_data_release(dh);
  1.1157 -        return PEP_KEY_NOT_FOUND;
  1.1158 -    case GPG_ERR_INV_VALUE:
  1.1159 -        assert(0);
  1.1160 -        _session->gpgme_data_release(dh);
  1.1161 -        return PEP_UNKNOWN_ERROR;
  1.1162 -    default:
  1.1163 -        assert(0);
  1.1164 -        _session->gpgme_data_release(dh);
  1.1165 -        return PEP_UNKNOWN_ERROR;
  1.1166 -    };
  1.1167 -
  1.1168 -    _size = _session->gpgme_data_seek(dh, 0, SEEK_END);
  1.1169 -    assert(_size != -1);
  1.1170 -    _session->gpgme_data_seek(dh, 0, SEEK_SET);
  1.1171 -
  1.1172 -    buffer = malloc(_size + 1);
  1.1173 -    assert(buffer);
  1.1174 -    if (buffer == NULL) {
  1.1175 -        _session->gpgme_data_release(dh);
  1.1176 -        return PEP_OUT_OF_MEMORY;
  1.1177 -    }
  1.1178 -
  1.1179 -    reading = _session->gpgme_data_read(dh, buffer, _size);
  1.1180 -    assert(_size == reading);
  1.1181 -
  1.1182 -    // safeguard for the naive user
  1.1183 -    buffer[_size] = 0;
  1.1184 -
  1.1185 -    *key_data = buffer;
  1.1186 -    *size = _size;
  1.1187 -
  1.1188 -    _session->gpgme_data_release(dh);
  1.1189 -    return PEP_STATUS_OK;
  1.1190 -}
  1.1191 -
  1.1192 -static void _switch_mode(pEpSession *_session, gpgme_keylist_mode_t remove_mode,
  1.1193 -        gpgme_keylist_mode_t add_mode)
  1.1194 -{
  1.1195 -	gpgme_error_t gpgme_error;
  1.1196 -    gpgme_keylist_mode_t mode;
  1.1197 -
  1.1198 -    mode = _session->gpgme_get_keylist_mode(_session->ctx);
  1.1199 -
  1.1200 -    mode &= ~remove_mode;
  1.1201 -    mode |= add_mode;
  1.1202 -
  1.1203 -    gpgme_error = _session->gpgme_set_keylist_mode(_session->ctx, mode);
  1.1204 -    assert(gpgme_error == GPG_ERR_NO_ERROR);
  1.1205 -}
  1.1206 -
  1.1207 -PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
  1.1208 -{
  1.1209 -	pEpSession *_session = (pEpSession *) session;
  1.1210 -	gpgme_error_t gpgme_error;
  1.1211 -    gpgme_key_t key;
  1.1212 -
  1.1213 -    assert(session);
  1.1214 -    assert(pattern);
  1.1215 -
  1.1216 -    _switch_mode(_session, GPGME_KEYLIST_MODE_LOCAL, GPGME_KEYLIST_MODE_EXTERN);
  1.1217 -
  1.1218 -    gpgme_error = _session->gpgme_op_keylist_start(_session->ctx, pattern, 0);
  1.1219 -    switch (gpgme_error) {
  1.1220 -    case GPG_ERR_NO_ERROR:
  1.1221 -        break;
  1.1222 -    case GPG_ERR_INV_VALUE:
  1.1223 -        assert(0);
  1.1224 -        _switch_mode(_session, GPGME_KEYLIST_MODE_EXTERN,
  1.1225 -                GPGME_KEYLIST_MODE_LOCAL);
  1.1226 -        return PEP_UNKNOWN_ERROR;
  1.1227 -    default:
  1.1228 -        _switch_mode(_session, GPGME_KEYLIST_MODE_EXTERN,
  1.1229 -                GPGME_KEYLIST_MODE_LOCAL);
  1.1230 -        return PEP_GET_KEY_FAILED;
  1.1231 -    };
  1.1232 -
  1.1233 -    do {
  1.1234 -        gpgme_error = _session->gpgme_op_keylist_next(_session->ctx, &key);
  1.1235 -        assert(gpgme_error != GPG_ERR_INV_VALUE);
  1.1236 -        switch (gpgme_error) {
  1.1237 -        case GPG_ERR_EOF:
  1.1238 -            break;
  1.1239 -        case GPG_ERR_NO_ERROR:
  1.1240 -            {
  1.1241 -                gpgme_error_t gpgme_error;
  1.1242 -                gpgme_key_t keys[2];
  1.1243 -
  1.1244 -                keys[0] = key;
  1.1245 -                keys[1] = NULL;
  1.1246 -
  1.1247 -                gpgme_error = _session->gpgme_op_import_keys(_session->ctx, keys);
  1.1248 -                _session->gpgme_key_unref(key);
  1.1249 -                assert(gpgme_error != GPG_ERR_INV_VALUE);
  1.1250 -                assert(gpgme_error != GPG_ERR_CONFLICT);
  1.1251 -            }
  1.1252 -            break;
  1.1253 -        case GPG_ERR_ENOMEM:
  1.1254 -            _switch_mode(_session, GPGME_KEYLIST_MODE_EXTERN,
  1.1255 -                    GPGME_KEYLIST_MODE_LOCAL);
  1.1256 -            _session->gpgme_op_keylist_end(_session->ctx);
  1.1257 -            return PEP_OUT_OF_MEMORY;
  1.1258 -        default:
  1.1259 -            // BUG: GPGME returns an illegal value instead of GPG_ERR_EOF after
  1.1260 -            // reading first key
  1.1261 -#ifndef NDEBUG
  1.1262 -            fprintf(stderr, "warning: unknown result 0x%x of"
  1.1263 -                    " gpgme_op_keylist_next()\n", gpgme_error);
  1.1264 -#endif
  1.1265 -            gpgme_error = GPG_ERR_EOF;
  1.1266 -            break;
  1.1267 -        };
  1.1268 -    } while (gpgme_error != GPG_ERR_EOF);
  1.1269 -
  1.1270 -    _session->gpgme_op_keylist_end(_session->ctx);
  1.1271 -    _switch_mode(_session, GPGME_KEYLIST_MODE_EXTERN,
  1.1272 -            GPGME_KEYLIST_MODE_LOCAL);
  1.1273 -    return PEP_STATUS_OK;
  1.1274 -}
  1.1275 -
  1.1276 -DYNAMIC_API PEP_STATUS find_keys(
  1.1277 -        PEP_SESSION session, const char *pattern, stringlist_t **keylist
  1.1278 -    )
  1.1279 -{
  1.1280 -	pEpSession *_session = (pEpSession *) session;
  1.1281 -	gpgme_error_t gpgme_error;
  1.1282 -    gpgme_key_t key;
  1.1283 -    stringlist_t *_keylist;
  1.1284 -    char *fpr;
  1.1285 -
  1.1286 -    assert(session);
  1.1287 -    assert(pattern);
  1.1288 -    assert(keylist);
  1.1289 -
  1.1290 -    *keylist = NULL;
  1.1291 -
  1.1292 -    gpgme_error = _session->gpgme_op_keylist_start(_session->ctx, pattern, 0);
  1.1293 -    switch (gpgme_error) {
  1.1294 -    case GPG_ERR_NO_ERROR:
  1.1295 -        break;
  1.1296 -    case GPG_ERR_INV_VALUE:
  1.1297 -        assert(0);
  1.1298 -        return PEP_UNKNOWN_ERROR;
  1.1299 -    default:
  1.1300 -        return PEP_GET_KEY_FAILED;
  1.1301 -    };
  1.1302 -
  1.1303 -    _keylist = new_stringlist(NULL);
  1.1304 -    stringlist_t *_k = _keylist;
  1.1305 -
  1.1306 -    do {
  1.1307 -        gpgme_error = _session->gpgme_op_keylist_next(_session->ctx, &key);
  1.1308 -        assert(gpgme_error != GPG_ERR_INV_VALUE);
  1.1309 -        switch (gpgme_error) {
  1.1310 -        case GPG_ERR_EOF:
  1.1311 -            break;
  1.1312 -        case GPG_ERR_NO_ERROR:
  1.1313 -            assert(key);
  1.1314 -            assert(key->subkeys);
  1.1315 -            fpr = key->subkeys->fpr;
  1.1316 -            assert(fpr);
  1.1317 -            _k = stringlist_add(_k, fpr);
  1.1318 -            assert(_k);
  1.1319 -            if (_k != NULL)
  1.1320 -                break;
  1.1321 -        case GPG_ERR_ENOMEM:
  1.1322 -            free_stringlist(_keylist);
  1.1323 -            _session->gpgme_op_keylist_end(_session->ctx);
  1.1324 -            return PEP_OUT_OF_MEMORY;
  1.1325 -        default:
  1.1326 -            // BUG: GPGME returns an illegal value instead of GPG_ERR_EOF after
  1.1327 -            // reading first key
  1.1328 -#ifndef NDEBUG
  1.1329 -            fprintf(stderr, "warning: unknown result 0x%x of"
  1.1330 -                    " gpgme_op_keylist_next()\n", gpgme_error);
  1.1331 -#endif
  1.1332 -            gpgme_error = GPG_ERR_EOF;
  1.1333 -            break;
  1.1334 -        };
  1.1335 -    } while (gpgme_error != GPG_ERR_EOF);
  1.1336 -
  1.1337 -    _session->gpgme_op_keylist_end(_session->ctx);
  1.1338 -    *keylist = _keylist;
  1.1339 -    return PEP_STATUS_OK;
  1.1340 -}
  1.1341 -
  1.1342 -PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
  1.1343 -{
  1.1344 -	pEpSession *_session = (pEpSession *) session;
  1.1345 -	gpgme_error_t gpgme_error;
  1.1346 -
  1.1347 -    assert(session);
  1.1348 -    assert(pattern);
  1.1349 -
  1.1350 -    gpgme_error = _session->gpgme_op_export(_session->ctx, pattern,
  1.1351 -            GPGME_EXPORT_MODE_EXTERN, NULL);
  1.1352 -    assert(gpgme_error != GPG_ERR_INV_VALUE);
  1.1353 -    if (gpgme_error == GPG_ERR_NO_ERROR)
  1.1354 -        return PEP_STATUS_OK;
  1.1355 -    else
  1.1356 -        return PEP_CANNOT_SEND_KEY;
  1.1357 -}
  1.1358 -
  1.1359  void pEp_free(void *p)
  1.1360  {
  1.1361      free(p);
  1.1362 @@ -1971,105 +721,76 @@
  1.1363      return status;
  1.1364  }
  1.1365  
  1.1366 +DYNAMIC_API PEP_STATUS decrypt_and_verify(
  1.1367 +    PEP_SESSION session, const char *ctext, size_t csize,
  1.1368 +    char **ptext, size_t *psize, stringlist_t **keylist
  1.1369 +    )
  1.1370 +{
  1.1371 +    return pgp_decrypt_and_verify(session, ctext, csize, ptext, psize, keylist);
  1.1372 +}
  1.1373 +
  1.1374 +DYNAMIC_API PEP_STATUS encrypt_and_sign(
  1.1375 +    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
  1.1376 +    size_t psize, char **ctext, size_t *csize
  1.1377 +    )
  1.1378 +{
  1.1379 +    return pgp_encrypt_and_sign(session, keylist, ptext, psize, ctext, csize);
  1.1380 +}
  1.1381 +
  1.1382 +DYNAMIC_API PEP_STATUS verify_text(
  1.1383 +    PEP_SESSION session, const char *text, size_t size,
  1.1384 +    const char *signature, size_t sig_size, stringlist_t **keylist
  1.1385 +    )
  1.1386 +{
  1.1387 +    return pgp_verify_text(session, text, size, signature, sig_size, keylist);
  1.1388 +}
  1.1389 +
  1.1390 +DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
  1.1391 +{
  1.1392 +    return pgp_delete_keypair(session, fpr);
  1.1393 +}
  1.1394 +
  1.1395 +DYNAMIC_API PEP_STATUS export_key(
  1.1396 +    PEP_SESSION session, const char *fpr, char **key_data, size_t *size
  1.1397 +    )
  1.1398 +{
  1.1399 +    return pgp_export_key(session, fpr, key_data, size);
  1.1400 +}
  1.1401 +
  1.1402 +DYNAMIC_API PEP_STATUS find_keys(
  1.1403 +    PEP_SESSION session, const char *pattern, stringlist_t **keylist
  1.1404 +    )
  1.1405 +{
  1.1406 +    return pgp_find_keys(session, pattern, keylist);
  1.1407 +}
  1.1408 +
  1.1409 +DYNAMIC_API PEP_STATUS generate_keypair(
  1.1410 +    PEP_SESSION session, pEp_identity *identity
  1.1411 +    )
  1.1412 +{
  1.1413 +    return pgp_generate_keypair(session, identity);
  1.1414 +}
  1.1415 +
  1.1416  DYNAMIC_API PEP_STATUS get_key_rating(
  1.1417      PEP_SESSION session,
  1.1418      const char *fpr,
  1.1419      PEP_comm_type *comm_type
  1.1420      )
  1.1421  {
  1.1422 -    pEpSession *_session = (pEpSession *) session;
  1.1423 -    PEP_STATUS status = PEP_STATUS_OK;
  1.1424 -    gpgme_error_t gpgme_error;
  1.1425 -    gpgme_key_t key;
  1.1426 +    return pgp_get_key_rating(session, fpr, comm_type);
  1.1427 +}
  1.1428  
  1.1429 -    assert(session);
  1.1430 -    assert(fpr);
  1.1431 -    assert(comm_type);
  1.1432 -    
  1.1433 -    *comm_type = PEP_ct_unknown;
  1.1434 +DYNAMIC_API PEP_STATUS import_key(PEP_SESSION session, const char *key_data, size_t size)
  1.1435 +{
  1.1436 +    return pgp_import_key(session, key_data, size);
  1.1437 +}
  1.1438  
  1.1439 -    gpgme_error = _session->gpgme_op_keylist_start(_session->ctx, fpr, 0);
  1.1440 -    switch (gpgme_error) {
  1.1441 -    case GPG_ERR_NO_ERROR:
  1.1442 -        break;
  1.1443 -    case GPG_ERR_INV_VALUE:
  1.1444 -        assert(0);
  1.1445 -        return PEP_UNKNOWN_ERROR;
  1.1446 -    default:
  1.1447 -        return PEP_GET_KEY_FAILED;
  1.1448 -    };
  1.1449 +DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
  1.1450 +{
  1.1451 +    return pgp_recv_key(session, pattern);
  1.1452 +}
  1.1453  
  1.1454 -    gpgme_error = _session->gpgme_op_keylist_next(_session->ctx, &key);
  1.1455 -    assert(gpgme_error != GPG_ERR_INV_VALUE);
  1.1456 -
  1.1457 -    if (key == NULL) {
  1.1458 -        _session->gpgme_op_keylist_end(_session->ctx);
  1.1459 -        return PEP_KEY_NOT_FOUND;
  1.1460 -    }
  1.1461 -
  1.1462 -    switch (key->protocol) {
  1.1463 -    case GPGME_PROTOCOL_OpenPGP:
  1.1464 -    case GPGME_PROTOCOL_DEFAULT:
  1.1465 -        *comm_type = PEP_ct_OpenPGP_unconfirmed;
  1.1466 -        break;
  1.1467 -    case GPGME_PROTOCOL_CMS:
  1.1468 -        *comm_type = PEP_ct_CMS_unconfirmed;
  1.1469 -        break;
  1.1470 -    default:
  1.1471 -        *comm_type = PEP_ct_unknown;
  1.1472 -        _session->gpgme_op_keylist_end(_session->ctx);
  1.1473 -        return PEP_STATUS_OK;
  1.1474 -    }
  1.1475 -
  1.1476 -    switch (gpgme_error) {
  1.1477 -    case GPG_ERR_EOF:
  1.1478 -        break;
  1.1479 -    case GPG_ERR_NO_ERROR:
  1.1480 -        assert(key);
  1.1481 -        assert(key->subkeys);
  1.1482 -        for (gpgme_subkey_t sk = key->subkeys; sk != NULL; sk = sk->next) {
  1.1483 -            if (sk->length < 1024)
  1.1484 -                *comm_type = PEP_ct_key_too_short;
  1.1485 -            else if (
  1.1486 -                (
  1.1487 -                       (sk->pubkey_algo == GPGME_PK_RSA)
  1.1488 -                    || (sk->pubkey_algo == GPGME_PK_RSA_E)
  1.1489 -                    || (sk->pubkey_algo == GPGME_PK_RSA_S)
  1.1490 -                )
  1.1491 -                && sk->length == 1024
  1.1492 -            )
  1.1493 -                *comm_type = PEP_ct_OpenPGP_1024_RSA_unconfirmed;
  1.1494 -
  1.1495 -            if (sk->invalid) {
  1.1496 -                *comm_type = PEP_ct_key_b0rken;
  1.1497 -                break;
  1.1498 -            }
  1.1499 -            if (sk->expired) {
  1.1500 -                *comm_type = PEP_ct_key_expired;
  1.1501 -                break;
  1.1502 -            }
  1.1503 -            if (sk->revoked) {
  1.1504 -                *comm_type = PEP_ct_key_revoked;
  1.1505 -                break;
  1.1506 -            }
  1.1507 -        }
  1.1508 -        break;
  1.1509 -    case GPG_ERR_ENOMEM:
  1.1510 -        _session->gpgme_op_keylist_end(_session->ctx);
  1.1511 -        *comm_type = PEP_ct_unknown;
  1.1512 -        return PEP_OUT_OF_MEMORY;
  1.1513 -    default:
  1.1514 -        // BUG: GPGME returns an illegal value instead of GPG_ERR_EOF after
  1.1515 -        // reading first key
  1.1516 -#ifndef NDEBUG
  1.1517 -        fprintf(stderr, "warning: unknown result 0x%x of"
  1.1518 -            " gpgme_op_keylist_next()\n", gpgme_error);
  1.1519 -#endif
  1.1520 -        gpgme_error = GPG_ERR_EOF;
  1.1521 -        break;
  1.1522 -    };
  1.1523 -
  1.1524 -    _session->gpgme_op_keylist_end(_session->ctx);
  1.1525 -
  1.1526 -    return status;
  1.1527 +DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
  1.1528 +{
  1.1529 +    return pgp_send_key(session, pattern);
  1.1530  }