fixed memleak in keylistiterator

added encryptsign and decryptverify
moved gpgme-init into objects_new
This commit is contained in:
Thilo Raufeisen 2005-10-09 15:55:02 +00:00
parent f70929193a
commit 6bacc28bb6
6 changed files with 231 additions and 112 deletions

14
README
View file

@ -129,3 +129,17 @@ Methods
- string decrypt(string enctext)
decrypts the given enctext
- string encryptsign(string text)
encrypts and signs the given text with the keys, whicih are set with setencryptkey and setsignerkey
- array decryptverify(string text, string &plaintext)
verifies the given clearsigned text and returns information about the result in an array
the plaintext is passed into $plaintext
gnupg_keylistiterator
---------------------
This extension comes with an iterator for looping through the keyring
see examples/keylistiterator.php
if an argument is passed in the constructor, only keys that are matching this argument gets returned

14
examples/encryptsign.php Normal file
View file

@ -0,0 +1,14 @@
<?php
require_once (dirname(__FILE__)."/main.php");
$gnupg -> setSignerKey ($fingerprint);
$gnupg -> setEncryptKey ($fingerprint);
$gnupg -> setPassPhrase ($passphrase);
$text = $gnupg -> encryptsign ($mailtext);
echo $text;
echo "\n-------------------------\n";
$plaintext = false;
$retval = $gnupg -> decryptverify ($text,$plaintext);
print_r($retval);
print_r($plaintext);
?>

221
gnupg.c
View file

