Finish porting gnupg.c using phpc

This commit is contained in:
Jakub Zelenka 2016-05-22 17:54:19 +01:00
parent 4229b54fd6
commit bc85cdf849
3 changed files with 306 additions and 309 deletions

299
gnupg.c
View file

@ -8,11 +8,10 @@
+--------------------------------------------------------------------+ +--------------------------------------------------------------------+
| Copyright (c) 2006, Thilo Raufeisen <traufeisen@php.net> | | Copyright (c) 2006, Thilo Raufeisen <traufeisen@php.net> |
| Copyright (c) 2013, Jim Jagielski <jimjag@php.net> | | Copyright (c) 2013, Jim Jagielski <jimjag@php.net> |
| Copyright (c) 2016, Jakub Zelenka <bukka@php.net> |
+--------------------------------------------------------------------+ +--------------------------------------------------------------------+
*/ */
/* $Id$ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include "config.h" #include "config.h"
#endif #endif
@ -30,6 +29,8 @@
static int le_gnupg; static int le_gnupg;
static zend_class_entry *gnupg_class_entry;
PHPC_OBJ_DEFINE_HANDLER_VAR(gnupg); PHPC_OBJ_DEFINE_HANDLER_VAR(gnupg);
/* {{{ GNUPG_GETOBJ */ /* {{{ GNUPG_GETOBJ */
@ -466,17 +467,18 @@ gpgme_error_t passphrase_decrypt_cb (
/* {{{ gnupg_fetchsignatures */ /* {{{ gnupg_fetchsignatures */
int gnupg_fetchsignatures(gpgme_signature_t gpgme_signatures, zval *main_arr) int gnupg_fetchsignatures(gpgme_signature_t gpgme_signatures, zval *main_arr)
{ {
zval sig_arr; phpc_val sig_arr;
PHPC_ARRAY_INIT(main_arr); PHPC_ARRAY_INIT(main_arr);
while (gpgme_signatures) { while (gpgme_signatures) {
PHPC_ARRAY_INIT(&sig_arr); PHPC_VAL_MAKE(sig_arr);
PHPC_ARRAY_ADD_ASSOC_CSTR(&sig_arr, "fingerprint", gpgme_signatures->fpr); PHPC_ARRAY_INIT(PHPC_VAL_CAST_TO_PZVAL(sig_arr));
PHPC_ARRAY_ADD_ASSOC_LONG(&sig_arr, "validity", gpgme_signatures->validity); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "fingerprint", gpgme_signatures->fpr);
PHPC_ARRAY_ADD_ASSOC_LONG(&sig_arr, "timestamp", gpgme_signatures->timestamp); PHPC_ARRAY_ADD_ASSOC_LONG(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "validity", gpgme_signatures->validity);
PHPC_ARRAY_ADD_ASSOC_LONG(&sig_arr, "status", gpgme_signatures->status); PHPC_ARRAY_ADD_ASSOC_LONG(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "timestamp", gpgme_signatures->timestamp);
PHPC_ARRAY_ADD_ASSOC_LONG(&sig_arr, "summary", gpgme_signatures->summary); PHPC_ARRAY_ADD_ASSOC_LONG(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "status", gpgme_signatures->status);
PHPC_ARRAY_ADD_NEXT_INDEX_ZVAL(main_arr, &sig_arr); PHPC_ARRAY_ADD_ASSOC_LONG(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "summary", gpgme_signatures->summary);
PHPC_ARRAY_ADD_NEXT_INDEX_ZVAL(main_arr, PHPC_VAL_CAST_TO_PZVAL(sig_arr));
gpgme_signatures = gpgme_signatures->next; gpgme_signatures = gpgme_signatures->next;
} }
@ -565,7 +567,7 @@ PHP_FUNCTION(gnupg_seterrormode)
*/ */
PHP_FUNCTION(gnupg_setsignmode) PHP_FUNCTION(gnupg_setsignmode)
{ {
zend_long signmode; phpc_long_t signmode;
GNUPG_GETOBJ(); GNUPG_GETOBJ();
@ -577,7 +579,7 @@ PHP_FUNCTION(gnupg_setsignmode)
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &res, &signmode) == FAILURE) { if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &res, &signmode) == FAILURE) {
return; return;
} }
PHPC_THIS = (gnupg_object *) PHPC_RES_FETCH(res, "ctx", le_gnupg); GNUPG_RES_FETCH();
} }
switch (signmode) { switch (signmode) {
case GPGME_SIG_MODE_NORMAL: case GPGME_SIG_MODE_NORMAL:
@ -853,7 +855,7 @@ PHP_FUNCTION(gnupg_clearsignkeys)
GNUPG_RES_FETCH(); GNUPG_RES_FETCH();
} }
gpgme_signers_clear (PHPC_THIS->ctx); gpgme_signers_clear(PHPC_THIS->ctx);
zend_hash_clean(PHPC_THIS->signkeys); zend_hash_clean(PHPC_THIS->signkeys);
RETURN_TRUE; RETURN_TRUE;
} }
@ -906,10 +908,8 @@ PHP_FUNCTION(gnupg_sign)
{ {
char *value = NULL; char *value = NULL;
phpc_str_size_t value_len; phpc_str_size_t value_len;
char *userret; char *userret;
phpc_str_size_t ret_size; phpc_str_size_t ret_size;
gpgme_data_t in, out; gpgme_data_t in, out;
gpgme_sign_result_t result; gpgme_sign_result_t result;
@ -926,17 +926,17 @@ PHP_FUNCTION(gnupg_sign)
GNUPG_RES_FETCH(); GNUPG_RES_FETCH();
} }
gpgme_set_passphrase_cb (PHPC_THIS->ctx, (void*) passphrase_cb, PHPC_THIS); gpgme_set_passphrase_cb(PHPC_THIS->ctx, (void *)passphrase_cb, PHPC_THIS);
if ((PHPC_THIS->err = gpgme_data_new_from_mem (&in, value, value_len, 0))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_data_new_from_mem(&in, value, value_len, 0)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("could not create in-data buffer"); GNUPG_ERR("could not create in-data buffer");
return; return;
} }
if ((PHPC_THIS->err = gpgme_data_new(&out))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_data_new(&out)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("could not create out-data buffer"); GNUPG_ERR("could not create out-data buffer");
gpgme_data_release(in); gpgme_data_release(in);
return; return;
} }
if ((PHPC_THIS->err = gpgme_op_sign(PHPC_THIS->ctx, in, out, PHPC_THIS->signmode))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_op_sign(PHPC_THIS->ctx, in, out, PHPC_THIS->signmode)) != GPG_ERR_NO_ERROR) {
if (!PHPC_THIS->errortxt) { if (!PHPC_THIS->errortxt) {
GNUPG_ERR("data signing failed"); GNUPG_ERR("data signing failed");
} }
@ -945,7 +945,7 @@ PHP_FUNCTION(gnupg_sign)
RETVAL_FALSE; RETVAL_FALSE;
return; return;
} }
result = gpgme_op_sign_result (PHPC_THIS->ctx); result = gpgme_op_sign_result(PHPC_THIS->ctx);
if (result->invalid_signers) { if (result->invalid_signers) {
GNUPG_ERR("invalid signers found"); GNUPG_ERR("invalid signers found");
gpgme_data_release(in); gpgme_data_release(in);
@ -958,14 +958,14 @@ PHP_FUNCTION(gnupg_sign)
gpgme_data_release(out); gpgme_data_release(out);
return; return;
} }
userret = gpgme_data_release_and_get_mem(out,&ret_size); userret = gpgme_data_release_and_get_mem(out, &ret_size);
if (ret_size < 1) { if (ret_size < 1) {
RETVAL_FALSE; RETVAL_FALSE;
} else { } else {
RETVAL_STRINGL (userret,ret_size); PHPC_CSTRL_RETVAL(userret, ret_size);
} }
gpgme_data_release (in); gpgme_data_release(in);
free (userret); free(userret);
} }
/* }}} */ /* }}} */
@ -980,7 +980,6 @@ PHP_FUNCTION(gnupg_encrypt)
phpc_str_size_t value_len; phpc_str_size_t value_len;
char *userret = NULL; char *userret = NULL;
phpc_str_size_t ret_size; phpc_str_size_t ret_size;
gpgme_data_t in, out; gpgme_data_t in, out;
gpgme_encrypt_result_t result; gpgme_encrypt_result_t result;
@ -1000,32 +999,32 @@ PHP_FUNCTION(gnupg_encrypt)
GNUPG_ERR("no key for encryption set"); GNUPG_ERR("no key for encryption set");
return; return;
} }
if ((PHPC_THIS->err = gpgme_data_new_from_mem (&in, value, value_len, 0))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_data_new_from_mem(&in, value, value_len, 0)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("could no create in-data buffer"); GNUPG_ERR("could no create in-data buffer");
return; return;
} }
if ((PHPC_THIS->err = gpgme_data_new(&out))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_data_new(&out)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("could not create out-data buffer"); GNUPG_ERR("could not create out-data buffer");
gpgme_data_release(in); gpgme_data_release(in);
return; return;
} }
if ((PHPC_THIS->err = gpgme_op_encrypt(PHPC_THIS->ctx, PHPC_THIS->encryptkeys, GPGME_ENCRYPT_ALWAYS_TRUST, in, out))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_op_encrypt(PHPC_THIS->ctx, PHPC_THIS->encryptkeys, GPGME_ENCRYPT_ALWAYS_TRUST, in, out)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("encrypt failed"); GNUPG_ERR("encrypt failed");
gpgme_data_release(in); gpgme_data_release(in);
gpgme_data_release(out); gpgme_data_release(out);
return; return;
} }
result = gpgme_op_encrypt_result (PHPC_THIS->ctx); result = gpgme_op_encrypt_result(PHPC_THIS->ctx);
if (result->invalid_recipients) { if (result->invalid_recipients) {
GNUPG_ERR("Invalid recipient encountered"); GNUPG_ERR("Invalid recipient encountered");
gpgme_data_release(in); gpgme_data_release(in);
gpgme_data_release(out); gpgme_data_release(out);
return; return;
} }
userret = gpgme_data_release_and_get_mem(out,&ret_size); userret = gpgme_data_release_and_get_mem(out, &ret_size);
gpgme_data_release (in); gpgme_data_release(in);
RETVAL_STRINGL (userret,ret_size); PHPC_CSTRL_RETVAL(userret, ret_size);
free (userret); free(userret);
if (ret_size < 1) { if (ret_size < 1) {
RETURN_FALSE; RETURN_FALSE;
} }
@ -1042,7 +1041,6 @@ PHP_FUNCTION(gnupg_encryptsign)
phpc_str_size_t value_len; phpc_str_size_t value_len;
char *userret = NULL; char *userret = NULL;
phpc_str_size_t ret_size; phpc_str_size_t ret_size;
gpgme_data_t in, out; gpgme_data_t in, out;
gpgme_encrypt_result_t result; gpgme_encrypt_result_t result;
gpgme_sign_result_t sign_result; gpgme_sign_result_t sign_result;
@ -1064,17 +1062,17 @@ PHP_FUNCTION(gnupg_encryptsign)
GNUPG_ERR("no key for encryption set"); GNUPG_ERR("no key for encryption set");
return; return;
} }
gpgme_set_passphrase_cb (PHPC_THIS->ctx, (void*) passphrase_cb, PHPC_THIS); gpgme_set_passphrase_cb(PHPC_THIS->ctx, (void *)passphrase_cb, PHPC_THIS);
if ((PHPC_THIS->err = gpgme_data_new_from_mem (&in, value, value_len, 0))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_data_new_from_mem (&in, value, value_len, 0)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("could not create in-data buffer"); GNUPG_ERR("could not create in-data buffer");
return; return;
} }
if ((PHPC_THIS->err = gpgme_data_new(&out))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_data_new(&out)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("could not create out-data buffer"); GNUPG_ERR("could not create out-data buffer");
gpgme_data_release(in); gpgme_data_release(in);
return; return;
} }
if ((PHPC_THIS->err = gpgme_op_encrypt_sign(PHPC_THIS->ctx, PHPC_THIS->encryptkeys, GPGME_ENCRYPT_ALWAYS_TRUST, in, out))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_op_encrypt_sign(PHPC_THIS->ctx, PHPC_THIS->encryptkeys, GPGME_ENCRYPT_ALWAYS_TRUST, in, out)) != GPG_ERR_NO_ERROR) {
if (!PHPC_THIS->errortxt) { if (!PHPC_THIS->errortxt) {
GNUPG_ERR("encrypt-sign failed"); GNUPG_ERR("encrypt-sign failed");
} }
@ -1107,8 +1105,8 @@ PHP_FUNCTION(gnupg_encryptsign)
} }
userret = gpgme_data_release_and_get_mem(out,&ret_size); userret = gpgme_data_release_and_get_mem(out,&ret_size);
gpgme_data_release (in); gpgme_data_release(in);
RETVAL_STRINGL (userret,ret_size); PHPC_CSTRL_RETVAL(userret, ret_size);
free (userret); free (userret);
if (ret_size < 1) { if (ret_size < 1) {
RETURN_FALSE; RETURN_FALSE;
@ -1123,11 +1121,12 @@ PHP_FUNCTION(gnupg_verify)
{ {
gpgme_data_t gpgme_text, gpgme_sig; gpgme_data_t gpgme_text, gpgme_sig;
gpgme_verify_result_t gpgme_result; gpgme_verify_result_t gpgme_result;
/* text without the signature, if its a detached one, or the text incl the sig */
zval *signed_text = NULL; /* text without the signature, if its a detached one, or the text incl the sig */ zval *signed_text = NULL;
zval *signature = NULL; /* signature, if its a detached one */ /* signature, if its a detached one */
zval *plain_text = NULL; /* signed_text without the signature if its not a detached sig */ zval *signature = NULL;
/* signed_text without the signature if its not a detached sig */
zval *plain_text = NULL;
char *gpg_plain; char *gpg_plain;
phpc_str_size_t gpg_plain_len; phpc_str_size_t gpg_plain_len;
@ -1145,60 +1144,60 @@ PHP_FUNCTION(gnupg_verify)
} }
if (Z_TYPE_P(signature) == IS_STRING) { /* detached signature */ if (Z_TYPE_P(signature) == IS_STRING) { /* detached signature */
/* setup signature-databuffer for gpgme */ /* setup signature-databuffer for gpgme */
if ((PHPC_THIS->err = gpgme_data_new_from_mem (&gpgme_sig, Z_STRVAL_P(signature), Z_STRLEN_P(signature), 0))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_data_new_from_mem(&gpgme_sig, Z_STRVAL_P(signature), Z_STRLEN_P(signature), 0)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR ("could not create signature-databuffer"); GNUPG_ERR("could not create signature-databuffer");
return; return;
} }
/* and the text */ /* and the text */
if ((PHPC_THIS->err = gpgme_data_new_from_mem (&gpgme_text, Z_STRVAL_P(signed_text), Z_STRLEN_P(signed_text), 0))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_data_new_from_mem(&gpgme_text, Z_STRVAL_P(signed_text), Z_STRLEN_P(signed_text), 0)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR ("could not create text-databuffer"); GNUPG_ERR("could not create text-databuffer");
gpgme_data_release (gpgme_sig); gpgme_data_release(gpgme_sig);
gpgme_data_release (gpgme_text); gpgme_data_release(gpgme_text);
return; return;
} }
/* now verify sig + text */ /* now verify sig + text */
if ((PHPC_THIS->err = gpgme_op_verify (PHPC_THIS->ctx, gpgme_sig, gpgme_text, NULL))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_op_verify (PHPC_THIS->ctx, gpgme_sig, gpgme_text, NULL)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR ("verify failed"); GNUPG_ERR("verify failed");
gpgme_data_release (gpgme_sig); gpgme_data_release(gpgme_sig);
gpgme_data_release (gpgme_text); gpgme_data_release(gpgme_text);
return; return;
} }
} else { /* clearsign or normal signature */ } else { /* clearsign or normal signature */
if ((PHPC_THIS->err = gpgme_data_new_from_mem (&gpgme_sig, Z_STRVAL_P(signed_text), Z_STRLEN_P(signed_text), 0))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_data_new_from_mem(&gpgme_sig, Z_STRVAL_P(signed_text), Z_STRLEN_P(signed_text), 0)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR ("could not create signature-databuffer"); GNUPG_ERR("could not create signature-databuffer");
return; return;
} }
/* set a NULL databuffer for gpgme */ /* set a NULL databuffer for gpgme */
if ((PHPC_THIS->err = gpgme_data_new_from_mem (&gpgme_text, NULL, 0, 0))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_data_new_from_mem (&gpgme_text, NULL, 0, 0)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR ("could not create text-databuffer"); GNUPG_ERR("could not create text-databuffer");
gpgme_data_release (gpgme_sig); gpgme_data_release(gpgme_sig);
gpgme_data_release (gpgme_text); gpgme_data_release(gpgme_text);
return; return;
} }
/* and verify the 'signature' */ /* and verify the 'signature' */
if ((PHPC_THIS->err = gpgme_op_verify (PHPC_THIS->ctx, gpgme_sig, NULL, gpgme_text))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_op_verify(PHPC_THIS->ctx, gpgme_sig, NULL, gpgme_text)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR ("verify failed"); GNUPG_ERR("verify failed");
gpgme_data_release (gpgme_sig); gpgme_data_release(gpgme_sig);
gpgme_data_release (gpgme_text); gpgme_data_release(gpgme_text);
return; return;
} }
} }
/* now get the result */ /* now get the result */
gpgme_result = gpgme_op_verify_result (PHPC_THIS->ctx); gpgme_result = gpgme_op_verify_result(PHPC_THIS->ctx);
if (!gpgme_result->signatures) { if (!gpgme_result->signatures) {
GNUPG_ERR ("no signature found"); GNUPG_ERR("no signature found");
} else { } else {
/* fetch all signatures in an array */ /* fetch all signatures in an array */
gnupg_fetchsignatures (gpgme_result->signatures, return_value); gnupg_fetchsignatures(gpgme_result->signatures, return_value);
/* get a 'plain' version of the text without a signature */ /* get a 'plain' version of the text without a signature */
gpg_plain = gpgme_data_release_and_get_mem(gpgme_text,&gpg_plain_len); gpg_plain = gpgme_data_release_and_get_mem(gpgme_text, &gpg_plain_len);
if (gpg_plain && gpg_plain_len > 0 && plain_text) { if (gpg_plain && gpg_plain_len > 0 && plain_text) {
ZVAL_DEREF (plain_text); PHPC_PZVAL_DEREF(plain_text);
ZVAL_STRINGL (plain_text, gpg_plain,gpg_plain_len); PHPC_PZVAL_CSTRL(plain_text, gpg_plain, gpg_plain_len);
} }
free (gpg_plain); free(gpg_plain);
} }
gpgme_data_release (gpgme_sig); gpgme_data_release(gpgme_sig);
} }
/* }}} */ /* }}} */
@ -1209,10 +1208,8 @@ PHP_FUNCTION(gnupg_decrypt)
{ {
char *enctxt; char *enctxt;
phpc_str_size_t enctxt_len; phpc_str_size_t enctxt_len;
char *userret; char *userret;
phpc_str_size_t ret_size; phpc_str_size_t ret_size;
gpgme_data_t in, out; gpgme_data_t in, out;
gpgme_decrypt_result_t result; gpgme_decrypt_result_t result;
@ -1229,17 +1226,17 @@ PHP_FUNCTION(gnupg_decrypt)
GNUPG_RES_FETCH(); GNUPG_RES_FETCH();
} }
gpgme_set_passphrase_cb (PHPC_THIS->ctx, (void*) passphrase_decrypt_cb, PHPC_THIS); gpgme_set_passphrase_cb(PHPC_THIS->ctx, (void *)passphrase_decrypt_cb, PHPC_THIS);
if ((PHPC_THIS->err = gpgme_data_new_from_mem (&in, enctxt, enctxt_len, 0))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_data_new_from_mem(&in, enctxt, enctxt_len, 0)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("could not create in-data buffer"); GNUPG_ERR("could not create in-data buffer");
} }
if ((PHPC_THIS->err = gpgme_data_new (&out))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_data_new(&out)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("could not create out-data buffer"); GNUPG_ERR("could not create out-data buffer");
gpgme_data_release(in); gpgme_data_release(in);
return; return;
} }
if ((PHPC_THIS->err = gpgme_op_decrypt (PHPC_THIS->ctx, in, out))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_op_decrypt(PHPC_THIS->ctx, in, out)) != GPG_ERR_NO_ERROR) {
if (!PHPC_THIS->errortxt) { if (!PHPC_THIS->errortxt) {
GNUPG_ERR("decrypt failed"); GNUPG_ERR("decrypt failed");
} }
@ -1248,17 +1245,17 @@ PHP_FUNCTION(gnupg_decrypt)
RETVAL_FALSE; RETVAL_FALSE;
return; return;
} }
result = gpgme_op_decrypt_result (PHPC_THIS->ctx); result = gpgme_op_decrypt_result(PHPC_THIS->ctx);
if (result->unsupported_algorithm) { if (result->unsupported_algorithm) {
GNUPG_ERR("unsupported algorithm"); GNUPG_ERR("unsupported algorithm");
gpgme_data_release(in); gpgme_data_release(in);
gpgme_data_release(out); gpgme_data_release(out);
return; return;
} }
userret = gpgme_data_release_and_get_mem(out,&ret_size); userret = gpgme_data_release_and_get_mem(out, &ret_size);
gpgme_data_release (in); gpgme_data_release(in);
RETVAL_STRINGL (userret,ret_size); PHPC_CSTRL_RETVAL(userret, ret_size);
free (userret); free(userret);
if (ret_size < 1) { if (ret_size < 1) {
RETVAL_FALSE; RETVAL_FALSE;
} }
@ -1273,10 +1270,8 @@ PHP_FUNCTION(gnupg_decryptverify)
char *enctxt; char *enctxt;
phpc_str_size_t enctxt_len; phpc_str_size_t enctxt_len;
zval *plaintext; zval *plaintext;
char *userret; char *userret;
phpc_str_size_t ret_size; phpc_str_size_t ret_size;
gpgme_data_t in, out; gpgme_data_t in, out;
gpgme_decrypt_result_t decrypt_result; gpgme_decrypt_result_t decrypt_result;
gpgme_verify_result_t verify_result; gpgme_verify_result_t verify_result;
@ -1293,19 +1288,19 @@ PHP_FUNCTION(gnupg_decryptverify)
} }
GNUPG_RES_FETCH(); GNUPG_RES_FETCH();
} }
ZVAL_DEREF(plaintext); PHPC_PZVAL_DEREF(plaintext);
gpgme_set_passphrase_cb (PHPC_THIS->ctx, (void*) passphrase_decrypt_cb, PHPC_THIS); gpgme_set_passphrase_cb(PHPC_THIS->ctx, (void *)passphrase_decrypt_cb, PHPC_THIS);
if ((PHPC_THIS->err = gpgme_data_new_from_mem (&in, enctxt, enctxt_len, 0))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_data_new_from_mem(&in, enctxt, enctxt_len, 0)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("could not create in-data buffer"); GNUPG_ERR("could not create in-data buffer");
} }
if ((PHPC_THIS->err = gpgme_data_new (&out))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_data_new(&out)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("could not create out-data buffer"); GNUPG_ERR("could not create out-data buffer");
gpgme_data_release(in); gpgme_data_release(in);
return; return;
} }
if ((PHPC_THIS->err = gpgme_op_decrypt_verify (PHPC_THIS->ctx, in, out))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_op_decrypt_verify(PHPC_THIS->ctx, in, out)) != GPG_ERR_NO_ERROR) {
if (!PHPC_THIS->errortxt) { if (!PHPC_THIS->errortxt) {
GNUPG_ERR("decrypt-verify failed"); GNUPG_ERR("decrypt-verify failed");
} }
@ -1314,23 +1309,23 @@ PHP_FUNCTION(gnupg_decryptverify)
RETVAL_FALSE; RETVAL_FALSE;
return; return;
} }
userret = gpgme_data_release_and_get_mem(out,&ret_size); userret = gpgme_data_release_and_get_mem(out, &ret_size);
ZVAL_STRINGL (plaintext,userret,ret_size); PHPC_PZVAL_CSTRL(plaintext, userret, ret_size);
free (userret); free(userret);
decrypt_result = gpgme_op_decrypt_result (PHPC_THIS->ctx); decrypt_result = gpgme_op_decrypt_result(PHPC_THIS->ctx);
if (decrypt_result->unsupported_algorithm) { if (decrypt_result->unsupported_algorithm) {
GNUPG_ERR ("unsupported algorithm"); GNUPG_ERR("unsupported algorithm");
gpgme_data_release(in); gpgme_data_release(in);
return; return;
} }
verify_result = gpgme_op_verify_result (PHPC_THIS->ctx); verify_result = gpgme_op_verify_result(PHPC_THIS->ctx);
if (!verify_result->signatures) { if (!verify_result->signatures) {
GNUPG_ERR ("no signature found"); GNUPG_ERR("no signature found");
gpgme_data_release(in); gpgme_data_release(in);
return; return;
} }
gnupg_fetchsignatures (verify_result->signatures, return_value); gnupg_fetchsignatures(verify_result->signatures, return_value);
gpgme_data_release (in); gpgme_data_release(in);
} }
/* }}} */ /* }}} */
@ -1343,7 +1338,6 @@ PHP_FUNCTION(gnupg_export)
phpc_str_size_t searchkey_len; phpc_str_size_t searchkey_len;
char *userret; char *userret;
phpc_str_size_t ret_size; phpc_str_size_t ret_size;
gpgme_data_t out; gpgme_data_t out;
GNUPG_GETOBJ(); GNUPG_GETOBJ();
@ -1358,19 +1352,20 @@ PHP_FUNCTION(gnupg_export)
} }
GNUPG_RES_FETCH(); GNUPG_RES_FETCH();
} }
if ((PHPC_THIS->err = gpgme_data_new (&out))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_data_new(&out)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("could not create data buffer"); GNUPG_ERR("could not create data buffer");
return; return;
} }
if ((PHPC_THIS->err = gpgme_op_export (PHPC_THIS->ctx, searchkey, 0, out))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_op_export(PHPC_THIS->ctx, searchkey, 0, out)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("export failed"); GNUPG_ERR("export failed");
gpgme_data_release(out); gpgme_data_release(out);
return; return;
} }
userret = gpgme_data_release_and_get_mem(out,&ret_size); userret = gpgme_data_release_and_get_mem(out, &ret_size);
RETVAL_STRINGL (userret,ret_size);
if (ret_size < 1) { if (ret_size < 1) {
RETVAL_FALSE; RETVAL_FALSE;
} else {
PHPC_CSTRL_RETVAL(userret, ret_size);
} }
free(userret); free(userret);
} }
@ -1378,12 +1373,11 @@ PHP_FUNCTION(gnupg_export)
/* {{{ proto array gnupg_import(string key) /* {{{ proto array gnupg_import(string key)
* imports the given key and returns a status-array * imports the given key and returns a status-array
*/ */
PHP_FUNCTION(gnupg_import) PHP_FUNCTION(gnupg_import)
{ {
char *importkey = NULL; char *importkey = NULL;
phpc_str_size_t importkey_len; phpc_str_size_t importkey_len;
gpgme_data_t in; gpgme_data_t in;
gpgme_import_result_t result; gpgme_import_result_t result;
@ -1399,7 +1393,7 @@ PHP_FUNCTION(gnupg_import)
} }
GNUPG_RES_FETCH(); GNUPG_RES_FETCH();
} }
if ((PHPC_THIS->err = gpgme_data_new_from_mem (&in, importkey, importkey_len, 0))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_data_new_from_mem(&in, importkey, importkey_len, 0)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("could not create in-data buffer"); GNUPG_ERR("could not create in-data buffer");
return; return;
} }
@ -1409,35 +1403,34 @@ PHP_FUNCTION(gnupg_import)
return; return;
} }
gpgme_data_release(in); gpgme_data_release(in);
result = gpgme_op_import_result (PHPC_THIS->ctx); result = gpgme_op_import_result(PHPC_THIS->ctx);
if (!result || !result->imports || result->imports->result != GPG_ERR_NO_ERROR) { if (!result || !result->imports || result->imports->result != GPG_ERR_NO_ERROR) {
RETURN_FALSE; RETURN_FALSE;
} }
PHPC_ARRAY_INIT (return_value); PHPC_ARRAY_INIT(return_value);
PHPC_ARRAY_ADD_ASSOC_LONG (return_value, "imported", result->imported); PHPC_ARRAY_ADD_ASSOC_LONG(return_value, "imported", result->imported);
PHPC_ARRAY_ADD_ASSOC_LONG (return_value, "unchanged", result->unchanged); PHPC_ARRAY_ADD_ASSOC_LONG(return_value, "unchanged", result->unchanged);
PHPC_ARRAY_ADD_ASSOC_LONG (return_value, "newuserids", result->new_user_ids); PHPC_ARRAY_ADD_ASSOC_LONG(return_value, "newuserids", result->new_user_ids);
PHPC_ARRAY_ADD_ASSOC_LONG (return_value, "newsubkeys", result->new_sub_keys); PHPC_ARRAY_ADD_ASSOC_LONG(return_value, "newsubkeys", result->new_sub_keys);
PHPC_ARRAY_ADD_ASSOC_LONG (return_value, "secretimported", result->secret_imported); PHPC_ARRAY_ADD_ASSOC_LONG(return_value, "secretimported", result->secret_imported);
PHPC_ARRAY_ADD_ASSOC_LONG (return_value, "secretunchanged", result->secret_unchanged); PHPC_ARRAY_ADD_ASSOC_LONG(return_value, "secretunchanged", result->secret_unchanged);
PHPC_ARRAY_ADD_ASSOC_LONG (return_value, "newsignatures", result->new_signatures); PHPC_ARRAY_ADD_ASSOC_LONG(return_value, "newsignatures", result->new_signatures);
PHPC_ARRAY_ADD_ASSOC_LONG (return_value, "skippedkeys", result->skipped_new_keys); PHPC_ARRAY_ADD_ASSOC_LONG(return_value, "skippedkeys", result->skipped_new_keys);
if (result->imports && result->imports->fpr) { if (result->imports && result->imports->fpr) {
PHPC_ARRAY_ADD_ASSOC_CSTR (return_value, "fingerprint", result->imports->fpr); PHPC_ARRAY_ADD_ASSOC_CSTR(return_value, "fingerprint", result->imports->fpr);
} }
} }
/* }}} */ /* }}} */
/* {{{ proto book gnupg_deletekey(string key) /* {{{ proto book gnupg_deletekey(string key)
* deletes a key from the keyring * deletes a key from the keyring
*/ */
PHP_FUNCTION(gnupg_deletekey) PHP_FUNCTION(gnupg_deletekey)
{ {
char *key; char *key;
phpc_str_size_t key_len; phpc_str_size_t key_len;
zend_long allow_secret = 0; phpc_long_t allow_secret = 0;
gpgme_key_t gpgme_key; gpgme_key_t gpgme_key;
GNUPG_GETOBJ(); GNUPG_GETOBJ();
@ -1457,7 +1450,7 @@ PHP_FUNCTION(gnupg_deletekey)
GNUPG_ERR("get_key failed"); GNUPG_ERR("get_key failed");
return; return;
} }
if ((PHPC_THIS->err = gpgme_op_delete(PHPC_THIS->ctx,gpgme_key,allow_secret))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_op_delete(PHPC_THIS->ctx,gpgme_key,allow_secret)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("delete failed"); GNUPG_ERR("delete failed");
RETVAL_FALSE; RETVAL_FALSE;
} else { } else {
@ -1474,8 +1467,7 @@ PHP_FUNCTION(gnupg_gettrustlist)
{ {
char *pattern; char *pattern;
phpc_str_size_t pattern_len; phpc_str_size_t pattern_len;
zval sub_arr; phpc_val sub_arr;
gpgme_trust_item_t item; gpgme_trust_item_t item;
GNUPG_GETOBJ(); GNUPG_GETOBJ();
@ -1490,22 +1482,23 @@ PHP_FUNCTION(gnupg_gettrustlist)
} }
GNUPG_RES_FETCH(); GNUPG_RES_FETCH();
} }
if ((PHPC_THIS->err = gpgme_op_trustlist_start (PHPC_THIS->ctx, pattern, 0))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_op_trustlist_start (PHPC_THIS->ctx, pattern, 0)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("could not start trustlist"); GNUPG_ERR("could not start trustlist");
return; return;
} }
PHPC_ARRAY_INIT(return_value); PHPC_ARRAY_INIT(return_value);
while (!(PHPC_THIS->err = gpgme_op_trustlist_next (PHPC_THIS->ctx, &item))) { while (!(PHPC_THIS->err = gpgme_op_trustlist_next (PHPC_THIS->ctx, &item))) {
PHPC_ARRAY_INIT (&sub_arr); PHPC_VAL_MAKE(sub_arr);
PHPC_ARRAY_INIT(PHPC_VAL_CAST_TO_PZVAL(sub_arr));
PHPC_ARRAY_ADD_ASSOC_LONG (&sub_arr, "level", item->level ); PHPC_ARRAY_ADD_ASSOC_LONG(PHPC_VAL_CAST_TO_PZVAL(sub_arr), "level", item->level);
PHPC_ARRAY_ADD_ASSOC_LONG (&sub_arr, "type", item->type ); PHPC_ARRAY_ADD_ASSOC_LONG(PHPC_VAL_CAST_TO_PZVAL(sub_arr), "type", item->type);
PHPC_ARRAY_ADD_ASSOC_CSTR (&sub_arr, "keyid", item->keyid ); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sub_arr), "keyid", item->keyid);
PHPC_ARRAY_ADD_ASSOC_CSTR (&sub_arr, "ownertrust", item->owner_trust ); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sub_arr), "ownertrust", item->owner_trust);
PHPC_ARRAY_ADD_ASSOC_CSTR (&sub_arr, "validity", item->validity ); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sub_arr), "validity", item->validity);
PHPC_ARRAY_ADD_ASSOC_CSTR (&sub_arr, "name", item->name ); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sub_arr), "name", item->name);
gpgme_trust_item_unref (item); gpgme_trust_item_unref (item);
PHPC_ARRAY_ADD_NEXT_INDEX_ZVAL (return_value, &sub_arr); PHPC_ARRAY_ADD_NEXT_INDEX_ZVAL(return_value, PHPC_VAL_CAST_TO_PZVAL(sub_arr));
} }
} }
/* }}} */ /* }}} */
@ -1516,8 +1509,8 @@ PHP_FUNCTION(gnupg_listsignatures)
char *keyid; char *keyid;
phpc_str_size_t keyid_len; phpc_str_size_t keyid_len;
zval sub_arr; phpc_val sub_arr;
zval sig_arr; phpc_val sig_arr;
gpgme_key_t gpgme_key; gpgme_key_t gpgme_key;
gpgme_user_id_t gpgme_userid; gpgme_user_id_t gpgme_userid;
@ -1535,7 +1528,7 @@ PHP_FUNCTION(gnupg_listsignatures)
} }
GNUPG_RES_FETCH(); GNUPG_RES_FETCH();
} }
if ((PHPC_THIS->err = gpgme_set_keylist_mode(PHPC_THIS->ctx,GPGME_KEYLIST_MODE_SIGS))!=GPG_ERR_NO_ERROR) { if ((PHPC_THIS->err = gpgme_set_keylist_mode(PHPC_THIS->ctx, GPGME_KEYLIST_MODE_SIGS)) != GPG_ERR_NO_ERROR) {
GNUPG_ERR("could not switch to sigmode"); GNUPG_ERR("could not switch to sigmode");
return; return;
} }
@ -1551,23 +1544,29 @@ PHP_FUNCTION(gnupg_listsignatures)
PHPC_ARRAY_INIT(return_value); PHPC_ARRAY_INIT(return_value);
gpgme_userid = gpgme_key->uids; gpgme_userid = gpgme_key->uids;
while (gpgme_userid) { while (gpgme_userid) {
PHPC_ARRAY_INIT (&sub_arr); PHPC_VAL_MAKE(sub_arr);
PHPC_ARRAY_INIT(PHPC_VAL_CAST_TO_PZVAL(sub_arr));
gpgme_signature = gpgme_userid->signatures; gpgme_signature = gpgme_userid->signatures;
while (gpgme_signature) { while (gpgme_signature) {
PHPC_ARRAY_INIT (&sig_arr); PHPC_VAL_MAKE(sig_arr);
PHPC_ARRAY_INIT(PHPC_VAL_CAST_TO_PZVAL(sig_arr));
PHPC_ARRAY_ADD_ASSOC_CSTR (&sig_arr, "uid", gpgme_signature->uid ); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "uid", gpgme_signature->uid);
PHPC_ARRAY_ADD_ASSOC_CSTR(&sig_arr, "name", gpgme_signature->name ); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "name", gpgme_signature->name);
PHPC_ARRAY_ADD_ASSOC_CSTR(&sig_arr, "email", gpgme_signature->email ); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "email", gpgme_signature->email);
PHPC_ARRAY_ADD_ASSOC_CSTR(&sig_arr, "comment", gpgme_signature->comment ); PHPC_ARRAY_ADD_ASSOC_CSTR(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "comment", gpgme_signature->comment);
PHPC_ARRAY_ADD_ASSOC_LONG (&sig_arr, "expires", gpgme_signature->expires ); PHPC_ARRAY_ADD_ASSOC_LONG(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "expires", gpgme_signature->expires);
PHPC_ARRAY_ADD_ASSOC_BOOL (&sig_arr, "revoked", gpgme_signature->revoked ); PHPC_ARRAY_ADD_ASSOC_BOOL(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "revoked", gpgme_signature->revoked);
PHPC_ARRAY_ADD_ASSOC_BOOL (&sig_arr, "expired", gpgme_signature->expired ); PHPC_ARRAY_ADD_ASSOC_BOOL(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "expired", gpgme_signature->expired);
PHPC_ARRAY_ADD_ASSOC_BOOL (&sig_arr, "invalid", gpgme_signature->invalid ); PHPC_ARRAY_ADD_ASSOC_BOOL(PHPC_VAL_CAST_TO_PZVAL(sig_arr), "invalid", gpgme_signature->invalid);
add_assoc_zval (&sub_arr, gpgme_signature->keyid, &sig_arr); PHPC_ARRAY_ADD_ASSOC_ZVAL(
PHPC_VAL_CAST_TO_PZVAL(sub_arr),
gpgme_signature->keyid,
PHPC_VAL_CAST_TO_PZVAL(sig_arr)
);
gpgme_signature = gpgme_signature->next; gpgme_signature = gpgme_signature->next;
} }
add_assoc_zval (return_value,gpgme_userid->uid, &sub_arr); PHPC_ARRAY_ADD_ASSOC_ZVAL(return_value, gpgme_userid->uid, PHPC_VAL_CAST_TO_PZVAL(sub_arr));
gpgme_userid = gpgme_userid->next; gpgme_userid = gpgme_userid->next;
} }
gpgme_key_unref(gpgme_key); gpgme_key_unref(gpgme_key);

View file

@ -46,8 +46,6 @@ PHPC_OBJ_STRUCT_BEGIN(gnupg)
HashTable *decryptkeys; HashTable *decryptkeys;
PHPC_OBJ_STRUCT_END() PHPC_OBJ_STRUCT_END()
static zend_class_entry *gnupg_class_entry;
PHP_MINIT_FUNCTION(gnupg); PHP_MINIT_FUNCTION(gnupg);
PHP_MSHUTDOWN_FUNCTION(gnupg); PHP_MSHUTDOWN_FUNCTION(gnupg);
PHP_MINFO_FUNCTION(gnupg); PHP_MINFO_FUNCTION(gnupg);

2
phpc

@ -1 +1 @@
Subproject commit 11299415b61f6c90f26410517e0c673b47efb7d8 Subproject commit d549c2c50ce74a9857337ee0e697585594f29972