empty netpgp
authorEdouard Tisserant
Wed, 01 Apr 2015 16:38:20 +0200
changeset 1749c3d0eaa8ea1
parent 168 5ddaec401f66
child 175 59c8eabab690
empty netpgp
src/pgp_netpgp.c
src/pgp_netpgp.h
src/pgp_netpgp_internal.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/pgp_netpgp.c	Wed Apr 01 16:38:20 2015 +0200
     1.3 @@ -0,0 +1,467 @@
     1.4 +#include "pEp_internal.h"
     1.5 +#include "pgp_netpgp.h"
     1.6 +
     1.7 +#include <limits.h>
     1.8 +
     1.9 +#include "wrappers.h"
    1.10 +
    1.11 +#include <netpgp.h>
    1.12 +PEP_STATUS pgp_init(PEP_SESSION session, bool in_first)
    1.13 +{
    1.14 +    PEP_STATUS status = PEP_STATUS_OK;
    1.15 +   
    1.16 +    if (in_first) {
    1.17 +        /* TODO something maybe */
    1.18 +    }
    1.19 +
    1.20 +        // TODO ensure minimal config
    1.21 +          
    1.22 +        // "keyserver"
    1.23 +        // "hkp://keys.gnupg.net"
    1.24 +
    1.25 +        // "cert-digest-algo"
    1.26 +        // "SHA256"
    1.27 +
    1.28 +        // "no-emit-version"
    1.29 +        // ""
    1.30 +
    1.31 +        // "no-comments"
    1.32 +        // ""
    1.33 +
    1.34 +        // "personal-cipher-preferences"
    1.35 +        // "AES AES256 AES192 CAST5"
    1.36 +
    1.37 +        // "personal-digest-preferences"
    1.38 +        // "SHA512 SHA384 SHA256 SHA224"
    1.39 +        
    1.40 +        if (strcmp(setlocale(LC_ALL, NULL), "C") == 0)
    1.41 +            setlocale(LC_ALL, "");
    1.42 +
    1.43 +        // TODO unsset netpgp locale if any
    1.44 +        // LC_CTYPE
    1.45 +#ifdef LC_MESSAGES // Windoze
    1.46 +        // LC_MESSAGES
    1.47 +#endif
    1.48 +    }
    1.49 +
    1.50 +    // TODO Create netpgp handle
    1.51 +    // session->ctx = ...
    1.52 +    if (/* create error */) {
    1.53 +        status = PEP_INIT_GPGME_INIT_FAILED;
    1.54 +        goto pep_error;
    1.55 +    }
    1.56 +    assert(session->ctx);
    1.57 +
    1.58 +    // TODO set protocol to OpenPGP
    1.59 +    // TODO set to use armoring
    1.60 +
    1.61 +    return PEP_STATUS_OK;
    1.62 +
    1.63 +pep_error:
    1.64 +    pgp_release(session, in_first);
    1.65 +    return status;
    1.66 +}
    1.67 +
    1.68 +void pgp_release(PEP_SESSION session, bool out_last)
    1.69 +{
    1.70 +    if (session->ctx) {
    1.71 +        // TODO : release session->ctx
    1.72 +        session->ctx = NULL;
    1.73 +    }
    1.74 +
    1.75 +    if (out_last){
    1.76 +        // TODO anything ?
    1.77 +    }
    1.78 +
    1.79 +}
    1.80 +
    1.81 +PEP_STATUS pgp_decrypt_and_verify(
    1.82 +    PEP_SESSION session, const char *ctext, size_t csize,
    1.83 +    char **ptext, size_t *psize, stringlist_t **keylist
    1.84 +    )
    1.85 +{
    1.86 +    PEP_STATUS result;
    1.87 +
    1.88 +    stringlist_t *_keylist = NULL;
    1.89 +    int i_key = 0;
    1.90 +
    1.91 +    assert(session);
    1.92 +    assert(ctext);
    1.93 +    assert(csize);
    1.94 +    assert(ptext);
    1.95 +    assert(psize);
    1.96 +    assert(keylist);
    1.97 +
    1.98 +    *ptext = NULL;
    1.99 +    *psize = 0;
   1.100 +    *keylist = NULL;
   1.101 +
   1.102 +    /* TODO identify cipher text */
   1.103 +    /* if recognized */
   1.104 +    /* decrypt */
   1.105 +    /* if OK, verify */
   1.106 +    /*
   1.107 +    result = PEP_DECRYPTED_AND_VERIFIED;
   1.108 +    result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
   1.109 +    result = PEP_DECRYPTED;
   1.110 +    result = PEP_DECRYPT_WRONG_FORMAT;
   1.111 +    result = PEP_DECRYPT_NO_KEY;
   1.112 +    return PEP_OUT_OF_MEMORY;
   1.113 +    */
   1.114 +    result = PEP_UNKNOWN_ERROR;
   1.115 +                stringlist_t *k;
   1.116 +                _keylist = new_stringlist(NULL);
   1.117 +                assert(_keylist);
   1.118 +                if (_keylist == NULL) {
   1.119 +                    /* TODO */
   1.120 +                    return PEP_OUT_OF_MEMORY;
   1.121 +                }
   1.122 +                k = _keylist;
   1.123 +                do {
   1.124 +                        k = stringlist_add(k, "SIGNATURE FPR"/*TODO*/);
   1.125 +                } while (0 /* TODO sign next*/);
   1.126 +
   1.127 +    return result;
   1.128 +}
   1.129 +
   1.130 +PEP_STATUS pgp_verify_text(
   1.131 +    PEP_SESSION session, const char *text, size_t size,
   1.132 +    const char *signature, size_t sig_size, stringlist_t **keylist
   1.133 +    )
   1.134 +{
   1.135 +    PEP_STATUS result;
   1.136 +    gpgme_error_t gpgme_error;
   1.137 +    gpgme_data_t d_text, d_sig;
   1.138 +    stringlist_t *_keylist;
   1.139 +
   1.140 +    assert(session);
   1.141 +    assert(text);
   1.142 +    assert(size);
   1.143 +    assert(signature);
   1.144 +    assert(sig_size);
   1.145 +    assert(keylist);
   1.146 +
   1.147 +    *keylist = NULL;
   1.148 +    /* if OK, verify */
   1.149 +            stringlist_t *k;
   1.150 +            k = _keylist;
   1.151 +            result = PEP_VERIFIED;
   1.152 +            do {
   1.153 +                k = stringlist_add(k, "TODO");
   1.154 +                if (k == NULL) {
   1.155 +                    free_stringlist(_keylist);
   1.156 +                    /* TODO */
   1.157 +                    return PEP_OUT_OF_MEMORY;
   1.158 +                }
   1.159 +            } while (0 /*TODO*/);
   1.160 +            *keylist = _keylist;
   1.161 +    /*
   1.162 +    result = PEP_UNENCRYPTED;
   1.163 +    result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
   1.164 +    result = PEP_VERIFIED_AND_TRUSTED;
   1.165 +    result = PEP_VERIFY_NO_KEY;
   1.166 +    result = PEP_UNENCRYPTED;
   1.167 +    result = PEP_DECRYPT_WRONG_FORMAT;
   1.168 +    return PEP_OUT_OF_MEMORY;
   1.169 +    */
   1.170 +    result = PEP_UNKNOWN_ERROR;
   1.171 +
   1.172 +    return result;
   1.173 +}
   1.174 +
   1.175 +PEP_STATUS pgp_encrypt_and_sign(
   1.176 +    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
   1.177 +    size_t psize, char **ctext, size_t *csize
   1.178 +    )
   1.179 +{
   1.180 +    PEP_STATUS result;
   1.181 +    const stringlist_t *_keylist;
   1.182 +    int i, j;
   1.183 +
   1.184 +    assert(session);
   1.185 +    assert(keylist);
   1.186 +    assert(ptext);
   1.187 +    assert(psize);
   1.188 +    assert(ctext);
   1.189 +    assert(csize);
   1.190 +
   1.191 +    *ctext = NULL;
   1.192 +    *csize = 0;
   1.193 +
   1.194 +    for (_keylist = keylist, i = 0; _keylist != NULL; _keylist = _keylist->next, i++) {
   1.195 +        assert(_keylist->value);
   1.196 +        /* TODO */
   1.197 +        /* get key from  _keylist->value */
   1.198 +        /* add key to recipients/signers */
   1.199 +    }
   1.200 +
   1.201 +    /* Do encrypt and sign */ 
   1.202 +        char *_buffer = NULL;
   1.203 +        size_t length = /* TODO length*/ 0;
   1.204 +        assert(length != -1);
   1.205 +
   1.206 +        /* Allocate transferable buffer */
   1.207 +        _buffer = malloc(length + 1);
   1.208 +        assert(_buffer);
   1.209 +        if (_buffer == NULL) {
   1.210 +            /* TODO clean */
   1.211 +            return PEP_OUT_OF_MEMORY;
   1.212 +        }
   1.213 +
   1.214 +        *ctext = _buffer;
   1.215 +        *csize = length;
   1.216 +        (*ctext)[*csize] = 0; // safeguard for naive users
   1.217 +        result = PEP_STATUS_OK;
   1.218 +    }
   1.219 +
   1.220 +    
   1.221 +        result = PEP_UNKNOWN_ERROR;
   1.222 +    return result;
   1.223 +}
   1.224 +
   1.225 +PEP_STATUS pgp_generate_keypair(
   1.226 +    PEP_SESSION session, pEp_identity *identity
   1.227 +    )
   1.228 +{
   1.229 +    char *parms;
   1.230 +    const char *template =
   1.231 +        "Key-Type: RSA\n"
   1.232 +        "Key-Length: 4096\n"
   1.233 +        "Name-Real: %s\n"
   1.234 +        "Name-Email: %s\n"
   1.235 +        /* "Passphrase: %s\n" */
   1.236 +        "Expire-Date: 1y\n";
   1.237 +    int result;
   1.238 +
   1.239 +    assert(session);
   1.240 +    assert(identity);
   1.241 +    assert(identity->address);
   1.242 +    assert(identity->fpr == NULL);
   1.243 +    assert(identity->username);
   1.244 +
   1.245 +    parms = calloc(1, PARMS_MAX);
   1.246 +    assert(parms);
   1.247 +    if (parms == NULL)
   1.248 +        return PEP_OUT_OF_MEMORY;
   1.249 +
   1.250 +    result = snprintf(parms, PARMS_MAX, template, identity->username,
   1.251 +        identity->address);
   1.252 +    assert(result < PARMS_MAX);
   1.253 +    if (result >= PARMS_MAX) {
   1.254 +        free(parms);
   1.255 +        return PEP_BUFFER_TOO_SMALL;
   1.256 +    }
   1.257 +
   1.258 +    /* TODO generate key */
   1.259 +
   1.260 +    free(parms);
   1.261 +
   1.262 +        return PEP_UNKNOWN_ERROR;
   1.263 +        return PEP_ILLEGAL_VALUE;
   1.264 +        return PEP_CANNOT_CREATE_KEY;
   1.265 +
   1.266 +    identity->fpr = strdup("TODO generated key fpr");
   1.267 +
   1.268 +    return PEP_STATUS_OK;
   1.269 +}
   1.270 +
   1.271 +PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fpr)
   1.272 +{
   1.273 +    assert(session);
   1.274 +    assert(fpr);
   1.275 +
   1.276 +    /* TODO get key with given fpr */
   1.277 +        return PEP_KEY_NOT_FOUND;
   1.278 +        return PEP_ILLEGAL_VALUE;
   1.279 +        return PEP_KEY_HAS_AMBIG_NAME;
   1.280 +        return PEP_OUT_OF_MEMORY;
   1.281 +        return PEP_UNKNOWN_ERROR;
   1.282 +
   1.283 +    /* TODO delete that key */
   1.284 +        return PEP_UNKNOWN_ERROR;
   1.285 +        return PEP_KEY_NOT_FOUND;
   1.286 +        return PEP_KEY_HAS_AMBIG_NAME;
   1.287 +        return PEP_UNKNOWN_ERROR;
   1.288 +
   1.289 +    return PEP_STATUS_OK;
   1.290 +}
   1.291 +
   1.292 +PEP_STATUS pgp_import_key(PEP_SESSION session, const char *key_data, size_t size)
   1.293 +{
   1.294 +    assert(session);
   1.295 +    assert(key_data);
   1.296 +
   1.297 +    /* TODO import */
   1.298 +        return PEP_UNKNOWN_ERROR;
   1.299 +        return PEP_ILLEGAL_VALUE;
   1.300 +        return PEP_UNKNOWN_ERROR;
   1.301 +    return PEP_STATUS_OK;
   1.302 +}
   1.303 +
   1.304 +PEP_STATUS pgp_export_key(
   1.305 +    PEP_SESSION session, const char *fpr, char **key_data, size_t *size
   1.306 +    )
   1.307 +{
   1.308 +    size_t _size;
   1.309 +    char *buffer;
   1.310 +    int reading;
   1.311 +
   1.312 +    assert(session);
   1.313 +    assert(fpr);
   1.314 +    assert(key_data);
   1.315 +    assert(size);
   1.316 +
   1.317 +
   1.318 +    /* TODO export */
   1.319 +        return PEP_KEY_NOT_FOUND;
   1.320 +        return PEP_UNKNOWN_ERROR;
   1.321 +        return PEP_UNKNOWN_ERROR;
   1.322 +
   1.323 +    _size = /* TODO */ 0;
   1.324 +    assert(_size != -1);
   1.325 +
   1.326 +    buffer = malloc(_size + 1);
   1.327 +    assert(buffer);
   1.328 +    if (buffer == NULL) {
   1.329 +        /* TODO clean */
   1.330 +        return PEP_OUT_OF_MEMORY;
   1.331 +    }
   1.332 +
   1.333 +    // safeguard for the naive user
   1.334 +    buffer[_size] = 0;
   1.335 +
   1.336 +    *key_data = buffer;
   1.337 +    *size = _size;
   1.338 +
   1.339 +    return PEP_STATUS_OK;
   1.340 +}
   1.341 +
   1.342 +PEP_STATUS pgp_recv_key(PEP_SESSION session, const char *pattern)
   1.343 +{
   1.344 +    assert(session);
   1.345 +    assert(pattern);
   1.346 +
   1.347 +    /* TODO ask for key */
   1.348 +        return PEP_UNKNOWN_ERROR;
   1.349 +        return PEP_GET_KEY_FAILED;
   1.350 +
   1.351 +    do {
   1.352 +
   1.353 +        /* For each key */
   1.354 +        /* import key */
   1.355 +    } while (0);
   1.356 +
   1.357 +    return PEP_STATUS_OK;
   1.358 +}
   1.359 +
   1.360 +PEP_STATUS pgp_find_keys(
   1.361 +    PEP_SESSION session, const char *pattern, stringlist_t **keylist
   1.362 +    )
   1.363 +{
   1.364 +    stringlist_t *_keylist;
   1.365 +    char *fpr;
   1.366 +
   1.367 +    assert(session);
   1.368 +    assert(pattern);
   1.369 +    assert(keylist);
   1.370 +
   1.371 +    *keylist = NULL;
   1.372 +
   1.373 +    /* Ask for key */
   1.374 +        return PEP_UNKNOWN_ERROR;
   1.375 +        return PEP_GET_KEY_FAILED;
   1.376 +
   1.377 +    _keylist = new_stringlist(NULL);
   1.378 +    stringlist_t *_k = _keylist;
   1.379 +
   1.380 +    do {
   1.381 +            fpr = "TODO key->subkeys->fpr";
   1.382 +            assert(fpr);
   1.383 +            _k = stringlist_add(_k, fpr);
   1.384 +            assert(_k);
   1.385 +            if (_k == NULL){
   1.386 +                free_stringlist(_keylist);
   1.387 +                return PEP_OUT_OF_MEMORY;
   1.388 +            }
   1.389 +    } while (0);
   1.390 +
   1.391 +    *keylist = _keylist;
   1.392 +    return PEP_STATUS_OK;
   1.393 +}
   1.394 +
   1.395 +PEP_STATUS pgp_send_key(PEP_SESSION session, const char *pattern)
   1.396 +{
   1.397 +    assert(session);
   1.398 +    assert(pattern);
   1.399 +
   1.400 +    /* TODO send key */
   1.401 +
   1.402 +        return PEP_CANNOT_SEND_KEY;
   1.403 +        return PEP_STATUS_OK;
   1.404 +}
   1.405 +
   1.406 +
   1.407 +PEP_STATUS pgp_get_key_rating(
   1.408 +    PEP_SESSION session,
   1.409 +    const char *fpr,
   1.410 +    PEP_comm_type *comm_type
   1.411 +    )
   1.412 +{
   1.413 +    PEP_STATUS status = PEP_STATUS_OK;
   1.414 +
   1.415 +    assert(session);
   1.416 +    assert(fpr);
   1.417 +    assert(comm_type);
   1.418 +
   1.419 +    *comm_type = PEP_ct_unknown;
   1.420 +
   1.421 +    /* TODO get key from fpr */
   1.422 +    return PEP_UNKNOWN_ERROR;
   1.423 +    return PEP_GET_KEY_FAILED;
   1.424 +
   1.425 +    switch (/*TODO key->protocol*/ 4) {
   1.426 +    case /* TODO  OpenPGP */0:
   1.427 +    case /* TODO DEFAULT */1:
   1.428 +        *comm_type = PEP_ct_OpenPGP_unconfirmed;
   1.429 +        break;
   1.430 +    case /* TODO CMS */2:
   1.431 +        *comm_type = PEP_ct_CMS_unconfirmed;
   1.432 +        break;
   1.433 +    default:
   1.434 +        *comm_type = PEP_ct_unknown;
   1.435 +        return PEP_STATUS_OK;
   1.436 +    }
   1.437 +
   1.438 +        for (; 1 == 0; /* Each subkeys */ ) {
   1.439 +            if (/* TODO length */0 < 1024)
   1.440 +                *comm_type = PEP_ct_key_too_short;
   1.441 +            else if (
   1.442 +                (
   1.443 +                (   /* TODO pubkey_algo == RSA  */ 0)
   1.444 +                || (/* TODO pubkey_algo == RSA_E*/ 0)
   1.445 +                || (/* TODO pubkey_algo == RSA_S*/ 0)
   1.446 +                )
   1.447 +                && /* sk->length */0 == 1024
   1.448 +                )
   1.449 +                *comm_type = PEP_ct_OpenPGP_weak_unconfirmed;
   1.450 +
   1.451 +            if (/* TODO invalid */ 1) {
   1.452 +                *comm_type = PEP_ct_key_b0rken;
   1.453 +                break;
   1.454 +            }
   1.455 +            if (/* TODO expired */ 1) {
   1.456 +                *comm_type = PEP_ct_key_expired;
   1.457 +                break;
   1.458 +            }
   1.459 +            if (/* TODO revoked*/) {
   1.460 +                *comm_type = PEP_ct_key_revoked;
   1.461 +                break;
   1.462 +            }
   1.463 +        }
   1.464 +        *comm_type = PEP_ct_unknown;
   1.465 +        return PEP_OUT_OF_MEMORY;
   1.466 +        return PEP_UNKNOWN_ERROR;
   1.467 +
   1.468 +
   1.469 +    return status;
   1.470 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/pgp_netpgp.h	Wed Apr 01 16:38:20 2015 +0200
     2.3 @@ -0,0 +1,49 @@
     2.4 +#pragma once
     2.5 +
     2.6 +#include "pEpEngine.h"
     2.7 +
     2.8 +PEP_STATUS pgp_init(PEP_SESSION session, bool in_first);
     2.9 +void pgp_release(PEP_SESSION session, bool out_last);
    2.10 +
    2.11 +PEP_STATUS pgp_decrypt_and_verify(
    2.12 +        PEP_SESSION session, const char *ctext, size_t csize,
    2.13 +        char **ptext, size_t *psize, stringlist_t **keylist
    2.14 +    );
    2.15 +
    2.16 +PEP_STATUS pgp_encrypt_and_sign(
    2.17 +        PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
    2.18 +        size_t psize, char **ctext, size_t *csize
    2.19 +    );
    2.20 +
    2.21 +PEP_STATUS pgp_verify_text(
    2.22 +        PEP_SESSION session, const char *text, size_t size,
    2.23 +        const char *signature, size_t sig_size, stringlist_t **keylist
    2.24 +    );
    2.25 +
    2.26 +PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fpr);
    2.27 +
    2.28 +PEP_STATUS pgp_export_key(
    2.29 +        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
    2.30 +    );
    2.31 +
    2.32 +PEP_STATUS pgp_find_keys(
    2.33 +        PEP_SESSION session, const char *pattern, stringlist_t **keylist
    2.34 +    );
    2.35 +
    2.36 +PEP_STATUS pgp_generate_keypair(
    2.37 +        PEP_SESSION session, pEp_identity *identity
    2.38 +    );
    2.39 +
    2.40 +PEP_STATUS pgp_get_key_rating(
    2.41 +        PEP_SESSION session,
    2.42 +        const char *fpr,
    2.43 +        PEP_comm_type *comm_type
    2.44 +    );
    2.45 +
    2.46 +PEP_STATUS pgp_import_key(PEP_SESSION session, const char *key_data,
    2.47 +        size_t size);
    2.48 +
    2.49 +PEP_STATUS pgp_recv_key(PEP_SESSION session, const char *pattern);
    2.50 +
    2.51 +PEP_STATUS pgp_send_key(PEP_SESSION session, const char *pattern);
    2.52 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/pgp_netpgp_internal.h	Wed Apr 01 16:38:20 2015 +0200
     3.3 @@ -0,0 +1,3 @@
     3.4 +#pragma once
     3.5 +
     3.6 +#include <netpgp.h>