src/pgp_netpgp.c
changeset 207 b17a74f2acd9
parent 188 0a882568ccd3
child 208 0a01cff93e48
     1.1 --- a/src/pgp_netpgp.c	Fri Apr 17 01:17:59 2015 +0200
     1.2 +++ b/src/pgp_netpgp.c	Fri Apr 17 11:01:31 2015 +0200
     1.3 @@ -81,8 +81,8 @@
     1.4      // out_last unused here
     1.5  }
     1.6  
     1.7 -/* return 1 if the file contains ascii-armoured text 
     1.8 - * buf MUST be \0 terminated to be checked for armour */
     1.9 +// return 1 if the file contains ascii-armoured text 
    1.10 +// buf MUST be \0 terminated to be checked for armour
    1.11  static unsigned
    1.12  _armoured(const char *buf, size_t size, const char *pattern)
    1.13  {
    1.14 @@ -201,7 +201,7 @@
    1.15      return PEP_DECRYPT_WRONG_FORMAT;
    1.16  }
    1.17  
    1.18 -#define ARMOR_HEAD	"^-----BEGIN PGP MESSAGE-----\\s*$"
    1.19 +#define ARMOR_HEAD    "^-----BEGIN PGP MESSAGE-----\\s*$"
    1.20  PEP_STATUS pgp_decrypt_and_verify(
    1.21      PEP_SESSION session, const char *ctext, size_t csize,
    1.22      char **ptext, size_t *psize, stringlist_t **keylist
    1.23 @@ -294,7 +294,7 @@
    1.24      return result;
    1.25  }
    1.26  
    1.27 -#define ARMOR_SIG_HEAD	"^-----BEGIN PGP (SIGNATURE|SIGNED MESSAGE)-----\\s*$"
    1.28 +#define ARMOR_SIG_HEAD    "^-----BEGIN PGP (SIGNATURE|SIGNED MESSAGE)-----\\s*$"
    1.29  PEP_STATUS pgp_verify_text(
    1.30      PEP_SESSION session, const char *text, size_t size,
    1.31      const char *signature, size_t sig_size, stringlist_t **keylist
    1.32 @@ -304,7 +304,6 @@
    1.33      pgp_memory_t *signedmem;
    1.34      pgp_memory_t *sig;
    1.35      pgp_validation_t *vresult;
    1.36 -    pgp_io_t *io;
    1.37  
    1.38      PEP_STATUS result;
    1.39      stringlist_t *_keylist;
    1.40 @@ -341,7 +340,7 @@
    1.41  
    1.42      pgp_validate_mem_detached(netpgp->io, vresult, sig,
    1.43                  NULL,/* output */
    1.44 -                _armoured(text, size, ARMOR_SIG_HEAD),
    1.45 +                _armoured(signature, sig_size, ARMOR_SIG_HEAD),
    1.46                  netpgp->pubring,
    1.47                  signedmem);
    1.48  
    1.49 @@ -381,9 +380,16 @@
    1.50      size_t psize, char **ctext, size_t *csize
    1.51      )
    1.52  {
    1.53 +    netpgp_t *netpgp;
    1.54 +    const pgp_key_t *keypair;
    1.55 +    pgp_seckey_t *seckey;
    1.56 +	pgp_memory_t *signedmem;
    1.57 +    const char *userid;
    1.58 +    const char *hashalg;
    1.59 +
    1.60      PEP_STATUS result;
    1.61      const stringlist_t *_keylist;
    1.62 -    int i, j;
    1.63 +    int i;
    1.64  
    1.65      assert(session);
    1.66      assert(keylist);
    1.67 @@ -392,9 +398,53 @@
    1.68      assert(ctext);
    1.69      assert(csize);
    1.70  
    1.71 +    if(!session || !ptext || !psize || !ctext || !csize || !keylist) 
    1.72 +        return PEP_UNKNOWN_ERROR;
    1.73 +
    1.74      *ctext = NULL;
    1.75      *csize = 0;
    1.76  
    1.77 +    // Get signing details from netpgp
    1.78 +    if ((userid = netpgp_getvar(netpgp, "userid")) == NULL || 
    1.79 +        (keypair = pgp_getkeybyname(netpgp->io, netpgp->secring, userid)) == NULL ||
    1.80 +        (seckey = pgp_decrypt_seckey(keypair, NULL /*passfp*/)) == NULL) {
    1.81 +        return PEP_UNKNOWN_ERROR;
    1.82 +    }
    1.83 +    hashalg = netpgp_getvar(netpgp, "hash");
    1.84 +    if (seckey->pubkey.alg == PGP_PKA_DSA) {
    1.85 +    	hashalg = "sha1";
    1.86 +    }
    1.87 +
    1.88 +    // Sign data
    1.89 +    signedmem = pgp_sign_buf(netpgp->io, ptext, psize, seckey,
    1.90 +    			time(NULL), /* birthtime */
    1.91 +    			0 /* duration */,
    1.92 +    			hashalg, 
    1.93 +                0 /* armored */,
    1.94 +                0 /* cleartext */);
    1.95 +
    1.96 +    pgp_forget(seckey, (unsigned)sizeof(*seckey));
    1.97 +
    1.98 +    if (!signedmem) {
    1.99 +        return PEP_UNENCRYPTED;
   1.100 +    }
   1.101 +
   1.102 +    // Encrypt signed data
   1.103 +    pgp_memory_free(signedmem);
   1.104 +    result = PEP_UNKNOWN_ERROR;
   1.105 +    return result;
   1.106 +/*
   1.107 + *
   1.108 + *
   1.109 + *
   1.110 + *
   1.111 + *
   1.112 + *
   1.113 + *
   1.114 + *
   1.115 + *
   1.116 + *
   1.117 + * */
   1.118      for (_keylist = keylist, i = 0; _keylist != NULL; _keylist = _keylist->next, i++) {
   1.119          assert(_keylist->value);
   1.120          /* TODO */