-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpkcs11digestcontext.c
More file actions
130 lines (103 loc) · 3.73 KB
/
pkcs11digestcontext.c
File metadata and controls
130 lines (103 loc) · 3.73 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
+----------------------------------------------------------------------+
| PHP PKCS11 |
+----------------------------------------------------------------------+
| Copyright (c) Guillaume Amringer |
+----------------------------------------------------------------------+
| This source file is subject to the MIT license, that is bundled with |
| this package in the file LICENSE, and is available at the following |
| url: https://mit-license.org/ |
+----------------------------------------------------------------------+
| Author: Guillaume Amringer |
+----------------------------------------------------------------------+
*/
#include "pkcs11int.h"
zend_class_entry *ce_Pkcs11_DigestContext;
static zend_object_handlers pkcs11_digestcontext_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_keyUpdate, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, key, IS_OBJECT, 1)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_finalize, 0, 0, 0)
ZEND_END_ARG_INFO()
PHP_METHOD(DigestContext, update) {
CK_RV rv;
zend_string *data;
ZEND_PARSE_PARAMETERS_START(1,1)
Z_PARAM_STR(data)
ZEND_PARSE_PARAMETERS_END();
pkcs11_digestcontext_object *objval = Z_PKCS11_DIGESTCONTEXT_P(ZEND_THIS);
rv = objval->session->pkcs11->functionList->C_DigestUpdate(
objval->session->session,
ZSTR_VAL(data),
ZSTR_LEN(data)
);
if (rv != CKR_OK) {
pkcs11_error(rv, "Unable to update digest");
return;
}
}
PHP_METHOD(DigestContext, keyUpdate) {
CK_RV rv;
zval *key;
ZEND_PARSE_PARAMETERS_START(1,1)
Z_PARAM_ZVAL(key)
ZEND_PARSE_PARAMETERS_END();
pkcs11_digestcontext_object *objval = Z_PKCS11_DIGESTCONTEXT_P(ZEND_THIS);
pkcs11_key_object *keyobjval = Z_PKCS11_KEY_P(key);
rv = objval->session->pkcs11->functionList->C_DigestKey(
objval->session->session,
keyobjval->key
);
if (rv != CKR_OK) {
pkcs11_error(rv, "Unable to update digest with key");
return;
}
}
PHP_METHOD(DigestContext, finalize) {
CK_RV rv;
ZEND_PARSE_PARAMETERS_START(0,0)
ZEND_PARSE_PARAMETERS_END();
pkcs11_digestcontext_object *objval = Z_PKCS11_DIGESTCONTEXT_P(ZEND_THIS);
CK_ULONG digestLen;
rv = objval->session->pkcs11->functionList->C_DigestFinal(
objval->session->session,
NULL_PTR,
&digestLen
);
if (rv != CKR_OK) {
pkcs11_error(rv, "Unable to finalize digest");
return;
}
CK_BYTE_PTR digest = ecalloc(digestLen, sizeof(CK_BYTE));
rv = objval->session->pkcs11->functionList->C_DigestFinal(
objval->session->session,
digest,
&digestLen
);
if (rv != CKR_OK) {
pkcs11_error(rv, "Unable to finalize digest");
return;
}
zend_string *returnval;
returnval = zend_string_alloc(digestLen, 0);
memcpy(
ZSTR_VAL(returnval),
digest,
digestLen
);
efree(digest);
RETURN_STR(returnval);
}
void pkcs11_digestcontext_shutdown(pkcs11_digestcontext_object *obj) {
GC_DELREF(&obj->session->std);
}
static zend_function_entry digestcontext_class_functions[] = {
PHP_ME(DigestContext, update, arginfo_update, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(DigestContext, keyUpdate, arginfo_keyUpdate, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(DigestContext, finalize, arginfo_finalize, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_FE_END
};
DEFINE_MAGIC_FUNCS(pkcs11_digestcontext, digestcontext, DigestContext)