-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpkcs11encryptioncontext.c
More file actions
130 lines (106 loc) · 3.8 KB
/
pkcs11encryptioncontext.c
File metadata and controls
130 lines (106 loc) · 3.8 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_EncryptionContext;
static zend_object_handlers pkcs11_encryptioncontext_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(EncryptionContext, update) {
CK_RV rv;
zend_string *data;
ZEND_PARSE_PARAMETERS_START(1,1)
Z_PARAM_STR(data)
ZEND_PARSE_PARAMETERS_END();
pkcs11_encryptioncontext_object *objval = Z_PKCS11_ENCRYPTIONCONTEXT_P(ZEND_THIS);
CK_ULONG ciphertextLen;
rv = objval->key->session->pkcs11->functionList->C_EncryptUpdate(
objval->key->session->session,
ZSTR_VAL(data),
ZSTR_LEN(data),
NULL_PTR ,
&ciphertextLen
);
if (rv != CKR_OK) {
pkcs11_error(rv, "Unable to update encryption");
return;
}
CK_BYTE_PTR ciphertext = ecalloc(ciphertextLen, sizeof(CK_BYTE));
rv = objval->key->session->pkcs11->functionList->C_EncryptUpdate(
objval->key->session->session,
ZSTR_VAL(data),
ZSTR_LEN(data),
ciphertext,
&ciphertextLen
);
if (rv != CKR_OK) {
pkcs11_error(rv, "Unable to update encryption");
return;
}
zend_string *returnval;
returnval = zend_string_alloc(ciphertextLen, 0);
memcpy(
ZSTR_VAL(returnval),
ciphertext,
ciphertextLen
);
efree(ciphertext);
RETURN_STR(returnval);
}
PHP_METHOD(EncryptionContext, finalize) {
CK_RV rv;
ZEND_PARSE_PARAMETERS_START(0,0)
ZEND_PARSE_PARAMETERS_END();
pkcs11_encryptioncontext_object *objval = Z_PKCS11_ENCRYPTIONCONTEXT_P(ZEND_THIS);
CK_ULONG ciphertextLen;
rv = objval->key->session->pkcs11->functionList->C_EncryptFinal(
objval->key->session->session,
NULL_PTR ,
&ciphertextLen
);
if (rv != CKR_OK) {
pkcs11_error(rv, "Unable to finalize encryption");
return;
}
CK_BYTE_PTR ciphertext = ecalloc(ciphertextLen, sizeof(CK_BYTE));
rv = objval->key->session->pkcs11->functionList->C_EncryptFinal(
objval->key->session->session,
ciphertext,
&ciphertextLen
);
if (rv != CKR_OK) {
pkcs11_error(rv, "Unable to finalize encryption");
return;
}
zend_string *returnval;
returnval = zend_string_alloc(ciphertextLen, 0);
memcpy(
ZSTR_VAL(returnval),
ciphertext,
ciphertextLen
);
efree(ciphertext);
RETURN_STR(returnval);
}
void pkcs11_encryptioncontext_shutdown(pkcs11_encryptioncontext_object *obj) {
GC_DELREF(&obj->key->std);
}
static zend_function_entry encryptioncontext_class_functions[] = {
PHP_ME(EncryptionContext, update, arginfo_update, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(EncryptionContext, finalize, arginfo_finalize, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_FE_END
};
DEFINE_MAGIC_FUNCS(pkcs11_encryptioncontext, encryptioncontext, EncryptionContext)