@ -116,6 +116,10 @@ zend_object_value gnupg_objects_new(zend_class_entry *class_type TSRMLS_DC){
ze_gnupg_object *intern;
zval *tmp;
zend_object_value retval;
gnupg_object *gnupg_ptr;
ze_gnupg_object *ze_obj;
gpgme_ctx_t ctx;
intern = emalloc(sizeof(ze_gnupg_object));
intern->zo.ce = class_type;
intern->zo.in_get = 0;
@ -129,6 +133,14 @@ zend_object_value gnupg_objects_new(zend_class_entry *class_type TSRMLS_DC){
retval.handle = zend_objects_store_put(intern,NULL,(zend_objects_free_object_storage_t) gnupg_object_free_storage,NULL TSRMLS_CC);
retval.handlers = (zend_object_handlers *) & gnupg_object_handlers;
gpgme_new(&ctx);
gpgme_set_armor (ctx,1);
gnupg_ptr = emalloc(sizeof(gnupg_object));
gnupg_ptr->ctx = ctx;
gnupg_ptr->encryptkey = NULL;
gnupg_ptr->signmode = GPGME_SIG_MODE_CLEAR;
intern->gnupg_ptr = gnupg_ptr;
return retval;
}
/* }}} */
@ -138,6 +150,9 @@ zend_object_value gnupg_keylistiterator_objects_new(zend_class_entry *class_type
ze_gnupg_keylistiterator_object *intern;
zval *tmp;
zend_object_value retval;
gnupg_keylistiterator_object *gnupg_keylistiterator_ptr;
gpgme_ctx_t ctx;
intern = emalloc(sizeof(ze_gnupg_keylistiterator_object));
intern->zo.ce = class_type;
intern->zo.in_get = 0;
@ -149,33 +164,19 @@ zend_object_value gnupg_keylistiterator_objects_new(zend_class_entry *class_type
zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
retval.handle = zend_objects_store_put(intern,NULL,(zend_objects_free_object_storage_t) gnupg_keylistiterator_object_free_storage,NULL TSRMLS_CC);
retval.handlers = (zend_object_handlers *) & gnupg_keylistiterator_object_handlers;
gpgme_new(&ctx);
gnupg_keylistiterator_ptr = emalloc(sizeof(gnupg_keylistiterator_object));
gnupg_keylistiterator_ptr->ctx = ctx;
intern->gnupg_keylistiterator_ptr = gnupg_keylistiterator_ptr;
return retval;
}
/* }}} */
/* {{{ resource_destructor */
void gnupg_resource_destructor(zend_rsrc_list_entry *rsrc TSRMLS_DC){
/*
if(rsrc->ptr){
printf("debug");
}
*/
}
/* }}} */
void gnupg_keylistiterator_resource_destructor(zend_rsrc_list_entry *rsrc TSRMLS_DC){
}
/* {{{ functionlist */
function_entry gnupg_functions[] = {
{NULL, NULL, NULL} /* Must be the last line in gnupg_functions[] */
};
/* }}} */
/* {{{ methodlist gnupg */
static zend_function_entry gnupg_methods[] = {
PHP_ME_MAPPING(__construct, gnupg_construct, NULL)
PHP_ME_MAPPING(keyinfo, gnupg_keyinfo, NULL)
PHP_ME_MAPPING(verify, gnupg_verify, NULL)
PHP_ME_MAPPING(getError, gnupg_geterror, NULL)
@ -190,6 +191,8 @@ static zend_function_entry gnupg_methods[] = {
PHP_ME_MAPPING(getprotocol, gnupg_getprotocol, NULL)
PHP_ME_MAPPING(setsignmode, gnupg_setsignmode, NULL)
PHP_ME_MAPPING(sign, gnupg_sign, NULL)
PHP_ME_MAPPING(encryptsign, gnupg_encryptsign, NULL)
PHP_ME_MAPPING(decryptverify, gnupg_decryptverify, NULL)
{NULL, NULL, NULL}
};
/* }}} */
@ -205,6 +208,7 @@ static zend_function_entry gnupg_keylistiterator_methods[] = {
{NULL, NULL, NULL}
};
/* }}} */
/* {{{ class constants */
static void gnupg_declare_long_constant(const char *const_name, long value TSRMLS_DC){
#if PHP_MAJOR_VERSION > 5 || PHP_MINOR_VERSION >= 1
@ -233,7 +237,7 @@ zend_module_entry gnupg_module_entry = {
STANDARD_MODULE_HEADER,
#endif
"gnupg",
gnupg_functions,
NULL,
PHP_MINIT(gnupg),
PHP_MSHUTDOWN(gnupg),
NULL, /* Replace with NULL if there's nothing to do at request start */
@ -263,16 +267,14 @@ PHP_MINIT_FUNCTION(gnupg)
ce.create_object = gnupg_objects_new;
gnupg_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
memcpy(&gnupg_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
le_gnupg = zend_register_list_destructors_ex(gnupg_resource_destructor, NULL, "ctx", module_number);
/*
zend_class_entry itce;
*/
le_gnupg = zend_register_list_destructors_ex(NULL, NULL, "ctx", module_number);
INIT_CLASS_ENTRY(ce, "gnupg_keylistiterator", gnupg_keylistiterator_methods);
ce.create_object = gnupg_keylistiterator_objects_new;
gnupg_keylistiterator_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
memcpy(&gnupg_keylistiterator_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
le_gnupg_keylistiterator = zend_register_list_destructors_ex(gnupg_keylistiterator_resource_destructor, NULL, "ctx_keylistiterator", module_number);
le_gnupg_keylistiterator = zend_register_list_destructors_ex(NULL, NULL, "ctx_keylistiterator", module_number);
zend_class_implements (gnupg_keylistiterator_class_entry TSRMLS_DC, 1, zend_ce_iterator);
@ -341,38 +343,6 @@ gpgme_error_t passphrase_cb (gnupg_object *intern, const char *uid_hint, const c
}
/* }}} */
/* {{{proto object gnupg_construct([PROTOCOL])
* constructor.
* if passed, only GPGME_PROTOCOL_OpenPGP is currently valid
*/
PHP_FUNCTION(gnupg_construct){
gnupg_object *intern;
zval *this = getThis();
ze_gnupg_object *ze_obj;
int protocol = GPGME_PROTOCOL_OpenPGP;
gpgme_ctx_t ctx;
gpgme_error_t err;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &protocol) == FAILURE){
return;
}
if(protocol != GPGME_PROTOCOL_OpenPGP){
zend_throw_exception(zend_exception_get_default(),"only OpenPGP is currently supported",1 TSRMLS_CC);
}
if((err = gpgme_new(&ctx))!=GPG_ERR_NO_ERROR){
zend_throw_exception(zend_exception_get_default(),gpg_strerror(err),1 TSRMLS_CC);
}
gpgme_set_armor (ctx,1);
ze_obj = (ze_gnupg_object*) zend_object_store_get_object(this TSRMLS_CC);
intern = emalloc(sizeof(gnupg_object));
intern->ctx = ctx;
intern->encryptkey = NULL;
intern->signmode = GPGME_SIG_MODE_CLEAR;
ze_obj->gnupg_ptr = intern;
}
/* }}} */
/* {{{ proto bool gnupg_setarmor(int armor)
* turn on/off armor mode
@ -399,7 +369,6 @@ PHP_FUNCTION(gnupg_setarmor){
}
/* }}} */
/* {{{ proto bool gnupg_setsignmode(int signmode)
* sets the mode for signing operations
*/
@ -746,13 +715,56 @@ PHP_FUNCTION(gnupg_encrypt){
}
gpgme_data_release (in);
free (out);
/*
gpgme_key_release (gpgme_key);
*/
RETURN_STRINGL (userret,ret_size,1);
}
/* }}} */
/* {{{ proto string gnupg_encrypt_sign(string text)
* encrypts and signs the given text with the keys, which weres set with setencryptkey and setsignkey before
* and returns the encrypted text
*/
PHP_FUNCTION(gnupg_encryptsign){
char *value = NULL;
int value_len;
char *userret = NULL;
int ret_size;
zval *this = getThis();
gnupg_object *intern;
gpgme_data_t in, out;
gpgme_sign_result_t sign_result;
GNUPG_FROM_OBJECT(intern, this);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &value, &value_len) == FAILURE){
return;
}
if(!intern->encryptkey){
zend_update_property_string(Z_OBJCE_P(this), this, "error", 5, "no key for encryption set" TSRMLS_DC);
RETURN_FALSE;
}
gpgme_set_passphrase_cb (intern->ctx, (void*) passphrase_cb, intern);
if((intern->err = gpgme_data_new_from_mem (&in, value, value_len, 0))!=GPG_ERR_NO_ERROR){
GNUPG_ERROR(intern,this);
}
if((intern->err = gpgme_data_new(&out))!=GPG_ERR_NO_ERROR){
GNUPG_ERROR(intern,this);
}
if((intern->err = gpgme_op_encrypt_sign(intern->ctx, &intern->encryptkey, GPGME_ENCRYPT_ALWAYS_TRUST, in, out))!=GPG_ERR_NO_ERROR){
GNUPG_ERROR(intern,this);
}
sign_result = gpgme_op_sign_result (intern->ctx);
userret = gpgme_data_release_and_get_mem(out,&ret_size);
if(ret_size < 1){
RETURN_FALSE;
}
gpgme_data_release (in);
free (out);
RETURN_STRINGL (userret,ret_size,1);
}
/* }}} */
/* {{{ proto array gnupg_verify(string text [, string &plaintext])
* verifies the given clearsigned text and returns information about the result in an array
*/
@ -852,6 +864,64 @@ PHP_FUNCTION(gnupg_decrypt){
}
/* }}} */
/* {{{ proto string gnupg_decryptverify(string enctext, string &plaintext)
* decrypts the given enctext
*/
PHP_FUNCTION(gnupg_decryptverify){
char *enctxt;
int enctxt_len;
zval *plaintext;
zval *this = getThis();
gnupg_object *intern;
char *userret;
int ret_size;
gpgme_data_t in, out;
gpgme_verify_result_t result;
gpgme_signature_t nextsig;
GNUPG_FROM_OBJECT(intern, this);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &enctxt, &enctxt_len, &plaintext) == FAILURE){
return;
}
gpgme_set_passphrase_cb (intern->ctx, (void*) passphrase_cb, intern);
if((intern->err = gpgme_data_new_from_mem (&in, enctxt, enctxt_len, 0))!=GPG_ERR_NO_ERROR){
GNUPG_ERROR(intern,this);
}
if((intern->err = gpgme_data_new (&out))!=GPG_ERR_NO_ERROR){
GNUPG_ERROR(intern,this);
}
if((intern->err = gpgme_op_decrypt_verify (intern->ctx, in, out))!=GPG_ERR_NO_ERROR){
GNUPG_ERROR(intern,this);
}
userret = gpgme_data_release_and_get_mem(out,&ret_size);
ZVAL_STRINGL (plaintext,userret,ret_size,1);
result = gpgme_op_verify_result (intern->ctx);
array_init (return_value);
add_assoc_string (return_value, "fingerprint", result->signatures->fpr, 1);
add_assoc_long (return_value, "validity", result->signatures->validity );
add_assoc_long (return_value, "timestamp", result->signatures->timestamp );
add_assoc_long (return_value, "status", result->signatures->status );
nextsig = result->signatures->next;
if(nextsig){
zend_update_property_string(Z_OBJCE_P(this), this, "error", 5, "multiple signatures found" TSRMLS_DC);
RETURN_FALSE;
}
gpgme_data_release (in);
free (out);
}
/* }}} */
/* {{{ proto string gnupg_export(string pattern)
* exports the first public key which matches against the given pattern
*/
@ -888,33 +958,22 @@ PHP_FUNCTION(gnupg_export){
PHP_FUNCTION(gnupg_keylistiterator_construct){
zval *pattern;
gnupg_keylistiterator_object *intern;
zval *this = getThis();
ze_gnupg_keylistiterator_object *ze_obj;
gpgme_ctx_t ctx;
gpgme_error_t err;
int args = ZEND_NUM_ARGS();
GNUPG_GET_ITERATOR(intern, this);
if(args > 0){
if (zend_parse_parameters(args TSRMLS_CC, "|z", &pattern) == FAILURE){
return;
}
if((err = gpgme_new(&ctx))!=GPG_ERR_NO_ERROR){
zend_throw_exception(zend_exception_get_default(),gpg_strerror(err),1 TSRMLS_CC);
}
if(args < 1){
ALLOC_INIT_ZVAL(pattern);
ZVAL_EMPTY_STRING(pattern);
}
ze_obj = (ze_gnupg_keylistiterator_object*) zend_object_store_get_object(this TSRMLS_CC);
intern = emalloc(sizeof(gnupg_keylistiterator_object));
intern->ctx = ctx;
intern->pattern = *pattern;
zval_copy_ctor(&intern->pattern);
ze_obj->gnupg_keylistiterator_ptr = intern;
}else{
convert_to_string(&intern->pattern);
}
}
PHP_FUNCTION(gnupg_keylistiterator_current){
zval *this = getThis();
@ -936,7 +995,6 @@ PHP_FUNCTION(gnupg_keylistiterator_next){
gpgme_error_t err;
GNUPG_GET_ITERATOR(intern, this);
intern->itkey++;
if(err = gpgme_op_keylist_next(intern->ctx, &intern->gpgkey)){
gpgme_key_release(intern->gpgkey);
intern->gpgkey = NULL;
@ -950,7 +1008,6 @@ PHP_FUNCTION(gnupg_keylistiterator_rewind){
gpgme_error_t err;
GNUPG_GET_ITERATOR(intern, this);
intern->itkey = 0;
if((err = gpgme_op_keylist_start(intern->ctx, Z_STRVAL(intern->pattern), 0)) != GPG_ERR_NO_ERROR){
zend_throw_exception(zend_exception_get_default(),gpg_strerror(err),1 TSRMLS_CC);
}

View file

@ -15,11 +15,11 @@ So you can sign, encrypt, verify directly from php
</maintainer>
</maintainers>
<release>
<version>0.1</version>
<date>2005-10-07</date>
<version>0.2</version>
<date>2005-10-09</date>
<license>PHP License</license>
<state>beta</state>
<notes>First release and not feature complete. Don&apos;t use in production enviroments
<notes>added keylistiterator, encryptsign and decryptverify
</notes>
<deps>
<dep type="php" rel="ge" version="5.0"/>
@ -37,9 +37,16 @@ So you can sign, encrypt, verify directly from php
<file role="doc" name="examples/encrypt.php"/>
<file role="doc" name="examples/decrypt.php"/>
<file role="doc" name="examples/keyinfo.php"/>
<file role="doc" name="examples/encryptsign.php" />
</filelist>
</release>
<changelog>
<release>
<version>0.2</version>
<date>2005-10-09</date>
<state>beta</state>
<notes>added keylistiterator, encryptsign and decryptverify</notes>
</release>
<release>
<version>0.1</version>
<date>2005-10-07</date>

View file

@ -1,43 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<package packagerversion="1.4.1" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
<package packagerversion="1.4.1" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0
http://pear.php.net/dtd/tasks-1.0.xsd
http://pear.php.net/dtd/package-2.0
http://pear.php.net/dtd/package-2.0.xsd">
<name>gnupg</name>
<channel>pecl.php.net</channel>
<summary>wrapper around the gpgme library</summary>
<description>This extension provides methods to interact with gnupg.
So you can sign, encrypt, verify directly from php</description>
So you can sign, encrypt, verify directly from php
</description>
<lead>
<name>Thilo Raufeisen</name>
<user>traufeisen</user>
<email>traufeisen@php.net</email>
<active>yes</active>
</lead>
<date>2005-10-07</date>
<time>20:17:04</time>
<date>2005-10-09</date>
<time>17:39:05</time>
<version>
<release>0.1</release>
<api>0.1</api>
<release>0.2</release>
<api>0.2</api>
</version>
<stability>
<release>beta</release>
<api>beta</api>
</stability>
<license uri="http://www.php.net/license">PHP License</license>
<notes>First release and not feature complete. Don&apos;t use in production enviroments</notes>
<notes>added keylistiterator, encryptsign and decryptverify
</notes>
<contents>
<dir name="/">
<file md5sum="1f2eefcaf44c8a7533b2ad19c69ede0e" name="examples/clearsign.php" role="doc" />
<file md5sum="9ccddfd2c22ccec0fe8b13fd41ccb252" name="examples/decrypt.php" role="doc" />
<file md5sum="15c3488ad344ed264ef123d31d49012f" name="examples/encrypt.php" role="doc" />
<file md5sum="ba7c637ebaeb12e61d13f2678171cee1" name="examples/keyinfo.php" role="doc" />
<file md5sum="7a06adc6cf3139a79a5dfb840d876af1" name="examples/main.php" role="doc" />
<file md5sum="d7e45084bed6b4c8c1526056b4e78140" name="examples/verify.php" role="doc" />
<file md5sum="180bd17c5bd37dd69bcfe568e7722c02" name="config.m4" role="src" />
<file md5sum="d41d8cd98f00b204e9800998ecf8427e" name="EXPERIMENTAL" role="doc" />
<file md5sum="39ae229a064d5fe1fa27320ca708d0ec" name="gnupg.c" role="src" />
<file md5sum="b4fae3bcb2e182f6758f29542342d332" name="LICENSE" role="doc" />
<file md5sum="4d27dece931fbfc08519ca0631845125" name="php_gnupg.h" role="src" />
<file md5sum="9f78dcbdab946661590d9ff510bfb0ee" name="README" role="doc" />
</dir>
<dir name="examples">
<file name="clearsign.php" role="doc" />
<file name="decrypt.php" role="doc" />
<file name="encrypt.php" role="doc" />
<file name="encryptsign.php" role="doc" />
<file name="keyinfo.php" role="doc" />
<file name="main.php" role="doc" />
<file name="verify.php" role="doc" />
</dir> <!-- //examples -->
<file name="config.m4" role="src" />
<file name="EXPERIMENTAL" role="doc" />
<file name="gnupg.c" role="src" />
<file name="LICENSE" role="doc" />
<file name="php_gnupg.h" role="src" />
<file name="README" role="doc" />
</dir> <!-- / -->
</contents>
<dependencies>
<required>
@ -52,6 +62,20 @@ So you can sign, encrypt, verify directly from php</description>
<providesextension>gnupg</providesextension>
<extsrcrelease />
<changelog>
<release>
<version>
<release>0.2</release>
<api>0.2</api>
</version>
<stability>
<release>beta</release>
<api>beta</api>
</stability>
<date>2005-10-09</date>
<license uri="http://www.php.net/license">PHP License</license>
<notes>added keylistiterator, encryptsign and decryptverify
</notes>
</release>
<release>
<version>
<release>0.1</release>
@ -63,7 +87,9 @@ So you can sign, encrypt, verify directly from php</description>
</stability>
<date>2005-10-07</date>
<license uri="http://www.php.net/license">PHP License</license>
<notes>initial release</notes>
<notes>initial release
</notes>
</release>
</changelog>
</package>

View file

@ -54,7 +54,6 @@ typedef struct _gnupg_keylistiterator_object{
gpgme_ctx_t ctx;
gpgme_error_t err;
gpgme_key_t gpgkey;
int itkey;
zval pattern;
} gnupg_keylistiterator_object;
@ -83,7 +82,9 @@ PHP_FUNCTION(gnupg_sign);
PHP_FUNCTION(gnupg_clearsignerkey);
PHP_FUNCTION(gnupg_getprotocol);
PHP_FUNCTION(gnupg_encrypt);
PHP_FUNCTION(gnupg_encryptsign);
PHP_FUNCTION(gnupg_decrypt);
PHP_FUNCTION(gnupg_decryptverify);
PHP_FUNCTION(gnupg_export);
PHP_FUNCTION(gnupg_keylistiterator_construct);