-
Notifications
You must be signed in to change notification settings - Fork 13
/
pkcs11signaturecontext.c
107 lines (86 loc) · 3.4 KB
/
pkcs11signaturecontext.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Guillaume Amringer |
+----------------------------------------------------------------------+
*/
#include "pkcs11int.h"
zend_class_entry *ce_Pkcs11_SignatureContext;
static zend_object_handlers pkcs11_signaturecontext_handlers;
ZEND_BEGIN_ARG_INFO_EX(arginfo_update, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 1)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_finalize, 0, 0, 0)
ZEND_END_ARG_INFO()
PHP_METHOD(SignatureContext, update) {
CK_RV rv;
zend_string *data;
ZEND_PARSE_PARAMETERS_START(1,1)
Z_PARAM_STR(data)
ZEND_PARSE_PARAMETERS_END();
pkcs11_signaturecontext_object *objval = Z_PKCS11_SIGNATURECONTEXT_P(ZEND_THIS);
rv = objval->key->session->pkcs11->functionList->C_SignUpdate(
objval->key->session->session,
ZSTR_VAL(data),
ZSTR_LEN(data)
);
if (rv != CKR_OK) {
pkcs11_error(rv, "Unable to update signature");
return;
}
}
PHP_METHOD(SignatureContext, finalize) {
CK_RV rv;
ZEND_PARSE_PARAMETERS_START(0,0)
ZEND_PARSE_PARAMETERS_END();
pkcs11_signaturecontext_object *objval = Z_PKCS11_SIGNATURECONTEXT_P(ZEND_THIS);
CK_ULONG signatureLen;
rv = objval->key->session->pkcs11->functionList->C_SignFinal(
objval->key->session->session,
NULL_PTR,
&signatureLen
);
if (rv != CKR_OK) {
pkcs11_error(rv, "Unable to finalize signature");
return;
}
CK_BYTE_PTR signature = ecalloc(signatureLen, sizeof(CK_BYTE));
rv = objval->key->session->pkcs11->functionList->C_SignFinal(
objval->key->session->session,
signature,
&signatureLen
);
if (rv != CKR_OK) {
pkcs11_error(rv, "Unable to finalize signature");
return;
}
zend_string *returnval;
returnval = zend_string_alloc(signatureLen, 0);
memcpy(
ZSTR_VAL(returnval),
signature,
signatureLen
);
efree(signature);
RETURN_STR(returnval);
}
void pkcs11_signaturecontext_shutdown(pkcs11_signaturecontext_object *obj) {
GC_DELREF(&obj->key->std);
}
static zend_function_entry signaturecontext_class_functions[] = {
PHP_ME(SignatureContext, update, arginfo_update, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(SignatureContext, finalize, arginfo_finalize, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_FE_END
};
DEFINE_MAGIC_FUNCS(pkcs11_signaturecontext, signaturecontext, SignatureContext)