Skip to content

Commit

Permalink
Merge pull request #58 from gamringer/revise_errors
Browse files Browse the repository at this point in the history
Revise errors
  • Loading branch information
gamringer authored Mar 23, 2021
2 parents ed6a096 + cbb42b9 commit fde9a54
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
10 changes: 8 additions & 2 deletions pkcs11.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@
#include "pkcs11int.h"

static const char * strCK_RV(const CK_RV rv);
static zend_class_entry *zend_pkcs11_exception_ce;

void general_error(char *generic, char *specific) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%s: %s", generic, specific);
zend_throw_exception(zend_ce_exception, buf, 0);
zend_throw_exception(zend_pkcs11_exception_ce, buf, 0);
}

void pkcs11_error(CK_RV rv, char *error) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "(0x%08lx/%s) PKCS#11 module error: %s",
rv, strCK_RV(rv), error);
zend_throw_exception(zend_ce_exception, buf, 0);
zend_throw_exception(zend_pkcs11_exception_ce, buf, rv);
}

static const char * strCK_RV(const CK_RV rv) {
Expand Down Expand Up @@ -231,6 +232,11 @@ PHP_MINIT_FUNCTION(pkcs11)
register_pkcs11_encryptioncontext();
register_pkcs11_decryptioncontext();

zend_class_entry ce;

INIT_NS_CLASS_ENTRY(ce, "Pkcs11", "Exception", NULL);
zend_pkcs11_exception_ce = zend_register_internal_class_ex(&ce, zend_ce_exception);

# define REGISTER_PKCS11_CONSTANT(n) REGISTER_NS_LONG_CONSTANT("Pkcs11", #n, n, CONST_CS | CONST_PERSISTENT)

REGISTER_PKCS11_CONSTANT(CKM_RSA_PKCS_KEY_PAIR_GEN);
Expand Down
2 changes: 1 addition & 1 deletion pkcs11module.c
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ PHP_METHOD(Module, waitForSlotEvent) {
RETURN_NULL();
}

zend_throw_exception(zend_ce_exception, "Error waiting for events", 0);
pkcs11_error(rv, "Error waiting for events");
}

/*
Expand Down
8 changes: 6 additions & 2 deletions tests/0117-waitForSlotEvent.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ if (trim($info["manufacturerID"]) == 'SoftHSM'

$modulePath = getenv('PHP11_MODULE');
$module = new Pkcs11\Module($modulePath);
$slotId = $module->waitForSlotEvent(Pkcs11\CKF_DONT_BLOCK);
var_dump(is_int($slotId) || $slotId === null);
try {
$slotId = $module->waitForSlotEvent(Pkcs11\CKF_DONT_BLOCK);
var_dump(is_int($slotId) || $slotId === null);
} catch (\Pkcs11\Exception $e) {
var_dump($e->getCode() == \Pkcs11\CKR_FUNCTION_NOT_SUPPORTED);
}

?>
--EXPECTF--
Expand Down

0 comments on commit fde9a54

Please sign in to comment.