src/pgp_netpgp.c
changeset 209 8572f358fb61
parent 208 0a01cff93e48
child 210 f599bb1ebb90
     1.1 --- a/src/pgp_netpgp.c	Fri Apr 17 11:29:01 2015 +0200
     1.2 +++ b/src/pgp_netpgp.c	Mon Apr 20 22:58:53 2015 +0200
     1.3 @@ -383,9 +383,11 @@
     1.4      netpgp_t *netpgp;
     1.5      const pgp_key_t *keypair;
     1.6      pgp_seckey_t *seckey;
     1.7 -	pgp_memory_t *signedmem;
     1.8 +    pgp_memory_t *signedmem;
     1.9 +    pgp_memory_t *cmem;
    1.10      const char *userid;
    1.11      const char *hashalg;
    1.12 +    pgp_keyring_t *rcpts;
    1.13  
    1.14      PEP_STATUS result;
    1.15      const stringlist_t *_keylist;
    1.16 @@ -412,16 +414,18 @@
    1.17          (seckey = pgp_decrypt_seckey(keypair, NULL /*passfp*/)) == NULL) {
    1.18          return PEP_UNKNOWN_ERROR;
    1.19      }
    1.20 +
    1.21      hashalg = netpgp_getvar(netpgp, "hash");
    1.22 +    // netpgp (l)imitation - XXX why ? 
    1.23      if (seckey->pubkey.alg == PGP_PKA_DSA) {
    1.24 -    	hashalg = "sha1";
    1.25 +        hashalg = "sha1";
    1.26      }
    1.27  
    1.28      // Sign data
    1.29      signedmem = pgp_sign_buf(netpgp->io, ptext, psize, seckey,
    1.30 -    			time(NULL), /* birthtime */
    1.31 -    			0 /* duration */,
    1.32 -    			hashalg, 
    1.33 +                time(NULL), /* birthtime */
    1.34 +                0 /* duration */,
    1.35 +                hashalg, 
    1.36                  0 /* armored */,
    1.37                  0 /* cleartext */);
    1.38  
    1.39 @@ -432,48 +436,72 @@
    1.40      }
    1.41  
    1.42      // Encrypt signed data
    1.43 -    pgp_memory_free(signedmem);
    1.44 -    result = PEP_UNKNOWN_ERROR;
    1.45 -    return result;
    1.46 -/*
    1.47 - *
    1.48 - *
    1.49 - *
    1.50 - *
    1.51 - *
    1.52 - *
    1.53 - *
    1.54 - *
    1.55 - *
    1.56 - *
    1.57 - * */
    1.58 -    for (_keylist = keylist, i = 0; _keylist != NULL; _keylist = _keylist->next, i++) {
    1.59 +    if ((rcpts = calloc(1, sizeof(*rcpts))) == NULL) {
    1.60 +        result = PEP_OUT_OF_MEMORY;
    1.61 +        goto free_signedmem;
    1.62 +    }
    1.63 +    for (_keylist = keylist; _keylist != NULL; _keylist = _keylist->next) {
    1.64          assert(_keylist->value);
    1.65 -        /* TODO */
    1.66 -        /* get key from  _keylist->value */
    1.67 -        /* add key to recipients/signers */
    1.68 +        // get key from netpgp's pubring
    1.69 +        const pgp_key_t *key;
    1.70 +        key = pgp_getkeybyname(netpgp->io,
    1.71 +                               netpgp->pubring,
    1.72 +                               _keylist->value);
    1.73 +
    1.74 +        if(key == NULL){
    1.75 +            result = PEP_KEY_NOT_FOUND;
    1.76 +            goto free_rcpts;
    1.77 +        }
    1.78 +#ifdef PEP_NETPGP_DEBUG
    1.79 +        pgp_print_keydata(netpgp->io, netpgp->pubring, key,
    1.80 +                          "recipient pubkey ", &key->key.pubkey, 0);
    1.81 +#endif //PEP_NETPGP_DEBUG
    1.82 +
    1.83 +        // add key to recipients/signers
    1.84 +        pgp_keyring_add(rcpts, key);
    1.85 +        if(rcpts->keys == NULL){
    1.86 +            result = PEP_OUT_OF_MEMORY;
    1.87 +            goto free_signedmem;
    1.88 +        }
    1.89      }
    1.90  
    1.91 -    /* Do encrypt and sign */ 
    1.92 -    char *_buffer = NULL;
    1.93 -    size_t length = /* TODO length*/ 0;
    1.94 -    assert(length != -1);
    1.95 +    cmem = pgp_encrypt_buf(netpgp->io, pgp_mem_data(signedmem),
    1.96 +            pgp_mem_len(signedmem), rcpts, 1 /* armored */,
    1.97 +            netpgp_getvar(netpgp, "cipher"), 
    1.98 +            1 /* takes raw OpenPGP message */);
    1.99 +
   1.100 +    if (cmem == NULL) {
   1.101 +        result = PEP_OUT_OF_MEMORY;
   1.102 +        goto free_signedmem;
   1.103 +    }else{
   1.104 +
   1.105 +        char *_buffer = NULL;
   1.106 +        size_t length = pgp_mem_len(cmem);
   1.107 +        assert(length != -1);
   1.108  
   1.109 -    /* Allocate transferable buffer */
   1.110 -    _buffer = malloc(length + 1);
   1.111 -    assert(_buffer);
   1.112 -    if (_buffer == NULL) {
   1.113 -        /* TODO clean */
   1.114 -        return PEP_OUT_OF_MEMORY;
   1.115 +        // Allocate transferable buffer
   1.116 +        _buffer = malloc(length + 1);
   1.117 +        assert(_buffer);
   1.118 +        if (_buffer == NULL) {
   1.119 +            result = PEP_OUT_OF_MEMORY;
   1.120 +            goto free_cmem;
   1.121 +        }
   1.122 +
   1.123 +        memcpy(_buffer, pgp_mem_data(cmem), length);
   1.124 +
   1.125 +        *ctext = _buffer;
   1.126 +        *csize = length;
   1.127 +        (*ctext)[*csize] = 0; // safeguard for naive users
   1.128 +        result = PEP_STATUS_OK;
   1.129      }
   1.130  
   1.131 -    *ctext = _buffer;
   1.132 -    *csize = length;
   1.133 -    (*ctext)[*csize] = 0; // safeguard for naive users
   1.134 -    result = PEP_STATUS_OK;
   1.135 +free_cmem :
   1.136 +    pgp_memory_free(cmem);
   1.137 +free_rcpts :
   1.138 +    pgp_keyring_free(rcpts);
   1.139 +free_signedmem :
   1.140 +    pgp_memory_free(signedmem);
   1.141  
   1.142 -    
   1.143 -    result = PEP_UNKNOWN_ERROR;
   1.144      return result;
   1.145  }
   1.146